Coverage Report

Created: 2025-07-11 06:23

/src/icu/source/common/ucnvmbcs.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
******************************************************************************
5
*
6
*   Copyright (C) 2000-2016, International Business Machines
7
*   Corporation and others.  All Rights Reserved.
8
*
9
******************************************************************************
10
*   file name:  ucnvmbcs.cpp
11
*   encoding:   UTF-8
12
*   tab size:   8 (not used)
13
*   indentation:4
14
*
15
*   created on: 2000jul03
16
*   created by: Markus W. Scherer
17
*
18
*   The current code in this file replaces the previous implementation
19
*   of conversion code from multi-byte codepages to Unicode and back.
20
*   This implementation supports the following:
21
*   - legacy variable-length codepages with up to 4 bytes per character
22
*   - all Unicode code points (up to 0x10ffff)
23
*   - efficient distinction of unassigned vs. illegal byte sequences
24
*   - it is possible in fromUnicode() to directly deal with simple
25
*     stateful encodings (used for EBCDIC_STATEFUL)
26
*   - it is possible to convert Unicode code points
27
*     to a single zero byte (but not as a fallback except for SBCS)
28
*
29
*   Remaining limitations in fromUnicode:
30
*   - byte sequences must not have leading zero bytes
31
*   - except for SBCS codepages: no fallback mapping from Unicode to a zero byte
32
*   - limitation to up to 4 bytes per character
33
*
34
*   ICU 2.8 (late 2003) adds a secondary data structure which lifts some of these
35
*   limitations and adds m:n character mappings and other features.
36
*   See ucnv_ext.h for details.
37
*
38
*   Change history: 
39
*
40
*    5/6/2001       Ram       Moved  MBCS_SINGLE_RESULT_FROM_U,MBCS_STAGE_2_FROM_U,
41
*                             MBCS_VALUE_2_FROM_STAGE_2, MBCS_VALUE_4_FROM_STAGE_2
42
*                             macros to ucnvmbcs.h file
43
*/
44
45
#include "unicode/utypes.h"
46
47
#if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
48
49
#include "unicode/ucnv.h"
50
#include "unicode/ucnv_cb.h"
51
#include "unicode/udata.h"
52
#include "unicode/uset.h"
53
#include "unicode/utf8.h"
54
#include "unicode/utf16.h"
55
#include "ucnv_bld.h"
56
#include "ucnvmbcs.h"
57
#include "ucnv_ext.h"
58
#include "ucnv_cnv.h"
59
#include "cmemory.h"
60
#include "cstring.h"
61
#include "umutex.h"
62
63
/* control optimizations according to the platform */
64
#define MBCS_UNROLL_SINGLE_TO_BMP 1
65
#define MBCS_UNROLL_SINGLE_FROM_BMP 0
66
67
/*
68
 * _MBCSHeader versions 5.3 & 4.3
69
 * (Note that the _MBCSHeader version is in addition to the converter formatVersion.)
70
 *
71
 * This version is optional. Version 5 is used for incompatible data format changes.
72
 * makeconv will continue to generate version 4 files if possible.
73
 *
74
 * Changes from version 4:
75
 *
76
 * The main difference is an additional _MBCSHeader field with
77
 * - the length (number of uint32_t) of the _MBCSHeader
78
 * - flags for further incompatible data format changes
79
 * - flags for further, backward compatible data format changes
80
 *
81
 * The MBCS_OPT_FROM_U flag indicates that most of the fromUnicode data is omitted from
82
 * the file and needs to be reconstituted at load time.
83
 * This requires a utf8Friendly format with an additional mbcsIndex table for fast
84
 * (and UTF-8-friendly) fromUnicode conversion for Unicode code points up to maxFastUChar.
85
 * (For details about these structures see below, and see ucnvmbcs.h.)
86
 *
87
 *   utf8Friendly also implies that the fromUnicode mappings are stored in ascending order
88
 *   of the Unicode code points. (This requires that the .ucm file has the |0 etc.
89
 *   precision markers for all mappings.)
90
 *
91
 *   All fallbacks have been moved to the extension table, leaving only roundtrips in the
92
 *   omitted data that can be reconstituted from the toUnicode data.
93
 *
94
 *   Of the stage 2 table, the part corresponding to maxFastUChar and below is omitted.
95
 *   With only roundtrip mappings in the base fromUnicode data, this part is fully
96
 *   redundant with the mbcsIndex and will be reconstituted from that (also using the
97
 *   stage 1 table which contains the information about how stage 2 was compacted).
98
 *
99
 *   The rest of the stage 2 table, the part for code points above maxFastUChar,
100
 *   is stored in the file and will be appended to the reconstituted part.
101
 *
102
 *   The entire fromUBytes array is omitted from the file and will be reconstitued.
103
 *   This is done by enumerating all toUnicode roundtrip mappings, performing
104
 *   each mapping (using the stage 1 and reconstituted stage 2 tables) and
105
 *   writing instead of reading the byte values.
106
 *
107
 * _MBCSHeader version 4.3
108
 *
109
 * Change from version 4.2:
110
 * - Optional utf8Friendly data structures, with 64-entry stage 3 block
111
 *   allocation for parts of the BMP, and an additional mbcsIndex in non-SBCS
112
 *   files which can be used instead of stages 1 & 2.
113
 *   Faster lookups for roundtrips from most commonly used characters,
114
 *   and lookups from UTF-8 byte sequences with a natural bit distribution.
115
 *   See ucnvmbcs.h for more details.
116
 *
117
 * Change from version 4.1:
118
 * - Added an optional extension table structure at the end of the .cnv file.
119
 *   It is present if the upper bits of the header flags field contains a non-zero
120
 *   byte offset to it.
121
 *   Files that contain only a conversion table and no base table
122
 *   use the special outputType MBCS_OUTPUT_EXT_ONLY.
123
 *   These contain the base table name between the MBCS header and the extension
124
 *   data.
125
 *
126
 * Change from version 4.0:
127
 * - Replace header.reserved with header.fromUBytesLength so that all
128
 *   fields in the data have length.
129
 *
130
 * Changes from version 3 (for performance improvements):
131
 * - new bit distribution for state table entries
132
 * - reordered action codes
133
 * - new data structure for single-byte fromUnicode
134
 *   + stage 2 only contains indexes
135
 *   + stage 3 stores 16 bits per character with classification bits 15..8
136
 * - no multiplier for stage 1 entries
137
 * - stage 2 for non-single-byte codepages contains the index and the flags in
138
 *   one 32-bit value
139
 * - 2-byte and 4-byte fromUnicode results are stored directly as 16/32-bit integers
140
 *
141
 * For more details about old versions of the MBCS data structure, see
142
 * the corresponding versions of this file.
143
 *
144
 * Converting stateless codepage data ---------------------------------------***
145
 * (or codepage data with simple states) to Unicode.
146
 *
147
 * Data structure and algorithm for converting from complex legacy codepages
148
 * to Unicode. (Designed before 2000-may-22.)
149
 *
150
 * The basic idea is that the structure of legacy codepages can be described
151
 * with state tables.
152
 * When reading a byte stream, each input byte causes a state transition.
153
 * Some transitions result in the output of a code point, some result in
154
 * "unassigned" or "illegal" output.
155
 * This is used here for character conversion.
156
 *
157
 * The data structure begins with a state table consisting of a row
158
 * per state, with 256 entries (columns) per row for each possible input
159
 * byte value.
160
 * Each entry is 32 bits wide, with two formats distinguished by
161
 * the sign bit (bit 31):
162
 *
163
 * One format for transitional entries (bit 31 not set) for non-final bytes, and
164
 * one format for final entries (bit 31 set).
165
 * Both formats contain the number of the next state in the same bit
166
 * positions.
167
 * State 0 is the initial state.
168
 *
169
 * Most of the time, the offset values of subsequent states are added
170
 * up to a scalar value. This value will eventually be the index of
171
 * the Unicode code point in a table that follows the state table.
172
 * The effect is that the code points for final state table rows
173
 * are contiguous. The code points of final state rows follow each other
174
 * in the order of the references to those final states by previous
175
 * states, etc.
176
 *
177
 * For some terminal states, the offset is itself the output Unicode
178
 * code point (16 bits for a BMP code point or 20 bits for a supplementary
179
 * code point (stored as code point minus 0x10000 so that 20 bits are enough).
180
 * For others, the code point in the Unicode table is stored with either
181
 * one or two code units: one for BMP code points, two for a pair of
182
 * surrogates.
183
 * All code points for a final state entry take up the same number of code
184
 * units, regardless of whether they all actually _use_ the same number
185
 * of code units. This is necessary for simple array access.
186
 *
187
 * An additional feature comes in with what in ICU is called "fallback"
188
 * mappings:
189
 *
190
 * In addition to round-trippable, precise, 1:1 mappings, there are often
191
 * mappings defined between similar, though not the same, characters.
192
 * Typically, such mappings occur only in fromUnicode mapping tables because
193
 * Unicode has a superset repertoire of most other codepages. However, it
194
 * is possible to provide such mappings in the toUnicode tables, too.
195
 * In this case, the fallback mappings are partly integrated into the
196
 * general state tables because the structure of the encoding includes their
197
 * byte sequences.
198
 * For final entries in an initial state, fallback mappings are stored in
199
 * the entry itself like with roundtrip mappings.
200
 * For other final entries, they are stored in the code units table if
201
 * the entry is for a pair of code units.
202
 * For single-unit results in the code units table, there is no space to
203
 * alternatively hold a fallback mapping; in this case, the code unit
204
 * is stored as U+fffe (unassigned), and the fallback mapping needs to
205
 * be looked up by the scalar offset value in a separate table.
206
 *
207
 * "Unassigned" state entries really mean "structurally unassigned",
208
 * i.e., such a byte sequence will never have a mapping result.
209
 *
210
 * The interpretation of the bits in each entry is as follows:
211
 *
212
 * Bit 31 not set, not a terminal entry ("transitional"):
213
 * 30..24 next state
214
 * 23..0  offset delta, to be added up
215
 *
216
 * Bit 31 set, terminal ("final") entry:
217
 * 30..24 next state (regardless of action code)
218
 * 23..20 action code:
219
 *        action codes 0 and 1 result in precise-mapping Unicode code points
220
 *        0  valid byte sequence
221
 *           19..16 not used, 0
222
 *           15..0  16-bit Unicode BMP code point
223
 *                  never U+fffe or U+ffff
224
 *        1  valid byte sequence
225
 *           19..0  20-bit Unicode supplementary code point
226
 *                  never U+fffe or U+ffff
227
 *
228
 *        action codes 2 and 3 result in fallback (unidirectional-mapping) Unicode code points
229
 *        2  valid byte sequence (fallback)
230
 *           19..16 not used, 0
231
 *           15..0  16-bit Unicode BMP code point as fallback result
232
 *        3  valid byte sequence (fallback)
233
 *           19..0  20-bit Unicode supplementary code point as fallback result
234
 *
235
 *        action codes 4 and 5 may result in roundtrip/fallback/unassigned/illegal results
236
 *        depending on the code units they result in
237
 *        4  valid byte sequence
238
 *           19..9  not used, 0
239
 *            8..0  final offset delta
240
 *                  pointing to one 16-bit code unit which may be
241
 *                  fffe  unassigned -- look for a fallback for this offset
242
 *                  ffff  illegal
243
 *        5  valid byte sequence
244
 *           19..9  not used, 0
245
 *            8..0  final offset delta
246
 *                  pointing to two 16-bit code units
247
 *                  (typically UTF-16 surrogates)
248
 *                  the result depends on the first code unit as follows:
249
 *                  0000..d7ff  roundtrip BMP code point (1st alone)
250
 *                  d800..dbff  roundtrip surrogate pair (1st, 2nd)
251
 *                  dc00..dfff  fallback surrogate pair (1st-400, 2nd)
252
 *                  e000        roundtrip BMP code point (2nd alone)
253
 *                  e001        fallback BMP code point (2nd alone)
254
 *                  fffe        unassigned
255
 *                  ffff        illegal
256
 *           (the final offset deltas are at most 255 * 2,
257
 *            times 2 because of storing code unit pairs)
258
 *
259
 *        6  unassigned byte sequence
260
 *           19..16 not used, 0
261
 *           15..0  16-bit Unicode BMP code point U+fffe (new with version 2)
262
 *                  this does not contain a final offset delta because the main
263
 *                  purpose of this action code is to save scalar offset values;
264
 *                  therefore, fallback values cannot be assigned to byte
265
 *                  sequences that result in this action code
266
 *        7  illegal byte sequence
267
 *           19..16 not used, 0
268
 *           15..0  16-bit Unicode BMP code point U+ffff (new with version 2)
269
 *        8  state change only
270
 *           19..0  not used, 0
271
 *           useful for state changes in simple stateful encodings,
272
 *           at Shift-In/Shift-Out codes
273
 *
274
 *
275
 *        9..15 reserved for future use
276
 *           current implementations will only perform a state change
277
 *           and ignore bits 19..0
278
 *
279
 * An encoding with contiguous ranges of unassigned byte sequences, like
280
 * Shift-JIS and especially EUC-TW, can be stored efficiently by having
281
 * at least two states for the trail bytes:
282
 * One trail byte state that results in code points, and one that only
283
 * has "unassigned" and "illegal" terminal states.
284
 *
285
 * Note: partly by accident, this data structure supports simple stateful
286
 * encodings without any additional logic.
287
 * Currently, only simple Shift-In/Shift-Out schemes are handled with
288
 * appropriate state tables (especially EBCDIC_STATEFUL!).
289
 *
290
 * MBCS version 2 added:
291
 * unassigned and illegal action codes have U+fffe and U+ffff
292
 * instead of unused bits; this is useful for _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP()
293
 *
294
 * Converting from Unicode to codepage bytes --------------------------------***
295
 *
296
 * The conversion data structure for fromUnicode is designed for the known
297
 * structure of Unicode. It maps from 21-bit code points (0..0x10ffff) to
298
 * a sequence of 1..4 bytes, in addition to a flag that indicates if there is
299
 * a roundtrip mapping.
300
 *
301
 * The lookup is done with a 3-stage trie, using 11/6/4 bits for stage 1/2/3
302
 * like in the character properties table.
303
 * The beginning of the trie is at offsetFromUTable, the beginning of stage 3
304
 * with the resulting bytes is at offsetFromUBytes.
305
 *
306
 * Beginning with version 4, single-byte codepages have a significantly different
307
 * trie compared to other codepages.
308
 * In all cases, the entry in stage 1 is directly the index of the block of
309
 * 64 entries in stage 2.
310
 *
311
 * Single-byte lookup:
312
 *
313
 * Stage 2 only contains 16-bit indexes directly to the 16-blocks in stage 3.
314
 * Stage 3 contains one 16-bit word per result:
315
 * Bits 15..8 indicate the kind of result:
316
 *    f  roundtrip result
317
 *    c  fallback result from private-use code point
318
 *    8  fallback result from other code points
319
 *    0  unassigned
320
 * Bits 7..0 contain the codepage byte. A zero byte is always possible.
321
 *
322
 * In version 4.3, the runtime code can build an sbcsIndex for a utf8Friendly
323
 * file. For 2-byte UTF-8 byte sequences and some 3-byte sequences the lookup
324
 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
325
 * ASCII code points can be looked up with a linear array access into stage 3.
326
 * See maxFastUChar and other details in ucnvmbcs.h.
327
 *
328
 * Multi-byte lookup:
329
 *
330
 * Stage 2 contains a 32-bit word for each 16-block in stage 3:
331
 * Bits 31..16 contain flags for which stage 3 entries contain roundtrip results
332
 *             test: MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)
333
 *             If this test is false, then a non-zero result will be interpreted as
334
 *             a fallback mapping.
335
 * Bits 15..0  contain the index to stage 3, which must be multiplied by 16*(bytes per char)
336
 *
337
 * Stage 3 contains 2, 3, or 4 bytes per result.
338
 * 2 or 4 bytes are stored as uint16_t/uint32_t in platform endianness,
339
 * while 3 bytes are stored as bytes in big-endian order.
340
 * Leading zero bytes are ignored, and the number of bytes is counted.
341
 * A zero byte mapping result is possible as a roundtrip result.
342
 * For some output types, the actual result is processed from this;
343
 * see ucnv_MBCSFromUnicodeWithOffsets().
344
 *
345
 * Note that stage 1 always contains 0x440=1088 entries (0x440==0x110000>>10),
346
 * or (version 3 and up) for BMP-only codepages, it contains 64 entries.
347
 *
348
 * In version 4.3, a utf8Friendly file contains an mbcsIndex table.
349
 * For 2-byte UTF-8 byte sequences and most 3-byte sequences the lookup
350
 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
351
 * ASCII code points can be looked up with a linear array access into stage 3.
352
 * See maxFastUChar, mbcsIndex and other details in ucnvmbcs.h.
353
 *
354
 * In version 3, stage 2 blocks may overlap by multiples of the multiplier
355
 * for compaction.
356
 * In version 4, stage 2 blocks (and for single-byte codepages, stage 3 blocks)
357
 * may overlap by any number of entries.
358
 *
359
 * MBCS version 2 added:
360
 * the converter checks for known output types, which allows
361
 * adding new ones without crashing an unaware converter
362
 */
363
364
/**
365
 * Callback from ucnv_MBCSEnumToUnicode(), takes 32 mappings from
366
 * consecutive sequences of bytes, starting from the one encoded in value,
367
 * to Unicode code points. (Multiple mappings to reduce per-function call overhead.)
368
 * Does not currently support m:n mappings or reverse fallbacks.
369
 * This function will not be called for sequences of bytes with leading zeros.
370
 *
371
 * @param context an opaque pointer, as passed into ucnv_MBCSEnumToUnicode()
372
 * @param value contains 1..4 bytes of the first byte sequence, right-aligned
373
 * @param codePoints resulting Unicode code points, or negative if a byte sequence does
374
 *        not map to anything
375
 * @return TRUE to continue enumeration, FALSE to stop
376
 */
377
typedef UBool U_CALLCONV
378
UConverterEnumToUCallback(const void *context, uint32_t value, UChar32 codePoints[32]);
379
380
static void U_CALLCONV
381
ucnv_MBCSLoad(UConverterSharedData *sharedData,
382
          UConverterLoadArgs *pArgs,
383
          const uint8_t *raw,
384
          UErrorCode *pErrorCode);
385
386
static void U_CALLCONV
387
ucnv_MBCSUnload(UConverterSharedData *sharedData);
388
389
static void U_CALLCONV
390
ucnv_MBCSOpen(UConverter *cnv,
391
              UConverterLoadArgs *pArgs,
392
              UErrorCode *pErrorCode);
393
394
static UChar32 U_CALLCONV
395
ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
396
                  UErrorCode *pErrorCode);
397
398
static void U_CALLCONV
399
ucnv_MBCSGetStarters(const UConverter* cnv,
400
                 UBool starters[256],
401
                 UErrorCode *pErrorCode);
402
403
U_CDECL_BEGIN
404
static const char* U_CALLCONV
405
ucnv_MBCSGetName(const UConverter *cnv);
406
U_CDECL_END
407
408
static void U_CALLCONV
409
ucnv_MBCSWriteSub(UConverterFromUnicodeArgs *pArgs,
410
              int32_t offsetIndex,
411
              UErrorCode *pErrorCode);
412
413
static UChar32 U_CALLCONV
414
ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
415
                  UErrorCode *pErrorCode);
416
417
static void U_CALLCONV
418
ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
419
                  UConverterToUnicodeArgs *pToUArgs,
420
                  UErrorCode *pErrorCode);
421
422
static void U_CALLCONV
423
ucnv_MBCSGetUnicodeSet(const UConverter *cnv,
424
                   const USetAdder *sa,
425
                   UConverterUnicodeSet which,
426
                   UErrorCode *pErrorCode);
427
428
static void U_CALLCONV
429
ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
430
                  UConverterToUnicodeArgs *pToUArgs,
431
                  UErrorCode *pErrorCode);
432
433
static const UConverterImpl _SBCSUTF8Impl={
434
    UCNV_MBCS,
435
436
    ucnv_MBCSLoad,
437
    ucnv_MBCSUnload,
438
439
    ucnv_MBCSOpen,
440
    NULL,
441
    NULL,
442
443
    ucnv_MBCSToUnicodeWithOffsets,
444
    ucnv_MBCSToUnicodeWithOffsets,
445
    ucnv_MBCSFromUnicodeWithOffsets,
446
    ucnv_MBCSFromUnicodeWithOffsets,
447
    ucnv_MBCSGetNextUChar,
448
449
    ucnv_MBCSGetStarters,
450
    ucnv_MBCSGetName,
451
    ucnv_MBCSWriteSub,
452
    NULL,
453
    ucnv_MBCSGetUnicodeSet,
454
455
    NULL,
456
    ucnv_SBCSFromUTF8
457
};
458
459
static const UConverterImpl _DBCSUTF8Impl={
460
    UCNV_MBCS,
461
462
    ucnv_MBCSLoad,
463
    ucnv_MBCSUnload,
464
465
    ucnv_MBCSOpen,
466
    NULL,
467
    NULL,
468
469
    ucnv_MBCSToUnicodeWithOffsets,
470
    ucnv_MBCSToUnicodeWithOffsets,
471
    ucnv_MBCSFromUnicodeWithOffsets,
472
    ucnv_MBCSFromUnicodeWithOffsets,
473
    ucnv_MBCSGetNextUChar,
474
475
    ucnv_MBCSGetStarters,
476
    ucnv_MBCSGetName,
477
    ucnv_MBCSWriteSub,
478
    NULL,
479
    ucnv_MBCSGetUnicodeSet,
480
481
    NULL,
482
    ucnv_DBCSFromUTF8
483
};
484
485
static const UConverterImpl _MBCSImpl={
486
    UCNV_MBCS,
487
488
    ucnv_MBCSLoad,
489
    ucnv_MBCSUnload,
490
491
    ucnv_MBCSOpen,
492
    NULL,
493
    NULL,
494
495
    ucnv_MBCSToUnicodeWithOffsets,
496
    ucnv_MBCSToUnicodeWithOffsets,
497
    ucnv_MBCSFromUnicodeWithOffsets,
498
    ucnv_MBCSFromUnicodeWithOffsets,
499
    ucnv_MBCSGetNextUChar,
500
501
    ucnv_MBCSGetStarters,
502
    ucnv_MBCSGetName,
503
    ucnv_MBCSWriteSub,
504
    NULL,
505
    ucnv_MBCSGetUnicodeSet,
506
    NULL,
507
    NULL
508
};
509
510
/* Static data is in tools/makeconv/ucnvstat.c for data-based
511
 * converters. Be sure to update it as well.
512
 */
513
514
const UConverterSharedData _MBCSData={
515
    sizeof(UConverterSharedData), 1,
516
    NULL, NULL, FALSE, TRUE, &_MBCSImpl,
517
    0, UCNV_MBCS_TABLE_INITIALIZER
518
};
519
520
521
/* GB 18030 data ------------------------------------------------------------ */
522
523
/* helper macros for linear values for GB 18030 four-byte sequences */
524
0
#define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
525
526
0
#define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
527
528
#define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
529
530
/*
531
 * Some ranges of GB 18030 where both the Unicode code points and the
532
 * GB four-byte sequences are contiguous and are handled algorithmically by
533
 * the special callback functions below.
534
 * The values are start & end of Unicode & GB codes.
535
 *
536
 * Note that single surrogates are not mapped by GB 18030
537
 * as of the re-released mapping tables from 2000-nov-30.
538
 */
