Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Deflater.cs
Go to the documentation of this file.
2
4
5internal sealed class Deflater : IDisposable
6{
8
10
11 private bool _isDisposed;
12
13 private object SyncLock => this;
14
15 internal Deflater(CompressionLevel compressionLevel, int windowBits)
16 {
18 int memLevel;
19 switch (compressionLevel)
20 {
21 case CompressionLevel.Optimal:
22 level = ZLibNative.CompressionLevel.DefaultCompression;
23 memLevel = 8;
24 break;
25 case CompressionLevel.Fastest:
26 level = ZLibNative.CompressionLevel.BestSpeed;
27 memLevel = 8;
28 break;
29 case CompressionLevel.NoCompression:
30 level = ZLibNative.CompressionLevel.NoCompression;
31 memLevel = 7;
32 break;
33 case CompressionLevel.SmallestSize:
34 level = ZLibNative.CompressionLevel.BestCompression;
35 memLevel = 8;
36 break;
37 default:
38 throw new ArgumentOutOfRangeException("compressionLevel");
39 }
41 ZLibNative.ErrorCode errorCode;
42 try
43 {
44 errorCode = ZLibNative.CreateZLibStreamForDeflate(out _zlibStream, level, windowBits, memLevel, strategy);
45 }
46 catch (Exception innerException)
47 {
48 throw new ZLibException(System.SR.ZLibErrorDLLLoadError, innerException);
49 }
50 switch (errorCode)
51 {
52 case ZLibNative.ErrorCode.Ok:
53 break;
54 case ZLibNative.ErrorCode.MemError:
55 throw new ZLibException(System.SR.ZLibErrorNotEnoughMemory, "deflateInit2_", (int)errorCode, _zlibStream.GetErrorMessage());
56 case ZLibNative.ErrorCode.VersionError:
57 throw new ZLibException(System.SR.ZLibErrorVersionMismatch, "deflateInit2_", (int)errorCode, _zlibStream.GetErrorMessage());
58 case ZLibNative.ErrorCode.StreamError:
59 throw new ZLibException(System.SR.ZLibErrorIncorrectInitParameters, "deflateInit2_", (int)errorCode, _zlibStream.GetErrorMessage());
60 default:
61 throw new ZLibException(System.SR.ZLibErrorUnexpected, "deflateInit2_", (int)errorCode, _zlibStream.GetErrorMessage());
62 }
63 }
64
66 {
67 Dispose(disposing: false);
68 }
69
70 public void Dispose()
71 {
72 Dispose(disposing: true);
73 GC.SuppressFinalize(this);
74 }
75
76 private void Dispose(bool disposing)
77 {
78 if (!_isDisposed)
79 {
80 if (disposing)
81 {
82 _zlibStream.Dispose();
83 }
85 _isDisposed = true;
86 }
87 }
88
89 public bool NeedsInput()
90 {
91 return _zlibStream.AvailIn == 0;
92 }
93
94 internal unsafe void SetInput(ReadOnlyMemory<byte> inputBuffer)
95 {
96 if (inputBuffer.Length == 0)
97 {
98 return;
99 }
100 lock (SyncLock)
101 {
102 _inputBufferHandle = inputBuffer.Pin();
103 _zlibStream.NextIn = (IntPtr)_inputBufferHandle.Pointer;
104 _zlibStream.AvailIn = (uint)inputBuffer.Length;
105 }
106 }
107
108 internal unsafe void SetInput(byte* inputBufferPtr, int count)
109 {
110 if (count == 0)
111 {
112 return;
113 }
114 lock (SyncLock)
115 {
116 _zlibStream.NextIn = (IntPtr)inputBufferPtr;
117 _zlibStream.AvailIn = (uint)count;
118 }
119 }
120
121 internal int GetDeflateOutput(byte[] outputBuffer)
122 {
123 try
124 {
125 ReadDeflateOutput(outputBuffer, ZLibNative.FlushCode.NoFlush, out var bytesRead);
126 return bytesRead;
127 }
128 finally
129 {
130 if (_zlibStream.AvailIn == 0)
131 {
133 }
134 }
135 }
136
137 private unsafe ZLibNative.ErrorCode ReadDeflateOutput(byte[] outputBuffer, ZLibNative.FlushCode flushCode, out int bytesRead)
138 {
139 lock (SyncLock)
140 {
141 fixed (byte* ptr = &outputBuffer[0])
142 {
143 _zlibStream.NextOut = (IntPtr)ptr;
144 _zlibStream.AvailOut = (uint)outputBuffer.Length;
145 ZLibNative.ErrorCode result = Deflate(flushCode);
146 bytesRead = outputBuffer.Length - (int)_zlibStream.AvailOut;
147 return result;
148 }
149 }
150 }
151
152 internal bool Finish(byte[] outputBuffer, out int bytesRead)
153 {
154 ZLibNative.ErrorCode errorCode = ReadDeflateOutput(outputBuffer, ZLibNative.FlushCode.Finish, out bytesRead);
155 return errorCode == ZLibNative.ErrorCode.StreamEnd;
156 }
157
158 internal bool Flush(byte[] outputBuffer, out int bytesRead)
159 {
160 return ReadDeflateOutput(outputBuffer, ZLibNative.FlushCode.SyncFlush, out bytesRead) == ZLibNative.ErrorCode.Ok;
161 }
162
164 {
165 lock (SyncLock)
166 {
167 _zlibStream.AvailIn = 0u;
168 _zlibStream.NextIn = ZLibNative.ZNullPtr;
170 }
171 }
172
174 {
175 ZLibNative.ErrorCode errorCode;
176 try
177 {
178 errorCode = _zlibStream.Deflate(flushCode);
179 }
180 catch (Exception innerException)
181 {
182 throw new ZLibException(System.SR.ZLibErrorDLLLoadError, innerException);
183 }
184 switch (errorCode)
185 {
186 case ZLibNative.ErrorCode.Ok:
187 case ZLibNative.ErrorCode.StreamEnd:
188 return errorCode;
189 case ZLibNative.ErrorCode.BufError:
190 return errorCode;
191 case ZLibNative.ErrorCode.StreamError:
192 throw new ZLibException(System.SR.ZLibErrorInconsistentStream, "deflate", (int)errorCode, _zlibStream.GetErrorMessage());
193 default:
194 throw new ZLibException(System.SR.ZLibErrorUnexpected, "deflate", (int)errorCode, _zlibStream.GetErrorMessage());
195 }
196 }
197}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
bool Finish(byte[] outputBuffer, out int bytesRead)
Definition Deflater.cs:152
Deflater(CompressionLevel compressionLevel, int windowBits)
Definition Deflater.cs:15
ZLibNative.ErrorCode Deflate(ZLibNative.FlushCode flushCode)
Definition Deflater.cs:173
void Dispose(bool disposing)
Definition Deflater.cs:76
readonly ZLibNative.ZLibStreamHandle _zlibStream
Definition Deflater.cs:7
int GetDeflateOutput(byte[] outputBuffer)
Definition Deflater.cs:121
MemoryHandle _inputBufferHandle
Definition Deflater.cs:9
unsafe ZLibNative.ErrorCode ReadDeflateOutput(byte[] outputBuffer, ZLibNative.FlushCode flushCode, out int bytesRead)
Definition Deflater.cs:137
unsafe void SetInput(ReadOnlyMemory< byte > inputBuffer)
Definition Deflater.cs:94
unsafe void SetInput(byte *inputBufferPtr, int count)
Definition Deflater.cs:108
bool Flush(byte[] outputBuffer, out int bytesRead)
Definition Deflater.cs:158
static readonly IntPtr ZNullPtr
static ErrorCode CreateZLibStreamForDeflate(out ZLibStreamHandle zLibStreamHandle, CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
static string ZLibErrorNotEnoughMemory
Definition SR.cs:46
static string ZLibErrorVersionMismatch
Definition SR.cs:48
static string ZLibErrorDLLLoadError
Definition SR.cs:40
static string ZLibErrorInconsistentStream
Definition SR.cs:42
static string ZLibErrorUnexpected
Definition SR.cs:50
static string ZLibErrorIncorrectInitParameters
Definition SR.cs:44
Definition SR.cs:7
unsafe MemoryHandle Pin()