2 ********************************************************************
4 * Copyright (c) 1996-2016, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 ********************************************************************
10 * Defines functions that are used in the creation/initialization/deletion
11 * of converters and related structures.
12 * uses uconv_io.h routines to access disk information
13 * is used by ucnv.h to implement public API create/delete/flushCache routines
14 * Modification History:
16 * Date Name Description
18 * 06/20/2000 helena OS/400 port changes; mostly typecast.
19 * 06/29/2000 helena Major rewrite of the callback interface.
22 #include "unicode/utypes.h"
24 #if !UCONFIG_NO_CONVERSION
26 #include "unicode/putil.h"
27 #include "unicode/udata.h"
28 #include "unicode/ucnv.h"
29 #include "unicode/uloc.h"
50 extern void UCNV_DEBUG_LOG(char *what
, char *who
, void *p
, int l
);
51 #define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
53 # define UCNV_DEBUG_LOG(x,y,z)
56 static const UConverterSharedData
* const
57 converterData
[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
]={
60 #if UCONFIG_NO_LEGACY_CONVERSION
67 &_UTF8Data
, &_UTF16BEData
, &_UTF16LEData
,
68 #if UCONFIG_ONLY_HTML_CONVERSION
71 &_UTF32BEData
, &_UTF32LEData
,
75 #if UCONFIG_NO_LEGACY_CONVERSION
81 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
82 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
83 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
86 &_LMBCSData1
,&_LMBCSData2
, &_LMBCSData3
, &_LMBCSData4
, &_LMBCSData5
, &_LMBCSData6
,
87 &_LMBCSData8
,&_LMBCSData11
,&_LMBCSData16
,&_LMBCSData17
,&_LMBCSData18
,&_LMBCSData19
,
91 #if UCONFIG_ONLY_HTML_CONVERSION
98 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
105 #if UCONFIG_ONLY_HTML_CONVERSION
106 NULL
, NULL
, &_UTF16Data
, NULL
, NULL
, NULL
,
108 &_UTF7Data
, &_Bocu1Data
, &_UTF16Data
, &_UTF32Data
, &_CESU8Data
, &_IMAPData
,
111 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_ONLY_HTML_CONVERSION
118 /* Please keep this in binary sorted order for getAlgorithmicTypeFromName.
119 Also the name should be in lower case and all spaces, dashes and underscores
124 const UConverterType type
;
125 } const cnvNameType
[] = {
126 #if !UCONFIG_ONLY_HTML_CONVERSION
127 { "bocu1", UCNV_BOCU1
},
128 { "cesu8", UCNV_CESU8
},
130 #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
133 #if !UCONFIG_ONLY_HTML_CONVERSION
134 { "imapmailboxname", UCNV_IMAP_MAILBOX
},
136 #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
137 { "iscii", UCNV_ISCII
},
139 #if !UCONFIG_NO_LEGACY_CONVERSION
140 { "iso2022", UCNV_ISO_2022
},
142 { "iso88591", UCNV_LATIN_1
},
143 #if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
144 { "lmbcs1", UCNV_LMBCS_1
},
145 { "lmbcs11",UCNV_LMBCS_11
},
146 { "lmbcs16",UCNV_LMBCS_16
},
147 { "lmbcs17",UCNV_LMBCS_17
},
148 { "lmbcs18",UCNV_LMBCS_18
},
149 { "lmbcs19",UCNV_LMBCS_19
},
150 { "lmbcs2", UCNV_LMBCS_2
},
151 { "lmbcs3", UCNV_LMBCS_3
},
152 { "lmbcs4", UCNV_LMBCS_4
},
153 { "lmbcs5", UCNV_LMBCS_5
},
154 { "lmbcs6", UCNV_LMBCS_6
},
155 { "lmbcs8", UCNV_LMBCS_8
},
157 #if !UCONFIG_ONLY_HTML_CONVERSION
158 { "scsu", UCNV_SCSU
},
160 { "usascii", UCNV_US_ASCII
},
161 { "utf16", UCNV_UTF16
},
162 { "utf16be", UCNV_UTF16_BigEndian
},
163 { "utf16le", UCNV_UTF16_LittleEndian
},
165 { "utf16oppositeendian", UCNV_UTF16_LittleEndian
},
166 { "utf16platformendian", UCNV_UTF16_BigEndian
},
168 { "utf16oppositeendian", UCNV_UTF16_BigEndian
},
169 { "utf16platformendian", UCNV_UTF16_LittleEndian
},
171 #if !UCONFIG_ONLY_HTML_CONVERSION
172 { "utf32", UCNV_UTF32
},
173 { "utf32be", UCNV_UTF32_BigEndian
},
174 { "utf32le", UCNV_UTF32_LittleEndian
},
176 { "utf32oppositeendian", UCNV_UTF32_LittleEndian
},
177 { "utf32platformendian", UCNV_UTF32_BigEndian
},
179 { "utf32oppositeendian", UCNV_UTF32_BigEndian
},
180 { "utf32platformendian", UCNV_UTF32_LittleEndian
},
183 #if !UCONFIG_ONLY_HTML_CONVERSION
184 { "utf7", UCNV_UTF7
},
186 { "utf8", UCNV_UTF8
},
187 #if !UCONFIG_ONLY_HTML_CONVERSION
188 { "x11compoundtext", UCNV_COMPOUND_TEXT
}
193 /*initializes some global variables */
194 static UHashtable
*SHARED_DATA_HASHTABLE
= NULL
;
195 static UMutex cnvCacheMutex
= U_MUTEX_INITIALIZER
; /* Mutex for synchronizing cnv cache access. */
196 /* Note: the global mutex is used for */
197 /* reference count updates. */
199 static const char **gAvailableConverters
= NULL
;
200 static uint16_t gAvailableConverterCount
= 0;
201 static icu::UInitOnce gAvailableConvertersInitOnce
= U_INITONCE_INITIALIZER
;
203 #if !U_CHARSET_IS_UTF8
205 /* This contains the resolved converter name. So no further alias lookup is needed again. */
206 static char gDefaultConverterNameBuffer
[UCNV_MAX_CONVERTER_NAME_LENGTH
+ 1]; /* +1 for NULL */
207 static const char *gDefaultConverterName
= NULL
;
210 If the default converter is an algorithmic converter, this is the cached value.
211 We don't cache a full UConverter and clone it because ucnv_clone doesn't have
212 less overhead than an algorithmic open. We don't cache non-algorithmic converters
213 because ucnv_flushCache must be able to unload the default converter and its table.
215 static const UConverterSharedData
*gDefaultAlgorithmicSharedData
= NULL
;
217 /* Does gDefaultConverterName have a converter option and require extra parsing? */
218 static UBool gDefaultConverterContainsOption
;
220 #endif /* !U_CHARSET_IS_UTF8 */
222 static const char DATA_TYPE
[] = "cnv";
224 /* ucnv_flushAvailableConverterCache. This is only called from ucnv_cleanup().
225 * If it is ever to be called from elsewhere, synchronization
226 * will need to be considered.
229 ucnv_flushAvailableConverterCache() {
230 gAvailableConverterCount
= 0;
231 if (gAvailableConverters
) {
232 uprv_free((char **)gAvailableConverters
);
233 gAvailableConverters
= NULL
;
235 gAvailableConvertersInitOnce
.reset();
238 /* ucnv_cleanup - delete all storage held by the converter cache, except any */
239 /* in use by open converters. */
240 /* Not thread safe. */
241 /* Not supported API. */
242 static UBool U_CALLCONV
ucnv_cleanup(void) {
244 if (SHARED_DATA_HASHTABLE
!= NULL
&& uhash_count(SHARED_DATA_HASHTABLE
) == 0) {
245 uhash_close(SHARED_DATA_HASHTABLE
);
246 SHARED_DATA_HASHTABLE
= NULL
;
249 /* Isn't called from flushCache because other threads may have preexisting references to the table. */
250 ucnv_flushAvailableConverterCache();
252 #if !U_CHARSET_IS_UTF8
253 gDefaultConverterName
= NULL
;
254 gDefaultConverterNameBuffer
[0] = 0;
255 gDefaultConverterContainsOption
= FALSE
;
256 gDefaultAlgorithmicSharedData
= NULL
;
259 return (SHARED_DATA_HASHTABLE
== NULL
);
262 static UBool U_CALLCONV
263 isCnvAcceptable(void * /*context*/,
264 const char * /*type*/, const char * /*name*/,
265 const UDataInfo
*pInfo
) {
268 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
269 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
270 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
271 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
272 pInfo
->dataFormat
[1]==0x6e &&
273 pInfo
->dataFormat
[2]==0x76 &&
274 pInfo
->dataFormat
[3]==0x74 &&
275 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
279 * Un flatten shared data from a UDATA..
281 static UConverterSharedData
*
282 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
284 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
285 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
286 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
287 UConverterSharedData
*data
;
288 UConverterType type
= (UConverterType
)source
->conversionType
;
290 if(U_FAILURE(*status
))
293 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
294 converterData
[type
] == NULL
||
295 !converterData
[type
]->isReferenceCounted
||
296 converterData
[type
]->referenceCounter
!= 1 ||
297 source
->structSize
!= sizeof(UConverterStaticData
))
299 *status
= U_INVALID_TABLE_FORMAT
;
303 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
305 *status
= U_MEMORY_ALLOCATION_ERROR
;
309 /* copy initial values from the static structure for this type */
310 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
312 data
->staticData
= source
;
314 data
->sharedDataCached
= FALSE
;
316 /* fill in fields from the loaded data */
317 data
->dataMemory
= (void*)pData
; /* for future use */
319 if(data
->impl
->load
!= NULL
) {
320 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
321 if(U_FAILURE(*status
)) {
329 /*Takes an alias name gets an actual converter file name
330 *goes to disk and opens it.
331 *allocates the memory and returns a new UConverter object
333 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
336 UConverterSharedData
*sharedData
;
338 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
340 if (U_FAILURE (*err
)) {
341 UTRACE_EXIT_STATUS(*err
);
345 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
347 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
350 UTRACE_EXIT_STATUS(*err
);
354 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
358 UTRACE_EXIT_STATUS(*err
);
363 * TODO Store pkg in a field in the shared data so that delta-only converters
364 * can load base converters from the same package.
365 * If the pkg name is longer than the field, then either do not load the converter
366 * in the first place, or just set the pkg field to "".
369 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
373 /*returns a converter type from a string
375 static const UConverterSharedData
*
376 getAlgorithmicTypeFromName(const char *realName
)
378 uint32_t mid
, start
, limit
;
381 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
383 /* Lower case and remove ignoreable characters. */
384 ucnv_io_stripForCompare(strippedName
, realName
);
386 /* do a binary search for the alias */
388 limit
= UPRV_LENGTHOF(cnvNameType
);
390 lastMid
= UINT32_MAX
;
393 mid
= (uint32_t)((start
+ limit
) / 2);
394 if (lastMid
== mid
) { /* Have we moved? */
395 break; /* We haven't moved, and it wasn't found. */
398 result
= uprv_strcmp(strippedName
, cnvNameType
[mid
].name
);
402 } else if (result
> 0) {
405 return converterData
[cnvNameType
[mid
].type
];
413 * Based on the number of known converters, this determines how many times larger
414 * the shared data hash table should be. When on small platforms, or just a couple
415 * of converters are used, this number should be 2. When memory is plentiful, or
416 * when ucnv_countAvailable is ever used with a lot of available converters,
418 * Larger numbers reduce the number of hash collisions, but use more memory.
420 #define UCNV_CACHE_LOAD_FACTOR 2
422 /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
423 /* Will always be called with the cnvCacheMutex alrady being held */
424 /* by the calling function. */
425 /* Stores the shared data in the SHARED_DATA_HASHTABLE
426 * @param data The shared data
429 ucnv_shareConverterData(UConverterSharedData
* data
)
431 UErrorCode err
= U_ZERO_ERROR
;
432 /*Lazy evaluates the Hashtable itself */
433 /*void *sanity = NULL;*/
435 if (SHARED_DATA_HASHTABLE
== NULL
)
437 SHARED_DATA_HASHTABLE
= uhash_openSize(uhash_hashChars
, uhash_compareChars
, NULL
,
438 ucnv_io_countKnownConverters(&err
)*UCNV_CACHE_LOAD_FACTOR
,
440 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
446 /* ### check to see if the element is not already there! */
449 sanity = ucnv_getSharedConverterData (data->staticData->name);
452 UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
454 UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
458 data
->sharedDataCached
= TRUE
;
460 uhash_put(SHARED_DATA_HASHTABLE
,
461 (void*) data
->staticData
->name
, /* Okay to cast away const as long as
462 keyDeleter == NULL */
465 UCNV_DEBUG_LOG("put", data
->staticData
->name
,data
);
469 /* Look up a converter name in the shared data cache. */
470 /* cnvCacheMutex must be held by the caller to protect the hash table. */
471 /* gets the shared data from the SHARED_DATA_HASHTABLE (might return NULL if it isn't there)
472 * @param name The name of the shared data
473 * @return the shared data from the SHARED_DATA_HASHTABLE
475 static UConverterSharedData
*
476 ucnv_getSharedConverterData(const char *name
)
478 /*special case when no Table has yet been created we return NULL */
479 if (SHARED_DATA_HASHTABLE
== NULL
)
485 UConverterSharedData
*rc
;
487 rc
= (UConverterSharedData
*)uhash_get(SHARED_DATA_HASHTABLE
, name
);
488 UCNV_DEBUG_LOG("get",name
,rc
);
493 /*frees the string of memory blocks associates with a sharedConverter
494 *if and only if the referenceCounter == 0
496 /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
497 * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
499 * otherwise returns FALSE
500 * @param sharedConverterData The shared data
501 * @return if not it frees all the memory stemming from sharedConverterData and
502 * returns TRUE, otherwise returns FALSE
505 ucnv_deleteSharedConverterData(UConverterSharedData
* deadSharedData
)
507 UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD
);
508 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "unload converter %s shared data %p", deadSharedData
->staticData
->name
, deadSharedData
);
510 if (deadSharedData
->referenceCounter
> 0) {
511 UTRACE_EXIT_VALUE((int32_t)FALSE
);
515 if (deadSharedData
->impl
->unload
!= NULL
) {
516 deadSharedData
->impl
->unload(deadSharedData
);
519 if(deadSharedData
->dataMemory
!= NULL
)
521 UDataMemory
*data
= (UDataMemory
*)deadSharedData
->dataMemory
;
525 uprv_free(deadSharedData
);
527 UTRACE_EXIT_VALUE((int32_t)TRUE
);
532 * Load a non-algorithmic converter.
533 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
535 UConverterSharedData
*
536 ucnv_load(UConverterLoadArgs
*pArgs
, UErrorCode
*err
) {
537 UConverterSharedData
*mySharedConverterData
;
539 if(err
== NULL
|| U_FAILURE(*err
)) {
543 if(pArgs
->pkg
!= NULL
&& *pArgs
->pkg
!= 0) {
544 /* application-provided converters are not currently cached */
545 return createConverterFromFile(pArgs
, err
);
548 mySharedConverterData
= ucnv_getSharedConverterData(pArgs
->name
);
549 if (mySharedConverterData
== NULL
)
551 /*Not cached, we need to stream it in from file */
552 mySharedConverterData
= createConverterFromFile(pArgs
, err
);
553 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
557 else if (!pArgs
->onlyTestIsLoadable
)
559 /* share it with other library clients */
560 ucnv_shareConverterData(mySharedConverterData
);
565 /* The data for this converter was already in the cache. */
566 /* Update the reference counter on the shared data: one more client */
567 mySharedConverterData
->referenceCounter
++;
570 return mySharedConverterData
;
574 * Unload a non-algorithmic converter.
575 * It must be sharedData->isReferenceCounted
576 * and this function must be called inside umtx_lock(&cnvCacheMutex).
579 ucnv_unload(UConverterSharedData
*sharedData
) {
580 if(sharedData
!= NULL
) {
581 if (sharedData
->referenceCounter
> 0) {
582 sharedData
->referenceCounter
--;
585 if((sharedData
->referenceCounter
<= 0)&&(sharedData
->sharedDataCached
== FALSE
)) {
586 ucnv_deleteSharedConverterData(sharedData
);
592 ucnv_unloadSharedDataIfReady(UConverterSharedData
*sharedData
)
594 if(sharedData
!= NULL
&& sharedData
->isReferenceCounted
) {
595 umtx_lock(&cnvCacheMutex
);
596 ucnv_unload(sharedData
);
597 umtx_unlock(&cnvCacheMutex
);
602 ucnv_incrementRefCount(UConverterSharedData
*sharedData
)
604 if(sharedData
!= NULL
&& sharedData
->isReferenceCounted
) {
605 umtx_lock(&cnvCacheMutex
);
606 sharedData
->referenceCounter
++;
607 umtx_unlock(&cnvCacheMutex
);
612 * *pPieces must be initialized.
613 * The name without options will be copied to pPieces->cnvName.
614 * The locale and options will be copied to pPieces only if present in inName,
615 * otherwise the existing values in pPieces remain.
616 * *pArgs will be set to the pPieces values.
619 parseConverterOptions(const char *inName
,
620 UConverterNamePieces
*pPieces
,
621 UConverterLoadArgs
*pArgs
,
624 char *cnvName
= pPieces
->cnvName
;
629 pArgs
->locale
=pPieces
->locale
;
630 pArgs
->options
=pPieces
->options
;
632 /* copy the converter name itself to cnvName */
633 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
634 if (++len
>=UCNV_MAX_CONVERTER_NAME_LENGTH
) {
635 *err
= U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
636 pPieces
->cnvName
[0]=0;
643 pArgs
->name
=pPieces
->cnvName
;
645 /* parse options. No more name copying should occur. */
646 while((c
=*inName
)!=0) {
647 if(c
==UCNV_OPTION_SEP_CHAR
) {
651 /* inName is behind an option separator */
652 if(uprv_strncmp(inName
, "locale=", 7)==0) {
653 /* do not modify locale itself in case we have multiple locale options */
654 char *dest
=pPieces
->locale
;
656 /* copy the locale option value */
659 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
662 if(++len
>=ULOC_FULLNAME_CAPACITY
) {
663 *err
=U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
664 pPieces
->locale
[0]=0;
671 } else if(uprv_strncmp(inName
, "version=", 8)==0) {
672 /* copy the version option value into bits 3..0 of pPieces->options */
676 pArgs
->options
=(pPieces
->options
&=~UCNV_OPTION_VERSION
);
678 } else if((uint8_t)(c
-'0')<10) {
679 pArgs
->options
=pPieces
->options
=(pPieces
->options
&~UCNV_OPTION_VERSION
)|(uint32_t)(c
-'0');
682 } else if(uprv_strncmp(inName
, "swaplfnl", 8)==0) {
684 pArgs
->options
=(pPieces
->options
|=UCNV_OPTION_SWAP_LFNL
);
685 /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
687 /* ignore any other options until we define some */
688 while(((c
= *inName
++) != 0) && (c
!= UCNV_OPTION_SEP_CHAR
)) {
697 /*Logic determines if the converter is Algorithmic AND/OR cached
699 * -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
700 * -Get it from a Hashtable (Data=X, Cached=TRUE)
701 * -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
702 * -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
704 U_CFUNC UConverterSharedData
*
705 ucnv_loadSharedData(const char *converterName
,
706 UConverterNamePieces
*pPieces
,
707 UConverterLoadArgs
*pArgs
,
709 UConverterNamePieces stackPieces
;
710 UConverterLoadArgs stackArgs
;
711 UConverterSharedData
*mySharedConverterData
= NULL
;
712 UErrorCode internalErrorCode
= U_ZERO_ERROR
;
713 UBool mayContainOption
= TRUE
;
714 UBool checkForAlgorithmic
= TRUE
;
716 if (U_FAILURE (*err
)) {
720 if(pPieces
== NULL
) {
723 * Bad: We may set pArgs pointers to stackPieces fields
724 * which will be invalid after this function returns.
726 *err
= U_INTERNAL_PROGRAM_ERROR
;
729 pPieces
= &stackPieces
;
732 uprv_memset(&stackArgs
, 0, sizeof(stackArgs
));
733 stackArgs
.size
= (int32_t)sizeof(stackArgs
);
737 pPieces
->cnvName
[0] = 0;
738 pPieces
->locale
[0] = 0;
739 pPieces
->options
= 0;
741 pArgs
->name
= converterName
;
742 pArgs
->locale
= pPieces
->locale
;
743 pArgs
->options
= pPieces
->options
;
745 /* In case "name" is NULL we want to open the default converter. */
746 if (converterName
== NULL
) {
747 #if U_CHARSET_IS_UTF8
748 pArgs
->name
= "UTF-8";
749 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
751 /* Call ucnv_getDefaultName first to query the name from the OS. */
752 pArgs
->name
= ucnv_getDefaultName();
753 if (pArgs
->name
== NULL
) {
754 *err
= U_MISSING_RESOURCE_ERROR
;
757 mySharedConverterData
= (UConverterSharedData
*)gDefaultAlgorithmicSharedData
;
758 checkForAlgorithmic
= FALSE
;
759 mayContainOption
= gDefaultConverterContainsOption
;
760 /* the default converter name is already canonical */
763 else if(UCNV_FAST_IS_UTF8(converterName
)) {
764 /* fastpath for UTF-8 */
765 pArgs
->name
= "UTF-8";
766 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
769 /* separate the converter name from the options */
770 parseConverterOptions(converterName
, pPieces
, pArgs
, err
);
771 if (U_FAILURE(*err
)) {
772 /* Very bad name used. */
776 /* get the canonical converter name */
777 pArgs
->name
= ucnv_io_getConverterName(pArgs
->name
, &mayContainOption
, &internalErrorCode
);
778 if (U_FAILURE(internalErrorCode
) || pArgs
->name
== NULL
) {
780 * set the input name in case the converter was added
781 * without updating the alias table, or when there is no alias table
783 pArgs
->name
= pPieces
->cnvName
;
784 } else if (internalErrorCode
== U_AMBIGUOUS_ALIAS_WARNING
) {
785 *err
= U_AMBIGUOUS_ALIAS_WARNING
;
789 /* separate the converter name from the options */
790 if(mayContainOption
&& pArgs
->name
!= pPieces
->cnvName
) {
791 parseConverterOptions(pArgs
->name
, pPieces
, pArgs
, err
);
794 /* get the shared data for an algorithmic converter, if it is one */
795 if (checkForAlgorithmic
) {
796 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(pArgs
->name
);
798 if (mySharedConverterData
== NULL
)
800 /* it is a data-based converter, get its shared data. */
801 /* Hold the cnvCacheMutex through the whole process of checking the */
802 /* converter data cache, and adding new entries to the cache */
803 /* to prevent other threads from modifying the cache during the */
805 pArgs
->nestedLoads
=1;
808 umtx_lock(&cnvCacheMutex
);
809 mySharedConverterData
= ucnv_load(pArgs
, err
);
810 umtx_unlock(&cnvCacheMutex
);
811 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
817 return mySharedConverterData
;
821 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
823 UConverterNamePieces stackPieces
;
824 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
825 UConverterSharedData
*mySharedConverterData
;
827 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
829 if(U_SUCCESS(*err
)) {
830 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
832 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
834 myUConverter
= ucnv_createConverterFromSharedData(
835 myUConverter
, mySharedConverterData
,
839 if(U_SUCCESS(*err
)) {
840 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
845 /* exit with error */
846 UTRACE_EXIT_STATUS(*err
);
851 ucnv_canCreateConverter(const char *converterName
, UErrorCode
*err
) {
852 UConverter myUConverter
;
853 UConverterNamePieces stackPieces
;
854 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
855 UConverterSharedData
*mySharedConverterData
;
857 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
859 if(U_SUCCESS(*err
)) {
860 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "test if can open converter %s", converterName
);
862 stackArgs
.onlyTestIsLoadable
=TRUE
;
863 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackPieces
, &stackArgs
, err
);
864 ucnv_createConverterFromSharedData(
865 &myUConverter
, mySharedConverterData
,
868 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
871 UTRACE_EXIT_STATUS(*err
);
872 return U_SUCCESS(*err
);
876 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
878 const char *locale
, uint32_t options
,
881 const UConverterSharedData
*sharedData
;
882 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
884 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
885 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
887 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
888 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
889 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
893 sharedData
= converterData
[type
];
894 if(sharedData
== NULL
|| sharedData
->isReferenceCounted
) {
895 /* not a valid type, or not an algorithmic converter */
896 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
897 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
902 stackArgs
.options
= options
;
903 stackArgs
.locale
=locale
;
904 cnv
= ucnv_createConverterFromSharedData(
905 myUConverter
, (UConverterSharedData
*)sharedData
,
908 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
913 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
915 UConverter
*myUConverter
;
916 UConverterSharedData
*mySharedConverterData
;
917 UConverterNamePieces stackPieces
;
918 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
920 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
922 if(U_FAILURE(*err
)) {
923 UTRACE_EXIT_STATUS(*err
);
927 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
929 /* first, get the options out of the converterName string */
930 stackPieces
.cnvName
[0] = 0;
931 stackPieces
.locale
[0] = 0;
932 stackPieces
.options
= 0;
933 parseConverterOptions(converterName
, &stackPieces
, &stackArgs
, err
);
934 if (U_FAILURE(*err
)) {
935 /* Very bad name used. */
936 UTRACE_EXIT_STATUS(*err
);
939 stackArgs
.nestedLoads
=1;
940 stackArgs
.pkg
=packageName
;
942 /* open the data, unflatten the shared structure */
943 mySharedConverterData
= createConverterFromFile(&stackArgs
, err
);
945 if (U_FAILURE(*err
)) {
946 UTRACE_EXIT_STATUS(*err
);
950 /* create the actual converter */
951 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, &stackArgs
, err
);
953 if (U_FAILURE(*err
)) {
954 ucnv_close(myUConverter
);
955 UTRACE_EXIT_STATUS(*err
);
959 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
965 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
966 UConverterSharedData
*mySharedConverterData
,
967 UConverterLoadArgs
*pArgs
,
972 if(U_FAILURE(*err
)) {
973 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
976 if(myUConverter
== NULL
)
978 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
979 if(myUConverter
== NULL
)
981 *err
= U_MEMORY_ALLOCATION_ERROR
;
982 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
990 /* initialize the converter */
991 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
992 myUConverter
->isCopyLocal
= isCopyLocal
;
993 /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
994 myUConverter
->sharedData
= mySharedConverterData
;
995 myUConverter
->options
= pArgs
->options
;
996 if(!pArgs
->onlyTestIsLoadable
) {
997 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
998 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_DEFAULT_CALLBACK
;
999 myUConverter
->fromUCharErrorBehaviour
= UCNV_FROM_U_DEFAULT_CALLBACK
;
1000 myUConverter
->toUnicodeStatus
= mySharedConverterData
->toUnicodeStatus
;
1001 myUConverter
->maxBytesPerUChar
= mySharedConverterData
->staticData
->maxBytesPerChar
;
1002 myUConverter
->subChar1
= mySharedConverterData
->staticData
->subChar1
;
1003 myUConverter
->subCharLen
= mySharedConverterData
->staticData
->subCharLen
;
1004 myUConverter
->subChars
= (uint8_t *)myUConverter
->subUChars
;
1005 uprv_memcpy(myUConverter
->subChars
, mySharedConverterData
->staticData
->subChar
, myUConverter
->subCharLen
);
1006 myUConverter
->toUCallbackReason
= UCNV_ILLEGAL
; /* default reason to invoke (*fromCharErrorBehaviour) */
1009 if(mySharedConverterData
->impl
->open
!= NULL
) {
1010 mySharedConverterData
->impl
->open(myUConverter
, pArgs
, err
);
1011 if(U_FAILURE(*err
) && !pArgs
->onlyTestIsLoadable
) {
1012 /* don't ucnv_close() if onlyTestIsLoadable because not fully initialized */
1013 ucnv_close(myUConverter
);
1018 return myUConverter
;
1021 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
1023 U_CAPI
int32_t U_EXPORT2
1026 UConverterSharedData
*mySharedData
= NULL
;
1028 int32_t tableDeletedNum
= 0;
1029 const UHashElement
*e
;
1030 /*UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;*/
1031 int32_t i
, remaining
;
1033 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
1035 /* Close the default converter without creating a new one so that everything will be flushed. */
1036 u_flushDefaultConverter();
1038 /*if shared data hasn't even been lazy evaluated yet
1041 if (SHARED_DATA_HASHTABLE
== NULL
) {
1042 UTRACE_EXIT_VALUE((int32_t)0);
1046 /*creates an enumeration to iterate through every element in the
1049 * Synchronization: holding cnvCacheMutex will prevent any other thread from
1050 * accessing or modifying the hash table during the iteration.
1051 * The reference count of an entry may be decremented by
1052 * ucnv_close while the iteration is in process, but this is
1053 * benign. It can't be incremented (in ucnv_createConverter())
1054 * because the sequence of looking up in the cache + incrementing
1055 * is protected by cnvCacheMutex.
1057 umtx_lock(&cnvCacheMutex
);
1059 * double loop: A delta/extension-only converter has a pointer to its base table's
1060 * shared data; the first iteration of the outer loop may see the delta converter
1061 * before the base converter, and unloading the delta converter may get the base
1062 * converter's reference counter down to 0.
1068 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
1070 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
1071 /*deletes only if reference counter == 0 */
1072 if (mySharedData
->referenceCounter
== 0)
1076 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
1078 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
1079 mySharedData
->sharedDataCached
= FALSE
;
1080 ucnv_deleteSharedConverterData (mySharedData
);
1085 } while(++i
== 1 && remaining
> 0);
1086 umtx_unlock(&cnvCacheMutex
);
1088 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
1090 UTRACE_EXIT_VALUE(tableDeletedNum
);
1091 return tableDeletedNum
;
1094 /* available converters list --------------------------------------------------- */
1096 static void U_CALLCONV
initAvailableConvertersList(UErrorCode
&errCode
) {
1097 U_ASSERT(gAvailableConverterCount
== 0);
1098 U_ASSERT(gAvailableConverters
== NULL
);
1100 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1101 UEnumeration
*allConvEnum
= ucnv_openAllNames(&errCode
);
1102 int32_t allConverterCount
= uenum_count(allConvEnum
, &errCode
);
1103 if (U_FAILURE(errCode
)) {
1107 /* We can't have more than "*converterTable" converters to open */
1108 gAvailableConverters
= (const char **) uprv_malloc(allConverterCount
* sizeof(char*));
1109 if (!gAvailableConverters
) {
1110 errCode
= U_MEMORY_ALLOCATION_ERROR
;
1114 /* Open the default converter to make sure that it has first dibs in the hash table. */
1115 UErrorCode localStatus
= U_ZERO_ERROR
;
1116 UConverter tempConverter
;
1117 ucnv_close(ucnv_createConverter(&tempConverter
, NULL
, &localStatus
));
1119 gAvailableConverterCount
= 0;
1121 for (int32_t idx
= 0; idx
< allConverterCount
; idx
++) {
1122 localStatus
= U_ZERO_ERROR
;
1123 const char *converterName
= uenum_next(allConvEnum
, NULL
, &localStatus
);
1124 if (ucnv_canCreateConverter(converterName
, &localStatus
)) {
1125 gAvailableConverters
[gAvailableConverterCount
++] = converterName
;
1129 uenum_close(allConvEnum
);
1133 static UBool
haveAvailableConverterList(UErrorCode
*pErrorCode
) {
1134 umtx_initOnce(gAvailableConvertersInitOnce
, &initAvailableConvertersList
, *pErrorCode
);
1135 return U_SUCCESS(*pErrorCode
);
1139 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
) {
1140 if (haveAvailableConverterList(pErrorCode
)) {
1141 return gAvailableConverterCount
;
1146 U_CFUNC
const char *
1147 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
) {
1148 if (haveAvailableConverterList(pErrorCode
)) {
1149 if (n
< gAvailableConverterCount
) {
1150 return gAvailableConverters
[n
];
1152 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
1157 /* default converter name --------------------------------------------------- */
1159 #if !U_CHARSET_IS_UTF8
1161 Copy the canonical converter name.
1162 ucnv_getDefaultName must be thread safe, which can call this function.
1164 ucnv_setDefaultName calls this function and it doesn't have to be
1165 thread safe because there is no reliable/safe way to reset the
1166 converter in use in all threads. If you did reset the converter, you
1167 would not be sure that retrieving a default converter for one string
1168 would be the same type of default converter for a successive string.
1169 Since the name is a returned via ucnv_getDefaultName without copying,
1170 you shouldn't be modifying or deleting the string from a separate thread.
1173 internalSetName(const char *name
, UErrorCode
*status
) {
1174 UConverterNamePieces stackPieces
;
1175 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
1176 int32_t length
=(int32_t)(uprv_strlen(name
));
1177 UBool containsOption
= (UBool
)(uprv_strchr(name
, UCNV_OPTION_SEP_CHAR
) != NULL
);
1178 const UConverterSharedData
*algorithmicSharedData
;
1180 stackArgs
.name
= name
;
1181 if(containsOption
) {
1182 stackPieces
.cnvName
[0] = 0;
1183 stackPieces
.locale
[0] = 0;
1184 stackPieces
.options
= 0;
1185 parseConverterOptions(name
, &stackPieces
, &stackArgs
, status
);
1186 if(U_FAILURE(*status
)) {
1190 algorithmicSharedData
= getAlgorithmicTypeFromName(stackArgs
.name
);
1192 umtx_lock(&cnvCacheMutex
);
1194 gDefaultAlgorithmicSharedData
= algorithmicSharedData
;
1195 gDefaultConverterContainsOption
= containsOption
;
1196 uprv_memcpy(gDefaultConverterNameBuffer
, name
, length
);
1197 gDefaultConverterNameBuffer
[length
]=0;
1199 /* gDefaultConverterName MUST be the last global var set by this function. */
1200 /* It is the variable checked in ucnv_getDefaultName() to see if initialization is required. */
1201 // But there is nothing here preventing that from being reordered, either by the compiler
1202 // or hardware. I'm adding the mutex to ucnv_getDefaultName for now. UMTX_CHECK is not enough.
1204 gDefaultConverterName
= gDefaultConverterNameBuffer
;
1206 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1208 umtx_unlock(&cnvCacheMutex
);
1213 * In order to be really thread-safe, the get function would have to take
1214 * a buffer parameter and copy the current string inside a mutex block.
1215 * This implementation only tries to be really thread-safe while
1217 * It assumes that setting a pointer is atomic.
1220 U_CAPI
const char* U_EXPORT2
1221 ucnv_getDefaultName() {
1222 #if U_CHARSET_IS_UTF8
1225 /* local variable to be thread-safe */
1229 Concurrent calls to ucnv_getDefaultName must be thread safe,
1230 but ucnv_setDefaultName is not thread safe.
1233 icu::Mutex
lock(&cnvCacheMutex
);
1234 name
= gDefaultConverterName
;
1237 UErrorCode errorCode
= U_ZERO_ERROR
;
1238 UConverter
*cnv
= NULL
;
1240 name
= uprv_getDefaultCodepage();
1242 /* if the name is there, test it out and get the canonical name with options */
1244 cnv
= ucnv_open(name
, &errorCode
);
1245 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1246 name
= ucnv_getName(cnv
, &errorCode
);
1250 if(name
== NULL
|| name
[0] == 0
1251 || U_FAILURE(errorCode
) || cnv
== NULL
1252 || uprv_strlen(name
)>=sizeof(gDefaultConverterNameBuffer
))
1254 /* Panic time, let's use a fallback. */
1255 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1257 /* there is no 'algorithmic' converter for EBCDIC */
1258 #elif U_PLATFORM == U_PF_OS390
1259 name
= "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING
;
1261 name
= "ibm-37_P100-1995";
1265 internalSetName(name
, &errorCode
);
1267 /* The close may make the current name go away. */
1275 #if U_CHARSET_IS_UTF8
1276 U_CAPI
void U_EXPORT2
ucnv_setDefaultName(const char *) {}
1279 This function is not thread safe, and it can't be thread safe.
1280 See internalSetName or the API reference for details.
1282 U_CAPI
void U_EXPORT2
1283 ucnv_setDefaultName(const char *converterName
) {
1284 if(converterName
==NULL
) {
1285 /* reset to the default codepage */
1286 gDefaultConverterName
=NULL
;
1288 UErrorCode errorCode
= U_ZERO_ERROR
;
1289 UConverter
*cnv
= NULL
;
1290 const char *name
= NULL
;
1292 /* if the name is there, test it out and get the canonical name with options */
1293 cnv
= ucnv_open(converterName
, &errorCode
);
1294 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1295 name
= ucnv_getName(cnv
, &errorCode
);
1298 if(U_SUCCESS(errorCode
) && name
!=NULL
) {
1299 internalSetName(name
, &errorCode
);
1301 /* else this converter is bad to use. Don't change it to a bad value. */
1303 /* The close may make the current name go away. */
1306 /* reset the converter cache */
1307 u_flushDefaultConverter();
1312 /* data swapping ------------------------------------------------------------ */
1314 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
1316 #if !UCONFIG_NO_LEGACY_CONVERSION
1318 U_CAPI
int32_t U_EXPORT2
1319 ucnv_swap(const UDataSwapper
*ds
,
1320 const void *inData
, int32_t length
, void *outData
,
1321 UErrorCode
*pErrorCode
) {
1322 const UDataInfo
*pInfo
;
1325 const uint8_t *inBytes
;
1328 uint32_t offset
, count
, staticDataSize
;
1331 const UConverterStaticData
*inStaticData
;
1332 UConverterStaticData
*outStaticData
;
1334 const _MBCSHeader
*inMBCSHeader
;
1335 _MBCSHeader
*outMBCSHeader
;
1336 _MBCSHeader mbcsHeader
;
1337 uint32_t mbcsHeaderLength
;
1338 UBool noFromU
=FALSE
;
1342 int32_t maxFastUChar
, mbcsIndexLength
;
1344 const int32_t *inExtIndexes
;
1347 /* udata_swapDataHeader checks the arguments */
1348 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1349 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1353 /* check data format and format version */
1354 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1356 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1357 pInfo
->dataFormat
[1]==0x6e &&
1358 pInfo
->dataFormat
[2]==0x76 &&
1359 pInfo
->dataFormat
[3]==0x74 &&
1360 pInfo
->formatVersion
[0]==6 &&
1361 pInfo
->formatVersion
[1]>=2
1363 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",
1364 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1365 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1366 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1367 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1371 inBytes
=(const uint8_t *)inData
+headerSize
;
1372 outBytes
=(uint8_t *)outData
+headerSize
;
1374 /* read the initial UConverterStaticData structure after the UDataInfo header */
1375 inStaticData
=(const UConverterStaticData
*)inBytes
;
1376 outStaticData
=(UConverterStaticData
*)outBytes
;
1379 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1382 if( length
<(int32_t)sizeof(UConverterStaticData
) ||
1383 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1385 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1387 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1393 /* swap the static data */
1394 if(inStaticData
!=outStaticData
) {
1395 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1398 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1399 &outStaticData
->structSize
, pErrorCode
);
1400 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1401 &outStaticData
->codepage
, pErrorCode
);
1403 ds
->swapInvChars(ds
, inStaticData
->name
, (int32_t)uprv_strlen(inStaticData
->name
),
1404 outStaticData
->name
, pErrorCode
);
1405 if(U_FAILURE(*pErrorCode
)) {
1406 udata_printError(ds
, "ucnv_swap(): error swapping converter name\n");
1411 inBytes
+=staticDataSize
;
1412 outBytes
+=staticDataSize
;
1414 length
-=(int32_t)staticDataSize
;
1417 /* check for supported conversionType values */
1418 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1419 /* swap MBCS data */
1420 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1421 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1423 if(0<=length
&& length
<(int32_t)sizeof(_MBCSHeader
)) {
1424 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1426 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1429 if(inMBCSHeader
->version
[0]==4 && inMBCSHeader
->version
[1]>=1) {
1430 mbcsHeaderLength
=MBCS_HEADER_V4_LENGTH
;
1431 } else if(inMBCSHeader
->version
[0]==5 && inMBCSHeader
->version
[1]>=3 &&
1432 ((mbcsHeader
.options
=ds
->readUInt32(inMBCSHeader
->options
))&
1433 MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0
1435 mbcsHeaderLength
=mbcsHeader
.options
&MBCS_OPT_LENGTH_MASK
;
1436 noFromU
=(UBool
)((mbcsHeader
.options
&MBCS_OPT_NO_FROM_U
)!=0);
1438 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1439 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1440 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1444 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1445 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1446 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1447 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1448 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1449 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1450 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1451 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1452 /* mbcsHeader.options have been read above */
1454 extOffset
=(int32_t)(mbcsHeader
.flags
>>8);
1455 outputType
=(uint8_t)mbcsHeader
.flags
;
1456 if(noFromU
&& outputType
==MBCS_OUTPUT_1
) {
1457 udata_printError(ds
, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
1458 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1462 /* make sure that the output type is known */
1463 switch(outputType
) {
1468 case MBCS_OUTPUT_3_EUC
:
1469 case MBCS_OUTPUT_4_EUC
:
1470 case MBCS_OUTPUT_2_SISO
:
1471 case MBCS_OUTPUT_EXT_ONLY
:
1475 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1477 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1481 /* calculate the length of the MBCS data */
1484 * utf8Friendly MBCS files (mbcsHeader.version 4.3)
1485 * contain an additional mbcsIndex table:
1486 * uint16_t[(maxFastUChar+1)>>6];
1487 * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
1491 if( outputType
!=MBCS_OUTPUT_EXT_ONLY
&& outputType
!=MBCS_OUTPUT_1
&&
1492 mbcsHeader
.version
[1]>=3 && (maxFastUChar
=mbcsHeader
.version
[2])!=0
1494 maxFastUChar
=(maxFastUChar
<<8)|0xff;
1495 mbcsIndexLength
=((maxFastUChar
+1)>>6)*2; /* number of bytes */
1499 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsIndexLength
);
1501 size
+=(int32_t)mbcsHeader
.fromUBytesLength
;
1504 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1507 /* there is extension data after the base data, see ucnv_ext.h */
1508 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1509 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1511 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1515 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1516 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1521 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1523 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1527 /* copy the data for inaccessible bytes */
1528 if(inBytes
!=outBytes
) {
1529 uprv_memcpy(outBytes
, inBytes
, size
);
1532 /* swap the MBCSHeader, except for the version field */
1533 count
=mbcsHeaderLength
*4;
1534 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, count
-4,
1535 &outMBCSHeader
->countStates
, pErrorCode
);
1537 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1539 * extension-only file,
1540 * contains a base name instead of normal base table data
1543 /* swap the base name, between the header and the extension data */
1544 const char *inBaseName
=(const char *)inBytes
+count
;
1545 char *outBaseName
=(char *)outBytes
+count
;
1546 ds
->swapInvChars(ds
, inBaseName
, (int32_t)uprv_strlen(inBaseName
),
1547 outBaseName
, pErrorCode
);
1549 /* normal file with base table data */
1551 /* swap the state table, 1kB per state */
1553 count
=mbcsHeader
.countStates
*1024;
1554 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1555 outBytes
+offset
, pErrorCode
);
1557 /* swap the toUFallbacks[] */
1559 count
=mbcsHeader
.countToUFallbacks
*8;
1560 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1561 outBytes
+offset
, pErrorCode
);
1563 /* swap the unicodeCodeUnits[] */
1564 offset
=mbcsHeader
.offsetToUCodeUnits
;
1565 count
=mbcsHeader
.offsetFromUTable
-offset
;
1566 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1567 outBytes
+offset
, pErrorCode
);
1569 /* offset to the stage 1 table, independent of the outputType */
1570 offset
=mbcsHeader
.offsetFromUTable
;
1572 if(outputType
==MBCS_OUTPUT_1
) {
1573 /* SBCS: swap the fromU tables, all 16 bits wide */
1574 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1575 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1576 outBytes
+offset
, pErrorCode
);
1578 /* otherwise: swap the stage tables separately */
1580 /* stage 1 table: uint16_t[0x440 or 0x40] */
1581 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1582 count
=0x440*2; /* for all of Unicode */
1584 count
=0x40*2; /* only BMP */
1586 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1587 outBytes
+offset
, pErrorCode
);
1589 /* stage 2 table: uint32_t[] */
1591 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1592 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1593 outBytes
+offset
, pErrorCode
);
1595 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1596 offset
=mbcsHeader
.offsetFromUBytes
;
1597 count
= noFromU
? 0 : mbcsHeader
.fromUBytesLength
;
1598 switch(outputType
) {
1600 case MBCS_OUTPUT_3_EUC
:
1601 case MBCS_OUTPUT_2_SISO
:
1602 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1603 outBytes
+offset
, pErrorCode
);
1606 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1607 outBytes
+offset
, pErrorCode
);
1610 /* just uint8_t[], nothing to swap */
1614 if(mbcsIndexLength
!=0) {
1616 count
=mbcsIndexLength
;
1617 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1618 outBytes
+offset
, pErrorCode
);
1624 /* swap the extension data */
1626 outBytes
+=extOffset
;
1628 /* swap toUTable[] */
1629 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1630 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1631 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1633 /* swap toUUChars[] */
1634 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1635 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1636 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1638 /* swap fromUTableUChars[] */
1639 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1640 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1641 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1643 /* swap fromUTableValues[] */
1644 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1645 /* same length as for fromUTableUChars[] */
1646 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1648 /* no need to swap fromUBytes[] */
1650 /* swap fromUStage12[] */
1651 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1652 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1653 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1655 /* swap fromUStage3[] */
1656 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1657 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1658 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1660 /* swap fromUStage3b[] */
1661 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1662 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1663 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1665 /* swap indexes[] */
1666 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1667 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1671 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1672 inStaticData
->conversionType
);
1673 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1677 return headerSize
+(int32_t)staticDataSize
+size
;
1680 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */