Coverage Report

Created: 2025-07-11 06:23

/src/icu/source/common/ucnv_ct.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
**********************************************************************
5
*   Copyright (C) 2010-2015, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
**********************************************************************
8
*   file name:  ucnv_ct.c
9
*   encoding:   UTF-8
10
*   tab size:   8 (not used)
11
*   indentation:4
12
*
13
*   created on: 2010Dec09
14
*   created by: Michael Ow
15
*/
16
17
#include "unicode/utypes.h"
18
19
#if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
20
21
#include "unicode/ucnv.h"
22
#include "unicode/uset.h"
23
#include "unicode/ucnv_err.h"
24
#include "unicode/ucnv_cb.h"
25
#include "unicode/utf16.h"
26
#include "ucnv_imp.h"
27
#include "ucnv_bld.h"
28
#include "ucnv_cnv.h"
29
#include "ucnvmbcs.h"
30
#include "cstring.h"
31
#include "cmemory.h"
32
33
typedef enum {
34
    INVALID = -2,
35
    DO_SEARCH = -1,
36
37
    COMPOUND_TEXT_SINGLE_0 = 0,
38
    COMPOUND_TEXT_SINGLE_1 = 1,
39
    COMPOUND_TEXT_SINGLE_2 = 2,
40
    COMPOUND_TEXT_SINGLE_3 = 3,
41
42
    COMPOUND_TEXT_DOUBLE_1 = 4,
43
    COMPOUND_TEXT_DOUBLE_2 = 5,
44
    COMPOUND_TEXT_DOUBLE_3 = 6,
45
    COMPOUND_TEXT_DOUBLE_4 = 7,
46
    COMPOUND_TEXT_DOUBLE_5 = 8,
47
    COMPOUND_TEXT_DOUBLE_6 = 9,
48
    COMPOUND_TEXT_DOUBLE_7 = 10,
49
50
    COMPOUND_TEXT_TRIPLE_DOUBLE = 11,
51
52
    IBM_915 = 12,
53
    IBM_916 = 13,
54
    IBM_914 = 14,
55
    IBM_874 = 15,
56
    IBM_912 = 16,
57
    IBM_913 = 17,
58
    ISO_8859_14 = 18,
59
    IBM_923 = 19,
60
    NUM_OF_CONVERTERS = 20
61
} COMPOUND_TEXT_CONVERTERS;
62
63
0
#define SEARCH_LENGTH 12
64
65
static const uint8_t escSeqCompoundText[NUM_OF_CONVERTERS][5] = {
66
    /* Single */
67
    { 0x1B, 0x2D, 0x41, 0, 0 },
68
    { 0x1B, 0x2D, 0x4D, 0, 0 },
69
    { 0x1B, 0x2D, 0x46, 0, 0 },
70
    { 0x1B, 0x2D, 0x47, 0, 0 },
71
72
    /* Double */
73
    { 0x1B, 0x24, 0x29, 0x41, 0 },
74
    { 0x1B, 0x24, 0x29, 0x42, 0 },
75
    { 0x1B, 0x24, 0x29, 0x43, 0 },
76
    { 0x1B, 0x24, 0x29, 0x44, 0 },
77
    { 0x1B, 0x24, 0x29, 0x47, 0 },
78
    { 0x1B, 0x24, 0x29, 0x48, 0 },
79
    { 0x1B, 0x24, 0x29, 0x49, 0 },
80
81
    /* Triple/Double */
82
    { 0x1B, 0x25, 0x47, 0, 0 },
83
84
    /*IBM-915*/
85
    { 0x1B, 0x2D, 0x4C, 0, 0 },
86
    /*IBM-916*/
87
    { 0x1B, 0x2D, 0x48, 0, 0 },
88
    /*IBM-914*/
89
    { 0x1B, 0x2D, 0x44, 0, 0 },
90
    /*IBM-874*/
91
    { 0x1B, 0x2D, 0x54, 0, 0 },
92
    /*IBM-912*/
93
    { 0x1B, 0x2D, 0x42, 0, 0 },
94
    /* IBM-913 */
95
    { 0x1B, 0x2D, 0x43, 0, 0 },
96
    /* ISO-8859_14 */
97
    { 0x1B, 0x2D, 0x5F, 0, 0 },
98
    /* IBM-923 */
99
    { 0x1B, 0x2D, 0x62, 0, 0 },
100
};
101
102
0
#define ESC_START 0x1B
103
104
#define isASCIIRange(codepoint) \
105
0
        ((codepoint == 0x0000) || (codepoint == 0x0009) || (codepoint == 0x000A) || \
106
0
         (codepoint >= 0x0020 && codepoint <= 0x007f) || (codepoint >= 0x00A0 && codepoint <= 0x00FF))
