1 /*
   2  * Copyright (c) 1997, 2017, 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 
  26 /*
  27  * @(#)QPDecoderStream.java   1.9 02/04/02
  28  */
  29 
  30 
  31 
  32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
  33 
  34 import java.io.*;
  35 
  36 /**
  37  * This class implements a QP Decoder. It is implemented as
  38  * a FilterInputStream, so one can just wrap this class around
  39  * any input stream and read bytes from this filter. The decoding
  40  * is done as the bytes are read out.
  41  *
  42  * @author John Mani
  43  */
  44 
  45 public class QPDecoderStream extends FilterInputStream {
  46     protected byte[] ba = new byte[2];
  47     protected int spaces = 0;
  48 
  49     /**
  50      * Create a Quoted Printable decoder that decodes the specified
  51      * input stream.
  52      * @param in        the input stream
  53      */
  54     public QPDecoderStream(InputStream in) {
  55         super(new PushbackInputStream(in, 2)); // pushback of size=2
  56     }
  57 
  58     /**
  59      * Read the next decoded byte from this input stream. The byte
  60      * is returned as an <code>int</code> in the range <code>0</code>
  61      * to <code>255</code>. If no byte is available because the end of
  62      * the stream has been reached, the value <code>-1</code> is returned.
  63      * This method blocks until input data is available, the end of the
  64      * stream is detected, or an exception is thrown.
  65      *
  66      * @return     the next byte of data, or <code>-1</code> if the end of the
  67      *             stream is reached.
  68      * @exception  IOException  if an I/O error occurs.
  69      */
  70     @Override
  71     public int read() throws IOException {
  72         if (spaces > 0) {
  73             // We have cached space characters, return one
  74             spaces--;
  75             return ' ';
  76         }
  77 
  78         int c = in.read();
  79 
  80         if (c == ' ') {
  81             // Got space, keep reading till we get a non-space char
  82             while ((c = in.read()) == ' ')
  83                 spaces++;
  84 
  85             if (c == '\r' || c == '\n' || c == -1)
  86                 // If the non-space char is CR/LF/EOF, the spaces we got
  87                 // so far is junk introduced during transport. Junk 'em.
  88                 spaces = 0;
  89             else {
  90                 // The non-space char is NOT CR/LF, the spaces are valid.
  91                 ((PushbackInputStream)in).unread(c);
  92                 c = ' ';
  93             }
  94             return c; // return either <SPACE> or <CR/LF>
  95         }
  96         else if (c == '=') {
  97             // QP Encoded atom. Decode the next two bytes
  98             int a = in.read();
  99 
 100             if (a == '\n') {
 101                 /* Hmm ... not really confirming QP encoding, but lets
 102                  * allow this as a LF terminated encoded line .. and
 103                  * consider this a soft linebreak and recurse to fetch
 104                  * the next char.
 105                  */
 106                 return read();
 107             } else if (a == '\r') {
 108                 // Expecting LF. This forms a soft linebreak to be ignored.
 109                 int b = in.read();
 110                 if (b != '\n')
 111                     /* Not really confirming QP encoding, but
 112                      * lets allow this as well.
 113                      */
 114                     ((PushbackInputStream)in).unread(b);
 115                 return read();
 116             } else if (a == -1) {
 117                 // Not valid QP encoding, but we be nice and tolerant here !
 118                 return -1;
 119             } else {
 120                 ba[0] = (byte)a;
 121                 ba[1] = (byte)in.read();
 122                 try {
 123                     return ASCIIUtility.parseInt(ba, 0, 2, 16);
 124                 } catch (NumberFormatException nex) {
 125                     /*
 126                     System.err.println(
 127                         "Illegal characters in QP encoded stream: " +
 128                         ASCIIUtility.toString(ba, 0, 2)
 129                     );
 130                     */
 131 
 132                     ((PushbackInputStream)in).unread(ba);
 133                     return c;
 134                 }
 135             }
 136         }
 137         return c;
 138     }
 139 
 140     /**
 141      * Reads up to <code>len</code> decoded bytes of data from this input stream
 142      * into an array of bytes. This method blocks until some input is
 143      * available.
 144      * <p>
 145      *
 146      * @param      buf   the buffer into which the data is read.
 147      * @param      off   the start offset of the data.
 148      * @param      len   the maximum number of bytes read.
 149      * @return     the total number of bytes read into the buffer, or
 150      *             <code>-1</code> if there is no more data because the end of
 151      *             the stream has been reached.
 152      * @exception  IOException  if an I/O error occurs.
 153      */
 154     @Override
 155     public int read(byte[] buf, int off, int len) throws IOException {
 156         int i, c;
 157         for (i = 0; i < len; i++) {
 158             if ((c = read()) == -1) {
 159                 if (i == 0) // At end of stream, so we should
 160                     i = -1; // return -1 , NOT 0.
 161                 break;
 162             }
 163             buf[off+i] = (byte)c;
 164         }
 165         return i;
 166     }
 167 
 168     /**
 169      * Tests if this input stream supports marks. Currently this class
 170      * does not support marks
 171      */
 172     @Override
 173     public boolean markSupported() {
 174         return false;
 175     }
 176 
 177     /**
 178      * Returns the number of bytes that can be read from this input
 179      * stream without blocking. The QP algorithm does not permit
 180      * a priori knowledge of the number of bytes after decoding, so
 181      * this method just invokes the <code>available</code> method
 182      * of the original input stream.
 183      */
 184     @Override
 185     public int available() throws IOException {
 186         // This is bogus ! We don't really know how much
 187         // bytes are available *after* decoding
 188         return in.available();
 189     }
 190 
 191     /**** begin TEST program
 192     public static void main(String argv[]) throws Exception {
 193         FileInputStream infile = new FileInputStream(argv[0]);
 194         QPDecoderStream decoder = new QPDecoderStream(infile);
 195         int c;
 196 
 197         while ((c = decoder.read()) != -1)
 198             System.out.print((char)c);
 199         System.out.println();
 200     }
 201     *** end TEST program ****/
 202 }