2 ********************************************************************
4 * Copyright (c) 1996-2013, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 ********************************************************************
10 * Defines functions that are used in the creation/initialization/deletion
11 * of converters and related structures.
12 * uses uconv_io.h routines to access disk information
13 * is used by ucnv.h to implement public API create/delete/flushCache routines
14 * Modification History:
16 * Date Name Description
18 * 06/20/2000 helena OS/400 port changes; mostly typecast.
19 * 06/29/2000 helena Major rewrite of the callback interface.
22 #include "unicode/utypes.h"
24 #if !UCONFIG_NO_CONVERSION
26 #include "unicode/putil.h"
27 #include "unicode/udata.h"
28 #include "unicode/ucnv.h"
29 #include "unicode/uloc.h"
50 extern void UCNV_DEBUG_LOG(char *what
, char *who
, void *p
, int l
);
51 #define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
53 # define UCNV_DEBUG_LOG(x,y,z)
56 static const UConverterSharedData
* const
57 converterData
[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
]={
60 #if UCONFIG_NO_LEGACY_CONVERSION
67 &_UTF8Data
, &_UTF16BEData
, &_UTF16LEData
, &_UTF32BEData
, &_UTF32LEData
,
70 #if UCONFIG_NO_LEGACY_CONVERSION
72 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
73 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
77 &_LMBCSData1
,&_LMBCSData2
, &_LMBCSData3
, &_LMBCSData4
, &_LMBCSData5
, &_LMBCSData6
,
78 &_LMBCSData8
,&_LMBCSData11
,&_LMBCSData16
,&_LMBCSData17
,&_LMBCSData18
,&_LMBCSData19
,
84 #if UCONFIG_NO_LEGACY_CONVERSION
91 &_UTF7Data
, &_Bocu1Data
, &_UTF16Data
, &_UTF32Data
, &_CESU8Data
, &_IMAPData
,
93 #if UCONFIG_NO_LEGACY_CONVERSION
100 /* Please keep this in binary sorted order for getAlgorithmicTypeFromName.
101 Also the name should be in lower case and all spaces, dashes and underscores
106 const UConverterType type
;
107 } const cnvNameType
[] = {
108 { "bocu1", UCNV_BOCU1
},
109 { "cesu8", UCNV_CESU8
},
110 #if !UCONFIG_NO_LEGACY_CONVERSION
113 { "imapmailboxname", UCNV_IMAP_MAILBOX
},
114 #if !UCONFIG_NO_LEGACY_CONVERSION
115 { "iscii", UCNV_ISCII
},
116 { "iso2022", UCNV_ISO_2022
},
118 { "iso88591", UCNV_LATIN_1
},
119 #if !UCONFIG_NO_LEGACY_CONVERSION
120 { "lmbcs1", UCNV_LMBCS_1
},
121 { "lmbcs11",UCNV_LMBCS_11
},
122 { "lmbcs16",UCNV_LMBCS_16
},
123 { "lmbcs17",UCNV_LMBCS_17
},
124 { "lmbcs18",UCNV_LMBCS_18
},
125 { "lmbcs19",UCNV_LMBCS_19
},
126 { "lmbcs2", UCNV_LMBCS_2
},
127 { "lmbcs3", UCNV_LMBCS_3
},
128 { "lmbcs4", UCNV_LMBCS_4
},
129 { "lmbcs5", UCNV_LMBCS_5
},
130 { "lmbcs6", UCNV_LMBCS_6
},
131 { "lmbcs8", UCNV_LMBCS_8
},
133 { "scsu", UCNV_SCSU
},
134 { "usascii", UCNV_US_ASCII
},
135 { "utf16", UCNV_UTF16
},
136 { "utf16be", UCNV_UTF16_BigEndian
},
137 { "utf16le", UCNV_UTF16_LittleEndian
},
139 { "utf16oppositeendian", UCNV_UTF16_LittleEndian
},
140 { "utf16platformendian", UCNV_UTF16_BigEndian
},
142 { "utf16oppositeendian", UCNV_UTF16_BigEndian
},
143 { "utf16platformendian", UCNV_UTF16_LittleEndian
},
145 { "utf32", UCNV_UTF32
},
146 { "utf32be", UCNV_UTF32_BigEndian
},
147 { "utf32le", UCNV_UTF32_LittleEndian
},
149 { "utf32oppositeendian", UCNV_UTF32_LittleEndian
},
150 { "utf32platformendian", UCNV_UTF32_BigEndian
},
152 { "utf32oppositeendian", UCNV_UTF32_BigEndian
},
153 { "utf32platformendian", UCNV_UTF32_LittleEndian
},
155 { "utf7", UCNV_UTF7
},
156 { "utf8", UCNV_UTF8
},
157 { "x11compoundtext", UCNV_COMPOUND_TEXT
}
161 /*initializes some global variables */
162 static UHashtable
*SHARED_DATA_HASHTABLE
= NULL
;
163 static UMutex cnvCacheMutex
= U_MUTEX_INITIALIZER
; /* Mutex for synchronizing cnv cache access. */
164 /* Note: the global mutex is used for */
165 /* reference count updates. */
167 static const char **gAvailableConverters
= NULL
;
168 static uint16_t gAvailableConverterCount
= 0;
169 static icu::UInitOnce gAvailableConvertersInitOnce
= U_INITONCE_INITIALIZER
;
171 #if !U_CHARSET_IS_UTF8
173 /* This contains the resolved converter name. So no further alias lookup is needed again. */
174 static char gDefaultConverterNameBuffer
[UCNV_MAX_CONVERTER_NAME_LENGTH
+ 1]; /* +1 for NULL */
175 static const char *gDefaultConverterName
= NULL
;
178 If the default converter is an algorithmic converter, this is the cached value.
179 We don't cache a full UConverter and clone it because ucnv_clone doesn't have
180 less overhead than an algorithmic open. We don't cache non-algorithmic converters
181 because ucnv_flushCache must be able to unload the default converter and its table.
183 static const UConverterSharedData
*gDefaultAlgorithmicSharedData
= NULL
;
185 /* Does gDefaultConverterName have a converter option and require extra parsing? */
186 static UBool gDefaultConverterContainsOption
;
188 #endif /* !U_CHARSET_IS_UTF8 */
190 static const char DATA_TYPE
[] = "cnv";
192 /* ucnv_flushAvailableConverterCache. This is only called from ucnv_cleanup().
193 * If it is ever to be called from elsewhere, synchronization
194 * will need to be considered.
197 ucnv_flushAvailableConverterCache() {
198 gAvailableConverterCount
= 0;
199 if (gAvailableConverters
) {
200 uprv_free((char **)gAvailableConverters
);
201 gAvailableConverters
= NULL
;
203 gAvailableConvertersInitOnce
.reset();
206 /* ucnv_cleanup - delete all storage held by the converter cache, except any */
207 /* in use by open converters. */
208 /* Not thread safe. */
209 /* Not supported API. */
210 static UBool U_CALLCONV
ucnv_cleanup(void) {
212 if (SHARED_DATA_HASHTABLE
!= NULL
&& uhash_count(SHARED_DATA_HASHTABLE
) == 0) {
213 uhash_close(SHARED_DATA_HASHTABLE
);
214 SHARED_DATA_HASHTABLE
= NULL
;
217 /* Isn't called from flushCache because other threads may have preexisting references to the table. */
218 ucnv_flushAvailableConverterCache();
220 #if !U_CHARSET_IS_UTF8
221 gDefaultConverterName
= NULL
;
222 gDefaultConverterNameBuffer
[0] = 0;
223 gDefaultConverterContainsOption
= FALSE
;
224 gDefaultAlgorithmicSharedData
= NULL
;
227 return (SHARED_DATA_HASHTABLE
== NULL
);
230 static UBool U_CALLCONV
231 isCnvAcceptable(void * /*context*/,
232 const char * /*type*/, const char * /*name*/,
233 const UDataInfo
*pInfo
) {
236 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
237 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
238 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
239 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
240 pInfo
->dataFormat
[1]==0x6e &&
241 pInfo
->dataFormat
[2]==0x76 &&
242 pInfo
->dataFormat
[3]==0x74 &&
243 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
247 * Un flatten shared data from a UDATA..
249 static UConverterSharedData
*
250 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
252 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
253 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
254 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
255 UConverterSharedData
*data
;
256 UConverterType type
= (UConverterType
)source
->conversionType
;
258 if(U_FAILURE(*status
))
261 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
262 converterData
[type
] == NULL
||
263 converterData
[type
]->referenceCounter
!= 1 ||
264 source
->structSize
!= sizeof(UConverterStaticData
))
266 *status
= U_INVALID_TABLE_FORMAT
;
270 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
272 *status
= U_MEMORY_ALLOCATION_ERROR
;
276 /* copy initial values from the static structure for this type */
277 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
279 #if 0 /* made UConverterMBCSTable part of UConverterSharedData -- markus 20031107 */
281 * It would be much more efficient if the table were a direct member, not a pointer.
282 * However, that would add to the size of all UConverterSharedData objects
283 * even if they do not use this table (especially algorithmic ones).
284 * If this changes, then the static templates from converterData[type]
287 * In principle, it would be cleaner if the load() function below
288 * allocated the table.
290 data
->table
= (UConverterTable
*)uprv_malloc(sizeof(UConverterTable
));
291 if(data
->table
== NULL
) {
293 *status
= U_MEMORY_ALLOCATION_ERROR
;
296 uprv_memset(data
->table
, 0, sizeof(UConverterTable
));
299 data
->staticData
= source
;
301 data
->sharedDataCached
= FALSE
;
303 /* fill in fields from the loaded data */
304 data
->dataMemory
= (void*)pData
; /* for future use */
306 if(data
->impl
->load
!= NULL
) {
307 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
308 if(U_FAILURE(*status
)) {
309 uprv_free(data
->table
);
317 /*Takes an alias name gets an actual converter file name
318 *goes to disk and opens it.
319 *allocates the memory and returns a new UConverter object
321 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
324 UConverterSharedData
*sharedData
;
326 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
328 if (U_FAILURE (*err
)) {
329 UTRACE_EXIT_STATUS(*err
);
333 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
335 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
338 UTRACE_EXIT_STATUS(*err
);
342 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
346 UTRACE_EXIT_STATUS(*err
);
351 * TODO Store pkg in a field in the shared data so that delta-only converters
352 * can load base converters from the same package.
353 * If the pkg name is longer than the field, then either do not load the converter
354 * in the first place, or just set the pkg field to "".
357 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
361 /*returns a converter type from a string
363 static const UConverterSharedData
*
364 getAlgorithmicTypeFromName(const char *realName
)
366 uint32_t mid
, start
, limit
;
369 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
371 /* Lower case and remove ignoreable characters. */
372 ucnv_io_stripForCompare(strippedName
, realName
);
374 /* do a binary search for the alias */
376 limit
= sizeof(cnvNameType
)/sizeof(cnvNameType
[0]);
378 lastMid
= UINT32_MAX
;
381 mid
= (uint32_t)((start
+ limit
) / 2);
382 if (lastMid
== mid
) { /* Have we moved? */
383 break; /* We haven't moved, and it wasn't found. */
386 result
= uprv_strcmp(strippedName
, cnvNameType
[mid
].name
);
390 } else if (result
> 0) {
393 return converterData
[cnvNameType
[mid
].type
];
401 * Based on the number of known converters, this determines how many times larger
402 * the shared data hash table should be. When on small platforms, or just a couple
403 * of converters are used, this number should be 2. When memory is plentiful, or
404 * when ucnv_countAvailable is ever used with a lot of available converters,
406 * Larger numbers reduce the number of hash collisions, but use more memory.
408 #define UCNV_CACHE_LOAD_FACTOR 2
410 /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
411 /* Will always be called with the cnvCacheMutex alrady being held */
412 /* by the calling function. */
413 /* Stores the shared data in the SHARED_DATA_HASHTABLE
414 * @param data The shared data
417 ucnv_shareConverterData(UConverterSharedData
* data
)
419 UErrorCode err
= U_ZERO_ERROR
;
420 /*Lazy evaluates the Hashtable itself */
421 /*void *sanity = NULL;*/
423 if (SHARED_DATA_HASHTABLE
== NULL
)
425 SHARED_DATA_HASHTABLE
= uhash_openSize(uhash_hashChars
, uhash_compareChars
, NULL
,
426 ucnv_io_countKnownConverters(&err
)*UCNV_CACHE_LOAD_FACTOR
,
428 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
434 /* ### check to see if the element is not already there! */
437 sanity = ucnv_getSharedConverterData (data->staticData->name);
440 UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
442 UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
446 data
->sharedDataCached
= TRUE
;
448 uhash_put(SHARED_DATA_HASHTABLE
,
449 (void*) data
->staticData
->name
, /* Okay to cast away const as long as
450 keyDeleter == NULL */
453 UCNV_DEBUG_LOG("put", data
->staticData
->name
,data
);
457 /* Look up a converter name in the shared data cache. */
458 /* cnvCacheMutex must be held by the caller to protect the hash table. */
459 /* gets the shared data from the SHARED_DATA_HASHTABLE (might return NULL if it isn't there)
460 * @param name The name of the shared data
461 * @return the shared data from the SHARED_DATA_HASHTABLE
463 static UConverterSharedData
*
464 ucnv_getSharedConverterData(const char *name
)
466 /*special case when no Table has yet been created we return NULL */
467 if (SHARED_DATA_HASHTABLE
== NULL
)
473 UConverterSharedData
*rc
;
475 rc
= (UConverterSharedData
*)uhash_get(SHARED_DATA_HASHTABLE
, name
);
476 UCNV_DEBUG_LOG("get",name
,rc
);
481 /*frees the string of memory blocks associates with a sharedConverter
482 *if and only if the referenceCounter == 0
484 /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
485 * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
487 * otherwise returns FALSE
488 * @param sharedConverterData The shared data
489 * @return if not it frees all the memory stemming from sharedConverterData and
490 * returns TRUE, otherwise returns FALSE
493 ucnv_deleteSharedConverterData(UConverterSharedData
* deadSharedData
)
495 UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD
);
496 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "unload converter %s shared data %p", deadSharedData
->staticData
->name
, deadSharedData
);
498 if (deadSharedData
->referenceCounter
> 0) {
499 UTRACE_EXIT_VALUE((int32_t)FALSE
);
503 if (deadSharedData
->impl
->unload
!= NULL
) {
504 deadSharedData
->impl
->unload(deadSharedData
);
507 if(deadSharedData
->dataMemory
!= NULL
)
509 UDataMemory
*data
= (UDataMemory
*)deadSharedData
->dataMemory
;
513 if(deadSharedData
->table
!= NULL
)
515 uprv_free(deadSharedData
->table
);
519 /* if the static data is actually owned by the shared data */
520 /* enable if we ever have this situation. */
521 if(deadSharedData
->staticDataOwned
== TRUE
) /* see ucnv_bld.h */
523 uprv_free((void*)deadSharedData
->staticData
);
529 uprv_memset(deadSharedData
->0, sizeof(*deadSharedData
));
532 uprv_free(deadSharedData
);
534 UTRACE_EXIT_VALUE((int32_t)TRUE
);
539 * Load a non-algorithmic converter.
540 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
542 UConverterSharedData
*
543 ucnv_load(UConverterLoadArgs
*pArgs
, UErrorCode
*err
) {
544 UConverterSharedData
*mySharedConverterData
;
546 if(err
== NULL
|| U_FAILURE(*err
)) {
550 if(pArgs
->pkg
!= NULL
&& *pArgs
->pkg
!= 0) {
551 /* application-provided converters are not currently cached */
552 return createConverterFromFile(pArgs
, err
);
555 mySharedConverterData
= ucnv_getSharedConverterData(pArgs
->name
);
556 if (mySharedConverterData
== NULL
)
558 /*Not cached, we need to stream it in from file */
559 mySharedConverterData
= createConverterFromFile(pArgs
, err
);
560 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
564 else if (!pArgs
->onlyTestIsLoadable
)
566 /* share it with other library clients */
567 ucnv_shareConverterData(mySharedConverterData
);
572 /* The data for this converter was already in the cache. */
573 /* Update the reference counter on the shared data: one more client */
574 mySharedConverterData
->referenceCounter
++;
577 return mySharedConverterData
;
581 * Unload a non-algorithmic converter.
582 * It must be sharedData->referenceCounter != ~0
583 * and this function must be called inside umtx_lock(&cnvCacheMutex).
586 ucnv_unload(UConverterSharedData
*sharedData
) {
587 if(sharedData
!= NULL
) {
588 if (sharedData
->referenceCounter
> 0) {
589 sharedData
->referenceCounter
--;
592 if((sharedData
->referenceCounter
<= 0)&&(sharedData
->sharedDataCached
== FALSE
)) {
593 ucnv_deleteSharedConverterData(sharedData
);
599 ucnv_unloadSharedDataIfReady(UConverterSharedData
*sharedData
)
602 Checking whether it's an algorithic converter is okay
603 in multithreaded applications because the value never changes.
604 Don't check referenceCounter for any other value.
606 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= (uint32_t)~0) {
607 umtx_lock(&cnvCacheMutex
);
608 ucnv_unload(sharedData
);
609 umtx_unlock(&cnvCacheMutex
);
614 ucnv_incrementRefCount(UConverterSharedData
*sharedData
)
617 Checking whether it's an algorithic converter is okay
618 in multithreaded applications because the value never changes.
619 Don't check referenceCounter for any other value.
621 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= (uint32_t)~0) {
622 umtx_lock(&cnvCacheMutex
);
623 sharedData
->referenceCounter
++;
624 umtx_unlock(&cnvCacheMutex
);
629 * *pPieces must be initialized.
630 * The name without options will be copied to pPieces->cnvName.
631 * The locale and options will be copied to pPieces only if present in inName,
632 * otherwise the existing values in pPieces remain.
633 * *pArgs will be set to the pPieces values.
636 parseConverterOptions(const char *inName
,
637 UConverterNamePieces
*pPieces
,
638 UConverterLoadArgs
*pArgs
,
641 char *cnvName
= pPieces
->cnvName
;
646 pArgs
->locale
=pPieces
->locale
;
647 pArgs
->options
=pPieces
->options
;
649 /* copy the converter name itself to cnvName */
650 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
651 if (++len
>=UCNV_MAX_CONVERTER_NAME_LENGTH
) {
652 *err
= U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
653 pPieces
->cnvName
[0]=0;
660 pArgs
->name
=pPieces
->cnvName
;
662 /* parse options. No more name copying should occur. */
663 while((c
=*inName
)!=0) {
664 if(c
==UCNV_OPTION_SEP_CHAR
) {
668 /* inName is behind an option separator */
669 if(uprv_strncmp(inName
, "locale=", 7)==0) {
670 /* do not modify locale itself in case we have multiple locale options */
671 char *dest
=pPieces
->locale
;
673 /* copy the locale option value */
676 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
679 if(++len
>=ULOC_FULLNAME_CAPACITY
) {
680 *err
=U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
681 pPieces
->locale
[0]=0;
688 } else if(uprv_strncmp(inName
, "version=", 8)==0) {
689 /* copy the version option value into bits 3..0 of pPieces->options */
693 pArgs
->options
=(pPieces
->options
&=~UCNV_OPTION_VERSION
);
695 } else if((uint8_t)(c
-'0')<10) {
696 pArgs
->options
=pPieces
->options
=(pPieces
->options
&~UCNV_OPTION_VERSION
)|(uint32_t)(c
-'0');
699 } else if(uprv_strncmp(inName
, "swaplfnl", 8)==0) {
701 pArgs
->options
=(pPieces
->options
|=UCNV_OPTION_SWAP_LFNL
);
702 /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
704 /* ignore any other options until we define some */
705 while(((c
= *inName
++) != 0) && (c
!= UCNV_OPTION_SEP_CHAR
)) {
714 /*Logic determines if the converter is Algorithmic AND/OR cached
716 * -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
717 * -Get it from a Hashtable (Data=X, Cached=TRUE)
718 * -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
719 * -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
721 U_CFUNC UConverterSharedData
*
722 ucnv_loadSharedData(const char *converterName
,
723 UConverterNamePieces
*pPieces
,
724 UConverterLoadArgs
*pArgs
,
726 UConverterNamePieces stackPieces
;
727 UConverterLoadArgs stackArgs
;
728 UConverterSharedData
*mySharedConverterData
= NULL
;
729 UErrorCode internalErrorCode
= U_ZERO_ERROR
;
730 UBool mayContainOption
= TRUE
;
731 UBool checkForAlgorithmic
= TRUE
;
733 if (U_FAILURE (*err
)) {
737 if(pPieces
== NULL
) {
740 * Bad: We may set pArgs pointers to stackPieces fields
741 * which will be invalid after this function returns.
743 *err
= U_INTERNAL_PROGRAM_ERROR
;
746 pPieces
= &stackPieces
;
749 uprv_memset(&stackArgs
, 0, sizeof(stackArgs
));
750 stackArgs
.size
= (int32_t)sizeof(stackArgs
);
754 pPieces
->cnvName
[0] = 0;
755 pPieces
->locale
[0] = 0;
756 pPieces
->options
= 0;
758 pArgs
->name
= converterName
;
759 pArgs
->locale
= pPieces
->locale
;
760 pArgs
->options
= pPieces
->options
;
762 /* In case "name" is NULL we want to open the default converter. */
763 if (converterName
== NULL
) {
764 #if U_CHARSET_IS_UTF8
765 pArgs
->name
= "UTF-8";
766 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
768 /* Call ucnv_getDefaultName first to query the name from the OS. */
769 pArgs
->name
= ucnv_getDefaultName();
770 if (pArgs
->name
== NULL
) {
771 *err
= U_MISSING_RESOURCE_ERROR
;
774 mySharedConverterData
= (UConverterSharedData
*)gDefaultAlgorithmicSharedData
;
775 checkForAlgorithmic
= FALSE
;
776 mayContainOption
= gDefaultConverterContainsOption
;
777 /* the default converter name is already canonical */
780 else if(UCNV_FAST_IS_UTF8(converterName
)) {
781 /* fastpath for UTF-8 */
782 pArgs
->name
= "UTF-8";
783 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
786 /* separate the converter name from the options */
787 parseConverterOptions(converterName
, pPieces
, pArgs
, err
);
788 if (U_FAILURE(*err
)) {
789 /* Very bad name used. */
793 /* get the canonical converter name */
794 pArgs
->name
= ucnv_io_getConverterName(pArgs
->name
, &mayContainOption
, &internalErrorCode
);
795 if (U_FAILURE(internalErrorCode
) || pArgs
->name
== NULL
) {
797 * set the input name in case the converter was added
798 * without updating the alias table, or when there is no alias table
800 pArgs
->name
= pPieces
->cnvName
;
801 } else if (internalErrorCode
== U_AMBIGUOUS_ALIAS_WARNING
) {
802 *err
= U_AMBIGUOUS_ALIAS_WARNING
;
806 /* separate the converter name from the options */
807 if(mayContainOption
&& pArgs
->name
!= pPieces
->cnvName
) {
808 parseConverterOptions(pArgs
->name
, pPieces
, pArgs
, err
);
811 /* get the shared data for an algorithmic converter, if it is one */
812 if (checkForAlgorithmic
) {
813 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(pArgs
->name
);
815 if (mySharedConverterData
== NULL
)
817 /* it is a data-based converter, get its shared data. */
818 /* Hold the cnvCacheMutex through the whole process of checking the */
819 /* converter data cache, and adding new entries to the cache */
820 /* to prevent other threads from modifying the cache during the */
822 pArgs
->nestedLoads
=1;
825 umtx_lock(&cnvCacheMutex
);
826 mySharedConverterData
= ucnv_load(pArgs
, err
);
827 umtx_unlock(&cnvCacheMutex
);
828 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
834 return mySharedConverterData
;
838 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
840 UConverterNamePieces stackPieces
;
841 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
842 UConverterSharedData
*mySharedConverterData
;
844 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
846 if(U_SUCCESS(*err
)) {
847 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
849 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
851 myUConverter
= ucnv_createConverterFromSharedData(
852 myUConverter
, mySharedConverterData
,
856 if(U_SUCCESS(*err
)) {
857 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
862 /* exit with error */
863 UTRACE_EXIT_STATUS(*err
);
868 ucnv_canCreateConverter(const char *converterName
, UErrorCode
*err
) {
869 UConverter myUConverter
;
870 UConverterNamePieces stackPieces
;
871 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
872 UConverterSharedData
*mySharedConverterData
;
874 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
876 if(U_SUCCESS(*err
)) {
877 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "test if can open converter %s", converterName
);
879 stackArgs
.onlyTestIsLoadable
=TRUE
;
880 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
881 ucnv_createConverterFromSharedData(
882 &myUConverter
, mySharedConverterData
,
885 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
888 UTRACE_EXIT_STATUS(*err
);
889 return U_SUCCESS(*err
);
893 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
895 const char *locale
, uint32_t options
,
898 const UConverterSharedData
*sharedData
;
899 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
901 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
902 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
904 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
905 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
906 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
910 sharedData
= converterData
[type
];
912 Checking whether it's an algorithic converter is okay
913 in multithreaded applications because the value never changes.
914 Don't check referenceCounter for any other value.
916 if(sharedData
== NULL
|| sharedData
->referenceCounter
!= (uint32_t)~0) {
917 /* not a valid type, or not an algorithmic converter */
918 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
919 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
924 stackArgs
.options
= options
;
925 stackArgs
.locale
=locale
;
926 cnv
= ucnv_createConverterFromSharedData(
927 myUConverter
, (UConverterSharedData
*)sharedData
,
930 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
935 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
937 UConverter
*myUConverter
;
938 UConverterSharedData
*mySharedConverterData
;
939 UConverterNamePieces stackPieces
;
940 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
942 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
944 if(U_FAILURE(*err
)) {
945 UTRACE_EXIT_STATUS(*err
);
949 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
951 /* first, get the options out of the converterName string */
952 stackPieces
.cnvName
[0] = 0;
953 stackPieces
.locale
[0] = 0;
954 stackPieces
.options
= 0;
955 parseConverterOptions(converterName
, &stackPieces
, &stackArgs
, err
);
956 if (U_FAILURE(*err
)) {
957 /* Very bad name used. */
958 UTRACE_EXIT_STATUS(*err
);
961 stackArgs
.nestedLoads
=1;
962 stackArgs
.pkg
=packageName
;
964 /* open the data, unflatten the shared structure */
965 mySharedConverterData
= createConverterFromFile(&stackArgs
, err
);
967 if (U_FAILURE(*err
)) {
968 UTRACE_EXIT_STATUS(*err
);
972 /* create the actual converter */
973 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, &stackArgs
, err
);
975 if (U_FAILURE(*err
)) {
976 ucnv_close(myUConverter
);
977 UTRACE_EXIT_STATUS(*err
);
981 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
987 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
988 UConverterSharedData
*mySharedConverterData
,
989 UConverterLoadArgs
*pArgs
,
994 if(U_FAILURE(*err
)) {
995 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
998 if(myUConverter
== NULL
)
1000 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
1001 if(myUConverter
== NULL
)
1003 *err
= U_MEMORY_ALLOCATION_ERROR
;
1004 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
1007 isCopyLocal
= FALSE
;
1012 /* initialize the converter */
1013 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
1014 myUConverter
->isCopyLocal
= isCopyLocal
;
1015 /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
1016 myUConverter
->sharedData
= mySharedConverterData
;
1017 myUConverter
->options
= pArgs
->options
;
1018 if(!pArgs
->onlyTestIsLoadable
) {
1019 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
1020 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_DEFAULT_CALLBACK
;
1021 myUConverter
->fromUCharErrorBehaviour
= UCNV_FROM_U_DEFAULT_CALLBACK
;
1022 myUConverter
->toUnicodeStatus
= mySharedConverterData
->toUnicodeStatus
;
1023 myUConverter
->maxBytesPerUChar
= mySharedConverterData
->staticData
->maxBytesPerChar
;
1024 myUConverter
->subChar1
= mySharedConverterData
->staticData
->subChar1
;
1025 myUConverter
->subCharLen
= mySharedConverterData
->staticData
->subCharLen
;
1026 myUConverter
->subChars
= (uint8_t *)myUConverter
->subUChars
;
1027 uprv_memcpy(myUConverter
->subChars
, mySharedConverterData
->staticData
->subChar
, myUConverter
->subCharLen
);
1028 myUConverter
->toUCallbackReason
= UCNV_ILLEGAL
; /* default reason to invoke (*fromCharErrorBehaviour) */
1031 if(mySharedConverterData
->impl
->open
!= NULL
) {
1032 mySharedConverterData
->impl
->open(myUConverter
, pArgs
, err
);
1033 if(U_FAILURE(*err
) && !pArgs
->onlyTestIsLoadable
) {
1034 /* don't ucnv_close() if onlyTestIsLoadable because not fully initialized */
1035 ucnv_close(myUConverter
);
1040 return myUConverter
;
1043 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
1045 U_CAPI
int32_t U_EXPORT2
1048 UConverterSharedData
*mySharedData
= NULL
;
1050 int32_t tableDeletedNum
= 0;
1051 const UHashElement
*e
;
1052 /*UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;*/
1053 int32_t i
, remaining
;
1055 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
1057 /* Close the default converter without creating a new one so that everything will be flushed. */
1058 u_flushDefaultConverter();
1060 /*if shared data hasn't even been lazy evaluated yet
1063 if (SHARED_DATA_HASHTABLE
== NULL
) {
1064 UTRACE_EXIT_VALUE((int32_t)0);
1068 /*creates an enumeration to iterate through every element in the
1071 * Synchronization: holding cnvCacheMutex will prevent any other thread from
1072 * accessing or modifying the hash table during the iteration.
1073 * The reference count of an entry may be decremented by
1074 * ucnv_close while the iteration is in process, but this is
1075 * benign. It can't be incremented (in ucnv_createConverter())
1076 * because the sequence of looking up in the cache + incrementing
1077 * is protected by cnvCacheMutex.
1079 umtx_lock(&cnvCacheMutex
);
1081 * double loop: A delta/extension-only converter has a pointer to its base table's
1082 * shared data; the first iteration of the outer loop may see the delta converter
1083 * before the base converter, and unloading the delta converter may get the base
1084 * converter's reference counter down to 0.
1090 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
1092 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
1093 /*deletes only if reference counter == 0 */
1094 if (mySharedData
->referenceCounter
== 0)
1098 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
1100 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
1101 mySharedData
->sharedDataCached
= FALSE
;
1102 ucnv_deleteSharedConverterData (mySharedData
);
1107 } while(++i
== 1 && remaining
> 0);
1108 umtx_unlock(&cnvCacheMutex
);
1110 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
1112 UTRACE_EXIT_VALUE(tableDeletedNum
);
1113 return tableDeletedNum
;
1116 /* available converters list --------------------------------------------------- */
1118 static void U_CALLCONV
initAvailableConvertersList(UErrorCode
&errCode
) {
1119 U_ASSERT(gAvailableConverterCount
== 0);
1120 U_ASSERT(gAvailableConverters
== NULL
);
1122 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1123 UEnumeration
*allConvEnum
= ucnv_openAllNames(&errCode
);
1124 int32_t allConverterCount
= uenum_count(allConvEnum
, &errCode
);
1125 if (U_FAILURE(errCode
)) {
1129 /* We can't have more than "*converterTable" converters to open */
1130 gAvailableConverters
= (const char **) uprv_malloc(allConverterCount
* sizeof(char*));
1131 if (!gAvailableConverters
) {
1132 errCode
= U_MEMORY_ALLOCATION_ERROR
;
1136 /* Open the default converter to make sure that it has first dibs in the hash table. */
1137 UErrorCode localStatus
= U_ZERO_ERROR
;
1138 UConverter tempConverter
;
1139 ucnv_close(ucnv_createConverter(&tempConverter
, NULL
, &localStatus
));
1141 gAvailableConverterCount
= 0;
1143 for (int32_t idx
= 0; idx
< allConverterCount
; idx
++) {
1144 localStatus
= U_ZERO_ERROR
;
1145 const char *converterName
= uenum_next(allConvEnum
, NULL
, &localStatus
);
1146 if (ucnv_canCreateConverter(converterName
, &localStatus
)) {
1147 gAvailableConverters
[gAvailableConverterCount
++] = converterName
;
1151 uenum_close(allConvEnum
);
1155 static UBool
haveAvailableConverterList(UErrorCode
*pErrorCode
) {
1156 umtx_initOnce(gAvailableConvertersInitOnce
, &initAvailableConvertersList
, *pErrorCode
);
1157 return U_SUCCESS(*pErrorCode
);
1161 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
) {
1162 if (haveAvailableConverterList(pErrorCode
)) {
1163 return gAvailableConverterCount
;
1168 U_CFUNC
const char *
1169 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
) {
1170 if (haveAvailableConverterList(pErrorCode
)) {
1171 if (n
< gAvailableConverterCount
) {
1172 return gAvailableConverters
[n
];
1174 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
1179 /* default converter name --------------------------------------------------- */
1181 #if !U_CHARSET_IS_UTF8
1183 Copy the canonical converter name.
1184 ucnv_getDefaultName must be thread safe, which can call this function.
1186 ucnv_setDefaultName calls this function and it doesn't have to be
1187 thread safe because there is no reliable/safe way to reset the
1188 converter in use in all threads. If you did reset the converter, you
1189 would not be sure that retrieving a default converter for one string
1190 would be the same type of default converter for a successive string.
1191 Since the name is a returned via ucnv_getDefaultName without copying,
1192 you shouldn't be modifying or deleting the string from a separate thread.
1195 internalSetName(const char *name
, UErrorCode
*status
) {
1196 UConverterNamePieces stackPieces
;
1197 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
1198 int32_t length
=(int32_t)(uprv_strlen(name
));
1199 UBool containsOption
= (UBool
)(uprv_strchr(name
, UCNV_OPTION_SEP_CHAR
) != NULL
);
1200 const UConverterSharedData
*algorithmicSharedData
;
1202 stackArgs
.name
= name
;
1203 if(containsOption
) {
1204 stackPieces
.cnvName
[0] = 0;
1205 stackPieces
.locale
[0] = 0;
1206 stackPieces
.options
= 0;
1207 parseConverterOptions(name
, &stackPieces
, &stackArgs
, status
);
1208 if(U_FAILURE(*status
)) {
1212 algorithmicSharedData
= getAlgorithmicTypeFromName(stackArgs
.name
);
1214 umtx_lock(&cnvCacheMutex
);
1216 gDefaultAlgorithmicSharedData
= algorithmicSharedData
;
1217 gDefaultConverterContainsOption
= containsOption
;
1218 uprv_memcpy(gDefaultConverterNameBuffer
, name
, length
);
1219 gDefaultConverterNameBuffer
[length
]=0;
1221 /* gDefaultConverterName MUST be the last global var set by this function. */
1222 /* It is the variable checked in ucnv_getDefaultName() to see if initialization is required. */
1223 // But there is nothing here preventing that from being reordered, either by the compiler
1224 // or hardware. I'm adding the mutex to ucnv_getDefaultName for now. UMTX_CHECK is not enough.
1226 gDefaultConverterName
= gDefaultConverterNameBuffer
;
1228 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1230 umtx_unlock(&cnvCacheMutex
);
1235 * In order to be really thread-safe, the get function would have to take
1236 * a buffer parameter and copy the current string inside a mutex block.
1237 * This implementation only tries to be really thread-safe while
1239 * It assumes that setting a pointer is atomic.
1242 U_CAPI
const char* U_EXPORT2
1243 ucnv_getDefaultName() {
1244 #if U_CHARSET_IS_UTF8
1247 /* local variable to be thread-safe */
1251 Concurrent calls to ucnv_getDefaultName must be thread safe,
1252 but ucnv_setDefaultName is not thread safe.
1255 icu::Mutex
lock(&cnvCacheMutex
);
1256 name
= gDefaultConverterName
;
1259 UErrorCode errorCode
= U_ZERO_ERROR
;
1260 UConverter
*cnv
= NULL
;
1262 name
= uprv_getDefaultCodepage();
1264 /* if the name is there, test it out and get the canonical name with options */
1266 cnv
= ucnv_open(name
, &errorCode
);
1267 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1268 name
= ucnv_getName(cnv
, &errorCode
);
1272 if(name
== NULL
|| name
[0] == 0
1273 || U_FAILURE(errorCode
) || cnv
== NULL
1274 || uprv_strlen(name
)>=sizeof(gDefaultConverterNameBuffer
))
1276 /* Panic time, let's use a fallback. */
1277 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1279 /* there is no 'algorithmic' converter for EBCDIC */
1280 #elif U_PLATFORM == U_PF_OS390
1281 name
= "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING
;
1283 name
= "ibm-37_P100-1995";
1287 internalSetName(name
, &errorCode
);
1289 /* The close may make the current name go away. */
1297 #if U_CHARSET_IS_UTF8
1298 U_CAPI
void U_EXPORT2
ucnv_setDefaultName(const char *) {}
1301 This function is not thread safe, and it can't be thread safe.
1302 See internalSetName or the API reference for details.
1304 U_CAPI
void U_EXPORT2
1305 ucnv_setDefaultName(const char *converterName
) {
1306 if(converterName
==NULL
) {
1307 /* reset to the default codepage */
1308 gDefaultConverterName
=NULL
;
1310 UErrorCode errorCode
= U_ZERO_ERROR
;
1311 UConverter
*cnv
= NULL
;
1312 const char *name
= NULL
;
1314 /* if the name is there, test it out and get the canonical name with options */
1315 cnv
= ucnv_open(converterName
, &errorCode
);
1316 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1317 name
= ucnv_getName(cnv
, &errorCode
);
1320 if(U_SUCCESS(errorCode
) && name
!=NULL
) {
1321 internalSetName(name
, &errorCode
);
1323 /* else this converter is bad to use. Don't change it to a bad value. */
1325 /* The close may make the current name go away. */
1328 /* reset the converter cache */
1329 u_flushDefaultConverter();
1334 /* data swapping ------------------------------------------------------------ */
1336 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
1338 #if !UCONFIG_NO_LEGACY_CONVERSION
1340 U_CAPI
int32_t U_EXPORT2
1341 ucnv_swap(const UDataSwapper
*ds
,
1342 const void *inData
, int32_t length
, void *outData
,
1343 UErrorCode
*pErrorCode
) {
1344 const UDataInfo
*pInfo
;
1347 const uint8_t *inBytes
;
1350 uint32_t offset
, count
, staticDataSize
;
1353 const UConverterStaticData
*inStaticData
;
1354 UConverterStaticData
*outStaticData
;
1356 const _MBCSHeader
*inMBCSHeader
;
1357 _MBCSHeader
*outMBCSHeader
;
1358 _MBCSHeader mbcsHeader
;
1359 uint32_t mbcsHeaderLength
;
1360 UBool noFromU
=FALSE
;
1364 int32_t maxFastUChar
, mbcsIndexLength
;
1366 const int32_t *inExtIndexes
;
1369 /* udata_swapDataHeader checks the arguments */
1370 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1371 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1375 /* check data format and format version */
1376 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1378 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1379 pInfo
->dataFormat
[1]==0x6e &&
1380 pInfo
->dataFormat
[2]==0x76 &&
1381 pInfo
->dataFormat
[3]==0x74 &&
1382 pInfo
->formatVersion
[0]==6 &&
1383 pInfo
->formatVersion
[1]>=2
1385 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",
1386 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1387 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1388 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1389 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1393 inBytes
=(const uint8_t *)inData
+headerSize
;
1394 outBytes
=(uint8_t *)outData
+headerSize
;
1396 /* read the initial UConverterStaticData structure after the UDataInfo header */
1397 inStaticData
=(const UConverterStaticData
*)inBytes
;
1398 outStaticData
=(UConverterStaticData
*)outBytes
;
1401 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1404 if( length
<(int32_t)sizeof(UConverterStaticData
) ||
1405 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1407 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1409 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1415 /* swap the static data */
1416 if(inStaticData
!=outStaticData
) {
1417 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1420 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1421 &outStaticData
->structSize
, pErrorCode
);
1422 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1423 &outStaticData
->codepage
, pErrorCode
);
1425 ds
->swapInvChars(ds
, inStaticData
->name
, (int32_t)uprv_strlen(inStaticData
->name
),
1426 outStaticData
->name
, pErrorCode
);
1427 if(U_FAILURE(*pErrorCode
)) {
1428 udata_printError(ds
, "ucnv_swap(): error swapping converter name\n");
1433 inBytes
+=staticDataSize
;
1434 outBytes
+=staticDataSize
;
1436 length
-=(int32_t)staticDataSize
;
1439 /* check for supported conversionType values */
1440 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1441 /* swap MBCS data */
1442 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1443 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1445 if(0<=length
&& length
<(int32_t)sizeof(_MBCSHeader
)) {
1446 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1448 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1451 if(inMBCSHeader
->version
[0]==4 && inMBCSHeader
->version
[1]>=1) {
1452 mbcsHeaderLength
=MBCS_HEADER_V4_LENGTH
;
1453 } else if(inMBCSHeader
->version
[0]==5 && inMBCSHeader
->version
[1]>=3 &&
1454 ((mbcsHeader
.options
=ds
->readUInt32(inMBCSHeader
->options
))&
1455 MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0
1457 mbcsHeaderLength
=mbcsHeader
.options
&MBCS_OPT_LENGTH_MASK
;
1458 noFromU
=(UBool
)((mbcsHeader
.options
&MBCS_OPT_NO_FROM_U
)!=0);
1460 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1461 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1462 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1466 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1467 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1468 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1469 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1470 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1471 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1472 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1473 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1474 /* mbcsHeader.options have been read above */
1476 extOffset
=(int32_t)(mbcsHeader
.flags
>>8);
1477 outputType
=(uint8_t)mbcsHeader
.flags
;
1478 if(noFromU
&& outputType
==MBCS_OUTPUT_1
) {
1479 udata_printError(ds
, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
1480 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1484 /* make sure that the output type is known */
1485 switch(outputType
) {
1490 case MBCS_OUTPUT_3_EUC
:
1491 case MBCS_OUTPUT_4_EUC
:
1492 case MBCS_OUTPUT_2_SISO
:
1493 case MBCS_OUTPUT_EXT_ONLY
:
1497 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1499 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1503 /* calculate the length of the MBCS data */
1506 * utf8Friendly MBCS files (mbcsHeader.version 4.3)
1507 * contain an additional mbcsIndex table:
1508 * uint16_t[(maxFastUChar+1)>>6];
1509 * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
1513 if( outputType
!=MBCS_OUTPUT_EXT_ONLY
&& outputType
!=MBCS_OUTPUT_1
&&
1514 mbcsHeader
.version
[1]>=3 && (maxFastUChar
=mbcsHeader
.version
[2])!=0
1516 maxFastUChar
=(maxFastUChar
<<8)|0xff;
1517 mbcsIndexLength
=((maxFastUChar
+1)>>6)*2; /* number of bytes */
1521 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsIndexLength
);
1523 size
+=(int32_t)mbcsHeader
.fromUBytesLength
;
1526 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1529 /* there is extension data after the base data, see ucnv_ext.h */
1530 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1531 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1533 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1537 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1538 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1543 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1545 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1549 /* copy the data for inaccessible bytes */
1550 if(inBytes
!=outBytes
) {
1551 uprv_memcpy(outBytes
, inBytes
, size
);
1554 /* swap the MBCSHeader, except for the version field */
1555 count
=mbcsHeaderLength
*4;
1556 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, count
-4,
1557 &outMBCSHeader
->countStates
, pErrorCode
);
1559 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1561 * extension-only file,
1562 * contains a base name instead of normal base table data
1565 /* swap the base name, between the header and the extension data */
1566 const char *inBaseName
=(const char *)inBytes
+count
;
1567 char *outBaseName
=(char *)outBytes
+count
;
1568 ds
->swapInvChars(ds
, inBaseName
, (int32_t)uprv_strlen(inBaseName
),
1569 outBaseName
, pErrorCode
);
1571 /* normal file with base table data */
1573 /* swap the state table, 1kB per state */
1575 count
=mbcsHeader
.countStates
*1024;
1576 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1577 outBytes
+offset
, pErrorCode
);
1579 /* swap the toUFallbacks[] */
1581 count
=mbcsHeader
.countToUFallbacks
*8;
1582 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1583 outBytes
+offset
, pErrorCode
);
1585 /* swap the unicodeCodeUnits[] */
1586 offset
=mbcsHeader
.offsetToUCodeUnits
;
1587 count
=mbcsHeader
.offsetFromUTable
-offset
;
1588 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1589 outBytes
+offset
, pErrorCode
);
1591 /* offset to the stage 1 table, independent of the outputType */
1592 offset
=mbcsHeader
.offsetFromUTable
;
1594 if(outputType
==MBCS_OUTPUT_1
) {
1595 /* SBCS: swap the fromU tables, all 16 bits wide */
1596 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1597 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1598 outBytes
+offset
, pErrorCode
);
1600 /* otherwise: swap the stage tables separately */
1602 /* stage 1 table: uint16_t[0x440 or 0x40] */
1603 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1604 count
=0x440*2; /* for all of Unicode */
1606 count
=0x40*2; /* only BMP */
1608 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1609 outBytes
+offset
, pErrorCode
);
1611 /* stage 2 table: uint32_t[] */
1613 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1614 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1615 outBytes
+offset
, pErrorCode
);
1617 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1618 offset
=mbcsHeader
.offsetFromUBytes
;
1619 count
= noFromU
? 0 : mbcsHeader
.fromUBytesLength
;
1620 switch(outputType
) {
1622 case MBCS_OUTPUT_3_EUC
:
1623 case MBCS_OUTPUT_2_SISO
:
1624 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1625 outBytes
+offset
, pErrorCode
);
1628 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1629 outBytes
+offset
, pErrorCode
);
1632 /* just uint8_t[], nothing to swap */
1636 if(mbcsIndexLength
!=0) {
1638 count
=mbcsIndexLength
;
1639 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1640 outBytes
+offset
, pErrorCode
);
1646 /* swap the extension data */
1648 outBytes
+=extOffset
;
1650 /* swap toUTable[] */
1651 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1652 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1653 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1655 /* swap toUUChars[] */
1656 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1657 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1658 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1660 /* swap fromUTableUChars[] */
1661 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1662 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1663 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1665 /* swap fromUTableValues[] */
1666 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1667 /* same length as for fromUTableUChars[] */
1668 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1670 /* no need to swap fromUBytes[] */
1672 /* swap fromUStage12[] */
1673 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1674 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1675 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1677 /* swap fromUStage3[] */
1678 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1679 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1680 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1682 /* swap fromUStage3b[] */
1683 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1684 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1685 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1687 /* swap indexes[] */
1688 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1689 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1693 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1694 inStaticData
->conversionType
);
1695 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1699 return headerSize
+(int32_t)staticDataSize
+size
;
1702 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */