2 ********************************************************************
4 * Copyright (c) 1996-2008, 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"
48 extern void UCNV_DEBUG_LOG(char *what
, char *who
, void *p
, int l
);
49 #define UCNV_DEBUG_LOG(x,y,z) UCNV_DEBUG_LOG(x,y,z,__LINE__)
51 # define UCNV_DEBUG_LOG(x,y,z)
54 static const UConverterSharedData
* const
55 converterData
[UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
]={
58 #if UCONFIG_NO_LEGACY_CONVERSION
65 &_UTF8Data
, &_UTF16BEData
, &_UTF16LEData
, &_UTF32BEData
, &_UTF32LEData
,
68 #if UCONFIG_NO_LEGACY_CONVERSION
70 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
71 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
75 &_LMBCSData1
,&_LMBCSData2
, &_LMBCSData3
, &_LMBCSData4
, &_LMBCSData5
, &_LMBCSData6
,
76 &_LMBCSData8
,&_LMBCSData11
,&_LMBCSData16
,&_LMBCSData17
,&_LMBCSData18
,&_LMBCSData19
,
82 #if UCONFIG_NO_LEGACY_CONVERSION
89 &_UTF7Data
, &_Bocu1Data
, &_UTF16Data
, &_UTF32Data
, &_CESU8Data
, &_IMAPData
92 /* Please keep this in binary sorted order for getAlgorithmicTypeFromName.
93 Also the name should be in lower case and all spaces, dashes and underscores
98 const UConverterType type
;
99 } const cnvNameType
[] = {
100 { "bocu1", UCNV_BOCU1
},
101 { "cesu8", UCNV_CESU8
},
102 #if !UCONFIG_NO_LEGACY_CONVERSION
105 { "imapmailboxname", UCNV_IMAP_MAILBOX
},
106 #if !UCONFIG_NO_LEGACY_CONVERSION
107 { "iscii", UCNV_ISCII
},
108 { "iso2022", UCNV_ISO_2022
},
110 { "iso88591", UCNV_LATIN_1
},
111 #if !UCONFIG_NO_LEGACY_CONVERSION
112 { "lmbcs1", UCNV_LMBCS_1
},
113 { "lmbcs11",UCNV_LMBCS_11
},
114 { "lmbcs16",UCNV_LMBCS_16
},
115 { "lmbcs17",UCNV_LMBCS_17
},
116 { "lmbcs18",UCNV_LMBCS_18
},
117 { "lmbcs19",UCNV_LMBCS_19
},
118 { "lmbcs2", UCNV_LMBCS_2
},
119 { "lmbcs3", UCNV_LMBCS_3
},
120 { "lmbcs4", UCNV_LMBCS_4
},
121 { "lmbcs5", UCNV_LMBCS_5
},
122 { "lmbcs6", UCNV_LMBCS_6
},
123 { "lmbcs8", UCNV_LMBCS_8
},
125 { "scsu", UCNV_SCSU
},
126 { "usascii", UCNV_US_ASCII
},
127 { "utf16", UCNV_UTF16
},
128 { "utf16be", UCNV_UTF16_BigEndian
},
129 { "utf16le", UCNV_UTF16_LittleEndian
},
131 { "utf16oppositeendian", UCNV_UTF16_LittleEndian
},
132 { "utf16platformendian", UCNV_UTF16_BigEndian
},
134 { "utf16oppositeendian", UCNV_UTF16_BigEndian
},
135 { "utf16platformendian", UCNV_UTF16_LittleEndian
},
137 { "utf32", UCNV_UTF32
},
138 { "utf32be", UCNV_UTF32_BigEndian
},
139 { "utf32le", UCNV_UTF32_LittleEndian
},
141 { "utf32oppositeendian", UCNV_UTF32_LittleEndian
},
142 { "utf32platformendian", UCNV_UTF32_BigEndian
},
144 { "utf32oppositeendian", UCNV_UTF32_BigEndian
},
145 { "utf32platformendian", UCNV_UTF32_LittleEndian
},
147 { "utf7", UCNV_UTF7
},
148 { "utf8", UCNV_UTF8
}
152 /*initializes some global variables */
153 static UHashtable
*SHARED_DATA_HASHTABLE
= NULL
;
154 static UMTX cnvCacheMutex
= NULL
; /* Mutex for synchronizing cnv cache access. */
155 /* Note: the global mutex is used for */
156 /* reference count updates. */
158 static const char **gAvailableConverters
= NULL
;
159 static uint16_t gAvailableConverterCount
= 0;
161 /* This contains the resolved converter name. So no further alias lookup is needed again. */
162 static char gDefaultConverterNameBuffer
[UCNV_MAX_CONVERTER_NAME_LENGTH
+ 1]; /* +1 for NULL */
163 static const char *gDefaultConverterName
= NULL
;
166 If the default converter is an algorithmic converter, this is the cached value.
167 We don't cache a full UConverter and clone it because ucnv_clone doesn't have
168 less overhead than an algorithmic open. We don't cache non-algorithmic converters
169 because ucnv_flushCache must be able to unload the default converter and its table.
171 static const UConverterSharedData
*gDefaultAlgorithmicSharedData
= NULL
;
173 /* Does gDefaultConverterName have a converter option and require extra parsing? */
174 static UBool gDefaultConverterContainsOption
;
177 static const char DATA_TYPE
[] = "cnv";
180 ucnv_flushAvailableConverterCache() {
181 if (gAvailableConverters
) {
182 umtx_lock(&cnvCacheMutex
);
183 gAvailableConverterCount
= 0;
184 uprv_free((char **)gAvailableConverters
);
185 gAvailableConverters
= NULL
;
186 umtx_unlock(&cnvCacheMutex
);
190 /* ucnv_cleanup - delete all storage held by the converter cache, except any */
191 /* in use by open converters. */
192 /* Not thread safe. */
193 /* Not supported API. */
194 static UBool U_CALLCONV
ucnv_cleanup(void) {
196 if (SHARED_DATA_HASHTABLE
!= NULL
&& uhash_count(SHARED_DATA_HASHTABLE
) == 0) {
197 uhash_close(SHARED_DATA_HASHTABLE
);
198 SHARED_DATA_HASHTABLE
= NULL
;
201 /* Isn't called from flushCache because other threads may have preexisting references to the table. */
202 ucnv_flushAvailableConverterCache();
204 gDefaultConverterName
= NULL
;
205 gDefaultConverterNameBuffer
[0] = 0;
206 gDefaultConverterContainsOption
= FALSE
;
207 gDefaultAlgorithmicSharedData
= NULL
;
209 umtx_destroy(&cnvCacheMutex
); /* Don't worry about destroying the mutex even */
210 /* if the hash table still exists. The mutex */
211 /* will lazily re-init itself if needed. */
212 return (SHARED_DATA_HASHTABLE
== NULL
);
215 static UBool U_CALLCONV
216 isCnvAcceptable(void *context
,
217 const char *type
, const char *name
,
218 const UDataInfo
*pInfo
) {
221 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
222 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
223 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
224 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
225 pInfo
->dataFormat
[1]==0x6e &&
226 pInfo
->dataFormat
[2]==0x76 &&
227 pInfo
->dataFormat
[3]==0x74 &&
228 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
232 * Un flatten shared data from a UDATA..
234 static UConverterSharedData
*
235 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
237 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
238 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
239 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
240 UConverterSharedData
*data
;
241 UConverterType type
= (UConverterType
)source
->conversionType
;
243 if(U_FAILURE(*status
))
246 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
247 converterData
[type
] == NULL
||
248 converterData
[type
]->referenceCounter
!= 1 ||
249 source
->structSize
!= sizeof(UConverterStaticData
))
251 *status
= U_INVALID_TABLE_FORMAT
;
255 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
257 *status
= U_MEMORY_ALLOCATION_ERROR
;
261 /* copy initial values from the static structure for this type */
262 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
264 #if 0 /* made UConverterMBCSTable part of UConverterSharedData -- markus 20031107 */
266 * It would be much more efficient if the table were a direct member, not a pointer.
267 * However, that would add to the size of all UConverterSharedData objects
268 * even if they do not use this table (especially algorithmic ones).
269 * If this changes, then the static templates from converterData[type]
272 * In principle, it would be cleaner if the load() function below
273 * allocated the table.
275 data
->table
= (UConverterTable
*)uprv_malloc(sizeof(UConverterTable
));
276 if(data
->table
== NULL
) {
278 *status
= U_MEMORY_ALLOCATION_ERROR
;
281 uprv_memset(data
->table
, 0, sizeof(UConverterTable
));
284 data
->staticData
= source
;
286 data
->sharedDataCached
= FALSE
;
288 /* fill in fields from the loaded data */
289 data
->dataMemory
= (void*)pData
; /* for future use */
291 if(data
->impl
->load
!= NULL
) {
292 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
293 if(U_FAILURE(*status
)) {
294 uprv_free(data
->table
);
302 /*Takes an alias name gets an actual converter file name
303 *goes to disk and opens it.
304 *allocates the memory and returns a new UConverter object
306 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
309 UConverterSharedData
*sharedData
;
311 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
313 if (U_FAILURE (*err
)) {
314 UTRACE_EXIT_STATUS(*err
);
318 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
320 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
323 UTRACE_EXIT_STATUS(*err
);
327 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
331 UTRACE_EXIT_STATUS(*err
);
336 * TODO Store pkg in a field in the shared data so that delta-only converters
337 * can load base converters from the same package.
338 * If the pkg name is longer than the field, then either do not load the converter
339 * in the first place, or just set the pkg field to "".
342 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
346 /*returns a converter type from a string
348 static const UConverterSharedData
*
349 getAlgorithmicTypeFromName(const char *realName
)
351 uint32_t mid
, start
, limit
;
354 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
356 /* Lower case and remove ignoreable characters. */
357 ucnv_io_stripForCompare(strippedName
, realName
);
359 /* do a binary search for the alias */
361 limit
= sizeof(cnvNameType
)/sizeof(cnvNameType
[0]);
363 lastMid
= UINT32_MAX
;
366 mid
= (uint32_t)((start
+ limit
) / 2);
367 if (lastMid
== mid
) { /* Have we moved? */
368 break; /* We haven't moved, and it wasn't found. */
371 result
= uprv_strcmp(strippedName
, cnvNameType
[mid
].name
);
375 } else if (result
> 0) {
378 return converterData
[cnvNameType
[mid
].type
];
386 * Based on the number of known converters, this determines how many times larger
387 * the shared data hash table should be. When on small platforms, or just a couple
388 * of converters are used, this number should be 2. When memory is plentiful, or
389 * when ucnv_countAvailable is ever used with a lot of available converters,
391 * Larger numbers reduce the number of hash collisions, but use more memory.
393 #define UCNV_CACHE_LOAD_FACTOR 2
395 /* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
396 /* Will always be called with the cnvCacheMutex alrady being held */
397 /* by the calling function. */
398 /* Stores the shared data in the SHARED_DATA_HASHTABLE
399 * @param data The shared data
402 ucnv_shareConverterData(UConverterSharedData
* data
)
404 UErrorCode err
= U_ZERO_ERROR
;
405 /*Lazy evaluates the Hashtable itself */
406 /*void *sanity = NULL;*/
408 if (SHARED_DATA_HASHTABLE
== NULL
)
410 SHARED_DATA_HASHTABLE
= uhash_openSize(uhash_hashChars
, uhash_compareChars
, NULL
,
411 ucnv_io_countKnownConverters(&err
)*UCNV_CACHE_LOAD_FACTOR
,
413 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
419 /* ### check to see if the element is not already there! */
422 sanity = ucnv_getSharedConverterData (data->staticData->name);
425 UCNV_DEBUG_LOG("put:overwrite!",data->staticData->name,sanity);
427 UCNV_DEBUG_LOG("put:chk",data->staticData->name,sanity);
431 data
->sharedDataCached
= TRUE
;
433 uhash_put(SHARED_DATA_HASHTABLE
,
434 (void*) data
->staticData
->name
, /* Okay to cast away const as long as
435 keyDeleter == NULL */
438 UCNV_DEBUG_LOG("put", data
->staticData
->name
,data
);
442 /* Look up a converter name in the shared data cache. */
443 /* cnvCacheMutex must be held by the caller to protect the hash table. */
444 /* gets the shared data from the SHARED_DATA_HASHTABLE (might return NULL if it isn't there)
445 * @param name The name of the shared data
446 * @return the shared data from the SHARED_DATA_HASHTABLE
448 static UConverterSharedData
*
449 ucnv_getSharedConverterData(const char *name
)
451 /*special case when no Table has yet been created we return NULL */
452 if (SHARED_DATA_HASHTABLE
== NULL
)
458 UConverterSharedData
*rc
;
460 rc
= (UConverterSharedData
*)uhash_get(SHARED_DATA_HASHTABLE
, name
);
461 UCNV_DEBUG_LOG("get",name
,rc
);
466 /*frees the string of memory blocks associates with a sharedConverter
467 *if and only if the referenceCounter == 0
469 /* Deletes (frees) the Shared data it's passed. first it checks the referenceCounter to
470 * see if anyone is using it, if not it frees all the memory stemming from sharedConverterData and
472 * otherwise returns FALSE
473 * @param sharedConverterData The shared data
474 * @return if not it frees all the memory stemming from sharedConverterData and
475 * returns TRUE, otherwise returns FALSE
478 ucnv_deleteSharedConverterData(UConverterSharedData
* deadSharedData
)
480 UTRACE_ENTRY_OC(UTRACE_UCNV_UNLOAD
);
481 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "unload converter %s shared data %p", deadSharedData
->staticData
->name
, deadSharedData
);
483 if (deadSharedData
->referenceCounter
> 0) {
484 UTRACE_EXIT_VALUE((int32_t)FALSE
);
488 if (deadSharedData
->impl
->unload
!= NULL
) {
489 deadSharedData
->impl
->unload(deadSharedData
);
492 if(deadSharedData
->dataMemory
!= NULL
)
494 UDataMemory
*data
= (UDataMemory
*)deadSharedData
->dataMemory
;
498 if(deadSharedData
->table
!= NULL
)
500 uprv_free(deadSharedData
->table
);
504 /* if the static data is actually owned by the shared data */
505 /* enable if we ever have this situation. */
506 if(deadSharedData
->staticDataOwned
== TRUE
) /* see ucnv_bld.h */
508 uprv_free((void*)deadSharedData
->staticData
);
514 uprv_memset(deadSharedData
->0, sizeof(*deadSharedData
));
517 uprv_free(deadSharedData
);
519 UTRACE_EXIT_VALUE((int32_t)TRUE
);
524 * Load a non-algorithmic converter.
525 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
527 UConverterSharedData
*
528 ucnv_load(UConverterLoadArgs
*pArgs
, UErrorCode
*err
) {
529 UConverterSharedData
*mySharedConverterData
;
531 if(err
== NULL
|| U_FAILURE(*err
)) {
535 if(pArgs
->pkg
!= NULL
&& *pArgs
->pkg
!= 0) {
536 /* application-provided converters are not currently cached */
537 return createConverterFromFile(pArgs
, err
);
540 mySharedConverterData
= ucnv_getSharedConverterData(pArgs
->name
);
541 if (mySharedConverterData
== NULL
)
543 /*Not cached, we need to stream it in from file */
544 mySharedConverterData
= createConverterFromFile(pArgs
, err
);
545 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
551 /* share it with other library clients */
552 ucnv_shareConverterData(mySharedConverterData
);
557 /* The data for this converter was already in the cache. */
558 /* Update the reference counter on the shared data: one more client */
559 mySharedConverterData
->referenceCounter
++;
562 return mySharedConverterData
;
566 * Unload a non-algorithmic converter.
567 * It must be sharedData->referenceCounter != ~0
568 * and this function must be called inside umtx_lock(&cnvCacheMutex).
571 ucnv_unload(UConverterSharedData
*sharedData
) {
572 if(sharedData
!= NULL
) {
573 if (sharedData
->referenceCounter
> 0) {
574 sharedData
->referenceCounter
--;
577 if((sharedData
->referenceCounter
<= 0)&&(sharedData
->sharedDataCached
== FALSE
)) {
578 ucnv_deleteSharedConverterData(sharedData
);
584 ucnv_unloadSharedDataIfReady(UConverterSharedData
*sharedData
)
587 Checking whether it's an algorithic converter is okay
588 in multithreaded applications because the value never changes.
589 Don't check referenceCounter for any other value.
591 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= ~0) {
592 umtx_lock(&cnvCacheMutex
);
593 ucnv_unload(sharedData
);
594 umtx_unlock(&cnvCacheMutex
);
599 ucnv_incrementRefCount(UConverterSharedData
*sharedData
)
601 if(sharedData
!= NULL
&& sharedData
->referenceCounter
!= ~0) {
602 umtx_lock(&cnvCacheMutex
);
603 sharedData
->referenceCounter
++;
604 umtx_unlock(&cnvCacheMutex
);
609 parseConverterOptions(const char *inName
,
618 /* copy the converter name itself to cnvName */
619 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
620 if (++len
>=UCNV_MAX_CONVERTER_NAME_LENGTH
) {
621 *err
= U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
630 /* parse options. No more name copying should occur. */
631 while((c
=*inName
)!=0) {
632 if(c
==UCNV_OPTION_SEP_CHAR
) {
636 /* inName is behind an option separator */
637 if(uprv_strncmp(inName
, "locale=", 7)==0) {
638 /* do not modify locale itself in case we have multiple locale options */
641 /* copy the locale option value */
644 while((c
=*inName
)!=0 && c
!=UCNV_OPTION_SEP_CHAR
) {
647 if(++len
>=ULOC_FULLNAME_CAPACITY
) {
648 *err
=U_ILLEGAL_ARGUMENT_ERROR
; /* bad name */
656 } else if(uprv_strncmp(inName
, "version=", 8)==0) {
657 /* copy the version option value into bits 3..0 of *pFlags */
661 *pFlags
&=~UCNV_OPTION_VERSION
;
663 } else if((uint8_t)(c
-'0')<10) {
664 *pFlags
=(*pFlags
&~UCNV_OPTION_VERSION
)|(uint32_t)(c
-'0');
667 } else if(uprv_strncmp(inName
, "swaplfnl", 8)==0) {
669 *pFlags
|=UCNV_OPTION_SWAP_LFNL
;
670 /* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
672 /* ignore any other options until we define some */
673 while(((c
= *inName
++) != 0) && (c
!= UCNV_OPTION_SEP_CHAR
)) {
682 /*Logic determines if the converter is Algorithmic AND/OR cached
684 * -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
685 * -Get it from a Hashtable (Data=X, Cached=TRUE)
686 * -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
687 * -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
689 UConverterSharedData
*
690 ucnv_loadSharedData(const char *converterName
, UConverterLookupData
*lookup
, UErrorCode
* err
) {
691 UConverterLookupData stackLookup
;
692 UConverterSharedData
*mySharedConverterData
= NULL
;
693 UErrorCode internalErrorCode
= U_ZERO_ERROR
;
694 UBool mayContainOption
= TRUE
;
695 UBool checkForAlgorithmic
= TRUE
;
697 if (U_FAILURE (*err
)) {
702 lookup
= &stackLookup
;
705 lookup
->locale
[0] = 0;
708 /* In case "name" is NULL we want to open the default converter. */
709 if (converterName
== NULL
) {
710 /* Call ucnv_getDefaultName first to query the name from the OS. */
711 lookup
->realName
= ucnv_getDefaultName();
712 if (lookup
->realName
== NULL
) {
713 *err
= U_MISSING_RESOURCE_ERROR
;
716 mySharedConverterData
= (UConverterSharedData
*)gDefaultAlgorithmicSharedData
;
717 checkForAlgorithmic
= FALSE
;
718 mayContainOption
= gDefaultConverterContainsOption
;
719 /* the default converter name is already canonical */
721 else if((converterName
[0] == 'U' ?
722 ( converterName
[1] == 'T' && converterName
[2] == 'F') :
723 (converterName
[0] == 'u' && converterName
[1] == 't' && converterName
[2] == 'f'))
725 (converterName
[3] == '-' ?
726 (converterName
[4] == '8' && converterName
[5] == 0) :
727 (converterName
[3] == '8' && converterName
[4] == 0)))
729 /* fastpath for UTF-8 */
730 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
733 /* separate the converter name from the options */
734 parseConverterOptions(converterName
, lookup
->cnvName
, lookup
->locale
, &lookup
->options
, err
);
735 if (U_FAILURE(*err
)) {
736 /* Very bad name used. */
740 /* get the canonical converter name */
741 lookup
->realName
= ucnv_io_getConverterName(lookup
->cnvName
, &mayContainOption
, &internalErrorCode
);
742 if (U_FAILURE(internalErrorCode
) || lookup
->realName
== NULL
) {
744 * set the input name in case the converter was added
745 * without updating the alias table, or when there is no alias table
747 lookup
->realName
= lookup
->cnvName
;
751 /* separate the converter name from the options */
752 if(mayContainOption
&& lookup
->realName
!= lookup
->cnvName
) {
753 parseConverterOptions(lookup
->realName
, lookup
->cnvName
, lookup
->locale
, &lookup
->options
, err
);
754 lookup
->realName
= lookup
->cnvName
;
757 /* get the shared data for an algorithmic converter, if it is one */
758 if (checkForAlgorithmic
) {
759 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(lookup
->realName
);
761 if (mySharedConverterData
== NULL
)
763 /* it is a data-based converter, get its shared data. */
764 /* Hold the cnvCacheMutex through the whole process of checking the */
765 /* converter data cache, and adding new entries to the cache */
766 /* to prevent other threads from modifying the cache during the */
768 UConverterLoadArgs args
={ 0 };
770 args
.size
=sizeof(UConverterLoadArgs
);
772 args
.options
=lookup
->options
;
774 args
.name
=lookup
->realName
;
776 umtx_lock(&cnvCacheMutex
);
777 mySharedConverterData
= ucnv_load(&args
, err
);
778 umtx_unlock(&cnvCacheMutex
);
779 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
785 return mySharedConverterData
;
789 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
791 UConverterLookupData stackLookup
;
792 UConverterSharedData
*mySharedConverterData
;
794 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
796 if(U_SUCCESS(*err
)) {
797 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
799 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackLookup
, err
);
801 if(U_SUCCESS(*err
)) {
802 myUConverter
= ucnv_createConverterFromSharedData(
803 myUConverter
, mySharedConverterData
,
804 stackLookup
.realName
, stackLookup
.locale
, stackLookup
.options
,
807 if(U_SUCCESS(*err
)) {
808 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
812 else mySharedConverterData was already cleaned up by
813 ucnv_createConverterFromSharedData.
816 ucnv_unloadSharedDataIfReady(mySharedConverterData);
821 /* exit with error */
822 UTRACE_EXIT_STATUS(*err
);
827 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
829 const char *locale
, uint32_t options
,
832 const UConverterSharedData
*sharedData
;
833 UBool isAlgorithmicConverter
;
835 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
836 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
838 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
839 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
840 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
844 sharedData
= converterData
[type
];
845 umtx_lock(&cnvCacheMutex
);
846 isAlgorithmicConverter
= (UBool
)(sharedData
== NULL
|| sharedData
->referenceCounter
!= ~0);
847 umtx_unlock(&cnvCacheMutex
);
848 if (isAlgorithmicConverter
) {
849 /* not a valid type, or not an algorithmic converter */
850 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
851 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
855 cnv
= ucnv_createConverterFromSharedData(myUConverter
, (UConverterSharedData
*)sharedData
, "",
856 locale
!= NULL
? locale
: "", options
, err
);
858 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
863 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
865 char cnvName
[UCNV_MAX_CONVERTER_NAME_LENGTH
], locale
[ULOC_FULLNAME_CAPACITY
];
866 UConverter
*myUConverter
;
867 UConverterSharedData
*mySharedConverterData
;
869 UConverterLoadArgs args
={ 0 };
871 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
873 if(U_FAILURE(*err
)) {
874 UTRACE_EXIT_STATUS(*err
);
878 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
880 args
.size
=sizeof(UConverterLoadArgs
);
882 args
.pkg
=packageName
;
884 /* first, get the options out of the converterName string */
885 parseConverterOptions(converterName
, cnvName
, locale
, &args
.options
, err
);
886 if (U_FAILURE(*err
)) {
887 /* Very bad name used. */
888 UTRACE_EXIT_STATUS(*err
);
893 /* open the data, unflatten the shared structure */
894 mySharedConverterData
= createConverterFromFile(&args
, err
);
896 if (U_FAILURE(*err
)) {
897 UTRACE_EXIT_STATUS(*err
);
901 /* create the actual converter */
902 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, cnvName
, locale
, args
.options
, err
);
904 if (U_FAILURE(*err
)) {
905 ucnv_close(myUConverter
);
906 UTRACE_EXIT_STATUS(*err
);
910 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
916 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
917 UConverterSharedData
*mySharedConverterData
,
918 const char *realName
, const char *locale
, uint32_t options
,
923 if(myUConverter
== NULL
)
925 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
926 if(myUConverter
== NULL
)
928 *err
= U_MEMORY_ALLOCATION_ERROR
;
936 /* initialize the converter */
937 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
938 myUConverter
->isCopyLocal
= isCopyLocal
;
939 /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
940 myUConverter
->sharedData
= mySharedConverterData
;
941 myUConverter
->options
= options
;
942 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
943 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_DEFAULT_CALLBACK
;
944 myUConverter
->fromUCharErrorBehaviour
= UCNV_FROM_U_DEFAULT_CALLBACK
;
945 myUConverter
->toUnicodeStatus
= mySharedConverterData
->toUnicodeStatus
;
946 myUConverter
->maxBytesPerUChar
= mySharedConverterData
->staticData
->maxBytesPerChar
;
947 myUConverter
->subChar1
= mySharedConverterData
->staticData
->subChar1
;
948 myUConverter
->subCharLen
= mySharedConverterData
->staticData
->subCharLen
;
949 myUConverter
->subChars
= (uint8_t *)myUConverter
->subUChars
;
950 uprv_memcpy(myUConverter
->subChars
, mySharedConverterData
->staticData
->subChar
, myUConverter
->subCharLen
);
951 myUConverter
->toUCallbackReason
= UCNV_ILLEGAL
; /* default reason to invoke (*fromCharErrorBehaviour) */
953 if(mySharedConverterData
->impl
->open
!= NULL
) {
954 mySharedConverterData
->impl
->open(myUConverter
, realName
, locale
, options
, err
);
955 if(U_FAILURE(*err
)) {
956 ucnv_close(myUConverter
);
964 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
966 U_CAPI
int32_t U_EXPORT2
969 UConverterSharedData
*mySharedData
= NULL
;
971 int32_t tableDeletedNum
= 0;
972 const UHashElement
*e
;
973 UErrorCode status
= U_ILLEGAL_ARGUMENT_ERROR
;
974 int32_t i
, remaining
;
976 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
978 /* Close the default converter without creating a new one so that everything will be flushed. */
979 ucnv_close(u_getDefaultConverter(&status
));
981 /*if shared data hasn't even been lazy evaluated yet
984 if (SHARED_DATA_HASHTABLE
== NULL
) {
985 UTRACE_EXIT_VALUE((int32_t)0);
989 /*creates an enumeration to iterate through every element in the
992 * Synchronization: holding cnvCacheMutex will prevent any other thread from
993 * accessing or modifying the hash table during the iteration.
994 * The reference count of an entry may be decremented by
995 * ucnv_close while the iteration is in process, but this is
996 * benign. It can't be incremented (in ucnv_createConverter())
997 * because the sequence of looking up in the cache + incrementing
998 * is protected by cnvCacheMutex.
1000 umtx_lock(&cnvCacheMutex
);
1002 * double loop: A delta/extension-only converter has a pointer to its base table's
1003 * shared data; the first iteration of the outer loop may see the delta converter
1004 * before the base converter, and unloading the delta converter may get the base
1005 * converter's reference counter down to 0.
1011 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
1013 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
1014 /*deletes only if reference counter == 0 */
1015 if (mySharedData
->referenceCounter
== 0)
1019 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
1021 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
1022 mySharedData
->sharedDataCached
= FALSE
;
1023 ucnv_deleteSharedConverterData (mySharedData
);
1028 } while(++i
== 1 && remaining
> 0);
1029 umtx_unlock(&cnvCacheMutex
);
1031 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
1033 UTRACE_EXIT_VALUE(tableDeletedNum
);
1034 return tableDeletedNum
;
1037 /* available converters list --------------------------------------------------- */
1039 static UBool
haveAvailableConverterList(UErrorCode
*pErrorCode
) {
1041 UMTX_CHECK(&cnvCacheMutex
, (gAvailableConverters
== NULL
), needInit
);
1043 UConverter tempConverter
;
1044 UEnumeration
*allConvEnum
= NULL
;
1046 uint16_t localConverterCount
;
1047 uint16_t allConverterCount
;
1048 UErrorCode localStatus
;
1049 const char *converterName
;
1050 const char **localConverterList
;
1052 allConvEnum
= ucnv_openAllNames(pErrorCode
);
1053 allConverterCount
= uenum_count(allConvEnum
, pErrorCode
);
1054 if (U_FAILURE(*pErrorCode
)) {
1058 /* We can't have more than "*converterTable" converters to open */
1059 localConverterList
= (const char **) uprv_malloc(allConverterCount
* sizeof(char*));
1060 if (!localConverterList
) {
1061 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1065 /* Open the default converter to make sure that it has first dibs in the hash table. */
1066 localStatus
= U_ZERO_ERROR
;
1067 ucnv_close(ucnv_createConverter(&tempConverter
, NULL
, &localStatus
));
1069 localConverterCount
= 0;
1071 for (idx
= 0; idx
< allConverterCount
; idx
++) {
1072 localStatus
= U_ZERO_ERROR
;
1073 converterName
= uenum_next(allConvEnum
, NULL
, &localStatus
);
1074 ucnv_close(ucnv_createConverter(&tempConverter
, converterName
, &localStatus
));
1075 if (U_SUCCESS(localStatus
)) {
1076 localConverterList
[localConverterCount
++] = converterName
;
1079 uenum_close(allConvEnum
);
1081 umtx_lock(&cnvCacheMutex
);
1082 if (gAvailableConverters
== NULL
) {
1083 gAvailableConverters
= localConverterList
;
1084 gAvailableConverterCount
= localConverterCount
;
1085 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1088 uprv_free((char **)localConverterList
);
1090 umtx_unlock(&cnvCacheMutex
);
1096 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
) {
1097 if (haveAvailableConverterList(pErrorCode
)) {
1098 return gAvailableConverterCount
;
1103 U_CFUNC
const char *
1104 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
) {
1105 if (haveAvailableConverterList(pErrorCode
)) {
1106 if (n
< gAvailableConverterCount
) {
1107 return gAvailableConverters
[n
];
1109 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
1114 /* default converter name --------------------------------------------------- */
1117 Copy the canonical converter name.
1118 ucnv_getDefaultName must be thread safe, which can call this function.
1120 ucnv_setDefaultName calls this function and it doesn't have to be
1121 thread safe because there is no reliable/safe way to reset the
1122 converter in use in all threads. If you did reset the converter, you
1123 would not be sure that retrieving a default converter for one string
1124 would be the same type of default converter for a successive string.
1125 Since the name is a returned via ucnv_getDefaultName without copying,
1126 you shouldn't be modifying or deleting the string from a separate thread.
1128 static U_INLINE
void
1129 internalSetName(const char *name
, UErrorCode
*status
) {
1130 UConverterLookupData lookup
;
1131 int32_t length
=(int32_t)(uprv_strlen(name
));
1132 UBool containsOption
= (UBool
)(uprv_strchr(name
, UCNV_OPTION_SEP_CHAR
) != NULL
);
1133 const UConverterSharedData
*algorithmicSharedData
;
1135 lookup
.locale
[0] = 0;
1137 lookup
.realName
= name
;
1138 if(containsOption
) {
1139 parseConverterOptions(lookup
.realName
, lookup
.cnvName
, lookup
.locale
, &lookup
.options
, status
);
1140 lookup
.realName
= lookup
.cnvName
;
1142 algorithmicSharedData
= getAlgorithmicTypeFromName(lookup
.realName
);
1144 umtx_lock(&cnvCacheMutex
);
1146 gDefaultAlgorithmicSharedData
= algorithmicSharedData
;
1147 gDefaultConverterContainsOption
= containsOption
;
1148 uprv_memcpy(gDefaultConverterNameBuffer
, name
, length
);
1149 gDefaultConverterNameBuffer
[length
]=0;
1150 gDefaultConverterName
= gDefaultConverterNameBuffer
;
1152 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1154 umtx_unlock(&cnvCacheMutex
);
1158 * In order to be really thread-safe, the get function would have to take
1159 * a buffer parameter and copy the current string inside a mutex block.
1160 * This implementation only tries to be really thread-safe while
1162 * It assumes that setting a pointer is atomic.
1165 U_CAPI
const char* U_EXPORT2
1166 ucnv_getDefaultName() {
1167 /* local variable to be thread-safe */
1171 Multiple calls to ucnv_getDefaultName must be thread safe,
1172 but ucnv_setDefaultName is not thread safe.
1174 UMTX_CHECK(&cnvCacheMutex
, gDefaultConverterName
, name
);
1176 UErrorCode errorCode
= U_ZERO_ERROR
;
1177 UConverter
*cnv
= NULL
;
1179 name
= uprv_getDefaultCodepage();
1181 /* if the name is there, test it out and get the canonical name with options */
1183 cnv
= ucnv_open(name
, &errorCode
);
1184 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1185 name
= ucnv_getName(cnv
, &errorCode
);
1189 if(name
== NULL
|| name
[0] == 0
1190 || U_FAILURE(errorCode
) || cnv
== NULL
1191 || uprv_strlen(name
)>=sizeof(gDefaultConverterNameBuffer
))
1193 /* Panic time, let's use a fallback. */
1194 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1196 /* there is no 'algorithmic' converter for EBCDIC */
1197 #elif defined(OS390)
1198 name
= "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING
;
1200 name
= "ibm-37_P100-1995";
1204 internalSetName(name
, &errorCode
);
1206 /* The close may make the current name go away. */
1214 This function is not thread safe, and it can't be thread safe.
1215 See internalSetName or the API reference for details.
1217 U_CAPI
void U_EXPORT2
1218 ucnv_setDefaultName(const char *converterName
) {
1219 if(converterName
==NULL
) {
1220 /* reset to the default codepage */
1221 gDefaultConverterName
=NULL
;
1223 UErrorCode errorCode
= U_ZERO_ERROR
;
1224 UConverter
*cnv
= NULL
;
1225 const char *name
= NULL
;
1227 /* if the name is there, test it out and get the canonical name with options */
1228 cnv
= ucnv_open(converterName
, &errorCode
);
1229 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1230 name
= ucnv_getName(cnv
, &errorCode
);
1233 if(U_SUCCESS(errorCode
) && name
!=NULL
) {
1234 internalSetName(name
, &errorCode
);
1236 /* else this converter is bad to use. Don't change it to a bad value. */
1238 /* The close may make the current name go away. */
1243 /* data swapping ------------------------------------------------------------ */
1245 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
1247 #if !UCONFIG_NO_LEGACY_CONVERSION
1249 U_CAPI
int32_t U_EXPORT2
1250 ucnv_swap(const UDataSwapper
*ds
,
1251 const void *inData
, int32_t length
, void *outData
,
1252 UErrorCode
*pErrorCode
) {
1253 const UDataInfo
*pInfo
;
1256 const uint8_t *inBytes
;
1259 uint32_t offset
, count
, staticDataSize
;
1262 const UConverterStaticData
*inStaticData
;
1263 UConverterStaticData
*outStaticData
;
1265 const _MBCSHeader
*inMBCSHeader
;
1266 _MBCSHeader
*outMBCSHeader
;
1267 _MBCSHeader mbcsHeader
;
1268 uint32_t mbcsHeaderLength
;
1269 UBool noFromU
=FALSE
;
1273 int32_t maxFastUChar
, mbcsIndexLength
;
1275 const int32_t *inExtIndexes
;
1278 /* udata_swapDataHeader checks the arguments */
1279 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1280 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1284 /* check data format and format version */
1285 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1287 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1288 pInfo
->dataFormat
[1]==0x6e &&
1289 pInfo
->dataFormat
[2]==0x76 &&
1290 pInfo
->dataFormat
[3]==0x74 &&
1291 pInfo
->formatVersion
[0]==6 &&
1292 pInfo
->formatVersion
[1]>=2
1294 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",
1295 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1296 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1297 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1298 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1302 inBytes
=(const uint8_t *)inData
+headerSize
;
1303 outBytes
=(uint8_t *)outData
+headerSize
;
1305 /* read the initial UConverterStaticData structure after the UDataInfo header */
1306 inStaticData
=(const UConverterStaticData
*)inBytes
;
1307 outStaticData
=(UConverterStaticData
*)outBytes
;
1310 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1313 if( length
<sizeof(UConverterStaticData
) ||
1314 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1316 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1318 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1324 /* swap the static data */
1325 if(inStaticData
!=outStaticData
) {
1326 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1329 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1330 &outStaticData
->structSize
, pErrorCode
);
1331 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1332 &outStaticData
->codepage
, pErrorCode
);
1334 ds
->swapInvChars(ds
, inStaticData
->name
, (int32_t)uprv_strlen(inStaticData
->name
),
1335 outStaticData
->name
, pErrorCode
);
1336 if(U_FAILURE(*pErrorCode
)) {
1337 udata_printError(ds
, "ucnv_swap(): error swapping converter name\n");
1342 inBytes
+=staticDataSize
;
1343 outBytes
+=staticDataSize
;
1345 length
-=(int32_t)staticDataSize
;
1348 /* check for supported conversionType values */
1349 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1350 /* swap MBCS data */
1351 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1352 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1354 if(0<=length
&& length
<sizeof(_MBCSHeader
)) {
1355 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1357 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1360 if(inMBCSHeader
->version
[0]==4 && inMBCSHeader
->version
[1]>=1) {
1361 mbcsHeaderLength
=MBCS_HEADER_V4_LENGTH
;
1362 } else if(inMBCSHeader
->version
[0]==5 && inMBCSHeader
->version
[1]>=3 &&
1363 ((mbcsHeader
.options
=ds
->readUInt32(inMBCSHeader
->options
))&
1364 MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0
1366 mbcsHeaderLength
=mbcsHeader
.options
&MBCS_OPT_LENGTH_MASK
;
1367 noFromU
=(UBool
)((mbcsHeader
.options
&MBCS_OPT_NO_FROM_U
)!=0);
1369 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1370 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1371 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1375 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1376 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1377 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1378 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1379 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1380 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1381 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1382 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1383 /* mbcsHeader.options have been read above */
1385 extOffset
=(int32_t)(mbcsHeader
.flags
>>8);
1386 outputType
=(uint8_t)mbcsHeader
.flags
;
1387 if(noFromU
&& outputType
==MBCS_OUTPUT_1
) {
1388 udata_printError(ds
, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
1389 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1393 /* make sure that the output type is known */
1394 switch(outputType
) {
1399 case MBCS_OUTPUT_3_EUC
:
1400 case MBCS_OUTPUT_4_EUC
:
1401 case MBCS_OUTPUT_2_SISO
:
1402 case MBCS_OUTPUT_EXT_ONLY
:
1406 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1408 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1412 /* calculate the length of the MBCS data */
1415 * utf8Friendly MBCS files (mbcsHeader.version 4.3)
1416 * contain an additional mbcsIndex table:
1417 * uint16_t[(maxFastUChar+1)>>6];
1418 * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
1422 if( outputType
!=MBCS_OUTPUT_EXT_ONLY
&& outputType
!=MBCS_OUTPUT_1
&&
1423 mbcsHeader
.version
[1]>=3 && (maxFastUChar
=mbcsHeader
.version
[2])!=0
1425 maxFastUChar
=(maxFastUChar
<<8)|0xff;
1426 mbcsIndexLength
=((maxFastUChar
+1)>>6)*2; /* number of bytes */
1430 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsIndexLength
);
1432 size
+=(int32_t)mbcsHeader
.fromUBytesLength
;
1435 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1438 /* there is extension data after the base data, see ucnv_ext.h */
1439 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1440 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1442 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1446 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1447 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1452 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1454 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1458 /* copy the data for inaccessible bytes */
1459 if(inBytes
!=outBytes
) {
1460 uprv_memcpy(outBytes
, inBytes
, size
);
1463 /* swap the MBCSHeader, except for the version field */
1464 count
=mbcsHeaderLength
*4;
1465 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, count
-4,
1466 &outMBCSHeader
->countStates
, pErrorCode
);
1468 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1470 * extension-only file,
1471 * contains a base name instead of normal base table data
1474 /* swap the base name, between the header and the extension data */
1475 const char *inBaseName
=(const char *)inBytes
+count
;
1476 char *outBaseName
=(char *)outBytes
+count
;
1477 ds
->swapInvChars(ds
, inBaseName
, (int32_t)uprv_strlen(inBaseName
),
1478 outBaseName
, pErrorCode
);
1480 /* normal file with base table data */
1482 /* swap the state table, 1kB per state */
1484 count
=mbcsHeader
.countStates
*1024;
1485 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1486 outBytes
+offset
, pErrorCode
);
1488 /* swap the toUFallbacks[] */
1490 count
=mbcsHeader
.countToUFallbacks
*8;
1491 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1492 outBytes
+offset
, pErrorCode
);
1494 /* swap the unicodeCodeUnits[] */
1495 offset
=mbcsHeader
.offsetToUCodeUnits
;
1496 count
=mbcsHeader
.offsetFromUTable
-offset
;
1497 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1498 outBytes
+offset
, pErrorCode
);
1500 /* offset to the stage 1 table, independent of the outputType */
1501 offset
=mbcsHeader
.offsetFromUTable
;
1503 if(outputType
==MBCS_OUTPUT_1
) {
1504 /* SBCS: swap the fromU tables, all 16 bits wide */
1505 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1506 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1507 outBytes
+offset
, pErrorCode
);
1509 /* otherwise: swap the stage tables separately */
1511 /* stage 1 table: uint16_t[0x440 or 0x40] */
1512 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1513 count
=0x440*2; /* for all of Unicode */
1515 count
=0x40*2; /* only BMP */
1517 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1518 outBytes
+offset
, pErrorCode
);
1520 /* stage 2 table: uint32_t[] */
1522 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1523 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1524 outBytes
+offset
, pErrorCode
);
1526 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1527 offset
=mbcsHeader
.offsetFromUBytes
;
1528 count
= noFromU
? 0 : mbcsHeader
.fromUBytesLength
;
1529 switch(outputType
) {
1531 case MBCS_OUTPUT_3_EUC
:
1532 case MBCS_OUTPUT_2_SISO
:
1533 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1534 outBytes
+offset
, pErrorCode
);
1537 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1538 outBytes
+offset
, pErrorCode
);
1541 /* just uint8_t[], nothing to swap */
1545 if(mbcsIndexLength
!=0) {
1547 count
=mbcsIndexLength
;
1548 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1549 outBytes
+offset
, pErrorCode
);
1555 /* swap the extension data */
1557 outBytes
+=extOffset
;
1559 /* swap toUTable[] */
1560 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1561 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1562 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1564 /* swap toUUChars[] */
1565 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1566 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1567 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1569 /* swap fromUTableUChars[] */
1570 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1571 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1572 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1574 /* swap fromUTableValues[] */
1575 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1576 /* same length as for fromUTableUChars[] */
1577 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1579 /* no need to swap fromUBytes[] */
1581 /* swap fromUStage12[] */
1582 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1583 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1584 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1586 /* swap fromUStage3[] */
1587 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1588 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1589 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1591 /* swap fromUStage3b[] */
1592 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1593 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1594 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1596 /* swap indexes[] */
1597 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1598 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1602 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1603 inStaticData
->conversionType
);
1604 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1608 return headerSize
+(int32_t)staticDataSize
+size
;
1611 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */