1 8 9 package org.jboss.axis.utils; 10 11 34 final class Base64 35 { 36 private static final int BASELENGTH = 255; 37 private static final int LOOKUPLENGTH = 64; 38 private static final int TWENTYFOURBITGROUP = 24; 39 private static final int EIGHTBIT = 8; 40 private static final int SIXTEENBIT = 16; 41 private static final int SIXBIT = 6; 42 private static final int FOURBYTE = 4; 43 private static final int SIGN = -128; 44 private static final byte PAD = (byte)'='; 45 private static byte[] base64Alphabet = new byte[BASELENGTH]; 46 private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH]; 47 48 static 49 { 50 51 for (int i = 0; i < BASELENGTH; i++) 52 { 53 base64Alphabet[i] = -1; 54 } 55 for (int i = 'Z'; i >= 'A'; i--) 56 { 57 base64Alphabet[i] = (byte)(i - 'A'); 58 } 59 for (int i = 'z'; i >= 'a'; i--) 60 { 61 base64Alphabet[i] = (byte)(i - 'a' + 26); 62 } 63 64 for (int i = '9'; i >= '0'; i--) 65 { 66 base64Alphabet[i] = (byte)(i - '0' + 52); 67 } 68 69 base64Alphabet['+'] = 62; 70 base64Alphabet['/'] = 63; 71 72 for (int i = 0; i <= 25; i++) 73 { 74 lookUpBase64Alphabet[i] = (byte)('A' + i); 75 } 76 77 for (int i = 26, j = 0; i <= 51; i++, j++) 78 { 79 lookUpBase64Alphabet[i] = (byte)('a' + j); 80 } 81 82 for (int i = 52, j = 0; i <= 61; i++, j++) 83 { 84 lookUpBase64Alphabet[i] = (byte)('0' + j); 85 } 86 lookUpBase64Alphabet[62] = (byte)'+'; 87 lookUpBase64Alphabet[63] = (byte)'/'; 88 89 } 90 91 static boolean isBase64(String isValidString) 92 { 93 return (isArrayByteBase64(isValidString.getBytes())); 94 } 95 96 97 static boolean isBase64(byte octect) 98 { 99 return (octect == PAD || base64Alphabet[octect] != -1); 101 } 102 103 104 static boolean isArrayByteBase64(byte[] arrayOctect) 105 { 106 int length = arrayOctect.length; 107 if (length == 0) 108 { 109 return true; 110 } 111 for (int i = 0; i < length; i++) 112 { 113 if (Base64.isBase64(arrayOctect[i]) == false) 114 { 115 return false; 116 } 117 } 118 return true; 119 } 120 121 127 static byte[] encode(byte[] binaryData) 128 { 129 130 int lengthDataBits = binaryData.length * EIGHTBIT; 131 int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP; 132 int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP; 133 byte encodedData[] = null; 134 135 136 if (fewerThan24bits != 0) 137 { 138 encodedData = new byte[(numberTriplets + 1) * 4]; 140 } 141 else 142 { 143 encodedData = new byte[numberTriplets * 4]; 145 } 146 147 byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; 148 149 int encodedIndex = 0; 150 int dataIndex = 0; 151 int i = 0; 152 for (i = 0; i < numberTriplets; i++) 153 { 154 155 dataIndex = i * 3; 156 b1 = binaryData[dataIndex]; 157 b2 = binaryData[dataIndex + 1]; 158 b3 = binaryData[dataIndex + 2]; 159 160 l = (byte)(b2 & 0x0f); 161 k = (byte)(b1 & 0x03); 162 163 encodedIndex = i * 4; 164 byte val1 = ((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0); 165 166 byte val2 = ((b2 & SIGN) == 0) ? (byte)(b2 >> 4) : (byte)((b2) >> 4 ^ 0xf0); 167 byte val3 = ((b3 & SIGN) == 0) ? (byte)(b3 >> 6) : (byte)((b3) >> 6 ^ 0xfc); 168 169 encodedData[encodedIndex] = lookUpBase64Alphabet[val1]; 170 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)]; 171 encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2) | val3]; 172 encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f]; 173 } 174 175 dataIndex = i * 3; 177 encodedIndex = i * 4; 178 if (fewerThan24bits == EIGHTBIT) 179 { 180 b1 = binaryData[dataIndex]; 181 k = (byte)(b1 & 0x03); 182 byte val1 = ((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0); 183 encodedData[encodedIndex] = lookUpBase64Alphabet[val1]; 184 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4]; 185 encodedData[encodedIndex + 2] = PAD; 186 encodedData[encodedIndex + 3] = PAD; 187 } 188 else if (fewerThan24bits == SIXTEENBIT) 189 { 190 b1 = binaryData[dataIndex]; 191 b2 = binaryData[dataIndex + 1]; 192 l = (byte)(b2 & 0x0f); 193 k = (byte)(b1 & 0x03); 194 195 byte val1 = ((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0); 196 byte val2 = ((b2 & SIGN) == 0) ? (byte)(b2 >> 4) : (byte)((b2) >> 4 ^ 0xf0); 197 198 encodedData[encodedIndex] = lookUpBase64Alphabet[val1]; 199 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)]; 200 encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2]; 201 encodedData[encodedIndex + 3] = PAD; 202 } 203 return encodedData; 204 } 205 206 207 213 static byte[] decode(byte[] base64Data) 214 { 215 217 if (base64Data.length == 0) 219 { 220 return new byte[0]; 221 } 222 223 int numberQuadruple = base64Data.length / FOURBYTE; 224 byte decodedData[] = null; 225 byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0; 226 227 int encodedIndex = 0; 228 int dataIndex = 0; 229 { 230 int lastData = base64Data.length; 232 while (base64Data[lastData - 1] == PAD) 234 { 235 if (--lastData == 0) 236 { 237 return new byte[0]; 238 } 239 } 240 decodedData = new byte[lastData - numberQuadruple]; 241 } 242 243 for (int i = 0; i < numberQuadruple; i++) 244 { 245 dataIndex = i * 4; 246 marker0 = base64Data[dataIndex + 2]; 247 marker1 = base64Data[dataIndex + 3]; 248 249 b1 = base64Alphabet[base64Data[dataIndex]]; 250 b2 = base64Alphabet[base64Data[dataIndex + 1]]; 251 252 if (marker0 != PAD && marker1 != PAD) 253 { b3 = base64Alphabet[marker0]; 255 b4 = base64Alphabet[marker1]; 256 257 decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4); 258 decodedData[encodedIndex + 1] = (byte)(((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 259 decodedData[encodedIndex + 2] = (byte)(b3 << 6 | b4); 260 } 261 else if (marker0 == PAD) 262 { decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4); 264 } 265 else if (marker1 == PAD) 266 { b3 = base64Alphabet[marker0]; 268 269 decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4); 270 decodedData[encodedIndex + 1] = (byte)(((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 271 } 272 encodedIndex += 3; 273 } 274 return decodedData; 275 } 276 277 } 278 | Popular Tags |