539
static const uint32_t
540
gb18030Ranges[14][4]={
541
    {0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
542
    {0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
543
    {0x0452, 0x1E3E, LINEAR(0x8130D330), LINEAR(0x8135F436)},
544
    {0x1E40, 0x200F, LINEAR(0x8135F438), LINEAR(0x8136A531)},
545
    {0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
546
    {0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
547
    {0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
548
    {0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
549
    {0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
550
    {0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
551
    {0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
552
    {0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
553
    {0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
554
    {0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
555
};
556
557
/* bit flag for UConverter.options indicating GB 18030 special handling */
558
0
#define _MBCS_OPTION_GB18030 0x8000
559
560
/* bit flag for UConverter.options indicating KEIS,JEF,JIF special handling */
561
0
#define _MBCS_OPTION_KEIS 0x01000
562
0
#define _MBCS_OPTION_JEF  0x02000
563
0
#define _MBCS_OPTION_JIPS 0x04000
564
565
0
#define KEIS_SO_CHAR_1 0x0A
566
0
#define KEIS_SO_CHAR_2 0x42
567
0
#define KEIS_SI_CHAR_1 0x0A
568
0
#define KEIS_SI_CHAR_2 0x41
569
570
0
#define JEF_SO_CHAR 0x28
571
0
#define JEF_SI_CHAR 0x29
572
573
0
#define JIPS_SO_CHAR_1 0x1A
574
0
#define JIPS_SO_CHAR_2 0x70
575
0
#define JIPS_SI_CHAR_1 0x1A
576
0
#define JIPS_SI_CHAR_2 0x71
577
578
enum SISO_Option {
579
    SI,
580
    SO
581
};
582
typedef enum SISO_Option SISO_Option;
583
584
0
static int32_t getSISOBytes(SISO_Option option, uint32_t cnvOption, uint8_t *value) {
585
0
    int32_t SISOLength = 0;
586
587
0
    switch (option) {
588
0
        case SI:
589
0
            if ((cnvOption&_MBCS_OPTION_KEIS)!=0) {
590
0
                value[0] = KEIS_SI_CHAR_1;
591
0
                value[1] = KEIS_SI_CHAR_2;
592
0
                SISOLength = 2;
593
0
            } else if ((cnvOption&_MBCS_OPTION_JEF)!=0) {
594
0
                value[0] = JEF_SI_CHAR;
595
0
                SISOLength = 1;
596
0
            } else if ((cnvOption&_MBCS_OPTION_JIPS)!=0) {
597
0
                value[0] = JIPS_SI_CHAR_1;
598
0
                value[1] = JIPS_SI_CHAR_2;
599
0
                SISOLength = 2;
600
0
            } else {
601
0
                value[0] = UCNV_SI;
602
0
                SISOLength = 1;
603
0
            }
604
0
            break;
605
0
        case SO:
606
0
            if ((cnvOption&_MBCS_OPTION_KEIS)!=0) {
607
0
                value[0] = KEIS_SO_CHAR_1;
608
0
                value[1] = KEIS_SO_CHAR_2;
609
0
                SISOLength = 2;
610
0
            } else if ((cnvOption&_MBCS_OPTION_JEF)!=0) {
611
0
                value[0] = JEF_SO_CHAR;
612
0
                SISOLength = 1;
613
0
            } else if ((cnvOption&_MBCS_OPTION_JIPS)!=0) {
614
0
                value[0] = JIPS_SO_CHAR_1;
615
0
                value[1] = JIPS_SO_CHAR_2;
616
0
                SISOLength = 2;
617
0
            } else {
618
0
                value[0] = UCNV_SO;
619
0
                SISOLength = 1;
620
0
            }
621
0
            break;
622
0
        default:
623
            /* Should never happen. */
624
0
            break;
625
0
    }
626
627
0
    return SISOLength;
628
0
}
629
630
/* Miscellaneous ------------------------------------------------------------ */
631
632
/* similar to ucnv_MBCSGetNextUChar() but recursive */
633
static UBool
634
enumToU(UConverterMBCSTable *mbcsTable, int8_t stateProps[],
635
        int32_t state, uint32_t offset,
636
        uint32_t value,
637
        UConverterEnumToUCallback *callback, const void *context,
638
0
        UErrorCode *pErrorCode) {
639
0
    UChar32 codePoints[32];
640
0
    const int32_t *row;
641
0
    const uint16_t *unicodeCodeUnits;
642
0
    UChar32 anyCodePoints;
643
0
    int32_t b, limit;
644
645
0
    row=mbcsTable->stateTable[state];
646
0
    unicodeCodeUnits=mbcsTable->unicodeCodeUnits;
647
648
0
    value<<=8;
649
0
    anyCodePoints=-1;  /* becomes non-negative if there is a mapping */
650
651
0
    b=(stateProps[state]&0x38)<<2;
652
0
    if(b==0 && stateProps[state]>=0x40) {
653
        /* skip byte sequences with leading zeros because they are not stored in the fromUnicode table */
654
0
        codePoints[0]=U_SENTINEL;
655
0
        b=1;
656
0
    }
657
0
    limit=((stateProps[state]&7)+1)<<5;
658
0
    while(b<limit) {
659
0
        int32_t entry=row[b];
660
0
        if(MBCS_ENTRY_IS_TRANSITION(entry)) {
661
0
            int32_t nextState=MBCS_ENTRY_TRANSITION_STATE(entry);
662
0
            if(stateProps[nextState]>=0) {
663
                /* recurse to a state with non-ignorable actions */
664
0
                if(!enumToU(
665
0
                        mbcsTable, stateProps, nextState,
666
0
                        offset+MBCS_ENTRY_TRANSITION_OFFSET(entry),
667
0
                        value|(uint32_t)b,
668
0
                        callback, context,
669
0
                        pErrorCode)) {
670
0
                    return FALSE;
671
0
                }
672
0
            }
673
0
            codePoints[b&0x1f]=U_SENTINEL;
674
0
        } else {
675
0
            UChar32 c;
676
0
            int32_t action;
677
678
            /*
679
             * An if-else-if chain provides more reliable performance for
680
             * the most common cases compared to a switch.
681
             */
682
0
            action=MBCS_ENTRY_FINAL_ACTION(entry);
683
0
            if(action==MBCS_STATE_VALID_DIRECT_16) {
684
                /* output BMP code point */
685
0
                c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
686
0
            } else if(action==MBCS_STATE_VALID_16) {
687
0
                int32_t finalOffset=offset+MBCS_ENTRY_FINAL_VALUE_16(entry);
688
0
                c=unicodeCodeUnits[finalOffset];
689
0
                if(c<0xfffe) {
690
                    /* output BMP code point */
691
0
                } else {
692
0
                    c=U_SENTINEL;
693
0
                }
694
0
            } else if(action==MBCS_STATE_VALID_16_PAIR) {
695
0
                int32_t finalOffset=offset+MBCS_ENTRY_FINAL_VALUE_16(entry);
696
0
                c=unicodeCodeUnits[finalOffset++];
697
0
                if(c<0xd800) {
698
                    /* output BMP code point below 0xd800 */
699
0
                } else if(c<=0xdbff) {
700
                    /* output roundtrip or fallback supplementary code point */
701
0
                    c=((c&0x3ff)<<10)+unicodeCodeUnits[finalOffset]+(0x10000-0xdc00);
702
0
                } else if(c==0xe000) {
703
                    /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
704
0
                    c=unicodeCodeUnits[finalOffset];
705
0
                } else {
706
0
                    c=U_SENTINEL;
707
0
                }
708
0
            } else if(action==MBCS_STATE_VALID_DIRECT_20) {
709
                /* output supplementary code point */
710
0
                c=(UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
711
0
            } else {
712
0
                c=U_SENTINEL;
713
0
            }
714
715
0
            codePoints[b&0x1f]=c;
716
0
            anyCodePoints&=c;
717
0
        }
718
0
        if(((++b)&0x1f)==0) {
719
0
            if(anyCodePoints>=0) {
720
0
                if(!callback(context, value|(uint32_t)(b-0x20), codePoints)) {
721
0
                    return FALSE;
722
0
                }
723
0
                anyCodePoints=-1;
724
0
            }
725
0
        }
726
0
    }
727
0
    return TRUE;
728
0
}
729
730
/*
731
 * Only called if stateProps[state]==-1.
732
 * A recursive call may do stateProps[state]|=0x40 if this state is the target of an
733
 * MBCS_STATE_CHANGE_ONLY.
734
 */
735
static int8_t
736
0
getStateProp(const int32_t (*stateTable)[256], int8_t stateProps[], int state) {
737
0
    const int32_t *row;
738
0
    int32_t min, max, entry, nextState;
739
740
0
    row=stateTable[state];
741
0
    stateProps[state]=0;
742
743
    /* find first non-ignorable state */
744
0
    for(min=0;; ++min) {
745
0
        entry=row[min];
746
0
        nextState=MBCS_ENTRY_STATE(entry);
747
0
        if(stateProps[nextState]==-1) {
748
0
            getStateProp(stateTable, stateProps, nextState);
749
0
        }
750
0
        if(MBCS_ENTRY_IS_TRANSITION(entry)) {
751
0
            if(stateProps[nextState]>=0) {
752
0
                break;
753
0
            }
754
0
        } else if(MBCS_ENTRY_FINAL_ACTION(entry)<MBCS_STATE_UNASSIGNED) {
755
0
            break;
756
0
        }
757
0
        if(min==0xff) {
758
0
            stateProps[state]=-0x40;  /* (int8_t)0xc0 */
759
0
            return stateProps[state];
760
0
        }
761
0
    }
762
0
    stateProps[state]|=(int8_t)((min>>5)<<3);
763
764
    /* find last non-ignorable state */
765
0
    for(max=0xff; min<max; --max) {
766
0
        entry=row[max];
767
0
        nextState=MBCS_ENTRY_STATE(entry);
768
0
        if(stateProps[nextState]==-1) {
769
0
            getStateProp(stateTable, stateProps, nextState);
770
0
        }
771
0
        if(MBCS_ENTRY_IS_TRANSITION(entry)) {
772
0
            if(stateProps[nextState]>=0) {
773
0
                break;
774
0
            }
775
0
        } else if(MBCS_ENTRY_FINAL_ACTION(entry)<MBCS_STATE_UNASSIGNED) {
776
0
            break;
777
0
        }
778
0
    }
779
0
    stateProps[state]|=(int8_t)(max>>5);
780
781
    /* recurse further and collect direct-state information */
782
0
    while(min<=max) {
783
0
        entry=row[min];
784
0
        nextState=MBCS_ENTRY_STATE(entry);
785
0
        if(stateProps[nextState]==-1) {
786
0
            getStateProp(stateTable, stateProps, nextState);
787
0
        }
788
0
        if(MBCS_ENTRY_IS_FINAL(entry)) {
789
0
            stateProps[nextState]|=0x40;
790
0
            if(MBCS_ENTRY_FINAL_ACTION(entry)<=MBCS_STATE_FALLBACK_DIRECT_20) {
791
0
                stateProps[state]|=0x40;
792
0
            }
793
0
        }
794
0
        ++min;
795
0
    }
796
0
    return stateProps[state];
797
0
}
798
799
/*
800
 * Internal function enumerating the toUnicode data of an MBCS converter.
801
 * Currently only used for reconstituting data for a MBCS_OPT_NO_FROM_U
802
 * table, but could also be used for a future ucnv_getUnicodeSet() option
803
 * that includes reverse fallbacks (after updating this function's implementation).
804
 * Currently only handles roundtrip mappings.
805
 * Does not currently handle extensions.
806
 */
807
static void
808
ucnv_MBCSEnumToUnicode(UConverterMBCSTable *mbcsTable,
809
                       UConverterEnumToUCallback *callback, const void *context,
810
0
                       UErrorCode *pErrorCode) {
811
    /*
812
     * Properties for each state, to speed up the enumeration.
813
     * Ignorable actions are unassigned/illegal/state-change-only:
814
     * They do not lead to mappings.
815
     *
816
     * Bits 7..6:
817
     * 1 direct/initial state (stateful converters have multiple)
818
     * 0 non-initial state with transitions or with non-ignorable result actions
819
     * -1 final state with only ignorable actions
820
     *
821
     * Bits 5..3:
822
     * The lowest byte value with non-ignorable actions is
823
     * value<<5 (rounded down).
824
     *
825
     * Bits 2..0:
826
     * The highest byte value with non-ignorable actions is
827
     * (value<<5)&0x1f (rounded up).
828
     */
829
0
    int8_t stateProps[MBCS_MAX_STATE_COUNT];
830
0
    int32_t state;
831
832
0
    uprv_memset(stateProps, -1, sizeof(stateProps));
833
834
    /* recurse from state 0 and set all stateProps */
835
0
    getStateProp(mbcsTable->stateTable, stateProps, 0);
836
837
0
    for(state=0; state<mbcsTable->countStates; ++state) {
838
        /*if(stateProps[state]==-1) {
839
            printf("unused/unreachable <icu:state> %d\n", state);
840
        }*/
841
0
        if(stateProps[state]>=0x40) {
842
            /* start from each direct state */
843
0
            enumToU(
844
0
                mbcsTable, stateProps, state, 0, 0,
845
0
                callback, context,
846
0
                pErrorCode);
847
0
        }
848
0
    }
849
0
}
850
851
U_CFUNC void 
852
ucnv_MBCSGetFilteredUnicodeSetForUnicode(const UConverterSharedData *sharedData,
853
                                         const USetAdder *sa,
854
                                         UConverterUnicodeSet which,
855
                                         UConverterSetFilter filter,
856
0
                                         UErrorCode *pErrorCode) {
857
0
    const UConverterMBCSTable *mbcsTable;
858
0
    const uint16_t *table;
859
860
0
    uint32_t st3;
861
0
    uint16_t st1, maxStage1, st2;
862
863
0
    UChar32 c;
864
865
    /* enumerate the from-Unicode trie table */
866
0
    mbcsTable=&sharedData->mbcs;
867
0
    table=mbcsTable->fromUnicodeTable;
868
0
    if(mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
869
0
        maxStage1=0x440;
870
0
    } else {
871
0
        maxStage1=0x40;
872
0
    }
873
874
0
    c=0; /* keep track of the current code point while enumerating */
875
876
0
    if(mbcsTable->outputType==MBCS_OUTPUT_1) {
877
0
        const uint16_t *stage2, *stage3, *results;
878
0
        uint16_t minValue;
879
880
0
        results=(const uint16_t *)mbcsTable->fromUnicodeBytes;
881
882
        /*
883
         * Set a threshold variable for selecting which mappings to use.
884
         * See ucnv_MBCSSingleFromBMPWithOffsets() and
885
         * MBCS_SINGLE_RESULT_FROM_U() for details.
886
         */
887
0
        if(which==UCNV_ROUNDTRIP_SET) {
888
            /* use only roundtrips */
889
0
            minValue=0xf00;
890
0
        } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ {
891
            /* use all roundtrip and fallback results */
892
0
            minValue=0x800;
893
0
        }
894
895
0
        for(st1=0; st1<maxStage1; ++st1) {
896
0
            st2=table[st1];
897
0
            if(st2>maxStage1) {
898
0
                stage2=table+st2;
899
0
                for(st2=0; st2<64; ++st2) {
900
0
                    if((st3=stage2[st2])!=0) {
901
                        /* read the stage 3 block */
902
0
                        stage3=results+st3;
903
904
0
                        do {
905
0
                            if(*stage3++>=minValue) {
906
0
                                sa->add(sa->set, c);
907
0
                            }
908
0
                        } while((++c&0xf)!=0);
909
0
                    } else {
910
0
                        c+=16; /* empty stage 3 block */
911
0
                    }
912
0
                }
913
0
            } else {
914
0
                c+=1024; /* empty stage 2 block */
915
0
            }
916
0
        }
917
0
    } else {
918
0
        const uint32_t *stage2;
919
0
        const uint8_t *stage3, *bytes;
920
0
        uint32_t st3Multiplier;
921
0
        uint32_t value;
922
0
        UBool useFallback;
923
924
0
        bytes=mbcsTable->fromUnicodeBytes;
925
926
0
        useFallback=(UBool)(which==UCNV_ROUNDTRIP_AND_FALLBACK_SET);
927
928
0
        switch(mbcsTable->outputType) {
929
0
        case MBCS_OUTPUT_3:
930
0
        case MBCS_OUTPUT_4_EUC:
931
0
            st3Multiplier=3;
932
0
            break;
933
0
        case MBCS_OUTPUT_4:
934
0
            st3Multiplier=4;
935
0
            break;
936
0
        default:
937
0
            st3Multiplier=2;
938
0
            break;
939
0
        }
940
941
0
        for(st1=0; st1<maxStage1; ++st1) {
942
0
            st2=table[st1];
943
0
            if(st2>(maxStage1>>1)) {
944
0
                stage2=(const uint32_t *)table+st2;
945
0
                for(st2=0; st2<64; ++st2) {
946
0
                    if((st3=stage2[st2])!=0) {
947
                        /* read the stage 3 block */
948
0
                        stage3=bytes+st3Multiplier*16*(uint32_t)(uint16_t)st3;
949
950
                        /* get the roundtrip flags for the stage 3 block */
951
0
                        st3>>=16;
952
953
                        /*
954
                         * Add code points for which the roundtrip flag is set,
955
                         * or which map to non-zero bytes if we use fallbacks.
956
                         * See ucnv_MBCSFromUnicodeWithOffsets() for details.
957
                         */
958
0
                        switch(filter) {
959
0
                        case UCNV_SET_FILTER_NONE:
960
0
                            do {
961
0
                                if(st3&1) {
962
0
                                    sa->add(sa->set, c);
963
0
                                    stage3+=st3Multiplier;
964
0
                                } else if(useFallback) {
965
0
                                    uint8_t b=0;
966
0
                                    switch(st3Multiplier) {
967
0
                                    case 4:
968
0
                                        b|=*stage3++;
969
0
                                        U_FALLTHROUGH;
970
0
                                    case 3:
971
0
                                        b|=*stage3++;
972
0
                                        U_FALLTHROUGH;
973
0
                                    case 2:
974
0
                                        b|=stage3[0]|stage3[1];
975
0
                                        stage3+=2;
976
0
                                        U_FALLTHROUGH;
977
0
                                    default:
978
0
                                        break;
979
0
                                    }
980
0
                                    if(b!=0) {
981
0
                                        sa->add(sa->set, c);
982
0
                                    }
983
0
                                }
984
0
                                st3>>=1;
985
0
                            } while((++c&0xf)!=0);
986
0
                            break;
987
0
                        case UCNV_SET_FILTER_DBCS_ONLY:
988
                             /* Ignore single-byte results (<0x100). */
989
0
                            do {
990
0
                                if(((st3&1)!=0 || useFallback) && *((const uint16_t *)stage3)>=0x100) {
991
0
                                    sa->add(sa->set, c);
992
0
                                }
993
0
                                st3>>=1;
994
0
                                stage3+=2;  /* +=st3Multiplier */
995
0
                            } while((++c&0xf)!=0);
996
0
                            break;
997
0
                        case UCNV_SET_FILTER_2022_CN:
998
                             /* Only add code points that map to CNS 11643 planes 1 & 2 for non-EXT ISO-2022-CN. */
999
0
                            do {
1000
0
                                if(((st3&1)!=0 || useFallback) && ((value=*stage3)==0x81 || value==0x82)) {
1001
0
                                    sa->add(sa->set, c);
1002
0
                                }
1003
0
                                st3>>=1;
1004
0
                                stage3+=3;  /* +=st3Multiplier */
1005
0
                            } while((++c&0xf)!=0);
1006
0
                            break;
1007
0
                        case UCNV_SET_FILTER_SJIS:
1008
                             /* Only add code points that map to Shift-JIS codes corresponding to JIS X 0208. */
1009
0
                            do {
1010
0
                                if(((st3&1)!=0 || useFallback) && (value=*((const uint16_t *)stage3))>=0x8140 && value<=0xeffc) {
1011
0
                                    sa->add(sa->set, c);
1012
0
                                }
1013
0
                                st3>>=1;
1014
0
                                stage3+=2;  /* +=st3Multiplier */
1015
0
                            } while((++c&0xf)!=0);
1016
0
                            break;
1017
0
                        case UCNV_SET_FILTER_GR94DBCS:
1018
                            /* Only add code points that map to ISO 2022 GR 94 DBCS codes (each byte A1..FE). */
1019
0
                            do {
1020
0
                                if( ((st3&1)!=0 || useFallback) &&
1021
0
                                    (uint16_t)((value=*((const uint16_t *)stage3)) - 0xa1a1)<=(0xfefe - 0xa1a1) &&
1022
0
                                    (uint8_t)(value-0xa1)<=(0xfe - 0xa1)
1023
0
                                ) {
1024
0
                                    sa->add(sa->set, c);
1025
0
                                }
1026
0
                                st3>>=1;
1027
0
                                stage3+=2;  /* +=st3Multiplier */
1028
0
                            } while((++c&0xf)!=0);
1029
0
                            break;
1030
0
                        case UCNV_SET_FILTER_HZ:
1031
                            /* Only add code points that are suitable for HZ DBCS (lead byte A1..FD). */
1032
0
                            do {
1033
0
                                if( ((st3&1)!=0 || useFallback) &&
1034
0
                                    (uint16_t)((value=*((const uint16_t *)stage3))-0xa1a1)<=(0xfdfe - 0xa1a1) &&
1035
0
                                    (uint8_t)(value-0xa1)<=(0xfe - 0xa1)
1036
0
                                ) {
1037
0
                                    sa->add(sa->set, c);
1038
0
                                }
1039
0
                                st3>>=1;
1040
0
                                stage3+=2;  /* +=st3Multiplier */
1041
0
                            } while((++c&0xf)!=0);
1042
0
                            break;
1043
0
                        default:
1044
0
                            *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
1045
0
                            return;
1046
0
                        }
1047
0
                    } else {
1048
0
                        c+=16; /* empty stage 3 block */
1049
0
                    }
1050
0
                }
1051
0
            } else {
1052
0
                c+=1024; /* empty stage 2 block */
1053
0
            }
1054
0
        }
1055
0
    }
1056
1057
0
    ucnv_extGetUnicodeSet(sharedData, sa, which, filter, pErrorCode);
1058
0
}
1059
1060
U_CFUNC void
1061
ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData *sharedData,
1062
                                 const USetAdder *sa,
1063
                                 UConverterUnicodeSet which,
1064
0
                                 UErrorCode *pErrorCode) {
1065
0
    ucnv_MBCSGetFilteredUnicodeSetForUnicode(
1066
0
        sharedData, sa, which,
1067
0
        sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY ?
1068
0
            UCNV_SET_FILTER_DBCS_ONLY :
1069
0
            UCNV_SET_FILTER_NONE,
1070
0
        pErrorCode);
1071
0
}
1072
1073
static void U_CALLCONV
1074
ucnv_MBCSGetUnicodeSet(const UConverter *cnv,
1075
                   const USetAdder *sa,
1076
                   UConverterUnicodeSet which,
1077
0
                   UErrorCode *pErrorCode) {
1078
0
    if(cnv->options&_MBCS_OPTION_GB18030) {
1079
0
        sa->addRange(sa->set, 0, 0xd7ff);
1080
0
        sa->addRange(sa->set, 0xe000, 0x10ffff);
1081
0
    } else {
1082
0
        ucnv_MBCSGetUnicodeSetForUnicode(cnv->sharedData, sa, which, pErrorCode);
1083
0
    }
1084
0
}
1085
1086
/* conversion extensions for input not in the main table -------------------- */
1087
1088
/*
1089
 * Hardcoded extension handling for GB 18030.
1090
 * Definition of LINEAR macros and gb18030Ranges see near the beginning of the file.
1091
 *
1092
 * In the future, conversion extensions may handle m:n mappings and delta tables,
1093
 * see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/conversion/conversion_extensions.html
1094
 *
1095
 * If an input character cannot be mapped, then these functions set an error
1096
 * code. The framework will then call the callback function.
1097
 */
1098
1099
/*
1100
 * @return if(U_FAILURE) return the code point for cnv->fromUChar32
1101
 *         else return 0 after output has been written to the target
1102
 */
1103
static UChar32
1104
_extFromU(UConverter *cnv, const UConverterSharedData *sharedData,
1105
          UChar32 cp,
1106
          const UChar **source, const UChar *sourceLimit,
1107
          uint8_t **target, const uint8_t *targetLimit,
1108
          int32_t **offsets, int32_t sourceIndex,
1109
          UBool flush,
1110
0
          UErrorCode *pErrorCode) {
1111
0
    const int32_t *cx;
1112
1113
0
    cnv->useSubChar1=FALSE;
1114
1115
0
    if( (cx=sharedData->mbcs.extIndexes)!=NULL &&
1116
0
        ucnv_extInitialMatchFromU(
1117
0
            cnv, cx,
1118
0
            cp, source, sourceLimit,
1119
0
            (char **)target, (char *)targetLimit,
1120
0
            offsets, sourceIndex,
1121
0
            flush,
1122
0
            pErrorCode)
1123
0
    ) {
1124
0
        return 0; /* an extension mapping handled the input */
1125
0
    }
1126
1127
    /* GB 18030 */
1128
0
    if((cnv->options&_MBCS_OPTION_GB18030)!=0) {
1129
0
        const uint32_t *range;
1130
0
        int32_t i;
1131
1132
0
        range=gb18030Ranges[0];
1133
0
        for(i=0; i<UPRV_LENGTHOF(gb18030Ranges); range+=4, ++i) {
1134
0
            if(range[0]<=(uint32_t)cp && (uint32_t)cp<=range[1]) {
1135
                /* found the Unicode code point, output the four-byte sequence for it */
1136
0
                uint32_t linear;
1137
0
                char bytes[4];
1138
1139
                /* get the linear value of the first GB 18030 code in this range */
1140
0
                linear=range[2]-LINEAR_18030_BASE;
1141
1142
                /* add the offset from the beginning of the range */
1143
0
                linear+=((uint32_t)cp-range[0]);
1144
1145
                /* turn this into a four-byte sequence */
1146
0
                bytes[3]=(char)(0x30+linear%10); linear/=10;
1147
0
                bytes[2]=(char)(0x81+linear%126); linear/=126;
1148
0
                bytes[1]=(char)(0x30+linear%10); linear/=10;
1149
0
                bytes[0]=(char)(0x81+linear);
1150
1151
                /* output this sequence */
1152
0
                ucnv_fromUWriteBytes(cnv,
1153
0
                                     bytes, 4, (char **)target, (char *)targetLimit,
1154
0
                                     offsets, sourceIndex, pErrorCode);
1155
0
                return 0;
1156
0
            }
1157
0
        }
1158
0
    }
1159
1160
    /* no mapping */
1161
0
    *pErrorCode=U_INVALID_CHAR_FOUND;
1162
0
    return cp;
1163
0
}
1164
1165
/*
1166
 * Input sequence: cnv->toUBytes[0..length[
1167
 * @return if(U_FAILURE) return the length (toULength, byteIndex) for the input
1168
 *         else return 0 after output has been written to the target
1169
 */
1170
static int8_t
1171
_extToU(UConverter *cnv, const UConverterSharedData *sharedData,
1172
        int8_t length,
1173
        const uint8_t **source, const uint8_t *sourceLimit,
1174
        UChar **target, const UChar *targetLimit,
1175
        int32_t **offsets, int32_t sourceIndex,
1176
        UBool flush,
1177
0
        UErrorCode *pErrorCode) {
1178
0
    const int32_t *cx;
1179
1180
0
    if( (cx=sharedData->mbcs.extIndexes)!=NULL &&
1181
0
        ucnv_extInitialMatchToU(
1182
0
            cnv, cx,
1183
0
            length, (const char **)source, (const char *)sourceLimit,
1184
0
            target, targetLimit,
1185
0
            offsets, sourceIndex,
1186
0
            flush,
1187
0
            pErrorCode)
1188
0
    ) {
1189
0
        return 0; /* an extension mapping handled the input */
1190
0
    }
1191
1192
    /* GB 18030 */
1193
0
    if(length==4 && (cnv->options&_MBCS_OPTION_GB18030)!=0) {
1194
0
        const uint32_t *range;
1195
0
        uint32_t linear;
1196
0
        int32_t i;
1197
1198
0
        linear=LINEAR_18030(cnv->toUBytes[0], cnv->toUBytes[1], cnv->toUBytes[2], cnv->toUBytes[3]);
1199
0
        range=gb18030Ranges[0];
1200
0
        for(i=0; i<UPRV_LENGTHOF(gb18030Ranges); range+=4, ++i) {
1201
0
            if(range[2]<=linear && linear<=range[3]) {
1202
                /* found the sequence, output the Unicode code point for it */
1203
0
                *pErrorCode=U_ZERO_ERROR;
1204
1205
                /* add the linear difference between the input and start sequences to the start code point */
1206
0
                linear=range[0]+(linear-range[2]);
1207
1208
                /* output this code point */
1209
0
                ucnv_toUWriteCodePoint(cnv, linear, target, targetLimit, offsets, sourceIndex, pErrorCode);
1210
1211
0
                return 0;
1212
0
            }
1213
0
        }
1214
0
    }
1215
1216
    /* no mapping */
1217
0
    *pErrorCode=U_INVALID_CHAR_FOUND;
1218
0
    return length;
1219
0
}
1220
1221
/* EBCDIC swap LF<->NL ------------------------------------------------------ */
1222
1223
/*
1224
 * This code modifies a standard EBCDIC<->Unicode mapping table for
1225
 * OS/390 (z/OS) Unix System Services (Open Edition).
1226
 * The difference is in the mapping of Line Feed and New Line control codes:
1227
 * Standard EBCDIC maps
1228
 *
1229
 *   <U000A> \x25 |0
1230
 *   <U0085> \x15 |0
1231
 *
1232
 * but OS/390 USS EBCDIC swaps the control codes for LF and NL,
1233
 * mapping
1234
 *
1235
 *   <U000A> \x15 |0
1236
 *   <U0085> \x25 |0
1237
 *
1238
 * This code modifies a loaded standard EBCDIC<->Unicode mapping table
1239
 * by copying it into allocated memory and swapping the LF and NL values.
1240
 * It allows to support the same EBCDIC charset in both versions without
1241
 * duplicating the entire installed table.
1242
 */
1243
1244
/* standard EBCDIC codes */
1245
0
#define EBCDIC_LF 0x25
1246
0
#define EBCDIC_NL 0x15
1247
1248
/* standard EBCDIC codes with roundtrip flag as stored in Unicode-to-single-byte tables */
1249
0
#define EBCDIC_RT_LF 0xf25
1250
0
#define EBCDIC_RT_NL 0xf15
1251
1252
/* Unicode code points */
1253
#define U_LF 0x0a
1254
#define U_NL 0x85
1255
1256
static UBool
1257
0
_EBCDICSwapLFNL(UConverterSharedData *sharedData, UErrorCode *pErrorCode) {
1258
0
    UConverterMBCSTable *mbcsTable;
1259
1260
0
    const uint16_t *table, *results;
1261
0
    const uint8_t *bytes;
1262
1263
0
    int32_t (*newStateTable)[256];
1264
0
    uint16_t *newResults;
1265
0
    uint8_t *p;
1266
0
    char *name;
1267
1268
0
    uint32_t stage2Entry;
1269
0
    uint32_t size, sizeofFromUBytes;
1270
1271
0
    mbcsTable=&sharedData->mbcs;
1272
1273
0
    table=mbcsTable->fromUnicodeTable;
1274
0
    bytes=mbcsTable->fromUnicodeBytes;
1275
0
    results=(const uint16_t *)bytes;
1276
1277
    /*
1278
     * Check that this is an EBCDIC table with SBCS portion -
1279
     * SBCS or EBCDIC_STATEFUL with standard EBCDIC LF and NL mappings.
1280
     *
1281
     * If not, ignore the option. Options are always ignored if they do not apply.
1282
     */
1283
0
    if(!(
1284
0
         (mbcsTable->outputType==MBCS_OUTPUT_1 || mbcsTable->outputType==MBCS_OUTPUT_2_SISO) &&
1285
0
         mbcsTable->stateTable[0][EBCDIC_LF]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF) &&
1286
0
         mbcsTable->stateTable[0][EBCDIC_NL]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL)
1287
0
    )) {
1288
0
        return FALSE;
1289
0
    }
1290
1291
0
    if(mbcsTable->outputType==MBCS_OUTPUT_1) {
1292
0
        if(!(
1293
0
             EBCDIC_RT_LF==MBCS_SINGLE_RESULT_FROM_U(table, results, U_LF) &&
1294
0
             EBCDIC_RT_NL==MBCS_SINGLE_RESULT_FROM_U(table, results, U_NL)
1295
0
        )) {
1296
0
            return FALSE;
1297
0
        }
1298
0
    } else /* MBCS_OUTPUT_2_SISO */ {
1299
0
        stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
1300
0
        if(!(
1301
0
             MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_LF)!=0 &&
1302
0
             EBCDIC_LF==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_LF)
1303
0
        )) {
1304
0
            return FALSE;
1305
0
        }
1306
1307
0
        stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
1308
0
        if(!(
1309
0
             MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_NL)!=0 &&
1310
0
             EBCDIC_NL==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_NL)
1311
0
        )) {
1312
0
            return FALSE;
1313
0
        }
1314
0
    }
1315
1316
0
    if(mbcsTable->fromUBytesLength>0) {
1317
        /*
1318
         * We _know_ the number of bytes in the fromUnicodeBytes array
1319
         * starting with header.version 4.1.
1320
         */
1321
0
        sizeofFromUBytes=mbcsTable->fromUBytesLength;
1322
0
    } else {
1323
        /*
1324
         * Otherwise:
1325
         * There used to be code to enumerate the fromUnicode
1326
         * trie and find the highest entry, but it was removed in ICU 3.2
1327
         * because it was not tested and caused a low code coverage number.
1328
         * See Jitterbug 3674.
1329
         * This affects only some .cnv file formats with a header.version
1330
         * below 4.1, and only when swaplfnl is requested.
1331
         *
1332
         * ucnvmbcs.c revision 1.99 is the last one with the
1333
         * ucnv_MBCSSizeofFromUBytes() function.
1334
         */
1335
0
        *pErrorCode=U_INVALID_FORMAT_ERROR;
1336
0
        return FALSE;
1337
0
    }
1338
1339
    /*
1340
     * The table has an appropriate format.
1341
     * Allocate and build
1342
     * - a modified to-Unicode state table
1343
     * - a modified from-Unicode output array
1344
     * - a converter name string with the swap option appended
1345
     */
1346
0
    size=
1347
0
        mbcsTable->countStates*1024+
1348
0
        sizeofFromUBytes+
1349
0
        UCNV_MAX_CONVERTER_NAME_LENGTH+20;
1350
0
    p=(uint8_t *)uprv_malloc(size);
1351
0
    if(p==NULL) {
1352
0
        *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1353
0
        return FALSE;
1354
0
    }
1355
1356
    /* copy and modify the to-Unicode state table */
1357
0
    newStateTable=(int32_t (*)[256])p;
1358
0
    uprv_memcpy(newStateTable, mbcsTable->stateTable, mbcsTable->countStates*1024);
1359
1360
0
    newStateTable[0][EBCDIC_LF]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL);
1361
0
    newStateTable[0][EBCDIC_NL]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF);
1362
1363
    /* copy and modify the from-Unicode result table */
1364
0
    newResults=(uint16_t *)newStateTable[mbcsTable->countStates];
1365
0
    uprv_memcpy(newResults, bytes, sizeofFromUBytes);
1366
1367
    /* conveniently, the table access macros work on the left side of expressions */
1368
0
    if(mbcsTable->outputType==MBCS_OUTPUT_1) {
1369
0
        MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_LF)=EBCDIC_RT_NL;
1370
0
        MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_NL)=EBCDIC_RT_LF;
1371
0
    } else /* MBCS_OUTPUT_2_SISO */ {
1372
0
        stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
1373
0
        MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_LF)=EBCDIC_NL;
1374
1375
0
        stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
1376
0
        MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_NL)=EBCDIC_LF;
1377
0
    }
1378
1379
    /* set the canonical converter name */
1380
0
    name=(char *)newResults+sizeofFromUBytes;
1381
0
    uprv_strcpy(name, sharedData->staticData->name);
1382
0
    uprv_strcat(name, UCNV_SWAP_LFNL_OPTION_STRING);
1383
1384
    /* set the pointers */
1385
0
    umtx_lock(NULL);
1386
0
    if(mbcsTable->swapLFNLStateTable==NULL) {
1387
0
        mbcsTable->swapLFNLStateTable=newStateTable;
1388
0
        mbcsTable->swapLFNLFromUnicodeBytes=(uint8_t *)newResults;
1389
0
        mbcsTable->swapLFNLName=name;
1390
1391
0
        newStateTable=NULL;
1392
0
    }
1393
0
    umtx_unlock(NULL);
1394
1395
    /* release the allocated memory if another thread beat us to it */
1396
0
    if(newStateTable!=NULL) {
1397
0
        uprv_free(newStateTable);
1398
0
    }
1399
0
    return TRUE;
1400
0
}
1401
1402
/* reconstitute omitted fromUnicode data ------------------------------------ */
1403
1404
/* for details, compare with genmbcs.c MBCSAddFromUnicode() and transformEUC() */
1405
static UBool U_CALLCONV
1406
0
writeStage3Roundtrip(const void *context, uint32_t value, UChar32 codePoints[32]) {
1407
0
    UConverterMBCSTable *mbcsTable=(UConverterMBCSTable *)context;
1408
0
    const uint16_t *table;
1409
0
    uint32_t *stage2;
1410
0
    uint8_t *bytes, *p;
1411
0
    UChar32 c;
1412
0
    int32_t i, st3;
1413
1414
0
    table=mbcsTable->fromUnicodeTable;
1415
0
    bytes=(uint8_t *)mbcsTable->fromUnicodeBytes;
1416
1417
    /* for EUC outputTypes, modify the value like genmbcs.c's transformEUC() */
1418
0
    switch(mbcsTable->outputType) {
1419
0
    case MBCS_OUTPUT_3_EUC:
1420
0
        if(value<=0xffff) {
1421
            /* short sequences are stored directly */
1422
            /* code set 0 or 1 */
1423
0
        } else if(value<=0x8effff) {
1424
            /* code set 2 */
1425
0
            value&=0x7fff;
1426
0
        } else /* first byte is 0x8f */ {
1427
            /* code set 3 */
1428
0
            value&=0xff7f;
1429
0
        }
1430
0
        break;
1431
0
    case MBCS_OUTPUT_4_EUC:
1432
0
        if(value<=0xffffff) {
1433
            /* short sequences are stored directly */
1434
            /* code set 0 or 1 */
1435
0
        } else if(value<=0x8effffff) {
1436
            /* code set 2 */
1437
0
            value&=0x7fffff;
1438
0
        } else /* first byte is 0x8f */ {
1439
            /* code set 3 */
1440
0
            value&=0xff7fff;
1441
0
        }
1442
0
        break;
1443
0
    default:
1444
0
        break;
1445
0
    }
1446
1447
0
    for(i=0; i<=0x1f; ++value, ++i) {
1448
0
        c=codePoints[i];
1449
0
        if(c<0) {
1450
0
            continue;
1451
0
        }
1452
1453
        /* locate the stage 2 & 3 data */
1454
0
        stage2=((uint32_t *)table)+table[c>>10]+((c>>4)&0x3f);
1455
0
        p=bytes;
1456
0
        st3=(int32_t)(uint16_t)*stage2*16+(c&0xf);
1457
1458
        /* write the codepage bytes into stage 3 */
1459
0
        switch(mbcsTable->outputType) {
1460
0
        case MBCS_OUTPUT_3:
1461
0
        case MBCS_OUTPUT_4_EUC:
1462
0
            p+=st3*3;
1463
0
            p[0]=(uint8_t)(value>>16);
1464
0
            p[1]=(uint8_t)(value>>8);
1465
0
            p[2]=(uint8_t)value;
1466
0
            break;
1467
0
        case MBCS_OUTPUT_4:
1468
0
            ((uint32_t *)p)[st3]=value;
1469
0
            break;
1470
0
        default:
1471
            /* 2 bytes per character */
1472
0
            ((uint16_t *)p)[st3]=(uint16_t)value;
1473
0
            break;
1474
0
        }
1475
1476
        /* set the roundtrip flag */
1477
0
        *stage2|=(1UL<<(16+(c&0xf)));
1478
0
    }
1479
0
    return TRUE;
1480
0
 }