107
108
#define isIBM915(codepoint) \
109
0
        ((codepoint >= 0x0401 && codepoint <= 0x045F) || (codepoint == 0x2116))
110
111
#define isIBM916(codepoint) \
112
0
        ((codepoint >= 0x05D0 && codepoint <= 0x05EA) || (codepoint == 0x2017) || (codepoint == 0x203E))
113
114
#define isCompoundS3(codepoint) \
115
0
        ((codepoint == 0x060C) || (codepoint == 0x061B) || (codepoint == 0x061F) || (codepoint >= 0x0621 && codepoint <= 0x063A) || \
116
0
         (codepoint >= 0x0640 && codepoint <= 0x0652) || (codepoint >= 0x0660 && codepoint <= 0x066D) || (codepoint == 0x200B) || \
117
0
         (codepoint >= 0x0FE70 && codepoint <= 0x0FE72) || (codepoint == 0x0FE74) || (codepoint >= 0x0FE76 && codepoint <= 0x0FEBE))
118
119
#define isCompoundS2(codepoint) \
120
0
        ((codepoint == 0x02BC) || (codepoint == 0x02BD) || (codepoint >= 0x0384 && codepoint <= 0x03CE) || (codepoint == 0x2015))
121
122
#define isIBM914(codepoint) \
123
0
        ((codepoint == 0x0100) || (codepoint == 0x0101) || (codepoint == 0x0112) || (codepoint == 0x0113) || (codepoint == 0x0116) || (codepoint == 0x0117) || \
124
0
         (codepoint == 0x0122) || (codepoint == 0x0123) || (codepoint >= 0x0128 && codepoint <= 0x012B) || (codepoint == 0x012E) || (codepoint == 0x012F) || \
125
0
         (codepoint >= 0x0136 && codepoint <= 0x0138) || (codepoint == 0x013B) || (codepoint == 0x013C) || (codepoint == 0x0145) || (codepoint ==  0x0146) || \
126
0
         (codepoint >= 0x014A && codepoint <= 0x014D) || (codepoint == 0x0156) || (codepoint == 0x0157) || (codepoint >= 0x0166 && codepoint <= 0x016B) || \
127
0
         (codepoint == 0x0172) || (codepoint == 0x0173))
128
129
#define isIBM874(codepoint) \
130
0
        ((codepoint >= 0x0E01 && codepoint <= 0x0E3A) || (codepoint >= 0x0E3F && codepoint <= 0x0E5B))
131
132
#define isIBM912(codepoint) \
133
0
        ((codepoint >= 0x0102 && codepoint <= 0x0107) || (codepoint >= 0x010C && codepoint <= 0x0111) || (codepoint >= 0x0118 && codepoint <= 0x011B) || \
134
0
         (codepoint == 0x0139) || (codepoint == 0x013A) || (codepoint == 0x013D) || (codepoint == 0x013E) || (codepoint >= 0x0141 && codepoint <= 0x0144) || \
135
0
         (codepoint == 0x0147) || (codepoint == 0x0147) || (codepoint == 0x0150) || (codepoint == 0x0151) || (codepoint == 0x0154) || (codepoint == 0x0155) || \
136
0
         (codepoint >= 0x0158 && codepoint <= 0x015B) || (codepoint == 0x015E) || (codepoint == 0x015F) || (codepoint >= 0x0160 && codepoint <= 0x0165) || \
137
0
         (codepoint == 0x016E) || (codepoint == 0x016F) || (codepoint == 0x0170) || (codepoint ==  0x0171) || (codepoint >= 0x0179 && codepoint <= 0x017E) || \
138
0
         (codepoint == 0x02C7) || (codepoint == 0x02D8) || (codepoint == 0x02D9) || (codepoint == 0x02DB) || (codepoint == 0x02DD))
