View Javadoc
1   //
2   //  NOTE: The following source code is heavily derived from the
3   //  iHarder.net public domain Base64 library.  See the original at
4   //  http://iharder.sourceforge.net/current/java/base64/
5   //
6   
7   package org.eclipse.jgit.util;
8   
9   import static java.nio.charset.StandardCharsets.UTF_8;
10  
11  import java.text.MessageFormat;
12  import java.util.Arrays;
13  
14  import org.eclipse.jgit.internal.JGitText;
15  
16  /**
17   * Encodes and decodes to and from Base64 notation.
18   * <p>
19   * I am placing this code in the Public Domain. Do with it as you will. This
20   * software comes with no guarantees or warranties but with plenty of
21   * well-wishing instead! Please visit
22   * <a href="http://iharder.net/base64">http://iharder.net/base64</a>
23   * periodically to check for updates or to contribute improvements.
24   * </p>
25   *
26   * @author Robert Harder
27   * @author rob@iharder.net
28   */
29  public class Base64 {
30  	/** The equals sign (=) as a byte. */
31  	private static final byte EQUALS_SIGN = (byte) '=';
32  
33  	/** Indicates equals sign in encoding. */
34  	private static final byte EQUALS_SIGN_DEC = -1;
35  
36  	/** Indicates white space in encoding. */
37  	private static final byte WHITE_SPACE_DEC = -2;
38  
39  	/** Indicates an invalid byte during decoding. */
40  	private static final byte INVALID_DEC = -3;
41  
42  	/** The 64 valid Base64 values. */
43  	private static final byte[] ENC;
44  
45  	/**
46  	 * Translates a Base64 value to either its 6-bit reconstruction value or a
47  	 * negative number indicating some other meaning. The table is only 7 bits
48  	 * wide, as the 8th bit is discarded during decoding.
49  	 */
50  	private static final byte[] DEC;
51  
52  	static {
53  		ENC = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" // //$NON-NLS-1$
54  				+ "abcdefghijklmnopqrstuvwxyz" // //$NON-NLS-1$
55  				+ "0123456789" // //$NON-NLS-1$
56  				+ "+/" // //$NON-NLS-1$
57  		).getBytes(UTF_8);
58  
59  		DEC = new byte[128];
60  		Arrays.fill(DEC, INVALID_DEC);
61  
62  		for (int i = 0; i < 64; i++)
63  			DEC[ENC[i]] = (byte) i;
64  		DEC[EQUALS_SIGN] = EQUALS_SIGN_DEC;
65  
66  		DEC['\t'] = WHITE_SPACE_DEC;
67  		DEC['\n'] = WHITE_SPACE_DEC;
68  		DEC['\r'] = WHITE_SPACE_DEC;
69  		DEC[' '] = WHITE_SPACE_DEC;
70  	}
71  
72  	/** Defeats instantiation. */
73  	private Base64() {
74  		// Suppress empty block warning.
75  	}
76  
77  	/**
78  	 * Encodes up to three bytes of the array <var>source</var> and writes the
79  	 * resulting four Base64 bytes to <var>destination</var>. The source and
80  	 * destination arrays can be manipulated anywhere along their length by
81  	 * specifying <var>srcOffset</var> and <var>destOffset</var>. This method
82  	 * does not check to make sure your arrays are large enough to accommodate
83  	 * <var>srcOffset</var> + 3 for the <var>source</var> array or
84  	 * <var>destOffset</var> + 4 for the <var>destination</var> array. The
85  	 * actual number of significant bytes in your array is given by
86  	 * <var>numSigBytes</var>.
87  	 *
88  	 * @param source
89  	 *            the array to convert
90  	 * @param srcOffset
91  	 *            the index where conversion begins
92  	 * @param numSigBytes
93  	 *            the number of significant bytes in your array
94  	 * @param destination
95  	 *            the array to hold the conversion
96  	 * @param destOffset
97  	 *            the index where output will be put
98  	 */
99  	private static void encode3to4(byte[] source, int srcOffset,
100 			int numSigBytes, byte[] destination, int destOffset) {
101 		// We have to shift left 24 in order to flush out the 1's that appear
102 		// when Java treats a value as negative that is cast from a byte.
103 
104 		int inBuff = 0;
105 		switch (numSigBytes) {
106 		case 3:
107 			inBuff |= (source[srcOffset + 2] << 24) >>> 24;
108 			//$FALL-THROUGH$
109 
110 		case 2:
111 			inBuff |= (source[srcOffset + 1] << 24) >>> 16;
112 			//$FALL-THROUGH$
113 
114 		case 1:
115 			inBuff |= (source[srcOffset] << 24) >>> 8;
116 		}
117 
118 		switch (numSigBytes) {
119 		case 3:
120 			destination[destOffset] = ENC[(inBuff >>> 18)];
121 			destination[destOffset + 1] = ENC[(inBuff >>> 12) & 0x3f];
122 			destination[destOffset + 2] = ENC[(inBuff >>> 6) & 0x3f];
123 			destination[destOffset + 3] = ENC[(inBuff) & 0x3f];
124 			break;
125 
126 		case 2:
127 			destination[destOffset] = ENC[(inBuff >>> 18)];
128 			destination[destOffset + 1] = ENC[(inBuff >>> 12) & 0x3f];
129 			destination[destOffset + 2] = ENC[(inBuff >>> 6) & 0x3f];
130 			destination[destOffset + 3] = EQUALS_SIGN;
131 			break;
132 
133 		case 1:
134 			destination[destOffset] = ENC[(inBuff >>> 18)];
135 			destination[destOffset + 1] = ENC[(inBuff >>> 12) & 0x3f];
136 			destination[destOffset + 2] = EQUALS_SIGN;
137 			destination[destOffset + 3] = EQUALS_SIGN;
138 			break;
139 		}
140 	}
141 
142 	/**
143 	 * Encodes a byte array into Base64 notation.
144 	 *
145 	 * @param source
146 	 *            The data to convert
147 	 * @return encoded base64 representation of source.
148 	 */
149 	public static String encodeBytes(byte[] source) {
150 		return encodeBytes(source, 0, source.length);
151 	}
152 
153 	/**
154 	 * Encodes a byte array into Base64 notation.
155 	 *
156 	 * @param source
157 	 *            The data to convert
158 	 * @param off
159 	 *            Offset in array where conversion should begin
160 	 * @param len
161 	 *            Length of data to convert
162 	 * @return encoded base64 representation of source.
163 	 */
164 	public static String encodeBytes(byte[] source, int off, int len) {
165 		final int len43 = len * 4 / 3;
166 
167 		byte[] outBuff = new byte[len43 + ((len % 3) > 0 ? 4 : 0)];
168 		int d = 0;
169 		int e = 0;
170 		int len2 = len - 2;
171 
172 		for (; d < len2; d += 3, e += 4)
173 			encode3to4(source, d + off, 3, outBuff, e);
174 
175 		if (d < len) {
176 			encode3to4(source, d + off, len - d, outBuff, e);
177 			e += 4;
178 		}
179 
180 		return new String(outBuff, 0, e, UTF_8);
181 	}
182 
183 	/**
184 	 * Decodes four bytes from array <var>source</var> and writes the resulting
185 	 * bytes (up to three of them) to <var>destination</var>. The source and
186 	 * destination arrays can be manipulated anywhere along their length by
187 	 * specifying <var>srcOffset</var> and <var>destOffset</var>. This method
188 	 * does not check to make sure your arrays are large enough to accommodate
189 	 * <var>srcOffset</var> + 4 for the <var>source</var> array or
190 	 * <var>destOffset</var> + 3 for the <var>destination</var> array. This
191 	 * method returns the actual number of bytes that were converted from the
192 	 * Base64 encoding.
193 	 *
194 	 * @param source
195 	 *            the array to convert
196 	 * @param srcOffset
197 	 *            the index where conversion begins
198 	 * @param destination
199 	 *            the array to hold the conversion
200 	 * @param destOffset
201 	 *            the index where output will be put
202 	 * @return the number of decoded bytes converted
203 	 */
204 	private static int decode4to3(byte[] source, int srcOffset,
205 			byte[] destination, int destOffset) {
206 		// Example: Dk==
207 		if (source[srcOffset + 2] == EQUALS_SIGN) {
208 			int outBuff = ((DEC[source[srcOffset]] & 0xFF) << 18)
209 					| ((DEC[source[srcOffset + 1]] & 0xFF) << 12);
210 			destination[destOffset] = (byte) (outBuff >>> 16);
211 			return 1;
212 		}
213 
214 		// Example: DkL=
215 		else if (source[srcOffset + 3] == EQUALS_SIGN) {
216 			int outBuff = ((DEC[source[srcOffset]] & 0xFF) << 18)
217 					| ((DEC[source[srcOffset + 1]] & 0xFF) << 12)
218 					| ((DEC[source[srcOffset + 2]] & 0xFF) << 6);
219 			destination[destOffset] = (byte) (outBuff >>> 16);
220 			destination[destOffset + 1] = (byte) (outBuff >>> 8);
221 			return 2;
222 		}
223 
224 		// Example: DkLE
225 		else {
226 			int outBuff = ((DEC[source[srcOffset]] & 0xFF) << 18)
227 					| ((DEC[source[srcOffset + 1]] & 0xFF) << 12)
228 					| ((DEC[source[srcOffset + 2]] & 0xFF) << 6)
229 					| ((DEC[source[srcOffset + 3]] & 0xFF));
230 
231 			destination[destOffset] = (byte) (outBuff >> 16);
232 			destination[destOffset + 1] = (byte) (outBuff >> 8);
233 			destination[destOffset + 2] = (byte) (outBuff);
234 
235 			return 3;
236 		}
237 	}
238 
239 	/**
240 	 * Low-level decoding ASCII characters from a byte array.
241 	 *
242 	 * @param source
243 	 *            The Base64 encoded data
244 	 * @param off
245 	 *            The offset of where to begin decoding
246 	 * @param len
247 	 *            The length of characters to decode
248 	 * @return decoded data
249 	 * @throws java.lang.IllegalArgumentException
250 	 *             the input is not a valid Base64 sequence.
251 	 */
252 	public static byte[] decode(byte[] source, int off, int len) {
253 		byte[] outBuff = new byte[len * 3 / 4]; // Upper limit on size of output
254 		int outBuffPosn = 0;
255 
256 		byte[] b4 = new byte[4];
257 		int b4Posn = 0;
258 
259 		for (int i = off; i < off + len; i++) {
260 			byte sbiCrop = (byte) (source[i] & 0x7f);
261 			byte sbiDecode = DEC[sbiCrop];
262 
263 			if (EQUALS_SIGN_DEC <= sbiDecode) {
264 				b4[b4Posn++] = sbiCrop;
265 				if (b4Posn > 3) {
266 					outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn);
267 					b4Posn = 0;
268 
269 					// If that was the equals sign, break out of 'for' loop
270 					if (sbiCrop == EQUALS_SIGN)
271 						break;
272 				}
273 
274 			} else if (sbiDecode != WHITE_SPACE_DEC)
275 				throw new IllegalArgumentException(MessageFormat.format(
276 						JGitText.get().badBase64InputCharacterAt,
277 						Integer.valueOf(i), Integer.valueOf(source[i] & 0xff)));
278 		}
279 
280 		if (outBuff.length == outBuffPosn)
281 			return outBuff;
282 
283 		byte[] out = new byte[outBuffPosn];
284 		System.arraycopy(outBuff, 0, out, 0, outBuffPosn);
285 		return out;
286 	}
287 
288 	/**
289 	 * Decodes data from Base64 notation.
290 	 *
291 	 * @param s
292 	 *            the string to decode
293 	 * @return the decoded data
294 	 */
295 	public static byte[] decode(String s) {
296 		byte[] bytes = s.getBytes(UTF_8);
297 		return decode(bytes, 0, bytes.length);
298 	}
299 }