QuotedPrintableDecoder.java

  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. import java.io.IOException;
  19. import java.io.OutputStream;

  20. /**
  21.  */
  22. final class QuotedPrintableDecoder {

  23.     /**
  24.      * The shift value required to create the upper nibble from the first of 2 byte values converted from ASCII hex.
  25.      */
  26.     private static final int UPPER_NIBBLE_SHIFT = Byte.SIZE / 2;

  27.     /**
  28.      * Decodes the encoded byte data writing it to the given output stream.
  29.      *
  30.      * @param data The array of byte data to decode.
  31.      * @param out  The output stream used to return the decoded data.
  32.      * @return the number of bytes produced.
  33.      * @throws IOException if an IO error occurs
  34.      */
  35.     public static int decode(final byte[] data, final OutputStream out) throws IOException {
  36.         var off = 0;
  37.         final var length = data.length;
  38.         final var endOffset = off + length;
  39.         var bytesWritten = 0;

  40.         while (off < endOffset) {
  41.             final var ch = data[off++];

  42.             // space characters were translated to '_' on encode, so we need to translate them back.
  43.             if (ch == '_') {
  44.                 out.write(' ');
  45.             } else if (ch == '=') {
  46.                 // we found an encoded character. Reduce the 3 char sequence to one.
  47.                 // but first, make sure we have two characters to work with.
  48.                 if (off + 1 >= endOffset) {
  49.                     throw new IOException("Invalid quoted printable encoding; truncated escape sequence");
  50.                 }

  51.                 final var b1 = data[off++];
  52.                 final var b2 = data[off++];

  53.                 // we've found an encoded carriage return. The next char needs to be a newline
  54.                 if (b1 == '\r') {
  55.                     if (b2 != '\n') {
  56.                         throw new IOException("Invalid quoted printable encoding; CR must be followed by LF");
  57.                     }
  58.                     // this was a soft linebreak inserted by the encoding. We just toss this away
  59.                     // on decode.
  60.                 } else {
  61.                     // this is a hex pair we need to convert back to a single byte.
  62.                     final var c1 = hexToBinary(b1);
  63.                     final var c2 = hexToBinary(b2);
  64.                     out.write(c1 << UPPER_NIBBLE_SHIFT | c2);
  65.                     // 3 bytes in, one byte out
  66.                     bytesWritten++;
  67.                 }
  68.             } else {
  69.                 // simple character, just write it out.
  70.                 out.write(ch);
  71.                 bytesWritten++;
  72.             }
  73.         }

  74.         return bytesWritten;
  75.     }

  76.     /**
  77.      * Converts a hexadecimal digit to the binary value it represents.
  78.      *
  79.      * @param b the ASCII hexadecimal byte to convert (0-0, A-F, a-f)
  80.      * @return the int value of the hexadecimal byte, 0-15
  81.      * @throws IOException if the byte is not a valid hexadecimal digit.
  82.      */
  83.     private static int hexToBinary(final byte b) throws IOException {
  84.         // CHECKSTYLE IGNORE MagicNumber FOR NEXT 1 LINE
  85.         final var i = Character.digit((char) b, 16);
  86.         if (i == -1) {
  87.             throw new IOException("Invalid quoted printable encoding: not a valid hex digit: " + b);
  88.         }
  89.         return i;
  90.     }

  91.     /**
  92.      * Hidden constructor, this class must not be instantiated.
  93.      */
  94.     private QuotedPrintableDecoder() {
  95.         // do nothing
  96.     }

  97. }