intl/icu/source/common/ucnv_cnv.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 1999-2011, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 *
michael@0 7 * ucnv_cnv.h:
michael@0 8 * Definitions for converter implementations.
michael@0 9 *
michael@0 10 * Modification History:
michael@0 11 *
michael@0 12 * Date Name Description
michael@0 13 * 05/09/00 helena Added implementation to handle fallback mappings.
michael@0 14 * 06/29/2000 helena Major rewrite of the callback APIs.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef UCNV_CNV_H
michael@0 18 #define UCNV_CNV_H
michael@0 19
michael@0 20 #include "unicode/utypes.h"
michael@0 21
michael@0 22 #if !UCONFIG_NO_CONVERSION
michael@0 23
michael@0 24 #include "unicode/ucnv.h"
michael@0 25 #include "unicode/ucnv_err.h"
michael@0 26 #include "unicode/uset.h"
michael@0 27 #include "uset_imp.h"
michael@0 28
michael@0 29 U_CDECL_BEGIN
michael@0 30
michael@0 31 /* this is used in fromUnicode DBCS tables as an "unassigned" marker */
michael@0 32 #define missingCharMarker 0xFFFF
michael@0 33
michael@0 34 /*
michael@0 35 * #define missingUCharMarker 0xfffe
michael@0 36 *
michael@0 37 * commented out because there are actually two values used in toUnicode tables:
michael@0 38 * U+fffe "unassigned"
michael@0 39 * U+ffff "illegal"
michael@0 40 */
michael@0 41
michael@0 42 /** Forward declaration, see ucnv_bld.h */
michael@0 43 struct UConverterSharedData;
michael@0 44 typedef struct UConverterSharedData UConverterSharedData;
michael@0 45
michael@0 46 /* function types for UConverterImpl ---------------------------------------- */
michael@0 47
michael@0 48 /* struct with arguments for UConverterLoad and ucnv_load() */
michael@0 49 typedef struct {
michael@0 50 int32_t size; /* sizeof(UConverterLoadArgs) */
michael@0 51 int32_t nestedLoads; /* count nested ucnv_load() calls */
michael@0 52 UBool onlyTestIsLoadable; /* input: don't actually load */
michael@0 53 UBool reserved0; /* reserved - for good alignment of the pointers */
michael@0 54 int16_t reserved; /* reserved - for good alignment of the pointers */
michael@0 55 uint32_t options;
michael@0 56 const char *pkg, *name, *locale;
michael@0 57 } UConverterLoadArgs;
michael@0 58
michael@0 59 #define UCNV_LOAD_ARGS_INITIALIZER \
michael@0 60 { (int32_t)sizeof(UConverterLoadArgs), 0, FALSE, FALSE, 0, 0, NULL, NULL, NULL }
michael@0 61
michael@0 62 typedef void (*UConverterLoad) (UConverterSharedData *sharedData,
michael@0 63 UConverterLoadArgs *pArgs,
michael@0 64 const uint8_t *raw, UErrorCode *pErrorCode);
michael@0 65 typedef void (*UConverterUnload) (UConverterSharedData *sharedData);
michael@0 66
michael@0 67 typedef void (*UConverterOpen) (UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *pErrorCode);
michael@0 68 typedef void (*UConverterClose) (UConverter *cnv);
michael@0 69
michael@0 70 typedef enum UConverterResetChoice {
michael@0 71 UCNV_RESET_BOTH,
michael@0 72 UCNV_RESET_TO_UNICODE,
michael@0 73 UCNV_RESET_FROM_UNICODE
michael@0 74 } UConverterResetChoice;
michael@0 75
michael@0 76 typedef void (*UConverterReset) (UConverter *cnv, UConverterResetChoice choice);
michael@0 77
michael@0 78 /*
michael@0 79 * Converter implementation function(s) for ucnv_toUnicode().
michael@0 80 * If the toUnicodeWithOffsets function pointer is NULL,
michael@0 81 * then the toUnicode function will be used and the offsets will be set to -1.
michael@0 82 *
michael@0 83 * Must maintain state across buffers. Use toUBytes[toULength] for partial input
michael@0 84 * sequences; it will be checked in ucnv.c at the end of the input stream
michael@0 85 * to detect truncated input.
michael@0 86 * Some converters may need additional detection and may then set U_TRUNCATED_CHAR_FOUND.
michael@0 87 *
michael@0 88 * The toUnicodeWithOffsets must write exactly as many offset values as target
michael@0 89 * units. Write offset values of -1 for when the source index corresponding to
michael@0 90 * the output unit is not known (e.g., the character started in an earlier buffer).
michael@0 91 * The pArgs->offsets pointer need not be moved forward.
michael@0 92 *
michael@0 93 * At function return, either one of the following conditions must be true:
michael@0 94 * - U_BUFFER_OVERFLOW_ERROR and the target is full: target==targetLimit
michael@0 95 * - another error code with toUBytes[toULength] set to the offending input
michael@0 96 * - no error, and the source is consumed: source==sourceLimit
michael@0 97 *
michael@0 98 * The ucnv.c code will handle the end of the input (reset)
michael@0 99 * (reset, and truncation detection) and callbacks.
michael@0 100 */
michael@0 101 typedef void (*UConverterToUnicode) (UConverterToUnicodeArgs *, UErrorCode *);
michael@0 102
michael@0 103 /*
michael@0 104 * Same rules as for UConverterToUnicode.
michael@0 105 * A lead surrogate is kept in fromUChar32 across buffers, and if an error
michael@0 106 * occurs, then the offending input code point must be put into fromUChar32
michael@0 107 * as well.
michael@0 108 */
michael@0 109 typedef void (*UConverterFromUnicode) (UConverterFromUnicodeArgs *, UErrorCode *);
michael@0 110
michael@0 111 /*
michael@0 112 * Converter implementation function for ucnv_convertEx(), for direct conversion
michael@0 113 * between two charsets without pivoting through UTF-16.
michael@0 114 * The rules are the same as for UConverterToUnicode and UConverterFromUnicode.
michael@0 115 * In addition,
michael@0 116 * - The toUnicode side must behave and keep state exactly like the
michael@0 117 * UConverterToUnicode implementation for the same source charset.
michael@0 118 * - A U_USING_DEFAULT_WARNING can be set to request to temporarily fall back
michael@0 119 * to pivoting. When this function is called, the conversion framework makes
michael@0 120 * sure that this warning is not set on input.
michael@0 121 * - Continuing a partial match and flushing the toUnicode replay buffer
michael@0 122 * are handled by pivoting, using the toUnicode and fromUnicode functions.
michael@0 123 */
michael@0 124 typedef void (*UConverterConvert) (UConverterFromUnicodeArgs *pFromUArgs,
michael@0 125 UConverterToUnicodeArgs *pToUArgs,
michael@0 126 UErrorCode *pErrorCode);
michael@0 127
michael@0 128 /*
michael@0 129 * Converter implementation function for ucnv_getNextUChar().
michael@0 130 * If the function pointer is NULL, then the toUnicode function will be used.
michael@0 131 *
michael@0 132 * Will be called at a character boundary (toULength==0).
michael@0 133 * May return with
michael@0 134 * - U_INDEX_OUTOFBOUNDS_ERROR if there was no output for the input
michael@0 135 * (the return value will be ignored)
michael@0 136 * - U_TRUNCATED_CHAR_FOUND or another error code (never U_BUFFER_OVERFLOW_ERROR!)
michael@0 137 * with toUBytes[toULength] set to the offending input
michael@0 138 * (the return value will be ignored)
michael@0 139 * - return UCNV_GET_NEXT_UCHAR_USE_TO_U, without moving the source pointer,
michael@0 140 * to indicate that the ucnv.c code shall call the toUnicode function instead
michael@0 141 * - return a real code point result
michael@0 142 *
michael@0 143 * Unless UCNV_GET_NEXT_UCHAR_USE_TO_U is returned, the source bytes must be consumed.
michael@0 144 *
michael@0 145 * The ucnv.c code will handle the end of the input (reset)
michael@0 146 * (except for truncation detection!) and callbacks.
michael@0 147 */
michael@0 148 typedef UChar32 (*UConverterGetNextUChar) (UConverterToUnicodeArgs *, UErrorCode *);
michael@0 149
michael@0 150 typedef void (*UConverterGetStarters)(const UConverter* converter,
michael@0 151 UBool starters[256],
michael@0 152 UErrorCode *pErrorCode);
michael@0 153
michael@0 154 /* If this function pointer is null or if the function returns null
michael@0 155 * the name field in static data struct should be returned by
michael@0 156 * ucnv_getName() API function
michael@0 157 */
michael@0 158 typedef const char * (*UConverterGetName) (const UConverter *cnv);
michael@0 159
michael@0 160 /**
michael@0 161 * Write the codepage substitution character.
michael@0 162 * If this function is not set, then ucnv_cbFromUWriteSub() writes
michael@0 163 * the substitution character from UConverter.
michael@0 164 * For stateful converters, it is typically necessary to handle this
michael@0 165 * specificially for the converter in order to properly maintain the state.
michael@0 166 */
michael@0 167 typedef void (*UConverterWriteSub) (UConverterFromUnicodeArgs *pArgs, int32_t offsetIndex, UErrorCode *pErrorCode);
michael@0 168
michael@0 169 /**
michael@0 170 * For converter-specific safeClone processing
michael@0 171 * If this function is not set, then ucnv_safeClone assumes that the converter has no private data that changes
michael@0 172 * after the converter is done opening.
michael@0 173 * If this function is set, then it is called just after a memcpy() of
michael@0 174 * converter data to the new, empty converter, and is expected to set up
michael@0 175 * the initial state of the converter. It is not expected to increment the
michael@0 176 * reference counts of the standard data types such as the shared data.
michael@0 177 */
michael@0 178 typedef UConverter * (*UConverterSafeClone) (const UConverter *cnv,
michael@0 179 void *stackBuffer,
michael@0 180 int32_t *pBufferSize,
michael@0 181 UErrorCode *status);
michael@0 182
michael@0 183 /**
michael@0 184 * Filters for some ucnv_getUnicodeSet() implementation code.
michael@0 185 */
michael@0 186 typedef enum UConverterSetFilter {
michael@0 187 UCNV_SET_FILTER_NONE,
michael@0 188 UCNV_SET_FILTER_DBCS_ONLY,
michael@0 189 UCNV_SET_FILTER_2022_CN,
michael@0 190 UCNV_SET_FILTER_SJIS,
michael@0 191 UCNV_SET_FILTER_GR94DBCS,
michael@0 192 UCNV_SET_FILTER_HZ,
michael@0 193 UCNV_SET_FILTER_COUNT
michael@0 194 } UConverterSetFilter;
michael@0 195
michael@0 196 /**
michael@0 197 * Fills the set of Unicode code points that can be converted by an ICU converter.
michael@0 198 * The API function ucnv_getUnicodeSet() clears the USet before calling
michael@0 199 * the converter's getUnicodeSet() implementation; the converter should only
michael@0 200 * add the appropriate code points to allow recursive use.
michael@0 201 * For example, the ISO-2022-JP converter will call each subconverter's
michael@0 202 * getUnicodeSet() implementation to consecutively add code points to
michael@0 203 * the same USet, which will result in a union of the sets of all subconverters.
michael@0 204 *
michael@0 205 * For more documentation, see ucnv_getUnicodeSet() in ucnv.h.
michael@0 206 */
michael@0 207 typedef void (*UConverterGetUnicodeSet) (const UConverter *cnv,
michael@0 208 const USetAdder *sa,
michael@0 209 UConverterUnicodeSet which,
michael@0 210 UErrorCode *pErrorCode);
michael@0 211
michael@0 212 UBool CONVERSION_U_SUCCESS (UErrorCode err);
michael@0 213
michael@0 214 /**
michael@0 215 * UConverterImpl contains all the data and functions for a converter type.
michael@0 216 * Its function pointers work much like a C++ vtable.
michael@0 217 * Many converter types need to define only a subset of the functions;
michael@0 218 * when a function pointer is NULL, then a default action will be performed.
michael@0 219 *
michael@0 220 * Every converter type must implement toUnicode, fromUnicode, and getNextUChar,
michael@0 221 * otherwise the converter may crash.
michael@0 222 * Every converter type that has variable-length codepage sequences should
michael@0 223 * also implement toUnicodeWithOffsets and fromUnicodeWithOffsets for
michael@0 224 * correct offset handling.
michael@0 225 * All other functions may or may not be implemented - it depends only on
michael@0 226 * whether the converter type needs them.
michael@0 227 *
michael@0 228 * When open() fails, then close() will be called, if present.
michael@0 229 */
michael@0 230 struct UConverterImpl {
michael@0 231 UConverterType type;
michael@0 232
michael@0 233 UConverterLoad load;
michael@0 234 UConverterUnload unload;
michael@0 235
michael@0 236 UConverterOpen open;
michael@0 237 UConverterClose close;
michael@0 238 UConverterReset reset;
michael@0 239
michael@0 240 UConverterToUnicode toUnicode;
michael@0 241 UConverterToUnicode toUnicodeWithOffsets;
michael@0 242 UConverterFromUnicode fromUnicode;
michael@0 243 UConverterFromUnicode fromUnicodeWithOffsets;
michael@0 244 UConverterGetNextUChar getNextUChar;
michael@0 245
michael@0 246 UConverterGetStarters getStarters;
michael@0 247 UConverterGetName getName;
michael@0 248 UConverterWriteSub writeSub;
michael@0 249 UConverterSafeClone safeClone;
michael@0 250 UConverterGetUnicodeSet getUnicodeSet;
michael@0 251
michael@0 252 UConverterConvert toUTF8;
michael@0 253 UConverterConvert fromUTF8;
michael@0 254 };
michael@0 255
michael@0 256 extern const UConverterSharedData
michael@0 257 _MBCSData, _Latin1Data,
michael@0 258 _UTF8Data, _UTF16BEData, _UTF16LEData, _UTF32BEData, _UTF32LEData,
michael@0 259 _ISO2022Data,
michael@0 260 _LMBCSData1,_LMBCSData2, _LMBCSData3, _LMBCSData4, _LMBCSData5, _LMBCSData6,
michael@0 261 _LMBCSData8,_LMBCSData11,_LMBCSData16,_LMBCSData17,_LMBCSData18,_LMBCSData19,
michael@0 262 _HZData,_ISCIIData, _SCSUData, _ASCIIData,
michael@0 263 _UTF7Data, _Bocu1Data, _UTF16Data, _UTF32Data, _CESU8Data, _IMAPData, _CompoundTextData;
michael@0 264
michael@0 265 U_CDECL_END
michael@0 266
michael@0 267 /** Always use fallbacks from codepage to Unicode */
michael@0 268 #define TO_U_USE_FALLBACK(useFallback) TRUE
michael@0 269 #define UCNV_TO_U_USE_FALLBACK(cnv) TRUE
michael@0 270
michael@0 271 /** Use fallbacks from Unicode to codepage when cnv->useFallback or for private-use code points */
michael@0 272 #define IS_PRIVATE_USE(c) ((uint32_t)((c)-0xe000)<0x1900 || (uint32_t)((c)-0xf0000)<0x20000)
michael@0 273 #define FROM_U_USE_FALLBACK(useFallback, c) ((useFallback) || IS_PRIVATE_USE(c))
michael@0 274 #define UCNV_FROM_U_USE_FALLBACK(cnv, c) FROM_U_USE_FALLBACK((cnv)->useFallback, c)
michael@0 275
michael@0 276 /**
michael@0 277 * Magic number for ucnv_getNextUChar(), returned by a
michael@0 278 * getNextUChar() implementation to indicate to use the converter's toUnicode()
michael@0 279 * instead of the native function.
michael@0 280 * @internal
michael@0 281 */
michael@0 282 #define UCNV_GET_NEXT_UCHAR_USE_TO_U -9
michael@0 283
michael@0 284 U_CFUNC void
michael@0 285 ucnv_getCompleteUnicodeSet(const UConverter *cnv,
michael@0 286 const USetAdder *sa,
michael@0 287 UConverterUnicodeSet which,
michael@0 288 UErrorCode *pErrorCode);
michael@0 289
michael@0 290 U_CFUNC void
michael@0 291 ucnv_getNonSurrogateUnicodeSet(const UConverter *cnv,
michael@0 292 const USetAdder *sa,
michael@0 293 UConverterUnicodeSet which,
michael@0 294 UErrorCode *pErrorCode);
michael@0 295
michael@0 296 U_CFUNC void
michael@0 297 ucnv_fromUWriteBytes(UConverter *cnv,
michael@0 298 const char *bytes, int32_t length,
michael@0 299 char **target, const char *targetLimit,
michael@0 300 int32_t **offsets,
michael@0 301 int32_t sourceIndex,
michael@0 302 UErrorCode *pErrorCode);
michael@0 303 U_CFUNC void
michael@0 304 ucnv_toUWriteUChars(UConverter *cnv,
michael@0 305 const UChar *uchars, int32_t length,
michael@0 306 UChar **target, const UChar *targetLimit,
michael@0 307 int32_t **offsets,
michael@0 308 int32_t sourceIndex,
michael@0 309 UErrorCode *pErrorCode);
michael@0 310
michael@0 311 U_CFUNC void
michael@0 312 ucnv_toUWriteCodePoint(UConverter *cnv,
michael@0 313 UChar32 c,
michael@0 314 UChar **target, const UChar *targetLimit,
michael@0 315 int32_t **offsets,
michael@0 316 int32_t sourceIndex,
michael@0 317 UErrorCode *pErrorCode);
michael@0 318
michael@0 319 #endif
michael@0 320
michael@0 321 #endif /* UCNV_CNV */

mercurial