aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* FROM mail.jar */ aoqi@0: package com.sun.xml.internal.org.jvnet.mimepull; aoqi@0: aoqi@0: import java.io.*; aoqi@0: aoqi@0: /** aoqi@0: * This class implements a UUDecoder. It is implemented as aoqi@0: * a FilterInputStream, so one can just wrap this class around aoqi@0: * any input stream and read bytes from this filter. The decoding aoqi@0: * is done as the bytes are read out. aoqi@0: * aoqi@0: * @author John Mani aoqi@0: * @author Bill Shannon aoqi@0: */ aoqi@0: aoqi@0: final class UUDecoderStream extends FilterInputStream { aoqi@0: private String name; aoqi@0: private int mode; aoqi@0: aoqi@0: private byte[] buffer = new byte[45]; // max decoded chars in a line = 45 aoqi@0: private int bufsize = 0; // size of the cache aoqi@0: private int index = 0; // index into the cache aoqi@0: private boolean gotPrefix = false; aoqi@0: private boolean gotEnd = false; aoqi@0: private LineInputStream lin; aoqi@0: private boolean ignoreErrors; aoqi@0: private boolean ignoreMissingBeginEnd; aoqi@0: private String readAhead; aoqi@0: aoqi@0: /** aoqi@0: * Create a UUdecoder that decodes the specified input stream. aoqi@0: * The System property mail.mime.uudecode.ignoreerrors aoqi@0: * controls whether errors in the encoded data cause an exception aoqi@0: * or are ignored. The default is false (errors cause exception). aoqi@0: * The System property mail.mime.uudecode.ignoremissingbeginend aoqi@0: * controls whether a missing begin or end line cause an exception aoqi@0: * or are ignored. The default is false (errors cause exception). aoqi@0: * @param in the input stream aoqi@0: */ aoqi@0: public UUDecoderStream(InputStream in) { aoqi@0: super(in); aoqi@0: lin = new LineInputStream(in); aoqi@0: // default to false aoqi@0: ignoreErrors = PropUtil.getBooleanSystemProperty( aoqi@0: "mail.mime.uudecode.ignoreerrors", false); aoqi@0: // default to false aoqi@0: ignoreMissingBeginEnd = PropUtil.getBooleanSystemProperty( aoqi@0: "mail.mime.uudecode.ignoremissingbeginend", false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a UUdecoder that decodes the specified input stream. aoqi@0: * @param in the input stream aoqi@0: * @param ignoreErrors ignore errors? aoqi@0: * @param ignoreMissingBeginEnd ignore missing begin or end? aoqi@0: */ aoqi@0: public UUDecoderStream(InputStream in, boolean ignoreErrors, aoqi@0: boolean ignoreMissingBeginEnd) { aoqi@0: super(in); aoqi@0: lin = new LineInputStream(in); aoqi@0: this.ignoreErrors = ignoreErrors; aoqi@0: this.ignoreMissingBeginEnd = ignoreMissingBeginEnd; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Read the next decoded byte from this input stream. The byte aoqi@0: * is returned as an int in the range 0 aoqi@0: * to 255. If no byte is available because the end of aoqi@0: * the stream has been reached, the value -1 is returned. aoqi@0: * This method blocks until input data is available, the end of the aoqi@0: * stream is detected, or an exception is thrown. aoqi@0: * aoqi@0: * @return next byte of data, or -1 if the end of aoqi@0: * stream is reached. aoqi@0: * @exception IOException if an I/O error occurs. aoqi@0: * @see java.io.FilterInputStream#in aoqi@0: */ aoqi@0: @Override aoqi@0: public int read() throws IOException { aoqi@0: if (index >= bufsize) { aoqi@0: readPrefix(); aoqi@0: if (!decode()) { aoqi@0: return -1; aoqi@0: } aoqi@0: index = 0; // reset index into buffer aoqi@0: } aoqi@0: return buffer[index++] & 0xff; // return lower byte aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int read(byte[] buf, int off, int len) throws IOException { aoqi@0: int i, c; aoqi@0: for (i = 0; i < len; i++) { aoqi@0: if ((c = read()) == -1) { aoqi@0: if (i == 0) {// At end of stream, so we should aoqi@0: i = -1; // return -1, NOT 0. aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: buf[off+i] = (byte)c; aoqi@0: } aoqi@0: return i; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean markSupported() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int available() throws IOException { aoqi@0: // This is only an estimate, since in.available() aoqi@0: // might include CRLFs too .. aoqi@0: return ((in.available() * 3)/4 + (bufsize-index)); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the "name" field from the prefix. This is meant to aoqi@0: * be the pathname of the decoded file aoqi@0: * aoqi@0: * @return name of decoded file aoqi@0: * @exception IOException if an I/O error occurs. aoqi@0: */ aoqi@0: public String getName() throws IOException { aoqi@0: readPrefix(); aoqi@0: return name; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the "mode" field from the prefix. This is the permission aoqi@0: * mode of the source file. aoqi@0: * aoqi@0: * @return permission mode of source file aoqi@0: * @exception IOException if an I/O error occurs. aoqi@0: */ aoqi@0: public int getMode() throws IOException { aoqi@0: readPrefix(); aoqi@0: return mode; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * UUencoded streams start off with the line: aoqi@0: * "begin " aoqi@0: * Search for this prefix and gobble it up. aoqi@0: */ aoqi@0: private void readPrefix() throws IOException { aoqi@0: if (gotPrefix) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: mode = 0666; // defaults, overridden below aoqi@0: name = "encoder.buf"; // same default used by encoder aoqi@0: String line; aoqi@0: for (;;) { aoqi@0: // read till we get the prefix: "begin MODE FILENAME" aoqi@0: line = lin.readLine(); // NOTE: readLine consumes CRLF pairs too aoqi@0: if (line == null) { aoqi@0: if (!ignoreMissingBeginEnd) { aoqi@0: throw new DecodingException("UUDecoder: Missing begin"); aoqi@0: } aoqi@0: // at EOF, fake it aoqi@0: gotPrefix = true; aoqi@0: gotEnd = true; aoqi@0: break; aoqi@0: } aoqi@0: if (line.regionMatches(false, 0, "begin", 0, 5)) { aoqi@0: try { aoqi@0: mode = Integer.parseInt(line.substring(6,9)); aoqi@0: } catch (NumberFormatException ex) { aoqi@0: if (!ignoreErrors) { aoqi@0: throw new DecodingException( aoqi@0: "UUDecoder: Error in mode: " + ex.toString()); aoqi@0: } aoqi@0: } aoqi@0: if (line.length() > 10) { aoqi@0: name = line.substring(10); aoqi@0: } else { aoqi@0: if (!ignoreErrors) { aoqi@0: throw new DecodingException( aoqi@0: "UUDecoder: Missing name: " + line); aoqi@0: } aoqi@0: } aoqi@0: gotPrefix = true; aoqi@0: break; aoqi@0: } else if (ignoreMissingBeginEnd && line.length() != 0) { aoqi@0: int count = line.charAt(0); aoqi@0: count = (count - ' ') & 0x3f; aoqi@0: int need = ((count * 8)+5)/6; aoqi@0: if (need == 0 || line.length() >= need + 1) { aoqi@0: /* aoqi@0: * Looks like a legitimate encoded line. aoqi@0: * Pretend we saw the "begin" line and aoqi@0: * save this line for later processing in aoqi@0: * decode(). aoqi@0: */ aoqi@0: readAhead = line; aoqi@0: gotPrefix = true; // fake it aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private boolean decode() throws IOException { aoqi@0: aoqi@0: if (gotEnd) { aoqi@0: return false; aoqi@0: } aoqi@0: bufsize = 0; aoqi@0: int count = 0; aoqi@0: String line; aoqi@0: for (;;) { aoqi@0: /* aoqi@0: * If we ignored a missing "begin", the first line aoqi@0: * will be saved in readAhead. aoqi@0: */ aoqi@0: if (readAhead != null) { aoqi@0: line = readAhead; aoqi@0: readAhead = null; aoqi@0: } else { aoqi@0: line = lin.readLine(); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Improperly encoded data sometimes omits the zero length aoqi@0: * line that starts with a space character, we detect the aoqi@0: * following "end" line here. aoqi@0: */ aoqi@0: if (line == null) { aoqi@0: if (!ignoreMissingBeginEnd) { aoqi@0: throw new DecodingException( aoqi@0: "UUDecoder: Missing end at EOF"); aoqi@0: } aoqi@0: gotEnd = true; aoqi@0: return false; aoqi@0: } aoqi@0: if (line.equals("end")) { aoqi@0: gotEnd = true; aoqi@0: return false; aoqi@0: } aoqi@0: if (line.length() == 0) { aoqi@0: continue; aoqi@0: } aoqi@0: count = line.charAt(0); aoqi@0: if (count < ' ') { aoqi@0: if (!ignoreErrors) { aoqi@0: throw new DecodingException( aoqi@0: "UUDecoder: Buffer format error"); aoqi@0: } aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * The first character in a line is the number of original (not aoqi@0: * the encoded atoms) characters in the line. Note that all the aoqi@0: * code below has to handle the character that indicates aoqi@0: * end of encoded stream. aoqi@0: */ aoqi@0: count = (count - ' ') & 0x3f; aoqi@0: aoqi@0: if (count == 0) { aoqi@0: line = lin.readLine(); aoqi@0: if (line == null || !line.equals("end")) { aoqi@0: if (!ignoreMissingBeginEnd) { aoqi@0: throw new DecodingException( aoqi@0: "UUDecoder: Missing End after count 0 line"); aoqi@0: } aoqi@0: } aoqi@0: gotEnd = true; aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: int need = ((count * 8)+5)/6; aoqi@0: //System.out.println("count " + count + ", need " + need + ", len " + line.length()); aoqi@0: if (line.length() < need + 1) { aoqi@0: if (!ignoreErrors) { aoqi@0: throw new DecodingException( aoqi@0: "UUDecoder: Short buffer error"); aoqi@0: } aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // got a line we're committed to, break out and decode it aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: int i = 1; aoqi@0: byte a, b; aoqi@0: /* aoqi@0: * A correct uuencoder always encodes 3 characters at a time, even aoqi@0: * if there aren't 3 characters left. But since some people out aoqi@0: * there have broken uuencoders we handle the case where they aoqi@0: * don't include these "unnecessary" characters. aoqi@0: */ aoqi@0: while (bufsize < count) { aoqi@0: // continue decoding until we get 'count' decoded chars aoqi@0: a = (byte)((line.charAt(i++) - ' ') & 0x3f); aoqi@0: b = (byte)((line.charAt(i++) - ' ') & 0x3f); aoqi@0: buffer[bufsize++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)); aoqi@0: aoqi@0: if (bufsize < count) { aoqi@0: a = b; aoqi@0: b = (byte)((line.charAt(i++) - ' ') & 0x3f); aoqi@0: buffer[bufsize++] = aoqi@0: (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf)); aoqi@0: } aoqi@0: aoqi@0: if (bufsize < count) { aoqi@0: a = b; aoqi@0: b = (byte)((line.charAt(i++) - ' ') & 0x3f); aoqi@0: buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: /*** begin TEST program ***** aoqi@0: public static void main(String argv[]) throws Exception { aoqi@0: FileInputStream infile = new FileInputStream(argv[0]); aoqi@0: UUDecoderStream decoder = new UUDecoderStream(infile); aoqi@0: int c; aoqi@0: aoqi@0: try { aoqi@0: while ((c = decoder.read()) != -1) aoqi@0: System.out.write(c); aoqi@0: System.out.flush(); aoqi@0: } catch (Exception e) { aoqi@0: e.printStackTrace(); aoqi@0: } aoqi@0: } aoqi@0: **** end TEST program ****/ aoqi@0: }