139
140
#define isIBM913(codepoint) \
141
0
        ((codepoint >= 0x0108 && codepoint <= 0x010B) || (codepoint == 0x011C) || \
142
0
         (codepoint == 0x011D) || (codepoint == 0x0120) || (codepoint == 0x0121) || \
143
0
         (codepoint >= 0x0124 && codepoint <= 0x0127) || (codepoint == 0x0134) || (codepoint == 0x0135) || \
144
0
         (codepoint == 0x015C) || (codepoint == 0x015D) || (codepoint == 0x016C) || (codepoint ==  0x016D))
145
146
#define isCompoundS1(codepoint) \
147
0
        ((codepoint == 0x011E) || (codepoint == 0x011F) || (codepoint == 0x0130) || \
148
0
         (codepoint == 0x0131) || (codepoint >= 0x0218 && codepoint <= 0x021B))
149
150
#define isISO8859_14(codepoint) \
151
0
        ((codepoint >= 0x0174 && codepoint <= 0x0177) || (codepoint == 0x1E0A) || \
152
0
         (codepoint == 0x1E0B) || (codepoint == 0x1E1E) || (codepoint == 0x1E1F) || \
153
0
         (codepoint == 0x1E40) || (codepoint == 0x1E41) || (codepoint == 0x1E56) || \
154
0
         (codepoint == 0x1E57) || (codepoint == 0x1E60) || (codepoint == 0x1E61) || \
155
0
         (codepoint == 0x1E6A) || (codepoint == 0x1E6B) || (codepoint == 0x1EF2) || \
156
0
         (codepoint == 0x1EF3) || (codepoint >= 0x1E80 && codepoint <= 0x1E85))
157
158
#define isIBM923(codepoint) \
159
0
        ((codepoint == 0x0152) || (codepoint == 0x0153) || (codepoint == 0x0178) || (codepoint == 0x20AC))
160
161
162
typedef struct{
163
    UConverterSharedData *myConverterArray[NUM_OF_CONVERTERS];
164
    COMPOUND_TEXT_CONVERTERS state;
165
} UConverterDataCompoundText;
166
167
/*********** Compound Text Converter Protos ***********/
168
U_CDECL_BEGIN
169
static void U_CALLCONV
170
_CompoundTextOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode);
171
172
static void U_CALLCONV
173
 _CompoundTextClose(UConverter *converter);
