]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ucnv_lmb.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / ucnv_lmb.c
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
3* Copyright (C) 2000-2003, International Business Machines
4* Corporation and others. All Rights Reserved.
5**********************************************************************
6* file name: ucnv_lmb.cpp
7* encoding: US-ASCII
8* tab size: 4 (not used)
9* indentation:4
10*
11* created on: 2000feb09
12* created by: Brendan Murray
13* extensively hacked up by: Jim Snyder-Grant
14*
15* Modification History:
16*
17* Date Name Description
18*
19* 06/20/2000 helena OS/400 port changes; mostly typecast.
20* 06/27/2000 Jim Snyder-Grant Deal with partial characters and small buffers.
21* Add comments to document LMBCS format and implementation
22* restructured order & breakdown of functions
23* 06/28/2000 helena Major rewrite for the callback API changes.
24*/
25
26#include "unicode/utypes.h"
27
28#if !UCONFIG_NO_LEGACY_CONVERSION
29
30#include "cmemory.h"
31#include "unicode/ucnv_err.h"
32#include "ucnv_bld.h"
33#include "unicode/ucnv.h"
34#include "ucnv_cnv.h"
35
36/*
37 LMBCS
38
39 (Lotus Multi-Byte Character Set)
40
41 LMBCS was invented in the late 1980's and is primarily used in Lotus Notes
42 databases and in Lotus 1-2-3 files. Programmers who work with the APIs
43 into these products will sometimes need to deal with strings in this format.
44
45 The code in this file provides an implementation for an ICU converter of
46 LMBCS to and from Unicode.
47
48 Since the LMBCS character set is only sparsely documented in existing
49 printed or online material, we have added extensive annotation to this
50 file to serve as a guide to understanding LMBCS.
51
52 LMBCS was originally designed with these four sometimes-competing design goals:
53
54 -Provide encodings for the characters in 12 existing national standards
55 (plus a few other characters)
56 -Minimal memory footprint
57 -Maximal speed of conversion into the existing national character sets
58 -No need to track a changing state as you interpret a string.
59
60
61 All of the national character sets LMBCS was trying to encode are 'ANSI'
62 based, in that the bytes from 0x20 - 0x7F are almost exactly the
63 same common Latin unaccented characters and symbols in all character sets.
64
65 So, in order to help meet the speed & memory design goals, the common ANSI
66 bytes from 0x20-0x7F are represented by the same single-byte values in LMBCS.
67
68 The general LMBCS code unit is from 1-3 bytes. We can describe the 3 bytes as
69 follows:
70
71 [G] D1 [D2]
72
73 That is, a sometimes-optional 'group' byte, followed by 1 and sometimes 2
74 data bytes. The maximum size of a LMBCS chjaracter is 3 bytes:
75*/
76#define ULMBCS_CHARSIZE_MAX 3
77/*
78 The single-byte values from 0x20 to 0x7F are examples of single D1 bytes.
79 We often have to figure out if byte values are below or above this, so we
80 use the ANSI nomenclature 'C0' and 'C1' to refer to the range of control
81 characters just above & below the common lower-ANSI range */
82#define ULMBCS_C0END 0x1F
83#define ULMBCS_C1START 0x80
84/*
85 Since LMBCS is always dealing in byte units. we create a local type here for
86 dealing with these units of LMBCS code units:
87
88*/
89typedef uint8_t ulmbcs_byte_t;
90
91/*
92 Most of the values less than 0x20 are reserved in LMBCS to announce
93 which national character standard is being used for the 'D' bytes.
94 In the comments we show the common name and the IBM character-set ID
95 for these character-set announcers:
96*/
97
98#define ULMBCS_GRP_L1 0x01 /* Latin-1 :ibm-850 */
99#define ULMBCS_GRP_GR 0x02 /* Greek :ibm-851 */
100#define ULMBCS_GRP_HE 0x03 /* Hebrew :ibm-1255 */
101#define ULMBCS_GRP_AR 0x04 /* Arabic :ibm-1256 */
102#define ULMBCS_GRP_RU 0x05 /* Cyrillic :ibm-1251 */
103#define ULMBCS_GRP_L2 0x06 /* Latin-2 :ibm-852 */
104#define ULMBCS_GRP_TR 0x08 /* Turkish :ibm-1254 */
105#define ULMBCS_GRP_TH 0x0B /* Thai :ibm-874 */
106#define ULMBCS_GRP_JA 0x10 /* Japanese :ibm-943 */
107#define ULMBCS_GRP_KO 0x11 /* Korean :ibm-1261 */
108#define ULMBCS_GRP_TW 0x12 /* Chinese SC :ibm-950 */
109#define ULMBCS_GRP_CN 0x13 /* Chinese TC :ibm-1386 */
110
111/*
112 So, the beginning of understanding LMBCS is that IF the first byte of a LMBCS
113 character is one of those 12 values, you can interpret the remaining bytes of
114 that character as coming from one of those character sets. Since the lower
115 ANSI bytes already are represented in single bytes, using one of the character
116 set announcers is used to announce a character that starts with a byte of
117 0x80 or greater.
118
119 The character sets are arranged so that the single byte sets all appear
120 before the multi-byte character sets. When we need to tell whether a
121 group byte is for a single byte char set or not we use this define: */
122
123#define ULMBCS_DOUBLEOPTGROUP_START 0x10
124
125/*
126However, to fully understand LMBCS, you must also understand a series of
127exceptions & optimizations made in service of the design goals.
128
129First, those of you who are character set mavens may have noticed that
130the 'double-byte' character sets are actually multi-byte character sets
131that can have 1 or two bytes, even in the upper-ascii range. To force
132each group byte to introduce a fixed-width encoding (to make it faster to
133count characters), we use a convention of doubling up on the group byte
134to introduce any single-byte character > 0x80 in an otherwise double-byte
135character set. So, for example, the LMBCS sequence x10 x10 xAE is the
136same as '0xAE' in the Japanese code page 943.
137
138Next, you will notice that the list of group bytes has some gaps.
139These are used in various ways.
140
141We reserve a few special single byte values for common control
142characters. These are in the same place as their ANSI eqivalents for speed.
143*/
144
145#define ULMBCS_HT 0x09 /* Fixed control char - Horizontal Tab */
146#define ULMBCS_LF 0x0A /* Fixed control char - Line Feed */
147#define ULMBCS_CR 0x0D /* Fixed control char - Carriage Return */
148
149/* Then, 1-2-3 reserved a special single-byte character to put at the
150beginning of internal 'system' range names: */
151
152#define ULMBCS_123SYSTEMRANGE 0x19
153
154/* Then we needed a place to put all the other ansi control characters
155that must be moved to different values because LMBCS reserves those
156values for other purposes. To represent the control characters, we start
157with a first byte of 0xF & add the control chaarcter value as the
158second byte */
159#define ULMBCS_GRP_CTRL 0x0F
160
161/* For the C0 controls (less than 0x20), we add 0x20 to preserve the
162useful doctrine that any byte less than 0x20 in a LMBCS char must be
163the first byte of a character:*/
164#define ULMBCS_CTRLOFFSET 0x20
165
166/*
167Where to put the characters that aren't part of any of the 12 national
168character sets? The first thing that was done, in the earlier years of
169LMBCS, was to use up the spaces of the form
170
171 [G] D1,
172
173 where 'G' was one of the single-byte character groups, and
174 D1 was less than 0x80. These sequences are gathered together
175 into a Lotus-invented doublebyte character set to represent a
176 lot of stray values. Internally, in this implementation, we track this
177 as group '0', as a place to tuck this exceptions list.*/
178
179#define ULMBCS_GRP_EXCEPT 0x00
180/*
181 Finally, as the durability and usefulness of UNICODE became clear,
182 LOTUS added a new group 0x14 to hold Unicode values not otherwise
183 represented in LMBCS: */
184#define ULMBCS_GRP_UNICODE 0x14
185/* The two bytes appearing after a 0x14 are intrepreted as UFT-16 BE
186(Big-Endian) characters. The exception comes when the UTF16
187representation would have a zero as the second byte. In that case,
188'F6' is used in its place, and the bytes are swapped. (This prevents
189LMBCS from encoding any Unicode values of the form U+F6xx, but that's OK:
1900xF6xx is in the middle of the Private Use Area.)*/
191#define ULMBCS_UNICOMPATZERO 0xF6
192
193/* It is also useful in our code to have a constant for the size of
194a LMBCS char that holds a literal Unicode value */
195#define ULMBCS_UNICODE_SIZE 3
196
197/*
198To squish the LMBCS representations down even further, and to make
199translations even faster,sometimes the optimization group byte can be dropped
200from a LMBCS character. This is decided on a process-by-process basis. The
201group byte that is dropped is called the 'optimization group'.
202
203For Notes, the optimzation group is always 0x1.*/
204#define ULMBCS_DEFAULTOPTGROUP 0x1
205/* For 1-2-3 files, the optimzation group is stored in the header of the 1-2-3
206file.
207
208 In any case, when using ICU, you either pass in the
209optimization group as part of the name of the converter (LMBCS-1, LMBCS-2,
210etc.). Using plain 'LMBCS' as the name of the converter will give you
211LMBCS-1.
212
213
214*** Implementation strategy ***
215
216
217Because of the extensive use of other character sets, the LMBCS converter
218keeps a mapping between optimization groups and IBM character sets, so that
219ICU converters can be created and used as needed. */
220
221static const char * const OptGroupByteToCPName[ULMBCS_CTRLOFFSET] = {
222 /* 0x0000 */ "lmb-excp", /* internal home for the LOTUS exceptions list */
223 /* 0x0001 */ "ibm-850",
224 /* 0x0002 */ "ibm-851",
225 /* 0x0003 */ "windows-1255",
226 /* 0x0004 */ "windows-1256",
227 /* 0x0005 */ "windows-1251",
228 /* 0x0006 */ "ibm-852",
229 /* 0x0007 */ NULL, /* Unused */
230 /* 0x0008 */ "windows-1254",
231 /* 0x0009 */ NULL, /* Control char HT */
232 /* 0x000A */ NULL, /* Control char LF */
233 /* 0x000B */ "windows-874",
234 /* 0x000C */ NULL, /* Unused */
235 /* 0x000D */ NULL, /* Control char CR */
236 /* 0x000E */ NULL, /* Unused */
237 /* 0x000F */ NULL, /* Control chars: 0x0F20 + C0/C1 character: algorithmic */
238 /* 0x0010 */ "windows-932",
239 /* 0x0011 */ "windows-949",
240 /* 0x0012 */ "windows-950",
241 /* 0x0013 */ "windows-936"
242
243 /* The rest are null, including the 0x0014 Unicode compatibility region
244 and 0x0019, the 1-2-3 system range control char */
245};
246
247/* As you can see, even though any byte below 0x20 could be an optimization
248byte, only those at 0x13 or below can map to an actual converter. To limit
249some loops and searches, we define a value for that last group converter:*/
250
251#define ULMBCS_GRP_LAST 0x13 /* last LMBCS group that has a converter */
252
253
254/* That's approximately all the data that's needed for translating
255 LMBCS to Unicode.
256
257
258However, to translate Unicode to LMBCS, we need some more support.
259
260That's because there are often more than one possible mappings from a Unicode
261code point back into LMBCS. The first thing we do is look up into a table
262to figure out if there are more than one possible mappings. This table,
263arranged by Unicode values (including ranges) either lists which group
264to use, or says that it could go into one or more of the SBCS sets, or
265into one or more of the DBCS sets. (If the character exists in both DBCS &
266SBCS, the table will place it in the SBCS sets, to make the LMBCS code point
267length as small as possible. Here's the two special markers we use to indicate
268ambiguous mappings: */
269
270#define ULMBCS_AMBIGUOUS_SBCS 0x80 /* could fit in more than one
271 LMBCS sbcs native encoding
272 (example: most accented latin) */
273#define ULMBCS_AMBIGUOUS_MBCS 0x81 /* could fit in more than one
274 LMBCS mbcs native encoding
275 (example: Unihan) */
276
277/* And here's a simple way to see if a group falls in an appropriate range */
278#define ULMBCS_AMBIGUOUS_MATCH(agroup, xgroup) \
279 ((((agroup) == ULMBCS_AMBIGUOUS_SBCS) && \
280 (xgroup) < ULMBCS_DOUBLEOPTGROUP_START) || \
281 (((agroup) == ULMBCS_AMBIGUOUS_MBCS) && \
282 (xgroup) >= ULMBCS_DOUBLEOPTGROUP_START))
283
284
285/* The table & some code to use it: */
286
287
288static const struct _UniLMBCSGrpMap
289{
290 const UChar uniStartRange;
291 const UChar uniEndRange;
292 const ulmbcs_byte_t GrpType;
293} UniLMBCSGrpMap[]
294=
295{
296
297 {0x0001, 0x001F, ULMBCS_GRP_CTRL},
298 {0x0080, 0x009F, ULMBCS_GRP_CTRL},
299 {0x00A0, 0x01CD, ULMBCS_AMBIGUOUS_SBCS},
300 {0x01CE, 0x01CE, ULMBCS_GRP_TW },
301 {0x01CF, 0x02B9, ULMBCS_AMBIGUOUS_SBCS},
302 {0x02BA, 0x02BA, ULMBCS_GRP_CN},
303 {0x02BC, 0x02C8, ULMBCS_AMBIGUOUS_SBCS},
304 {0x02C9, 0x02D0, ULMBCS_AMBIGUOUS_MBCS},
305 {0x02D8, 0x02DD, ULMBCS_AMBIGUOUS_SBCS},
306 {0x0384, 0x03CE, ULMBCS_AMBIGUOUS_SBCS},
307 {0x0400, 0x044E, ULMBCS_GRP_RU},
308 {0x044F, 0x044F, ULMBCS_AMBIGUOUS_MBCS},
309 {0x0450, 0x0491, ULMBCS_GRP_RU},
310 {0x05B0, 0x05F2, ULMBCS_GRP_HE},
311 {0x060C, 0x06AF, ULMBCS_GRP_AR},
312 {0x0E01, 0x0E5B, ULMBCS_GRP_TH},
313 {0x200C, 0x200F, ULMBCS_AMBIGUOUS_SBCS},
314 {0x2010, 0x2010, ULMBCS_AMBIGUOUS_MBCS},
315 {0x2013, 0x2015, ULMBCS_AMBIGUOUS_SBCS},
316 {0x2016, 0x2016, ULMBCS_AMBIGUOUS_MBCS},
317 {0x2017, 0x2024, ULMBCS_AMBIGUOUS_SBCS},
318 {0x2025, 0x2025, ULMBCS_AMBIGUOUS_MBCS},
319 {0x2026, 0x2026, ULMBCS_AMBIGUOUS_SBCS},
320 {0x2027, 0x2027, ULMBCS_GRP_CN},
321 {0x2030, 0x2033, ULMBCS_AMBIGUOUS_SBCS},
322 {0x2035, 0x2035, ULMBCS_AMBIGUOUS_MBCS},
323 {0x2039, 0x203A, ULMBCS_AMBIGUOUS_SBCS},
324 {0x203B, 0x203B, ULMBCS_AMBIGUOUS_MBCS},
325 {0x2074, 0x2074, ULMBCS_GRP_KO},
326 {0x207F, 0x207F, ULMBCS_GRP_EXCEPT},
327 {0x2081, 0x2084, ULMBCS_GRP_KO},
328 {0x20A4, 0x20AC, ULMBCS_AMBIGUOUS_SBCS},
329 {0x2103, 0x2109, ULMBCS_AMBIGUOUS_MBCS},
330 {0x2111, 0x2126, ULMBCS_AMBIGUOUS_SBCS},
331 {0x212B, 0x212B, ULMBCS_AMBIGUOUS_MBCS},
332 {0x2135, 0x2135, ULMBCS_AMBIGUOUS_SBCS},
333 {0x2153, 0x2154, ULMBCS_GRP_KO},
334 {0x215B, 0x215E, ULMBCS_GRP_EXCEPT},
335 {0x2160, 0x2179, ULMBCS_AMBIGUOUS_MBCS},
336 {0x2190, 0x2195, ULMBCS_GRP_EXCEPT},
337 {0x2196, 0x2199, ULMBCS_AMBIGUOUS_MBCS},
338 {0x21A8, 0x21A8, ULMBCS_GRP_EXCEPT},
339 {0x21B8, 0x21B9, ULMBCS_GRP_CN},
340 {0x21D0, 0x21D5, ULMBCS_GRP_EXCEPT},
341 {0x21E7, 0x21E7, ULMBCS_GRP_CN},
342 {0x2200, 0x220B, ULMBCS_GRP_EXCEPT},
343 {0x220F, 0x2215, ULMBCS_AMBIGUOUS_MBCS},
344 {0x2219, 0x2220, ULMBCS_GRP_EXCEPT},
345 {0x2223, 0x2228, ULMBCS_AMBIGUOUS_MBCS},
346 {0x2229, 0x222B, ULMBCS_GRP_EXCEPT},
347 {0x222C, 0x223D, ULMBCS_AMBIGUOUS_MBCS},
348 {0x2245, 0x2248, ULMBCS_GRP_EXCEPT},
349 {0x224C, 0x224C, ULMBCS_GRP_TW},
350 {0x2252, 0x2252, ULMBCS_AMBIGUOUS_MBCS},
351 {0x2260, 0x2265, ULMBCS_GRP_EXCEPT},
352 {0x2266, 0x226F, ULMBCS_AMBIGUOUS_MBCS},
353 {0x2282, 0x2297, ULMBCS_GRP_EXCEPT},
354 {0x2299, 0x22BF, ULMBCS_AMBIGUOUS_MBCS},
355 {0x22C0, 0x22C0, ULMBCS_GRP_EXCEPT},
356 {0x2310, 0x2310, ULMBCS_GRP_EXCEPT},
357 {0x2312, 0x2312, ULMBCS_AMBIGUOUS_MBCS},
358 {0x2318, 0x2321, ULMBCS_GRP_EXCEPT},
359 {0x2318, 0x2321, ULMBCS_GRP_CN},
360 {0x2460, 0x24E9, ULMBCS_AMBIGUOUS_MBCS},
361 {0x2500, 0x2500, ULMBCS_AMBIGUOUS_SBCS},
362 {0x2501, 0x2501, ULMBCS_AMBIGUOUS_MBCS},
363 {0x2502, 0x2502, ULMBCS_AMBIGUOUS_SBCS},
364 {0x2503, 0x2503, ULMBCS_AMBIGUOUS_MBCS},
365 {0x2504, 0x2505, ULMBCS_GRP_TW},
366 {0x2506, 0x2665, ULMBCS_AMBIGUOUS_MBCS},
367 {0x2666, 0x2666, ULMBCS_GRP_EXCEPT},
368 {0x2666, 0x2666, ULMBCS_GRP_EXCEPT},
369 {0x2667, 0x2E7F, ULMBCS_AMBIGUOUS_SBCS},
370 {0x2E80, 0xF861, ULMBCS_AMBIGUOUS_MBCS},
371 {0xF862, 0xF8FF, ULMBCS_GRP_EXCEPT},
372 {0xF900, 0xFA2D, ULMBCS_AMBIGUOUS_MBCS},
373 {0xFB00, 0xFEFF, ULMBCS_AMBIGUOUS_SBCS},
374 {0xFF01, 0xFFEE, ULMBCS_AMBIGUOUS_MBCS},
375 {0xFFFF, 0xFFFF, ULMBCS_GRP_UNICODE}
376};
377
378static ulmbcs_byte_t
379FindLMBCSUniRange(UChar uniChar)
380{
381 const struct _UniLMBCSGrpMap * pTable = UniLMBCSGrpMap;
382
383 while (uniChar > pTable->uniEndRange)
384 {
385 pTable++;
386 }
387
388 if (uniChar >= pTable->uniStartRange)
389 {
390 return pTable->GrpType;
391 }
392 return ULMBCS_GRP_UNICODE;
393}
394
395/*
396We also ask the creator of a converter to send in a preferred locale
397that we can use in resolving ambiguous mappings. They send the locale
398in as a string, and we map it, if possible, to one of the
399LMBCS groups. We use this table, and the associated code, to
400do the lookup: */
401
402/**************************************************
403 This table maps locale ID's to LMBCS opt groups.
404 The default return is group 0x01. Note that for
405 performance reasons, the table is sorted in
406 increasing alphabetic order, with the notable
407 exception of zhTW. This is to force the check
408 for Traditonal Chinese before dropping back to
409 Simplified.
410
411 Note too that the Latin-1 groups have been
412 commented out because it's the default, and
413 this shortens the table, allowing a serial
414 search to go quickly.
415 *************************************************/
416
417static const struct _LocaleLMBCSGrpMap
418{
419 const char *LocaleID;
420 const ulmbcs_byte_t OptGroup;
421} LocaleLMBCSGrpMap[] =
422{
423 {"ar", ULMBCS_GRP_AR},
424 {"be", ULMBCS_GRP_RU},
425 {"bg", ULMBCS_GRP_L2},
426 /* {"ca", ULMBCS_GRP_L1}, */
427 {"cs", ULMBCS_GRP_L2},
428 /* {"da", ULMBCS_GRP_L1}, */
429 /* {"de", ULMBCS_GRP_L1}, */
430 {"el", ULMBCS_GRP_GR},
431 /* {"en", ULMBCS_GRP_L1}, */
432 /* {"es", ULMBCS_GRP_L1}, */
433 /* {"et", ULMBCS_GRP_L1}, */
434 /* {"fi", ULMBCS_GRP_L1}, */
435 /* {"fr", ULMBCS_GRP_L1}, */
436 {"he", ULMBCS_GRP_HE},
437 {"hu", ULMBCS_GRP_L2},
438 /* {"is", ULMBCS_GRP_L1}, */
439 /* {"it", ULMBCS_GRP_L1}, */
440 {"iw", ULMBCS_GRP_HE},
441 {"ja", ULMBCS_GRP_JA},
442 {"ko", ULMBCS_GRP_KO},
443 /* {"lt", ULMBCS_GRP_L1}, */
444 /* {"lv", ULMBCS_GRP_L1}, */
445 {"mk", ULMBCS_GRP_RU},
446 /* {"nl", ULMBCS_GRP_L1}, */
447 /* {"no", ULMBCS_GRP_L1}, */
448 {"pl", ULMBCS_GRP_L2},
449 /* {"pt", ULMBCS_GRP_L1}, */
450 {"ro", ULMBCS_GRP_L2},
451 {"ru", ULMBCS_GRP_RU},
452 {"sh", ULMBCS_GRP_L2},
453 {"sk", ULMBCS_GRP_L2},
454 {"sl", ULMBCS_GRP_L2},
455 {"sq", ULMBCS_GRP_L2},
456 {"sr", ULMBCS_GRP_RU},
457 /* {"sv", ULMBCS_GRP_L1}, */
458 {"th", ULMBCS_GRP_TH},
459 {"tr", ULMBCS_GRP_TR},
460 {"uk", ULMBCS_GRP_RU},
461 /* {"vi", ULMBCS_GRP_L1}, */
462 {"zhTW", ULMBCS_GRP_TW},
463 {"zh", ULMBCS_GRP_CN},
464 {NULL, ULMBCS_GRP_L1}
465};
466
467
468static ulmbcs_byte_t
469FindLMBCSLocale(const char *LocaleID)
470{
471 const struct _LocaleLMBCSGrpMap *pTable = LocaleLMBCSGrpMap;
472
473 if ((!LocaleID) || (!*LocaleID))
474 {
475 return 0;
476 }
477
478 while (pTable->LocaleID)
479 {
480 if (*pTable->LocaleID == *LocaleID) /* Check only first char for speed */
481 {
482 /* First char matches - check whole name, for entry-length */
483 if (strncmp(pTable->LocaleID, LocaleID, strlen(pTable->LocaleID)) == 0)
484 return pTable->OptGroup;
485 }
486 else
487 if (*pTable->LocaleID > *LocaleID) /* Sorted alphabetically - exit */
488 break;
489 pTable++;
490 }
491 return ULMBCS_GRP_L1;
492}
493
494
495/*
496 Before we get to the main body of code, here's how we hook up to the rest
497 of ICU. ICU converters are required to define a structure that includes
498 some function pointers, and some common data, in the style of a C++
499 vtable. There is also room in there for converter-specific data. LMBCS
500 uses that converter-specific data to keep track of the 12 subconverters
501 we use, the optimization group, and the group (if any) that matches the
502 locale. We have one structure instantiated for each of the 12 possible
503 optimization groups. To avoid typos & to avoid boring the reader, we
504 put the declarations of these structures and functions into macros. To see
505 the definitions of these structures, see unicode\ucnv_bld.h
506*/
507
508
509
510#define DECLARE_LMBCS_DATA(n) \
511static const UConverterImpl _LMBCSImpl##n={\
512 UCNV_LMBCS_##n,\
513 NULL,NULL,\
514 _LMBCSOpen##n,\
515 _LMBCSClose,\
516 NULL,\
517 _LMBCSToUnicodeWithOffsets,\
518 _LMBCSToUnicodeWithOffsets,\
519 _LMBCSFromUnicode,\
520 _LMBCSFromUnicode,\
521 _LMBCSGetNextUChar,\
522 NULL,\
523 NULL,\
524 NULL,\
525 NULL,\
526 ucnv_getCompleteUnicodeSet\
527};\
528static const UConverterStaticData _LMBCSStaticData##n={\
529 sizeof(UConverterStaticData),\
530 "LMBCS-" #n,\
531 0, UCNV_IBM, UCNV_LMBCS_##n, 1, 2,\
532 { 0x3f, 0, 0, 0 },1,FALSE,FALSE,0,0,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} \
533};\
534const UConverterSharedData _LMBCSData##n={\
535 sizeof(UConverterSharedData), ~((uint32_t) 0),\
536 NULL, NULL, &_LMBCSStaticData##n, FALSE, &_LMBCSImpl##n, \
537 0 \
538};
539
540 /* The only function we needed to duplicate 12 times was the 'open'
541function, which will do basically the same thing except set a different
542optimization group. So, we put the common stuff into a worker function,
543and set up another macro to stamp out the 12 open functions:*/
544#define DEFINE_LMBCS_OPEN(n) \
545static void \
546 _LMBCSOpen##n(UConverter* _this,const char* name,const char* locale,uint32_t options,UErrorCode* err) \
547{ _LMBCSOpenWorker(_this, name,locale,options, err, n);}
548
549
550
551/* Here's the open worker & the common close function */
552static void
553_LMBCSOpenWorker(UConverter* _this,
554 const char* name,
555 const char* locale,
556 uint32_t options,
557 UErrorCode* err,
558 ulmbcs_byte_t OptGroup
559 )
560{
561 UConverterDataLMBCS * extraInfo = (UConverterDataLMBCS*)uprv_malloc (sizeof (UConverterDataLMBCS));
562 if(extraInfo != NULL)
563 {
564 ulmbcs_byte_t i;
565 ulmbcs_byte_t imax;
566 imax = sizeof(extraInfo->OptGrpConverter)/sizeof(extraInfo->OptGrpConverter[0]);
567
568 for (i=0; i < imax; i++)
569 {
570 extraInfo->OptGrpConverter[i] =
571 (OptGroupByteToCPName[i] != NULL) ?
572 ucnv_open(OptGroupByteToCPName[i], err) : NULL;
573 }
574 extraInfo->OptGroup = OptGroup;
575 extraInfo->localeConverterIndex = FindLMBCSLocale(locale);
576 }
577 else
578 {
579 *err = U_MEMORY_ALLOCATION_ERROR;
580 }
581 _this->extraInfo = extraInfo;
582}
583
584static void
585_LMBCSClose(UConverter * _this)
586{
587 if (_this->extraInfo != NULL && !_this->isExtraLocal)
588 {
589 ulmbcs_byte_t Ix;
590 UConverterDataLMBCS * extraInfo = (UConverterDataLMBCS *) _this->extraInfo;
591
592 for (Ix=0; Ix < ULMBCS_GRP_UNICODE; Ix++)
593 {
594 if (extraInfo->OptGrpConverter[Ix] != NULL)
595 ucnv_close (extraInfo->OptGrpConverter[Ix]);
596 }
597 uprv_free (_this->extraInfo);
598 }
599}
600
601/*
602Here's an all-crash stop for debugging, since ICU does not have asserts.
603Turn this on by defining LMBCS_DEBUG, or by changing it to
604#if 1
605*/
606#if LMBCS_DEBUG
607#define MyAssert(b) {if (!(b)) {*(char *)0 = 1;}}
608#else
609#define MyAssert(b)
610#endif
611
612/*
613 Here's the basic helper function that we use when converting from
614 Unicode to LMBCS, and we suspect that a Unicode character will fit into
615 one of the 12 groups. The return value is the number of bytes written
616 starting at pStartLMBCS (if any).
617*/
618
619static size_t
620LMBCSConversionWorker (
621 UConverterDataLMBCS * extraInfo, /* subconverters, opt & locale groups */
622 ulmbcs_byte_t group, /* The group to try */
623 ulmbcs_byte_t * pStartLMBCS, /* where to put the results */
624 UChar * pUniChar, /* The input unicode character */
625 ulmbcs_byte_t * lastConverterIndex, /* output: track last successful group used */
626 UBool * groups_tried /* output: track any unsuccessful groups */
627)
628{
629 ulmbcs_byte_t * pLMBCS = pStartLMBCS;
630 UConverter * xcnv = extraInfo->OptGrpConverter[group];
631
632 int bytesConverted;
633 uint32_t value;
634 ulmbcs_byte_t firstByte;
635
636 MyAssert(xcnv);
637 MyAssert(group<ULMBCS_GRP_UNICODE);
638
639 bytesConverted = _MBCSFromUChar32(xcnv->sharedData, *pUniChar, &value, FALSE);
640
641 /* get the first result byte */
642 switch(bytesConverted)
643 {
644 case 4:
645 firstByte = (ulmbcs_byte_t)(value >> 24);
646 break;
647 case 3:
648 firstByte = (ulmbcs_byte_t)(value >> 16);
649 break;
650 case 2:
651 firstByte = (ulmbcs_byte_t)(value >> 8);
652 break;
653 case 1:
654 firstByte = (ulmbcs_byte_t)value;
655 break;
656 default:
657 /* most common failure mode is an unassigned character */
658 groups_tried[group] = TRUE;
659 return 0;
660 }
661
662 *lastConverterIndex = group;
663
664 /* All initial byte values in lower ascii range should have been caught by now,
665 except with the exception group.
666 */
667 MyAssert((firstByte <= ULMBCS_C0END) || (firstByte >= ULMBCS_C1START) || (group == ULMBCS_GRP_EXCEPT));
668
669 /* use converted data: first write 0, 1 or two group bytes */
670 if (group != ULMBCS_GRP_EXCEPT && extraInfo->OptGroup != group)
671 {
672 *pLMBCS++ = group;
673 if (bytesConverted == 1 && group >= ULMBCS_DOUBLEOPTGROUP_START)
674 {
675 *pLMBCS++ = group;
676 }
677 }
678
679 /* don't emit control chars */
680 if ( bytesConverted == 1 && firstByte < 0x20 )
681 return 0;
682
683
684 /* then move over the converted data */
685 switch(bytesConverted)
686 {
687 case 4:
688 *pLMBCS++ = (ulmbcs_byte_t)(value >> 24);
689 case 3:
690 *pLMBCS++ = (ulmbcs_byte_t)(value >> 16);
691 case 2:
692 *pLMBCS++ = (ulmbcs_byte_t)(value >> 8);
693 case 1:
694 *pLMBCS++ = (ulmbcs_byte_t)value;
695 default:
696 /* will never occur */
697 break;
698 }
699
700 return (pLMBCS - pStartLMBCS);
701}
702
703
704/* This is a much simpler version of above, when we
705know we are writing LMBCS using the Unicode group
706*/
707static size_t
708LMBCSConvertUni(ulmbcs_byte_t * pLMBCS, UChar uniChar)
709{
710 /* encode into LMBCS Unicode range */
711 uint8_t LowCh = (uint8_t)(uniChar & 0x00FF);
712 uint8_t HighCh = (uint8_t)(uniChar >> 8);
713
714 *pLMBCS++ = ULMBCS_GRP_UNICODE;
715
716 if (LowCh == 0)
717 {
718 *pLMBCS++ = ULMBCS_UNICOMPATZERO;
719 *pLMBCS++ = HighCh;
720 }
721 else
722 {
723 *pLMBCS++ = HighCh;
724 *pLMBCS++ = LowCh;
725 }
726 return ULMBCS_UNICODE_SIZE;
727}
728
729
730
731/* The main Unicode to LMBCS conversion function */
732static void
733_LMBCSFromUnicode(UConverterFromUnicodeArgs* args,
734 UErrorCode* err)
735{
736 ulmbcs_byte_t lastConverterIndex = 0;
737 UChar uniChar;
738 ulmbcs_byte_t LMBCS[ULMBCS_CHARSIZE_MAX];
739 ulmbcs_byte_t * pLMBCS;
740 int bytes_written;
741 UBool groups_tried[ULMBCS_GRP_LAST+1];
742 UConverterDataLMBCS * extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
743 int sourceIndex = 0;
744
745
746 /* Basic strategy: attempt to fill in local LMBCS 1-char buffer.(LMBCS)
747 If that succeeds, see if it will all fit into the target & copy it over
748 if it does.
749
750 We try conversions in the following order:
751
752 1. Single-byte ascii & special fixed control chars (&null)
753 2. Look up group in table & try that (could be
754 A) Unicode group
755 B) control group,
756 C) national encoding,
757 or ambiguous SBCS or MBCS group (on to step 4...)
758
759 3. If its ambiguous, try this order:
760 A) The optimization group
761 B) The locale group
762 C) The last group that succeeded with this string.
763 D) every other group that's relevent (single or double)
764 E) If its single-byte ambiguous, try the exceptions group
765
766 4. And as a grand fallback: Unicode
767 */
768
769 while (args->source < args->sourceLimit && !U_FAILURE(*err))
770 {
771 if (args->target >= args->targetLimit)
772 {
773 *err = U_BUFFER_OVERFLOW_ERROR;
774 break;
775 }
776 uniChar = *(args->source);
777 bytes_written = 0;
778 pLMBCS = LMBCS;
779
780 /* check cases in rough order of how common they are, for speed */
781
782 /* single byte matches: strategy 1 */
783
784 if (((uniChar > ULMBCS_C0END) && (uniChar < ULMBCS_C1START)) ||
785 uniChar == 0 || uniChar == ULMBCS_HT || uniChar == ULMBCS_CR ||
786 uniChar == ULMBCS_LF || uniChar == ULMBCS_123SYSTEMRANGE
787 )
788 {
789 *pLMBCS++ = (ulmbcs_byte_t ) uniChar;
790 bytes_written = 1;
791 }
792
793
794 if (!bytes_written)
795 {
796 /* Check by UNICODE range (Strategy 2) */
797 ulmbcs_byte_t group = FindLMBCSUniRange(uniChar);
798
799 if (group == ULMBCS_GRP_UNICODE) /* (Strategy 2A) */
800 {
801 pLMBCS += LMBCSConvertUni(pLMBCS,uniChar);
802
803 bytes_written = pLMBCS - LMBCS;
804 }
805 else if (group == ULMBCS_GRP_CTRL) /* (Strategy 2B) */
806 {
807 /* Handle control characters here */
808 if (uniChar <= ULMBCS_C0END)
809 {
810 *pLMBCS++ = ULMBCS_GRP_CTRL;
811 *pLMBCS++ = (ulmbcs_byte_t)(ULMBCS_CTRLOFFSET + uniChar);
812 }
813 else if (uniChar >= ULMBCS_C1START && uniChar <= ULMBCS_C1START + ULMBCS_CTRLOFFSET)
814 {
815 *pLMBCS++ = ULMBCS_GRP_CTRL;
816 *pLMBCS++ = (ulmbcs_byte_t ) (uniChar & 0x00FF);
817 }
818 bytes_written = pLMBCS - LMBCS;
819 }
820 else if (group < ULMBCS_GRP_UNICODE) /* (Strategy 2C) */
821 {
822 /* a specific converter has been identified - use it */
823 bytes_written = LMBCSConversionWorker (
824 extraInfo, group, pLMBCS, &uniChar,
825 &lastConverterIndex, groups_tried);
826 }
827 if (!bytes_written) /* the ambiguous group cases (Strategy 3) */
828 {
829 memset(groups_tried, 0, sizeof(groups_tried));
830
831 /* check for non-default optimization group (Strategy 3A )*/
832 if (extraInfo->OptGroup != 1
833 && ULMBCS_AMBIGUOUS_MATCH(group, extraInfo->OptGroup))
834 {
835 bytes_written = LMBCSConversionWorker (extraInfo,
836 extraInfo->OptGroup, pLMBCS, &uniChar,
837 &lastConverterIndex, groups_tried);
838 }
839 /* check for locale optimization group (Strategy 3B) */
840 if (!bytes_written
841 && (extraInfo->localeConverterIndex)
842 && (ULMBCS_AMBIGUOUS_MATCH(group, extraInfo->localeConverterIndex)))
843 {
844 bytes_written = LMBCSConversionWorker (extraInfo,
845 extraInfo->localeConverterIndex, pLMBCS, &uniChar,
846 &lastConverterIndex, groups_tried);
847 }
848 /* check for last optimization group used for this string (Strategy 3C) */
849 if (!bytes_written
850 && (lastConverterIndex)
851 && (ULMBCS_AMBIGUOUS_MATCH(group, lastConverterIndex)))
852 {
853 bytes_written = LMBCSConversionWorker (extraInfo,
854 lastConverterIndex, pLMBCS, &uniChar,
855 &lastConverterIndex, groups_tried);
856
857 }
858 if (!bytes_written)
859 {
860 /* just check every possible matching converter (Strategy 3D) */
861 ulmbcs_byte_t grp_start;
862 ulmbcs_byte_t grp_end;
863 ulmbcs_byte_t grp_ix;
864 grp_start = (ulmbcs_byte_t)((group == ULMBCS_AMBIGUOUS_MBCS)
865 ? ULMBCS_DOUBLEOPTGROUP_START
866 : ULMBCS_GRP_L1);
867 grp_end = (ulmbcs_byte_t)((group == ULMBCS_AMBIGUOUS_MBCS)
868 ? ULMBCS_GRP_LAST
869 : ULMBCS_GRP_TH);
870 for (grp_ix = grp_start;
871 grp_ix <= grp_end && !bytes_written;
872 grp_ix++)
873 {
874 if (extraInfo->OptGrpConverter [grp_ix] && !groups_tried [grp_ix])
875 {
876 bytes_written = LMBCSConversionWorker (extraInfo,
877 grp_ix, pLMBCS, &uniChar,
878 &lastConverterIndex, groups_tried);
879 }
880 }
881 /* a final conversion fallback to the exceptions group if its likely
882 to be single byte (Strategy 3E) */
883 if (!bytes_written && grp_start == ULMBCS_GRP_L1)
884 {
885 bytes_written = LMBCSConversionWorker (extraInfo,
886 ULMBCS_GRP_EXCEPT, pLMBCS, &uniChar,
887 &lastConverterIndex, groups_tried);
888 }
889 }
890 /* all of our other strategies failed. Fallback to Unicode. (Strategy 4)*/
891 if (!bytes_written)
892 {
893
894 pLMBCS += LMBCSConvertUni(pLMBCS, uniChar);
895 bytes_written = pLMBCS - LMBCS;
896 }
897 }
898 }
899
900 /* we have a translation. increment source and write as much as posible to target */
901 args->source++;
902 pLMBCS = LMBCS;
903 while (args->target < args->targetLimit && bytes_written--)
904 {
905 *(args->target)++ = *pLMBCS++;
906 if (args->offsets)
907 {
908 *(args->offsets)++ = sourceIndex;
909 }
910 }
911 sourceIndex++;
912 if (bytes_written > 0)
913 {
914 /* write any bytes that didn't fit in target to the error buffer,
915 common code will move this to target if we get called back with
916 enough target room
917 */
918 uint8_t * pErrorBuffer = args->converter->charErrorBuffer;
919 *err = U_BUFFER_OVERFLOW_ERROR;
920 args->converter->charErrorBufferLength = (int8_t)bytes_written;
921 while (bytes_written--)
922 {
923 *pErrorBuffer++ = *pLMBCS++;
924 }
925 }
926 }
927}
928
929
930/* Now, the Unicode from LMBCS section */
931
932
933/*
934 Special codes for the getNextUnicodeWorker -- usually as the result of
935 special error-callback behavior:
936 ULMBCS_SKIP To control skipping over LMBCS sequences
937 ULMBCS_MULTI To indicate that a single LMBCS char translates to
938 multiple uniChars
939*/
940#define ULMBCS_SKIP U_ERROR_LIMIT
941#define ULMBCS_MULTI ULMBCS_SKIP+1
942
943/* A function to call when we are looking at the Unicode group byte in LMBCS */
944static UChar
945GetUniFromLMBCSUni(char const ** ppLMBCSin) /* Called with LMBCS-style Unicode byte stream */
946{
947 uint8_t HighCh = *(*ppLMBCSin)++; /* Big-endian Unicode in LMBCS compatibility group*/
948 uint8_t LowCh = *(*ppLMBCSin)++;
949
950 if (HighCh == ULMBCS_UNICOMPATZERO )
951 {
952 HighCh = LowCh;
953 LowCh = 0; /* zero-byte in LSB special character */
954 }
955 return (UChar)((HighCh << 8) | LowCh);
956}
957
958
959
960/* CHECK_SOURCE_LIMIT: Helper macro to verify that there are at least'index'
961 bytes left in source up to sourceLimit.Errors appropriately if not
962*/
963
964#define CHECK_SOURCE_LIMIT(index) \
965 if (args->source+index > args->sourceLimit){\
966 *err = U_TRUNCATED_CHAR_FOUND;\
967 args->source = saveSource;\
968 return 0xffff;}
969
970/* Return the Unicode representation for the current LMBCS character
971
972 This worker function is used by both ucnv_getNextUChar() and ucnv_ToUnicode().
973 The last parameter says whether the return value should be treated as UTF-16 or
974 UTF-32. The only difference is in surrogate handling
975*/
976
977static UChar32
978_LMBCSGetNextUCharWorker(UConverterToUnicodeArgs* args,
979 UErrorCode* err,
980 UBool returnUTF32)
981{
982 UChar32 uniChar = 0; /* an output UNICODE char */
983 ulmbcs_byte_t CurByte; /* A byte from the input stream */
984 const char * saveSource;
985
986 /* error check */
987 if (args->source >= args->sourceLimit)
988 {
989 *err = U_ILLEGAL_ARGUMENT_ERROR;
990 return 0xffff;
991 }
992 /* Grab first byte & save address for error recovery */
993 CurByte = *((ulmbcs_byte_t *) (saveSource = args->source++));
994
995 /*
996 * at entry of each if clause:
997 * 1. 'CurByte' points at the first byte of a LMBCS character
998 * 2. '*source'points to the next byte of the source stream after 'CurByte'
999 *
1000 * the job of each if clause is:
1001 * 1. set '*source' to point at the beginning of next char (nop if LMBCS char is only 1 byte)
1002 * 2. set 'uniChar' up with the right Unicode value, or set 'err' appropriately
1003 */
1004
1005 /* First lets check the simple fixed values. */
1006
1007 if(((CurByte > ULMBCS_C0END) && (CurByte < ULMBCS_C1START)) /* ascii range */
1008 || (CurByte == 0)
1009 || CurByte == ULMBCS_HT || CurByte == ULMBCS_CR
1010 || CurByte == ULMBCS_LF || CurByte == ULMBCS_123SYSTEMRANGE)
1011 {
1012 uniChar = CurByte;
1013 }
1014 else
1015 {
1016 UConverterDataLMBCS * extraInfo;
1017 ulmbcs_byte_t group;
1018 UConverter* cnv;
1019
1020 if (CurByte == ULMBCS_GRP_CTRL) /* Control character group - no opt group update */
1021 {
1022 ulmbcs_byte_t C0C1byte;
1023 CHECK_SOURCE_LIMIT(1);
1024 C0C1byte = *(args->source)++;
1025 uniChar = (C0C1byte < ULMBCS_C1START) ? C0C1byte - ULMBCS_CTRLOFFSET : C0C1byte;
1026 }
1027 else
1028 if (CurByte == ULMBCS_GRP_UNICODE) /* Unicode compatibility group: BigEndian UTF16 */
1029 {
1030 UChar second;
1031 CHECK_SOURCE_LIMIT(2);
1032
1033 uniChar = GetUniFromLMBCSUni(&(args->source));
1034
1035 /* at this point we are usually done, but we need to make sure we are not in
1036 a situation where we can successfully put together a surrogate pair */
1037
1038 if(returnUTF32 && UTF_IS_FIRST_SURROGATE(uniChar) && (args->source+3 <= args->sourceLimit)
1039 && *(args->source)++ == ULMBCS_GRP_UNICODE
1040 && UTF_IS_SECOND_SURROGATE(second = GetUniFromLMBCSUni(&(args->source))))
1041 {
1042 uniChar = UTF16_GET_PAIR_VALUE(uniChar, second);
1043 }
1044 }
1045 else if (CurByte <= ULMBCS_CTRLOFFSET)
1046 {
1047 group = CurByte; /* group byte is in the source */
1048 extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
1049 cnv = extraInfo->OptGrpConverter[group];
1050 if (!cnv)
1051 {
1052 /* this is not a valid group byte - no converter*/
1053 *err = U_INVALID_CHAR_FOUND;
1054 }
1055 else if (group >= ULMBCS_DOUBLEOPTGROUP_START) /* double byte conversion */
1056 {
1057
1058 CHECK_SOURCE_LIMIT(2);
1059
1060 /* check for LMBCS doubled-group-byte case */
1061 if (*args->source == group) {
1062 /* single byte */
1063 ++args->source;
1064 uniChar = _MBCSSimpleGetNextUChar(cnv->sharedData, &args->source, args->source + 1, FALSE);
1065 } else {
1066 /* double byte */
1067 const char *newLimit = args->source + 2;
1068 uniChar = _MBCSSimpleGetNextUChar(cnv->sharedData, &args->source, newLimit, FALSE);
1069 args->source = newLimit; /* set the correct limit even in case of an error */
1070 }
1071 }
1072 else { /* single byte conversion */
1073 CHECK_SOURCE_LIMIT(1);
1074 CurByte = *(args->source)++;
1075
1076 if (CurByte >= ULMBCS_C1START)
1077 {
1078 uniChar = _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(cnv->sharedData, CurByte);
1079 }
1080 else
1081 {
1082 /* The non-optimizable oddballs where there is an explicit byte
1083 * AND the second byte is not in the upper ascii range
1084 */
1085 const char *s;
1086 char bytes[2];
1087
1088 extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
1089 cnv = extraInfo->OptGrpConverter [ULMBCS_GRP_EXCEPT];
1090
1091 /* Lookup value must include opt group */
1092 bytes[0] = group;
1093 bytes[1] = CurByte;
1094 s = bytes;
1095 uniChar = _MBCSSimpleGetNextUChar(cnv->sharedData, &s, bytes + 2, FALSE);
1096 }
1097 }
1098 }
1099 else if (CurByte >= ULMBCS_C1START) /* group byte is implicit */
1100 {
1101 extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
1102 group = extraInfo->OptGroup;
1103 cnv = extraInfo->OptGrpConverter[group];
1104 if (group >= ULMBCS_DOUBLEOPTGROUP_START) /* double byte conversion */
1105 {
1106 if (!_MBCSIsLeadByte(cnv->sharedData, CurByte))
1107 {
1108 CHECK_SOURCE_LIMIT(0);
1109
1110 /* let the MBCS conversion consume CurByte again */
1111 --args->source;
1112 uniChar = _MBCSSimpleGetNextUChar(cnv->sharedData, &args->source, args->source + 1, FALSE);
1113 }
1114 else
1115 {
1116 CHECK_SOURCE_LIMIT(1);
1117 /* let the MBCS conversion consume CurByte again */
1118 --args->source;
1119 /* since we know that we start at a lead byte, args->source _will_ be incremented by 2 */
1120 uniChar = _MBCSSimpleGetNextUChar(cnv->sharedData, &args->source, args->source + 2, FALSE);
1121 }
1122 }
1123 else /* single byte conversion */
1124 {
1125 uniChar = _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(cnv->sharedData, CurByte);
1126 }
1127 }
1128 }
1129 if (((uint32_t)uniChar - 0xfffe) <= 1) /* 0xfffe<=uniChar<=0xffff */
1130 {
1131 UConverterToUnicodeArgs cbArgs = *args;
1132 UConverterCallbackReason reason;
1133 UChar UCh;
1134
1135 if (uniChar == 0xfffe)
1136 {
1137 reason = UCNV_UNASSIGNED;
1138 *err = U_INVALID_CHAR_FOUND;
1139 }
1140 else
1141 {
1142 reason = UCNV_ILLEGAL;
1143 *err = U_ILLEGAL_CHAR_FOUND;
1144 }
1145
1146 cbArgs.target = &UCh;
1147 cbArgs.targetLimit = &UCh + 1;
1148 cbArgs.converter->fromCharErrorBehaviour(cbArgs.converter->toUContext,
1149 &cbArgs,
1150 saveSource,
1151 args->source - saveSource,
1152 reason,
1153 err);
1154
1155 if (cbArgs.target != &UCh)
1156 {
1157 uniChar = (UChar32) UCh;
1158 }
1159 /* Did error functor skip */
1160 if (U_SUCCESS(*err) && cbArgs.target == &UCh)
1161 {
1162 *err = ULMBCS_SKIP;
1163 }
1164 /* Did error functor try to write multiple UChars? */
1165 else if (*err == U_BUFFER_OVERFLOW_ERROR)
1166 {
1167 *err = ULMBCS_MULTI;
1168 }
1169 }
1170 return uniChar;
1171}
1172
1173
1174/* The exported function that gets one UTF32 character from a LMBCS stream
1175*/
1176static UChar32
1177_LMBCSGetNextUChar(UConverterToUnicodeArgs* args,
1178 UErrorCode* err)
1179{
1180 UChar32 nextUChar;
1181 do {
1182 nextUChar = _LMBCSGetNextUCharWorker(args, err, TRUE);
1183 } while (*err == ULMBCS_SKIP);
1184
1185 if (*err == ULMBCS_MULTI)
1186 {
1187 *err = U_ZERO_ERROR;
1188 }
1189 return nextUChar;
1190}
1191
1192/* The exported function that converts lmbcs to one or more
1193 UChars - currently UTF-16
1194*/
1195static void
1196_LMBCSToUnicodeWithOffsets(UConverterToUnicodeArgs* args,
1197 UErrorCode* err)
1198{
1199 UChar uniChar; /* one output UNICODE char */
1200 const char * saveSource = args->source; /* beginning of current code point */
1201 const char * pStartLMBCS = args->source; /* beginning of whole string */
1202
1203 if (args->targetLimit == args->target) /* error check may belong in common code */
1204 {
1205 *err = U_BUFFER_OVERFLOW_ERROR;
1206 return;
1207 }
1208
1209 /* Process from source to limit, or until error */
1210 while (!*err && args->sourceLimit > args->source && args->targetLimit > args->target)
1211 {
1212 saveSource = args->source; /* beginning of current code point */
1213
1214 if (args->converter->invalidCharLength) /* reassemble char from previous call */
1215 {
1216 char LMBCS [ULMBCS_CHARSIZE_MAX];
1217 const char *pLMBCS = LMBCS, *saveSourceLimit;
1218 size_t size_old = args->converter->invalidCharLength;
1219
1220 /* limit from source is either reminder of temp buffer, or user limit on source */
1221 size_t size_new_maybe_1 = sizeof(LMBCS) - size_old;
1222 size_t size_new_maybe_2 = args->sourceLimit - args->source;
1223 size_t size_new = (size_new_maybe_1 < size_new_maybe_2) ? size_new_maybe_1 : size_new_maybe_2;
1224
1225
1226 uprv_memcpy(LMBCS, args->converter->invalidCharBuffer, size_old);
1227 uprv_memcpy(LMBCS + size_old, args->source, size_new);
1228 saveSourceLimit = args->sourceLimit;
1229 args->source = pLMBCS;
1230 args->sourceLimit = pLMBCS+size_old+size_new;
1231 uniChar = (UChar) _LMBCSGetNextUCharWorker(args, err, FALSE);
1232 pLMBCS = args->source;
1233 args->source =saveSource;
1234 args->sourceLimit = saveSourceLimit;
1235 args->source += (pLMBCS - LMBCS - size_old);
1236
1237 if (*err == U_TRUNCATED_CHAR_FOUND && !args->flush)
1238 {
1239 /* evil special case: source buffers so small a char spans more than 2 buffers */
1240 int8_t savebytes = (int8_t)(size_old+size_new);
1241 args->converter->invalidCharLength = savebytes;
1242 uprv_memcpy(args->converter->invalidCharBuffer, LMBCS, savebytes);
1243 args->source = args->sourceLimit;
1244 *err = U_ZERO_ERROR;
1245 return;
1246 }
1247 else
1248 {
1249 /* clear the partial-char marker */
1250 args->converter->invalidCharLength = 0;
1251 }
1252 }
1253 else
1254 {
1255 uniChar = (UChar) _LMBCSGetNextUCharWorker(args, err, FALSE);
1256 }
1257 if (U_SUCCESS(*err))
1258 {
1259 if (uniChar < 0xfffe)
1260 {
1261 *(args->target)++ = uniChar;
1262 if(args->offsets)
1263 {
1264 *(args->offsets)++ = saveSource - pStartLMBCS;
1265 }
1266 }
1267 else if (uniChar == 0xfffe)
1268 {
1269 *err = U_INVALID_CHAR_FOUND;
1270 }
1271 else /* if (uniChar == 0xffff) */
1272 {
1273 *err = U_ILLEGAL_CHAR_FOUND;
1274 }
1275 }
1276 else if (*err == ULMBCS_MULTI)
1277 {
1278 UChar * pUChar = args->converter->UCharErrorBuffer;
1279 int8_t BufferLength = args->converter->UCharErrorBufferLength;
1280
1281 *err = U_ZERO_ERROR;
1282 do
1283 { /* error functor wants to write multiple UniChars */
1284 *(args->target)++ = uniChar;
1285 if(args->offsets)
1286 {
1287 *(args->offsets)++ = saveSource - pStartLMBCS;
1288 }
1289 uniChar = *pUChar++;
1290 }
1291 while(BufferLength-- && args->targetLimit > args->target);
1292
1293 if (++BufferLength > 0)
1294 { /* fix up remaining UChars that can't fit in caller's buffer */
1295 uprv_memmove( args->converter->UCharErrorBuffer,
1296 args->converter->UCharErrorBuffer + args->converter->UCharErrorBufferLength - BufferLength,
1297 sizeof(UChar) * BufferLength);
1298 }
1299 args->converter->UCharErrorBufferLength = BufferLength;
1300 }
1301 else if (*err == ULMBCS_SKIP)
1302 {
1303 *err = U_ZERO_ERROR; /* and just go around again..*/
1304 }
1305 }
1306 /* if target ran out before source, return U_BUFFER_OVERFLOW_ERROR */
1307 if (U_SUCCESS(*err) && args->sourceLimit > args->source && args->targetLimit <= args->target)
1308 {
1309 *err = U_BUFFER_OVERFLOW_ERROR;
1310 }
1311
1312 /* If character incomplete, store away partial char if more to come */
1313 if (*err == U_TRUNCATED_CHAR_FOUND)
1314 {
1315 args->source = args->sourceLimit;
1316 if (!args->flush )
1317 {
1318 int8_t savebytes = (int8_t)(args->sourceLimit - saveSource);
1319 args->converter->invalidCharLength = (int8_t)savebytes;
1320 uprv_memcpy(args->converter->invalidCharBuffer, saveSource, savebytes);
1321 *err = U_ZERO_ERROR;
1322 }
1323 }
1324}
1325
1326/* And now, the macroized declarations of data & functions: */
1327DEFINE_LMBCS_OPEN(1)
1328DEFINE_LMBCS_OPEN(2)
1329DEFINE_LMBCS_OPEN(3)
1330DEFINE_LMBCS_OPEN(4)
1331DEFINE_LMBCS_OPEN(5)
1332DEFINE_LMBCS_OPEN(6)
1333DEFINE_LMBCS_OPEN(8)
1334DEFINE_LMBCS_OPEN(11)
1335DEFINE_LMBCS_OPEN(16)
1336DEFINE_LMBCS_OPEN(17)
1337DEFINE_LMBCS_OPEN(18)
1338DEFINE_LMBCS_OPEN(19)
1339
1340
1341DECLARE_LMBCS_DATA(1)
1342DECLARE_LMBCS_DATA(2)
1343DECLARE_LMBCS_DATA(3)
1344DECLARE_LMBCS_DATA(4)
1345DECLARE_LMBCS_DATA(5)
1346DECLARE_LMBCS_DATA(6)
1347DECLARE_LMBCS_DATA(8)
1348DECLARE_LMBCS_DATA(11)
1349DECLARE_LMBCS_DATA(16)
1350DECLARE_LMBCS_DATA(17)
1351DECLARE_LMBCS_DATA(18)
1352DECLARE_LMBCS_DATA(19)
1353
1354#endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */