Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BrotliDecoder.cs
Go to the documentation of this file.
4
6
8{
10
11 private bool _disposed;
12
13 internal void InitializeDecoder()
14 {
15 _state = global::Interop.Brotli.BrotliDecoderCreateInstance(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
16 if (_state.IsInvalid)
17 {
19 }
20 }
21
22 internal void EnsureInitialized()
23 {
25 if (_state == null)
26 {
28 }
29 }
30
31 public void Dispose()
32 {
33 _disposed = true;
34 _state?.Dispose();
35 }
36
37 private void EnsureNotDisposed()
38 {
39 if (_disposed)
40 {
41 throw new ObjectDisposedException("BrotliDecoder", System.SR.BrotliDecoder_Disposed);
42 }
43 }
44
45 public unsafe OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten)
46 {
48 bytesConsumed = 0;
49 bytesWritten = 0;
50 if (global::Interop.Brotli.BrotliDecoderIsFinished(_state) != 0)
51 {
52 return OperationStatus.Done;
53 }
54 nuint availableOut = (nuint)destination.Length;
55 nuint availableIn = (nuint)source.Length;
56 while ((int)availableOut > 0)
57 {
58 fixed (byte* ptr = &MemoryMarshal.GetReference(source))
59 {
60 byte* ptr2 = ptr;
61 fixed (byte* ptr3 = &MemoryMarshal.GetReference(destination))
62 {
63 byte* ptr4 = ptr3;
64 UIntPtr totalOut;
65 int num = global::Interop.Brotli.BrotliDecoderDecompressStream(_state, ref availableIn, &ptr2, ref availableOut, &ptr4, out totalOut);
66 if (num == 0)
67 {
68 return OperationStatus.InvalidData;
69 }
70 bytesConsumed += source.Length - (int)availableIn;
71 bytesWritten += destination.Length - (int)availableOut;
72 switch (num)
73 {
74 case 1:
75 return OperationStatus.Done;
76 case 3:
77 return OperationStatus.DestinationTooSmall;
78 }
79 source = source.Slice(source.Length - (int)availableIn);
80 destination = destination.Slice(destination.Length - (int)availableOut);
81 if (num == 2 && source.Length == 0)
82 {
83 return OperationStatus.NeedMoreData;
84 }
85 }
86 }
87 }
88 return OperationStatus.DestinationTooSmall;
89 }
90
91 public unsafe static bool TryDecompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
92 {
93 fixed (byte* inBytes = &MemoryMarshal.GetReference(source))
94 {
95 fixed (byte* outBytes = &MemoryMarshal.GetReference(destination))
96 {
97 nuint num = (nuint)destination.Length;
98 bool result = global::Interop.Brotli.BrotliDecoderDecompress((nuint)source.Length, inBytes, &num, outBytes) != global::Interop.BOOL.FALSE;
99 bytesWritten = (int)num;
100 return result;
101 }
102 }
103 }
104}
static string BrotliDecoder_Create
Definition SR.cs:34
static string BrotliDecoder_Disposed
Definition SR.cs:36
Definition SR.cs:7
SafeBrotliDecoderHandle _state
static unsafe bool TryDecompress(ReadOnlySpan< byte > source, Span< byte > destination, out int bytesWritten)
unsafe OperationStatus Decompress(ReadOnlySpan< byte > source, Span< byte > destination, out int bytesConsumed, out int bytesWritten)
static readonly IntPtr Zero
Definition IntPtr.cs:18