Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FromBase64Transform.cs
Go to the documentation of this file.
3
5
7{
8 private byte[] _inputBuffer = new byte[4];
9
10 private int _inputIndex;
11
13
14 public int InputBlockSize => 4;
15
16 public int OutputBlockSize => 3;
17
18 public bool CanTransformMultipleBlocks => true;
19
20 public virtual bool CanReuseTransform => true;
21
26
28 {
29 _whitespaces = whitespaces;
30 }
31
32 public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
33 {
34 ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount);
35 if (_inputBuffer == null)
36 {
38 }
39 if (outputBuffer == null)
40 {
42 }
43 byte[] array = null;
44 Span<byte> tmpBuffer = stackalloc byte[32];
45 if (inputCount > 32)
46 {
47 tmpBuffer = (array = System.Security.Cryptography.CryptoPool.Rent(inputCount));
48 }
49 tmpBuffer = GetTempBuffer(inputBuffer.AsSpan(inputOffset, inputCount), tmpBuffer);
50 int num = _inputIndex + tmpBuffer.Length;
51 if (num < InputBlockSize)
52 {
53 tmpBuffer.CopyTo(_inputBuffer.AsSpan(_inputIndex));
54 _inputIndex = num;
56 return 0;
57 }
58 ConvertFromBase64(tmpBuffer, outputBuffer.AsSpan(outputOffset), out var _, out var written);
60 return written;
61 }
62
63 public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
64 {
65 ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount);
66 if (_inputBuffer == null)
67 {
69 }
70 if (inputCount == 0)
71 {
72 return Array.Empty<byte>();
73 }
74 byte[] array = null;
75 Span<byte> tmpBuffer = stackalloc byte[32];
76 if (inputCount > 32)
77 {
78 tmpBuffer = (array = System.Security.Cryptography.CryptoPool.Rent(inputCount));
79 }
80 tmpBuffer = GetTempBuffer(inputBuffer.AsSpan(inputOffset, inputCount), tmpBuffer);
81 int num = _inputIndex + tmpBuffer.Length;
82 if (num < InputBlockSize)
83 {
84 Reset();
86 return Array.Empty<byte>();
87 }
88 int outputSize = GetOutputSize(num, tmpBuffer);
89 byte[] array2 = new byte[outputSize];
90 ConvertFromBase64(tmpBuffer, array2, out var _, out var _);
92 Reset();
93 return array2;
94 }
95
96 private Span<byte> GetTempBuffer(Span<byte> inputBuffer, Span<byte> tmpBuffer)
97 {
98 if (_whitespaces == FromBase64TransformMode.DoNotIgnoreWhiteSpaces)
99 {
100 return inputBuffer;
101 }
102 return DiscardWhiteSpaces(inputBuffer, tmpBuffer);
103 }
104
105 [MethodImpl(MethodImplOptions.AggressiveInlining)]
106 private static Span<byte> DiscardWhiteSpaces(Span<byte> inputBuffer, Span<byte> tmpBuffer)
107 {
108 int length = 0;
109 for (int i = 0; i < inputBuffer.Length; i++)
110 {
111 if (!IsWhitespace(inputBuffer[i]))
112 {
113 tmpBuffer[length++] = inputBuffer[i];
114 }
115 }
116 return tmpBuffer.Slice(0, length);
117 }
118
119 private static bool IsWhitespace(byte value)
120 {
121 if (value != 32)
122 {
123 return (uint)(value - 9) <= 4u;
124 }
125 return true;
126 }
127
128 [MethodImpl(MethodImplOptions.AggressiveInlining)]
129 private int GetOutputSize(int bytesToTransform, Span<byte> tmpBuffer)
130 {
131 int num = Base64.GetMaxDecodedFromUtf8Length(bytesToTransform);
132 int length = tmpBuffer.Length;
133 if (tmpBuffer[length - 2] == 61)
134 {
135 num--;
136 }
137 if (tmpBuffer[length - 1] == 61)
138 {
139 num--;
140 }
141 return num;
142 }
143
144 private void ConvertFromBase64(Span<byte> tmpBuffer, Span<byte> outputBuffer, out int consumed, out int written)
145 {
146 int num = _inputIndex + tmpBuffer.Length;
147 byte[] array = null;
148 Span<byte> span = stackalloc byte[32];
149 if (num > 32)
150 {
152 }
153 _inputBuffer.AsSpan(0, _inputIndex).CopyTo(span);
154 tmpBuffer.CopyTo(span.Slice(_inputIndex));
155 _inputIndex = num & 3;
156 num -= _inputIndex;
157 tmpBuffer.Slice(tmpBuffer.Length - _inputIndex).CopyTo(_inputBuffer);
158 span = span.Slice(0, num);
159 if (Base64.DecodeFromUtf8(span, outputBuffer, out consumed, out written) != 0)
160 {
162 }
164 }
165
166 private void ReturnToCryptoPool(byte[] array, int clearSize)
167 {
168 if (array != null)
169 {
171 }
172 }
173
174 public void Clear()
175 {
176 Dispose();
177 }
178
179 private void Reset()
180 {
181 _inputIndex = 0;
182 }
183
184 public void Dispose()
185 {
186 Dispose(disposing: true);
187 GC.SuppressFinalize(this);
188 }
189
190 protected virtual void Dispose(bool disposing)
191 {
192 if (disposing)
193 {
194 if (_inputBuffer != null)
195 {
197 _inputBuffer = null;
198 }
199 Reset();
200 }
201 }
202
204 {
205 Dispose(disposing: false);
206 }
207}
static int GetMaxDecodedFromUtf8Length(int length)
Definition Base64.cs:214
static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan< byte > utf8, Span< byte > bytes, out int bytesConsumed, out int bytesWritten, bool isFinalBlock=true)
Definition Base64.cs:43
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
static Span< byte > DiscardWhiteSpaces(Span< byte > inputBuffer, Span< byte > tmpBuffer)
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
FromBase64Transform(FromBase64TransformMode whitespaces)
byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
void ConvertFromBase64(Span< byte > tmpBuffer, Span< byte > outputBuffer, out int consumed, out int written)
void ReturnToCryptoPool(byte[] array, int clearSize)
Span< byte > GetTempBuffer(Span< byte > inputBuffer, Span< byte > tmpBuffer)
int GetOutputSize(int bytesToTransform, Span< byte > tmpBuffer)
static void ValidateTransformBlock(byte[] inputBuffer, int inputOffset, int inputCount)
static void ThrowArgumentNull(ExceptionArgument argument)
void CopyTo(Span< T > destination)
Definition Span.cs:224
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70