1481
1482
static void
1483
reconstituteData(UConverterMBCSTable *mbcsTable,
1484
                 uint32_t stage1Length, uint32_t stage2Length,
1485
                 uint32_t fullStage2Length,  /* lengths are numbers of units, not bytes */
1486
0
                 UErrorCode *pErrorCode) {
1487
0
    uint16_t *stage1;
1488
0
    uint32_t *stage2;
1489
0
    uint32_t dataLength=stage1Length*2+fullStage2Length*4+mbcsTable->fromUBytesLength;
1490
0
    mbcsTable->reconstitutedData=(uint8_t *)uprv_malloc(dataLength);
1491
0
    if(mbcsTable->reconstitutedData==NULL) {
1492
0
        *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1493
0
        return;
1494
0
    }
1495
0
    uprv_memset(mbcsTable->reconstitutedData, 0, dataLength);
1496
1497
    /* copy existing data and reroute the pointers */
1498
0
    stage1=(uint16_t *)mbcsTable->reconstitutedData;
1499
0
    uprv_memcpy(stage1, mbcsTable->fromUnicodeTable, stage1Length*2);
1500
1501
0
    stage2=(uint32_t *)(stage1+stage1Length);
1502
0
    uprv_memcpy(stage2+(fullStage2Length-stage2Length),
1503
0
                mbcsTable->fromUnicodeTable+stage1Length,
1504
0
                stage2Length*4);
1505
1506
0
    mbcsTable->fromUnicodeTable=stage1;
1507
0
    mbcsTable->fromUnicodeBytes=(uint8_t *)(stage2+fullStage2Length);
1508
1509
    /* indexes into stage 2 count from the bottom of the fromUnicodeTable */
1510
0
    stage2=(uint32_t *)stage1;
1511
1512
    /* reconstitute the initial part of stage 2 from the mbcsIndex */
1513
0
    {
1514
0
        int32_t stageUTF8Length=((int32_t)mbcsTable->maxFastUChar+1)>>6;
1515
0
        int32_t stageUTF8Index=0;
1516
0
        int32_t st1, st2, st3, i;
1517
1518
0
        for(st1=0; stageUTF8Index<stageUTF8Length; ++st1) {
1519
0
            st2=stage1[st1];
1520
0
            if(st2!=(int32_t)stage1Length/2) {
1521
                /* each stage 2 block has 64 entries corresponding to 16 entries in the mbcsIndex */
1522
0
                for(i=0; i<16; ++i) {
1523
0
                    st3=mbcsTable->mbcsIndex[stageUTF8Index++];
1524
0
                    if(st3!=0) {
1525
                        /* an stage 2 entry's index is per stage 3 16-block, not per stage 3 entry */
1526
0
                        st3>>=4;
1527
                        /*
1528
                         * 4 stage 2 entries point to 4 consecutive stage 3 16-blocks which are
1529
                         * allocated together as a single 64-block for access from the mbcsIndex
1530
                         */
1531
0
                        stage2[st2++]=st3++;
1532
0
                        stage2[st2++]=st3++;
1533
0
                        stage2[st2++]=st3++;
1534
0
                        stage2[st2++]=st3;
1535
0
                    } else {
1536
                        /* no stage 3 block, skip */
1537
0
                        st2+=4;
1538
0
                    }
1539
0
                }
1540
0
            } else {
1541
                /* no stage 2 block, skip */
1542
0
                stageUTF8Index+=16;
1543
0
            }
1544
0
        }
1545
0
    }
1546
1547
    /* reconstitute fromUnicodeBytes with roundtrips from toUnicode data */
1548
0
    ucnv_MBCSEnumToUnicode(mbcsTable, writeStage3Roundtrip, mbcsTable, pErrorCode);
1549
0
}
1550
1551
/* MBCS setup functions ----------------------------------------------------- */
1552
1553
static void U_CALLCONV
1554
ucnv_MBCSLoad(UConverterSharedData *sharedData,
1555
          UConverterLoadArgs *pArgs,
1556
          const uint8_t *raw,
1557
0
          UErrorCode *pErrorCode) {
1558
0
    UDataInfo info;
1559
0
    UConverterMBCSTable *mbcsTable=&sharedData->mbcs;
1560
0
    _MBCSHeader *header=(_MBCSHeader *)raw;
1561
0
    uint32_t offset;
1562
0
    uint32_t headerLength;
1563
0
    UBool noFromU=FALSE;
1564
1565
0
    if(header->version[0]==4) {
1566
0
        headerLength=MBCS_HEADER_V4_LENGTH;
1567
0
    } else if(header->version[0]==5 && header->version[1]>=3 &&
1568
0
              (header->options&MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK)==0) {
1569
0
        headerLength=header->options&MBCS_OPT_LENGTH_MASK;
1570
0
        noFromU=(UBool)((header->options&MBCS_OPT_NO_FROM_U)!=0);
1571
0
    } else {
1572
0
        *pErrorCode=U_INVALID_TABLE_FORMAT;
1573
0
        return;
1574
0
    }
1575
1576
0
    mbcsTable->outputType=(uint8_t)header->flags;
1577
0
    if(noFromU && mbcsTable->outputType==MBCS_OUTPUT_1) {
1578
0
        *pErrorCode=U_INVALID_TABLE_FORMAT;
1579
0
        return;
1580
0
    }
1581
1582
    /* extension data, header version 4.2 and higher */
1583
0
    offset=header->flags>>8;
1584
0
    if(offset!=0) {
1585
0
        mbcsTable->extIndexes=(const int32_t *)(raw+offset);
1586
0
    }
1587
1588
0
    if(mbcsTable->outputType==MBCS_OUTPUT_EXT_ONLY) {
1589
0
        UConverterLoadArgs args=UCNV_LOAD_ARGS_INITIALIZER;
1590
0
        UConverterSharedData *baseSharedData;
1591
0
        const int32_t *extIndexes;
1592
0
        const char *baseName;
1593
1594
        /* extension-only file, load the base table and set values appropriately */
1595
0
        if((extIndexes=mbcsTable->extIndexes)==NULL) {
1596
            /* extension-only file without extension */
1597
0
            *pErrorCode=U_INVALID_TABLE_FORMAT;
1598
0
            return;
1599
0
        }
1600
1601
0
        if(pArgs->nestedLoads!=1) {
1602
            /* an extension table must not be loaded as a base table */
1603
0
            *pErrorCode=U_INVALID_TABLE_FILE;
1604
0
            return;
1605
0
        }
1606
1607
        /* load the base table */
1608
0
        baseName=(const char *)header+headerLength*4;
1609
0
        if(0==uprv_strcmp(baseName, sharedData->staticData->name)) {
1610
            /* forbid loading this same extension-only file */
1611
0
            *pErrorCode=U_INVALID_TABLE_FORMAT;
1612
0
            return;
1613
0
        }
1614
1615
        /* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
1616
0
        args.size=sizeof(UConverterLoadArgs);
1617
0
        args.nestedLoads=2;
1618
0
        args.onlyTestIsLoadable=pArgs->onlyTestIsLoadable;
1619
0
        args.reserved=pArgs->reserved;
1620
0
        args.options=pArgs->options;
1621
0
        args.pkg=pArgs->pkg;
1622
0
        args.name=baseName;
1623
0
        baseSharedData=ucnv_load(&args, pErrorCode);
1624
0
        if(U_FAILURE(*pErrorCode)) {
1625
0
            return;
1626
0
        }
1627
0
        if( baseSharedData->staticData->conversionType!=UCNV_MBCS ||
1628
0
            baseSharedData->mbcs.baseSharedData!=NULL
1629
0
        ) {
1630
0
            ucnv_unload(baseSharedData);
1631
0
            *pErrorCode=U_INVALID_TABLE_FORMAT;
1632
0
            return;
1633
0
        }
1634
0
        if(pArgs->onlyTestIsLoadable) {
1635
            /*
1636
             * Exit as soon as we know that we can load the converter
1637
             * and the format is valid and supported.
1638
             * The worst that can happen in the following code is a memory
1639
             * allocation error.
1640
             */
1641
0
            ucnv_unload(baseSharedData);
1642
0
            return;
1643
0
        }
1644
1645
        /* copy the base table data */
1646
0
        uprv_memcpy(mbcsTable, &baseSharedData->mbcs, sizeof(UConverterMBCSTable));
1647
1648
        /* overwrite values with relevant ones for the extension converter */
1649
0
        mbcsTable->baseSharedData=baseSharedData;
1650
0
        mbcsTable->extIndexes=extIndexes;
1651
1652
        /*
1653
         * It would be possible to share the swapLFNL data with a base converter,
1654
         * but the generated name would have to be different, and the memory
1655
         * would have to be free'd only once.
1656
         * It is easier to just create the data for the extension converter
1657
         * separately when it is requested.
1658
         */
1659
0
        mbcsTable->swapLFNLStateTable=NULL;
1660
0
        mbcsTable->swapLFNLFromUnicodeBytes=NULL;
1661
0
        mbcsTable->swapLFNLName=NULL;
1662
1663
        /*
1664
         * The reconstitutedData must be deleted only when the base converter
1665
         * is unloaded.
1666
         */
1667
0
        mbcsTable->reconstitutedData=NULL;
1668
1669
        /*
1670
         * Set a special, runtime-only outputType if the extension converter
1671
         * is a DBCS version of a base converter that also maps single bytes.
1672
         */
1673
0
        if( sharedData->staticData->conversionType==UCNV_DBCS ||
1674
0
                (sharedData->staticData->conversionType==UCNV_MBCS &&
1675
0
                 sharedData->staticData->minBytesPerChar>=2)
1676
0
        ) {
1677
0
            if(baseSharedData->mbcs.outputType==MBCS_OUTPUT_2_SISO) {
1678
                /* the base converter is SI/SO-stateful */
1679
0
                int32_t entry;
1680
1681
                /* get the dbcs state from the state table entry for SO=0x0e */
1682
0
                entry=mbcsTable->stateTable[0][0xe];
1683
0
                if( MBCS_ENTRY_IS_FINAL(entry) &&
1684
0
                    MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_CHANGE_ONLY &&
1685
0
                    MBCS_ENTRY_FINAL_STATE(entry)!=0
1686
0
                ) {
1687
0
                    mbcsTable->dbcsOnlyState=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry);
1688
1689
0
                    mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
1690
0
                }
1691
0
            } else if(
1692
0
                baseSharedData->staticData->conversionType==UCNV_MBCS &&
1693
0
                baseSharedData->staticData->minBytesPerChar==1 &&
1694
0
                baseSharedData->staticData->maxBytesPerChar==2 &&
1695
0
                mbcsTable->countStates<=127
1696
0
            ) {
1697
                /* non-stateful base converter, need to modify the state table */
1698
0
                int32_t (*newStateTable)[256];
1699
0
                int32_t *state;
1700
0
                int32_t i, count;
1701
1702
                /* allocate a new state table and copy the base state table contents */
1703
0
                count=mbcsTable->countStates;
1704
0
                newStateTable=(int32_t (*)[256])uprv_malloc((count+1)*1024);
1705
0
                if(newStateTable==NULL) {
1706
0
                    ucnv_unload(baseSharedData);
1707
0
                    *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1708
0
                    return;
1709
0
                }
1710
1711
0
                uprv_memcpy(newStateTable, mbcsTable->stateTable, count*1024);
1712
1713
                /* change all final single-byte entries to go to a new all-illegal state */
1714
0
                state=newStateTable[0];
1715
0
                for(i=0; i<256; ++i) {
1716
0
                    if(MBCS_ENTRY_IS_FINAL(state[i])) {
1717
0
                        state[i]=MBCS_ENTRY_TRANSITION(count, 0);
1718
0
                    }
1719
0
                }
1720
1721
                /* build the new all-illegal state */
1722
0
                state=newStateTable[count];
1723
0
                for(i=0; i<256; ++i) {
1724
0
                    state[i]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL, 0);
1725
0
                }
1726
0
                mbcsTable->stateTable=(const int32_t (*)[256])newStateTable;
1727
0
                mbcsTable->countStates=(uint8_t)(count+1);
1728
0
                mbcsTable->stateTableOwned=TRUE;
1729
1730
0
                mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
1731
0
            }
1732
0
        }
1733
1734
        /*
1735
         * unlike below for files with base tables, do not get the unicodeMask
1736
         * from the sharedData; instead, use the base table's unicodeMask,
1737
         * which we copied in the memcpy above;
1738
         * this is necessary because the static data unicodeMask, especially
1739
         * the UCNV_HAS_SUPPLEMENTARY flag, is part of the base table data
1740
         */
1741
0
    } else {
1742
        /* conversion file with a base table; an additional extension table is optional */
1743
        /* make sure that the output type is known */
1744
0
        switch(mbcsTable->outputType) {
1745
0
        case MBCS_OUTPUT_1:
1746
0
        case MBCS_OUTPUT_2:
1747
0
        case MBCS_OUTPUT_3:
1748
0
        case MBCS_OUTPUT_4:
1749
0
        case MBCS_OUTPUT_3_EUC:
1750
0
        case MBCS_OUTPUT_4_EUC:
1751
0
        case MBCS_OUTPUT_2_SISO:
1752
            /* OK */
1753
0
            break;
1754
0
        default:
1755
0
            *pErrorCode=U_INVALID_TABLE_FORMAT;
1756
0
            return;
1757
0
        }
1758
0
        if(pArgs->onlyTestIsLoadable) {
1759
            /*
1760
             * Exit as soon as we know that we can load the converter
1761
             * and the format is valid and supported.
1762
             * The worst that can happen in the following code is a memory
1763
             * allocation error.
1764
             */
1765
0
            return;
1766
0
        }
1767
1768
0
        mbcsTable->countStates=(uint8_t)header->countStates;
1769
0
        mbcsTable->countToUFallbacks=header->countToUFallbacks;
1770
0
        mbcsTable->stateTable=(const int32_t (*)[256])(raw+headerLength*4);
1771
0
        mbcsTable->toUFallbacks=(const _MBCSToUFallback *)(mbcsTable->stateTable+header->countStates);
1772
0
        mbcsTable->unicodeCodeUnits=(const uint16_t *)(raw+header->offsetToUCodeUnits);
1773
1774
0
        mbcsTable->fromUnicodeTable=(const uint16_t *)(raw+header->offsetFromUTable);
1775
0
        mbcsTable->fromUnicodeBytes=(const uint8_t *)(raw+header->offsetFromUBytes);
1776
0
        mbcsTable->fromUBytesLength=header->fromUBytesLength;
1777
1778
        /*
1779
         * converter versions 6.1 and up contain a unicodeMask that is
1780
         * used here to select the most efficient function implementations
1781
         */
1782
0
        info.size=sizeof(UDataInfo);
1783
0
        udata_getInfo((UDataMemory *)sharedData->dataMemory, &info);
1784
0
        if(info.formatVersion[0]>6 || (info.formatVersion[0]==6 && info.formatVersion[1]>=1)) {
1785
            /* mask off possible future extensions to be safe */
1786
0
            mbcsTable->unicodeMask=(uint8_t)(sharedData->staticData->unicodeMask&3);
1787
0
        } else {
1788
            /* for older versions, assume worst case: contains anything possible (prevent over-optimizations) */
1789
0
            mbcsTable->unicodeMask=UCNV_HAS_SUPPLEMENTARY|UCNV_HAS_SURROGATES;
1790
0
        }
1791
1792
        /*
1793
         * _MBCSHeader.version 4.3 adds utf8Friendly data structures.
1794
         * Check for the header version, SBCS vs. MBCS, and for whether the
1795
         * data structures are optimized for code points as high as what the
1796
         * runtime code is designed for.
1797
         * The implementation does not handle mapping tables with entries for
1798
         * unpaired surrogates.
1799
         */
1800
0
        if( header->version[1]>=3 &&
1801
0
            (mbcsTable->unicodeMask&UCNV_HAS_SURROGATES)==0 &&
1802
0
            (mbcsTable->countStates==1 ?
1803
0
                (header->version[2]>=(SBCS_FAST_MAX>>8)) :
1804
0
                (header->version[2]>=(MBCS_FAST_MAX>>8))
1805
0
            )
1806
0
        ) {
1807
0
            mbcsTable->utf8Friendly=TRUE;
1808
1809
0
            if(mbcsTable->countStates==1) {
1810
                /*
1811
                 * SBCS: Stage 3 is allocated in 64-entry blocks for U+0000..SBCS_FAST_MAX or higher.
1812
                 * Build a table with indexes to each block, to be used instead of
1813
                 * the regular stage 1/2 table.
1814
                 */
1815
0
                int32_t i;
1816
0
                for(i=0; i<(SBCS_FAST_LIMIT>>6); ++i) {
1817
0
                    mbcsTable->sbcsIndex[i]=mbcsTable->fromUnicodeTable[mbcsTable->fromUnicodeTable[i>>4]+((i<<2)&0x3c)];
1818
0
                }
1819
                /* set SBCS_FAST_MAX to reflect the reach of sbcsIndex[] even if header->version[2]>(SBCS_FAST_MAX>>8) */
1820
0
                mbcsTable->maxFastUChar=SBCS_FAST_MAX;
1821
0
            } else {
1822
                /*
1823
                 * MBCS: Stage 3 is allocated in 64-entry blocks for U+0000..MBCS_FAST_MAX or higher.
1824
                 * The .cnv file is prebuilt with an additional stage table with indexes
1825
                 * to each block.
1826
                 */
1827
0
                mbcsTable->mbcsIndex=(const uint16_t *)
1828
0
                    (mbcsTable->fromUnicodeBytes+
1829
0
                     (noFromU ? 0 : mbcsTable->fromUBytesLength));
1830
0
                mbcsTable->maxFastUChar=(((UChar)header->version[2])<<8)|0xff;
1831
0
            }
1832
0
        }
1833
1834
        /* calculate a bit set of 4 ASCII characters per bit that round-trip to ASCII bytes */
1835
0
        {
1836
0
            uint32_t asciiRoundtrips=0xffffffff;
1837
0
            int32_t i;
1838
1839
0
            for(i=0; i<0x80; ++i) {
1840
0
                if(mbcsTable->stateTable[0][i]!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, i)) {
1841
0
                    asciiRoundtrips&=~((uint32_t)1<<(i>>2));
1842
0
                }
1843
0
            }
1844
0
            mbcsTable->asciiRoundtrips=asciiRoundtrips;
1845
0
        }
1846
1847
0
        if(noFromU) {
1848
0
            uint32_t stage1Length=
1849
0
                mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY ?
1850
0
                    0x440 : 0x40;
1851
0
            uint32_t stage2Length=
1852
0
                (header->offsetFromUBytes-header->offsetFromUTable)/4-
1853
0
                stage1Length/2;
1854
0
            reconstituteData(mbcsTable, stage1Length, stage2Length, header->fullStage2Length, pErrorCode);
1855
0
        }
1856
0
    }
1857
1858
    /* Set the impl pointer here so that it is set for both extension-only and base tables. */
1859
0
    if(mbcsTable->utf8Friendly) {
1860
0
        if(mbcsTable->countStates==1) {
1861
0
            sharedData->impl=&_SBCSUTF8Impl;
1862
0
        } else {
1863
0
            if(mbcsTable->outputType==MBCS_OUTPUT_2) {
1864
0
                sharedData->impl=&_DBCSUTF8Impl;
1865
0
            }
1866
0
        }
1867
0
    }
1868
1869
0
    if(mbcsTable->outputType==MBCS_OUTPUT_DBCS_ONLY || mbcsTable->outputType==MBCS_OUTPUT_2_SISO) {
1870
        /*
1871
         * MBCS_OUTPUT_DBCS_ONLY: No SBCS mappings, therefore ASCII does not roundtrip.
1872
         * MBCS_OUTPUT_2_SISO: Bypass the ASCII fastpath to handle prevLength correctly.
1873
         */
1874
0
        mbcsTable->asciiRoundtrips=0;
1875
0
    }
1876
0
}
1877
1878
static void U_CALLCONV
1879
0
ucnv_MBCSUnload(UConverterSharedData *sharedData) {
1880
0
    UConverterMBCSTable *mbcsTable=&sharedData->mbcs;
1881
1882
0
    if(mbcsTable->swapLFNLStateTable!=NULL) {
1883
0
        uprv_free(mbcsTable->swapLFNLStateTable);
1884
0
    }
1885
0
    if(mbcsTable->stateTableOwned) {
1886
0
        uprv_free((void *)mbcsTable->stateTable);
1887
0
    }
1888
0
    if(mbcsTable->baseSharedData!=NULL) {
1889
0
        ucnv_unload(mbcsTable->baseSharedData);
1890
0
    }
1891
0
    if(mbcsTable->reconstitutedData!=NULL) {
1892
0
        uprv_free(mbcsTable->reconstitutedData);
1893
0
    }
1894
0
}
1895
1896
static void U_CALLCONV
1897
ucnv_MBCSOpen(UConverter *cnv,
1898
              UConverterLoadArgs *pArgs,
1899
0
              UErrorCode *pErrorCode) {
1900
0
    UConverterMBCSTable *mbcsTable;
1901
0
    const int32_t *extIndexes;
1902
0
    uint8_t outputType;
1903
0
    int8_t maxBytesPerUChar;
1904
1905
0
    if(pArgs->onlyTestIsLoadable) {
1906
0
        return;
1907
0
    }
1908
1909
0
    mbcsTable=&cnv->sharedData->mbcs;
1910
0
    outputType=mbcsTable->outputType;
1911
1912
0
    if(outputType==MBCS_OUTPUT_DBCS_ONLY) {
1913
        /* the swaplfnl option does not apply, remove it */
1914
0
        cnv->options=pArgs->options&=~UCNV_OPTION_SWAP_LFNL;
1915
0
    }
1916
1917
0
    if((pArgs->options&UCNV_OPTION_SWAP_LFNL)!=0) {
1918
        /* do this because double-checked locking is broken */
1919
0
        UBool isCached;
1920
1921
0
        umtx_lock(NULL);
1922
0
        isCached=mbcsTable->swapLFNLStateTable!=NULL;
1923
0
        umtx_unlock(NULL);
1924
1925
0
        if(!isCached) {
1926
0
            if(!_EBCDICSwapLFNL(cnv->sharedData, pErrorCode)) {
1927
0
                if(U_FAILURE(*pErrorCode)) {
1928
0
                    return; /* something went wrong */
1929
0
                }
1930
1931
                /* the option does not apply, remove it */
1932
0
                cnv->options=pArgs->options&=~UCNV_OPTION_SWAP_LFNL;
1933
0
            }
1934
0
        }
1935
0
    }
1936
1937
0
    if(uprv_strstr(pArgs->name, "18030")!=NULL) {
1938
0
        if(uprv_strstr(pArgs->name, "gb18030")!=NULL || uprv_strstr(pArgs->name, "GB18030")!=NULL) {
1939
            /* set a flag for GB 18030 mode, which changes the callback behavior */
1940
0
            cnv->options|=_MBCS_OPTION_GB18030;
1941
0
        }
1942
0
    } else if((uprv_strstr(pArgs->name, "KEIS")!=NULL) || (uprv_strstr(pArgs->name, "keis")!=NULL)) {
1943
        /* set a flag for KEIS converter, which changes the SI/SO character sequence */
1944
0
        cnv->options|=_MBCS_OPTION_KEIS;
1945
0
    } else if((uprv_strstr(pArgs->name, "JEF")!=NULL) || (uprv_strstr(pArgs->name, "jef")!=NULL)) {
1946
        /* set a flag for JEF converter, which changes the SI/SO character sequence */
1947
0
        cnv->options|=_MBCS_OPTION_JEF;
1948
0
    } else if((uprv_strstr(pArgs->name, "JIPS")!=NULL) || (uprv_strstr(pArgs->name, "jips")!=NULL)) {
1949
        /* set a flag for JIPS converter, which changes the SI/SO character sequence */
1950
0
        cnv->options|=_MBCS_OPTION_JIPS;
1951
0
    }
1952
1953
    /* fix maxBytesPerUChar depending on outputType and options etc. */
1954
0
    if(outputType==MBCS_OUTPUT_2_SISO) {
1955
0
        cnv->maxBytesPerUChar=3; /* SO+DBCS */
1956
0
    }
1957
1958
0
    extIndexes=mbcsTable->extIndexes;
1959
0
    if(extIndexes!=NULL) {
1960
0
        maxBytesPerUChar=(int8_t)UCNV_GET_MAX_BYTES_PER_UCHAR(extIndexes);
1961
0
        if(outputType==MBCS_OUTPUT_2_SISO) {
1962
0
            ++maxBytesPerUChar; /* SO + multiple DBCS */
1963
0
        }
1964
1965
0
        if(maxBytesPerUChar>cnv->maxBytesPerUChar) {
1966
0
            cnv->maxBytesPerUChar=maxBytesPerUChar;
1967
0
        }
1968
0
    }
1969
1970
#if 0
1971
    /*
1972
     * documentation of UConverter fields used for status
1973
     * all of these fields are (re)set to 0 by ucnv_bld.c and ucnv_reset()
1974
     */
1975
1976
    /* toUnicode */
1977
    cnv->toUnicodeStatus=0;     /* offset */
1978
    cnv->mode=0;                /* state */
1979
    cnv->toULength=0;           /* byteIndex */
1980
1981
    /* fromUnicode */
1982
    cnv->fromUChar32=0;
1983
    cnv->fromUnicodeStatus=1;   /* prevLength */
1984
#endif
1985
0
}
1986
1987
U_CDECL_BEGIN
1988
1989
static const char* U_CALLCONV
1990
0
ucnv_MBCSGetName(const UConverter *cnv) {
1991
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0 && cnv->sharedData->mbcs.swapLFNLName!=NULL) {
1992
0
        return cnv->sharedData->mbcs.swapLFNLName;
1993
0
    } else {
1994
0
        return cnv->sharedData->staticData->name;
1995
0
    }
