1 /*
2  * Copyright (c) 1995, 2011, 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 package sun.misc;
26 
27 import java.io.OutputStream;
28 import java.io.PushbackInputStream;
29 import java.io.PrintStream;
30 
31 /**
32  * This class implements a BASE64 Character decoder as specified in RFC1521.
33  *
34  * This RFC is part of the MIME specification which is published by the
35  * Internet Engineering Task Force (IETF). Unlike some other encoding
36  * schemes there is nothing in this encoding that tells the decoder
37  * where a buffer starts or stops, so to use it you will need to isolate
38  * your encoded data into a single chunk and then feed them this decoder.
39  * The simplest way to do that is to read all of the encoded data into a
40  * string and then use:
41  * <pre>
42  *      byte    mydata[];
43  *      BASE64Decoder base64 = new BASE64Decoder();
44  *
45  *      mydata = base64.decodeBuffer(bufferString);
46  * </pre>
47  * This will decode the String in <i>bufferString</i> and give you an array
48  * of bytes in the array <i>myData</i>.
49  *
50  * On errors, this class throws a CEFormatException with the following detail
51  * strings:
52  * <pre>
53  *    "BASE64Decoder: Not enough bytes for an atom."
54  * </pre>
55  *
56  * @author      Chuck McManis
57  * @see         CharacterEncoder
58  * @see         BASE64Decoder
59  */
60 
61 public class BASE64Decoder extends CharacterDecoder {
62 
63     /** This class has 4 bytes per atom */
bytesPerAtom()64     protected int bytesPerAtom() {
65         return (4);
66     }
67 
68     /** Any multiple of 4 will do, 72 might be common */
bytesPerLine()69     protected int bytesPerLine() {
70         return (72);
71     }
72 
73     /**
74      * This character array provides the character to value map
75      * based on RFC1521.
76      */
77     private final static char pem_array[] = {
78         //       0   1   2   3   4   5   6   7
79                 'A','B','C','D','E','F','G','H', // 0
80                 'I','J','K','L','M','N','O','P', // 1
81                 'Q','R','S','T','U','V','W','X', // 2
82                 'Y','Z','a','b','c','d','e','f', // 3
83                 'g','h','i','j','k','l','m','n', // 4
84                 'o','p','q','r','s','t','u','v', // 5
85                 'w','x','y','z','0','1','2','3', // 6
86                 '4','5','6','7','8','9','+','/'  // 7
87         };
88 
89     private final static byte pem_convert_array[] = new byte[256];
90 
91     static {
92         for (int i = 0; i < 255; i++) {
93             pem_convert_array[i] = -1;
94         }
95         for (int i = 0; i < pem_array.length; i++) {
96             pem_convert_array[pem_array[i]] = (byte) i;
97         }
98     }
99 
100     byte decode_buffer[] = new byte[4];
101 
102     /**
103      * Decode one BASE64 atom into 1, 2, or 3 bytes of data.
104      */
105     @SuppressWarnings("fallthrough")
decodeAtom(PushbackInputStream inStream, OutputStream outStream, int rem)106     protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int rem)
107         throws java.io.IOException
108     {
109         int     i;
110         byte    a = -1, b = -1, c = -1, d = -1;
111 
112         if (rem < 2) {
113             throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");
114         }
115         do {
116             i = inStream.read();
117             if (i == -1) {
118                 throw new CEStreamExhausted();
119             }
120         } while (i == '\n' || i == '\r');
121         decode_buffer[0] = (byte) i;
122 
123         i = readFully(inStream, decode_buffer, 1, rem-1);
124         if (i == -1) {
125             throw new CEStreamExhausted();
126         }
127 
128         if (rem > 3 && decode_buffer[3] == '=') {
129             rem = 3;
130         }
131         if (rem > 2 && decode_buffer[2] == '=') {
132             rem = 2;
133         }
134         switch (rem) {
135         case 4:
136             d = pem_convert_array[decode_buffer[3] & 0xff];
137             // NOBREAK
138         case 3:
139             c = pem_convert_array[decode_buffer[2] & 0xff];
140             // NOBREAK
141         case 2:
142             b = pem_convert_array[decode_buffer[1] & 0xff];
143             a = pem_convert_array[decode_buffer[0] & 0xff];
144             break;
145         }
146 
147         switch (rem) {
148         case 2:
149             outStream.write( (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
150             break;
151         case 3:
152             outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
153             outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
154             break;
155         case 4:
156             outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
157             outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
158             outStream.write( (byte) (((c << 6) & 0xc0) | (d  & 0x3f)) );
159             break;
160         }
161         return;
162     }
163 }
164