ucnv_cnv.h 13 KB

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