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     @UnsupportedAppUsage
UserData()28     public UserData() {
29     }
30 
31     /**
32      * User data encoding types.
33      * (See 3GPP2 C.R1001-F, v1.0, table 9.1-1)
34      */
35     public static final int ENCODING_OCTET                      = 0x00;
36     public static final int ENCODING_IS91_EXTENDED_PROTOCOL     = 0x01;
37     public static final int ENCODING_7BIT_ASCII                 = 0x02;
38     public static final int ENCODING_IA5                        = 0x03;
39     public static final int ENCODING_UNICODE_16                 = 0x04;
40     public static final int ENCODING_SHIFT_JIS                  = 0x05;
41     public static final int ENCODING_KOREAN                     = 0x06;
42     public static final int ENCODING_LATIN_HEBREW               = 0x07;
43     public static final int ENCODING_LATIN                      = 0x08;
44     public static final int ENCODING_GSM_7BIT_ALPHABET          = 0x09;
45     public static final int ENCODING_GSM_DCS                    = 0x0A;
46 
47     /**
48      * User data message type encoding types.
49      * (See 3GPP2 C.S0015-B, 4.5.2 and 3GPP 23.038, Section 4)
50      */
51     public static final int ENCODING_GSM_DCS_7BIT               = 0x00;
52     public static final int ENCODING_GSM_DCS_8BIT               = 0x01;
53     public static final int ENCODING_GSM_DCS_16BIT              = 0x02;
54 
55     /**
56      * IS-91 message types.
57      * (See TIA/EIS/IS-91-A-ENGL 1999, table 3.7.1.1-3)
58      */
59     public static final int IS91_MSG_TYPE_VOICEMAIL_STATUS   = 0x82;
60     public static final int IS91_MSG_TYPE_SHORT_MESSAGE_FULL = 0x83;
61     public static final int IS91_MSG_TYPE_CLI                = 0x84;
62     public static final int IS91_MSG_TYPE_SHORT_MESSAGE      = 0x85;
63 
64     /**
65      * US ASCII character mapping table.
66      *
67      * This table contains only the printable ASCII characters, with a
68      * 0x20 offset, meaning that the ASCII SPACE character is at index
69      * 0, with the resulting code of 0x20.
70      *
71      * Note this mapping is also equivalent to that used by both the
72      * IA5 and the IS-91 encodings.  For the former this is defined
73      * using CCITT Rec. T.50 Tables 1 and 3.  For the latter IS 637 B,
74      * Table 4.3.1.4.1-1 -- and note the encoding uses only 6 bits,
75      * and hence only maps entries up to the '_' character.
76      *
77      */
78     public static final char[] ASCII_MAP = {
79         ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
80         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
81         '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
82         'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
83         '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
84         'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'};
85 
86     /**
87      * Character to use when forced to encode otherwise unencodable
88      * characters, meaning those not in the respective ASCII or GSM
89      * 7-bit encoding tables.  Current choice is SPACE, which is 0x20
90      * in both the GSM-7bit and ASCII-7bit encodings.
91      */
92     static final byte UNENCODABLE_7_BIT_CHAR = 0x20;
93 
94     /**
95      * Only elements between these indices in the ASCII table are printable.
96      */
97     public static final int PRINTABLE_ASCII_MIN_INDEX = 0x20;
98     public static final int ASCII_NL_INDEX = 0x0A;
99     public static final int ASCII_CR_INDEX = 0x0D;
100     @UnsupportedAppUsage
101     public static final SparseIntArray charToAscii = new SparseIntArray();
102     static {
103         for (int i = 0; i < ASCII_MAP.length; i++) {
charToAscii.put(ASCII_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i)104             charToAscii.put(ASCII_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i);
105         }
106         charToAscii.put('\n', ASCII_NL_INDEX);
107         charToAscii.put('\r', ASCII_CR_INDEX);
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