1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 namespace System.Xml
6 {
7     internal static partial class BinHexEncoder
8     {
9         private const string s_hexDigits = "0123456789ABCDEF";
10         private const int CharsChunkSize = 128;
11 
Encode(byte[] buffer, int index, int count, XmlWriter writer)12         internal static void Encode(byte[] buffer, int index, int count, XmlWriter writer)
13         {
14             if (buffer == null)
15             {
16                 throw new ArgumentNullException(nameof(buffer));
17             }
18             if (index < 0)
19             {
20                 throw new ArgumentOutOfRangeException(nameof(index));
21             }
22             if (count < 0)
23             {
24                 throw new ArgumentOutOfRangeException(nameof(count));
25             }
26             if (count > buffer.Length - index)
27             {
28                 throw new ArgumentOutOfRangeException(nameof(count));
29             }
30 
31             char[] chars = new char[(count * 2) < CharsChunkSize ? (count * 2) : CharsChunkSize];
32             int endIndex = index + count;
33             while (index < endIndex)
34             {
35                 int cnt = (count < CharsChunkSize / 2) ? count : CharsChunkSize / 2;
36                 int charCount = Encode(buffer, index, cnt, chars);
37                 writer.WriteRaw(chars, 0, charCount);
38                 index += cnt;
39                 count -= cnt;
40             }
41         }
42 
Encode(byte[] inArray, int offsetIn, int count)43         internal static string Encode(byte[] inArray, int offsetIn, int count)
44         {
45             if (null == inArray)
46             {
47                 throw new ArgumentNullException(nameof(inArray));
48             }
49             if (0 > offsetIn)
50             {
51                 throw new ArgumentOutOfRangeException(nameof(offsetIn));
52             }
53             if (0 > count)
54             {
55                 throw new ArgumentOutOfRangeException(nameof(count));
56             }
57             if (count > inArray.Length - offsetIn)
58             {
59                 throw new ArgumentOutOfRangeException(nameof(count));
60             }
61 
62             char[] outArray = new char[2 * count];
63             int lenOut = Encode(inArray, offsetIn, count, outArray);
64             return new String(outArray, 0, lenOut);
65         }
66 
Encode(byte[] inArray, int offsetIn, int count, char[] outArray)67         private static int Encode(byte[] inArray, int offsetIn, int count, char[] outArray)
68         {
69             int curOffsetOut = 0, offsetOut = 0;
70             byte b;
71             int lengthOut = outArray.Length;
72 
73             for (int j = 0; j < count; j++)
74             {
75                 b = inArray[offsetIn++];
76                 outArray[curOffsetOut++] = s_hexDigits[b >> 4];
77                 if (curOffsetOut == lengthOut)
78                 {
79                     break;
80                 }
81                 outArray[curOffsetOut++] = s_hexDigits[b & 0xF];
82                 if (curOffsetOut == lengthOut)
83                 {
84                     break;
85                 }
86             }
87             return curOffsetOut - offsetOut;
88         } // function
89     } // class
90 } // namespace
91