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