-
Notifications
You must be signed in to change notification settings - Fork 1
/
Convert.cs
623 lines (565 loc) · 30.4 KB
/
Convert.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
using System;
using System.Diagnostics;
#if SPAN
using System.Runtime.InteropServices;
#endif
#if INTRINSICS
using System.Runtime.Intrinsics.X86;
#endif
// System candidate
namespace HexMate
{
public static class Convert
{
/// <summary>
/// Converts a subset of a Unicode character array, which encodes binary data as hex characters,
/// to an equivalent 8-bit unsigned integer array.
/// Parameters specify the subset in the input array and the number of elements to convert.
/// </summary>
/// <param name="inArray">A Unicode character array.</param>
/// <param name="offset">A position within <paramref name="inArray"/>.</param>
/// <param name="length">The number of elements in <paramref name="inArray"/> to convert.</param>
/// <returns>An array of 8-bit unsigned integers equivalent to <paramref name="length"/> elements at position <paramref name="offset"/> in <paramref name="inArray"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="inArray"/> is <code>null</code>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="length"/> is less than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> plus <paramref name="length"/> indicates a position not within inArray.</exception>
/// <exception cref="FormatException">The length of <paramref name="inArray"/>, ignoring white-space characters, is not zero or a multiple of 2.</exception>
/// <exception cref="FormatException">The format of <paramref name="inArray"/> is invalid. <paramref name="inArray"/> contains a non-hex character.</exception>
public static byte[] FromHexCharArray(char[] inArray, int offset, int length)
{
if (inArray == null)
throw new ArgumentNullException(nameof(inArray));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_Index);
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_GenericPositive);
if (offset > (inArray.Length - length))
throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_OffsetLength);
if (length == 0)
return Array.Empty<byte>();
unsafe
{
fixed (char* sIn = inArray)
{
var input = sIn + offset;
var resultLength = FromHex_ComputeResultLength(input, length);
#if GC_ALLOC_UNINIT
var result = GC.AllocateUninitializedArray<byte>(resultLength);
#else
var result = new byte[resultLength];
#endif
fixed (byte* outPtr = result)
{
var res = ConvertFromHexArray(outPtr, result.Length, input, length);
if (res < 0)
throw new FormatException(SR.Format_BadHexChar);
Debug.Assert(res == result.Length);
}
return result;
}
}
}
/// <summary>
/// Converts the specified string, which encodes binary data as hex characters, to an equivalent 8-bit unsigned integer array.
/// </summary>
/// <param name="s">The string to convert.</param>
/// <returns>An array of 8-bit unsigned integers that is equivalent to <paramref name="s"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> is <code>null</code>.</exception>
/// <exception cref="FormatException">The length of <paramref name="s"/>, ignoring white-space characters, is not zero or a multiple of 2.</exception>
/// <exception cref="FormatException">The format of <paramref name="s"/> is invalid. <paramref name="s"/> contains a non-hex character.</exception>
public static byte[] FromHexString(string s)
{
if (s == null)
throw new ArgumentNullException(nameof(s));
if (s.Length == 0)
return Array.Empty<byte>();
unsafe
{
fixed (char* inPtr = s)
{
var resultLength = FromHex_ComputeResultLength(inPtr, s.Length);
#if GC_ALLOC_UNINIT
var result = GC.AllocateUninitializedArray<byte>(resultLength);
#else
var result = new byte[resultLength];
#endif
fixed (byte* outPtr = result)
{
var res = ConvertFromHexArray(outPtr, result.Length, inPtr, s.Length);
if (res < 0)
throw new FormatException(SR.Format_BadHexChar);
Debug.Assert(res == result.Length);
}
return result;
}
}
}
/// <summary>
/// Converts a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with hex characters.
/// Parameters specify the subsets as offsets in the input and output arrays, the number of elements in the input array to convert,
/// whether line breaks are inserted in the output array, and whether to insert upper- or lowercase hex characters.
/// </summary>
/// <param name="inArray">An input array of 8-bit unsigned integers.</param>
/// <param name="offsetIn">A position within <paramref name="inArray"/>.</param>
/// <param name="length">The number of elements of <paramref name="inArray"/> to convert.</param>
/// <param name="outArray">An output array of Unicode characters.</param>
/// <param name="offsetOut">A position within <paramref name="outArray"/>.</param>
/// <param name="options"><see cref="HexFormattingOptions.Lowercase"/> to produce lowercase output. <see cref="HexFormattingOptions.InsertLineBreaks"/> to insert a line break every 72 characters. <see cref="HexFormattingOptions.None"/> to do neither.</param>
/// <returns>A 32-bit signed integer containing the number of bytes in <paramref name="outArray"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="inArray"/> or <paramref name="outArray"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offsetIn"/>, <paramref name="offsetOut"/>, or <paramref name="length"/> is negative.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offsetIn"/> plus <paramref name="length"/> is greater than the length of <paramref name="inArray"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offsetOut"/> plus the number of elements to return is greater than the length of <paramref name="outArray"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="options"/> is not a valid <see cref="HexFormattingOptions"/> value.</exception>
public static int ToHexCharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, HexFormattingOptions options = default)
{
if (inArray == null)
throw new ArgumentNullException(nameof(inArray));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_Index);
if (offsetIn < 0)
throw new ArgumentOutOfRangeException(nameof(offsetIn), SR.ArgumentOutOfRange_GenericPositive);
if (offsetIn > (inArray.Length - length))
throw new ArgumentOutOfRangeException(nameof(offsetIn), SR.ArgumentOutOfRange_OffsetLength);
if (outArray == null)
throw new ArgumentNullException(nameof(outArray));
if (offsetOut < 0)
throw new ArgumentOutOfRangeException(nameof(offsetOut), SR.ArgumentOutOfRange_GenericPositive);
if (options < HexFormattingOptions.None || options > (HexFormattingOptions.Lowercase | HexFormattingOptions.InsertLineBreaks))
throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options));
var insertLineBreaks = (options & HexFormattingOptions.InsertLineBreaks) != 0;
var outlen = ToHex_CalculateAndValidateOutputLength(inArray.Length - offsetIn, insertLineBreaks);
if (offsetOut > outArray.Length - outlen)
throw new ArgumentOutOfRangeException(nameof(offsetOut), SR.ArgumentOutOfRange_OffsetOut);
unsafe
{
fixed (byte* inPtr = inArray)
fixed (char* outPtr = outArray)
{
ConvertToHexArray(outPtr + offsetOut, outlen, inPtr + offsetIn, length, options);
}
}
return outlen;
}
/// <summary>
/// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with hex characters.
/// A parameter specifies whether to insert line breaks in the return value and whether to insert upper- or lowercase hex characters.
/// </summary>
/// <param name="inArray">An array of 8-bit unsigned integers.</param>
/// <param name="options"><see cref="HexFormattingOptions.Lowercase"/> to produce lowercase output. <see cref="HexFormattingOptions.InsertLineBreaks"/> to insert a line break every 72 characters. <see cref="HexFormattingOptions.None"/> to do neither.</param>
/// <returns>The string representation in hex of the elements in <paramref name="inArray"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="inArray"/> is <code>null</code>.</exception>
/// <exception cref="ArgumentException"><paramref name="options"/> is not a valid <see cref="HexFormattingOptions"/> value.</exception>
public static string ToHexString(byte[] inArray, HexFormattingOptions options = default)
=> ToHexString(inArray, 0, inArray?.Length ?? 0, options);
/// <summary>
/// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with hex characters.
/// Parameters specify the subset as an offset in the input array, the number of elements in the array to convert,
/// whether to insert line breaks in the return value, and whether to insert upper- or lowercase hex characters.
/// </summary>
/// <param name="inArray">An array of 8-bit unsigned integers.</param>
/// <param name="offset">An offset in <paramref name="inArray"/>.</param>
/// <param name="length">The number of elements of <paramref name="inArray"/> to convert.</param>
/// <param name="options"><see cref="HexFormattingOptions.Lowercase"/> to produce lowercase output. <see cref="HexFormattingOptions.InsertLineBreaks"/> to insert a line break every 72 characters. <see cref="HexFormattingOptions.None"/> to do neither.</param>
/// <returns>The string representation in hex of <paramref name="length"/> elements of <paramref name="inArray"/>, starting at position <paramref name="offset"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="inArray"/> is <code>null</code>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="length"/> is negative.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> plus <paramref name="length"/> is greater than the length of <paramref name="inArray"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="options"/> is not a valid <see cref="HexFormattingOptions"/> value.</exception>
public static string ToHexString(byte[] inArray, int offset, int length, HexFormattingOptions options = default)
{
if (inArray == null)
throw new ArgumentNullException(nameof(inArray));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_Index);
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_GenericPositive);
if (offset > (inArray.Length - length))
throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_OffsetLength);
if (options < HexFormattingOptions.None || options > (HexFormattingOptions.Lowercase | HexFormattingOptions.InsertLineBreaks))
throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options));
if (length == 0)
return string.Empty;
var insertLineBreaks = (options & HexFormattingOptions.InsertLineBreaks) != 0;
var outlen = ToHex_CalculateAndValidateOutputLength(inArray.Length - offset, insertLineBreaks);
#if SPAN
return string.Create(outlen, (arr: inArray, off: offset, len: length, opt: options), (span, state) =>
{
unsafe
{
fixed (byte* inPtr = state.arr)
fixed (char* outPtr = &MemoryMarshal.GetReference(span))
{
ConvertToHexArray(outPtr, span.Length, inPtr + state.off, state.len, state.opt);
}
}
});
#else
var result = new string('\0', outlen);
unsafe
{
fixed (byte* inPtr = inArray)
fixed (char* outPtr = result)
{
ConvertToHexArray(outPtr, outlen, inPtr + offset, length, options);
}
}
return result;
#endif
}
#if SPAN
/// <summary>
/// Converts the span, which encodes binary data as hex characters, to an equivalent 8-bit unsigned integer array.
/// </summary>
/// <param name="chars">The span to convert.</param>
/// <returns>An array of 8-bit unsigned integers that is equivalent to <paramref name="chars"/>.</returns>
/// <exception cref="FormatException">The length of <paramref name="chars"/>, ignoring white-space characters, is not zero or a multiple of 2.</exception>
/// <exception cref="FormatException">The format of <paramref name="chars"/> is invalid. <paramref name="chars"/> contains a non-hex character.</exception>
public static byte[] FromHexString(ReadOnlySpan<char> chars)
{
if (chars.Length == 0)
return Array.Empty<byte>();
unsafe
{
fixed (char* inPtr = &MemoryMarshal.GetReference(chars))
{
var resultLength = FromHex_ComputeResultLength(inPtr, chars.Length);
#if GC_ALLOC_UNINIT
var result = GC.AllocateUninitializedArray<byte>(resultLength);
#else
var result = new byte[resultLength];
#endif
fixed (byte* outPtr = result)
{
var res = ConvertFromHexArray(outPtr, result.Length, inPtr, chars.Length);
if (res < 0)
throw new FormatException(SR.Format_BadHexChar);
Debug.Assert(res == result.Length);
}
return result;
}
}
}
/// <summary>
/// Converts a span of 8-bit unsigned integers to its equivalent string representation that is encoded with hex characters.
/// A parameter specifies whether to insert line breaks in the return value and whether to insert upper- or lowercase hex characters.
/// </summary>
/// <param name="bytes">A span of 8-bit unsigned integers.</param>
/// <param name="options"><see cref="HexFormattingOptions.Lowercase"/> to produce lowercase output. <see cref="HexFormattingOptions.InsertLineBreaks"/> to insert a line break every 72 characters. <see cref="HexFormattingOptions.None"/> to do neither.</param>
/// <returns>The string representation in hex of the elements in <paramref name="bytes"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="options"/> is not a valid <see cref="HexFormattingOptions"/> value.</exception>
public static string ToHexString(ReadOnlySpan<byte> bytes, HexFormattingOptions options = default)
{
if (options < HexFormattingOptions.None || options > (HexFormattingOptions.Lowercase | HexFormattingOptions.InsertLineBreaks))
throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options));
if (bytes.Length == 0)
return string.Empty;
var insertLineBreaks = (options & HexFormattingOptions.InsertLineBreaks) != 0;
var outlen = ToHex_CalculateAndValidateOutputLength(bytes.Length, insertLineBreaks);
unsafe
{
fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
{
return string.Create(outlen, (Bytes: (IntPtr) bytesPtr, bytes.Length, Options: options), (span, state) =>
{
fixed (char* outPtr = &MemoryMarshal.GetReference(span))
{
ConvertToHexArray(outPtr, span.Length, (byte*) state.Bytes, state.Length, state.Options);
}
});
}
}
}
/// <summary>
/// Converts the span, which encodes binary data as hex characters, into a span of equivalent 8-bit unsigned integers.
/// </summary>
/// <param name="chars">The span to convert.</param>
/// <param name="bytes">The output span.</param>
/// <param name="bytesWritten">The number of bytes written into <see cref="bytes"/>.</param>
/// <returns>Whether the conversion operation completed successfully.</returns>
public static bool TryFromHexChars(ReadOnlySpan<char> chars, Span<byte> bytes, out int bytesWritten)
{
if (chars.Length == 0)
{
bytesWritten = 0;
return true;
}
unsafe
{
fixed (char* inPtr = &MemoryMarshal.GetReference(chars))
fixed (byte* outPtr = &MemoryMarshal.GetReference(bytes))
{
var res = ConvertFromHexArray(outPtr, bytes.Length, inPtr, chars.Length);
if (res < 0)
{
bytesWritten = 0;
return false;
}
bytesWritten = res;
return true;
}
}
}
/// <summary>
/// Converts the specified string, which encodes binary data as hex characters, into a span of equivalent 8-bit unsigned integers.
/// </summary>
/// <param name="s">The string to convert.</param>
/// <param name="bytes">The output span.</param>
/// <param name="bytesWritten">The number of bytes written into <see cref="bytes"/>.</param>
/// <returns>Whether the conversion operation completed successfully.</returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> is <code>null</code>.</exception>
public static bool TryFromHexString(string s, Span<byte> bytes, out int bytesWritten)
{
if (s == null)
throw new ArgumentNullException(nameof(s));
return TryFromHexChars(s.AsSpan(), bytes, out bytesWritten);
}
/// <summary>
/// Converts a span of 8-bit unsigned integers to an equivalent span of a Unicode characters encoded with hex characters.
/// A parameter specifies whether to insert line breaks in the return value and whether to insert upper- or lowercase hex characters.
/// </summary>
/// <param name="bytes">The input span.</param>
/// <param name="chars">The output span</param>
/// <param name="charsWritten">The number of bytes written into <see cref="chars"/>.</param>
/// <param name="options"><see cref="HexFormattingOptions.Lowercase"/> to produce lowercase output. <see cref="HexFormattingOptions.InsertLineBreaks"/> to insert a line break every 72 characters. <see cref="HexFormattingOptions.None"/> to do neither.</param>
/// <returns>Whether the conversion operation completed successfully.</returns>
/// <exception cref="ArgumentException"><paramref name="options"/> is not a valid <see cref="HexFormattingOptions"/> value.</exception>
public static bool TryToHexChars(ReadOnlySpan<byte> bytes, Span<char> chars, out int charsWritten, HexFormattingOptions options = default)
{
if (options < HexFormattingOptions.None || options > (HexFormattingOptions.Lowercase | HexFormattingOptions.InsertLineBreaks))
throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options));
if (bytes.Length == 0)
{
charsWritten = 0;
return true;
}
var insertLineBreaks = (options & HexFormattingOptions.InsertLineBreaks) != 0;
var outlen = ToHex_CalculateAndValidateOutputLength(bytes.Length, insertLineBreaks);
if (chars.Length < outlen)
{
charsWritten = 0;
return false;
}
unsafe
{
fixed (byte* inPtr = &MemoryMarshal.GetReference(bytes))
fixed (char* outPtr = &MemoryMarshal.GetReference(chars))
{
ConvertToHexArray(outPtr, chars.Length, inPtr, bytes.Length, options);
}
}
charsWritten = outlen;
return true;
}
#endif
private const int hexLineBreakPosition = 72;
private const int errInvalidData = -1;
private const int errDestTooShort = -2;
private static int ToHex_CalculateAndValidateOutputLength(int inputLength, bool insertLineBreaks)
{
var outlen = (long) inputLength * 2;
if (outlen == 0)
return 0;
if (insertLineBreaks)
{
var newLines = outlen / hexLineBreakPosition;
if ((outlen % hexLineBreakPosition) == 0)
{
--newLines;
}
outlen += newLines * 2; // the number of line break chars we'll add, "\r\n"
}
// If we overflow an int then we cannot allocate enough
// memory to output the value so throw
if (outlen > int.MaxValue)
throw new OutOfMemoryException();
return (int)outlen;
}
private static unsafe void ConvertToHexArray(char* outChars, int outLength, byte* inData, int inLength, HexFormattingOptions options)
{
const int perLine = hexLineBreakPosition / 2;
var remaining = inLength;
var src = inData + inLength;
var dest = outChars + outLength;
var toLower = (options & HexFormattingOptions.Lowercase) != 0;
var insertLineBreaks = (options & HexFormattingOptions.InsertLineBreaks) != 0;
if (!insertLineBreaks)
{
#if INTRINSICS
if (Avx2.IsSupported & remaining >= 32)
{
remaining -= Utf16HexFormatter.Avx2.Format(ref src, ref dest, remaining, toLower);
Debug.Assert(remaining < 32);
}
if (remaining >= 16)
{
if (Ssse3.IsSupported)
{
remaining -= Utf16HexFormatter.Ssse3.Format(ref src, ref dest, remaining, toLower);
Debug.Assert(remaining < 16);
}
else if (Sse2.IsSupported)
{
remaining -= Utf16HexFormatter.Sse2.Format(ref src, ref dest, remaining, toLower);
Debug.Assert(remaining < 16);
}
}
#endif
if (remaining > 0)
{
remaining -= Utf16HexFormatter.Fixed.Format(ref src, ref dest, remaining, toLower);
}
Debug.Assert(remaining == 0);
}
else
{
var remainder = remaining % perLine;
var remainingOnCurrentLine = remainder == 0 ? perLine : remainder;
var bytesInIteration = remainingOnCurrentLine;
while (remaining != 0)
{
#if INTRINSICS
if (Avx2.IsSupported)
{
if (remainingOnCurrentLine >= 32)
{
remainingOnCurrentLine -= Utf16HexFormatter.Avx2.Format(ref src, ref dest, remainingOnCurrentLine, toLower);
Debug.Assert(remainingOnCurrentLine < 32);
}
}
else if (remainingOnCurrentLine >= 16)
{
// Never run SSE code if AVX2 is available, as the remaining 8 bytes per line cannot be SSE-processed
if (Ssse3.IsSupported)
{
remainingOnCurrentLine -= Utf16HexFormatter.Ssse3.Format(ref src, ref dest, remainingOnCurrentLine, toLower);
Debug.Assert(remainingOnCurrentLine < 16);
}
else if (Sse2.IsSupported)
{
remainingOnCurrentLine -= Utf16HexFormatter.Sse2.Format(ref src, ref dest, remainingOnCurrentLine, toLower);
Debug.Assert(remainingOnCurrentLine < 16);
}
}
#endif
if (remainingOnCurrentLine > 0)
{
remainingOnCurrentLine -= Utf16HexFormatter.Fixed.Format(ref src, ref dest, remainingOnCurrentLine, toLower);
}
Debug.Assert(remainingOnCurrentLine == 0);
remaining -= bytesInIteration;
if (remaining == 0) return;
*(--dest) = '\n';
*(--dest) = '\r';
remainingOnCurrentLine = perLine;
bytesInIteration = remainingOnCurrentLine;
}
}
}
private static bool IsWhitespace(int character)
=> character == ' ' || character == '\n' || character == '\r' || character == '\t';
private static bool IsValid(uint character)
{
character -= '0';
if (character > 9) goto Invalid;
character -= 'A' - '0';
if (character > 5) goto Invalid;
character -= 'a' - 'A';
if (character > 5) goto Invalid;
return true;
Invalid:
return false;
}
private static unsafe int FromHex_ComputeResultLength(char* inputPtr, int inputLength)
=> CharHelper.CountUsefulCharacters(inputPtr, inputLength) / 2;
private static unsafe int ConvertFromHexArray(byte* outData, int outLength, char* inChars, int inLength)
{
Debug.Assert(inLength != 0);
if (inLength == 1)
{
return errInvalidData;
}
var charsToRead = inLength;
var remainingIn = charsToRead;
var bytesToWrite = outLength;
var remainingOut = bytesToWrite;
var src = inChars;
var last = src;
var srcEnd = src + inLength;
var dest = outData;
while (remainingOut > 0)
{
#if INTRINSICS
// Ignore errors in the SIMD part, and handle them in scalar part below
if (Avx2.IsSupported && remainingIn >= 64 && remainingOut >= 32)
{
Utf16HexParser.Avx2.TryParse(ref src, ref dest, remainingOut);
remainingIn = charsToRead - (int) (src - inChars);
remainingOut = bytesToWrite - (int) (dest - outData);
}
if (remainingIn >= 32 && remainingOut >= 16)
{
if (Ssse3.IsSupported)
{
Utf16HexParser.Ssse3.TryParse(ref src, ref dest, remainingOut);
remainingIn = charsToRead - (int) (src - inChars);
remainingOut = bytesToWrite - (int) (dest - outData);
}
else if (Sse2.IsSupported)
{
Utf16HexParser.Sse2.TryParse(ref src, ref dest, remainingOut);
remainingIn = charsToRead - (int) (src - inChars);
remainingOut = bytesToWrite - (int) (dest - outData);
}
}
#endif
#if SPAN
if (!Utf16HexParser.Span.TryParse(ref src, remainingIn, ref dest, remainingOut))
#else
if (!Utf16HexParser.Fixed.TryParse(ref src, remainingIn, ref dest, remainingOut))
#endif
{
goto InvalidData;
}
remainingIn = charsToRead - (int) (src - inChars);
remainingOut = bytesToWrite - (int) (dest - outData);
if (src >= srcEnd)
{
goto Ok;
}
if (src == last)
{
// No progress == invalid data
goto InvalidData;
}
last = src;
}
// No more space in destination
if (src < srcEnd)
{
while (IsWhitespace(*src))
{
// Skip past any consecutive trailing white-space in the input
if (++src >= srcEnd)
{
goto Ok;
}
}
if (IsValid(*src))
{
goto OutLenTooShort;
}
else
{
goto InvalidData;
}
}
Ok:
return (int) (dest - outData);
InvalidData:
return errInvalidData;
OutLenTooShort:
return errDestTooShort;
}
}
}