点击(此处)折叠或打开
-
import java.math.BigInteger;
-
import java.util.Arrays;
-
-
-
public class SanyiDecode extends SanyiCodec {
-
-
private static final int BITS_PER_ENCODED_BYTE = 6;
-
private static final int BYTES_PER_UNENCODED_BLOCK = 3;
-
private static final int BYTES_PER_ENCODED_BLOCK = 4;
-
-
static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};
-
-
private static final byte[] STANDARD_ENCODE_TABLE = {
-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
-
};
-
-
private static final byte[] URL_SAFE_ENCODE_TABLE = {
-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
-
};
-
-
private static final byte[] DECODE_TABLE = {
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54,
-
55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
-
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
-
24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
-
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
-
};
-
-
private static final int MASK_6BITS = 0x3f;
-
-
private final byte[] encodeTable;
-
-
private final byte[] decodeTable = DECODE_TABLE;
-
private final byte[] lineSeparator;
-
-
private final int decodeSize;
-
-
private final int encodeSize;
-
-
public SanyiDecode() {
-
this(0);
-
}
-
-
public SanyiDecode(final boolean urlSafe) {
-
this(MIME_CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
-
}
-
public SanyiDecode(final int lineLength) {
-
this(lineLength, CHUNK_SEPARATOR);
-
}
-
-
public SanyiDecode(final int lineLength, final byte[] lineSeparator) {
-
this(lineLength, lineSeparator, false);
-
}
-
-
public SanyiDecode(final int lineLength, final byte[] lineSeparator, final boolean urlSafe) {
-
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK,
-
lineLength,
-
lineSeparator == null ? 0 : lineSeparator.length);
-
-
if (lineSeparator != null) {
-
if (containsAlphabetOrPad(lineSeparator)) {
-
final String sep = StringUtils.newStringUtf8(lineSeparator);
-
throw new IllegalArgumentException("lineSeparator must not contain base64 characters: [" + sep + "]");
-
}
-
if (lineLength > 0){ // null line-sep forces no chunking rather than throwing IAE
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
-
this.lineSeparator = new byte[lineSeparator.length];
-
System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
-
} else {
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
-
this.lineSeparator = null;
-
}
-
} else {
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
-
this.lineSeparator = null;
-
}
-
this.decodeSize = this.encodeSize - 1;
-
this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
-
}
-
-
public boolean isUrlSafe() {
-
return this.encodeTable == URL_SAFE_ENCODE_TABLE;
-
}
-
-
@Override
-
void decry(final byte[] in, int inPos, final int inAvail, final Context context) {
-
if (context.eof) {
-
return;
-
}
-
if (inAvail < 0) {
-
context.eof = true;
-
}
-
for (int i = 0; i < inAvail; i++) {
-
final byte[] buffer = ensureBufferSize(decodeSize, context);
-
final byte b = in[inPos++];
-
if (b == pad) {
-
// We're done.
-
context.eof = true;
-
break;
-
} else {
-
if (b >= 0 && b < DECODE_TABLE.length) {
-
final int result = DECODE_TABLE[b];
-
if (result >= 0) {
-
context.modulus = (context.modulus+1) % BYTES_PER_ENCODED_BLOCK;
-
context.ibitWorkArea = (context.ibitWorkArea << BITS_PER_ENCODED_BYTE) + result;
-
if (context.modulus == 0) {
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 16) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) (context.ibitWorkArea & MASK_8BITS);
-
}
-
}
-
}
-
}
-
}
-
-
if (context.eof && context.modulus != 0) {
-
final byte[] buffer = ensureBufferSize(decodeSize, context);
-
-
-
switch (context.modulus) {
-
// case 0 : // impossible, as excluded above
-
case 1 : // 6 bits - ignore entirely
-
// TODO not currently tested; perhaps it is impossible?
-
break;
-
case 2 : // 12 bits = 8 + 4
-
context.ibitWorkArea = context.ibitWorkArea >> 4; // dump the extra 4 bits
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea) & MASK_8BITS);
-
break;
-
case 3 : // 18 bits = 8 + 8 + 2
-
context.ibitWorkArea = context.ibitWorkArea >> 2; // dump 2 bits
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea) & MASK_8BITS);
-
break;
-
default:
-
throw new IllegalStateException("Impossible modulus "+context.modulus);
-
}
-
}
-
}
-
-
-
public static byte[] decryData(final String dataString) {
-
return new SanyiDecode().decode(dataString);
-
}
-
-
public static byte[] decryData(final byte[] data) {
-
return new SanyiDecode().decode(data);
-
}
-
-
public static BigInteger decryInteger(final byte[] pArray) {
-
return new BigInteger(1, decryData(pArray));
-
}
-
-
@Override
-
protected boolean isInAlphabet(final byte octet) {
-
return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
-
}
-
-
}
-
-
-
abstract class SanyiCodec {
-
-
static class Context {
-
-
int ibitWorkArea;
-
-
long lbitWorkArea;
-
-
byte[] buffer;
-
-
int pos;
-
-
int readPos;
-
-
boolean eof;
-
-
int currentLinePos;
-
-
int modulus;
-
-
Context() {
-
}
-
-
-
@SuppressWarnings("boxing") // OK to ignore boxing here
-
@Override
-
public String toString() {
-
return String.format("%s[buffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, " +
-
"modulus=%s, pos=%s, readPos=%s]", this.getClass().getSimpleName(), Arrays.toString(buffer),
-
currentLinePos, eof, ibitWorkArea, lbitWorkArea, modulus, pos, readPos);
-
}
-
}
-
-
-
static final int EOF = -1;
-
-
public static final int MIME_CHUNK_SIZE = 76;
-
-
public static final int PEM_CHUNK_SIZE = 64;
-
-
private static final int DEFAULT_BUFFER_RESIZE_FACTOR = 2;
-
-
-
private static final int DEFAULT_BUFFER_SIZE = 8192;
-
-
-
protected static final int MASK_8BITS = 0xff;
-
-
-
protected static final byte PAD_DEFAULT = '='; // Allow static access to default
-
-
-
@Deprecated
-
protected final byte PAD = PAD_DEFAULT; // instance variable just in case it needs to vary later
-
-
protected final byte pad; // instance variable just in case it needs to vary later
-
-
private final int unencodedBlockSize;
-
-
private final int encodedBlockSize;
-
-
-
protected final int lineLength;
-
-
-
private final int chunkSeparatorLength;
-
-
-
protected SanyiCodec(final int unencodedBlockSize, final int encodedBlockSize,
-
final int lineLength, final int chunkSeparatorLength) {
-
this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, PAD_DEFAULT);
-
}
-
-
protected SanyiCodec(final int unencodedBlockSize, final int encodedBlockSize,
-
final int lineLength, final int chunkSeparatorLength, final byte pad) {
-
this.unencodedBlockSize = unencodedBlockSize;
-
this.encodedBlockSize = encodedBlockSize;
-
final boolean useChunking = lineLength > 0 && chunkSeparatorLength > 0;
-
this.lineLength = useChunking ? (lineLength / encodedBlockSize) * encodedBlockSize : 0;
-
this.chunkSeparatorLength = chunkSeparatorLength;
-
-
this.pad = pad;
-
}
-
-
-
boolean hasData(final Context context) { // package protected for access from I/O streams
-
return context.buffer != null;
-
}
-
-
-
int available(final Context context) { // package protected for access from I/O streams
-
return context.buffer != null ? context.pos - context.readPos : 0;
-
}
-
-
-
protected int getDefaultBufferSize() {
-
return DEFAULT_BUFFER_SIZE;
-
}
-
-
-
private byte[] resizeBuffer(final Context context) {
-
if (context.buffer == null) {
-
context.buffer = new byte[getDefaultBufferSize()];
-
context.pos = 0;
-
context.readPos = 0;
-
} else {
-
final byte[] b = new byte[context.buffer.length * DEFAULT_BUFFER_RESIZE_FACTOR];
-
System.arraycopy(context.buffer, 0, b, 0, context.buffer.length);
-
context.buffer = b;
-
}
-
return context.buffer;
-
}
-
-
-
protected byte[] ensureBufferSize(final int size, final Context context){
-
if ((context.buffer == null) || (context.buffer.length < context.pos + size)){
-
return resizeBuffer(context);
-
}
-
return context.buffer;
-
}
-
-
-
int readResults(final byte[] b, final int bPos, final int bAvail, final Context context) {
-
if (context.buffer != null) {
-
final int len = Math.min(available(context), bAvail);
-
System.arraycopy(context.buffer, context.readPos, b, bPos, len);
-
context.readPos += len;
-
if (context.readPos >= context.pos) {
-
context.buffer = null; // so hasData() will return false, and this method can return -1
-
}
-
return len;
-
}
-
return context.eof ? EOF : 0;
-
}
-
-
-
protected static boolean isWhiteSpace(final byte byteToCheck) {
-
switch (byteToCheck) {
-
case ' ' :
-
case '\n' :
-
case '\r' :
-
case '\t' :
-
return true;
-
default :
-
return false;
-
}
-
}
-
-
-
public Object decode(final Object obj) throws SanyiDecoderException {
-
if (obj instanceof byte[]) {
-
return decode((byte[]) obj);
-
} else if (obj instanceof String) {
-
return decode((String) obj);
-
} else {
-
throw new SanyiDecoderException("Parameter supplied to Base-N decode is not a byte[] or a String");
-
}
-
}
-
-
-
public byte[] decode(final String pArray) {
-
return decode(StringUtils.getBytesUtf8(pArray));
-
}
-
-
-
-
public byte[] decode(final byte[] pArray) {
-
if (pArray == null || pArray.length == 0) {
-
return pArray;
-
}
-
final Context context = new Context();
-
decry(pArray, 0, pArray.length, context);
-
decry(pArray, 0, EOF, context); // Notify decoder of EOF.
-
final byte[] result = new byte[context.pos];
-
readResults(result, 0, result.length, context);
-
return result;
-
}
-
-
abstract void decry(byte[] pArray, int i, int length, Context context);
-
-
protected abstract boolean isInAlphabet(byte value);
-
-
-
public boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad) {
-
for (int i = 0; i < arrayOctet.length; i++) {
-
if (!isInAlphabet(arrayOctet[i]) &&
-
(!allowWSPad || (arrayOctet[i] != pad) && !isWhiteSpace(arrayOctet[i]))) {
-
return false;
-
}
-
}
-
return true;
-
}
-
-
-
public boolean isInAlphabet(final String basen) {
-
return isInAlphabet(StringUtils.getBytesUtf8(basen), true);
-
}
-
-
-
protected boolean containsAlphabetOrPad(final byte[] arrayOctet) {
-
if (arrayOctet == null) {
-
return false;
-
}
-
for (final byte element : arrayOctet) {
-
if (pad == element || isInAlphabet(element)) {
-
return true;
-
}
-
}
-
return false;
-
}
-
-
-
public long getEncodedLength(final byte[] pArray) {
-
long len = ((pArray.length + unencodedBlockSize-1) / unencodedBlockSize) * (long) encodedBlockSize;
-
if (lineLength > 0) { // We're using chunking
-
len += ((len + lineLength-1) / lineLength) * chunkSeparatorLength;
-
}
-
return len;
-
}
-
}
-
-
-
class SanyiDecoderException extends Exception {
-
-
private static final long serialVersionUID = 1L;
-
-
-
public SanyiDecoderException() {
-
super();
-
}
-
-
-
public SanyiDecoderException(String message) {
-
super(message);
-
}
-
-
-
public SanyiDecoderException(String message, Throwable cause) {
-
super(message, cause);
-
}
-
-
-
public SanyiDecoderException(Throwable cause) {
-
super(cause);
-
}
-
}
-
-
class SanyiCode32 extends SanyiCodec {
-
-
private static final int BITS_PER_ENCODED_BYTE = 5;
-
private static final int BYTES_PER_ENCODED_BLOCK = 8;
-
private static final int BYTES_PER_UNENCODED_BLOCK = 5;
-
-
private static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};
-
-
-
private static final byte[] DECODE_TABLE = {
-
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
-
-1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
-
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40-4f A-N
-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 50-5a O-Z
-
};
-
-
-
private static final byte[] ENCODE_TABLE = {
-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-
'2', '3', '4', '5', '6', '7',
-
};
-
-
private static final byte[] HEX_DECODE_TABLE = {
-
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
-
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 40-4f A-N
-
25, 26, 27, 28, 29, 30, 31, 32, // 50-57 O-V
-
};
-
-
-
private static final byte[] HEX_ENCODE_TABLE = {
-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
-
};
-
-
-
private static final int MASK_5BITS = 0x1f;
-
-
-
private final int decodeSize;
-
-
-
private final byte[] decodeTable;
-
-
-
private final int encodeSize;
-
-
-
private final byte[] encodeTable;
-
-
-
private final byte[] lineSeparator;
-
-
-
public SanyiCode32() {
-
this(false);
-
}
-
-
-
public SanyiCode32(final byte pad) {
-
this(false, pad);
-
}
-
-
-
public SanyiCode32(final boolean useHex) {
-
this(0, null, useHex, PAD_DEFAULT);
-
}
-
-
-
public SanyiCode32(final boolean useHex, final byte pad) {
-
this(0, null, useHex, pad);
-
}
-
-
-
public SanyiCode32(final int lineLength) {
-
this(lineLength, CHUNK_SEPARATOR);
-
}
-
-
public SanyiCode32(final int lineLength, final byte[] lineSeparator) {
-
this(lineLength, lineSeparator, false, PAD_DEFAULT);
-
}
-
-
-
public SanyiCode32(final int lineLength, final byte[] lineSeparator, final boolean useHex) {
-
this(lineLength, lineSeparator, useHex, PAD_DEFAULT);
-
}
-
-
-
public SanyiCode32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte pad) {
-
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength,
-
lineSeparator == null ? 0 : lineSeparator.length, pad);
-
if (useHex) {
-
this.encodeTable = HEX_ENCODE_TABLE;
-
this.decodeTable = HEX_DECODE_TABLE;
-
} else {
-
this.encodeTable = ENCODE_TABLE;
-
this.decodeTable = DECODE_TABLE;
-
}
-
if (lineLength > 0) {
-
if (lineSeparator == null) {
-
throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null");
-
}
-
// Must be done after initializing the tables
-
if (containsAlphabetOrPad(lineSeparator)) {
-
final String sep = StringUtils.newStringUtf8(lineSeparator);
-
throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]");
-
}
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
-
this.lineSeparator = new byte[lineSeparator.length];
-
System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
-
} else {
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
-
this.lineSeparator = null;
-
}
-
this.decodeSize = this.encodeSize - 1;
-
-
if (isInAlphabet(pad) || isWhiteSpace(pad)) {
-
throw new IllegalArgumentException("pad must not be in alphabet or whitespace");
-
}
-
}
-
-
-
@Override
-
void decry(final byte[] in, int inPos, final int inAvail, final Context context) {
-
// package protected for access from I/O streams
-
-
if (context.eof) {
-
return;
-
}
-
if (inAvail < 0) {
-
context.eof = true;
-
}
-
for (int i = 0; i < inAvail; i++) {
-
final byte b = in[inPos++];
-
if (b == pad) {
-
// We're done.
-
context.eof = true;
-
break;
-
} else {
-
final byte[] buffer = ensureBufferSize(decodeSize, context);
-
if (b >= 0 && b < this.decodeTable.length) {
-
final int result = this.decodeTable[b];
-
if (result >= 0) {
-
context.modulus = (context.modulus+1) % BYTES_PER_ENCODED_BLOCK;
-
// collect decoded bytes
-
context.lbitWorkArea = (context.lbitWorkArea << BITS_PER_ENCODED_BYTE) + result;
-
if (context.modulus == 0) { // we can output the 5 bytes
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 32) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 24) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
-
}
-
}
-
}
-
}
-
}
-
-
if (context.eof && context.modulus >= 2) { // if modulus < 2, nothing to do
-
final byte[] buffer = ensureBufferSize(decodeSize, context);
-
-
// we ignore partial bytes, i.e. only multiples of 8 count
-
switch (context.modulus) {
-
case 2 : // 10 bits, drop 2 and output one byte
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 2) & MASK_8BITS);
-
break;
-
case 3 : // 15 bits, drop 7 and output 1 byte
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 7) & MASK_8BITS);
-
break;
-
case 4 : // 20 bits = 2*8 + 4
-
context.lbitWorkArea = context.lbitWorkArea >> 4; // drop 4 bits
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
-
break;
-
case 5 : // 25bits = 3*8 + 1
-
context.lbitWorkArea = context.lbitWorkArea >> 1;
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
-
break;
-
case 6 : // 30bits = 3*8 + 6
-
context.lbitWorkArea = context.lbitWorkArea >> 6;
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
-
break;
-
case 7 : // 35 = 4*8 +3
-
context.lbitWorkArea = context.lbitWorkArea >> 3;
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 24) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
-
break;
-
default:
-
// modulus can be 0-7, and we excluded 0,1 already
-
throw new IllegalStateException("Impossible modulus "+context.modulus);
-
}
-
}
-
}
-
-
-
@Override
-
public boolean isInAlphabet(final byte octet) {
-
return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
-
}
-
}
-
-
-
class SanyiCode64 extends SanyiCodec {
-
-
private static final int BITS_PER_ENCODED_BYTE = 6;
-
private static final int BYTES_PER_UNENCODED_BLOCK = 3;
-
private static final int BYTES_PER_ENCODED_BLOCK = 4;
-
-
-
static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};
-
-
-
private static final byte[] STANDARD_ENCODE_TABLE = {
-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
-
};
-
-
-
private static final byte[] URL_SAFE_ENCODE_TABLE = {
-
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
-
};
-
-
private static final byte[] DECODE_TABLE = {
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-
-1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54,
-
55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
-
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
-
24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
-
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
-
};
-
-
-
private static final int MASK_6BITS = 0x3f;
-
-
-
private final byte[] encodeTable;
-
-
-
private final byte[] decodeTable = DECODE_TABLE;
-
-
-
private final byte[] lineSeparator;
-
-
-
private final int decodeSize;
-
-
-
private final int encodeSize;
-
-
-
public SanyiCode64() {
-
this(0);
-
}
-
-
-
public SanyiCode64(final boolean urlSafe) {
-
this(MIME_CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
-
}
-
-
-
public SanyiCode64(final int lineLength) {
-
this(lineLength, CHUNK_SEPARATOR);
-
}
-
-
-
public SanyiCode64(final int lineLength, final byte[] lineSeparator) {
-
this(lineLength, lineSeparator, false);
-
}
-
-
-
public SanyiCode64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe) {
-
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK,
-
lineLength,
-
lineSeparator == null ? 0 : lineSeparator.length);
-
// TODO could be simplified if there is no requirement to reject invalid line sep when length <=0
-
// @see test case Base64Test.testConstructors()
-
if (lineSeparator != null) {
-
if (containsAlphabetOrPad(lineSeparator)) {
-
final String sep = StringUtils.newStringUtf8(lineSeparator);
-
throw new IllegalArgumentException("lineSeparator must not contain base64 characters: [" + sep + "]");
-
}
-
if (lineLength > 0){ // null line-sep forces no chunking rather than throwing IAE
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
-
this.lineSeparator = new byte[lineSeparator.length];
-
System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
-
} else {
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
-
this.lineSeparator = null;
-
}
-
} else {
-
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
-
this.lineSeparator = null;
-
}
-
this.decodeSize = this.encodeSize - 1;
-
this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
-
}
-
-
-
public boolean isUrlSafe() {
-
return this.encodeTable == URL_SAFE_ENCODE_TABLE;
-
}
-
-
-
@Override
-
void decry(final byte[] in, int inPos, final int inAvail, final Context context) {
-
if (context.eof) {
-
return;
-
}
-
if (inAvail < 0) {
-
context.eof = true;
-
}
-
for (int i = 0; i < inAvail; i++) {
-
final byte[] buffer = ensureBufferSize(decodeSize, context);
-
final byte b = in[inPos++];
-
if (b == pad) {
-
// We're done.
-
context.eof = true;
-
break;
-
} else {
-
if (b >= 0 && b < DECODE_TABLE.length) {
-
final int result = DECODE_TABLE[b];
-
if (result >= 0) {
-
context.modulus = (context.modulus+1) % BYTES_PER_ENCODED_BLOCK;
-
context.ibitWorkArea = (context.ibitWorkArea << BITS_PER_ENCODED_BYTE) + result;
-
if (context.modulus == 0) {
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 16) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) (context.ibitWorkArea & MASK_8BITS);
-
}
-
}
-
}
-
}
-
}
-
-
// Two forms of EOF as far as base64 decoder is concerned: actual
-
// EOF (-1) and first time '=' character is encountered in stream.
-
// This approach makes the '=' padding characters completely optional.
-
if (context.eof && context.modulus != 0) {
-
final byte[] buffer = ensureBufferSize(decodeSize, context);
-
-
// We have some spare bits remaining
-
// Output all whole multiples of 8 bits and ignore the rest
-
switch (context.modulus) {
-
// case 0 : // impossible, as excluded above
-
case 1 : // 6 bits - ignore entirely
-
// TODO not currently tested; perhaps it is impossible?
-
break;
-
case 2 : // 12 bits = 8 + 4
-
context.ibitWorkArea = context.ibitWorkArea >> 4; // dump the extra 4 bits
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea) & MASK_8BITS);
-
break;
-
case 3 : // 18 bits = 8 + 8 + 2
-
context.ibitWorkArea = context.ibitWorkArea >> 2; // dump 2 bits
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 8) & MASK_8BITS);
-
buffer[context.pos++] = (byte) ((context.ibitWorkArea) & MASK_8BITS);
-
break;
-
default:
-
throw new IllegalStateException("Impossible modulus "+context.modulus);
-
}
-
}
-
}
-
-
-
@Deprecated
-
public static boolean isArrayByteBase64(final byte[] arrayOctet) {
-
return isCode(arrayOctet);
-
}
-
-
-
public static boolean isCode(final byte octet) {
-
return octet == PAD_DEFAULT || (octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1);
-
}
-
-
-
public static boolean isCode(final String base64) {
-
return isCode(StringUtils.getBytesUtf8(base64));
-
}
-
-
-
public static boolean isCode(final byte[] arrayOctet) {
-
for (int i = 0; i < arrayOctet.length; i++) {
-
if (!isCode(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) {
-
return false;
-
}
-
}
-
return true;
-
}
-
-
public static byte[] decryData(final String dataring) {
-
return new SanyiCode64().decode(dataring);
-
}
-
-
-
public static byte[] decryData(final byte[] data) {
-
return new SanyiCode64().decode(data);
-
}
-
-
public static BigInteger decryInteger(final byte[] pArray) {
-
return new BigInteger(1, decryData(pArray));
-
}
-
-
static byte[] toIntegerBytes(final BigInteger bigInt) {
-
int bitlen = bigInt.bitLength();
-
// round bitlen
-
bitlen = ((bitlen + 7) >> 3) << 3;
-
final byte[] bigBytes = bigInt.toByteArray();
-
-
if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
-
return bigBytes;
-
}
-
// set up params for copying everything but sign bit
-
int startSrc = 0;
-
int len = bigBytes.length;
-
-
// if bigInt is exactly byte-aligned, just skip signbit in copy
-
if ((bigInt.bitLength() % 8) == 0) {
-
startSrc = 1;
-
len--;
-
}
-
final int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
-
final byte[] resizedBytes = new byte[bitlen / 8];
-
System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
-
return resizedBytes;
-
}
-
-
-
@Override
-
protected boolean isInAlphabet(final byte octet) {
-
return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
-
}
-
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31347383/viewspace-2122413/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31347383/viewspace-2122413/
本文介绍了一种Base64和Base32解码算法的实现细节,包括解码表、编码表、解码过程等关键组件。通过对不同场景下解码流程的分析,展示了如何处理各种边界情况,例如填充字符和结束标志。
2338

被折叠的 条评论
为什么被折叠?



