2 ********************************************************************
4 * Copyright (c) 1996-2004, 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/udata.h"
27 #include "unicode/ucnv.h"
28 #include "unicode/uloc.h"
47 extern void UCNV_DEBUG_LOG(char *what
, char *who
, void *p
, int l
);
48 #define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
50 # define UCNV_DEBUG_LOG(x,y,z)
53 static const UConverterSharedData
* const
54 converterData
[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
]={
57 #if UCONFIG_NO_LEGACY_CONVERSION
64 &_UTF8Data
, &_UTF16BEData
, &_UTF16LEData
, &_UTF32BEData
, &_UTF32LEData
,
67 #if UCONFIG_NO_LEGACY_CONVERSION
69 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
70 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
74 &_LMBCSData1
,&_LMBCSData2
, &_LMBCSData3
, &_LMBCSData4
, &_LMBCSData5
, &_LMBCSData6
,
75 &_LMBCSData8
,&_LMBCSData11
,&_LMBCSData16
,&_LMBCSData17
,&_LMBCSData18
,&_LMBCSData19
,
81 #if UCONFIG_NO_LEGACY_CONVERSION
88 &_UTF7Data
, &_Bocu1Data
, &_UTF16Data
, &_UTF32Data
, &_CESU8Data
, &_IMAPData
91 /* Please keep this in binary sorted order for getAlgorithmicTypeFromName.
92 Also the name should be in lower case and all spaces, dashes and underscores
97 const UConverterType type
;
98 } const cnvNameType
[] = {
99 { "bocu1", UCNV_BOCU1
},
100 { "cesu8", UCNV_CESU8
},
101 #if !UCONFIG_NO_LEGACY_CONVERSION
104 { "imapmailboxname", UCNV_IMAP_MAILBOX
},
105 #if !UCONFIG_NO_LEGACY_CONVERSION
106 { "iscii", UCNV_ISCII
},
107 { "iso2022", UCNV_ISO_2022
},
109 { "iso88591", UCNV_LATIN_1
},
110 #if !UCONFIG_NO_LEGACY_CONVERSION
111 { "lmbcs1", UCNV_LMBCS_1
},
112 { "lmbcs11",UCNV_LMBCS_11
},
113 { "lmbcs16",UCNV_LMBCS_16
},
114 { "lmbcs17",UCNV_LMBCS_17
},
115 { "lmbcs18",UCNV_LMBCS_18
},
116 { "lmbcs19",UCNV_LMBCS_19
},
117 { "lmbcs2", UCNV_LMBCS_2
},
118 { "lmbcs3", UCNV_LMBCS_3
},
119 { "lmbcs4", UCNV_LMBCS_4
},
120 { "lmbcs5", UCNV_LMBCS_5
},
121 { "lmbcs6", UCNV_LMBCS_6
},
122 { "lmbcs8", UCNV_LMBCS_8
},
124 { "scsu", UCNV_SCSU
},
125 { "usascii", UCNV_US_ASCII
},
126 { "utf16", UCNV_UTF16
},
127 { "utf16be", UCNV_UTF16_BigEndian
},
128 { "utf16le", UCNV_UTF16_LittleEndian
},
130 { "utf16oppositeendian", UCNV_UTF16_LittleEndian
},
131 { "utf16platformendian", UCNV_UTF16_BigEndian
},
133 { "utf16oppositeendian", UCNV_UTF16_BigEndian
},
134 { "utf16platformendian", UCNV_UTF16_LittleEndian
},
136 { "utf32", UCNV_UTF32
},
137 { "utf32be", UCNV_UTF32_BigEndian
},
138 { "utf32le", UCNV_UTF32_LittleEndian
},
140 { "utf32oppositeendian", UCNV_UTF32_LittleEndian
},
141 { "utf32platformendian", UCNV_UTF32_BigEndian
},
143 { "utf32oppositeendian", UCNV_UTF32_BigEndian
},
144 { "utf32platformendian", UCNV_UTF32_LittleEndian
},
146 { "utf7", UCNV_UTF7
},
147 { "utf8", UCNV_UTF8
}
151 /*initializes some global variables */
152 static UHashtable
*SHARED_DATA_HASHTABLE
= NULL
;
153 static UMTX cnvCacheMutex
= NULL
; /* Mutex for synchronizing cnv cache access. */
154 /* Note: the global mutex is used for */
155 /* reference count updates. */
158 static const char DATA_TYPE
[] = "cnv";
160 /* ucnv_cleanup - delete all storage held by the converter cache, except any in use */
161 /* by open converters. */
162 /* Not thread safe. */
163 /* Not supported API. Marked U_CAPI only for use by test programs. */
164 static UBool U_CALLCONV
ucnv_cleanup(void) {
165 if (SHARED_DATA_HASHTABLE
!= NULL
) {
167 if (SHARED_DATA_HASHTABLE
!= NULL
&& uhash_count(SHARED_DATA_HASHTABLE
) == 0) {
168 uhash_close(SHARED_DATA_HASHTABLE
);
169 SHARED_DATA_HASHTABLE
= NULL
;
173 umtx_destroy(&cnvCacheMutex
); /* Don't worry about destroying the mutex even */
174 /* if the hash table still exists. The mutex */
175 /* will lazily re-init itself if needed. */
176 return (SHARED_DATA_HASHTABLE
== NULL
);
179 static UBool U_CALLCONV
180 isCnvAcceptable(void *context
,
181 const char *type
, const char *name
,
182 const UDataInfo
*pInfo
) {
185 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
186 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
187 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
188 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
189 pInfo
->dataFormat
[1]==0x6e &&
190 pInfo
->dataFormat
[2]==0x76 &&
191 pInfo
->dataFormat
[3]==0x74 &&
192 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
196 * Un flatten shared data from a UDATA..
198 static UConverterSharedData
*
199 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
201 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
202 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
203 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
204 UConverterSharedData
*data
;
205 UConverterType type
= (UConverterType
)source
->conversionType
;
207 if(U_FAILURE(*status
))
210 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
211 converterData
[type
] == NULL
||
212 converterData
[type
]->referenceCounter
!= 1 ||
213 source
->structSize
!= sizeof(UConverterStaticData
))
215 *status
= U_INVALID_TABLE_FORMAT
;
219 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
221 *status
= U_MEMORY_ALLOCATION_ERROR
;
225 /* copy initial values from the static structure for this type */
226 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
228 #if 0 /* made UConverterMBCSTable part of UConverterSharedData -- markus 20031107 */
230 * It would be much more efficient if the table were a direct member, not a pointer.
231 * However, that would add to the size of all UConverterSharedData objects
232 * even if they do not use this table (especially algorithmic ones).
233 * If this changes, then the static templates from converterData[type]
236 * In principle, it would be cleaner if the load() function below
237 * allocated the table.
239 data
->table
= (UConverterTable
*)uprv_malloc(sizeof(UConverterTable
));
240 if(data
->table
== NULL
) {
242 *status
= U_MEMORY_ALLOCATION_ERROR
;
245 uprv_memset(data
->table
, 0, sizeof(UConverterTable
));
248 data
->staticData
= source
;
250 data
->sharedDataCached
= FALSE
;
252 /* fill in fields from the loaded data */
253 data
->dataMemory
= (void*)pData
; /* for future use */
255 if(data
->impl
->load
!= NULL
) {
256 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
257 if(U_FAILURE(*status
)) {
258 uprv_free(data
->table
);
266 /*Takes an alias name gets an actual converter file name
267 *goes to disk and opens it.
268 *allocates the memory and returns a new UConverter object
270 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
273 UConverterSharedData
*sharedData
;
275 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
277 if (err
== NULL
|| U_FAILURE (*err
)) {
278 UTRACE_EXIT_STATUS(*err
);
282 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
284 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
287 UTRACE_EXIT_STATUS(*err
);
291 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
295 UTRACE_EXIT_STATUS(*err
);
300 * TODO Store pkg in a field in the shared data so that delta-only converters
301 * can load base converters from the same package.
302 * If the pkg name is longer than the field, then either do not load the converter
303 * in the first place, or just set the pkg field to "".
306 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
311 ucnv_copyPlatformString(char *platformString
, UConverterPlatform pltfrm
)
316 uprv_strcpy(platformString
, "ibm-");
322 /* default to empty string */
327 /*returns a converter type from a string
329 static const UConverterSharedData
*
330 getAlgorithmicTypeFromName(const char *realName
)
332 uint32_t mid
, start
, limit
;
335 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
337 /* Lower case and remove ignoreable characters. */
338 ucnv_io_stripForCompare(strippedName
, realName
);
340 /* do a binary search for the alias */
342 limit
= sizeof(cnvNameType
)/sizeof(cnvNameType
[0]);
344 lastMid
= UINT32_MAX
;
347 mid
= (uint32_t)((start
+ limit
) / 2);
348 if (lastMid
== mid
) { /* Have we moved? */
349 break; /* We haven't moved, and it wasn't found. */
352 result
= uprv_strcmp(strippedName
, cnvNameType
[mid
].name
);
356 } else if (result
> 0) {
359 return converterData
[cnvNameType
[mid
].type
];
366 /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
367 /* Will always be called with the cnvCacheMutex alrady being held */
368 /* by the calling function. */
369 /* Stores the shared data in the SHARED_DATA_HASHTABLE
370 * @param data The shared data
373 ucnv_shareConverterData(UConverterSharedData
* data
)
375 UErrorCode err
= U_ZERO_ERROR
;
376 /*Lazy evaluates the Hashtable itself */
377 /*void *sanity = NULL;*/
379 if (SHARED_DATA_HASHTABLE
== NULL
)
381 SHARED_DATA_HASHTABLE
= uhash_openSize(uhash_hashChars
, uhash_compareChars
,
382 ucnv_io_countAvailableAliases(&err
),
384 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
390 /* ### check to see if the element is not already there! */
393 sanity = ucnv_getSharedConverterData (data->staticData->name);
396 UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
398 UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
402 data
->sharedDataCached
= TRUE
;
404 uhash_put(SHARED_DATA_HASHTABLE
,
405 (void*) data
->staticData
->name
, /* Okay to cast away const as long as
406 keyDeleter == NULL */
409 UCNV_DEBUG_LOG("put", data
->staticData
->name
,data
);
413 /* Look up a converter name in the shared data cache. */
414 /* cnvCacheMutex must be held by the caller to protect the hash table. */
415 /* gets the shared data from the SHARED_DATA_HASHTABLE (might return NULL if it isn't there)
416 * @param name The name of the shared data
417 * @return the shared data from the SHARED_DATA_HASHTABLE
419 static UConverterSharedData
*
420 ucnv_getSharedConverterData(const char *name
)
422 /*special case when no Table has yet been created we return NULL */
423 if (SHARED_DATA_HASHTABLE
== NULL
)
429 UConverterSharedData
*rc
;
431 rc
= (UConverterSharedData
*)uhash_get(SHARED_DATA_HASHTABLE
, name
);
432 UCNV_DEBUG_LOG("get",name
,rc
);
437 /*frees the string of memory blocks associates with a sharedConverter
438 *if and only if the referenceCounter == 0
440 /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
441 * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
443 * otherwise returns FALSE
444 * @param sharedConverterData The shared data
445 * @return if not it frees all the memory stemming from sharedConverterData and
446 * returns TRUE, otherwise returns FALSE
449 ucnv_deleteSharedConverterData(UConverterSharedData
* deadSharedData
)
451 UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD
);
452 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "unload converter %s shared data %p", deadSharedData
->staticData
->name
, deadSharedData
);
454 if (deadSharedData
->referenceCounter
> 0) {
455 UTRACE_EXIT_VALUE((int32_t)FALSE
);
459 if (deadSharedData
->impl
->unload
!= NULL
) {
460 deadSharedData
->impl
->unload(deadSharedData
);
463 if(deadSharedData
->dataMemory
!= NULL
)
465 UDataMemory
*data
= (UDataMemory
*)deadSharedData
->dataMemory
;
469 if(deadSharedData
->table
!= NULL
)
471 uprv_free(deadSharedData
->table
);
475 /* if the static data is actually owned by the shared data */
476 /* enable if we ever have this situation. */
477 if(deadSharedData
->staticDataOwned
== TRUE
) /* see ucnv_bld.h */
479 uprv_free((void*)deadSharedData
->staticData
);
485 uprv_memset(deadSharedData
->0, sizeof(*deadSharedData
));
488 uprv_free(deadSharedData
);
490 UTRACE_EXIT_VALUE((int32_t)TRUE
);
495 * Load a non-algorithmic converter.
496 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
498 UConverterSharedData
*
499 ucnv_load(UConverterLoadArgs
*pArgs
, UErrorCode
*err
) {
500 UConverterSharedData
*mySharedConverterData
;
502 if(err
== NULL
|| U_FAILURE(*err
)) {
506 if(pArgs
->pkg
!= NULL
&& *pArgs
->pkg
!= 0) {
507 /* application-provided converters are not currently cached */
508 return createConverterFromFile(pArgs
, err
);
511 mySharedConverterData
= ucnv_getSharedConverterData(pArgs
->name
);
512 if (mySharedConverterData
== NULL
)
514 /*Not cached, we need to stream it in from file */
515 mySharedConverterData
= createConverterFromFile(pArgs
, err
);
516 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
522 /* share it with other library clients */
523 ucnv_shareConverterData(mySharedConverterData
);
528 /* The data for this converter was already in the cache. */
529 /* Update the reference counter on the shared data: one more client */
530 mySharedConverterData
->referenceCounter
++;
533 return mySharedConverterData
;
537 * Unload a non-algorithmic converter.
538 * It must be sharedData->referenceCounter != ~0
539 * and this function must be called inside umtx_lock(&cnvCacheMutex).
542 ucnv_unload(UConverterSharedData
*sharedData
) {
543 if(sharedData
!= NULL
) {
544 if (sharedData
->referenceCounter
> 0) {
545 sharedData
->referenceCounter
--;
548 if((sharedData
->referenceCounter
<= 0)&&(sharedData
->sharedDataCached
== FALSE
)) {
549 ucnv_deleteSharedConverterData(sharedData
);
555 ucnv_unloadSharedDataIfReady(UConverterSharedData
*sharedData
)
558 Checking whether it's an algorithic converter is okay
559 in multithreaded applications because the value never changes.
560 Don't check referenceCounter for any other value.
562 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= ~0) {
563 umtx_lock(&cnvCacheMutex
);
564 ucnv_unload(sharedData
);
565 umtx_unlock(&cnvCacheMutex
);
570 ucnv_incrementRefCount(UConverterSharedData
*sharedData
)
572 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= ~0) {
573 umtx_lock(&cnvCacheMutex
);
574 sharedData
->referenceCounter
++;
575 umtx_unlock(&cnvCacheMutex
);
580 parseConverterOptions(const char *inName
,
589 /* copy the converter name itself to cnvName */
590 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
591 if (++len
>=UCNV_MAX_CONVERTER_NAME_LENGTH
) {
592 *err
= U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
601 /* parse options. No more name copying should occur. */
602 while((c
=*inName
)!=0) {
603 if(c
==UCNV_OPTION_SEP_CHAR
) {
607 /* inName is behind an option separator */
608 if(uprv_strncmp(inName
, "locale=", 7)==0) {
609 /* do not modify locale itself in case we have multiple locale options */
612 /* copy the locale option value */
615 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
618 if(++len
>=ULOC_FULLNAME_CAPACITY
) {
619 *err
=U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
627 } else if(uprv_strncmp(inName
, "version=", 8)==0) {
628 /* copy the version option value into bits 3..0 of *pFlags */
632 *pFlags
&=~UCNV_OPTION_VERSION
;
634 } else if((uint8_t)(c
-'0')<10) {
635 *pFlags
=(*pFlags
&~UCNV_OPTION_VERSION
)|(uint32_t)(c
-'0');
638 } else if(uprv_strncmp(inName
, "swaplfnl", 8)==0) {
640 *pFlags
|=UCNV_OPTION_SWAP_LFNL
;
641 /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
643 /* ignore any other options until we define some */
644 while(((c
= *inName
++) != 0) && (c
!= UCNV_OPTION_SEP_CHAR
)) {
653 /*Logic determines if the converter is Algorithmic AND/OR cached
655 * -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
656 * -Get it from a Hashtable (Data=X, Cached=TRUE)
657 * -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
658 * -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
660 UConverterSharedData
*
661 ucnv_loadSharedData(const char *converterName
, UConverterLookupData
*lookup
, UErrorCode
* err
) {
662 UConverterLookupData stackLookup
;
663 UConverterSharedData
*mySharedConverterData
= NULL
;
664 UErrorCode internalErrorCode
= U_ZERO_ERROR
;
666 if (U_FAILURE (*err
)) {
671 lookup
= &stackLookup
;
674 lookup
->locale
[0] = 0;
677 /* In case "name" is NULL we want to open the default converter. */
678 if (converterName
== NULL
) {
679 lookup
->realName
= ucnv_io_getDefaultConverterName();
680 if (lookup
->realName
== NULL
) {
681 *err
= U_MISSING_RESOURCE_ERROR
;
684 /* the default converter name is already canonical */
686 /* separate the converter name from the options */
687 parseConverterOptions(converterName
, lookup
->cnvName
, lookup
->locale
, &lookup
->options
, err
);
688 if (U_FAILURE(*err
)) {
689 /* Very bad name used. */
693 /* get the canonical converter name */
694 lookup
->realName
= ucnv_io_getConverterName(lookup
->cnvName
, &internalErrorCode
);
695 if (U_FAILURE(internalErrorCode
) || lookup
->realName
== NULL
) {
697 * set the input name in case the converter was added
698 * without updating the alias table, or when there is no alias table
700 lookup
->realName
= lookup
->cnvName
;
704 /* separate the converter name from the options */
705 if(lookup
->realName
!= lookup
->cnvName
) {
706 parseConverterOptions(lookup
->realName
, lookup
->cnvName
, lookup
->locale
, &lookup
->options
, err
);
707 lookup
->realName
= lookup
->cnvName
;
710 /* get the shared data for an algorithmic converter, if it is one */
711 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(lookup
->realName
);
712 if (mySharedConverterData
== NULL
)
714 /* it is a data-based converter, get its shared data. */
715 /* Hold the cnvCacheMutex through the whole process of checking the */
716 /* converter data cache, and adding new entries to the cache */
717 /* to prevent other threads from modifying the cache during the */
719 UConverterLoadArgs args
={ 0 };
721 args
.size
=sizeof(UConverterLoadArgs
);
723 args
.options
=lookup
->options
;
725 args
.name
=lookup
->realName
;
727 umtx_lock(&cnvCacheMutex
);
728 mySharedConverterData
= ucnv_load(&args
, err
);
729 umtx_unlock(&cnvCacheMutex
);
730 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
736 return mySharedConverterData
;
740 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
742 UConverterLookupData stackLookup
;
743 UConverterSharedData
*mySharedConverterData
;
745 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
747 if(U_SUCCESS(*err
)) {
748 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
750 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackLookup
, err
);
752 if(U_SUCCESS(*err
)) {
753 myUConverter
= ucnv_createConverterFromSharedData(
754 myUConverter
, mySharedConverterData
,
755 stackLookup
.realName
, stackLookup
.locale
, stackLookup
.options
,
758 if(U_SUCCESS(*err
)) {
759 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
762 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
767 /* exit with error */
768 UTRACE_EXIT_STATUS(*err
);
773 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
775 const char *locale
, uint32_t options
,
778 const UConverterSharedData
*sharedData
;
779 UBool isAlgorithmicConverter
;
781 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
782 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
784 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
785 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
786 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
790 sharedData
= converterData
[type
];
791 umtx_lock(&cnvCacheMutex
);
792 isAlgorithmicConverter
= (UBool
)(sharedData
== NULL
|| sharedData
->referenceCounter
!= ~0);
793 umtx_unlock(&cnvCacheMutex
);
794 if (isAlgorithmicConverter
) {
795 /* not a valid type, or not an algorithmic converter */
796 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
797 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
801 cnv
= ucnv_createConverterFromSharedData(myUConverter
, (UConverterSharedData
*)sharedData
, "",
802 locale
!= NULL
? locale
: "", options
, err
);
804 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
809 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
811 char cnvName
[UCNV_MAX_CONVERTER_NAME_LENGTH
], locale
[ULOC_FULLNAME_CAPACITY
];
812 UConverter
*myUConverter
;
813 UConverterSharedData
*mySharedConverterData
;
815 UConverterLoadArgs args
={ 0 };
817 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
819 if(U_FAILURE(*err
)) {
820 UTRACE_EXIT_STATUS(*err
);
824 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
826 args
.size
=sizeof(UConverterLoadArgs
);
828 args
.pkg
=packageName
;
830 /* first, get the options out of the converterName string */
831 parseConverterOptions(converterName
, cnvName
, locale
, &args
.options
, err
);
832 if (U_FAILURE(*err
)) {
833 /* Very bad name used. */
834 UTRACE_EXIT_STATUS(*err
);
839 /* open the data, unflatten the shared structure */
840 mySharedConverterData
= createConverterFromFile(&args
, err
);
842 if (U_FAILURE(*err
)) {
843 UTRACE_EXIT_STATUS(*err
);
847 /* create the actual converter */
848 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, cnvName
, locale
, args
.options
, err
);
850 if (U_FAILURE(*err
)) {
851 ucnv_close(myUConverter
);
852 UTRACE_EXIT_STATUS(*err
);
856 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
862 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
863 UConverterSharedData
*mySharedConverterData
,
864 const char *realName
, const char *locale
, uint32_t options
,
869 if(myUConverter
== NULL
)
871 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
872 if(myUConverter
== NULL
)
874 *err
= U_MEMORY_ALLOCATION_ERROR
;
882 /* initialize the converter */
883 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
884 myUConverter
->isCopyLocal
= isCopyLocal
;
885 myUConverter
->isExtraLocal
= FALSE
;
886 myUConverter
->sharedData
= mySharedConverterData
;
887 myUConverter
->options
= options
;
888 myUConverter
->fromCharErrorBehaviour
= (UConverterToUCallback
) UCNV_TO_U_CALLBACK_SUBSTITUTE
;
889 myUConverter
->fromUCharErrorBehaviour
= (UConverterFromUCallback
) UCNV_FROM_U_CALLBACK_SUBSTITUTE
;
890 myUConverter
->toUnicodeStatus
= myUConverter
->sharedData
->toUnicodeStatus
;
891 myUConverter
->maxBytesPerUChar
= myUConverter
->sharedData
->staticData
->maxBytesPerChar
;
892 myUConverter
->subChar1
= myUConverter
->sharedData
->staticData
->subChar1
;
893 myUConverter
->subCharLen
= myUConverter
->sharedData
->staticData
->subCharLen
;
894 uprv_memcpy (myUConverter
->subChar
, myUConverter
->sharedData
->staticData
->subChar
, myUConverter
->subCharLen
);
895 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
897 if(myUConverter
!= NULL
&& myUConverter
->sharedData
->impl
->open
!= NULL
) {
898 myUConverter
->sharedData
->impl
->open(myUConverter
, realName
, locale
,options
, err
);
899 if(U_FAILURE(*err
)) {
900 ucnv_close(myUConverter
);
908 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
910 U_CAPI
int32_t U_EXPORT2
913 UConverterSharedData
*mySharedData
= NULL
;
915 int32_t tableDeletedNum
= 0;
916 const UHashElement
*e
;
917 UErrorCode status
= U_ILLEGAL_ARGUMENT_ERROR
;
918 int32_t i
, remaining
;
920 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
922 /* Close the default converter without creating a new one so that everything will be flushed. */
923 ucnv_close(u_getDefaultConverter(&status
));
925 /*if shared data hasn't even been lazy evaluated yet
928 if (SHARED_DATA_HASHTABLE
== NULL
) {
929 UTRACE_EXIT_VALUE((int32_t)0);
933 /*creates an enumeration to iterate through every element in the
936 * Synchronization: holding cnvCacheMutex will prevent any other thread from
937 * accessing or modifying the hash table during the iteration.
938 * The reference count of an entry may be decremented by
939 * ucnv_close while the iteration is in process, but this is
940 * benign. It can't be incremented (in ucnv_createConverter())
941 * because the sequence of looking up in the cache + incrementing
942 * is protected by cnvCacheMutex.
944 umtx_lock(&cnvCacheMutex
);
946 * double loop: A delta/extension-only converter has a pointer to its base table's
947 * shared data; the first iteration of the outer loop may see the delta converter
948 * before the base converter, and unloading the delta converter may get the base
949 * converter's reference counter down to 0.
955 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
957 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
958 /*deletes only if reference counter == 0 */
959 if (mySharedData
->referenceCounter
== 0)
963 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
965 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
966 mySharedData
->sharedDataCached
= FALSE
;
967 ucnv_deleteSharedConverterData (mySharedData
);
972 } while(++i
== 1 && remaining
> 0);
973 umtx_unlock(&cnvCacheMutex
);
975 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
977 ucnv_io_flushAvailableConverterCache();
979 UTRACE_EXIT_VALUE(tableDeletedNum
);
980 return tableDeletedNum
;
983 /* data swapping ------------------------------------------------------------ */
985 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
987 #if !UCONFIG_NO_LEGACY_CONVERSION
989 U_CAPI
int32_t U_EXPORT2
990 ucnv_swap(const UDataSwapper
*ds
,
991 const void *inData
, int32_t length
, void *outData
,
992 UErrorCode
*pErrorCode
) {
993 const UDataInfo
*pInfo
;
996 const uint8_t *inBytes
;
999 uint32_t offset
, count
, staticDataSize
;
1002 const UConverterStaticData
*inStaticData
;
1003 UConverterStaticData
*outStaticData
;
1005 const _MBCSHeader
*inMBCSHeader
;
1006 _MBCSHeader
*outMBCSHeader
;
1007 _MBCSHeader mbcsHeader
;
1010 const int32_t *inExtIndexes
;
1013 /* udata_swapDataHeader checks the arguments */
1014 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1015 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1019 /* check data format and format version */
1020 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1022 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1023 pInfo
->dataFormat
[1]==0x6e &&
1024 pInfo
->dataFormat
[2]==0x76 &&
1025 pInfo
->dataFormat
[3]==0x74 &&
1026 pInfo
->formatVersion
[0]==6 &&
1027 pInfo
->formatVersion
[1]>=2
1029 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",
1030 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1031 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1032 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1033 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1037 inBytes
=(const uint8_t *)inData
+headerSize
;
1038 outBytes
=(uint8_t *)outData
+headerSize
;
1040 /* read the initial UConverterStaticData structure after the UDataInfo header */
1041 inStaticData
=(const UConverterStaticData
*)inBytes
;
1042 outStaticData
=(UConverterStaticData
*)outBytes
;
1045 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1048 if( length
<sizeof(UConverterStaticData
) ||
1049 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1051 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1053 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1059 /* swap the static data */
1060 if(inStaticData
!=outStaticData
) {
1061 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1064 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1065 &outStaticData
->structSize
, pErrorCode
);
1066 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1067 &outStaticData
->codepage
, pErrorCode
);
1069 ds
->swapInvChars(ds
, inStaticData
->name
, uprv_strlen(inStaticData
->name
),
1070 outStaticData
->name
, pErrorCode
);
1071 if(U_FAILURE(*pErrorCode
)) {
1072 udata_printError(ds
, "ucnv_swap(): error swapping converter name - %s\n",
1073 u_errorName(*pErrorCode
));
1078 inBytes
+=staticDataSize
;
1079 outBytes
+=staticDataSize
;
1081 length
-=(int32_t)staticDataSize
;
1084 /* check for supported conversionType values */
1085 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1086 /* swap MBCS data */
1087 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1088 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1090 if(!(inMBCSHeader
->version
[0]==4 || inMBCSHeader
->version
[1]>=1)) {
1091 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1092 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1093 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1097 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1098 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1099 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1100 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1101 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1102 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1103 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1104 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1106 extOffset
=(int32_t)mbcsHeader
.flags
>>8;
1107 outputType
=(uint8_t)mbcsHeader
.flags
;
1109 /* make sure that the output type is known */
1110 switch(outputType
) {
1115 case MBCS_OUTPUT_3_EUC
:
1116 case MBCS_OUTPUT_4_EUC
:
1117 case MBCS_OUTPUT_2_SISO
:
1118 case MBCS_OUTPUT_EXT_ONLY
:
1122 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1124 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1128 /* calculate the length of the MBCS data */
1130 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsHeader
.fromUBytesLength
);
1132 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1135 /* there is extension data after the base data, see ucnv_ext.h */
1136 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1137 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1139 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1143 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1144 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1149 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1151 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1155 /* copy the data for inaccessible bytes */
1156 if(inBytes
!=outBytes
) {
1157 uprv_memcpy(outBytes
, inBytes
, size
);
1160 /* swap the MBCSHeader */
1161 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, 7*4,
1162 &outMBCSHeader
->countStates
, pErrorCode
);
1164 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1166 * extension-only file,
1167 * contains a base name instead of normal base table data
1170 /* swap the base name, between the header and the extension data */
1171 ds
->swapInvChars(ds
, inMBCSHeader
+1, uprv_strlen((const char *)(inMBCSHeader
+1)),
1172 outMBCSHeader
+1, pErrorCode
);
1174 /* normal file with base table data */
1176 /* swap the state table, 1kB per state */
1177 ds
->swapArray32(ds
, inMBCSHeader
+1, (int32_t)(mbcsHeader
.countStates
*1024),
1178 outMBCSHeader
+1, pErrorCode
);
1180 /* swap the toUFallbacks[] */
1181 offset
=sizeof(_MBCSHeader
)+mbcsHeader
.countStates
*1024;
1182 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)(mbcsHeader
.countToUFallbacks
*8),
1183 outBytes
+offset
, pErrorCode
);
1185 /* swap the unicodeCodeUnits[] */
1186 offset
=mbcsHeader
.offsetToUCodeUnits
;
1187 count
=mbcsHeader
.offsetFromUTable
-offset
;
1188 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1189 outBytes
+offset
, pErrorCode
);
1191 /* offset to the stage 1 table, independent of the outputType */
1192 offset
=mbcsHeader
.offsetFromUTable
;
1194 if(outputType
==MBCS_OUTPUT_1
) {
1195 /* SBCS: swap the fromU tables, all 16 bits wide */
1196 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1197 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1198 outBytes
+offset
, pErrorCode
);
1200 /* otherwise: swap the stage tables separately */
1202 /* stage 1 table: uint16_t[0x440 or 0x40] */
1203 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1204 count
=0x440*2; /* for all of Unicode */
1206 count
=0x40*2; /* only BMP */
1208 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1209 outBytes
+offset
, pErrorCode
);
1211 /* stage 2 table: uint32_t[] */
1213 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1214 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1215 outBytes
+offset
, pErrorCode
);
1217 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1218 offset
=mbcsHeader
.offsetFromUBytes
;
1219 count
=mbcsHeader
.fromUBytesLength
;
1220 switch(outputType
) {
1222 case MBCS_OUTPUT_3_EUC
:
1223 case MBCS_OUTPUT_2_SISO
:
1224 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1225 outBytes
+offset
, pErrorCode
);
1228 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1229 outBytes
+offset
, pErrorCode
);
1232 /* just uint8_t[], nothing to swap */
1239 /* swap the extension data */
1241 outBytes
+=extOffset
;
1243 /* swap toUTable[] */
1244 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1245 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1246 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1248 /* swap toUUChars[] */
1249 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1250 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1251 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1253 /* swap fromUTableUChars[] */
1254 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1255 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1256 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1258 /* swap fromUTableValues[] */
1259 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1260 /* same length as for fromUTableUChars[] */
1261 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1263 /* no need to swap fromUBytes[] */
1265 /* swap fromUStage12[] */
1266 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1267 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1268 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1270 /* swap fromUStage3[] */
1271 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1272 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1273 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1275 /* swap fromUStage3b[] */
1276 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1277 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1278 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1280 /* swap indexes[] */
1281 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1282 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1286 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1287 inStaticData
->conversionType
);
1288 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1292 return headerSize
+(int32_t)staticDataSize
+size
;
1295 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */