2 ********************************************************************
4 * Copyright (c) 1996-2006, 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 static char gDefaultConverterNameBuffer
[UCNV_MAX_CONVERTER_NAME_LENGTH
+ 1]; /* +1 for NULL */
162 static const char *gDefaultConverterName
= NULL
;
163 static const UConverterSharedData
*gDefaultAlgorithmicSharedData
= NULL
;
164 static UBool gDefaultConverterContainsOption
;
167 static const char DATA_TYPE
[] = "cnv";
169 /* ucnv_cleanup - delete all storage held by the converter cache, except any in use */
170 /* by open converters. */
171 /* Not thread safe. */
172 /* Not supported API. Marked U_CAPI only for use by test programs. */
173 static UBool U_CALLCONV
ucnv_cleanup(void) {
174 if (SHARED_DATA_HASHTABLE
!= NULL
) {
176 if (SHARED_DATA_HASHTABLE
!= NULL
&& uhash_count(SHARED_DATA_HASHTABLE
) == 0) {
177 uhash_close(SHARED_DATA_HASHTABLE
);
178 SHARED_DATA_HASHTABLE
= NULL
;
182 /* Called from ucnv_flushCache because it allocates the hashtable */
183 /*ucnv_flushAvailableConverterCache();*/
185 gDefaultConverterName
= NULL
;
186 gDefaultConverterNameBuffer
[0] = 0;
187 gDefaultConverterContainsOption
= FALSE
;
188 gDefaultAlgorithmicSharedData
= NULL
;
190 umtx_destroy(&cnvCacheMutex
); /* Don't worry about destroying the mutex even */
191 /* if the hash table still exists. The mutex */
192 /* will lazily re-init itself if needed. */
193 return (SHARED_DATA_HASHTABLE
== NULL
);
196 static UBool U_CALLCONV
197 isCnvAcceptable(void *context
,
198 const char *type
, const char *name
,
199 const UDataInfo
*pInfo
) {
202 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
203 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
204 pInfo
->sizeofUChar
==U_SIZEOF_UCHAR
&&
205 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
206 pInfo
->dataFormat
[1]==0x6e &&
207 pInfo
->dataFormat
[2]==0x76 &&
208 pInfo
->dataFormat
[3]==0x74 &&
209 pInfo
->formatVersion
[0]==6); /* Everything will be version 6 */
213 * Un flatten shared data from a UDATA..
215 static UConverterSharedData
*
216 ucnv_data_unFlattenClone(UConverterLoadArgs
*pArgs
, UDataMemory
*pData
, UErrorCode
*status
)
218 /* UDataInfo info; -- necessary only if some converters have different formatVersion */
219 const uint8_t *raw
= (const uint8_t *)udata_getMemory(pData
);
220 const UConverterStaticData
*source
= (const UConverterStaticData
*) raw
;
221 UConverterSharedData
*data
;
222 UConverterType type
= (UConverterType
)source
->conversionType
;
224 if(U_FAILURE(*status
))
227 if( (uint16_t)type
>= UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
||
228 converterData
[type
] == NULL
||
229 converterData
[type
]->referenceCounter
!= 1 ||
230 source
->structSize
!= sizeof(UConverterStaticData
))
232 *status
= U_INVALID_TABLE_FORMAT
;
236 data
= (UConverterSharedData
*)uprv_malloc(sizeof(UConverterSharedData
));
238 *status
= U_MEMORY_ALLOCATION_ERROR
;
242 /* copy initial values from the static structure for this type */
243 uprv_memcpy(data
, converterData
[type
], sizeof(UConverterSharedData
));
245 #if 0 /* made UConverterMBCSTable part of UConverterSharedData -- markus 20031107 */
247 * It would be much more efficient if the table were a direct member, not a pointer.
248 * However, that would add to the size of all UConverterSharedData objects
249 * even if they do not use this table (especially algorithmic ones).
250 * If this changes, then the static templates from converterData[type]
253 * In principle, it would be cleaner if the load() function below
254 * allocated the table.
256 data
->table
= (UConverterTable
*)uprv_malloc(sizeof(UConverterTable
));
257 if(data
->table
== NULL
) {
259 *status
= U_MEMORY_ALLOCATION_ERROR
;
262 uprv_memset(data
->table
, 0, sizeof(UConverterTable
));
265 data
->staticData
= source
;
267 data
->sharedDataCached
= FALSE
;
269 /* fill in fields from the loaded data */
270 data
->dataMemory
= (void*)pData
; /* for future use */
272 if(data
->impl
->load
!= NULL
) {
273 data
->impl
->load(data
, pArgs
, raw
+ source
->structSize
, status
);
274 if(U_FAILURE(*status
)) {
275 uprv_free(data
->table
);
283 /*Takes an alias name gets an actual converter file name
284 *goes to disk and opens it.
285 *allocates the memory and returns a new UConverter object
287 static UConverterSharedData
*createConverterFromFile(UConverterLoadArgs
*pArgs
, UErrorCode
* err
)
290 UConverterSharedData
*sharedData
;
292 UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD
);
294 if (U_FAILURE (*err
)) {
295 UTRACE_EXIT_STATUS(*err
);
299 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "load converter %s from package %s", pArgs
->name
, pArgs
->pkg
);
301 data
= udata_openChoice(pArgs
->pkg
, DATA_TYPE
, pArgs
->name
, isCnvAcceptable
, NULL
, err
);
304 UTRACE_EXIT_STATUS(*err
);
308 sharedData
= ucnv_data_unFlattenClone(pArgs
, data
, err
);
312 UTRACE_EXIT_STATUS(*err
);
317 * TODO Store pkg in a field in the shared data so that delta-only converters
318 * can load base converters from the same package.
319 * If the pkg name is longer than the field, then either do not load the converter
320 * in the first place, or just set the pkg field to "".
323 UTRACE_EXIT_PTR_STATUS(sharedData
, *err
);
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
, NULL
,
382 ucnv_io_countTotalAliases(&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
;
665 UBool mayContainOption
= TRUE
;
666 UBool checkForAlgorithmic
= TRUE
;
668 if (U_FAILURE (*err
)) {
673 lookup
= &stackLookup
;
676 lookup
->locale
[0] = 0;
679 /* In case "name" is NULL we want to open the default converter. */
680 if (converterName
== NULL
) {
681 /* Call ucnv_getDefaultName first to query the name from the OS. */
682 lookup
->realName
= ucnv_getDefaultName();
683 if (lookup
->realName
== NULL
) {
684 *err
= U_MISSING_RESOURCE_ERROR
;
687 mySharedConverterData
= (UConverterSharedData
*)gDefaultAlgorithmicSharedData
;
688 checkForAlgorithmic
= FALSE
;
689 mayContainOption
= gDefaultConverterContainsOption
;
690 /* the default converter name is already canonical */
692 else if((converterName
[0] == 'U' ?
693 ( converterName
[1] == 'T' && converterName
[2] == 'F') :
694 (converterName
[0] == 'u' && converterName
[1] == 't' && converterName
[2] == 'f'))
696 (converterName
[3] == '-' ?
697 (converterName
[4] == '8' && converterName
[5] == 0) :
698 (converterName
[3] == '8' && converterName
[4] == 0)))
700 /* fastpath for UTF-8 */
701 return (UConverterSharedData
*)converterData
[UCNV_UTF8
];
704 /* separate the converter name from the options */
705 parseConverterOptions(converterName
, lookup
->cnvName
, lookup
->locale
, &lookup
->options
, err
);
706 if (U_FAILURE(*err
)) {
707 /* Very bad name used. */
711 /* get the canonical converter name */
712 lookup
->realName
= ucnv_io_getConverterName(lookup
->cnvName
, &mayContainOption
, &internalErrorCode
);
713 if (U_FAILURE(internalErrorCode
) || lookup
->realName
== NULL
) {
715 * set the input name in case the converter was added
716 * without updating the alias table, or when there is no alias table
718 lookup
->realName
= lookup
->cnvName
;
722 /* separate the converter name from the options */
723 if(mayContainOption
&& lookup
->realName
!= lookup
->cnvName
) {
724 parseConverterOptions(lookup
->realName
, lookup
->cnvName
, lookup
->locale
, &lookup
->options
, err
);
725 lookup
->realName
= lookup
->cnvName
;
728 /* get the shared data for an algorithmic converter, if it is one */
729 if (checkForAlgorithmic
) {
730 mySharedConverterData
= (UConverterSharedData
*)getAlgorithmicTypeFromName(lookup
->realName
);
732 if (mySharedConverterData
== NULL
)
734 /* it is a data-based converter, get its shared data. */
735 /* Hold the cnvCacheMutex through the whole process of checking the */
736 /* converter data cache, and adding new entries to the cache */
737 /* to prevent other threads from modifying the cache during the */
739 UConverterLoadArgs args
={ 0 };
741 args
.size
=sizeof(UConverterLoadArgs
);
743 args
.options
=lookup
->options
;
745 args
.name
=lookup
->realName
;
747 umtx_lock(&cnvCacheMutex
);
748 mySharedConverterData
= ucnv_load(&args
, err
);
749 umtx_unlock(&cnvCacheMutex
);
750 if (U_FAILURE (*err
) || (mySharedConverterData
== NULL
))
756 return mySharedConverterData
;
760 ucnv_createConverter(UConverter
*myUConverter
, const char *converterName
, UErrorCode
* err
)
762 UConverterLookupData stackLookup
;
763 UConverterSharedData
*mySharedConverterData
;
765 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN
);
767 if(U_SUCCESS(*err
)) {
768 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open converter %s", converterName
);
770 mySharedConverterData
= ucnv_loadSharedData(converterName
, &stackLookup
, err
);
772 if(U_SUCCESS(*err
)) {
773 myUConverter
= ucnv_createConverterFromSharedData(
774 myUConverter
, mySharedConverterData
,
775 stackLookup
.realName
, stackLookup
.locale
, stackLookup
.options
,
778 if(U_SUCCESS(*err
)) {
779 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
782 ucnv_unloadSharedDataIfReady(mySharedConverterData
);
787 /* exit with error */
788 UTRACE_EXIT_STATUS(*err
);
793 ucnv_createAlgorithmicConverter(UConverter
*myUConverter
,
795 const char *locale
, uint32_t options
,
798 const UConverterSharedData
*sharedData
;
799 UBool isAlgorithmicConverter
;
801 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC
);
802 UTRACE_DATA1(UTRACE_OPEN_CLOSE
, "open algorithmic converter type %d", (int32_t)type
);
804 if(type
<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES
<=type
) {
805 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
806 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
810 sharedData
= converterData
[type
];
811 umtx_lock(&cnvCacheMutex
);
812 isAlgorithmicConverter
= (UBool
)(sharedData
== NULL
|| sharedData
->referenceCounter
!= ~0);
813 umtx_unlock(&cnvCacheMutex
);
814 if (isAlgorithmicConverter
) {
815 /* not a valid type, or not an algorithmic converter */
816 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
817 UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR
);
821 cnv
= ucnv_createConverterFromSharedData(myUConverter
, (UConverterSharedData
*)sharedData
, "",
822 locale
!= NULL
? locale
: "", options
, err
);
824 UTRACE_EXIT_PTR_STATUS(cnv
, *err
);
829 ucnv_createConverterFromPackage(const char *packageName
, const char *converterName
, UErrorCode
* err
)
831 char cnvName
[UCNV_MAX_CONVERTER_NAME_LENGTH
], locale
[ULOC_FULLNAME_CAPACITY
];
832 UConverter
*myUConverter
;
833 UConverterSharedData
*mySharedConverterData
;
835 UConverterLoadArgs args
={ 0 };
837 UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE
);
839 if(U_FAILURE(*err
)) {
840 UTRACE_EXIT_STATUS(*err
);
844 UTRACE_DATA2(UTRACE_OPEN_CLOSE
, "open converter %s from package %s", converterName
, packageName
);
846 args
.size
=sizeof(UConverterLoadArgs
);
848 args
.pkg
=packageName
;
850 /* first, get the options out of the converterName string */
851 parseConverterOptions(converterName
, cnvName
, locale
, &args
.options
, err
);
852 if (U_FAILURE(*err
)) {
853 /* Very bad name used. */
854 UTRACE_EXIT_STATUS(*err
);
859 /* open the data, unflatten the shared structure */
860 mySharedConverterData
= createConverterFromFile(&args
, err
);
862 if (U_FAILURE(*err
)) {
863 UTRACE_EXIT_STATUS(*err
);
867 /* create the actual converter */
868 myUConverter
= ucnv_createConverterFromSharedData(NULL
, mySharedConverterData
, cnvName
, locale
, args
.options
, err
);
870 if (U_FAILURE(*err
)) {
871 ucnv_close(myUConverter
);
872 UTRACE_EXIT_STATUS(*err
);
876 UTRACE_EXIT_PTR_STATUS(myUConverter
, *err
);
882 ucnv_createConverterFromSharedData(UConverter
*myUConverter
,
883 UConverterSharedData
*mySharedConverterData
,
884 const char *realName
, const char *locale
, uint32_t options
,
889 if(myUConverter
== NULL
)
891 myUConverter
= (UConverter
*) uprv_malloc (sizeof (UConverter
));
892 if(myUConverter
== NULL
)
894 *err
= U_MEMORY_ALLOCATION_ERROR
;
902 /* initialize the converter */
903 uprv_memset(myUConverter
, 0, sizeof(UConverter
));
904 myUConverter
->isCopyLocal
= isCopyLocal
;
905 /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
906 myUConverter
->sharedData
= mySharedConverterData
;
907 myUConverter
->options
= options
;
908 myUConverter
->preFromUFirstCP
= U_SENTINEL
;
909 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_DEFAULT_CALLBACK
;
910 myUConverter
->fromUCharErrorBehaviour
= UCNV_FROM_U_DEFAULT_CALLBACK
;
911 myUConverter
->toUnicodeStatus
= mySharedConverterData
->toUnicodeStatus
;
912 myUConverter
->maxBytesPerUChar
= mySharedConverterData
->staticData
->maxBytesPerChar
;
913 myUConverter
->subChar1
= mySharedConverterData
->staticData
->subChar1
;
914 myUConverter
->subCharLen
= mySharedConverterData
->staticData
->subCharLen
;
915 myUConverter
->subChars
= (uint8_t *)myUConverter
->subUChars
;
916 uprv_memcpy(myUConverter
->subChars
, mySharedConverterData
->staticData
->subChar
, myUConverter
->subCharLen
);
918 if(mySharedConverterData
->impl
->open
!= NULL
) {
919 mySharedConverterData
->impl
->open(myUConverter
, realName
, locale
, options
, err
);
920 if(U_FAILURE(*err
)) {
921 ucnv_close(myUConverter
);
930 ucnv_flushAvailableConverterCache() {
931 if (gAvailableConverters
) {
932 umtx_lock(&cnvCacheMutex
);
933 gAvailableConverterCount
= 0;
934 uprv_free((char **)gAvailableConverters
);
935 gAvailableConverters
= NULL
;
936 umtx_unlock(&cnvCacheMutex
);
940 /*Frees all shared immutable objects that aren't referred to (reference count = 0)
942 U_CAPI
int32_t U_EXPORT2
945 UConverterSharedData
*mySharedData
= NULL
;
947 int32_t tableDeletedNum
= 0;
948 const UHashElement
*e
;
949 UErrorCode status
= U_ILLEGAL_ARGUMENT_ERROR
;
950 int32_t i
, remaining
;
952 UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE
);
954 /* Close the default converter without creating a new one so that everything will be flushed. */
955 ucnv_close(u_getDefaultConverter(&status
));
957 /*if shared data hasn't even been lazy evaluated yet
960 if (SHARED_DATA_HASHTABLE
== NULL
) {
961 UTRACE_EXIT_VALUE((int32_t)0);
965 /*creates an enumeration to iterate through every element in the
968 * Synchronization: holding cnvCacheMutex will prevent any other thread from
969 * accessing or modifying the hash table during the iteration.
970 * The reference count of an entry may be decremented by
971 * ucnv_close while the iteration is in process, but this is
972 * benign. It can't be incremented (in ucnv_createConverter())
973 * because the sequence of looking up in the cache + incrementing
974 * is protected by cnvCacheMutex.
976 umtx_lock(&cnvCacheMutex
);
978 * double loop: A delta/extension-only converter has a pointer to its base table's
979 * shared data; the first iteration of the outer loop may see the delta converter
980 * before the base converter, and unloading the delta converter may get the base
981 * converter's reference counter down to 0.
987 while ((e
= uhash_nextElement (SHARED_DATA_HASHTABLE
, &pos
)) != NULL
)
989 mySharedData
= (UConverterSharedData
*) e
->value
.pointer
;
990 /*deletes only if reference counter == 0 */
991 if (mySharedData
->referenceCounter
== 0)
995 UCNV_DEBUG_LOG("del",mySharedData
->staticData
->name
,mySharedData
);
997 uhash_removeElement(SHARED_DATA_HASHTABLE
, e
);
998 mySharedData
->sharedDataCached
= FALSE
;
999 ucnv_deleteSharedConverterData (mySharedData
);
1004 } while(++i
== 1 && remaining
> 0);
1005 umtx_unlock(&cnvCacheMutex
);
1007 UTRACE_DATA1(UTRACE_INFO
, "ucnv_flushCache() exits with %d converters remaining", remaining
);
1009 ucnv_flushAvailableConverterCache();
1011 UTRACE_EXIT_VALUE(tableDeletedNum
);
1012 return tableDeletedNum
;
1015 /* available converters list --------------------------------------------------- */
1017 static UBool
haveAvailableConverterList(UErrorCode
*pErrorCode
) {
1019 UMTX_CHECK(&cnvCacheMutex
, (gAvailableConverters
== NULL
), needInit
);
1021 UConverter tempConverter
;
1022 UEnumeration
*allConvEnum
= NULL
;
1024 uint16_t localConverterCount
;
1025 uint16_t allConverterCount
;
1026 UErrorCode localStatus
;
1027 const char *converterName
;
1028 const char **localConverterList
;
1030 allConvEnum
= ucnv_openAllNames(pErrorCode
);
1031 allConverterCount
= uenum_count(allConvEnum
, pErrorCode
);
1032 if (U_FAILURE(*pErrorCode
)) {
1036 /* We can't have more than "*converterTable" converters to open */
1037 localConverterList
= (const char **) uprv_malloc(allConverterCount
* sizeof(char*));
1038 if (!localConverterList
) {
1039 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1043 localConverterCount
= 0;
1045 for (idx
= 0; idx
< allConverterCount
; idx
++) {
1046 localStatus
= U_ZERO_ERROR
;
1047 converterName
= uenum_next(allConvEnum
, NULL
, &localStatus
);
1048 ucnv_close(ucnv_createConverter(&tempConverter
, converterName
, &localStatus
));
1049 if (U_SUCCESS(localStatus
)) {
1050 localConverterList
[localConverterCount
++] = converterName
;
1053 uenum_close(allConvEnum
);
1055 umtx_lock(&cnvCacheMutex
);
1056 if (gAvailableConverters
== NULL
) {
1057 gAvailableConverters
= localConverterList
;
1058 gAvailableConverterCount
= localConverterCount
;
1059 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1062 uprv_free((char **)localConverterList
);
1064 umtx_unlock(&cnvCacheMutex
);
1070 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
) {
1071 if (haveAvailableConverterList(pErrorCode
)) {
1072 return gAvailableConverterCount
;
1077 U_CFUNC
const char *
1078 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
) {
1079 if (haveAvailableConverterList(pErrorCode
)) {
1080 if (n
< gAvailableConverterCount
) {
1081 return gAvailableConverters
[n
];
1083 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
1088 /* default converter name --------------------------------------------------- */
1090 /* Copy the canonical converter name. */
1091 static U_INLINE
void
1092 internalSetName(const char *name
, UErrorCode
*status
) {
1093 UConverterLookupData lookup
;
1094 int32_t length
=(int32_t)(uprv_strlen(name
));
1095 UBool containsOption
= (UBool
)(uprv_strchr(name
, UCNV_OPTION_SEP_CHAR
) != NULL
);
1096 const UConverterSharedData
*algorithmicSharedData
;
1098 lookup
.locale
[0] = 0;
1100 lookup
.realName
= name
;
1101 if(containsOption
) {
1102 parseConverterOptions(lookup
.realName
, lookup
.cnvName
, lookup
.locale
, &lookup
.options
, status
);
1103 lookup
.realName
= lookup
.cnvName
;
1105 algorithmicSharedData
= getAlgorithmicTypeFromName(lookup
.realName
);
1107 umtx_lock(&cnvCacheMutex
);
1109 uprv_memcpy(gDefaultConverterNameBuffer
, name
, length
);
1110 gDefaultConverterNameBuffer
[length
]=0;
1111 gDefaultConverterName
= gDefaultConverterNameBuffer
;
1112 gDefaultConverterContainsOption
= containsOption
;
1113 gDefaultAlgorithmicSharedData
= algorithmicSharedData
;
1115 ucln_common_registerCleanup(UCLN_COMMON_UCNV
, ucnv_cleanup
);
1117 umtx_unlock(&cnvCacheMutex
);
1121 * In order to be really thread-safe, the get function would have to take
1122 * a buffer parameter and copy the current string inside a mutex block.
1123 * This implementation only tries to be really thread-safe while
1125 * It assumes that setting a pointer is atomic.
1128 U_CAPI
const char* U_EXPORT2
1129 ucnv_getDefaultName() {
1130 /* local variable to be thread-safe */
1133 UMTX_CHECK(&cnvCacheMutex
, gDefaultConverterName
, name
);
1135 UErrorCode errorCode
= U_ZERO_ERROR
;
1136 UConverter
*cnv
= NULL
;
1138 name
= uprv_getDefaultCodepage();
1140 /* if the name is there, test it out and get the canonical name with options */
1142 cnv
= ucnv_open(name
, &errorCode
);
1143 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1144 name
= ucnv_getName(cnv
, &errorCode
);
1148 if(name
== NULL
|| name
[0] == 0
1149 || U_FAILURE(errorCode
) || cnv
== NULL
1150 || uprv_strlen(name
)>=sizeof(gDefaultConverterNameBuffer
))
1152 /* Panic time, let's use a fallback. */
1153 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1155 /* there is no 'algorithmic' converter for EBCDIC */
1156 #elif defined(OS390)
1157 name
= "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING
;
1159 name
= "ibm-37_P100-1995";
1163 internalSetName(name
, &errorCode
);
1165 /* The close may make the current name go away. */
1172 U_CAPI
void U_EXPORT2
1173 ucnv_setDefaultName(const char *converterName
) {
1174 if(converterName
==NULL
) {
1175 /* reset to the default codepage */
1176 umtx_lock(&cnvCacheMutex
);
1177 gDefaultConverterName
=NULL
;
1178 umtx_unlock(&cnvCacheMutex
);
1180 UErrorCode errorCode
= U_ZERO_ERROR
;
1181 UConverter
*cnv
= NULL
;
1182 const char *name
= NULL
;
1184 /* if the name is there, test it out and get the canonical name with options */
1185 cnv
= ucnv_open(converterName
, &errorCode
);
1186 if(U_SUCCESS(errorCode
) && cnv
!= NULL
) {
1187 name
= ucnv_getName(cnv
, &errorCode
);
1190 if(U_SUCCESS(errorCode
) && name
!=NULL
) {
1191 internalSetName(name
, &errorCode
);
1193 /* else this converter is bad to use. Don't change it to a bad value. */
1195 /* The close may make the current name go away. */
1200 /* data swapping ------------------------------------------------------------ */
1202 /* most of this might belong more properly into ucnvmbcs.c, but that is so large */
1204 #if !UCONFIG_NO_LEGACY_CONVERSION
1206 U_CAPI
int32_t U_EXPORT2
1207 ucnv_swap(const UDataSwapper
*ds
,
1208 const void *inData
, int32_t length
, void *outData
,
1209 UErrorCode
*pErrorCode
) {
1210 const UDataInfo
*pInfo
;
1213 const uint8_t *inBytes
;
1216 uint32_t offset
, count
, staticDataSize
;
1219 const UConverterStaticData
*inStaticData
;
1220 UConverterStaticData
*outStaticData
;
1222 const _MBCSHeader
*inMBCSHeader
;
1223 _MBCSHeader
*outMBCSHeader
;
1224 _MBCSHeader mbcsHeader
;
1227 const int32_t *inExtIndexes
;
1230 /* udata_swapDataHeader checks the arguments */
1231 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1232 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1236 /* check data format and format version */
1237 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1239 pInfo
->dataFormat
[0]==0x63 && /* dataFormat="cnvt" */
1240 pInfo
->dataFormat
[1]==0x6e &&
1241 pInfo
->dataFormat
[2]==0x76 &&
1242 pInfo
->dataFormat
[3]==0x74 &&
1243 pInfo
->formatVersion
[0]==6 &&
1244 pInfo
->formatVersion
[1]>=2
1246 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",
1247 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1248 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1249 pInfo
->formatVersion
[0], pInfo
->formatVersion
[1]);
1250 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1254 inBytes
=(const uint8_t *)inData
+headerSize
;
1255 outBytes
=(uint8_t *)outData
+headerSize
;
1257 /* read the initial UConverterStaticData structure after the UDataInfo header */
1258 inStaticData
=(const UConverterStaticData
*)inBytes
;
1259 outStaticData
=(UConverterStaticData
*)outBytes
;
1262 staticDataSize
=ds
->readUInt32(inStaticData
->structSize
);
1265 if( length
<sizeof(UConverterStaticData
) ||
1266 (uint32_t)length
<(staticDataSize
=ds
->readUInt32(inStaticData
->structSize
))
1268 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after header) for an ICU .cnv conversion table\n",
1270 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1276 /* swap the static data */
1277 if(inStaticData
!=outStaticData
) {
1278 uprv_memcpy(outStaticData
, inStaticData
, staticDataSize
);
1281 ds
->swapArray32(ds
, &inStaticData
->structSize
, 4,
1282 &outStaticData
->structSize
, pErrorCode
);
1283 ds
->swapArray32(ds
, &inStaticData
->codepage
, 4,
1284 &outStaticData
->codepage
, pErrorCode
);
1286 ds
->swapInvChars(ds
, inStaticData
->name
, (int32_t)uprv_strlen(inStaticData
->name
),
1287 outStaticData
->name
, pErrorCode
);
1288 if(U_FAILURE(*pErrorCode
)) {
1289 udata_printError(ds
, "ucnv_swap(): error swapping converter name\n");
1294 inBytes
+=staticDataSize
;
1295 outBytes
+=staticDataSize
;
1297 length
-=(int32_t)staticDataSize
;
1300 /* check for supported conversionType values */
1301 if(inStaticData
->conversionType
==UCNV_MBCS
) {
1302 /* swap MBCS data */
1303 inMBCSHeader
=(const _MBCSHeader
*)inBytes
;
1304 outMBCSHeader
=(_MBCSHeader
*)outBytes
;
1306 if(0<=length
&& length
<sizeof(_MBCSHeader
)) {
1307 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1309 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1312 if(!(inMBCSHeader
->version
[0]==4 && inMBCSHeader
->version
[1]>=1)) {
1313 udata_printError(ds
, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
1314 inMBCSHeader
->version
[0], inMBCSHeader
->version
[1]);
1315 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1319 uprv_memcpy(mbcsHeader
.version
, inMBCSHeader
->version
, 4);
1320 mbcsHeader
.countStates
= ds
->readUInt32(inMBCSHeader
->countStates
);
1321 mbcsHeader
.countToUFallbacks
= ds
->readUInt32(inMBCSHeader
->countToUFallbacks
);
1322 mbcsHeader
.offsetToUCodeUnits
= ds
->readUInt32(inMBCSHeader
->offsetToUCodeUnits
);
1323 mbcsHeader
.offsetFromUTable
= ds
->readUInt32(inMBCSHeader
->offsetFromUTable
);
1324 mbcsHeader
.offsetFromUBytes
= ds
->readUInt32(inMBCSHeader
->offsetFromUBytes
);
1325 mbcsHeader
.flags
= ds
->readUInt32(inMBCSHeader
->flags
);
1326 mbcsHeader
.fromUBytesLength
= ds
->readUInt32(inMBCSHeader
->fromUBytesLength
);
1328 extOffset
=(int32_t)(mbcsHeader
.flags
>>8);
1329 outputType
=(uint8_t)mbcsHeader
.flags
;
1331 /* make sure that the output type is known */
1332 switch(outputType
) {
1337 case MBCS_OUTPUT_3_EUC
:
1338 case MBCS_OUTPUT_4_EUC
:
1339 case MBCS_OUTPUT_2_SISO
:
1340 case MBCS_OUTPUT_EXT_ONLY
:
1344 udata_printError(ds
, "ucnv_swap(): unsupported MBCS output type 0x%x\n",
1346 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1350 /* calculate the length of the MBCS data */
1352 size
=(int32_t)(mbcsHeader
.offsetFromUBytes
+mbcsHeader
.fromUBytesLength
);
1354 /* avoid compiler warnings - not otherwise necessary, and the value does not matter */
1357 /* there is extension data after the base data, see ucnv_ext.h */
1358 if(length
>=0 && length
<(extOffset
+UCNV_EXT_INDEXES_MIN_LENGTH
*4)) {
1359 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table with extension data\n",
1361 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1365 inExtIndexes
=(const int32_t *)(inBytes
+extOffset
);
1366 size
=extOffset
+udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_SIZE
]);
1371 udata_printError(ds
, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
1373 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1377 /* copy the data for inaccessible bytes */
1378 if(inBytes
!=outBytes
) {
1379 uprv_memcpy(outBytes
, inBytes
, size
);
1382 /* swap the MBCSHeader */
1383 ds
->swapArray32(ds
, &inMBCSHeader
->countStates
, 7*4,
1384 &outMBCSHeader
->countStates
, pErrorCode
);
1386 if(outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1388 * extension-only file,
1389 * contains a base name instead of normal base table data
1392 /* swap the base name, between the header and the extension data */
1393 ds
->swapInvChars(ds
, inMBCSHeader
+1, (int32_t)uprv_strlen((const char *)(inMBCSHeader
+1)),
1394 outMBCSHeader
+1, pErrorCode
);
1396 /* normal file with base table data */
1398 /* swap the state table, 1kB per state */
1399 ds
->swapArray32(ds
, inMBCSHeader
+1, (int32_t)(mbcsHeader
.countStates
*1024),
1400 outMBCSHeader
+1, pErrorCode
);
1402 /* swap the toUFallbacks[] */
1403 offset
=sizeof(_MBCSHeader
)+mbcsHeader
.countStates
*1024;
1404 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)(mbcsHeader
.countToUFallbacks
*8),
1405 outBytes
+offset
, pErrorCode
);
1407 /* swap the unicodeCodeUnits[] */
1408 offset
=mbcsHeader
.offsetToUCodeUnits
;
1409 count
=mbcsHeader
.offsetFromUTable
-offset
;
1410 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1411 outBytes
+offset
, pErrorCode
);
1413 /* offset to the stage 1 table, independent of the outputType */
1414 offset
=mbcsHeader
.offsetFromUTable
;
1416 if(outputType
==MBCS_OUTPUT_1
) {
1417 /* SBCS: swap the fromU tables, all 16 bits wide */
1418 count
=(mbcsHeader
.offsetFromUBytes
-offset
)+mbcsHeader
.fromUBytesLength
;
1419 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1420 outBytes
+offset
, pErrorCode
);
1422 /* otherwise: swap the stage tables separately */
1424 /* stage 1 table: uint16_t[0x440 or 0x40] */
1425 if(inStaticData
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
1426 count
=0x440*2; /* for all of Unicode */
1428 count
=0x40*2; /* only BMP */
1430 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1431 outBytes
+offset
, pErrorCode
);
1433 /* stage 2 table: uint32_t[] */
1435 count
=mbcsHeader
.offsetFromUBytes
-offset
;
1436 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1437 outBytes
+offset
, pErrorCode
);
1439 /* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
1440 offset
=mbcsHeader
.offsetFromUBytes
;
1441 count
=mbcsHeader
.fromUBytesLength
;
1442 switch(outputType
) {
1444 case MBCS_OUTPUT_3_EUC
:
1445 case MBCS_OUTPUT_2_SISO
:
1446 ds
->swapArray16(ds
, inBytes
+offset
, (int32_t)count
,
1447 outBytes
+offset
, pErrorCode
);
1450 ds
->swapArray32(ds
, inBytes
+offset
, (int32_t)count
,
1451 outBytes
+offset
, pErrorCode
);
1454 /* just uint8_t[], nothing to swap */
1461 /* swap the extension data */
1463 outBytes
+=extOffset
;
1465 /* swap toUTable[] */
1466 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_INDEX
]);
1467 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_LENGTH
]);
1468 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1470 /* swap toUUChars[] */
1471 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]);
1472 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]);
1473 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1475 /* swap fromUTableUChars[] */
1476 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]);
1477 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_LENGTH
]);
1478 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1480 /* swap fromUTableValues[] */
1481 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]);
1482 /* same length as for fromUTableUChars[] */
1483 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1485 /* no need to swap fromUBytes[] */
1487 /* swap fromUStage12[] */
1488 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]);
1489 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]);
1490 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1492 /* swap fromUStage3[] */
1493 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]);
1494 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]);
1495 ds
->swapArray16(ds
, inBytes
+offset
, length
*2, outBytes
+offset
, pErrorCode
);
1497 /* swap fromUStage3b[] */
1498 offset
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]);
1499 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]);
1500 ds
->swapArray32(ds
, inBytes
+offset
, length
*4, outBytes
+offset
, pErrorCode
);
1502 /* swap indexes[] */
1503 length
=udata_readInt32(ds
, inExtIndexes
[UCNV_EXT_INDEXES_LENGTH
]);
1504 ds
->swapArray32(ds
, inBytes
, length
*4, outBytes
, pErrorCode
);
1508 udata_printError(ds
, "ucnv_swap(): unknown conversionType=%d!=UCNV_MBCS\n",
1509 inStaticData
->conversionType
);
1510 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1514 return headerSize
+(int32_t)staticDataSize
+size
;
1517 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */