1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * @(#)BASE64DecoderStream.java 1.8 02/03/27 28 */ 29 30 31 32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util; 33 34 import java.io.*; 35 36 /** 37 * This class implements a BASE64 Decoder. It is implemented as 38 * a FilterInputStream, so one can just wrap this class around 39 * any input stream and read bytes from this filter. The decoding 40 * is done as the bytes are read out. 41 * 42 * @author John Mani 43 * @author Bill Shannon 44 */ 45 46 public class BASE64DecoderStream extends FilterInputStream { 47 private byte[] buffer; // cache of decoded bytes 48 private int bufsize = 0; // size of the cache 49 private int index = 0; // index into the cache 50 51 /** 52 * Create a BASE64 decoder that decodes the specified input stream 53 * @param in the input stream 54 */ 55 public BASE64DecoderStream(InputStream in) { 56 super(in); 57 buffer = new byte[3]; 58 } 59 60 /** 61 * Read the next decoded byte from this input stream. The byte 62 * is returned as an <code>int</code> in the range <code>0</code> 63 * to <code>255</code>. If no byte is available because the end of 64 * the stream has been reached, the value <code>-1</code> is returned. 65 * This method blocks until input data is available, the end of the 66 * stream is detected, or an exception is thrown. 67 * 68 * @return next byte of data, or <code>-1</code> if the end of the 69 * stream is reached. 70 * @exception IOException if an I/O error occurs. 71 * @see java.io.FilterInputStream#in 72 */ 73 @Override 74 public int read() throws IOException { 75 if (index >= bufsize) { 76 decode(); // Fills up buffer 77 if (bufsize == 0) // buffer is empty 78 return -1; 79 index = 0; // reset index into buffer 80 } 81 return buffer[index++] & 0xff; // Zero off the MSB 82 } 83 84 /** 85 * Reads up to <code>len</code> decoded bytes of data from this input stream 86 * into an array of bytes. This method blocks until some input is 87 * available. 88 * <p> 89 * 90 * @param buf the buffer into which the data is read. 91 * @param off the start offset of the data. 92 * @param len the maximum number of bytes read. 93 * @return the total number of bytes read into the buffer, or 94 * <code>-1</code> if there is no more data because the end of 95 * the stream has been reached. 96 * @exception IOException if an I/O error occurs. 97 */ 98 @Override 99 public int read(byte[] buf, int off, int len) throws IOException { 100 int i, c; 101 for (i = 0; i < len; i++) { 102 if ((c = read()) == -1) { 103 if (i == 0) // At end of stream, so we should 104 i = -1; // return -1 , NOT 0. 105 break; 106 } 107 buf[off+i] = (byte)c; 108 } 109 110 return i; 111 } 112 113 /** 114 * Tests if this input stream supports marks. Currently this class 115 * does not support marks 116 */ 117 @Override 118 public boolean markSupported() { 119 return false; // Maybe later .. 120 } 121 122 /** 123 * Returns the number of bytes that can be read from this input 124 * stream without blocking. However, this figure is only 125 * a close approximation in case the original encoded stream 126 * contains embedded CRLFs; since the CRLFs are discarded, not decoded 127 */ 128 @Override 129 public int available() throws IOException { 130 // This is only an estimate, since in.available() 131 // might include CRLFs too .. 132 return ((in.available() * 3)/4 + (bufsize-index)); 133 } 134 135 /** 136 * This character array provides the character to value map 137 * based on RFC1521. 138 */ 139 private final static char pem_array[] = { 140 'A','B','C','D','E','F','G','H', // 0 141 'I','J','K','L','M','N','O','P', // 1 142 'Q','R','S','T','U','V','W','X', // 2 143 'Y','Z','a','b','c','d','e','f', // 3 144 'g','h','i','j','k','l','m','n', // 4 145 'o','p','q','r','s','t','u','v', // 5 146 'w','x','y','z','0','1','2','3', // 6 147 '4','5','6','7','8','9','+','/' // 7 148 }; 149 150 private final static byte pem_convert_array[] = new byte[256]; 151 152 static { 153 for (int i = 0; i < 255; i++) 154 pem_convert_array[i] = -1; 155 for(int i = 0; i < pem_array.length; i++) 156 pem_convert_array[pem_array[i]] = (byte) i; 157 } 158 159 /* The decoder algorithm */ 160 private byte[] decode_buffer = new byte[4]; 161 private void decode() throws IOException { 162 bufsize = 0; 163 /* 164 * We need 4 valid base64 characters before we start decoding. 165 * We skip anything that's not a valid base64 character (usually 166 * just CRLF). 167 */ 168 int got = 0; 169 while (got < 4) { 170 int i = in.read(); 171 if (i == -1) { 172 if (got == 0) 173 return; // EOF before any data is ok 174 throw new IOException("Error in encoded stream, got " + got); 175 } 176 if (i >= 0 && i < 256 && i == '=' || pem_convert_array[i] != -1) 177 decode_buffer[got++] = (byte)i; 178 } 179 180 byte a, b; 181 a = pem_convert_array[decode_buffer[0] & 0xff]; 182 b = pem_convert_array[decode_buffer[1] & 0xff]; 183 // The first decoded byte 184 buffer[bufsize++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)); 185 186 if (decode_buffer[2] == '=') // End of this BASE64 encoding 187 return; 188 a = b; 189 b = pem_convert_array[decode_buffer[2] & 0xff]; 190 // The second decoded byte 191 buffer[bufsize++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf)); 192 193 if (decode_buffer[3] == '=') // End of this BASE64 encoding 194 return; 195 a = b; 196 b = pem_convert_array[decode_buffer[3] & 0xff]; 197 // The third decoded byte 198 buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); 199 } 200 201 /** 202 * Base64 decode a byte array. No line breaks are allowed. 203 * This method is suitable for short strings, such as those 204 * in the IMAP AUTHENTICATE protocol, but not to decode the 205 * entire content of a MIME part. 206 * 207 * @param inbuf byte array to decode 208 * 209 * @return decoded byte array 210 * 211 * NOTE: inbuf may only contain valid base64 characters. 212 * Whitespace is not ignored. 213 */ 214 public static byte[] decode(byte[] inbuf) { 215 int size = (inbuf.length / 4) * 3; 216 if (size == 0) 217 return inbuf; 218 219 if (inbuf[inbuf.length - 1] == '=') { 220 size--; 221 if (inbuf[inbuf.length - 2] == '=') 222 size--; 223 } 224 byte[] outbuf = new byte[size]; 225 226 int inpos = 0, outpos = 0; 227 size = inbuf.length; 228 while (size > 0) { 229 byte a, b; 230 a = pem_convert_array[inbuf[inpos++] & 0xff]; 231 b = pem_convert_array[inbuf[inpos++] & 0xff]; 232 // The first decoded byte 233 outbuf[outpos++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)); 234 235 if (inbuf[inpos] == '=') // End of this BASE64 encoding 236 return outbuf; 237 a = b; 238 b = pem_convert_array[inbuf[inpos++] & 0xff]; 239 // The second decoded byte 240 outbuf[outpos++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf)); 241 242 if (inbuf[inpos] == '=') // End of this BASE64 encoding 243 return outbuf; 244 a = b; 245 b = pem_convert_array[inbuf[inpos++] & 0xff]; 246 // The third decoded byte 247 outbuf[outpos++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); 248 size -= 4; 249 } 250 return outbuf; 251 } 252 253 /*** begin TEST program *** 254 public static void main(String argv[]) throws Exception { 255 FileInputStream infile = new FileInputStream(argv[0]); 256 BASE64DecoderStream decoder = new BASE64DecoderStream(infile); 257 int c; 258 259 while ((c = decoder.read()) != -1) 260 System.out.print((char)c); 261 System.out.flush(); 262 } 263 *** end TEST program ***/ 264 }