File tree Expand file tree Collapse file tree
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44using System . Buffers . Binary ;
55using System . Collections . Generic ;
66using System . Diagnostics . CodeAnalysis ;
7- using System . Numerics ;
87using System . Runtime . CompilerServices ;
98using System . Runtime . InteropServices ;
109
@@ -20,42 +19,6 @@ internal static class DateTimeOffsetPolyfills
2019 }
2120 }
2221
23- internal static class BigIntegerPolyfills
24- {
25- extension ( BigInteger self )
26- {
27- public byte [ ] ToByteArray ( bool isUnsigned , bool isBigEndian )
28- {
29- byte [ ] littleEndianBytes = self . ToByteArray ( ) ;
30-
31- if ( littleEndianBytes . Length == 1 )
32- return littleEndianBytes ;
33-
34- Span < byte > bytesAsSpan = littleEndianBytes ;
35-
36- if ( isBigEndian )
37- bytesAsSpan . Reverse ( ) ;
38-
39- if ( isUnsigned )
40- {
41- int start = 0 ;
42- for ( int i = 0 ; i < bytesAsSpan . Length ; i ++ )
43- {
44- if ( bytesAsSpan [ i ] == 0x00 )
45- start ++ ;
46- else
47- break ;
48- }
49-
50- if ( start > 0 )
51- return bytesAsSpan . Slice ( start ) . ToArray ( ) ;
52- }
53-
54- return littleEndianBytes ;
55- }
56- }
57- }
58-
5922 internal static class DecimalPolyfills
6023 {
6124 extension ( decimal )
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ public void WriteBigInteger(BigInteger value)
9393 {
9494 bool isUnsigned = value . Sign >= 0 ;
9595 BigInteger unsignedValue = isUnsigned ? value : - 1 - value ;
96- byte [ ] unsignedBigEndianEncoding = unsignedValue . ToByteArray ( isUnsigned : true , isBigEndian : true ) ;
96+ byte [ ] unsignedBigEndianEncoding = CreateUnsignedBigEndianBytes ( unsignedValue ) ;
9797
9898 WriteTag ( isUnsigned ? CborTag . UnsignedBigNum : CborTag . NegativeBigNum ) ;
9999 WriteByteString ( unsignedBigEndianEncoding ) ;
@@ -207,5 +207,31 @@ public static decimal Reconstruct(decimal mantissa, long exponent)
207207 }
208208 }
209209 }
210+
211+ private static byte [ ] CreateUnsignedBigEndianBytes ( BigInteger value )
212+ {
213+ #if NET
214+ return value . ToByteArray ( isUnsigned : true , isBigEndian : true ) ;
215+ #else
216+ byte [ ] littleEndianBytes = value . ToByteArray ( ) ;
217+
218+ if ( littleEndianBytes . Length == 1 )
219+ return littleEndianBytes ;
220+
221+ Span < byte > bytesAsSpan = littleEndianBytes ;
222+ bytesAsSpan . Reverse ( ) ;
223+
224+ int start = 0 ;
225+ for ( int i = 0 ; i < bytesAsSpan . Length ; i ++ )
226+ {
227+ if ( bytesAsSpan [ i ] == 0x00 )
228+ start ++ ;
229+ else
230+ break ;
231+ }
232+
233+ return start == 0 ? littleEndianBytes : bytesAsSpan . Slice ( start ) . ToArray ( ) ;
234+ #endif
235+ }
210236 }
211237}
You can’t perform that action at this time.
0 commit comments