Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ZLibNative.cs
Go to the documentation of this file.
2
4
5internal static class ZLibNative
6{
7 public enum FlushCode
8 {
9 NoFlush = 0,
10 SyncFlush = 2,
11 Finish = 4,
12 Block = 5
13 }
14
15 public enum ErrorCode
16 {
17 Ok = 0,
18 StreamEnd = 1,
19 StreamError = -2,
20 DataError = -3,
21 MemError = -4,
22 BufError = -5,
23 VersionError = -6
24 }
25
26 public enum CompressionLevel
27 {
28 NoCompression = 0,
29 BestSpeed = 1,
32 }
33
35 {
37 }
38
40 {
41 Deflated = 8
42 }
43
44 public sealed class ZLibStreamHandle : SafeHandle
45 {
53
55
56 private volatile State _initializationState;
57
58 public override bool IsInvalid => handle == new IntPtr(-1);
59
61
63 {
64 get
65 {
66 return _zStream.nextIn;
67 }
68 set
69 {
70 _zStream.nextIn = value;
71 }
72 }
73
74 public uint AvailIn
75 {
76 get
77 {
78 return _zStream.availIn;
79 }
80 set
81 {
82 _zStream.availIn = value;
83 }
84 }
85
87 {
88 set
89 {
90 _zStream.nextOut = value;
91 }
92 }
93
94 public uint AvailOut
95 {
96 get
97 {
98 return _zStream.availOut;
99 }
100 set
101 {
102 _zStream.availOut = value;
103 }
104 }
105
107 : base(new IntPtr(-1), ownsHandle: true)
108 {
109 _zStream.Init();
110 _initializationState = State.NotInitialized;
112 }
113
114 protected override bool ReleaseHandle()
115 {
116 return InitializationState switch
117 {
118 State.NotInitialized => true,
119 State.InitializedForDeflate => DeflateEnd() == ErrorCode.Ok,
120 State.InitializedForInflate => InflateEnd() == ErrorCode.Ok,
121 State.Disposed => true,
122 _ => false,
123 };
124 }
125
126 private void EnsureNotDisposed()
127 {
128 if (InitializationState == State.Disposed)
129 {
130 throw new ObjectDisposedException(GetType().ToString());
131 }
132 }
133
134 private void EnsureState(State requiredState)
135 {
136 if (InitializationState != requiredState)
137 {
138 throw new InvalidOperationException("InitializationState != " + requiredState);
139 }
140 }
141
142 public unsafe ErrorCode DeflateInit2_(CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
143 {
145 EnsureState(State.NotInitialized);
146 fixed (ZStream* stream = &_zStream)
147 {
148 ErrorCode result = global::Interop.zlib.DeflateInit2_(stream, level, CompressionMethod.Deflated, windowBits, memLevel, strategy);
149 _initializationState = State.InitializedForDeflate;
150 return result;
151 }
152 }
153
154 public unsafe ErrorCode Deflate(FlushCode flush)
155 {
157 EnsureState(State.InitializedForDeflate);
158 fixed (ZStream* stream = &_zStream)
159 {
160 return global::Interop.zlib.Deflate(stream, flush);
161 }
162 }
163
164 public unsafe ErrorCode DeflateEnd()
165 {
167 EnsureState(State.InitializedForDeflate);
168 fixed (ZStream* stream = &_zStream)
169 {
170 ErrorCode result = global::Interop.zlib.DeflateEnd(stream);
171 _initializationState = State.Disposed;
172 return result;
173 }
174 }
175
176 public unsafe ErrorCode InflateInit2_(int windowBits)
177 {
179 EnsureState(State.NotInitialized);
180 fixed (ZStream* stream = &_zStream)
181 {
182 ErrorCode result = global::Interop.zlib.InflateInit2_(stream, windowBits);
183 _initializationState = State.InitializedForInflate;
184 return result;
185 }
186 }
187
188 public unsafe ErrorCode Inflate(FlushCode flush)
189 {
191 EnsureState(State.InitializedForInflate);
192 fixed (ZStream* stream = &_zStream)
193 {
194 return global::Interop.zlib.Inflate(stream, flush);
195 }
196 }
197
198 public unsafe ErrorCode InflateEnd()
199 {
201 EnsureState(State.InitializedForInflate);
202 fixed (ZStream* stream = &_zStream)
203 {
204 ErrorCode result = global::Interop.zlib.InflateEnd(stream);
205 _initializationState = State.Disposed;
206 return result;
207 }
208 }
209
210 public string GetErrorMessage()
211 {
212 if (!(_zStream.msg != ZNullPtr))
213 {
214 return string.Empty;
215 }
217 }
218 }
219
220 internal struct ZStream
221 {
222 internal IntPtr nextIn;
223
224 internal IntPtr nextOut;
225
226 internal IntPtr msg;
227
228 private readonly IntPtr internalState;
229
230 internal uint availIn;
231
232 internal uint availOut;
233
234 internal void Init()
235 {
236 }
237 }
238
239 internal static readonly IntPtr ZNullPtr = IntPtr.Zero;
240
241 public static ErrorCode CreateZLibStreamForDeflate(out ZLibStreamHandle zLibStreamHandle, CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
242 {
243 zLibStreamHandle = new ZLibStreamHandle();
244 return zLibStreamHandle.DeflateInit2_(level, windowBits, memLevel, strategy);
245 }
246
247 public static ErrorCode CreateZLibStreamForInflate(out ZLibStreamHandle zLibStreamHandle, int windowBits)
248 {
249 zLibStreamHandle = new ZLibStreamHandle();
250 return zLibStreamHandle.InflateInit2_(windowBits);
251 }
252}
unsafe ErrorCode DeflateInit2_(CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
unsafe ErrorCode Inflate(FlushCode flush)
unsafe ErrorCode InflateInit2_(int windowBits)
unsafe ErrorCode Deflate(FlushCode flush)
static ErrorCode CreateZLibStreamForInflate(out ZLibStreamHandle zLibStreamHandle, int windowBits)
static readonly IntPtr ZNullPtr
static ErrorCode CreateZLibStreamForDeflate(out ZLibStreamHandle zLibStreamHandle, CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
static unsafe? string PtrToStringAnsi(IntPtr ptr)
Definition Marshal.cs:630
static readonly IntPtr Zero
Definition IntPtr.cs:18