1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1999-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
10 * Definitions for converter implementations.
12 * Modification History:
14 * Date Name Description
15 * 05/09/00 helena Added implementation to handle fallback mappings.
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/uset.h"
33 /* this is used in fromUnicode DBCS tables as an "unassigned" marker */
34 #define missingCharMarker 0xFFFF
37 * #define missingUCharMarker 0xfffe
39 * commented out because there are actually two values used in toUnicode tables:
44 /** Forward declaration, see ucnv_bld.h */
45 struct UConverterSharedData
;
46 typedef struct UConverterSharedData UConverterSharedData
;
48 /* function types for UConverterImpl ---------------------------------------- */
50 /* struct with arguments for UConverterLoad and ucnv_load() */
52 int32_t size
; /* sizeof(UConverterLoadArgs) */
53 int32_t nestedLoads
; /* count nested ucnv_load() calls */
54 UBool onlyTestIsLoadable
; /* input: don't actually load */
55 UBool reserved0
; /* reserved - for good alignment of the pointers */
56 int16_t reserved
; /* reserved - for good alignment of the pointers */
58 const char *pkg
, *name
, *locale
;
61 #define UCNV_LOAD_ARGS_INITIALIZER \
62 { (int32_t)sizeof(UConverterLoadArgs), 0, FALSE, FALSE, 0, 0, NULL, NULL, NULL }
64 typedef void (*UConverterLoad
) (UConverterSharedData
*sharedData
,
65 UConverterLoadArgs
*pArgs
,
66 const uint8_t *raw
, UErrorCode
*pErrorCode
);
67 typedef void (*UConverterUnload
) (UConverterSharedData
*sharedData
);
69 typedef void (*UConverterOpen
) (UConverter
*cnv
, UConverterLoadArgs
*pArgs
, UErrorCode
*pErrorCode
);
70 typedef void (*UConverterClose
) (UConverter
*cnv
);
72 typedef enum UConverterResetChoice
{
74 UCNV_RESET_TO_UNICODE
,
75 UCNV_RESET_FROM_UNICODE
76 } UConverterResetChoice
;
78 typedef void (*UConverterReset
) (UConverter
*cnv
, UConverterResetChoice choice
);
81 * Converter implementation function(s) for ucnv_toUnicode().
82 * If the toUnicodeWithOffsets function pointer is NULL,
83 * then the toUnicode function will be used and the offsets will be set to -1.
85 * Must maintain state across buffers. Use toUBytes[toULength] for partial input
86 * sequences; it will be checked in ucnv.c at the end of the input stream
87 * to detect truncated input.
88 * Some converters may need additional detection and may then set U_TRUNCATED_CHAR_FOUND.
90 * The toUnicodeWithOffsets must write exactly as many offset values as target
91 * units. Write offset values of -1 for when the source index corresponding to
92 * the output unit is not known (e.g., the character started in an earlier buffer).
93 * The pArgs->offsets pointer need not be moved forward.
95 * At function return, either one of the following conditions must be true:
96 * - U_BUFFER_OVERFLOW_ERROR and the target is full: target==targetLimit
97 * - another error code with toUBytes[toULength] set to the offending input
98 * - no error, and the source is consumed: source==sourceLimit
100 * The ucnv.c code will handle the end of the input (reset)
101 * (reset, and truncation detection) and callbacks.
103 typedef void (*UConverterToUnicode
) (UConverterToUnicodeArgs
*, UErrorCode
*);
106 * Same rules as for UConverterToUnicode.
107 * A lead surrogate is kept in fromUChar32 across buffers, and if an error
108 * occurs, then the offending input code point must be put into fromUChar32
111 typedef void (*UConverterFromUnicode
) (UConverterFromUnicodeArgs
*, UErrorCode
*);
114 * Converter implementation function for ucnv_convertEx(), for direct conversion
115 * between two charsets without pivoting through UTF-16.
116 * The rules are the same as for UConverterToUnicode and UConverterFromUnicode.
118 * - The toUnicode side must behave and keep state exactly like the
119 * UConverterToUnicode implementation for the same source charset.
120 * - A U_USING_DEFAULT_WARNING can be set to request to temporarily fall back
121 * to pivoting. When this function is called, the conversion framework makes
122 * sure that this warning is not set on input.
123 * - Continuing a partial match and flushing the toUnicode replay buffer
124 * are handled by pivoting, using the toUnicode and fromUnicode functions.
126 typedef void (*UConverterConvert
) (UConverterFromUnicodeArgs
*pFromUArgs
,
127 UConverterToUnicodeArgs
*pToUArgs
,
128 UErrorCode
*pErrorCode
);
131 * Converter implementation function for ucnv_getNextUChar().
132 * If the function pointer is NULL, then the toUnicode function will be used.
134 * Will be called at a character boundary (toULength==0).
136 * - U_INDEX_OUTOFBOUNDS_ERROR if there was no output for the input
137 * (the return value will be ignored)
138 * - U_TRUNCATED_CHAR_FOUND or another error code (never U_BUFFER_OVERFLOW_ERROR!)
139 * with toUBytes[toULength] set to the offending input
140 * (the return value will be ignored)
141 * - return UCNV_GET_NEXT_UCHAR_USE_TO_U, without moving the source pointer,
142 * to indicate that the ucnv.c code shall call the toUnicode function instead
143 * - return a real code point result
145 * Unless UCNV_GET_NEXT_UCHAR_USE_TO_U is returned, the source bytes must be consumed.
147 * The ucnv.c code will handle the end of the input (reset)
148 * (except for truncation detection!) and callbacks.
150 typedef UChar32 (*UConverterGetNextUChar
) (UConverterToUnicodeArgs
*, UErrorCode
*);
152 typedef void (*UConverterGetStarters
)(const UConverter
* converter
,
154 UErrorCode
*pErrorCode
);
156 /* If this function pointer is null or if the function returns null
157 * the name field in static data struct should be returned by
158 * ucnv_getName() API function
160 typedef const char * (*UConverterGetName
) (const UConverter
*cnv
);
163 * Write the codepage substitution character.
164 * If this function is not set, then ucnv_cbFromUWriteSub() writes
165 * the substitution character from UConverter.
166 * For stateful converters, it is typically necessary to handle this
167 * specificially for the converter in order to properly maintain the state.
169 typedef void (*UConverterWriteSub
) (UConverterFromUnicodeArgs
*pArgs
, int32_t offsetIndex
, UErrorCode
*pErrorCode
);
172 * For converter-specific safeClone processing
173 * If this function is not set, then ucnv_safeClone assumes that the converter has no private data that changes
174 * after the converter is done opening.
175 * If this function is set, then it is called just after a memcpy() of
176 * converter data to the new, empty converter, and is expected to set up
177 * the initial state of the converter. It is not expected to increment the
178 * reference counts of the standard data types such as the shared data.
180 typedef UConverter
* (*UConverterSafeClone
) (const UConverter
*cnv
,
182 int32_t *pBufferSize
,
186 * Filters for some ucnv_getUnicodeSet() implementation code.
188 typedef enum UConverterSetFilter
{
189 UCNV_SET_FILTER_NONE
,
190 UCNV_SET_FILTER_DBCS_ONLY
,
191 UCNV_SET_FILTER_2022_CN
,
192 UCNV_SET_FILTER_SJIS
,
193 UCNV_SET_FILTER_GR94DBCS
,
195 UCNV_SET_FILTER_COUNT
196 } UConverterSetFilter
;
199 * Fills the set of Unicode code points that can be converted by an ICU converter.
200 * The API function ucnv_getUnicodeSet() clears the USet before calling
201 * the converter's getUnicodeSet() implementation; the converter should only
202 * add the appropriate code points to allow recursive use.
203 * For example, the ISO-2022-JP converter will call each subconverter's
204 * getUnicodeSet() implementation to consecutively add code points to
205 * the same USet, which will result in a union of the sets of all subconverters.
207 * For more documentation, see ucnv_getUnicodeSet() in ucnv.h.
209 typedef void (*UConverterGetUnicodeSet
) (const UConverter
*cnv
,
211 UConverterUnicodeSet which
,
212 UErrorCode
*pErrorCode
);
214 UBool
CONVERSION_U_SUCCESS (UErrorCode err
);
217 * UConverterImpl contains all the data and functions for a converter type.
218 * Its function pointers work much like a C++ vtable.
219 * Many converter types need to define only a subset of the functions;
220 * when a function pointer is NULL, then a default action will be performed.
222 * Every converter type must implement toUnicode, fromUnicode, and getNextUChar,
223 * otherwise the converter may crash.
224 * Every converter type that has variable-length codepage sequences should
225 * also implement toUnicodeWithOffsets and fromUnicodeWithOffsets for
226 * correct offset handling.
227 * All other functions may or may not be implemented - it depends only on
228 * whether the converter type needs them.
230 * When open() fails, then close() will be called, if present.
232 struct UConverterImpl
{
236 UConverterUnload unload
;
239 UConverterClose close
;
240 UConverterReset reset
;
242 UConverterToUnicode toUnicode
;
243 UConverterToUnicode toUnicodeWithOffsets
;
244 UConverterFromUnicode fromUnicode
;
245 UConverterFromUnicode fromUnicodeWithOffsets
;
246 UConverterGetNextUChar getNextUChar
;
248 UConverterGetStarters getStarters
;
249 UConverterGetName getName
;
250 UConverterWriteSub writeSub
;
251 UConverterSafeClone safeClone
;
252 UConverterGetUnicodeSet getUnicodeSet
;
254 UConverterConvert toUTF8
;
255 UConverterConvert fromUTF8
;
258 extern const UConverterSharedData
259 _MBCSData
, _Latin1Data
,
260 _UTF8Data
, _UTF16BEData
, _UTF16LEData
, _UTF32BEData
, _UTF32LEData
,
262 _LMBCSData1
,_LMBCSData2
, _LMBCSData3
, _LMBCSData4
, _LMBCSData5
, _LMBCSData6
,
263 _LMBCSData8
,_LMBCSData11
,_LMBCSData16
,_LMBCSData17
,_LMBCSData18
,_LMBCSData19
,
264 _HZData
,_ISCIIData
, _SCSUData
, _ASCIIData
,
265 _UTF7Data
, _Bocu1Data
, _UTF16Data
, _UTF32Data
, _CESU8Data
, _IMAPData
, _CompoundTextData
;
269 /** Always use fallbacks from codepage to Unicode */
270 #define TO_U_USE_FALLBACK(useFallback) TRUE
271 #define UCNV_TO_U_USE_FALLBACK(cnv) TRUE
273 /** Use fallbacks from Unicode to codepage when cnv->useFallback or for private-use code points */
274 #define IS_PRIVATE_USE(c) ((uint32_t)((c)-0xe000)<0x1900 || (uint32_t)((c)-0xf0000)<0x20000)
275 #define FROM_U_USE_FALLBACK(useFallback, c) ((useFallback) || IS_PRIVATE_USE(c))
276 #define UCNV_FROM_U_USE_FALLBACK(cnv, c) FROM_U_USE_FALLBACK((cnv)->useFallback, c)
279 * Magic number for ucnv_getNextUChar(), returned by a
280 * getNextUChar() implementation to indicate to use the converter's toUnicode()
281 * instead of the native function.
284 #define UCNV_GET_NEXT_UCHAR_USE_TO_U -9
287 ucnv_getCompleteUnicodeSet(const UConverter
*cnv
,
289 UConverterUnicodeSet which
,
290 UErrorCode
*pErrorCode
);
293 ucnv_getNonSurrogateUnicodeSet(const UConverter
*cnv
,
295 UConverterUnicodeSet which
,
296 UErrorCode
*pErrorCode
);
299 ucnv_fromUWriteBytes(UConverter
*cnv
,
300 const char *bytes
, int32_t length
,
301 char **target
, const char *targetLimit
,
304 UErrorCode
*pErrorCode
);
306 ucnv_toUWriteUChars(UConverter
*cnv
,
307 const UChar
*uchars
, int32_t length
,
308 UChar
**target
, const UChar
*targetLimit
,
311 UErrorCode
*pErrorCode
);
314 ucnv_toUWriteCodePoint(UConverter
*cnv
,
316 UChar
**target
, const UChar
*targetLimit
,
319 UErrorCode
*pErrorCode
);
323 #endif /* UCNV_CNV */