Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b456415

Browse files
gfoidlbartonjs
authored andcommitted
FromBase64Transform.InputBlockSize set to 4 instead of 1
1 parent 30e06a4 commit b456415

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/System.Security.Cryptography.Encoding/src/System/Security/Cryptography/Base64Transforms.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ public FromBase64Transform(FromBase64TransformMode whitespaces)
120120
_whitespaces = whitespaces;
121121
}
122122

123-
// Converting from Base64 generates 3 bytes output from each 4 bytes input block
124-
private const int Base64InputBlockSize = 4;
125123
// A buffer with size 32 is stack allocated, to cover common cases and benefit from JIT's optimizations.
126124
private const int StackAllocSize = 32;
127-
public int InputBlockSize => 1;
125+
126+
// Converting from Base64 generates 3 bytes output from each 4 bytes input block
127+
public int InputBlockSize => 4;
128128
public int OutputBlockSize => 3;
129-
public bool CanTransformMultipleBlocks => false;
129+
public bool CanTransformMultipleBlocks => true;
130130
public virtual bool CanReuseTransform => true;
131131

132132
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
@@ -152,7 +152,7 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
152152
int bytesToTransform = _inputIndex + tmpBuffer.Length;
153153

154154
// Too little data to decode: save data to _inputBuffer, so it can be transformed later
155-
if (bytesToTransform < Base64InputBlockSize)
155+
if (bytesToTransform < InputBlockSize)
156156
{
157157
tmpBuffer.CopyTo(_inputBuffer.AsSpan(_inputIndex));
158158

@@ -197,7 +197,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
197197
int bytesToTransform = _inputIndex + tmpBuffer.Length;
198198

199199
// Too little data to decode
200-
if (bytesToTransform < Base64InputBlockSize)
200+
if (bytesToTransform < InputBlockSize)
201201
{
202202
// reinitialize the transform
203203
Reset();

src/System.Security.Cryptography.Encoding/tests/Base64TransformsTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,45 @@ public static void ValidateWhitespace(string expected, string data)
254254
Assert.Throws<FormatException>(() => cs.Read(outputBytes, 0, outputBytes.Length));
255255
}
256256
}
257+
258+
[Fact]
259+
public void Blocksizes_ToBase64Transform()
260+
{
261+
using (var transform = new ToBase64Transform())
262+
{
263+
Assert.Equal(3, transform.InputBlockSize);
264+
Assert.Equal(4, transform.OutputBlockSize);
265+
}
266+
}
267+
268+
[Fact]
269+
public void Blocksizes_FromBase64Transform()
270+
{
271+
using (var transform = new FromBase64Transform())
272+
{
273+
Assert.Equal(4, transform.InputBlockSize);
274+
Assert.Equal(3, transform.OutputBlockSize);
275+
}
276+
}
277+
278+
[Fact]
279+
public void TransformUsageFlags_ToBase64Transform()
280+
{
281+
using (var transform = new ToBase64Transform())
282+
{
283+
Assert.False(transform.CanTransformMultipleBlocks);
284+
Assert.True(transform.CanReuseTransform);
285+
}
286+
}
287+
288+
[Fact]
289+
public void TransformUsageFlags_FromBase64Transform()
290+
{
291+
using (var transform = new FromBase64Transform())
292+
{
293+
Assert.True(transform.CanTransformMultipleBlocks);
294+
Assert.True(transform.CanReuseTransform);
295+
}
296+
}
257297
}
258298
}

0 commit comments

Comments
 (0)