package org.glassfish.grizzly.http.util;
import java.io.ByteArrayOutputStream;
public final class HexUtils {
static final boolean[] IS_HEX_DIGIT = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, false,
false, false, false, false, false, false, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, };
static final int[] DEC = { -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, -1, -1, -1, -1, -1, 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13,
14, 15, -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, 10, 11, 12, 13, 14, 15, -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, -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, -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, };
static final byte[] HEX = { (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' };
public static byte[] convert(String digits) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i = 0; i < digits.length(); i += 2) {
char c1 = digits.charAt(i);
if (i + 1 >= digits.length()) {
throw new IllegalArgumentException("hexUtil.odd");
}
char c2 = digits.charAt(i + 1);
byte b = 0;
if (c1 >= '0' && c1 <= '9') {
b += (c1 - '0') * 16;
} else if (c1 >= 'a' && c1 <= 'f') {
b += (c1 - 'a' + 10) * 16;
} else if (c1 >= 'A' && c1 <= 'F') {
b += (c1 - 'A' + 10) * 16;
} else {
throw new IllegalArgumentException("hexUtil.bad");
}
if (c2 >= '0' && c2 <= '9') {
b += c2 - '0';
} else if (c2 >= 'a' && c2 <= 'f') {
b += c2 - 'a' + 10;
} else if (c2 >= 'A' && c2 <= 'F') {
b += c2 - 'A' + 10;
} else {
throw new IllegalArgumentException("hexUtil.bad");
}
baos.write(b);
}
return baos.toByteArray();
}
public static String convert(byte bytes[]) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
sb.append(convertDigit(bytes[i] >> 4));
sb.append(convertDigit(bytes[i] & 0x0f));
}
return sb.toString();
}
public static int convert2Int(byte[] hex) {
int len;
if (hex.length < 4) {
return 0;
}
if (DEC[hex[0]] < 0) {
throw new IllegalArgumentException("hexUtil.bad");
}
len = DEC[hex[0]];
len = len << 4;
if (DEC[hex[1]] < 0) {
throw new IllegalArgumentException("hexUtil.bad");
}
len += DEC[hex[1]];
len = len << 4;
if (DEC[hex[2]] < 0) {
throw new IllegalArgumentException("hexUtil.bad");
}
len += DEC[hex[2]];
len = len << 4;
if (DEC[hex[3]] < 0) {
throw new IllegalArgumentException("hexUtil.bad");
}
len += DEC[hex[3]];
return len;
}
public static int[] getDecBytes() {
return DEC.clone();
}
private static char convertDigit(int value) {
value &= 0x0f;
if (value >= 10) {
return (char) (value - 10 + 'a');
} else {
return (char) (value + '0');
}
}
public static boolean isHexDigit(byte c) {
return IS_HEX_DIGIT[c];
}
public static boolean isHexDigit(int c) {
return IS_HEX_DIGIT[c];
}
public static int hexDigit2Dec(byte hexDigit) {
return DEC[hexDigit];
}
public static int hexDigit2Dec(int hexDigit) {
return DEC[hexDigit];
}
}