Home | History | Annotate | Download | only in pdu
      1 /*
      2  * Copyright (C) 2007 Esmertec AG.
      3  * Copyright (C) 2007 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * 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 
     18 package com.android.messaging.mmslib.pdu;
     19 
     20 import java.io.ByteArrayOutputStream;
     21 
     22 public class QuotedPrintable {
     23     private static byte ESCAPE_CHAR = '=';
     24 
     25     /**
     26      * Decodes an array quoted-printable characters into an array of original bytes.
     27      * Escaped characters are converted back to their original representation.
     28      *
     29      * <p>
     30      * This function implements a subset of
     31      * quoted-printable encoding specification (rule #1 and rule #2)
     32      * as defined in RFC 1521.
     33      * </p>
     34      *
     35      * @param bytes array of quoted-printable characters
     36      * @return array of original bytes,
     37      *         null if quoted-printable decoding is unsuccessful.
     38      */
     39     public static final byte[] decodeQuotedPrintable(byte[] bytes) {
     40         if (bytes == null) {
     41             return null;
     42         }
     43         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     44         for (int i = 0; i < bytes.length; i++) {
     45             int b = bytes[i];
     46             if (b == ESCAPE_CHAR) {
     47                 try {
     48                     if ('\r' == (char) bytes[i + 1] &&
     49                             '\n' == (char) bytes[i + 2]) {
     50                         i += 2;
     51                         continue;
     52                     }
     53                     int u = Character.digit((char) bytes[++i], 16);
     54                     int l = Character.digit((char) bytes[++i], 16);
     55                     if (u == -1 || l == -1) {
     56                         return null;
     57                     }
     58                     buffer.write((char) ((u << 4) + l));
     59                 } catch (ArrayIndexOutOfBoundsException e) {
     60                     return null;
     61                 }
     62             } else {
     63                 buffer.write(b);
     64             }
     65         }
     66         return buffer.toByteArray();
     67     }
     68 }
     69