ucnv_ct.cpp 25 KB

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