0N/A/*
2362N/A * Copyright (c) 1995, 2000, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.misc;
0N/A
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.PushbackInputStream;
0N/Aimport java.io.PrintStream;
0N/A
0N/A/**
0N/A * This class implements a BASE64 Character decoder as specified in RFC1521.
0N/A *
0N/A * This RFC is part of the MIME specification which is published by the
0N/A * Internet Engineering Task Force (IETF). Unlike some other encoding
0N/A * schemes there is nothing in this encoding that tells the decoder
0N/A * where a buffer starts or stops, so to use it you will need to isolate
0N/A * your encoded data into a single chunk and then feed them this decoder.
0N/A * The simplest way to do that is to read all of the encoded data into a
0N/A * string and then use:
0N/A * <pre>
0N/A * byte mydata[];
0N/A * BASE64Decoder base64 = new BASE64Decoder();
0N/A *
0N/A * mydata = base64.decodeBuffer(bufferString);
0N/A * </pre>
0N/A * This will decode the String in <i>bufferString</i> and give you an array
0N/A * of bytes in the array <i>myData</i>.
0N/A *
0N/A * On errors, this class throws a CEFormatException with the following detail
0N/A * strings:
0N/A * <pre>
0N/A * "BASE64Decoder: Not enough bytes for an atom."
0N/A * </pre>
0N/A *
0N/A * @author Chuck McManis
0N/A * @see CharacterEncoder
0N/A * @see BASE64Decoder
0N/A */
0N/A
0N/Apublic class BASE64Decoder extends CharacterDecoder {
0N/A
0N/A /** This class has 4 bytes per atom */
0N/A protected int bytesPerAtom() {
0N/A return (4);
0N/A }
0N/A
0N/A /** Any multiple of 4 will do, 72 might be common */
0N/A protected int bytesPerLine() {
0N/A return (72);
0N/A }
0N/A
0N/A /**
0N/A * This character array provides the character to value map
0N/A * based on RFC1521.
0N/A */
0N/A private final static char pem_array[] = {
0N/A // 0 1 2 3 4 5 6 7
0N/A 'A','B','C','D','E','F','G','H', // 0
0N/A 'I','J','K','L','M','N','O','P', // 1
0N/A 'Q','R','S','T','U','V','W','X', // 2
0N/A 'Y','Z','a','b','c','d','e','f', // 3
0N/A 'g','h','i','j','k','l','m','n', // 4
0N/A 'o','p','q','r','s','t','u','v', // 5
0N/A 'w','x','y','z','0','1','2','3', // 6
0N/A '4','5','6','7','8','9','+','/' // 7
0N/A };
0N/A
0N/A private final static byte pem_convert_array[] = new byte[256];
0N/A
0N/A static {
0N/A for (int i = 0; i < 255; i++) {
0N/A pem_convert_array[i] = -1;
0N/A }
0N/A for (int i = 0; i < pem_array.length; i++) {
0N/A pem_convert_array[pem_array[i]] = (byte) i;
0N/A }
0N/A }
0N/A
0N/A byte decode_buffer[] = new byte[4];
0N/A
0N/A /**
0N/A * Decode one BASE64 atom into 1, 2, or 3 bytes of data.
0N/A */
0N/A protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int rem)
0N/A throws java.io.IOException
0N/A {
0N/A int i;
0N/A byte a = -1, b = -1, c = -1, d = -1;
0N/A
0N/A if (rem < 2) {
0N/A throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");
0N/A }
0N/A do {
0N/A i = inStream.read();
0N/A if (i == -1) {
0N/A throw new CEStreamExhausted();
0N/A }
0N/A } while (i == '\n' || i == '\r');
0N/A decode_buffer[0] = (byte) i;
0N/A
0N/A i = readFully(inStream, decode_buffer, 1, rem-1);
0N/A if (i == -1) {
0N/A throw new CEStreamExhausted();
0N/A }
0N/A
0N/A if (rem > 3 && decode_buffer[3] == '=') {
0N/A rem = 3;
0N/A }
0N/A if (rem > 2 && decode_buffer[2] == '=') {
0N/A rem = 2;
0N/A }
0N/A switch (rem) {
0N/A case 4:
0N/A d = pem_convert_array[decode_buffer[3] & 0xff];
0N/A // NOBREAK
0N/A case 3:
0N/A c = pem_convert_array[decode_buffer[2] & 0xff];
0N/A // NOBREAK
0N/A case 2:
0N/A b = pem_convert_array[decode_buffer[1] & 0xff];
0N/A a = pem_convert_array[decode_buffer[0] & 0xff];
0N/A break;
0N/A }
0N/A
0N/A switch (rem) {
0N/A case 2:
0N/A outStream.write( (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
0N/A break;
0N/A case 3:
0N/A outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
0N/A outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
0N/A break;
0N/A case 4:
0N/A outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
0N/A outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
0N/A outStream.write( (byte) (((c << 6) & 0xc0) | (d & 0x3f)) );
0N/A break;
0N/A }
0N/A return;
0N/A }
0N/A}