001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012:
013: package org.tmatesoft.svn.core.internal.util;
014:
015: /**
016: * @version 1.1.1
017: * @author TMate Software Ltd.
018: */
019: public class SVNBase64 {
020:
021: public static String byteArrayToBase64(byte[] a) {
022: return byteArrayToBase64(a, false);
023: }
024:
025: public static String byteArrayToAltBase64(byte[] a) {
026: return byteArrayToBase64(a, true);
027: }
028:
029: private static String byteArrayToBase64(byte[] a, boolean alternate) {
030: int aLen = a.length;
031: int numFullGroups = aLen / 3;
032: int numBytesInPartialGroup = aLen - 3 * numFullGroups;
033: int resultLen = 4 * ((aLen + 2) / 3);
034: StringBuffer result = new StringBuffer(resultLen);
035: char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
036:
037: // Translate all full groups from byte array elements to SVNBase64
038: int inCursor = 0;
039: for (int i = 0; i < numFullGroups; i++) {
040: int byte0 = a[inCursor++] & 0xff;
041: int byte1 = a[inCursor++] & 0xff;
042: int byte2 = a[inCursor++] & 0xff;
043: result.append(intToAlpha[byte0 >> 2]);
044: result
045: .append(intToAlpha[(byte0 << 4) & 0x3f
046: | (byte1 >> 4)]);
047: result
048: .append(intToAlpha[(byte1 << 2) & 0x3f
049: | (byte2 >> 6)]);
050: result.append(intToAlpha[byte2 & 0x3f]);
051: }
052:
053: // Translate partial group if present
054: if (numBytesInPartialGroup != 0) {
055: int byte0 = a[inCursor++] & 0xff;
056: result.append(intToAlpha[byte0 >> 2]);
057: if (numBytesInPartialGroup == 1) {
058: result.append(intToAlpha[(byte0 << 4) & 0x3f]);
059: result.append("==");
060: } else {
061: // assert numBytesInPartialGroup == 2;
062: int byte1 = a[inCursor++] & 0xff;
063: result.append(intToAlpha[(byte0 << 4) & 0x3f
064: | (byte1 >> 4)]);
065: result.append(intToAlpha[(byte1 << 2) & 0x3f]);
066: result.append('=');
067: }
068: }
069: // assert inCursor == a.length;
070: // assert result.length() == resultLen;
071: return result.toString();
072: }
073:
074: private static final char intToBase64[] = { 'A', 'B', 'C', 'D',
075: 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
076: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
077: 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
078: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
079: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
080:
081: private static final char intToAltBase64[] = { '!', '"', '#', '$',
082: '%', '&', '\'', '(', ')', ',', '-', '.', ':', ';', '<',
083: '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~', 'a',
084: 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
085: 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
086: 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+',
087: '?' };
088:
089: /**
090: * Translates the specified SVNBase64 string (as per Preferences.get(byte[]))
091: * into a byte array.
092: *
093: * @throws IllegalArgumentException
094: * if <tt>s</tt> is not a valid SVNBase64 string.
095: */
096: public static int base64ToByteArray(StringBuffer s, byte[] buffer) {
097: return base64ToByteArray(s, buffer, false);
098: }
099:
100: private static int base64ToByteArray(StringBuffer s, byte[] result,
101: boolean alternate) {
102: byte[] alphaToInt = (alternate ? altBase64ToInt : base64ToInt);
103: int sLen = s.length();
104: int numGroups = sLen / 4;
105: if (4 * numGroups != sLen) {
106: for (int i = 0; i < 4 - (sLen % 4); i++) {
107: s = s.append('=');
108: }
109: numGroups++;
110: sLen = 4 * numGroups;
111: }
112: int missingBytesInLastGroup = 0;
113: int numFullGroups = numGroups;
114: if (sLen != 0) {
115: if (s.charAt(sLen - 1) == '=') {
116: missingBytesInLastGroup++;
117: numFullGroups--;
118: }
119: if (s.charAt(sLen - 2) == '=')
120: missingBytesInLastGroup++;
121: }
122: int resultLength = 3 * numGroups - missingBytesInLastGroup;
123:
124: // Translate all full groups from base64 to byte array elements
125: int inCursor = 0, outCursor = 0;
126: for (int i = 0; i < numFullGroups; i++) {
127: int ch0 = alphaToInt[s.charAt(inCursor++)];
128: int ch1 = alphaToInt[s.charAt(inCursor++)];
129: int ch2 = alphaToInt[s.charAt(inCursor++)];
130: int ch3 = alphaToInt[s.charAt(inCursor++)];
131: result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
132: result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
133: result[outCursor++] = (byte) ((ch2 << 6) | ch3);
134: }
135:
136: // Translate partial group, if present
137: if (missingBytesInLastGroup != 0) {
138: int ch0 = alphaToInt[s.charAt(inCursor++)];
139: int ch1 = alphaToInt[s.charAt(inCursor++)];
140: result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
141:
142: if (missingBytesInLastGroup == 1) {
143: int ch2 = alphaToInt[s.charAt(inCursor++)];
144: result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
145: }
146: }
147: return resultLength;
148: }
149:
150: /**
151: * Translates the specified character, which is assumed to be in the "Base
152: * 64 Alphabet" into its equivalent 6-bit positive integer.
153: *
154: * @throws IllegalArgumentException
155: * or ArrayOutOfBoundsException if c is not in the SVNBase64
156: * Alphabet.
157: */
158: /*
159: * private static int base64toInt(char c, byte[] alphaToInt) { int result =
160: * alphaToInt[c]; if (result < 0) throw new
161: * IllegalArgumentException("Illegal character " + c); return result; }
162: */
163:
164: /**
165: * This array is a lookup table that translates unicode characters drawn
166: * from the "SVNBase64 Alphabet" (as specified in Table 1 of RFC 2045) into
167: * their 6-bit positive integer equivalents. Characters that are not in the
168: * SVNBase64 alphabet but fall within the bounds of the array are translated to
169: * -1.
170: */
171: private static final byte base64ToInt[] = { -1, -1, -1, -1, -1, -1,
172: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174: -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
175: 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0,
176: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
177: 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26,
178: 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
179: 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
180:
181: /**
182: * This array is the analogue of base64ToInt, but for the nonstandard
183: * variant that avoids the use of uppercase alphabetic characters.
184: */
185: private static final byte altBase64ToInt[] = { -1, -1, -1, -1, -1,
186: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
187: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
188: 2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1, 52, 53, 54, 55,
189: 56, 57, 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1,
190: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
191: -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, -1, 18, 19, 21, 20,
192: 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
193: 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 22, 23, 24, 25 };
194:
195: }
|