2 ********************************************************************
4 * Copyright (c) 1996-2011, 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 UMTX cnvCacheMutex
= NULL
; /* 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 umtx_destroy(&cnvCacheMutex
); /* Don't worry about destroying the mutex even */
223 /* if the hash table still exists. The mutex */
224 /* will lazily re-init itself if needed. */
225 return (SHARED_DATA_HASHTABLE
== NULL
);
228 static UBool U_CALLCONV
229 isCnvAcceptable(void * /*context*/,
230 const char * /*type*/, const char * /*name*/,
231 const UDataInfo
*pInfo
) {
234 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
235 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
236 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
237 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
238 pInfo
->dataFormat
[1]==0x6e &&
239 pInfo
->dataFormat
[2]==0x76 &&
240 pInfo
->dataFormat
[3]==0x74 &&
241 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
245 * Un flatten shared data from a UDATA..
247 static UConverterSharedData
*
248 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
250 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
251 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
252 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
253 UConverterSharedData
*data
;
254 UConverterType type
= (UConverterType
)source
->conversionType
;
256 if(U_FAILURE(*status
))
259 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
260 converterData
[type
] == NULL
||
261 converterData
[type
]->referenceCounter
!= 1 ||
262 source
->structSize
!= sizeof(UConverterStaticData
))
264 *status
= U_INVALID_TABLE_FORMAT
;
268 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
270 *status
= U_MEMORY_ALLOCATION_ERROR
;
274 /* copy initial values from the static structure for this type */
275 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
277 #if 0 /* made UConverterMBCSTable part of UConverterSharedData -- markus 20031107 */
279 * It would be much more efficient if the table were a direct member, not a pointer.
280 * However, that would add to the size of all UConverterSharedData objects
281 * even if they do not use this table (especially algorithmic ones).
282 * If this changes, then the static templates from converterData[type]
285 * In principle, it would be cleaner if the load() function below
286 * allocated the table.
288 data
->table
= (UConverterTable
*)uprv_malloc(sizeof(UConverterTable
));
289 if(data
->table
== NULL
) {
291 *status
= U_MEMORY_ALLOCATION_ERROR
;
294 uprv_memset(data
->table
, 0, sizeof(UConverterTable
));
297 data
->staticData
= source
;
299 data
->sharedDataCached
= FALSE
;
301 /* fill in fields from the loaded data */
302 data
->dataMemory
= (void*)pData
; /* for future use */
304 if(data
->impl
->load
!= NULL
) {
305 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
306 if(U_FAILURE(*status
)) {
307 uprv_free(data
->table
);
315 /*Takes an alias name gets an actual converter file name
316 *goes to disk and opens it.
317 *allocates the memory and returns a new UConverter object
319 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
322 UConverterSharedData
*sharedData
;
324 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
326 if (U_FAILURE (*err
)) {
327 UTRACE_EXIT_STATUS(*err
);
331 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
333 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
336 UTRACE_EXIT_STATUS(*err
);
340 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
344 UTRACE_EXIT_STATUS(*err
);
349 * TODO Store pkg in a field in the shared data so that delta-only converters
350 * can load base converters from the same package.
351 * If the pkg name is longer than the field, then either do not load the converter
352 * in the first place, or just set the pkg field to "".
355 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
359 /*returns a converter type from a string
361 static const UConverterSharedData
*
362 getAlgorithmicTypeFromName(const char *realName
)
364 uint32_t mid
, start
, limit
;
367 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
369 /* Lower case and remove ignoreable characters. */
370 ucnv_io_stripForCompare(strippedName
, realName
);
372 /* do a binary search for the alias */
374 limit
= sizeof(cnvNameType
)/sizeof(cnvNameType
[0]);
376 lastMid
= UINT32_MAX
;
379 mid
= (uint32_t)((start
+ limit
) / 2);
380 if (lastMid
== mid
) { /* Have we moved? */
381 break; /* We haven't moved, and it wasn't found. */
384 result
= uprv_strcmp(strippedName
, cnvNameType
[mid
].name
);
388 } else if (result
> 0) {
391 return converterData
[cnvNameType
[mid
].type
];
399 * Based on the number of known converters, this determines how many times larger
400 * the shared data hash table should be. When on small platforms, or just a couple
401 * of converters are used, this number should be 2. When memory is plentiful, or
402 * when ucnv_countAvailable is ever used with a lot of available converters,
404 * Larger numbers reduce the number of hash collisions, but use more memory.
406 #define UCNV_CACHE_LOAD_FACTOR 2
408 /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
409 /* Will always be called with the cnvCacheMutex alrady being held */
410 /* by the calling function. */
411 /* Stores the shared data in the SHARED_DATA_HASHTABLE
412 * @param data The shared data
415 ucnv_shareConverterData(UConverterSharedData
* data
)
417 UErrorCode err
= U_ZERO_ERROR
;
418 /*Lazy evaluates the Hashtable itself */
419 /*void *sanity = NULL;*/
421 if (SHARED_DATA_HASHTABLE
== NULL
)
423 SHARED_DATA_HASHTABLE
= uhash_openSize(uhash_hashChars
, uhash_compareChars
, NULL
,
424 ucnv_io_countKnownConverters(&err
)*UCNV_CACHE_LOAD_FACTOR
,
426 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
432 /* ### check to see if the element is not already there! */
435 sanity = ucnv_getSharedConverterData (data->staticData->name);
438 UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
440 UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
444 data
->sharedDataCached
= TRUE
;
446 uhash_put(SHARED_DATA_HASHTABLE
,
447 (void*) data
->staticData
->name
, /* Okay to cast away const as long as
448 keyDeleter == NULL */
451 UCNV_DEBUG_LOG("put", data
->staticData
->name
,data
);
455 /* Look up a converter name in the shared data cache. */
456 /* cnvCacheMutex must be held by the caller to protect the hash table. */
457 /* gets the shared data from the SHARED_DATA_HASHTABLE (might return NULL if it isn't there)
458 * @param name The name of the shared data
459 * @return the shared data from the SHARED_DATA_HASHTABLE
461 static UConverterSharedData
*
462 ucnv_getSharedConverterData(const char *name
)
464 /*special case when no Table has yet been created we return NULL */
465 if (SHARED_DATA_HASHTABLE
== NULL
)
471 UConverterSharedData
*rc
;
473 rc
= (UConverterSharedData
*)uhash_get(SHARED_DATA_HASHTABLE
, name
);
474 UCNV_DEBUG_LOG("get",name
,rc
);
479 /*frees the string of memory blocks associates with a sharedConverter
480 *if and only if the referenceCounter == 0
482 /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
483 * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
485 * otherwise returns FALSE
486 * @param sharedConverterData The shared data
487 * @return if not it frees all the memory stemming from sharedConverterData and
488 * returns TRUE, otherwise returns FALSE
491 ucnv_deleteSharedConverterData(UConverterSharedData
* deadSharedData
)
493 UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD
);
494 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "unload converter %s shared data %p", deadSharedData
->staticData
->name
, deadSharedData
);
496 if (deadSharedData
->referenceCounter
> 0) {
497 UTRACE_EXIT_VALUE((int32_t)FALSE
);
501 if (deadSharedData
->impl
->unload
!= NULL
) {
502 deadSharedData
->impl
->unload(deadSharedData
);
505 if(deadSharedData
->dataMemory
!= NULL
)
507 UDataMemory
*data
= (UDataMemory
*)deadSharedData
->dataMemory
;
511 if(deadSharedData
->table
!= NULL
)
513 uprv_free(deadSharedData
->table
);
517 /* if the static data is actually owned by the shared data */
518 /* enable if we ever have this situation. */
519 if(deadSharedData
->staticDataOwned
== TRUE
) /* see ucnv_bld.h */
521 uprv_free((void*)deadSharedData
->staticData
);
527 uprv_memset(deadSharedData
->0, sizeof(*deadSharedData
));
530 uprv_free(deadSharedData
);
532 UTRACE_EXIT_VALUE((int32_t)TRUE
);
537 * Load a non-algorithmic converter.
538 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
540 UConverterSharedData
*
541 ucnv_load(UConverterLoadArgs
*pArgs
, UErrorCode
*err
) {
542 UConverterSharedData
*mySharedConverterData
;
544 if(err
== NULL
|| U_FAILURE(*err
)) {
548 if(pArgs
->pkg
!= NULL
&& *pArgs
->pkg
!= 0) {
549 /* application-provided converters are not currently cached */
550 return createConverterFromFile(pArgs
, err
);
553 mySharedConverterData
= ucnv_getSharedConverterData(pArgs
->name
);
554 if (mySharedConverterData
== NULL
)
556 /*Not cached, we need to stream it in from file */
557 mySharedConverterData
= createConverterFromFile(pArgs
, err
);
558 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
562 else if (!pArgs
->onlyTestIsLoadable
)
564 /* share it with other library clients */
565 ucnv_shareConverterData(mySharedConverterData
);
570 /* The data for this converter was already in the cache. */
571 /* Update the reference counter on the shared data: one more client */
572 mySharedConverterData
->referenceCounter
++;
575 return mySharedConverterData
;
579 * Unload a non-algorithmic converter.
580 * It must be sharedData->referenceCounter != ~0
581 * and this function must be called inside umtx_lock(&cnvCacheMutex).
584 ucnv_unload(UConverterSharedData
*sharedData
) {
585 if(sharedData
!= NULL
) {
586 if (sharedData
->referenceCounter
> 0) {
587 sharedData
->referenceCounter
--;
590 if((sharedData
->referenceCounter
<= 0)&&(sharedData
->sharedDataCached
== FALSE
)) {
591 ucnv_deleteSharedConverterData(sharedData
);
597 ucnv_unloadSharedDataIfReady(UConverterSharedData
*sharedData
)
600 Checking whether it's an algorithic converter is okay
601 in multithreaded applications because the value never changes.
602 Don't check referenceCounter for any other value.
604 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= (uint32_t)~0) {
605 umtx_lock(&cnvCacheMutex
);
606 ucnv_unload(sharedData
);
607 umtx_unlock(&cnvCacheMutex
);
612 ucnv_incrementRefCount(UConverterSharedData
*sharedData
)
615 Checking whether it's an algorithic converter is okay
616 in multithreaded applications because the value never changes.
617 Don't check referenceCounter for any other value.
619 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= (uint32_t)~0) {
620 umtx_lock(&cnvCacheMutex
);
621 sharedData
->referenceCounter
++;
622 umtx_unlock(&cnvCacheMutex
);
627 * *pPieces must be initialized.
628 * The name without options will be copied to pPieces->cnvName.
629 * The locale and options will be copied to pPieces only if present in inName,
630 * otherwise the existing values in pPieces remain.
631 * *pArgs will be set to the pPieces values.
634 parseConverterOptions(const char *inName
,
635 UConverterNamePieces
*pPieces
,
636 UConverterLoadArgs
*pArgs
,
639 char *cnvName
= pPieces
->cnvName
;
644 pArgs
->locale
=pPieces
->locale
;
645 pArgs
->options
=pPieces
->options
;
647 /* copy the converter name itself to cnvName */
648 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
649 if (++len
>=UCNV_MAX_CONVERTER_NAME_LENGTH
) {
650 *err
= U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
651 pPieces
->cnvName
[0]=0;
658 pArgs
->name
=pPieces
->cnvName
;
660 /* parse options. No more name copying should occur. */
661 while((c
=*inName
)!=0) {
662 if(c
==UCNV_OPTION_SEP_CHAR
) {
666 /* inName is behind an option separator */
667 if(uprv_strncmp(inName
, "locale=", 7)==0) {
668 /* do not modify locale itself in case we have multiple locale options */
669 char *dest
=pPieces
->locale
;
671 /* copy the locale option value */
674 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
677 if(++len
>=ULOC_FULLNAME_CAPACITY
) {
678 *err
=U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
679 pPieces
->locale
[0]=0;
686 } else if(uprv_strncmp(inName
, "version=", 8)==0) {
687 /* copy the version option value into bits 3..0 of pPieces->options */
691 pArgs
->options
=(pPieces
->options
&=~UCNV_OPTION_VERSION
);
693 } else if((uint8_t)(c
-'0')<10) {
694 pArgs
->options
=pPieces
->options
=(pPieces
->options
&~UCNV_OPTION_VERSION
)|(uint32_t)(c
-'0');
697 } else if(uprv_strncmp(inName
, "swaplfnl", 8)==0) {
699 pArgs
->options
=(pPieces
->options
|=UCNV_OPTION_SWAP_LFNL
);
700 /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
702 /* ignore any other options until we define some */
703 while(((c
= *inName
++) != 0) && (c
!= UCNV_OPTION_SEP_CHAR
)) {
712 /*Logic determines if the converter is Algorithmic AND/OR cached
714 * -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
715 * -Get it from a Hashtable (Data=X, Cached=TRUE)
716 * -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
717 * -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
719 U_CFUNC UConverterSharedData
*
720 ucnv_loadSharedData(const char *converterName
,
721 UConverterNamePieces
*pPieces
,
722 UConverterLoadArgs
*pArgs
,
724 UConverterNamePieces stackPieces
;
725 UConverterLoadArgs stackArgs
;
726 UConverterSharedData
*mySharedConverterData
= NULL
;
727 UErrorCode internalErrorCode
= U_ZERO_ERROR
;
728 UBool mayContainOption
= TRUE
;
729 UBool checkForAlgorithmic
= TRUE
;
731 if (U_FAILURE (*err
)) {
735 if(pPieces
== NULL
) {
738 * Bad: We may set pArgs pointers to stackPieces fields
739 * which will be invalid after this function returns.
741 *err
= U_INTERNAL_PROGRAM_ERROR
;
744 pPieces
= &stackPieces
;
747 uprv_memset(&stackArgs
, 0, sizeof(stackArgs
));
748 stackArgs
.size
= (int32_t)sizeof(stackArgs
);
752 pPieces
->cnvName
[0] = 0;
753 pPieces
->locale
[0] = 0;
754 pPieces
->options
= 0;
756 pArgs
->name
= converterName
;
757 pArgs
->locale
= pPieces
->locale
;
758 pArgs
->options
= pPieces
->options
;
760 /* In case "name" is NULL we want to open the default converter. */
761 if (converterName
== NULL
) {
762 #if U_CHARSET_IS_UTF8
763 pArgs
->name
= "UTF-8";
764 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
766 /* Call ucnv_getDefaultName first to query the name from the OS. */
767 pArgs
->name
= ucnv_getDefaultName();
768 if (pArgs
->name
== NULL
) {
769 *err
= U_MISSING_RESOURCE_ERROR
;
772 mySharedConverterData
= (UConverterSharedData
*)gDefaultAlgorithmicSharedData
;
773 checkForAlgorithmic
= FALSE
;
774 mayContainOption
= gDefaultConverterContainsOption
;
775 /* the default converter name is already canonical */
778 else if(UCNV_FAST_IS_UTF8(converterName
)) {
779 /* fastpath for UTF-8 */
780 pArgs
->name
= "UTF-8";
781 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
784 /* separate the converter name from the options */
785 parseConverterOptions(converterName
, pPieces
, pArgs
, err
);
786 if (U_FAILURE(*err
)) {
787 /* Very bad name used. */
791 /* get the canonical converter name */
792 pArgs
->name
= ucnv_io_getConverterName(pArgs
->name
, &mayContainOption
, &internalErrorCode
);
793 if (U_FAILURE(internalErrorCode
) || pArgs
->name
== NULL
) {
795 * set the input name in case the converter was added
796 * without updating the alias table, or when there is no alias table
798 pArgs
->name
= pPieces
->cnvName
;
802 /* separate the converter name from the options */
803 if(mayContainOption
&& pArgs
->name
!= pPieces
->cnvName
) {
804 parseConverterOptions(pArgs
->name
, pPieces
, pArgs
, err
);
807 /* get the shared data for an algorithmic converter, if it is one */
808 if (checkForAlgorithmic
) {
809 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(pArgs
->name
);
811 if (mySharedConverterData
== NULL
)
813 /* it is a data-based converter, get its shared data. */
814 /* Hold the cnvCacheMutex through the whole process of checking the */
815 /* converter data cache, and adding new entries to the cache */
816 /* to prevent other threads from modifying the cache during the */
818 pArgs
->nestedLoads
=1;
821 umtx_lock(&cnvCacheMutex
);
822 mySharedConverterData
= ucnv_load(pArgs
, err
);
823 umtx_unlock(&cnvCacheMutex
);
824 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
830 return mySharedConverterData
;
834 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
836 UConverterNamePieces stackPieces
;
837 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
838 UConverterSharedData
*mySharedConverterData
;
840 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
842 if(U_SUCCESS(*err
)) {
843 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
845 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
847 myUConverter
= ucnv_createConverterFromSharedData(
848 myUConverter
, mySharedConverterData
,
852 if(U_SUCCESS(*err
)) {
853 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
858 /* exit with error */
859 UTRACE_EXIT_STATUS(*err
);
864 ucnv_canCreateConverter(const char *converterName
, UErrorCode
*err
) {
865 UConverter myUConverter
;
866 UConverterNamePieces stackPieces
;
867 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
868 UConverterSharedData
*mySharedConverterData
;
870 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
872 if(U_SUCCESS(*err
)) {
873 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "test if can open converter %s", converterName
);
875 stackArgs
.onlyTestIsLoadable
=TRUE
;
876 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
877 ucnv_createConverterFromSharedData(
878 &myUConverter
, mySharedConverterData
,
881 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
884 UTRACE_EXIT_STATUS(*err
);
885 return U_SUCCESS(*err
);
889 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
891 const char *locale
, uint32_t options
,
894 const UConverterSharedData
*sharedData
;
895 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
897 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
898 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
900 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
901 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
902 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
906 sharedData
= converterData
[type
];
908 Checking whether it's an algorithic converter is okay
909 in multithreaded applications because the value never changes.
910 Don't check referenceCounter for any other value.
912 if(sharedData
== NULL
|| sharedData
->referenceCounter
!= (uint32_t)~0) {
913 /* not a valid type, or not an algorithmic converter */
914 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
915 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
920 stackArgs
.options
= options
;
921 stackArgs
.locale
=locale
;
922 cnv
= ucnv_createConverterFromSharedData(
923 myUConverter
, (UConverterSharedData
*)sharedData
,
926 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
931 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
933 UConverter
*myUConverter
;
934 UConverterSharedData
*mySharedConverterData
;
935 UConverterNamePieces stackPieces
;
936 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
938 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
940 if(U_FAILURE(*err
)) {
941 UTRACE_EXIT_STATUS(*err
);
945 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
947 /* first, get the options out of the converterName string */
948 stackPieces
.cnvName
[0] = 0;
949 stackPieces
.locale
[0] = 0;
950 stackPieces
.options
= 0;
951 parseConverterOptions(converterName
, &stackPieces
, &stackArgs
, err
);
952 if (U_FAILURE(*err
)) {
953 /* Very bad name used. */
954 UTRACE_EXIT_STATUS(*err
);
957 stackArgs
.nestedLoads
=1;
958 stackArgs
.pkg
=packageName
;
960 /* open the data, unflatten the shared structure */
961 mySharedConverterData
= createConverterFromFile(&stackArgs
, err
);
963 if (U_FAILURE(*err
)) {
964 UTRACE_EXIT_STATUS(*err
);
968 /* create the actual converter */
969 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, &stackArgs
, err
);
971 if (U_FAILURE(*err
)) {
972 ucnv_close(myUConverter
);
973 UTRACE_EXIT_STATUS(*err
);
977 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
983 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
984 UConverterSharedData
*mySharedConverterData
,
985 UConverterLoadArgs
*pArgs
,
990 if(U_FAILURE(*err
)) {
991 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
994 if(myUConverter
== NULL
)
996 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
997 if(myUConverter
== NULL
)
999 *err
= U_MEMORY_ALLOCATION_ERROR
;
1000 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
1003 isCopyLocal
= FALSE
;
1008 /* initialize the converter */
1009 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
1010 myUConverter
->isCopyLocal
= isCopyLocal
;
1011 /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
1012 myUConverter
->sharedData
= mySharedConverterData
;
1013 myUConverter
->options
= pArgs
->options
;
1014 if(!pArgs
->onlyTestIsLoadable
) {
1015 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
1016 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_DEFAULT_CALLBACK
;
1017 myUConverter
->fromUCharErrorBehaviour
= UCNV_FROM_U_DEFAULT_CALLBACK
;
1018 myUConverter
->toUnicodeStatus
= mySharedConverterData
->toUnicodeStatus
;
1019 myUConverter
->maxBytesPerUChar
= mySharedConverterData
->staticData
->maxBytesPerChar
;
1020 myUConverter
->subChar1
= mySharedConverterData
->staticData
->subChar1
;
1021 myUConverter
->subCharLen
= mySharedConverterData
->staticData
->subCharLen
;
1022 myUConverter
->subChars
= (uint8_t *)myUConverter
->subUChars
;
1023 uprv_memcpy(myUConverter
->subChars
, mySharedConverterData
->staticData
->subChar
, myUConverter
->subCharLen
);
1024 myUConverter
->toUCallbackReason
= UCNV_ILLEGAL
; /* default reason to invoke (*fromCharErrorBehaviour) */
1027 if(mySharedConverterData
->impl
->open
!= NULL
) {
1028 mySharedConverterData
->impl
->open(myUConverter
, pArgs
, err
);
1029 if(U_FAILURE(*err
) && !pArgs
->onlyTestIsLoadable
) {
1030 /* don't ucnv_close() if onlyTestIsLoadable because not fully initialized */
1031 ucnv_close(myUConverter
);
1036 return myUConverter
;
1039 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
1041 U_CAPI
int32_t U_EXPORT2
1044 UConverterSharedData
*mySharedData
= NULL
;
1046 int32_t tableDeletedNum
= 0;
1047 const UHashElement
*e
;
1048 /*UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;*/
1049 int32_t i
, remaining
;
1051 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
1053 /* Close the default converter without creating a new one so that everything will be flushed. */
1054 u_flushDefaultConverter();
1056 /*if shared data hasn't even been lazy evaluated yet
1059 if (SHARED_DATA_HASHTABLE
== NULL
) {
1060 UTRACE_EXIT_VALUE((int32_t)0);
1064 /*creates an enumeration to iterate through every element in the
1067 * Synchronization: holding cnvCacheMutex will prevent any other thread from
1068 * accessing or modifying the hash table during the iteration.
1069 * The reference count of an entry may be decremented by
1070 * ucnv_close while the iteration is in process, but this is
1071 * benign. It can't be incremented (in ucnv_createConverter())
1072 * because the sequence of looking up in the cache + incrementing
1073 * is protected by cnvCacheMutex.
1075 umtx_lock(&cnvCacheMutex
);
1077 * double loop: A delta/extension-only converter has a pointer to its base table's
1078 * shared data; the first iteration of the outer loop may see the delta converter
1079 * before the base converter, and unloading the delta converter may get the base
1080 * converter's reference counter down to 0.
1086 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
1088 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
1089 /*deletes only if reference counter == 0 */
1090 if (mySharedData
->referenceCounter
== 0)
1094 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
1096 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
1097 mySharedData
->sharedDataCached
= FALSE
;
1098 ucnv_deleteSharedConverterData (mySharedData
);
1103 } while(++i
== 1 && remaining
> 0);
1104 umtx_unlock(&cnvCacheMutex
);
1106 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
1108 UTRACE_EXIT_VALUE(tableDeletedNum
);
1109 return tableDeletedNum
;
1112 /* available converters list --------------------------------------------------- */
1114 static UBool
haveAvailableConverterList(UErrorCode
*pErrorCode
) {
1116 UMTX_CHECK(&cnvCacheMutex
, (gAvailableConverters
== NULL
), needInit
);
1118 UConverter tempConverter
;
1119 UEnumeration
*allConvEnum
= NULL
;
1121 uint16_t localConverterCount
;
1122 uint16_t allConverterCount
;
1123 UErrorCode localStatus
;
1124 const char *converterName
;
1125 const char **localConverterList
;
1127 allConvEnum
= ucnv_openAllNames(pErrorCode
);
1128 allConverterCount
= uenum_count(allConvEnum
, pErrorCode
);
1129 if (U_FAILURE(*pErrorCode
)) {
1133 /* We can't have more than "*converterTable" converters to open */
1134 localConverterList
= (const char **) uprv_malloc(allConverterCount
* sizeof(char*));
1135 if (!localConverterList
) {
1136 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1140 /* Open the default converter to make sure that it has first dibs in the hash table. */
1141 localStatus
= U_ZERO_ERROR
;
1142 ucnv_close(ucnv_createConverter(&tempConverter
, NULL
, &localStatus
));
1144 localConverterCount
= 0;
1146 for (idx
= 0; idx
< allConverterCount
; idx
++) {
1147 localStatus
= U_ZERO_ERROR
;
1148 converterName
= uenum_next(allConvEnum
, NULL
, &localStatus
);
1149 if (ucnv_canCreateConverter(converterName
, &localStatus
)) {
1150 localConverterList
[localConverterCount
++] = converterName
;
1153 uenum_close(allConvEnum
);
1155 umtx_lock(&cnvCacheMutex
);
1156 if (gAvailableConverters
== NULL
) {
1157 gAvailableConverterCount
= localConverterCount
;
1158 gAvailableConverters
= localConverterList
;
1159 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1162 uprv_free((char **)localConverterList
);
1164 umtx_unlock(&cnvCacheMutex
);
1170 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
) {
1171 if (haveAvailableConverterList(pErrorCode
)) {
1172 return gAvailableConverterCount
;
1177 U_CFUNC
const char *
1178 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
) {
1179 if (haveAvailableConverterList(pErrorCode
)) {
1180 if (n
< gAvailableConverterCount
) {
1181 return gAvailableConverters
[n
];
1183 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
1188 /* default converter name --------------------------------------------------- */
1190 #if !U_CHARSET_IS_UTF8
1192 Copy the canonical converter name.
1193 ucnv_getDefaultName must be thread safe, which can call this function.
1195 ucnv_setDefaultName calls this function and it doesn't have to be
1196 thread safe because there is no reliable/safe way to reset the
1197 converter in use in all threads. If you did reset the converter, you
1198 would not be sure that retrieving a default converter for one string
1199 would be the same type of default converter for a successive string.
1200 Since the name is a returned via ucnv_getDefaultName without copying,
1201 you shouldn't be modifying or deleting the string from a separate thread.
1204 internalSetName(const char *name
, UErrorCode
*status
) {
1205 UConverterNamePieces stackPieces
;
1206 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
1207 int32_t length
=(int32_t)(uprv_strlen(name
));
1208 UBool containsOption
= (UBool
)(uprv_strchr(name
, UCNV_OPTION_SEP_CHAR
) != NULL
);
1209 const UConverterSharedData
*algorithmicSharedData
;
1211 stackArgs
.name
= name
;
1212 if(containsOption
) {
1213 stackPieces
.cnvName
[0] = 0;
1214 stackPieces
.locale
[0] = 0;
1215 stackPieces
.options
= 0;
1216 parseConverterOptions(name
, &stackPieces
, &stackArgs
, status
);
1217 if(U_FAILURE(*status
)) {
1221 algorithmicSharedData
= getAlgorithmicTypeFromName(stackArgs
.name
);
1223 umtx_lock(&cnvCacheMutex
);
1225 gDefaultAlgorithmicSharedData
= algorithmicSharedData
;
1226 gDefaultConverterContainsOption
= containsOption
;
1227 uprv_memcpy(gDefaultConverterNameBuffer
, name
, length
);
1228 gDefaultConverterNameBuffer
[length
]=0;
1230 /* gDefaultConverterName MUST be the last global var set by this function. */
1231 /* It is the variable checked in ucnv_getDefaultName() to see if initialization is required. */
1232 gDefaultConverterName
= gDefaultConverterNameBuffer
;
1234 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1236 umtx_unlock(&cnvCacheMutex
);
1241 * In order to be really thread-safe, the get function would have to take
1242 * a buffer parameter and copy the current string inside a mutex block.
1243 * This implementation only tries to be really thread-safe while
1245 * It assumes that setting a pointer is atomic.
1248 U_CAPI
const char* U_EXPORT2
1249 ucnv_getDefaultName() {
1250 #if U_CHARSET_IS_UTF8
1253 /* local variable to be thread-safe */
1257 Multiple calls to ucnv_getDefaultName must be thread safe,
1258 but ucnv_setDefaultName is not thread safe.
1260 UMTX_CHECK(&cnvCacheMutex
, gDefaultConverterName
, name
);
1262 UErrorCode errorCode
= U_ZERO_ERROR
;
1263 UConverter
*cnv
= NULL
;
1265 name
= uprv_getDefaultCodepage();
1267 /* if the name is there, test it out and get the canonical name with options */
1269 cnv
= ucnv_open(name
, &errorCode
);
1270 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1271 name
= ucnv_getName(cnv
, &errorCode
);
1275 if(name
== NULL
|| name
[0] == 0
1276 || U_FAILURE(errorCode
) || cnv
== NULL
1277 || uprv_strlen(name
)>=sizeof(gDefaultConverterNameBuffer
))
1279 /* Panic time, let's use a fallback. */
1280 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1282 /* there is no 'algorithmic' converter for EBCDIC */
1283 #elif U_PLATFORM == U_PF_OS390
1284 name
= "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING
;
1286 name
= "ibm-37_P100-1995";
1290 internalSetName(name
, &errorCode
);
1292 /* The close may make the current name go away. */
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 !U_CHARSET_IS_UTF8
1307 if(converterName
==NULL
) {
1308 /* reset to the default codepage */
1309 gDefaultConverterName
=NULL
;
1311 UErrorCode errorCode
= U_ZERO_ERROR
;
1312 UConverter
*cnv
= NULL
;
1313 const char *name
= NULL
;
1315 /* if the name is there, test it out and get the canonical name with options */
1316 cnv
= ucnv_open(converterName
, &errorCode
);
1317 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1318 name
= ucnv_getName(cnv
, &errorCode
);
1321 if(U_SUCCESS(errorCode
) && name
!=NULL
) {
1322 internalSetName(name
, &errorCode
);
1324 /* else this converter is bad to use. Don't change it to a bad value. */
1326 /* The close may make the current name go away. */
1329 /* reset the converter cache */
1330 u_flushDefaultConverter();
1335 /* data swapping ------------------------------------------------------------ */
1337 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
1339 #if !UCONFIG_NO_LEGACY_CONVERSION
1341 U_CAPI
int32_t U_EXPORT2
1342 ucnv_swap(const UDataSwapper
*ds
,
1343 const void *inData
, int32_t length
, void *outData
,
1344 UErrorCode
*pErrorCode
) {
1345 const UDataInfo
*pInfo
;
1348 const uint8_t *inBytes
;
1351 uint32_t offset
, count
, staticDataSize
;
1354 const UConverterStaticData
*inStaticData
;
1355 UConverterStaticData
*outStaticData
;
1357 const _MBCSHeader
*inMBCSHeader
;
1358 _MBCSHeader
*outMBCSHeader
;
1359 _MBCSHeader mbcsHeader
;
1360 uint32_t mbcsHeaderLength
;
1361 UBool noFromU
=FALSE
;
1365 int32_t maxFastUChar
, mbcsIndexLength
;
1367 const int32_t *inExtIndexes
;
1370 /* udata_swapDataHeader checks the arguments */
1371 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1372 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1376 /* check data format and format version */
1377 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1379 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1380 pInfo
->dataFormat
[1]==0x6e &&
1381 pInfo
->dataFormat
[2]==0x76 &&
1382 pInfo
->dataFormat
[3]==0x74 &&
1383 pInfo
->formatVersion
[0]==6 &&
1384 pInfo
->formatVersion
[1]>=2
1386 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",
1387 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1388 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1389 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1390 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1394 inBytes
=(const uint8_t *)inData
+headerSize
;
1395 outBytes
=(uint8_t *)outData
+headerSize
;
1397 /* read the initial UConverterStaticData structure after the UDataInfo header */
1398 inStaticData
=(const UConverterStaticData
*)inBytes
;
1399 outStaticData
=(UConverterStaticData
*)outBytes
;
1402 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1405 if( length
<(int32_t)sizeof(UConverterStaticData
) ||
1406 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1408 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1410 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1416 /* swap the static data */
1417 if(inStaticData
!=outStaticData
) {
1418 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1421 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1422 &outStaticData
->structSize
, pErrorCode
);
1423 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1424 &outStaticData
->codepage
, pErrorCode
);
1426 ds
->swapInvChars(ds
, inStaticData
->name
, (int32_t)uprv_strlen(inStaticData
->name
),
1427 outStaticData
->name
, pErrorCode
);
1428 if(U_FAILURE(*pErrorCode
)) {
1429 udata_printError(ds
, "ucnv_swap(): error swapping converter name\n");
1434 inBytes
+=staticDataSize
;
1435 outBytes
+=staticDataSize
;
1437 length
-=(int32_t)staticDataSize
;
1440 /* check for supported conversionType values */
1441 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1442 /* swap MBCS data */
1443 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1444 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1446 if(0<=length
&& length
<(int32_t)sizeof(_MBCSHeader
)) {
1447 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1449 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1452 if(inMBCSHeader
->version
[0]==4 && inMBCSHeader
->version
[1]>=1) {
1453 mbcsHeaderLength
=MBCS_HEADER_V4_LENGTH
;
1454 } else if(inMBCSHeader
->version
[0]==5 && inMBCSHeader
->version
[1]>=3 &&
1455 ((mbcsHeader
.options
=ds
->readUInt32(inMBCSHeader
->options
))&
1456 MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0
1458 mbcsHeaderLength
=mbcsHeader
.options
&MBCS_OPT_LENGTH_MASK
;
1459 noFromU
=(UBool
)((mbcsHeader
.options
&MBCS_OPT_NO_FROM_U
)!=0);
1461 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1462 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1463 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1467 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1468 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1469 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1470 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1471 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1472 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1473 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1474 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1475 /* mbcsHeader.options have been read above */
1477 extOffset
=(int32_t)(mbcsHeader
.flags
>>8);
1478 outputType
=(uint8_t)mbcsHeader
.flags
;
1479 if(noFromU
&& outputType
==MBCS_OUTPUT_1
) {
1480 udata_printError(ds
, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
1481 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1485 /* make sure that the output type is known */
1486 switch(outputType
) {
1491 case MBCS_OUTPUT_3_EUC
:
1492 case MBCS_OUTPUT_4_EUC
:
1493 case MBCS_OUTPUT_2_SISO
:
1494 case MBCS_OUTPUT_EXT_ONLY
:
1498 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1500 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1504 /* calculate the length of the MBCS data */
1507 * utf8Friendly MBCS files (mbcsHeader.version 4.3)
1508 * contain an additional mbcsIndex table:
1509 * uint16_t[(maxFastUChar+1)>>6];
1510 * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
1514 if( outputType
!=MBCS_OUTPUT_EXT_ONLY
&& outputType
!=MBCS_OUTPUT_1
&&
1515 mbcsHeader
.version
[1]>=3 && (maxFastUChar
=mbcsHeader
.version
[2])!=0
1517 maxFastUChar
=(maxFastUChar
<<8)|0xff;
1518 mbcsIndexLength
=((maxFastUChar
+1)>>6)*2; /* number of bytes */
1522 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsIndexLength
);
1524 size
+=(int32_t)mbcsHeader
.fromUBytesLength
;
1527 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1530 /* there is extension data after the base data, see ucnv_ext.h */
1531 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1532 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1534 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1538 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1539 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1544 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1546 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1550 /* copy the data for inaccessible bytes */
1551 if(inBytes
!=outBytes
) {
1552 uprv_memcpy(outBytes
, inBytes
, size
);
1555 /* swap the MBCSHeader, except for the version field */
1556 count
=mbcsHeaderLength
*4;
1557 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, count
-4,
1558 &outMBCSHeader
->countStates
, pErrorCode
);
1560 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1562 * extension-only file,
1563 * contains a base name instead of normal base table data
1566 /* swap the base name, between the header and the extension data */
1567 const char *inBaseName
=(const char *)inBytes
+count
;
1568 char *outBaseName
=(char *)outBytes
+count
;
1569 ds
->swapInvChars(ds
, inBaseName
, (int32_t)uprv_strlen(inBaseName
),
1570 outBaseName
, pErrorCode
);
1572 /* normal file with base table data */
1574 /* swap the state table, 1kB per state */
1576 count
=mbcsHeader
.countStates
*1024;
1577 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1578 outBytes
+offset
, pErrorCode
);
1580 /* swap the toUFallbacks[] */
1582 count
=mbcsHeader
.countToUFallbacks
*8;
1583 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1584 outBytes
+offset
, pErrorCode
);
1586 /* swap the unicodeCodeUnits[] */
1587 offset
=mbcsHeader
.offsetToUCodeUnits
;
1588 count
=mbcsHeader
.offsetFromUTable
-offset
;
1589 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1590 outBytes
+offset
, pErrorCode
);
1592 /* offset to the stage 1 table, independent of the outputType */
1593 offset
=mbcsHeader
.offsetFromUTable
;
1595 if(outputType
==MBCS_OUTPUT_1
) {
1596 /* SBCS: swap the fromU tables, all 16 bits wide */
1597 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1598 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1599 outBytes
+offset
, pErrorCode
);
1601 /* otherwise: swap the stage tables separately */
1603 /* stage 1 table: uint16_t[0x440 or 0x40] */
1604 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1605 count
=0x440*2; /* for all of Unicode */
1607 count
=0x40*2; /* only BMP */
1609 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1610 outBytes
+offset
, pErrorCode
);
1612 /* stage 2 table: uint32_t[] */
1614 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1615 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1616 outBytes
+offset
, pErrorCode
);
1618 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1619 offset
=mbcsHeader
.offsetFromUBytes
;
1620 count
= noFromU
? 0 : mbcsHeader
.fromUBytesLength
;
1621 switch(outputType
) {
1623 case MBCS_OUTPUT_3_EUC
:
1624 case MBCS_OUTPUT_2_SISO
:
1625 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1626 outBytes
+offset
, pErrorCode
);
1629 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1630 outBytes
+offset
, pErrorCode
);
1633 /* just uint8_t[], nothing to swap */
1637 if(mbcsIndexLength
!=0) {
1639 count
=mbcsIndexLength
;
1640 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1641 outBytes
+offset
, pErrorCode
);
1647 /* swap the extension data */
1649 outBytes
+=extOffset
;
1651 /* swap toUTable[] */
1652 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1653 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1654 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1656 /* swap toUUChars[] */
1657 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1658 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1659 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1661 /* swap fromUTableUChars[] */
1662 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1663 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1664 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1666 /* swap fromUTableValues[] */
1667 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1668 /* same length as for fromUTableUChars[] */
1669 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1671 /* no need to swap fromUBytes[] */
1673 /* swap fromUStage12[] */
1674 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1675 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1676 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1678 /* swap fromUStage3[] */
1679 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1680 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1681 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1683 /* swap fromUStage3b[] */
1684 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1685 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1686 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1688 /* swap indexes[] */
1689 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1690 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1694 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1695 inStaticData
->conversionType
);
1696 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1700 return headerSize
+(int32_t)staticDataSize
+size
;
1703 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */