Coverage Report

Created: 2026-03-31 06:12

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