Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Base64 decoding regressions from whitespace handling #86396

Merged
merged 3 commits into from
May 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,10 @@ private static unsafe OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, o
uint destIndex = 0;

// only decode input if it is a multiple of 4
if (bufferLength != ((bufferLength >> 2) * 4))
if (bufferLength % 4 != 0)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
goto InvalidExit;
}
if (bufferLength == 0)
{
goto DoneExit;
}

ref sbyte decodingMap = ref MemoryMarshal.GetReference(DecodingMap);

Expand All @@ -387,8 +383,8 @@ private static unsafe OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, o
uint t2 = bufferBytes[bufferLength - 2];
uint t3 = bufferBytes[bufferLength - 1];

int i0 = Unsafe.Add(ref decodingMap, (IntPtr)t0);
int i1 = Unsafe.Add(ref decodingMap, (IntPtr)t1);
int i0 = Unsafe.Add(ref decodingMap, t0);
int i1 = Unsafe.Add(ref decodingMap, t1);

i0 <<= 18;
i1 <<= 12;
Expand All @@ -397,8 +393,8 @@ private static unsafe OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, o

if (t3 != EncodingPad)
{
int i2 = Unsafe.Add(ref decodingMap, (IntPtr)t2);
int i3 = Unsafe.Add(ref decodingMap, (IntPtr)t3);
int i2 = Unsafe.Add(ref decodingMap, t2);
int i3 = Unsafe.Add(ref decodingMap, t3);

i2 <<= 6;

Expand All @@ -415,7 +411,7 @@ private static unsafe OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, o
}
else if (t2 != EncodingPad)
{
int i2 = Unsafe.Add(ref decodingMap, (IntPtr)t2);
int i2 = Unsafe.Add(ref decodingMap, t2);

i2 <<= 6;

Expand All @@ -441,7 +437,6 @@ private static unsafe OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, o
destIndex += 1;
}

DoneExit:
bytesWritten = (int)destIndex;
return OperationStatus.Done;

Expand Down Expand Up @@ -949,10 +944,10 @@ private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
uint t2 = encodedBytes[2];
uint t3 = encodedBytes[3];

int i0 = Unsafe.Add(ref decodingMap, (IntPtr)t0);
int i1 = Unsafe.Add(ref decodingMap, (IntPtr)t1);
int i2 = Unsafe.Add(ref decodingMap, (IntPtr)t2);
int i3 = Unsafe.Add(ref decodingMap, (IntPtr)t3);
int i0 = Unsafe.Add(ref decodingMap, t0);
int i1 = Unsafe.Add(ref decodingMap, t1);
int i2 = Unsafe.Add(ref decodingMap, t2);
int i3 = Unsafe.Add(ref decodingMap, t3);

i0 <<= 18;
i1 <<= 12;
Expand Down