michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 2010-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * file name: ucnv_ct.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2010Dec09 michael@0: * created by: Michael Ow michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION michael@0: michael@0: #include "unicode/ucnv.h" michael@0: #include "unicode/uset.h" michael@0: #include "unicode/ucnv_err.h" michael@0: #include "unicode/ucnv_cb.h" michael@0: #include "unicode/utf16.h" michael@0: #include "ucnv_imp.h" michael@0: #include "ucnv_bld.h" michael@0: #include "ucnv_cnv.h" michael@0: #include "ucnvmbcs.h" michael@0: #include "cstring.h" michael@0: #include "cmemory.h" michael@0: michael@0: #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) michael@0: michael@0: typedef enum { michael@0: INVALID = -2, michael@0: DO_SEARCH = -1, michael@0: michael@0: COMPOUND_TEXT_SINGLE_0 = 0, michael@0: COMPOUND_TEXT_SINGLE_1 = 1, michael@0: COMPOUND_TEXT_SINGLE_2 = 2, michael@0: COMPOUND_TEXT_SINGLE_3 = 3, michael@0: michael@0: COMPOUND_TEXT_DOUBLE_1 = 4, michael@0: COMPOUND_TEXT_DOUBLE_2 = 5, michael@0: COMPOUND_TEXT_DOUBLE_3 = 6, michael@0: COMPOUND_TEXT_DOUBLE_4 = 7, michael@0: COMPOUND_TEXT_DOUBLE_5 = 8, michael@0: COMPOUND_TEXT_DOUBLE_6 = 9, michael@0: COMPOUND_TEXT_DOUBLE_7 = 10, michael@0: michael@0: COMPOUND_TEXT_TRIPLE_DOUBLE = 11, michael@0: michael@0: IBM_915 = 12, michael@0: IBM_916 = 13, michael@0: IBM_914 = 14, michael@0: IBM_874 = 15, michael@0: IBM_912 = 16, michael@0: IBM_913 = 17, michael@0: ISO_8859_14 = 18, michael@0: IBM_923 = 19, michael@0: NUM_OF_CONVERTERS = 20 michael@0: } COMPOUND_TEXT_CONVERTERS; michael@0: michael@0: #define SEARCH_LENGTH 12 michael@0: michael@0: static const uint8_t escSeqCompoundText[NUM_OF_CONVERTERS][5] = { michael@0: /* Single */ michael@0: { 0x1B, 0x2D, 0x41, 0, 0 }, michael@0: { 0x1B, 0x2D, 0x4D, 0, 0 }, michael@0: { 0x1B, 0x2D, 0x46, 0, 0 }, michael@0: { 0x1B, 0x2D, 0x47, 0, 0 }, michael@0: michael@0: /* Double */ michael@0: { 0x1B, 0x24, 0x29, 0x41, 0 }, michael@0: { 0x1B, 0x24, 0x29, 0x42, 0 }, michael@0: { 0x1B, 0x24, 0x29, 0x43, 0 }, michael@0: { 0x1B, 0x24, 0x29, 0x44, 0 }, michael@0: { 0x1B, 0x24, 0x29, 0x47, 0 }, michael@0: { 0x1B, 0x24, 0x29, 0x48, 0 }, michael@0: { 0x1B, 0x24, 0x29, 0x49, 0 }, michael@0: michael@0: /* Triple/Double */ michael@0: { 0x1B, 0x25, 0x47, 0, 0 }, michael@0: michael@0: /*IBM-915*/ michael@0: { 0x1B, 0x2D, 0x4C, 0, 0 }, michael@0: /*IBM-916*/ michael@0: { 0x1B, 0x2D, 0x48, 0, 0 }, michael@0: /*IBM-914*/ michael@0: { 0x1B, 0x2D, 0x44, 0, 0 }, michael@0: /*IBM-874*/ michael@0: { 0x1B, 0x2D, 0x54, 0, 0 }, michael@0: /*IBM-912*/ michael@0: { 0x1B, 0x2D, 0x42, 0, 0 }, michael@0: /* IBM-913 */ michael@0: { 0x1B, 0x2D, 0x43, 0, 0 }, michael@0: /* ISO-8859_14 */ michael@0: { 0x1B, 0x2D, 0x5F, 0, 0 }, michael@0: /* IBM-923 */ michael@0: { 0x1B, 0x2D, 0x62, 0, 0 }, michael@0: }; michael@0: michael@0: #define ESC_START 0x1B michael@0: michael@0: #define isASCIIRange(codepoint) \ michael@0: ((codepoint == 0x0000) || (codepoint == 0x0009) || (codepoint == 0x000A) || \ michael@0: (codepoint >= 0x0020 && codepoint <= 0x007f) || (codepoint >= 0x00A0 && codepoint <= 0x00FF)) michael@0: michael@0: #define isIBM915(codepoint) \ michael@0: ((codepoint >= 0x0401 && codepoint <= 0x045F) || (codepoint == 0x2116)) michael@0: michael@0: #define isIBM916(codepoint) \ michael@0: ((codepoint >= 0x05D0 && codepoint <= 0x05EA) || (codepoint == 0x2017) || (codepoint == 0x203E)) michael@0: michael@0: #define isCompoundS3(codepoint) \ michael@0: ((codepoint == 0x060C) || (codepoint == 0x061B) || (codepoint == 0x061F) || (codepoint >= 0x0621 && codepoint <= 0x063A) || \ michael@0: (codepoint >= 0x0640 && codepoint <= 0x0652) || (codepoint >= 0x0660 && codepoint <= 0x066D) || (codepoint == 0x200B) || \ michael@0: (codepoint >= 0x0FE70 && codepoint <= 0x0FE72) || (codepoint == 0x0FE74) || (codepoint >= 0x0FE76 && codepoint <= 0x0FEBE)) michael@0: michael@0: #define isCompoundS2(codepoint) \ michael@0: ((codepoint == 0x02BC) || (codepoint == 0x02BD) || (codepoint >= 0x0384 && codepoint <= 0x03CE) || (codepoint == 0x2015)) michael@0: michael@0: #define isIBM914(codepoint) \ michael@0: ((codepoint == 0x0100) || (codepoint == 0x0101) || (codepoint == 0x0112) || (codepoint == 0x0113) || (codepoint == 0x0116) || (codepoint == 0x0117) || \ michael@0: (codepoint == 0x0122) || (codepoint == 0x0123) || (codepoint >= 0x0128 && codepoint <= 0x012B) || (codepoint == 0x012E) || (codepoint == 0x012F) || \ michael@0: (codepoint >= 0x0136 && codepoint <= 0x0138) || (codepoint == 0x013B) || (codepoint == 0x013C) || (codepoint == 0x0145) || (codepoint == 0x0146) || \ michael@0: (codepoint >= 0x014A && codepoint <= 0x014D) || (codepoint == 0x0156) || (codepoint == 0x0157) || (codepoint >= 0x0166 && codepoint <= 0x016B) || \ michael@0: (codepoint == 0x0172) || (codepoint == 0x0173)) michael@0: michael@0: #define isIBM874(codepoint) \ michael@0: ((codepoint >= 0x0E01 && codepoint <= 0x0E3A) || (codepoint >= 0x0E3F && codepoint <= 0x0E5B)) michael@0: michael@0: #define isIBM912(codepoint) \ michael@0: ((codepoint >= 0x0102 && codepoint <= 0x0107) || (codepoint >= 0x010C && codepoint <= 0x0111) || (codepoint >= 0x0118 && codepoint <= 0x011B) || \ michael@0: (codepoint == 0x0139) || (codepoint == 0x013A) || (codepoint == 0x013D) || (codepoint == 0x013E) || (codepoint >= 0x0141 && codepoint <= 0x0144) || \ michael@0: (codepoint == 0x0147) || (codepoint == 0x0147) || (codepoint == 0x0150) || (codepoint == 0x0151) || (codepoint == 0x0154) || (codepoint == 0x0155) || \ michael@0: (codepoint >= 0x0158 && codepoint <= 0x015B) || (codepoint == 0x015E) || (codepoint == 0x015F) || (codepoint >= 0x0160 && codepoint <= 0x0165) || \ michael@0: (codepoint == 0x016E) || (codepoint == 0x016F) || (codepoint == 0x0170) || (codepoint == 0x0171) || (codepoint >= 0x0179 && codepoint <= 0x017E) || \ michael@0: (codepoint == 0x02C7) || (codepoint == 0x02D8) || (codepoint == 0x02D9) || (codepoint == 0x02DB) || (codepoint == 0x02DD)) michael@0: michael@0: #define isIBM913(codepoint) \ michael@0: ((codepoint >= 0x0108 && codepoint <= 0x010B) || (codepoint == 0x011C) || \ michael@0: (codepoint == 0x011D) || (codepoint == 0x0120) || (codepoint == 0x0121) || \ michael@0: (codepoint >= 0x0124 && codepoint <= 0x0127) || (codepoint == 0x0134) || (codepoint == 0x0135) || \ michael@0: (codepoint == 0x015C) || (codepoint == 0x015D) || (codepoint == 0x016C) || (codepoint == 0x016D)) michael@0: michael@0: #define isCompoundS1(codepoint) \ michael@0: ((codepoint == 0x011E) || (codepoint == 0x011F) || (codepoint == 0x0130) || \ michael@0: (codepoint == 0x0131) || (codepoint >= 0x0218 && codepoint <= 0x021B)) michael@0: michael@0: #define isISO8859_14(codepoint) \ michael@0: ((codepoint >= 0x0174 && codepoint <= 0x0177) || (codepoint == 0x1E0A) || \ michael@0: (codepoint == 0x1E0B) || (codepoint == 0x1E1E) || (codepoint == 0x1E1F) || \ michael@0: (codepoint == 0x1E40) || (codepoint == 0x1E41) || (codepoint == 0x1E56) || \ michael@0: (codepoint == 0x1E57) || (codepoint == 0x1E60) || (codepoint == 0x1E61) || \ michael@0: (codepoint == 0x1E6A) || (codepoint == 0x1E6B) || (codepoint == 0x1EF2) || \ michael@0: (codepoint == 0x1EF3) || (codepoint >= 0x1E80 && codepoint <= 0x1E85)) michael@0: michael@0: #define isIBM923(codepoint) \ michael@0: ((codepoint == 0x0152) || (codepoint == 0x0153) || (codepoint == 0x0178) || (codepoint == 0x20AC)) michael@0: michael@0: michael@0: typedef struct{ michael@0: UConverterSharedData *myConverterArray[NUM_OF_CONVERTERS]; michael@0: COMPOUND_TEXT_CONVERTERS state; michael@0: } UConverterDataCompoundText; michael@0: michael@0: /*********** Compound Text Converter Protos ***********/ michael@0: static void michael@0: _CompoundTextOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode); michael@0: michael@0: static void michael@0: _CompoundTextClose(UConverter *converter); michael@0: michael@0: static void michael@0: _CompoundTextReset(UConverter *converter, UConverterResetChoice choice); michael@0: michael@0: static const char* michael@0: _CompoundTextgetName(const UConverter* cnv); michael@0: michael@0: michael@0: static int32_t findNextEsc(const char *source, const char *sourceLimit) { michael@0: int32_t length = sourceLimit - source; michael@0: int32_t i; michael@0: for (i = 1; i < length; i++) { michael@0: if (*(source + i) == 0x1B) { michael@0: return i; michael@0: } michael@0: } michael@0: michael@0: return length; michael@0: } michael@0: michael@0: static COMPOUND_TEXT_CONVERTERS getState(int codepoint) { michael@0: COMPOUND_TEXT_CONVERTERS state = DO_SEARCH; michael@0: michael@0: if (isASCIIRange(codepoint)) { michael@0: state = COMPOUND_TEXT_SINGLE_0; michael@0: } else if (isIBM912(codepoint)) { michael@0: state = IBM_912; michael@0: }else if (isIBM913(codepoint)) { michael@0: state = IBM_913; michael@0: } else if (isISO8859_14(codepoint)) { michael@0: state = ISO_8859_14; michael@0: } else if (isIBM923(codepoint)) { michael@0: state = IBM_923; michael@0: } else if (isIBM874(codepoint)) { michael@0: state = IBM_874; michael@0: } else if (isIBM914(codepoint)) { michael@0: state = IBM_914; michael@0: } else if (isCompoundS2(codepoint)) { michael@0: state = COMPOUND_TEXT_SINGLE_2; michael@0: } else if (isCompoundS3(codepoint)) { michael@0: state = COMPOUND_TEXT_SINGLE_3; michael@0: } else if (isIBM916(codepoint)) { michael@0: state = IBM_916; michael@0: } else if (isIBM915(codepoint)) { michael@0: state = IBM_915; michael@0: } else if (isCompoundS1(codepoint)) { michael@0: state = COMPOUND_TEXT_SINGLE_1; michael@0: } michael@0: michael@0: return state; michael@0: } michael@0: michael@0: static COMPOUND_TEXT_CONVERTERS findStateFromEscSeq(const char* source, const char* sourceLimit, const uint8_t* toUBytesBuffer, int32_t toUBytesBufferLength, UErrorCode *err) { michael@0: COMPOUND_TEXT_CONVERTERS state = INVALID; michael@0: UBool matchFound = FALSE; michael@0: int32_t i, n, offset = toUBytesBufferLength; michael@0: michael@0: for (i = 0; i < NUM_OF_CONVERTERS; i++) { michael@0: matchFound = TRUE; michael@0: for (n = 0; escSeqCompoundText[i][n] != 0; n++) { michael@0: if (n < toUBytesBufferLength) { michael@0: if (toUBytesBuffer[n] != escSeqCompoundText[i][n]) { michael@0: matchFound = FALSE; michael@0: break; michael@0: } michael@0: } else if ((source + (n - offset)) >= sourceLimit) { michael@0: *err = U_TRUNCATED_CHAR_FOUND; michael@0: matchFound = FALSE; michael@0: break; michael@0: } else if (*(source + (n - offset)) != escSeqCompoundText[i][n]) { michael@0: matchFound = FALSE; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (matchFound) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (matchFound) { michael@0: state = (COMPOUND_TEXT_CONVERTERS)i; michael@0: } michael@0: michael@0: return state; michael@0: } michael@0: michael@0: static void michael@0: _CompoundTextOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode){ michael@0: cnv->extraInfo = uprv_malloc (sizeof (UConverterDataCompoundText)); michael@0: if (cnv->extraInfo != NULL) { michael@0: UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *) cnv->extraInfo; michael@0: michael@0: UConverterNamePieces stackPieces; michael@0: UConverterLoadArgs stackArgs={ (int32_t)sizeof(UConverterLoadArgs) }; michael@0: michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_0] = NULL; michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_1] = ucnv_loadSharedData("icu-internal-compound-s1", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_2] = ucnv_loadSharedData("icu-internal-compound-s2", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_SINGLE_3] = ucnv_loadSharedData("icu-internal-compound-s3", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_1] = ucnv_loadSharedData("icu-internal-compound-d1", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_2] = ucnv_loadSharedData("icu-internal-compound-d2", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_3] = ucnv_loadSharedData("icu-internal-compound-d3", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_4] = ucnv_loadSharedData("icu-internal-compound-d4", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_5] = ucnv_loadSharedData("icu-internal-compound-d5", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_6] = ucnv_loadSharedData("icu-internal-compound-d6", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_DOUBLE_7] = ucnv_loadSharedData("icu-internal-compound-d7", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[COMPOUND_TEXT_TRIPLE_DOUBLE] = ucnv_loadSharedData("icu-internal-compound-t", &stackPieces, &stackArgs, errorCode); michael@0: michael@0: myConverterData->myConverterArray[IBM_915] = ucnv_loadSharedData("ibm-915_P100-1995", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[IBM_916] = ucnv_loadSharedData("ibm-916_P100-1995", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[IBM_914] = ucnv_loadSharedData("ibm-914_P100-1995", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[IBM_874] = ucnv_loadSharedData("ibm-874_P100-1995", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[IBM_912] = ucnv_loadSharedData("ibm-912_P100-1995", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[IBM_913] = ucnv_loadSharedData("ibm-913_P100-2000", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[ISO_8859_14] = ucnv_loadSharedData("iso-8859_14-1998", &stackPieces, &stackArgs, errorCode); michael@0: myConverterData->myConverterArray[IBM_923] = ucnv_loadSharedData("ibm-923_P100-1998", &stackPieces, &stackArgs, errorCode); michael@0: michael@0: if (U_FAILURE(*errorCode) || pArgs->onlyTestIsLoadable) { michael@0: _CompoundTextClose(cnv); michael@0: return; michael@0: } michael@0: michael@0: myConverterData->state = (COMPOUND_TEXT_CONVERTERS)0; michael@0: } else { michael@0: *errorCode = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: _CompoundTextClose(UConverter *converter) { michael@0: UConverterDataCompoundText* myConverterData = (UConverterDataCompoundText*)(converter->extraInfo); michael@0: int32_t i; michael@0: michael@0: if (converter->extraInfo != NULL) { michael@0: /*close the array of converter pointers and free the memory*/ michael@0: for (i = 0; i < NUM_OF_CONVERTERS; i++) { michael@0: if (myConverterData->myConverterArray[i] != NULL) { michael@0: ucnv_unloadSharedDataIfReady(myConverterData->myConverterArray[i]); michael@0: } michael@0: } michael@0: michael@0: uprv_free(converter->extraInfo); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: _CompoundTextReset(UConverter *converter, UConverterResetChoice choice) { michael@0: } michael@0: michael@0: static const char* michael@0: _CompoundTextgetName(const UConverter* cnv){ michael@0: return "x11-compound-text"; michael@0: } michael@0: michael@0: static void michael@0: UConverter_fromUnicode_CompoundText_OFFSETS(UConverterFromUnicodeArgs* args, UErrorCode* err){ michael@0: UConverter *cnv = args->converter; michael@0: uint8_t *target = (uint8_t *) args->target; michael@0: const uint8_t *targetLimit = (const uint8_t *) args->targetLimit; michael@0: const UChar* source = args->source; michael@0: const UChar* sourceLimit = args->sourceLimit; michael@0: /* int32_t* offsets = args->offsets; */ michael@0: UChar32 sourceChar; michael@0: UBool useFallback = cnv->useFallback; michael@0: uint8_t tmpTargetBuffer[7]; michael@0: int32_t tmpTargetBufferLength = 0; michael@0: COMPOUND_TEXT_CONVERTERS currentState, tmpState; michael@0: uint32_t pValue; michael@0: int32_t pValueLength = 0; michael@0: int32_t i, n, j; michael@0: michael@0: UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *) cnv->extraInfo; michael@0: michael@0: currentState = myConverterData->state; michael@0: michael@0: /* check if the last codepoint of previous buffer was a lead surrogate*/ michael@0: if((sourceChar = cnv->fromUChar32)!=0 && target< targetLimit) { michael@0: goto getTrail; michael@0: } michael@0: michael@0: while( source < sourceLimit){ michael@0: if(target < targetLimit){ michael@0: michael@0: sourceChar = *(source++); michael@0: /*check if the char is a First surrogate*/ michael@0: if(U16_IS_SURROGATE(sourceChar)) { michael@0: if(U16_IS_SURROGATE_LEAD(sourceChar)) { michael@0: getTrail: michael@0: /*look ahead to find the trail surrogate*/ michael@0: if(source < sourceLimit) { michael@0: /* test the following code unit */ michael@0: UChar trail=(UChar) *source; michael@0: if(U16_IS_TRAIL(trail)) { michael@0: source++; michael@0: sourceChar=U16_GET_SUPPLEMENTARY(sourceChar, trail); michael@0: cnv->fromUChar32=0x00; michael@0: /* convert this supplementary code point */ michael@0: /* exit this condition tree */ michael@0: } else { michael@0: /* this is an unmatched lead code unit (1st surrogate) */ michael@0: /* callback(illegal) */ michael@0: *err=U_ILLEGAL_CHAR_FOUND; michael@0: cnv->fromUChar32=sourceChar; michael@0: break; michael@0: } michael@0: } else { michael@0: /* no more input */ michael@0: cnv->fromUChar32=sourceChar; michael@0: break; michael@0: } michael@0: } else { michael@0: /* this is an unmatched trail code unit (2nd surrogate) */ michael@0: /* callback(illegal) */ michael@0: *err=U_ILLEGAL_CHAR_FOUND; michael@0: cnv->fromUChar32=sourceChar; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: tmpTargetBufferLength = 0; michael@0: tmpState = getState(sourceChar); michael@0: michael@0: if (tmpState != DO_SEARCH && currentState != tmpState) { michael@0: /* Get escape sequence if necessary */ michael@0: currentState = tmpState; michael@0: for (i = 0; escSeqCompoundText[currentState][i] != 0; i++) { michael@0: tmpTargetBuffer[tmpTargetBufferLength++] = escSeqCompoundText[currentState][i]; michael@0: } michael@0: } michael@0: michael@0: if (tmpState == DO_SEARCH) { michael@0: /* Test all available converters */ michael@0: for (i = 1; i < SEARCH_LENGTH; i++) { michael@0: pValueLength = ucnv_MBCSFromUChar32(myConverterData->myConverterArray[i], sourceChar, &pValue, useFallback); michael@0: if (pValueLength > 0) { michael@0: tmpState = (COMPOUND_TEXT_CONVERTERS)i; michael@0: if (currentState != tmpState) { michael@0: currentState = tmpState; michael@0: for (j = 0; escSeqCompoundText[currentState][j] != 0; j++) { michael@0: tmpTargetBuffer[tmpTargetBufferLength++] = escSeqCompoundText[currentState][j]; michael@0: } michael@0: } michael@0: for (n = (pValueLength - 1); n >= 0; n--) { michael@0: tmpTargetBuffer[tmpTargetBufferLength++] = (uint8_t)(pValue >> (n * 8)); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } else if (tmpState == COMPOUND_TEXT_SINGLE_0) { michael@0: tmpTargetBuffer[tmpTargetBufferLength++] = (uint8_t)sourceChar; michael@0: } else { michael@0: pValueLength = ucnv_MBCSFromUChar32(myConverterData->myConverterArray[currentState], sourceChar, &pValue, useFallback); michael@0: if (pValueLength > 0) { michael@0: for (n = (pValueLength - 1); n >= 0; n--) { michael@0: tmpTargetBuffer[tmpTargetBufferLength++] = (uint8_t)(pValue >> (n * 8)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < tmpTargetBufferLength; i++) { michael@0: if (target < targetLimit) { michael@0: *target++ = tmpTargetBuffer[i]; michael@0: } else { michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (*err == U_BUFFER_OVERFLOW_ERROR) { michael@0: for (; i < tmpTargetBufferLength; i++) { michael@0: args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = tmpTargetBuffer[i]; michael@0: } michael@0: } michael@0: } else { michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /*save the state and return */ michael@0: myConverterData->state = currentState; michael@0: args->source = source; michael@0: args->target = (char*)target; michael@0: } michael@0: michael@0: michael@0: static void michael@0: UConverter_toUnicode_CompoundText_OFFSETS(UConverterToUnicodeArgs *args, michael@0: UErrorCode* err){ michael@0: const char *mySource = (char *) args->source; michael@0: UChar *myTarget = args->target; michael@0: const char *mySourceLimit = args->sourceLimit; michael@0: const char *tmpSourceLimit = mySourceLimit; michael@0: uint32_t mySourceChar = 0x0000; michael@0: COMPOUND_TEXT_CONVERTERS currentState, tmpState; michael@0: int32_t sourceOffset = 0; michael@0: UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *) args->converter->extraInfo; michael@0: UConverterSharedData* savedSharedData = NULL; michael@0: michael@0: UConverterToUnicodeArgs subArgs; michael@0: int32_t minArgsSize; michael@0: michael@0: /* set up the subconverter arguments */ michael@0: if(args->sizesize; michael@0: } else { michael@0: minArgsSize = (int32_t)sizeof(UConverterToUnicodeArgs); michael@0: } michael@0: michael@0: uprv_memcpy(&subArgs, args, minArgsSize); michael@0: subArgs.size = (uint16_t)minArgsSize; michael@0: michael@0: currentState = tmpState = myConverterData->state; michael@0: michael@0: while(mySource < mySourceLimit){ michael@0: if(myTarget < args->targetLimit){ michael@0: if (args->converter->toULength > 0) { michael@0: mySourceChar = args->converter->toUBytes[0]; michael@0: } else { michael@0: mySourceChar = (uint8_t)*mySource; michael@0: } michael@0: michael@0: if (mySourceChar == ESC_START) { michael@0: tmpState = findStateFromEscSeq(mySource, mySourceLimit, args->converter->toUBytes, args->converter->toULength, err); michael@0: michael@0: if (*err == U_TRUNCATED_CHAR_FOUND) { michael@0: for (; mySource < mySourceLimit;) { michael@0: args->converter->toUBytes[args->converter->toULength++] = *mySource++; michael@0: } michael@0: *err = U_ZERO_ERROR; michael@0: break; michael@0: } else if (tmpState == INVALID) { michael@0: if (args->converter->toULength == 0) { michael@0: mySource++; /* skip over the 0x1b byte */ michael@0: } michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: michael@0: if (tmpState != currentState) { michael@0: currentState = tmpState; michael@0: } michael@0: michael@0: sourceOffset = uprv_strlen((char*)escSeqCompoundText[currentState]) - args->converter->toULength; michael@0: michael@0: mySource += sourceOffset; michael@0: michael@0: args->converter->toULength = 0; michael@0: } michael@0: michael@0: if (currentState == COMPOUND_TEXT_SINGLE_0) { michael@0: while (mySource < mySourceLimit) { michael@0: if (*mySource == ESC_START) { michael@0: break; michael@0: } michael@0: if (myTarget < args->targetLimit) { michael@0: *myTarget++ = 0x00ff&(*mySource++); michael@0: } else { michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } else if (mySource < mySourceLimit){ michael@0: sourceOffset = findNextEsc(mySource, mySourceLimit); michael@0: michael@0: tmpSourceLimit = mySource + sourceOffset; michael@0: michael@0: subArgs.source = mySource; michael@0: subArgs.sourceLimit = tmpSourceLimit; michael@0: subArgs.target = myTarget; michael@0: savedSharedData = subArgs.converter->sharedData; michael@0: subArgs.converter->sharedData = myConverterData->myConverterArray[currentState]; michael@0: michael@0: ucnv_MBCSToUnicodeWithOffsets(&subArgs, err); michael@0: michael@0: subArgs.converter->sharedData = savedSharedData; michael@0: michael@0: mySource = subArgs.source; michael@0: myTarget = subArgs.target; michael@0: michael@0: if (U_FAILURE(*err)) { michael@0: if(*err == U_BUFFER_OVERFLOW_ERROR) { michael@0: if(subArgs.converter->UCharErrorBufferLength > 0) { michael@0: uprv_memcpy(args->converter->UCharErrorBuffer, subArgs.converter->UCharErrorBuffer, michael@0: subArgs.converter->UCharErrorBufferLength); michael@0: } michael@0: args->converter->UCharErrorBufferLength=subArgs.converter->UCharErrorBufferLength; michael@0: subArgs.converter->UCharErrorBufferLength = 0; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } else { michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: myConverterData->state = currentState; michael@0: args->target = myTarget; michael@0: args->source = mySource; michael@0: } michael@0: michael@0: static void michael@0: _CompoundText_GetUnicodeSet(const UConverter *cnv, michael@0: const USetAdder *sa, michael@0: UConverterUnicodeSet which, michael@0: UErrorCode *pErrorCode) { michael@0: UConverterDataCompoundText *myConverterData = (UConverterDataCompoundText *)cnv->extraInfo; michael@0: int32_t i; michael@0: michael@0: for (i = 1; i < NUM_OF_CONVERTERS; i++) { michael@0: ucnv_MBCSGetUnicodeSetForUnicode(myConverterData->myConverterArray[i], sa, which, pErrorCode); michael@0: } michael@0: sa->add(sa->set, 0x0000); michael@0: sa->add(sa->set, 0x0009); michael@0: sa->add(sa->set, 0x000A); michael@0: sa->addRange(sa->set, 0x0020, 0x007F); michael@0: sa->addRange(sa->set, 0x00A0, 0x00FF); michael@0: } michael@0: michael@0: static const UConverterImpl _CompoundTextImpl = { michael@0: michael@0: UCNV_COMPOUND_TEXT, michael@0: michael@0: NULL, michael@0: NULL, michael@0: michael@0: _CompoundTextOpen, michael@0: _CompoundTextClose, michael@0: _CompoundTextReset, michael@0: michael@0: UConverter_toUnicode_CompoundText_OFFSETS, michael@0: UConverter_toUnicode_CompoundText_OFFSETS, michael@0: UConverter_fromUnicode_CompoundText_OFFSETS, michael@0: UConverter_fromUnicode_CompoundText_OFFSETS, michael@0: NULL, michael@0: michael@0: NULL, michael@0: _CompoundTextgetName, michael@0: NULL, michael@0: NULL, michael@0: _CompoundText_GetUnicodeSet michael@0: }; michael@0: static const UConverterStaticData _CompoundTextStaticData = { michael@0: sizeof(UConverterStaticData), michael@0: "COMPOUND_TEXT", michael@0: 0, michael@0: UCNV_IBM, michael@0: UCNV_COMPOUND_TEXT, michael@0: 1, michael@0: 6, michael@0: { 0xef, 0, 0, 0 }, michael@0: 1, michael@0: FALSE, michael@0: FALSE, michael@0: 0, michael@0: 0, michael@0: { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */ michael@0: }; michael@0: const UConverterSharedData _CompoundTextData = { michael@0: sizeof(UConverterSharedData), michael@0: ~((uint32_t) 0), michael@0: NULL, michael@0: NULL, michael@0: &_CompoundTextStaticData, michael@0: FALSE, michael@0: &_CompoundTextImpl, michael@0: 0 michael@0: }; michael@0: michael@0: #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */