1 17 18 package org.apache.tomcat.util.buf; 19 20 import java.io.ByteArrayOutputStream ; 21 import org.apache.tomcat.util.res.StringManager; 22 23 30 31 public final class HexUtils { 32 33 34 36 37 40 public static final int[] DEC = { 41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 44 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, 45 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 47 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 57 }; 58 59 60 63 public static final byte[] HEX = 64 { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', 65 (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', 66 (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f' }; 67 68 69 72 private static StringManager sm = 73 StringManager.getManager("org.apache.tomcat.util.buf.res"); 74 75 76 78 79 89 public static byte[] convert(String digits) { 90 91 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 92 for (int i = 0; i < digits.length(); i += 2) { 93 char c1 = digits.charAt(i); 94 if ((i+1) >= digits.length()) 95 throw new IllegalArgumentException 96 (sm.getString("hexUtil.odd")); 97 char c2 = digits.charAt(i + 1); 98 byte b = 0; 99 if ((c1 >= '0') && (c1 <= '9')) 100 b += ((c1 - '0') * 16); 101 else if ((c1 >= 'a') && (c1 <= 'f')) 102 b += ((c1 - 'a' + 10) * 16); 103 else if ((c1 >= 'A') && (c1 <= 'F')) 104 b += ((c1 - 'A' + 10) * 16); 105 else 106 throw new IllegalArgumentException 107 (sm.getString("hexUtil.bad")); 108 if ((c2 >= '0') && (c2 <= '9')) 109 b += (c2 - '0'); 110 else if ((c2 >= 'a') && (c2 <= 'f')) 111 b += (c2 - 'a' + 10); 112 else if ((c2 >= 'A') && (c2 <= 'F')) 113 b += (c2 - 'A' + 10); 114 else 115 throw new IllegalArgumentException 116 (sm.getString("hexUtil.bad")); 117 baos.write(b); 118 } 119 return (baos.toByteArray()); 120 121 } 122 123 124 130 public static String convert(byte bytes[]) { 131 132 StringBuffer sb = new StringBuffer (bytes.length * 2); 133 for (int i = 0; i < bytes.length; i++) { 134 sb.append(convertDigit((int) (bytes[i] >> 4))); 135 sb.append(convertDigit((int) (bytes[i] & 0x0f))); 136 } 137 return (sb.toString()); 138 139 } 140 141 150 public static int convert2Int( byte[] hex ) { 151 153 int len; 156 if(hex.length < 4 ) return 0; 157 if( DEC[hex[0]]<0 ) 158 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 159 len = DEC[hex[0]]; 160 len = len << 4; 161 if( DEC[hex[1]]<0 ) 162 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 163 len += DEC[hex[1]]; 164 len = len << 4; 165 if( DEC[hex[2]]<0 ) 166 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 167 len += DEC[hex[2]]; 168 len = len << 4; 169 if( DEC[hex[3]]<0 ) 170 throw new IllegalArgumentException (sm.getString("hexUtil.bad")); 171 len += DEC[hex[3]]; 172 return len; 173 } 174 175 176 177 183 private static char convertDigit(int value) { 184 185 value &= 0x0f; 186 if (value >= 10) 187 return ((char) (value - 10 + 'a')); 188 else 189 return ((char) (value + '0')); 190 191 } 192 193 194 } 195 | Popular Tags |