1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1999-2015 International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
11 * Contains internal data structure definitions
12 * Created by Bertrand A. Damiba
16 * 06/29/2000 helena Major rewrite of the callback APIs.
22 #include "unicode/utypes.h"
24 #if !UCONFIG_NO_CONVERSION
26 #include "unicode/ucnv.h"
27 #include "unicode/ucnv_err.h"
28 #include "unicode/utf16.h"
34 /* size of the overflow buffers in UConverter, enough for escaping callbacks */
35 #define UCNV_ERROR_BUFFER_LENGTH 32
37 /* at most 4 bytes per substitution character (part of .cnv file format! see UConverterStaticData) */
38 #define UCNV_MAX_SUBCHAR_LEN 4
40 /* at most 8 bytes per character in toUBytes[] (UTF-8 uses up to 6) */
41 #define UCNV_MAX_CHAR_LEN 8
43 /* converter options bits */
44 #define UCNV_OPTION_VERSION 0xf
45 #define UCNV_OPTION_SWAP_LFNL 0x10
47 #define UCNV_GET_VERSION(cnv) ((cnv)->options&UCNV_OPTION_VERSION)
49 U_CDECL_BEGIN /* We must declare the following as 'extern "C"' so that if ucnv
50 itself is compiled under C++, the linkage of the funcptrs will
54 union UConverterTable {
55 UConverterMBCSTable mbcs;
58 typedef union UConverterTable UConverterTable;
60 struct UConverterImpl;
61 typedef struct UConverterImpl UConverterImpl;
63 /** values for the unicodeMask */
64 #define UCNV_HAS_SUPPLEMENTARY 1
65 #define UCNV_HAS_SURROGATES 2
67 typedef struct UConverterStaticData { /* +offset: size */
68 uint32_t structSize; /* +0: 4 Size of this structure */
71 [UCNV_MAX_CONVERTER_NAME_LENGTH]; /* +4: 60 internal name of the converter- invariant chars */
73 int32_t codepage; /* +64: 4 codepage # (now IBM-$codepage) */
75 int8_t platform; /* +68: 1 platform of the converter (only IBM now) */
76 int8_t conversionType; /* +69: 1 conversion type */
78 int8_t minBytesPerChar; /* +70: 1 Minimum # bytes per char in this codepage */
79 int8_t maxBytesPerChar; /* +71: 1 Maximum # bytes output per UChar in this codepage */
81 uint8_t subChar[UCNV_MAX_SUBCHAR_LEN]; /* +72: 4 [note: 4 and 8 byte boundary] */
82 int8_t subCharLen; /* +76: 1 */
84 uint8_t hasToUnicodeFallback; /* +77: 1 UBool needs to be changed to UBool to be consistent across platform */
85 uint8_t hasFromUnicodeFallback; /* +78: 1 */
86 uint8_t unicodeMask; /* +79: 1 bit 0: has supplementary bit 1: has single surrogates */
87 uint8_t subChar1; /* +80: 1 single-byte substitution character for IBM MBCS (0 if none) */
88 uint8_t reserved[19]; /* +81: 19 to round out the structure */
90 } UConverterStaticData;
93 * Defines the UConverterSharedData struct,
94 * the immutable, shared part of UConverter.
96 struct UConverterSharedData {
97 uint32_t structSize; /* Size of this structure */
98 uint32_t referenceCounter; /* used to count number of clients, unused for static/immutable SharedData */
100 const void *dataMemory; /* from udata_openChoice() - for cleanup */
102 const UConverterStaticData *staticData; /* pointer to the static (non changing) data. */
104 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 */
105 /** If FALSE, then referenceCounter is not used. Must not change after initialization. */
106 UBool isReferenceCounted;
108 const UConverterImpl *impl; /* vtable-style struct of mostly function pointers */
110 /*initial values of some members of the mutable part of object */
111 uint32_t toUnicodeStatus;
114 * Shared data structures currently come in two flavors:
115 * - readonly for built-in algorithmic converters
116 * - allocated for MBCS, with a pointer to an allocated UConverterTable
117 * which always has a UConverterMBCSTable
119 * To eliminate one allocation, I am making the UConverterMBCSTable
120 * a member of the shared data.
124 UConverterMBCSTable mbcs;
127 /** UConverterSharedData initializer for static, non-reference-counted converters. */
128 #define UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(pStaticData, pImpl) \
130 sizeof(UConverterSharedData), ~((uint32_t)0), \
131 NULL, pStaticData, FALSE, FALSE, pImpl, \
132 0, UCNV_MBCS_TABLE_INITIALIZER \
135 /* Defines a UConverter, the lightweight mutable part the user sees */
139 * Error function pointer called when conversion issues
140 * occur during a ucnv_fromUnicode call
142 void (U_EXPORT2 *fromUCharErrorBehaviour) (const void *context,
143 UConverterFromUnicodeArgs *args,
144 const UChar *codeUnits,
147 UConverterCallbackReason reason,
150 * Error function pointer called when conversion issues
151 * occur during a ucnv_toUnicode call
153 void (U_EXPORT2 *fromCharErrorBehaviour) (const void *context,
154 UConverterToUnicodeArgs *args,
155 const char *codeUnits,
157 UConverterCallbackReason reason,
161 * Pointer to additional data that depends on the converter type.
162 * Used by ISO 2022, SCSU, GB 18030 converters, possibly more.
166 const void *fromUContext;
167 const void *toUContext;
170 * Pointer to charset bytes for substitution string if subCharLen>0,
171 * or pointer to Unicode string (UChar *) if subCharLen<0.
172 * subCharLen==0 is equivalent to using a skip callback.
173 * If the pointer is !=subUChars then it is allocated with
174 * UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR bytes.
175 * The subUChars field is declared as UChar[] not uint8_t[] to
176 * guarantee alignment for UChars.
180 UConverterSharedData *sharedData; /* Pointer to the shared immutable part of the converter object */
182 uint32_t options; /* options flags from UConverterOpen, may contain additional bits */
184 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 */
185 UBool isCopyLocal; /* TRUE if UConverter is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
186 UBool isExtraLocal; /* TRUE if extraInfo is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
189 int8_t toULength; /* number of bytes in toUBytes */
190 uint8_t toUBytes[UCNV_MAX_CHAR_LEN-1];/* more "toU status"; keeps the bytes of the current character */
191 uint32_t toUnicodeStatus; /* Used to internalize stream status information */
193 uint32_t fromUnicodeStatus;
196 * More fromUnicode() status. Serves 3 purposes:
197 * - keeps a lead surrogate between buffers (similar to toUBytes[])
198 * - keeps a lead surrogate at the end of the stream,
199 * which the framework handles as truncated input
200 * - if the fromUnicode() implementation returns to the framework
201 * (ucnv.c ucnv_fromUnicode()), then the framework calls the callback
202 * for this code point
207 * value for ucnv_getMaxCharSize()
209 * usually simply copied from the static data, but ucnvmbcs.c modifies
210 * the value depending on the converter type and options
212 int8_t maxBytesPerUChar;
214 int8_t subCharLen; /* length of the codepage specific character sequence */
215 int8_t invalidCharLength;
216 int8_t charErrorBufferLength; /* number of valid bytes in charErrorBuffer */
218 int8_t invalidUCharLength;
219 int8_t UCharErrorBufferLength; /* number of valid UChars in charErrorBuffer */
221 uint8_t subChar1; /* single-byte substitution character if different from subChar */
223 char invalidCharBuffer[UCNV_MAX_CHAR_LEN]; /* bytes from last error/callback situation */
224 uint8_t charErrorBuffer[UCNV_ERROR_BUFFER_LENGTH]; /* codepage output from Error functions */
225 UChar subUChars[UCNV_MAX_SUBCHAR_LEN/U_SIZEOF_UCHAR]; /* see subChars documentation */
227 UChar invalidUCharBuffer[U16_MAX_LENGTH]; /* UChars from last error/callback situation */
228 UChar UCharErrorBuffer[UCNV_ERROR_BUFFER_LENGTH]; /* unicode output from Error functions */
230 /* fields for conversion extension */
232 /* store previous UChars/chars to continue partial matches */
233 UChar32 preFromUFirstCP; /* >=0: partial match */
234 UChar preFromU[UCNV_EXT_MAX_UCHARS];
235 char preToU[UCNV_EXT_MAX_BYTES];
236 int8_t preFromULength, preToULength; /* negative: replay */
237 int8_t preToUFirstLength; /* length of first character */
239 /* new fields for ICU 4.0 */
240 UConverterCallbackReason toUCallbackReason; /* (*fromCharErrorBehaviour) reason, set when error is detected */
243 U_CDECL_END /* end of UConverter */
245 #define CONVERTER_FILE_EXTENSION ".cnv"
249 * Return the number of all converter names.
250 * @param pErrorCode The error code
251 * @return the number of all converter names
254 ucnv_bld_countAvailableConverters(UErrorCode *pErrorCode);
257 * Return the (n)th converter name in mixed case, or NULL
258 * if there is none (typically, if the data cannot be loaded).
259 * 0<=index<ucnv_io_countAvailableConverters().
260 * @param n The number specifies which converter name to get
261 * @param pErrorCode The error code
262 * @return the (n)th converter name in mixed case, or NULL if there is none.
265 ucnv_bld_getAvailableConverter(uint16_t n, UErrorCode *pErrorCode);
268 * Load a non-algorithmic converter.
269 * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
271 U_CAPI UConverterSharedData *
272 ucnv_load(UConverterLoadArgs *pArgs, UErrorCode *err);
275 * Unload a non-algorithmic converter.
276 * It must be sharedData->isReferenceCounted
277 * and this function must be called inside umtx_lock(&cnvCacheMutex).
280 ucnv_unload(UConverterSharedData *sharedData);
283 * Swap ICU .cnv conversion tables. See udataswp.h.
286 U_CAPI int32_t U_EXPORT2
287 ucnv_swap(const UDataSwapper *ds,
288 const void *inData, int32_t length, void *outData,
289 UErrorCode *pErrorCode);
293 #endif /* _UCNV_BLD */