View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.fileupload2.core;
    18
    19  import java.io.IOException;
    20  import java.io.OutputStream;
    21
    22  /**
    23   */
    24  final class QuotedPrintableDecoder {
    25
    26      /**
    27       * The shift value required to create the upper nibble from the first of 2 byte values converted from ASCII hex.
    28       */
    29      private static final int UPPER_NIBBLE_SHIFT = Byte.SIZE / 2;
    30
    31      /**
    32       * Decodes the encoded byte data writing it to the given output stream.
    33       *
    34       * @param data The array of byte data to decode.
    35       * @param out  The output stream used to return the decoded data.
    36       *
    37       * @return the number of bytes produced.
    38       * @throws IOException if an IO error occurs
    39       */
    40      public static int decode(final byte[] data, final OutputStream out) throws IOException {
    41          var off = 0;
    42          final var length = data.length;
    43          final var endOffset = off + length;
    44          var bytesWritten = 0;
    45
    46          while (off < endOffset) {
    47              final var ch = data[off++];
    48
    49              // space characters were translated to '_' on encode, so we need to translate them back.
    50              if (ch == '_') {
    51                  out.write(' ');
    52              } else if (ch == '=') {
    53                  // we found an encoded character. Reduce the 3 char sequence to one.
    54                  // but first, make sure we have two characters to work with.
    55                  if (off + 1 >= endOffset) {
    56                      throw new IOException("Invalid quoted printable encoding; truncated escape sequence");
    57                  }
    58
    59                  final var b1 = data[off++];
    60                  final var b2 = data[off++];
    61
    62                  // we've found an encoded carriage return. The next char needs to be a newline
    63                  if (b1 == '\r') {
    64                      if (b2 != '\n') {
    65                          throw new IOException("Invalid quoted printable encoding; CR must be followed by LF");
    66                      }
    67                      // this was a soft linebreak inserted by the encoding. We just toss this away
    68                      // on decode.
    69                  } else {
    70                      // this is a hex pair we need to convert back to a single byte.
    71                      final var c1 = hexToBinary(b1);
    72                      final var c2 = hexToBinary(b2);
    73                      out.write(c1 << UPPER_NIBBLE_SHIFT | c2);
    74                      // 3 bytes in, one byte out
    75                      bytesWritten++;
    76                  }
    77              } else {
    78                  // simple character, just write it out.
    79                  out.write(ch);
    80                  bytesWritten++;
    81              }
    82          }
    83
    84          return bytesWritten;
    85      }
    86
    87      /**
    88       * Converts a hexadecimal digit to the binary value it represents.
    89       *
    90       * @param b the ASCII hexadecimal byte to convert (0-0, A-F, a-f)
    91       * @return the int value of the hexadecimal byte, 0-15
    92       * @throws IOException if the byte is not a valid hexadecimal digit.
    93       */
    94      private static int hexToBinary(final byte b) throws IOException {
    95          // CHECKSTYLE IGNORE MagicNumber FOR NEXT 1 LINE
    96          final var i = Character.digit((char) b, 16);
    97          if (i == -1) {
    98              throw new IOException("Invalid quoted printable encoding: not a valid hex digit: " + b);
    99          }
    100         return i;
    101     }
    102
    103     /**
    104      * Hidden constructor, this class must not be instantiated.
    105      */
    106     private QuotedPrintableDecoder() {
    107         // do nothing
    108     }
    109
    110 }