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