123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.IO;
- namespace Org.BouncyCastle.Utilities.Encoders
- {
- public class HexEncoder
- : IEncoder
- {
- protected readonly byte[] encodingTable =
- {
- (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
- (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
- };
- /*
- * set up the decoding table.
- */
- protected readonly byte[] decodingTable = new byte[128];
- protected void InitialiseDecodingTable()
- {
- Arrays.Fill(decodingTable, (byte)0xff);
- for (int i = 0; i < encodingTable.Length; i++)
- {
- decodingTable[encodingTable[i]] = (byte)i;
- }
- decodingTable['A'] = decodingTable['a'];
- decodingTable['B'] = decodingTable['b'];
- decodingTable['C'] = decodingTable['c'];
- decodingTable['D'] = decodingTable['d'];
- decodingTable['E'] = decodingTable['e'];
- decodingTable['F'] = decodingTable['f'];
- }
- public HexEncoder()
- {
- InitialiseDecodingTable();
- }
- /**
- * encode the input data producing a Hex output stream.
- *
- * @return the number of bytes produced.
- */
- public int Encode(
- byte[] data,
- int off,
- int length,
- Stream outStream)
- {
- for (int i = off; i < (off + length); i++)
- {
- int v = data[i];
- outStream.WriteByte(encodingTable[v >> 4]);
- outStream.WriteByte(encodingTable[v & 0xf]);
- }
- return length * 2;
- }
- private static bool Ignore(char c)
- {
- return c == '\n' || c =='\r' || c == '\t' || c == ' ';
- }
- /**
- * decode the Hex encoded byte data writing it to the given output stream,
- * whitespace characters will be ignored.
- *
- * @return the number of bytes produced.
- */
- public int Decode(
- byte[] data,
- int off,
- int length,
- Stream outStream)
- {
- byte b1, b2;
- int outLen = 0;
- int end = off + length;
- while (end > off)
- {
- if (!Ignore((char)data[end - 1]))
- {
- break;
- }
- end--;
- }
- int i = off;
- while (i < end)
- {
- while (i < end && Ignore((char)data[i]))
- {
- i++;
- }
- b1 = decodingTable[data[i++]];
- while (i < end && Ignore((char)data[i]))
- {
- i++;
- }
- b2 = decodingTable[data[i++]];
- if ((b1 | b2) >= 0x80)
- throw new IOException("invalid characters encountered in Hex data");
- outStream.WriteByte((byte)((b1 << 4) | b2));
- outLen++;
- }
- return outLen;
- }
- /**
- * decode the Hex encoded string data writing it to the given output stream,
- * whitespace characters will be ignored.
- *
- * @return the number of bytes produced.
- */
- public int DecodeString(
- string data,
- Stream outStream)
- {
- byte b1, b2;
- int length = 0;
- int end = data.Length;
- while (end > 0)
- {
- if (!Ignore(data[end - 1]))
- {
- break;
- }
- end--;
- }
- int i = 0;
- while (i < end)
- {
- while (i < end && Ignore(data[i]))
- {
- i++;
- }
- b1 = decodingTable[data[i++]];
- while (i < end && Ignore(data[i]))
- {
- i++;
- }
- b2 = decodingTable[data[i++]];
- if ((b1 | b2) >= 0x80)
- throw new IOException("invalid characters encountered in Hex data");
- outStream.WriteByte((byte)((b1 << 4) | b2));
- length++;
- }
- return length;
- }
- }
- }
- #endif
|