]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/charset/src/com/ibm/icu/charset/CharsetLMBCS.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / charset / src / com / ibm / icu / charset / CharsetLMBCS.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2008-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.charset;\r
8 \r
9 import java.nio.ByteBuffer;\r
10 import java.nio.CharBuffer;\r
11 import java.nio.IntBuffer;\r
12 import java.nio.charset.CharsetDecoder;\r
13 import java.nio.charset.CharsetEncoder;\r
14 import java.nio.charset.CoderResult;\r
15 \r
16 import com.ibm.icu.charset.CharsetMBCS.CharsetDecoderMBCS;\r
17 import com.ibm.icu.charset.CharsetMBCS.CharsetEncoderMBCS;\r
18 import com.ibm.icu.text.UnicodeSet;\r
19 import com.ibm.icu.util.ULocale;\r
20 /**\r
21  * @author Michael Ow\r
22  *\r
23  */\r
24 \r
25 /*\r
26  * LMBCS\r
27  * \r
28  * (Lotus Multi-Byte Character Set)\r
29  * \r
30  * LMBS was invented in the alte 1980's and is primarily used in Lotus Notes\r
31  * databases and in Lotus 1-2-3 files. Programmers who work with the APIs\r
32  * into these products will sometimes need to deal with strings in this format.\r
33  * \r
34  * The code in this file provides an implementation for an ICU converter of\r
35  * LMBCS to and from Unicode.\r
36  * \r
37  * Since the LMBCS character set is only sparsely documented in existing\r
38  * printed or online material, we have added extensive annotation to this\r
39  * file to serve as a guide to understanding LMBCS.\r
40  * \r
41  * LMBCS was originally designed with these four sometimes-competing design goals:\r
42  * -Provide encodings for characters in 12 existing national standards\r
43  *  (plus a few other characters)\r
44  * -Minimal memory footprint\r
45  * -Maximal speed of conversion into the existing national character sets\r
46  * -No need to track a changing state as you interpret a string.\r
47  * \r
48  * All of the national character sets LMBCS was trying to encode are 'ANSI'\r
49  * based, in that the bytes from 0x20 - 0x7F are almost exactly the\r
50  * same common Latin unaccented characters and symbols in all character sets.\r
51  * \r
52  * So, in order to help meet the speed & memory design goals, the common ANSI\r
53  * bytes from 0x20-0x7F are represented by the same single-byte values in LMBCS.\r
54  */\r
55 class CharsetLMBCS extends CharsetICU {\r
56     /*\r
57      * The general LMBCS code unit is from 1-3 bytes. We can describe the 3 bytes as\r
58      * follows:\r
59      * [G] D1 [D2]\r
60      * That is, a sometimes-optional 'group' byte, followed by 1 and sometimes 2\r
61      * data bytes. The maximum size of a LMBCS character is 3 bytes:\r
62      */\r
63     private static final short ULMBCS_CHARSIZE_MAX = 3;\r
64     /*\r
65      * The single-byte values from 0x20 to 0x7F are examples of single D1 bytes.\r
66      * We often have to figure out if byte values are below or above this, so we\r
67      * use the ANSI nomenclature 'C0' and 'C1' to refer to the range of control\r
68      * characters just above & below the common lower-ANSI range.\r
69      */\r
70     private static final short ULMBCS_C0END    = 0x1F;\r
71     private static final short ULMBCS_C1START  = 0x80;\r
72     /*\r
73      * Most of the values less than 0x20 are reserved in LMBCS to announce\r
74      * which national character standard is being used for the 'D' bytes.\r
75      * In the comments we show that common name and the IBM character-set ID\r
76      * for these character-set announcers:\r
77      */\r
78     private static final short ULMBCS_GRP_L1   = 0x01; /* Latin-1      :ibm-850    */\r
79     private static final short ULMBCS_GRP_GR   = 0x02; /* Greek        :ibm-851    */\r
80     private static final short ULMBCS_GRP_HE   = 0x03; /* Hebrew       :ibm-1255   */\r
81     private static final short ULMBCS_GRP_AR   = 0x04; /* Arabic       :ibm-1256   */\r
82     private static final short ULMBCS_GRP_RU   = 0x05; /* Cyrillic     :ibm-1251   */\r
83     private static final short ULMBCS_GRP_L2   = 0x06; /* Latin-2      :ibm-852    */\r
84     private static final short ULMBCS_GRP_TR   = 0x08; /* Turkish      :ibm-1254   */\r
85     private static final short ULMBCS_GRP_TH   = 0x0B; /* Thai         :ibm-874    */\r
86     private static final short ULMBCS_GRP_JA   = 0x10; /* Japanese     :ibm-943    */\r
87     private static final short ULMBCS_GRP_KO   = 0x11; /* Korean       :ibm-1261   */\r
88     private static final short ULMBCS_GRP_TW   = 0x12; /* Chinese SC   :ibm-950    */\r
89     private static final short ULMBCS_GRP_CN   = 0x13; /* Chinese TC   :ibm-1386   */\r
90     /* \r
91      * So, the beginnning of understanding LMBCS is that IF the first byte of a LMBCS\r
92      * character is one of those 12 values, you can interpret the remaining bytes of \r
93      * that character as coming from one of those character sets. Since the lower\r
94      * ANSI bytes already are represented in singl bytes, using one of the chracter\r
95      * set announcers is used to announce a character that starts with a byte of\r
96      * 0x80 or greater.\r
97      * \r
98      * The character sets are arranged so that the single byte sets all appear\r
99      * before the multi-byte character sets. When we need to tell whether a\r
100      * group byte is for a single byte char set or not we use this definition:\r
101      */\r
102     private static final short ULMBCS_DOUBLEOPTGROUP_START = 0x10;\r
103     /*\r
104      * However, to fully understand LMBCS, you must also understand a series of\r
105      * exceptions & optimizations made in service of the design goals.\r
106      * \r
107      * First, those of you who are character set mavens may have noticed that\r
108      * the 'double-byte' character sets are actually multi-byte chracter sets\r
109      * that can have 1 or two bytes, even in upper-ascii range. To force\r
110      * each group byte to introduce a fixed-width encoding (to make it faster to\r
111      * count characters), we use a convention of doubling up on the group byte\r
112      * to introduce any single-byte character > 0x80 in an otherwise double-byte\r
113      * character set. So, for example, the LMBCS sequence x10 x10 xAE is the\r
114      * same as '0xAE' in the Japanese code page 943.\r
115      * \r
116      * Next, you will notice that the list of group bytes has some gaps.\r
117      * These are used in various ways.\r
118      * \r
119      * We reserve a few special single byte values for common control\r
120      * characters. These are in the same place as their ANSI equivalents for speed.\r
121      */\r
122     private static final short ULMBCS_HT   = 0x09; /* Fixed control-char - Horizontal Tab */\r
123     private static final short ULMBCS_LF   = 0x0A; /* Fixed control-char - Line Feed */\r
124     private static final short ULMBCS_CR   = 0x0D; /* Fixed control-char - Carriage Return */\r
125     /*\r
126      * Then, 1-2-3 reserved a special single-byte character to put at the\r
127      * beginning of internal 'system' range names:\r
128      */\r
129     private static final short ULMBCS_123SYSTEMRANGE   = 0x19;\r
130     /*\r
131      * Then we needed a place to put all the other ansi control characters\r
132      * that must be moved to different values because LMBCS reserves those\r
133      * values for other purposes. To represent the control characters, we start\r
134      * with a first byte of 0x0F & add the control character value as the\r
135      * second byte.\r
136      */\r
137     private static final short ULMBCS_GRP_CTRL = 0x0F;\r
138     /*\r
139      * For the C0 controls (less than 0x20), we add 0x20 to preserve the\r
140      * useful doctrine that any byte less than 0x20 in a LMBCS char must be\r
141      * the first byte of a character:\r
142      */\r
143     private static final short ULMBCS_CTRLOFFSET   = 0x20;\r
144     /*\r
145      * Where to put the characters that aren't part of any of the 12 national\r
146      * character sets? The first thing that was done, in the earlier years of\r
147      * LMBCS, was to use up the spaces of the form\r
148      *  [G] D1,\r
149      * where 'G' was one of the single-byte character groups, and\r
150      * D1 was less than 0x80. These sequences are gathered together\r
151      * into a Lotus-invented doublebyte character set to represent a\r
152      * lot of stray values. Internally, in this implementation, we track this\r
153      * as group '0', as a place to tuck this exceptions list.\r
154      */\r
155     private static final short ULMBCS_GRP_EXCEPT   = 0x00;\r
156     /*\r
157      * Finally, as the durability and usefulness of UNICODE became clear,\r
158      * LOTUS added a new group 0x14 to hold Unicode values not otherwise\r
159      * represented in LMBCS:\r
160      */\r
161     private static final short ULMBCS_GRP_UNICODE  = 0x14;\r
162     /*\r
163      * The two bytes appearing after a 0x14 are interpreted as UTF-16 BE\r
164      * (Big Endian) characters. The exception comes when UTF16 \r
165      * representation would have a zero as the second byte. In that case,\r
166      * 'F6' is used in its place, and the bytes are swapped. (This prevents\r
167      * LMBCS from encoding any Unicode values of the form U+F6xx, but that's OK:\r
168      * 0xF6xx is in the middle of the Private Use Area.)\r
169      */\r
170     private static char ULMBCS_UNICOMPATZERO    = 0x00F6;\r
171     /*\r
172      * It is also useful in our code to have a constant for the size of\r
173      * a LMBCS char that holds a literal Unicode value.\r
174      */\r
175     private static final short ULMBCS_UNICODE_SIZE = 3;\r
176     /*\r
177      * To squish the LMBCS representation down even further, and to make\r
178      * translations even faster, sometimes the optimization group byte can be dropped\r
179      * from a LMBCS character. This is decided on a process-by-process basis. The\r
180      * group byte that is dropped is called the 'optimization group.'\r
181      * \r
182      * For Notes, the optimization group is always 0x1.\r
183      */\r
184     //private static final short ULMBCS_DEFAULTOPTGROUP  = 0x01;\r
185     /* For 1-2-3 files, the optimization group is stored in the header of the 1-2-3\r
186      * file.\r
187      * In any case, when using ICU, you either pass in the\r
188      * optimization group as part of the name of the converter (LMBCS-1, LMBCS-2,\r
189      * etc.). Using plain 'LMBCS' as the name of the converter will give you\r
190      * LMBCS-1.\r
191      */\r
192     \r
193     /* Implementation strategy */\r
194     /* \r
195      * Because of the extensive use of other character sets, the LMBCS converter\r
196      * keeps a mapping between optimization groups and IBM character sets, so that\r
197      * ICU converters can be created and used as needed.\r
198      * \r
199      * As you can see, even though any byte below 0x20 could be an optimization\r
200      * byte, only those at 0x13 or below can map to an actual converter. To limit\r
201      * some loops and searches, we define a value for that last group converter:\r
202      */\r
203     private static final short ULMBCS_GRP_LAST = 0x13; /* last LMBCS group that has a converter */\r
204     \r
205     private static final String[] OptGroupByteToCPName = {\r
206         /* 0x0000 */ "lmb-excp", /* internal home for the LOTUS exceptions list */\r
207         /* 0x0001 */ "ibm-850",\r
208         /* 0x0002 */ "ibm-851",\r
209         /* 0x0003 */ "windows-1255",\r
210         /* 0x0004 */ "windows-1256",\r
211         /* 0x0005 */ "windows-1251",\r
212         /* 0x0006 */ "ibm-852",\r
213         /* 0x0007 */ null,      /* Unused */\r
214         /* 0x0008 */ "windows-1254",\r
215         /* 0x0009 */ null,      /* Control char HT */\r
216         /* 0x000A */ null,      /* Control char LF */\r
217         /* 0x000B */ "windows-874",\r
218         /* 0x000C */ null,      /* Unused */\r
219         /* 0x000D */ null,      /* Control char CR */\r
220         /* 0x000E */ null,      /* Unused */\r
221         /* 0x000F */ null,      /* Control chars: 0x0F20 + C0/C1 character: algorithmic */\r
222         /* 0x0010 */ "windows-932",\r
223         /* 0x0011 */ "windows-949",\r
224         /* 0x0012 */ "windows-950",\r
225         /* 0x0013 */ "windows-936",\r
226         /* The rest are null, including the 0x0014 Unicode compatibility region\r
227          * and 0x0019, the 1-2-3 system range control char */\r
228         /* 0x0014 */ null \r
229     };\r
230     \r
231     /* That's approximately all the data that's needed for translating\r
232      * LMBCS to Unicode.\r
233      * \r
234      * However, to translate Unicode to LMBCS, we need some more support.\r
235      * \r
236      * That's because there are often more than one possible mappings from a Unicode\r
237      * code point back into LMBCS. The first thing we do is look up into a table\r
238      * to figure out if there are more than one possible mapplings. This table,\r
239      * arranged by Unicode values (including ranges) either lists which group\r
240      * to use, or says that it could go into one or more of the SBCS sets, or\r
241      * into one or more of the DBCS sets. (If the character exists in both DBCS &\r
242      * SBCS, the table will place it in the SBCS sets, to make the LMBCS code point\r
243      * length as small as possible. Here's the two special markers we use to indicate\r
244      * ambiguous mappings:\r
245      */\r
246     private static final short ULMBCS_AMBIGUOUS_SBCS   = 0x80; /* could fit in more than one\r
247                                                            LMBCS sbcs native encoding\r
248                                                            (example: most accented latin) */\r
249     private static final short ULMBCS_AMBIGUOUS_MBCS   = 0x81; /* could fit in more than one\r
250                                                            LMBCS mbcs native encoding\r
251                                                            (example: Unihan) */\r
252     private static final short ULMBCS_AMBIGUOUS_ALL    = 0x82;\r
253     \r
254     /* And here's a simple way to see if a group falls in an appropriate range */\r
255     private boolean ULMBCS_AMBIGUOUS_MATCH(short agroup, short xgroup) {\r
256         return (((agroup == ULMBCS_AMBIGUOUS_SBCS) &&\r
257                  (xgroup < ULMBCS_DOUBLEOPTGROUP_START)) ||\r
258                 ((agroup == ULMBCS_AMBIGUOUS_MBCS) &&\r
259                  (xgroup >= ULMBCS_DOUBLEOPTGROUP_START)) ||\r
260                  ((agroup) == ULMBCS_AMBIGUOUS_ALL));\r
261     }\r
262     \r
263     /* The table & some code to use it: */\r
264     private static class _UniLMBCSGrpMap {\r
265         int uniStartRange;\r
266         int uniEndRange;\r
267         short GrpType;\r
268         _UniLMBCSGrpMap(int uniStartRange, int uniEndRange, short GrpType) {\r
269             this.uniStartRange  = uniStartRange;\r
270             this.uniEndRange    = uniEndRange;\r
271             this.GrpType        = GrpType;\r
272         }\r
273     }\r
274     \r
275     private static final _UniLMBCSGrpMap[] UniLMBCSGrpMap = {\r
276         new _UniLMBCSGrpMap(0x0001, 0x001F, ULMBCS_GRP_CTRL),\r
277         new _UniLMBCSGrpMap(0x0080, 0x009F, ULMBCS_GRP_CTRL),\r
278         new _UniLMBCSGrpMap(0x00A0, 0x00A6, ULMBCS_AMBIGUOUS_SBCS),\r
279         new _UniLMBCSGrpMap(0x00A7, 0x00A8, ULMBCS_AMBIGUOUS_ALL),\r
280         new _UniLMBCSGrpMap(0x00A9, 0x00AF, ULMBCS_AMBIGUOUS_SBCS),\r
281         new _UniLMBCSGrpMap(0x00B0, 0x00B1, ULMBCS_AMBIGUOUS_ALL),\r
282         new _UniLMBCSGrpMap(0x00B2, 0x00B3, ULMBCS_AMBIGUOUS_SBCS),\r
283         new _UniLMBCSGrpMap(0x00B4, 0x00B4, ULMBCS_AMBIGUOUS_ALL),\r
284         new _UniLMBCSGrpMap(0x00B5, 0x00B5, ULMBCS_AMBIGUOUS_SBCS),\r
285         new _UniLMBCSGrpMap(0x00B6, 0x00B6, ULMBCS_AMBIGUOUS_ALL),\r
286         new _UniLMBCSGrpMap(0x00B7, 0x00D6, ULMBCS_AMBIGUOUS_SBCS),\r
287         new _UniLMBCSGrpMap(0x00D7, 0x00D7, ULMBCS_AMBIGUOUS_ALL),\r
288         new _UniLMBCSGrpMap(0x00D8, 0x00F6, ULMBCS_AMBIGUOUS_SBCS),\r
289         new _UniLMBCSGrpMap(0x00F7, 0x00F7, ULMBCS_AMBIGUOUS_ALL),\r
290         new _UniLMBCSGrpMap(0x00F8, 0x01CD, ULMBCS_AMBIGUOUS_SBCS),\r
291         new _UniLMBCSGrpMap(0x01CE, 0x01CE, ULMBCS_GRP_TW ),\r
292         new _UniLMBCSGrpMap(0x01CF, 0x02B9, ULMBCS_AMBIGUOUS_SBCS),\r
293         new _UniLMBCSGrpMap(0x02BA, 0x02BA, ULMBCS_GRP_CN),\r
294         new _UniLMBCSGrpMap(0x02BC, 0x02C8, ULMBCS_AMBIGUOUS_SBCS),\r
295         new _UniLMBCSGrpMap(0x02C9, 0x02D0, ULMBCS_AMBIGUOUS_MBCS),\r
296         new _UniLMBCSGrpMap(0x02D8, 0x02DD, ULMBCS_AMBIGUOUS_SBCS),\r
297         new _UniLMBCSGrpMap(0x0384, 0x0390, ULMBCS_AMBIGUOUS_SBCS),\r
298         new _UniLMBCSGrpMap(0x0391, 0x03A9, ULMBCS_AMBIGUOUS_ALL),\r
299         new _UniLMBCSGrpMap(0x03AA, 0x03B0, ULMBCS_AMBIGUOUS_SBCS),\r
300         new _UniLMBCSGrpMap(0x03B1, 0x03C9, ULMBCS_AMBIGUOUS_ALL),\r
301         new _UniLMBCSGrpMap(0x03CA, 0x03CE, ULMBCS_AMBIGUOUS_SBCS),\r
302         new _UniLMBCSGrpMap(0x0400, 0x0400, ULMBCS_GRP_RU),\r
303         new _UniLMBCSGrpMap(0x0401, 0x0401, ULMBCS_AMBIGUOUS_ALL),\r
304         new _UniLMBCSGrpMap(0x0402, 0x040F, ULMBCS_GRP_RU),\r
305         new _UniLMBCSGrpMap(0x0410, 0x0431, ULMBCS_AMBIGUOUS_ALL),\r
306         new _UniLMBCSGrpMap(0x0432, 0x044E, ULMBCS_GRP_RU),\r
307         new _UniLMBCSGrpMap(0x044F, 0x044F, ULMBCS_AMBIGUOUS_ALL),\r
308         new _UniLMBCSGrpMap(0x0450, 0x0491, ULMBCS_GRP_RU),\r
309         new _UniLMBCSGrpMap(0x05B0, 0x05F2, ULMBCS_GRP_HE),\r
310         new _UniLMBCSGrpMap(0x060C, 0x06AF, ULMBCS_GRP_AR),\r
311         new _UniLMBCSGrpMap(0x0E01, 0x0E5B, ULMBCS_GRP_TH),\r
312         new _UniLMBCSGrpMap(0x200C, 0x200F, ULMBCS_AMBIGUOUS_SBCS),\r
313         new _UniLMBCSGrpMap(0x2010, 0x2010, ULMBCS_AMBIGUOUS_MBCS),\r
314         new _UniLMBCSGrpMap(0x2013, 0x2014, ULMBCS_AMBIGUOUS_SBCS),\r
315         new _UniLMBCSGrpMap(0x2015, 0x2015, ULMBCS_AMBIGUOUS_MBCS),\r
316         new _UniLMBCSGrpMap(0x2016, 0x2016, ULMBCS_AMBIGUOUS_MBCS),\r
317         new _UniLMBCSGrpMap(0x2017, 0x2017, ULMBCS_AMBIGUOUS_SBCS),\r
318         new _UniLMBCSGrpMap(0x2018, 0x2019, ULMBCS_AMBIGUOUS_ALL),\r
319         new _UniLMBCSGrpMap(0x201A, 0x201B, ULMBCS_AMBIGUOUS_SBCS),\r
320         new _UniLMBCSGrpMap(0x201C, 0x201D, ULMBCS_AMBIGUOUS_ALL),\r
321         new _UniLMBCSGrpMap(0x201E, 0x201F, ULMBCS_AMBIGUOUS_SBCS),\r
322         new _UniLMBCSGrpMap(0x2020, 0x2021, ULMBCS_AMBIGUOUS_ALL),\r
323         new _UniLMBCSGrpMap(0x2022, 0x2024, ULMBCS_AMBIGUOUS_SBCS),\r
324         new _UniLMBCSGrpMap(0x2025, 0x2025, ULMBCS_AMBIGUOUS_MBCS),\r
325         new _UniLMBCSGrpMap(0x2026, 0x2026, ULMBCS_AMBIGUOUS_ALL),\r
326         new _UniLMBCSGrpMap(0x2027, 0x2027, ULMBCS_GRP_TW),\r
327         new _UniLMBCSGrpMap(0x2030, 0x2030, ULMBCS_AMBIGUOUS_ALL),\r
328         new _UniLMBCSGrpMap(0x2031, 0x2031, ULMBCS_AMBIGUOUS_SBCS),\r
329         new _UniLMBCSGrpMap(0x2032, 0x2033, ULMBCS_AMBIGUOUS_MBCS),\r
330         new _UniLMBCSGrpMap(0x2035, 0x2035, ULMBCS_AMBIGUOUS_MBCS),\r
331         new _UniLMBCSGrpMap(0x2039, 0x203A, ULMBCS_AMBIGUOUS_SBCS),\r
332         new _UniLMBCSGrpMap(0x203B, 0x203B, ULMBCS_AMBIGUOUS_MBCS),\r
333         new _UniLMBCSGrpMap(0x203C, 0x203C, ULMBCS_GRP_EXCEPT),\r
334         new _UniLMBCSGrpMap(0x2074, 0x2074, ULMBCS_GRP_KO),\r
335         new _UniLMBCSGrpMap(0x207F, 0x207F, ULMBCS_GRP_EXCEPT),\r
336         new _UniLMBCSGrpMap(0x2081, 0x2084, ULMBCS_GRP_KO),\r
337         new _UniLMBCSGrpMap(0x20A4, 0x20AC, ULMBCS_AMBIGUOUS_SBCS),\r
338         new _UniLMBCSGrpMap(0x2103, 0x2109, ULMBCS_AMBIGUOUS_MBCS),\r
339         new _UniLMBCSGrpMap(0x2111, 0x2120, ULMBCS_AMBIGUOUS_SBCS),\r
340         /*zhujin: upgrade, for regressiont test, spr HKIA4YHTSU*/\r
341         new _UniLMBCSGrpMap(0x2121, 0x2121, ULMBCS_AMBIGUOUS_MBCS),\r
342         new _UniLMBCSGrpMap(0x2122, 0x2126, ULMBCS_AMBIGUOUS_SBCS),\r
343         new _UniLMBCSGrpMap(0x212B, 0x212B, ULMBCS_AMBIGUOUS_MBCS),\r
344         new _UniLMBCSGrpMap(0x2135, 0x2135, ULMBCS_AMBIGUOUS_SBCS),\r
345         new _UniLMBCSGrpMap(0x2153, 0x2154, ULMBCS_GRP_KO),\r
346         new _UniLMBCSGrpMap(0x215B, 0x215E, ULMBCS_GRP_EXCEPT),\r
347         new _UniLMBCSGrpMap(0x2160, 0x2179, ULMBCS_AMBIGUOUS_MBCS),\r
348         new _UniLMBCSGrpMap(0x2190, 0x2193, ULMBCS_AMBIGUOUS_ALL),\r
349         new _UniLMBCSGrpMap(0x2194, 0x2195, ULMBCS_GRP_EXCEPT),\r
350         new _UniLMBCSGrpMap(0x2196, 0x2199, ULMBCS_AMBIGUOUS_MBCS),\r
351         new _UniLMBCSGrpMap(0x21A8, 0x21A8, ULMBCS_GRP_EXCEPT),\r
352         new _UniLMBCSGrpMap(0x21B8, 0x21B9, ULMBCS_GRP_CN),\r
353         new _UniLMBCSGrpMap(0x21D0, 0x21D1, ULMBCS_GRP_EXCEPT),\r
354         new _UniLMBCSGrpMap(0x21D2, 0x21D2, ULMBCS_AMBIGUOUS_MBCS),\r
355         new _UniLMBCSGrpMap(0x21D3, 0x21D3, ULMBCS_GRP_EXCEPT),\r
356         new _UniLMBCSGrpMap(0x21D4, 0x21D4, ULMBCS_AMBIGUOUS_MBCS),\r
357         new _UniLMBCSGrpMap(0x21D5, 0x21D5, ULMBCS_GRP_EXCEPT),\r
358         new _UniLMBCSGrpMap(0x21E7, 0x21E7, ULMBCS_GRP_CN),\r
359         new _UniLMBCSGrpMap(0x2200, 0x2200, ULMBCS_AMBIGUOUS_MBCS),\r
360         new _UniLMBCSGrpMap(0x2201, 0x2201, ULMBCS_GRP_EXCEPT),\r
361         new _UniLMBCSGrpMap(0x2202, 0x2202, ULMBCS_AMBIGUOUS_MBCS),\r
362         new _UniLMBCSGrpMap(0x2203, 0x2203, ULMBCS_AMBIGUOUS_MBCS),\r
363         new _UniLMBCSGrpMap(0x2204, 0x2206, ULMBCS_GRP_EXCEPT),\r
364         new _UniLMBCSGrpMap(0x2207, 0x2208, ULMBCS_AMBIGUOUS_MBCS),\r
365         new _UniLMBCSGrpMap(0x2209, 0x220A, ULMBCS_GRP_EXCEPT),\r
366         new _UniLMBCSGrpMap(0x220B, 0x220B, ULMBCS_AMBIGUOUS_MBCS),\r
367         new _UniLMBCSGrpMap(0x220F, 0x2215, ULMBCS_AMBIGUOUS_MBCS),\r
368         new _UniLMBCSGrpMap(0x2219, 0x2219, ULMBCS_GRP_EXCEPT),\r
369         new _UniLMBCSGrpMap(0x221A, 0x221A, ULMBCS_AMBIGUOUS_MBCS),\r
370         new _UniLMBCSGrpMap(0x221B, 0x221C, ULMBCS_GRP_EXCEPT),\r
371         new _UniLMBCSGrpMap(0x221D, 0x221E, ULMBCS_AMBIGUOUS_MBCS),\r
372         new _UniLMBCSGrpMap(0x221F, 0x221F, ULMBCS_GRP_EXCEPT),\r
373         new _UniLMBCSGrpMap(0x2220, 0x2220, ULMBCS_AMBIGUOUS_MBCS),\r
374         new _UniLMBCSGrpMap(0x2223, 0x222A, ULMBCS_AMBIGUOUS_MBCS),\r
375         new _UniLMBCSGrpMap(0x222B, 0x223D, ULMBCS_AMBIGUOUS_MBCS),\r
376         new _UniLMBCSGrpMap(0x2245, 0x2248, ULMBCS_GRP_EXCEPT),\r
377         new _UniLMBCSGrpMap(0x224C, 0x224C, ULMBCS_GRP_TW),\r
378         new _UniLMBCSGrpMap(0x2252, 0x2252, ULMBCS_AMBIGUOUS_MBCS),\r
379         new _UniLMBCSGrpMap(0x2260, 0x2261, ULMBCS_AMBIGUOUS_MBCS),\r
380         new _UniLMBCSGrpMap(0x2262, 0x2265, ULMBCS_GRP_EXCEPT),\r
381         new _UniLMBCSGrpMap(0x2266, 0x226F, ULMBCS_AMBIGUOUS_MBCS),\r
382         new _UniLMBCSGrpMap(0x2282, 0x2283, ULMBCS_AMBIGUOUS_MBCS),\r
383         new _UniLMBCSGrpMap(0x2284, 0x2285, ULMBCS_GRP_EXCEPT),\r
384         new _UniLMBCSGrpMap(0x2286, 0x2287, ULMBCS_AMBIGUOUS_MBCS),\r
385         new _UniLMBCSGrpMap(0x2288, 0x2297, ULMBCS_GRP_EXCEPT),\r
386         new _UniLMBCSGrpMap(0x2299, 0x22BF, ULMBCS_AMBIGUOUS_MBCS),\r
387         new _UniLMBCSGrpMap(0x22C0, 0x22C0, ULMBCS_GRP_EXCEPT),\r
388         new _UniLMBCSGrpMap(0x2310, 0x2310, ULMBCS_GRP_EXCEPT),\r
389         new _UniLMBCSGrpMap(0x2312, 0x2312, ULMBCS_AMBIGUOUS_MBCS),\r
390         new _UniLMBCSGrpMap(0x2318, 0x2321, ULMBCS_GRP_EXCEPT),\r
391         new _UniLMBCSGrpMap(0x2318, 0x2321, ULMBCS_GRP_CN),\r
392         new _UniLMBCSGrpMap(0x2460, 0x24E9, ULMBCS_AMBIGUOUS_MBCS),\r
393         new _UniLMBCSGrpMap(0x2500, 0x2500, ULMBCS_AMBIGUOUS_SBCS),\r
394         new _UniLMBCSGrpMap(0x2501, 0x2501, ULMBCS_AMBIGUOUS_MBCS),\r
395         new _UniLMBCSGrpMap(0x2502, 0x2502, ULMBCS_AMBIGUOUS_ALL),\r
396         new _UniLMBCSGrpMap(0x2503, 0x2503, ULMBCS_AMBIGUOUS_MBCS),\r
397         new _UniLMBCSGrpMap(0x2504, 0x2505, ULMBCS_GRP_TW),\r
398         new _UniLMBCSGrpMap(0x2506, 0x2665, ULMBCS_AMBIGUOUS_ALL),\r
399         new _UniLMBCSGrpMap(0x2666, 0x2666, ULMBCS_GRP_EXCEPT),\r
400         new _UniLMBCSGrpMap(0x2667, 0x2669, ULMBCS_AMBIGUOUS_SBCS),\r
401         new _UniLMBCSGrpMap(0x266A, 0x266A, ULMBCS_AMBIGUOUS_ALL),\r
402         new _UniLMBCSGrpMap(0x266B, 0x266C, ULMBCS_AMBIGUOUS_SBCS),\r
403         new _UniLMBCSGrpMap(0x266D, 0x266D, ULMBCS_AMBIGUOUS_MBCS),\r
404         new _UniLMBCSGrpMap(0x266E, 0x266E, ULMBCS_AMBIGUOUS_SBCS),\r
405         new _UniLMBCSGrpMap(0x266F, 0x266F, ULMBCS_GRP_JA),\r
406         new _UniLMBCSGrpMap(0x2670, 0x2E7F, ULMBCS_AMBIGUOUS_SBCS),\r
407         new _UniLMBCSGrpMap(0x2E80, 0xF861, ULMBCS_AMBIGUOUS_MBCS),\r
408         new _UniLMBCSGrpMap(0xF862, 0xF8FF, ULMBCS_GRP_EXCEPT),\r
409         new _UniLMBCSGrpMap(0xF900, 0xFA2D, ULMBCS_AMBIGUOUS_MBCS),\r
410         new _UniLMBCSGrpMap(0xFB00, 0xFEFF, ULMBCS_AMBIGUOUS_SBCS),\r
411         new _UniLMBCSGrpMap(0xFF01, 0xFFEE, ULMBCS_AMBIGUOUS_MBCS),\r
412         new _UniLMBCSGrpMap(0xFFFF, 0xFFFF, ULMBCS_GRP_UNICODE)\r
413     };\r
414     \r
415     static short FindLMBCSUniRange(char uniChar) {\r
416         int index = 0;\r
417         \r
418         while (uniChar > UniLMBCSGrpMap[index].uniEndRange) {\r
419             index++;\r
420         }\r
421         \r
422         if (uniChar >= UniLMBCSGrpMap[index].uniStartRange) {\r
423             return UniLMBCSGrpMap[index].GrpType;\r
424         }\r
425         return ULMBCS_GRP_UNICODE;\r
426     }\r
427     \r
428     /*\r
429      * We also ask the creator of a converter to send in a preferred locale\r
430      * that we can use in resolving ambiguous mappings. They send the locale\r
431      * in as a string, and we map it, if possible, to one of the\r
432      * LMBCS groups. We use this table, and the associated code, to\r
433      * do the lookup:\r
434      * \r
435      *     This table maps locale ID's to LMBCS opt groups.\r
436      *     The default return is group 0x01. Note that for\r
437      *     performance reasons, the table is sorted in\r
438      *     increasing alphabetic order, with the notable\r
439      *     exception of zhTW. This is to force the check\r
440      *     for Traditional Chinese before dropping back to\r
441      *     Simplified.\r
442      *     Note too that the Latin-1 groups have been\r
443      *     commented out because it's the default, and\r
444      *     this shortens the table, allowing a serial\r
445      *     search to go quickly.\r
446      */\r
447     private static class _LocaleLMBCSGrpMap {\r
448         String LocaleID;\r
449         short OptGroup;\r
450         _LocaleLMBCSGrpMap(String LocaleID, short OptGroup) {\r
451             this.LocaleID = LocaleID;\r
452             this.OptGroup = OptGroup;\r
453         }\r
454     }\r
455     private static final _LocaleLMBCSGrpMap[] LocaleLMBCSGrpMap = {\r
456         new _LocaleLMBCSGrpMap("ar", ULMBCS_GRP_AR),\r
457         new _LocaleLMBCSGrpMap("be", ULMBCS_GRP_RU),\r
458         new _LocaleLMBCSGrpMap("bg", ULMBCS_GRP_L2),\r
459         // new _LocaleLMBCSGrpMap("ca", ULMBCS_GRP_L1),\r
460         new _LocaleLMBCSGrpMap("cs", ULMBCS_GRP_L2),\r
461         // new _LocaleLMBCSGrpMap("da", ULMBCS_GRP_L1),\r
462         // new _LocaleLMBCSGrpMap("de", ULMBCS_GRP_L1),\r
463         new _LocaleLMBCSGrpMap("el", ULMBCS_GRP_GR),\r
464         // new _LocaleLMBCSGrpMap("en", ULMBCS_GRP_L1),\r
465         // new _LocaleLMBCSGrpMap("es", ULMBCS_GRP_L1),\r
466         // new _LocaleLMBCSGrpMap("et", ULMBCS_GRP_L1),\r
467         // new _LocaleLMBCSGrpMap("fi", ULMBCS_GRP_L1),\r
468         // new _LocaleLMBCSGrpMap("fr", ULMBCS_GRP_L1),\r
469         new _LocaleLMBCSGrpMap("he", ULMBCS_GRP_HE),\r
470         new _LocaleLMBCSGrpMap("hu", ULMBCS_GRP_L2),\r
471         // new _LocaleLMBCSGrpMap("is", ULMBCS_GRP_L1),\r
472         // new _LocaleLMBCSGrpMap("it", ULMBCS_GRP_L1),\r
473         new _LocaleLMBCSGrpMap("iw", ULMBCS_GRP_HE),\r
474         new _LocaleLMBCSGrpMap("ja", ULMBCS_GRP_JA),\r
475         new _LocaleLMBCSGrpMap("ko", ULMBCS_GRP_KO),\r
476         // new _LocaleLMBCSGrpMap("lt", ULMBCS_GRP_L1),\r
477         // new _LocaleLMBCSGrpMap("lv", ULMBCS_GRP_L1),\r
478         new _LocaleLMBCSGrpMap("mk", ULMBCS_GRP_RU),\r
479         // new _LocaleLMBCSGrpMap("nl", ULMBCS_GRP_L1),\r
480         // new _LocaleLMBCSGrpMap("no", ULMBCS_GRP_L1),\r
481         new _LocaleLMBCSGrpMap("pl", ULMBCS_GRP_L2),\r
482         // new _LocaleLMBCSGrpMap("pt", ULMBCS_GRP_L1),\r
483         new _LocaleLMBCSGrpMap("ro", ULMBCS_GRP_L2),\r
484         new _LocaleLMBCSGrpMap("ru", ULMBCS_GRP_RU),\r
485         new _LocaleLMBCSGrpMap("sh", ULMBCS_GRP_L2),\r
486         new _LocaleLMBCSGrpMap("sk", ULMBCS_GRP_L2),\r
487         new _LocaleLMBCSGrpMap("sl", ULMBCS_GRP_L2),\r
488         new _LocaleLMBCSGrpMap("sq", ULMBCS_GRP_L2),\r
489         new _LocaleLMBCSGrpMap("sr", ULMBCS_GRP_RU),\r
490         // new _LocaleLMBCSGrpMap("sv", ULMBCS_GRP_L1),\r
491         new _LocaleLMBCSGrpMap("th", ULMBCS_GRP_TH),\r
492         new _LocaleLMBCSGrpMap("tr", ULMBCS_GRP_TR),\r
493         new _LocaleLMBCSGrpMap("uk", ULMBCS_GRP_RU),\r
494         // new _LocaleLMBCSGrpMap("vi", ULMBCS_GRP_L1),\r
495         new _LocaleLMBCSGrpMap("zhTW", ULMBCS_GRP_TW),\r
496         new _LocaleLMBCSGrpMap("zh", ULMBCS_GRP_CN),\r
497         new _LocaleLMBCSGrpMap(null, ULMBCS_GRP_L1)\r
498     };\r
499     static short FindLMBCSLocale(String LocaleID) {\r
500         int index = 0;\r
501         \r
502         if (LocaleID == null) {\r
503             return 0;\r
504         }\r
505         \r
506         while (LocaleLMBCSGrpMap[index].LocaleID != null) {\r
507             if (LocaleLMBCSGrpMap[index].LocaleID == LocaleID) {\r
508                 return LocaleLMBCSGrpMap[index].OptGroup;\r
509             } else if (LocaleLMBCSGrpMap[index].LocaleID.compareTo(LocaleID) > 0){\r
510                break;\r
511             }\r
512             index++;\r
513         }\r
514         return ULMBCS_GRP_L1;\r
515     }\r
516     \r
517     /*\r
518      * Before we get to the main body of code, here's how we hook up the rest\r
519      * of ICU. ICU converters are required to define a structure that includes\r
520      * some function pointers, and some common data, in the style of a C++\r
521      * vtable. There is also room in there for converter-specific data. LMBCS\r
522      * uses that converter-specific data to keep track of the 12 subconverters\r
523      * we use, the optimization group, and the group (if any) that matches the\r
524      * locale. We have one structure instantiated for each of the 12 possible\r
525      * optimization groups.\r
526      */\r
527     private class UConverterDataLMBCS {\r
528         UConverterSharedData[] OptGrpConverter; /* Converter per Opt. grp. */\r
529         short OptGroup;                         /* default Opt. grp. for this LMBCS session */\r
530         short localeConverterIndex;             /* reasonable locale match for index */\r
531         CharsetDecoderMBCS decoder;\r
532         CharsetEncoderMBCS encoder;\r
533         CharsetMBCS charset;\r
534         UConverterDataLMBCS() {\r
535             OptGrpConverter = new UConverterSharedData[ULMBCS_GRP_LAST + 1];\r
536             charset = (CharsetMBCS)CharsetICU.forNameICU("ibm-850");\r
537             encoder = (CharsetEncoderMBCS)charset.newEncoder();\r
538             decoder = (CharsetDecoderMBCS)charset.newDecoder();\r
539         }\r
540     }\r
541     \r
542     private UConverterDataLMBCS extraInfo; /* extraInfo in ICU4C implementation */\r
543     \r
544     public CharsetLMBCS(String icuCanonicalName, String javaCanonicalName, String[] aliases) {\r
545         super(icuCanonicalName, javaCanonicalName, aliases);\r
546         maxBytesPerChar = ULMBCS_CHARSIZE_MAX; \r
547         minBytesPerChar = 1;\r
548         maxCharsPerByte = 1;\r
549         \r
550         extraInfo = new UConverterDataLMBCS();\r
551         \r
552         for (int i = 0; i <= ULMBCS_GRP_LAST; i++) {\r
553             if (OptGroupByteToCPName[i] != null) {\r
554                 extraInfo.OptGrpConverter[i] = ((CharsetMBCS)CharsetICU.forNameICU(OptGroupByteToCPName[i])).sharedData;\r
555             }\r
556         }\r
557         \r
558       //get the Opt Group number for the LMBCS converter\r
559         int option = Integer.parseInt(icuCanonicalName.substring(6));\r
560         extraInfo.OptGroup = (short)option;\r
561         extraInfo.localeConverterIndex = FindLMBCSLocale(ULocale.getDefault().getBaseName());\r
562     }\r
563     \r
564     class CharsetDecoderLMBCS extends CharsetDecoderICU {\r
565         public CharsetDecoderLMBCS(CharsetICU cs) {\r
566             super(cs);\r
567             implReset();\r
568         }\r
569     \r
570         protected void implReset() {\r
571             super.implReset();\r
572         }\r
573         \r
574         /* A function to call when we are looking at the Unicode group byte in LMBCS */\r
575         private char GetUniFromLMBCSUni(ByteBuffer ppLMBCSin) {\r
576             short HighCh = (short)(ppLMBCSin.get() & UConverterConstants.UNSIGNED_BYTE_MASK);\r
577             short LowCh  = (short)(ppLMBCSin.get() & UConverterConstants.UNSIGNED_BYTE_MASK);\r
578             \r
579             if (HighCh == ULMBCS_UNICOMPATZERO) {\r
580                 HighCh = LowCh;\r
581                 LowCh = 0; /* zero-byte in LSB special character */\r
582             }\r
583             \r
584             return (char)((HighCh << 8) | LowCh);\r
585         }\r
586         \r
587         private int LMBCS_SimpleGetNextUChar(UConverterSharedData cnv, ByteBuffer source, int positionOffset, int length) {\r
588             int uniChar;\r
589             int oldSourceLimit;\r
590             int oldSourcePos;\r
591             \r
592             extraInfo.charset.sharedData = cnv;\r
593             \r
594             oldSourceLimit = source.limit();\r
595             oldSourcePos = source.position();\r
596             \r
597             source.position(oldSourcePos + positionOffset);\r
598             source.limit(source.position() + length);\r
599             \r
600             uniChar = extraInfo.decoder.simpleGetNextUChar(source, false);\r
601             \r
602             source.limit(oldSourceLimit);\r
603             source.position(oldSourcePos);\r
604 \r
605             return uniChar;\r
606         }\r
607         /* Return the Unicode representation for the current LMBCS character. */\r
608         /*\r
609          * Note: Because there is no U_TRUNCATED_CHAR_FOUND error code in ICU4J, we\r
610          *       are going to use BufferOverFlow. The error will be handled correctly\r
611          *       by the calling function.\r
612          */\r
613         private int LMBCSGetNextUCharWorker(ByteBuffer source, CoderResult[] err) {\r
614             int uniChar = 0;  /* an output Unicode char */\r
615             short CurByte;   /* A byte from the input stream */\r
616             \r
617             /* error check */\r
618             if (!source.hasRemaining()) {\r
619                 err[0] = CoderResult.malformedForLength(0);\r
620                 return 0xffff;\r
621             }\r
622             /* Grab first byte & save address for error recovery */\r
623             CurByte = (short)(source.get() & UConverterConstants.UNSIGNED_BYTE_MASK);\r
624             \r
625             /*\r
626              * at entry of each if clause:\r
627              * 1. 'CurByte' points at the first byte of a LMBCS character\r
628              * 2. 'source' points to the next byte of the source stream after 'CurByte'\r
629              * \r
630              * the job of each if clause is:\r
631              * 1. set 'source' to the point at the beginning of the next char (not if LMBCS char is only 1 byte)\r
632              * 2. set 'uniChar' up with the right Unicode value, or set 'err' appropriately\r
633              */\r
634             /* First lets check the simple fixed values. */\r
635             if ((CurByte > ULMBCS_C0END && CurByte < ULMBCS_C1START) /* ascii range */ ||\r
636                 CurByte == 0 || CurByte == ULMBCS_HT || CurByte == ULMBCS_CR || CurByte == ULMBCS_LF ||\r
637                 CurByte == ULMBCS_123SYSTEMRANGE) {\r
638                 \r
639                 uniChar = CurByte;\r
640             } else {\r
641                 short group;\r
642                 UConverterSharedData cnv;\r
643                 \r
644                 if (CurByte == ULMBCS_GRP_CTRL) {  /* Control character group - no opt group update */\r
645                     short C0C1byte;\r
646                     /* CHECK_SOURCE_LIMIT(1) */\r
647                     if (source.position() + 1 > source.limit()) {\r
648                         err[0] = CoderResult.OVERFLOW;\r
649                         source.position(source.limit());\r
650                         return 0xFFFF;\r
651                     }\r
652                     C0C1byte = (short)(source.get() & UConverterConstants.UNSIGNED_BYTE_MASK);\r
653                     uniChar = (C0C1byte < ULMBCS_C1START) ? C0C1byte - ULMBCS_CTRLOFFSET : C0C1byte;\r
654                 } else if (CurByte == ULMBCS_GRP_UNICODE) { /* Unicode Compatibility group: Big Endian UTF16 */\r
655                     /* CHECK_SOURCE_LIMIT(2) */\r
656                     if (source.position() + 2 > source.limit()) {\r
657                         err[0] = CoderResult.OVERFLOW;\r
658                         source.position(source.limit());\r
659                         return 0xFFFF;\r
660                     }\r
661                     \r
662                     /* don't check for error indicators fffe/ffff below */\r
663                     return GetUniFromLMBCSUni(source);\r
664                 } else if (CurByte <= ULMBCS_CTRLOFFSET) {\r
665                     group = CurByte;\r
666                     if (group > ULMBCS_GRP_LAST || (cnv = extraInfo.OptGrpConverter[group]) == null) {\r
667                         /* this is not a valid group byte - no converter */\r
668                         err[0] = CoderResult.unmappableForLength(1);\r
669                     } else if (group >= ULMBCS_DOUBLEOPTGROUP_START) {\r
670                         /* CHECK_SOURCE_LIMIT(2) */\r
671                         if (source.position() + 2 > source.limit()) {\r
672                             err[0] = CoderResult.OVERFLOW;\r
673                             source.position(source.limit());\r
674                             return 0xFFFF;\r
675                         }\r
676                         \r
677                         /* check for LMBCS doubled-group-byte case */\r
678                         if (source.get(source.position()) == group) {\r
679                             /* single byte */\r
680                             source.get();\r
681                             uniChar = LMBCS_SimpleGetNextUChar(cnv, source, 0, 1);\r
682                             source.get();\r
683                         } else {\r
684                             /* double byte */\r
685                             uniChar = LMBCS_SimpleGetNextUChar(cnv, source, 0, 2);\r
686                             source.get();\r
687                             source.get();\r
688                         }\r
689                     } else { /* single byte conversion */\r
690                         /* CHECK_SOURCE_LIMIT(1) */\r
691                         if (source.position() + 1 > source.limit()) {\r
692                             err[0] = CoderResult.OVERFLOW;\r
693                             source.position(source.limit());\r
694                             return 0xFFFF;\r
695                         }\r
696                         CurByte = (short)(source.get() & UConverterConstants.UNSIGNED_BYTE_MASK);\r
697                         \r
698                         if (CurByte >= ULMBCS_C1START) {\r
699                             uniChar = CharsetMBCS.MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(cnv.mbcs, CurByte);\r
700                         } else {\r
701                             /*\r
702                              * The non-optimizable oddballs where there is an explicit byte\r
703                              * AND the second byte is not in the upper ascii range\r
704                              */\r
705                             byte[] bytes = new byte[2];\r
706                             \r
707                             cnv = extraInfo.OptGrpConverter[ULMBCS_GRP_EXCEPT];\r
708                             \r
709                             /* Lookup value must include opt group */\r
710                             bytes[0] = (byte)group;\r
711                             bytes[1] = (byte)CurByte;\r
712                             uniChar = LMBCS_SimpleGetNextUChar(cnv, ByteBuffer.wrap(bytes), 0, 2);\r
713                         }\r
714                     }\r
715                     \r
716                 } else if (CurByte >= ULMBCS_C1START) { /* group byte is implicit */\r
717                     group = extraInfo.OptGroup;\r
718                     cnv = extraInfo.OptGrpConverter[group];\r
719                     if (group >= ULMBCS_DOUBLEOPTGROUP_START) { /* double byte conversion */\r
720                         if (CharsetMBCS.MBCS_ENTRY_IS_TRANSITION(cnv.mbcs.stateTable[0][CurByte]) /* isLeadByte */) {\r
721                             /* CHECK_SOURCE_LIMIT(0) */\r
722                             if (source.position() + 0 > source.limit()) {\r
723                                 err[0] = CoderResult.OVERFLOW;\r
724                                 source.position(source.limit());\r
725                                 return 0xFFFF;\r
726                             }\r
727                             \r
728                             /* let the MBCS conversion consume CurByte again */\r
729                             uniChar = LMBCS_SimpleGetNextUChar(cnv, source, -1, 1);\r
730                         } else {\r
731                             /* CHECK_SOURCE_LIMIT(1) */\r
732                             if (source.position() + 1 > source.limit()) {\r
733                                 err[0] = CoderResult.OVERFLOW;\r
734                                 source.position(source.limit());\r
735                                 return 0xFFFF;\r
736                             }\r
737                             \r
738                             /* let the MBCS conversion consume CurByte again */\r
739                             uniChar = LMBCS_SimpleGetNextUChar(cnv, source, -1, 2);\r
740                             source.get();\r
741                         }\r
742                     } else {\r
743                         uniChar = CharsetMBCS.MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(cnv.mbcs, CurByte);\r
744                     }\r
745                 }\r
746             }\r
747             \r
748             return uniChar;\r
749         }\r
750         \r
751         protected CoderResult decodeLoop(ByteBuffer source, CharBuffer target, IntBuffer offsets, boolean flush) { \r
752             CoderResult[] err = new CoderResult[1];\r
753             err[0] = CoderResult.UNDERFLOW;\r
754             byte[] LMBCS = new byte[ULMBCS_CHARSIZE_MAX * 2]; /* Increase the size for proper handling in subsequent calls to MBCS functions */\r
755             char uniChar;   /* one output Unicode char */\r
756             int saveSource; /* beginning of current code point */\r
757             int errSource = 0; /* index to actual input in case an error occurs */\r
758             byte savebytes = 0;\r
759             \r
760             /* Process from source to limit, or until error */\r
761             while (err[0].isUnderflow() && source.hasRemaining() && target.hasRemaining()) {\r
762                 saveSource = source.position(); /* beginning of current code point */\r
763                 if (toULength > 0) { /* reassemble char from previous call */\r
764                     int size_old = toULength;\r
765                     ByteBuffer tmpSourceBuffer;\r
766                     \r
767                     /* limit from source is either remainder of temp buffer, or user limit on source */\r
768                     int size_new_maybe_1 = ULMBCS_CHARSIZE_MAX - size_old;\r
769                     int size_new_maybe_2 = source.remaining();\r
770                     int size_new = (size_new_maybe_1 < size_new_maybe_2) ? size_new_maybe_1 : size_new_maybe_2;\r
771                     savebytes = (byte)(size_old + size_new);\r
772                     for (int i = 0; i < savebytes; i++) {\r
773                         if (i < size_old) {\r
774                             LMBCS[i] = toUBytesArray[i];\r
775                         } else {\r
776                             LMBCS[i] = source.get();\r
777                         }\r
778                     }\r
779                     tmpSourceBuffer = ByteBuffer.wrap(LMBCS);\r
780                     tmpSourceBuffer.limit(savebytes);\r
781                     uniChar = (char)LMBCSGetNextUCharWorker(tmpSourceBuffer, err);\r
782                     source.position(saveSource + tmpSourceBuffer.position() - size_old);\r
783                     errSource = saveSource - size_old;\r
784                     \r
785                     if (err[0].isOverflow()) { /* err == U_TRUNCATED_CHAR_FOUND */ \r
786                         /* evil special case: source buffers so small a char spans more than 2 buffers */\r
787                         toULength = savebytes;\r
788                         for (int i = 0; i < savebytes; i++) {\r
789                             toUBytesArray[i] = LMBCS[i];\r
790                         }\r
791                         source.position(source.limit());\r
792                         err[0] = CoderResult.UNDERFLOW;\r
793                         return err[0];\r
794                     } else {\r
795                         /* clear the partial-char marker */\r
796                         toULength = 0;\r
797                     }\r
798                 } else {\r
799                     errSource = saveSource;\r
800                     uniChar = (char)LMBCSGetNextUCharWorker(source, err);\r
801                     savebytes = (byte)(source.position() - saveSource);\r
802                 }\r
803                 \r
804                 if (err[0].isUnderflow()) {\r
805                     if (uniChar < 0x0fffe) {\r
806                         target.put(uniChar);\r
807                         if (offsets != null) {\r
808                             offsets.put(saveSource);\r
809                         }\r
810                     } else if (uniChar == 0xfffe) {\r
811                         err[0] = CoderResult.unmappableForLength(source.position() - saveSource);\r
812                     } else /* if (uniChar == 0xffff) */ {\r
813                         err[0] = CoderResult.malformedForLength(source.position() - saveSource);\r
814                     }\r
815                 }\r
816             }\r
817             /* If target ran out before source, return over flow buffer error. */\r
818             if (err[0].isUnderflow() && source.hasRemaining() && !target.hasRemaining()) {\r
819                 err[0] = CoderResult.OVERFLOW;\r
820             } else if (!err[0].isUnderflow()) {\r
821                 /* If character incomplete or unmappable/illegal, store it in toUBytesArray[] */\r
822                 toULength = savebytes;\r
823                 if (savebytes > 0) {\r
824                     for (int i = 0; i < savebytes; i++) {\r
825                         toUBytesArray[i] = source.get(errSource + i);\r
826                     }\r
827                 }\r
828                 if (err[0].isOverflow()) { /* err == U_TRUNCATED_CHAR_FOUND */\r
829                     err[0] = CoderResult.UNDERFLOW;\r
830                 }\r
831             }\r
832             return err[0];\r
833         }\r
834     }\r
835     \r
836     class CharsetEncoderLMBCS extends CharsetEncoderICU {\r
837         public CharsetEncoderLMBCS(CharsetICU cs) {\r
838             super(cs, fromUSubstitution);\r
839             implReset();\r
840         }\r
841         \r
842         protected void implReset() {\r
843             super.implReset();\r
844         }\r
845         /*\r
846          * Here's the basic helper function that we use when converting from\r
847          * Unicode to LMBCS, and we suspect that a Unicode character will fit into\r
848          * one of the 12 groups. The return value is the number of bytes written\r
849          * starting at pStartLMBCS (if any).\r
850          */\r
851         @SuppressWarnings("fallthrough")\r
852         private int LMBCSConversionWorker(short group, byte[] LMBCS, char pUniChar, short[] lastConverterIndex, boolean[] groups_tried) {\r
853             byte pLMBCS = 0;\r
854             UConverterSharedData xcnv = extraInfo.OptGrpConverter[group];\r
855             \r
856             int bytesConverted;\r
857             int[] value = new int[1];\r
858             short firstByte;\r
859             \r
860             extraInfo.charset.sharedData = xcnv;\r
861             bytesConverted = extraInfo.encoder.fromUChar32(pUniChar, value, false);\r
862             \r
863             /* get the first result byte */\r
864             if (bytesConverted > 0) {\r
865                 firstByte = (short)((value[0] >> ((bytesConverted - 1) * 8)) & UConverterConstants.UNSIGNED_BYTE_MASK);\r
866             } else {\r
867                 /* most common failure mode is an unassigned character */\r
868                 groups_tried[group] = true;\r
869                 return 0;\r
870             }\r
871             \r
872             lastConverterIndex[0] = group;\r
873             \r
874             /* \r
875              * All initial byte values in lower ascii range should have been caught by now,\r
876              * except with the exception group.\r
877              */\r
878             \r
879             /* use converted data: first write 0, 1 or two group bytes */\r
880             if (group != ULMBCS_GRP_EXCEPT && extraInfo.OptGroup != group) {\r
881                 LMBCS[pLMBCS++] = (byte)group;\r
882                 if (bytesConverted == 1 && group >= ULMBCS_DOUBLEOPTGROUP_START) {\r
883                     LMBCS[pLMBCS++] = (byte)group;\r
884                 }\r
885             }\r
886             \r
887             /* don't emit control chars */\r
888             if (bytesConverted == 1 && firstByte < 0x20) {\r
889                 return 0;\r
890             }\r
891             \r
892             /* then move over the converted data */\r
893             switch (bytesConverted) {\r
894             case 4:\r
895                 LMBCS[pLMBCS++] = (byte)(value[0] >> 24);\r
896             case 3:\r
897                 LMBCS[pLMBCS++] = (byte)(value[0] >> 16);\r
898             case 2:\r
899                 LMBCS[pLMBCS++] = (byte)(value[0] >> 8);\r
900             case 1:\r
901                 LMBCS[pLMBCS++] = (byte)value[0];\r
902             default:\r
903                 /* will never occur */\r
904                 break;\r
905             }\r
906             \r
907             return pLMBCS;\r
908         }\r
909         /*\r
910          * This is a much simpler version of above, when we\r
911          * know we are writing LMBCS using the Unicode group.\r
912          */\r
913         private int LMBCSConvertUni(byte[] LMBCS, char uniChar) {\r
914             int index = 0;\r
915             short LowCh  = (short)(uniChar & UConverterConstants.UNSIGNED_BYTE_MASK);\r
916             short HighCh = (short)((uniChar >> 8) & UConverterConstants.UNSIGNED_BYTE_MASK);\r
917             \r
918             LMBCS[index++] = (byte)ULMBCS_GRP_UNICODE;\r
919             \r
920             if (LowCh == 0) {\r
921                 LMBCS[index++] = (byte)ULMBCS_UNICOMPATZERO;\r
922                 LMBCS[index++] = (byte)HighCh;\r
923             } else {\r
924                 LMBCS[index++] = (byte)HighCh;\r
925                 LMBCS[index++] = (byte)LowCh;\r
926             }\r
927             return ULMBCS_UNICODE_SIZE;\r
928         }\r
929         /* The main Unicode to LMBCS conversion function */\r
930         protected CoderResult encodeLoop(CharBuffer source, ByteBuffer target, IntBuffer offsets, boolean flush) {\r
931             CoderResult err = CoderResult.UNDERFLOW;\r
932             short[] lastConverterIndex = new short[1];\r
933             char uniChar;\r
934             byte[] LMBCS = new byte[ULMBCS_CHARSIZE_MAX];\r
935             byte pLMBCS;\r
936             int bytes_written;\r
937             boolean[] groups_tried = new boolean[ULMBCS_GRP_LAST+1];\r
938             int sourceIndex = 0;\r
939             \r
940             /*\r
941              * Basic strategy: attempt to fill in local LMBCS 1-char buffer.(LMBCS)\r
942              * If that succeeds, see if it will all fit into the target & copy it over\r
943              * if it does.\r
944              * \r
945              * We try conversions in the following order:\r
946              * 1. Single-byte ascii & special fixed control chars (&null)\r
947              * 2. Look up group in table & try that (could b\r
948              *     A) Unicode group\r
949              *     B) control group\r
950              *     C) national encodeing\r
951              *        or ambiguous SBCS or MBCS group (on to step 4...)\r
952              * 3. If its ambiguous, try this order:\r
953              *     A) The optimization group\r
954              *     B) The locale group\r
955              *     C) The last group that succeeded with this string.\r
956              *     D) every other group that's relevant\r
957              *     E) If its single-byte ambiguous, try the exceptions group\r
958              * 4. And as a grand fallback: Unicode\r
959              */\r
960             \r
961             short OldConverterIndex = 0;\r
962             \r
963             while (source.hasRemaining() && err.isUnderflow()) {\r
964                 OldConverterIndex = extraInfo.localeConverterIndex;\r
965                 \r
966                 if (!target.hasRemaining()) {\r
967                     err = CoderResult.OVERFLOW;\r
968                     break;\r
969                 }\r
970                 \r
971                 uniChar = source.get(source.position());\r
972                 bytes_written = 0;\r
973                 pLMBCS = 0;\r
974                 \r
975                 /* check cases in rough order of how common they are, for speed */\r
976                 \r
977                 /* single-byte matches: strategy 1 */\r
978                 if((uniChar>=0x80) && (uniChar<=0xff) && (uniChar!=0xB1) && (uniChar!=0xD7) && (uniChar!=0xF7) &&\r
979                    (uniChar!=0xB0) && (uniChar!=0xB4) && (uniChar!=0xB6) && (uniChar!=0xA7) && (uniChar!=0xA8)) {\r
980                       extraInfo.localeConverterIndex = ULMBCS_GRP_L1;\r
981                 }\r
982                 if (((uniChar > ULMBCS_C0END) && (uniChar < ULMBCS_C1START)) ||\r
983                     uniChar == 0 || uniChar == ULMBCS_HT || uniChar == ULMBCS_CR ||\r
984                     uniChar == ULMBCS_LF || uniChar == ULMBCS_123SYSTEMRANGE) {\r
985                     LMBCS[pLMBCS++] = (byte)uniChar;\r
986                     bytes_written = 1;\r
987                 }\r
988                 \r
989                 if (bytes_written == 0) {\r
990                     /* Check by Unicode rage (Strategy 2) */\r
991                     short group = FindLMBCSUniRange(uniChar);\r
992                     if (group == ULMBCS_GRP_UNICODE) { /* (Strategy 2A) */\r
993                         bytes_written = LMBCSConvertUni(LMBCS, uniChar);\r
994                     } else if (group == ULMBCS_GRP_CTRL) { /* Strategy 2B) */\r
995                         /* Handle control characters here */\r
996                         if (uniChar <= ULMBCS_C0END) {\r
997                             LMBCS[pLMBCS++] = ULMBCS_GRP_CTRL;\r
998                             LMBCS[pLMBCS++] = (byte)(ULMBCS_CTRLOFFSET + uniChar);\r
999                         } else if (uniChar >= ULMBCS_C1START && uniChar <= (ULMBCS_C1START + ULMBCS_CTRLOFFSET)) {\r
1000                             LMBCS[pLMBCS++] = ULMBCS_GRP_CTRL;\r
1001                             LMBCS[pLMBCS++] = (byte)uniChar;\r
1002                         }\r
1003                         bytes_written = pLMBCS;\r
1004                     } else if (group < ULMBCS_GRP_UNICODE) { /* (Strategy 2C) */\r
1005                         /* a specific converter has been identified - use it */\r
1006                         bytes_written = LMBCSConversionWorker(group, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1007                     }\r
1008                     if (bytes_written == 0) { /* the ambiguous group cases (Strategy 3) */\r
1009                         groups_tried = new boolean[ULMBCS_GRP_LAST+1];\r
1010                         \r
1011                         /* check for non-default optimization group (Strategy 3A) */\r
1012                         if (extraInfo.OptGroup != 1 && ULMBCS_AMBIGUOUS_MATCH(group, extraInfo.OptGroup)) {\r
1013                             if(extraInfo.localeConverterIndex < ULMBCS_DOUBLEOPTGROUP_START) {\r
1014                                 bytes_written = LMBCSConversionWorker (ULMBCS_GRP_L1, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1015                             \r
1016                                 if(bytes_written == 0) {\r
1017                                     bytes_written = LMBCSConversionWorker (ULMBCS_GRP_EXCEPT, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1018                                 }\r
1019                                 if(bytes_written == 0) {\r
1020                                     bytes_written = LMBCSConversionWorker (extraInfo.localeConverterIndex, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1021                                 }\r
1022                             } else {\r
1023                                  bytes_written = LMBCSConversionWorker (extraInfo.localeConverterIndex, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1024                             }\r
1025                         }\r
1026                         /* check for locale optimization group (Strategy 3B) */\r
1027                         if (bytes_written == 0 && extraInfo.localeConverterIndex > 0 && ULMBCS_AMBIGUOUS_MATCH(group, extraInfo.localeConverterIndex)) {\r
1028                             \r
1029                             bytes_written = LMBCSConversionWorker(extraInfo.localeConverterIndex, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1030                         }\r
1031                         /* check for last optimization group used for this string (Strategy 3C) */\r
1032                         if (bytes_written == 0 && lastConverterIndex[0] > 0 && ULMBCS_AMBIGUOUS_MATCH(group, lastConverterIndex[0])) {\r
1033                             bytes_written = LMBCSConversionWorker(lastConverterIndex[0], LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1034                         }\r
1035                         if (bytes_written == 0) {\r
1036                             /* just check every possible matching converter (Strategy 3D) */\r
1037                             short grp_start;\r
1038                             short grp_end;\r
1039                             short grp_ix;\r
1040                             \r
1041                             grp_start = (group == ULMBCS_AMBIGUOUS_MBCS) ? ULMBCS_DOUBLEOPTGROUP_START : ULMBCS_GRP_L1;\r
1042                             grp_end   = (group == ULMBCS_AMBIGUOUS_MBCS) ? ULMBCS_GRP_LAST : ULMBCS_GRP_TH;\r
1043                             \r
1044                             if(group == ULMBCS_AMBIGUOUS_ALL) {\r
1045                                 grp_start = ULMBCS_GRP_L1;\r
1046                                 grp_end = ULMBCS_GRP_LAST;\r
1047                             }\r
1048                             \r
1049                             for (grp_ix = grp_start; grp_ix <= grp_end && bytes_written == 0; grp_ix++) {\r
1050                                 if (extraInfo.OptGrpConverter[grp_ix] != null && !groups_tried[grp_ix]) {\r
1051                                     bytes_written = LMBCSConversionWorker(grp_ix, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1052                                 }\r
1053                             }\r
1054                             /* \r
1055                              * a final conversion fallback to the exceptions group if its likely\r
1056                              * to be single byte (Strategy 3E) \r
1057                              */\r
1058                             if (bytes_written == 0 && grp_start == ULMBCS_GRP_L1) {\r
1059                                 bytes_written = LMBCSConversionWorker(ULMBCS_GRP_EXCEPT, LMBCS, uniChar, lastConverterIndex, groups_tried);\r
1060                             }\r
1061                         }\r
1062                         /* all of our other strategies failed. Fallback to Unicode. (Strategy 4) */\r
1063                         if (bytes_written == 0) {\r
1064                             bytes_written = LMBCSConvertUni(LMBCS, uniChar);\r
1065                         }\r
1066                     }\r
1067                 }\r
1068                 /* we have a translation. increment source and write as much as possible to target */\r
1069                 source.get();\r
1070                 pLMBCS = 0;\r
1071                 while (target.hasRemaining() && bytes_written > 0) {\r
1072                     bytes_written--;\r
1073                     target.put(LMBCS[pLMBCS++]);\r
1074                     if (offsets != null) {\r
1075                         offsets.put(sourceIndex);\r
1076                     }\r
1077                 }\r
1078                 sourceIndex++;\r
1079                 if (bytes_written > 0) {\r
1080                     /*\r
1081                      * write any bytes that didn't fit in target to the error buffer,\r
1082                      * common code will move this to target if we get called back with\r
1083                      * enough target room\r
1084                      */\r
1085                     err = CoderResult.OVERFLOW;\r
1086                     errorBufferLength = bytes_written;\r
1087                     for (int i = 0; bytes_written > 0; i++, bytes_written--) {\r
1088                         errorBuffer[i] = LMBCS[pLMBCS++];\r
1089                     }\r
1090                 }\r
1091                 extraInfo.localeConverterIndex = OldConverterIndex;\r
1092             }\r
1093             \r
1094             return err;\r
1095         }\r
1096     }\r
1097     public CharsetDecoder newDecoder() {\r
1098         return new CharsetDecoderLMBCS(this);\r
1099     }\r
1100     \r
1101     public CharsetEncoder newEncoder() {\r
1102         return new CharsetEncoderLMBCS(this);\r
1103     }\r
1104     \r
1105     void getUnicodeSetImpl(UnicodeSet setFillIn, int which){\r
1106         getCompleteUnicodeSet(setFillIn);\r
1107     }\r
1108     private byte[] fromUSubstitution = new byte[]{ 0x3F };\r
1109 }\r