Fri, 04 Oct 2013 16:21:34 +0100
8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | /* FROM mail.jar */ |
aoqi@0 | 27 | package com.sun.xml.internal.org.jvnet.mimepull; |
aoqi@0 | 28 | |
aoqi@0 | 29 | import java.io.*; |
aoqi@0 | 30 | |
aoqi@0 | 31 | /** |
aoqi@0 | 32 | * This class implements a BASE64 Decoder. It is implemented as |
aoqi@0 | 33 | * a FilterInputStream, so one can just wrap this class around |
aoqi@0 | 34 | * any input stream and read bytes from this filter. The decoding |
aoqi@0 | 35 | * is done as the bytes are read out. |
aoqi@0 | 36 | * |
aoqi@0 | 37 | * @author John Mani |
aoqi@0 | 38 | * @author Bill Shannon |
aoqi@0 | 39 | */ |
aoqi@0 | 40 | |
aoqi@0 | 41 | final class BASE64DecoderStream extends FilterInputStream { |
aoqi@0 | 42 | // buffer of decoded bytes for single byte reads |
aoqi@0 | 43 | private byte[] buffer = new byte[3]; |
aoqi@0 | 44 | private int bufsize = 0; // size of the cache |
aoqi@0 | 45 | private int index = 0; // index into the cache |
aoqi@0 | 46 | |
aoqi@0 | 47 | // buffer for almost 8K of typical 76 chars + CRLF lines, |
aoqi@0 | 48 | // used by getByte method. this buffer contains encoded bytes. |
aoqi@0 | 49 | private byte[] input_buffer = new byte[78*105]; |
aoqi@0 | 50 | private int input_pos = 0; |
aoqi@0 | 51 | private int input_len = 0;; |
aoqi@0 | 52 | |
aoqi@0 | 53 | private boolean ignoreErrors = false; |
aoqi@0 | 54 | |
aoqi@0 | 55 | /** |
aoqi@0 | 56 | * Create a BASE64 decoder that decodes the specified input stream. |
aoqi@0 | 57 | * The System property <code>mail.mime.base64.ignoreerrors</code> |
aoqi@0 | 58 | * controls whether errors in the encoded data cause an exception |
aoqi@0 | 59 | * or are ignored. The default is false (errors cause exception). |
aoqi@0 | 60 | * |
aoqi@0 | 61 | * @param in the input stream |
aoqi@0 | 62 | */ |
aoqi@0 | 63 | public BASE64DecoderStream(InputStream in) { |
aoqi@0 | 64 | super(in); |
aoqi@0 | 65 | // default to false |
aoqi@0 | 66 | ignoreErrors = PropUtil.getBooleanSystemProperty( |
aoqi@0 | 67 | "mail.mime.base64.ignoreerrors", false); |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | /** |
aoqi@0 | 71 | * Create a BASE64 decoder that decodes the specified input stream. |
aoqi@0 | 72 | * |
aoqi@0 | 73 | * @param in the input stream |
aoqi@0 | 74 | * @param ignoreErrors ignore errors in encoded data? |
aoqi@0 | 75 | */ |
aoqi@0 | 76 | public BASE64DecoderStream(InputStream in, boolean ignoreErrors) { |
aoqi@0 | 77 | super(in); |
aoqi@0 | 78 | this.ignoreErrors = ignoreErrors; |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | /** |
aoqi@0 | 82 | * Read the next decoded byte from this input stream. The byte |
aoqi@0 | 83 | * is returned as an <code>int</code> in the range <code>0</code> |
aoqi@0 | 84 | * to <code>255</code>. If no byte is available because the end of |
aoqi@0 | 85 | * the stream has been reached, the value <code>-1</code> is returned. |
aoqi@0 | 86 | * This method blocks until input data is available, the end of the |
aoqi@0 | 87 | * stream is detected, or an exception is thrown. |
aoqi@0 | 88 | * |
aoqi@0 | 89 | * @return next byte of data, or <code>-1</code> if the end of the |
aoqi@0 | 90 | * stream is reached. |
aoqi@0 | 91 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 92 | * @see java.io.FilterInputStream#in |
aoqi@0 | 93 | */ |
aoqi@0 | 94 | @Override |
aoqi@0 | 95 | public int read() throws IOException { |
aoqi@0 | 96 | if (index >= bufsize) { |
aoqi@0 | 97 | bufsize = decode(buffer, 0, buffer.length); |
aoqi@0 | 98 | if (bufsize <= 0) { |
aoqi@0 | 99 | return -1; |
aoqi@0 | 100 | } |
aoqi@0 | 101 | index = 0; // reset index into buffer |
aoqi@0 | 102 | } |
aoqi@0 | 103 | return buffer[index++] & 0xff; // Zero off the MSB |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | /** |
aoqi@0 | 107 | * Reads up to <code>len</code> decoded bytes of data from this input stream |
aoqi@0 | 108 | * into an array of bytes. This method blocks until some input is |
aoqi@0 | 109 | * available. |
aoqi@0 | 110 | * <p> |
aoqi@0 | 111 | * |
aoqi@0 | 112 | * @param buf the buffer into which the data is read. |
aoqi@0 | 113 | * @param off the start offset of the data. |
aoqi@0 | 114 | * @param len the maximum number of bytes read. |
aoqi@0 | 115 | * @return the total number of bytes read into the buffer, or |
aoqi@0 | 116 | * <code>-1</code> if there is no more data because the end of |
aoqi@0 | 117 | * the stream has been reached. |
aoqi@0 | 118 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 119 | */ |
aoqi@0 | 120 | @Override |
aoqi@0 | 121 | public int read(byte[] buf, int off, int len) throws IOException { |
aoqi@0 | 122 | // empty out single byte read buffer |
aoqi@0 | 123 | int off0 = off; |
aoqi@0 | 124 | while (index < bufsize && len > 0) { |
aoqi@0 | 125 | buf[off++] = buffer[index++]; |
aoqi@0 | 126 | len--; |
aoqi@0 | 127 | } |
aoqi@0 | 128 | if (index >= bufsize) { |
aoqi@0 | 129 | bufsize = index = 0; |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | int bsize = (len / 3) * 3; // round down to multiple of 3 bytes |
aoqi@0 | 133 | if (bsize > 0) { |
aoqi@0 | 134 | int size = decode(buf, off, bsize); |
aoqi@0 | 135 | off += size; |
aoqi@0 | 136 | len -= size; |
aoqi@0 | 137 | |
aoqi@0 | 138 | if (size != bsize) { // hit EOF? |
aoqi@0 | 139 | if (off == off0) { |
aoqi@0 | 140 | return -1; |
aoqi@0 | 141 | } else { |
aoqi@0 | 142 | return off - off0; |
aoqi@0 | 143 | } |
aoqi@0 | 144 | } |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | // finish up with a partial read if necessary |
aoqi@0 | 148 | for (; len > 0; len--) { |
aoqi@0 | 149 | int c = read(); |
aoqi@0 | 150 | if (c == -1) { |
aoqi@0 | 151 | break; |
aoqi@0 | 152 | } |
aoqi@0 | 153 | buf[off++] = (byte)c; |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | if (off == off0) { |
aoqi@0 | 157 | return -1; |
aoqi@0 | 158 | } else { |
aoqi@0 | 159 | return off - off0; |
aoqi@0 | 160 | } |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | /** |
aoqi@0 | 164 | * Skips over and discards n bytes of data from this stream. |
aoqi@0 | 165 | */ |
aoqi@0 | 166 | @Override |
aoqi@0 | 167 | public long skip(long n) throws IOException { |
aoqi@0 | 168 | long skipped = 0; |
aoqi@0 | 169 | while (n-- > 0 && read() >= 0) { |
aoqi@0 | 170 | skipped++; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | return skipped; |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** |
aoqi@0 | 176 | * Tests if this input stream supports marks. Currently this class |
aoqi@0 | 177 | * does not support marks |
aoqi@0 | 178 | */ |
aoqi@0 | 179 | @Override |
aoqi@0 | 180 | public boolean markSupported() { |
aoqi@0 | 181 | return false; // Maybe later .. |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | /** |
aoqi@0 | 185 | * Returns the number of bytes that can be read from this input |
aoqi@0 | 186 | * stream without blocking. However, this figure is only |
aoqi@0 | 187 | * a close approximation in case the original encoded stream |
aoqi@0 | 188 | * contains embedded CRLFs; since the CRLFs are discarded, not decoded |
aoqi@0 | 189 | */ |
aoqi@0 | 190 | @Override |
aoqi@0 | 191 | public int available() throws IOException { |
aoqi@0 | 192 | // This is only an estimate, since in.available() |
aoqi@0 | 193 | // might include CRLFs too .. |
aoqi@0 | 194 | return ((in.available() * 3)/4 + (bufsize-index)); |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | /** |
aoqi@0 | 198 | * This character array provides the character to value map |
aoqi@0 | 199 | * based on RFC1521. |
aoqi@0 | 200 | */ |
aoqi@0 | 201 | private final static char pem_array[] = { |
aoqi@0 | 202 | 'A','B','C','D','E','F','G','H', // 0 |
aoqi@0 | 203 | 'I','J','K','L','M','N','O','P', // 1 |
aoqi@0 | 204 | 'Q','R','S','T','U','V','W','X', // 2 |
aoqi@0 | 205 | 'Y','Z','a','b','c','d','e','f', // 3 |
aoqi@0 | 206 | 'g','h','i','j','k','l','m','n', // 4 |
aoqi@0 | 207 | 'o','p','q','r','s','t','u','v', // 5 |
aoqi@0 | 208 | 'w','x','y','z','0','1','2','3', // 6 |
aoqi@0 | 209 | '4','5','6','7','8','9','+','/' // 7 |
aoqi@0 | 210 | }; |
aoqi@0 | 211 | |
aoqi@0 | 212 | private final static byte pem_convert_array[] = new byte[256]; |
aoqi@0 | 213 | |
aoqi@0 | 214 | static { |
aoqi@0 | 215 | for (int i = 0; i < 255; i++) { |
aoqi@0 | 216 | pem_convert_array[i] = -1; |
aoqi@0 | 217 | } |
aoqi@0 | 218 | for (int i = 0; i < pem_array.length; i++) { |
aoqi@0 | 219 | pem_convert_array[pem_array[i]] = (byte)i; |
aoqi@0 | 220 | } |
aoqi@0 | 221 | } |
aoqi@0 | 222 | |
aoqi@0 | 223 | /** |
aoqi@0 | 224 | * The decoder algorithm. Most of the complexity here is dealing |
aoqi@0 | 225 | * with error cases. Returns the number of bytes decoded, which |
aoqi@0 | 226 | * may be zero. Decoding is done by filling an int with 4 6-bit |
aoqi@0 | 227 | * values by shifting them in from the bottom and then extracting |
aoqi@0 | 228 | * 3 8-bit bytes from the int by shifting them out from the bottom. |
aoqi@0 | 229 | * |
aoqi@0 | 230 | * @param outbuf the buffer into which to put the decoded bytes |
aoqi@0 | 231 | * @param pos position in the buffer to start filling |
aoqi@0 | 232 | * @param len the number of bytes to fill |
aoqi@0 | 233 | * @return the number of bytes filled, always a multiple |
aoqi@0 | 234 | * of three, and may be zero |
aoqi@0 | 235 | * @exception IOException if the data is incorrectly formatted |
aoqi@0 | 236 | */ |
aoqi@0 | 237 | private int decode(byte[] outbuf, int pos, int len) throws IOException { |
aoqi@0 | 238 | int pos0 = pos; |
aoqi@0 | 239 | while (len >= 3) { |
aoqi@0 | 240 | /* |
aoqi@0 | 241 | * We need 4 valid base64 characters before we start decoding. |
aoqi@0 | 242 | * We skip anything that's not a valid base64 character (usually |
aoqi@0 | 243 | * just CRLF). |
aoqi@0 | 244 | */ |
aoqi@0 | 245 | int got = 0; |
aoqi@0 | 246 | int val = 0; |
aoqi@0 | 247 | while (got < 4) { |
aoqi@0 | 248 | int i = getByte(); |
aoqi@0 | 249 | if (i == -1 || i == -2) { |
aoqi@0 | 250 | boolean atEOF; |
aoqi@0 | 251 | if (i == -1) { |
aoqi@0 | 252 | if (got == 0) { |
aoqi@0 | 253 | return pos - pos0; |
aoqi@0 | 254 | } |
aoqi@0 | 255 | if (!ignoreErrors) { |
aoqi@0 | 256 | throw new DecodingException( |
aoqi@0 | 257 | "BASE64Decoder: Error in encoded stream: " + |
aoqi@0 | 258 | "needed 4 valid base64 characters " + |
aoqi@0 | 259 | "but only got " + got + " before EOF" + |
aoqi@0 | 260 | recentChars()); |
aoqi@0 | 261 | } |
aoqi@0 | 262 | atEOF = true; // don't read any more |
aoqi@0 | 263 | } else { // i == -2 |
aoqi@0 | 264 | // found a padding character, we're at EOF |
aoqi@0 | 265 | // XXX - should do something to make EOF "sticky" |
aoqi@0 | 266 | if (got < 2 && !ignoreErrors) { |
aoqi@0 | 267 | throw new DecodingException( |
aoqi@0 | 268 | "BASE64Decoder: Error in encoded stream: " + |
aoqi@0 | 269 | "needed at least 2 valid base64 characters," + |
aoqi@0 | 270 | " but only got " + got + |
aoqi@0 | 271 | " before padding character (=)" + |
aoqi@0 | 272 | recentChars()); |
aoqi@0 | 273 | } |
aoqi@0 | 274 | |
aoqi@0 | 275 | // didn't get any characters before padding character? |
aoqi@0 | 276 | if (got == 0) { |
aoqi@0 | 277 | return pos - pos0; |
aoqi@0 | 278 | } |
aoqi@0 | 279 | atEOF = false; // need to keep reading |
aoqi@0 | 280 | } |
aoqi@0 | 281 | |
aoqi@0 | 282 | // pad partial result with zeroes |
aoqi@0 | 283 | |
aoqi@0 | 284 | // how many bytes will we produce on output? |
aoqi@0 | 285 | // (got always < 4, so size always < 3) |
aoqi@0 | 286 | int size = got - 1; |
aoqi@0 | 287 | if (size == 0) { |
aoqi@0 | 288 | size = 1; |
aoqi@0 | 289 | } |
aoqi@0 | 290 | |
aoqi@0 | 291 | // handle the one padding character we've seen |
aoqi@0 | 292 | got++; |
aoqi@0 | 293 | val <<= 6; |
aoqi@0 | 294 | |
aoqi@0 | 295 | while (got < 4) { |
aoqi@0 | 296 | if (!atEOF) { |
aoqi@0 | 297 | // consume the rest of the padding characters, |
aoqi@0 | 298 | // filling with zeroes |
aoqi@0 | 299 | i = getByte(); |
aoqi@0 | 300 | if (i == -1) { |
aoqi@0 | 301 | if (!ignoreErrors) { |
aoqi@0 | 302 | throw new DecodingException( |
aoqi@0 | 303 | "BASE64Decoder: Error in encoded " + |
aoqi@0 | 304 | "stream: hit EOF while looking for " + |
aoqi@0 | 305 | "padding characters (=)" + |
aoqi@0 | 306 | recentChars()); |
aoqi@0 | 307 | } |
aoqi@0 | 308 | } else if (i != -2) { |
aoqi@0 | 309 | if (!ignoreErrors) { |
aoqi@0 | 310 | throw new DecodingException( |
aoqi@0 | 311 | "BASE64Decoder: Error in encoded " + |
aoqi@0 | 312 | "stream: found valid base64 " + |
aoqi@0 | 313 | "character after a padding character " + |
aoqi@0 | 314 | "(=)" + recentChars()); |
aoqi@0 | 315 | } |
aoqi@0 | 316 | } |
aoqi@0 | 317 | } |
aoqi@0 | 318 | val <<= 6; |
aoqi@0 | 319 | got++; |
aoqi@0 | 320 | } |
aoqi@0 | 321 | |
aoqi@0 | 322 | // now pull out however many valid bytes we got |
aoqi@0 | 323 | val >>= 8; // always skip first one |
aoqi@0 | 324 | if (size == 2) { |
aoqi@0 | 325 | outbuf[pos + 1] = (byte)(val & 0xff); |
aoqi@0 | 326 | } |
aoqi@0 | 327 | val >>= 8; |
aoqi@0 | 328 | outbuf[pos] = (byte)(val & 0xff); |
aoqi@0 | 329 | // len -= size; // not needed, return below |
aoqi@0 | 330 | pos += size; |
aoqi@0 | 331 | return pos - pos0; |
aoqi@0 | 332 | } else { |
aoqi@0 | 333 | // got a valid byte |
aoqi@0 | 334 | val <<= 6; |
aoqi@0 | 335 | got++; |
aoqi@0 | 336 | val |= i; |
aoqi@0 | 337 | } |
aoqi@0 | 338 | } |
aoqi@0 | 339 | |
aoqi@0 | 340 | // read 4 valid characters, now extract 3 bytes |
aoqi@0 | 341 | outbuf[pos + 2] = (byte)(val & 0xff); |
aoqi@0 | 342 | val >>= 8; |
aoqi@0 | 343 | outbuf[pos + 1] = (byte)(val & 0xff); |
aoqi@0 | 344 | val >>= 8; |
aoqi@0 | 345 | outbuf[pos] = (byte)(val & 0xff); |
aoqi@0 | 346 | len -= 3; |
aoqi@0 | 347 | pos += 3; |
aoqi@0 | 348 | } |
aoqi@0 | 349 | return pos - pos0; |
aoqi@0 | 350 | } |
aoqi@0 | 351 | |
aoqi@0 | 352 | /** |
aoqi@0 | 353 | * Read the next valid byte from the input stream. |
aoqi@0 | 354 | * Buffer lots of data from underlying stream in input_buffer, |
aoqi@0 | 355 | * for efficiency. |
aoqi@0 | 356 | * |
aoqi@0 | 357 | * @return the next byte, -1 on EOF, or -2 if next byte is '=' |
aoqi@0 | 358 | * (padding at end of encoded data) |
aoqi@0 | 359 | */ |
aoqi@0 | 360 | private int getByte() throws IOException { |
aoqi@0 | 361 | int c; |
aoqi@0 | 362 | do { |
aoqi@0 | 363 | if (input_pos >= input_len) { |
aoqi@0 | 364 | try { |
aoqi@0 | 365 | input_len = in.read(input_buffer); |
aoqi@0 | 366 | } catch (EOFException ex) { |
aoqi@0 | 367 | return -1; |
aoqi@0 | 368 | } |
aoqi@0 | 369 | if (input_len <= 0) { |
aoqi@0 | 370 | return -1; |
aoqi@0 | 371 | } |
aoqi@0 | 372 | input_pos = 0; |
aoqi@0 | 373 | } |
aoqi@0 | 374 | // get the next byte in the buffer |
aoqi@0 | 375 | c = input_buffer[input_pos++] & 0xff; |
aoqi@0 | 376 | // is it a padding byte? |
aoqi@0 | 377 | if (c == '=') { |
aoqi@0 | 378 | return -2; |
aoqi@0 | 379 | } |
aoqi@0 | 380 | // no, convert it |
aoqi@0 | 381 | c = pem_convert_array[c]; |
aoqi@0 | 382 | // loop until we get a legitimate byte |
aoqi@0 | 383 | } while (c == -1); |
aoqi@0 | 384 | return c; |
aoqi@0 | 385 | } |
aoqi@0 | 386 | |
aoqi@0 | 387 | /** |
aoqi@0 | 388 | * Return the most recent characters, for use in an error message. |
aoqi@0 | 389 | */ |
aoqi@0 | 390 | private String recentChars() { |
aoqi@0 | 391 | // reach into the input buffer and extract up to 10 |
aoqi@0 | 392 | // recent characters, to help in debugging. |
aoqi@0 | 393 | StringBuilder errstr = new StringBuilder(); |
aoqi@0 | 394 | int nc = input_pos > 10 ? 10 : input_pos; |
aoqi@0 | 395 | if (nc > 0) { |
aoqi@0 | 396 | errstr.append(", the ").append(nc).append(" most recent characters were: \""); |
aoqi@0 | 397 | for (int k = input_pos - nc; k < input_pos; k++) { |
aoqi@0 | 398 | char c = (char)(input_buffer[k] & 0xff); |
aoqi@0 | 399 | switch (c) { |
aoqi@0 | 400 | case '\r': errstr.append("\\r"); break; |
aoqi@0 | 401 | case '\n': errstr.append("\\n"); break; |
aoqi@0 | 402 | case '\t': errstr.append("\\t"); break; |
aoqi@0 | 403 | default: |
aoqi@0 | 404 | if (c >= ' ' && c < 0177) { |
aoqi@0 | 405 | errstr.append(c); |
aoqi@0 | 406 | } else { |
aoqi@0 | 407 | errstr.append("\\").append((int)c); |
aoqi@0 | 408 | } |
aoqi@0 | 409 | } |
aoqi@0 | 410 | } |
aoqi@0 | 411 | errstr.append("\""); |
aoqi@0 | 412 | } |
aoqi@0 | 413 | return errstr.toString(); |
aoqi@0 | 414 | } |
aoqi@0 | 415 | |
aoqi@0 | 416 | /** |
aoqi@0 | 417 | * Base64 decode a byte array. No line breaks are allowed. |
aoqi@0 | 418 | * This method is suitable for short strings, such as those |
aoqi@0 | 419 | * in the IMAP AUTHENTICATE protocol, but not to decode the |
aoqi@0 | 420 | * entire content of a MIME part. |
aoqi@0 | 421 | * |
aoqi@0 | 422 | * NOTE: inbuf may only contain valid base64 characters. |
aoqi@0 | 423 | * Whitespace is not ignored. |
aoqi@0 | 424 | */ |
aoqi@0 | 425 | public static byte[] decode(byte[] inbuf) { |
aoqi@0 | 426 | int size = (inbuf.length / 4) * 3; |
aoqi@0 | 427 | if (size == 0) { |
aoqi@0 | 428 | return inbuf; |
aoqi@0 | 429 | } |
aoqi@0 | 430 | |
aoqi@0 | 431 | if (inbuf[inbuf.length - 1] == '=') { |
aoqi@0 | 432 | size--; |
aoqi@0 | 433 | if (inbuf[inbuf.length - 2] == '=') { |
aoqi@0 | 434 | size--; |
aoqi@0 | 435 | } |
aoqi@0 | 436 | } |
aoqi@0 | 437 | byte[] outbuf = new byte[size]; |
aoqi@0 | 438 | |
aoqi@0 | 439 | int inpos = 0, outpos = 0; |
aoqi@0 | 440 | size = inbuf.length; |
aoqi@0 | 441 | while (size > 0) { |
aoqi@0 | 442 | int val; |
aoqi@0 | 443 | int osize = 3; |
aoqi@0 | 444 | val = pem_convert_array[inbuf[inpos++] & 0xff]; |
aoqi@0 | 445 | val <<= 6; |
aoqi@0 | 446 | val |= pem_convert_array[inbuf[inpos++] & 0xff]; |
aoqi@0 | 447 | val <<= 6; |
aoqi@0 | 448 | if (inbuf[inpos] != '=') { |
aoqi@0 | 449 | val |= pem_convert_array[inbuf[inpos++] & 0xff]; |
aoqi@0 | 450 | } else { |
aoqi@0 | 451 | osize--; |
aoqi@0 | 452 | } |
aoqi@0 | 453 | val <<= 6; |
aoqi@0 | 454 | if (inbuf[inpos] != '=') { |
aoqi@0 | 455 | val |= pem_convert_array[inbuf[inpos++] & 0xff]; |
aoqi@0 | 456 | } else { |
aoqi@0 | 457 | osize--; |
aoqi@0 | 458 | } |
aoqi@0 | 459 | if (osize > 2) { |
aoqi@0 | 460 | outbuf[outpos + 2] = (byte)(val & 0xff); |
aoqi@0 | 461 | } |
aoqi@0 | 462 | val >>= 8; |
aoqi@0 | 463 | if (osize > 1) { |
aoqi@0 | 464 | outbuf[outpos + 1] = (byte)(val & 0xff); |
aoqi@0 | 465 | } |
aoqi@0 | 466 | val >>= 8; |
aoqi@0 | 467 | outbuf[outpos] = (byte)(val & 0xff); |
aoqi@0 | 468 | outpos += osize; |
aoqi@0 | 469 | size -= 4; |
aoqi@0 | 470 | } |
aoqi@0 | 471 | return outbuf; |
aoqi@0 | 472 | } |
aoqi@0 | 473 | |
aoqi@0 | 474 | /*** begin TEST program *** |
aoqi@0 | 475 | public static void main(String argv[]) throws Exception { |
aoqi@0 | 476 | FileInputStream infile = new FileInputStream(argv[0]); |
aoqi@0 | 477 | BASE64DecoderStream decoder = new BASE64DecoderStream(infile); |
aoqi@0 | 478 | int c; |
aoqi@0 | 479 | |
aoqi@0 | 480 | while ((c = decoder.read()) != -1) |
aoqi@0 | 481 | System.out.print((char)c); |
aoqi@0 | 482 | System.out.flush(); |
aoqi@0 | 483 | } |
aoqi@0 | 484 | *** end TEST program ***/ |
aoqi@0 | 485 | } |