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