1 /*
2  * Copyright (C) 2006 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.cellbroadcastservice;
18 
19 import android.text.TextUtils;
20 import android.util.Log;
21 import android.util.SparseIntArray;
22 
23 import java.nio.ByteBuffer;
24 import java.nio.charset.Charset;
25 
26 /**
27  * This class implements the character set mapping between
28  * the GSM SMS 7-bit alphabet specified in TS 23.038 6.2.1
29  * and UTF-16
30  *
31  * {@hide}
32  */
33 public class GsmAlphabet {
34     private static final String TAG = "GSM";
35 
GsmAlphabet()36     private GsmAlphabet() {
37     }
38 
39     /**
40      * This escapes extended characters, and when present indicates that the
41      * following character should be looked up in the "extended" table.
42      *
43      * gsmToChar(GSM_EXTENDED_ESCAPE) returns 0xffff
44      */
45     public static final byte GSM_EXTENDED_ESCAPE = 0x1B;
46 
47     /**
48      * User data header requires one octet for length. Count as one septet, because
49      * all combinations of header elements below will have at least one free bit
50      * when padding to the nearest septet boundary.
51      */
52     public static final int UDH_SEPTET_COST_LENGTH = 1;
53 
54     /**
55      * Using a non-default language locking shift table OR single shift table
56      * requires a user data header of 3 octets, or 4 septets, plus UDH length.
57      */
58     public static final int UDH_SEPTET_COST_ONE_SHIFT_TABLE = 4;
59 
60     /**
61      * Using a non-default language locking shift table AND single shift table
62      * requires a user data header of 6 octets, or 7 septets, plus UDH length.
63      */
64     public static final int UDH_SEPTET_COST_TWO_SHIFT_TABLES = 7;
65 
66     /**
67      * Multi-part messages require a user data header of 5 octets, or 6 septets,
68      * plus UDH length.
69      */
70     public static final int UDH_SEPTET_COST_CONCATENATED_MESSAGE = 6;
71 
72     /** Reverse mapping from Unicode characters to indexes into language tables. */
73     private static SparseIntArray[] sCharsToGsmTables;
74 
75     /** Reverse mapping from Unicode characters to indexes into language shift tables. */
76     private static SparseIntArray[] sCharsToShiftTables;
77 
78     /**
79      * GSM default 7 bit alphabet plus national language locking shift character tables.
80      * Comment lines above strings indicate the lower four bits of the table position.
81      */
82     private static final String[] sLanguageTables = {
83             /* 3GPP TS 23.038 V9.1.1 section 6.2.1 - GSM 7 bit Default Alphabet
84              01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0....
85              .1 */
86             "@\u00a3$\u00a5\u00e8\u00e9\u00f9\u00ec\u00f2\u00c7\n\u00d8\u00f8\r\u00c5\u00e5\u0394_"
87                     // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.
88                     // ....
89                     + "\u03a6\u0393\u039b\u03a9\u03a0\u03a8\u03a3\u0398\u039e\uffff\u00c6\u00e6"
90                     + "\u00df"
91                     // F.....012.34.....56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789A
92                     + "\u00c9 !\"#\u00a4%&'()*+,-./0123456789:;<=>?\u00a1ABCDEFGHIJKLMNOPQRSTUVWXYZ"
93                     // B.....C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C....
94                     // .D.....
95                     + "\u00c4\u00d6\u00d1\u00dc\u00a7\u00bfabcdefghijklmnopqrstuvwxyz\u00e4\u00f6"
96                     + "\u00f1"
97                     // E.....F.....
98                     + "\u00fc\u00e0",
99 
100             /* A.3.1 Turkish National Language Locking Shift Table
101              01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0....
102              .1 */
103             "@\u00a3$\u00a5\u20ac\u00e9\u00f9\u0131\u00f2\u00c7\n\u011e\u011f\r\u00c5\u00e5\u0394_"
104                     // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.
105                     // ....
106                     + "\u03a6\u0393\u039b\u03a9\u03a0\u03a8\u03a3\u0398\u039e\uffff\u015e\u015f"
107                     + "\u00df"
108                     // F.....012.34.....56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789A
109                     + "\u00c9 !\"#\u00a4%&'()*+,-./0123456789:;<=>?\u0130ABCDEFGHIJKLMNOPQRSTUVWXYZ"
110                     // B.....C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C....
111                     // .D.....
112                     + "\u00c4\u00d6\u00d1\u00dc\u00a7\u00e7abcdefghijklmnopqrstuvwxyz\u00e4\u00f6"
113                     + "\u00f1"
114                     // E.....F.....
115                     + "\u00fc\u00e0",
116 
117             /* A.3.2 Void (no locking shift table for Spanish) */
118             "",
119 
120             /* A.3.3 Portuguese National Language Locking Shift Table
121              01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0....
122              .1 */
123             "@\u00a3$\u00a5\u00ea\u00e9\u00fa\u00ed\u00f3\u00e7\n\u00d4\u00f4\r\u00c1\u00e1\u0394_"
124                     // 2.....3.....4.....5.....67.8.....9.....AB.....C.....D.....E.....F.....012
125                     // .34.....
126                     + "\u00aa\u00c7\u00c0\u221e^\\\u20ac\u00d3|\uffff\u00c2\u00e2\u00ca\u00c9 "
127                     + "!\"#\u00ba"
128                     // 56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789AB.....C.....D..
129                     // ...E.....
130                     + "%&'()*+,-./0123456789:;<=>?\u00cdABCDEFGHIJKLMNOPQRSTUVWXYZ\u00c3\u00d5"
131                     + "\u00da\u00dc"
132                     // F.....0123456789ABCDEF0123456789AB.....C.....DE.....F.....
133                     + "\u00a7~abcdefghijklmnopqrstuvwxyz\u00e3\u00f5`\u00fc\u00e0",
134 
135             /* A.3.4 Bengali National Language Locking Shift Table
136              0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.EF.....0..... */
137             "\u0981\u0982\u0983\u0985\u0986\u0987\u0988\u0989\u098a\u098b\n\u098c \r \u098f\u0990"
138                     // 123.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E....
139                     // .F.....
140                     + "  \u0993\u0994\u0995\u0996\u0997\u0998\u0999\u099a\uffff\u099b\u099c\u099d"
141                     + "\u099e"
142                     // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF....
143                     // .0123456789ABC
144                     + " !\u099f\u09a0\u09a1\u09a2\u09a3\u09a4)(\u09a5\u09a6,\u09a7"
145                     + ".\u09a80123456789:; "
146                     // D.....E.....F0.....1.....2.....3.....4.....56.....789A.....B.....C.....D.....
147                     + "\u09aa\u09ab?\u09ac\u09ad\u09ae\u09af\u09b0 \u09b2   "
148                     + "\u09b6\u09b7\u09b8\u09b9"
149                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....789.....A.....BCD...
150                     // ..E.....
151                     + "\u09bc\u09bd\u09be\u09bf\u09c0\u09c1\u09c2\u09c3\u09c4  \u09c7\u09c8  "
152                     + "\u09cb\u09cc"
153                     // F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
154                     + "\u09cd\u09ceabcdefghijklmnopqrstuvwxyz\u09d7\u09dc\u09dd\u09f0\u09f1",
155 
156             /* A.3.5 Gujarati National Language Locking Shift Table
157              0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.EF.....0
158              .....*/
159             "\u0a81\u0a82\u0a83\u0a85\u0a86\u0a87\u0a88\u0a89\u0a8a\u0a8b\n\u0a8c\u0a8d\r "
160                     + "\u0a8f\u0a90"
161                     // 1.....23.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E
162                     // .....
163                     + "\u0a91 \u0a93\u0a94\u0a95\u0a96\u0a97\u0a98\u0a99\u0a9a\uffff\u0a9b\u0a9c"
164                     + "\u0a9d"
165                     // F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF....
166                     // .0123456789AB
167                     + "\u0a9e !\u0a9f\u0aa0\u0aa1\u0aa2\u0aa3\u0aa4)(\u0aa5\u0aa6,\u0aa7"
168                     + ".\u0aa80123456789:;"
169                     // CD.....E.....F0.....1.....2.....3.....4.....56.....7.....89.....A.....B...
170                     // ..C.....
171                     + " \u0aaa\u0aab?\u0aac\u0aad\u0aae\u0aaf\u0ab0 \u0ab2\u0ab3 "
172                     + "\u0ab5\u0ab6\u0ab7\u0ab8"
173                     // D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89.....A
174                     // .....
175                     + "\u0ab9\u0abc\u0abd\u0abe\u0abf\u0ac0\u0ac1\u0ac2\u0ac3\u0ac4\u0ac5 "
176                     + "\u0ac7\u0ac8"
177                     // B.....CD.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D....
178                     // .E.....
179                     + "\u0ac9 \u0acb\u0acc\u0acd\u0ad0abcdefghijklmnopqrstuvwxyz\u0ae0\u0ae1"
180                     + "\u0ae2\u0ae3"
181                     // F.....
182                     + "\u0af1",
183 
184             /* A.3.6 Hindi National Language Locking Shift Table
185              0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.
186              ....*/
187             "\u0901\u0902\u0903\u0905\u0906\u0907\u0908\u0909\u090a\u090b\n\u090c\u090d\r\u090e"
188                     + "\u090f"
189                     // 0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.
190                     // ....D.....
191                     + "\u0910\u0911\u0912\u0913\u0914\u0915\u0916\u0917\u0918\u0919\u091a\uffff"
192                     + "\u091b\u091c"
193                     // E.....F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.
194                     // ....012345
195                     + "\u091d\u091e !\u091f\u0920\u0921\u0922\u0923\u0924)(\u0925\u0926,\u0927"
196                     + ".\u0928012345"
197                     // 6789ABC.....D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8
198                     // .....
199                     + "6789:;\u0929\u092a\u092b?\u092c\u092d\u092e\u092f\u0930\u0931\u0932\u0933"
200                     + "\u0934"
201                     // 9.....A.....B.....C.....D.....E.....F.....0.....1.....2.....3.....4.....5.
202                     // ....6.....
203                     + "\u0935\u0936\u0937\u0938\u0939\u093c\u093d\u093e\u093f\u0940\u0941\u0942"
204                     + "\u0943\u0944"
205                     // 7.....8.....9.....A.....B.....C.....D.....E.....F.....0....
206                     // .123456789ABCDEF012345678
207                     + "\u0945\u0946\u0947\u0948\u0949\u094a\u094b\u094c\u094d"
208                     + "\u0950abcdefghijklmnopqrstuvwx"
209                     // 9AB.....C.....D.....E.....F.....
210                     + "yz\u0972\u097b\u097c\u097e\u097f",
211 
212             /* A.3.7 Kannada National Language Locking Shift Table
213                NOTE: TS 23.038 V9.1.1 shows code 0x24 as \u0caa, corrected to \u0ca1 (typo)
214              01.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0....
215              .1 */
216             " \u0c82\u0c83\u0c85\u0c86\u0c87\u0c88\u0c89\u0c8a\u0c8b\n\u0c8c \r\u0c8e\u0c8f\u0c90 "
217                     // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.
218                     // ....F.....
219                     + "\u0c92\u0c93\u0c94\u0c95\u0c96\u0c97\u0c98\u0c99\u0c9a\uffff\u0c9b\u0c9c"
220                     + "\u0c9d\u0c9e"
221                     // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF....
222                     // .0123456789ABC
223                     + " !\u0c9f\u0ca0\u0ca1\u0ca2\u0ca3\u0ca4)(\u0ca5\u0ca6,\u0ca7"
224                     + ".\u0ca80123456789:; "
225                     // D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....89.....A....
226                     // .B.....
227                     + "\u0caa\u0cab?\u0cac\u0cad\u0cae\u0caf\u0cb0\u0cb1\u0cb2\u0cb3 "
228                     + "\u0cb5\u0cb6\u0cb7"
229                     // C.....D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....78.....9
230                     // .....
231                     + "\u0cb8\u0cb9\u0cbc\u0cbd\u0cbe\u0cbf\u0cc0\u0cc1\u0cc2\u0cc3\u0cc4 "
232                     + "\u0cc6\u0cc7"
233                     // A.....BC.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C....
234                     // .D.....
235                     + "\u0cc8 \u0cca\u0ccb\u0ccc\u0ccd\u0cd5abcdefghijklmnopqrstuvwxyz\u0cd6"
236                     + "\u0ce0\u0ce1"
237                     // E.....F.....
238                     + "\u0ce2\u0ce3",
239 
240             /* A.3.8 Malayalam National Language Locking Shift Table
241              01.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0....
242              .1 */
243             " \u0d02\u0d03\u0d05\u0d06\u0d07\u0d08\u0d09\u0d0a\u0d0b\n\u0d0c \r\u0d0e\u0d0f\u0d10 "
244                     // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.
245                     // ....F.....
246                     + "\u0d12\u0d13\u0d14\u0d15\u0d16\u0d17\u0d18\u0d19\u0d1a\uffff\u0d1b\u0d1c"
247                     + "\u0d1d\u0d1e"
248                     // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF....
249                     // .0123456789ABC
250                     + " !\u0d1f\u0d20\u0d21\u0d22\u0d23\u0d24)(\u0d25\u0d26,\u0d27"
251                     + ".\u0d280123456789:; "
252                     // D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A
253                     // .....
254                     + "\u0d2a\u0d2b?\u0d2c\u0d2d\u0d2e\u0d2f\u0d30\u0d31\u0d32\u0d33\u0d34\u0d35"
255                     + "\u0d36"
256                     // B.....C.....D.....EF.....0.....1.....2.....3.....4.....5.....6.....78....
257                     // .9.....
258                     + "\u0d37\u0d38\u0d39 \u0d3d\u0d3e\u0d3f\u0d40\u0d41\u0d42\u0d43\u0d44 "
259                     + "\u0d46\u0d47"
260                     // A.....BC.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C....
261                     // .D.....
262                     + "\u0d48 \u0d4a\u0d4b\u0d4c\u0d4d\u0d57abcdefghijklmnopqrstuvwxyz\u0d60"
263                     + "\u0d61\u0d62"
264                     // E.....F.....
265                     + "\u0d63\u0d79",
266 
267             /* A.3.9 Oriya National Language Locking Shift Table
268              0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.EF.....0....
269              .12 */
270             "\u0b01\u0b02\u0b03\u0b05\u0b06\u0b07\u0b08\u0b09\u0b0a\u0b0b\n\u0b0c \r \u0b0f\u0b10  "
271                     // 3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.
272                     // ....01
273                     + "\u0b13\u0b14\u0b15\u0b16\u0b17\u0b18\u0b19\u0b1a\uffff\u0b1b\u0b1c\u0b1d"
274                     + "\u0b1e !"
275                     // 2.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF....
276                     // .0123456789ABCD.....
277                     + "\u0b1f\u0b20\u0b21\u0b22\u0b23\u0b24)(\u0b25\u0b26,\u0b27"
278                     + ".\u0b280123456789:; \u0b2a"
279                     // E.....F0.....1.....2.....3.....4.....56.....7.....89.....A.....B.....C....
280                     // .D.....
281                     + "\u0b2b?\u0b2c\u0b2d\u0b2e\u0b2f\u0b30 \u0b32\u0b33 "
282                     + "\u0b35\u0b36\u0b37\u0b38\u0b39"
283                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....789.....A.....BCD...
284                     // ..E.....
285                     + "\u0b3c\u0b3d\u0b3e\u0b3f\u0b40\u0b41\u0b42\u0b43\u0b44  \u0b47\u0b48  "
286                     + "\u0b4b\u0b4c"
287                     // F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
288                     + "\u0b4d\u0b56abcdefghijklmnopqrstuvwxyz\u0b57\u0b60\u0b61\u0b62\u0b63",
289 
290             /* A.3.10 Punjabi National Language Locking Shift Table
291              0.....1.....2.....3.....4.....5.....6.....7.....8.....9A.BCD.EF.....0.....123.....4.
292              ....*/
293             "\u0a01\u0a02\u0a03\u0a05\u0a06\u0a07\u0a08\u0a09\u0a0a \n  \r \u0a0f\u0a10  "
294                     + "\u0a13\u0a14"
295                     // 5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....012....
296                     // .3.....
297                     + "\u0a15\u0a16\u0a17\u0a18\u0a19\u0a1a\uffff\u0a1b\u0a1c\u0a1d\u0a1e "
298                     + "!\u0a1f\u0a20"
299                     // 4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABCD.....E..
300                     // ...F0.....
301                     + "\u0a21\u0a22\u0a23\u0a24)(\u0a25\u0a26,\u0a27.\u0a280123456789:; "
302                     + "\u0a2a\u0a2b?\u0a2c"
303                     // 1.....2.....3.....4.....56.....7.....89.....A.....BC.....D.....E.....F0...
304                     // ..1.....
305                     + "\u0a2d\u0a2e\u0a2f\u0a30 \u0a32\u0a33 \u0a35\u0a36 \u0a38\u0a39\u0a3c "
306                     + "\u0a3e\u0a3f"
307                     // 2.....3.....4.....56789.....A.....BCD.....E.....F.....0....
308                     // .123456789ABCDEF012345678
309                     + "\u0a40\u0a41\u0a42    \u0a47\u0a48  "
310                     + "\u0a4b\u0a4c\u0a4d\u0a51abcdefghijklmnopqrstuvwx"
311                     // 9AB.....C.....D.....E.....F.....
312                     + "yz\u0a70\u0a71\u0a72\u0a73\u0a74",
313 
314             /* A.3.11 Tamil National Language Locking Shift Table
315              01.....2.....3.....4.....5.....6.....7.....8.....9A.BCD.E.....F.....0.....12.....3..
316              ... */
317             " \u0b82\u0b83\u0b85\u0b86\u0b87\u0b88\u0b89\u0b8a \n  \r\u0b8e\u0b8f\u0b90 "
318                     + "\u0b92\u0b93"
319                     // 4.....5.....6789.....A.....B.....CD.....EF.....012.....3456.....7....
320                     // .89ABCDEF.....
321                     + "\u0b94\u0b95   \u0b99\u0b9a\uffff \u0b9c \u0b9e !\u0b9f   \u0ba3\u0ba4)(  "
322                     + ", .\u0ba8"
323                     // 0123456789ABC.....D.....EF012.....3.....4.....5.....6.....7.....8.....9...
324                     // ..A.....
325                     + "0123456789:;\u0ba9\u0baa ?  "
326                     + "\u0bae\u0baf\u0bb0\u0bb1\u0bb2\u0bb3\u0bb4\u0bb5\u0bb6"
327                     // B.....C.....D.....EF0.....1.....2.....3.....4.....5678.....9.....A.....BC.
328                     // ....D.....
329                     + "\u0bb7\u0bb8\u0bb9  \u0bbe\u0bbf\u0bc0\u0bc1\u0bc2   \u0bc6\u0bc7\u0bc8 "
330                     + "\u0bca\u0bcb"
331                     // E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
332                     + "\u0bcc\u0bcd\u0bd0abcdefghijklmnopqrstuvwxyz\u0bd7\u0bf0\u0bf1\u0bf2\u0bf9",
333 
334             /* A.3.12 Telugu National Language Locking Shift Table
335              0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0
336              .....*/
337             "\u0c01\u0c02\u0c03\u0c05\u0c06\u0c07\u0c08\u0c09\u0c0a\u0c0b\n\u0c0c "
338                     + "\r\u0c0e\u0c0f\u0c10"
339                     // 12.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E
340                     // .....
341                     + " \u0c12\u0c13\u0c14\u0c15\u0c16\u0c17\u0c18\u0c19\u0c1a\uffff\u0c1b\u0c1c"
342                     + "\u0c1d"
343                     // F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF....
344                     // .0123456789AB
345                     + "\u0c1e !\u0c1f\u0c20\u0c21\u0c22\u0c23\u0c24)(\u0c25\u0c26,\u0c27"
346                     + ".\u0c280123456789:;"
347                     // CD.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....89.....A....
348                     // .B.....
349                     + " \u0c2a\u0c2b?\u0c2c\u0c2d\u0c2e\u0c2f\u0c30\u0c31\u0c32\u0c33 "
350                     + "\u0c35\u0c36\u0c37"
351                     // C.....D.....EF.....0.....1.....2.....3.....4.....5.....6.....78.....9....
352                     // .A.....B
353                     + "\u0c38\u0c39 \u0c3d\u0c3e\u0c3f\u0c40\u0c41\u0c42\u0c43\u0c44 "
354                     + "\u0c46\u0c47\u0c48 "
355                     // C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D....
356                     // .E.....
357                     + "\u0c4a\u0c4b\u0c4c\u0c4d\u0c55abcdefghijklmnopqrstuvwxyz\u0c56\u0c60\u0c61"
358                     + "\u0c62"
359                     // F.....
360                     + "\u0c63",
361 
362             /* A.3.13 Urdu National Language Locking Shift Table
363              0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.
364              ....*/
365             "\u0627\u0622\u0628\u067b\u0680\u067e\u06a6\u062a\u06c2\u067f\n\u0679\u067d\r\u067a"
366                     + "\u067c"
367                     // 0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.
368                     // ....D.....
369                     + "\u062b\u062c\u0681\u0684\u0683\u0685\u0686\u0687\u062d\u062e\u062f\uffff"
370                     + "\u068c\u0688"
371                     // E.....F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.
372                     // ....012345
373                     + "\u0689\u068a !\u068f\u068d\u0630\u0631\u0691\u0693)(\u0699\u0632,\u0696"
374                     + ".\u0698012345"
375                     // 6789ABC.....D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8
376                     // .....
377                     + "6789:;\u069a\u0633\u0634?\u0635\u0636\u0637\u0638\u0639\u0641\u0642\u06a9"
378                     + "\u06aa"
379                     // 9.....A.....B.....C.....D.....E.....F.....0.....1.....2.....3.....4.....5.
380                     // ....6.....
381                     + "\u06ab\u06af\u06b3\u06b1\u0644\u0645\u0646\u06ba\u06bb\u06bc\u0648\u06c4"
382                     + "\u06d5\u06c1"
383                     // 7.....8.....9.....A.....B.....C.....D.....E.....F.....0....
384                     // .123456789ABCDEF012345678
385                     + "\u06be\u0621\u06cc\u06d0\u06d2\u064d\u0650\u064f\u0657"
386                     + "\u0654abcdefghijklmnopqrstuvwx"
387                     // 9AB.....C.....D.....E.....F.....
388                     + "yz\u0655\u0651\u0653\u0656\u0670"
389     };
390 
391     /**
392      * GSM default extension table plus national language single shift character tables.
393      */
394     private static final String[] sLanguageShiftTables = new String[]{
395             /* 6.2.1.1 GSM 7 bit Default Alphabet Extension Table
396              0123456789A.....BCDEF0123456789ABCDEF0123456789ABCDEF
397              .0123456789ABCDEF0123456789ABCDEF */
398             "          \u000c         ^                   {}     \\            [~] |               "
399                     // 0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
400                     + "                     \u20ac                          ",
401 
402             /* A.2.1 Turkish National Language Single Shift Table
403              0123456789A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF01234567.....8 */
404             "          \u000c         ^                   {}     \\            [~] |      \u011e "
405                     // 9.....ABCDEF0123.....456789ABCDEF0123.....45.....67.....89.....ABCDEF0123.
406                     // ....
407                     + "\u0130         \u015e               \u00e7 \u20ac \u011f \u0131         "
408                     + "\u015f"
409                     // 456789ABCDEF
410                     + "            ",
411 
412             /* A.2.2 Spanish National Language Single Shift Table
413              0123456789.....A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF01.....23 */
414             "         \u00e7\u000c         ^                   {}     \\            [~] |\u00c1  "
415                     // 456789.....ABCDEF.....012345.....6789ABCDEF01.....2345.....6789.....ABCDEF
416                     // .....012
417                     + "     \u00cd     \u00d3     \u00da           \u00e1   \u20ac   \u00ed     "
418                     + "\u00f3   "
419                     // 345.....6789ABCDEF
420                     + "  \u00fa          ",
421 
422             /* A.2.3 Portuguese National Language Single Shift Table
423              012345.....6789.....A.....B.....C.....DE.....F.....012.....3.....45.....6.....7....
424              .8....*/
425             "     \u00ea   \u00e7\u000c\u00d4\u00f4 \u00c1\u00e1  "
426                     + "\u03a6\u0393^\u03a9\u03a0\u03a8\u03a3"
427                     // 9.....ABCDEF.....0123456789ABCDEF.0123456789ABCDEF01.....23456789.....ABCDE
428                     + "\u0398     \u00ca        {}     \\            [~] |\u00c0       \u00cd     "
429                     // F.....012345.....6789AB.....C.....DEF01.....2345.....6789.....ABCDEF....
430                     // .01234
431                     + "\u00d3     \u00da     \u00c3\u00d5    \u00c2   \u20ac   \u00ed     \u00f3 "
432                     + "    "
433                     // 5.....6789AB.....C.....DEF.....
434                     + "\u00fa     \u00e3\u00f5  \u00e2",
435 
436             /* A.2.4 Bengali National Language Single Shift Table
437              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
438              .. */
439             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u09e6\u09e7 \u09e8\u09e9"
440                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B....
441                     // .C.....
442                     + "\u09ea\u09eb\u09ec\u09ed\u09ee\u09ef\u09df\u09e0\u09e1\u09e2{}\u09e3\u09f2"
443                     + "\u09f3"
444                     // D.....E.....F.0.....1.....2.....3.....4.....56789ABCDEF0123456789ABCDEF
445                     + "\u09f4\u09f5\\\u09f6\u09f7\u09f8\u09f9\u09fa       [~] |ABCDEFGHIJKLMNO"
446                     // 0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
447                     + "PQRSTUVWXYZ          \u20ac                          ",
448 
449             /* A.2.5 Gujarati National Language Single Shift Table
450              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
451              .. */
452             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0ae6\u0ae7"
453                     // E.....F.....0.....1.....2.....3.....4.....5.....6789ABCDEF.0123456789ABCDEF
454                     + "\u0ae8\u0ae9\u0aea\u0aeb\u0aec\u0aed\u0aee\u0aef  {}     \\            [~] "
455                     // 0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
456                     + "|ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
457 
458             /* A.2.6 Hindi National Language Single Shift Table
459              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
460              .. */
461             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0966\u0967"
462                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B....
463                     // .C.....
464                     + "\u0968\u0969\u096a\u096b\u096c\u096d\u096e\u096f\u0951\u0952{}\u0953\u0954"
465                     + "\u0958"
466                     // D.....E.....F.0.....1.....2.....3.....4.....5.....6.....7.....8.....9....
467                     // .A.....
468                     + "\u0959\u095a\\\u095b\u095c\u095d\u095e\u095f\u0960\u0961\u0962\u0963\u0970"
469                     + "\u0971"
470                     // BCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
471                     + " [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
472 
473             /* A.2.7 Kannada National Language Single Shift Table
474              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
475              .. */
476             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0ce6\u0ce7"
477                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....BCDEF
478                     // .01234567
479                     + "\u0ce8\u0ce9\u0cea\u0ceb\u0cec\u0ced\u0cee\u0cef\u0cde\u0cf1{}\u0cf2    \\"
480                     + "        "
481                     // 89ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
482                     + "    [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                       "
483                     + "   ",
484 
485             /* A.2.8 Malayalam National Language Single Shift Table
486              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
487              .. */
488             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0d66\u0d67"
489                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B....
490                     // .C.....
491                     + "\u0d68\u0d69\u0d6a\u0d6b\u0d6c\u0d6d\u0d6e\u0d6f\u0d70\u0d71{}\u0d72\u0d73"
492                     + "\u0d74"
493                     // D.....E.....F.0.....1.....2.....3.....4....
494                     // .56789ABCDEF0123456789ABCDEF0123456789A
495                     + "\u0d75\u0d7a\\\u0d7b\u0d7c\u0d7d\u0d7e\u0d7f       [~] "
496                     + "|ABCDEFGHIJKLMNOPQRSTUVWXYZ"
497                     // BCDEF012345.....6789ABCDEF0123456789ABCDEF
498                     + "          \u20ac                          ",
499 
500             /* A.2.9 Oriya National Language Single Shift Table
501              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
502              .. */
503             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0b66\u0b67"
504                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B....
505                     // .C.....DE
506                     + "\u0b68\u0b69\u0b6a\u0b6b\u0b6c\u0b6d\u0b6e\u0b6f\u0b5c\u0b5d{}\u0b5f\u0b70"
507                     + "\u0b71  "
508                     // F.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345....
509                     // .6789ABCDEF0123456789A
510                     + "\\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac             "
511                     + "        "
512                     // BCDEF
513                     + "     ",
514 
515             /* A.2.10 Punjabi National Language Single Shift Table
516              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
517              .. */
518             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0a66\u0a67"
519                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B....
520                     // .C.....
521                     + "\u0a68\u0a69\u0a6a\u0a6b\u0a6c\u0a6d\u0a6e\u0a6f\u0a59\u0a5a{}\u0a5b\u0a5c"
522                     + "\u0a5e"
523                     // D.....EF.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345....
524                     // .6789ABCDEF01
525                     + "\u0a75 \\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac      "
526                     + "      "
527                     // 23456789ABCDEF
528                     + "              ",
529 
530             /* A.2.11 Tamil National Language Single Shift Table
531                NOTE: TS 23.038 V9.1.1 shows code 0x24 as \u0bef, corrected to \u0bee (typo)
532              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
533              .. */
534             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0be6\u0be7"
535                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B....
536                     // .C.....
537                     + "\u0be8\u0be9\u0bea\u0beb\u0bec\u0bed\u0bee\u0bef\u0bf3\u0bf4{}\u0bf5\u0bf6"
538                     + "\u0bf7"
539                     // D.....E.....F.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345....
540                     // .6789ABC
541                     + "\u0bf8\u0bfa\\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac "
542                     + "      "
543                     // DEF0123456789ABCDEF
544                     + "                   ",
545 
546             /* A.2.12 Telugu National Language Single Shift Table
547                NOTE: TS 23.038 V9.1.1 shows code 0x22-0x23 as \u06cc\u06cd, corrected to
548                \u0c6c\u0c6d
549              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789ABC.....D.....E.....F.
550              .... */
551             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*   "
552                     + "\u0c66\u0c67\u0c68\u0c69"
553                     // 0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....D....
554                     // .E.....F.
555                     + "\u0c6a\u0c6b\u0c6c\u0c6d\u0c6e\u0c6f\u0c58\u0c59{}\u0c78\u0c79\u0c7a\u0c7b"
556                     + "\u0c7c\\"
557                     // 0.....1.....2.....3456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345....
558                     // .6789ABCD
559                     + "\u0c7d\u0c7e\u0c7f         [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac"
560                     + "        "
561                     // EF0123456789ABCDEF
562                     + "                  ",
563 
564             /* A.2.13 Urdu National Language Single Shift Table
565              01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D...
566              .. */
567             "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0600\u0601 \u06f0\u06f1"
568                     // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B....
569                     // .C.....
570                     + "\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9\u060c\u060d{}\u060e\u060f"
571                     + "\u0610"
572                     // D.....E.....F.0.....1.....2.....3.....4.....5.....6.....7.....8.....9....
573                     // .A.....
574                     + "\u0611\u0612\\\u0613\u0614\u061b\u061f\u0640\u0652\u0658\u066b\u066c\u0672"
575                     + "\u0673"
576                     // B.....CDEF.....0123456789ABCDEF0123456789ABCDEF012345....
577                     // .6789ABCDEF0123456789ABCDEF
578                     + "\u06cd[~]\u06d4|ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                "
579                     + "          "
580     };
581 
582     static {
583         int numTables = sLanguageTables.length;
584         int numShiftTables = sLanguageShiftTables.length;
585         if (numTables != numShiftTables) {
Log.e(TAG, "Error: language tables array length " + numTables + " != shift tables array length " + numShiftTables)586             Log.e(TAG, "Error: language tables array length " + numTables
587                     + " != shift tables array length " + numShiftTables);
588         }
589 
590         sCharsToGsmTables = new SparseIntArray[numTables];
591         for (int i = 0; i < numTables; i++) {
592             String table = sLanguageTables[i];
593 
594             int tableLen = table.length();
595             if (tableLen != 0 && tableLen != 128) {
Log.e(TAG, "Error: language tables index " + i + " length " + tableLen + " (expected 128 or 0)")596                 Log.e(TAG, "Error: language tables index " + i + " length " + tableLen
597                         + " (expected 128 or 0)");
598             }
599 
600             SparseIntArray charToGsmTable = new SparseIntArray(tableLen);
601             sCharsToGsmTables[i] = charToGsmTable;
602             for (int j = 0; j < tableLen; j++) {
603                 char c = table.charAt(j);
charToGsmTable.put(c, j)604                 charToGsmTable.put(c, j);
605             }
606         }
607 
608         sCharsToShiftTables = new SparseIntArray[numShiftTables];
609         for (int i = 0; i < numShiftTables; i++) {
610             String shiftTable = sLanguageShiftTables[i];
611 
612             int shiftTableLen = shiftTable.length();
613             if (shiftTableLen != 0 && shiftTableLen != 128) {
Log.e(TAG, "Error: language shift tables index " + i + " length " + shiftTableLen + " (expected 128 or 0)")614                 Log.e(TAG, "Error: language shift tables index " + i + " length " + shiftTableLen
615                         + " (expected 128 or 0)");
616             }
617 
618             SparseIntArray charToShiftTable = new SparseIntArray(shiftTableLen);
619             sCharsToShiftTables[i] = charToShiftTable;
620             for (int j = 0; j < shiftTableLen; j++) {
621                 char c = shiftTable.charAt(j);
622                 if (c != ' ') {
charToShiftTable.put(c, j)623                     charToShiftTable.put(c, j);
624                 }
625             }
626         }
627     }
628 
629     /**
630      * Convert a GSM alphabet 7 bit packed string (SMS string) into a
631      * {@link java.lang.String}.
632      *
633      * See TS 23.038 6.1.2.1 for SMS Character Packing
634      *
635      * @param pdu           the raw data from the pdu
636      * @param offset        the byte offset of
637      * @param lengthSeptets string length in septets, not bytes
638      * @return String representation or null on decoding exception
639      */
gsm7BitPackedToString(byte[] pdu, int offset, int lengthSeptets)640     public static String gsm7BitPackedToString(byte[] pdu, int offset,
641             int lengthSeptets) {
642         return gsm7BitPackedToString(pdu, offset, lengthSeptets, 0, 0, 0);
643     }
644 
645     /**
646      * Convert a GSM alphabet 7 bit packed string (SMS string) into a
647      * {@link java.lang.String}.
648      *
649      * See TS 23.038 6.1.2.1 for SMS Character Packing
650      *
651      * @param pdu            the raw data from the pdu
652      * @param offset         the byte offset of
653      * @param lengthSeptets  string length in septets, not bytes
654      * @param numPaddingBits the number of padding bits before the start of the
655      *                       string in the first byte
656      * @param languageTable  the 7 bit language table, or 0 for the default GSM alphabet
657      * @param shiftTable     the 7 bit single shift language table, or 0 for the default
658      *                       GSM extension table
659      * @return String representation or null on decoding exception
660      */
gsm7BitPackedToString(byte[] pdu, int offset, int lengthSeptets, int numPaddingBits, int languageTable, int shiftTable)661     public static String gsm7BitPackedToString(byte[] pdu, int offset,
662             int lengthSeptets, int numPaddingBits, int languageTable, int shiftTable) {
663         StringBuilder ret = new StringBuilder(lengthSeptets);
664 
665         if (languageTable < 0 || languageTable > sLanguageTables.length) {
666             Log.w(TAG, "unknown language table " + languageTable + ", using default");
667             languageTable = 0;
668         }
669         if (shiftTable < 0 || shiftTable > sLanguageShiftTables.length) {
670             Log.w(TAG, "unknown single shift table " + shiftTable + ", using default");
671             shiftTable = 0;
672         }
673 
674         try {
675             boolean prevCharWasEscape = false;
676             String languageTableToChar = sLanguageTables[languageTable];
677             String shiftTableToChar = sLanguageShiftTables[shiftTable];
678 
679             if (languageTableToChar.isEmpty()) {
680                 Log.w(TAG, "no language table for code " + languageTable + ", using default");
681                 languageTableToChar = sLanguageTables[0];
682             }
683             if (shiftTableToChar.isEmpty()) {
684                 Log.w(TAG, "no single shift table for code " + shiftTable + ", using default");
685                 shiftTableToChar = sLanguageShiftTables[0];
686             }
687 
688             for (int i = 0; i < lengthSeptets; i++) {
689                 int bitOffset = (7 * i) + numPaddingBits;
690 
691                 int byteOffset = bitOffset / 8;
692                 int shift = bitOffset % 8;
693                 int gsmVal;
694 
695                 gsmVal = (0x7f & (pdu[offset + byteOffset] >> shift));
696 
697                 // if it crosses a byte boundary
698                 if (shift > 1) {
699                     // set msb bits to 0
700                     gsmVal &= 0x7f >> (shift - 1);
701 
702                     gsmVal |= 0x7f & (pdu[offset + byteOffset + 1] << (8 - shift));
703                 }
704 
705                 if (prevCharWasEscape) {
706                     if (gsmVal == GSM_EXTENDED_ESCAPE) {
707                         ret.append(' ');    // display ' ' for reserved double escape sequence
708                     } else {
709                         char c = shiftTableToChar.charAt(gsmVal);
710                         if (c == ' ') {
711                             ret.append(languageTableToChar.charAt(gsmVal));
712                         } else {
713                             ret.append(c);
714                         }
715                     }
716                     prevCharWasEscape = false;
717                 } else if (gsmVal == GSM_EXTENDED_ESCAPE) {
718                     prevCharWasEscape = true;
719                 } else {
720                     ret.append(languageTableToChar.charAt(gsmVal));
721                 }
722             }
723         } catch (RuntimeException ex) {
724             Log.e(TAG, "Error GSM 7 bit packed: ", ex);
725             return null;
726         }
727 
728         return ret.toString();
729     }
730 
731     /**
732      * Convert a GSM alphabet string that's stored in 8-bit unpacked
733      * format (as it often appears in SIM records) into a String
734      *
735      * Field may be padded with trailing 0xff's. The decode stops
736      * at the first 0xff encountered.
737      *
738      * @param data the byte array to decode
739      * @param offset array offset for the first character to decode
740      * @param length the number of bytes to decode
741      * @return the decoded string
742      */
gsm8BitUnpackedToString(byte[] data, int offset, int length)743     public static String gsm8BitUnpackedToString(byte[] data, int offset, int length) {
744         return gsm8BitUnpackedToString(data, offset, length, "");
745     }
746 
747     /**
748      * Convert a GSM alphabet string that's stored in 8-bit unpacked
749      * format (as it often appears in SIM records) into a String
750      *
751      * Field may be padded with trailing 0xff's. The decode stops
752      * at the first 0xff encountered.
753      *
754      * Additionally, in some country(ex. Korea), there are non-ASCII or MBCS characters.
755      * If a character set is given, characters in data are treat as MBCS.
756      */
gsm8BitUnpackedToString( byte[] data, int offset, int length, String characterset)757     public static String gsm8BitUnpackedToString(
758             byte[] data, int offset, int length, String characterset) {
759         boolean isMbcs = false;
760         Charset charset = null;
761         ByteBuffer mbcsBuffer = null;
762 
763         if (!TextUtils.isEmpty(characterset)
764                 && !characterset.equalsIgnoreCase("us-ascii")
765                 && Charset.isSupported(characterset)) {
766             isMbcs = true;
767             charset = Charset.forName(characterset);
768             mbcsBuffer = ByteBuffer.allocate(2);
769         }
770 
771         // Always use GSM 7 bit default alphabet table for this method
772         String languageTableToChar = sLanguageTables[0];
773         String shiftTableToChar = sLanguageShiftTables[0];
774 
775         StringBuilder ret = new StringBuilder(length);
776         boolean prevWasEscape = false;
777         for (int i = offset; i < offset + length; i++) {
778             // Never underestimate the pain that can be caused
779             // by signed bytes
780             int c = data[i] & 0xff;
781 
782             if (c == 0xff) {
783                 break;
784             } else if (c == GSM_EXTENDED_ESCAPE) {
785                 if (prevWasEscape) {
786                     // Two escape chars in a row
787                     // We treat this as a space
788                     // See Note 1 in table 6.2.1.1 of TS 23.038 v7.00
789                     ret.append(' ');
790                     prevWasEscape = false;
791                 } else {
792                     prevWasEscape = true;
793                 }
794             } else {
795                 if (prevWasEscape) {
796                     char shiftChar =
797                             c < shiftTableToChar.length() ? shiftTableToChar.charAt(c) : ' ';
798                     if (shiftChar == ' ') {
799                         // display character from main table if not present in shift table
800                         if (c < languageTableToChar.length()) {
801                             ret.append(languageTableToChar.charAt(c));
802                         } else {
803                             ret.append(' ');
804                         }
805                     } else {
806                         ret.append(shiftChar);
807                     }
808                 } else {
809                     if (!isMbcs || c < 0x80 || i + 1 >= offset + length) {
810                         if (c < languageTableToChar.length()) {
811                             ret.append(languageTableToChar.charAt(c));
812                         } else {
813                             ret.append(' ');
814                         }
815                     } else {
816                         // isMbcs must be true. So both mbcsBuffer and charset are initialized.
817                         mbcsBuffer.clear();
818                         mbcsBuffer.put(data, i++, 2);
819                         mbcsBuffer.flip();
820                         ret.append(charset.decode(mbcsBuffer).toString());
821                     }
822                 }
823                 prevWasEscape = false;
824             }
825         }
826 
827         return ret.toString();
828     }
829 }
830