1996
0
}
1997
U_CDECL_END
1998
1999
2000
/* MBCS-to-Unicode conversion functions ------------------------------------- */
2001
2002
static UChar32 U_CALLCONV
2003
0
ucnv_MBCSGetFallback(UConverterMBCSTable *mbcsTable, uint32_t offset) {
2004
0
    const _MBCSToUFallback *toUFallbacks;
2005
0
    uint32_t i, start, limit;
2006
2007
0
    limit=mbcsTable->countToUFallbacks;
2008
0
    if(limit>0) {
2009
        /* do a binary search for the fallback mapping */
2010
0
        toUFallbacks=mbcsTable->toUFallbacks;
2011
0
        start=0;
2012
0
        while(start<limit-1) {
2013
0
            i=(start+limit)/2;
2014
0
            if(offset<toUFallbacks[i].offset) {
2015
0
                limit=i;
2016
0
            } else {
2017
0
                start=i;
2018
0
            }
2019
0
        }
2020
2021
        /* did we really find it? */
2022
0
        if(offset==toUFallbacks[start].offset) {
2023
0
            return toUFallbacks[start].codePoint;
2024
0
        }
2025
0
    }
2026
2027
0
    return 0xfffe;
2028
0
}
2029
2030
/* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */
2031
static void
2032
ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
2033
0
                                UErrorCode *pErrorCode) {
2034
0
    UConverter *cnv;
2035
0
    const uint8_t *source, *sourceLimit;
2036
0
    UChar *target;
2037
0
    const UChar *targetLimit;
2038
0
    int32_t *offsets;
2039
2040
0
    const int32_t (*stateTable)[256];
2041
2042
0
    int32_t sourceIndex;
2043
2044
0
    int32_t entry;
2045
0
    UChar c;
2046
0
    uint8_t action;
2047
2048
    /* set up the local pointers */
2049
0
    cnv=pArgs->converter;
2050
0
    source=(const uint8_t *)pArgs->source;
2051
0
    sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2052
0
    target=pArgs->target;
2053
0
    targetLimit=pArgs->targetLimit;
2054
0
    offsets=pArgs->offsets;
2055
2056
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2057
0
        stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2058
0
    } else {
2059
0
        stateTable=cnv->sharedData->mbcs.stateTable;
2060
0
    }
2061
2062
    /* sourceIndex=-1 if the current character began in the previous buffer */
2063
0
    sourceIndex=0;
2064
2065
    /* conversion loop */
2066
0
    while(source<sourceLimit) {
2067
        /*
2068
         * This following test is to see if available input would overflow the output.
2069
         * It does not catch output of more than one code unit that
2070
         * overflows as a result of a surrogate pair or callback output
2071
         * from the last source byte.
2072
         * Therefore, those situations also test for overflows and will
2073
         * then break the loop, too.
2074
         */
2075
0
        if(target>=targetLimit) {
2076
            /* target is full */
2077
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2078
0
            break;
2079
0
        }
2080
2081
0
        entry=stateTable[0][*source++];
2082
        /* MBCS_ENTRY_IS_FINAL(entry) */
2083
2084
        /* test the most common case first */
2085
0
        if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2086
            /* output BMP code point */
2087
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2088
0
            if(offsets!=NULL) {
2089
0
                *offsets++=sourceIndex;
2090
0
            }
2091
2092
            /* normal end of action codes: prepare for a new character */
2093
0
            ++sourceIndex;
2094
0
            continue;
2095
0
        }
2096
2097
        /*
2098
         * An if-else-if chain provides more reliable performance for
2099
         * the most common cases compared to a switch.
2100
         */
2101
0
        action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2102
0
        if(action==MBCS_STATE_VALID_DIRECT_20 ||
2103
0
           (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
2104
0
        ) {
2105
0
            entry=MBCS_ENTRY_FINAL_VALUE(entry);
2106
            /* output surrogate pair */
2107
0
            *target++=(UChar)(0xd800|(UChar)(entry>>10));
2108
0
            if(offsets!=NULL) {
2109
0
                *offsets++=sourceIndex;
2110
0
            }
2111
0
            c=(UChar)(0xdc00|(UChar)(entry&0x3ff));
2112
0
            if(target<targetLimit) {
2113
0
                *target++=c;
2114
0
                if(offsets!=NULL) {
2115
0
                    *offsets++=sourceIndex;
2116
0
                }
2117
0
            } else {
2118
                /* target overflow */
2119
0
                cnv->UCharErrorBuffer[0]=c;
2120
0
                cnv->UCharErrorBufferLength=1;
2121
0
                *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2122
0
                break;
2123
0
            }
2124
2125
0
            ++sourceIndex;
2126
0
            continue;
2127
0
        } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2128
0
            if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2129
                /* output BMP code point */
2130
0
                *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2131
0
                if(offsets!=NULL) {
2132
0
                    *offsets++=sourceIndex;
2133
0
                }
2134
2135
0
                ++sourceIndex;
2136
0
                continue;
2137
0
            }
2138
0
        } else if(action==MBCS_STATE_UNASSIGNED) {
2139
            /* just fall through */
2140
0
        } else if(action==MBCS_STATE_ILLEGAL) {
2141
            /* callback(illegal) */
2142
0
            *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2143
0
        } else {
2144
            /* reserved, must never occur */
2145
0
            ++sourceIndex;
2146
0
            continue;
2147
0
        }
2148
2149
0
        if(U_FAILURE(*pErrorCode)) {
2150
            /* callback(illegal) */
2151
0
            break;
2152
0
        } else /* unassigned sequences indicated with byteIndex>0 */ {
2153
            /* try an extension mapping */
2154
0
            pArgs->source=(const char *)source;
2155
0
            cnv->toUBytes[0]=*(source-1);
2156
0
            cnv->toULength=_extToU(cnv, cnv->sharedData,
2157
0
                                    1, &source, sourceLimit,
2158
0
                                    &target, targetLimit,
2159
0
                                    &offsets, sourceIndex,
2160
0
                                    pArgs->flush,
2161
0
                                    pErrorCode);
2162
0
            sourceIndex+=1+(int32_t)(source-(const uint8_t *)pArgs->source);
2163
2164
0
            if(U_FAILURE(*pErrorCode)) {
2165
                /* not mappable or buffer overflow */
2166
0
                break;
2167
0
            }
2168
0
        }
2169
0
    }
2170
2171
    /* write back the updated pointers */
2172
0
    pArgs->source=(const char *)source;
2173
0
    pArgs->target=target;
2174
0
    pArgs->offsets=offsets;
2175
0
}
2176
2177
/*
2178
 * This version of ucnv_MBCSSingleToUnicodeWithOffsets() is optimized for single-byte, single-state codepages
2179
 * that only map to and from the BMP.
2180
 * In addition to single-byte optimizations, the offset calculations
2181
 * become much easier.
2182
 */
2183
static void
2184
ucnv_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs *pArgs,
2185
0
                            UErrorCode *pErrorCode) {
2186
0
    UConverter *cnv;
2187
0
    const uint8_t *source, *sourceLimit, *lastSource;
2188
0
    UChar *target;
2189
0
    int32_t targetCapacity, length;
2190
0
    int32_t *offsets;
2191
2192
0
    const int32_t (*stateTable)[256];
2193
2194
0
    int32_t sourceIndex;
2195
2196
0
    int32_t entry;
2197
0
    uint8_t action;
2198
2199
    /* set up the local pointers */
2200
0
    cnv=pArgs->converter;
2201
0
    source=(const uint8_t *)pArgs->source;
2202
0
    sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2203
0
    target=pArgs->target;
2204
0
    targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
2205
0
    offsets=pArgs->offsets;
2206
2207
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2208
0
        stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2209
0
    } else {
2210
0
        stateTable=cnv->sharedData->mbcs.stateTable;
2211
0
    }
2212
2213
    /* sourceIndex=-1 if the current character began in the previous buffer */
2214
0
    sourceIndex=0;
2215
0
    lastSource=source;
2216
2217
    /*
2218
     * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
2219
     * for the minimum of the sourceLength and targetCapacity
2220
     */
2221
0
    length=(int32_t)(sourceLimit-source);
2222
0
    if(length<targetCapacity) {
2223
0
        targetCapacity=length;
2224
0
    }
2225
2226
0
#if MBCS_UNROLL_SINGLE_TO_BMP
2227
    /* unrolling makes it faster on Pentium III/Windows 2000 */
2228
    /* unroll the loop with the most common case */
2229
0
unrolled:
2230
0
    if(targetCapacity>=16) {
2231
0
        int32_t count, loops, oredEntries;
2232
2233
0
        loops=count=targetCapacity>>4;
2234
0
        do {
2235
0
            oredEntries=entry=stateTable[0][*source++];
2236
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2237
0
            oredEntries|=entry=stateTable[0][*source++];
2238
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2239
0
            oredEntries|=entry=stateTable[0][*source++];
2240
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2241
0
            oredEntries|=entry=stateTable[0][*source++];
2242
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2243
0
            oredEntries|=entry=stateTable[0][*source++];
2244
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2245
0
            oredEntries|=entry=stateTable[0][*source++];
2246
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2247
0
            oredEntries|=entry=stateTable[0][*source++];
2248
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2249
0
            oredEntries|=entry=stateTable[0][*source++];
2250
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2251
0
            oredEntries|=entry=stateTable[0][*source++];
2252
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2253
0
            oredEntries|=entry=stateTable[0][*source++];
2254
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2255
0
            oredEntries|=entry=stateTable[0][*source++];
2256
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2257
0
            oredEntries|=entry=stateTable[0][*source++];
2258
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2259
0
            oredEntries|=entry=stateTable[0][*source++];
2260
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2261
0
            oredEntries|=entry=stateTable[0][*source++];
2262
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2263
0
            oredEntries|=entry=stateTable[0][*source++];
2264
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2265
0
            oredEntries|=entry=stateTable[0][*source++];
2266
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2267
2268
            /* were all 16 entries really valid? */
2269
0
            if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries)) {
2270
                /* no, return to the first of these 16 */
2271
0
                source-=16;
2272
0
                target-=16;
2273
0
                break;
2274
0
            }
2275
0
        } while(--count>0);
2276
0
        count=loops-count;
2277
0
        targetCapacity-=16*count;
2278
2279
0
        if(offsets!=NULL) {
2280
0
            lastSource+=16*count;
2281
0
            while(count>0) {
2282
0
                *offsets++=sourceIndex++;
2283
0
                *offsets++=sourceIndex++;
2284
0
                *offsets++=sourceIndex++;
2285
0
                *offsets++=sourceIndex++;
2286
0
                *offsets++=sourceIndex++;
2287
0
                *offsets++=sourceIndex++;
2288
0
                *offsets++=sourceIndex++;
2289
0
                *offsets++=sourceIndex++;
2290
0
                *offsets++=sourceIndex++;
2291
0
                *offsets++=sourceIndex++;
2292
0
                *offsets++=sourceIndex++;
2293
0
                *offsets++=sourceIndex++;
2294
0
                *offsets++=sourceIndex++;
2295
0
                *offsets++=sourceIndex++;
2296
0
                *offsets++=sourceIndex++;
2297
0
                *offsets++=sourceIndex++;
2298
0
                --count;
2299
0
            }
2300
0
        }
2301
0
    }
2302
0
#endif
2303
2304
    /* conversion loop */
2305
0
    while(targetCapacity > 0 && source < sourceLimit) {
2306
0
        entry=stateTable[0][*source++];
2307
        /* MBCS_ENTRY_IS_FINAL(entry) */
2308
2309
        /* test the most common case first */
2310
0
        if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2311
            /* output BMP code point */
2312
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2313
0
            --targetCapacity;
2314
0
            continue;
2315
0
        }
2316
2317
        /*
2318
         * An if-else-if chain provides more reliable performance for
2319
         * the most common cases compared to a switch.
2320
         */
2321
0
        action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2322
0
        if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2323
0
            if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2324
                /* output BMP code point */
2325
0
                *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2326
0
                --targetCapacity;
2327
0
                continue;
2328
0
            }
2329
0
        } else if(action==MBCS_STATE_UNASSIGNED) {
2330
            /* just fall through */
2331
0
        } else if(action==MBCS_STATE_ILLEGAL) {
2332
            /* callback(illegal) */
2333
0
            *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2334
0
        } else {
2335
            /* reserved, must never occur */
2336
0
            continue;
2337
0
        }
2338
2339
        /* set offsets since the start or the last extension */
2340
0
        if(offsets!=NULL) {
2341
0
            int32_t count=(int32_t)(source-lastSource);
2342
2343
            /* predecrement: do not set the offset for the callback-causing character */
2344
0
            while(--count>0) {
2345
0
                *offsets++=sourceIndex++;
2346
0
            }
2347
            /* offset and sourceIndex are now set for the current character */
2348
0
        }
2349
2350
0
        if(U_FAILURE(*pErrorCode)) {
2351
            /* callback(illegal) */
2352
0
            break;
2353
0
        } else /* unassigned sequences indicated with byteIndex>0 */ {
2354
            /* try an extension mapping */
2355
0
            lastSource=source;
2356
0
            cnv->toUBytes[0]=*(source-1);
2357
0
            cnv->toULength=_extToU(cnv, cnv->sharedData,
2358
0
                                    1, &source, sourceLimit,
2359
0
                                    &target, pArgs->targetLimit,
2360
0
                                    &offsets, sourceIndex,
2361
0
                                    pArgs->flush,
2362
0
                                    pErrorCode);
2363
0
            sourceIndex+=1+(int32_t)(source-lastSource);
2364
2365
0
            if(U_FAILURE(*pErrorCode)) {
2366
                /* not mappable or buffer overflow */
2367
0
                break;
2368
0
            }
2369
2370
            /* recalculate the targetCapacity after an extension mapping */
2371
0
            targetCapacity=(int32_t)(pArgs->targetLimit-target);
2372
0
            length=(int32_t)(sourceLimit-source);
2373
0
            if(length<targetCapacity) {
2374
0
                targetCapacity=length;
2375
0
            }
2376
0
        }
2377
2378
0
#if MBCS_UNROLL_SINGLE_TO_BMP
2379
        /* unrolling makes it faster on Pentium III/Windows 2000 */
2380
0
        goto unrolled;
2381
0
#endif
2382
0
    }
2383
2384
0
    if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=pArgs->targetLimit) {
2385
        /* target is full */
2386
0
        *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2387
0
    }
2388
2389
    /* set offsets since the start or the last callback */
2390
0
    if(offsets!=NULL) {
2391
0
        size_t count=source-lastSource;
2392
0
        while(count>0) {
2393
0
            *offsets++=sourceIndex++;
2394
0
            --count;
2395
0
        }
2396
0
    }
2397
2398
    /* write back the updated pointers */
2399
0
    pArgs->source=(const char *)source;
2400
0
    pArgs->target=target;
2401
0
    pArgs->offsets=offsets;
2402
0
}
2403
2404
static UBool
2405
0
hasValidTrailBytes(const int32_t (*stateTable)[256], uint8_t state) {
2406
0
    const int32_t *row=stateTable[state];
2407
0
    int32_t b, entry;
2408
    /* First test for final entries in this state for some commonly valid byte values. */
2409
0
    entry=row[0xa1];
2410
0
    if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
2411
0
        MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
2412
0
    ) {
2413
0
        return TRUE;
2414
0
    }
2415
0
    entry=row[0x41];
2416
0
    if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
2417
0
        MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
2418
0
    ) {
2419
0
        return TRUE;
2420
0
    }
2421
    /* Then test for final entries in this state. */
2422
0
    for(b=0; b<=0xff; ++b) {
2423
0
        entry=row[b];
2424
0
        if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
2425
0
            MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
2426
0
        ) {
2427
0
            return TRUE;
2428
0
        }
2429
0
    }
2430
    /* Then recurse for transition entries. */
2431
0
    for(b=0; b<=0xff; ++b) {
2432
0
        entry=row[b];
2433
0
        if( MBCS_ENTRY_IS_TRANSITION(entry) &&
2434
0
            hasValidTrailBytes(stateTable, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry))
2435
0
        ) {
2436
0
            return TRUE;
2437
0
        }
2438
0
    }
2439
0
    return FALSE;
2440
0
}
2441
2442
/*
2443
 * Is byte b a single/lead byte in this state?
2444
 * Recurse for transition states, because here we don't want to say that
2445
 * b is a lead byte if all byte sequences that start with b are illegal.
2446
 */
2447
static UBool
2448
0
isSingleOrLead(const int32_t (*stateTable)[256], uint8_t state, UBool isDBCSOnly, uint8_t b) {
2449
0
    const int32_t *row=stateTable[state];
2450
0
    int32_t entry=row[b];
2451
0
    if(MBCS_ENTRY_IS_TRANSITION(entry)) {   /* lead byte */
2452
0
        return hasValidTrailBytes(stateTable, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry));
2453
0
    } else {
2454
0
        uint8_t action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2455
0
        if(action==MBCS_STATE_CHANGE_ONLY && isDBCSOnly) {
2456
0
            return FALSE;   /* SI/SO are illegal for DBCS-only conversion */
2457
0
        } else {
2458
0
            return action!=MBCS_STATE_ILLEGAL;
2459
0
        }
2460
0
    }
2461
0
}
2462
2463
U_CFUNC void
2464
ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
2465
0
                          UErrorCode *pErrorCode) {
2466
0
    UConverter *cnv;
2467
0
    const uint8_t *source, *sourceLimit;
2468
0
    UChar *target;
2469
0
    const UChar *targetLimit;
2470
0
    int32_t *offsets;
2471
2472
0
    const int32_t (*stateTable)[256];
2473
0
    const uint16_t *unicodeCodeUnits;
2474
2475
0
    uint32_t offset;
2476
0
    uint8_t state;
2477
0
    int8_t byteIndex;
2478
0
    uint8_t *bytes;
2479
2480
0
    int32_t sourceIndex, nextSourceIndex;
2481
2482
0
    int32_t entry;
2483
0
    UChar c;
2484
0
    uint8_t action;
2485
2486
    /* use optimized function if possible */
2487
0
    cnv=pArgs->converter;
2488
2489
0
    if(cnv->preToULength>0) {
2490
        /*
2491
         * pass sourceIndex=-1 because we continue from an earlier buffer
2492
         * in the future, this may change with continuous offsets
2493
         */
2494
0
        ucnv_extContinueMatchToU(cnv, pArgs, -1, pErrorCode);
2495
2496
0
        if(U_FAILURE(*pErrorCode) || cnv->preToULength<0) {
2497
0
            return;
2498
0
        }
2499
0
    }
2500
2501
0
    if(cnv->sharedData->mbcs.countStates==1) {
2502
0
        if(!(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
2503
0
            ucnv_MBCSSingleToBMPWithOffsets(pArgs, pErrorCode);
2504
0
        } else {
2505
0
            ucnv_MBCSSingleToUnicodeWithOffsets(pArgs, pErrorCode);
2506
0
        }
2507
0
        return;
2508
0
    }
2509
2510
    /* set up the local pointers */
2511
0
    source=(const uint8_t *)pArgs->source;
2512
0
    sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2513
0
    target=pArgs->target;
2514
0
    targetLimit=pArgs->targetLimit;
2515
0
    offsets=pArgs->offsets;
2516
2517
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2518
0
        stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2519
0
    } else {
2520
0
        stateTable=cnv->sharedData->mbcs.stateTable;
2521
0
    }
2522
0
    unicodeCodeUnits=cnv->sharedData->mbcs.unicodeCodeUnits;
2523
2524
    /* get the converter state from UConverter */
2525
0
    offset=cnv->toUnicodeStatus;
2526
0
    byteIndex=cnv->toULength;
2527
0
    bytes=cnv->toUBytes;
2528
2529
    /*
2530
     * if we are in the SBCS state for a DBCS-only converter,
2531
     * then load the DBCS state from the MBCS data
2532
     * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2533
     */
2534
0
    if((state=(uint8_t)(cnv->mode))==0) {
2535
0
        state=cnv->sharedData->mbcs.dbcsOnlyState;
2536
0
    }
2537
2538
    /* sourceIndex=-1 if the current character began in the previous buffer */
2539
0
    sourceIndex=byteIndex==0 ? 0 : -1;
2540
0
    nextSourceIndex=0;
2541
2542
    /* conversion loop */
2543
0
    while(source<sourceLimit) {
2544
        /*
2545
         * This following test is to see if available input would overflow the output.
2546
         * It does not catch output of more than one code unit that
2547
         * overflows as a result of a surrogate pair or callback output
2548
         * from the last source byte.
2549
         * Therefore, those situations also test for overflows and will
2550
         * then break the loop, too.
2551
         */
2552
0
        if(target>=targetLimit) {
2553
            /* target is full */
2554
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2555
0
            break;
2556
0
        }
2557
2558
0
        if(byteIndex==0) {
2559
            /* optimized loop for 1/2-byte input and BMP output */
2560
0
            if(offsets==NULL) {
2561
0
                do {
2562
0
                    entry=stateTable[state][*source];
2563
0
                    if(MBCS_ENTRY_IS_TRANSITION(entry)) {
2564
0
                        state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
2565
0
                        offset=MBCS_ENTRY_TRANSITION_OFFSET(entry);
2566
2567
0
                        ++source;
2568
0
                        if( source<sourceLimit &&
2569
0
                            MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
2570
0
                            MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
2571
0
                            (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
2572
0
                        ) {
2573
0
                            ++source;
2574
0
                            *target++=c;
2575
0
                            state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2576
0
                            offset=0;
2577
0
                        } else {
2578
                            /* set the state and leave the optimized loop */
2579
0
                            bytes[0]=*(source-1);
2580
0
                            byteIndex=1;
2581
0
                            break;
2582
0
                        }
2583
0
                    } else {
2584
0
                        if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2585
                            /* output BMP code point */
2586
0
                            ++source;
2587
0
                            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2588
0
                            state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2589
0
                        } else {
2590
                            /* leave the optimized loop */
2591
0
                            break;
2592
0
                        }
2593
0
                    }
2594
0
                } while(source<sourceLimit && target<targetLimit);
2595
0
            } else /* offsets!=NULL */ {
2596
0
                do {
2597
0
                    entry=stateTable[state][*source];
2598
0
                    if(MBCS_ENTRY_IS_TRANSITION(entry)) {
2599
0
                        state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
2600
0
                        offset=MBCS_ENTRY_TRANSITION_OFFSET(entry);
2601
2602
0
                        ++source;
2603
0
                        if( source<sourceLimit &&
2604
0
                            MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
2605
0
                            MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
2606
0
                            (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
2607
0
                        ) {
2608
0
                            ++source;
2609
0
                            *target++=c;
2610
0
                            if(offsets!=NULL) {
2611
0
                                *offsets++=sourceIndex;
2612
0
                                sourceIndex=(nextSourceIndex+=2);
2613
0
                            }
2614
0
                            state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2615
0
                            offset=0;
2616
0
                        } else {
2617
                            /* set the state and leave the optimized loop */
2618
0
                            ++nextSourceIndex;
2619
0
                            bytes[0]=*(source-1);
2620
0
                            byteIndex=1;
2621
0
                            break;
2622
0
                        }
2623
0
                    } else {
2624
0
                        if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2625
                            /* output BMP code point */
2626
0
                            ++source;
2627
0
                            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2628
0
                            if(offsets!=NULL) {
2629
0
                                *offsets++=sourceIndex;
2630
0
                                sourceIndex=++nextSourceIndex;
2631
0
                            }
2632
0
                            state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2633
0
                        } else {
2634
                            /* leave the optimized loop */
2635
0
                            break;
2636
0
                        }
2637
0
                    }
2638
0
                } while(source<sourceLimit && target<targetLimit);
2639
0
            }
2640
2641
            /*
2642
             * these tests and break statements could be put inside the loop
2643
             * if C had "break outerLoop" like Java
2644
             */
2645
0
            if(source>=sourceLimit) {
2646
0
                break;
2647
0
            }
2648
0
            if(target>=targetLimit) {
2649
                /* target is full */
2650
0
                *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2651
0
                break;
2652
0
            }
2653
2654
0
            ++nextSourceIndex;
2655
0
            bytes[byteIndex++]=*source++;
2656
0
        } else /* byteIndex>0 */ {
2657
0
            ++nextSourceIndex;
2658
0
            entry=stateTable[state][bytes[byteIndex++]=*source++];
2659
0
        }
2660
2661
0
        if(MBCS_ENTRY_IS_TRANSITION(entry)) {
2662
0
            state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
2663
0
            offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
2664
0
            continue;
2665
0
        }
2666
2667
        /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2668
0
        cnv->mode=state;
2669
2670
        /* set the next state early so that we can reuse the entry variable */
2671
0
        state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2672
2673
        /*
2674
         * An if-else-if chain provides more reliable performance for
2675
         * the most common cases compared to a switch.
2676
         */
2677
0
        action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2678
0
        if(action==MBCS_STATE_VALID_16) {
2679
0
            offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
2680
0
            c=unicodeCodeUnits[offset];
2681
0
            if(c<0xfffe) {
2682
                /* output BMP code point */
2683
0
                *target++=c;
2684
0
                if(offsets!=NULL) {
2685
0
                    *offsets++=sourceIndex;
2686
0
                }
2687
0
                byteIndex=0;
2688
0
            } else if(c==0xfffe) {
2689
0
                if(UCNV_TO_U_USE_FALLBACK(cnv) && (entry=(int32_t)ucnv_MBCSGetFallback(&cnv->sharedData->mbcs, offset))!=0xfffe) {
2690
                    /* output fallback BMP code point */
2691
0
                    *target++=(UChar)entry;
2692
0
                    if(offsets!=NULL) {
2693
0
                        *offsets++=sourceIndex;
2694
0
                    }
2695
0
                    byteIndex=0;
2696
0
                }
2697
0
            } else {
2698
                /* callback(illegal) */
2699
0
                *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2700
0
            }
2701
0
        } else if(action==MBCS_STATE_VALID_DIRECT_16) {
2702
            /* output BMP code point */
2703
0
            *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2704
0
            if(offsets!=NULL) {
2705
0
                *offsets++=sourceIndex;
2706
0
            }
2707
0
            byteIndex=0;
2708
0
        } else if(action==MBCS_STATE_VALID_16_PAIR) {
2709
0
            offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
2710
0
            c=unicodeCodeUnits[offset++];
2711
0
            if(c<0xd800) {
2712
                /* output BMP code point below 0xd800 */
2713
0
                *target++=c;
2714
0
                if(offsets!=NULL) {
2715
0
                    *offsets++=sourceIndex;
2716
0
                }
2717
0
                byteIndex=0;
2718
0
            } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
2719
                /* output roundtrip or fallback surrogate pair */
2720
0
                *target++=(UChar)(c&0xdbff);
2721
0
                if(offsets!=NULL) {
2722
0
                    *offsets++=sourceIndex;
2723
0
                }
2724
0
                byteIndex=0;
2725
0
                if(target<targetLimit) {
2726
0
                    *target++=unicodeCodeUnits[offset];
2727
0
                    if(offsets!=NULL) {
2728
0
                        *offsets++=sourceIndex;
2729
0
                    }
2730
0
                } else {
2731
                    /* target overflow */
2732
0
                    cnv->UCharErrorBuffer[0]=unicodeCodeUnits[offset];
2733
0
                    cnv->UCharErrorBufferLength=1;
2734
0
                    *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2735
2736
0
                    offset=0;
2737
0
                    break;
2738
0
                }
2739
0
            } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
2740
                /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2741
0
                *target++=unicodeCodeUnits[offset];
2742
0
                if(offsets!=NULL) {
2743
0
                    *offsets++=sourceIndex;
2744
0
                }
2745
0
                byteIndex=0;
2746
0
            } else if(c==0xffff) {
2747
                /* callback(illegal) */
2748
0
                *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2749
0
            }
2750
0
        } else if(action==MBCS_STATE_VALID_DIRECT_20 ||
2751
0
                  (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
2752
0
        ) {
2753
0
            entry=MBCS_ENTRY_FINAL_VALUE(entry);
2754
            /* output surrogate pair */
2755
0
            *target++=(UChar)(0xd800|(UChar)(entry>>10));
2756
0
            if(offsets!=NULL) {
2757
0
                *offsets++=sourceIndex;
2758
0
            }
2759
0
            byteIndex=0;
2760
0
            c=(UChar)(0xdc00|(UChar)(entry&0x3ff));
2761
0
            if(target<targetLimit) {
2762
0
                *target++=c;
2763
0
                if(offsets!=NULL) {
2764
0
                    *offsets++=sourceIndex;
2765
0
                }
2766
0
            } else {
2767
                /* target overflow */
2768
0
                cnv->UCharErrorBuffer[0]=c;
2769
0
                cnv->UCharErrorBufferLength=1;
2770
0
                *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2771
2772
0
                offset=0;
2773
0
                break;
2774
0
            }
2775
0
        } else if(action==MBCS_STATE_CHANGE_ONLY) {
2776
            /*
2777
             * This serves as a state change without any output.
2778
             * It is useful for reading simple stateful encodings,
2779
             * for example using just Shift-In/Shift-Out codes.
2780
             * The 21 unused bits may later be used for more sophisticated
2781
             * state transitions.
2782
             */
2783
0
            if(cnv->sharedData->mbcs.dbcsOnlyState==0) {
2784
0
                byteIndex=0;
2785
0
            } else {
2786
                /* SI/SO are illegal for DBCS-only conversion */
2787
0
                state=(uint8_t)(cnv->mode); /* restore the previous state */
2788
2789
                /* callback(illegal) */
2790
0
                *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2791
0
            }
2792
0
        } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2793
0
            if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2794
                /* output BMP code point */
2795
0
                *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2796
0
                if(offsets!=NULL) {
2797
0
                    *offsets++=sourceIndex;
2798
0
                }
2799
0
                byteIndex=0;
2800
0
            }
