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.internal.telephony;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.res.Resources;
21 import android.os.Build;
22 import android.util.Log;
23 import android.text.TextUtils;
24 import android.util.SparseIntArray;
25 
26 import com.android.internal.R;
27 
28 import java.nio.ByteBuffer;
29 import java.nio.charset.Charset;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * This class implements the character set mapping between
35  * the GSM SMS 7-bit alphabet specified in TS 23.038 6.2.1
36  * and UTF-16
37  *
38  * {@hide}
39  */
40 public class GsmAlphabet {
41     private static final String TAG = "GSM";
42 
43     /**
44      * This escapes extended characters, and when present indicates that the
45      * following character should be looked up in the "extended" table.
46      *
47      * gsmToChar(GSM_EXTENDED_ESCAPE) returns 0xffff
48      */
49     public static final byte GSM_EXTENDED_ESCAPE = 0x1B;
50 
51     /**
52      * User data header requires one octet for length. Count as one septet, because
53      * all combinations of header elements below will have at least one free bit
54      * when padding to the nearest septet boundary.
55      */
56     public static final int UDH_SEPTET_COST_LENGTH = 1;
57 
58     /**
59      * Using a non-default language locking shift table OR single shift table
60      * requires a user data header of 3 octets, or 4 septets, plus UDH length.
61      */
62     public static final int UDH_SEPTET_COST_ONE_SHIFT_TABLE = 4;
63 
64     /**
65      * Using a non-default language locking shift table AND single shift table
66      * requires a user data header of 6 octets, or 7 septets, plus UDH length.
67      */
68     public static final int UDH_SEPTET_COST_TWO_SHIFT_TABLES = 7;
69 
70     /**
71      * Multi-part messages require a user data header of 5 octets, or 6 septets,
72      * plus UDH length.
73      */
74     public static final int UDH_SEPTET_COST_CONCATENATED_MESSAGE = 6;
75 
76     /**
77      * For a specific text string, this object describes protocol
78      * properties of encoding it for transmission as message user
79      * data.
80      */
81     public static class TextEncodingDetails {
82 
83         @UnsupportedAppUsage
TextEncodingDetails()84         public TextEncodingDetails() {
85         }
86 
87         /**
88          *The number of SMS's required to encode the text.
89          */
90         @UnsupportedAppUsage
91         public int msgCount;
92 
93         /**
94          * The number of code units consumed so far, where code units
95          * are basically characters in the encoding -- for example,
96          * septets for the standard ASCII and GSM encodings, and 16
97          * bits for Unicode.
98          */
99         @UnsupportedAppUsage
100         public int codeUnitCount;
101 
102         /**
103          * How many code units are still available without spilling
104          * into an additional message.
105          */
106         @UnsupportedAppUsage
107         public int codeUnitsRemaining;
108 
109         /**
110          * The encoding code unit size (specified using
111          * android.telephony.SmsMessage ENCODING_*).
112          */
113         @UnsupportedAppUsage
114         public int codeUnitSize;
115 
116         /**
117          * The GSM national language table to use, or 0 for the default 7-bit alphabet.
118          */
119         @UnsupportedAppUsage
120         public int languageTable;
121 
122         /**
123          * The GSM national language shift table to use, or 0 for the default 7-bit extension table.
124          */
125         @UnsupportedAppUsage
126         public int languageShiftTable;
127 
128         @Override
toString()129         public String toString() {
130             return "TextEncodingDetails " +
131                     "{ msgCount=" + msgCount +
132                     ", codeUnitCount=" + codeUnitCount +
133                     ", codeUnitsRemaining=" + codeUnitsRemaining +
134                     ", codeUnitSize=" + codeUnitSize +
135                     ", languageTable=" + languageTable +
136                     ", languageShiftTable=" + languageShiftTable +
137                     " }";
138         }
139     }
140 
141     /**
142      * Converts a char to a GSM 7 bit table index.
143      * Returns ' ' in GSM alphabet if there's no possible match. Returns
144      * GSM_EXTENDED_ESCAPE if this character is in the extended table.
145      * In this case, you must call charToGsmExtended() for the value
146      * that should follow GSM_EXTENDED_ESCAPE in the GSM alphabet string.
147      * @param c the character to convert
148      * @return the GSM 7 bit table index for the specified character
149      */
150     @UnsupportedAppUsage
151     public static int
charToGsm(char c)152     charToGsm(char c) {
153         try {
154             return charToGsm(c, false);
155         } catch (EncodeException ex) {
156             // this should never happen
157             return sCharsToGsmTables[0].get(' ', ' ');
158         }
159     }
160 
161     /**
162      * Converts a char to a GSM 7 bit table index.
163      * Returns GSM_EXTENDED_ESCAPE if this character is in the extended table.
164      * In this case, you must call charToGsmExtended() for the value that
165      * should follow GSM_EXTENDED_ESCAPE in the GSM alphabet string.
166      *
167      * @param c the character to convert
168      * @param throwException If true, throws EncodeException on invalid char.
169      *   If false, returns GSM alphabet ' ' char.
170      * @throws EncodeException encode error when throwException is true
171      * @return the GSM 7 bit table index for the specified character
172      */
173     @UnsupportedAppUsage
174     public static int
charToGsm(char c, boolean throwException)175     charToGsm(char c, boolean throwException) throws EncodeException {
176         int ret;
177 
178         ret = sCharsToGsmTables[0].get(c, -1);
179 
180         if (ret == -1) {
181             ret = sCharsToShiftTables[0].get(c, -1);
182 
183             if (ret == -1) {
184                 if (throwException) {
185                     throw new EncodeException(c);
186                 } else {
187                     return sCharsToGsmTables[0].get(' ', ' ');
188                 }
189             } else {
190                 return GSM_EXTENDED_ESCAPE;
191             }
192         }
193 
194         return ret;
195     }
196 
197     /**
198      * Converts a char to an extended GSM 7 bit table index.
199      * Extended chars should be escaped with GSM_EXTENDED_ESCAPE.
200      * Returns ' ' in GSM alphabet if there's no possible match.
201      * @param c the character to convert
202      * @return the GSM 7 bit extended table index for the specified character
203      */
204     public static int
charToGsmExtended(char c)205     charToGsmExtended(char c) {
206         int ret;
207 
208         ret = sCharsToShiftTables[0].get(c, -1);
209 
210         if (ret == -1) {
211             return sCharsToGsmTables[0].get(' ', ' ');
212         }
213 
214         return ret;
215     }
216 
217     /**
218      * Converts a character in the GSM alphabet into a char.
219      *
220      * If GSM_EXTENDED_ESCAPE is passed, 0xffff is returned. In this case,
221      * the following character in the stream should be decoded with
222      * gsmExtendedToChar().
223      *
224      * If an unmappable value is passed (one greater than 127), ' ' is returned.
225      *
226      * @param gsmChar the GSM 7 bit table index to convert
227      * @return the decoded character
228      */
229     @UnsupportedAppUsage
230     public static char
gsmToChar(int gsmChar)231     gsmToChar(int gsmChar) {
232         if (gsmChar >= 0 && gsmChar < 128) {
233             return sLanguageTables[0].charAt(gsmChar);
234         } else {
235             return ' ';
236         }
237     }
238 
239     /**
240      * Converts a character in the extended GSM alphabet into a char
241      *
242      * if GSM_EXTENDED_ESCAPE is passed, ' ' is returned since no second
243      * extension page has yet been defined (see Note 1 in table 6.2.1.1 of
244      * TS 23.038 v7.00)
245      *
246      * If an unmappable value is passed, the character from the GSM 7 bit
247      * default table will be used (table 6.2.1.1 of TS 23.038).
248      *
249      * @param gsmChar the GSM 7 bit extended table index to convert
250      * @return the decoded character
251      */
252     public static char
gsmExtendedToChar(int gsmChar)253     gsmExtendedToChar(int gsmChar) {
254         if (gsmChar == GSM_EXTENDED_ESCAPE) {
255             return ' ';
256         } else if (gsmChar >= 0 && gsmChar < 128) {
257             char c = sLanguageShiftTables[0].charAt(gsmChar);
258             if (c == ' ') {
259                 return sLanguageTables[0].charAt(gsmChar);
260             } else {
261                 return c;
262             }
263         } else {
264             return ' ';     // out of range
265         }
266     }
267 
268     /**
269      * Converts a String into a byte array containing the 7-bit packed
270      * GSM Alphabet representation of the string. If a header is provided,
271      * this is included in the returned byte array and padded to a septet
272      * boundary. This method is used by OEM code.
273      *
274      * @param data The text string to encode.
275      * @param header Optional header (including length byte) that precedes
276      * the encoded data, padded to septet boundary.
277      * @return Byte array containing header and encoded data.
278      * @throws EncodeException if String is too large to encode
279      * @see #stringToGsm7BitPackedWithHeader(String, byte[], int, int)
280      */
stringToGsm7BitPackedWithHeader(String data, byte[] header)281     public static byte[] stringToGsm7BitPackedWithHeader(String data, byte[] header)
282             throws EncodeException {
283         return stringToGsm7BitPackedWithHeader(data, header, 0, 0);
284     }
285 
286     /**
287      * Converts a String into a byte array containing the 7-bit packed
288      * GSM Alphabet representation of the string. If a header is provided,
289      * this is included in the returned byte array and padded to a septet
290      * boundary.
291      *
292      * Unencodable chars are encoded as spaces
293      *
294      * Byte 0 in the returned byte array is the count of septets used,
295      * including the header and header padding. The returned byte array is
296      * the minimum size required to store the packed septets. The returned
297      * array cannot contain more than 255 septets.
298      *
299      * @param data The text string to encode.
300      * @param header Optional header (including length byte) that precedes
301      * the encoded data, padded to septet boundary.
302      * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
303      * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
304      *     GSM extension table
305      * @return Byte array containing header and encoded data.
306      * @throws EncodeException if String is too large to encode
307      */
308     @UnsupportedAppUsage
stringToGsm7BitPackedWithHeader(String data, byte[] header, int languageTable, int languageShiftTable)309     public static byte[] stringToGsm7BitPackedWithHeader(String data, byte[] header,
310             int languageTable, int languageShiftTable)
311             throws EncodeException {
312         if (header == null || header.length == 0) {
313             return stringToGsm7BitPacked(data, languageTable, languageShiftTable);
314         }
315 
316         int headerBits = (header.length + 1) * 8;
317         int headerSeptets = (headerBits + 6) / 7;
318 
319         byte[] ret = stringToGsm7BitPacked(data, headerSeptets, true, languageTable,
320                 languageShiftTable);
321 
322         // Paste in the header
323         ret[1] = (byte)header.length;
324         System.arraycopy(header, 0, ret, 2, header.length);
325         return ret;
326     }
327 
328     /**
329      * Converts a String into a byte array containing
330      * the 7-bit packed GSM Alphabet representation of the string.
331      *
332      * Unencodable chars are encoded as spaces
333      *
334      * Byte 0 in the returned byte array is the count of septets used
335      * The returned byte array is the minimum size required to store
336      * the packed septets. The returned array cannot contain more than 255
337      * septets.
338      *
339      * @param data the data string to encode
340      * @return the encoded string
341      * @throws EncodeException if String is too large to encode
342      */
343     @UnsupportedAppUsage
stringToGsm7BitPacked(String data)344     public static byte[] stringToGsm7BitPacked(String data)
345             throws EncodeException {
346         return stringToGsm7BitPacked(data, 0, true, 0, 0);
347     }
348 
349     /**
350      * Converts a String into a byte array containing
351      * the 7-bit packed GSM Alphabet representation of the string.
352      *
353      * Unencodable chars are encoded as spaces
354      *
355      * Byte 0 in the returned byte array is the count of septets used
356      * The returned byte array is the minimum size required to store
357      * the packed septets. The returned array cannot contain more than 255
358      * septets.
359      *
360      * @param data the data string to encode
361      * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
362      * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
363      *     GSM extension table
364      * @return the encoded string
365      * @throws EncodeException if String is too large to encode
366      */
stringToGsm7BitPacked(String data, int languageTable, int languageShiftTable)367     public static byte[] stringToGsm7BitPacked(String data, int languageTable,
368             int languageShiftTable)
369             throws EncodeException {
370         return stringToGsm7BitPacked(data, 0, true, languageTable, languageShiftTable);
371     }
372 
373     /**
374      * Converts a String into a byte array containing
375      * the 7-bit packed GSM Alphabet representation of the string.
376      *
377      * Byte 0 in the returned byte array is the count of septets used
378      * The returned byte array is the minimum size required to store
379      * the packed septets. The returned array cannot contain more than 255
380      * septets.
381      *
382      * @param data the text to convert to septets
383      * @param startingSeptetOffset the number of padding septets to put before
384      *  the character data at the beginning of the array
385      * @param throwException If true, throws EncodeException on invalid char.
386      *   If false, replaces unencodable char with GSM alphabet space char.
387      * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
388      * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
389      *     GSM extension table
390      * @return the encoded message
391      *
392      * @throws EncodeException if String is too large to encode or any characters are unencodable
393      */
394     @UnsupportedAppUsage
stringToGsm7BitPacked(String data, int startingSeptetOffset, boolean throwException, int languageTable, int languageShiftTable)395     public static byte[] stringToGsm7BitPacked(String data, int startingSeptetOffset,
396             boolean throwException, int languageTable, int languageShiftTable)
397             throws EncodeException {
398         int dataLen = data.length();
399         int septetCount = countGsmSeptetsUsingTables(data, !throwException,
400                 languageTable, languageShiftTable);
401         if (septetCount == -1) {
402             throw new EncodeException("countGsmSeptetsUsingTables(): unencodable char");
403         }
404         septetCount += startingSeptetOffset;
405         if (septetCount > 255) {
406             throw new EncodeException(
407                     "Payload cannot exceed 255 septets", EncodeException.ERROR_EXCEED_SIZE);
408         }
409         int byteCount = ((septetCount * 7) + 7) / 8;
410         byte[] ret = new byte[byteCount + 1];  // Include space for one byte length prefix.
411         SparseIntArray charToLanguageTable = sCharsToGsmTables[languageTable];
412         SparseIntArray charToShiftTable = sCharsToShiftTables[languageShiftTable];
413         for (int i = 0, septets = startingSeptetOffset, bitOffset = startingSeptetOffset * 7;
414                  i < dataLen && septets < septetCount;
415                  i++, bitOffset += 7) {
416             char c = data.charAt(i);
417             int v = charToLanguageTable.get(c, -1);
418             if (v == -1) {
419                 v = charToShiftTable.get(c, -1);  // Lookup the extended char.
420                 if (v == -1) {
421                     if (throwException) {
422                         throw new EncodeException("stringToGsm7BitPacked(): unencodable char");
423                     } else {
424                         v = charToLanguageTable.get(' ', ' ');   // should return ASCII space
425                     }
426                 } else {
427                     packSmsChar(ret, bitOffset, GSM_EXTENDED_ESCAPE);
428                     bitOffset += 7;
429                     septets++;
430                 }
431             }
432             packSmsChar(ret, bitOffset, v);
433             septets++;
434         }
435         ret[0] = (byte) (septetCount);  // Validated by check above.
436         return ret;
437     }
438 
439     /**
440      * Pack a 7-bit char into its appropriate place in a byte array
441      *
442      * @param packedChars the destination byte array
443      * @param bitOffset the bit offset that the septet should be packed at
444      *                  (septet index * 7)
445      * @param value the 7-bit character to store
446      */
447     @UnsupportedAppUsage
448     private static void
packSmsChar(byte[] packedChars, int bitOffset, int value)449     packSmsChar(byte[] packedChars, int bitOffset, int value) {
450         int byteOffset = bitOffset / 8;
451         int shift = bitOffset % 8;
452 
453         packedChars[++byteOffset] |= value << shift;
454 
455         if (shift > 1) {
456             packedChars[++byteOffset] = (byte)(value >> (8 - shift));
457         }
458     }
459 
460     /**
461      * Convert a GSM alphabet 7 bit packed string (SMS string) into a
462      * {@link java.lang.String}.
463      *
464      * See TS 23.038 6.1.2.1 for SMS Character Packing
465      *
466      * @param pdu the raw data from the pdu
467      * @param offset the byte offset of
468      * @param lengthSeptets string length in septets, not bytes
469      * @return String representation or null on decoding exception
470      */
471     @UnsupportedAppUsage
gsm7BitPackedToString(byte[] pdu, int offset, int lengthSeptets)472     public static String gsm7BitPackedToString(byte[] pdu, int offset,
473             int lengthSeptets) {
474         return gsm7BitPackedToString(pdu, offset, lengthSeptets, 0, 0, 0);
475     }
476 
477     /**
478      * Convert a GSM alphabet 7 bit packed string (SMS string) into a
479      * {@link java.lang.String}.
480      *
481      * See TS 23.038 6.1.2.1 for SMS Character Packing
482      *
483      * @param pdu the raw data from the pdu
484      * @param offset the byte offset of
485      * @param lengthSeptets string length in septets, not bytes
486      * @param numPaddingBits the number of padding bits before the start of the
487      *  string in the first byte
488      * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
489      * @param shiftTable the 7 bit single shift language table, or 0 for the default
490      *     GSM extension table
491      * @return String representation or null on decoding exception
492      */
493     @UnsupportedAppUsage
gsm7BitPackedToString(byte[] pdu, int offset, int lengthSeptets, int numPaddingBits, int languageTable, int shiftTable)494     public static String gsm7BitPackedToString(byte[] pdu, int offset,
495             int lengthSeptets, int numPaddingBits, int languageTable, int shiftTable) {
496         StringBuilder ret = new StringBuilder(lengthSeptets);
497 
498         if (languageTable < 0 || languageTable > sLanguageTables.length) {
499             Log.w(TAG, "unknown language table " + languageTable + ", using default");
500             languageTable = 0;
501         }
502         if (shiftTable < 0 || shiftTable > sLanguageShiftTables.length) {
503             Log.w(TAG, "unknown single shift table " + shiftTable + ", using default");
504             shiftTable = 0;
505         }
506 
507         try {
508             boolean prevCharWasEscape = false;
509             String languageTableToChar = sLanguageTables[languageTable];
510             String shiftTableToChar = sLanguageShiftTables[shiftTable];
511 
512             if (languageTableToChar.isEmpty()) {
513                 Log.w(TAG, "no language table for code " + languageTable + ", using default");
514                 languageTableToChar = sLanguageTables[0];
515             }
516             if (shiftTableToChar.isEmpty()) {
517                 Log.w(TAG, "no single shift table for code " + shiftTable + ", using default");
518                 shiftTableToChar = sLanguageShiftTables[0];
519             }
520 
521             for (int i = 0 ; i < lengthSeptets ; i++) {
522                 int bitOffset = (7 * i) + numPaddingBits;
523 
524                 int byteOffset = bitOffset / 8;
525                 int shift = bitOffset % 8;
526                 int gsmVal;
527 
528                 gsmVal = (0x7f & (pdu[offset + byteOffset] >> shift));
529 
530                 // if it crosses a byte boundary
531                 if (shift > 1) {
532                     // set msb bits to 0
533                     gsmVal &= 0x7f >> (shift - 1);
534 
535                     gsmVal |= 0x7f & (pdu[offset + byteOffset + 1] << (8 - shift));
536                 }
537 
538                 if (prevCharWasEscape) {
539                     if (gsmVal == GSM_EXTENDED_ESCAPE) {
540                         ret.append(' ');    // display ' ' for reserved double escape sequence
541                     } else {
542                         char c = shiftTableToChar.charAt(gsmVal);
543                         if (c == ' ') {
544                             ret.append(languageTableToChar.charAt(gsmVal));
545                         } else {
546                             ret.append(c);
547                         }
548                     }
549                     prevCharWasEscape = false;
550                 } else if (gsmVal == GSM_EXTENDED_ESCAPE) {
551                     prevCharWasEscape = true;
552                 } else {
553                     ret.append(languageTableToChar.charAt(gsmVal));
554                 }
555             }
556         } catch (RuntimeException ex) {
557             Log.e(TAG, "Error GSM 7 bit packed: ", ex);
558             return null;
559         }
560 
561         return ret.toString();
562     }
563 
564 
565     /**
566      * Convert a GSM alphabet string that's stored in 8-bit unpacked
567      * format (as it often appears in SIM records) into a String
568      *
569      * Field may be padded with trailing 0xff's. The decode stops
570      * at the first 0xff encountered.
571      *
572      * @param data the byte array to decode
573      * @param offset array offset for the first character to decode
574      * @param length the number of bytes to decode
575      * @return the decoded string
576      */
577     @UnsupportedAppUsage
578     public static String
gsm8BitUnpackedToString(byte[] data, int offset, int length)579     gsm8BitUnpackedToString(byte[] data, int offset, int length) {
580         return gsm8BitUnpackedToString(data, offset, length, "");
581     }
582 
583     /**
584      * Convert a GSM alphabet string that's stored in 8-bit unpacked
585      * format (as it often appears in SIM records) into a String
586      *
587      * Field may be padded with trailing 0xff's. The decode stops
588      * at the first 0xff encountered.
589      *
590      * Additionally, in some country(ex. Korea), there are non-ASCII or MBCS characters.
591      * If a character set is given, characters in data are treat as MBCS.
592      */
593     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
594     public static String
gsm8BitUnpackedToString(byte[] data, int offset, int length, String characterset)595     gsm8BitUnpackedToString(byte[] data, int offset, int length, String characterset) {
596         boolean isMbcs = false;
597         Charset charset = null;
598         ByteBuffer mbcsBuffer = null;
599 
600         if (!TextUtils.isEmpty(characterset)
601                 && !characterset.equalsIgnoreCase("us-ascii")
602                 && Charset.isSupported(characterset)) {
603             isMbcs = true;
604             charset = Charset.forName(characterset);
605             mbcsBuffer = ByteBuffer.allocate(2);
606         }
607 
608         // Always use GSM 7 bit default alphabet table for this method
609         String languageTableToChar = sLanguageTables[0];
610         String shiftTableToChar = sLanguageShiftTables[0];
611 
612         StringBuilder ret = new StringBuilder(length);
613         boolean prevWasEscape = false;
614         for (int i = offset ; i < offset + length ; i++) {
615             // Never underestimate the pain that can be caused
616             // by signed bytes
617             int c = data[i] & 0xff;
618 
619             if (c == 0xff) {
620                 break;
621             } else if (c == GSM_EXTENDED_ESCAPE) {
622                 if (prevWasEscape) {
623                     // Two escape chars in a row
624                     // We treat this as a space
625                     // See Note 1 in table 6.2.1.1 of TS 23.038 v7.00
626                     ret.append(' ');
627                     prevWasEscape = false;
628                 } else {
629                     prevWasEscape = true;
630                 }
631             } else {
632                 if (prevWasEscape) {
633                     char shiftChar =
634                             c < shiftTableToChar.length() ? shiftTableToChar.charAt(c) : ' ';
635                     if (shiftChar == ' ') {
636                         // display character from main table if not present in shift table
637                         if (c < languageTableToChar.length()) {
638                             ret.append(languageTableToChar.charAt(c));
639                         } else {
640                             ret.append(' ');
641                         }
642                     } else {
643                         ret.append(shiftChar);
644                     }
645                 } else {
646                     if (!isMbcs || c < 0x80 || i + 1 >= offset + length) {
647                         if (c < languageTableToChar.length()) {
648                             ret.append(languageTableToChar.charAt(c));
649                         } else {
650                             ret.append(' ');
651                         }
652                     } else {
653                         // isMbcs must be true. So both mbcsBuffer and charset are initialized.
654                         mbcsBuffer.clear();
655                         mbcsBuffer.put(data, i++, 2);
656                         mbcsBuffer.flip();
657                         ret.append(charset.decode(mbcsBuffer).toString());
658                     }
659                 }
660                 prevWasEscape = false;
661             }
662         }
663 
664         return ret.toString();
665     }
666 
667     /**
668      * Convert a string into an 8-bit unpacked GSM alphabet byte array.
669      * Always uses GSM default 7-bit alphabet and extension table.
670      * @param s the string to encode
671      * @return the 8-bit GSM encoded byte array for the string
672      */
673     @UnsupportedAppUsage
674     public static byte[]
675     stringToGsm8BitPacked(String s) {
676         byte[] ret;
677 
678         int septets = countGsmSeptetsUsingTables(s, true, 0, 0);
679 
680         // Enough for all the septets and the length byte prefix
681         ret = new byte[septets];
682 
683         stringToGsm8BitUnpackedField(s, ret, 0, ret.length);
684 
685         return ret;
686     }
687 
688 
689     /**
690      * Write a String into a GSM 8-bit unpacked field of
691      * Field is padded with 0xff's, string is truncated if necessary
692      *
693      * @param s the string to encode
694      * @param dest the destination byte array
695      * @param offset the starting offset for the encoded string
696      * @param length the maximum number of bytes to write
697      */
698     public static void
699     stringToGsm8BitUnpackedField(String s, byte dest[], int offset, int length) {
700         int outByteIndex = offset;
701         SparseIntArray charToLanguageTable = sCharsToGsmTables[0];
702         SparseIntArray charToShiftTable = sCharsToShiftTables[0];
703 
704         // Septets are stored in byte-aligned octets
705         for (int i = 0, sz = s.length()
706                 ; i < sz && (outByteIndex - offset) < length
707                 ; i++
708         ) {
709             char c = s.charAt(i);
710 
711             int v = charToLanguageTable.get(c, -1);
712 
713             if (v == -1) {
714                 v = charToShiftTable.get(c, -1);
715                 if (v == -1) {
716                     v = charToLanguageTable.get(' ', ' ');  // fall back to ASCII space
717                 } else {
718                     // make sure we can fit an escaped char
719                     if (! (outByteIndex + 1 - offset < length)) {
720                         break;
721                     }
722 
723                     dest[outByteIndex++] = GSM_EXTENDED_ESCAPE;
724                 }
725             }
726 
727             dest[outByteIndex++] = (byte)v;
728         }
729 
730         // pad with 0xff's
731         while((outByteIndex - offset) < length) {
732             dest[outByteIndex++] = (byte)0xff;
733         }
734     }
735 
736     /**
737      * Returns the count of 7-bit GSM alphabet characters
738      * needed to represent this character. Counts unencodable char as 1 septet.
739      * @param c the character to examine
740      * @return the number of septets for this character
741      */
742     public static int
743     countGsmSeptets(char c) {
744         try {
745             return countGsmSeptets(c, false);
746         } catch (EncodeException ex) {
747             // This should never happen.
748             return 0;
749         }
750     }
751 
752     /**
753      * Returns the count of 7-bit GSM alphabet characters
754      * needed to represent this character using the default 7 bit GSM alphabet.
755      * @param c the character to examine
756      * @param throwsException If true, throws EncodeException if unencodable
757      * char. Otherwise, counts invalid char as 1 septet.
758      * @return the number of septets for this character
759      * @throws EncodeException the character can't be encoded and throwsException is true
760      */
761     @UnsupportedAppUsage
762     public static int
763     countGsmSeptets(char c, boolean throwsException) throws EncodeException {
764         if (sCharsToGsmTables[0].get(c, -1) != -1) {
765             return 1;
766         }
767 
768         if (sCharsToShiftTables[0].get(c, -1) != -1) {
769             return 2;
770         }
771 
772         if (throwsException) {
773             throw new EncodeException(c);
774         } else {
775             // count as a space char
776             return 1;
777         }
778     }
779 
780     public static boolean isGsmSeptets(char c) {
781         if (sCharsToGsmTables[0].get(c, -1) != -1) {
782             return true;
783         }
784 
785         if (sCharsToShiftTables[0].get(c, -1) != -1) {
786             return true;
787         }
788 
789         return false;
790     }
791 
792     /**
793      * Returns the count of 7-bit GSM alphabet characters needed
794      * to represent this string, using the specified 7-bit language table
795      * and extension table (0 for GSM default tables).
796      * @param s the Unicode string that will be encoded
797      * @param use7bitOnly allow using space in place of unencodable character if true,
798      *     otherwise, return -1 if any characters are unencodable
799      * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
800      * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
801      *     GSM extension table
802      * @return the septet count for s using the specified language tables, or -1 if any
803      *     characters are unencodable and use7bitOnly is false
804      */
805     public static int countGsmSeptetsUsingTables(CharSequence s, boolean use7bitOnly,
806             int languageTable, int languageShiftTable) {
807         int count = 0;
808         int sz = s.length();
809         SparseIntArray charToLanguageTable = sCharsToGsmTables[languageTable];
810         SparseIntArray charToShiftTable = sCharsToShiftTables[languageShiftTable];
811         for (int i = 0; i < sz; i++) {
812             char c = s.charAt(i);
813             if (c == GSM_EXTENDED_ESCAPE) {
814                 Log.w(TAG, "countGsmSeptets() string contains Escape character, skipping.");
815                 continue;
816             }
817             if (charToLanguageTable.get(c, -1) != -1) {
818                 count++;
819             } else if (charToShiftTable.get(c, -1) != -1) {
820                 count += 2; // escape + shift table index
821             } else if (use7bitOnly) {
822                 count++;    // encode as space
823             } else {
824                 return -1;  // caller must check for this case
825             }
826         }
827         return count;
828     }
829 
830     /**
831      * Returns the count of 7-bit GSM alphabet characters
832      * needed to represent this string, and the language table and
833      * language shift table used to achieve this result.
834      * For multi-part text messages, each message part may use its
835      * own language table encoding as specified in the message header
836      * for that message. However, this method will only return the
837      * optimal encoding for the message as a whole. When the individual
838      * pieces are encoded, a more optimal encoding may be chosen for each
839      * piece of the message, but the message will be split into pieces
840      * based on the encoding chosen for the message as a whole.
841      * @param s the Unicode string that will be encoded
842      * @param use7bitOnly allow using space in place of unencodable character if true,
843      *     using the language table pair with the fewest unencodable characters
844      * @return a TextEncodingDetails object containing the message and
845      *     character counts for the most efficient 7-bit encoding,
846      *     or null if there are no suitable language tables to encode the string.
847      */
848     public static TextEncodingDetails
849     countGsmSeptets(CharSequence s, boolean use7bitOnly) {
850         // Load enabled language tables from config.xml, including any MCC overlays
851         if (!sDisableCountryEncodingCheck) {
852             enableCountrySpecificEncodings();
853         }
854         // fast path for common case where no national language shift tables are enabled
855         if (sEnabledSingleShiftTables.length + sEnabledLockingShiftTables.length == 0) {
856             TextEncodingDetails ted = new TextEncodingDetails();
857             int septets = GsmAlphabet.countGsmSeptetsUsingTables(s, use7bitOnly, 0, 0);
858             if (septets == -1) {
859                 return null;
860             }
861             ted.codeUnitSize = SmsConstants.ENCODING_7BIT;
862             ted.codeUnitCount = septets;
863             if (septets > SmsConstants.MAX_USER_DATA_SEPTETS) {
864                 ted.msgCount = (septets + (SmsConstants.MAX_USER_DATA_SEPTETS_WITH_HEADER - 1)) /
865                         SmsConstants.MAX_USER_DATA_SEPTETS_WITH_HEADER;
866                 ted.codeUnitsRemaining = (ted.msgCount *
867                         SmsConstants.MAX_USER_DATA_SEPTETS_WITH_HEADER) - septets;
868             } else {
869                 ted.msgCount = 1;
870                 ted.codeUnitsRemaining = SmsConstants.MAX_USER_DATA_SEPTETS - septets;
871             }
872             return ted;
873         }
874 
875         int maxSingleShiftCode = sHighestEnabledSingleShiftCode;
876         List<LanguagePairCount> lpcList = new ArrayList<LanguagePairCount>(
877                 sEnabledLockingShiftTables.length + 1);
878 
879         // Always add default GSM 7-bit alphabet table
880         lpcList.add(new LanguagePairCount(0));
881         for (int i : sEnabledLockingShiftTables) {
882             // Avoid adding default table twice in case 0 is in the list of allowed tables
883             if (i != 0 && !sLanguageTables[i].isEmpty()) {
884                 lpcList.add(new LanguagePairCount(i));
885             }
886         }
887 
888         int sz = s.length();
889         // calculate septet count for each valid table / shift table pair
890         for (int i = 0; i < sz && !lpcList.isEmpty(); i++) {
891             char c = s.charAt(i);
892             if (c == GSM_EXTENDED_ESCAPE) {
893                 Log.w(TAG, "countGsmSeptets() string contains Escape character, ignoring!");
894                 continue;
895             }
896             // iterate through enabled locking shift tables
897             for (LanguagePairCount lpc : lpcList) {
898                 int tableIndex = sCharsToGsmTables[lpc.languageCode].get(c, -1);
899                 if (tableIndex == -1) {
900                     // iterate through single shift tables for this locking table
901                     for (int table = 0; table <= maxSingleShiftCode; table++) {
902                         if (lpc.septetCounts[table] != -1) {
903                             int shiftTableIndex = sCharsToShiftTables[table].get(c, -1);
904                             if (shiftTableIndex == -1) {
905                                 if (use7bitOnly) {
906                                     // can't encode char, use space instead
907                                     lpc.septetCounts[table]++;
908                                     lpc.unencodableCounts[table]++;
909                                 } else {
910                                     // can't encode char, remove language pair from list
911                                     lpc.septetCounts[table] = -1;
912                                 }
913                             } else {
914                                 // encode as Escape + index into shift table
915                                 lpc.septetCounts[table] += 2;
916                             }
917                         }
918                     }
919                 } else {
920                     // encode as index into locking shift table for all pairs
921                     for (int table = 0; table <= maxSingleShiftCode; table++) {
922                         if (lpc.septetCounts[table] != -1) {
923                             lpc.septetCounts[table]++;
924                         }
925                     }
926                 }
927             }
928         }
929 
930         // find the least cost encoding (lowest message count and most code units remaining)
931         TextEncodingDetails ted = new TextEncodingDetails();
932         ted.msgCount = Integer.MAX_VALUE;
933         ted.codeUnitSize = SmsConstants.ENCODING_7BIT;
934         int minUnencodableCount = Integer.MAX_VALUE;
935         for (LanguagePairCount lpc : lpcList) {
936             for (int shiftTable = 0; shiftTable <= maxSingleShiftCode; shiftTable++) {
937                 int septets = lpc.septetCounts[shiftTable];
938                 if (septets == -1) {
939                     continue;
940                 }
941                 int udhLength;
942                 if (lpc.languageCode != 0 && shiftTable != 0) {
943                     udhLength = UDH_SEPTET_COST_LENGTH + UDH_SEPTET_COST_TWO_SHIFT_TABLES;
944                 } else if (lpc.languageCode != 0 || shiftTable != 0) {
945                     udhLength = UDH_SEPTET_COST_LENGTH + UDH_SEPTET_COST_ONE_SHIFT_TABLE;
946                 } else {
947                     udhLength = 0;
948                 }
949                 int msgCount;
950                 int septetsRemaining;
951                 if (septets + udhLength > SmsConstants.MAX_USER_DATA_SEPTETS) {
952                     if (udhLength == 0) {
953                         udhLength = UDH_SEPTET_COST_LENGTH;
954                     }
955                     udhLength += UDH_SEPTET_COST_CONCATENATED_MESSAGE;
956                     int septetsPerMessage = SmsConstants.MAX_USER_DATA_SEPTETS - udhLength;
957                     msgCount = (septets + septetsPerMessage - 1) / septetsPerMessage;
958                     septetsRemaining = (msgCount * septetsPerMessage) - septets;
959                 } else {
960                     msgCount = 1;
961                     septetsRemaining = SmsConstants.MAX_USER_DATA_SEPTETS - udhLength - septets;
962                 }
963                 // for 7-bit only mode, use language pair with the least unencodable chars
964                 int unencodableCount = lpc.unencodableCounts[shiftTable];
965                 if (use7bitOnly && unencodableCount > minUnencodableCount) {
966                     continue;
967                 }
968                 if ((use7bitOnly && unencodableCount < minUnencodableCount)
969                         || msgCount < ted.msgCount || (msgCount == ted.msgCount
970                         && septetsRemaining > ted.codeUnitsRemaining)) {
971                     minUnencodableCount = unencodableCount;
972                     ted.msgCount = msgCount;
973                     ted.codeUnitCount = septets;
974                     ted.codeUnitsRemaining = septetsRemaining;
975                     ted.languageTable = lpc.languageCode;
976                     ted.languageShiftTable = shiftTable;
977                 }
978             }
979         }
980 
981         if (ted.msgCount == Integer.MAX_VALUE) {
982             return null;
983         }
984 
985         return ted;
986     }
987 
988     /**
989      * Returns the index into <code>s</code> of the first character
990      * after <code>limit</code> septets have been reached, starting at
991      * index <code>start</code>.  This is used when dividing messages
992      * into units within the SMS message size limit.
993      *
994      * @param s source string
995      * @param start index of where to start counting septets
996      * @param limit maximum septets to include,
997      *   e.g. <code>MAX_USER_DATA_SEPTETS</code>
998      * @param langTable the 7 bit character table to use (0 for default GSM 7-bit alphabet)
999      * @param langShiftTable the 7 bit shift table to use (0 for default GSM extension table)
1000      * @return index of first character that won't fit, or the length
1001      *   of the entire string if everything fits
1002      */
1003     @UnsupportedAppUsage
1004     public static int
1005     findGsmSeptetLimitIndex(String s, int start, int limit, int langTable, int langShiftTable) {
1006         int accumulator = 0;
1007         int size = s.length();
1008 
1009         SparseIntArray charToLangTable = sCharsToGsmTables[langTable];
1010         SparseIntArray charToLangShiftTable = sCharsToShiftTables[langShiftTable];
1011         for (int i = start; i < size; i++) {
1012             int encodedSeptet = charToLangTable.get(s.charAt(i), -1);
1013             if (encodedSeptet == -1) {
1014                 encodedSeptet = charToLangShiftTable.get(s.charAt(i), -1);
1015                 if (encodedSeptet == -1) {
1016                     // char not found, assume we're replacing with space
1017                     accumulator++;
1018                 } else {
1019                     accumulator += 2;  // escape character + shift table index
1020                 }
1021             } else {
1022                 accumulator++;
1023             }
1024             if (accumulator > limit) {
1025                 return i;
1026             }
1027         }
1028         return size;
1029     }
1030 
1031     /**
1032      * Modify the array of enabled national language single shift tables for SMS
1033      * encoding. This is used for unit testing, but could also be used to
1034      * modify the enabled encodings based on the active MCC/MNC, for example.
1035      *
1036      * @param tables the new list of enabled single shift tables
1037      */
1038     public static synchronized void setEnabledSingleShiftTables(int[] tables) {
1039         sEnabledSingleShiftTables = tables;
1040         sDisableCountryEncodingCheck = true;
1041 
1042         if (tables.length > 0) {
1043             sHighestEnabledSingleShiftCode = tables[tables.length - 1];
1044         } else {
1045             sHighestEnabledSingleShiftCode = 0;
1046         }
1047     }
1048 
1049     /**
1050      * Modify the array of enabled national language locking shift tables for SMS
1051      * encoding. This is used for unit testing, but could also be used to
1052      * modify the enabled encodings based on the active MCC/MNC, for example.
1053      *
1054      * @param tables the new list of enabled locking shift tables
1055      */
1056     public static synchronized void setEnabledLockingShiftTables(int[] tables) {
1057         sEnabledLockingShiftTables = tables;
1058         sDisableCountryEncodingCheck = true;
1059     }
1060 
1061     /**
1062      * Return the array of enabled national language single shift tables for SMS
1063      * encoding. This is used for unit testing. The returned array is not a copy, so
1064      * the caller should be careful not to modify it.
1065      *
1066      * @return the list of enabled single shift tables
1067      */
1068     public static synchronized int[] getEnabledSingleShiftTables() {
1069         return sEnabledSingleShiftTables;
1070     }
1071 
1072     /**
1073      * Return the array of enabled national language locking shift tables for SMS
1074      * encoding. This is used for unit testing. The returned array is not a copy, so
1075      * the caller should be careful not to modify it.
1076      *
1077      * @return the list of enabled locking shift tables
1078      */
1079     public static synchronized int[] getEnabledLockingShiftTables() {
1080         return sEnabledLockingShiftTables;
1081     }
1082 
1083     /**
1084      * Enable country-specific language tables from MCC-specific overlays.
1085      * @context the context to use to get the TelephonyManager
1086      */
1087     private static void enableCountrySpecificEncodings() {
1088         Resources r = Resources.getSystem();
1089         // See comments in frameworks/base/core/res/res/values/config.xml for allowed values
1090         sEnabledSingleShiftTables = r.getIntArray(R.array.config_sms_enabled_single_shift_tables);
1091         sEnabledLockingShiftTables = r.getIntArray(R.array.config_sms_enabled_locking_shift_tables);
1092 
1093         if (sEnabledSingleShiftTables.length > 0) {
1094             sHighestEnabledSingleShiftCode =
1095                     sEnabledSingleShiftTables[sEnabledSingleShiftTables.length-1];
1096         } else {
1097             sHighestEnabledSingleShiftCode = 0;
1098         }
1099     }
1100 
1101     /** Reverse mapping from Unicode characters to indexes into language tables. */
1102     @UnsupportedAppUsage
1103     private static final SparseIntArray[] sCharsToGsmTables;
1104 
1105     /** Reverse mapping from Unicode characters to indexes into language shift tables. */
1106     @UnsupportedAppUsage
1107     private static final SparseIntArray[] sCharsToShiftTables;
1108 
1109     /** OEM configured list of enabled national language single shift tables for encoding. */
1110     @UnsupportedAppUsage
1111     private static int[] sEnabledSingleShiftTables;
1112 
1113     /** OEM configured list of enabled national language locking shift tables for encoding. */
1114     @UnsupportedAppUsage
1115     private static int[] sEnabledLockingShiftTables;
1116 
1117     /** Highest language code to include in array of single shift counters. */
1118     @UnsupportedAppUsage
1119     private static int sHighestEnabledSingleShiftCode;
1120 
1121     /** Flag to bypass check for country-specific overlays (for test cases only). */
1122     private static boolean sDisableCountryEncodingCheck = false;
1123 
1124     /**
1125      * Septet counter for a specific locking shift table and all of
1126      * the single shift tables that it can be paired with.
1127      */
1128     private static class LanguagePairCount {
1129         @UnsupportedAppUsage
1130         final int languageCode;
1131         @UnsupportedAppUsage
1132         final int[] septetCounts;
1133         @UnsupportedAppUsage
1134         final int[] unencodableCounts;
1135         @UnsupportedAppUsage
1136         LanguagePairCount(int code) {
1137             this.languageCode = code;
1138             int maxSingleShiftCode = sHighestEnabledSingleShiftCode;
1139             septetCounts = new int[maxSingleShiftCode + 1];
1140             unencodableCounts = new int[maxSingleShiftCode + 1];
1141             // set counters for disabled single shift tables to -1
1142             // (GSM default extension table index 0 is always enabled)
1143             for (int i = 1, tableOffset = 0; i <= maxSingleShiftCode; i++) {
1144                 if (sEnabledSingleShiftTables[tableOffset] == i) {
1145                     tableOffset++;
1146                 } else {
1147                     septetCounts[i] = -1;   // disabled
1148                 }
1149             }
1150             // exclude Turkish locking + Turkish single shift table and
1151             // Portuguese locking + Spanish single shift table (these
1152             // combinations will never be optimal for any input).
1153             if (code == 1 && maxSingleShiftCode >= 1) {
1154                 septetCounts[1] = -1;   // Turkish + Turkish
1155             } else if (code == 3 && maxSingleShiftCode >= 2) {
1156                 septetCounts[2] = -1;   // Portuguese + Spanish
1157             }
1158         }
1159     }
1160 
1161     /**
1162      * GSM default 7 bit alphabet plus national language locking shift character tables.
1163      * Comment lines above strings indicate the lower four bits of the table position.
1164      */
1165     @UnsupportedAppUsage
1166     private static final String[] sLanguageTables = {
1167         /* 3GPP TS 23.038 V9.1.1 section 6.2.1 - GSM 7 bit Default Alphabet
1168          01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0.....1 */
1169         "@\u00a3$\u00a5\u00e8\u00e9\u00f9\u00ec\u00f2\u00c7\n\u00d8\u00f8\r\u00c5\u00e5\u0394_"
1170             // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
1171             + "\u03a6\u0393\u039b\u03a9\u03a0\u03a8\u03a3\u0398\u039e\uffff\u00c6\u00e6\u00df"
1172             // F.....012.34.....56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789A
1173             + "\u00c9 !\"#\u00a4%&'()*+,-./0123456789:;<=>?\u00a1ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1174             // B.....C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
1175             + "\u00c4\u00d6\u00d1\u00dc\u00a7\u00bfabcdefghijklmnopqrstuvwxyz\u00e4\u00f6\u00f1"
1176             // E.....F.....
1177             + "\u00fc\u00e0",
1178 
1179         /* A.3.1 Turkish National Language Locking Shift Table
1180          01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0.....1 */
1181         "@\u00a3$\u00a5\u20ac\u00e9\u00f9\u0131\u00f2\u00c7\n\u011e\u011f\r\u00c5\u00e5\u0394_"
1182             // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
1183             + "\u03a6\u0393\u039b\u03a9\u03a0\u03a8\u03a3\u0398\u039e\uffff\u015e\u015f\u00df"
1184             // F.....012.34.....56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789A
1185             + "\u00c9 !\"#\u00a4%&'()*+,-./0123456789:;<=>?\u0130ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1186             // B.....C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
1187             + "\u00c4\u00d6\u00d1\u00dc\u00a7\u00e7abcdefghijklmnopqrstuvwxyz\u00e4\u00f6\u00f1"
1188             // E.....F.....
1189             + "\u00fc\u00e0",
1190 
1191         /* A.3.2 Void (no locking shift table for Spanish) */
1192         "",
1193 
1194         /* A.3.3 Portuguese National Language Locking Shift Table
1195          01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0.....1 */
1196         "@\u00a3$\u00a5\u00ea\u00e9\u00fa\u00ed\u00f3\u00e7\n\u00d4\u00f4\r\u00c1\u00e1\u0394_"
1197             // 2.....3.....4.....5.....67.8.....9.....AB.....C.....D.....E.....F.....012.34.....
1198             + "\u00aa\u00c7\u00c0\u221e^\\\u20ac\u00d3|\uffff\u00c2\u00e2\u00ca\u00c9 !\"#\u00ba"
1199             // 56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789AB.....C.....D.....E.....
1200             + "%&'()*+,-./0123456789:;<=>?\u00cdABCDEFGHIJKLMNOPQRSTUVWXYZ\u00c3\u00d5\u00da\u00dc"
1201             // F.....0123456789ABCDEF0123456789AB.....C.....DE.....F.....
1202             + "\u00a7~abcdefghijklmnopqrstuvwxyz\u00e3\u00f5`\u00fc\u00e0",
1203 
1204         /* A.3.4 Bengali National Language Locking Shift Table
1205          0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.EF.....0..... */
1206         "\u0981\u0982\u0983\u0985\u0986\u0987\u0988\u0989\u098a\u098b\n\u098c \r \u098f\u0990"
1207             // 123.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....
1208             + "  \u0993\u0994\u0995\u0996\u0997\u0998\u0999\u099a\uffff\u099b\u099c\u099d\u099e"
1209             // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABC
1210             + " !\u099f\u09a0\u09a1\u09a2\u09a3\u09a4)(\u09a5\u09a6,\u09a7.\u09a80123456789:; "
1211             // D.....E.....F0.....1.....2.....3.....4.....56.....789A.....B.....C.....D.....
1212             + "\u09aa\u09ab?\u09ac\u09ad\u09ae\u09af\u09b0 \u09b2   \u09b6\u09b7\u09b8\u09b9"
1213             // E.....F.....0.....1.....2.....3.....4.....5.....6.....789.....A.....BCD.....E.....
1214             + "\u09bc\u09bd\u09be\u09bf\u09c0\u09c1\u09c2\u09c3\u09c4  \u09c7\u09c8  \u09cb\u09cc"
1215             // F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
1216             + "\u09cd\u09ceabcdefghijklmnopqrstuvwxyz\u09d7\u09dc\u09dd\u09f0\u09f1",
1217 
1218         /* A.3.5 Gujarati National Language Locking Shift Table
1219          0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.EF.....0.....*/
1220         "\u0a81\u0a82\u0a83\u0a85\u0a86\u0a87\u0a88\u0a89\u0a8a\u0a8b\n\u0a8c\u0a8d\r \u0a8f\u0a90"
1221             // 1.....23.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
1222             + "\u0a91 \u0a93\u0a94\u0a95\u0a96\u0a97\u0a98\u0a99\u0a9a\uffff\u0a9b\u0a9c\u0a9d"
1223             // F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789AB
1224             + "\u0a9e !\u0a9f\u0aa0\u0aa1\u0aa2\u0aa3\u0aa4)(\u0aa5\u0aa6,\u0aa7.\u0aa80123456789:;"
1225             // CD.....E.....F0.....1.....2.....3.....4.....56.....7.....89.....A.....B.....C.....
1226             + " \u0aaa\u0aab?\u0aac\u0aad\u0aae\u0aaf\u0ab0 \u0ab2\u0ab3 \u0ab5\u0ab6\u0ab7\u0ab8"
1227             // D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89.....A.....
1228             + "\u0ab9\u0abc\u0abd\u0abe\u0abf\u0ac0\u0ac1\u0ac2\u0ac3\u0ac4\u0ac5 \u0ac7\u0ac8"
1229             // B.....CD.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....
1230             + "\u0ac9 \u0acb\u0acc\u0acd\u0ad0abcdefghijklmnopqrstuvwxyz\u0ae0\u0ae1\u0ae2\u0ae3"
1231             // F.....
1232             + "\u0af1",
1233 
1234         /* A.3.6 Hindi National Language Locking Shift Table
1235          0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....*/
1236         "\u0901\u0902\u0903\u0905\u0906\u0907\u0908\u0909\u090a\u090b\n\u090c\u090d\r\u090e\u090f"
1237             // 0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....
1238             + "\u0910\u0911\u0912\u0913\u0914\u0915\u0916\u0917\u0918\u0919\u091a\uffff\u091b\u091c"
1239             // E.....F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....012345
1240             + "\u091d\u091e !\u091f\u0920\u0921\u0922\u0923\u0924)(\u0925\u0926,\u0927.\u0928012345"
1241             // 6789ABC.....D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8.....
1242             + "6789:;\u0929\u092a\u092b?\u092c\u092d\u092e\u092f\u0930\u0931\u0932\u0933\u0934"
1243             // 9.....A.....B.....C.....D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....
1244             + "\u0935\u0936\u0937\u0938\u0939\u093c\u093d\u093e\u093f\u0940\u0941\u0942\u0943\u0944"
1245             // 7.....8.....9.....A.....B.....C.....D.....E.....F.....0.....123456789ABCDEF012345678
1246             + "\u0945\u0946\u0947\u0948\u0949\u094a\u094b\u094c\u094d\u0950abcdefghijklmnopqrstuvwx"
1247             // 9AB.....C.....D.....E.....F.....
1248             + "yz\u0972\u097b\u097c\u097e\u097f",
1249 
1250         /* A.3.7 Kannada National Language Locking Shift Table
1251            NOTE: TS 23.038 V9.1.1 shows code 0x24 as \u0caa, corrected to \u0ca1 (typo)
1252          01.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0.....1 */
1253         " \u0c82\u0c83\u0c85\u0c86\u0c87\u0c88\u0c89\u0c8a\u0c8b\n\u0c8c \r\u0c8e\u0c8f\u0c90 "
1254             // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....
1255             + "\u0c92\u0c93\u0c94\u0c95\u0c96\u0c97\u0c98\u0c99\u0c9a\uffff\u0c9b\u0c9c\u0c9d\u0c9e"
1256             // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABC
1257             + " !\u0c9f\u0ca0\u0ca1\u0ca2\u0ca3\u0ca4)(\u0ca5\u0ca6,\u0ca7.\u0ca80123456789:; "
1258             // D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....89.....A.....B.....
1259             + "\u0caa\u0cab?\u0cac\u0cad\u0cae\u0caf\u0cb0\u0cb1\u0cb2\u0cb3 \u0cb5\u0cb6\u0cb7"
1260             // C.....D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....78.....9.....
1261             + "\u0cb8\u0cb9\u0cbc\u0cbd\u0cbe\u0cbf\u0cc0\u0cc1\u0cc2\u0cc3\u0cc4 \u0cc6\u0cc7"
1262             // A.....BC.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
1263             + "\u0cc8 \u0cca\u0ccb\u0ccc\u0ccd\u0cd5abcdefghijklmnopqrstuvwxyz\u0cd6\u0ce0\u0ce1"
1264             // E.....F.....
1265             + "\u0ce2\u0ce3",
1266 
1267         /* A.3.8 Malayalam National Language Locking Shift Table
1268          01.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0.....1 */
1269         " \u0d02\u0d03\u0d05\u0d06\u0d07\u0d08\u0d09\u0d0a\u0d0b\n\u0d0c \r\u0d0e\u0d0f\u0d10 "
1270             // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....
1271             + "\u0d12\u0d13\u0d14\u0d15\u0d16\u0d17\u0d18\u0d19\u0d1a\uffff\u0d1b\u0d1c\u0d1d\u0d1e"
1272             // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABC
1273             + " !\u0d1f\u0d20\u0d21\u0d22\u0d23\u0d24)(\u0d25\u0d26,\u0d27.\u0d280123456789:; "
1274             // D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....
1275             + "\u0d2a\u0d2b?\u0d2c\u0d2d\u0d2e\u0d2f\u0d30\u0d31\u0d32\u0d33\u0d34\u0d35\u0d36"
1276             // B.....C.....D.....EF.....0.....1.....2.....3.....4.....5.....6.....78.....9.....
1277             + "\u0d37\u0d38\u0d39 \u0d3d\u0d3e\u0d3f\u0d40\u0d41\u0d42\u0d43\u0d44 \u0d46\u0d47"
1278             // A.....BC.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
1279             + "\u0d48 \u0d4a\u0d4b\u0d4c\u0d4d\u0d57abcdefghijklmnopqrstuvwxyz\u0d60\u0d61\u0d62"
1280             // E.....F.....
1281             + "\u0d63\u0d79",
1282 
1283         /* A.3.9 Oriya National Language Locking Shift Table
1284          0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.EF.....0.....12 */
1285         "\u0b01\u0b02\u0b03\u0b05\u0b06\u0b07\u0b08\u0b09\u0b0a\u0b0b\n\u0b0c \r \u0b0f\u0b10  "
1286             // 3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....01
1287             + "\u0b13\u0b14\u0b15\u0b16\u0b17\u0b18\u0b19\u0b1a\uffff\u0b1b\u0b1c\u0b1d\u0b1e !"
1288             // 2.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABCD.....
1289             + "\u0b1f\u0b20\u0b21\u0b22\u0b23\u0b24)(\u0b25\u0b26,\u0b27.\u0b280123456789:; \u0b2a"
1290             // E.....F0.....1.....2.....3.....4.....56.....7.....89.....A.....B.....C.....D.....
1291             + "\u0b2b?\u0b2c\u0b2d\u0b2e\u0b2f\u0b30 \u0b32\u0b33 \u0b35\u0b36\u0b37\u0b38\u0b39"
1292             // E.....F.....0.....1.....2.....3.....4.....5.....6.....789.....A.....BCD.....E.....
1293             + "\u0b3c\u0b3d\u0b3e\u0b3f\u0b40\u0b41\u0b42\u0b43\u0b44  \u0b47\u0b48  \u0b4b\u0b4c"
1294             // F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
1295             + "\u0b4d\u0b56abcdefghijklmnopqrstuvwxyz\u0b57\u0b60\u0b61\u0b62\u0b63",
1296 
1297         /* A.3.10 Punjabi National Language Locking Shift Table
1298          0.....1.....2.....3.....4.....5.....6.....7.....8.....9A.BCD.EF.....0.....123.....4.....*/
1299         "\u0a01\u0a02\u0a03\u0a05\u0a06\u0a07\u0a08\u0a09\u0a0a \n  \r \u0a0f\u0a10  \u0a13\u0a14"
1300             // 5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....012.....3.....
1301             + "\u0a15\u0a16\u0a17\u0a18\u0a19\u0a1a\uffff\u0a1b\u0a1c\u0a1d\u0a1e !\u0a1f\u0a20"
1302             // 4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABCD.....E.....F0.....
1303             + "\u0a21\u0a22\u0a23\u0a24)(\u0a25\u0a26,\u0a27.\u0a280123456789:; \u0a2a\u0a2b?\u0a2c"
1304             // 1.....2.....3.....4.....56.....7.....89.....A.....BC.....D.....E.....F0.....1.....
1305             + "\u0a2d\u0a2e\u0a2f\u0a30 \u0a32\u0a33 \u0a35\u0a36 \u0a38\u0a39\u0a3c \u0a3e\u0a3f"
1306             // 2.....3.....4.....56789.....A.....BCD.....E.....F.....0.....123456789ABCDEF012345678
1307             + "\u0a40\u0a41\u0a42    \u0a47\u0a48  \u0a4b\u0a4c\u0a4d\u0a51abcdefghijklmnopqrstuvwx"
1308             // 9AB.....C.....D.....E.....F.....
1309             + "yz\u0a70\u0a71\u0a72\u0a73\u0a74",
1310 
1311         /* A.3.11 Tamil National Language Locking Shift Table
1312          01.....2.....3.....4.....5.....6.....7.....8.....9A.BCD.E.....F.....0.....12.....3..... */
1313         " \u0b82\u0b83\u0b85\u0b86\u0b87\u0b88\u0b89\u0b8a \n  \r\u0b8e\u0b8f\u0b90 \u0b92\u0b93"
1314             // 4.....5.....6789.....A.....B.....CD.....EF.....012.....3456.....7.....89ABCDEF.....
1315             + "\u0b94\u0b95   \u0b99\u0b9a\uffff \u0b9c \u0b9e !\u0b9f   \u0ba3\u0ba4)(  , .\u0ba8"
1316             // 0123456789ABC.....D.....EF012.....3.....4.....5.....6.....7.....8.....9.....A.....
1317             + "0123456789:;\u0ba9\u0baa ?  \u0bae\u0baf\u0bb0\u0bb1\u0bb2\u0bb3\u0bb4\u0bb5\u0bb6"
1318             // B.....C.....D.....EF0.....1.....2.....3.....4.....5678.....9.....A.....BC.....D.....
1319             + "\u0bb7\u0bb8\u0bb9  \u0bbe\u0bbf\u0bc0\u0bc1\u0bc2   \u0bc6\u0bc7\u0bc8 \u0bca\u0bcb"
1320             // E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
1321             + "\u0bcc\u0bcd\u0bd0abcdefghijklmnopqrstuvwxyz\u0bd7\u0bf0\u0bf1\u0bf2\u0bf9",
1322 
1323         /* A.3.12 Telugu National Language Locking Shift Table
1324          0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0.....*/
1325         "\u0c01\u0c02\u0c03\u0c05\u0c06\u0c07\u0c08\u0c09\u0c0a\u0c0b\n\u0c0c \r\u0c0e\u0c0f\u0c10"
1326             // 12.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
1327             + " \u0c12\u0c13\u0c14\u0c15\u0c16\u0c17\u0c18\u0c19\u0c1a\uffff\u0c1b\u0c1c\u0c1d"
1328             // F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789AB
1329             + "\u0c1e !\u0c1f\u0c20\u0c21\u0c22\u0c23\u0c24)(\u0c25\u0c26,\u0c27.\u0c280123456789:;"
1330             // CD.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....89.....A.....B.....
1331             + " \u0c2a\u0c2b?\u0c2c\u0c2d\u0c2e\u0c2f\u0c30\u0c31\u0c32\u0c33 \u0c35\u0c36\u0c37"
1332             // C.....D.....EF.....0.....1.....2.....3.....4.....5.....6.....78.....9.....A.....B
1333             + "\u0c38\u0c39 \u0c3d\u0c3e\u0c3f\u0c40\u0c41\u0c42\u0c43\u0c44 \u0c46\u0c47\u0c48 "
1334             // C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....
1335             + "\u0c4a\u0c4b\u0c4c\u0c4d\u0c55abcdefghijklmnopqrstuvwxyz\u0c56\u0c60\u0c61\u0c62"
1336             // F.....
1337             + "\u0c63",
1338 
1339         /* A.3.13 Urdu National Language Locking Shift Table
1340          0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....*/
1341         "\u0627\u0622\u0628\u067b\u0680\u067e\u06a6\u062a\u06c2\u067f\n\u0679\u067d\r\u067a\u067c"
1342             // 0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....
1343             + "\u062b\u062c\u0681\u0684\u0683\u0685\u0686\u0687\u062d\u062e\u062f\uffff\u068c\u0688"
1344             // E.....F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....012345
1345             + "\u0689\u068a !\u068f\u068d\u0630\u0631\u0691\u0693)(\u0699\u0632,\u0696.\u0698012345"
1346             // 6789ABC.....D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8.....
1347             + "6789:;\u069a\u0633\u0634?\u0635\u0636\u0637\u0638\u0639\u0641\u0642\u06a9\u06aa"
1348             // 9.....A.....B.....C.....D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....
1349             + "\u06ab\u06af\u06b3\u06b1\u0644\u0645\u0646\u06ba\u06bb\u06bc\u0648\u06c4\u06d5\u06c1"
1350             // 7.....8.....9.....A.....B.....C.....D.....E.....F.....0.....123456789ABCDEF012345678
1351             + "\u06be\u0621\u06cc\u06d0\u06d2\u064d\u0650\u064f\u0657\u0654abcdefghijklmnopqrstuvwx"
1352             // 9AB.....C.....D.....E.....F.....
1353             + "yz\u0655\u0651\u0653\u0656\u0670"
1354     };
1355 
1356     /**
1357      * GSM default extension table plus national language single shift character tables.
1358      */
1359     @UnsupportedAppUsage
1360     private static final String[] sLanguageShiftTables = new String[]{
1361         /* 6.2.1.1 GSM 7 bit Default Alphabet Extension Table
1362          0123456789A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF0123456789ABCDEF */
1363         "          \u000c         ^                   {}     \\            [~] |               "
1364             // 0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1365             + "                     \u20ac                          ",
1366 
1367         /* A.2.1 Turkish National Language Single Shift Table
1368          0123456789A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF01234567.....8 */
1369         "          \u000c         ^                   {}     \\            [~] |      \u011e "
1370             // 9.....ABCDEF0123.....456789ABCDEF0123.....45.....67.....89.....ABCDEF0123.....
1371             + "\u0130         \u015e               \u00e7 \u20ac \u011f \u0131         \u015f"
1372             // 456789ABCDEF
1373             + "            ",
1374 
1375         /* A.2.2 Spanish National Language Single Shift Table
1376          0123456789.....A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF01.....23 */
1377         "         \u00e7\u000c         ^                   {}     \\            [~] |\u00c1  "
1378             // 456789.....ABCDEF.....012345.....6789ABCDEF01.....2345.....6789.....ABCDEF.....012
1379             + "     \u00cd     \u00d3     \u00da           \u00e1   \u20ac   \u00ed     \u00f3   "
1380             // 345.....6789ABCDEF
1381             + "  \u00fa          ",
1382 
1383         /* A.2.3 Portuguese National Language Single Shift Table
1384          012345.....6789.....A.....B.....C.....DE.....F.....012.....3.....45.....6.....7.....8....*/
1385         "     \u00ea   \u00e7\u000c\u00d4\u00f4 \u00c1\u00e1  \u03a6\u0393^\u03a9\u03a0\u03a8\u03a3"
1386             // 9.....ABCDEF.....0123456789ABCDEF.0123456789ABCDEF01.....23456789.....ABCDE
1387             + "\u0398     \u00ca        {}     \\            [~] |\u00c0       \u00cd     "
1388             // F.....012345.....6789AB.....C.....DEF01.....2345.....6789.....ABCDEF.....01234
1389             + "\u00d3     \u00da     \u00c3\u00d5    \u00c2   \u20ac   \u00ed     \u00f3     "
1390             // 5.....6789AB.....C.....DEF.....
1391             + "\u00fa     \u00e3\u00f5  \u00e2",
1392 
1393         /* A.2.4 Bengali National Language Single Shift Table
1394          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1395         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u09e6\u09e7 \u09e8\u09e9"
1396             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1397             + "\u09ea\u09eb\u09ec\u09ed\u09ee\u09ef\u09df\u09e0\u09e1\u09e2{}\u09e3\u09f2\u09f3"
1398             // D.....E.....F.0.....1.....2.....3.....4.....56789ABCDEF0123456789ABCDEF
1399             + "\u09f4\u09f5\\\u09f6\u09f7\u09f8\u09f9\u09fa       [~] |ABCDEFGHIJKLMNO"
1400             // 0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1401             + "PQRSTUVWXYZ          \u20ac                          ",
1402 
1403         /* A.2.5 Gujarati National Language Single Shift Table
1404          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1405         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0ae6\u0ae7"
1406             // E.....F.....0.....1.....2.....3.....4.....5.....6789ABCDEF.0123456789ABCDEF
1407             + "\u0ae8\u0ae9\u0aea\u0aeb\u0aec\u0aed\u0aee\u0aef  {}     \\            [~] "
1408             // 0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1409             + "|ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
1410 
1411         /* A.2.6 Hindi National Language Single Shift Table
1412          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1413         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0966\u0967"
1414             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1415             + "\u0968\u0969\u096a\u096b\u096c\u096d\u096e\u096f\u0951\u0952{}\u0953\u0954\u0958"
1416             // D.....E.....F.0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....
1417             + "\u0959\u095a\\\u095b\u095c\u095d\u095e\u095f\u0960\u0961\u0962\u0963\u0970\u0971"
1418             // BCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1419             + " [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
1420 
1421         /* A.2.7 Kannada National Language Single Shift Table
1422          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1423         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0ce6\u0ce7"
1424             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....BCDEF.01234567
1425             + "\u0ce8\u0ce9\u0cea\u0ceb\u0cec\u0ced\u0cee\u0cef\u0cde\u0cf1{}\u0cf2    \\        "
1426             // 89ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1427             + "    [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
1428 
1429         /* A.2.8 Malayalam National Language Single Shift Table
1430          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1431         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0d66\u0d67"
1432             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1433             + "\u0d68\u0d69\u0d6a\u0d6b\u0d6c\u0d6d\u0d6e\u0d6f\u0d70\u0d71{}\u0d72\u0d73\u0d74"
1434             // D.....E.....F.0.....1.....2.....3.....4.....56789ABCDEF0123456789ABCDEF0123456789A
1435             + "\u0d75\u0d7a\\\u0d7b\u0d7c\u0d7d\u0d7e\u0d7f       [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1436             // BCDEF012345.....6789ABCDEF0123456789ABCDEF
1437             + "          \u20ac                          ",
1438 
1439         /* A.2.9 Oriya National Language Single Shift Table
1440          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1441         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0b66\u0b67"
1442             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....DE
1443             + "\u0b68\u0b69\u0b6a\u0b6b\u0b6c\u0b6d\u0b6e\u0b6f\u0b5c\u0b5d{}\u0b5f\u0b70\u0b71  "
1444             // F.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789A
1445             + "\\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                     "
1446             // BCDEF
1447             + "     ",
1448 
1449         /* A.2.10 Punjabi National Language Single Shift Table
1450          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1451         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0a66\u0a67"
1452             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1453             + "\u0a68\u0a69\u0a6a\u0a6b\u0a6c\u0a6d\u0a6e\u0a6f\u0a59\u0a5a{}\u0a5b\u0a5c\u0a5e"
1454             // D.....EF.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF01
1455             + "\u0a75 \\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac            "
1456             // 23456789ABCDEF
1457             + "              ",
1458 
1459         /* A.2.11 Tamil National Language Single Shift Table
1460            NOTE: TS 23.038 V9.1.1 shows code 0x24 as \u0bef, corrected to \u0bee (typo)
1461          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1462         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0be6\u0be7"
1463             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1464             + "\u0be8\u0be9\u0bea\u0beb\u0bec\u0bed\u0bee\u0bef\u0bf3\u0bf4{}\u0bf5\u0bf6\u0bf7"
1465             // D.....E.....F.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABC
1466             + "\u0bf8\u0bfa\\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac       "
1467             // DEF0123456789ABCDEF
1468             + "                   ",
1469 
1470         /* A.2.12 Telugu National Language Single Shift Table
1471            NOTE: TS 23.038 V9.1.1 shows code 0x22-0x23 as \u06cc\u06cd, corrected to \u0c6c\u0c6d
1472          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789ABC.....D.....E.....F..... */
1473         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*   \u0c66\u0c67\u0c68\u0c69"
1474             // 0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....D.....E.....F.
1475             + "\u0c6a\u0c6b\u0c6c\u0c6d\u0c6e\u0c6f\u0c58\u0c59{}\u0c78\u0c79\u0c7a\u0c7b\u0c7c\\"
1476             // 0.....1.....2.....3456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCD
1477             + "\u0c7d\u0c7e\u0c7f         [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac        "
1478             // EF0123456789ABCDEF
1479             + "                  ",
1480 
1481         /* A.2.13 Urdu National Language Single Shift Table
1482          01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1483         "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0600\u0601 \u06f0\u06f1"
1484             // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1485             + "\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9\u060c\u060d{}\u060e\u060f\u0610"
1486             // D.....E.....F.0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....
1487             + "\u0611\u0612\\\u0613\u0614\u061b\u061f\u0640\u0652\u0658\u066b\u066c\u0672\u0673"
1488             // B.....CDEF.....0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1489             + "\u06cd[~]\u06d4|ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          "
1490     };
1491 
1492     static {
1493         enableCountrySpecificEncodings();
1494         int numTables = sLanguageTables.length;
1495         int numShiftTables = sLanguageShiftTables.length;
1496         if (numTables != numShiftTables) {
1497             Log.e(TAG, "Error: language tables array length " + numTables +
1498                     " != shift tables array length " + numShiftTables);
1499         }
1500 
1501         sCharsToGsmTables = new SparseIntArray[numTables];
1502         for (int i = 0; i < numTables; i++) {
1503             String table = sLanguageTables[i];
1504 
1505             int tableLen = table.length();
1506             if (tableLen != 0 && tableLen != 128) {
1507                 Log.e(TAG, "Error: language tables index " + i +
1508                         " length " + tableLen + " (expected 128 or 0)");
1509             }
1510 
1511             SparseIntArray charToGsmTable = new SparseIntArray(tableLen);
1512             sCharsToGsmTables[i] = charToGsmTable;
1513             for (int j = 0; j < tableLen; j++) {
1514                 char c = table.charAt(j);
1515                 charToGsmTable.put(c, j);
1516             }
1517         }
1518 
1519         sCharsToShiftTables = new SparseIntArray[numShiftTables];
1520         for (int i = 0; i < numShiftTables; i++) {
1521             String shiftTable = sLanguageShiftTables[i];
1522 
1523             int shiftTableLen = shiftTable.length();
1524             if (shiftTableLen != 0 && shiftTableLen != 128) {
1525                 Log.e(TAG, "Error: language shift tables index " + i +
1526                         " length " + shiftTableLen + " (expected 128 or 0)");
1527             }
1528 
1529             SparseIntArray charToShiftTable = new SparseIntArray(shiftTableLen);
1530             sCharsToShiftTables[i] = charToShiftTable;
1531             for (int j = 0; j < shiftTableLen; j++) {
1532                 char c = shiftTable.charAt(j);
1533                 if (c != ' ') {
1534                     charToShiftTable.put(c, j);
1535                 }
1536             }
1537         }
1538     }
1539 }
1540