| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Text |
| | | 11 | | { |
| | | 12 | | internal class BinHexEncoding : Encoding |
| | | 13 | | { |
| | 0 | 14 | | private static byte[] s_char2val = new byte[128] |
| | 0 | 15 | | { |
| | 0 | 16 | | /* 0-15 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 17 | | /* 16-31 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 18 | | /* 32-47 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 19 | | /* 48-63 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 20 | | /* 64-79 */ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 21 | | /* 80-95 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 22 | | /* 96-111 */ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 23 | | /* 112-127 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| | 0 | 24 | | }; |
| | 0 | 25 | | private static string s_val2char = "0123456789ABCDEF"; |
| | | 26 | | |
| | | 27 | | #if DEBUG |
| | | 28 | | static BinHexEncoding() |
| | | 29 | | { |
| | | 30 | | for (char ch = '0'; ch <= '9'; ch++) |
| | | 31 | | { |
| | | 32 | | Fx.Assert(s_char2val[ch] == ch - '0', ""); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | for (char ch = 'A'; ch <= 'F'; ch++) |
| | | 36 | | { |
| | | 37 | | Fx.Assert(s_char2val[ch] == ch - 'A' + 10, ""); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | for (char ch = 'a'; ch <= 'f'; ch++) |
| | | 41 | | { |
| | | 42 | | Fx.Assert(s_char2val[ch] == ch - 'a' + 10, ""); |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | #endif |
| | | 46 | | |
| | | 47 | | public override int GetMaxByteCount(int charCount) |
| | | 48 | | { |
| | 0 | 49 | | if (charCount < 0) |
| | 0 | 50 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCou |
| | 0 | 51 | | if ((charCount % 2) != 0) |
| | 0 | 52 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.Format(SR.XmlInvalidBin |
| | 0 | 53 | | return charCount / 2; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public override int GetByteCount(char[] chars, int index, int count) |
| | | 57 | | { |
| | 0 | 58 | | return GetMaxByteCount(count); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) |
| | | 62 | | { |
| | 0 | 63 | | if (chars == null) |
| | 0 | 64 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(chars))); |
| | 0 | 65 | | if (charIndex < 0) |
| | 0 | 66 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd |
| | 0 | 67 | | if (charIndex > chars.Length) |
| | 0 | 68 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd |
| | 0 | 69 | | if (charCount < 0) |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCou |
| | 0 | 71 | | if (charCount > chars.Length - charIndex) |
| | 0 | 72 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCou |
| | 0 | 73 | | if (bytes == null) |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(bytes))); |
| | 0 | 75 | | if (byteIndex < 0) |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd |
| | 0 | 77 | | if (byteIndex > bytes.Length) |
| | 0 | 78 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd |
| | 0 | 79 | | int byteCount = GetByteCount(chars, charIndex, charCount); |
| | 0 | 80 | | if (byteCount < 0 || byteCount > bytes.Length - byteIndex) |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.XmlArrayTooSmall, nam |
| | 0 | 82 | | if (charCount > 0) |
| | | 83 | | { |
| | 0 | 84 | | fixed (byte* _char2val = s_char2val) |
| | 0 | 85 | | { |
| | 0 | 86 | | fixed (byte* _bytes = &bytes[byteIndex]) |
| | 0 | 87 | | { |
| | 0 | 88 | | fixed (char* _chars = &chars[charIndex]) |
| | | 89 | | { |
| | 0 | 90 | | char* pch = _chars; |
| | 0 | 91 | | char* pchMax = _chars + charCount; |
| | 0 | 92 | | byte* pb = _bytes; |
| | 0 | 93 | | while (pch < pchMax) |
| | | 94 | | { |
| | | 95 | | Fx.Assert(pch + 2 <= pchMax, ""); |
| | 0 | 96 | | char pch0 = pch[0]; |
| | 0 | 97 | | char pch1 = pch[1]; |
| | 0 | 98 | | if ((pch0 | pch1) >= 128) |
| | 0 | 99 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.For |
| | 0 | 100 | | byte d1 = _char2val[pch0]; |
| | 0 | 101 | | byte d2 = _char2val[pch1]; |
| | 0 | 102 | | if ((d1 | d2) == 0xFF) |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.For |
| | 0 | 104 | | pb[0] = (byte)((d1 << 4) + d2); |
| | 0 | 105 | | pch += 2; |
| | 0 | 106 | | pb++; |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | } |
| | 0 | 112 | | return byteCount; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public override int GetMaxCharCount(int byteCount) |
| | | 116 | | { |
| | 0 | 117 | | if (byteCount < 0 || byteCount > int.MaxValue / 2) |
| | 0 | 118 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCou |
| | 0 | 119 | | return byteCount * 2; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | public override int GetCharCount(byte[] bytes, int index, int count) |
| | | 123 | | { |
| | 0 | 124 | | return GetMaxCharCount(count); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) |
| | | 128 | | { |
| | 0 | 129 | | if (bytes == null) |
| | 0 | 130 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(bytes))); |
| | 0 | 131 | | if (byteIndex < 0) |
| | 0 | 132 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd |
| | 0 | 133 | | if (byteIndex > bytes.Length) |
| | 0 | 134 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd |
| | 0 | 135 | | if (byteCount < 0) |
| | 0 | 136 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCou |
| | 0 | 137 | | if (byteCount > bytes.Length - byteIndex) |
| | 0 | 138 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCou |
| | 0 | 139 | | int charCount = GetCharCount(bytes, byteIndex, byteCount); |
| | 0 | 140 | | if (chars == null) |
| | 0 | 141 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(chars))); |
| | 0 | 142 | | if (charIndex < 0) |
| | 0 | 143 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd |
| | 0 | 144 | | if (charIndex > chars.Length) |
| | 0 | 145 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd |
| | 0 | 146 | | if (charCount < 0 || charCount > chars.Length - charIndex) |
| | 0 | 147 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.XmlArrayTooSmall, "ch |
| | 0 | 148 | | if (byteCount > 0) |
| | 0 | 149 | | { |
| | 0 | 150 | | fixed (char* _val2char = s_val2char) |
| | 0 | 151 | | { |
| | 0 | 152 | | fixed (byte* _bytes = &bytes[byteIndex]) |
| | 0 | 153 | | { |
| | 0 | 154 | | fixed (char* _chars = &chars[charIndex]) |
| | | 155 | | { |
| | 0 | 156 | | char* pch = _chars; |
| | 0 | 157 | | byte* pb = _bytes; |
| | 0 | 158 | | byte* pbMax = _bytes + byteCount; |
| | 0 | 159 | | while (pb < pbMax) |
| | | 160 | | { |
| | 0 | 161 | | pch[0] = _val2char[pb[0] >> 4]; |
| | 0 | 162 | | pch[1] = _val2char[pb[0] & 0x0F]; |
| | 0 | 163 | | pb++; |
| | 0 | 164 | | pch += 2; |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | } |
| | 0 | 170 | | return charCount; |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | } |