1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************
6 * Copyright (c) 1996-2016, International Business Machines Corporation and
7 * others. All Rights Reserved.
8 ********************************************************************
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:
18 * Date Name Description
20 * 06/20/2000 helena OS/400 port changes; mostly typecast.
21 * 06/29/2000 helena Major rewrite of the callback interface.
24 #include "unicode/utypes.h"
26 #if !UCONFIG_NO_CONVERSION
28 #include "unicode/putil.h"
29 #include "unicode/udata.h"
30 #include "unicode/ucnv.h"
31 #include "unicode/uloc.h"
52 extern 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__)
55 # define UCNV_DEBUG_LOG(x,y,z)
58 static const UConverterSharedData
* const
59 converterData
[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
]={
62 #if UCONFIG_NO_LEGACY_CONVERSION
69 &_UTF8Data
, &_UTF16BEData
, &_UTF16LEData
,
70 #if UCONFIG_ONLY_HTML_CONVERSION
73 &_UTF32BEData
, &_UTF32LEData
,
77 #if UCONFIG_NO_LEGACY_CONVERSION
83 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
84 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
85 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
88 &_LMBCSData1
,&_LMBCSData2
, &_LMBCSData3
, &_LMBCSData4
, &_LMBCSData5
, &_LMBCSData6
,
89 &_LMBCSData8
,&_LMBCSData11
,&_LMBCSData16
,&_LMBCSData17
,&_LMBCSData18
,&_LMBCSData19
,
93 #if UCONFIG_ONLY_HTML_CONVERSION
100 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
107 #if UCONFIG_ONLY_HTML_CONVERSION
108 NULL
, NULL
, &_UTF16Data
, NULL
, NULL
, NULL
,
110 &_UTF7Data
, &_Bocu1Data
, &_UTF16Data
, &_UTF32Data
, &_CESU8Data
, &_IMAPData
,
113 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
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
126 const UConverterType type
;
127 } const cnvNameType
[] = {
128 #if !UCONFIG_ONLY_HTML_CONVERSION
129 { "bocu1", UCNV_BOCU1
},
130 { "cesu8", UCNV_CESU8
},
132 #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
135 #if !UCONFIG_ONLY_HTML_CONVERSION
136 { "imapmailboxname", UCNV_IMAP_MAILBOX
},
138 #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
139 { "iscii", UCNV_ISCII
},
141 #if !UCONFIG_NO_LEGACY_CONVERSION
142 { "iso2022", UCNV_ISO_2022
},
144 { "iso88591", UCNV_LATIN_1
},
145 #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
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
},
159 #if !UCONFIG_ONLY_HTML_CONVERSION
160 { "scsu", UCNV_SCSU
},
162 { "usascii", UCNV_US_ASCII
},
163 { "utf16", UCNV_UTF16
},
164 { "utf16be", UCNV_UTF16_BigEndian
},
165 { "utf16le", UCNV_UTF16_LittleEndian
},
167 { "utf16oppositeendian", UCNV_UTF16_LittleEndian
},
168 { "utf16platformendian", UCNV_UTF16_BigEndian
},
170 { "utf16oppositeendian", UCNV_UTF16_BigEndian
},
171 { "utf16platformendian", UCNV_UTF16_LittleEndian
},
173 #if !UCONFIG_ONLY_HTML_CONVERSION
174 { "utf32", UCNV_UTF32
},
175 { "utf32be", UCNV_UTF32_BigEndian
},
176 { "utf32le", UCNV_UTF32_LittleEndian
},
178 { "utf32oppositeendian", UCNV_UTF32_LittleEndian
},
179 { "utf32platformendian", UCNV_UTF32_BigEndian
},
181 { "utf32oppositeendian", UCNV_UTF32_BigEndian
},
182 { "utf32platformendian", UCNV_UTF32_LittleEndian
},
185 #if !UCONFIG_ONLY_HTML_CONVERSION
186 { "utf7", UCNV_UTF7
},
188 { "utf8", UCNV_UTF8
},
189 #if !UCONFIG_ONLY_HTML_CONVERSION
190 { "x11compoundtext", UCNV_COMPOUND_TEXT
}
195 /*initializes some global variables */
196 static UHashtable
*SHARED_DATA_HASHTABLE
= NULL
;
197 static UMutex cnvCacheMutex
= U_MUTEX_INITIALIZER
; /* Mutex for synchronizing cnv cache access. */
198 /* Note: the global mutex is used for */
199 /* reference count updates. */
201 static const char **gAvailableConverters
= NULL
;
202 static uint16_t gAvailableConverterCount
= 0;
203 static icu::UInitOnce gAvailableConvertersInitOnce
= U_INITONCE_INITIALIZER
;
205 #if !U_CHARSET_IS_UTF8
207 /* This contains the resolved converter name. So no further alias lookup is needed again. */
208 static char gDefaultConverterNameBuffer
[UCNV_MAX_CONVERTER_NAME_LENGTH
+ 1]; /* +1 for NULL */
209 static const char *gDefaultConverterName
= NULL
;
212 If the default converter is an algorithmic converter, this is the cached value.
213 We don't cache a full UConverter and clone it because ucnv_clone doesn't have
214 less overhead than an algorithmic open. We don't cache non-algorithmic converters
215 because ucnv_flushCache must be able to unload the default converter and its table.
217 static const UConverterSharedData
*gDefaultAlgorithmicSharedData
= NULL
;
219 /* Does gDefaultConverterName have a converter option and require extra parsing? */
220 static UBool gDefaultConverterContainsOption
;
222 #endif /* !U_CHARSET_IS_UTF8 */
224 static const char DATA_TYPE
[] = "cnv";
226 /* ucnv_flushAvailableConverterCache. This is only called from ucnv_cleanup().
227 * If it is ever to be called from elsewhere, synchronization
228 * will need to be considered.
231 ucnv_flushAvailableConverterCache() {
232 gAvailableConverterCount
= 0;
233 if (gAvailableConverters
) {
234 uprv_free((char **)gAvailableConverters
);
235 gAvailableConverters
= NULL
;
237 gAvailableConvertersInitOnce
.reset();
240 /* ucnv_cleanup - delete all storage held by the converter cache, except any */
241 /* in use by open converters. */
242 /* Not thread safe. */
243 /* Not supported API. */
244 static UBool U_CALLCONV
ucnv_cleanup(void) {
246 if (SHARED_DATA_HASHTABLE
!= NULL
&& uhash_count(SHARED_DATA_HASHTABLE
) == 0) {
247 uhash_close(SHARED_DATA_HASHTABLE
);
248 SHARED_DATA_HASHTABLE
= NULL
;
251 /* Isn't called from flushCache because other threads may have preexisting references to the table. */
252 ucnv_flushAvailableConverterCache();
254 #if !U_CHARSET_IS_UTF8
255 gDefaultConverterName
= NULL
;
256 gDefaultConverterNameBuffer
[0] = 0;
257 gDefaultConverterContainsOption
= FALSE
;
258 gDefaultAlgorithmicSharedData
= NULL
;
261 return (SHARED_DATA_HASHTABLE
== NULL
);
264 static UBool U_CALLCONV
265 isCnvAcceptable(void * /*context*/,
266 const char * /*type*/, const char * /*name*/,
267 const UDataInfo
*pInfo
) {
270 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
271 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
272 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
273 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
274 pInfo
->dataFormat
[1]==0x6e &&
275 pInfo
->dataFormat
[2]==0x76 &&
276 pInfo
->dataFormat
[3]==0x74 &&
277 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
281 * Un flatten shared data from a UDATA..
283 static UConverterSharedData
*
284 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
286 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
287 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
288 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
289 UConverterSharedData
*data
;
290 UConverterType type
= (UConverterType
)source
->conversionType
;
292 if(U_FAILURE(*status
))
295 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
296 converterData
[type
] == NULL
||
297 !converterData
[type
]->isReferenceCounted
||
298 converterData
[type
]->referenceCounter
!= 1 ||
299 source
->structSize
!= sizeof(UConverterStaticData
))
301 *status
= U_INVALID_TABLE_FORMAT
;
305 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
307 *status
= U_MEMORY_ALLOCATION_ERROR
;
311 /* copy initial values from the static structure for this type */
312 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
314 data
->staticData
= source
;
316 data
->sharedDataCached
= FALSE
;
318 /* fill in fields from the loaded data */
319 data
->dataMemory
= (void*)pData
; /* for future use */
321 if(data
->impl
->load
!= NULL
) {
322 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
323 if(U_FAILURE(*status
)) {
331 /*Takes an alias name gets an actual converter file name
332 *goes to disk and opens it.
333 *allocates the memory and returns a new UConverter object
335 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
338 UConverterSharedData
*sharedData
;
340 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
342 if (U_FAILURE (*err
)) {
343 UTRACE_EXIT_STATUS(*err
);
347 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
349 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
352 UTRACE_EXIT_STATUS(*err
);
356 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
360 UTRACE_EXIT_STATUS(*err
);
365 * TODO Store pkg in a field in the shared data so that delta-only converters
366 * can load base converters from the same package.
367 * If the pkg name is longer than the field, then either do not load the converter
368 * in the first place, or just set the pkg field to "".
371 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
375 /*returns a converter type from a string
377 static const UConverterSharedData
*
378 getAlgorithmicTypeFromName(const char *realName
)
380 uint32_t mid
, start
, limit
;
383 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
385 /* Lower case and remove ignoreable characters. */
386 ucnv_io_stripForCompare(strippedName
, realName
);
388 /* do a binary search for the alias */
390 limit
= UPRV_LENGTHOF(cnvNameType
);
392 lastMid
= UINT32_MAX
;
395 mid
= (uint32_t)((start
+ limit
) / 2);
396 if (lastMid
== mid
) { /* Have we moved? */
397 break; /* We haven't moved, and it wasn't found. */
400 result
= uprv_strcmp(strippedName
, cnvNameType
[mid
].name
);
404 } else if (result
> 0) {
407 return converterData
[cnvNameType
[mid
].type
];
415 * Based on the number of known converters, this determines how many times larger
416 * the shared data hash table should be. When on small platforms, or just a couple
417 * of converters are used, this number should be 2. When memory is plentiful, or
418 * when ucnv_countAvailable is ever used with a lot of available converters,
420 * Larger numbers reduce the number of hash collisions, but use more memory.
422 #define UCNV_CACHE_LOAD_FACTOR 2
424 /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
425 /* Will always be called with the cnvCacheMutex alrady being held */
426 /* by the calling function. */
427 /* Stores the shared data in the SHARED_DATA_HASHTABLE
428 * @param data The shared data
431 ucnv_shareConverterData(UConverterSharedData
* data
)
433 UErrorCode err
= U_ZERO_ERROR
;
434 /*Lazy evaluates the Hashtable itself */
435 /*void *sanity = NULL;*/
437 if (SHARED_DATA_HASHTABLE
== NULL
)
439 SHARED_DATA_HASHTABLE
= uhash_openSize(uhash_hashChars
, uhash_compareChars
, NULL
,
440 ucnv_io_countKnownConverters(&err
)*UCNV_CACHE_LOAD_FACTOR
,
442 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
448 /* ### check to see if the element is not already there! */
451 sanity = ucnv_getSharedConverterData (data->staticData->name);
454 UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
456 UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
460 data
->sharedDataCached
= TRUE
;
462 uhash_put(SHARED_DATA_HASHTABLE
,
463 (void*) data
->staticData
->name
, /* Okay to cast away const as long as
464 keyDeleter == NULL */
467 UCNV_DEBUG_LOG("put", data
->staticData
->name
,data
);
471 /* Look up a converter name in the shared data cache. */
472 /* cnvCacheMutex must be held by the caller to protect the hash table. */
473 /* gets the shared data from the SHARED_DATA_HASHTABLE (might return NULL if it isn't there)
474 * @param name The name of the shared data
475 * @return the shared data from the SHARED_DATA_HASHTABLE
477 static UConverterSharedData
*
478 ucnv_getSharedConverterData(const char *name
)
480 /*special case when no Table has yet been created we return NULL */
481 if (SHARED_DATA_HASHTABLE
== NULL
)
487 UConverterSharedData
*rc
;
489 rc
= (UConverterSharedData
*)uhash_get(SHARED_DATA_HASHTABLE
, name
);
490 UCNV_DEBUG_LOG("get",name
,rc
);
495 /*frees the string of memory blocks associates with a sharedConverter
496 *if and only if the referenceCounter == 0
498 /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
499 * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
501 * otherwise returns FALSE
502 * @param sharedConverterData The shared data
503 * @return if not it frees all the memory stemming from sharedConverterData and
504 * returns TRUE, otherwise returns FALSE
507 ucnv_deleteSharedConverterData(UConverterSharedData
* deadSharedData
)
509 UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD
);
510 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "unload converter %s shared data %p", deadSharedData
->staticData
->name
, deadSharedData
);
512 if (deadSharedData
->referenceCounter
> 0) {
513 UTRACE_EXIT_VALUE((int32_t)FALSE
);
517 if (deadSharedData
->impl
->unload
!= NULL
) {
518 deadSharedData
->impl
->unload(deadSharedData
);
521 if(deadSharedData
->dataMemory
!= NULL
)
523 UDataMemory
*data
= (UDataMemory
*)deadSharedData
->dataMemory
;
527 uprv_free(deadSharedData
);
529 UTRACE_EXIT_VALUE((int32_t)TRUE
);
534 * Load a non-algorithmic converter.
535 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
537 UConverterSharedData
*
538 ucnv_load(UConverterLoadArgs
*pArgs
, UErrorCode
*err
) {
539 UConverterSharedData
*mySharedConverterData
;
541 if(err
== NULL
|| U_FAILURE(*err
)) {
545 if(pArgs
->pkg
!= NULL
&& *pArgs
->pkg
!= 0) {
546 /* application-provided converters are not currently cached */
547 return createConverterFromFile(pArgs
, err
);
550 mySharedConverterData
= ucnv_getSharedConverterData(pArgs
->name
);
551 if (mySharedConverterData
== NULL
)
553 /*Not cached, we need to stream it in from file */
554 mySharedConverterData
= createConverterFromFile(pArgs
, err
);
555 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
559 else if (!pArgs
->onlyTestIsLoadable
)
561 /* share it with other library clients */
562 ucnv_shareConverterData(mySharedConverterData
);
567 /* The data for this converter was already in the cache. */
568 /* Update the reference counter on the shared data: one more client */
569 mySharedConverterData
->referenceCounter
++;
572 return mySharedConverterData
;
576 * Unload a non-algorithmic converter.
577 * It must be sharedData->isReferenceCounted
578 * and this function must be called inside umtx_lock(&cnvCacheMutex).
581 ucnv_unload(UConverterSharedData
*sharedData
) {
582 if(sharedData
!= NULL
) {
583 if (sharedData
->referenceCounter
> 0) {
584 sharedData
->referenceCounter
--;
587 if((sharedData
->referenceCounter
<= 0)&&(sharedData
->sharedDataCached
== FALSE
)) {
588 ucnv_deleteSharedConverterData(sharedData
);
594 ucnv_unloadSharedDataIfReady(UConverterSharedData
*sharedData
)
596 if(sharedData
!= NULL
&& sharedData
->isReferenceCounted
) {
597 umtx_lock(&cnvCacheMutex
);
598 ucnv_unload(sharedData
);
599 umtx_unlock(&cnvCacheMutex
);
604 ucnv_incrementRefCount(UConverterSharedData
*sharedData
)
606 if(sharedData
!= NULL
&& sharedData
->isReferenceCounted
) {
607 umtx_lock(&cnvCacheMutex
);
608 sharedData
->referenceCounter
++;
609 umtx_unlock(&cnvCacheMutex
);
614 * *pPieces must be initialized.
615 * The name without options will be copied to pPieces->cnvName.
616 * The locale and options will be copied to pPieces only if present in inName,
617 * otherwise the existing values in pPieces remain.
618 * *pArgs will be set to the pPieces values.
621 parseConverterOptions(const char *inName
,
622 UConverterNamePieces
*pPieces
,
623 UConverterLoadArgs
*pArgs
,
626 char *cnvName
= pPieces
->cnvName
;
631 pArgs
->locale
=pPieces
->locale
;
632 pArgs
->options
=pPieces
->options
;
634 /* copy the converter name itself to cnvName */
635 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
636 if (++len
>=UCNV_MAX_CONVERTER_NAME_LENGTH
) {
637 *err
= U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
638 pPieces
->cnvName
[0]=0;
645 pArgs
->name
=pPieces
->cnvName
;
647 /* parse options. No more name copying should occur. */
648 while((c
=*inName
)!=0) {
649 if(c
==UCNV_OPTION_SEP_CHAR
) {
653 /* inName is behind an option separator */
654 if(uprv_strncmp(inName
, "locale=", 7)==0) {
655 /* do not modify locale itself in case we have multiple locale options */
656 char *dest
=pPieces
->locale
;
658 /* copy the locale option value */
661 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
664 if(++len
>=ULOC_FULLNAME_CAPACITY
) {
665 *err
=U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
666 pPieces
->locale
[0]=0;
673 } else if(uprv_strncmp(inName
, "version=", 8)==0) {
674 /* copy the version option value into bits 3..0 of pPieces->options */
678 pArgs
->options
=(pPieces
->options
&=~UCNV_OPTION_VERSION
);
680 } else if((uint8_t)(c
-'0')<10) {
681 pArgs
->options
=pPieces
->options
=(pPieces
->options
&~UCNV_OPTION_VERSION
)|(uint32_t)(c
-'0');
684 } else if(uprv_strncmp(inName
, "swaplfnl", 8)==0) {
686 pArgs
->options
=(pPieces
->options
|=UCNV_OPTION_SWAP_LFNL
);
687 /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
689 /* ignore any other options until we define some */
690 while(((c
= *inName
++) != 0) && (c
!= UCNV_OPTION_SEP_CHAR
)) {
699 /*Logic determines if the converter is Algorithmic AND/OR cached
701 * -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
702 * -Get it from a Hashtable (Data=X, Cached=TRUE)
703 * -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
704 * -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
706 U_CFUNC UConverterSharedData
*
707 ucnv_loadSharedData(const char *converterName
,
708 UConverterNamePieces
*pPieces
,
709 UConverterLoadArgs
*pArgs
,
711 UConverterNamePieces stackPieces
;
712 UConverterLoadArgs stackArgs
;
713 UConverterSharedData
*mySharedConverterData
= NULL
;
714 UErrorCode internalErrorCode
= U_ZERO_ERROR
;
715 UBool mayContainOption
= TRUE
;
716 UBool checkForAlgorithmic
= TRUE
;
718 if (U_FAILURE (*err
)) {
722 if(pPieces
== NULL
) {
725 * Bad: We may set pArgs pointers to stackPieces fields
726 * which will be invalid after this function returns.
728 *err
= U_INTERNAL_PROGRAM_ERROR
;
731 pPieces
= &stackPieces
;
734 uprv_memset(&stackArgs
, 0, sizeof(stackArgs
));
735 stackArgs
.size
= (int32_t)sizeof(stackArgs
);
739 pPieces
->cnvName
[0] = 0;
740 pPieces
->locale
[0] = 0;
741 pPieces
->options
= 0;
743 pArgs
->name
= converterName
;
744 pArgs
->locale
= pPieces
->locale
;
745 pArgs
->options
= pPieces
->options
;
747 /* In case "name" is NULL we want to open the default converter. */
748 if (converterName
== NULL
) {
749 #if U_CHARSET_IS_UTF8
750 pArgs
->name
= "UTF-8";
751 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
753 /* Call ucnv_getDefaultName first to query the name from the OS. */
754 pArgs
->name
= ucnv_getDefaultName();
755 if (pArgs
->name
== NULL
) {
756 *err
= U_MISSING_RESOURCE_ERROR
;
759 mySharedConverterData
= (UConverterSharedData
*)gDefaultAlgorithmicSharedData
;
760 checkForAlgorithmic
= FALSE
;
761 mayContainOption
= gDefaultConverterContainsOption
;
762 /* the default converter name is already canonical */
765 else if(UCNV_FAST_IS_UTF8(converterName
)) {
766 /* fastpath for UTF-8 */
767 pArgs
->name
= "UTF-8";
768 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
771 /* separate the converter name from the options */
772 parseConverterOptions(converterName
, pPieces
, pArgs
, err
);
773 if (U_FAILURE(*err
)) {
774 /* Very bad name used. */
778 /* get the canonical converter name */
779 pArgs
->name
= ucnv_io_getConverterName(pArgs
->name
, &mayContainOption
, &internalErrorCode
);
780 if (U_FAILURE(internalErrorCode
) || pArgs
->name
== NULL
) {
782 * set the input name in case the converter was added
783 * without updating the alias table, or when there is no alias table
785 pArgs
->name
= pPieces
->cnvName
;
786 } else if (internalErrorCode
== U_AMBIGUOUS_ALIAS_WARNING
) {
787 *err
= U_AMBIGUOUS_ALIAS_WARNING
;
791 /* separate the converter name from the options */
792 if(mayContainOption
&& pArgs
->name
!= pPieces
->cnvName
) {
793 parseConverterOptions(pArgs
->name
, pPieces
, pArgs
, err
);
796 /* get the shared data for an algorithmic converter, if it is one */
797 if (checkForAlgorithmic
) {
798 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(pArgs
->name
);
800 if (mySharedConverterData
== NULL
)
802 /* it is a data-based converter, get its shared data. */
803 /* Hold the cnvCacheMutex through the whole process of checking the */
804 /* converter data cache, and adding new entries to the cache */
805 /* to prevent other threads from modifying the cache during the */
807 pArgs
->nestedLoads
=1;
810 umtx_lock(&cnvCacheMutex
);
811 mySharedConverterData
= ucnv_load(pArgs
, err
);
812 umtx_unlock(&cnvCacheMutex
);
813 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
819 return mySharedConverterData
;
823 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
825 UConverterNamePieces stackPieces
;
826 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
827 UConverterSharedData
*mySharedConverterData
;
829 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
831 if(U_SUCCESS(*err
)) {
832 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
834 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
836 myUConverter
= ucnv_createConverterFromSharedData(
837 myUConverter
, mySharedConverterData
,
841 if(U_SUCCESS(*err
)) {
842 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
847 /* exit with error */
848 UTRACE_EXIT_STATUS(*err
);
853 ucnv_canCreateConverter(const char *converterName
, UErrorCode
*err
) {
854 UConverter myUConverter
;
855 UConverterNamePieces stackPieces
;
856 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
857 UConverterSharedData
*mySharedConverterData
;
859 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
861 if(U_SUCCESS(*err
)) {
862 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "test if can open converter %s", converterName
);
864 stackArgs
.onlyTestIsLoadable
=TRUE
;
865 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
866 ucnv_createConverterFromSharedData(
867 &myUConverter
, mySharedConverterData
,
870 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
873 UTRACE_EXIT_STATUS(*err
);
874 return U_SUCCESS(*err
);
878 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
880 const char *locale
, uint32_t options
,
883 const UConverterSharedData
*sharedData
;
884 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
886 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
887 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
889 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
890 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
891 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
895 sharedData
= converterData
[type
];
896 if(sharedData
== NULL
|| sharedData
->isReferenceCounted
) {
897 /* not a valid type, or not an algorithmic converter */
898 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
899 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
904 stackArgs
.options
= options
;
905 stackArgs
.locale
=locale
;
906 cnv
= ucnv_createConverterFromSharedData(
907 myUConverter
, (UConverterSharedData
*)sharedData
,
910 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
915 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
917 UConverter
*myUConverter
;
918 UConverterSharedData
*mySharedConverterData
;
919 UConverterNamePieces stackPieces
;
920 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
922 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
924 if(U_FAILURE(*err
)) {
925 UTRACE_EXIT_STATUS(*err
);
929 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
931 /* first, get the options out of the converterName string */
932 stackPieces
.cnvName
[0] = 0;
933 stackPieces
.locale
[0] = 0;
934 stackPieces
.options
= 0;
935 parseConverterOptions(converterName
, &stackPieces
, &stackArgs
, err
);
936 if (U_FAILURE(*err
)) {
937 /* Very bad name used. */
938 UTRACE_EXIT_STATUS(*err
);
941 stackArgs
.nestedLoads
=1;
942 stackArgs
.pkg
=packageName
;
944 /* open the data, unflatten the shared structure */
945 mySharedConverterData
= createConverterFromFile(&stackArgs
, err
);
947 if (U_FAILURE(*err
)) {
948 UTRACE_EXIT_STATUS(*err
);
952 /* create the actual converter */
953 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, &stackArgs
, err
);
955 if (U_FAILURE(*err
)) {
956 ucnv_close(myUConverter
);
957 UTRACE_EXIT_STATUS(*err
);
961 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
967 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
968 UConverterSharedData
*mySharedConverterData
,
969 UConverterLoadArgs
*pArgs
,
974 if(U_FAILURE(*err
)) {
975 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
978 if(myUConverter
== NULL
)
980 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
981 if(myUConverter
== NULL
)
983 *err
= U_MEMORY_ALLOCATION_ERROR
;
984 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
992 /* initialize the converter */
993 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
994 myUConverter
->isCopyLocal
= isCopyLocal
;
995 /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
996 myUConverter
->sharedData
= mySharedConverterData
;
997 myUConverter
->options
= pArgs
->options
;
998 if(!pArgs
->onlyTestIsLoadable
) {
999 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
1000 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_DEFAULT_CALLBACK
;
1001 myUConverter
->fromUCharErrorBehaviour
= UCNV_FROM_U_DEFAULT_CALLBACK
;
1002 myUConverter
->toUnicodeStatus
= mySharedConverterData
->toUnicodeStatus
;
1003 myUConverter
->maxBytesPerUChar
= mySharedConverterData
->staticData
->maxBytesPerChar
;
1004 myUConverter
->subChar1
= mySharedConverterData
->staticData
->subChar1
;
1005 myUConverter
->subCharLen
= mySharedConverterData
->staticData
->subCharLen
;
1006 myUConverter
->subChars
= (uint8_t *)myUConverter
->subUChars
;
1007 uprv_memcpy(myUConverter
->subChars
, mySharedConverterData
->staticData
->subChar
, myUConverter
->subCharLen
);
1008 myUConverter
->toUCallbackReason
= UCNV_ILLEGAL
; /* default reason to invoke (*fromCharErrorBehaviour) */
1011 if(mySharedConverterData
->impl
->open
!= NULL
) {
1012 mySharedConverterData
->impl
->open(myUConverter
, pArgs
, err
);
1013 if(U_FAILURE(*err
) && !pArgs
->onlyTestIsLoadable
) {
1014 /* don't ucnv_close() if onlyTestIsLoadable because not fully initialized */
1015 ucnv_close(myUConverter
);
1020 return myUConverter
;
1023 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
1025 U_CAPI
int32_t U_EXPORT2
1028 UConverterSharedData
*mySharedData
= NULL
;
1030 int32_t tableDeletedNum
= 0;
1031 const UHashElement
*e
;
1032 /*UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;*/
1033 int32_t i
, remaining
;
1035 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
1037 /* Close the default converter without creating a new one so that everything will be flushed. */
1038 u_flushDefaultConverter();
1040 /*if shared data hasn't even been lazy evaluated yet
1043 if (SHARED_DATA_HASHTABLE
== NULL
) {
1044 UTRACE_EXIT_VALUE((int32_t)0);
1048 /*creates an enumeration to iterate through every element in the
1051 * Synchronization: holding cnvCacheMutex will prevent any other thread from
1052 * accessing or modifying the hash table during the iteration.
1053 * The reference count of an entry may be decremented by
1054 * ucnv_close while the iteration is in process, but this is
1055 * benign. It can't be incremented (in ucnv_createConverter())
1056 * because the sequence of looking up in the cache + incrementing
1057 * is protected by cnvCacheMutex.
1059 umtx_lock(&cnvCacheMutex
);
1061 * double loop: A delta/extension-only converter has a pointer to its base table's
1062 * shared data; the first iteration of the outer loop may see the delta converter
1063 * before the base converter, and unloading the delta converter may get the base
1064 * converter's reference counter down to 0.
1070 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
1072 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
1073 /*deletes only if reference counter == 0 */
1074 if (mySharedData
->referenceCounter
== 0)
1078 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
1080 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
1081 mySharedData
->sharedDataCached
= FALSE
;
1082 ucnv_deleteSharedConverterData (mySharedData
);
1087 } while(++i
== 1 && remaining
> 0);
1088 umtx_unlock(&cnvCacheMutex
);
1090 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
1092 UTRACE_EXIT_VALUE(tableDeletedNum
);
1093 return tableDeletedNum
;
1096 /* available converters list --------------------------------------------------- */
1098 static void U_CALLCONV
initAvailableConvertersList(UErrorCode
&errCode
) {
1099 U_ASSERT(gAvailableConverterCount
== 0);
1100 U_ASSERT(gAvailableConverters
== NULL
);
1102 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1103 UEnumeration
*allConvEnum
= ucnv_openAllNames(&errCode
);
1104 int32_t allConverterCount
= uenum_count(allConvEnum
, &errCode
);
1105 if (U_FAILURE(errCode
)) {
1109 /* We can't have more than "*converterTable" converters to open */
1110 gAvailableConverters
= (const char **) uprv_malloc(allConverterCount
* sizeof(char*));
1111 if (!gAvailableConverters
) {
1112 errCode
= U_MEMORY_ALLOCATION_ERROR
;
1116 /* Open the default converter to make sure that it has first dibs in the hash table. */
1117 UErrorCode localStatus
= U_ZERO_ERROR
;
1118 UConverter tempConverter
;
1119 ucnv_close(ucnv_createConverter(&tempConverter
, NULL
, &localStatus
));
1121 gAvailableConverterCount
= 0;
1123 for (int32_t idx
= 0; idx
< allConverterCount
; idx
++) {
1124 localStatus
= U_ZERO_ERROR
;
1125 const char *converterName
= uenum_next(allConvEnum
, NULL
, &localStatus
);
1126 if (ucnv_canCreateConverter(converterName
, &localStatus
)) {
1127 gAvailableConverters
[gAvailableConverterCount
++] = converterName
;
1131 uenum_close(allConvEnum
);
1135 static UBool
haveAvailableConverterList(UErrorCode
*pErrorCode
) {
1136 umtx_initOnce(gAvailableConvertersInitOnce
, &initAvailableConvertersList
, *pErrorCode
);
1137 return U_SUCCESS(*pErrorCode
);
1141 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
) {
1142 if (haveAvailableConverterList(pErrorCode
)) {
1143 return gAvailableConverterCount
;
1148 U_CFUNC
const char *
1149 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
) {
1150 if (haveAvailableConverterList(pErrorCode
)) {
1151 if (n
< gAvailableConverterCount
) {
1152 return gAvailableConverters
[n
];
1154 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
1159 /* default converter name --------------------------------------------------- */
1161 #if !U_CHARSET_IS_UTF8
1163 Copy the canonical converter name.
1164 ucnv_getDefaultName must be thread safe, which can call this function.
1166 ucnv_setDefaultName calls this function and it doesn't have to be
1167 thread safe because there is no reliable/safe way to reset the
1168 converter in use in all threads. If you did reset the converter, you
1169 would not be sure that retrieving a default converter for one string
1170 would be the same type of default converter for a successive string.
1171 Since the name is a returned via ucnv_getDefaultName without copying,
1172 you shouldn't be modifying or deleting the string from a separate thread.
1175 internalSetName(const char *name
, UErrorCode
*status
) {
1176 UConverterNamePieces stackPieces
;
1177 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
1178 int32_t length
=(int32_t)(uprv_strlen(name
));
1179 UBool containsOption
= (UBool
)(uprv_strchr(name
, UCNV_OPTION_SEP_CHAR
) != NULL
);
1180 const UConverterSharedData
*algorithmicSharedData
;
1182 stackArgs
.name
= name
;
1183 if(containsOption
) {
1184 stackPieces
.cnvName
[0] = 0;
1185 stackPieces
.locale
[0] = 0;
1186 stackPieces
.options
= 0;
1187 parseConverterOptions(name
, &stackPieces
, &stackArgs
, status
);
1188 if(U_FAILURE(*status
)) {
1192 algorithmicSharedData
= getAlgorithmicTypeFromName(stackArgs
.name
);
1194 umtx_lock(&cnvCacheMutex
);
1196 gDefaultAlgorithmicSharedData
= algorithmicSharedData
;
1197 gDefaultConverterContainsOption
= containsOption
;
1198 uprv_memcpy(gDefaultConverterNameBuffer
, name
, length
);
1199 gDefaultConverterNameBuffer
[length
]=0;
1201 /* gDefaultConverterName MUST be the last global var set by this function. */
1202 /* It is the variable checked in ucnv_getDefaultName() to see if initialization is required. */
1203 // But there is nothing here preventing that from being reordered, either by the compiler
1204 // or hardware. I'm adding the mutex to ucnv_getDefaultName for now. UMTX_CHECK is not enough.
1206 gDefaultConverterName
= gDefaultConverterNameBuffer
;
1208 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1210 umtx_unlock(&cnvCacheMutex
);
1215 * In order to be really thread-safe, the get function would have to take
1216 * a buffer parameter and copy the current string inside a mutex block.
1217 * This implementation only tries to be really thread-safe while
1219 * It assumes that setting a pointer is atomic.
1222 U_CAPI
const char* U_EXPORT2
1223 ucnv_getDefaultName() {
1224 #if U_CHARSET_IS_UTF8
1227 /* local variable to be thread-safe */
1231 Concurrent calls to ucnv_getDefaultName must be thread safe,
1232 but ucnv_setDefaultName is not thread safe.
1235 icu::Mutex
lock(&cnvCacheMutex
);
1236 name
= gDefaultConverterName
;
1239 UErrorCode errorCode
= U_ZERO_ERROR
;
1240 UConverter
*cnv
= NULL
;
1242 name
= uprv_getDefaultCodepage();
1244 /* if the name is there, test it out and get the canonical name with options */
1246 cnv
= ucnv_open(name
, &errorCode
);
1247 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1248 name
= ucnv_getName(cnv
, &errorCode
);
1252 if(name
== NULL
|| name
[0] == 0
1253 || U_FAILURE(errorCode
) || cnv
== NULL
1254 || uprv_strlen(name
)>=sizeof(gDefaultConverterNameBuffer
))
1256 /* Panic time, let's use a fallback. */
1257 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1259 /* there is no 'algorithmic' converter for EBCDIC */
1260 #elif U_PLATFORM == U_PF_OS390
1261 name
= "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING
;
1263 name
= "ibm-37_P100-1995";
1267 internalSetName(name
, &errorCode
);
1269 /* The close may make the current name go away. */
1277 #if U_CHARSET_IS_UTF8
1278 U_CAPI
void U_EXPORT2
ucnv_setDefaultName(const char *) {}
1281 This function is not thread safe, and it can't be thread safe.
1282 See internalSetName or the API reference for details.
1284 U_CAPI
void U_EXPORT2
1285 ucnv_setDefaultName(const char *converterName
) {
1286 if(converterName
==NULL
) {
1287 /* reset to the default codepage */
1288 gDefaultConverterName
=NULL
;
1290 UErrorCode errorCode
= U_ZERO_ERROR
;
1291 UConverter
*cnv
= NULL
;
1292 const char *name
= NULL
;
1294 /* if the name is there, test it out and get the canonical name with options */
1295 cnv
= ucnv_open(converterName
, &errorCode
);
1296 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1297 name
= ucnv_getName(cnv
, &errorCode
);
1300 if(U_SUCCESS(errorCode
) && name
!=NULL
) {
1301 internalSetName(name
, &errorCode
);
1303 /* else this converter is bad to use. Don't change it to a bad value. */
1305 /* The close may make the current name go away. */
1308 /* reset the converter cache */
1309 u_flushDefaultConverter();
1314 /* data swapping ------------------------------------------------------------ */
1316 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
1318 #if !UCONFIG_NO_LEGACY_CONVERSION
1320 U_CAPI
int32_t U_EXPORT2
1321 ucnv_swap(const UDataSwapper
*ds
,
1322 const void *inData
, int32_t length
, void *outData
,
1323 UErrorCode
*pErrorCode
) {
1324 const UDataInfo
*pInfo
;
1327 const uint8_t *inBytes
;
1330 uint32_t offset
, count
, staticDataSize
;
1333 const UConverterStaticData
*inStaticData
;
1334 UConverterStaticData
*outStaticData
;
1336 const _MBCSHeader
*inMBCSHeader
;
1337 _MBCSHeader
*outMBCSHeader
;
1338 _MBCSHeader mbcsHeader
;
1339 uint32_t mbcsHeaderLength
;
1340 UBool noFromU
=FALSE
;
1344 int32_t maxFastUChar
, mbcsIndexLength
;
1346 const int32_t *inExtIndexes
;
1349 /* udata_swapDataHeader checks the arguments */
1350 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1351 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1355 /* check data format and format version */
1356 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1358 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1359 pInfo
->dataFormat
[1]==0x6e &&
1360 pInfo
->dataFormat
[2]==0x76 &&
1361 pInfo
->dataFormat
[3]==0x74 &&
1362 pInfo
->formatVersion
[0]==6 &&
1363 pInfo
->formatVersion
[1]>=2
1365 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",
1366 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1367 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1368 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1369 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1373 inBytes
=(const uint8_t *)inData
+headerSize
;
1374 outBytes
=(uint8_t *)outData
+headerSize
;
1376 /* read the initial UConverterStaticData structure after the UDataInfo header */
1377 inStaticData
=(const UConverterStaticData
*)inBytes
;
1378 outStaticData
=(UConverterStaticData
*)outBytes
;
1381 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1384 if( length
<(int32_t)sizeof(UConverterStaticData
) ||
1385 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1387 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1389 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1395 /* swap the static data */
1396 if(inStaticData
!=outStaticData
) {
1397 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1400 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1401 &outStaticData
->structSize
, pErrorCode
);
1402 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1403 &outStaticData
->codepage
, pErrorCode
);
1405 ds
->swapInvChars(ds
, inStaticData
->name
, (int32_t)uprv_strlen(inStaticData
->name
),
1406 outStaticData
->name
, pErrorCode
);
1407 if(U_FAILURE(*pErrorCode
)) {
1408 udata_printError(ds
, "ucnv_swap(): error swapping converter name\n");
1413 inBytes
+=staticDataSize
;
1414 outBytes
+=staticDataSize
;
1416 length
-=(int32_t)staticDataSize
;
1419 /* check for supported conversionType values */
1420 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1421 /* swap MBCS data */
1422 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1423 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1425 if(0<=length
&& length
<(int32_t)sizeof(_MBCSHeader
)) {
1426 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1428 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1431 if(inMBCSHeader
->version
[0]==4 && inMBCSHeader
->version
[1]>=1) {
1432 mbcsHeaderLength
=MBCS_HEADER_V4_LENGTH
;
1433 } else if(inMBCSHeader
->version
[0]==5 && inMBCSHeader
->version
[1]>=3 &&
1434 ((mbcsHeader
.options
=ds
->readUInt32(inMBCSHeader
->options
))&
1435 MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0
1437 mbcsHeaderLength
=mbcsHeader
.options
&MBCS_OPT_LENGTH_MASK
;
1438 noFromU
=(UBool
)((mbcsHeader
.options
&MBCS_OPT_NO_FROM_U
)!=0);
1440 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1441 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1442 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1446 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1447 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1448 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1449 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1450 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1451 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1452 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1453 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1454 /* mbcsHeader.options have been read above */
1456 extOffset
=(int32_t)(mbcsHeader
.flags
>>8);
1457 outputType
=(uint8_t)mbcsHeader
.flags
;
1458 if(noFromU
&& outputType
==MBCS_OUTPUT_1
) {
1459 udata_printError(ds
, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
1460 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1464 /* make sure that the output type is known */
1465 switch(outputType
) {
1470 case MBCS_OUTPUT_3_EUC
:
1471 case MBCS_OUTPUT_4_EUC
:
1472 case MBCS_OUTPUT_2_SISO
:
1473 case MBCS_OUTPUT_EXT_ONLY
:
1477 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1479 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1483 /* calculate the length of the MBCS data */
1486 * utf8Friendly MBCS files (mbcsHeader.version 4.3)
1487 * contain an additional mbcsIndex table:
1488 * uint16_t[(maxFastUChar+1)>>6];
1489 * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
1493 if( outputType
!=MBCS_OUTPUT_EXT_ONLY
&& outputType
!=MBCS_OUTPUT_1
&&
1494 mbcsHeader
.version
[1]>=3 && (maxFastUChar
=mbcsHeader
.version
[2])!=0
1496 maxFastUChar
=(maxFastUChar
<<8)|0xff;
1497 mbcsIndexLength
=((maxFastUChar
+1)>>6)*2; /* number of bytes */
1501 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsIndexLength
);
1503 size
+=(int32_t)mbcsHeader
.fromUBytesLength
;
1506 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1509 /* there is extension data after the base data, see ucnv_ext.h */
1510 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1511 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1513 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1517 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1518 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1523 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1525 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1529 /* copy the data for inaccessible bytes */
1530 if(inBytes
!=outBytes
) {
1531 uprv_memcpy(outBytes
, inBytes
, size
);
1534 /* swap the MBCSHeader, except for the version field */
1535 count
=mbcsHeaderLength
*4;
1536 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, count
-4,
1537 &outMBCSHeader
->countStates
, pErrorCode
);
1539 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1541 * extension-only file,
1542 * contains a base name instead of normal base table data
1545 /* swap the base name, between the header and the extension data */
1546 const char *inBaseName
=(const char *)inBytes
+count
;
1547 char *outBaseName
=(char *)outBytes
+count
;
1548 ds
->swapInvChars(ds
, inBaseName
, (int32_t)uprv_strlen(inBaseName
),
1549 outBaseName
, pErrorCode
);
1551 /* normal file with base table data */
1553 /* swap the state table, 1kB per state */
1555 count
=mbcsHeader
.countStates
*1024;
1556 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1557 outBytes
+offset
, pErrorCode
);
1559 /* swap the toUFallbacks[] */
1561 count
=mbcsHeader
.countToUFallbacks
*8;
1562 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1563 outBytes
+offset
, pErrorCode
);
1565 /* swap the unicodeCodeUnits[] */
1566 offset
=mbcsHeader
.offsetToUCodeUnits
;
1567 count
=mbcsHeader
.offsetFromUTable
-offset
;
1568 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1569 outBytes
+offset
, pErrorCode
);
1571 /* offset to the stage 1 table, independent of the outputType */
1572 offset
=mbcsHeader
.offsetFromUTable
;
1574 if(outputType
==MBCS_OUTPUT_1
) {
1575 /* SBCS: swap the fromU tables, all 16 bits wide */
1576 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1577 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1578 outBytes
+offset
, pErrorCode
);
1580 /* otherwise: swap the stage tables separately */
1582 /* stage 1 table: uint16_t[0x440 or 0x40] */
1583 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1584 count
=0x440*2; /* for all of Unicode */
1586 count
=0x40*2; /* only BMP */
1588 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1589 outBytes
+offset
, pErrorCode
);
1591 /* stage 2 table: uint32_t[] */
1593 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1594 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1595 outBytes
+offset
, pErrorCode
);
1597 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1598 offset
=mbcsHeader
.offsetFromUBytes
;
1599 count
= noFromU
? 0 : mbcsHeader
.fromUBytesLength
;
1600 switch(outputType
) {
1602 case MBCS_OUTPUT_3_EUC
:
1603 case MBCS_OUTPUT_2_SISO
:
1604 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1605 outBytes
+offset
, pErrorCode
);
1608 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1609 outBytes
+offset
, pErrorCode
);
1612 /* just uint8_t[], nothing to swap */
1616 if(mbcsIndexLength
!=0) {
1618 count
=mbcsIndexLength
;
1619 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1620 outBytes
+offset
, pErrorCode
);
1626 /* swap the extension data */
1628 outBytes
+=extOffset
;
1630 /* swap toUTable[] */
1631 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1632 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1633 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1635 /* swap toUUChars[] */
1636 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1637 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1638 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1640 /* swap fromUTableUChars[] */
1641 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1642 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1643 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1645 /* swap fromUTableValues[] */
1646 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1647 /* same length as for fromUTableUChars[] */
1648 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1650 /* no need to swap fromUBytes[] */
1652 /* swap fromUStage12[] */
1653 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1654 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1655 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1657 /* swap fromUStage3[] */
1658 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1659 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1660 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1662 /* swap fromUStage3b[] */
1663 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1664 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1665 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1667 /* swap indexes[] */
1668 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1669 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1673 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1674 inStaticData
->conversionType
);
1675 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1679 return headerSize
+(int32_t)staticDataSize
+size
;
1682 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */