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
54 private ZStream _zStream;
55
56 private volatile State _initializationState;
57
58 public override bool IsInvalid => handle == new IntPtr(-1);
59
61
62 public IntPtr NextIn
63 {
64 set
65 {
66 _zStream.nextIn = value;
67 }
68 }
69
70 public uint AvailIn
71 {
72 get
73 {
74 return _zStream.availIn;
75 }
76 set
77 {
78 _zStream.availIn = value;
79 }
80 }
81
82 public IntPtr NextOut
83 {
84 set
85 {
86 _zStream.nextOut = value;
87 }
88 }
89
90 public uint AvailOut
91 {
92 get
93 {
94 return _zStream.availOut;
95 }
96 set
97 {
98 _zStream.availOut = value;
99 }
100 }
101
103 : base(new IntPtr(-1), ownsHandle: true)
104 {
105 _zStream.Init();
106 _initializationState = State.NotInitialized;
108 }
109
110 protected override bool ReleaseHandle()
111 {
112 return InitializationState switch
113 {
114 State.NotInitialized => true,
115 State.InitializedForDeflate => DeflateEnd() == ErrorCode.Ok,
116 State.InitializedForInflate => InflateEnd() == ErrorCode.Ok,
117 State.Disposed => true,
118 _ => false,
119 };
120 }
121
122 private void EnsureNotDisposed()
123 {
124 if (InitializationState == State.Disposed)
125 {
126 throw new ObjectDisposedException(GetType().ToString());
127 }
128 }
129
130 private void EnsureState(State requiredState)
131 {
132 if (InitializationState != requiredState)
133 {
134 throw new InvalidOperationException("InitializationState != " + requiredState);
135 }
136 }
137
138 public unsafe ErrorCode DeflateInit2_(CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
139 {
141 EnsureState(State.NotInitialized);
142 fixed (ZStream* stream = &_zStream)
143 {
144 ErrorCode result = global::Interop.zlib.DeflateInit2_(stream, level, CompressionMethod.Deflated, windowBits, memLevel, strategy);
145 _initializationState = State.InitializedForDeflate;
146 return result;
147 }
148 }
149
150 public unsafe ErrorCode Deflate(FlushCode flush)
151 {
153 EnsureState(State.InitializedForDeflate);
154 fixed (ZStream* stream = &_zStream)
155 {
156 return global::Interop.zlib.Deflate(stream, flush);
157 }
158 }
159
160 public unsafe ErrorCode DeflateEnd()
161 {
163 EnsureState(State.InitializedForDeflate);
164 fixed (ZStream* stream = &_zStream)
165 {
166 ErrorCode result = global::Interop.zlib.DeflateEnd(stream);
167 _initializationState = State.Disposed;
168 return result;
169 }
170 }
171
172 public unsafe ErrorCode InflateInit2_(int windowBits)
173 {
175 EnsureState(State.NotInitialized);
176 fixed (ZStream* stream = &_zStream)
177 {
178 ErrorCode result = global::Interop.zlib.InflateInit2_(stream, windowBits);
179 _initializationState = State.InitializedForInflate;
180 return result;
181 }
182 }
183
184 public unsafe ErrorCode Inflate(FlushCode flush)
185 {
187 EnsureState(State.InitializedForInflate);
188 fixed (ZStream* stream = &_zStream)
189 {
190 return global::Interop.zlib.Inflate(stream, flush);
191 }
192 }
193
194 public unsafe ErrorCode InflateEnd()
195 {
197 EnsureState(State.InitializedForInflate);
198 fixed (ZStream* stream = &_zStream)
199 {
200 ErrorCode result = global::Interop.zlib.InflateEnd(stream);
201 _initializationState = State.Disposed;
202 return result;
203 }
204 }
205 }
206
207 internal struct ZStream
208 {
209 internal IntPtr nextIn;
210
211 internal IntPtr nextOut;
212
213 internal IntPtr msg;
214
215 private readonly IntPtr internalState;
216
217 internal uint availIn;
218
219 internal uint availOut;
220
221 internal void Init()
222 {
223 }
224 }
225
226 public static ErrorCode CreateZLibStreamForDeflate(out ZLibStreamHandle zLibStreamHandle, CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
227 {
228 zLibStreamHandle = new ZLibStreamHandle();
229 return zLibStreamHandle.DeflateInit2_(level, windowBits, memLevel, strategy);
230 }
231
232 public static ErrorCode CreateZLibStreamForInflate(out ZLibStreamHandle zLibStreamHandle, int windowBits)
233 {
234 zLibStreamHandle = new ZLibStreamHandle();
235 return zLibStreamHandle.InflateInit2_(windowBits);
236 }
237}
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 ErrorCode CreateZLibStreamForDeflate(out ZLibStreamHandle zLibStreamHandle, CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
static readonly IntPtr Zero
Definition IntPtr.cs:18