michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 1999-2011 International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * michael@0: * michael@0: * ucnv_bld.h: michael@0: * Contains internal data structure definitions michael@0: * Created by Bertrand A. Damiba michael@0: * michael@0: * Change history: michael@0: * michael@0: * 06/29/2000 helena Major rewrite of the callback APIs. michael@0: */ michael@0: michael@0: #ifndef UCNV_BLD_H michael@0: #define UCNV_BLD_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_CONVERSION michael@0: michael@0: #include "unicode/ucnv.h" michael@0: #include "unicode/ucnv_err.h" michael@0: #include "unicode/utf16.h" michael@0: #include "ucnv_cnv.h" michael@0: #include "ucnvmbcs.h" michael@0: #include "ucnv_ext.h" michael@0: #include "udataswp.h" michael@0: michael@0: /* size of the overflow buffers in UConverter, enough for escaping callbacks */ michael@0: #define UCNV_ERROR_BUFFER_LENGTH 32 michael@0: michael@0: /* at most 4 bytes per substitution character (part of .cnv file format! see UConverterStaticData) */ michael@0: #define UCNV_MAX_SUBCHAR_LEN 4 michael@0: michael@0: /* at most 8 bytes per character in toUBytes[] (UTF-8 uses up to 6) */ michael@0: #define UCNV_MAX_CHAR_LEN 8 michael@0: michael@0: /* converter options bits */ michael@0: #define UCNV_OPTION_VERSION 0xf michael@0: #define UCNV_OPTION_SWAP_LFNL 0x10 michael@0: michael@0: #define UCNV_GET_VERSION(cnv) ((cnv)->options&UCNV_OPTION_VERSION) michael@0: michael@0: U_CDECL_BEGIN /* We must declare the following as 'extern "C"' so that if ucnv michael@0: itself is compiled under C++, the linkage of the funcptrs will michael@0: work. michael@0: */ michael@0: michael@0: union UConverterTable { michael@0: UConverterMBCSTable mbcs; michael@0: }; michael@0: michael@0: typedef union UConverterTable UConverterTable; michael@0: michael@0: struct UConverterImpl; michael@0: typedef struct UConverterImpl UConverterImpl; michael@0: michael@0: /** values for the unicodeMask */ michael@0: #define UCNV_HAS_SUPPLEMENTARY 1 michael@0: #define UCNV_HAS_SURROGATES 2 michael@0: michael@0: typedef struct UConverterStaticData { /* +offset: size */ michael@0: uint32_t structSize; /* +0: 4 Size of this structure */ michael@0: michael@0: char name michael@0: [UCNV_MAX_CONVERTER_NAME_LENGTH]; /* +4: 60 internal name of the converter- invariant chars */ michael@0: michael@0: int32_t codepage; /* +64: 4 codepage # (now IBM-$codepage) */ michael@0: michael@0: int8_t platform; /* +68: 1 platform of the converter (only IBM now) */ michael@0: int8_t conversionType; /* +69: 1 conversion type */ michael@0: michael@0: int8_t minBytesPerChar; /* +70: 1 Minimum # bytes per char in this codepage */ michael@0: int8_t maxBytesPerChar; /* +71: 1 Maximum # bytes output per UChar in this codepage */ michael@0: michael@0: uint8_t subChar[UCNV_MAX_SUBCHAR_LEN]; /* +72: 4 [note: 4 and 8 byte boundary] */ michael@0: int8_t subCharLen; /* +76: 1 */ michael@0: michael@0: uint8_t hasToUnicodeFallback; /* +77: 1 UBool needs to be changed to UBool to be consistent across platform */ michael@0: uint8_t hasFromUnicodeFallback; /* +78: 1 */ michael@0: uint8_t unicodeMask; /* +79: 1 bit 0: has supplementary bit 1: has single surrogates */ michael@0: uint8_t subChar1; /* +80: 1 single-byte substitution character for IBM MBCS (0 if none) */ michael@0: uint8_t reserved[19]; /* +81: 19 to round out the structure */ michael@0: /* total size: 100 */ michael@0: } UConverterStaticData; michael@0: michael@0: /* michael@0: * Defines the UConverterSharedData struct, michael@0: * the immutable, shared part of UConverter. michael@0: */ michael@0: struct UConverterSharedData { michael@0: uint32_t structSize; /* Size of this structure */ michael@0: uint32_t referenceCounter; /* used to count number of clients, 0xffffffff for static SharedData */ michael@0: michael@0: const void *dataMemory; /* from udata_openChoice() - for cleanup */ michael@0: void *table; /* Unused. This used to be a UConverterTable - Pointer to conversion data - see mbcs below */ michael@0: michael@0: const UConverterStaticData *staticData; /* pointer to the static (non changing) data. */ michael@0: michael@0: 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 */ michael@0: /*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. */ michael@0: michael@0: const UConverterImpl *impl; /* vtable-style struct of mostly function pointers */ michael@0: michael@0: /*initial values of some members of the mutable part of object */ michael@0: uint32_t toUnicodeStatus; michael@0: michael@0: /* michael@0: * Shared data structures currently come in two flavors: michael@0: * - readonly for built-in algorithmic converters michael@0: * - allocated for MBCS, with a pointer to an allocated UConverterTable michael@0: * which always has a UConverterMBCSTable michael@0: * michael@0: * To eliminate one allocation, I am making the UConverterMBCSTable michael@0: * a member of the shared data. It is the last member so that static michael@0: * definitions of UConverterSharedData work as before. michael@0: * The table field above also remains to avoid updating all static michael@0: * definitions, but is now unused. michael@0: * michael@0: * markus 2003-nov-07 michael@0: */ michael@0: UConverterMBCSTable mbcs; michael@0: }; michael@0: michael@0: /* Defines a UConverter, the lightweight mutable part the user sees */ michael@0: michael@0: struct UConverter { michael@0: /* michael@0: * Error function pointer called when conversion issues michael@0: * occur during a ucnv_fromUnicode call michael@0: */ michael@0: void (U_EXPORT2 *fromUCharErrorBehaviour) (const void *context, michael@0: UConverterFromUnicodeArgs *args, michael@0: const UChar *codeUnits, michael@0: int32_t length, michael@0: UChar32 codePoint, michael@0: UConverterCallbackReason reason, michael@0: UErrorCode *); michael@0: /* michael@0: * Error function pointer called when conversion issues michael@0: * occur during a ucnv_toUnicode call michael@0: */ michael@0: void (U_EXPORT2 *fromCharErrorBehaviour) (const void *context, michael@0: UConverterToUnicodeArgs *args, michael@0: const char *codeUnits, michael@0: int32_t length, michael@0: UConverterCallbackReason reason, michael@0: UErrorCode *); michael@0: michael@0: /* michael@0: * Pointer to additional data that depends on the converter type. michael@0: * Used by ISO 2022, SCSU, GB 18030 converters, possibly more. michael@0: */ michael@0: void *extraInfo; michael@0: michael@0: const void *fromUContext; michael@0: const void *toUContext; michael@0: michael@0: /* michael@0: * Pointer to charset bytes for substitution string if subCharLen>0, michael@0: * or pointer to Unicode string (UChar *) if subCharLen<0. michael@0: * subCharLen==0 is equivalent to using a skip callback. michael@0: * If the pointer is !=subUChars then it is allocated with michael@0: * UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR bytes. michael@0: * The subUChars field is declared as UChar[] not uint8_t[] to michael@0: * guarantee alignment for UChars. michael@0: */ michael@0: uint8_t *subChars; michael@0: michael@0: UConverterSharedData *sharedData; /* Pointer to the shared immutable part of the converter object */ michael@0: michael@0: uint32_t options; /* options flags from UConverterOpen, may contain additional bits */ michael@0: michael@0: 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 */ michael@0: UBool isCopyLocal; /* TRUE if UConverter is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */ michael@0: UBool isExtraLocal; /* TRUE if extraInfo is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */ michael@0: michael@0: UBool useFallback; michael@0: int8_t toULength; /* number of bytes in toUBytes */ michael@0: uint8_t toUBytes[UCNV_MAX_CHAR_LEN-1];/* more "toU status"; keeps the bytes of the current character */ michael@0: uint32_t toUnicodeStatus; /* Used to internalize stream status information */ michael@0: int32_t mode; michael@0: uint32_t fromUnicodeStatus; michael@0: michael@0: /* michael@0: * More fromUnicode() status. Serves 3 purposes: michael@0: * - keeps a lead surrogate between buffers (similar to toUBytes[]) michael@0: * - keeps a lead surrogate at the end of the stream, michael@0: * which the framework handles as truncated input michael@0: * - if the fromUnicode() implementation returns to the framework michael@0: * (ucnv.c ucnv_fromUnicode()), then the framework calls the callback michael@0: * for this code point michael@0: */ michael@0: UChar32 fromUChar32; michael@0: michael@0: /* michael@0: * value for ucnv_getMaxCharSize() michael@0: * michael@0: * usually simply copied from the static data, but ucnvmbcs.c modifies michael@0: * the value depending on the converter type and options michael@0: */ michael@0: int8_t maxBytesPerUChar; michael@0: michael@0: int8_t subCharLen; /* length of the codepage specific character sequence */ michael@0: int8_t invalidCharLength; michael@0: int8_t charErrorBufferLength; /* number of valid bytes in charErrorBuffer */ michael@0: michael@0: int8_t invalidUCharLength; michael@0: int8_t UCharErrorBufferLength; /* number of valid UChars in charErrorBuffer */ michael@0: michael@0: uint8_t subChar1; /* single-byte substitution character if different from subChar */ michael@0: UBool useSubChar1; michael@0: char invalidCharBuffer[UCNV_MAX_CHAR_LEN]; /* bytes from last error/callback situation */ michael@0: uint8_t charErrorBuffer[UCNV_ERROR_BUFFER_LENGTH]; /* codepage output from Error functions */ michael@0: UChar subUChars[UCNV_MAX_SUBCHAR_LEN/U_SIZEOF_UCHAR]; /* see subChars documentation */ michael@0: michael@0: UChar invalidUCharBuffer[U16_MAX_LENGTH]; /* UChars from last error/callback situation */ michael@0: UChar UCharErrorBuffer[UCNV_ERROR_BUFFER_LENGTH]; /* unicode output from Error functions */ michael@0: michael@0: /* fields for conversion extension */ michael@0: michael@0: /* store previous UChars/chars to continue partial matches */ michael@0: UChar32 preFromUFirstCP; /* >=0: partial match */ michael@0: UChar preFromU[UCNV_EXT_MAX_UCHARS]; michael@0: char preToU[UCNV_EXT_MAX_BYTES]; michael@0: int8_t preFromULength, preToULength; /* negative: replay */ michael@0: int8_t preToUFirstLength; /* length of first character */ michael@0: michael@0: /* new fields for ICU 4.0 */ michael@0: UConverterCallbackReason toUCallbackReason; /* (*fromCharErrorBehaviour) reason, set when error is detected */ michael@0: }; michael@0: michael@0: U_CDECL_END /* end of UConverter */ michael@0: michael@0: #define CONVERTER_FILE_EXTENSION ".cnv" michael@0: michael@0: michael@0: /** michael@0: * Return the number of all converter names. michael@0: * @param pErrorCode The error code michael@0: * @return the number of all converter names michael@0: */ michael@0: U_CFUNC uint16_t michael@0: ucnv_bld_countAvailableConverters(UErrorCode *pErrorCode); michael@0: michael@0: /** michael@0: * Return the (n)th converter name in mixed case, or NULL michael@0: * if there is none (typically, if the data cannot be loaded). michael@0: * 0<=indexreferenceCounter != ~0 michael@0: * and this function must be called inside umtx_lock(&cnvCacheMutex). michael@0: */ michael@0: U_CAPI void michael@0: ucnv_unload(UConverterSharedData *sharedData); michael@0: michael@0: /** michael@0: * Swap ICU .cnv conversion tables. See udataswp.h. michael@0: * @internal michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucnv_swap(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode); michael@0: michael@0: #endif michael@0: michael@0: #endif /* _UCNV_BLD */