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