2801
0
        } else if(action==MBCS_STATE_UNASSIGNED) {
2802
            /* just fall through */
2803
0
        } else if(action==MBCS_STATE_ILLEGAL) {
2804
            /* callback(illegal) */
2805
0
            *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2806
0
        } else {
2807
            /* reserved, must never occur */
2808
0
            byteIndex=0;
2809
0
        }
2810
2811
        /* end of action codes: prepare for a new character */
2812
0
        offset=0;
2813
2814
0
        if(byteIndex==0) {
2815
0
            sourceIndex=nextSourceIndex;
2816
0
        } else if(U_FAILURE(*pErrorCode)) {
2817
            /* callback(illegal) */
2818
0
            if(byteIndex>1) {
2819
                /*
2820
                 * Ticket 5691: consistent illegal sequences:
2821
                 * - We include at least the first byte in the illegal sequence.
2822
                 * - If any of the non-initial bytes could be the start of a character,
2823
                 *   we stop the illegal sequence before the first one of those.
2824
                 */
2825
0
                UBool isDBCSOnly=(UBool)(cnv->sharedData->mbcs.dbcsOnlyState!=0);
2826
0
                int8_t i;
2827
0
                for(i=1;
2828
0
                    i<byteIndex && !isSingleOrLead(stateTable, state, isDBCSOnly, bytes[i]);
2829
0
                    ++i) {}
2830
0
                if(i<byteIndex) {
2831
                    /* Back out some bytes. */
2832
0
                    int8_t backOutDistance=byteIndex-i;
2833
0
                    int32_t bytesFromThisBuffer=(int32_t)(source-(const uint8_t *)pArgs->source);
2834
0
                    byteIndex=i;  /* length of reported illegal byte sequence */
2835
0
                    if(backOutDistance<=bytesFromThisBuffer) {
2836
0
                        source-=backOutDistance;
2837
0
                    } else {
2838
                        /* Back out bytes from the previous buffer: Need to replay them. */
2839
0
                        cnv->preToULength=(int8_t)(bytesFromThisBuffer-backOutDistance);
2840
                        /* preToULength is negative! */
2841
0
                        uprv_memcpy(cnv->preToU, bytes+i, -cnv->preToULength);
2842
0
                        source=(const uint8_t *)pArgs->source;
2843
0
                    }
2844
0
                }
2845
0
            }
2846
0
            break;
2847
0
        } else /* unassigned sequences indicated with byteIndex>0 */ {
2848
            /* try an extension mapping */
2849
0
            pArgs->source=(const char *)source;
2850
0
            byteIndex=_extToU(cnv, cnv->sharedData,
2851
0
                              byteIndex, &source, sourceLimit,
2852
0
                              &target, targetLimit,
2853
0
                              &offsets, sourceIndex,
2854
0
                              pArgs->flush,
2855
0
                              pErrorCode);
2856
0
            sourceIndex=nextSourceIndex+=(int32_t)(source-(const uint8_t *)pArgs->source);
2857
2858
0
            if(U_FAILURE(*pErrorCode)) {
2859
                /* not mappable or buffer overflow */
2860
0
                break;
2861
0
            }
2862
0
        }
2863
0
    }
2864
2865
    /* set the converter state back into UConverter */
2866
0
    cnv->toUnicodeStatus=offset;
2867
0
    cnv->mode=state;
2868
0
    cnv->toULength=byteIndex;
2869
2870
    /* write back the updated pointers */
2871
0
    pArgs->source=(const char *)source;
2872
0
    pArgs->target=target;
2873
0
    pArgs->offsets=offsets;
2874
0
}
2875
2876
/*
2877
 * This version of ucnv_MBCSGetNextUChar() is optimized for single-byte, single-state codepages.
2878
 * We still need a conversion loop in case we find reserved action codes, which are to be ignored.
2879
 */
2880
static UChar32
2881
ucnv_MBCSSingleGetNextUChar(UConverterToUnicodeArgs *pArgs,
2882
0
                        UErrorCode *pErrorCode) {
2883
0
    UConverter *cnv;
2884
0
    const int32_t (*stateTable)[256];
2885
0
    const uint8_t *source, *sourceLimit;
2886
2887
0
    int32_t entry;
2888
0
    uint8_t action;
2889
2890
    /* set up the local pointers */
2891
0
    cnv=pArgs->converter;
2892
0
    source=(const uint8_t *)pArgs->source;
2893
0
    sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2894
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2895
0
        stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2896
0
    } else {
2897
0
        stateTable=cnv->sharedData->mbcs.stateTable;
2898
0
    }
2899
2900
    /* conversion loop */
2901
0
    while(source<sourceLimit) {
2902
0
        entry=stateTable[0][*source++];
2903
        /* MBCS_ENTRY_IS_FINAL(entry) */
2904
2905
        /* write back the updated pointer early so that we can return directly */
2906
0
        pArgs->source=(const char *)source;
2907
2908
0
        if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2909
            /* output BMP code point */
2910
0
            return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2911
0
        }
2912
2913
        /*
2914
         * An if-else-if chain provides more reliable performance for
2915
         * the most common cases compared to a switch.
2916
         */
2917
0
        action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2918
0
        if( action==MBCS_STATE_VALID_DIRECT_20 ||
2919
0
            (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
2920
0
        ) {
2921
            /* output supplementary code point */
2922
0
            return (UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
2923
0
        } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2924
0
            if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2925
                /* output BMP code point */
2926
0
                return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2927
0
            }
2928
0
        } else if(action==MBCS_STATE_UNASSIGNED) {
2929
            /* just fall through */
2930
0
        } else if(action==MBCS_STATE_ILLEGAL) {
2931
            /* callback(illegal) */
2932
0
            *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2933
0
        } else {
2934
            /* reserved, must never occur */
2935
0
            continue;
2936
0
        }
2937
2938
0
        if(U_FAILURE(*pErrorCode)) {
2939
            /* callback(illegal) */
2940
0
            break;
2941
0
        } else /* unassigned sequence */ {
2942
            /* defer to the generic implementation */
2943
0
            pArgs->source=(const char *)source-1;
2944
0
            return UCNV_GET_NEXT_UCHAR_USE_TO_U;
2945
0
        }
2946
0
    }
2947
2948
    /* no output because of empty input or only state changes */
2949
0
    *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
2950
0
    return 0xffff;
2951
0
}
2952
2953
/*
2954
 * Version of _MBCSToUnicodeWithOffsets() optimized for single-character
2955
 * conversion without offset handling.
2956
 *
2957
 * When a character does not have a mapping to Unicode, then we return to the
2958
 * generic ucnv_getNextUChar() code for extension/GB 18030 and error/callback
2959
 * handling.
2960
 * We also defer to the generic code in other complicated cases and have them
2961
 * ultimately handled by _MBCSToUnicodeWithOffsets() itself.
2962
 *
2963
 * All normal mappings and errors are handled here.
2964
 */
2965
static UChar32 U_CALLCONV
2966
ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
2967
0
                  UErrorCode *pErrorCode) {
2968
0
    UConverter *cnv;
2969
0
    const uint8_t *source, *sourceLimit, *lastSource;
2970
2971
0
    const int32_t (*stateTable)[256];
2972
0
    const uint16_t *unicodeCodeUnits;
2973
2974
0
    uint32_t offset;
2975
0
    uint8_t state;
2976
2977
0
    int32_t entry;
2978
0
    UChar32 c;
2979
0
    uint8_t action;
2980
2981
    /* use optimized function if possible */
2982
0
    cnv=pArgs->converter;
2983
2984
0
    if(cnv->preToULength>0) {
2985
        /* use the generic code in ucnv_getNextUChar() to continue with a partial match */
2986
0
        return UCNV_GET_NEXT_UCHAR_USE_TO_U;
2987
0
    }
2988
2989
0
    if(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SURROGATES) {
2990
        /*
2991
         * Using the generic ucnv_getNextUChar() code lets us deal correctly
2992
         * with the rare case of a codepage that maps single surrogates
2993
         * without adding the complexity to this already complicated function here.
2994
         */
2995
0
        return UCNV_GET_NEXT_UCHAR_USE_TO_U;
2996
0
    } else if(cnv->sharedData->mbcs.countStates==1) {
2997
0
        return ucnv_MBCSSingleGetNextUChar(pArgs, pErrorCode);
2998
0
    }
2999
3000
    /* set up the local pointers */
3001
0
    source=lastSource=(const uint8_t *)pArgs->source;
3002
0
    sourceLimit=(const uint8_t *)pArgs->sourceLimit;
3003
3004
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3005
0
        stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
3006
0
    } else {
3007
0
        stateTable=cnv->sharedData->mbcs.stateTable;
3008
0
    }
3009
0
    unicodeCodeUnits=cnv->sharedData->mbcs.unicodeCodeUnits;
3010
3011
    /* get the converter state from UConverter */
3012
0
    offset=cnv->toUnicodeStatus;
3013
3014
    /*
3015
     * if we are in the SBCS state for a DBCS-only converter,
3016
     * then load the DBCS state from the MBCS data
3017
     * (dbcsOnlyState==0 if it is not a DBCS-only converter)
3018
     */
3019
0
    if((state=(uint8_t)(cnv->mode))==0) {
3020
0
        state=cnv->sharedData->mbcs.dbcsOnlyState;
3021
0
    }
3022
3023
    /* conversion loop */
3024
0
    c=U_SENTINEL;
3025
0
    while(source<sourceLimit) {
3026
0
        entry=stateTable[state][*source++];
3027
0
        if(MBCS_ENTRY_IS_TRANSITION(entry)) {
3028
0
            state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
3029
0
            offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
3030
3031
            /* optimization for 1/2-byte input and BMP output */
3032
0
            if( source<sourceLimit &&
3033
0
                MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
3034
0
                MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
3035
0
                (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
3036
0
            ) {
3037
0
                ++source;
3038
0
                state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
3039
                /* output BMP code point */
3040
0
                break;
3041
0
            }
3042
0
        } else {
3043
            /* save the previous state for proper extension mapping with SI/SO-stateful converters */
3044
0
            cnv->mode=state;
3045
3046
            /* set the next state early so that we can reuse the entry variable */
3047
0
            state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
3048
3049
            /*
3050
             * An if-else-if chain provides more reliable performance for
3051
             * the most common cases compared to a switch.
3052
             */
3053
0
            action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
3054
0
            if(action==MBCS_STATE_VALID_DIRECT_16) {
3055
                /* output BMP code point */
3056
0
                c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3057
0
                break;
3058
0
            } else if(action==MBCS_STATE_VALID_16) {
3059
0
                offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
3060
0
                c=unicodeCodeUnits[offset];
3061
0
                if(c<0xfffe) {
3062
                    /* output BMP code point */
3063
0
                    break;
3064
0
                } else if(c==0xfffe) {
3065
0
                    if(UCNV_TO_U_USE_FALLBACK(cnv) && (c=ucnv_MBCSGetFallback(&cnv->sharedData->mbcs, offset))!=0xfffe) {
3066
0
                        break;
3067
0
                    }
3068
0
                } else {
3069
                    /* callback(illegal) */
3070
0
                    *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3071
0
                }
3072
0
            } else if(action==MBCS_STATE_VALID_16_PAIR) {
3073
0
                offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
3074
0
                c=unicodeCodeUnits[offset++];
3075
0
                if(c<0xd800) {
3076
                    /* output BMP code point below 0xd800 */
3077
0
                    break;
3078
0
                } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
3079
                    /* output roundtrip or fallback supplementary code point */
3080
0
                    c=((c&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00);
3081
0
                    break;
3082
0
                } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
3083
                    /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
3084
0
                    c=unicodeCodeUnits[offset];
3085
0
                    break;
3086
0
                } else if(c==0xffff) {
3087
                    /* callback(illegal) */
3088
0
                    *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3089
0
                }
3090
0
            } else if(action==MBCS_STATE_VALID_DIRECT_20 ||
3091
0
                      (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
3092
0
            ) {
3093
                /* output supplementary code point */
3094
0
                c=(UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
3095
0
                break;
3096
0
            } else if(action==MBCS_STATE_CHANGE_ONLY) {
3097
                /*
3098
                 * This serves as a state change without any output.
3099
                 * It is useful for reading simple stateful encodings,
3100
                 * for example using just Shift-In/Shift-Out codes.
3101
                 * The 21 unused bits may later be used for more sophisticated
3102
                 * state transitions.
3103
                 */
3104
0
                if(cnv->sharedData->mbcs.dbcsOnlyState!=0) {
3105
                    /* SI/SO are illegal for DBCS-only conversion */
3106
0
                    state=(uint8_t)(cnv->mode); /* restore the previous state */
3107
3108
                    /* callback(illegal) */
3109
0
                    *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3110
0
                }
3111
0
            } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
3112
0
                if(UCNV_TO_U_USE_FALLBACK(cnv)) {
3113
                    /* output BMP code point */
3114
0
                    c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3115
0
                    break;
3116
0
                }
3117
0
            } else if(action==MBCS_STATE_UNASSIGNED) {
3118
                /* just fall through */
3119
0
            } else if(action==MBCS_STATE_ILLEGAL) {
3120
                /* callback(illegal) */
3121
0
                *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3122
0
            } else {
3123
                /* reserved (must never occur), or only state change */
3124
0
                offset=0;
3125
0
                lastSource=source;
3126
0
                continue;
3127
0
            }
3128
3129
            /* end of action codes: prepare for a new character */
3130
0
            offset=0;
3131
3132
0
            if(U_FAILURE(*pErrorCode)) {
3133
                /* callback(illegal) */
3134
0
                break;
3135
0
            } else /* unassigned sequence */ {
3136
                /* defer to the generic implementation */
3137
0
                cnv->toUnicodeStatus=0;
3138
0
                cnv->mode=state;
3139
0
                pArgs->source=(const char *)lastSource;
3140
0
                return UCNV_GET_NEXT_UCHAR_USE_TO_U;
3141
0
            }
3142
0
        }
3143
0
    }
3144
3145
0
    if(c<0) {
3146
0
        if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source) {
3147
            /* incomplete character byte sequence */
3148
0
            uint8_t *bytes=cnv->toUBytes;
3149
0
            cnv->toULength=(int8_t)(source-lastSource);
3150
0
            do {
3151
0
                *bytes++=*lastSource++;
3152
0
            } while(lastSource<source);
3153
0
            *pErrorCode=U_TRUNCATED_CHAR_FOUND;
3154
0
        } else if(U_FAILURE(*pErrorCode)) {
3155
            /* callback(illegal) */
3156
            /*
3157
             * Ticket 5691: consistent illegal sequences:
3158
             * - We include at least the first byte in the illegal sequence.
3159
             * - If any of the non-initial bytes could be the start of a character,
3160
             *   we stop the illegal sequence before the first one of those.
3161
             */
3162
0
            UBool isDBCSOnly=(UBool)(cnv->sharedData->mbcs.dbcsOnlyState!=0);
3163
0
            uint8_t *bytes=cnv->toUBytes;
3164
0
            *bytes++=*lastSource++;     /* first byte */
3165
0
            if(lastSource==source) {
3166
0
                cnv->toULength=1;
3167
0
            } else /* lastSource<source: multi-byte character */ {
3168
0
                int8_t i;
3169
0
                for(i=1;
3170
0
                    lastSource<source && !isSingleOrLead(stateTable, state, isDBCSOnly, *lastSource);
3171
0
                    ++i
3172
0
                ) {
3173
0
                    *bytes++=*lastSource++;
3174
0
                }
3175
0
                cnv->toULength=i;
3176
0
                source=lastSource;
3177
0
            }
3178
0
        } else {
3179
            /* no output because of empty input or only state changes */
3180
0
            *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
3181
0
        }
3182
0
        c=0xffff;
3183
0
    }
3184
3185
    /* set the converter state back into UConverter, ready for a new character */
3186
0
    cnv->toUnicodeStatus=0;
3187
0
    cnv->mode=state;
3188
3189
    /* write back the updated pointer */
3190
0
    pArgs->source=(const char *)source;
3191
0
    return c;
3192
0
}
3193
3194
#if 0
3195
/*
3196
 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3197
 * Removal improves code coverage.
3198
 */
3199
/**
3200
 * This version of ucnv_MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages.
3201
 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3202
 * It does not handle conversion extensions (_extToU()).
3203
 */
3204
U_CFUNC UChar32
3205
ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData *sharedData,
3206
                              uint8_t b, UBool useFallback) {
3207
    int32_t entry;
3208
    uint8_t action;
3209
3210
    entry=sharedData->mbcs.stateTable[0][b];
3211
    /* MBCS_ENTRY_IS_FINAL(entry) */
3212
3213
    if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
3214
        /* output BMP code point */
3215
        return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3216
    }
3217
3218
    /*
3219
     * An if-else-if chain provides more reliable performance for
3220
     * the most common cases compared to a switch.
3221
     */
3222
    action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
3223
    if(action==MBCS_STATE_VALID_DIRECT_20) {
3224
        /* output supplementary code point */
3225
        return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3226
    } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
3227
        if(!TO_U_USE_FALLBACK(useFallback)) {
3228
            return 0xfffe;
3229
        }
3230
        /* output BMP code point */
3231
        return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3232
    } else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
3233
        if(!TO_U_USE_FALLBACK(useFallback)) {
3234
            return 0xfffe;
3235
        }
3236
        /* output supplementary code point */
3237
        return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3238
    } else if(action==MBCS_STATE_UNASSIGNED) {
3239
        return 0xfffe;
3240
    } else if(action==MBCS_STATE_ILLEGAL) {
3241
        return 0xffff;
3242
    } else {
3243
        /* reserved, must never occur */
3244
        return 0xffff;
3245
    }
3246
}
3247
#endif
3248
3249
/*
3250
 * This is a simple version of _MBCSGetNextUChar() that is used
3251
 * by other converter implementations.
3252
 * It only returns an "assigned" result if it consumes the entire input.
3253
 * It does not use state from the converter, nor error codes.
3254
 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3255
 * It handles conversion extensions but not GB 18030.
3256
 *
3257
 * Return value:
3258
 * U+fffe   unassigned
3259
 * U+ffff   illegal
3260
 * otherwise the Unicode code point
3261
 */
3262
U_CFUNC UChar32
3263
ucnv_MBCSSimpleGetNextUChar(UConverterSharedData *sharedData,
3264
                        const char *source, int32_t length,
3265
0
                        UBool useFallback) {
3266
0
    const int32_t (*stateTable)[256];
3267
0
    const uint16_t *unicodeCodeUnits;
3268
3269
0
    uint32_t offset;
3270
0
    uint8_t state, action;
3271
3272
0
    UChar32 c;
3273
0
    int32_t i, entry;
3274
3275
0
    if(length<=0) {
3276
        /* no input at all: "illegal" */
3277
0
        return 0xffff;
3278
0
    }
3279
3280
#if 0
3281
/*
3282
 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3283
 * TODO In future releases, verify that this function is never called for SBCS
3284
 * conversions, i.e., that sharedData->mbcs.countStates==1 is still true.
3285
 * Removal improves code coverage.
3286
 */
3287
    /* use optimized function if possible */
3288
    if(sharedData->mbcs.countStates==1) {
3289
        if(length==1) {
3290
            return ucnv_MBCSSingleSimpleGetNextUChar(sharedData, (uint8_t)*source, useFallback);
3291
        } else {
3292
            return 0xffff; /* illegal: more than a single byte for an SBCS converter */
3293
        }
3294
    }
3295
#endif
3296
3297
    /* set up the local pointers */
3298
0
    stateTable=sharedData->mbcs.stateTable;
3299
0
    unicodeCodeUnits=sharedData->mbcs.unicodeCodeUnits;
3300
3301
    /* converter state */
3302
0
    offset=0;
3303
0
    state=sharedData->mbcs.dbcsOnlyState;
3304
3305
    /* conversion loop */
3306
0
    for(i=0;;) {
3307
0
        entry=stateTable[state][(uint8_t)source[i++]];
3308
0
        if(MBCS_ENTRY_IS_TRANSITION(entry)) {
3309
0
            state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
3310
0
            offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
3311
3312
0
            if(i==length) {
3313
0
                return 0xffff; /* truncated character */
3314
0
            }
3315
0
        } else {
3316
            /*
3317
             * An if-else-if chain provides more reliable performance for
3318
             * the most common cases compared to a switch.
3319
             */
3320
0
            action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
3321
0
            if(action==MBCS_STATE_VALID_16) {
3322
0
                offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
3323
0
                c=unicodeCodeUnits[offset];
3324
0
                if(c!=0xfffe) {
3325
                    /* done */
3326
0
                } else if(UCNV_TO_U_USE_FALLBACK(cnv)) {
3327
0
                    c=ucnv_MBCSGetFallback(&sharedData->mbcs, offset);
3328
                /* else done with 0xfffe */
3329
0
                }
3330
0
                break;
3331
0
            } else if(action==MBCS_STATE_VALID_DIRECT_16) {
3332
                /* output BMP code point */
3333
0
                c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3334
0
                break;
3335
0
            } else if(action==MBCS_STATE_VALID_16_PAIR) {
3336
0
                offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
3337
0
                c=unicodeCodeUnits[offset++];
3338
0
                if(c<0xd800) {
3339
                    /* output BMP code point below 0xd800 */
3340
0
                } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
3341
                    /* output roundtrip or fallback supplementary code point */
3342
0
                    c=(UChar32)(((c&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00));
3343
0
                } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
3344
                    /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
3345
0
                    c=unicodeCodeUnits[offset];
3346
0
                } else if(c==0xffff) {
3347
0
                    return 0xffff;
3348
0
                } else {
3349
0
                    c=0xfffe;
3350
0
                }
3351
0
                break;
3352
0
            } else if(action==MBCS_STATE_VALID_DIRECT_20) {
3353
                /* output supplementary code point */
3354
0
                c=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3355
0
                break;
3356
0
            } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
3357
0
                if(!TO_U_USE_FALLBACK(useFallback)) {
3358
0
                    c=0xfffe;
3359
0
                    break;
3360
0
                }
3361
                /* output BMP code point */
3362
0
                c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3363
0
                break;
3364
0
            } else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
3365
0
                if(!TO_U_USE_FALLBACK(useFallback)) {
3366
0
                    c=0xfffe;
3367
0
                    break;
3368
0
                }
3369
                /* output supplementary code point */
3370
0
                c=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3371
0
                break;
3372
0
            } else if(action==MBCS_STATE_UNASSIGNED) {
3373
0
                c=0xfffe;
3374
0
                break;
3375
0
            }
3376
3377
            /*
3378
             * forbid MBCS_STATE_CHANGE_ONLY for this function,
3379
             * and MBCS_STATE_ILLEGAL and reserved action codes
3380
             */
3381
0
            return 0xffff;
3382
0
        }
3383
0
    }
3384
3385
0
    if(i!=length) {
3386
        /* illegal for this function: not all input consumed */
3387
0
        return 0xffff;
3388
0
    }
3389
3390
0
    if(c==0xfffe) {
3391
        /* try an extension mapping */
3392
0
        const int32_t *cx=sharedData->mbcs.extIndexes;
3393
0
        if(cx!=NULL) {
3394
0
            return ucnv_extSimpleMatchToU(cx, source, length, useFallback);
3395
0
        }
3396
0
    }
3397
3398
0
    return c;
3399
0
}
3400
3401
/* MBCS-from-Unicode conversion functions ----------------------------------- */
3402
3403
/* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for double-byte codepages. */
3404
static void
3405
ucnv_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
3406
0
                                  UErrorCode *pErrorCode) {
3407
0
    UConverter *cnv;
3408
0
    const UChar *source, *sourceLimit;
3409
0
    uint8_t *target;
3410
0
    int32_t targetCapacity;
3411
0
    int32_t *offsets;
3412
3413
0
    const uint16_t *table;
3414
0
    const uint16_t *mbcsIndex;
3415
0
    const uint8_t *bytes;
3416
3417
0
    UChar32 c;
3418
3419
0
    int32_t sourceIndex, nextSourceIndex;
3420
3421
0
    uint32_t stage2Entry;
3422
0
    uint32_t asciiRoundtrips;
3423
0
    uint32_t value;
3424
0
    uint8_t unicodeMask;
3425
3426
    /* use optimized function if possible */
3427
0
    cnv=pArgs->converter;
3428
0
    unicodeMask=cnv->sharedData->mbcs.unicodeMask;
3429
3430
    /* set up the local pointers */
3431
0
    source=pArgs->source;
3432
0
    sourceLimit=pArgs->sourceLimit;
3433
0
    target=(uint8_t *)pArgs->target;
3434
0
    targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
3435
0
    offsets=pArgs->offsets;
3436
3437
0
    table=cnv->sharedData->mbcs.fromUnicodeTable;
3438
0
    mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
3439
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3440
0
        bytes=cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
3441
0
    } else {
3442
0
        bytes=cnv->sharedData->mbcs.fromUnicodeBytes;
3443
0
    }
3444
0
    asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
3445
3446
    /* get the converter state from UConverter */
3447
0
    c=cnv->fromUChar32;
3448
3449
    /* sourceIndex=-1 if the current character began in the previous buffer */
3450
0
    sourceIndex= c==0 ? 0 : -1;
3451
0
    nextSourceIndex=0;
3452
3453
    /* conversion loop */
3454
0
    if(c!=0 && targetCapacity>0) {
3455
0
        goto getTrail;
3456
0
    }
