ucnv_bld.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ********************************************************************
  5. * COPYRIGHT:
  6. * Copyright (c) 1996-2016, International Business Machines Corporation and
  7. * others. All Rights Reserved.
  8. ********************************************************************
  9. *
  10. * ucnv_bld.cpp:
  11. *
  12. * Defines functions that are used in the creation/initialization/deletion
  13. * of converters and related structures.
  14. * uses uconv_io.h routines to access disk information
  15. * is used by ucnv.h to implement public API create/delete/flushCache routines
  16. * Modification History:
  17. *
  18. * Date Name Description
  19. *
  20. * 06/20/2000 helena OS/400 port changes; mostly typecast.
  21. * 06/29/2000 helena Major rewrite of the callback interface.
  22. */
  23. #include "unicode/utypes.h"
  24. #if !UCONFIG_NO_CONVERSION
  25. #include "unicode/putil.h"
  26. #include "unicode/udata.h"
  27. #include "unicode/ucnv.h"
  28. #include "unicode/uloc.h"
  29. #include "mutex.h"
  30. #include "putilimp.h"
  31. #include "uassert.h"
  32. #include "utracimp.h"
  33. #include "ucnv_io.h"
  34. #include "ucnv_bld.h"
  35. #include "ucnvmbcs.h"
  36. #include "ucnv_ext.h"
  37. #include "ucnv_cnv.h"
  38. #include "ucnv_imp.h"
  39. #include "uhash.h"
  40. #include "umutex.h"
  41. #include "cstring.h"
  42. #include "cmemory.h"
  43. #include "ucln_cmn.h"
  44. #include "ustr_cnv.h"
  45. #if 0
  46. #include <stdio.h>
  47. extern void UCNV_DEBUG_LOG(char *what, char *who, void *p, int l);
  48. #define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
  49. #else
  50. # define UCNV_DEBUG_LOG(x,y,z)
  51. #endif
  52. static const UConverterSharedData * const
  53. converterData[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES]={
  54. nullptr, nullptr,
  55. #if UCONFIG_NO_LEGACY_CONVERSION
  56. nullptr,
  57. #else
  58. &_MBCSData,
  59. #endif
  60. &_Latin1Data,
  61. &_UTF8Data, &_UTF16BEData, &_UTF16LEData,
  62. #if UCONFIG_ONLY_HTML_CONVERSION
  63. nullptr, nullptr,
  64. #else
  65. &_UTF32BEData, &_UTF32LEData,
  66. #endif
  67. nullptr,
  68. #if UCONFIG_NO_LEGACY_CONVERSION
  69. nullptr,
  70. #else
  71. &_ISO2022Data,
  72. #endif
  73. #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
  74. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  75. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  76. nullptr,
  77. #else
  78. &_LMBCSData1,&_LMBCSData2, &_LMBCSData3, &_LMBCSData4, &_LMBCSData5, &_LMBCSData6,
  79. &_LMBCSData8,&_LMBCSData11,&_LMBCSData16,&_LMBCSData17,&_LMBCSData18,&_LMBCSData19,
  80. &_HZData,
  81. #endif
  82. #if UCONFIG_ONLY_HTML_CONVERSION
  83. nullptr,
  84. #else
  85. &_SCSUData,
  86. #endif
  87. #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
  88. nullptr,
  89. #else
  90. &_ISCIIData,
  91. #endif
  92. &_ASCIIData,
  93. #if UCONFIG_ONLY_HTML_CONVERSION
  94. nullptr, nullptr, &_UTF16Data, nullptr, nullptr, nullptr,
  95. #else
  96. &_UTF7Data, &_Bocu1Data, &_UTF16Data, &_UTF32Data, &_CESU8Data, &_IMAPData,
  97. #endif
  98. #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
  99. nullptr,
  100. #else
  101. &_CompoundTextData
  102. #endif
  103. };
  104. /* Please keep this in binary sorted order for getAlgorithmicTypeFromName.
  105. Also the name should be in lower case and all spaces, dashes and underscores
  106. removed
  107. */
  108. static struct {
  109. const char *name;
  110. const UConverterType type;
  111. } const cnvNameType[] = {
  112. #if !UCONFIG_ONLY_HTML_CONVERSION
  113. { "bocu1", UCNV_BOCU1 },
  114. { "cesu8", UCNV_CESU8 },
  115. #endif
  116. #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
  117. { "hz",UCNV_HZ },
  118. #endif
  119. #if !UCONFIG_ONLY_HTML_CONVERSION
  120. { "imapmailboxname", UCNV_IMAP_MAILBOX },
  121. #endif
  122. #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
  123. { "iscii", UCNV_ISCII },
  124. #endif
  125. #if !UCONFIG_NO_LEGACY_CONVERSION
  126. { "iso2022", UCNV_ISO_2022 },
  127. #endif
  128. { "iso88591", UCNV_LATIN_1 },
  129. #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
  130. { "lmbcs1", UCNV_LMBCS_1 },
  131. { "lmbcs11",UCNV_LMBCS_11 },
  132. { "lmbcs16",UCNV_LMBCS_16 },
  133. { "lmbcs17",UCNV_LMBCS_17 },
  134. { "lmbcs18",UCNV_LMBCS_18 },
  135. { "lmbcs19",UCNV_LMBCS_19 },
  136. { "lmbcs2", UCNV_LMBCS_2 },
  137. { "lmbcs3", UCNV_LMBCS_3 },
  138. { "lmbcs4", UCNV_LMBCS_4 },
  139. { "lmbcs5", UCNV_LMBCS_5 },
  140. { "lmbcs6", UCNV_LMBCS_6 },
  141. { "lmbcs8", UCNV_LMBCS_8 },
  142. #endif
  143. #if !UCONFIG_ONLY_HTML_CONVERSION
  144. { "scsu", UCNV_SCSU },
  145. #endif
  146. { "usascii", UCNV_US_ASCII },
  147. { "utf16", UCNV_UTF16 },
  148. { "utf16be", UCNV_UTF16_BigEndian },
  149. { "utf16le", UCNV_UTF16_LittleEndian },
  150. #if U_IS_BIG_ENDIAN
  151. { "utf16oppositeendian", UCNV_UTF16_LittleEndian },
  152. { "utf16platformendian", UCNV_UTF16_BigEndian },
  153. #else
  154. { "utf16oppositeendian", UCNV_UTF16_BigEndian},
  155. { "utf16platformendian", UCNV_UTF16_LittleEndian },
  156. #endif
  157. #if !UCONFIG_ONLY_HTML_CONVERSION
  158. { "utf32", UCNV_UTF32 },
  159. { "utf32be", UCNV_UTF32_BigEndian },
  160. { "utf32le", UCNV_UTF32_LittleEndian },
  161. #if U_IS_BIG_ENDIAN
  162. { "utf32oppositeendian", UCNV_UTF32_LittleEndian },
  163. { "utf32platformendian", UCNV_UTF32_BigEndian },
  164. #else
  165. { "utf32oppositeendian", UCNV_UTF32_BigEndian },
  166. { "utf32platformendian", UCNV_UTF32_LittleEndian },
  167. #endif
  168. #endif
  169. #if !UCONFIG_ONLY_HTML_CONVERSION
  170. { "utf7", UCNV_UTF7 },
  171. #endif
  172. { "utf8", UCNV_UTF8 },
  173. #if !UCONFIG_ONLY_HTML_CONVERSION
  174. { "x11compoundtext", UCNV_COMPOUND_TEXT}
  175. #endif
  176. };
  177. /*initializes some global variables */
  178. static UHashtable *SHARED_DATA_HASHTABLE = nullptr;
  179. static icu::UMutex cnvCacheMutex;
  180. /* Note: the global mutex is used for */
  181. /* reference count updates. */
  182. static const char **gAvailableConverters = nullptr;
  183. static uint16_t gAvailableConverterCount = 0;
  184. static icu::UInitOnce gAvailableConvertersInitOnce {};
  185. #if !U_CHARSET_IS_UTF8
  186. /* This contains the resolved converter name. So no further alias lookup is needed again. */
  187. static char gDefaultConverterNameBuffer[UCNV_MAX_CONVERTER_NAME_LENGTH + 1]; /* +1 for nullptr */
  188. static const char *gDefaultConverterName = nullptr;
  189. /*
  190. If the default converter is an algorithmic converter, this is the cached value.
  191. We don't cache a full UConverter and clone it because ucnv_clone doesn't have
  192. less overhead than an algorithmic open. We don't cache non-algorithmic converters
  193. because ucnv_flushCache must be able to unload the default converter and its table.
  194. */
  195. static const UConverterSharedData *gDefaultAlgorithmicSharedData = nullptr;
  196. /* Does gDefaultConverterName have a converter option and require extra parsing? */
  197. static UBool gDefaultConverterContainsOption;
  198. #endif /* !U_CHARSET_IS_UTF8 */
  199. static const char DATA_TYPE[] = "cnv";
  200. /* ucnv_flushAvailableConverterCache. This is only called from ucnv_cleanup().
  201. * If it is ever to be called from elsewhere, synchronization
  202. * will need to be considered.
  203. */
  204. static void
  205. ucnv_flushAvailableConverterCache() {
  206. gAvailableConverterCount = 0;
  207. if (gAvailableConverters) {
  208. uprv_free(const_cast<char**>(gAvailableConverters));
  209. gAvailableConverters = nullptr;
  210. }
  211. gAvailableConvertersInitOnce.reset();
  212. }
  213. /* ucnv_cleanup - delete all storage held by the converter cache, except any */
  214. /* in use by open converters. */
  215. /* Not thread safe. */
  216. /* Not supported API. */
  217. static UBool U_CALLCONV ucnv_cleanup() {
  218. ucnv_flushCache();
  219. if (SHARED_DATA_HASHTABLE != nullptr && uhash_count(SHARED_DATA_HASHTABLE) == 0) {
  220. uhash_close(SHARED_DATA_HASHTABLE);
  221. SHARED_DATA_HASHTABLE = nullptr;
  222. }
  223. /* Isn't called from flushCache because other threads may have preexisting references to the table. */
  224. ucnv_flushAvailableConverterCache();
  225. #if !U_CHARSET_IS_UTF8
  226. gDefaultConverterName = nullptr;
  227. gDefaultConverterNameBuffer[0] = 0;
  228. gDefaultConverterContainsOption = false;
  229. gDefaultAlgorithmicSharedData = nullptr;
  230. #endif
  231. return (SHARED_DATA_HASHTABLE == nullptr);
  232. }
  233. U_CAPI void U_EXPORT2
  234. ucnv_enableCleanup() {
  235. ucln_common_registerCleanup(UCLN_COMMON_UCNV, ucnv_cleanup);
  236. }
  237. static UBool U_CALLCONV
  238. isCnvAcceptable(void * /*context*/,
  239. const char * /*type*/, const char * /*name*/,
  240. const UDataInfo *pInfo) {
  241. return
  242. pInfo->size>=20 &&
  243. pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
  244. pInfo->charsetFamily==U_CHARSET_FAMILY &&
  245. pInfo->sizeofUChar==U_SIZEOF_UCHAR &&
  246. pInfo->dataFormat[0]==0x63 && /* dataFormat="cnvt" */
  247. pInfo->dataFormat[1]==0x6e &&
  248. pInfo->dataFormat[2]==0x76 &&
  249. pInfo->dataFormat[3]==0x74 &&
  250. pInfo->formatVersion[0]==6; /* Everything will be version 6 */
  251. }
  252. /**
  253. * Un flatten shared data from a UDATA..
  254. */
  255. static UConverterSharedData*
  256. ucnv_data_unFlattenClone(UConverterLoadArgs *pArgs, UDataMemory *pData, UErrorCode *status)
  257. {
  258. /* UDataInfo info; -- necessary only if some converters have different formatVersion */
  259. const uint8_t* raw = static_cast<const uint8_t*>(udata_getMemory(pData));
  260. const UConverterStaticData* source = reinterpret_cast<const UConverterStaticData*>(raw);
  261. UConverterSharedData *data;
  262. UConverterType type = static_cast<UConverterType>(source->conversionType);
  263. if(U_FAILURE(*status))
  264. return nullptr;
  265. if (static_cast<uint16_t>(type) >= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES ||
  266. converterData[type] == nullptr ||
  267. !converterData[type]->isReferenceCounted ||
  268. converterData[type]->referenceCounter != 1 ||
  269. source->structSize != sizeof(UConverterStaticData))
  270. {
  271. *status = U_INVALID_TABLE_FORMAT;
  272. return nullptr;
  273. }
  274. data = static_cast<UConverterSharedData*>(uprv_malloc(sizeof(UConverterSharedData)));
  275. if(data == nullptr) {
  276. *status = U_MEMORY_ALLOCATION_ERROR;
  277. return nullptr;
  278. }
  279. /* copy initial values from the static structure for this type */
  280. uprv_memcpy(data, converterData[type], sizeof(UConverterSharedData));
  281. data->staticData = source;
  282. data->sharedDataCached = false;
  283. /* fill in fields from the loaded data */
  284. data->dataMemory = (void*)pData; /* for future use */
  285. if(data->impl->load != nullptr) {
  286. data->impl->load(data, pArgs, raw + source->structSize, status);
  287. if(U_FAILURE(*status)) {
  288. uprv_free(data);
  289. return nullptr;
  290. }
  291. }
  292. return data;
  293. }
  294. /*Takes an alias name gets an actual converter file name
  295. *goes to disk and opens it.
  296. *allocates the memory and returns a new UConverter object
  297. */
  298. static UConverterSharedData *createConverterFromFile(UConverterLoadArgs *pArgs, UErrorCode * err)
  299. {
  300. UDataMemory *data;
  301. UConverterSharedData *sharedData;
  302. UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD);
  303. if (U_FAILURE (*err)) {
  304. UTRACE_EXIT_STATUS(*err);
  305. return nullptr;
  306. }
  307. UTRACE_DATA2(UTRACE_OPEN_CLOSE, "load converter %s from package %s", pArgs->name, pArgs->pkg);
  308. data = udata_openChoice(pArgs->pkg, DATA_TYPE, pArgs->name, isCnvAcceptable, nullptr, err);
  309. if(U_FAILURE(*err))
  310. {
  311. UTRACE_EXIT_STATUS(*err);
  312. return nullptr;
  313. }
  314. sharedData = ucnv_data_unFlattenClone(pArgs, data, err);
  315. if(U_FAILURE(*err))
  316. {
  317. udata_close(data);
  318. UTRACE_EXIT_STATUS(*err);
  319. return nullptr;
  320. }
  321. /*
  322. * TODO Store pkg in a field in the shared data so that delta-only converters
  323. * can load base converters from the same package.
  324. * If the pkg name is longer than the field, then either do not load the converter
  325. * in the first place, or just set the pkg field to "".
  326. */
  327. UTRACE_EXIT_PTR_STATUS(sharedData, *err);
  328. return sharedData;
  329. }
  330. /*returns a converter type from a string
  331. */
  332. static const UConverterSharedData *
  333. getAlgorithmicTypeFromName(const char *realName)
  334. {
  335. uint32_t mid, start, limit;
  336. uint32_t lastMid;
  337. int result;
  338. char strippedName[UCNV_MAX_CONVERTER_NAME_LENGTH];
  339. /* Lower case and remove ignoreable characters. */
  340. ucnv_io_stripForCompare(strippedName, realName);
  341. /* do a binary search for the alias */
  342. start = 0;
  343. limit = UPRV_LENGTHOF(cnvNameType);
  344. mid = limit;
  345. lastMid = UINT32_MAX;
  346. for (;;) {
  347. mid = (start + limit) / 2;
  348. if (lastMid == mid) { /* Have we moved? */
  349. break; /* We haven't moved, and it wasn't found. */
  350. }
  351. lastMid = mid;
  352. result = uprv_strcmp(strippedName, cnvNameType[mid].name);
  353. if (result < 0) {
  354. limit = mid;
  355. } else if (result > 0) {
  356. start = mid;
  357. } else {
  358. return converterData[cnvNameType[mid].type];
  359. }
  360. }
  361. return nullptr;
  362. }
  363. /*
  364. * Based on the number of known converters, this determines how many times larger
  365. * the shared data hash table should be. When on small platforms, or just a couple
  366. * of converters are used, this number should be 2. When memory is plentiful, or
  367. * when ucnv_countAvailable is ever used with a lot of available converters,
  368. * this should be 4.
  369. * Larger numbers reduce the number of hash collisions, but use more memory.
  370. */
  371. #define UCNV_CACHE_LOAD_FACTOR 2
  372. /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
  373. /* Will always be called with the cnvCacheMutex already being held */
  374. /* by the calling function. */
  375. /* Stores the shared data in the SHARED_DATA_HASHTABLE
  376. * @param data The shared data
  377. */
  378. static void
  379. ucnv_shareConverterData(UConverterSharedData * data)
  380. {
  381. UErrorCode err = U_ZERO_ERROR;
  382. /*Lazy evaluates the Hashtable itself */
  383. /*void *sanity = nullptr;*/
  384. if (SHARED_DATA_HASHTABLE == nullptr)
  385. {
  386. SHARED_DATA_HASHTABLE = uhash_openSize(uhash_hashChars, uhash_compareChars, nullptr,
  387. ucnv_io_countKnownConverters(&err)*UCNV_CACHE_LOAD_FACTOR,
  388. &err);
  389. ucnv_enableCleanup();
  390. if (U_FAILURE(err))
  391. return;
  392. }
  393. /* ### check to see if the element is not already there! */
  394. /*
  395. sanity = ucnv_getSharedConverterData (data->staticData->name);
  396. if(sanity != nullptr)
  397. {
  398. UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
  399. }
  400. UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
  401. */
  402. /* Mark it shared */
  403. data->sharedDataCached = true;
  404. uhash_put(SHARED_DATA_HASHTABLE,
  405. (void*) data->staticData->name, /* Okay to cast away const as long as
  406. keyDeleter == nullptr */
  407. data,
  408. &err);
  409. UCNV_DEBUG_LOG("put", data->staticData->name,data);
  410. }
  411. /* Look up a converter name in the shared data cache. */
  412. /* cnvCacheMutex must be held by the caller to protect the hash table. */
  413. /* gets the shared data from the SHARED_DATA_HASHTABLE (might return nullptr if it isn't there)
  414. * @param name The name of the shared data
  415. * @return the shared data from the SHARED_DATA_HASHTABLE
  416. */
  417. static UConverterSharedData *
  418. ucnv_getSharedConverterData(const char *name)
  419. {
  420. /*special case when no Table has yet been created we return nullptr */
  421. if (SHARED_DATA_HASHTABLE == nullptr)
  422. {
  423. return nullptr;
  424. }
  425. else
  426. {
  427. UConverterSharedData *rc;
  428. rc = static_cast<UConverterSharedData*>(uhash_get(SHARED_DATA_HASHTABLE, name));
  429. UCNV_DEBUG_LOG("get",name,rc);
  430. return rc;
  431. }
  432. }
  433. /*frees the string of memory blocks associates with a sharedConverter
  434. *if and only if the referenceCounter == 0
  435. */
  436. /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
  437. * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
  438. * returns true,
  439. * otherwise returns false
  440. * @param sharedConverterData The shared data
  441. * @return if not it frees all the memory stemming from sharedConverterData and
  442. * returns true, otherwise returns false
  443. */
  444. static UBool
  445. ucnv_deleteSharedConverterData(UConverterSharedData * deadSharedData)
  446. {
  447. UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD);
  448. UTRACE_DATA2(UTRACE_OPEN_CLOSE, "unload converter %s shared data %p", deadSharedData->staticData->name, deadSharedData);
  449. if (deadSharedData->referenceCounter > 0) {
  450. UTRACE_EXIT_VALUE((int32_t)false);
  451. return false;
  452. }
  453. if (deadSharedData->impl->unload != nullptr) {
  454. deadSharedData->impl->unload(deadSharedData);
  455. }
  456. if(deadSharedData->dataMemory != nullptr)
  457. {
  458. UDataMemory *data = (UDataMemory*)deadSharedData->dataMemory;
  459. udata_close(data);
  460. }
  461. uprv_free(deadSharedData);
  462. UTRACE_EXIT_VALUE((int32_t)true);
  463. return true;
  464. }
  465. /**
  466. * Load a non-algorithmic converter.
  467. * If pkg==nullptr, then this function must be called inside umtx_lock(&cnvCacheMutex).
  468. */
  469. UConverterSharedData *
  470. ucnv_load(UConverterLoadArgs *pArgs, UErrorCode *err) {
  471. UConverterSharedData *mySharedConverterData;
  472. if(err == nullptr || U_FAILURE(*err)) {
  473. return nullptr;
  474. }
  475. if(pArgs->pkg != nullptr && *pArgs->pkg != 0) {
  476. /* application-provided converters are not currently cached */
  477. return createConverterFromFile(pArgs, err);
  478. }
  479. mySharedConverterData = ucnv_getSharedConverterData(pArgs->name);
  480. if (mySharedConverterData == nullptr)
  481. {
  482. /*Not cached, we need to stream it in from file */
  483. mySharedConverterData = createConverterFromFile(pArgs, err);
  484. if (U_FAILURE (*err) || (mySharedConverterData == nullptr))
  485. {
  486. return nullptr;
  487. }
  488. else if (!pArgs->onlyTestIsLoadable)
  489. {
  490. /* share it with other library clients */
  491. ucnv_shareConverterData(mySharedConverterData);
  492. }
  493. }
  494. else
  495. {
  496. /* The data for this converter was already in the cache. */
  497. /* Update the reference counter on the shared data: one more client */
  498. mySharedConverterData->referenceCounter++;
  499. }
  500. return mySharedConverterData;
  501. }
  502. /**
  503. * Unload a non-algorithmic converter.
  504. * It must be sharedData->isReferenceCounted
  505. * and this function must be called inside umtx_lock(&cnvCacheMutex).
  506. */
  507. U_CAPI void
  508. ucnv_unload(UConverterSharedData *sharedData) {
  509. if(sharedData != nullptr) {
  510. if (sharedData->referenceCounter > 0) {
  511. sharedData->referenceCounter--;
  512. }
  513. if((sharedData->referenceCounter <= 0)&&(sharedData->sharedDataCached == false)) {
  514. ucnv_deleteSharedConverterData(sharedData);
  515. }
  516. }
  517. }
  518. U_CFUNC void
  519. ucnv_unloadSharedDataIfReady(UConverterSharedData *sharedData)
  520. {
  521. if(sharedData != nullptr && sharedData->isReferenceCounted) {
  522. umtx_lock(&cnvCacheMutex);
  523. ucnv_unload(sharedData);
  524. umtx_unlock(&cnvCacheMutex);
  525. }
  526. }
  527. U_CFUNC void
  528. ucnv_incrementRefCount(UConverterSharedData *sharedData)
  529. {
  530. if(sharedData != nullptr && sharedData->isReferenceCounted) {
  531. umtx_lock(&cnvCacheMutex);
  532. sharedData->referenceCounter++;
  533. umtx_unlock(&cnvCacheMutex);
  534. }
  535. }
  536. /*
  537. * *pPieces must be initialized.
  538. * The name without options will be copied to pPieces->cnvName.
  539. * The locale and options will be copied to pPieces only if present in inName,
  540. * otherwise the existing values in pPieces remain.
  541. * *pArgs will be set to the pPieces values.
  542. */
  543. static void
  544. parseConverterOptions(const char *inName,
  545. UConverterNamePieces *pPieces,
  546. UConverterLoadArgs *pArgs,
  547. UErrorCode *err)
  548. {
  549. char *cnvName = pPieces->cnvName;
  550. char c;
  551. int32_t len = 0;
  552. pArgs->name=inName;
  553. pArgs->locale=pPieces->locale;
  554. pArgs->options=pPieces->options;
  555. /* copy the converter name itself to cnvName */
  556. while((c=*inName)!=0 && c!=UCNV_OPTION_SEP_CHAR) {
  557. if (++len>=UCNV_MAX_CONVERTER_NAME_LENGTH) {
  558. *err = U_ILLEGAL_ARGUMENT_ERROR; /* bad name */
  559. pPieces->cnvName[0]=0;
  560. return;
  561. }
  562. *cnvName++=c;
  563. inName++;
  564. }
  565. *cnvName=0;
  566. pArgs->name=pPieces->cnvName;
  567. /* parse options. No more name copying should occur. */
  568. while((c=*inName)!=0) {
  569. if(c==UCNV_OPTION_SEP_CHAR) {
  570. ++inName;
  571. }
  572. /* inName is behind an option separator */
  573. if(uprv_strncmp(inName, "locale=", 7)==0) {
  574. /* do not modify locale itself in case we have multiple locale options */
  575. char *dest=pPieces->locale;
  576. /* copy the locale option value */
  577. inName+=7;
  578. len=0;
  579. while((c=*inName)!=0 && c!=UCNV_OPTION_SEP_CHAR) {
  580. ++inName;
  581. if(++len>=ULOC_FULLNAME_CAPACITY) {
  582. *err=U_ILLEGAL_ARGUMENT_ERROR; /* bad name */
  583. pPieces->locale[0]=0;
  584. return;
  585. }
  586. *dest++=c;
  587. }
  588. *dest=0;
  589. } else if(uprv_strncmp(inName, "version=", 8)==0) {
  590. /* copy the version option value into bits 3..0 of pPieces->options */
  591. inName+=8;
  592. c=*inName;
  593. if(c==0) {
  594. pArgs->options=(pPieces->options&=~UCNV_OPTION_VERSION);
  595. return;
  596. } else if (static_cast<uint8_t>(c - '0') < 10) {
  597. pArgs->options = pPieces->options = (pPieces->options & ~UCNV_OPTION_VERSION) | static_cast<uint32_t>(c - '0');
  598. ++inName;
  599. }
  600. } else if(uprv_strncmp(inName, "swaplfnl", 8)==0) {
  601. inName+=8;
  602. pArgs->options=(pPieces->options|=UCNV_OPTION_SWAP_LFNL);
  603. /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
  604. } else {
  605. /* ignore any other options until we define some */
  606. while(((c = *inName++) != 0) && (c != UCNV_OPTION_SEP_CHAR)) {
  607. }
  608. if(c==0) {
  609. return;
  610. }
  611. }
  612. }
  613. }
  614. /*Logic determines if the converter is Algorithmic AND/OR cached
  615. *depending on that:
  616. * -we either go to get data from disk and cache it (Data=true, Cached=false)
  617. * -Get it from a Hashtable (Data=X, Cached=true)
  618. * -Call dataConverter initializer (Data=true, Cached=true)
  619. * -Call AlgorithmicConverter initializer (Data=false, Cached=true)
  620. */
  621. U_CFUNC UConverterSharedData *
  622. ucnv_loadSharedData(const char *converterName,
  623. UConverterNamePieces *pPieces,
  624. UConverterLoadArgs *pArgs,
  625. UErrorCode * err) {
  626. UConverterNamePieces stackPieces;
  627. UConverterLoadArgs stackArgs;
  628. UConverterSharedData *mySharedConverterData = nullptr;
  629. UErrorCode internalErrorCode = U_ZERO_ERROR;
  630. UBool mayContainOption = true;
  631. UBool checkForAlgorithmic = true;
  632. if (U_FAILURE (*err)) {
  633. return nullptr;
  634. }
  635. if(pPieces == nullptr) {
  636. if(pArgs != nullptr) {
  637. /*
  638. * Bad: We may set pArgs pointers to stackPieces fields
  639. * which will be invalid after this function returns.
  640. */
  641. *err = U_INTERNAL_PROGRAM_ERROR;
  642. return nullptr;
  643. }
  644. pPieces = &stackPieces;
  645. }
  646. if(pArgs == nullptr) {
  647. uprv_memset(&stackArgs, 0, sizeof(stackArgs));
  648. stackArgs.size = (int32_t)sizeof(stackArgs);
  649. pArgs = &stackArgs;
  650. }
  651. pPieces->cnvName[0] = 0;
  652. pPieces->locale[0] = 0;
  653. pPieces->options = 0;
  654. pArgs->name = converterName;
  655. pArgs->locale = pPieces->locale;
  656. pArgs->options = pPieces->options;
  657. /* In case "name" is nullptr we want to open the default converter. */
  658. if (converterName == nullptr) {
  659. #if U_CHARSET_IS_UTF8
  660. pArgs->name = "UTF-8";
  661. return (UConverterSharedData *)converterData[UCNV_UTF8];
  662. #else
  663. /* Call ucnv_getDefaultName first to query the name from the OS. */
  664. pArgs->name = ucnv_getDefaultName();
  665. if (pArgs->name == nullptr) {
  666. *err = U_MISSING_RESOURCE_ERROR;
  667. return nullptr;
  668. }
  669. mySharedConverterData = (UConverterSharedData *)gDefaultAlgorithmicSharedData;
  670. checkForAlgorithmic = false;
  671. mayContainOption = gDefaultConverterContainsOption;
  672. /* the default converter name is already canonical */
  673. #endif
  674. }
  675. else if(UCNV_FAST_IS_UTF8(converterName)) {
  676. /* fastpath for UTF-8 */
  677. pArgs->name = "UTF-8";
  678. return (UConverterSharedData *)converterData[UCNV_UTF8];
  679. }
  680. else {
  681. /* separate the converter name from the options */
  682. parseConverterOptions(converterName, pPieces, pArgs, err);
  683. if (U_FAILURE(*err)) {
  684. /* Very bad name used. */
  685. return nullptr;
  686. }
  687. /* get the canonical converter name */
  688. pArgs->name = ucnv_io_getConverterName(pArgs->name, &mayContainOption, &internalErrorCode);
  689. if (U_FAILURE(internalErrorCode) || pArgs->name == nullptr) {
  690. /*
  691. * set the input name in case the converter was added
  692. * without updating the alias table, or when there is no alias table
  693. */
  694. pArgs->name = pPieces->cnvName;
  695. } else if (internalErrorCode == U_AMBIGUOUS_ALIAS_WARNING) {
  696. *err = U_AMBIGUOUS_ALIAS_WARNING;
  697. }
  698. }
  699. /* separate the converter name from the options */
  700. if(mayContainOption && pArgs->name != pPieces->cnvName) {
  701. parseConverterOptions(pArgs->name, pPieces, pArgs, err);
  702. }
  703. /* get the shared data for an algorithmic converter, if it is one */
  704. if (checkForAlgorithmic) {
  705. mySharedConverterData = (UConverterSharedData *)getAlgorithmicTypeFromName(pArgs->name);
  706. }
  707. if (mySharedConverterData == nullptr)
  708. {
  709. /* it is a data-based converter, get its shared data. */
  710. /* Hold the cnvCacheMutex through the whole process of checking the */
  711. /* converter data cache, and adding new entries to the cache */
  712. /* to prevent other threads from modifying the cache during the */
  713. /* process. */
  714. pArgs->nestedLoads=1;
  715. pArgs->pkg=nullptr;
  716. umtx_lock(&cnvCacheMutex);
  717. mySharedConverterData = ucnv_load(pArgs, err);
  718. umtx_unlock(&cnvCacheMutex);
  719. if (U_FAILURE (*err) || (mySharedConverterData == nullptr))
  720. {
  721. return nullptr;
  722. }
  723. }
  724. return mySharedConverterData;
  725. }
  726. U_CAPI UConverter *
  727. ucnv_createConverter(UConverter *myUConverter, const char *converterName, UErrorCode * err)
  728. {
  729. UConverterNamePieces stackPieces;
  730. UConverterLoadArgs stackArgs=UCNV_LOAD_ARGS_INITIALIZER;
  731. UConverterSharedData *mySharedConverterData;
  732. UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN);
  733. if(U_SUCCESS(*err)) {
  734. UTRACE_DATA1(UTRACE_OPEN_CLOSE, "open converter %s", converterName);
  735. mySharedConverterData = ucnv_loadSharedData(converterName, &stackPieces, &stackArgs, err);
  736. myUConverter = ucnv_createConverterFromSharedData(
  737. myUConverter, mySharedConverterData,
  738. &stackArgs,
  739. err);
  740. if(U_SUCCESS(*err)) {
  741. UTRACE_EXIT_PTR_STATUS(myUConverter, *err);
  742. return myUConverter;
  743. }
  744. }
  745. /* exit with error */
  746. UTRACE_EXIT_STATUS(*err);
  747. return nullptr;
  748. }
  749. U_CFUNC UBool
  750. ucnv_canCreateConverter(const char *converterName, UErrorCode *err) {
  751. UConverter myUConverter;
  752. UConverterNamePieces stackPieces;
  753. UConverterLoadArgs stackArgs=UCNV_LOAD_ARGS_INITIALIZER;
  754. UConverterSharedData *mySharedConverterData;
  755. UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN);
  756. if(U_SUCCESS(*err)) {
  757. UTRACE_DATA1(UTRACE_OPEN_CLOSE, "test if can open converter %s", converterName);
  758. stackArgs.onlyTestIsLoadable=true;
  759. mySharedConverterData = ucnv_loadSharedData(converterName, &stackPieces, &stackArgs, err);
  760. ucnv_createConverterFromSharedData(
  761. &myUConverter, mySharedConverterData,
  762. &stackArgs,
  763. err);
  764. ucnv_unloadSharedDataIfReady(mySharedConverterData);
  765. }
  766. UTRACE_EXIT_STATUS(*err);
  767. return U_SUCCESS(*err);
  768. }
  769. UConverter *
  770. ucnv_createAlgorithmicConverter(UConverter *myUConverter,
  771. UConverterType type,
  772. const char *locale, uint32_t options,
  773. UErrorCode *err) {
  774. UConverter *cnv;
  775. const UConverterSharedData *sharedData;
  776. UConverterLoadArgs stackArgs=UCNV_LOAD_ARGS_INITIALIZER;
  777. UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC);
  778. UTRACE_DATA1(UTRACE_OPEN_CLOSE, "open algorithmic converter type %d", (int32_t)type);
  779. if(type<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES<=type) {
  780. *err = U_ILLEGAL_ARGUMENT_ERROR;
  781. UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR);
  782. return nullptr;
  783. }
  784. sharedData = converterData[type];
  785. if(sharedData == nullptr || sharedData->isReferenceCounted) {
  786. /* not a valid type, or not an algorithmic converter */
  787. *err = U_ILLEGAL_ARGUMENT_ERROR;
  788. UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR);
  789. return nullptr;
  790. }
  791. stackArgs.name = "";
  792. stackArgs.options = options;
  793. stackArgs.locale=locale;
  794. cnv = ucnv_createConverterFromSharedData(
  795. myUConverter, const_cast<UConverterSharedData*>(sharedData),
  796. &stackArgs, err);
  797. UTRACE_EXIT_PTR_STATUS(cnv, *err);
  798. return cnv;
  799. }
  800. U_CFUNC UConverter*
  801. ucnv_createConverterFromPackage(const char *packageName, const char *converterName, UErrorCode * err)
  802. {
  803. UConverter *myUConverter;
  804. UConverterSharedData *mySharedConverterData;
  805. UConverterNamePieces stackPieces;
  806. UConverterLoadArgs stackArgs=UCNV_LOAD_ARGS_INITIALIZER;
  807. UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE);
  808. if(U_FAILURE(*err)) {
  809. UTRACE_EXIT_STATUS(*err);
  810. return nullptr;
  811. }
  812. UTRACE_DATA2(UTRACE_OPEN_CLOSE, "open converter %s from package %s", converterName, packageName);
  813. /* first, get the options out of the converterName string */
  814. stackPieces.cnvName[0] = 0;
  815. stackPieces.locale[0] = 0;
  816. stackPieces.options = 0;
  817. parseConverterOptions(converterName, &stackPieces, &stackArgs, err);
  818. if (U_FAILURE(*err)) {
  819. /* Very bad name used. */
  820. UTRACE_EXIT_STATUS(*err);
  821. return nullptr;
  822. }
  823. stackArgs.nestedLoads=1;
  824. stackArgs.pkg=packageName;
  825. /* open the data, unflatten the shared structure */
  826. mySharedConverterData = createConverterFromFile(&stackArgs, err);
  827. if (U_FAILURE(*err)) {
  828. UTRACE_EXIT_STATUS(*err);
  829. return nullptr;
  830. }
  831. /* create the actual converter */
  832. myUConverter = ucnv_createConverterFromSharedData(nullptr, mySharedConverterData, &stackArgs, err);
  833. if (U_FAILURE(*err)) {
  834. ucnv_close(myUConverter);
  835. UTRACE_EXIT_STATUS(*err);
  836. return nullptr;
  837. }
  838. UTRACE_EXIT_PTR_STATUS(myUConverter, *err);
  839. return myUConverter;
  840. }
  841. U_CFUNC UConverter*
  842. ucnv_createConverterFromSharedData(UConverter *myUConverter,
  843. UConverterSharedData *mySharedConverterData,
  844. UConverterLoadArgs *pArgs,
  845. UErrorCode *err)
  846. {
  847. UBool isCopyLocal;
  848. if(U_FAILURE(*err)) {
  849. ucnv_unloadSharedDataIfReady(mySharedConverterData);
  850. return myUConverter;
  851. }
  852. if(myUConverter == nullptr)
  853. {
  854. myUConverter = (UConverter *) uprv_malloc (sizeof (UConverter));
  855. if(myUConverter == nullptr)
  856. {
  857. *err = U_MEMORY_ALLOCATION_ERROR;
  858. ucnv_unloadSharedDataIfReady(mySharedConverterData);
  859. return nullptr;
  860. }
  861. isCopyLocal = false;
  862. } else {
  863. isCopyLocal = true;
  864. }
  865. /* initialize the converter */
  866. uprv_memset(myUConverter, 0, sizeof(UConverter));
  867. myUConverter->isCopyLocal = isCopyLocal;
  868. /*myUConverter->isExtraLocal = false;*/ /* Set by the memset call */
  869. myUConverter->sharedData = mySharedConverterData;
  870. myUConverter->options = pArgs->options;
  871. if(!pArgs->onlyTestIsLoadable) {
  872. myUConverter->preFromUFirstCP = U_SENTINEL;
  873. myUConverter->fromCharErrorBehaviour = UCNV_TO_U_DEFAULT_CALLBACK;
  874. myUConverter->fromUCharErrorBehaviour = UCNV_FROM_U_DEFAULT_CALLBACK;
  875. myUConverter->toUnicodeStatus = mySharedConverterData->toUnicodeStatus;
  876. myUConverter->maxBytesPerUChar = mySharedConverterData->staticData->maxBytesPerChar;
  877. myUConverter->subChar1 = mySharedConverterData->staticData->subChar1;
  878. myUConverter->subCharLen = mySharedConverterData->staticData->subCharLen;
  879. myUConverter->subChars = (uint8_t *)myUConverter->subUChars;
  880. uprv_memcpy(myUConverter->subChars, mySharedConverterData->staticData->subChar, myUConverter->subCharLen);
  881. myUConverter->toUCallbackReason = UCNV_ILLEGAL; /* default reason to invoke (*fromCharErrorBehaviour) */
  882. }
  883. if(mySharedConverterData->impl->open != nullptr) {
  884. mySharedConverterData->impl->open(myUConverter, pArgs, err);
  885. if(U_FAILURE(*err) && !pArgs->onlyTestIsLoadable) {
  886. /* don't ucnv_close() if onlyTestIsLoadable because not fully initialized */
  887. ucnv_close(myUConverter);
  888. return nullptr;
  889. }
  890. }
  891. return myUConverter;
  892. }
  893. /*Frees all shared immutable objects that aren't referred to (reference count = 0)
  894. */
  895. U_CAPI int32_t U_EXPORT2
  896. ucnv_flushCache ()
  897. {
  898. UConverterSharedData *mySharedData = nullptr;
  899. int32_t pos;
  900. int32_t tableDeletedNum = 0;
  901. const UHashElement *e;
  902. /*UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;*/
  903. int32_t i, remaining;
  904. UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE);
  905. /* Close the default converter without creating a new one so that everything will be flushed. */
  906. u_flushDefaultConverter();
  907. /*if shared data hasn't even been lazy evaluated yet
  908. * return 0
  909. */
  910. if (SHARED_DATA_HASHTABLE == nullptr) {
  911. UTRACE_EXIT_VALUE((int32_t)0);
  912. return 0;
  913. }
  914. /*creates an enumeration to iterate through every element in the
  915. * table
  916. *
  917. * Synchronization: holding cnvCacheMutex will prevent any other thread from
  918. * accessing or modifying the hash table during the iteration.
  919. * The reference count of an entry may be decremented by
  920. * ucnv_close while the iteration is in process, but this is
  921. * benign. It can't be incremented (in ucnv_createConverter())
  922. * because the sequence of looking up in the cache + incrementing
  923. * is protected by cnvCacheMutex.
  924. */
  925. umtx_lock(&cnvCacheMutex);
  926. /*
  927. * double loop: A delta/extension-only converter has a pointer to its base table's
  928. * shared data; the first iteration of the outer loop may see the delta converter
  929. * before the base converter, and unloading the delta converter may get the base
  930. * converter's reference counter down to 0.
  931. */
  932. i = 0;
  933. do {
  934. remaining = 0;
  935. pos = UHASH_FIRST;
  936. while ((e = uhash_nextElement (SHARED_DATA_HASHTABLE, &pos)) != nullptr)
  937. {
  938. mySharedData = (UConverterSharedData *) e->value.pointer;
  939. /*deletes only if reference counter == 0 */
  940. if (mySharedData->referenceCounter == 0)
  941. {
  942. tableDeletedNum++;
  943. UCNV_DEBUG_LOG("del",mySharedData->staticData->name,mySharedData);
  944. uhash_removeElement(SHARED_DATA_HASHTABLE, e);
  945. mySharedData->sharedDataCached = false;
  946. ucnv_deleteSharedConverterData (mySharedData);
  947. } else {
  948. ++remaining;
  949. }
  950. }
  951. } while(++i == 1 && remaining > 0);
  952. umtx_unlock(&cnvCacheMutex);
  953. UTRACE_DATA1(UTRACE_INFO, "ucnv_flushCache() exits with %d converters remaining", remaining);
  954. UTRACE_EXIT_VALUE(tableDeletedNum);
  955. return tableDeletedNum;
  956. }
  957. /* available converters list --------------------------------------------------- */
  958. static void U_CALLCONV initAvailableConvertersList(UErrorCode &errCode) {
  959. U_ASSERT(gAvailableConverterCount == 0);
  960. U_ASSERT(gAvailableConverters == nullptr);
  961. ucnv_enableCleanup();
  962. UEnumeration *allConvEnum = ucnv_openAllNames(&errCode);
  963. int32_t allConverterCount = uenum_count(allConvEnum, &errCode);
  964. if (U_FAILURE(errCode)) {
  965. return;
  966. }
  967. /* We can't have more than "*converterTable" converters to open */
  968. gAvailableConverters = static_cast<const char**>(uprv_malloc(allConverterCount * sizeof(char*)));
  969. if (!gAvailableConverters) {
  970. errCode = U_MEMORY_ALLOCATION_ERROR;
  971. return;
  972. }
  973. /* Open the default converter to make sure that it has first dibs in the hash table. */
  974. UErrorCode localStatus = U_ZERO_ERROR;
  975. UConverter tempConverter;
  976. ucnv_close(ucnv_createConverter(&tempConverter, nullptr, &localStatus));
  977. gAvailableConverterCount = 0;
  978. for (int32_t idx = 0; idx < allConverterCount; idx++) {
  979. localStatus = U_ZERO_ERROR;
  980. const char *converterName = uenum_next(allConvEnum, nullptr, &localStatus);
  981. if (ucnv_canCreateConverter(converterName, &localStatus)) {
  982. gAvailableConverters[gAvailableConverterCount++] = converterName;
  983. }
  984. }
  985. uenum_close(allConvEnum);
  986. }
  987. static UBool haveAvailableConverterList(UErrorCode *pErrorCode) {
  988. umtx_initOnce(gAvailableConvertersInitOnce, &initAvailableConvertersList, *pErrorCode);
  989. return U_SUCCESS(*pErrorCode);
  990. }
  991. U_CFUNC uint16_t
  992. ucnv_bld_countAvailableConverters(UErrorCode *pErrorCode) {
  993. if (haveAvailableConverterList(pErrorCode)) {
  994. return gAvailableConverterCount;
  995. }
  996. return 0;
  997. }
  998. U_CFUNC const char *
  999. ucnv_bld_getAvailableConverter(uint16_t n, UErrorCode *pErrorCode) {
  1000. if (haveAvailableConverterList(pErrorCode)) {
  1001. if (n < gAvailableConverterCount) {
  1002. return gAvailableConverters[n];
  1003. }
  1004. *pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
  1005. }
  1006. return nullptr;
  1007. }
  1008. /* default converter name --------------------------------------------------- */
  1009. #if !U_CHARSET_IS_UTF8
  1010. /*
  1011. Copy the canonical converter name.
  1012. ucnv_getDefaultName must be thread safe, which can call this function.
  1013. ucnv_setDefaultName calls this function and it doesn't have to be
  1014. thread safe because there is no reliable/safe way to reset the
  1015. converter in use in all threads. If you did reset the converter, you
  1016. would not be sure that retrieving a default converter for one string
  1017. would be the same type of default converter for a successive string.
  1018. Since the name is a returned via ucnv_getDefaultName without copying,
  1019. you shouldn't be modifying or deleting the string from a separate thread.
  1020. */
  1021. static inline void
  1022. internalSetName(const char *name, UErrorCode *status) {
  1023. UConverterNamePieces stackPieces;
  1024. UConverterLoadArgs stackArgs=UCNV_LOAD_ARGS_INITIALIZER;
  1025. int32_t length=(int32_t)(uprv_strlen(name));
  1026. UBool containsOption = (UBool)(uprv_strchr(name, UCNV_OPTION_SEP_CHAR) != nullptr);
  1027. const UConverterSharedData *algorithmicSharedData;
  1028. stackArgs.name = name;
  1029. if(containsOption) {
  1030. stackPieces.cnvName[0] = 0;
  1031. stackPieces.locale[0] = 0;
  1032. stackPieces.options = 0;
  1033. parseConverterOptions(name, &stackPieces, &stackArgs, status);
  1034. if(U_FAILURE(*status)) {
  1035. return;
  1036. }
  1037. }
  1038. algorithmicSharedData = getAlgorithmicTypeFromName(stackArgs.name);
  1039. umtx_lock(&cnvCacheMutex);
  1040. gDefaultAlgorithmicSharedData = algorithmicSharedData;
  1041. gDefaultConverterContainsOption = containsOption;
  1042. uprv_memcpy(gDefaultConverterNameBuffer, name, length);
  1043. gDefaultConverterNameBuffer[length]=0;
  1044. /* gDefaultConverterName MUST be the last global var set by this function. */
  1045. /* It is the variable checked in ucnv_getDefaultName() to see if initialization is required. */
  1046. // But there is nothing here preventing that from being reordered, either by the compiler
  1047. // or hardware. I'm adding the mutex to ucnv_getDefaultName for now. UMTX_CHECK is not enough.
  1048. // -- Andy
  1049. gDefaultConverterName = gDefaultConverterNameBuffer;
  1050. ucnv_enableCleanup();
  1051. umtx_unlock(&cnvCacheMutex);
  1052. }
  1053. #endif
  1054. /*
  1055. * In order to be really thread-safe, the get function would have to take
  1056. * a buffer parameter and copy the current string inside a mutex block.
  1057. * This implementation only tries to be really thread-safe while
  1058. * setting the name.
  1059. * It assumes that setting a pointer is atomic.
  1060. */
  1061. U_CAPI const char* U_EXPORT2
  1062. ucnv_getDefaultName() {
  1063. #if U_CHARSET_IS_UTF8
  1064. return "UTF-8";
  1065. #else
  1066. /* local variable to be thread-safe */
  1067. const char *name;
  1068. /*
  1069. Concurrent calls to ucnv_getDefaultName must be thread safe,
  1070. but ucnv_setDefaultName is not thread safe.
  1071. */
  1072. {
  1073. icu::Mutex lock(&cnvCacheMutex);
  1074. name = gDefaultConverterName;
  1075. }
  1076. if(name==nullptr) {
  1077. UErrorCode errorCode = U_ZERO_ERROR;
  1078. UConverter *cnv = nullptr;
  1079. name = uprv_getDefaultCodepage();
  1080. /* if the name is there, test it out and get the canonical name with options */
  1081. if(name != nullptr) {
  1082. cnv = ucnv_open(name, &errorCode);
  1083. if(U_SUCCESS(errorCode) && cnv != nullptr) {
  1084. name = ucnv_getName(cnv, &errorCode);
  1085. }
  1086. }
  1087. if(name == nullptr || name[0] == 0
  1088. || U_FAILURE(errorCode) || cnv == nullptr
  1089. || uprv_strlen(name)>=sizeof(gDefaultConverterNameBuffer))
  1090. {
  1091. /* Panic time, let's use a fallback. */
  1092. #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
  1093. name = "US-ASCII";
  1094. /* there is no 'algorithmic' converter for EBCDIC */
  1095. #elif U_PLATFORM == U_PF_OS390
  1096. name = "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING;
  1097. #else
  1098. name = "ibm-37_P100-1995";
  1099. #endif
  1100. }
  1101. internalSetName(name, &errorCode);
  1102. /* The close may make the current name go away. */
  1103. ucnv_close(cnv);
  1104. }
  1105. return name;
  1106. #endif
  1107. }
  1108. #if U_CHARSET_IS_UTF8
  1109. U_CAPI void U_EXPORT2 ucnv_setDefaultName(const char *) {}
  1110. #else
  1111. /*
  1112. This function is not thread safe, and it can't be thread safe.
  1113. See internalSetName or the API reference for details.
  1114. */
  1115. U_CAPI void U_EXPORT2
  1116. ucnv_setDefaultName(const char *converterName) {
  1117. if(converterName==nullptr) {
  1118. /* reset to the default codepage */
  1119. gDefaultConverterName=nullptr;
  1120. } else {
  1121. UErrorCode errorCode = U_ZERO_ERROR;
  1122. UConverter *cnv = nullptr;
  1123. const char *name = nullptr;
  1124. /* if the name is there, test it out and get the canonical name with options */
  1125. cnv = ucnv_open(converterName, &errorCode);
  1126. if(U_SUCCESS(errorCode) && cnv != nullptr) {
  1127. name = ucnv_getName(cnv, &errorCode);
  1128. }
  1129. if(U_SUCCESS(errorCode) && name!=nullptr) {
  1130. internalSetName(name, &errorCode);
  1131. }
  1132. /* else this converter is bad to use. Don't change it to a bad value. */
  1133. /* The close may make the current name go away. */
  1134. ucnv_close(cnv);
  1135. /* reset the converter cache */
  1136. u_flushDefaultConverter();
  1137. }
  1138. }
  1139. #endif
  1140. /* data swapping ------------------------------------------------------------ */
  1141. /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
  1142. #if !UCONFIG_NO_LEGACY_CONVERSION
  1143. U_CAPI int32_t U_EXPORT2
  1144. ucnv_swap(const UDataSwapper *ds,
  1145. const void *inData, int32_t length, void *outData,
  1146. UErrorCode *pErrorCode) {
  1147. const UDataInfo *pInfo;
  1148. int32_t headerSize;
  1149. const uint8_t *inBytes;
  1150. uint8_t *outBytes;
  1151. uint32_t offset, count, staticDataSize;
  1152. int32_t size;
  1153. const UConverterStaticData *inStaticData;
  1154. UConverterStaticData *outStaticData;
  1155. const _MBCSHeader *inMBCSHeader;
  1156. _MBCSHeader *outMBCSHeader;
  1157. _MBCSHeader mbcsHeader;
  1158. uint32_t mbcsHeaderLength;
  1159. UBool noFromU=false;
  1160. uint8_t outputType;
  1161. int32_t maxFastUChar, mbcsIndexLength;
  1162. const int32_t *inExtIndexes;
  1163. int32_t extOffset;
  1164. /* udata_swapDataHeader checks the arguments */
  1165. headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
  1166. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  1167. return 0;
  1168. }
  1169. /* check data format and format version */
  1170. pInfo=(const UDataInfo *)((const char *)inData+4);
  1171. if(!(
  1172. pInfo->dataFormat[0]==0x63 && /* dataFormat="cnvt" */
  1173. pInfo->dataFormat[1]==0x6e &&
  1174. pInfo->dataFormat[2]==0x76 &&
  1175. pInfo->dataFormat[3]==0x74 &&
  1176. pInfo->formatVersion[0]==6 &&
  1177. pInfo->formatVersion[1]>=2
  1178. )) {
  1179. udata_printError(ds, "ucnv_swap(): data format %02x.%02x.%02x.%02x (format version %02x.%02x) is not recognized as an ICU .cnv conversion table\n",
  1180. pInfo->dataFormat[0], pInfo->dataFormat[1],
  1181. pInfo->dataFormat[2], pInfo->dataFormat[3],
  1182. pInfo->formatVersion[0], pInfo->formatVersion[1]);
  1183. *pErrorCode=U_UNSUPPORTED_ERROR;
  1184. return 0;
  1185. }
  1186. inBytes=(const uint8_t *)inData+headerSize;
  1187. outBytes=(outData == nullptr) ? nullptr : (uint8_t *)outData+headerSize;
  1188. /* read the initial UConverterStaticData structure after the UDataInfo header */
  1189. inStaticData=(const UConverterStaticData *)inBytes;
  1190. outStaticData=(UConverterStaticData *)outBytes;
  1191. if(length<0) {
  1192. staticDataSize=ds->readUInt32(inStaticData->structSize);
  1193. } else {
  1194. length-=headerSize;
  1195. if( length<(int32_t)sizeof(UConverterStaticData) ||
  1196. (uint32_t)length<(staticDataSize=ds->readUInt32(inStaticData->structSize))
  1197. ) {
  1198. udata_printError(ds, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
  1199. length);
  1200. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  1201. return 0;
  1202. }
  1203. }
  1204. if(length>=0) {
  1205. /* swap the static data */
  1206. if(inStaticData!=outStaticData) {
  1207. uprv_memcpy(outStaticData, inStaticData, staticDataSize);
  1208. }
  1209. ds->swapArray32(ds, &inStaticData->structSize, 4,
  1210. &outStaticData->structSize, pErrorCode);
  1211. ds->swapArray32(ds, &inStaticData->codepage, 4,
  1212. &outStaticData->codepage, pErrorCode);
  1213. ds->swapInvChars(ds, inStaticData->name, (int32_t)uprv_strlen(inStaticData->name),
  1214. outStaticData->name, pErrorCode);
  1215. if(U_FAILURE(*pErrorCode)) {
  1216. udata_printError(ds, "ucnv_swap(): error swapping converter name\n");
  1217. return 0;
  1218. }
  1219. }
  1220. inBytes+=staticDataSize;
  1221. if (outBytes != nullptr) outBytes+=staticDataSize;
  1222. if(length>=0) {
  1223. length-=(int32_t)staticDataSize;
  1224. }
  1225. /* check for supported conversionType values */
  1226. if(inStaticData->conversionType==UCNV_MBCS) {
  1227. /* swap MBCS data */
  1228. inMBCSHeader=(const _MBCSHeader *)inBytes;
  1229. outMBCSHeader=(_MBCSHeader *)outBytes;
  1230. if(0<=length && length<(int32_t)sizeof(_MBCSHeader)) {
  1231. udata_printError(ds, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
  1232. length);
  1233. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  1234. return 0;
  1235. }
  1236. if(inMBCSHeader->version[0]==4 && inMBCSHeader->version[1]>=1) {
  1237. mbcsHeaderLength=MBCS_HEADER_V4_LENGTH;
  1238. } else if(inMBCSHeader->version[0]==5 && inMBCSHeader->version[1]>=3 &&
  1239. ((mbcsHeader.options=ds->readUInt32(inMBCSHeader->options))&
  1240. MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK)==0
  1241. ) {
  1242. mbcsHeaderLength=mbcsHeader.options&MBCS_OPT_LENGTH_MASK;
  1243. noFromU = (mbcsHeader.options & MBCS_OPT_NO_FROM_U) != 0;
  1244. } else {
  1245. udata_printError(ds, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
  1246. inMBCSHeader->version[0], inMBCSHeader->version[1]);
  1247. *pErrorCode=U_UNSUPPORTED_ERROR;
  1248. return 0;
  1249. }
  1250. uprv_memcpy(mbcsHeader.version, inMBCSHeader->version, 4);
  1251. mbcsHeader.countStates= ds->readUInt32(inMBCSHeader->countStates);
  1252. mbcsHeader.countToUFallbacks= ds->readUInt32(inMBCSHeader->countToUFallbacks);
  1253. mbcsHeader.offsetToUCodeUnits= ds->readUInt32(inMBCSHeader->offsetToUCodeUnits);
  1254. mbcsHeader.offsetFromUTable= ds->readUInt32(inMBCSHeader->offsetFromUTable);
  1255. mbcsHeader.offsetFromUBytes= ds->readUInt32(inMBCSHeader->offsetFromUBytes);
  1256. mbcsHeader.flags= ds->readUInt32(inMBCSHeader->flags);
  1257. mbcsHeader.fromUBytesLength= ds->readUInt32(inMBCSHeader->fromUBytesLength);
  1258. /* mbcsHeader.options have been read above */
  1259. extOffset=(int32_t)(mbcsHeader.flags>>8);
  1260. outputType=(uint8_t)mbcsHeader.flags;
  1261. if(noFromU && outputType==MBCS_OUTPUT_1) {
  1262. udata_printError(ds, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
  1263. *pErrorCode=U_UNSUPPORTED_ERROR;
  1264. return 0;
  1265. }
  1266. /* make sure that the output type is known */
  1267. switch(outputType) {
  1268. case MBCS_OUTPUT_1:
  1269. case MBCS_OUTPUT_2:
  1270. case MBCS_OUTPUT_3:
  1271. case MBCS_OUTPUT_4:
  1272. case MBCS_OUTPUT_3_EUC:
  1273. case MBCS_OUTPUT_4_EUC:
  1274. case MBCS_OUTPUT_2_SISO:
  1275. case MBCS_OUTPUT_EXT_ONLY:
  1276. /* OK */
  1277. break;
  1278. default:
  1279. udata_printError(ds, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
  1280. outputType);
  1281. *pErrorCode=U_UNSUPPORTED_ERROR;
  1282. return 0;
  1283. }
  1284. /* calculate the length of the MBCS data */
  1285. /*
  1286. * utf8Friendly MBCS files (mbcsHeader.version 4.3)
  1287. * contain an additional mbcsIndex table:
  1288. * uint16_t[(maxFastUChar+1)>>6];
  1289. * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
  1290. */
  1291. maxFastUChar=0;
  1292. mbcsIndexLength=0;
  1293. if( outputType!=MBCS_OUTPUT_EXT_ONLY && outputType!=MBCS_OUTPUT_1 &&
  1294. mbcsHeader.version[1]>=3 && (maxFastUChar=mbcsHeader.version[2])!=0
  1295. ) {
  1296. maxFastUChar=(maxFastUChar<<8)|0xff;
  1297. mbcsIndexLength=((maxFastUChar+1)>>6)*2; /* number of bytes */
  1298. }
  1299. if(extOffset==0) {
  1300. size=(int32_t)(mbcsHeader.offsetFromUBytes+mbcsIndexLength);
  1301. if(!noFromU) {
  1302. size+=(int32_t)mbcsHeader.fromUBytesLength;
  1303. }
  1304. /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
  1305. inExtIndexes=nullptr;
  1306. } else {
  1307. /* there is extension data after the base data, see ucnv_ext.h */
  1308. if(length>=0 && length<(extOffset+UCNV_EXT_INDEXES_MIN_LENGTH*4)) {
  1309. udata_printError(ds, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
  1310. length);
  1311. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  1312. return 0;
  1313. }
  1314. inExtIndexes=(const int32_t *)(inBytes+extOffset);
  1315. size=extOffset+udata_readInt32(ds, inExtIndexes[UCNV_EXT_SIZE]);
  1316. }
  1317. if(length>=0) {
  1318. if(length<size) {
  1319. udata_printError(ds, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
  1320. length);
  1321. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  1322. return 0;
  1323. }
  1324. /* copy the data for inaccessible bytes */
  1325. if(inBytes!=outBytes) {
  1326. uprv_memcpy(outBytes, inBytes, size);
  1327. }
  1328. /* swap the MBCSHeader, except for the version field */
  1329. count=mbcsHeaderLength*4;
  1330. ds->swapArray32(ds, &inMBCSHeader->countStates, count-4,
  1331. &outMBCSHeader->countStates, pErrorCode);
  1332. if(outputType==MBCS_OUTPUT_EXT_ONLY) {
  1333. /*
  1334. * extension-only file,
  1335. * contains a base name instead of normal base table data
  1336. */
  1337. /* swap the base name, between the header and the extension data */
  1338. const char *inBaseName=(const char *)inBytes+count;
  1339. char *outBaseName=(char *)outBytes+count;
  1340. ds->swapInvChars(ds, inBaseName, (int32_t)uprv_strlen(inBaseName),
  1341. outBaseName, pErrorCode);
  1342. } else {
  1343. /* normal file with base table data */
  1344. /* swap the state table, 1kB per state */
  1345. offset=count;
  1346. count=mbcsHeader.countStates*1024;
  1347. ds->swapArray32(ds, inBytes+offset, (int32_t)count,
  1348. outBytes+offset, pErrorCode);
  1349. /* swap the toUFallbacks[] */
  1350. offset+=count;
  1351. count=mbcsHeader.countToUFallbacks*8;
  1352. ds->swapArray32(ds, inBytes+offset, (int32_t)count,
  1353. outBytes+offset, pErrorCode);
  1354. /* swap the unicodeCodeUnits[] */
  1355. offset=mbcsHeader.offsetToUCodeUnits;
  1356. count=mbcsHeader.offsetFromUTable-offset;
  1357. ds->swapArray16(ds, inBytes+offset, (int32_t)count,
  1358. outBytes+offset, pErrorCode);
  1359. /* offset to the stage 1 table, independent of the outputType */
  1360. offset=mbcsHeader.offsetFromUTable;
  1361. if(outputType==MBCS_OUTPUT_1) {
  1362. /* SBCS: swap the fromU tables, all 16 bits wide */
  1363. count=(mbcsHeader.offsetFromUBytes-offset)+mbcsHeader.fromUBytesLength;
  1364. ds->swapArray16(ds, inBytes+offset, (int32_t)count,
  1365. outBytes+offset, pErrorCode);
  1366. } else {
  1367. /* otherwise: swap the stage tables separately */
  1368. /* stage 1 table: uint16_t[0x440 or 0x40] */
  1369. if(inStaticData->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
  1370. count=0x440*2; /* for all of Unicode */
  1371. } else {
  1372. count=0x40*2; /* only BMP */
  1373. }
  1374. ds->swapArray16(ds, inBytes+offset, (int32_t)count,
  1375. outBytes+offset, pErrorCode);
  1376. /* stage 2 table: uint32_t[] */
  1377. offset+=count;
  1378. count=mbcsHeader.offsetFromUBytes-offset;
  1379. ds->swapArray32(ds, inBytes+offset, (int32_t)count,
  1380. outBytes+offset, pErrorCode);
  1381. /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
  1382. offset=mbcsHeader.offsetFromUBytes;
  1383. count= noFromU ? 0 : mbcsHeader.fromUBytesLength;
  1384. switch(outputType) {
  1385. case MBCS_OUTPUT_2:
  1386. case MBCS_OUTPUT_3_EUC:
  1387. case MBCS_OUTPUT_2_SISO:
  1388. ds->swapArray16(ds, inBytes+offset, (int32_t)count,
  1389. outBytes+offset, pErrorCode);
  1390. break;
  1391. case MBCS_OUTPUT_4:
  1392. ds->swapArray32(ds, inBytes+offset, (int32_t)count,
  1393. outBytes+offset, pErrorCode);
  1394. break;
  1395. default:
  1396. /* just uint8_t[], nothing to swap */
  1397. break;
  1398. }
  1399. if(mbcsIndexLength!=0) {
  1400. offset+=count;
  1401. count=mbcsIndexLength;
  1402. ds->swapArray16(ds, inBytes+offset, (int32_t)count,
  1403. outBytes+offset, pErrorCode);
  1404. }
  1405. }
  1406. }
  1407. if(extOffset!=0) {
  1408. /* swap the extension data */
  1409. inBytes+=extOffset;
  1410. outBytes+=extOffset;
  1411. /* swap toUTable[] */
  1412. offset=udata_readInt32(ds, inExtIndexes[UCNV_EXT_TO_U_INDEX]);
  1413. length=udata_readInt32(ds, inExtIndexes[UCNV_EXT_TO_U_LENGTH]);
  1414. ds->swapArray32(ds, inBytes+offset, length*4, outBytes+offset, pErrorCode);
  1415. /* swap toUUChars[] */
  1416. offset=udata_readInt32(ds, inExtIndexes[UCNV_EXT_TO_U_UCHARS_INDEX]);
  1417. length=udata_readInt32(ds, inExtIndexes[UCNV_EXT_TO_U_UCHARS_LENGTH]);
  1418. ds->swapArray16(ds, inBytes+offset, length*2, outBytes+offset, pErrorCode);
  1419. /* swap fromUTableUChars[] */
  1420. offset=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_UCHARS_INDEX]);
  1421. length=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_LENGTH]);
  1422. ds->swapArray16(ds, inBytes+offset, length*2, outBytes+offset, pErrorCode);
  1423. /* swap fromUTableValues[] */
  1424. offset=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_VALUES_INDEX]);
  1425. /* same length as for fromUTableUChars[] */
  1426. ds->swapArray32(ds, inBytes+offset, length*4, outBytes+offset, pErrorCode);
  1427. /* no need to swap fromUBytes[] */
  1428. /* swap fromUStage12[] */
  1429. offset=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_STAGE_12_INDEX]);
  1430. length=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_STAGE_12_LENGTH]);
  1431. ds->swapArray16(ds, inBytes+offset, length*2, outBytes+offset, pErrorCode);
  1432. /* swap fromUStage3[] */
  1433. offset=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_STAGE_3_INDEX]);
  1434. length=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_STAGE_3_LENGTH]);
  1435. ds->swapArray16(ds, inBytes+offset, length*2, outBytes+offset, pErrorCode);
  1436. /* swap fromUStage3b[] */
  1437. offset=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_STAGE_3B_INDEX]);
  1438. length=udata_readInt32(ds, inExtIndexes[UCNV_EXT_FROM_U_STAGE_3B_LENGTH]);
  1439. ds->swapArray32(ds, inBytes+offset, length*4, outBytes+offset, pErrorCode);
  1440. /* swap indexes[] */
  1441. length=udata_readInt32(ds, inExtIndexes[UCNV_EXT_INDEXES_LENGTH]);
  1442. ds->swapArray32(ds, inBytes, length*4, outBytes, pErrorCode);
  1443. }
  1444. }
  1445. } else {
  1446. udata_printError(ds, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
  1447. inStaticData->conversionType);
  1448. *pErrorCode=U_UNSUPPORTED_ERROR;
  1449. return 0;
  1450. }
  1451. return headerSize+(int32_t)staticDataSize+size;
  1452. }
  1453. #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */
  1454. #endif