174
175
static void U_CALLCONV
176
_CompoundTextReset(UConverter *converter, UConverterResetChoice choice);
177
178
static const char* U_CALLCONV
179
_CompoundTextgetName(const UConverter* cnv);
180
181
182
0
static int32_t findNextEsc(const char *source, const char *sourceLimit) {
183
0
    int32_t length = sourceLimit - source;
184
0
    int32_t i;
185
0
    for (i = 1; i < length; i++) {
186
0
        if (*(source + i) == 0x1B) {
187
0
            return i;
188
0
        }
189
0
    }
190
191
0
    return length;
192
0
}
193
194
0
static COMPOUND_TEXT_CONVERTERS getState(int codepoint) {
195
0
    COMPOUND_TEXT_CONVERTERS state = DO_SEARCH;
196
197
0
    if (isASCIIRange(codepoint)) {
198
0
        state = COMPOUND_TEXT_SINGLE_0;
199
0
    } else if (isIBM912(codepoint)) {
200
0
        state = IBM_912;
201
0
    }else if (isIBM913(codepoint)) {
202
0
        state = IBM_913;
203
0
    } else if (isISO8859_14(codepoint)) {
204
0
        state = ISO_8859_14;
205
0
    } else if (isIBM923(codepoint)) {
206
0
        state = IBM_923;
207
0
    } else if (isIBM874(codepoint)) {
208
0
        state = IBM_874;
209
0
    } else if (isIBM914(codepoint)) {
210
0
        state = IBM_914;
211
0
    } else if (isCompoundS2(codepoint)) {
212
0
        state = COMPOUND_TEXT_SINGLE_2;
213
0
    } else if (isCompoundS3(codepoint)) {
214
0
        state = COMPOUND_TEXT_SINGLE_3;
215
0
    } else if (isIBM916(codepoint)) {
216
0
        state = IBM_916;
217
0
    } else if (isIBM915(codepoint)) {
218
0
        state = IBM_915;
219
0
    } else if (isCompoundS1(codepoint)) {
220
0
        state = COMPOUND_TEXT_SINGLE_1;
221
0
    }
222
223
0
    return state;
224
0
}
225
226
0
static COMPOUND_TEXT_CONVERTERS findStateFromEscSeq(const char* source, const char* sourceLimit, const uint8_t* toUBytesBuffer, int32_t toUBytesBufferLength, UErrorCode *err) {
227
0
    COMPOUND_TEXT_CONVERTERS state = INVALID;
228
0
    UBool matchFound = FALSE;
229
0
    int32_t i, n, offset = toUBytesBufferLength;
230
231
0
    for (i = 0; i < NUM_OF_CONVERTERS; i++) {
232
0
        matchFound = TRUE;
233
0
        for (n = 0; escSeqCompoundText[i][n] != 0; n++) {
234
0
            if (n < toUBytesBufferLength) {
235
0
                if (toUBytesBuffer[n] != escSeqCompoundText[i][n]) {
236
0
                    matchFound = FALSE;
237
0
                    break;
238
0
                }
239
0
            } else if ((source + (n - offset)) >= sourceLimit) {
240
0
                *err = U_TRUNCATED_CHAR_FOUND;
241
0
                matchFound = FALSE;
242
0
                break;
243
0
            } else if (*(source + (n - offset)) != escSeqCompoundText[i][n]) {
244
0
                matchFound = FALSE;
245
0
                break;
246
0
            }
247
0
        }
248
249
0
        if (matchFound) {
250
0
            break;
251
0
        }
252
0
    }
253
254
0
    if (matchFound) {
255
0
        state = (COMPOUND_TEXT_CONVERTERS)i;
256
0
    }
257
258
0
    return state;
259
0
}
260
261
static void U_CALLCONV
262
0
_CompoundTextOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode){
263
0
    cnv->extraInfo = uprv_malloc (sizeof (UConverterDataCompoundText));
264
0
    if (cnv->extraInfo != NULL) {
265
0
        UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *) cnv->extraInfo;
266
267
0
        UConverterNamePieces stackPieces;
268
0
        UConverterLoadArgs stackArgs=UCNV_LOAD_ARGS_INITIALIZER;
269
270
0
        myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_0] = NULL;
271
0
        myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_1] = ucnv_loadSharedData("icu-internal-compound-s1", &stackPieces, &stackArgs, errorCode);
272
0
        myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_2] = ucnv_loadSharedData("icu-internal-compound-s2", &stackPieces, &stackArgs, errorCode);
273
0
        myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_3] = ucnv_loadSharedData("icu-internal-compound-s3", &stackPieces, &stackArgs, errorCode);
274
0
        myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_1] = ucnv_loadSharedData("icu-internal-compound-d1", &stackPieces, &stackArgs, errorCode);
275
0
        myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_2] = ucnv_loadSharedData("icu-internal-compound-d2", &stackPieces, &stackArgs, errorCode);
276
0
        myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_3] = ucnv_loadSharedData("icu-internal-compound-d3", &stackPieces, &stackArgs, errorCode);
277
0
        myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_4] = ucnv_loadSharedData("icu-internal-compound-d4", &stackPieces, &stackArgs, errorCode);
278
0
        myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_5] = ucnv_loadSharedData("icu-internal-compound-d5", &stackPieces, &stackArgs, errorCode);
279
0
        myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_6] = ucnv_loadSharedData("icu-internal-compound-d6", &stackPieces, &stackArgs, errorCode);
280
0
        myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_7] = ucnv_loadSharedData("icu-internal-compound-d7", &stackPieces, &stackArgs, errorCode);
281
0
        myConverterData->myConverterArray[COMPOUND_TEXT_TRIPLE_DOUBLE] = ucnv_loadSharedData("icu-internal-compound-t", &stackPieces, &stackArgs, errorCode);