3457
3458
0
    while(source<sourceLimit) {
3459
        /*
3460
         * This following test is to see if available input would overflow the output.
3461
         * It does not catch output of more than one byte that
3462
         * overflows as a result of a multi-byte character or callback output
3463
         * from the last source character.
3464
         * Therefore, those situations also test for overflows and will
3465
         * then break the loop, too.
3466
         */
3467
0
        if(targetCapacity>0) {
3468
            /*
3469
             * Get a correct Unicode code point:
3470
             * a single UChar for a BMP code point or
3471
             * a matched surrogate pair for a "supplementary code point".
3472
             */
3473
0
            c=*source++;
3474
0
            ++nextSourceIndex;
3475
0
            if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
3476
0
                *target++=(uint8_t)c;
3477
0
                if(offsets!=NULL) {
3478
0
                    *offsets++=sourceIndex;
3479
0
                    sourceIndex=nextSourceIndex;
3480
0
                }
3481
0
                --targetCapacity;
3482
0
                c=0;
3483
0
                continue;
3484
0
            }
3485
            /*
3486
             * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
3487
             * to avoid dealing with surrogates.
3488
             * MBCS_FAST_MAX must be >=0xd7ff.
3489
             */
3490
0
            if(c<=0xd7ff) {
3491
0
                value=DBCS_RESULT_FROM_MOST_BMP(mbcsIndex, (const uint16_t *)bytes, c);
3492
                /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
3493
0
                if(value==0) {
3494
0
                    goto unassigned;
3495
0
                }
3496
                /* output the value */
3497
0
            } else {
3498
                /*
3499
                 * This also tests if the codepage maps single surrogates.
3500
                 * If it does, then surrogates are not paired but mapped separately.
3501
                 * Note that in this case unmatched surrogates are not detected.
3502
                 */
3503
0
                if(U16_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
3504
0
                    if(U16_IS_SURROGATE_LEAD(c)) {
3505
0
getTrail:
3506
0
                        if(source<sourceLimit) {
3507
                            /* test the following code unit */
3508
0
                            UChar trail=*source;
3509
0
                            if(U16_IS_TRAIL(trail)) {
3510
0
                                ++source;
3511
0
                                ++nextSourceIndex;
3512
0
                                c=U16_GET_SUPPLEMENTARY(c, trail);
3513
0
                                if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
3514
                                    /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3515
                                    /* callback(unassigned) */
3516
0
                                    goto unassigned;
3517
0
                                }
3518
                                /* convert this supplementary code point */
3519
                                /* exit this condition tree */
3520
0
                            } else {
3521
                                /* this is an unmatched lead code unit (1st surrogate) */
3522
                                /* callback(illegal) */
3523
0
                                *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3524
0
                                break;
3525
0
                            }
3526
0
                        } else {
3527
                            /* no more input */
3528
0
                            break;
3529
0
                        }
3530
0
                    } else {
3531
                        /* this is an unmatched trail code unit (2nd surrogate) */
3532
                        /* callback(illegal) */
3533
0
                        *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3534
0
                        break;
3535
0
                    }
3536
0
                }
3537
3538
                /* convert the Unicode code point in c into codepage bytes */
3539
0
                stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
3540
3541
                /* get the bytes and the length for the output */
3542
                /* MBCS_OUTPUT_2 */
3543
0
                value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
3544
3545
                /* is this code point assigned, or do we use fallbacks? */
3546
0
                if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
3547
0
                     (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
3548
0
                ) {
3549
                    /*
3550
                     * We allow a 0 byte output if the "assigned" bit is set for this entry.
3551
                     * There is no way with this data structure for fallback output
3552
                     * to be a zero byte.
3553
                     */
3554
3555
0
unassigned:
3556
                    /* try an extension mapping */
3557
0
                    pArgs->source=source;
3558
0
                    c=_extFromU(cnv, cnv->sharedData,
3559
0
                                c, &source, sourceLimit,
3560
0
                                &target, target+targetCapacity,
3561
0
                                &offsets, sourceIndex,
3562
0
                                pArgs->flush,
3563
0
                                pErrorCode);
3564
0
                    nextSourceIndex+=(int32_t)(source-pArgs->source);
3565
3566
0
                    if(U_FAILURE(*pErrorCode)) {
3567
                        /* not mappable or buffer overflow */
3568
0
                        break;
3569
0
                    } else {
3570
                        /* a mapping was written to the target, continue */
3571
3572
                        /* recalculate the targetCapacity after an extension mapping */
3573
0
                        targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
3574
3575
                        /* normal end of conversion: prepare for a new character */
3576
0
                        sourceIndex=nextSourceIndex;
3577
0
                        continue;
3578
0
                    }
3579
0
                }
3580
0
            }
3581
3582
            /* write the output character bytes from value and length */
3583
            /* from the first if in the loop we know that targetCapacity>0 */
3584
0
            if(value<=0xff) {
3585
                /* this is easy because we know that there is enough space */
3586
0
                *target++=(uint8_t)value;
3587
0
                if(offsets!=NULL) {
3588
0
                    *offsets++=sourceIndex;
3589
0
                }
3590
0
                --targetCapacity;
3591
0
            } else /* length==2 */ {
3592
0
                *target++=(uint8_t)(value>>8);
3593
0
                if(2<=targetCapacity) {
3594
0
                    *target++=(uint8_t)value;
3595
0
                    if(offsets!=NULL) {
3596
0
                        *offsets++=sourceIndex;
3597
0
                        *offsets++=sourceIndex;
3598
0
                    }
3599
0
                    targetCapacity-=2;
3600
0
                } else {
3601
0
                    if(offsets!=NULL) {
3602
0
                        *offsets++=sourceIndex;
3603
0
                    }
3604
0
                    cnv->charErrorBuffer[0]=(char)value;
3605
0
                    cnv->charErrorBufferLength=1;
3606
3607
                    /* target overflow */
3608
0
                    targetCapacity=0;
3609
0
                    *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
3610
0
                    c=0;
3611
0
                    break;
3612
0
                }
3613
0
            }
3614
3615
            /* normal end of conversion: prepare for a new character */
3616
0
            c=0;
3617
0
            sourceIndex=nextSourceIndex;
3618
0
            continue;
3619
0
        } else {
3620
            /* target is full */
3621
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
3622
0
            break;
3623
0
        }
3624
0
    }
3625
3626
    /* set the converter state back into UConverter */
3627
0
    cnv->fromUChar32=c;
3628
3629
    /* write back the updated pointers */
3630
0
    pArgs->source=source;
3631
0
    pArgs->target=(char *)target;
3632
0
    pArgs->offsets=offsets;
3633
0
}
3634
3635
/* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */
3636
static void
3637
ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
3638
0
                                  UErrorCode *pErrorCode) {
3639
0
    UConverter *cnv;
3640
0
    const UChar *source, *sourceLimit;
3641
0
    uint8_t *target;
3642
0
    int32_t targetCapacity;
3643
0
    int32_t *offsets;
3644
3645
0
    const uint16_t *table;
3646
0
    const uint16_t *results;
3647
3648
0
    UChar32 c;
3649
3650
0
    int32_t sourceIndex, nextSourceIndex;
3651
3652
0
    uint16_t value, minValue;
3653
0
    UBool hasSupplementary;
3654
3655
    /* set up the local pointers */
3656
0
    cnv=pArgs->converter;
3657
0
    source=pArgs->source;
3658
0
    sourceLimit=pArgs->sourceLimit;
3659
0
    target=(uint8_t *)pArgs->target;
3660
0
    targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
3661
0
    offsets=pArgs->offsets;
3662
3663
0
    table=cnv->sharedData->mbcs.fromUnicodeTable;
3664
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3665
0
        results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
3666
0
    } else {
3667
0
        results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
3668
0
    }
3669
3670
0
    if(cnv->useFallback) {
3671
        /* use all roundtrip and fallback results */
3672
0
        minValue=0x800;
3673
0
    } else {
3674
        /* use only roundtrips and fallbacks from private-use characters */
3675
0
        minValue=0xc00;
3676
0
    }
3677
0
    hasSupplementary=(UBool)(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY);
3678
3679
    /* get the converter state from UConverter */
3680
0
    c=cnv->fromUChar32;
3681
3682
    /* sourceIndex=-1 if the current character began in the previous buffer */
3683
0
    sourceIndex= c==0 ? 0 : -1;
3684
0
    nextSourceIndex=0;
3685
3686
    /* conversion loop */
3687
0
    if(c!=0 && targetCapacity>0) {
3688
0
        goto getTrail;
3689
0
    }
3690
3691
0
    while(source<sourceLimit) {
3692
        /*
3693
         * This following test is to see if available input would overflow the output.
3694
         * It does not catch output of more than one byte that
3695
         * overflows as a result of a multi-byte character or callback output
3696
         * from the last source character.
3697
         * Therefore, those situations also test for overflows and will
3698
         * then break the loop, too.
3699
         */
3700
0
        if(targetCapacity>0) {
3701
            /*
3702
             * Get a correct Unicode code point:
3703
             * a single UChar for a BMP code point or
3704
             * a matched surrogate pair for a "supplementary code point".
3705
             */
3706
0
            c=*source++;
3707
0
            ++nextSourceIndex;
3708
0
            if(U16_IS_SURROGATE(c)) {
3709
0
                if(U16_IS_SURROGATE_LEAD(c)) {
3710
0
getTrail:
3711
0
                    if(source<sourceLimit) {
3712
                        /* test the following code unit */
3713
0
                        UChar trail=*source;
3714
0
                        if(U16_IS_TRAIL(trail)) {
3715
0
                            ++source;
3716
0
                            ++nextSourceIndex;
3717
0
                            c=U16_GET_SUPPLEMENTARY(c, trail);
3718
0
                            if(!hasSupplementary) {
3719
                                /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3720
                                /* callback(unassigned) */
3721
0
                                goto unassigned;
3722
0
                            }
3723
                            /* convert this supplementary code point */
3724
                            /* exit this condition tree */
3725
0
                        } else {
3726
                            /* this is an unmatched lead code unit (1st surrogate) */
3727
                            /* callback(illegal) */
3728
0
                            *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3729
0
                            break;
3730
0
                        }
3731
0
                    } else {
3732
                        /* no more input */
3733
0
                        break;
3734
0
                    }
3735
0
                } else {
3736
                    /* this is an unmatched trail code unit (2nd surrogate) */
3737
                    /* callback(illegal) */
3738
0
                    *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3739
0
                    break;
3740
0
                }
3741
0
            }
3742
3743
            /* convert the Unicode code point in c into codepage bytes */
3744
0
            value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3745
3746
            /* is this code point assigned, or do we use fallbacks? */
3747
0
            if(value>=minValue) {
3748
                /* assigned, write the output character bytes from value and length */
3749
                /* length==1 */
3750
                /* this is easy because we know that there is enough space */
3751
0
                *target++=(uint8_t)value;
3752
0
                if(offsets!=NULL) {
3753
0
                    *offsets++=sourceIndex;
3754
0
                }
3755
0
                --targetCapacity;
3756
3757
                /* normal end of conversion: prepare for a new character */
3758
0
                c=0;
3759
0
                sourceIndex=nextSourceIndex;
3760
0
            } else { /* unassigned */
3761
0
unassigned:
3762
                /* try an extension mapping */
3763
0
                pArgs->source=source;
3764
0
                c=_extFromU(cnv, cnv->sharedData,
3765
0
                            c, &source, sourceLimit,
3766
0
                            &target, target+targetCapacity,
3767
0
                            &offsets, sourceIndex,
3768
0
                            pArgs->flush,
3769
0
                            pErrorCode);
3770
0
                nextSourceIndex+=(int32_t)(source-pArgs->source);
3771
3772
0
                if(U_FAILURE(*pErrorCode)) {
3773
                    /* not mappable or buffer overflow */
3774
0
                    break;
3775
0
                } else {
3776
                    /* a mapping was written to the target, continue */
3777
3778
                    /* recalculate the targetCapacity after an extension mapping */
3779
0
                    targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
3780
3781
                    /* normal end of conversion: prepare for a new character */
3782
0
                    sourceIndex=nextSourceIndex;
3783
0
                }
3784
0
            }
3785
0
        } else {
3786
            /* target is full */
3787
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
3788
0
            break;
3789
0
        }
3790
0
    }
3791
3792
    /* set the converter state back into UConverter */
3793
0
    cnv->fromUChar32=c;
3794
3795
    /* write back the updated pointers */
3796
0
    pArgs->source=source;
3797
0
    pArgs->target=(char *)target;
3798
0
    pArgs->offsets=offsets;
3799
0
}
3800
3801
/*
3802
 * This version of ucnv_MBCSFromUnicode() is optimized for single-byte codepages
3803
 * that map only to and from the BMP.
3804
 * In addition to single-byte/state optimizations, the offset calculations
3805
 * become much easier.
3806
 * It would be possible to use the sbcsIndex for UTF-8-friendly tables,
3807
 * but measurements have shown that this diminishes performance
3808
 * in more cases than it improves it.
3809
 * See SVN revision 21013 (2007-feb-06) for the last version with #if switches
3810
 * for various MBCS and SBCS optimizations.
3811
 */
3812
static void
3813
ucnv_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs *pArgs,
3814
0
                              UErrorCode *pErrorCode) {
3815
0
    UConverter *cnv;
3816
0
    const UChar *source, *sourceLimit, *lastSource;
3817
0
    uint8_t *target;
3818
0
    int32_t targetCapacity, length;
3819
0
    int32_t *offsets;
3820
3821
0
    const uint16_t *table;
3822
0
    const uint16_t *results;
3823
3824
0
    UChar32 c;
3825
3826
0
    int32_t sourceIndex;
3827
3828
0
    uint32_t asciiRoundtrips;
3829
0
    uint16_t value, minValue;
3830
3831
    /* set up the local pointers */
3832
0
    cnv=pArgs->converter;
3833
0
    source=pArgs->source;
3834
0
    sourceLimit=pArgs->sourceLimit;
3835
0
    target=(uint8_t *)pArgs->target;
3836
0
    targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
3837
0
    offsets=pArgs->offsets;
3838
3839
0
    table=cnv->sharedData->mbcs.fromUnicodeTable;
3840
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3841
0
        results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
3842
0
    } else {
3843
0
        results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
3844
0
    }
3845
0
    asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
3846
3847
0
    if(cnv->useFallback) {
3848
        /* use all roundtrip and fallback results */
3849
0
        minValue=0x800;
3850
0
    } else {
3851
        /* use only roundtrips and fallbacks from private-use characters */
3852
0
        minValue=0xc00;
3853
0
    }
3854
3855
    /* get the converter state from UConverter */
3856
0
    c=cnv->fromUChar32;
3857
3858
    /* sourceIndex=-1 if the current character began in the previous buffer */
3859
0
    sourceIndex= c==0 ? 0 : -1;
3860
0
    lastSource=source;
3861
3862
    /*
3863
     * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
3864
     * for the minimum of the sourceLength and targetCapacity
3865
     */
3866
0
    length=(int32_t)(sourceLimit-source);
3867
0
    if(length<targetCapacity) {
3868
0
        targetCapacity=length;
3869
0
    }
3870
3871
    /* conversion loop */
3872
0
    if(c!=0 && targetCapacity>0) {
3873
0
        goto getTrail;
3874
0
    }
3875
3876
#if MBCS_UNROLL_SINGLE_FROM_BMP
3877
    /* unrolling makes it slower on Pentium III/Windows 2000?! */
3878
    /* unroll the loop with the most common case */
3879
unrolled:
3880
    if(targetCapacity>=4) {
3881
        int32_t count, loops;
3882
        uint16_t andedValues;
3883
3884
        loops=count=targetCapacity>>2;
3885
        do {
3886
            c=*source++;
3887
            andedValues=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3888
            *target++=(uint8_t)value;
3889
            c=*source++;
3890
            andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3891
            *target++=(uint8_t)value;
3892
            c=*source++;
3893
            andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3894
            *target++=(uint8_t)value;
3895
            c=*source++;
3896
            andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3897
            *target++=(uint8_t)value;
3898
3899
            /* were all 4 entries really valid? */
3900
            if(andedValues<minValue) {
3901
                /* no, return to the first of these 4 */
3902
                source-=4;
3903
                target-=4;
3904
                break;
3905
            }
3906
        } while(--count>0);
3907
        count=loops-count;
3908
        targetCapacity-=4*count;
3909
3910
        if(offsets!=NULL) {
3911
            lastSource+=4*count;
3912
            while(count>0) {
3913
                *offsets++=sourceIndex++;
3914
                *offsets++=sourceIndex++;
3915
                *offsets++=sourceIndex++;
3916
                *offsets++=sourceIndex++;
3917
                --count;
3918
            }
3919
        }
3920
3921
        c=0;
3922
    }
3923
#endif
3924
3925
0
    while(targetCapacity>0) {
3926
        /*
3927
         * Get a correct Unicode code point:
3928
         * a single UChar for a BMP code point or
3929
         * a matched surrogate pair for a "supplementary code point".
3930
         */
3931
0
        c=*source++;
3932
        /*
3933
         * Do not immediately check for single surrogates:
3934
         * Assume that they are unassigned and check for them in that case.
3935
         * This speeds up the conversion of assigned characters.
3936
         */
3937
        /* convert the Unicode code point in c into codepage bytes */
3938
0
        if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
3939
0
            *target++=(uint8_t)c;
3940
0
            --targetCapacity;
3941
0
            c=0;
3942
0
            continue;
3943
0
        }
3944
0
        value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3945
        /* is this code point assigned, or do we use fallbacks? */
3946
0
        if(value>=minValue) {
3947
            /* assigned, write the output character bytes from value and length */
3948
            /* length==1 */
3949
            /* this is easy because we know that there is enough space */
3950
0
            *target++=(uint8_t)value;
3951
0
            --targetCapacity;
3952
3953
            /* normal end of conversion: prepare for a new character */
3954
0
            c=0;
3955
0
            continue;
3956
0
        } else if(!U16_IS_SURROGATE(c)) {
3957
            /* normal, unassigned BMP character */
3958
0
        } else if(U16_IS_SURROGATE_LEAD(c)) {
3959
0
getTrail:
3960
0
            if(source<sourceLimit) {
3961
                /* test the following code unit */
3962
0
                UChar trail=*source;
3963
0
                if(U16_IS_TRAIL(trail)) {
3964
0
                    ++source;
3965
0
                    c=U16_GET_SUPPLEMENTARY(c, trail);
3966
                    /* this codepage does not map supplementary code points */
3967
                    /* callback(unassigned) */
3968
0
                } else {
3969
                    /* this is an unmatched lead code unit (1st surrogate) */
3970
                    /* callback(illegal) */
3971
0
                    *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3972
0
                    break;
3973
0
                }
3974
0
            } else {
3975
                /* no more input */
3976
0
                if (pArgs->flush) {
3977
0
                    *pErrorCode=U_TRUNCATED_CHAR_FOUND;
3978
0
                }
3979
0
                break;
3980
0
            }
3981
0
        } else {
3982
            /* this is an unmatched trail code unit (2nd surrogate) */
3983
            /* callback(illegal) */
3984
0
            *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3985
0
            break;
3986
0
        }
3987
3988
        /* c does not have a mapping */
3989
3990
        /* get the number of code units for c to correctly advance sourceIndex */
3991
0
        length=U16_LENGTH(c);
3992
3993
        /* set offsets since the start or the last extension */
3994
0
        if(offsets!=NULL) {
3995
0
            int32_t count=(int32_t)(source-lastSource);
3996
3997
            /* do not set the offset for this character */
3998
0
            count-=length;
3999
4000
0
            while(count>0) {
4001
0
                *offsets++=sourceIndex++;
4002
0
                --count;
4003
0
            }
4004
            /* offsets and sourceIndex are now set for the current character */
4005
0
        }
4006
4007
        /* try an extension mapping */
4008
0
        lastSource=source;
4009
0
        c=_extFromU(cnv, cnv->sharedData,
4010
0
                    c, &source, sourceLimit,
4011
0
                    &target, (const uint8_t *)(pArgs->targetLimit),
4012
0
                    &offsets, sourceIndex,
4013
0
                    pArgs->flush,
4014
0
                    pErrorCode);
4015
0
        sourceIndex+=length+(int32_t)(source-lastSource);
4016
0
        lastSource=source;
4017
4018
0
        if(U_FAILURE(*pErrorCode)) {
4019
            /* not mappable or buffer overflow */
4020
0
            break;
4021
0
        } else {
4022
            /* a mapping was written to the target, continue */
4023
4024
            /* recalculate the targetCapacity after an extension mapping */
4025
0
            targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
4026
0
            length=(int32_t)(sourceLimit-source);
4027
0
            if(length<targetCapacity) {
4028
0
                targetCapacity=length;
4029
0
            }
4030
0
        }
4031
4032
#if MBCS_UNROLL_SINGLE_FROM_BMP
4033
        /* unrolling makes it slower on Pentium III/Windows 2000?! */
4034
        goto unrolled;
4035
#endif
4036
0
    }
4037
4038
0
    if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=(uint8_t *)pArgs->targetLimit) {
4039
        /* target is full */
4040
0
        *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4041
0
    }
4042
4043
    /* set offsets since the start or the last callback */
4044
0
    if(offsets!=NULL) {
4045
0
        size_t count=source-lastSource;
4046
0
        if (count > 0 && *pErrorCode == U_TRUNCATED_CHAR_FOUND) {
4047
            /*
4048
            Caller gave us a partial supplementary character,
4049
            which this function couldn't convert in any case.
4050
            The callback will handle the offset.
4051
            */
4052
0
            count--;
4053
0
        }
4054
0
        while(count>0) {
4055
0
            *offsets++=sourceIndex++;
4056
0
            --count;
4057
0
        }
4058
0
    }
4059
4060
    /* set the converter state back into UConverter */
4061
0
    cnv->fromUChar32=c;
4062
4063
    /* write back the updated pointers */
4064
0
    pArgs->source=source;
4065
0
    pArgs->target=(char *)target;
4066
0
    pArgs->offsets=offsets;
4067
0
}
4068
4069
U_CFUNC void
4070
ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
4071
0
                            UErrorCode *pErrorCode) {
4072
0
    UConverter *cnv;
4073
0
    const UChar *source, *sourceLimit;
4074
0
    uint8_t *target;
4075
0
    int32_t targetCapacity;
4076
0
    int32_t *offsets;
4077
4078
0
    const uint16_t *table;
4079
0
    const uint16_t *mbcsIndex;
4080
0
    const uint8_t *p, *bytes;
4081
0
    uint8_t outputType;
4082
4083
0
    UChar32 c;
4084
4085
0
    int32_t prevSourceIndex, sourceIndex, nextSourceIndex;
4086
4087
0
    uint32_t stage2Entry;
4088
0
    uint32_t asciiRoundtrips;
4089
0
    uint32_t value;
4090
    /* Shift-In and Shift-Out byte sequences differ by encoding scheme. */
4091
0
    uint8_t siBytes[2] = {0, 0};
4092
0
    uint8_t soBytes[2] = {0, 0};
4093
0
    uint8_t siLength, soLength;
4094
0
    int32_t length = 0, prevLength;
4095
0
    uint8_t unicodeMask;
4096
4097
0
    cnv=pArgs->converter;
4098
4099
0
    if(cnv->preFromUFirstCP>=0) {
4100
        /*
4101
         * pass sourceIndex=-1 because we continue from an earlier buffer
4102
         * in the future, this may change with continuous offsets
4103
         */
4104
0
        ucnv_extContinueMatchFromU(cnv, pArgs, -1, pErrorCode);
4105
4106
0
        if(U_FAILURE(*pErrorCode) || cnv->preFromULength<0) {
4107
0
            return;
4108
0
        }
4109
0
    }
4110
4111
    /* use optimized function if possible */
4112
0
    outputType=cnv->sharedData->mbcs.outputType;
4113
0
    unicodeMask=cnv->sharedData->mbcs.unicodeMask;
4114
0
    if(outputType==MBCS_OUTPUT_1 && !(unicodeMask&UCNV_HAS_SURROGATES)) {
4115
0
        if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
4116
0
            ucnv_MBCSSingleFromBMPWithOffsets(pArgs, pErrorCode);
4117
0
        } else {
4118
0
            ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs, pErrorCode);
4119
0
        }
4120
0
        return;
4121
0
    } else if(outputType==MBCS_OUTPUT_2 && cnv->sharedData->mbcs.utf8Friendly) {
4122
0
        ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs, pErrorCode);
4123
0
        return;
4124
0
    }
4125
4126
    /* set up the local pointers */
4127
0
    source=pArgs->source;
4128
0
    sourceLimit=pArgs->sourceLimit;
4129
0
    target=(uint8_t *)pArgs->target;
4130
0
    targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
4131
0
    offsets=pArgs->offsets;
4132
4133
0
    table=cnv->sharedData->mbcs.fromUnicodeTable;
4134
0
    if(cnv->sharedData->mbcs.utf8Friendly) {
4135
0
        mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
4136
0
    } else {
4137
0
        mbcsIndex=NULL;
4138
0
    }
4139
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
4140
0
        bytes=cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
4141
0
    } else {
4142
0
        bytes=cnv->sharedData->mbcs.fromUnicodeBytes;
4143
0
    }
4144
0
    asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
4145
4146
    /* get the converter state from UConverter */
4147
0
    c=cnv->fromUChar32;
4148
4149
0
    if(outputType==MBCS_OUTPUT_2_SISO) {
4150
0
        prevLength=cnv->fromUnicodeStatus;
4151
0
        if(prevLength==0) {
4152
            /* set the real value */
4153
0
            prevLength=1;
4154
0
        }
4155
0
    } else {
4156
        /* prevent fromUnicodeStatus from being set to something non-0 */
4157
0
        prevLength=0;
4158
0
    }
4159
4160
    /* sourceIndex=-1 if the current character began in the previous buffer */
4161
0
    prevSourceIndex=-1;
4162
0
    sourceIndex= c==0 ? 0 : -1;
4163
0
    nextSourceIndex=0;
4164
4165
    /* Get the SI/SO character for the converter */
4166
0
    siLength = getSISOBytes(SI, cnv->options, siBytes);
4167
0
    soLength = getSISOBytes(SO, cnv->options, soBytes);
4168
4169
    /* conversion loop */
4170
    /*
4171
     * This is another piece of ugly code:
4172
     * A goto into the loop if the converter state contains a first surrogate
4173
     * from the previous function call.
4174
     * It saves me to check in each loop iteration a check of if(c==0)
4175
     * and duplicating the trail-surrogate-handling code in the else
4176
     * branch of that check.
4177
     * I could not find any other way to get around this other than
4178
     * using a function call for the conversion and callback, which would
4179
     * be even more inefficient.
4180
     *
4181
     * Markus Scherer 2000-jul-19
4182
     */
4183
0
    if(c!=0 && targetCapacity>0) {
4184
0
        goto getTrail;
4185
0
    }
