1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.telephony.cdma.sms;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.util.SparseIntArray;
21 
22 import com.android.internal.telephony.SmsHeader;
23 import com.android.internal.util.HexDump;
24 
25 public class UserData {
26 
27     /**
28      * User data encoding types.
29      * (See 3GPP2 C.R1001-F, v1.0, table 9.1-1)
30      */
31     public static final int ENCODING_OCTET                      = 0x00;
32     public static final int ENCODING_IS91_EXTENDED_PROTOCOL     = 0x01;
33     public static final int ENCODING_7BIT_ASCII                 = 0x02;
34     public static final int ENCODING_IA5                        = 0x03;
35     public static final int ENCODING_UNICODE_16                 = 0x04;
36     public static final int ENCODING_SHIFT_JIS                  = 0x05;
37     public static final int ENCODING_KOREAN                     = 0x06;
38     public static final int ENCODING_LATIN_HEBREW               = 0x07;
39     public static final int ENCODING_LATIN                      = 0x08;
40     public static final int ENCODING_GSM_7BIT_ALPHABET          = 0x09;
41     public static final int ENCODING_GSM_DCS                    = 0x0A;
42 
43     /**
44      * User data message type encoding types.
45      * (See 3GPP2 C.S0015-B, 4.5.2 and 3GPP 23.038, Section 4)
46      */
47     public static final int ENCODING_GSM_DCS_7BIT               = 0x00;
48     public static final int ENCODING_GSM_DCS_8BIT               = 0x01;
49     public static final int ENCODING_GSM_DCS_16BIT              = 0x02;
50 
51     /**
52      * IS-91 message types.
53      * (See TIA/EIS/IS-91-A-ENGL 1999, table 3.7.1.1-3)
54      */
55     public static final int IS91_MSG_TYPE_VOICEMAIL_STATUS   = 0x82;
56     public static final int IS91_MSG_TYPE_SHORT_MESSAGE_FULL = 0x83;
57     public static final int IS91_MSG_TYPE_CLI                = 0x84;
58     public static final int IS91_MSG_TYPE_SHORT_MESSAGE      = 0x85;
59 
60     /**
61      * US ASCII character mapping table.
62      *
63      * This table contains only the printable ASCII characters, with a
64      * 0x20 offset, meaning that the ASCII SPACE character is at index
65      * 0, with the resulting code of 0x20.
66      *
67      * Note this mapping is also equivalent to that used by both the
68      * IA5 and the IS-91 encodings.  For the former this is defined
69      * using CCITT Rec. T.50 Tables 1 and 3.  For the latter IS 637 B,
70      * Table 4.3.1.4.1-1 -- and note the encoding uses only 6 bits,
71      * and hence only maps entries up to the '_' character.
72      *
73      */
74     public static final char[] ASCII_MAP = {
75         ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
76         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
77         '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
78         'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
79         '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
80         'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'};
81 
82     /**
83      * Character to use when forced to encode otherwise unencodable
84      * characters, meaning those not in the respective ASCII or GSM
85      * 7-bit encoding tables.  Current choice is SPACE, which is 0x20
86      * in both the GSM-7bit and ASCII-7bit encodings.
87      */
88     static final byte UNENCODABLE_7_BIT_CHAR = 0x20;
89 
90     /**
91      * Only elements between these indices in the ASCII table are printable.
92      */
93     public static final int PRINTABLE_ASCII_MIN_INDEX = 0x20;
94     public static final int ASCII_NL_INDEX = 0x0A;
95     public static final int ASCII_CR_INDEX = 0x0D;
96     @UnsupportedAppUsage
97     public static final SparseIntArray charToAscii = new SparseIntArray();
98     static {
99         for (int i = 0; i < ASCII_MAP.length; i++) {
charToAscii.put(ASCII_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i)100             charToAscii.put(ASCII_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i);
101         }
102         charToAscii.put('\n', ASCII_NL_INDEX);
103         charToAscii.put('\r', ASCII_CR_INDEX);
104     }
105 
106     @UnsupportedAppUsage
UserData()107     public UserData() {
108     }
109 
110     /*
111      * TODO(cleanup): Move this very generic functionality somewhere
112      * more general.
113      */
114     /**
115      * Given a string generate a corresponding ASCII-encoded byte
116      * array, but limited to printable characters.  If the input
117      * contains unprintable characters, return null.
118      */
stringToAscii(String str)119     public static byte[] stringToAscii(String str) {
120         int len = str.length();
121         byte[] result = new byte[len];
122         for (int i = 0; i < len; i++) {
123             int charCode = charToAscii.get(str.charAt(i), -1);
124             if (charCode == -1) return null;
125             result[i] = (byte)charCode;
126         }
127         return result;
128     }
129 
130     /**
131      * Mapping for ASCII values less than 32 are flow control signals
132      * and not used here.
133      */
134     public static final int ASCII_MAP_BASE_INDEX = 0x20;
135     public static final int ASCII_MAP_MAX_INDEX = ASCII_MAP_BASE_INDEX + ASCII_MAP.length - 1;
136 
137     /**
138      * Contains the data header of the user data
139      */
140     @UnsupportedAppUsage
141     public SmsHeader userDataHeader;
142 
143     /**
144      * Contains the data encoding type for the SMS message
145      */
146     @UnsupportedAppUsage
147     public int msgEncoding;
148     @UnsupportedAppUsage
149     public boolean msgEncodingSet = false;
150 
151     public int msgType;
152 
153     /**
154      * Number of invalid bits in the last byte of data.
155      */
156     public int paddingBits;
157 
158     @UnsupportedAppUsage
159     public int numFields;
160 
161     /**
162      * Contains the user data of a SMS message
163      * (See 3GPP2 C.S0015-B, v2, 4.5.2)
164      */
165     @UnsupportedAppUsage
166     public byte[] payload;
167     @UnsupportedAppUsage
168     public String payloadStr;
169 
170     @Override
toString()171     public String toString() {
172         StringBuilder builder = new StringBuilder();
173         builder.append("UserData ");
174         builder.append("{ msgEncoding=" + (msgEncodingSet ? msgEncoding : "unset"));
175         builder.append(", msgType=" + msgType);
176         builder.append(", paddingBits=" + paddingBits);
177         builder.append(", numFields=" + numFields);
178         builder.append(", userDataHeader=" + userDataHeader);
179         builder.append(", payload='" + HexDump.toHexString(payload) + "'");
180         builder.append(", payloadStr='" + payloadStr + "'");
181         builder.append(" }");
182         return builder.toString();
183     }
184 
185 }
186