282
283
0
        myConverterData->myConverterArray[IBM_915] = ucnv_loadSharedData("ibm-915_P100-1995", &stackPieces, &stackArgs, errorCode);
284
0
        myConverterData->myConverterArray[IBM_916] = ucnv_loadSharedData("ibm-916_P100-1995", &stackPieces, &stackArgs, errorCode);
285
0
        myConverterData->myConverterArray[IBM_914] = ucnv_loadSharedData("ibm-914_P100-1995", &stackPieces, &stackArgs, errorCode);
286
0
        myConverterData->myConverterArray[IBM_874] = ucnv_loadSharedData("ibm-874_P100-1995", &stackPieces, &stackArgs, errorCode);
287
0
        myConverterData->myConverterArray[IBM_912] = ucnv_loadSharedData("ibm-912_P100-1995", &stackPieces, &stackArgs, errorCode);
288
0
        myConverterData->myConverterArray[IBM_913] = ucnv_loadSharedData("ibm-913_P100-2000", &stackPieces, &stackArgs, errorCode);
289
0
        myConverterData->myConverterArray[ISO_8859_14] = ucnv_loadSharedData("iso-8859_14-1998", &stackPieces, &stackArgs, errorCode);
290
0
        myConverterData->myConverterArray[IBM_923] = ucnv_loadSharedData("ibm-923_P100-1998", &stackPieces, &stackArgs, errorCode);
291
292
0
        if (U_FAILURE(*errorCode) || pArgs->onlyTestIsLoadable) {
293
0
            _CompoundTextClose(cnv);
294
0
            return;
295
0
        }
296
297
0
        myConverterData->state = (COMPOUND_TEXT_CONVERTERS)0;
298
0
    } else {
299
0
        *errorCode = U_MEMORY_ALLOCATION_ERROR;
300
0
    }
301
0
}
302
303
304
static void U_CALLCONV
305
0
_CompoundTextClose(UConverter *converter) {
306
0
    UConverterDataCompoundText* myConverterData = (UConverterDataCompoundText*)(converter->extraInfo);
307
0
    int32_t i;
308
309
0
    if (converter->extraInfo != NULL) {
310
        /*close the array of converter pointers and free the memory*/
311
0
        for (i = 0; i < NUM_OF_CONVERTERS; i++) {
312
0
            if (myConverterData->myConverterArray[i] != NULL) {
313
0
                ucnv_unloadSharedDataIfReady(myConverterData->myConverterArray[i]);
314
0
            }
315
0
        }
316
317
0
        uprv_free(converter->extraInfo);
318
0
    }
319
0
}
320
321
static void U_CALLCONV
322
0
_CompoundTextReset(UConverter *converter, UConverterResetChoice choice) {
323
0
    (void)converter;
324
0
    (void)choice;
325
0
}
326
327
static const char* U_CALLCONV
328
0
_CompoundTextgetName(const UConverter* cnv){
329
0
    (void)cnv;
330
0
    return "x11-compound-text";
331
0
}
332
333
static void U_CALLCONV
334
0
UConverter_fromUnicode_CompoundText_OFFSETS(UConverterFromUnicodeArgs* args, UErrorCode* err){
335
0
    UConverter *cnv = args->converter;
336
0
    uint8_t *target = (uint8_t *) args->target;
337
0
    const uint8_t *targetLimit = (const uint8_t *) args->targetLimit;
338
0
    const UChar* source = args->source;
339
0
    const UChar* sourceLimit = args->sourceLimit;
340
    /* int32_t* offsets = args->offsets; */
341
0
    UChar32 sourceChar;
342
0
    UBool useFallback = cnv->useFallback;
343
0
    uint8_t tmpTargetBuffer[7];
344
0
    int32_t tmpTargetBufferLength = 0;
345
0
    COMPOUND_TEXT_CONVERTERS currentState, tmpState;
346
0
    uint32_t pValue;
347
0
    int32_t pValueLength = 0;
348
0
    int32_t i, n, j;
349
350
0
    UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *) cnv->extraInfo;
351
352
0
    currentState = myConverterData->state;
353
354
    /* check if the last codepoint of previous buffer was a lead surrogate*/
355
0
    if((sourceChar = cnv->fromUChar32)!=0 && target< targetLimit) {
356
0
        goto getTrail;
357
0
    }
