Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FromBase64Transform.cs
Go to the documentation of this file.
1
using
System.Buffers.Text
;
2
using
System.Runtime.CompilerServices
;
3
4
namespace
System.Security.Cryptography
;
5
6
public
class
FromBase64Transform
:
ICryptoTransform
,
IDisposable
7
{
8
private
byte
[]
_inputBuffer
=
new
byte
[4];
9
10
private
int
_inputIndex
;
11
12
private
readonly
FromBase64TransformMode
_whitespaces
;
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
22
public
FromBase64Transform
()
23
: this(
FromBase64TransformMode
.
IgnoreWhiteSpaces
)
24
{
25
}
26
27
public
FromBase64Transform
(
FromBase64TransformMode
whitespaces)
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
{
37
ThrowHelper
.
ThrowObjectDisposed
();
38
}
39
if
(outputBuffer ==
null
)
40
{
41
ThrowHelper
.
ThrowArgumentNull
(
ThrowHelper
.
ExceptionArgument
.outputBuffer);
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;
55
ReturnToCryptoPool
(
array
, tmpBuffer.
Length
);
56
return
0;
57
}
58
ConvertFromBase64
(tmpBuffer, outputBuffer.AsSpan(outputOffset), out var _, out var written);
59
ReturnToCryptoPool
(
array
, tmpBuffer.
Length
);
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
{
68
ThrowHelper
.
ThrowObjectDisposed
();
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
();
85
ReturnToCryptoPool
(
array
, tmpBuffer.
Length
);
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 _);
91
ReturnToCryptoPool
(
array
, tmpBuffer.
Length
);
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
{
151
span = (
array
=
System
.
Security
.
Cryptography
.
CryptoPool
.
Rent
(num));
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
{
161
ThrowHelper
.
ThrowBase64FormatException
();
162
}
163
ReturnToCryptoPool
(
array
, span.
Length
);
164
}
165
166
private
void
ReturnToCryptoPool
(
byte
[]
array
,
int
clearSize)
167
{
168
if
(
array
!=
null
)
169
{
170
System
.
Security
.
Cryptography
.
CryptoPool
.
Return
(
array
, clearSize);
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
{
196
CryptographicOperations
.
ZeroMemory
(
_inputBuffer
);
197
_inputBuffer
=
null
;
198
}
199
Reset
();
200
}
201
}
202
203
~FromBase64Transform
()
204
{
205
Dispose
(disposing:
false
);
206
}
207
}
System.Array
Definition
Array.cs:16
System.Buffers.Text.Base64.GetMaxDecodedFromUtf8Length
static int GetMaxDecodedFromUtf8Length(int length)
Definition
Base64.cs:214
System.Buffers.Text.Base64.DecodeFromUtf8
static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan< byte > utf8, Span< byte > bytes, out int bytesConsumed, out int bytesWritten, bool isFinalBlock=true)
Definition
Base64.cs:43
System.Buffers.Text.Base64
Definition
Base64.cs:10
System.GC.SuppressFinalize
static void SuppressFinalize(object obj)
Definition
GC.cs:202
System.GC
Definition
GC.cs:8
System.Security.Cryptography.CryptoPool.Return
static void Return(byte[] array, int clearSize=-1)
Definition
CryptoPool.cs:12
System.Security.Cryptography.CryptoPool.Rent
static byte[] Rent(int minimumLength)
Definition
CryptoPool.cs:7
System.Security.Cryptography.CryptoPool
Definition
CryptoPool.cs:6
System.Security.Cryptography.CryptographicOperations.ZeroMemory
static void ZeroMemory(Span< byte > buffer)
Definition
CryptographicOperations.cs:24
System.Security.Cryptography.CryptographicOperations
Definition
CryptographicOperations.cs:6
System.Security.Cryptography.FromBase64Transform._whitespaces
readonly FromBase64TransformMode _whitespaces
Definition
FromBase64Transform.cs:12
System.Security.Cryptography.FromBase64Transform.IsWhitespace
static bool IsWhitespace(byte value)
Definition
FromBase64Transform.cs:119
System.Security.Cryptography.FromBase64Transform.DiscardWhiteSpaces
static Span< byte > DiscardWhiteSpaces(Span< byte > inputBuffer, Span< byte > tmpBuffer)
Definition
FromBase64Transform.cs:106
System.Security.Cryptography.FromBase64Transform.TransformBlock
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
Definition
FromBase64Transform.cs:32
System.Security.Cryptography.FromBase64Transform.FromBase64Transform
FromBase64Transform()
Definition
FromBase64Transform.cs:22
System.Security.Cryptography.FromBase64Transform.FromBase64Transform
FromBase64Transform(FromBase64TransformMode whitespaces)
Definition
FromBase64Transform.cs:27
System.Security.Cryptography.FromBase64Transform.Dispose
virtual void Dispose(bool disposing)
Definition
FromBase64Transform.cs:190
System.Security.Cryptography.FromBase64Transform.CanReuseTransform
virtual bool CanReuseTransform
Definition
FromBase64Transform.cs:20
System.Security.Cryptography.FromBase64Transform.TransformFinalBlock
byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
Definition
FromBase64Transform.cs:63
System.Security.Cryptography.FromBase64Transform.ConvertFromBase64
void ConvertFromBase64(Span< byte > tmpBuffer, Span< byte > outputBuffer, out int consumed, out int written)
Definition
FromBase64Transform.cs:144
System.Security.Cryptography.FromBase64Transform.ReturnToCryptoPool
void ReturnToCryptoPool(byte[] array, int clearSize)
Definition
FromBase64Transform.cs:166
System.Security.Cryptography.FromBase64Transform.Dispose
void Dispose()
Definition
FromBase64Transform.cs:184
System.Security.Cryptography.FromBase64Transform.InputBlockSize
int InputBlockSize
Definition
FromBase64Transform.cs:14
System.Security.Cryptography.FromBase64Transform.~FromBase64Transform
~FromBase64Transform()
Definition
FromBase64Transform.cs:203
System.Security.Cryptography.FromBase64Transform._inputIndex
int _inputIndex
Definition
FromBase64Transform.cs:10
System.Security.Cryptography.FromBase64Transform.GetTempBuffer
Span< byte > GetTempBuffer(Span< byte > inputBuffer, Span< byte > tmpBuffer)
Definition
FromBase64Transform.cs:96
System.Security.Cryptography.FromBase64Transform.Clear
void Clear()
Definition
FromBase64Transform.cs:174
System.Security.Cryptography.FromBase64Transform.CanTransformMultipleBlocks
bool CanTransformMultipleBlocks
Definition
FromBase64Transform.cs:18
System.Security.Cryptography.FromBase64Transform.GetOutputSize
int GetOutputSize(int bytesToTransform, Span< byte > tmpBuffer)
Definition
FromBase64Transform.cs:129
System.Security.Cryptography.FromBase64Transform._inputBuffer
byte[] _inputBuffer
Definition
FromBase64Transform.cs:8
System.Security.Cryptography.FromBase64Transform.Reset
void Reset()
Definition
FromBase64Transform.cs:179
System.Security.Cryptography.FromBase64Transform.OutputBlockSize
int OutputBlockSize
Definition
FromBase64Transform.cs:16
System.Security.Cryptography.FromBase64Transform
Definition
FromBase64Transform.cs:7
System.Security.Cryptography.ThrowHelper.ValidateTransformBlock
static void ValidateTransformBlock(byte[] inputBuffer, int inputOffset, int inputCount)
Definition
ThrowHelper.cs:17
System.Security.Cryptography.ThrowHelper.ThrowArgumentNull
static void ThrowArgumentNull(ExceptionArgument argument)
Definition
ThrowHelper.cs:38
System.Security.Cryptography.ThrowHelper.ThrowBase64FormatException
static void ThrowBase64FormatException()
Definition
ThrowHelper.cs:62
System.Security.Cryptography.ThrowHelper.ThrowObjectDisposed
static void ThrowObjectDisposed()
Definition
ThrowHelper.cs:56
System.Security.Cryptography.ThrowHelper.ExceptionArgument
ExceptionArgument
Definition
ThrowHelper.cs:9
System.Security.Cryptography.ThrowHelper
Definition
ThrowHelper.cs:7
System.IDisposable
Definition
IDisposable.cs:4
System.Security.Cryptography.ICryptoTransform
Definition
ICryptoTransform.cs:4
System.Buffers.Text
Definition
Base64.cs:7
System.Runtime.CompilerServices.MethodImplOptions
MethodImplOptions
Definition
MethodImplOptions.cs:5
System.Runtime.CompilerServices
Definition
NullablePublicOnlyAttribute.cs:3
System.Security.Cryptography.FromBase64TransformMode
FromBase64TransformMode
Definition
FromBase64TransformMode.cs:4
System.Security.Cryptography.FromBase64TransformMode.IgnoreWhiteSpaces
@ IgnoreWhiteSpaces
System.Security.Cryptography
Definition
CryptoPool.cs:3
System.Security
Definition
ExtendedProtectionPolicyTypeConverter.cs:8
System.ExceptionArgument.value
@ value
System.ExceptionArgument.length
@ length
System.ExceptionArgument.array
@ array
System
Definition
BlockingCollection.cs:8
System.Span.CopyTo
void CopyTo(Span< T > destination)
Definition
Span.cs:224
System.Span.Slice
Span< T > Slice(int start)
Definition
Span.cs:271
System.Span.Length
int Length
Definition
Span.cs:70
System.Span
Definition
Span.cs:14
source
System.Security.Cryptography.Encoding
System.Security.Cryptography
FromBase64Transform.cs
Generated by
1.10.0