4186
4187
0
    while(source<sourceLimit) {
4188
        /*
4189
         * This following test is to see if available input would overflow the output.
4190
         * It does not catch output of more than one byte that
4191
         * overflows as a result of a multi-byte character or callback output
4192
         * from the last source character.
4193
         * Therefore, those situations also test for overflows and will
4194
         * then break the loop, too.
4195
         */
4196
0
        if(targetCapacity>0) {
4197
            /*
4198
             * Get a correct Unicode code point:
4199
             * a single UChar for a BMP code point or
4200
             * a matched surrogate pair for a "supplementary code point".
4201
             */
4202
0
            c=*source++;
4203
0
            ++nextSourceIndex;
4204
0
            if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
4205
0
                *target++=(uint8_t)c;
4206
0
                if(offsets!=NULL) {
4207
0
                    *offsets++=sourceIndex;
4208
0
                    prevSourceIndex=sourceIndex;
4209
0
                    sourceIndex=nextSourceIndex;
4210
0
                }
4211
0
                --targetCapacity;
4212
0
                c=0;
4213
0
                continue;
4214
0
            }
4215
            /*
4216
             * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
4217
             * to avoid dealing with surrogates.
4218
             * MBCS_FAST_MAX must be >=0xd7ff.
4219
             */
4220
0
            if(c<=0xd7ff && mbcsIndex!=NULL) {
4221
0
                value=mbcsIndex[c>>6];
4222
4223
                /* get the bytes and the length for the output (copied from below and adapted for utf8Friendly data) */
4224
                /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
4225
0
                switch(outputType) {
4226
0
                case MBCS_OUTPUT_2:
4227
0
                    value=((const uint16_t *)bytes)[value +(c&0x3f)];
4228
0
                    if(value<=0xff) {
4229
0
                        if(value==0) {
4230
0
                            goto unassigned;
4231
0
                        } else {
4232
0
                            length=1;
4233
0
                        }
4234
0
                    } else {
4235
0
                        length=2;
4236
0
                    }
4237
0
                    break;
4238
0
                case MBCS_OUTPUT_2_SISO:
4239
                    /* 1/2-byte stateful with Shift-In/Shift-Out */
4240
                    /*
4241
                     * Save the old state in the converter object
4242
                     * right here, then change the local prevLength state variable if necessary.
4243
                     * Then, if this character turns out to be unassigned or a fallback that
4244
                     * is not taken, the callback code must not save the new state in the converter
4245
                     * because the new state is for a character that is not output.
4246
                     * However, the callback must still restore the state from the converter
4247
                     * in case the callback function changed it for its output.
4248
                     */
4249
0
                    cnv->fromUnicodeStatus=prevLength; /* save the old state */
4250
0
                    value=((const uint16_t *)bytes)[value +(c&0x3f)];
4251
0
                    if(value<=0xff) {
4252
0
                        if(value==0) {
4253
0
                            goto unassigned;
4254
0
                        } else if(prevLength<=1) {
4255
0
                            length=1;
4256
0
                        } else {
4257
                            /* change from double-byte mode to single-byte */
4258
0
                            if (siLength == 1) {
4259
0
                                value|=(uint32_t)siBytes[0]<<8;
4260
0
                                length = 2;
4261
0
                            } else if (siLength == 2) {
4262
0
                                value|=(uint32_t)siBytes[1]<<8;
4263
0
                                value|=(uint32_t)siBytes[0]<<16;
4264
0
                                length = 3;
4265
0
                            }
4266
0
                            prevLength=1;
4267
0
                        }
4268
0
                    } else {
4269
0
                        if(prevLength==2) {
4270
0
                            length=2;
4271
0
                        } else {
4272
                            /* change from single-byte mode to double-byte */
4273
0
                            if (soLength == 1) {
4274
0
                                value|=(uint32_t)soBytes[0]<<16;
4275
0
                                length = 3;
4276
0
                            } else if (soLength == 2) {
4277
0
                                value|=(uint32_t)soBytes[1]<<16;
4278
0
                                value|=(uint32_t)soBytes[0]<<24;
4279
0
                                length = 4;
4280
0
                            }
4281
0
                            prevLength=2;
4282
0
                        }
4283
0
                    }
4284
0
                    break;
4285
0
                case MBCS_OUTPUT_DBCS_ONLY:
4286
                    /* table with single-byte results, but only DBCS mappings used */
4287
0
                    value=((const uint16_t *)bytes)[value +(c&0x3f)];
4288
0
                    if(value<=0xff) {
4289
                        /* no mapping or SBCS result, not taken for DBCS-only */
4290
0
                        goto unassigned;
4291
0
                    } else {
4292
0
                        length=2;
4293
0
                    }
4294
0
                    break;
4295
0
                case MBCS_OUTPUT_3:
4296
0
                    p=bytes+(value+(c&0x3f))*3;
4297
0
                    value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4298
0
                    if(value<=0xff) {
4299
0
                        if(value==0) {
4300
0
                            goto unassigned;
4301
0
                        } else {
4302
0
                            length=1;
4303
0
                        }
4304
0
                    } else if(value<=0xffff) {
4305
0
                        length=2;
4306
0
                    } else {
4307
0
                        length=3;
4308
0
                    }
4309
0
                    break;
4310
0
                case MBCS_OUTPUT_4:
4311
0
                    value=((const uint32_t *)bytes)[value +(c&0x3f)];
4312
0
                    if(value<=0xff) {
4313
0
                        if(value==0) {
4314
0
                            goto unassigned;
4315
0
                        } else {
4316
0
                            length=1;
4317
0
                        }
4318
0
                    } else if(value<=0xffff) {
4319
0
                        length=2;
4320
0
                    } else if(value<=0xffffff) {
4321
0
                        length=3;
4322
0
                    } else {
4323
0
                        length=4;
4324
0
                    }
4325
0
                    break;
4326
0
                case MBCS_OUTPUT_3_EUC:
4327
0
                    value=((const uint16_t *)bytes)[value +(c&0x3f)];
4328
                    /* EUC 16-bit fixed-length representation */
4329
0
                    if(value<=0xff) {
4330
0
                        if(value==0) {
4331
0
                            goto unassigned;
4332
0
                        } else {
4333
0
                            length=1;
4334
0
                        }
4335
0
                    } else if((value&0x8000)==0) {
4336
0
                        value|=0x8e8000;
4337
0
                        length=3;
4338
0
                    } else if((value&0x80)==0) {
4339
0
                        value|=0x8f0080;
4340
0
                        length=3;
4341
0
                    } else {
4342
0
                        length=2;
4343
0
                    }
4344
0
                    break;
4345
0
                case MBCS_OUTPUT_4_EUC:
4346
0
                    p=bytes+(value+(c&0x3f))*3;
4347
0
                    value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4348
                    /* EUC 16-bit fixed-length representation applied to the first two bytes */
4349
0
                    if(value<=0xff) {
4350
0
                        if(value==0) {
4351
0
                            goto unassigned;
4352
0
                        } else {
4353
0
                            length=1;
4354
0
                        }
4355
0
                    } else if(value<=0xffff) {
4356
0
                        length=2;
4357
0
                    } else if((value&0x800000)==0) {
4358
0
                        value|=0x8e800000;
4359
0
                        length=4;
4360
0
                    } else if((value&0x8000)==0) {
4361
0
                        value|=0x8f008000;
4362
0
                        length=4;
4363
0
                    } else {
4364
0
                        length=3;
4365
0
                    }
4366
0
                    break;
4367
0
                default:
4368
                    /* must not occur */
4369
                    /*
4370
                     * To avoid compiler warnings that value & length may be
4371
                     * used without having been initialized, we set them here.
4372
                     * In reality, this is unreachable code.
4373
                     * Not having a default branch also causes warnings with
4374
                     * some compilers.
4375
                     */
4376
0
                    value=0;
4377
0
                    length=0;
4378
0
                    break;
4379
0
                }
4380
                /* output the value */
4381
0
            } else {
4382
                /*
4383
                 * This also tests if the codepage maps single surrogates.
4384
                 * If it does, then surrogates are not paired but mapped separately.
4385
                 * Note that in this case unmatched surrogates are not detected.
4386
                 */
4387
0
                if(U16_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
4388
0
                    if(U16_IS_SURROGATE_LEAD(c)) {
4389
0
getTrail:
4390
0
                        if(source<sourceLimit) {
4391
                            /* test the following code unit */
4392
0
                            UChar trail=*source;
4393
0
                            if(U16_IS_TRAIL(trail)) {
4394
0
                                ++source;
4395
0
                                ++nextSourceIndex;
4396
0
                                c=U16_GET_SUPPLEMENTARY(c, trail);
4397
0
                                if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
4398
                                    /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4399
0
                                    cnv->fromUnicodeStatus=prevLength; /* save the old state */
4400
                                    /* callback(unassigned) */
4401
0
                                    goto unassigned;
4402
0
                                }
4403
                                /* convert this supplementary code point */
4404
                                /* exit this condition tree */
4405
0
                            } else {
4406
                                /* this is an unmatched lead code unit (1st surrogate) */
4407
                                /* callback(illegal) */
4408
0
                                *pErrorCode=U_ILLEGAL_CHAR_FOUND;
4409
0
                                break;
4410
0
                            }
4411
0
                        } else {
4412
                            /* no more input */
4413
0
                            break;
4414
0
                        }
4415
0
                    } else {
4416
                        /* this is an unmatched trail code unit (2nd surrogate) */
4417
                        /* callback(illegal) */
4418
0
                        *pErrorCode=U_ILLEGAL_CHAR_FOUND;
4419
0
                        break;
4420
0
                    }
4421
0
                }
4422
4423
                /* convert the Unicode code point in c into codepage bytes */
4424
4425
                /*
4426
                 * The basic lookup is a triple-stage compact array (trie) lookup.
4427
                 * For details see the beginning of this file.
4428
                 *
4429
                 * Single-byte codepages are handled with a different data structure
4430
                 * by _MBCSSingle... functions.
4431
                 *
4432
                 * The result consists of a 32-bit value from stage 2 and
4433
                 * a pointer to as many bytes as are stored per character.
4434
                 * The pointer points to the character's bytes in stage 3.
4435
                 * Bits 15..0 of the stage 2 entry contain the stage 3 index
4436
                 * for that pointer, while bits 31..16 are flags for which of
4437
                 * the 16 characters in the block are roundtrip-assigned.
4438
                 *
4439
                 * For 2-byte and 4-byte codepages, the bytes are stored as uint16_t
4440
                 * respectively as uint32_t, in the platform encoding.
4441
                 * For 3-byte codepages, the bytes are always stored in big-endian order.
4442
                 *
4443
                 * For EUC encodings that use only either 0x8e or 0x8f as the first
4444
                 * byte of their longest byte sequences, the first two bytes in
4445
                 * this third stage indicate with their 7th bits whether these bytes
4446
                 * are to be written directly or actually need to be preceeded by
4447
                 * one of the two Single-Shift codes. With this, the third stage
4448
                 * stores one byte fewer per character than the actual maximum length of
4449
                 * EUC byte sequences.
4450
                 *
4451
                 * Other than that, leading zero bytes are removed and the other
4452
                 * bytes output. A single zero byte may be output if the "assigned"
4453
                 * bit in stage 2 was on.
4454
                 * The data structure does not support zero byte output as a fallback,
4455
                 * and also does not allow output of leading zeros.
4456
                 */
4457
0
                stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
4458
4459
                /* get the bytes and the length for the output */
4460
0
                switch(outputType) {
4461
0
                case MBCS_OUTPUT_2:
4462
0
                    value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4463
0
                    if(value<=0xff) {
4464
0
                        length=1;
4465
0
                    } else {
4466
0
                        length=2;
4467
0
                    }
4468
0
                    break;
4469
0
                case MBCS_OUTPUT_2_SISO:
4470
                    /* 1/2-byte stateful with Shift-In/Shift-Out */
4471
                    /*
4472
                     * Save the old state in the converter object
4473
                     * right here, then change the local prevLength state variable if necessary.
4474
                     * Then, if this character turns out to be unassigned or a fallback that
4475
                     * is not taken, the callback code must not save the new state in the converter
4476
                     * because the new state is for a character that is not output.
4477
                     * However, the callback must still restore the state from the converter
4478
                     * in case the callback function changed it for its output.
4479
                     */
4480
0
                    cnv->fromUnicodeStatus=prevLength; /* save the old state */
4481
0
                    value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4482
0
                    if(value<=0xff) {
4483
0
                        if(value==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)==0) {
4484
                            /* no mapping, leave value==0 */
4485
0
                            length=0;
4486
0
                        } else if(prevLength<=1) {
4487
0
                            length=1;
4488
0
                        } else {
4489
                            /* change from double-byte mode to single-byte */
4490
0
                            if (siLength == 1) {
4491
0
                                value|=(uint32_t)siBytes[0]<<8;
4492
0
                                length = 2;
4493
0
                            } else if (siLength == 2) {
4494
0
                                value|=(uint32_t)siBytes[1]<<8;
4495
0
                                value|=(uint32_t)siBytes[0]<<16;
4496
0
                                length = 3;
4497
0
                            }
4498
0
                            prevLength=1;
4499
0
                        }
4500
0
                    } else {
4501
0
                        if(prevLength==2) {
4502
0
                            length=2;
4503
0
                        } else {
4504
                            /* change from single-byte mode to double-byte */
4505
0
                            if (soLength == 1) {
4506
0
                                value|=(uint32_t)soBytes[0]<<16;
4507
0
                                length = 3;
4508
0
                            } else if (soLength == 2) {
4509
0
                                value|=(uint32_t)soBytes[1]<<16;
4510
0
                                value|=(uint32_t)soBytes[0]<<24;
4511
0
                                length = 4;
4512
0
                            }
4513
0
                            prevLength=2;
4514
0
                        }
4515
0
                    }
4516
0
                    break;
4517
0
                case MBCS_OUTPUT_DBCS_ONLY:
4518
                    /* table with single-byte results, but only DBCS mappings used */
4519
0
                    value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4520
0
                    if(value<=0xff) {
4521
                        /* no mapping or SBCS result, not taken for DBCS-only */
4522
0
                        value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
4523
0
                        length=0;
4524
0
                    } else {
4525
0
                        length=2;
4526
0
                    }
4527
0
                    break;
4528
0
                case MBCS_OUTPUT_3:
4529
0
                    p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
4530
0
                    value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4531
0
                    if(value<=0xff) {
4532
0
                        length=1;
4533
0
                    } else if(value<=0xffff) {
4534
0
                        length=2;
4535
0
                    } else {
4536
0
                        length=3;
4537
0
                    }
4538
0
                    break;
4539
0
                case MBCS_OUTPUT_4:
4540
0
                    value=MBCS_VALUE_4_FROM_STAGE_2(bytes, stage2Entry, c);
4541
0
                    if(value<=0xff) {
4542
0
                        length=1;
4543
0
                    } else if(value<=0xffff) {
4544
0
                        length=2;
4545
0
                    } else if(value<=0xffffff) {
4546
0
                        length=3;
4547
0
                    } else {
4548
0
                        length=4;
4549
0
                    }
4550
0
                    break;
4551
0
                case MBCS_OUTPUT_3_EUC:
4552
0
                    value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4553
                    /* EUC 16-bit fixed-length representation */
4554
0
                    if(value<=0xff) {
4555
0
                        length=1;
4556
0
                    } else if((value&0x8000)==0) {
4557
0
                        value|=0x8e8000;
4558
0
                        length=3;
4559
0
                    } else if((value&0x80)==0) {
4560
0
                        value|=0x8f0080;
4561
0
                        length=3;
4562
0
                    } else {
4563
0
                        length=2;
4564
0
                    }
4565
0
                    break;
4566
0
                case MBCS_OUTPUT_4_EUC:
4567
0
                    p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
4568
0
                    value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4569
                    /* EUC 16-bit fixed-length representation applied to the first two bytes */
4570
0
                    if(value<=0xff) {
4571
0
                        length=1;
4572
0
                    } else if(value<=0xffff) {
4573
0
                        length=2;
4574
0
                    } else if((value&0x800000)==0) {
4575
0
                        value|=0x8e800000;
4576
0
                        length=4;
4577
0
                    } else if((value&0x8000)==0) {
4578
0
                        value|=0x8f008000;
4579
0
                        length=4;
4580
0
                    } else {
4581
0
                        length=3;
4582
0
                    }
4583
0
                    break;
4584
0
                default:
4585
                    /* must not occur */
4586
                    /*
4587
                     * To avoid compiler warnings that value & length may be
4588
                     * used without having been initialized, we set them here.
4589
                     * In reality, this is unreachable code.
4590
                     * Not having a default branch also causes warnings with
4591
                     * some compilers.
4592
                     */
4593
0
                    value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
4594
0
                    length=0;
4595
0
                    break;
4596
0
                }
4597
4598
                /* is this code point assigned, or do we use fallbacks? */
4599
0
                if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)!=0 ||
4600
0
                     (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
4601
0
                ) {
4602
                    /*
4603
                     * We allow a 0 byte output if the "assigned" bit is set for this entry.
4604
                     * There is no way with this data structure for fallback output
4605
                     * to be a zero byte.
4606
                     */
4607
4608
0
unassigned:
4609
                    /* try an extension mapping */
4610
0
                    pArgs->source=source;
4611
0
                    c=_extFromU(cnv, cnv->sharedData,
4612
0
                                c, &source, sourceLimit,
4613
0
                                &target, target+targetCapacity,
4614
0
                                &offsets, sourceIndex,
4615
0
                                pArgs->flush,
4616
0
                                pErrorCode);
4617
0
                    nextSourceIndex+=(int32_t)(source-pArgs->source);
4618
0
                    prevLength=cnv->fromUnicodeStatus; /* restore SISO state */
4619
4620
0
                    if(U_FAILURE(*pErrorCode)) {
4621
                        /* not mappable or buffer overflow */
4622
0
                        break;
4623
0
                    } else {
4624
                        /* a mapping was written to the target, continue */
4625
4626
                        /* recalculate the targetCapacity after an extension mapping */
4627
0
                        targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
4628
4629
                        /* normal end of conversion: prepare for a new character */
4630
0
                        if(offsets!=NULL) {
4631
0
                            prevSourceIndex=sourceIndex;
4632
0
                            sourceIndex=nextSourceIndex;
4633
0
                        }
4634
0
                        continue;
4635
0
                    }
4636
0
                }
4637
0
            }
4638
4639
            /* write the output character bytes from value and length */
4640
            /* from the first if in the loop we know that targetCapacity>0 */
4641
0
            if(length<=targetCapacity) {
4642
0
                if(offsets==NULL) {
4643
0
                    switch(length) {
4644
                        /* each branch falls through to the next one */
4645
0
                    case 4:
4646
0
                        *target++=(uint8_t)(value>>24);
4647
0
                        U_FALLTHROUGH;
4648
0
                    case 3:
4649
0
                        *target++=(uint8_t)(value>>16);
4650
0
                        U_FALLTHROUGH;
4651
0
                    case 2:
4652
0
                        *target++=(uint8_t)(value>>8);
4653
0
                        U_FALLTHROUGH;
4654
0
                    case 1:
4655
0
                        *target++=(uint8_t)value;
4656
0
                        U_FALLTHROUGH;
4657
0
                    default:
4658
                        /* will never occur */
4659
0
                        break;
4660
0
                    }
4661
0
                } else {
4662
0
                    switch(length) {
4663
                        /* each branch falls through to the next one */
4664
0
                    case 4:
4665
0
                        *target++=(uint8_t)(value>>24);
4666
0
                        *offsets++=sourceIndex;
4667
0
                        U_FALLTHROUGH;
4668
0
                    case 3:
4669
0
                        *target++=(uint8_t)(value>>16);
4670
0
                        *offsets++=sourceIndex;
4671
0
                        U_FALLTHROUGH;
4672
0
                    case 2:
4673
0
                        *target++=(uint8_t)(value>>8);
4674
0
                        *offsets++=sourceIndex;
4675
0
                        U_FALLTHROUGH;
4676
0
                    case 1:
4677
0
                        *target++=(uint8_t)value;
4678
0
                        *offsets++=sourceIndex;
4679
0
                        U_FALLTHROUGH;
4680
0
                    default:
4681
                        /* will never occur */
4682
0
                        break;
4683
0
                    }
4684
0
                }
4685
0
                targetCapacity-=length;
4686
0
            } else {
4687
0
                uint8_t *charErrorBuffer;
4688
4689
                /*
4690
                 * We actually do this backwards here:
4691
                 * In order to save an intermediate variable, we output
4692
                 * first to the overflow buffer what does not fit into the
4693
                 * regular target.
4694
                 */
4695
                /* we know that 1<=targetCapacity<length<=4 */
4696
0
                length-=targetCapacity;
4697
0
                charErrorBuffer=(uint8_t *)cnv->charErrorBuffer;
4698
0
                switch(length) {
4699
                    /* each branch falls through to the next one */
4700
0
                case 3:
4701
0
                    *charErrorBuffer++=(uint8_t)(value>>16);
4702
0
                    U_FALLTHROUGH;
4703
0
                case 2:
4704
0
                    *charErrorBuffer++=(uint8_t)(value>>8);
4705
0
                    U_FALLTHROUGH;
4706
0
                case 1:
4707
0
                    *charErrorBuffer=(uint8_t)value;
4708
0
                    U_FALLTHROUGH;
4709
0
                default:
4710
                    /* will never occur */
4711
0
                    break;
4712
0
                }
4713
0
                cnv->charErrorBufferLength=(int8_t)length;
4714
4715
                /* now output what fits into the regular target */
4716
0
                value>>=8*length; /* length was reduced by targetCapacity */
4717
0
                switch(targetCapacity) {
4718
                    /* each branch falls through to the next one */
4719
0
                case 3:
4720
0
                    *target++=(uint8_t)(value>>16);
4721
0
                    if(offsets!=NULL) {
4722
0
                        *offsets++=sourceIndex;
4723
0
                    }
4724
0
                    U_FALLTHROUGH;
4725
0
                case 2:
4726
0
                    *target++=(uint8_t)(value>>8);
4727
0
                    if(offsets!=NULL) {
4728
0
                        *offsets++=sourceIndex;
4729
0
                    }
4730
0
                    U_FALLTHROUGH;
4731
0
                case 1:
4732
0
                    *target++=(uint8_t)value;
4733
0
                    if(offsets!=NULL) {
4734
0
                        *offsets++=sourceIndex;
4735
0
                    }
4736
0
                    U_FALLTHROUGH;
4737
0
                default:
4738
                    /* will never occur */
4739
0
                    break;
4740
0
                }
4741
4742
                /* target overflow */
4743
0
                targetCapacity=0;
4744
0
                *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4745
0
                c=0;
4746
0
                break;
4747
0
            }
4748
4749
            /* normal end of conversion: prepare for a new character */
4750
0
            c=0;
4751
0
            if(offsets!=NULL) {
4752
0
                prevSourceIndex=sourceIndex;
4753
0
                sourceIndex=nextSourceIndex;
4754
0
            }
4755
0
            continue;
4756
0
        } else {
4757
            /* target is full */
4758
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4759
0
            break;
4760
0
        }
4761
0
    }
4762
4763
    /*
4764
     * the end of the input stream and detection of truncated input
4765
     * are handled by the framework, but for EBCDIC_STATEFUL conversion
4766
     * we need to emit an SI at the very end
4767
     *
4768
     * conditions:
4769
     *   successful
4770
     *   EBCDIC_STATEFUL in DBCS mode
4771
     *   end of input and no truncated input
4772
     */
4773
0
    if( U_SUCCESS(*pErrorCode) &&
4774
0
        outputType==MBCS_OUTPUT_2_SISO && prevLength==2 &&
4775
0
        pArgs->flush && source>=sourceLimit && c==0
4776
0
    ) {
4777
        /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */
4778
0
        if(targetCapacity>0) {
4779
0
            *target++=(uint8_t)siBytes[0];
4780
0
            if (siLength == 2) {
4781
0
                if (targetCapacity<2) {
4782
0
                    cnv->charErrorBuffer[0]=(uint8_t)siBytes[1];
4783
0
                    cnv->charErrorBufferLength=1;
4784
0
                    *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4785
0
                } else {
4786
0
                    *target++=(uint8_t)siBytes[1];
4787
0
                }
4788
0
            }
4789
0
            if(offsets!=NULL) {
4790
                /* set the last source character's index (sourceIndex points at sourceLimit now) */
4791
0
                *offsets++=prevSourceIndex;
4792
0
            }
4793
0
        } else {
4794
            /* target is full */
4795
0
            cnv->charErrorBuffer[0]=(uint8_t)siBytes[0];
4796
0
            if (siLength == 2) {
4797
0
                cnv->charErrorBuffer[1]=(uint8_t)siBytes[1];
4798
0
            }
4799
0
            cnv->charErrorBufferLength=siLength;
4800
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4801
0
        }
4802
0
        prevLength=1; /* we switched into SBCS */
4803
0
    }
4804
4805
    /* set the converter state back into UConverter */
4806
0
    cnv->fromUChar32=c;
4807
0
    cnv->fromUnicodeStatus=prevLength;
4808
4809
    /* write back the updated pointers */
4810
0
    pArgs->source=source;
4811
0
    pArgs->target=(char *)target;
4812
0
    pArgs->offsets=offsets;
4813
0
}
4814
4815
/*
4816
 * This is another simple conversion function for internal use by other
4817
 * conversion implementations.
4818
 * It does not use the converter state nor call callbacks.
4819
 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4820
 * It handles conversion extensions but not GB 18030.
4821
 *
4822
 * It converts one single Unicode code point into codepage bytes, encoded
4823
 * as one 32-bit value. The function returns the number of bytes in *pValue:
4824
 * 1..4 the number of bytes in *pValue
4825
 * 0    unassigned (*pValue undefined)
4826
 * -1   illegal (currently not used, *pValue undefined)
4827
 *
4828
 * *pValue will contain the resulting bytes with the last byte in bits 7..0,
4829
 * the second to last byte in bits 15..8, etc.
4830
 * Currently, the function assumes but does not check that 0<=c<=0x10ffff.
4831
 */
4832
U_CFUNC int32_t
4833
ucnv_MBCSFromUChar32(UConverterSharedData *sharedData,
4834
                 UChar32 c, uint32_t *pValue,
4835
0
                 UBool useFallback) {
4836
0
    const int32_t *cx;
4837
0
    const uint16_t *table;
4838
#if 0
4839
/* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4840
    const uint8_t *p;
4841
#endif
4842
0
    uint32_t stage2Entry;
4843
0
    uint32_t value;
4844
0
    int32_t length;
4845
4846
    /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4847
0
    if(c<=0xffff || (sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
4848
0
        table=sharedData->mbcs.fromUnicodeTable;
4849
4850
        /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4851
0
        if(sharedData->mbcs.outputType==MBCS_OUTPUT_1) {
4852
0
            value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c);
4853
            /* is this code point assigned, or do we use fallbacks? */
4854
0
            if(useFallback ? value>=0x800 : value>=0xc00) {
4855
0
                *pValue=value&0xff;
4856
0
                return 1;
4857
0
            }
4858
0
        } else /* outputType!=MBCS_OUTPUT_1 */ {
4859
0
            stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
4860
4861
            /* get the bytes and the length for the output */
4862
0
            switch(sharedData->mbcs.outputType) {
4863
0
            case MBCS_OUTPUT_2:
4864
0
                value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4865
0
                if(value<=0xff) {
4866
0
                    length=1;
4867
0
                } else {
4868
0
                    length=2;
4869
0
                }
4870
0
                break;
4871
#if 0
4872
/* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4873
            case MBCS_OUTPUT_DBCS_ONLY:
4874
                /* table with single-byte results, but only DBCS mappings used */
4875
                value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4876
                if(value<=0xff) {
4877
                    /* no mapping or SBCS result, not taken for DBCS-only */
4878
                    value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
4879
                    length=0;
4880
                } else {
4881
                    length=2;
4882
                }
4883
                break;
4884
            case MBCS_OUTPUT_3:
4885
                p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4886
                value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4887
                if(value<=0xff) {
4888
                    length=1;
4889
                } else if(value<=0xffff) {
4890
                    length=2;
4891
                } else {
4892
                    length=3;
4893
                }
4894
                break;
4895
            case MBCS_OUTPUT_4:
4896
                value=MBCS_VALUE_4_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4897
                if(value<=0xff) {
4898
                    length=1;
4899
                } else if(value<=0xffff) {
4900
                    length=2;
4901
                } else if(value<=0xffffff) {
4902
                    length=3;
4903
                } else {
4904
                    length=4;
4905
                }
4906
                break;
4907
            case MBCS_OUTPUT_3_EUC:
4908
                value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4909
                /* EUC 16-bit fixed-length representation */
4910
                if(value<=0xff) {
4911
                    length=1;
4912
                } else if((value&0x8000)==0) {
4913
                    value|=0x8e8000;
4914
                    length=3;
4915
                } else if((value&0x80)==0) {
4916
                    value|=0x8f0080;
4917
                    length=3;
4918
                } else {
4919
                    length=2;
4920
                }
4921
                break;
4922
            case MBCS_OUTPUT_4_EUC:
4923
                p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4924
                value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4925
                /* EUC 16-bit fixed-length representation applied to the first two bytes */
4926
                if(value<=0xff) {
4927
                    length=1;
4928
                } else if(value<=0xffff) {
4929
                    length=2;
4930
                } else if((value&0x800000)==0) {
4931
                    value|=0x8e800000;
4932
                    length=4;
4933
                } else if((value&0x8000)==0) {
4934
                    value|=0x8f008000;
4935
                    length=4;
4936
                } else {
4937
                    length=3;
4938
                }
4939
                break;
4940
#endif
4941
0
            default:
4942
                /* must not occur */
4943
0
                return -1;
4944
0
            }
4945
4946
            /* is this code point assigned, or do we use fallbacks? */
4947
0
            if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
4948
0
                (FROM_U_USE_FALLBACK(useFallback, c) && value!=0)
4949
0
            ) {
4950
                /*
4951
                 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4952
                 * There is no way with this data structure for fallback output
4953
                 * to be a zero byte.
4954
                 */
4955
                /* assigned */
4956
0
                *pValue=value;
4957
0
                return length;
4958
0
            }
4959
0
        }
4960
0
    }
4961
4962
0
    cx=sharedData->mbcs.extIndexes;
4963
0
    if(cx!=NULL) {
4964
0
        length=ucnv_extSimpleMatchFromU(cx, c, pValue, useFallback);
4965
0
        return length>=0 ? length : -length;  /* return abs(length); */
4966
0
    }
4967
4968
    /* unassigned */
4969
0
    return 0;
4970
0
}
4971
4972
4973
#if 0
4974
/*
4975
 * This function has been moved to ucnv2022.c for inlining.
4976
 * This implementation is here only for documentation purposes
4977
 */
4978
4979
/**
4980
 * This version of ucnv_MBCSFromUChar32() is optimized for single-byte codepages.
4981
 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4982
 * It does not handle conversion extensions (_extFromU()).
4983
 *
4984
 * It returns the codepage byte for the code point, or -1 if it is unassigned.
4985
 */
4986
U_CFUNC int32_t
4987
ucnv_MBCSSingleFromUChar32(UConverterSharedData *sharedData,
4988
                       UChar32 c,
4989
                       UBool useFallback) {
4990
    const uint16_t *table;
4991
    int32_t value;
4992
4993
    /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4994
    if(c>=0x10000 && !(sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
4995
        return -1;
4996
    }
4997
4998
    /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4999
    table=sharedData->mbcs.fromUnicodeTable;
5000
5001
    /* get the byte for the output */
5002
    value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c);
5003
    /* is this code point assigned, or do we use fallbacks? */
5004
    if(useFallback ? value>=0x800 : value>=0xc00) {
5005
        return value&0xff;
5006
    } else {
5007
        return -1;
5008
    }
5009
}
5010
#endif
5011
5012
/* MBCS-from-UTF-8 conversion functions ------------------------------------- */
5013
5014
/* minimum code point values for n-byte UTF-8 sequences, n=0..4 */
5015
static const UChar32
5016
utf8_minLegal[5]={ 0, 0, 0x80, 0x800, 0x10000 };
5017
5018
/* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */
5019
static const UChar32
5020
utf8_offsets[7]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 };
5021
5022
static void U_CALLCONV
5023
ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
5024
                  UConverterToUnicodeArgs *pToUArgs,
