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.ByteArrayOutputStream;
0N/Aimport java.io.PushbackInputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class implements a robust character decoder. The decoder will
0N/A * converted encoded text into binary data.
0N/A *
0N/A * The basic encoding unit is a 3 character atom. It encodes two bytes
0N/A * of data. Bytes are encoded into a 64 character set, the characters
0N/A * were chosen specifically because they appear in all codesets.
0N/A * We don't care what their numerical equivalent is because
0N/A * we use a character array to map them. This is like UUencoding
0N/A * with the dependency on ASCII removed.
0N/A *
0N/A * The three chars that make up an atom are encoded as follows:
0N/A * <pre>
0N/A * 00xxxyyy 00axxxxx 00byyyyy
0N/A * 00 = leading zeros, all values are 0 - 63
0N/A * xxxyyy - Top 3 bits of X, Top 3 bits of Y
0N/A * axxxxx - a = X parity bit, xxxxx lower 5 bits of X
0N/A * byyyyy - b = Y parity bit, yyyyy lower 5 bits of Y
0N/A * </pre>
0N/A *
0N/A * The atoms are arranged into lines suitable for inclusion into an
0N/A * email message or text file. The number of bytes that are encoded
0N/A * per line is 48 which keeps the total line length under 80 chars)
0N/A *
0N/A * Each line has the form(
0N/A * <pre>
0N/A * *(LLSS)(DDDD)(DDDD)(DDDD)...(CRC)
0N/A * Where each (xxx) represents a three character atom.
0N/A * (LLSS) - 8 bit length (high byte), and sequence number
0N/A * modulo 256;
0N/A * (DDDD) - Data byte atoms, if length is odd, last data
0N/A * atom has (DD00) (high byte data, low byte 0)
0N/A * (CRC) - 16 bit CRC for the line, includes length,
0N/A * sequence, and all data bytes. If there is a
0N/A * zero pad byte (odd length) it is _NOT_
0N/A * included in the CRC.
0N/A * </pre>
0N/A *
0N/A * If an error is encountered during decoding this class throws a
0N/A * CEFormatException. The specific detail messages are:
0N/A *
0N/A * <pre>
0N/A * "UCDecoder: High byte parity error."
0N/A * "UCDecoder: Low byte parity error."
0N/A * "UCDecoder: Out of sequence line."
0N/A * "UCDecoder: CRC check failed."
0N/A * </pre>
0N/A *
0N/A * @author Chuck McManis
0N/A * @see CharacterEncoder
0N/A * @see UCEncoder
0N/A */
0N/Apublic class UCDecoder extends CharacterDecoder {
0N/A
0N/A /** This class encodes two bytes per atom. */
0N/A protected int bytesPerAtom() {
0N/A return (2);
0N/A }
0N/A
0N/A /** this class encodes 48 bytes per line */
0N/A protected int bytesPerLine() {
0N/A return (48);
0N/A }
0N/A
0N/A /* this is the UCE mapping of 0-63 to characters .. */
0N/A private final static byte map_array[] = {
0N/A // 0 1 2 3 4 5 6 7
0N/A (byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7', // 0
0N/A (byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F', // 1
0N/A (byte)'G',(byte)'H',(byte)'I',(byte)'J',(byte)'K',(byte)'L',(byte)'M',(byte)'N', // 2
0N/A (byte)'O',(byte)'P',(byte)'Q',(byte)'R',(byte)'S',(byte)'T',(byte)'U',(byte)'V', // 3
0N/A (byte)'W',(byte)'X',(byte)'Y',(byte)'Z',(byte)'a',(byte)'b',(byte)'c',(byte)'d', // 4
0N/A (byte)'e',(byte)'f',(byte)'g',(byte)'h',(byte)'i',(byte)'j',(byte)'k',(byte)'l', // 5
0N/A (byte)'m',(byte)'n',(byte)'o',(byte)'p',(byte)'q',(byte)'r',(byte)'s',(byte)'t', // 6
0N/A (byte)'u',(byte)'v',(byte)'w',(byte)'x',(byte)'y',(byte)'z',(byte)'(',(byte)')' // 7
0N/A };
0N/A
0N/A private int sequence;
0N/A private byte tmp[] = new byte[2];
0N/A private CRC16 crc = new CRC16();
0N/A
0N/A /**
0N/A * Decode one atom - reads the characters from the input stream, decodes
0N/A * them, and checks for valid parity.
0N/A */
0N/A protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l) throws IOException {
0N/A int i, p1, p2, np1, np2;
0N/A byte a = -1, b = -1, c = -1;
0N/A byte high_byte, low_byte;
0N/A byte tmp[] = new byte[3];
0N/A
0N/A i = inStream.read(tmp);
0N/A if (i != 3) {
0N/A throw new CEStreamExhausted();
0N/A }
0N/A for (i = 0; (i < 64) && ((a == -1) || (b == -1) || (c == -1)); i++) {
0N/A if (tmp[0] == map_array[i]) {
0N/A a = (byte) i;
0N/A }
0N/A if (tmp[1] == map_array[i]) {
0N/A b = (byte) i;
0N/A }
0N/A if (tmp[2] == map_array[i]) {
0N/A c = (byte) i;
0N/A }
0N/A }
0N/A high_byte = (byte) (((a & 0x38) << 2) + (b & 0x1f));
0N/A low_byte = (byte) (((a & 0x7) << 5) + (c & 0x1f));
0N/A p1 = 0;
0N/A p2 = 0;
0N/A for (i = 1; i < 256; i = i * 2) {
0N/A if ((high_byte & i) != 0)
0N/A p1++;
0N/A if ((low_byte & i) != 0)
0N/A p2++;
0N/A }
0N/A np1 = (b & 32) / 32;
0N/A np2 = (c & 32) / 32;
0N/A if ((p1 & 1) != np1) {
0N/A throw new CEFormatException("UCDecoder: High byte parity error.");
0N/A }
0N/A if ((p2 & 1) != np2) {
0N/A throw new CEFormatException("UCDecoder: Low byte parity error.");
0N/A }
0N/A outStream.write(high_byte);
0N/A crc.update(high_byte);
0N/A if (l == 2) {
0N/A outStream.write(low_byte);
0N/A crc.update(low_byte);
0N/A }
0N/A }
0N/A
0N/A private ByteArrayOutputStream lineAndSeq = new ByteArrayOutputStream(2);
0N/A
0N/A /**
0N/A * decodeBufferPrefix initializes the sequence number to zero.
0N/A */
0N/A protected void decodeBufferPrefix(PushbackInputStream inStream, OutputStream outStream) {
0N/A sequence = 0;
0N/A }
0N/A
0N/A /**
0N/A * decodeLinePrefix reads the sequence number and the number of
0N/A * encoded bytes from the line. If the sequence number is not the
0N/A * previous sequence number + 1 then an exception is thrown.
0N/A * UCE lines are line terminator immune, they all start with *
0N/A * so the other thing this method does is scan for the next line
0N/A * by looking for the * character.
0N/A *
0N/A * @exception CEFormatException out of sequence lines detected.
0N/A */
0N/A protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
0N/A int i;
0N/A int nLen, nSeq;
0N/A byte xtmp[];
0N/A int c;
0N/A
0N/A crc.value = 0;
0N/A while (true) {
0N/A c = inStream.read(tmp, 0, 1);
0N/A if (c == -1) {
0N/A throw new CEStreamExhausted();
0N/A }
0N/A if (tmp[0] == '*') {
0N/A break;
0N/A }
0N/A }
0N/A lineAndSeq.reset();
0N/A decodeAtom(inStream, lineAndSeq, 2);
0N/A xtmp = lineAndSeq.toByteArray();
0N/A nLen = xtmp[0] & 0xff;
0N/A nSeq = xtmp[1] & 0xff;
0N/A if (nSeq != sequence) {
0N/A throw new CEFormatException("UCDecoder: Out of sequence line.");
0N/A }
0N/A sequence = (sequence + 1) & 0xff;
0N/A return (nLen);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * this method reads the CRC that is at the end of every line and
0N/A * verifies that it matches the computed CRC.
0N/A *
0N/A * @exception CEFormatException if CRC check fails.
0N/A */
0N/A protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
0N/A int i;
0N/A int lineCRC = crc.value;
0N/A int readCRC;
0N/A byte tmp[];
0N/A
0N/A lineAndSeq.reset();
0N/A decodeAtom(inStream, lineAndSeq, 2);
0N/A tmp = lineAndSeq.toByteArray();
0N/A readCRC = ((tmp[0] << 8) & 0xFF00) + (tmp[1] & 0xff);
0N/A if (readCRC != lineCRC) {
0N/A throw new CEFormatException("UCDecoder: CRC check failed.");
0N/A }
0N/A }
0N/A}