358
359
0
    while( source < sourceLimit){
360
0
        if(target < targetLimit){
361
362
0
            sourceChar  = *(source++);
363
            /*check if the char is a First surrogate*/
364
0
             if(U16_IS_SURROGATE(sourceChar)) {
365
0
                if(U16_IS_SURROGATE_LEAD(sourceChar)) {
366
0
getTrail:
367
                    /*look ahead to find the trail surrogate*/
368
0
                    if(source < sourceLimit) {
369
                        /* test the following code unit */
370
0
                        UChar trail=(UChar) *source;
371
0
                        if(U16_IS_TRAIL(trail)) {
372
0
                            source++;
373
0
                            sourceChar=U16_GET_SUPPLEMENTARY(sourceChar, trail);
374
0
                            cnv->fromUChar32=0x00;
375
                            /* convert this supplementary code point */
376
                            /* exit this condition tree */
377
0
                        } else {
378
                            /* this is an unmatched lead code unit (1st surrogate) */
379
                            /* callback(illegal) */
380
0
                            *err=U_ILLEGAL_CHAR_FOUND;
381
0
                            cnv->fromUChar32=sourceChar;
382
0
                            break;
383
0
                        }
384
0
                    } else {
385
                        /* no more input */
386
0
                        cnv->fromUChar32=sourceChar;
387
0
                        break;
388
0
                    }
389
0
                } else {
390
                    /* this is an unmatched trail code unit (2nd surrogate) */
391
                    /* callback(illegal) */
392
0
                    *err=U_ILLEGAL_CHAR_FOUND;
393
0
                    cnv->fromUChar32=sourceChar;
394
0
                    break;
395
0
                }
396
0
            }
397
398
0
             tmpTargetBufferLength = 0;
399
0
             tmpState = getState(sourceChar);
400
401
0
             if (tmpState != DO_SEARCH && currentState != tmpState) {
402
                 /* Get escape sequence if necessary */
403
0
                 currentState = tmpState;
404
0
                 for (i = 0; escSeqCompoundText[currentState][i] != 0; i++) {
405
0
                     tmpTargetBuffer[tmpTargetBufferLength++] = escSeqCompoundText[currentState][i];
406
0
                 }
407
0
             }
408
409
0
             if (tmpState == DO_SEARCH) {
410
                 /* Test all available converters */
411
0
                 for (i = 1; i < SEARCH_LENGTH; i++) {
412
0
                     pValueLength = ucnv_MBCSFromUChar32(myConverterData->myConverterArray[i], sourceChar, &pValue, useFallback);
413
0
                     if (pValueLength > 0) {
414
0
                         tmpState = (COMPOUND_TEXT_CONVERTERS)i;
415
0
                         if (currentState != tmpState) {
416
0
                             currentState = tmpState;
417
0
                             for (j = 0; escSeqCompoundText[currentState][j] != 0; j++) {
418
0
                                 tmpTargetBuffer[tmpTargetBufferLength++] = escSeqCompoundText[currentState][j];
419
0
                             }
420
0
                         }
421
0
                         for (n = (pValueLength - 1); n >= 0; n--) {
422
0
                             tmpTargetBuffer[tmpTargetBufferLength++] = (uint8_t)(pValue >> (n * 8));
423
0
                         }
424
0
                         break;
425
0
                     }
426
0
                 }
427
0
             } else if (tmpState == COMPOUND_TEXT_SINGLE_0) {
428
0
                 tmpTargetBuffer[tmpTargetBufferLength++] = (uint8_t)sourceChar;
429
0
             } else {
430
0
                 pValueLength = ucnv_MBCSFromUChar32(myConverterData->myConverterArray[currentState], sourceChar, &pValue, useFallback);
431
0
                 if (pValueLength > 0) {
432
0
                     for (n = (pValueLength - 1); n >= 0; n--) {
433
0
                         tmpTargetBuffer[tmpTargetBufferLength++] = (uint8_t)(pValue >> (n * 8));
434
0
                     }
435
0
                 }
436
0
             }
437
438
0
             for (i = 0; i < tmpTargetBufferLength; i++) {
439
0
                 if (target < targetLimit) {
440
0
                     *target++ = tmpTargetBuffer[i];
441
0
                 } else {
442
0
                     *err = U_BUFFER_OVERFLOW_ERROR;
443
0
                     break;
444
0
                 }
445
0
             }
446
447
0
             if (*err == U_BUFFER_OVERFLOW_ERROR) {
448
0
                 for (; i < tmpTargetBufferLength; i++) {
449
0
                     args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = tmpTargetBuffer[i];
450
0
                 }
451
0
             }
452
0
        } else {
453
0
            *err = U_BUFFER_OVERFLOW_ERROR;
454
0
            break;
455
0
        }
456
0
    }
