2 **********************************************************************
3 * Copyright (C) 1999-2015 International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
9 * Contains internal data structure definitions
10 * Created by Bertrand A. Damiba
14 * 06/29/2000 helena Major rewrite of the callback APIs.
20 #include "unicode/utypes.h"
22 #if !UCONFIG_NO_CONVERSION
24 #include "unicode/ucnv.h"
25 #include "unicode/ucnv_err.h"
26 #include "unicode/utf16.h"
32 /* size of the overflow buffers in UConverter, enough for escaping callbacks */
33 #define UCNV_ERROR_BUFFER_LENGTH 32
35 /* at most 4 bytes per substitution character (part of .cnv file format! see UConverterStaticData) */
36 #define UCNV_MAX_SUBCHAR_LEN 4
38 /* at most 8 bytes per character in toUBytes[] (UTF-8 uses up to 6) */
39 #define UCNV_MAX_CHAR_LEN 8
41 /* converter options bits */
42 #define UCNV_OPTION_VERSION 0xf
43 #define UCNV_OPTION_SWAP_LFNL 0x10
45 #define UCNV_GET_VERSION(cnv) ((cnv)->options&UCNV_OPTION_VERSION)
47 U_CDECL_BEGIN
/* We must declare the following as 'extern "C"' so that if ucnv
48 itself is compiled under C++, the linkage of the funcptrs will
52 union UConverterTable
{
53 UConverterMBCSTable mbcs
;
56 typedef union UConverterTable UConverterTable
;
58 struct UConverterImpl
;
59 typedef struct UConverterImpl UConverterImpl
;
61 /** values for the unicodeMask */
62 #define UCNV_HAS_SUPPLEMENTARY 1
63 #define UCNV_HAS_SURROGATES 2
65 typedef struct UConverterStaticData
{ /* +offset: size */
66 uint32_t structSize
; /* +0: 4 Size of this structure */
69 [UCNV_MAX_CONVERTER_NAME_LENGTH
]; /* +4: 60 internal name of the converter- invariant chars */
71 int32_t codepage
; /* +64: 4 codepage # (now IBM-$codepage) */
73 int8_t platform
; /* +68: 1 platform of the converter (only IBM now) */
74 int8_t conversionType
; /* +69: 1 conversion type */
76 int8_t minBytesPerChar
; /* +70: 1 Minimum # bytes per char in this codepage */
77 int8_t maxBytesPerChar
; /* +71: 1 Maximum # bytes output per UChar in this codepage */
79 uint8_t subChar
[UCNV_MAX_SUBCHAR_LEN
]; /* +72: 4 [note: 4 and 8 byte boundary] */
80 int8_t subCharLen
; /* +76: 1 */
82 uint8_t hasToUnicodeFallback
; /* +77: 1 UBool needs to be changed to UBool to be consistent across platform */
83 uint8_t hasFromUnicodeFallback
; /* +78: 1 */
84 uint8_t unicodeMask
; /* +79: 1 bit 0: has supplementary bit 1: has single surrogates */
85 uint8_t subChar1
; /* +80: 1 single-byte substitution character for IBM MBCS (0 if none) */
86 uint8_t reserved
[19]; /* +81: 19 to round out the structure */
88 } UConverterStaticData
;
91 * Defines the UConverterSharedData struct,
92 * the immutable, shared part of UConverter.
94 struct UConverterSharedData
{
95 uint32_t structSize
; /* Size of this structure */
96 uint32_t referenceCounter
; /* used to count number of clients, unused for static/immutable SharedData */
98 const void *dataMemory
; /* from udata_openChoice() - for cleanup */
100 const UConverterStaticData
*staticData
; /* pointer to the static (non changing) data. */
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 /** If FALSE, then referenceCounter is not used. Must not change after initialization. */
104 UBool isReferenceCounted
;
106 const UConverterImpl
*impl
; /* vtable-style struct of mostly function pointers */
108 /*initial values of some members of the mutable part of object */
109 uint32_t toUnicodeStatus
;
112 * Shared data structures currently come in two flavors:
113 * - readonly for built-in algorithmic converters
114 * - allocated for MBCS, with a pointer to an allocated UConverterTable
115 * which always has a UConverterMBCSTable
117 * To eliminate one allocation, I am making the UConverterMBCSTable
118 * a member of the shared data.
122 UConverterMBCSTable mbcs
;
125 /** UConverterSharedData initializer for static, non-reference-counted converters. */
126 #define UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(pStaticData, pImpl) \
128 sizeof(UConverterSharedData), ~((uint32_t)0), \
129 NULL, pStaticData, FALSE, FALSE, pImpl, \
130 0, UCNV_MBCS_TABLE_INITIALIZER \
133 /* Defines a UConverter, the lightweight mutable part the user sees */
137 * Error function pointer called when conversion issues
138 * occur during a ucnv_fromUnicode call
140 void (U_EXPORT2
*fromUCharErrorBehaviour
) (const void *context
,
141 UConverterFromUnicodeArgs
*args
,
142 const UChar
*codeUnits
,
145 UConverterCallbackReason reason
,
148 * Error function pointer called when conversion issues
149 * occur during a ucnv_toUnicode call
151 void (U_EXPORT2
*fromCharErrorBehaviour
) (const void *context
,
152 UConverterToUnicodeArgs
*args
,
153 const char *codeUnits
,
155 UConverterCallbackReason reason
,
159 * Pointer to additional data that depends on the converter type.
160 * Used by ISO 2022, SCSU, GB 18030 converters, possibly more.
164 const void *fromUContext
;
165 const void *toUContext
;
168 * Pointer to charset bytes for substitution string if subCharLen>0,
169 * or pointer to Unicode string (UChar *) if subCharLen<0.
170 * subCharLen==0 is equivalent to using a skip callback.
171 * If the pointer is !=subUChars then it is allocated with
172 * UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR bytes.
173 * The subUChars field is declared as UChar[] not uint8_t[] to
174 * guarantee alignment for UChars.
178 UConverterSharedData
*sharedData
; /* Pointer to the shared immutable part of the converter object */
180 uint32_t options
; /* options flags from UConverterOpen, may contain additional bits */
182 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 */
183 UBool isCopyLocal
; /* TRUE if UConverter is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
184 UBool isExtraLocal
; /* TRUE if extraInfo is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
187 int8_t toULength
; /* number of bytes in toUBytes */
188 uint8_t toUBytes
[UCNV_MAX_CHAR_LEN
-1];/* more "toU status"; keeps the bytes of the current character */
189 uint32_t toUnicodeStatus
; /* Used to internalize stream status information */
191 uint32_t fromUnicodeStatus
;
194 * More fromUnicode() status. Serves 3 purposes:
195 * - keeps a lead surrogate between buffers (similar to toUBytes[])
196 * - keeps a lead surrogate at the end of the stream,
197 * which the framework handles as truncated input
198 * - if the fromUnicode() implementation returns to the framework
199 * (ucnv.c ucnv_fromUnicode()), then the framework calls the callback
200 * for this code point
205 * value for ucnv_getMaxCharSize()
207 * usually simply copied from the static data, but ucnvmbcs.c modifies
208 * the value depending on the converter type and options
210 int8_t maxBytesPerUChar
;
212 int8_t subCharLen
; /* length of the codepage specific character sequence */
213 int8_t invalidCharLength
;
214 int8_t charErrorBufferLength
; /* number of valid bytes in charErrorBuffer */
216 int8_t invalidUCharLength
;
217 int8_t UCharErrorBufferLength
; /* number of valid UChars in charErrorBuffer */
219 uint8_t subChar1
; /* single-byte substitution character if different from subChar */
221 char invalidCharBuffer
[UCNV_MAX_CHAR_LEN
]; /* bytes from last error/callback situation */
222 uint8_t charErrorBuffer
[UCNV_ERROR_BUFFER_LENGTH
]; /* codepage output from Error functions */
223 UChar subUChars
[UCNV_MAX_SUBCHAR_LEN
/U_SIZEOF_UCHAR
]; /* see subChars documentation */
225 UChar invalidUCharBuffer
[U16_MAX_LENGTH
]; /* UChars from last error/callback situation */
226 UChar UCharErrorBuffer
[UCNV_ERROR_BUFFER_LENGTH
]; /* unicode output from Error functions */
228 /* fields for conversion extension */
230 /* store previous UChars/chars to continue partial matches */
231 UChar32 preFromUFirstCP
; /* >=0: partial match */
232 UChar preFromU
[UCNV_EXT_MAX_UCHARS
];
233 char preToU
[UCNV_EXT_MAX_BYTES
];
234 int8_t preFromULength
, preToULength
; /* negative: replay */
235 int8_t preToUFirstLength
; /* length of first character */
237 /* new fields for ICU 4.0 */
238 UConverterCallbackReason toUCallbackReason
; /* (*fromCharErrorBehaviour) reason, set when error is detected */
241 U_CDECL_END
/* end of UConverter */
243 #define CONVERTER_FILE_EXTENSION ".cnv"
247 * Return the number of all converter names.
248 * @param pErrorCode The error code
249 * @return the number of all converter names
252 ucnv_bld_countAvailableConverters(UErrorCode
*pErrorCode
);
255 * Return the (n)th converter name in mixed case, or NULL
256 * if there is none (typically, if the data cannot be loaded).
257 * 0<=index<ucnv_io_countAvailableConverters().
258 * @param n The number specifies which converter name to get
259 * @param pErrorCode The error code
260 * @return the (n)th converter name in mixed case, or NULL if there is none.
263 ucnv_bld_getAvailableConverter(uint16_t n
, UErrorCode
*pErrorCode
);
266 * Load a non-algorithmic converter.
267 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
269 U_CAPI UConverterSharedData
*
270 ucnv_load(UConverterLoadArgs
*pArgs
, UErrorCode
*err
);
273 * Unload a non-algorithmic converter.
274 * It must be sharedData->isReferenceCounted
275 * and this function must be called inside umtx_lock(&cnvCacheMutex).
278 ucnv_unload(UConverterSharedData
*sharedData
);
281 * Swap ICU .cnv conversion tables. See udataswp.h.
284 U_CAPI
int32_t U_EXPORT2
285 ucnv_swap(const UDataSwapper
*ds
,
286 const void *inData
, int32_t length
, void *outData
,
287 UErrorCode
*pErrorCode
);
291 #endif /* _UCNV_BLD */