diff options
Diffstat (limited to 'core/src/main/java/org/bouncycastle/util/encoders')
13 files changed, 0 insertions, 1343 deletions
diff --git a/core/src/main/java/org/bouncycastle/util/encoders/Base64.java b/core/src/main/java/org/bouncycastle/util/encoders/Base64.java deleted file mode 100644 index c04a8cc1..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/Base64.java +++ /dev/null @@ -1,154 +0,0 @@ -package org.bouncycastle.util.encoders; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -import org.bouncycastle.util.Strings; - -/** - * Utility class for converting Base64 data to bytes and back again. - */ -public class Base64 -{ - private static final Encoder encoder = new Base64Encoder(); - - public static String toBase64String( - byte[] data) - { - return toBase64String(data, 0, data.length); - } - - public static String toBase64String( - byte[] data, - int off, - int length) - { - byte[] encoded = encode(data, off, length); - return Strings.fromByteArray(encoded); - } - - /** - * encode the input data producing a base 64 encoded byte array. - * - * @return a byte array containing the base 64 encoded data. - */ - public static byte[] encode( - byte[] data) - { - return encode(data, 0, data.length); - } - - /** - * encode the input data producing a base 64 encoded byte array. - * - * @return a byte array containing the base 64 encoded data. - */ - public static byte[] encode( - byte[] data, - int off, - int length) - { - int len = (length + 2) / 3 * 4; - ByteArrayOutputStream bOut = new ByteArrayOutputStream(len); - - try - { - encoder.encode(data, off, length, bOut); - } - catch (Exception e) - { - throw new EncoderException("exception encoding base64 string: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * Encode the byte data to base 64 writing it to the given output stream. - * - * @return the number of bytes produced. - */ - public static int encode( - byte[] data, - OutputStream out) - throws IOException - { - return encoder.encode(data, 0, data.length, out); - } - - /** - * Encode the byte data to base 64 writing it to the given output stream. - * - * @return the number of bytes produced. - */ - public static int encode( - byte[] data, - int off, - int length, - OutputStream out) - throws IOException - { - return encoder.encode(data, off, length, out); - } - - /** - * decode the base 64 encoded input data. It is assumed the input data is valid. - * - * @return a byte array representing the decoded data. - */ - public static byte[] decode( - byte[] data) - { - int len = data.length / 4 * 3; - ByteArrayOutputStream bOut = new ByteArrayOutputStream(len); - - try - { - encoder.decode(data, 0, data.length, bOut); - } - catch (Exception e) - { - throw new DecoderException("unable to decode base64 data: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * decode the base 64 encoded String data - whitespace will be ignored. - * - * @return a byte array representing the decoded data. - */ - public static byte[] decode( - String data) - { - int len = data.length() / 4 * 3; - ByteArrayOutputStream bOut = new ByteArrayOutputStream(len); - - try - { - encoder.decode(data, bOut); - } - catch (Exception e) - { - throw new DecoderException("unable to decode base64 string: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * decode the base 64 encoded String data writing it to the given output stream, - * whitespace characters will be ignored. - * - * @return the number of bytes produced. - */ - public static int decode( - String data, - OutputStream out) - throws IOException - { - return encoder.decode(data, out); - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/Base64Encoder.java b/core/src/main/java/org/bouncycastle/util/encoders/Base64Encoder.java deleted file mode 100644 index abad02c5..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/Base64Encoder.java +++ /dev/null @@ -1,331 +0,0 @@ -package org.bouncycastle.util.encoders; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * A streaming Base64 encoder. - */ -public class Base64Encoder - implements Encoder -{ - protected final byte[] encodingTable = - { - (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', - (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', - (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', - (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', - (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', - (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', - (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', - (byte)'v', - (byte)'w', (byte)'x', (byte)'y', (byte)'z', - (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', - (byte)'7', (byte)'8', (byte)'9', - (byte)'+', (byte)'/' - }; - - protected byte padding = (byte)'='; - - /* - * set up the decoding table. - */ - protected final byte[] decodingTable = new byte[128]; - - protected void initialiseDecodingTable() - { - for (int i = 0; i < decodingTable.length; i++) - { - decodingTable[i] = (byte)0xff; - } - - for (int i = 0; i < encodingTable.length; i++) - { - decodingTable[encodingTable[i]] = (byte)i; - } - } - - public Base64Encoder() - { - initialiseDecodingTable(); - } - - /** - * encode the input data producing a base 64 output stream. - * - * @return the number of bytes produced. - */ - public int encode( - byte[] data, - int off, - int length, - OutputStream out) - throws IOException - { - int modulus = length % 3; - int dataLength = (length - modulus); - int a1, a2, a3; - - for (int i = off; i < off + dataLength; i += 3) - { - a1 = data[i] & 0xff; - a2 = data[i + 1] & 0xff; - a3 = data[i + 2] & 0xff; - - out.write(encodingTable[(a1 >>> 2) & 0x3f]); - out.write(encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]); - out.write(encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f]); - out.write(encodingTable[a3 & 0x3f]); - } - - /* - * process the tail end. - */ - int b1, b2, b3; - int d1, d2; - - switch (modulus) - { - case 0: /* nothing left to do */ - break; - case 1: - d1 = data[off + dataLength] & 0xff; - b1 = (d1 >>> 2) & 0x3f; - b2 = (d1 << 4) & 0x3f; - - out.write(encodingTable[b1]); - out.write(encodingTable[b2]); - out.write(padding); - out.write(padding); - break; - case 2: - d1 = data[off + dataLength] & 0xff; - d2 = data[off + dataLength + 1] & 0xff; - - b1 = (d1 >>> 2) & 0x3f; - b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f; - b3 = (d2 << 2) & 0x3f; - - out.write(encodingTable[b1]); - out.write(encodingTable[b2]); - out.write(encodingTable[b3]); - out.write(padding); - break; - } - - return (dataLength / 3) * 4 + ((modulus == 0) ? 0 : 4); - } - - private boolean ignore( - char c) - { - return (c == '\n' || c =='\r' || c == '\t' || c == ' '); - } - - /** - * decode the base 64 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, - OutputStream out) - throws IOException - { - byte b1, b2, b3, b4; - int outLen = 0; - - int end = off + length; - - while (end > off) - { - if (!ignore((char)data[end - 1])) - { - break; - } - - end--; - } - - int i = off; - int finish = end - 4; - - i = nextI(data, i, finish); - - while (i < finish) - { - b1 = decodingTable[data[i++]]; - - i = nextI(data, i, finish); - - b2 = decodingTable[data[i++]]; - - i = nextI(data, i, finish); - - b3 = decodingTable[data[i++]]; - - i = nextI(data, i, finish); - - b4 = decodingTable[data[i++]]; - - if ((b1 | b2 | b3 | b4) < 0) - { - throw new IOException("invalid characters encountered in base64 data"); - } - - out.write((b1 << 2) | (b2 >> 4)); - out.write((b2 << 4) | (b3 >> 2)); - out.write((b3 << 6) | b4); - - outLen += 3; - - i = nextI(data, i, finish); - } - - outLen += decodeLastBlock(out, (char)data[end - 4], (char)data[end - 3], (char)data[end - 2], (char)data[end - 1]); - - return outLen; - } - - private int nextI(byte[] data, int i, int finish) - { - while ((i < finish) && ignore((char)data[i])) - { - i++; - } - return i; - } - - /** - * decode the base 64 encoded String data writing it to the given output stream, - * whitespace characters will be ignored. - * - * @return the number of bytes produced. - */ - public int decode( - String data, - OutputStream out) - throws IOException - { - byte b1, b2, b3, b4; - int length = 0; - - int end = data.length(); - - while (end > 0) - { - if (!ignore(data.charAt(end - 1))) - { - break; - } - - end--; - } - - int i = 0; - int finish = end - 4; - - i = nextI(data, i, finish); - - while (i < finish) - { - b1 = decodingTable[data.charAt(i++)]; - - i = nextI(data, i, finish); - - b2 = decodingTable[data.charAt(i++)]; - - i = nextI(data, i, finish); - - b3 = decodingTable[data.charAt(i++)]; - - i = nextI(data, i, finish); - - b4 = decodingTable[data.charAt(i++)]; - - if ((b1 | b2 | b3 | b4) < 0) - { - throw new IOException("invalid characters encountered in base64 data"); - } - - out.write((b1 << 2) | (b2 >> 4)); - out.write((b2 << 4) | (b3 >> 2)); - out.write((b3 << 6) | b4); - - length += 3; - - i = nextI(data, i, finish); - } - - length += decodeLastBlock(out, data.charAt(end - 4), data.charAt(end - 3), data.charAt(end - 2), data.charAt(end - 1)); - - return length; - } - - private int decodeLastBlock(OutputStream out, char c1, char c2, char c3, char c4) - throws IOException - { - byte b1, b2, b3, b4; - - if (c3 == padding) - { - b1 = decodingTable[c1]; - b2 = decodingTable[c2]; - - if ((b1 | b2) < 0) - { - throw new IOException("invalid characters encountered at end of base64 data"); - } - - out.write((b1 << 2) | (b2 >> 4)); - - return 1; - } - else if (c4 == padding) - { - b1 = decodingTable[c1]; - b2 = decodingTable[c2]; - b3 = decodingTable[c3]; - - if ((b1 | b2 | b3) < 0) - { - throw new IOException("invalid characters encountered at end of base64 data"); - } - - out.write((b1 << 2) | (b2 >> 4)); - out.write((b2 << 4) | (b3 >> 2)); - - return 2; - } - else - { - b1 = decodingTable[c1]; - b2 = decodingTable[c2]; - b3 = decodingTable[c3]; - b4 = decodingTable[c4]; - - if ((b1 | b2 | b3 | b4) < 0) - { - throw new IOException("invalid characters encountered at end of base64 data"); - } - - out.write((b1 << 2) | (b2 >> 4)); - out.write((b2 << 4) | (b3 >> 2)); - out.write((b3 << 6) | b4); - - return 3; - } - } - - private int nextI(String data, int i, int finish) - { - while ((i < finish) && ignore(data.charAt(i))) - { - i++; - } - return i; - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/BufferedDecoder.java b/core/src/main/java/org/bouncycastle/util/encoders/BufferedDecoder.java deleted file mode 100644 index eea85b9f..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/BufferedDecoder.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.bouncycastle.util.encoders; - - -/** - * A buffering class to allow translation from one format to another to - * be done in discrete chunks. - */ -public class BufferedDecoder -{ - protected byte[] buf; - protected int bufOff; - - protected Translator translator; - - /** - * @param translator the translator to use. - * @param bufSize amount of input to buffer for each chunk. - */ - public BufferedDecoder( - Translator translator, - int bufSize) - { - this.translator = translator; - - if ((bufSize % translator.getEncodedBlockSize()) != 0) - { - throw new IllegalArgumentException("buffer size not multiple of input block size"); - } - - buf = new byte[bufSize]; - bufOff = 0; - } - - public int processByte( - byte in, - byte[] out, - int outOff) - { - int resultLen = 0; - - buf[bufOff++] = in; - - if (bufOff == buf.length) - { - resultLen = translator.decode(buf, 0, buf.length, out, outOff); - bufOff = 0; - } - - return resultLen; - } - - public int processBytes( - byte[] in, - int inOff, - int len, - byte[] out, - int outOff) - { - if (len < 0) - { - throw new IllegalArgumentException("Can't have a negative input length!"); - } - - int resultLen = 0; - int gapLen = buf.length - bufOff; - - if (len > gapLen) - { - System.arraycopy(in, inOff, buf, bufOff, gapLen); - - resultLen += translator.decode(buf, 0, buf.length, out, outOff); - - bufOff = 0; - - len -= gapLen; - inOff += gapLen; - outOff += resultLen; - - int chunkSize = len - (len % buf.length); - - resultLen += translator.decode(in, inOff, chunkSize, out, outOff); - - len -= chunkSize; - inOff += chunkSize; - } - - if (len != 0) - { - System.arraycopy(in, inOff, buf, bufOff, len); - - bufOff += len; - } - - return resultLen; - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/BufferedEncoder.java b/core/src/main/java/org/bouncycastle/util/encoders/BufferedEncoder.java deleted file mode 100644 index 60a098de..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/BufferedEncoder.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.bouncycastle.util.encoders; - - -/** - * A buffering class to allow translation from one format to another to - * be done in discrete chunks. - */ -public class BufferedEncoder -{ - protected byte[] buf; - protected int bufOff; - - protected Translator translator; - - /** - * @param translator the translator to use. - * @param bufSize amount of input to buffer for each chunk. - */ - public BufferedEncoder( - Translator translator, - int bufSize) - { - this.translator = translator; - - if ((bufSize % translator.getEncodedBlockSize()) != 0) - { - throw new IllegalArgumentException("buffer size not multiple of input block size"); - } - - buf = new byte[bufSize]; - bufOff = 0; - } - - public int processByte( - byte in, - byte[] out, - int outOff) - { - int resultLen = 0; - - buf[bufOff++] = in; - - if (bufOff == buf.length) - { - resultLen = translator.encode(buf, 0, buf.length, out, outOff); - bufOff = 0; - } - - return resultLen; - } - - public int processBytes( - byte[] in, - int inOff, - int len, - byte[] out, - int outOff) - { - if (len < 0) - { - throw new IllegalArgumentException("Can't have a negative input length!"); - } - - int resultLen = 0; - int gapLen = buf.length - bufOff; - - if (len > gapLen) - { - System.arraycopy(in, inOff, buf, bufOff, gapLen); - - resultLen += translator.encode(buf, 0, buf.length, out, outOff); - - bufOff = 0; - - len -= gapLen; - inOff += gapLen; - outOff += resultLen; - - int chunkSize = len - (len % buf.length); - - resultLen += translator.encode(in, inOff, chunkSize, out, outOff); - - len -= chunkSize; - inOff += chunkSize; - } - - if (len != 0) - { - System.arraycopy(in, inOff, buf, bufOff, len); - - bufOff += len; - } - - return resultLen; - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/DecoderException.java b/core/src/main/java/org/bouncycastle/util/encoders/DecoderException.java deleted file mode 100644 index 1e6782a5..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/DecoderException.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.bouncycastle.util.encoders; - -/** - * Exception thrown if an attempt is made to decode invalid data, or some other failure occurs. - */ -public class DecoderException - extends IllegalStateException -{ - private Throwable cause; - - DecoderException(String msg, Throwable cause) - { - super(msg); - - this.cause = cause; - } - - public Throwable getCause() - { - return cause; - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/Encoder.java b/core/src/main/java/org/bouncycastle/util/encoders/Encoder.java deleted file mode 100644 index b0661210..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/Encoder.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.bouncycastle.util.encoders; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * Encode and decode byte arrays (typically from binary to 7-bit ASCII - * encodings). - */ -public interface Encoder -{ - int encode(byte[] data, int off, int length, OutputStream out) throws IOException; - - int decode(byte[] data, int off, int length, OutputStream out) throws IOException; - - int decode(String data, OutputStream out) throws IOException; -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/EncoderException.java b/core/src/main/java/org/bouncycastle/util/encoders/EncoderException.java deleted file mode 100644 index a1eb411e..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/EncoderException.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.bouncycastle.util.encoders; - -/** - * Exception thrown if an attempt is made to encode invalid data, or some other failure occurs. - */ -public class EncoderException - extends IllegalStateException -{ - private Throwable cause; - - EncoderException(String msg, Throwable cause) - { - super(msg); - - this.cause = cause; - } - - public Throwable getCause() - { - return cause; - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/Hex.java b/core/src/main/java/org/bouncycastle/util/encoders/Hex.java deleted file mode 100644 index 63e9c713..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/Hex.java +++ /dev/null @@ -1,151 +0,0 @@ -package org.bouncycastle.util.encoders; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -import org.bouncycastle.util.Strings; - -/** - * Utility class for converting hex data to bytes and back again. - */ -public class Hex -{ - private static final Encoder encoder = new HexEncoder(); - - public static String toHexString( - byte[] data) - { - return toHexString(data, 0, data.length); - } - - public static String toHexString( - byte[] data, - int off, - int length) - { - byte[] encoded = encode(data, off, length); - return Strings.fromByteArray(encoded); - } - - /** - * encode the input data producing a Hex encoded byte array. - * - * @return a byte array containing the Hex encoded data. - */ - public static byte[] encode( - byte[] data) - { - return encode(data, 0, data.length); - } - - /** - * encode the input data producing a Hex encoded byte array. - * - * @return a byte array containing the Hex encoded data. - */ - public static byte[] encode( - byte[] data, - int off, - int length) - { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - - try - { - encoder.encode(data, off, length, bOut); - } - catch (Exception e) - { - throw new EncoderException("exception encoding Hex string: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * Hex encode the byte data writing it to the given output stream. - * - * @return the number of bytes produced. - */ - public static int encode( - byte[] data, - OutputStream out) - throws IOException - { - return encoder.encode(data, 0, data.length, out); - } - - /** - * Hex encode the byte data writing it to the given output stream. - * - * @return the number of bytes produced. - */ - public static int encode( - byte[] data, - int off, - int length, - OutputStream out) - throws IOException - { - return encoder.encode(data, off, length, out); - } - - /** - * decode the Hex encoded input data. It is assumed the input data is valid. - * - * @return a byte array representing the decoded data. - */ - public static byte[] decode( - byte[] data) - { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - - try - { - encoder.decode(data, 0, data.length, bOut); - } - catch (Exception e) - { - throw new DecoderException("exception decoding Hex data: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * decode the Hex encoded String data - whitespace will be ignored. - * - * @return a byte array representing the decoded data. - */ - public static byte[] decode( - String data) - { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - - try - { - encoder.decode(data, bOut); - } - catch (Exception e) - { - throw new DecoderException("exception decoding Hex string: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * 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 static int decode( - String data, - OutputStream out) - throws IOException - { - return encoder.decode(data, out); - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/HexEncoder.java b/core/src/main/java/org/bouncycastle/util/encoders/HexEncoder.java deleted file mode 100644 index 52f8fa6d..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/HexEncoder.java +++ /dev/null @@ -1,190 +0,0 @@ -package org.bouncycastle.util.encoders; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * A streaming Hex encoder. - */ -public class HexEncoder - implements Encoder -{ - protected final 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 final byte[] decodingTable = new byte[128]; - - protected void initialiseDecodingTable() - { - for (int i = 0; i < decodingTable.length; i++) - { - decodingTable[i] = (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, - OutputStream out) - throws IOException - { - for (int i = off; i < (off + length); i++) - { - int v = data[i] & 0xff; - - out.write(encodingTable[(v >>> 4)]); - out.write(encodingTable[v & 0xf]); - } - - return length * 2; - } - - private static boolean 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, - OutputStream out) - throws IOException - { - 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) < 0) - { - throw new IOException("invalid characters encountered in Hex data"); - } - - out.write((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 decode( - String data, - OutputStream out) - throws IOException - { - byte b1, b2; - int length = 0; - - int end = data.length(); - - while (end > 0) - { - if (!ignore(data.charAt(end - 1))) - { - break; - } - - end--; - } - - int i = 0; - while (i < end) - { - while (i < end && ignore(data.charAt(i))) - { - i++; - } - - b1 = decodingTable[data.charAt(i++)]; - - while (i < end && ignore(data.charAt(i))) - { - i++; - } - - b2 = decodingTable[data.charAt(i++)]; - - if ((b1 | b2) < 0) - { - throw new IOException("invalid characters encountered in Hex string"); - } - - out.write((b1 << 4) | b2); - - length++; - } - - return length; - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java b/core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java deleted file mode 100644 index 3fff65a6..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.bouncycastle.util.encoders; - -/** - * Converters for going from hex to binary and back. Note: this class assumes ASCII processing. - */ -public class HexTranslator - implements Translator -{ - private static final byte[] hexTable = - { - (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' - }; - - /** - * size of the output block on encoding produced by getDecodedBlockSize() - * bytes. - */ - public int getEncodedBlockSize() - { - return 2; - } - - public int encode( - byte[] in, - int inOff, - int length, - byte[] out, - int outOff) - { - for (int i = 0, j = 0; i < length; i++, j += 2) - { - out[outOff + j] = hexTable[(in[inOff] >> 4) & 0x0f]; - out[outOff + j + 1] = hexTable[in[inOff] & 0x0f]; - - inOff++; - } - - return length * 2; - } - - /** - * size of the output block on decoding produced by getEncodedBlockSize() - * bytes. - */ - public int getDecodedBlockSize() - { - return 1; - } - - public int decode( - byte[] in, - int inOff, - int length, - byte[] out, - int outOff) - { - int halfLength = length / 2; - byte left, right; - for (int i = 0; i < halfLength; i++) - { - left = in[inOff + i * 2]; - right = in[inOff + i * 2 + 1]; - - if (left < (byte)'a') - { - out[outOff] = (byte)((left - '0') << 4); - } - else - { - out[outOff] = (byte)((left - 'a' + 10) << 4); - } - if (right < (byte)'a') - { - out[outOff] += (byte)(right - '0'); - } - else - { - out[outOff] += (byte)(right - 'a' + 10); - } - - outOff++; - } - - return halfLength; - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/Translator.java b/core/src/main/java/org/bouncycastle/util/encoders/Translator.java deleted file mode 100644 index 96381bcf..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/Translator.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.bouncycastle.util.encoders; - -/** - * General interface for an translator. - */ -public interface Translator -{ - /** - * size of the output block on encoding produced by getDecodedBlockSize() - * bytes. - */ - public int getEncodedBlockSize(); - - public int encode(byte[] in, int inOff, int length, byte[] out, int outOff); - - /** - * size of the output block on decoding produced by getEncodedBlockSize() - * bytes. - */ - public int getDecodedBlockSize(); - - public int decode(byte[] in, int inOff, int length, byte[] out, int outOff); -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/UrlBase64.java b/core/src/main/java/org/bouncycastle/util/encoders/UrlBase64.java deleted file mode 100644 index 3b83e956..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/UrlBase64.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.bouncycastle.util.encoders; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -/** - * Convert binary data to and from UrlBase64 encoding. This is identical to - * Base64 encoding, except that the padding character is "." and the other - * non-alphanumeric characters are "-" and "_" instead of "+" and "/". - * <p> - * The purpose of UrlBase64 encoding is to provide a compact encoding of binary - * data that is safe for use as an URL parameter. Base64 encoding does not - * produce encoded values that are safe for use in URLs, since "/" can be - * interpreted as a path delimiter; "+" is the encoded form of a space; and - * "=" is used to separate a name from the corresponding value in an URL - * parameter. - */ -public class UrlBase64 -{ - private static final Encoder encoder = new UrlBase64Encoder(); - - /** - * Encode the input data producing a URL safe base 64 encoded byte array. - * - * @return a byte array containing the URL safe base 64 encoded data. - */ - public static byte[] encode( - byte[] data) - { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - - try - { - encoder.encode(data, 0, data.length, bOut); - } - catch (Exception e) - { - throw new EncoderException("exception encoding URL safe base64 data: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * Encode the byte data writing it to the given output stream. - * - * @return the number of bytes produced. - */ - public static int encode( - byte[] data, - OutputStream out) - throws IOException - { - return encoder.encode(data, 0, data.length, out); - } - - /** - * Decode the URL safe base 64 encoded input data - white space will be ignored. - * - * @return a byte array representing the decoded data. - */ - public static byte[] decode( - byte[] data) - { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - - try - { - encoder.decode(data, 0, data.length, bOut); - } - catch (Exception e) - { - throw new DecoderException("exception decoding URL safe base64 string: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * decode the URL safe base 64 encoded byte data writing it to the given output stream, - * whitespace characters will be ignored. - * - * @return the number of bytes produced. - */ - public static int decode( - byte[] data, - OutputStream out) - throws IOException - { - return encoder.decode(data, 0, data.length, out); - } - - /** - * decode the URL safe base 64 encoded String data - whitespace will be ignored. - * - * @return a byte array representing the decoded data. - */ - public static byte[] decode( - String data) - { - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - - try - { - encoder.decode(data, bOut); - } - catch (Exception e) - { - throw new DecoderException("exception decoding URL safe base64 string: " + e.getMessage(), e); - } - - return bOut.toByteArray(); - } - - /** - * Decode the URL safe base 64 encoded String data writing it to the given output stream, - * whitespace characters will be ignored. - * - * @return the number of bytes produced. - */ - public static int decode( - String data, - OutputStream out) - throws IOException - { - return encoder.decode(data, out); - } -} diff --git a/core/src/main/java/org/bouncycastle/util/encoders/UrlBase64Encoder.java b/core/src/main/java/org/bouncycastle/util/encoders/UrlBase64Encoder.java deleted file mode 100644 index a5fff5ec..00000000 --- a/core/src/main/java/org/bouncycastle/util/encoders/UrlBase64Encoder.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.bouncycastle.util.encoders; - -/** - * Convert binary data to and from UrlBase64 encoding. This is identical to - * Base64 encoding, except that the padding character is "." and the other - * non-alphanumeric characters are "-" and "_" instead of "+" and "/". - * <p> - * The purpose of UrlBase64 encoding is to provide a compact encoding of binary - * data that is safe for use as an URL parameter. Base64 encoding does not - * produce encoded values that are safe for use in URLs, since "/" can be - * interpreted as a path delimiter; "+" is the encoded form of a space; and - * "=" is used to separate a name from the corresponding value in an URL - * parameter. - */ -public class UrlBase64Encoder extends Base64Encoder -{ - public UrlBase64Encoder() - { - encodingTable[encodingTable.length - 2] = (byte) '-'; - encodingTable[encodingTable.length - 1] = (byte) '_'; - padding = (byte) '.'; - // we must re-create the decoding table with the new encoded values. - initialiseDecodingTable(); - } -} |