2 ********************************************************************
4 * Copyright (c) 1996-2012, 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"
49 extern void UCNV_DEBUG_LOG(char *what
, char *who
, void *p
, int l
);
50 #define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
52 # define UCNV_DEBUG_LOG(x,y,z)
55 static const UConverterSharedData
* const
56 converterData
[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
]={
59 #if UCONFIG_NO_LEGACY_CONVERSION
66 &_UTF8Data
, &_UTF16BEData
, &_UTF16LEData
, &_UTF32BEData
, &_UTF32LEData
,
69 #if UCONFIG_NO_LEGACY_CONVERSION
71 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
72 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
76 &_LMBCSData1
,&_LMBCSData2
, &_LMBCSData3
, &_LMBCSData4
, &_LMBCSData5
, &_LMBCSData6
,
77 &_LMBCSData8
,&_LMBCSData11
,&_LMBCSData16
,&_LMBCSData17
,&_LMBCSData18
,&_LMBCSData19
,
83 #if UCONFIG_NO_LEGACY_CONVERSION
90 &_UTF7Data
, &_Bocu1Data
, &_UTF16Data
, &_UTF32Data
, &_CESU8Data
, &_IMAPData
,
92 #if UCONFIG_NO_LEGACY_CONVERSION
99 /* Please keep this in binary sorted order for getAlgorithmicTypeFromName.
100 Also the name should be in lower case and all spaces, dashes and underscores
105 const UConverterType type
;
106 } const cnvNameType
[] = {
107 { "bocu1", UCNV_BOCU1
},
108 { "cesu8", UCNV_CESU8
},
109 #if !UCONFIG_NO_LEGACY_CONVERSION
112 { "imapmailboxname", UCNV_IMAP_MAILBOX
},
113 #if !UCONFIG_NO_LEGACY_CONVERSION
114 { "iscii", UCNV_ISCII
},
115 { "iso2022", UCNV_ISO_2022
},
117 { "iso88591", UCNV_LATIN_1
},
118 #if !UCONFIG_NO_LEGACY_CONVERSION
119 { "lmbcs1", UCNV_LMBCS_1
},
120 { "lmbcs11",UCNV_LMBCS_11
},
121 { "lmbcs16",UCNV_LMBCS_16
},
122 { "lmbcs17",UCNV_LMBCS_17
},
123 { "lmbcs18",UCNV_LMBCS_18
},
124 { "lmbcs19",UCNV_LMBCS_19
},
125 { "lmbcs2", UCNV_LMBCS_2
},
126 { "lmbcs3", UCNV_LMBCS_3
},
127 { "lmbcs4", UCNV_LMBCS_4
},
128 { "lmbcs5", UCNV_LMBCS_5
},
129 { "lmbcs6", UCNV_LMBCS_6
},
130 { "lmbcs8", UCNV_LMBCS_8
},
132 { "scsu", UCNV_SCSU
},
133 { "usascii", UCNV_US_ASCII
},
134 { "utf16", UCNV_UTF16
},
135 { "utf16be", UCNV_UTF16_BigEndian
},
136 { "utf16le", UCNV_UTF16_LittleEndian
},
138 { "utf16oppositeendian", UCNV_UTF16_LittleEndian
},
139 { "utf16platformendian", UCNV_UTF16_BigEndian
},
141 { "utf16oppositeendian", UCNV_UTF16_BigEndian
},
142 { "utf16platformendian", UCNV_UTF16_LittleEndian
},
144 { "utf32", UCNV_UTF32
},
145 { "utf32be", UCNV_UTF32_BigEndian
},
146 { "utf32le", UCNV_UTF32_LittleEndian
},
148 { "utf32oppositeendian", UCNV_UTF32_LittleEndian
},
149 { "utf32platformendian", UCNV_UTF32_BigEndian
},
151 { "utf32oppositeendian", UCNV_UTF32_BigEndian
},
152 { "utf32platformendian", UCNV_UTF32_LittleEndian
},
154 { "utf7", UCNV_UTF7
},
155 { "utf8", UCNV_UTF8
},
156 { "x11compoundtext", UCNV_COMPOUND_TEXT
}
160 /*initializes some global variables */
161 static UHashtable
*SHARED_DATA_HASHTABLE
= NULL
;
162 static UMutex cnvCacheMutex
= U_MUTEX_INITIALIZER
; /* Mutex for synchronizing cnv cache access. */
163 /* Note: the global mutex is used for */
164 /* reference count updates. */
166 static const char **gAvailableConverters
= NULL
;
167 static uint16_t gAvailableConverterCount
= 0;
169 #if !U_CHARSET_IS_UTF8
171 /* This contains the resolved converter name. So no further alias lookup is needed again. */
172 static char gDefaultConverterNameBuffer
[UCNV_MAX_CONVERTER_NAME_LENGTH
+ 1]; /* +1 for NULL */
173 static const char *gDefaultConverterName
= NULL
;
176 If the default converter is an algorithmic converter, this is the cached value.
177 We don't cache a full UConverter and clone it because ucnv_clone doesn't have
178 less overhead than an algorithmic open. We don't cache non-algorithmic converters
179 because ucnv_flushCache must be able to unload the default converter and its table.
181 static const UConverterSharedData
*gDefaultAlgorithmicSharedData
= NULL
;
183 /* Does gDefaultConverterName have a converter option and require extra parsing? */
184 static UBool gDefaultConverterContainsOption
;
186 #endif /* !U_CHARSET_IS_UTF8 */
188 static const char DATA_TYPE
[] = "cnv";
191 ucnv_flushAvailableConverterCache() {
192 if (gAvailableConverters
) {
193 umtx_lock(&cnvCacheMutex
);
194 gAvailableConverterCount
= 0;
195 uprv_free((char **)gAvailableConverters
);
196 gAvailableConverters
= NULL
;
197 umtx_unlock(&cnvCacheMutex
);
201 /* ucnv_cleanup - delete all storage held by the converter cache, except any */
202 /* in use by open converters. */
203 /* Not thread safe. */
204 /* Not supported API. */
205 static UBool U_CALLCONV
ucnv_cleanup(void) {
207 if (SHARED_DATA_HASHTABLE
!= NULL
&& uhash_count(SHARED_DATA_HASHTABLE
) == 0) {
208 uhash_close(SHARED_DATA_HASHTABLE
);
209 SHARED_DATA_HASHTABLE
= NULL
;
212 /* Isn't called from flushCache because other threads may have preexisting references to the table. */
213 ucnv_flushAvailableConverterCache();
215 #if !U_CHARSET_IS_UTF8
216 gDefaultConverterName
= NULL
;
217 gDefaultConverterNameBuffer
[0] = 0;
218 gDefaultConverterContainsOption
= FALSE
;
219 gDefaultAlgorithmicSharedData
= NULL
;
222 return (SHARED_DATA_HASHTABLE
== NULL
);
225 static UBool U_CALLCONV
226 isCnvAcceptable(void * /*context*/,
227 const char * /*type*/, const char * /*name*/,
228 const UDataInfo
*pInfo
) {
231 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
232 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
233 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
234 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
235 pInfo
->dataFormat
[1]==0x6e &&
236 pInfo
->dataFormat
[2]==0x76 &&
237 pInfo
->dataFormat
[3]==0x74 &&
238 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
242 * Un flatten shared data from a UDATA..
244 static UConverterSharedData
*
245 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
247 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
248 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
249 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
250 UConverterSharedData
*data
;
251 UConverterType type
= (UConverterType
)source
->conversionType
;
253 if(U_FAILURE(*status
))
256 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
257 converterData
[type
] == NULL
||
258 converterData
[type
]->referenceCounter
!= 1 ||
259 source
->structSize
!= sizeof(UConverterStaticData
))
261 *status
= U_INVALID_TABLE_FORMAT
;
265 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
267 *status
= U_MEMORY_ALLOCATION_ERROR
;
271 /* copy initial values from the static structure for this type */
272 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
274 #if 0 /* made UConverterMBCSTable part of UConverterSharedData -- markus 20031107 */
276 * It would be much more efficient if the table were a direct member, not a pointer.
277 * However, that would add to the size of all UConverterSharedData objects
278 * even if they do not use this table (especially algorithmic ones).
279 * If this changes, then the static templates from converterData[type]
282 * In principle, it would be cleaner if the load() function below
283 * allocated the table.
285 data
->table
= (UConverterTable
*)uprv_malloc(sizeof(UConverterTable
));
286 if(data
->table
== NULL
) {
288 *status
= U_MEMORY_ALLOCATION_ERROR
;
291 uprv_memset(data
->table
, 0, sizeof(UConverterTable
));
294 data
->staticData
= source
;
296 data
->sharedDataCached
= FALSE
;
298 /* fill in fields from the loaded data */
299 data
->dataMemory
= (void*)pData
; /* for future use */
301 if(data
->impl
->load
!= NULL
) {
302 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
303 if(U_FAILURE(*status
)) {
304 uprv_free(data
->table
);
312 /*Takes an alias name gets an actual converter file name
313 *goes to disk and opens it.
314 *allocates the memory and returns a new UConverter object
316 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
319 UConverterSharedData
*sharedData
;
321 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
323 if (U_FAILURE (*err
)) {
324 UTRACE_EXIT_STATUS(*err
);
328 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
330 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
333 UTRACE_EXIT_STATUS(*err
);
337 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
341 UTRACE_EXIT_STATUS(*err
);
346 * TODO Store pkg in a field in the shared data so that delta-only converters
347 * can load base converters from the same package.
348 * If the pkg name is longer than the field, then either do not load the converter
349 * in the first place, or just set the pkg field to "".
352 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
356 /*returns a converter type from a string
358 static const UConverterSharedData
*
359 getAlgorithmicTypeFromName(const char *realName
)
361 uint32_t mid
, start
, limit
;
364 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
366 /* Lower case and remove ignoreable characters. */
367 ucnv_io_stripForCompare(strippedName
, realName
);
369 /* do a binary search for the alias */
371 limit
= sizeof(cnvNameType
)/sizeof(cnvNameType
[0]);
373 lastMid
= UINT32_MAX
;
376 mid
= (uint32_t)((start
+ limit
) / 2);
377 if (lastMid
== mid
) { /* Have we moved? */
378 break; /* We haven't moved, and it wasn't found. */
381 result
= uprv_strcmp(strippedName
, cnvNameType
[mid
].name
);
385 } else if (result
> 0) {
388 return converterData
[cnvNameType
[mid
].type
];
396 * Based on the number of known converters, this determines how many times larger
397 * the shared data hash table should be. When on small platforms, or just a couple
398 * of converters are used, this number should be 2. When memory is plentiful, or
399 * when ucnv_countAvailable is ever used with a lot of available converters,
401 * Larger numbers reduce the number of hash collisions, but use more memory.
403 #define UCNV_CACHE_LOAD_FACTOR 2
405 /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
406 /* Will always be called with the cnvCacheMutex alrady being held */
407 /* by the calling function. */
408 /* Stores the shared data in the SHARED_DATA_HASHTABLE
409 * @param data The shared data
412 ucnv_shareConverterData(UConverterSharedData
* data
)
414 UErrorCode err
= U_ZERO_ERROR
;
415 /*Lazy evaluates the Hashtable itself */
416 /*void *sanity = NULL;*/
418 if (SHARED_DATA_HASHTABLE
== NULL
)
420 SHARED_DATA_HASHTABLE
= uhash_openSize(uhash_hashChars
, uhash_compareChars
, NULL
,
421 ucnv_io_countKnownConverters(&err
)*UCNV_CACHE_LOAD_FACTOR
,
423 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
429 /* ### check to see if the element is not already there! */
432 sanity = ucnv_getSharedConverterData (data->staticData->name);
435 UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
437 UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
441 data
->sharedDataCached
= TRUE
;
443 uhash_put(SHARED_DATA_HASHTABLE
,
444 (void*) data
->staticData
->name
, /* Okay to cast away const as long as
445 keyDeleter == NULL */
448 UCNV_DEBUG_LOG("put", data
->staticData
->name
,data
);
452 /* Look up a converter name in the shared data cache. */
453 /* cnvCacheMutex must be held by the caller to protect the hash table. */
454 /* gets the shared data from the SHARED_DATA_HASHTABLE (might return NULL if it isn't there)
455 * @param name The name of the shared data
456 * @return the shared data from the SHARED_DATA_HASHTABLE
458 static UConverterSharedData
*
459 ucnv_getSharedConverterData(const char *name
)
461 /*special case when no Table has yet been created we return NULL */
462 if (SHARED_DATA_HASHTABLE
== NULL
)
468 UConverterSharedData
*rc
;
470 rc
= (UConverterSharedData
*)uhash_get(SHARED_DATA_HASHTABLE
, name
);
471 UCNV_DEBUG_LOG("get",name
,rc
);
476 /*frees the string of memory blocks associates with a sharedConverter
477 *if and only if the referenceCounter == 0
479 /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
480 * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
482 * otherwise returns FALSE
483 * @param sharedConverterData The shared data
484 * @return if not it frees all the memory stemming from sharedConverterData and
485 * returns TRUE, otherwise returns FALSE
488 ucnv_deleteSharedConverterData(UConverterSharedData
* deadSharedData
)
490 UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD
);
491 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "unload converter %s shared data %p", deadSharedData
->staticData
->name
, deadSharedData
);
493 if (deadSharedData
->referenceCounter
> 0) {
494 UTRACE_EXIT_VALUE((int32_t)FALSE
);
498 if (deadSharedData
->impl
->unload
!= NULL
) {
499 deadSharedData
->impl
->unload(deadSharedData
);
502 if(deadSharedData
->dataMemory
!= NULL
)
504 UDataMemory
*data
= (UDataMemory
*)deadSharedData
->dataMemory
;
508 if(deadSharedData
->table
!= NULL
)
510 uprv_free(deadSharedData
->table
);
514 /* if the static data is actually owned by the shared data */
515 /* enable if we ever have this situation. */
516 if(deadSharedData
->staticDataOwned
== TRUE
) /* see ucnv_bld.h */
518 uprv_free((void*)deadSharedData
->staticData
);
524 uprv_memset(deadSharedData
->0, sizeof(*deadSharedData
));
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->referenceCounter != ~0
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
)
597 Checking whether it's an algorithic converter is okay
598 in multithreaded applications because the value never changes.
599 Don't check referenceCounter for any other value.
601 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= (uint32_t)~0) {
602 umtx_lock(&cnvCacheMutex
);
603 ucnv_unload(sharedData
);
604 umtx_unlock(&cnvCacheMutex
);
609 ucnv_incrementRefCount(UConverterSharedData
*sharedData
)
612 Checking whether it's an algorithic converter is okay
613 in multithreaded applications because the value never changes.
614 Don't check referenceCounter for any other value.
616 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= (uint32_t)~0) {
617 umtx_lock(&cnvCacheMutex
);
618 sharedData
->referenceCounter
++;
619 umtx_unlock(&cnvCacheMutex
);
624 * *pPieces must be initialized.
625 * The name without options will be copied to pPieces->cnvName.
626 * The locale and options will be copied to pPieces only if present in inName,
627 * otherwise the existing values in pPieces remain.
628 * *pArgs will be set to the pPieces values.
631 parseConverterOptions(const char *inName
,
632 UConverterNamePieces
*pPieces
,
633 UConverterLoadArgs
*pArgs
,
636 char *cnvName
= pPieces
->cnvName
;
641 pArgs
->locale
=pPieces
->locale
;
642 pArgs
->options
=pPieces
->options
;
644 /* copy the converter name itself to cnvName */
645 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
646 if (++len
>=UCNV_MAX_CONVERTER_NAME_LENGTH
) {
647 *err
= U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
648 pPieces
->cnvName
[0]=0;
655 pArgs
->name
=pPieces
->cnvName
;
657 /* parse options. No more name copying should occur. */
658 while((c
=*inName
)!=0) {
659 if(c
==UCNV_OPTION_SEP_CHAR
) {
663 /* inName is behind an option separator */
664 if(uprv_strncmp(inName
, "locale=", 7)==0) {
665 /* do not modify locale itself in case we have multiple locale options */
666 char *dest
=pPieces
->locale
;
668 /* copy the locale option value */
671 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
674 if(++len
>=ULOC_FULLNAME_CAPACITY
) {
675 *err
=U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
676 pPieces
->locale
[0]=0;
683 } else if(uprv_strncmp(inName
, "version=", 8)==0) {
684 /* copy the version option value into bits 3..0 of pPieces->options */
688 pArgs
->options
=(pPieces
->options
&=~UCNV_OPTION_VERSION
);
690 } else if((uint8_t)(c
-'0')<10) {
691 pArgs
->options
=pPieces
->options
=(pPieces
->options
&~UCNV_OPTION_VERSION
)|(uint32_t)(c
-'0');
694 } else if(uprv_strncmp(inName
, "swaplfnl", 8)==0) {
696 pArgs
->options
=(pPieces
->options
|=UCNV_OPTION_SWAP_LFNL
);
697 /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
699 /* ignore any other options until we define some */
700 while(((c
= *inName
++) != 0) && (c
!= UCNV_OPTION_SEP_CHAR
)) {
709 /*Logic determines if the converter is Algorithmic AND/OR cached
711 * -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
712 * -Get it from a Hashtable (Data=X, Cached=TRUE)
713 * -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
714 * -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
716 U_CFUNC UConverterSharedData
*
717 ucnv_loadSharedData(const char *converterName
,
718 UConverterNamePieces
*pPieces
,
719 UConverterLoadArgs
*pArgs
,
721 UConverterNamePieces stackPieces
;
722 UConverterLoadArgs stackArgs
;
723 UConverterSharedData
*mySharedConverterData
= NULL
;
724 UErrorCode internalErrorCode
= U_ZERO_ERROR
;
725 UBool mayContainOption
= TRUE
;
726 UBool checkForAlgorithmic
= TRUE
;
728 if (U_FAILURE (*err
)) {
732 if(pPieces
== NULL
) {
735 * Bad: We may set pArgs pointers to stackPieces fields
736 * which will be invalid after this function returns.
738 *err
= U_INTERNAL_PROGRAM_ERROR
;
741 pPieces
= &stackPieces
;
744 uprv_memset(&stackArgs
, 0, sizeof(stackArgs
));
745 stackArgs
.size
= (int32_t)sizeof(stackArgs
);
749 pPieces
->cnvName
[0] = 0;
750 pPieces
->locale
[0] = 0;
751 pPieces
->options
= 0;
753 pArgs
->name
= converterName
;
754 pArgs
->locale
= pPieces
->locale
;
755 pArgs
->options
= pPieces
->options
;
757 /* In case "name" is NULL we want to open the default converter. */
758 if (converterName
== NULL
) {
759 #if U_CHARSET_IS_UTF8
760 pArgs
->name
= "UTF-8";
761 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
763 /* Call ucnv_getDefaultName first to query the name from the OS. */
764 pArgs
->name
= ucnv_getDefaultName();
765 if (pArgs
->name
== NULL
) {
766 *err
= U_MISSING_RESOURCE_ERROR
;
769 mySharedConverterData
= (UConverterSharedData
*)gDefaultAlgorithmicSharedData
;
770 checkForAlgorithmic
= FALSE
;
771 mayContainOption
= gDefaultConverterContainsOption
;
772 /* the default converter name is already canonical */
775 else if(UCNV_FAST_IS_UTF8(converterName
)) {
776 /* fastpath for UTF-8 */
777 pArgs
->name
= "UTF-8";
778 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
781 /* separate the converter name from the options */
782 parseConverterOptions(converterName
, pPieces
, pArgs
, err
);
783 if (U_FAILURE(*err
)) {
784 /* Very bad name used. */
788 /* get the canonical converter name */
789 pArgs
->name
= ucnv_io_getConverterName(pArgs
->name
, &mayContainOption
, &internalErrorCode
);
790 if (U_FAILURE(internalErrorCode
) || pArgs
->name
== NULL
) {
792 * set the input name in case the converter was added
793 * without updating the alias table, or when there is no alias table
795 pArgs
->name
= pPieces
->cnvName
;
796 } else if (internalErrorCode
== U_AMBIGUOUS_ALIAS_WARNING
) {
797 *err
= U_AMBIGUOUS_ALIAS_WARNING
;
801 /* separate the converter name from the options */
802 if(mayContainOption
&& pArgs
->name
!= pPieces
->cnvName
) {
803 parseConverterOptions(pArgs
->name
, pPieces
, pArgs
, err
);
806 /* get the shared data for an algorithmic converter, if it is one */
807 if (checkForAlgorithmic
) {
808 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(pArgs
->name
);
810 if (mySharedConverterData
== NULL
)
812 /* it is a data-based converter, get its shared data. */
813 /* Hold the cnvCacheMutex through the whole process of checking the */
814 /* converter data cache, and adding new entries to the cache */
815 /* to prevent other threads from modifying the cache during the */
817 pArgs
->nestedLoads
=1;
820 umtx_lock(&cnvCacheMutex
);
821 mySharedConverterData
= ucnv_load(pArgs
, err
);
822 umtx_unlock(&cnvCacheMutex
);
823 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
829 return mySharedConverterData
;
833 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
835 UConverterNamePieces stackPieces
;
836 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
837 UConverterSharedData
*mySharedConverterData
;
839 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
841 if(U_SUCCESS(*err
)) {
842 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
844 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
846 myUConverter
= ucnv_createConverterFromSharedData(
847 myUConverter
, mySharedConverterData
,
851 if(U_SUCCESS(*err
)) {
852 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
857 /* exit with error */
858 UTRACE_EXIT_STATUS(*err
);
863 ucnv_canCreateConverter(const char *converterName
, UErrorCode
*err
) {
864 UConverter myUConverter
;
865 UConverterNamePieces stackPieces
;
866 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
867 UConverterSharedData
*mySharedConverterData
;
869 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
871 if(U_SUCCESS(*err
)) {
872 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "test if can open converter %s", converterName
);
874 stackArgs
.onlyTestIsLoadable
=TRUE
;
875 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
876 ucnv_createConverterFromSharedData(
877 &myUConverter
, mySharedConverterData
,
880 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
883 UTRACE_EXIT_STATUS(*err
);
884 return U_SUCCESS(*err
);
888 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
890 const char *locale
, uint32_t options
,
893 const UConverterSharedData
*sharedData
;
894 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
896 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
897 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
899 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
900 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
901 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
905 sharedData
= converterData
[type
];
907 Checking whether it's an algorithic converter is okay
908 in multithreaded applications because the value never changes.
909 Don't check referenceCounter for any other value.
911 if(sharedData
== NULL
|| sharedData
->referenceCounter
!= (uint32_t)~0) {
912 /* not a valid type, or not an algorithmic converter */
913 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
914 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
919 stackArgs
.options
= options
;
920 stackArgs
.locale
=locale
;
921 cnv
= ucnv_createConverterFromSharedData(
922 myUConverter
, (UConverterSharedData
*)sharedData
,
925 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
930 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
932 UConverter
*myUConverter
;
933 UConverterSharedData
*mySharedConverterData
;
934 UConverterNamePieces stackPieces
;
935 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
937 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
939 if(U_FAILURE(*err
)) {
940 UTRACE_EXIT_STATUS(*err
);
944 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
946 /* first, get the options out of the converterName string */
947 stackPieces
.cnvName
[0] = 0;
948 stackPieces
.locale
[0] = 0;
949 stackPieces
.options
= 0;
950 parseConverterOptions(converterName
, &stackPieces
, &stackArgs
, err
);
951 if (U_FAILURE(*err
)) {
952 /* Very bad name used. */
953 UTRACE_EXIT_STATUS(*err
);
956 stackArgs
.nestedLoads
=1;
957 stackArgs
.pkg
=packageName
;
959 /* open the data, unflatten the shared structure */
960 mySharedConverterData
= createConverterFromFile(&stackArgs
, err
);
962 if (U_FAILURE(*err
)) {
963 UTRACE_EXIT_STATUS(*err
);
967 /* create the actual converter */
968 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, &stackArgs
, err
);
970 if (U_FAILURE(*err
)) {
971 ucnv_close(myUConverter
);
972 UTRACE_EXIT_STATUS(*err
);
976 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
982 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
983 UConverterSharedData
*mySharedConverterData
,
984 UConverterLoadArgs
*pArgs
,
989 if(U_FAILURE(*err
)) {
990 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
993 if(myUConverter
== NULL
)
995 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
996 if(myUConverter
== NULL
)
998 *err
= U_MEMORY_ALLOCATION_ERROR
;
999 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
1002 isCopyLocal
= FALSE
;
1007 /* initialize the converter */
1008 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
1009 myUConverter
->isCopyLocal
= isCopyLocal
;
1010 /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
1011 myUConverter
->sharedData
= mySharedConverterData
;
1012 myUConverter
->options
= pArgs
->options
;
1013 if(!pArgs
->onlyTestIsLoadable
) {
1014 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
1015 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_DEFAULT_CALLBACK
;
1016 myUConverter
->fromUCharErrorBehaviour
= UCNV_FROM_U_DEFAULT_CALLBACK
;
1017 myUConverter
->toUnicodeStatus
= mySharedConverterData
->toUnicodeStatus
;
1018 myUConverter
->maxBytesPerUChar
= mySharedConverterData
->staticData
->maxBytesPerChar
;
1019 myUConverter
->subChar1
= mySharedConverterData
->staticData
->subChar1
;
1020 myUConverter
->subCharLen
= mySharedConverterData
->staticData
->subCharLen
;
1021 myUConverter
->subChars
= (uint8_t *)myUConverter
->subUChars
;
1022 uprv_memcpy(myUConverter
->subChars
, mySharedConverterData
->staticData
->subChar
, myUConverter
->subCharLen
);
1023 myUConverter
->toUCallbackReason
= UCNV_ILLEGAL
; /* default reason to invoke (*fromCharErrorBehaviour) */
1026 if(mySharedConverterData
->impl
->open
!= NULL
) {
1027 mySharedConverterData
->impl
->open(myUConverter
, pArgs
, err
);
1028 if(U_FAILURE(*err
) && !pArgs
->onlyTestIsLoadable
) {
1029 /* don't ucnv_close() if onlyTestIsLoadable because not fully initialized */
1030 ucnv_close(myUConverter
);
1035 return myUConverter
;
1038 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
1040 U_CAPI
int32_t U_EXPORT2
1043 UConverterSharedData
*mySharedData
= NULL
;
1045 int32_t tableDeletedNum
= 0;
1046 const UHashElement
*e
;
1047 /*UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;*/
1048 int32_t i
, remaining
;
1050 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
1052 /* Close the default converter without creating a new one so that everything will be flushed. */
1053 u_flushDefaultConverter();
1055 /*if shared data hasn't even been lazy evaluated yet
1058 if (SHARED_DATA_HASHTABLE
== NULL
) {
1059 UTRACE_EXIT_VALUE((int32_t)0);
1063 /*creates an enumeration to iterate through every element in the
1066 * Synchronization: holding cnvCacheMutex will prevent any other thread from
1067 * accessing or modifying the hash table during the iteration.
1068 * The reference count of an entry may be decremented by
1069 * ucnv_close while the iteration is in process, but this is
1070 * benign. It can't be incremented (in ucnv_createConverter())
1071 * because the sequence of looking up in the cache + incrementing
1072 * is protected by cnvCacheMutex.
1074 umtx_lock(&cnvCacheMutex
);
1076 * double loop: A delta/extension-only converter has a pointer to its base table's
1077 * shared data; the first iteration of the outer loop may see the delta converter
1078 * before the base converter, and unloading the delta converter may get the base
1079 * converter's reference counter down to 0.
1085 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
1087 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
1088 /*deletes only if reference counter == 0 */
1089 if (mySharedData
->referenceCounter
== 0)
1093 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
1095 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
1096 mySharedData
->sharedDataCached
= FALSE
;
1097 ucnv_deleteSharedConverterData (mySharedData
);
1102 } while(++i
== 1 && remaining
> 0);
1103 umtx_unlock(&cnvCacheMutex
);
1105 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
1107 UTRACE_EXIT_VALUE(tableDeletedNum
);
1108 return tableDeletedNum
;
1111 /* available converters list --------------------------------------------------- */
1113 static UBool
haveAvailableConverterList(UErrorCode
*pErrorCode
) {
1115 UMTX_CHECK(&cnvCacheMutex
, (gAvailableConverters
== NULL
), needInit
);
1117 UConverter tempConverter
;
1118 UEnumeration
*allConvEnum
= NULL
;
1120 uint16_t localConverterCount
;
1121 uint16_t allConverterCount
;
1122 UErrorCode localStatus
;
1123 const char *converterName
;
1124 const char **localConverterList
;
1126 allConvEnum
= ucnv_openAllNames(pErrorCode
);
1127 allConverterCount
= uenum_count(allConvEnum
, pErrorCode
);
1128 if (U_FAILURE(*pErrorCode
)) {
1132 /* We can't have more than "*converterTable" converters to open */
1133 localConverterList
= (const char **) uprv_malloc(allConverterCount
* sizeof(char*));
1134 if (!localConverterList
) {
1135 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1139 /* Open the default converter to make sure that it has first dibs in the hash table. */
1140 localStatus
= U_ZERO_ERROR
;
1141 ucnv_close(ucnv_createConverter(&tempConverter
, NULL
, &localStatus
));
1143 localConverterCount
= 0;
1145 for (idx
= 0; idx
< allConverterCount
; idx
++) {
1146 localStatus
= U_ZERO_ERROR
;
1147 converterName
= uenum_next(allConvEnum
, NULL
, &localStatus
);
1148 if (ucnv_canCreateConverter(converterName
, &localStatus
)) {
1149 localConverterList
[localConverterCount
++] = converterName
;
1152 uenum_close(allConvEnum
);
1154 umtx_lock(&cnvCacheMutex
);
1155 if (gAvailableConverters
== NULL
) {
1156 gAvailableConverterCount
= localConverterCount
;
1157 gAvailableConverters
= localConverterList
;
1158 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1161 uprv_free((char **)localConverterList
);
1163 umtx_unlock(&cnvCacheMutex
);
1169 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
) {
1170 if (haveAvailableConverterList(pErrorCode
)) {
1171 return gAvailableConverterCount
;
1176 U_CFUNC
const char *
1177 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
) {
1178 if (haveAvailableConverterList(pErrorCode
)) {
1179 if (n
< gAvailableConverterCount
) {
1180 return gAvailableConverters
[n
];
1182 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
1187 /* default converter name --------------------------------------------------- */
1189 #if !U_CHARSET_IS_UTF8
1191 Copy the canonical converter name.
1192 ucnv_getDefaultName must be thread safe, which can call this function.
1194 ucnv_setDefaultName calls this function and it doesn't have to be
1195 thread safe because there is no reliable/safe way to reset the
1196 converter in use in all threads. If you did reset the converter, you
1197 would not be sure that retrieving a default converter for one string
1198 would be the same type of default converter for a successive string.
1199 Since the name is a returned via ucnv_getDefaultName without copying,
1200 you shouldn't be modifying or deleting the string from a separate thread.
1203 internalSetName(const char *name
, UErrorCode
*status
) {
1204 UConverterNamePieces stackPieces
;
1205 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
1206 int32_t length
=(int32_t)(uprv_strlen(name
));
1207 UBool containsOption
= (UBool
)(uprv_strchr(name
, UCNV_OPTION_SEP_CHAR
) != NULL
);
1208 const UConverterSharedData
*algorithmicSharedData
;
1210 stackArgs
.name
= name
;
1211 if(containsOption
) {
1212 stackPieces
.cnvName
[0] = 0;
1213 stackPieces
.locale
[0] = 0;
1214 stackPieces
.options
= 0;
1215 parseConverterOptions(name
, &stackPieces
, &stackArgs
, status
);
1216 if(U_FAILURE(*status
)) {
1220 algorithmicSharedData
= getAlgorithmicTypeFromName(stackArgs
.name
);
1222 umtx_lock(&cnvCacheMutex
);
1224 gDefaultAlgorithmicSharedData
= algorithmicSharedData
;
1225 gDefaultConverterContainsOption
= containsOption
;
1226 uprv_memcpy(gDefaultConverterNameBuffer
, name
, length
);
1227 gDefaultConverterNameBuffer
[length
]=0;
1229 /* gDefaultConverterName MUST be the last global var set by this function. */
1230 /* It is the variable checked in ucnv_getDefaultName() to see if initialization is required. */
1231 gDefaultConverterName
= gDefaultConverterNameBuffer
;
1233 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1235 umtx_unlock(&cnvCacheMutex
);
1240 * In order to be really thread-safe, the get function would have to take
1241 * a buffer parameter and copy the current string inside a mutex block.
1242 * This implementation only tries to be really thread-safe while
1244 * It assumes that setting a pointer is atomic.
1247 U_CAPI
const char* U_EXPORT2
1248 ucnv_getDefaultName() {
1249 #if U_CHARSET_IS_UTF8
1252 /* local variable to be thread-safe */
1256 Multiple calls to ucnv_getDefaultName must be thread safe,
1257 but ucnv_setDefaultName is not thread safe.
1259 UMTX_CHECK(&cnvCacheMutex
, gDefaultConverterName
, name
);
1261 UErrorCode errorCode
= U_ZERO_ERROR
;
1262 UConverter
*cnv
= NULL
;
1264 name
= uprv_getDefaultCodepage();
1266 /* if the name is there, test it out and get the canonical name with options */
1268 cnv
= ucnv_open(name
, &errorCode
);
1269 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1270 name
= ucnv_getName(cnv
, &errorCode
);
1274 if(name
== NULL
|| name
[0] == 0
1275 || U_FAILURE(errorCode
) || cnv
== NULL
1276 || uprv_strlen(name
)>=sizeof(gDefaultConverterNameBuffer
))
1278 /* Panic time, let's use a fallback. */
1279 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1281 /* there is no 'algorithmic' converter for EBCDIC */
1282 #elif U_PLATFORM == U_PF_OS390
1283 name
= "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING
;
1285 name
= "ibm-37_P100-1995";
1289 internalSetName(name
, &errorCode
);
1291 /* The close may make the current name go away. */
1299 #if U_CHARSET_IS_UTF8
1300 U_CAPI
void U_EXPORT2
ucnv_setDefaultName(const char *) {}
1303 This function is not thread safe, and it can't be thread safe.
1304 See internalSetName or the API reference for details.
1306 U_CAPI
void U_EXPORT2
1307 ucnv_setDefaultName(const char *converterName
) {
1308 if(converterName
==NULL
) {
1309 /* reset to the default codepage */
1310 gDefaultConverterName
=NULL
;
1312 UErrorCode errorCode
= U_ZERO_ERROR
;
1313 UConverter
*cnv
= NULL
;
1314 const char *name
= NULL
;
1316 /* if the name is there, test it out and get the canonical name with options */
1317 cnv
= ucnv_open(converterName
, &errorCode
);
1318 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1319 name
= ucnv_getName(cnv
, &errorCode
);
1322 if(U_SUCCESS(errorCode
) && name
!=NULL
) {
1323 internalSetName(name
, &errorCode
);
1325 /* else this converter is bad to use. Don't change it to a bad value. */
1327 /* The close may make the current name go away. */
1330 /* reset the converter cache */
1331 u_flushDefaultConverter();
1336 /* data swapping ------------------------------------------------------------ */
1338 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
1340 #if !UCONFIG_NO_LEGACY_CONVERSION
1342 U_CAPI
int32_t U_EXPORT2
1343 ucnv_swap(const UDataSwapper
*ds
,
1344 const void *inData
, int32_t length
, void *outData
,
1345 UErrorCode
*pErrorCode
) {
1346 const UDataInfo
*pInfo
;
1349 const uint8_t *inBytes
;
1352 uint32_t offset
, count
, staticDataSize
;
1355 const UConverterStaticData
*inStaticData
;
1356 UConverterStaticData
*outStaticData
;
1358 const _MBCSHeader
*inMBCSHeader
;
1359 _MBCSHeader
*outMBCSHeader
;
1360 _MBCSHeader mbcsHeader
;
1361 uint32_t mbcsHeaderLength
;
1362 UBool noFromU
=FALSE
;
1366 int32_t maxFastUChar
, mbcsIndexLength
;
1368 const int32_t *inExtIndexes
;
1371 /* udata_swapDataHeader checks the arguments */
1372 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1373 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1377 /* check data format and format version */
1378 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1380 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1381 pInfo
->dataFormat
[1]==0x6e &&
1382 pInfo
->dataFormat
[2]==0x76 &&
1383 pInfo
->dataFormat
[3]==0x74 &&
1384 pInfo
->formatVersion
[0]==6 &&
1385 pInfo
->formatVersion
[1]>=2
1387 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",
1388 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1389 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1390 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1391 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1395 inBytes
=(const uint8_t *)inData
+headerSize
;
1396 outBytes
=(uint8_t *)outData
+headerSize
;
1398 /* read the initial UConverterStaticData structure after the UDataInfo header */
1399 inStaticData
=(const UConverterStaticData
*)inBytes
;
1400 outStaticData
=(UConverterStaticData
*)outBytes
;
1403 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1406 if( length
<(int32_t)sizeof(UConverterStaticData
) ||
1407 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1409 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1411 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1417 /* swap the static data */
1418 if(inStaticData
!=outStaticData
) {
1419 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1422 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1423 &outStaticData
->structSize
, pErrorCode
);
1424 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1425 &outStaticData
->codepage
, pErrorCode
);
1427 ds
->swapInvChars(ds
, inStaticData
->name
, (int32_t)uprv_strlen(inStaticData
->name
),
1428 outStaticData
->name
, pErrorCode
);
1429 if(U_FAILURE(*pErrorCode
)) {
1430 udata_printError(ds
, "ucnv_swap(): error swapping converter name\n");
1435 inBytes
+=staticDataSize
;
1436 outBytes
+=staticDataSize
;
1438 length
-=(int32_t)staticDataSize
;
1441 /* check for supported conversionType values */
1442 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1443 /* swap MBCS data */
1444 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1445 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1447 if(0<=length
&& length
<(int32_t)sizeof(_MBCSHeader
)) {
1448 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1450 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1453 if(inMBCSHeader
->version
[0]==4 && inMBCSHeader
->version
[1]>=1) {
1454 mbcsHeaderLength
=MBCS_HEADER_V4_LENGTH
;
1455 } else if(inMBCSHeader
->version
[0]==5 && inMBCSHeader
->version
[1]>=3 &&
1456 ((mbcsHeader
.options
=ds
->readUInt32(inMBCSHeader
->options
))&
1457 MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0
1459 mbcsHeaderLength
=mbcsHeader
.options
&MBCS_OPT_LENGTH_MASK
;
1460 noFromU
=(UBool
)((mbcsHeader
.options
&MBCS_OPT_NO_FROM_U
)!=0);
1462 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1463 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1464 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1468 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1469 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1470 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1471 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1472 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1473 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1474 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1475 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1476 /* mbcsHeader.options have been read above */
1478 extOffset
=(int32_t)(mbcsHeader
.flags
>>8);
1479 outputType
=(uint8_t)mbcsHeader
.flags
;
1480 if(noFromU
&& outputType
==MBCS_OUTPUT_1
) {
1481 udata_printError(ds
, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
1482 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1486 /* make sure that the output type is known */
1487 switch(outputType
) {
1492 case MBCS_OUTPUT_3_EUC
:
1493 case MBCS_OUTPUT_4_EUC
:
1494 case MBCS_OUTPUT_2_SISO
:
1495 case MBCS_OUTPUT_EXT_ONLY
:
1499 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1501 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1505 /* calculate the length of the MBCS data */
1508 * utf8Friendly MBCS files (mbcsHeader.version 4.3)
1509 * contain an additional mbcsIndex table:
1510 * uint16_t[(maxFastUChar+1)>>6];
1511 * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
1515 if( outputType
!=MBCS_OUTPUT_EXT_ONLY
&& outputType
!=MBCS_OUTPUT_1
&&
1516 mbcsHeader
.version
[1]>=3 && (maxFastUChar
=mbcsHeader
.version
[2])!=0
1518 maxFastUChar
=(maxFastUChar
<<8)|0xff;
1519 mbcsIndexLength
=((maxFastUChar
+1)>>6)*2; /* number of bytes */
1523 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsIndexLength
);
1525 size
+=(int32_t)mbcsHeader
.fromUBytesLength
;
1528 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1531 /* there is extension data after the base data, see ucnv_ext.h */
1532 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1533 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1535 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1539 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1540 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1545 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1547 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1551 /* copy the data for inaccessible bytes */
1552 if(inBytes
!=outBytes
) {
1553 uprv_memcpy(outBytes
, inBytes
, size
);
1556 /* swap the MBCSHeader, except for the version field */
1557 count
=mbcsHeaderLength
*4;
1558 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, count
-4,
1559 &outMBCSHeader
->countStates
, pErrorCode
);
1561 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1563 * extension-only file,
1564 * contains a base name instead of normal base table data
1567 /* swap the base name, between the header and the extension data */
1568 const char *inBaseName
=(const char *)inBytes
+count
;
1569 char *outBaseName
=(char *)outBytes
+count
;
1570 ds
->swapInvChars(ds
, inBaseName
, (int32_t)uprv_strlen(inBaseName
),
1571 outBaseName
, pErrorCode
);
1573 /* normal file with base table data */
1575 /* swap the state table, 1kB per state */
1577 count
=mbcsHeader
.countStates
*1024;
1578 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1579 outBytes
+offset
, pErrorCode
);
1581 /* swap the toUFallbacks[] */
1583 count
=mbcsHeader
.countToUFallbacks
*8;
1584 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1585 outBytes
+offset
, pErrorCode
);
1587 /* swap the unicodeCodeUnits[] */
1588 offset
=mbcsHeader
.offsetToUCodeUnits
;
1589 count
=mbcsHeader
.offsetFromUTable
-offset
;
1590 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1591 outBytes
+offset
, pErrorCode
);
1593 /* offset to the stage 1 table, independent of the outputType */
1594 offset
=mbcsHeader
.offsetFromUTable
;
1596 if(outputType
==MBCS_OUTPUT_1
) {
1597 /* SBCS: swap the fromU tables, all 16 bits wide */
1598 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1599 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1600 outBytes
+offset
, pErrorCode
);
1602 /* otherwise: swap the stage tables separately */
1604 /* stage 1 table: uint16_t[0x440 or 0x40] */
1605 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1606 count
=0x440*2; /* for all of Unicode */
1608 count
=0x40*2; /* only BMP */
1610 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1611 outBytes
+offset
, pErrorCode
);
1613 /* stage 2 table: uint32_t[] */
1615 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1616 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1617 outBytes
+offset
, pErrorCode
);
1619 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1620 offset
=mbcsHeader
.offsetFromUBytes
;
1621 count
= noFromU
? 0 : mbcsHeader
.fromUBytesLength
;
1622 switch(outputType
) {
1624 case MBCS_OUTPUT_3_EUC
:
1625 case MBCS_OUTPUT_2_SISO
:
1626 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1627 outBytes
+offset
, pErrorCode
);
1630 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1631 outBytes
+offset
, pErrorCode
);
1634 /* just uint8_t[], nothing to swap */
1638 if(mbcsIndexLength
!=0) {
1640 count
=mbcsIndexLength
;
1641 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1642 outBytes
+offset
, pErrorCode
);
1648 /* swap the extension data */
1650 outBytes
+=extOffset
;
1652 /* swap toUTable[] */
1653 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1654 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1655 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1657 /* swap toUUChars[] */
1658 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1659 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1660 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1662 /* swap fromUTableUChars[] */
1663 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1664 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1665 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1667 /* swap fromUTableValues[] */
1668 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1669 /* same length as for fromUTableUChars[] */
1670 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1672 /* no need to swap fromUBytes[] */
1674 /* swap fromUStage12[] */
1675 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1676 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1677 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1679 /* swap fromUStage3[] */
1680 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1681 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1682 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1684 /* swap fromUStage3b[] */
1685 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1686 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1687 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1689 /* swap indexes[] */
1690 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1691 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1695 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1696 inStaticData
->conversionType
);
1697 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1701 return headerSize
+(int32_t)staticDataSize
+size
;
1704 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */