Amit Mahajan | 82f245f | 2019-09-10 13:19:05 -0700 | [diff] [blame] | 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.google.android.mms.pdu; |
| 19 | |
Austin Wang | a63a2c0 | 2019-12-19 06:38:19 +0000 | [diff] [blame] | 20 | import dalvik.annotation.compat.UnsupportedAppUsage; |
Amit Mahajan | 82f245f | 2019-09-10 13:19:05 -0700 | [diff] [blame] | 21 | |
| 22 | import java.io.ByteArrayOutputStream; |
| 23 | |
| 24 | public class QuotedPrintable { |
| 25 | private static byte ESCAPE_CHAR = '='; |
| 26 | |
| 27 | /** |
| 28 | * Decodes an array quoted-printable characters into an array of original bytes. |
| 29 | * Escaped characters are converted back to their original representation. |
| 30 | * |
| 31 | * <p> |
| 32 | * This function implements a subset of |
| 33 | * quoted-printable encoding specification (rule #1 and rule #2) |
| 34 | * as defined in RFC 1521. |
| 35 | * </p> |
| 36 | * |
| 37 | * @param bytes array of quoted-printable characters |
| 38 | * @return array of original bytes, |
| 39 | * null if quoted-printable decoding is unsuccessful. |
| 40 | */ |
| 41 | @UnsupportedAppUsage |
| 42 | public static final byte[] decodeQuotedPrintable(byte[] bytes) { |
| 43 | if (bytes == null) { |
| 44 | return null; |
| 45 | } |
| 46 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 47 | for (int i = 0; i < bytes.length; i++) { |
| 48 | int b = bytes[i]; |
| 49 | if (b == ESCAPE_CHAR) { |
| 50 | try { |
| 51 | if('\r' == (char)bytes[i + 1] && |
| 52 | '\n' == (char)bytes[i + 2]) { |
| 53 | i += 2; |
| 54 | continue; |
| 55 | } |
| 56 | int u = Character.digit((char) bytes[++i], 16); |
| 57 | int l = Character.digit((char) bytes[++i], 16); |
| 58 | if (u == -1 || l == -1) { |
| 59 | return null; |
| 60 | } |
| 61 | buffer.write((char) ((u << 4) + l)); |
| 62 | } catch (ArrayIndexOutOfBoundsException e) { |
| 63 | return null; |
| 64 | } |
| 65 | } else { |
| 66 | buffer.write(b); |
| 67 | } |
| 68 | } |
| 69 | return buffer.toByteArray(); |
| 70 | } |
| 71 | } |