Debian Codegraph @

src/com/adobe/internal/xmp/impl/Base64.java

raw symbols SCIP
1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2001 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9
10 package com.adobe.internal.xmp.impl;
11
12
13
14
15 /**
16 * A utility class to perform base64 encoding and decoding as specified
17 * in RFC-1521. See also RFC 1421.
18 *
19 * @author Mike P. Tardif
20 * @version $Revision: 1.4 $
21 */
22 public class Base64
23 {
24 /** marker for invalid bytes */
25 private static final byte INVALID = -1;
26 /** marker for accepted whitespace bytes */
27 private static final byte WHITESPACE = -2;
28 /** marker for an equal symbol */
29 private static final byte EQUAL = -3;
30
31 /** */
32 private static byte[] base64 = {
33 (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', // 0 to 3
34 (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', // 4 to 7
35 (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', // 8 to 11
36 (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', // 11 to 15
37 (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', // 16 to 19
38 (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', // 20 to 23
39 (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', // 24 to 27
40 (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', // 28 to 31
41 (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', // 32 to 35
42 (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', // 36 to 39
43 (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', // 40 to 43
44 (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', // 44 to 47
45 (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', // 48 to 51
46 (byte) '0', (byte) '1', (byte) '2', (byte) '3', // 52 to 55
47 (byte) '4', (byte) '5', (byte) '6', (byte) '7', // 56 to 59
48 (byte) '8', (byte) '9', (byte) '+', (byte) '/' // 60 to 63
49 };
50 /** */
51 private static byte[] ascii = new byte[255];
52 /** */
53 static {
54 // not valid bytes
55 for (int idx = 0; idx < 255; idx++)
56 {
57 ascii[idx] = INVALID;
58 }
59 // valid bytes
60 for (int idx = 0; idx < base64.length; idx++)
61 {
62 ascii[base64[idx]] = (byte) idx;
63 }
64 // whitespaces
65 ascii[0x09] = WHITESPACE;
66 ascii[0x0A] = WHITESPACE;
67 ascii[0x0D] = WHITESPACE;
68 ascii[0x20] = WHITESPACE;
69
70 // trailing equals
71 ascii[0x3d] = EQUAL;
72 }
73
74
75 /**
76 * Encode the given byte[].
77 *
78 * @param src the source string.
79 * @return the base64-encoded data.
80 */
81 public static final byte[] encode(byte[] src)
82 {
83 return encode(src, 0);
84 }
85
86
87 /**
88 * Encode the given byte[].
89 *
90 * @param src the source string.
91 * @param lineFeed a linefeed is added after <code>linefeed</code> characters;
92 * must be dividable by four; 0 means no linefeeds
93 * @return the base64-encoded data.
94 */
95 public static final byte[] encode(byte[] src, int lineFeed)
96 {
97 // linefeed must be dividable by 4
98 lineFeed = lineFeed / 4 * 4;
99 if (lineFeed < 0)
100 {
101 lineFeed = 0;
102 }
103
104 // determine code length
105 int codeLength = ((src.length + 2) / 3) * 4;
106 if (lineFeed > 0)
107 {
108 codeLength += (codeLength - 1) / lineFeed;
109 }
110
111 byte[] dst = new byte[codeLength];
112 int bits24;
113 int bits6;
114 //
115 // Do 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
116 //
117 int didx = 0;
118 int sidx = 0;
119 int lf = 0;
120 while (sidx + 3 <= src.length)
121 {
122 bits24 = (src[sidx++] & 0xFF) << 16;
123 bits24 |= (src[sidx++] & 0xFF) << 8;
124 bits24 |= (src[sidx++] & 0xFF) << 0;
125 bits6 = (bits24 & 0x00FC0000) >> 18;
126 dst[didx++] = base64[bits6];
127 bits6 = (bits24 & 0x0003F000) >> 12;
128 dst[didx++] = base64[bits6];
129 bits6 = (bits24 & 0x00000FC0) >> 6;
130 dst[didx++] = base64[bits6];
131 bits6 = (bits24 & 0x0000003F);
132 dst[didx++] = base64[bits6];
133
134 lf += 4;
135 if (didx < codeLength && lineFeed > 0 && lf % lineFeed == 0)
136 {
137 dst[didx++] = 0x0A;
138 }
139 }
140 if (src.length - sidx == 2)
141 {
142 bits24 = (src[sidx ] & 0xFF) << 16;
143 bits24 |= (src[sidx + 1] & 0xFF) << 8;
144 bits6 = (bits24 & 0x00FC0000) >> 18;
145 dst[didx++] = base64[bits6];
146 bits6 = (bits24 & 0x0003F000) >> 12;
147 dst[didx++] = base64[bits6];
148 bits6 = (bits24 & 0x00000FC0) >> 6;
149 dst[didx++] = base64[bits6];
150 dst[didx++] = (byte) '=';
151 }
152 else if (src.length - sidx == 1)
153 {
154 bits24 = (src[sidx] & 0xFF) << 16;
155 bits6 = (bits24 & 0x00FC0000) >> 18;
156 dst[didx++] = base64[bits6];
157 bits6 = (bits24 & 0x0003F000) >> 12;
158 dst[didx++] = base64[bits6];
159 dst[didx++] = (byte) '=';
160 dst[didx++] = (byte) '=';
161 }
162 return dst;
163 }
164
165
166 /**
167 * Encode the given string.
168 * @param src the source string.
169 * @return the base64-encoded string.
170 */
171 public static final String encode(String src)
172 {
173 return new String(encode(src.getBytes()));
174 }
175
176
177 /**
178 * Decode the given byte[].
179 *
180 * @param src
181 * the base64-encoded data.
182 * @return the decoded data.
183 * @throws IllegalArgumentException Thrown if the base 64 strings contains non-valid characters,
184 * beside the bas64 chars, LF, CR, tab and space are accepted.
185 */
186 public static final byte[] decode(byte[] src) throws IllegalArgumentException
187 {
188 //
189 // Do ascii printable to 0-63 conversion.
190 //
191 int sidx;
192 int srcLen = 0;
193 for (sidx = 0; sidx < src.length; sidx++)
194 {
195 byte val = ascii[src[sidx]];
196 if (val >= 0)
197 {
198 src[srcLen++] = val;
199 }
200 else if (val == INVALID)
201 {
202 throw new IllegalArgumentException("Invalid base 64 string");
203 }
204 }
205
206 //
207 // Trim any padding.
208 //
209 while (srcLen > 0 && src[srcLen - 1] == EQUAL)
210 {
211 srcLen--;
212 }
213 byte[] dst = new byte[srcLen * 3 / 4];
214
215 //
216 // Do 4-byte to 3-byte conversion.
217 //
218 int didx;
219 for (sidx = 0, didx = 0; didx < dst.length - 2; sidx += 4, didx += 3)
220 {
221 dst[didx ] = (byte) (((src[sidx ] << 2) & 0xFF)
222 | ((src[sidx + 1] >>> 4) & 0x03));
223 dst[didx + 1] = (byte) (((src[sidx + 1] << 4) & 0xFF)
224 | ((src[sidx + 2] >>> 2) & 0x0F));
225 dst[didx + 2] = (byte) (((src[sidx + 2] << 6) & 0xFF)
226 | ((src[sidx + 3]) & 0x3F));
227 }
228 if (didx < dst.length)
229 {
230 dst[didx] = (byte) (((src[sidx ] << 2) & 0xFF)
231 | ((src[sidx + 1] >>> 4) & 0x03));
232 }
233 if (++didx < dst.length)
234 {
235 dst[didx] = (byte) (((src[sidx + 1] << 4) & 0xFF)
236 | ((src[sidx + 2] >>> 2) & 0x0F));
237 }
238 return dst;
239 }
240
241
242 /**
243 * Decode the given string.
244 *
245 * @param src the base64-encoded string.
246 * @return the decoded string.
247 */
248 public static final String decode(String src)
249 {
250 return new String(decode(src.getBytes()));
251 }
252 }