457
458
    /*save the state and return */
459
0
    myConverterData->state = currentState;
460
0
    args->source = source;
461
0
    args->target = (char*)target;
462
0
}
463
464
465
static void U_CALLCONV
466
UConverter_toUnicode_CompoundText_OFFSETS(UConverterToUnicodeArgs *args,
467
0
                                               UErrorCode* err){
468
0
    const char *mySource = (char *) args->source;
469
0
    UChar *myTarget = args->target;
470
0
    const char *mySourceLimit = args->sourceLimit;
471
0
    const char *tmpSourceLimit = mySourceLimit;
472
0
    uint32_t mySourceChar = 0x0000;
473
0
    COMPOUND_TEXT_CONVERTERS currentState, tmpState;
474
0
    int32_t sourceOffset = 0;
475
0
    UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *) args->converter->extraInfo;
476
0
    UConverterSharedData* savedSharedData = NULL;
477
478
0
    UConverterToUnicodeArgs subArgs;
479
0
    int32_t minArgsSize;
480
481
    /* set up the subconverter arguments */
482
0
    if(args->size<sizeof(UConverterToUnicodeArgs)) {
483
0
        minArgsSize = args->size;
484
0
    } else {
485
0
        minArgsSize = (int32_t)sizeof(UConverterToUnicodeArgs);
486
0
    }
487
488
0
    uprv_memcpy(&subArgs, args, minArgsSize);
489
0
    subArgs.size = (uint16_t)minArgsSize;
490
491
0
    currentState = tmpState =  myConverterData->state;
492
493
0
    while(mySource < mySourceLimit){
494
0
        if(myTarget < args->targetLimit){
495
0
            if (args->converter->toULength > 0) {
496
0
                mySourceChar = args->converter->toUBytes[0];
497
0
            } else {
498
0
                mySourceChar = (uint8_t)*mySource;
499
0
            }
500
501
0
            if (mySourceChar == ESC_START) {
502
0
                tmpState = findStateFromEscSeq(mySource, mySourceLimit, args->converter->toUBytes, args->converter->toULength, err);
503
504
0
                if (*err == U_TRUNCATED_CHAR_FOUND) {
505
0
                    for (; mySource < mySourceLimit;) {
506
0
                        args->converter->toUBytes[args->converter->toULength++] = *mySource++;
507
0
                    }
508
0
                    *err = U_ZERO_ERROR;
509
0
                    break;
510
0
                } else if (tmpState == INVALID) {
511
0
                    if (args->converter->toULength == 0) {
512
0
                        mySource++; /* skip over the 0x1b byte */
513
0
                    }
514
0
                    *err = U_ILLEGAL_CHAR_FOUND;
515
0
                    break;
516
0
                }
517
518
0
                if (tmpState != currentState) {
519
0
                    currentState = tmpState;
520
0
                }
521
522
0
                sourceOffset = uprv_strlen((char*)escSeqCompoundText[currentState]) - args->converter->toULength;
523
524
0
                mySource += sourceOffset;
525
526
0
                args->converter->toULength = 0;
527
0
            }
528
529
0
            if (currentState == COMPOUND_TEXT_SINGLE_0) {
530
0
                while (mySource < mySourceLimit) {
531
0
                    if (*mySource == ESC_START) {
532
0
                        break;
533
0
                    }
534
0
                    if (myTarget < args->targetLimit) {
535
0
                        *myTarget++ = 0x00ff&(*mySource++);
536
0
                    } else {
537
0
                        *err = U_BUFFER_OVERFLOW_ERROR;
538
0
                        break;
539
0
                    }
540
0
                }
541
0
            } else if (mySource < mySourceLimit){
542
0
                sourceOffset = findNextEsc(mySource, mySourceLimit);
543
544
0
                tmpSourceLimit = mySource + sourceOffset;
545
546
0
                subArgs.source = mySource;
547
0
                subArgs.sourceLimit = tmpSourceLimit;
548
0
                subArgs.target = myTarget;
549
0
                savedSharedData = subArgs.converter->sharedData;
550
0
                subArgs.converter->sharedData = myConverterData->myConverterArray[currentState];
551
552
0
                ucnv_MBCSToUnicodeWithOffsets(&subArgs, err);
553
554
0
                subArgs.converter->sharedData = savedSharedData;
555
556
0
                mySource = subArgs.source;
557
0
                myTarget = subArgs.target;
558
559
0
                if (U_FAILURE(*err)) {
560
0
                    if(*err == U_BUFFER_OVERFLOW_ERROR) {
561
0
                        if(subArgs.converter->UCharErrorBufferLength > 0) {
562
0
                            uprv_memcpy(args->converter->UCharErrorBuffer, subArgs.converter->UCharErrorBuffer,
563
0
                                        subArgs.converter->UCharErrorBufferLength);
564
0
                        }
565
0
                        args->converter->UCharErrorBufferLength=subArgs.converter->UCharErrorBufferLength;
566
0
                        subArgs.converter->UCharErrorBufferLength = 0;
567
0
                    }
568
0
                    break;
569
0
                }
570
0
            }
571
0
        } else {
572
0
            *err = U_BUFFER_OVERFLOW_ERROR;
573
0
            break;
574
0
        }
575
0
    }