5025
0
                  UErrorCode *pErrorCode) {
5026
0
    UConverter *utf8, *cnv;
5027
0
    const uint8_t *source, *sourceLimit;
5028
0
    uint8_t *target;
5029
0
    int32_t targetCapacity;
5030
5031
0
    const uint16_t *table, *sbcsIndex;
5032
0
    const uint16_t *results;
5033
5034
0
    int8_t oldToULength, toULength, toULimit;
5035
5036
0
    UChar32 c;
5037
0
    uint8_t b, t1, t2;
5038
5039
0
    uint32_t asciiRoundtrips;
5040
0
    uint16_t value, minValue;
5041
0
    UBool hasSupplementary;
5042
5043
    /* set up the local pointers */
5044
0
    utf8=pToUArgs->converter;
5045
0
    cnv=pFromUArgs->converter;
5046
0
    source=(uint8_t *)pToUArgs->source;
5047
0
    sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
5048
0
    target=(uint8_t *)pFromUArgs->target;
5049
0
    targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target);
5050
5051
0
    table=cnv->sharedData->mbcs.fromUnicodeTable;
5052
0
    sbcsIndex=cnv->sharedData->mbcs.sbcsIndex;
5053
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
5054
0
        results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
5055
0
    } else {
5056
0
        results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
5057
0
    }
5058
0
    asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
5059
5060
0
    if(cnv->useFallback) {
5061
        /* use all roundtrip and fallback results */
5062
0
        minValue=0x800;
5063
0
    } else {
5064
        /* use only roundtrips and fallbacks from private-use characters */
5065
0
        minValue=0xc00;
5066
0
    }
5067
0
    hasSupplementary=(UBool)(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY);
5068
5069
    /* get the converter state from the UTF-8 UConverter */
5070
0
    c=(UChar32)utf8->toUnicodeStatus;
5071
0
    if(c!=0) {
5072
0
        toULength=oldToULength=utf8->toULength;
5073
0
        toULimit=(int8_t)utf8->mode;
5074
0
    } else {
5075
0
        toULength=oldToULength=toULimit=0;
5076
0
    }
5077
5078
    /*
5079
     * Make sure that the last byte sequence before sourceLimit is complete
5080
     * or runs into a lead byte.
5081
     * Do not go back into the bytes that will be read for finishing a partial
5082
     * sequence from the previous buffer.
5083
     * In the conversion loop compare source with sourceLimit only once
5084
     * per multi-byte character.
5085
     */
5086
0
    {
5087
0
        int32_t i, length;
5088
5089
0
        length=(int32_t)(sourceLimit-source) - (toULimit-oldToULength);
5090
0
        for(i=0; i<3 && i<length;) {
5091
0
            b=*(sourceLimit-i-1);
5092
0
            if(U8_IS_TRAIL(b)) {
5093
0
                ++i;
5094
0
            } else {
5095
0
                if(i<U8_COUNT_TRAIL_BYTES(b)) {
5096
                    /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
5097
0
                    sourceLimit-=i+1;
5098
0
                }
5099
0
                break;
5100
0
            }
5101
0
        }
5102
0
    }
5103
5104
0
    if(c!=0 && targetCapacity>0) {
5105
0
        utf8->toUnicodeStatus=0;
5106
0
        utf8->toULength=0;
5107
0
        goto moreBytes;
5108
        /*
5109
         * Note: We could avoid the goto by duplicating some of the moreBytes
5110
         * code, but only up to the point of collecting a complete UTF-8
5111
         * sequence; then recurse for the toUBytes[toULength]
5112
         * and then continue with normal conversion.
5113
         *
5114
         * If so, move this code to just after initializing the minimum
5115
         * set of local variables for reading the UTF-8 input
5116
         * (utf8, source, target, limits but not cnv, table, minValue, etc.).
5117
         *
5118
         * Potential advantages:
5119
         * - avoid the goto
5120
         * - oldToULength could become a local variable in just those code blocks
5121
         *   that deal with buffer boundaries
5122
         * - possibly faster if the goto prevents some compiler optimizations
5123
         *   (this would need measuring to confirm)
5124
         * Disadvantage:
5125
         * - code duplication
5126
         */
5127
0
    }
5128
5129
    /* conversion loop */
5130
0
    while(source<sourceLimit) {
5131
0
        if(targetCapacity>0) {
5132
0
            b=*source++;
5133
0
            if((int8_t)b>=0) {
5134
                /* convert ASCII */
5135
0
                if(IS_ASCII_ROUNDTRIP(b, asciiRoundtrips)) {
5136
0
                    *target++=(uint8_t)b;
5137
0
                    --targetCapacity;
5138
0
                    continue;
5139
0
                } else {
5140
0
                    c=b;
5141
0
                    value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, 0, c);
5142
0
                }
5143
0
            } else {
5144
0
                if(b<0xe0) {
5145
0
                    if( /* handle U+0080..U+07FF inline */
5146
0
                        b>=0xc2 &&
5147
0
                        (t1=(uint8_t)(*source-0x80)) <= 0x3f
5148
0
                    ) {
5149
0
                        c=b&0x1f;
5150
0
                        ++source;
5151
0
                        value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, c, t1);
5152
0
                        if(value>=minValue) {
5153
0
                            *target++=(uint8_t)value;
5154
0
                            --targetCapacity;
5155
0
                            continue;
5156
0
                        } else {
5157
0
                            c=(c<<6)|t1;
5158
0
                        }
5159
0
                    } else {
5160
0
                        c=-1;
5161
0
                    }
5162
0
                } else if(b==0xe0) {
5163
0
                    if( /* handle U+0800..U+0FFF inline */
5164
0
                        (t1=(uint8_t)(source[0]-0x80)) <= 0x3f && t1 >= 0x20 &&
5165
0
                        (t2=(uint8_t)(source[1]-0x80)) <= 0x3f
5166
0
                    ) {
5167
0
                        c=t1;
5168
0
                        source+=2;
5169
0
                        value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, c, t2);
5170
0
                        if(value>=minValue) {
5171
0
                            *target++=(uint8_t)value;
5172
0
                            --targetCapacity;
5173
0
                            continue;
5174
0
                        } else {
5175
0
                            c=(c<<6)|t2;
5176
0
                        }
5177
0
                    } else {
5178
0
                        c=-1;
5179
0
                    }
5180
0
                } else {
5181
0
                    c=-1;
5182
0
                }
5183
5184
0
                if(c<0) {
5185
                    /* handle "complicated" and error cases, and continuing partial characters */
5186
0
                    oldToULength=0;
5187
0
                    toULength=1;
5188
0
                    toULimit=U8_COUNT_TRAIL_BYTES(b)+1;
5189
0
                    c=b;
5190
0
moreBytes:
5191
0
                    while(toULength<toULimit) {
5192
                        /*
5193
                         * The sourceLimit may have been adjusted before the conversion loop
5194
                         * to stop before a truncated sequence.
5195
                         * Here we need to use the real limit in case we have two truncated
5196
                         * sequences at the end.
5197
                         * See ticket #7492.
5198
                         */
5199
0
                        if(source<(uint8_t *)pToUArgs->sourceLimit) {
5200
0
                            b=*source;
5201
0
                            if(U8_IS_TRAIL(b)) {
5202
0
                                ++source;
5203
0
                                ++toULength;
5204
0
                                c=(c<<6)+b;
5205
0
                            } else {
5206
0
                                break; /* sequence too short, stop with toULength<toULimit */
5207
0
                            }
5208
0
                        } else {
5209
                            /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5210
0
                            source-=(toULength-oldToULength);
5211
0
                            while(oldToULength<toULength) {
5212
0
                                utf8->toUBytes[oldToULength++]=*source++;
5213
0
                            }
5214
0
                            utf8->toUnicodeStatus=c;
5215
0
                            utf8->toULength=toULength;
5216
0
                            utf8->mode=toULimit;
5217
0
                            pToUArgs->source=(char *)source;
5218
0
                            pFromUArgs->target=(char *)target;
5219
0
                            return;
5220
0
                        }
5221
0
                    }
5222
5223
0
                    if( toULength==toULimit &&      /* consumed all trail bytes */
5224
0
                        (toULength==3 || toULength==2) &&             /* BMP */
5225
0
                        (c-=utf8_offsets[toULength])>=utf8_minLegal[toULength] &&
5226
0
                        (c<=0xd7ff || 0xe000<=c)    /* not a surrogate */
5227
0
                    ) {
5228
0
                        value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
5229
0
                    } else if(
5230
0
                        toULength==toULimit && toULength==4 &&
5231
0
                        (0x10000<=(c-=utf8_offsets[4]) && c<=0x10ffff)
5232
0
                    ) {
5233
                        /* supplementary code point */
5234
0
                        if(!hasSupplementary) {
5235
                            /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5236
0
                            value=0;
5237
0
                        } else {
5238
0
                            value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
5239
0
                        }
5240
0
                    } else {
5241
                        /* error handling: illegal UTF-8 byte sequence */
5242
0
                        source-=(toULength-oldToULength);
5243
0
                        while(oldToULength<toULength) {
5244
0
                            utf8->toUBytes[oldToULength++]=*source++;
5245
0
                        }
5246
0
                        utf8->toULength=toULength;
5247
0
                        pToUArgs->source=(char *)source;
5248
0
                        pFromUArgs->target=(char *)target;
5249
0
                        *pErrorCode=U_ILLEGAL_CHAR_FOUND;
5250
0
                        return;
5251
0
                    }
5252
0
                }
5253
0
            }
5254
5255
0
            if(value>=minValue) {
5256
                /* output the mapping for c */
5257
0
                *target++=(uint8_t)value;
5258
0
                --targetCapacity;
5259
0
            } else {
5260
                /* value<minValue means c is unassigned (unmappable) */
5261
                /*
5262
                 * Try an extension mapping.
5263
                 * Pass in no source because we don't have UTF-16 input.
5264
                 * If we have a partial match on c, we will return and revert
5265
                 * to UTF-8->UTF-16->charset conversion.
5266
                 */
5267
0
                static const UChar nul=0;
5268
0
                const UChar *noSource=&nul;
5269
0
                c=_extFromU(cnv, cnv->sharedData,
5270
0
                            c, &noSource, noSource,
5271
0
                            &target, target+targetCapacity,
5272
0
                            NULL, -1,
5273
0
                            pFromUArgs->flush,
5274
0
                            pErrorCode);
5275
5276
0
                if(U_FAILURE(*pErrorCode)) {
5277
                    /* not mappable or buffer overflow */
5278
0
                    cnv->fromUChar32=c;
5279
0
                    break;
5280
0
                } else if(cnv->preFromUFirstCP>=0) {
5281
                    /*
5282
                     * Partial match, return and revert to pivoting.
5283
                     * In normal from-UTF-16 conversion, we would just continue
5284
                     * but then exit the loop because the extension match would
5285
                     * have consumed the source.
5286
                     */
5287
0
                    *pErrorCode=U_USING_DEFAULT_WARNING;
5288
0
                    break;
5289
0
                } else {
5290
                    /* a mapping was written to the target, continue */
5291
5292
                    /* recalculate the targetCapacity after an extension mapping */
5293
0
                    targetCapacity=(int32_t)(pFromUArgs->targetLimit-(char *)target);
5294
0
                }
5295
0
            }
5296
0
        } else {
5297
            /* target is full */
5298
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
5299
0
            break;
5300
0
        }
5301
0
    }
5302
5303
    /*
5304
     * The sourceLimit may have been adjusted before the conversion loop
5305
     * to stop before a truncated sequence.
5306
     * If so, then collect the truncated sequence now.
5307
     */
5308
0
    if(U_SUCCESS(*pErrorCode) &&
5309
0
            cnv->preFromUFirstCP<0 &&
5310
0
            source<(sourceLimit=(uint8_t *)pToUArgs->sourceLimit)) {
5311
0
        c=utf8->toUBytes[0]=b=*source++;
5312
0
        toULength=1;
5313
0
        toULimit=U8_COUNT_TRAIL_BYTES(b)+1;
5314
0
        while(source<sourceLimit) {
5315
0
            utf8->toUBytes[toULength++]=b=*source++;
5316
0
            c=(c<<6)+b;
5317
0
        }
5318
0
        utf8->toUnicodeStatus=c;
5319
0
        utf8->toULength=toULength;
5320
0
        utf8->mode=toULimit;
5321
0
    }
5322
5323
    /* write back the updated pointers */
5324
0
    pToUArgs->source=(char *)source;
5325
0
    pFromUArgs->target=(char *)target;
5326
0
}
5327
5328
static void U_CALLCONV
5329
ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
5330
                  UConverterToUnicodeArgs *pToUArgs,
5331
0
                  UErrorCode *pErrorCode) {
5332
0
    UConverter *utf8, *cnv;
5333
0
    const uint8_t *source, *sourceLimit;
5334
0
    uint8_t *target;
5335
0
    int32_t targetCapacity;
5336
5337
0
    const uint16_t *table, *mbcsIndex;
5338
0
    const uint16_t *results;
5339
5340
0
    int8_t oldToULength, toULength, toULimit;
5341
5342
0
    UChar32 c;
5343
0
    uint8_t b, t1, t2;
5344
5345
0
    uint32_t stage2Entry;
5346
0
    uint32_t asciiRoundtrips;
5347
0
    uint16_t value;
5348
0
    UBool hasSupplementary;
5349
5350
    /* set up the local pointers */
5351
0
    utf8=pToUArgs->converter;
5352
0
    cnv=pFromUArgs->converter;
5353
0
    source=(uint8_t *)pToUArgs->source;
5354
0
    sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
5355
0
    target=(uint8_t *)pFromUArgs->target;
5356
0
    targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target);
5357
5358
0
    table=cnv->sharedData->mbcs.fromUnicodeTable;
5359
0
    mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
5360
0
    if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
5361
0
        results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
5362
0
    } else {
5363
0
        results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
5364
0
    }
5365
0
    asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
5366
5367
0
    hasSupplementary=(UBool)(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY);
5368
5369
    /* get the converter state from the UTF-8 UConverter */
5370
0
    c=(UChar32)utf8->toUnicodeStatus;
5371
0
    if(c!=0) {
5372
0
        toULength=oldToULength=utf8->toULength;
5373
0
        toULimit=(int8_t)utf8->mode;
5374
0
    } else {
5375
0
        toULength=oldToULength=toULimit=0;
5376
0
    }
5377
5378
    /*
5379
     * Make sure that the last byte sequence before sourceLimit is complete
5380
     * or runs into a lead byte.
5381
     * Do not go back into the bytes that will be read for finishing a partial
5382
     * sequence from the previous buffer.
5383
     * In the conversion loop compare source with sourceLimit only once
5384
     * per multi-byte character.
5385
     */
5386
0
    {
5387
0
        int32_t i, length;
5388
5389
0
        length=(int32_t)(sourceLimit-source) - (toULimit-oldToULength);
5390
0
        for(i=0; i<3 && i<length;) {
5391
0
            b=*(sourceLimit-i-1);
5392
0
            if(U8_IS_TRAIL(b)) {
5393
0
                ++i;
5394
0
            } else {
5395
0
                if(i<U8_COUNT_TRAIL_BYTES(b)) {
5396
                    /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
5397
0
                    sourceLimit-=i+1;
5398
0
                }
5399
0
                break;
5400
0
            }
5401
0
        }
5402
0
    }
5403
5404
0
    if(c!=0 && targetCapacity>0) {
5405
0
        utf8->toUnicodeStatus=0;
5406
0
        utf8->toULength=0;
5407
0
        goto moreBytes;
5408
        /* See note in ucnv_SBCSFromUTF8() about this goto. */
5409
0
    }
5410
5411
    /* conversion loop */
5412
0
    while(source<sourceLimit) {
5413
0
        if(targetCapacity>0) {
5414
0
            b=*source++;
5415
0
            if((int8_t)b>=0) {
5416
                /* convert ASCII */
5417
0
                if(IS_ASCII_ROUNDTRIP(b, asciiRoundtrips)) {
5418
0
                    *target++=b;
5419
0
                    --targetCapacity;
5420
0
                    continue;
5421
0
                } else {
5422
0
                    value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, 0, b);
5423
0
                    if(value==0) {
5424
0
                        c=b;
5425
0
                        goto unassigned;
5426
0
                    }
5427
0
                }
5428
0
            } else {
5429
0
                if(b>0xe0) {
5430
0
                    if( /* handle U+1000..U+D7FF inline */
5431
0
                        (((t1=(uint8_t)(source[0]-0x80), b<0xed) && (t1 <= 0x3f)) ||
5432
0
                                                        (b==0xed && (t1 <= 0x1f))) &&
5433
0
                        (t2=(uint8_t)(source[1]-0x80)) <= 0x3f
5434
0
                    ) {
5435
0
                        c=((b&0xf)<<6)|t1;
5436
0
                        source+=2;
5437
0
                        value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, c, t2);
5438
0
                        if(value==0) {
5439
0
                            c=(c<<6)|t2;
5440
0
                            goto unassigned;
5441
0
                        }
5442
0
                    } else {
5443
0
                        c=-1;
5444
0
                    }
5445
0
                } else if(b<0xe0) {
5446
0
                    if( /* handle U+0080..U+07FF inline */
5447
0
                        b>=0xc2 &&
5448
0
                        (t1=(uint8_t)(*source-0x80)) <= 0x3f
5449
0
                    ) {
5450
0
                        c=b&0x1f;
5451
0
                        ++source;
5452
0
                        value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, c, t1);
5453
0
                        if(value==0) {
5454
0
                            c=(c<<6)|t1;
5455
0
                            goto unassigned;
5456
0
                        }
5457
0
                    } else {
5458
0
                        c=-1;
5459
0
                    }
5460
0
                } else {
5461
0
                    c=-1;
5462
0
                }
5463
5464
0
                if(c<0) {
5465
                    /* handle "complicated" and error cases, and continuing partial characters */
5466
0
                    oldToULength=0;
5467
0
                    toULength=1;
5468
0
                    toULimit=U8_COUNT_TRAIL_BYTES(b)+1;
5469
0
                    c=b;
5470
0
moreBytes:
5471
0
                    while(toULength<toULimit) {
5472
                        /*
5473
                         * The sourceLimit may have been adjusted before the conversion loop
5474
                         * to stop before a truncated sequence.
5475
                         * Here we need to use the real limit in case we have two truncated
5476
                         * sequences at the end.
5477
                         * See ticket #7492.
5478
                         */
5479
0
                        if(source<(uint8_t *)pToUArgs->sourceLimit) {
5480
0
                            b=*source;
5481
0
                            if(U8_IS_TRAIL(b)) {
5482
0
                                ++source;
5483
0
                                ++toULength;
5484
0
                                c=(c<<6)+b;
5485
0
                            } else {
5486
0
                                break; /* sequence too short, stop with toULength<toULimit */
5487
0
                            }
5488
0
                        } else {
5489
                            /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5490
0
                            source-=(toULength-oldToULength);
5491
0
                            while(oldToULength<toULength) {
5492
0
                                utf8->toUBytes[oldToULength++]=*source++;
5493
0
                            }
5494
0
                            utf8->toUnicodeStatus=c;
5495
0
                            utf8->toULength=toULength;
5496
0
                            utf8->mode=toULimit;
5497
0
                            pToUArgs->source=(char *)source;
5498
0
                            pFromUArgs->target=(char *)target;
5499
0
                            return;
5500
0
                        }
5501
0
                    }
5502
5503
0
                    if( toULength==toULimit &&      /* consumed all trail bytes */
5504
0
                        (toULength==3 || toULength==2) &&             /* BMP */
5505
0
                        (c-=utf8_offsets[toULength])>=utf8_minLegal[toULength] &&
5506
0
                        (c<=0xd7ff || 0xe000<=c)    /* not a surrogate */
5507
0
                    ) {
5508
0
                        stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
5509
0
                    } else if(
5510
0
                        toULength==toULimit && toULength==4 &&
5511
0
                        (0x10000<=(c-=utf8_offsets[4]) && c<=0x10ffff)
5512
0
                    ) {
5513
                        /* supplementary code point */
5514
0
                        if(!hasSupplementary) {
5515
                            /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5516
0
                            stage2Entry=0;
5517
0
                        } else {
5518
0
                            stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
5519
0
                        }
5520
0
                    } else {
5521
                        /* error handling: illegal UTF-8 byte sequence */
5522
0
                        source-=(toULength-oldToULength);
5523
0
                        while(oldToULength<toULength) {
5524
0
                            utf8->toUBytes[oldToULength++]=*source++;
5525
0
                        }
5526
0
                        utf8->toULength=toULength;
5527
0
                        pToUArgs->source=(char *)source;
5528
0
                        pFromUArgs->target=(char *)target;
5529
0
                        *pErrorCode=U_ILLEGAL_CHAR_FOUND;
5530
0
                        return;
5531
0
                    }
5532
5533
                    /* get the bytes and the length for the output */
5534
                    /* MBCS_OUTPUT_2 */
5535
0
                    value=MBCS_VALUE_2_FROM_STAGE_2(results, stage2Entry, c);
5536
5537
                    /* is this code point assigned, or do we use fallbacks? */
5538
0
                    if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
5539
0
                         (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
5540
0
                    ) {
5541
0
                        goto unassigned;
5542
0
                    }
5543
0
                }
5544
0
            }
5545
5546
            /* write the output character bytes from value and length */
5547
            /* from the first if in the loop we know that targetCapacity>0 */
5548
0
            if(value<=0xff) {
5549
                /* this is easy because we know that there is enough space */
5550
0
                *target++=(uint8_t)value;
5551
0
                --targetCapacity;
5552
0
            } else /* length==2 */ {
5553
0
                *target++=(uint8_t)(value>>8);
5554
0
                if(2<=targetCapacity) {
5555
0
                    *target++=(uint8_t)value;
5556
0
                    targetCapacity-=2;
5557
0
                } else {
5558
0
                    cnv->charErrorBuffer[0]=(char)value;
5559
0
                    cnv->charErrorBufferLength=1;
5560
5561
                    /* target overflow */
5562
0
                    *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
5563
0
                    break;
5564
0
                }
5565
0
            }
5566
0
            continue;
5567
5568
0
unassigned:
5569
0
            {
5570
                /*
5571
                 * Try an extension mapping.
5572
                 * Pass in no source because we don't have UTF-16 input.
5573
                 * If we have a partial match on c, we will return and revert
5574
                 * to UTF-8->UTF-16->charset conversion.
5575
                 */
5576
0
                static const UChar nul=0;
5577
0
                const UChar *noSource=&nul;
5578
0
                c=_extFromU(cnv, cnv->sharedData,
5579
0
                            c, &noSource, noSource,
5580
0
                            &target, target+targetCapacity,
5581
0
                            NULL, -1,
5582
0
                            pFromUArgs->flush,
5583
0
                            pErrorCode);
5584
5585
0
                if(U_FAILURE(*pErrorCode)) {
5586
                    /* not mappable or buffer overflow */
5587
0
                    cnv->fromUChar32=c;
5588
0
                    break;
5589
0
                } else if(cnv->preFromUFirstCP>=0) {
5590
                    /*
5591
                     * Partial match, return and revert to pivoting.
5592
                     * In normal from-UTF-16 conversion, we would just continue
5593
                     * but then exit the loop because the extension match would
5594
                     * have consumed the source.
5595
                     */
5596
0
                    *pErrorCode=U_USING_DEFAULT_WARNING;
5597
0
                    break;
5598
0
                } else {
5599
                    /* a mapping was written to the target, continue */
5600
5601
                    /* recalculate the targetCapacity after an extension mapping */
5602
0
                    targetCapacity=(int32_t)(pFromUArgs->targetLimit-(char *)target);
5603
0
                    continue;
5604
0
                }
5605
0
            }
5606
0
        } else {
5607
            /* target is full */
5608
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
5609
0
            break;
5610
0
        }
5611
0
    }
5612
5613
    /*
5614
     * The sourceLimit may have been adjusted before the conversion loop
5615
     * to stop before a truncated sequence.
5616
     * If so, then collect the truncated sequence now.
5617
     */
5618
0
    if(U_SUCCESS(*pErrorCode) &&
5619
0
            cnv->preFromUFirstCP<0 &&
5620
0
            source<(sourceLimit=(uint8_t *)pToUArgs->sourceLimit)) {
5621
0
        c=utf8->toUBytes[0]=b=*source++;
5622
0
        toULength=1;
5623
0
        toULimit=U8_COUNT_TRAIL_BYTES(b)+1;
5624
0
        while(source<sourceLimit) {
5625
0
            utf8->toUBytes[toULength++]=b=*source++;
5626
0
            c=(c<<6)+b;
5627
0
        }
5628
0
        utf8->toUnicodeStatus=c;
5629
0
        utf8->toULength=toULength;
5630
0
        utf8->mode=toULimit;
5631
0
    }
5632
5633
    /* write back the updated pointers */
5634
0
    pToUArgs->source=(char *)source;
5635
0
    pFromUArgs->target=(char *)target;
5636
0
}
5637
5638
/* miscellaneous ------------------------------------------------------------ */
5639
5640
static void U_CALLCONV
5641
ucnv_MBCSGetStarters(const UConverter* cnv,
5642
                 UBool starters[256],
5643
0
                 UErrorCode *) {
5644
0
    const int32_t *state0;
5645
0
    int i;
5646
5647
0
    state0=cnv->sharedData->mbcs.stateTable[cnv->sharedData->mbcs.dbcsOnlyState];
5648
0
    for(i=0; i<256; ++i) {
5649
        /* all bytes that cause a state transition from state 0 are lead bytes */
5650
0
        starters[i]= (UBool)MBCS_ENTRY_IS_TRANSITION(state0[i]);
5651
0
    }
5652
0
}
5653
5654
/*
5655
 * This is an internal function that allows other converter implementations
5656
 * to check whether a byte is a lead byte.
5657
 */
5658
U_CFUNC UBool
5659
0
ucnv_MBCSIsLeadByte(UConverterSharedData *sharedData, char byte) {
5660
0
    return (UBool)MBCS_ENTRY_IS_TRANSITION(sharedData->mbcs.stateTable[0][(uint8_t)byte]);
5661
0
}
5662
5663
static void U_CALLCONV
5664
ucnv_MBCSWriteSub(UConverterFromUnicodeArgs *pArgs,
5665
              int32_t offsetIndex,
5666
0
              UErrorCode *pErrorCode) {
5667
0
    UConverter *cnv=pArgs->converter;
5668
0
    char *p, *subchar;
5669
0
    char buffer[4];
5670
0
    int32_t length;
5671
5672
    /* first, select between subChar and subChar1 */
5673
0
    if( cnv->subChar1!=0 &&
5674
0
        (cnv->sharedData->mbcs.extIndexes!=NULL ?
5675
0
            cnv->useSubChar1 :
5676
0
            (cnv->invalidUCharBuffer[0]<=0xff))
5677
0
    ) {
5678
        /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
5679
0
        subchar=(char *)&cnv->subChar1;
5680
0
        length=1;
5681
0
    } else {
5682
        /* select subChar in all other cases */
5683
0
        subchar=(char *)cnv->subChars;
5684
0
        length=cnv->subCharLen;
5685
0
    }
5686
5687
    /* reset the selector for the next code point */
5688
0
    cnv->useSubChar1=FALSE;
5689
5690
0
    if (cnv->sharedData->mbcs.outputType == MBCS_OUTPUT_2_SISO) {
5691
0
        p=buffer;
5692
5693
        /* fromUnicodeStatus contains prevLength */
5694
0
        switch(length) {
5695
0
        case 1:
5696
0
            if(cnv->fromUnicodeStatus==2) {
5697
                /* DBCS mode and SBCS sub char: change to SBCS */
5698
0
                cnv->fromUnicodeStatus=1;
5699
0
                *p++=UCNV_SI;
5700
0
            }
5701
0
            *p++=subchar[0];
5702
0
            break;
5703
0
        case 2:
5704
0
            if(cnv->fromUnicodeStatus<=1) {
5705
                /* SBCS mode and DBCS sub char: change to DBCS */
5706
0
                cnv->fromUnicodeStatus=2;
5707
0
                *p++=UCNV_SO;
5708
0
            }
5709
0
            *p++=subchar[0];
5710
0
            *p++=subchar[1];
5711
0
            break;
5712
0
        default:
5713
0
            *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
5714
0
            return;
5715
0
        }
5716
0
        subchar=buffer;
5717
0
        length=(int32_t)(p-buffer);
5718
0
    }
5719
5720
0
    ucnv_cbFromUWriteBytes(pArgs, subchar, length, offsetIndex, pErrorCode);
5721
0
}
5722
5723
U_CFUNC UConverterType
5724
0
ucnv_MBCSGetType(const UConverter* converter) {
5725
    /* SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but here we cheat a little */
5726
0
    if(converter->sharedData->mbcs.countStates==1) {
5727
0
        return (UConverterType)UCNV_SBCS;
5728
0
    } else if((converter->sharedData->mbcs.outputType&0xff)==MBCS_OUTPUT_2_SISO) {
5729
0
        return (UConverterType)UCNV_EBCDIC_STATEFUL;
5730
0
    } else if(converter->sharedData->staticData->minBytesPerChar==2 && converter->sharedData->staticData->maxBytesPerChar==2) {
5731
0
        return (UConverterType)UCNV_DBCS;
5732
0
    }
5733
0
    return (UConverterType)UCNV_MBCS;
5734
0
}
5735
5736
#endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */