@@ -37,22 +37,22 @@ public static partial class Base64
37
37
public static OperationStatus DecodeFromUtf8 ( ReadOnlySpan < byte > utf8 , Span < byte > bytes , out int bytesConsumed , out int bytesWritten , bool isFinalBlock = true ) =>
38
38
DecodeFrom < Base64DecoderByte , byte > ( utf8 , bytes , out bytesConsumed , out bytesWritten , isFinalBlock , ignoreWhiteSpace : true ) ;
39
39
40
- internal static unsafe OperationStatus DecodeFrom < TBase64Decoder , T > ( ReadOnlySpan < T > utf8 , Span < byte > bytes ,
40
+ internal static unsafe OperationStatus DecodeFrom < TBase64Decoder , T > ( ReadOnlySpan < T > source , Span < byte > bytes ,
41
41
out int bytesConsumed , out int bytesWritten , bool isFinalBlock , bool ignoreWhiteSpace )
42
42
where TBase64Decoder : IBase64Decoder < T >
43
43
where T : unmanaged
44
44
{
45
- if ( utf8 . IsEmpty )
45
+ if ( source . IsEmpty )
46
46
{
47
47
bytesConsumed = 0 ;
48
48
bytesWritten = 0 ;
49
49
return OperationStatus . Done ;
50
50
}
51
51
52
- fixed ( T * srcBytes = & MemoryMarshal . GetReference ( utf8 ) )
52
+ fixed ( T * srcBytes = & MemoryMarshal . GetReference ( source ) )
53
53
fixed ( byte * destBytes = & MemoryMarshal . GetReference ( bytes ) )
54
54
{
55
- int srcLength = TBase64Decoder . SrcLength ( isFinalBlock , utf8 . Length ) ;
55
+ int srcLength = TBase64Decoder . SrcLength ( isFinalBlock , source . Length ) ;
56
56
int destLength = bytes . Length ;
57
57
int maxSrcLength = srcLength ;
58
58
int decodedLength = TBase64Decoder . GetMaxDecodedLength ( srcLength ) ;
@@ -166,7 +166,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
166
166
goto InvalidDataExit ;
167
167
}
168
168
169
- if ( src == srcBytes + utf8 . Length )
169
+ if ( src == srcBytes + source . Length )
170
170
{
171
171
goto DoneExit ;
172
172
}
@@ -244,7 +244,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
244
244
src += remaining ;
245
245
}
246
246
247
- if ( srcLength != utf8 . Length )
247
+ if ( srcLength != source . Length )
248
248
{
249
249
goto InvalidDataExit ;
250
250
}
@@ -255,7 +255,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
255
255
return OperationStatus . Done ;
256
256
257
257
DestinationTooSmallExit :
258
- if ( srcLength != utf8 . Length && isFinalBlock )
258
+ if ( srcLength != source . Length && isFinalBlock )
259
259
{
260
260
goto InvalidDataExit ; // if input is not a multiple of 4, and there is no more data, return invalid data instead
261
261
}
@@ -273,7 +273,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(ReadOnlySpa
273
273
bytesConsumed = ( int ) ( src - srcBytes ) ;
274
274
bytesWritten = ( int ) ( dest - destBytes ) ;
275
275
return ignoreWhiteSpace ?
276
- InvalidDataFallback ( utf8 , bytes , ref bytesConsumed , ref bytesWritten , isFinalBlock ) :
276
+ InvalidDataFallback ( source , bytes , ref bytesConsumed , ref bytesWritten , isFinalBlock ) :
277
277
OperationStatus . InvalidData ;
278
278
}
279
279
@@ -492,33 +492,33 @@ internal static unsafe OperationStatus DecodeFromUtf8InPlace<TBase64Decoder>(Spa
492
492
}
493
493
}
494
494
495
- internal static OperationStatus DecodeWithWhiteSpaceBlockwise < TBase64Decoder > ( ReadOnlySpan < byte > utf8 , Span < byte > bytes , ref int bytesConsumed , ref int bytesWritten , bool isFinalBlock = true )
495
+ internal static OperationStatus DecodeWithWhiteSpaceBlockwise < TBase64Decoder > ( ReadOnlySpan < byte > source , Span < byte > bytes , ref int bytesConsumed , ref int bytesWritten , bool isFinalBlock = true )
496
496
where TBase64Decoder : IBase64Decoder < byte >
497
497
{
498
498
const int BlockSize = 4 ;
499
499
Span < byte > buffer = stackalloc byte [ BlockSize ] ;
500
500
OperationStatus status = OperationStatus . Done ;
501
501
502
- while ( ! utf8 . IsEmpty )
502
+ while ( ! source . IsEmpty )
503
503
{
504
504
int encodedIdx = 0 ;
505
505
int bufferIdx = 0 ;
506
506
int skipped = 0 ;
507
507
508
- for ( ; encodedIdx < utf8 . Length && ( uint ) bufferIdx < ( uint ) buffer . Length ; ++ encodedIdx )
508
+ for ( ; encodedIdx < source . Length && ( uint ) bufferIdx < ( uint ) buffer . Length ; ++ encodedIdx )
509
509
{
510
- if ( IsWhiteSpace ( utf8 [ encodedIdx ] ) )
510
+ if ( IsWhiteSpace ( source [ encodedIdx ] ) )
511
511
{
512
512
skipped ++ ;
513
513
}
514
514
else
515
515
{
516
- buffer [ bufferIdx ] = utf8 [ encodedIdx ] ;
516
+ buffer [ bufferIdx ] = source [ encodedIdx ] ;
517
517
bufferIdx ++ ;
518
518
}
519
519
}
520
520
521
- utf8 = utf8 . Slice ( encodedIdx ) ;
521
+ source = source . Slice ( encodedIdx ) ;
522
522
bytesConsumed += skipped ;
523
523
524
524
if ( bufferIdx == 0 )
@@ -530,11 +530,11 @@ internal static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(Re
530
530
531
531
if ( typeof ( TBase64Decoder ) == typeof ( Base64DecoderByte ) || bufferIdx == 1 )
532
532
{
533
- hasAnotherBlock = utf8 . Length >= BlockSize && bufferIdx == BlockSize ;
533
+ hasAnotherBlock = source . Length >= BlockSize && bufferIdx == BlockSize ;
534
534
}
535
535
else
536
536
{
537
- hasAnotherBlock = utf8 . Length > 1 ;
537
+ hasAnotherBlock = source . Length > 1 ;
538
538
}
539
539
540
540
bool localIsFinalBlock = ! hasAnotherBlock ;
@@ -567,9 +567,9 @@ internal static OperationStatus DecodeWithWhiteSpaceBlockwise<TBase64Decoder>(Re
567
567
// The remaining data must all be whitespace in order to be valid.
568
568
if ( ! hasAnotherBlock )
569
569
{
570
- for ( int i = 0 ; i < utf8 . Length ; ++ i )
570
+ for ( int i = 0 ; i < source . Length ; ++ i )
571
571
{
572
- if ( ! IsWhiteSpace ( utf8 [ i ] ) )
572
+ if ( ! IsWhiteSpace ( source [ i ] ) )
573
573
{
574
574
// Revert previous dest increment, since an invalid state followed.
575
575
bytesConsumed -= localConsumed ;
@@ -596,32 +596,39 @@ private static int GetPaddingCount<TBase64Decoder>(ref byte ptrToLastElement)
596
596
{
597
597
int padding = 0 ;
598
598
599
- if ( TBase64Decoder . IsValidPadding ( ptrToLastElement ) ) padding ++ ;
600
- if ( TBase64Decoder . IsValidPadding ( Unsafe . Subtract ( ref ptrToLastElement , 1 ) ) ) padding ++ ;
599
+ if ( TBase64Decoder . IsValidPadding ( ptrToLastElement ) )
600
+ {
601
+ padding ++ ;
602
+ }
603
+
604
+ if ( TBase64Decoder . IsValidPadding ( Unsafe . Subtract ( ref ptrToLastElement , 1 ) ) )
605
+ {
606
+ padding ++ ;
607
+ }
601
608
602
609
return padding ;
603
610
}
604
611
605
- private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace < TBase64Decoder > ( Span < byte > utf8 , ref int destIndex , uint sourceIndex )
612
+ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace < TBase64Decoder > ( Span < byte > source , ref int destIndex , uint sourceIndex )
606
613
where TBase64Decoder : IBase64Decoder < byte >
607
614
{
608
- int BlockSize = Math . Min ( utf8 . Length - ( int ) sourceIndex , 4 ) ;
615
+ int BlockSize = Math . Min ( source . Length - ( int ) sourceIndex , 4 ) ;
609
616
Span < byte > buffer = stackalloc byte [ BlockSize ] ;
610
617
611
618
OperationStatus status = OperationStatus . Done ;
612
619
int localDestIndex = destIndex ;
613
620
bool hasPaddingBeenProcessed = false ;
614
621
int localBytesWritten = 0 ;
615
622
616
- while ( sourceIndex < ( uint ) utf8 . Length )
623
+ while ( sourceIndex < ( uint ) source . Length )
617
624
{
618
625
int bufferIdx = 0 ;
619
626
620
- while ( bufferIdx < BlockSize && sourceIndex < ( uint ) utf8 . Length )
627
+ while ( bufferIdx < BlockSize && sourceIndex < ( uint ) source . Length )
621
628
{
622
- if ( ! IsWhiteSpace ( utf8 [ ( int ) sourceIndex ] ) )
629
+ if ( ! IsWhiteSpace ( source [ ( int ) sourceIndex ] ) )
623
630
{
624
- buffer [ bufferIdx ] = utf8 [ ( int ) sourceIndex ] ;
631
+ buffer [ bufferIdx ] = source [ ( int ) sourceIndex ] ;
625
632
bufferIdx ++ ;
626
633
}
627
634
@@ -645,7 +652,7 @@ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace<TBase64Decode
645
652
{
646
653
while ( bufferIdx < BlockSize ) // Can happen only for last block
647
654
{
648
- Debug . Assert ( utf8 . Length == sourceIndex ) ;
655
+ Debug . Assert ( source . Length == sourceIndex ) ;
649
656
buffer [ bufferIdx ++ ] = ( byte ) EncodingPad ;
650
657
}
651
658
}
@@ -672,7 +679,7 @@ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace<TBase64Decode
672
679
// Write result to source span in place.
673
680
for ( int i = 0 ; i < localBytesWritten ; i ++ )
674
681
{
675
- utf8 [ localDestIndex - localBytesWritten + i ] = buffer [ i ] ;
682
+ source [ localDestIndex - localBytesWritten + i ] = buffer [ i ] ;
676
683
}
677
684
}
678
685
0 commit comments