576
0
    myConverterData->state = currentState;
577
0
    args->target = myTarget;
578
0
    args->source = mySource;
579
0
}
580
581
static void U_CALLCONV
582
_CompoundText_GetUnicodeSet(const UConverter *cnv,
583
                    const USetAdder *sa,
584
                    UConverterUnicodeSet which,
585
0
                    UErrorCode *pErrorCode) {
586
0
    UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *)cnv->extraInfo;
587
0
    int32_t i;
588
589
0
    for (i = 1; i < NUM_OF_CONVERTERS; i++) {
590
0
        ucnv_MBCSGetUnicodeSetForUnicode(myConverterData->myConverterArray[i], sa, which, pErrorCode);
591
0
    }
592
0
    sa->add(sa->set, 0x0000);
593
0
    sa->add(sa->set, 0x0009);
594
0
    sa->add(sa->set, 0x000A);
595
0
    sa->addRange(sa->set, 0x0020, 0x007F);
596
0
    sa->addRange(sa->set, 0x00A0, 0x00FF);
597
0
}
598
U_CDECL_END
599
600
static const UConverterImpl _CompoundTextImpl = {
601
602
    UCNV_COMPOUND_TEXT,
603
604
    NULL,
605
    NULL,
606
607
    _CompoundTextOpen,
608
    _CompoundTextClose,
609
    _CompoundTextReset,
610
611
    UConverter_toUnicode_CompoundText_OFFSETS,
612
    UConverter_toUnicode_CompoundText_OFFSETS,
613
    UConverter_fromUnicode_CompoundText_OFFSETS,
614
    UConverter_fromUnicode_CompoundText_OFFSETS,
615
    NULL,
616
617
    NULL,
618
    _CompoundTextgetName,
619
    NULL,
620
    NULL,
621
    _CompoundText_GetUnicodeSet,
622
    NULL,
623
    NULL
624
};
625
626
static const UConverterStaticData _CompoundTextStaticData = {
627
    sizeof(UConverterStaticData),
628
    "COMPOUND_TEXT",
629
    0,
630
    UCNV_IBM,
631
    UCNV_COMPOUND_TEXT,
632
    1,
633
    6,
634
    { 0xef, 0, 0, 0 },
635
    1,
636
    FALSE,
637
    FALSE,
638
    0,
639
    0,
640
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
641
};
642
const UConverterSharedData _CompoundTextData =
643
        UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_CompoundTextStaticData, &_CompoundTextImpl);
644
645
#endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */