1 /* 2 ********************************************************************** 3 * Copyright (C) 1999-2009 International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ********************************************************************** 6 * 7 * 8 * ucnv_bld.h: 9 * Contains internal data structure definitions 10 * Created by Bertrand A. Damiba 11 * 12 * Change history: 13 * 14 * 06/29/2000 helena Major rewrite of the callback APIs. 15 */ 16 17 #ifndef UCNV_BLD_H 18 #define UCNV_BLD_H 19 20 #include "unicode/utypes.h" 21 22 #if !UCONFIG_NO_CONVERSION 23 24 #include "unicode/ucnv.h" 25 #include "unicode/ucnv_err.h" 26 #include "ucnv_cnv.h" 27 #include "ucnvmbcs.h" 28 #include "ucnv_ext.h" 29 #include "udataswp.h" 30 31 /* size of the overflow buffers in UConverter, enough for escaping callbacks */ 32 #define UCNV_ERROR_BUFFER_LENGTH 32 33 34 /* at most 4 bytes per substitution character (part of .cnv file format! see UConverterStaticData) */ 35 #define UCNV_MAX_SUBCHAR_LEN 4 36 37 /* at most 8 bytes per character in toUBytes[] (UTF-8 uses up to 6) */ 38 #define UCNV_MAX_CHAR_LEN 8 39 40 /* converter options bits */ 41 #define UCNV_OPTION_VERSION 0xf 42 #define UCNV_OPTION_SWAP_LFNL 0x10 43 44 #define UCNV_GET_VERSION(cnv) ((cnv)->options&UCNV_OPTION_VERSION) 45 46 U_CDECL_BEGIN /* We must declare the following as 'extern "C"' so that if ucnv 47 itself is compiled under C++, the linkage of the funcptrs will 48 work. 49 */ 50 51 union UConverterTable { 52 UConverterMBCSTable mbcs; 53 }; 54 55 typedef union UConverterTable UConverterTable; 56 57 struct UConverterImpl; 58 typedef struct UConverterImpl UConverterImpl; 59 60 /** values for the unicodeMask */ 61 #define UCNV_HAS_SUPPLEMENTARY 1 62 #define UCNV_HAS_SURROGATES 2 63 64 typedef struct UConverterStaticData { /* +offset: size */ 65 uint32_t structSize; /* +0: 4 Size of this structure */ 66 67 char name 68 [UCNV_MAX_CONVERTER_NAME_LENGTH]; /* +4: 60 internal name of the converter- invariant chars */ 69 70 int32_t codepage; /* +64: 4 codepage # (now IBM-$codepage) */ 71 72 int8_t platform; /* +68: 1 platform of the converter (only IBM now) */ 73 int8_t conversionType; /* +69: 1 conversion type */ 74 75 int8_t minBytesPerChar; /* +70: 1 Minimum # bytes per char in this codepage */ 76 int8_t maxBytesPerChar; /* +71: 1 Maximum # bytes output per UChar in this codepage */ 77 78 uint8_t subChar[UCNV_MAX_SUBCHAR_LEN]; /* +72: 4 [note: 4 and 8 byte boundary] */ 79 int8_t subCharLen; /* +76: 1 */ 80 81 uint8_t hasToUnicodeFallback; /* +77: 1 UBool needs to be changed to UBool to be consistent across platform */ 82 uint8_t hasFromUnicodeFallback; /* +78: 1 */ 83 uint8_t unicodeMask; /* +79: 1 bit 0: has supplementary bit 1: has single surrogates */ 84 uint8_t subChar1; /* +80: 1 single-byte substitution character for IBM MBCS (0 if none) */ 85 uint8_t reserved[19]; /* +81: 19 to round out the structure */ 86 /* total size: 100 */ 87 } UConverterStaticData; 88 89 /* 90 * Defines the UConverterSharedData struct, 91 * the immutable, shared part of UConverter. 92 */ 93 struct UConverterSharedData { 94 uint32_t structSize; /* Size of this structure */ 95 uint32_t referenceCounter; /* used to count number of clients, 0xffffffff for static SharedData */ 96 97 const void *dataMemory; /* from udata_openChoice() - for cleanup */ 98 void *table; /* Unused. This used to be a UConverterTable - Pointer to conversion data - see mbcs below */ 99 100 const UConverterStaticData *staticData; /* pointer to the static (non changing) data. */ 101 102 UBool sharedDataCached; /* TRUE: shared data is in cache, don't destroy on ucnv_close() if 0 ref. FALSE: shared data isn't in the cache, do attempt to clean it up if the ref is 0 */ 103 /*UBool staticDataOwned; TRUE if static data owned by shared data & should be freed with it, NEVER true for udata() loaded statics. This ignored variable was removed to make space for sharedDataCached. */ 104 105 const UConverterImpl *impl; /* vtable-style struct of mostly function pointers */ 106 107 /*initial values of some members of the mutable part of object */ 108 uint32_t toUnicodeStatus; 109 110 /* 111 * Shared data structures currently come in two flavors: 112 * - readonly for built-in algorithmic converters 113 * - allocated for MBCS, with a pointer to an allocated UConverterTable 114 * which always has a UConverterMBCSTable 115 * 116 * To eliminate one allocation, I am making the UConverterMBCSTable 117 * a member of the shared data. It is the last member so that static 118 * definitions of UConverterSharedData work as before. 119 * The table field above also remains to avoid updating all static 120 * definitions, but is now unused. 121 * 122 * markus 2003-nov-07 123 */ 124 UConverterMBCSTable mbcs; 125 }; 126 127 /* Defines a UConverter, the lightweight mutable part the user sees */ 128 129 struct UConverter { 130 /* 131 * Error function pointer called when conversion issues 132 * occur during a ucnv_fromUnicode call 133 */ 134 void (U_EXPORT2 *fromUCharErrorBehaviour) (const void *context, 135 UConverterFromUnicodeArgs *args, 136 const UChar *codeUnits, 137 int32_t length, 138 UChar32 codePoint, 139 UConverterCallbackReason reason, 140 UErrorCode *); 141 /* 142 * Error function pointer called when conversion issues 143 * occur during a ucnv_toUnicode call 144 */ 145 void (U_EXPORT2 *fromCharErrorBehaviour) (const void *context, 146 UConverterToUnicodeArgs *args, 147 const char *codeUnits, 148 int32_t length, 149 UConverterCallbackReason reason, 150 UErrorCode *); 151 152 /* 153 * Pointer to additional data that depends on the converter type. 154 * Used by ISO 2022, SCSU, GB 18030 converters, possibly more. 155 */ 156 void *extraInfo; 157 158 const void *fromUContext; 159 const void *toUContext; 160 161 /* 162 * Pointer to charset bytes for substitution string if subCharLen>0, 163 * or pointer to Unicode string (UChar *) if subCharLen<0. 164 * subCharLen==0 is equivalent to using a skip callback. 165 * If the pointer is !=subUChars then it is allocated with 166 * UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR bytes. 167 * The subUChars field is declared as UChar[] not uint8_t[] to 168 * guarantee alignment for UChars. 169 */ 170 uint8_t *subChars; 171 172 UConverterSharedData *sharedData; /* Pointer to the shared immutable part of the converter object */ 173 174 uint32_t options; /* options flags from UConverterOpen, may contain additional bits */ 175 176 UBool sharedDataIsCached; /* TRUE: shared data is in cache, don't destroy on ucnv_close() if 0 ref. FALSE: shared data isn't in the cache, do attempt to clean it up if the ref is 0 */ 177 UBool isCopyLocal; /* TRUE if UConverter is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */ 178 UBool isExtraLocal; /* TRUE if extraInfo is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */ 179 180 UBool useFallback; 181 int8_t toULength; /* number of bytes in toUBytes */ 182 uint8_t toUBytes[UCNV_MAX_CHAR_LEN-1];/* more "toU status"; keeps the bytes of the current character */ 183 uint32_t toUnicodeStatus; /* Used to internalize stream status information */ 184 int32_t mode; 185 uint32_t fromUnicodeStatus; 186 187 /* 188 * More fromUnicode() status. Serves 3 purposes: 189 * - keeps a lead surrogate between buffers (similar to toUBytes[]) 190 * - keeps a lead surrogate at the end of the stream, 191 * which the framework handles as truncated input 192 * - if the fromUnicode() implementation returns to the framework 193 * (ucnv.c ucnv_fromUnicode()), then the framework calls the callback 194 * for this code point 195 */ 196 UChar32 fromUChar32; 197 198 /* 199 * value for ucnv_getMaxCharSize() 200 * 201 * usually simply copied from the static data, but ucnvmbcs.c modifies 202 * the value depending on the converter type and options 203 */ 204 int8_t maxBytesPerUChar; 205 206 int8_t subCharLen; /* length of the codepage specific character sequence */ 207 int8_t invalidCharLength; 208 int8_t charErrorBufferLength; /* number of valid bytes in charErrorBuffer */ 209 210 int8_t invalidUCharLength; 211 int8_t UCharErrorBufferLength; /* number of valid UChars in charErrorBuffer */ 212 213 uint8_t subChar1; /* single-byte substitution character if different from subChar */ 214 UBool useSubChar1; 215 char invalidCharBuffer[UCNV_MAX_CHAR_LEN]; /* bytes from last error/callback situation */ 216 uint8_t charErrorBuffer[UCNV_ERROR_BUFFER_LENGTH]; /* codepage output from Error functions */ 217 UChar subUChars[UCNV_MAX_SUBCHAR_LEN/U_SIZEOF_UCHAR]; /* see subChars documentation */ 218 219 UChar invalidUCharBuffer[U16_MAX_LENGTH]; /* UChars from last error/callback situation */ 220 UChar UCharErrorBuffer[UCNV_ERROR_BUFFER_LENGTH]; /* unicode output from Error functions */ 221 222 /* fields for conversion extension */ 223 224 /* store previous UChars/chars to continue partial matches */ 225 UChar32 preFromUFirstCP; /* >=0: partial match */ 226 UChar preFromU[UCNV_EXT_MAX_UCHARS]; 227 char preToU[UCNV_EXT_MAX_BYTES]; 228 int8_t preFromULength, preToULength; /* negative: replay */ 229 int8_t preToUFirstLength; /* length of first character */ 230 231 /* new fields for ICU 4.0 */ 232 UConverterCallbackReason toUCallbackReason; /* (*fromCharErrorBehaviour) reason, set when error is detected */ 233 }; 234 235 U_CDECL_END /* end of UConverter */ 236 237 #define CONVERTER_FILE_EXTENSION ".cnv" 238 239 240 /** 241 * Return the number of all converter names. 242 * @param pErrorCode The error code 243 * @return the number of all converter names 244 */ 245 U_CFUNC uint16_t 246 ucnv_bld_countAvailableConverters(UErrorCode *pErrorCode); 247 248 /** 249 * Return the (n)th converter name in mixed case, or NULL 250 * if there is none (typically, if the data cannot be loaded). 251 * 0<=index<ucnv_io_countAvailableConverters(). 252 * @param n The number specifies which converter name to get 253 * @param pErrorCode The error code 254 * @return the (n)th converter name in mixed case, or NULL if there is none. 255 */ 256 U_CFUNC const char * 257 ucnv_bld_getAvailableConverter(uint16_t n, UErrorCode *pErrorCode); 258 259 /** 260 * Load a non-algorithmic converter. 261 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex). 262 */ 263 UConverterSharedData * 264 ucnv_load(UConverterLoadArgs *pArgs, UErrorCode *err); 265 266 /** 267 * Unload a non-algorithmic converter. 268 * It must be sharedData->referenceCounter != ~0 269 * and this function must be called inside umtx_lock(&cnvCacheMutex). 270 */ 271 void 272 ucnv_unload(UConverterSharedData *sharedData); 273 274 /** 275 * Swap ICU .cnv conversion tables. See udataswp.h. 276 * @internal 277 */ 278 U_CAPI int32_t U_EXPORT2 279 ucnv_swap(const UDataSwapper *ds, 280 const void *inData, int32_t length, void *outData, 281 UErrorCode *pErrorCode); 282 283 #endif 284 285 #endif /* _UCNV_BLD */ 286