| | | 1 | | using System.IO; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities.Encoders |
| | | 4 | | { |
| | | 5 | | internal class HexEncoder |
| | | 6 | | { |
| | 1 | 7 | | protected readonly byte[] encodingTable = |
| | 1 | 8 | | { |
| | 1 | 9 | | (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', |
| | 1 | 10 | | (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' |
| | 1 | 11 | | }; |
| | | 12 | | |
| | | 13 | | /* |
| | | 14 | | * set up the decoding table. |
| | | 15 | | */ |
| | 1 | 16 | | protected readonly byte[] decodingTable = new byte[128]; |
| | | 17 | | |
| | | 18 | | protected void InitialiseDecodingTable() |
| | 1 | 19 | | { |
| | 1 | 20 | | Arrays.Fill(decodingTable, (byte)0xff); |
| | | 21 | | |
| | 34 | 22 | | for (int i = 0; i < encodingTable.Length; i++) |
| | 16 | 23 | | { |
| | 16 | 24 | | decodingTable[encodingTable[i]] = (byte)i; |
| | 16 | 25 | | } |
| | | 26 | | |
| | 1 | 27 | | decodingTable['A'] = decodingTable['a']; |
| | 1 | 28 | | decodingTable['B'] = decodingTable['b']; |
| | 1 | 29 | | decodingTable['C'] = decodingTable['c']; |
| | 1 | 30 | | decodingTable['D'] = decodingTable['d']; |
| | 1 | 31 | | decodingTable['E'] = decodingTable['e']; |
| | 1 | 32 | | decodingTable['F'] = decodingTable['f']; |
| | 1 | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | public HexEncoder() |
| | 1 | 36 | | { |
| | 1 | 37 | | InitialiseDecodingTable(); |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | /** |
| | | 41 | | * encode the input data producing a Hex output stream. |
| | | 42 | | * |
| | | 43 | | * @return the number of bytes produced. |
| | | 44 | | */ |
| | | 45 | | public int Encode( |
| | | 46 | | byte[] data, |
| | | 47 | | int off, |
| | | 48 | | int length, |
| | | 49 | | Stream outStream) |
| | 0 | 50 | | { |
| | 0 | 51 | | for (int i = off; i < (off + length); i++) |
| | 0 | 52 | | { |
| | 0 | 53 | | int v = data[i]; |
| | | 54 | | |
| | 0 | 55 | | outStream.WriteByte(encodingTable[v >> 4]); |
| | 0 | 56 | | outStream.WriteByte(encodingTable[v & 0xf]); |
| | 0 | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return length * 2; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | private static bool Ignore(char c) |
| | 1896 | 63 | | { |
| | 1896 | 64 | | return c == '\n' || c =='\r' || c == '\t' || c == ' '; |
| | 1896 | 65 | | } |
| | | 66 | | |
| | | 67 | | /** |
| | | 68 | | * decode the Hex encoded byte data writing it to the given output stream, |
| | | 69 | | * whitespace characters will be ignored. |
| | | 70 | | * |
| | | 71 | | * @return the number of bytes produced. |
| | | 72 | | */ |
| | | 73 | | public int Decode( |
| | | 74 | | byte[] data, |
| | | 75 | | int off, |
| | | 76 | | int length, |
| | | 77 | | Stream outStream) |
| | 0 | 78 | | { |
| | | 79 | | byte b1, b2; |
| | 0 | 80 | | int outLen = 0; |
| | 0 | 81 | | int end = off + length; |
| | | 82 | | |
| | 0 | 83 | | while (end > off) |
| | 0 | 84 | | { |
| | 0 | 85 | | if (!Ignore((char)data[end - 1])) |
| | 0 | 86 | | { |
| | 0 | 87 | | break; |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | end--; |
| | 0 | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | int i = off; |
| | 0 | 94 | | while (i < end) |
| | 0 | 95 | | { |
| | 0 | 96 | | while (i < end && Ignore((char)data[i])) |
| | 0 | 97 | | { |
| | 0 | 98 | | i++; |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | b1 = decodingTable[data[i++]]; |
| | | 102 | | |
| | 0 | 103 | | while (i < end && Ignore((char)data[i])) |
| | 0 | 104 | | { |
| | 0 | 105 | | i++; |
| | 0 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | b2 = decodingTable[data[i++]]; |
| | | 109 | | |
| | 0 | 110 | | if ((b1 | b2) >= 0x80) |
| | 0 | 111 | | throw new IOException("invalid characters encountered in Hex data"); |
| | | 112 | | |
| | 0 | 113 | | outStream.WriteByte((byte)((b1 << 4) | b2)); |
| | | 114 | | |
| | 0 | 115 | | outLen++; |
| | 0 | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | return outLen; |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | /** |
| | | 122 | | * decode the Hex encoded string data writing it to the given output stream, |
| | | 123 | | * whitespace characters will be ignored. |
| | | 124 | | * |
| | | 125 | | * @return the number of bytes produced. |
| | | 126 | | */ |
| | | 127 | | public int DecodeString( |
| | | 128 | | string data, |
| | | 129 | | Stream outStream) |
| | 18 | 130 | | { |
| | | 131 | | byte b1, b2; |
| | 18 | 132 | | int length = 0; |
| | | 133 | | |
| | 18 | 134 | | int end = data.Length; |
| | | 135 | | |
| | 18 | 136 | | while (end > 0) |
| | 18 | 137 | | { |
| | 18 | 138 | | if (!Ignore(data[end - 1])) |
| | 18 | 139 | | { |
| | 18 | 140 | | break; |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | end--; |
| | 0 | 144 | | } |
| | | 145 | | |
| | 18 | 146 | | int i = 0; |
| | 957 | 147 | | while (i < end) |
| | 939 | 148 | | { |
| | 939 | 149 | | while (i < end && Ignore(data[i])) |
| | 0 | 150 | | { |
| | 0 | 151 | | i++; |
| | 0 | 152 | | } |
| | | 153 | | |
| | 939 | 154 | | b1 = decodingTable[data[i++]]; |
| | | 155 | | |
| | 939 | 156 | | while (i < end && Ignore(data[i])) |
| | 0 | 157 | | { |
| | 0 | 158 | | i++; |
| | 0 | 159 | | } |
| | | 160 | | |
| | 939 | 161 | | b2 = decodingTable[data[i++]]; |
| | | 162 | | |
| | 939 | 163 | | if ((b1 | b2) >= 0x80) |
| | 0 | 164 | | throw new IOException("invalid characters encountered in Hex data"); |
| | | 165 | | |
| | 939 | 166 | | outStream.WriteByte((byte)((b1 << 4) | b2)); |
| | | 167 | | |
| | 939 | 168 | | length++; |
| | 939 | 169 | | } |
| | | 170 | | |
| | 18 | 171 | | return length; |
| | 18 | 172 | | } |
| | | 173 | | } |
| | | 174 | | } |