0N/A/*
2362N/A * Copyright (c) 1995, 2001, 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.PushbackInputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class implements a Berkeley uu character decoder. This decoder
0N/A * was made famous by the uudecode program.
0N/A *
0N/A * The basic character coding is algorithmic, taking 6 bits of binary
0N/A * data and adding it to an ASCII ' ' (space) character. This converts
0N/A * these six bits into a printable representation. Note that it depends
0N/A * on the ASCII character encoding standard for english. Groups of three
0N/A * bytes are converted into 4 characters by treating the three bytes
0N/A * a four 6 bit groups, group 1 is byte 1's most significant six bits,
0N/A * group 2 is byte 1's least significant two bits plus byte 2's four
0N/A * most significant bits. etc.
0N/A *
0N/A * In this encoding, the buffer prefix is:
0N/A * <pre>
0N/A * begin [mode] [filename]
0N/A * </pre>
0N/A *
0N/A * This is followed by one or more lines of the form:
0N/A * <pre>
0N/A * (len)(data)(data)(data) ...
0N/A * </pre>
0N/A * where (len) is the number of bytes on this line. Note that groupings
0N/A * are always four characters, even if length is not a multiple of three
0N/A * bytes. When less than three characters are encoded, the values of the
0N/A * last remaining bytes is undefined and should be ignored.
0N/A *
0N/A * The last line of data in a uuencoded buffer is represented by a single
0N/A * space character. This is translated by the decoding engine to a line
0N/A * length of zero. This is immediately followed by a line which contains
0N/A * the word 'end[newline]'
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 * "UUDecoder: No begin line."
0N/A * "UUDecoder: Malformed begin line."
0N/A * "UUDecoder: Short Buffer."
0N/A * "UUDecoder: Bad Line Length."
0N/A * "UUDecoder: Missing 'end' line."
0N/A * </pre>
0N/A *
0N/A * @author Chuck McManis
0N/A * @see CharacterDecoder
0N/A * @see UUEncoder
0N/A */
0N/Apublic class UUDecoder extends CharacterDecoder {
0N/A
0N/A /**
0N/A * This string contains the name that was in the buffer being decoded.
0N/A */
0N/A public String bufferName;
0N/A
0N/A /**
0N/A * Represents UNIX(tm) mode bits. Generally three octal digits
0N/A * representing read, write, and execute permission of the owner,
0N/A * group owner, and others. They should be interpreted as the bit groups:
0N/A * <pre>
0N/A * (owner) (group) (others)
0N/A * rwx rwx rwx (r = read, w = write, x = execute)
0N/A *</pre>
0N/A *
0N/A */
0N/A public int mode;
0N/A
0N/A
0N/A /**
0N/A * UU encoding specifies 3 bytes per atom.
0N/A */
0N/A protected int bytesPerAtom() {
0N/A return (3);
0N/A }
0N/A
0N/A /**
0N/A * All UU lines have 45 bytes on them, for line length of 15*4+1 or 61
0N/A * characters per line.
0N/A */
0N/A protected int bytesPerLine() {
0N/A return (45);
0N/A }
0N/A
0N/A /** This is used to decode the atoms */
0N/A private byte decoderBuffer[] = new byte[4];
0N/A
0N/A /**
0N/A * Decode a UU atom. Note that if l is less than 3 we don't write
0N/A * the extra bits, however the encoder always encodes 4 character
0N/A * groups even when they are not needed.
0N/A */
0N/A protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l)
0N/A throws IOException {
0N/A int i, c1, c2, c3, c4;
0N/A int a, b, c;
0N/A StringBuffer x = new StringBuffer();
0N/A
0N/A for (i = 0; i < 4; i++) {
0N/A c1 = inStream.read();
0N/A if (c1 == -1) {
0N/A throw new CEStreamExhausted();
0N/A }
0N/A x.append((char)c1);
0N/A decoderBuffer[i] = (byte) ((c1 - ' ') & 0x3f);
0N/A }
0N/A a = ((decoderBuffer[0] << 2) & 0xfc) | ((decoderBuffer[1] >>> 4) & 3);
0N/A b = ((decoderBuffer[1] << 4) & 0xf0) | ((decoderBuffer[2] >>> 2) & 0xf);
0N/A c = ((decoderBuffer[2] << 6) & 0xc0) | (decoderBuffer[3] & 0x3f);
0N/A outStream.write((byte)(a & 0xff));
0N/A if (l > 1) {
0N/A outStream.write((byte)( b & 0xff));
0N/A }
0N/A if (l > 2) {
0N/A outStream.write((byte)(c&0xff));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * For uuencoded buffers, the data begins with a line of the form:
0N/A * begin MODE FILENAME
0N/A * This line always starts in column 1.
0N/A */
0N/A protected void decodeBufferPrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
0N/A int c;
0N/A StringBuffer q = new StringBuffer(32);
0N/A String r;
0N/A boolean sawNewLine;
0N/A
0N/A /*
0N/A * This works by ripping through the buffer until it finds a 'begin'
0N/A * line or the end of the buffer.
0N/A */
0N/A sawNewLine = true;
0N/A while (true) {
0N/A c = inStream.read();
0N/A if (c == -1) {
0N/A throw new CEFormatException("UUDecoder: No begin line.");
0N/A }
0N/A if ((c == 'b') && sawNewLine){
0N/A c = inStream.read();
0N/A if (c == 'e') {
0N/A break;
0N/A }
0N/A }
0N/A sawNewLine = (c == '\n') || (c == '\r');
0N/A }
0N/A
0N/A /*
0N/A * Now we think its begin, (we've seen ^be) so verify it here.
0N/A */
0N/A while ((c != '\n') && (c != '\r')) {
0N/A c = inStream.read();
0N/A if (c == -1) {
0N/A throw new CEFormatException("UUDecoder: No begin line.");
0N/A }
0N/A if ((c != '\n') && (c != '\r')) {
0N/A q.append((char)c);
0N/A }
0N/A }
0N/A r = q.toString();
0N/A if (r.indexOf(' ') != 3) {
0N/A throw new CEFormatException("UUDecoder: Malformed begin line.");
0N/A }
0N/A mode = Integer.parseInt(r.substring(4,7));
0N/A bufferName = r.substring(r.indexOf(' ',6)+1);
0N/A /*
0N/A * Check for \n after \r
0N/A */
0N/A if (c == '\r') {
0N/A c = inStream.read ();
0N/A if ((c != '\n') && (c != -1))
0N/A inStream.unread (c);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * In uuencoded buffers, encoded lines start with a character that
0N/A * represents the number of bytes encoded in this line. The last
0N/A * line of input is always a line that starts with a single space
0N/A * character, which would be a zero length line.
0N/A */
0N/A protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
0N/A int c;
0N/A
0N/A c = inStream.read();
0N/A if (c == ' ') {
0N/A c = inStream.read(); /* discard the (first)trailing CR or LF */
0N/A c = inStream.read(); /* check for a second one */
0N/A if ((c != '\n') && (c != -1))
0N/A inStream.unread (c);
0N/A throw new CEStreamExhausted();
0N/A } else if (c == -1) {
0N/A throw new CEFormatException("UUDecoder: Short Buffer.");
0N/A }
0N/A
0N/A c = (c - ' ') & 0x3f;
0N/A if (c > bytesPerLine()) {
0N/A throw new CEFormatException("UUDecoder: Bad Line Length.");
0N/A }
0N/A return (c);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Find the end of the line for the next operation.
0N/A * The following sequences are recognized as end-of-line
0N/A * CR, CR LF, or LF
0N/A */
0N/A protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
0N/A int c;
0N/A while (true) {
0N/A c = inStream.read();
0N/A if (c == -1) {
0N/A throw new CEStreamExhausted();
0N/A }
0N/A if (c == '\n') {
0N/A break;
0N/A }
0N/A if (c == '\r') {
0N/A c = inStream.read();
0N/A if ((c != '\n') && (c != -1)) {
0N/A inStream.unread (c);
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * UUencoded files have a buffer suffix which consists of the word
0N/A * end. This line should immediately follow the line with a single
0N/A * space in it.
0N/A */
0N/A protected void decodeBufferSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
0N/A int c;
0N/A
0N/A c = inStream.read(decoderBuffer);
0N/A if ((decoderBuffer[0] != 'e') || (decoderBuffer[1] != 'n') ||
0N/A (decoderBuffer[2] != 'd')) {
0N/A throw new CEFormatException("UUDecoder: Missing 'end' line.");
0N/A }
0N/A }
0N/A
0N/A}