|
| 1 | +/** |
| 2 | + * Copyright 2005-2014 Restlet |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of one of the following |
| 5 | + * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can |
| 6 | + * select the license that you prefer but you may not use this file except in |
| 7 | + * compliance with one of these Licenses. |
| 8 | + * |
| 9 | + * You can obtain a copy of the Apache 2.0 license at |
| 10 | + * http://www.opensource.org/licenses/apache-2.0 |
| 11 | + * |
| 12 | + * You can obtain a copy of the EPL 1.0 license at |
| 13 | + * http://www.opensource.org/licenses/eclipse-1.0 |
| 14 | + * |
| 15 | + * See the Licenses for the specific language governing permissions and |
| 16 | + * limitations under the Licenses. |
| 17 | + * |
| 18 | + * Alternatively, you can obtain a royalty free commercial license with less |
| 19 | + * limitations, transferable or non-transferable, directly at |
| 20 | + * http://restlet.com/products/restlet-framework |
| 21 | + * |
| 22 | + * Restlet is a registered trademark of Restlet S.A.S. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.restlet.engine.util; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | + |
| 29 | +import org.restlet.engine.io.IoUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * Minimal but fast Base64 codec. |
| 33 | + * |
| 34 | + * @author Ray Waldin (ray@waldin.net) |
| 35 | + */ |
| 36 | +public class Base64 { |
| 37 | + |
| 38 | + /** alphabet used for encoding bytes into base64 */ |
| 39 | + private static final char[] BASE64_DIGITS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 40 | + .toCharArray(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Decoding involves replacing each character with the character's value, or |
| 44 | + * position, from the above alphabet, and this table makes such lookups |
| 45 | + * quick and easy. Couldn't help myself with the corny name :) |
| 46 | + */ |
| 47 | + private static final byte[] DECODER_RING = new byte[128]; |
| 48 | + |
| 49 | + /** |
| 50 | + * Initializes the decoder ring. |
| 51 | + */ |
| 52 | + static { |
| 53 | + Arrays.fill(DECODER_RING, (byte) -1); |
| 54 | + int i = 0; |
| 55 | + for (final char c : BASE64_DIGITS) { |
| 56 | + DECODER_RING[c] = (byte) i++; |
| 57 | + } |
| 58 | + DECODER_RING['='] = 0; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the byte value at a given position in a bytes array. |
| 63 | + * |
| 64 | + * @param data |
| 65 | + * The bytes array. |
| 66 | + * @param block |
| 67 | + * The block size. |
| 68 | + * @param off |
| 69 | + * The offset value. |
| 70 | + * @return The extracted byte. |
| 71 | + */ |
| 72 | + private final static int byteAt(byte[] data, int block, int off) { |
| 73 | + return unsign(data[(block * 3) + off]); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Decodes base64 characters into bytes. Newline characters found at block |
| 78 | + * boundaries will be ignored. |
| 79 | + * |
| 80 | + * @param chars |
| 81 | + * The characters array to decode. |
| 82 | + * @return The decoded byte array. |
| 83 | + */ |
| 84 | + public static byte[] decode(final char[] chars) { |
| 85 | + // prepare to ignore newline chars |
| 86 | + int newlineCount = 0; |
| 87 | + for (char c : chars) { |
| 88 | + switch (c) { |
| 89 | + case '\r': |
| 90 | + case '\n': |
| 91 | + newlineCount++; |
| 92 | + break; |
| 93 | + default: |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + int len = chars.length - newlineCount; |
| 98 | + |
| 99 | + if (len % 4 != 0) { |
| 100 | + throw new IllegalArgumentException( |
| 101 | + "Base64.decode() requires input length to be a multiple of 4"); |
| 102 | + } |
| 103 | + |
| 104 | + int numBytes = ((len + 3) / 4) * 3; |
| 105 | + |
| 106 | + // fix up length relative to padding |
| 107 | + if (len > 1) { |
| 108 | + if (chars[chars.length - 2] == '=') { |
| 109 | + numBytes -= 2; |
| 110 | + } else if (chars[chars.length - 1] == '=') { |
| 111 | + numBytes--; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + byte[] result = new byte[numBytes]; |
| 116 | + int newlineOffset = 0; |
| 117 | + |
| 118 | + // decode each block of 4 chars into 3 bytes |
| 119 | + for (int i = 0; i < (len + 3) / 4; ++i) { |
| 120 | + int charOffset = newlineOffset + (i * 4); |
| 121 | + |
| 122 | + final char c1 = chars[charOffset++]; |
| 123 | + final char c2 = chars[charOffset++]; |
| 124 | + final char c3 = chars[charOffset++]; |
| 125 | + final char c4 = chars[charOffset++]; |
| 126 | + |
| 127 | + if (!(validChar(c1) && validChar(c2) && validChar(c3) && validChar(c4))) { |
| 128 | + throw new IllegalArgumentException( |
| 129 | + "Invalid Base64 character in block: '" + c1 + c2 + c3 |
| 130 | + + c4 + "'"); |
| 131 | + } |
| 132 | + |
| 133 | + // pack |
| 134 | + final int x = DECODER_RING[c1] << 18 | DECODER_RING[c2] << 12 |
| 135 | + | (c3 == '=' ? 0 : DECODER_RING[c3] << 6) |
| 136 | + | (c4 == '=' ? 0 : DECODER_RING[c4]); |
| 137 | + |
| 138 | + // unpack |
| 139 | + int byteOffset = i * 3; |
| 140 | + result[byteOffset++] = (byte) (x >> 16); |
| 141 | + if (c3 != '=') { |
| 142 | + result[byteOffset++] = (byte) ((x >> 8) & 0xFF); |
| 143 | + if (c4 != '=') { |
| 144 | + result[byteOffset++] = (byte) (x & 0xFF); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // skip newlines after block |
| 149 | + outer: while (chars.length > charOffset) { |
| 150 | + switch (chars[charOffset++]) { |
| 151 | + case '\r': |
| 152 | + case '\n': |
| 153 | + newlineOffset++; |
| 154 | + break; |
| 155 | + |
| 156 | + default: |
| 157 | + break outer; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + return result; |
| 162 | + }; |
| 163 | + |
| 164 | + /** |
| 165 | + * Decodes a base64 string into bytes. Newline characters found at block |
| 166 | + * boundaries will be ignored. |
| 167 | + * |
| 168 | + * @param encodedString |
| 169 | + * The string to decode. |
| 170 | + * @return The decoded byte array. |
| 171 | + */ |
| 172 | + public static byte[] decode(String encodedString) { |
| 173 | + return decode(encodedString.toCharArray()); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Encodes an entire byte array into a Base64 string, with optional newlines |
| 178 | + * after every 76 characters. |
| 179 | + * |
| 180 | + * @param bytes |
| 181 | + * The byte array to encode. |
| 182 | + * @param newlines |
| 183 | + * Indicates whether or not newlines are desired. |
| 184 | + * @return The encoded string. |
| 185 | + */ |
| 186 | + public static String encode(byte[] bytes, boolean newlines) { |
| 187 | + return encode(bytes, 0, bytes.length, newlines); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Encodes specified bytes into a Base64 string, with optional newlines |
| 192 | + * after every 76 characters. |
| 193 | + * |
| 194 | + * @param bytes |
| 195 | + * The byte array to encode. |
| 196 | + * @param off |
| 197 | + * The starting offset. |
| 198 | + * @param len |
| 199 | + * The number of bytes to encode. |
| 200 | + * @param newlines |
| 201 | + * Indicates whether or not newlines are desired. |
| 202 | + * |
| 203 | + * @return The encoded string. |
| 204 | + */ |
| 205 | + public static String encode(byte[] bytes, int off, int len, boolean newlines) { |
| 206 | + char[] output = new char[(((len + 2) / 3) * 4) |
| 207 | + + (newlines ? len / 43 : 0)]; |
| 208 | + int pos = 0; |
| 209 | + |
| 210 | + // encode each block of 3 bytes into 4 chars |
| 211 | + for (int i = 0; i < (len + 2) / 3; ++i) { |
| 212 | + int pad = 0; |
| 213 | + |
| 214 | + if (len + 1 < (i + 1) * 3) { |
| 215 | + // two trailing '='s |
| 216 | + pad = 2; |
| 217 | + } else if (len < (i + 1) * 3) { |
| 218 | + // one trailing '=' |
| 219 | + pad = 1; |
| 220 | + } |
| 221 | + |
| 222 | + // pack |
| 223 | + int x = (byteAt(bytes, i, off) << 16) |
| 224 | + | (pad > 1 ? 0 : (byteAt(bytes, i, off + 1) << 8)) |
| 225 | + | (pad > 0 ? 0 : (byteAt(bytes, i, off + 2))); |
| 226 | + |
| 227 | + // unpack |
| 228 | + output[pos++] = BASE64_DIGITS[x >> 18]; |
| 229 | + output[pos++] = BASE64_DIGITS[(x >> 12) & 0x3F]; |
| 230 | + output[pos++] = pad > 1 ? '=' : BASE64_DIGITS[(x >> 6) & 0x3F]; |
| 231 | + output[pos++] = pad > 0 ? '=' : BASE64_DIGITS[x & 0x3F]; |
| 232 | + |
| 233 | + if (newlines && ((i + 1) % 19 == 0)) { |
| 234 | + output[pos++] = '\n'; |
| 235 | + } |
| 236 | + } |
| 237 | + return new String(output, 0, pos); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Computes the unsigned value of a byte. |
| 242 | + * |
| 243 | + * @param b |
| 244 | + * The input byte. |
| 245 | + * @return The output unsigned value. |
| 246 | + */ |
| 247 | + private final static int unsign(byte b) { |
| 248 | + return b < 0 ? b + 256 : b; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Indicates if the character is valid and can be decoded. |
| 253 | + * |
| 254 | + * @param c |
| 255 | + * The input character. |
| 256 | + * @return True if the character is valid. |
| 257 | + */ |
| 258 | + private final static boolean validChar(char c) { |
| 259 | + return (c < 128) && (DECODER_RING[c] != -1); |
| 260 | + } |
| 261 | +} |
0 commit comments