Home | History | Annotate | Download | only in charset
      1 /*
      2  *******************************************************************************
      3  * Copyright (C) 2006-2015, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  *******************************************************************************
      6  */
      7 
      8 package com.ibm.icu.charset;
      9 
     10 import java.io.IOException;
     11 import java.nio.ByteBuffer;
     12 import java.nio.CharBuffer;
     13 import java.nio.IntBuffer;
     14 
     15 import com.ibm.icu.charset.CharsetMBCS.MBCSHeader;
     16 import com.ibm.icu.charset.CharsetMBCS.MBCSToUFallback;
     17 import com.ibm.icu.charset.CharsetMBCS.UConverterMBCSTable;
     18 import com.ibm.icu.impl.ICUBinary;
     19 import com.ibm.icu.impl.InvalidFormatException;
     20 
     21 /**
     22  * ucnvmbcs.h
     23  *
     24  * ICU conversion (.cnv) data file structure, following the usual UDataInfo
     25  * header.
     26  *
     27  * Format version: 6.2
     28  *
     29  * struct UConverterStaticData -- struct containing the converter name, IBM CCSID,
     30  *                                min/max bytes per character, etc.
     31  *                                see ucnv_bld.h
     32  *
     33  * --------------------
     34  *
     35  * The static data is followed by conversionType-specific data structures.
     36  * At the moment, there are only variations of MBCS converters. They all have
     37  * the same toUnicode structures, while the fromUnicode structures for SBCS
     38  * differ from those for other MBCS-style converters.
     39  *
     40  * _MBCSHeader.version 4.2 adds an optional conversion extension data structure.
     41  * If it is present, then an ICU version reading header versions 4.0 or 4.1
     42  * will be able to use the base table and ignore the extension.
     43  *
     44  * The unicodeMask in the static data is part of the base table data structure.
     45  * Especially, the UCNV_HAS_SUPPLEMENTARY flag determines the length of the
     46  * fromUnicode stage 1 array.
     47  * The static data unicodeMask refers only to the base table's properties if
     48  * a base table is included.
     49  * In an extension-only file, the static data unicodeMask is 0.
     50  * The extension data indexes have a separate field with the unicodeMask flags.
     51  *
     52  * MBCS-style data structure following the static data.
     53  * Offsets are counted in bytes from the beginning of the MBCS header structure.
     54  * Details about usage in comments in ucnvmbcs.c.
     55  *
     56  * struct _MBCSHeader (see the definition in this header file below)
     57  * contains 32-bit fields as follows:
     58  * 8 values:
     59  *  0   uint8_t[4]  MBCS version in UVersionInfo format (currently 4.2.0.0)
     60  *  1   uint32_t    countStates
     61  *  2   uint32_t    countToUFallbacks
     62  *  3   uint32_t    offsetToUCodeUnits
     63  *  4   uint32_t    offsetFromUTable
     64  *  5   uint32_t    offsetFromUBytes
     65  *  6   uint32_t    flags, bits:
     66  *                      31.. 8 offsetExtension -- _MBCSHeader.version 4.2 (ICU 2.8) and higher
     67  *                                                0 for older versions and if
     68  *                                                there is not extension structure
     69  *                       7.. 0 outputType
     70  *  7   uint32_t    fromUBytesLength -- _MBCSHeader.version 4.1 (ICU 2.4) and higher
     71  *                  counts bytes in fromUBytes[]
     72  *
     73  * if(outputType==MBCS_OUTPUT_EXT_ONLY) {
     74  *     -- base table name for extension-only table
     75  *     char baseTableName[variable]; -- with NUL plus padding for 4-alignment
     76  *
     77  *     -- all _MBCSHeader fields except for version and flags are 0
     78  * } else {
     79  *     -- normal base table with optional extension
     80  *
     81  *     int32_t stateTable[countStates][256];
     82  *
     83  *     struct _MBCSToUFallback { (fallbacks are sorted by offset)
     84  *         uint32_t offset;
     85  *         UChar32 codePoint;
     86  *     } toUFallbacks[countToUFallbacks];
     87  *
     88  *     uint16_t unicodeCodeUnits[(offsetFromUTable-offsetToUCodeUnits)/2];
     89  *                  (padded to an even number of units)
     90  *
     91  *     -- stage 1 tables
     92  *     if(staticData.unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
     93  *         -- stage 1 table for all of Unicode
     94  *         uint16_t fromUTable[0x440]; (32-bit-aligned)
     95  *     } else {
     96  *         -- BMP-only tables have a smaller stage 1 table
     97  *         uint16_t fromUTable[0x40]; (32-bit-aligned)
     98  *     }
     99  *
    100  *     -- stage 2 tables
    101  *        length determined by top of stage 1 and bottom of stage 3 tables
    102  *     if(outputType==MBCS_OUTPUT_1) {
    103  *         -- SBCS: pure indexes
    104  *         uint16_t stage 2 indexes[?];
    105  *     } else {
    106  *         -- DBCS, MBCS, EBCDIC_STATEFUL, ...: roundtrip flags and indexes
    107  *         uint32_t stage 2 flags and indexes[?];
    108  *     }
    109  *
    110  *     -- stage 3 tables with byte results
    111  *     if(outputType==MBCS_OUTPUT_1) {
    112  *         -- SBCS: each 16-bit result contains flags and the result byte, see ucnvmbcs.c
    113  *         uint16_t fromUBytes[fromUBytesLength/2];
    114  *     } else {
    115  *         -- DBCS, MBCS, EBCDIC_STATEFUL, ... 2/3/4 bytes result, see ucnvmbcs.c
    116  *         uint8_t fromUBytes[fromUBytesLength]; or
    117  *         uint16_t fromUBytes[fromUBytesLength/2]; or
    118  *         uint32_t fromUBytes[fromUBytesLength/4];
    119  *     }
    120  * }
    121  *
    122  * -- extension table, details see ucnv_ext.h
    123  * int32_t indexes[>=32]; ...
    124  */
    125 /*
    126  * ucnv_ext.h
    127  *
    128  * See icuhtml/design/conversion/conversion_extensions.html
    129  *
    130  * Conversion extensions serve two purposes:
    131  * 1. They support m:n mappings.
    132  * 2. They support extension-only conversion files that are used together
    133  *    with the regular conversion data in base files.
    134  *
    135  * A base file may contain an extension table (explicitly requested or
    136  * implicitly generated for m:n mappings), but its extension table is not
    137  * used when an extension-only file is used.
    138  *
    139  * It is an error if a base file contains any regular (not extension) mapping
    140  * from the same sequence as a mapping in the extension file
    141  * because the base mapping would hide the extension mapping.
    142  *
    143  *
    144  * Data for conversion extensions:
    145  *
    146  * One set of data structures per conversion direction (to/from Unicode).
    147  * The data structures are sorted by input units to allow for binary search.
    148  * Input sequences of more than one unit are handled like contraction tables
    149  * in collation:
    150  * The lookup value of a unit points to another table that is to be searched
    151  * for the next unit, recursively.
    152  *
    153  * For conversion from Unicode, the initial code point is looked up in
    154  * a 3-stage trie for speed,
    155  * with an additional table of unique results to save space.
    156  *
    157  * Long output strings are stored in separate arrays, with length and index
    158  * in the lookup tables.
    159  * Output results also include a flag distinguishing roundtrip from
    160  * (reverse) fallback mappings.
    161  *
    162  * Input Unicode strings must not begin or end with unpaired surrogates
    163  * to avoid problems with matches on parts of surrogate pairs.
    164  *
    165  * Mappings from multiple characters (code points or codepage state
    166  * table sequences) must be searched preferring the longest match.
    167  * For this to work and be efficient, the variable-width table must contain
    168  * all mappings that contain prefixes of the multiple characters.
    169  * If an extension table is built on top of a base table in another file
    170  * and a base table entry is a prefix of a multi-character mapping, then
    171  * this is an error.
    172  *
    173  *
    174  * Implementation note:
    175  *
    176  * Currently, the parser and several checks in the code limit the number
    177  * of UChars or bytes in a mapping to
    178  * UCNV_EXT_MAX_UCHARS and UCNV_EXT_MAX_BYTES, respectively,
    179  * which are output value limits in the data structure.
    180  *
    181  * For input, this is not strictly necessary - it is a hard limit only for the
    182  * buffers in UConverter that are used to store partial matches.
    183  *
    184  * Input sequences could otherwise be arbitrarily long if partial matches
    185  * need not be stored (i.e., if a sequence does not span several buffers with too
    186  * many units before the last buffer), although then results would differ
    187  * depending on whether partial matches exceed the limits or not,
    188  * which depends on the pattern of buffer sizes.
    189  *
    190  *
    191  * Data structure:
    192  *
    193  * int32_t indexes[>=32];
    194  *
    195  *   Array of indexes and lengths etc. The length of the array is at least 32.
    196  *   The actual length is stored in indexes[0] to be forward compatible.
    197  *
    198  *   Each index to another array is the number of bytes from indexes[].
    199  *   Each length of an array is the number of array base units in that array.
    200  *
    201  *   Some of the structures may not be present, in which case their indexes
    202  *   and lengths are 0.
    203  *
    204  *   Usage of indexes[i]:
    205  *   [0]  length of indexes[]
    206  *
    207  *   // to Unicode table
    208  *   [1]  index of toUTable[] (array of uint32_t)
    209  *   [2]  length of toUTable[]
    210  *   [3]  index of toUUChars[] (array of UChar)
    211  *   [4]  length of toUUChars[]
    212  *
    213  *   // from Unicode table, not for the initial code point
    214  *   [5]  index of fromUTableUChars[] (array of UChar)
    215  *   [6]  index of fromUTableValues[] (array of uint32_t)
    216  *   [7]  length of fromUTableUChars[] and fromUTableValues[]
    217  *   [8]  index of fromUBytes[] (array of char)
    218  *   [9]  length of fromUBytes[]
    219  *
    220  *   // from Unicode trie for initial-code point lookup
    221  *   [10] index of fromUStage12[] (combined array of uint16_t for stages 1 & 2)
    222  *   [11] length of stage 1 portion of fromUStage12[]
    223  *   [12] length of fromUStage12[]
    224  *   [13] index of fromUStage3[] (array of uint16_t indexes into fromUStage3b[])
    225  *   [14] length of fromUStage3[]
    226  *   [15] index of fromUStage3b[] (array of uint32_t like fromUTableValues[])
    227  *   [16] length of fromUStage3b[]
    228  *
    229  *   [17] Bit field containing numbers of bytes:
    230  *        31..24 reserved, 0
    231  *        23..16 maximum input bytes
    232  *        15.. 8 maximum output bytes
    233  *         7.. 0 maximum bytes per UChar
    234  *
    235  *   [18] Bit field containing numbers of UChars:
    236  *        31..24 reserved, 0
    237  *        23..16 maximum input UChars
    238  *        15.. 8 maximum output UChars
    239  *         7.. 0 maximum UChars per byte
    240  *
    241  *   [19] Bit field containing flags:
    242  *               (extension table unicodeMask)
    243  *         1     UCNV_HAS_SURROGATES flag for the extension table
    244  *         0     UCNV_HAS_SUPPLEMENTARY flag for the extension table
    245  *
    246  *   [20]..[30] reserved, 0
    247  *   [31] number of bytes for the entire extension structure
    248  *   [>31] reserved; there are indexes[0] indexes
    249  *
    250  *
    251  * uint32_t toUTable[];
    252  *
    253  *   Array of byte/value pairs for lookups for toUnicode conversion.
    254  *   The array is partitioned into sections like collation contraction tables.
    255  *   Each section contains one word with the number of following words and
    256  *   a default value for when the lookup in this section yields no match.
    257  *
    258  *   A section is sorted in ascending order of input bytes,
    259  *   allowing for fast linear or binary searches.
    260  *   The builder may store entries for a contiguous range of byte values
    261  *   (compare difference between the first and last one with count),
    262  *   which then allows for direct array access.
    263  *   The builder should always do this for the initial table section.
    264  *
    265  *   Entries may have 0 values, see below.
    266  *   No two entries in a section have the same byte values.
    267  *
    268  *   Each uint32_t contains an input byte value in bits 31..24 and the
    269  *   corresponding lookup value in bits 23..0.
    270  *   Interpret the value as follows:
    271  *     if(value==0) {
    272  *       no match, see below
    273  *     } else if(value<0x1f0000) {
    274  *       partial match - use value as index to the next toUTable section
    275  *       and match the next unit; (value indexes toUTable[value])
    276  *     } else {
    277  *       if(bit 23 set) {
    278  *         roundtrip;
    279  *       } else {
    280  *         fallback;
    281  *       }
    282  *       unset value bit 23;
    283  *       if(value<=0x2fffff) {
    284  *         (value-0x1f0000) is a code point; (BMP: value<=0x1fffff)
    285  *       } else {
    286  *         bits 17..0 (value&0x3ffff) is an index to
    287  *           the result UChars in toUUChars[]; (0 indexes toUUChars[0])
    288  *         length of the result=((value>>18)-12); (length=0..19)
    289  *       }
    290  *     }
    291  *
    292  *   The first word in a section contains the number of following words in the
    293  *   input byte position (bits 31..24, number=1..0xff).
    294  *   The value of the initial word is used when the current byte is not found
    295  *   in this section.
    296  *   If the value is not 0, then it represents a result as above.
    297  *   If the value is 0, then the search has to return a shorter match with an
    298  *   earlier default value as the result, or result in "unmappable" even for the
    299  *   initial bytes.
    300  *   If the value is 0 for the initial toUTable entry, then the initial byte
    301  *   does not start any mapping input.
    302  *
    303  *
    304  * UChar toUUChars[];
    305  *
    306  *   Contains toUnicode mapping results, stored as sequences of UChars.
    307  *   Indexes and lengths stored in the toUTable[].
    308  *
    309  *
    310  * UChar fromUTableUChars[];
    311  * uint32_t fromUTableValues[];
    312  *
    313  *   The fromUTable is split into two arrays, but works otherwise much like
    314  *   the toUTable. The array is partitioned into sections like collation
    315  *   contraction tables and toUTable.
    316  *   A row in the table consists of same-index entries in fromUTableUChars[]
    317  *   and fromUTableValues[].
    318  *
    319  *   Interpret a value as follows:
    320  *     if(value==0) {
    321  *       no match, see below
    322  *     } else if(value<=0xffffff) { (bits 31..24 are 0)
    323  *       partial match - use value as index to the next fromUTable section
    324  *       and match the next unit; (value indexes fromUTable[value])
    325  *     } else {
    326  *       if(value==0x80000001) {
    327  *         return no mapping, but request for <subchar1>;
    328  *       }
    329  *       if(bit 31 set) {
    330  *         roundtrip;
    331  *       } else {
    332  *         fallback;
    333  *       }
    334  *       // bits 30..29 reserved, 0
    335  *       length=(value>>24)&0x1f; (bits 28..24)
    336  *       if(length==1..3) {
    337  *         bits 23..0 contain 1..3 bytes, padded with 00s on the left;
    338  *       } else {
    339  *         bits 23..0 (value&0xffffff) is an index to
    340  *           the result bytes in fromUBytes[]; (0 indexes fromUBytes[0])
    341  *       }
    342  *     }
    343  *
    344  *   The first pair in a section contains the number of following pairs in the
    345  *   UChar position (16 bits, number=1..0xffff).
    346  *   The value of the initial pair is used when the current UChar is not found
    347  *   in this section.
    348  *   If the value is not 0, then it represents a result as above.
    349  *   If the value is 0, then the search has to return a shorter match with an
    350  *   earlier default value as the result, or result in "unmappable" even for the
    351  *   initial UChars.
    352  *
    353  *   If the from Unicode trie is present, then the from Unicode search tables
    354  *   are not used for initial code points.
    355  *   In this case, the first entries (index 0) in the tables are not used
    356  *   (reserved, set to 0) because a value of 0 is used in trie results
    357  *   to indicate no mapping.
    358  *
    359  *
    360  * uint16_t fromUStage12[];
    361  *
    362  *   Stages 1 & 2 of a trie that maps an initial code point.
    363  *   Indexes in stage 1 are all offset by the length of stage 1 so that the
    364  *   same array pointer can be used for both stages.
    365  *   If (c>>10)>=(length of stage 1) then c does not start any mapping.
    366  *   Same bit distribution as for regular conversion tries.
    367  *
    368  *
    369  * uint16_t fromUStage3[];
    370  * uint32_t fromUStage3b[];
    371  *
    372  *   Stage 3 of the trie. The first array simply contains indexes to the second,
    373  *   which contains words in the same format as fromUTableValues[].
    374  *   Use a stage 3 granularity of 4, which allows for 256k stage 3 entries,
    375  *   and 16-bit entries in stage 3 allow for 64k stage 3b entries.
    376  *   The stage 3 granularity means that the stage 2 entry needs to be left-shifted.
    377  *
    378  *   Two arrays are used because it is expected that more than half of the stage 3
    379  *   entries will be zero. The 16-bit index stage 3 array saves space even
    380  *   considering storing a total of 6 bytes per non-zero entry in both arrays
    381  *   together.
    382  *   Using a stage 3 granularity of >1 diminishes the compactability in that stage
    383  *   but provides a larger effective addressing space in stage 2.
    384  *   All but the final result stage use 16-bit entries to save space.
    385  *
    386  *   fromUStage3b[] contains a zero for "no mapping" at its index 0,
    387  *   and may contain UCNV_EXT_FROM_U_SUBCHAR1 at index 1 for "<subchar1> SUB mapping"
    388  *   (i.e., "no mapping" with preference for <subchar1> rather than <subchar>),
    389  *   and all other items are unique non-zero results.
    390  *
    391  *   The default value of a fromUTableValues[] section that is referenced
    392  *   _directly_ from a fromUStage3b[] item may also be UCNV_EXT_FROM_U_SUBCHAR1,
    393  *   but this value must not occur anywhere else in fromUTableValues[]
    394  *   because "no mapping" is always a property of a single code point,
    395  *   never of multiple.
    396  *
    397  *
    398  * char fromUBytes[];
    399  *
    400  *   Contains fromUnicode mapping results, stored as sequences of chars.
    401  *   Indexes and lengths stored in the fromUTableValues[].
    402  */
    403 
    404 final class UConverterDataReader {
    405     //private final static boolean debug = ICUDebug.enabled("UConverterDataReader");
    406 
    407     private static final class IsAcceptable implements ICUBinary.Authenticate {
    408         // @Override when we switch to Java 6
    409         public boolean isDataVersionAcceptable(byte formatVersion[]) {
    410             return formatVersion[0] == 6;
    411         }
    412     }
    413     private static final IsAcceptable IS_ACCEPTABLE = new IsAcceptable();
    414 
    415     /*
    416      *  UConverterDataReader(UConverterDataReader r)
    417         {
    418             byteBuffer = ICUBinary.getByteBufferFromInputStreamAndCloseStream(r.byteBuffer);
    419             unicodeVersion = r.unicodeVersion;
    420         }
    421         */
    422     /** The buffer position after the static data. */
    423     private int posAfterStaticData;
    424 
    425    /**
    426     * <p>Protected constructor.</p>
    427     * @param bytes ICU conversion data file
    428     * @exception IOException throw if data file fails authentication
    429     */
    430     protected UConverterDataReader(ByteBuffer bytes)
    431                                         throws IOException{
    432         //if(debug) System.out.println("Bytes in buffer " + bytes.remaining());
    433 
    434         byteBuffer = bytes;
    435         /*unicodeVersion = */ICUBinary.readHeader(byteBuffer, DATA_FORMAT_ID, IS_ACCEPTABLE);
    436 
    437         //if(debug) System.out.println("Bytes left in byteBuffer " + byteBuffer.remaining());
    438     }
    439 
    440     // protected methods -------------------------------------------------
    441 
    442     protected void readStaticData(UConverterStaticData sd) throws IOException
    443     {
    444         sd.structSize = byteBuffer.getInt();
    445         byte[] name = new byte[UConverterConstants.MAX_CONVERTER_NAME_LENGTH];
    446         byteBuffer.get(name);
    447         sd.name = new String(name, "US-ASCII");
    448         sd.codepage = byteBuffer.getInt();
    449         sd.platform = byteBuffer.get();
    450         sd.conversionType = byteBuffer.get();
    451         sd.minBytesPerChar = byteBuffer.get();
    452         sd.maxBytesPerChar = byteBuffer.get();
    453         byteBuffer.get(sd.subChar);
    454         sd.subCharLen = byteBuffer.get();
    455         sd.hasToUnicodeFallback = byteBuffer.get();
    456         sd.hasFromUnicodeFallback = byteBuffer.get();
    457         sd.unicodeMask = (short)(byteBuffer.get() & 0xff);
    458         sd.subChar1 = byteBuffer.get();
    459         byteBuffer.get(sd.reserved);
    460         posAfterStaticData = byteBuffer.position();
    461     }
    462 
    463     int bytesReadAfterStaticData() {
    464         return byteBuffer.position() - posAfterStaticData;
    465     }
    466 
    467     protected void readMBCSHeader(CharsetMBCS.MBCSHeader h) throws IOException
    468     {
    469         byteBuffer.get(h.version);
    470         h.countStates = byteBuffer.getInt();
    471         h.countToUFallbacks = byteBuffer.getInt();
    472         h.offsetToUCodeUnits = byteBuffer.getInt();
    473         h.offsetFromUTable = byteBuffer.getInt();
    474         h.offsetFromUBytes = byteBuffer.getInt();
    475         h.flags = byteBuffer.getInt();
    476         h.fromUBytesLength = byteBuffer.getInt();
    477         if (h.version[0] == 5 && h.version[1] >= 3) {
    478             h.options = byteBuffer.getInt();
    479             if ((h.options & CharsetMBCS.MBCS_OPT_NO_FROM_U) != 0) {
    480                 h.fullStage2Length = byteBuffer.getInt();
    481             }
    482         }
    483     }
    484 
    485     protected void readMBCSTable(MBCSHeader header, UConverterMBCSTable mbcsTable) throws IOException
    486     {
    487         IntBuffer intBuffer = byteBuffer.asIntBuffer();
    488         mbcsTable.countStates = (byte) header.countStates;
    489         mbcsTable.stateTable = new int[header.countStates][256];
    490         int i;
    491         for(i = 0; i < header.countStates; ++i) {
    492             intBuffer.get(mbcsTable.stateTable[i]);
    493         }
    494 
    495         mbcsTable.countToUFallbacks = header.countToUFallbacks;
    496         mbcsTable.toUFallbacks = new MBCSToUFallback[header.countToUFallbacks];
    497         for(i = 0; i < header.countToUFallbacks; ++i) {
    498             int offset = intBuffer.get();
    499             int codePoint = intBuffer.get();
    500             mbcsTable.toUFallbacks[i] = new MBCSToUFallback(offset, codePoint);
    501         }
    502         // Skip as many bytes as we have read from the IntBuffer.
    503         int length = intBuffer.position() * 4;
    504         ICUBinary.skipBytes(byteBuffer, length);
    505 
    506         // Consider leaving some large arrays as CharBuffer/IntBuffer rather than
    507         // reading them into Java arrays, to reduce initialization time and memory usage,
    508         // at the cost of some performance.
    509         // For example: unicodeCodeUnits, fromUnicodeTable, fromUnicodeInts.
    510         // Take care not to modify the buffer contents for swaplfnl.
    511         CharBuffer charBuffer = byteBuffer.asCharBuffer();
    512         length = header.offsetFromUTable - header.offsetToUCodeUnits;
    513         assert (length & 1) == 0;
    514         mbcsTable.unicodeCodeUnits = new char[length / 2];
    515         charBuffer.get(mbcsTable.unicodeCodeUnits);
    516         // Skip as many bytes as we have read from the CharBuffer.
    517         ICUBinary.skipBytes(byteBuffer, length);
    518 
    519         length = header.offsetFromUBytes - header.offsetFromUTable;
    520         assert (length & 1) == 0;
    521         int fromUTableCharsLength;
    522         if (mbcsTable.outputType == CharsetMBCS.MBCS_OUTPUT_1) {
    523             // single-byte table stage1 + stage2
    524             fromUTableCharsLength = length / 2;
    525         } else if (mbcsTable.hasSupplementary()) {
    526             // stage1 for Unicode limit 0x110000 >> 10
    527             fromUTableCharsLength = 0x440;
    528         } else {
    529             // stage1 for BMP limit 0x10000 >> 10
    530             fromUTableCharsLength = 0x40;
    531         }
    532         mbcsTable.fromUnicodeTable = new char[fromUTableCharsLength];
    533         charBuffer.get(mbcsTable.fromUnicodeTable);
    534         if (mbcsTable.outputType != CharsetMBCS.MBCS_OUTPUT_1) {
    535             // Read both stage1 and stage2 together into an int[] array.
    536             // Keeping the short stage1 in the array avoids offsetting at runtime.
    537             // The stage1 part of this array will not be used.
    538             assert (length & 3) == 0;
    539             mbcsTable.fromUnicodeTableInts = new int[length / 4];
    540             byteBuffer.asIntBuffer().get(mbcsTable.fromUnicodeTableInts);
    541         }
    542         // Skip as many bytes as are in stage1 + stage2.
    543         ICUBinary.skipBytes(byteBuffer, length);
    544 
    545         mbcsTable.fromUBytesLength = header.fromUBytesLength;
    546         boolean noFromU = ((header.options & CharsetMBCS.MBCS_OPT_NO_FROM_U) != 0);
    547         if (!noFromU) {
    548             switch (mbcsTable.outputType) {
    549             case CharsetMBCS.MBCS_OUTPUT_1:
    550             case CharsetMBCS.MBCS_OUTPUT_2:
    551             case CharsetMBCS.MBCS_OUTPUT_2_SISO:
    552             case CharsetMBCS.MBCS_OUTPUT_3_EUC:
    553                 mbcsTable.fromUnicodeChars = ICUBinary.getChars(
    554                         byteBuffer, header.fromUBytesLength / 2, 0);
    555                 break;
    556             case CharsetMBCS.MBCS_OUTPUT_3:
    557             case CharsetMBCS.MBCS_OUTPUT_4_EUC:
    558                 mbcsTable.fromUnicodeBytes = new byte[header.fromUBytesLength];
    559                 byteBuffer.get(mbcsTable.fromUnicodeBytes);
    560                 break;
    561             case CharsetMBCS.MBCS_OUTPUT_4:
    562                 mbcsTable.fromUnicodeInts = ICUBinary.getInts(
    563                         byteBuffer, header.fromUBytesLength / 4, 0);
    564                 break;
    565             default:
    566                 // Cannot occur, caller checked already.
    567                 assert false;
    568             }
    569         } else {
    570             // Optional utf8Friendly mbcsIndex -- _MBCSHeader.version 4.3 (ICU 3.8) and higher.
    571             // Needed for reconstituting omitted data.
    572             mbcsTable.mbcsIndex = byteBuffer.asCharBuffer();
    573         }
    574     }
    575 
    576     protected String readBaseTableName() throws IOException
    577     {
    578         char c;
    579         StringBuilder name = new StringBuilder();
    580         while((c = (char)byteBuffer.get()) !=  0){
    581             name.append(c);
    582         }
    583         return name.toString();
    584     }
    585 
    586     //protected int[] readExtIndexes(int skip) throws IOException
    587     protected ByteBuffer readExtIndexes(int skip) throws IOException, InvalidFormatException
    588     {
    589         ICUBinary.skipBytes(byteBuffer, skip);
    590         ByteBuffer b = ICUBinary.sliceWithOrder(byteBuffer);
    591         int lengthOfIndexes = b.getInt(0);
    592         if (lengthOfIndexes < 32) {
    593             throw new InvalidFormatException();
    594         }
    595         int numBytesExtensionStructure = b.getInt(31 * 4);
    596         b.limit(numBytesExtensionStructure);
    597         ICUBinary.skipBytes(byteBuffer, numBytesExtensionStructure);
    598         return b;
    599     }
    600 
    601     /**
    602      * Data formatVersion 6.1 and higher has a unicodeMask.
    603      */
    604     boolean dataFormatHasUnicodeMask() {
    605         int formatVersion0 = byteBuffer.get(16) & 0xff;
    606         return formatVersion0 > 6 || (formatVersion0 == 6 && byteBuffer.get(17) != 0);
    607     }
    608 
    609     // private data members -------------------------------------------------
    610 
    611     /**
    612     * ICU data file input stream
    613     */
    614     private ByteBuffer byteBuffer;
    615 
    616 //    private VersionInfo unicodeVersion;
    617 
    618     /**
    619     * File format version that this class understands.
    620     * No guarantees are made if a older version is used
    621     * see store.c of gennorm for more information and values
    622     */
    623     // DATA_FORMAT_ID_ values taken from icu4c isCnvAcceptable (ucnv_bld.c)
    624     private static final int DATA_FORMAT_ID = 0x636e7674; // dataFormat="cnvt"
    625 }
    626