1. /*
  2. * @(#)Base64.java 1.5 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.prefs;
  8. /**
  9. * Static methods for translating Base64 encoded strings to byte arrays
  10. * and vice-versa.
  11. *
  12. * @author Josh Bloch
  13. * @version 1.5, 12/19/03
  14. * @see Preferences
  15. * @since 1.4
  16. */
  17. class Base64 {
  18. /**
  19. * Translates the specified byte array into a Base64 string as per
  20. * Preferences.put(byte[]).
  21. */
  22. static String byteArrayToBase64(byte[] a) {
  23. return byteArrayToBase64(a, false);
  24. }
  25. /**
  26. * Translates the specified byte array into an "aternate representation"
  27. * Base64 string. This non-standard variant uses an alphabet that does
  28. * not contain the uppercase alphabetic characters, which makes it
  29. * suitable for use in situations where case-folding occurs.
  30. */
  31. static String byteArrayToAltBase64(byte[] a) {
  32. return byteArrayToBase64(a, true);
  33. }
  34. private static String byteArrayToBase64(byte[] a, boolean alternate) {
  35. int aLen = a.length;
  36. int numFullGroups = aLen3;
  37. int numBytesInPartialGroup = aLen - 3*numFullGroups;
  38. int resultLen = 4*((aLen + 2)/3);
  39. StringBuffer result = new StringBuffer(resultLen);
  40. char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
  41. // Translate all full groups from byte array elements to Base64
  42. int inCursor = 0;
  43. for (int i=0; i<numFullGroups; i++) {
  44. int byte0 = a[inCursor++] & 0xff;
  45. int byte1 = a[inCursor++] & 0xff;
  46. int byte2 = a[inCursor++] & 0xff;
  47. result.append(intToAlpha[byte0 >> 2]);
  48. result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
  49. result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
  50. result.append(intToAlpha[byte2 & 0x3f]);
  51. }
  52. // Translate partial group if present
  53. if (numBytesInPartialGroup != 0) {
  54. int byte0 = a[inCursor++] & 0xff;
  55. result.append(intToAlpha[byte0 >> 2]);
  56. if (numBytesInPartialGroup == 1) {
  57. result.append(intToAlpha[(byte0 << 4) & 0x3f]);
  58. result.append("==");
  59. } else {
  60. // assert numBytesInPartialGroup == 2;
  61. int byte1 = a[inCursor++] & 0xff;
  62. result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
  63. result.append(intToAlpha[(byte1 << 2)&0x3f]);
  64. result.append('=');
  65. }
  66. }
  67. // assert inCursor == a.length;
  68. // assert result.length() == resultLen;
  69. return result.toString();
  70. }
  71. /**
  72. * This array is a lookup table that translates 6-bit positive integer
  73. * index values into their "Base64 Alphabet" equivalents as specified
  74. * in Table 1 of RFC 2045.
  75. */
  76. private static final char intToBase64[] = {
  77. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  78. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  79. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  80. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  81. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
  82. };
  83. /**
  84. * This array is a lookup table that translates 6-bit positive integer
  85. * index values into their "Alternate Base64 Alphabet" equivalents.
  86. * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045.
  87. * This alternate alphabet does not use the capital letters. It is
  88. * designed for use in environments where "case folding" occurs.
  89. */
  90. private static final char intToAltBase64[] = {
  91. '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':',
  92. ';', '<', '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~',
  93. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  94. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  95. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '?'
  96. };
  97. /**
  98. * Translates the specified Base64 string (as per Preferences.get(byte[]))
  99. * into a byte array.
  100. *
  101. * @throw IllegalArgumentException if <tt>s</tt> is not a valid Base64
  102. * string.
  103. */
  104. static byte[] base64ToByteArray(String s) {
  105. return base64ToByteArray(s, false);
  106. }
  107. /**
  108. * Translates the specified "aternate representation" Base64 string
  109. * into a byte array.
  110. *
  111. * @throw IllegalArgumentException or ArrayOutOfBoundsException
  112. * if <tt>s</tt> is not a valid alternate representation
  113. * Base64 string.
  114. */
  115. static byte[] altBase64ToByteArray(String s) {
  116. return base64ToByteArray(s, true);
  117. }
  118. private static byte[] base64ToByteArray(String s, boolean alternate) {
  119. byte[] alphaToInt = (alternate ? altBase64ToInt : base64ToInt);
  120. int sLen = s.length();
  121. int numGroups = sLen4;
  122. if (4*numGroups != sLen)
  123. throw new IllegalArgumentException(
  124. "String length must be a multiple of four.");
  125. int missingBytesInLastGroup = 0;
  126. int numFullGroups = numGroups;
  127. if (sLen != 0) {
  128. if (s.charAt(sLen-1) == '=') {
  129. missingBytesInLastGroup++;
  130. numFullGroups--;
  131. }
  132. if (s.charAt(sLen-2) == '=')
  133. missingBytesInLastGroup++;
  134. }
  135. byte[] result = new byte[3*numGroups - missingBytesInLastGroup];
  136. // Translate all full groups from base64 to byte array elements
  137. int inCursor = 0, outCursor = 0;
  138. for (int i=0; i<numFullGroups; i++) {
  139. int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
  140. int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
  141. int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
  142. int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
  143. result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
  144. result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
  145. result[outCursor++] = (byte) ((ch2 << 6) | ch3);
  146. }
  147. // Translate partial group, if present
  148. if (missingBytesInLastGroup != 0) {
  149. int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
  150. int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
  151. result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
  152. if (missingBytesInLastGroup == 1) {
  153. int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
  154. result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
  155. }
  156. }
  157. // assert inCursor == s.length()-missingBytesInLastGroup;
  158. // assert outCursor == result.length;
  159. return result;
  160. }
  161. /**
  162. * Translates the specified character, which is assumed to be in the
  163. * "Base 64 Alphabet" into its equivalent 6-bit positive integer.
  164. *
  165. * @throw IllegalArgumentException or ArrayOutOfBoundsException if
  166. * c is not in the Base64 Alphabet.
  167. */
  168. private static int base64toInt(char c, byte[] alphaToInt) {
  169. int result = alphaToInt[c];
  170. if (result < 0)
  171. throw new IllegalArgumentException("Illegal character " + c);
  172. return result;
  173. }
  174. /**
  175. * This array is a lookup table that translates unicode characters
  176. * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
  177. * into their 6-bit positive integer equivalents. Characters that
  178. * are not in the Base64 alphabet but fall within the bounds of the
  179. * array are translated to -1.
  180. */
  181. private static final byte base64ToInt[] = {
  182. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  183. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  184. -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
  185. 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
  186. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
  187. 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  188. 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
  189. };
  190. /**
  191. * This array is the analogue of base64ToInt, but for the nonstandard
  192. * variant that avoids the use of uppercase alphabetic characters.
  193. */
  194. private static final byte altBase64ToInt[] = {
  195. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  196. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
  197. 2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1 , 52, 53, 54, 55, 56, 57,
  198. 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1,
  199. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  200. -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33,
  201. 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  202. 51, 22, 23, 24, 25
  203. };
  204. public static void main(String args[]) {
  205. int numRuns = Integer.parseInt(args[0]);
  206. int numBytes = Integer.parseInt(args[1]);
  207. java.util.Random rnd = new java.util.Random();
  208. for (int i=0; i<numRuns; i++) {
  209. for (int j=0; j<numBytes; j++) {
  210. byte[] arr = new byte[j];
  211. for (int k=0; k<j; k++)
  212. arr[k] = (byte)rnd.nextInt();
  213. String s = byteArrayToBase64(arr);
  214. byte [] b = base64ToByteArray(s);
  215. if (!java.util.Arrays.equals(arr, b))
  216. System.out.println("Dismal failure!");
  217. s = byteArrayToAltBase64(arr);
  218. b = altBase64ToByteArray(s);
  219. if (!java.util.Arrays.equals(arr, b))
  220. System.out.println("Alternate dismal failure!");
  221. }
  222. }
  223. }
  224. }