ucnv_err.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. **********************************************************************
  3. * Copyright (C) 1999-2009, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. **********************************************************************
  6. *
  7. *
  8. * ucnv_err.h:
  9. */
  10. /**
  11. * \file
  12. * \brief C UConverter predefined error callbacks
  13. *
  14. * <h2>Error Behaviour Functions</h2>
  15. * Defines some error behaviour functions called by ucnv_{from,to}Unicode
  16. * These are provided as part of ICU and many are stable, but they
  17. * can also be considered only as an example of what can be done with
  18. * callbacks. You may of course write your own.
  19. *
  20. * If you want to write your own, you may also find the functions from
  21. * ucnv_cb.h useful when writing your own callbacks.
  22. *
  23. * These functions, although public, should NEVER be called directly.
  24. * They should be used as parameters to the ucnv_setFromUCallback
  25. * and ucnv_setToUCallback functions, to set the behaviour of a converter
  26. * when it encounters ILLEGAL/UNMAPPED/INVALID sequences.
  27. *
  28. * usage example: 'STOP' doesn't need any context, but newContext
  29. * could be set to something other than 'NULL' if needed. The available
  30. * contexts in this header can modify the default behavior of the callback.
  31. *
  32. * \code
  33. * UErrorCode err = U_ZERO_ERROR;
  34. * UConverter *myConverter = ucnv_open("ibm-949", &err);
  35. * const void *oldContext;
  36. * UConverterFromUCallback oldAction;
  37. *
  38. *
  39. * if (U_SUCCESS(err))
  40. * {
  41. * ucnv_setFromUCallBack(myConverter,
  42. * UCNV_FROM_U_CALLBACK_STOP,
  43. * NULL,
  44. * &oldAction,
  45. * &oldContext,
  46. * &status);
  47. * }
  48. * \endcode
  49. *
  50. * The code above tells "myConverter" to stop when it encounters an
  51. * ILLEGAL/TRUNCATED/INVALID sequences when it is used to convert from
  52. * Unicode -> Codepage. The behavior from Codepage to Unicode is not changed,
  53. * and ucnv_setToUCallBack would need to be called in order to change
  54. * that behavior too.
  55. *
  56. * Here is an example with a context:
  57. *
  58. * \code
  59. * UErrorCode err = U_ZERO_ERROR;
  60. * UConverter *myConverter = ucnv_open("ibm-949", &err);
  61. * const void *oldContext;
  62. * UConverterFromUCallback oldAction;
  63. *
  64. *
  65. * if (U_SUCCESS(err))
  66. * {
  67. * ucnv_setToUCallBack(myConverter,
  68. * UCNV_TO_U_CALLBACK_SUBSTITUTE,
  69. * UCNV_SUB_STOP_ON_ILLEGAL,
  70. * &oldAction,
  71. * &oldContext,
  72. * &status);
  73. * }
  74. * \endcode
  75. *
  76. * The code above tells "myConverter" to stop when it encounters an
  77. * ILLEGAL/TRUNCATED/INVALID sequences when it is used to convert from
  78. * Codepage -> Unicode. Any unmapped and legal characters will be
  79. * substituted to be the default substitution character.
  80. */
  81. #ifndef UCNV_ERR_H
  82. #define UCNV_ERR_H
  83. #include "unicode/utypes.h"
  84. #if !UCONFIG_NO_CONVERSION
  85. /** Forward declaring the UConverter structure. @stable ICU 2.0 */
  86. struct UConverter;
  87. /** @stable ICU 2.0 */
  88. typedef struct UConverter UConverter;
  89. /**
  90. * FROM_U, TO_U context options for sub callback
  91. * @stable ICU 2.0
  92. */
  93. #define UCNV_SUB_STOP_ON_ILLEGAL "i"
  94. /**
  95. * FROM_U, TO_U context options for skip callback
  96. * @stable ICU 2.0
  97. */
  98. #define UCNV_SKIP_STOP_ON_ILLEGAL "i"
  99. /**
  100. * FROM_U_CALLBACK_ESCAPE context option to escape the code unit according to ICU (%UXXXX)
  101. * @stable ICU 2.0
  102. */
  103. #define UCNV_ESCAPE_ICU NULL
  104. /**
  105. * FROM_U_CALLBACK_ESCAPE context option to escape the code unit according to JAVA (\\uXXXX)
  106. * @stable ICU 2.0
  107. */
  108. #define UCNV_ESCAPE_JAVA "J"
  109. /**
  110. * FROM_U_CALLBACK_ESCAPE context option to escape the code unit according to C (\\uXXXX \\UXXXXXXXX)
  111. * TO_U_CALLBACK_ESCAPE option to escape the character value accoding to C (\\xXXXX)
  112. * @stable ICU 2.0
  113. */
  114. #define UCNV_ESCAPE_C "C"
  115. /**
  116. * FROM_U_CALLBACK_ESCAPE context option to escape the code unit according to XML Decimal escape \htmlonly(&amp;#DDDD;)\endhtmlonly
  117. * TO_U_CALLBACK_ESCAPE context option to escape the character value accoding to XML Decimal escape \htmlonly(&amp;#DDDD;)\endhtmlonly
  118. * @stable ICU 2.0
  119. */
  120. #define UCNV_ESCAPE_XML_DEC "D"
  121. /**
  122. * FROM_U_CALLBACK_ESCAPE context option to escape the code unit according to XML Hex escape \htmlonly(&amp;#xXXXX;)\endhtmlonly
  123. * TO_U_CALLBACK_ESCAPE context option to escape the character value accoding to XML Hex escape \htmlonly(&amp;#xXXXX;)\endhtmlonly
  124. * @stable ICU 2.0
  125. */
  126. #define UCNV_ESCAPE_XML_HEX "X"
  127. /**
  128. * FROM_U_CALLBACK_ESCAPE context option to escape the code unit according to Unicode (U+XXXXX)
  129. * @stable ICU 2.0
  130. */
  131. #define UCNV_ESCAPE_UNICODE "U"
  132. /**
  133. * FROM_U_CALLBACK_ESCAPE context option to escape the code unit according to CSS2 conventions (\\HH..H<space>, that is,
  134. * a backslash, 1..6 hex digits, and a space)
  135. * @stable ICU 4.0
  136. */
  137. #define UCNV_ESCAPE_CSS2 "S"
  138. /**
  139. * The process condition code to be used with the callbacks.
  140. * Codes which are greater than UCNV_IRREGULAR should be
  141. * passed on to any chained callbacks.
  142. * @stable ICU 2.0
  143. */
  144. typedef enum {
  145. UCNV_UNASSIGNED = 0, /**< The code point is unassigned.
  146. The error code U_INVALID_CHAR_FOUND will be set. */
  147. UCNV_ILLEGAL = 1, /**< The code point is illegal. For example,
  148. \\x81\\x2E is illegal in SJIS because \\x2E
  149. is not a valid trail byte for the \\x81
  150. lead byte.
  151. Also, starting with Unicode 3.0.1, non-shortest byte sequences
  152. in UTF-8 (like \\xC1\\xA1 instead of \\x61 for U+0061)
  153. are also illegal, not just irregular.
  154. The error code U_ILLEGAL_CHAR_FOUND will be set. */
  155. UCNV_IRREGULAR = 2, /**< The codepoint is not a regular sequence in
  156. the encoding. For example, \\xED\\xA0\\x80..\\xED\\xBF\\xBF
  157. are irregular UTF-8 byte sequences for single surrogate
  158. code points.
  159. The error code U_INVALID_CHAR_FOUND will be set. */
  160. UCNV_RESET = 3, /**< The callback is called with this reason when a
  161. 'reset' has occured. Callback should reset all
  162. state. */
  163. UCNV_CLOSE = 4, /**< Called when the converter is closed. The
  164. callback should release any allocated memory.*/
  165. UCNV_CLONE = 5 /**< Called when ucnv_safeClone() is called on the
  166. converter. the pointer available as the
  167. 'context' is an alias to the original converters'
  168. context pointer. If the context must be owned
  169. by the new converter, the callback must clone
  170. the data and call ucnv_setFromUCallback
  171. (or setToUCallback) with the correct pointer.
  172. @stable ICU 2.2
  173. */
  174. } UConverterCallbackReason;
  175. /**
  176. * The structure for the fromUnicode callback function parameter.
  177. * @stable ICU 2.0
  178. */
  179. typedef struct {
  180. uint16_t size; /**< The size of this struct. @stable ICU 2.0 */
  181. UBool flush; /**< The internal state of converter will be reset and data flushed if set to TRUE. @stable ICU 2.0 */
  182. UConverter *converter; /**< Pointer to the converter that is opened and to which this struct is passed as an argument. @stable ICU 2.0 */
  183. const UChar *source; /**< Pointer to the source source buffer. @stable ICU 2.0 */
  184. const UChar *sourceLimit; /**< Pointer to the limit (end + 1) of source buffer. @stable ICU 2.0 */
  185. char *target; /**< Pointer to the target buffer. @stable ICU 2.0 */
  186. const char *targetLimit; /**< Pointer to the limit (end + 1) of target buffer. @stable ICU 2.0 */
  187. int32_t *offsets; /**< Pointer to the buffer that recieves the offsets. *offset = blah ; offset++;. @stable ICU 2.0 */
  188. } UConverterFromUnicodeArgs;
  189. /**
  190. * The structure for the toUnicode callback function parameter.
  191. * @stable ICU 2.0
  192. */
  193. typedef struct {
  194. uint16_t size; /**< The size of this struct @stable ICU 2.0 */
  195. UBool flush; /**< The internal state of converter will be reset and data flushed if set to TRUE. @stable ICU 2.0 */
  196. UConverter *converter; /**< Pointer to the converter that is opened and to which this struct is passed as an argument. @stable ICU 2.0 */
  197. const char *source; /**< Pointer to the source source buffer. @stable ICU 2.0 */
  198. const char *sourceLimit; /**< Pointer to the limit (end + 1) of source buffer. @stable ICU 2.0 */
  199. UChar *target; /**< Pointer to the target buffer. @stable ICU 2.0 */
  200. const UChar *targetLimit; /**< Pointer to the limit (end + 1) of target buffer. @stable ICU 2.0 */
  201. int32_t *offsets; /**< Pointer to the buffer that recieves the offsets. *offset = blah ; offset++;. @stable ICU 2.0 */
  202. } UConverterToUnicodeArgs;
  203. /**
  204. * DO NOT CALL THIS FUNCTION DIRECTLY!
  205. * This From Unicode callback STOPS at the ILLEGAL_SEQUENCE,
  206. * returning the error code back to the caller immediately.
  207. *
  208. * @param context Pointer to the callback's private data
  209. * @param fromUArgs Information about the conversion in progress
  210. * @param codeUnits Points to 'length' UChars of the concerned Unicode sequence
  211. * @param length Size (in bytes) of the concerned codepage sequence
  212. * @param codePoint Single UChar32 (UTF-32) containing the concerend Unicode codepoint.
  213. * @param reason Defines the reason the callback was invoked
  214. * @param err This should always be set to a failure status prior to calling.
  215. * @stable ICU 2.0
  216. */
  217. U_STABLE void U_EXPORT2 UCNV_FROM_U_CALLBACK_STOP (
  218. const void *context,
  219. UConverterFromUnicodeArgs *fromUArgs,
  220. const UChar* codeUnits,
  221. int32_t length,
  222. UChar32 codePoint,
  223. UConverterCallbackReason reason,
  224. UErrorCode * err);
  225. /**
  226. * DO NOT CALL THIS FUNCTION DIRECTLY!
  227. * This To Unicode callback STOPS at the ILLEGAL_SEQUENCE,
  228. * returning the error code back to the caller immediately.
  229. *
  230. * @param context Pointer to the callback's private data
  231. * @param toUArgs Information about the conversion in progress
  232. * @param codeUnits Points to 'length' bytes of the concerned codepage sequence
  233. * @param length Size (in bytes) of the concerned codepage sequence
  234. * @param reason Defines the reason the callback was invoked
  235. * @param err This should always be set to a failure status prior to calling.
  236. * @stable ICU 2.0
  237. */
  238. U_STABLE void U_EXPORT2 UCNV_TO_U_CALLBACK_STOP (
  239. const void *context,
  240. UConverterToUnicodeArgs *toUArgs,
  241. const char* codeUnits,
  242. int32_t length,
  243. UConverterCallbackReason reason,
  244. UErrorCode * err);
  245. /**
  246. * DO NOT CALL THIS FUNCTION DIRECTLY!
  247. * This From Unicode callback skips any ILLEGAL_SEQUENCE, or
  248. * skips only UNASSINGED_SEQUENCE depending on the context parameter
  249. * simply ignoring those characters.
  250. *
  251. * @param context The function currently recognizes the callback options:
  252. * UCNV_SKIP_STOP_ON_ILLEGAL: STOPS at the ILLEGAL_SEQUENCE,
  253. * returning the error code back to the caller immediately.
  254. * NULL: Skips any ILLEGAL_SEQUENCE
  255. * @param fromUArgs Information about the conversion in progress
  256. * @param codeUnits Points to 'length' UChars of the concerned Unicode sequence
  257. * @param length Size (in bytes) of the concerned codepage sequence
  258. * @param codePoint Single UChar32 (UTF-32) containing the concerend Unicode codepoint.
  259. * @param reason Defines the reason the callback was invoked
  260. * @param err Return value will be set to success if the callback was handled,
  261. * otherwise this value will be set to a failure status.
  262. * @stable ICU 2.0
  263. */
  264. U_STABLE void U_EXPORT2 UCNV_FROM_U_CALLBACK_SKIP (
  265. const void *context,
  266. UConverterFromUnicodeArgs *fromUArgs,
  267. const UChar* codeUnits,
  268. int32_t length,
  269. UChar32 codePoint,
  270. UConverterCallbackReason reason,
  271. UErrorCode * err);
  272. /**
  273. * DO NOT CALL THIS FUNCTION DIRECTLY!
  274. * This From Unicode callback will Substitute the ILLEGAL SEQUENCE, or
  275. * UNASSIGNED_SEQUENCE depending on context parameter, with the
  276. * current substitution string for the converter. This is the default
  277. * callback.
  278. *
  279. * @param context The function currently recognizes the callback options:
  280. * UCNV_SUB_STOP_ON_ILLEGAL: STOPS at the ILLEGAL_SEQUENCE,
  281. * returning the error code back to the caller immediately.
  282. * NULL: Substitutes any ILLEGAL_SEQUENCE
  283. * @param fromUArgs Information about the conversion in progress
  284. * @param codeUnits Points to 'length' UChars of the concerned Unicode sequence
  285. * @param length Size (in bytes) of the concerned codepage sequence
  286. * @param codePoint Single UChar32 (UTF-32) containing the concerend Unicode codepoint.
  287. * @param reason Defines the reason the callback was invoked
  288. * @param err Return value will be set to success if the callback was handled,
  289. * otherwise this value will be set to a failure status.
  290. * @see ucnv_setSubstChars
  291. * @stable ICU 2.0
  292. */
  293. U_STABLE void U_EXPORT2 UCNV_FROM_U_CALLBACK_SUBSTITUTE (
  294. const void *context,
  295. UConverterFromUnicodeArgs *fromUArgs,
  296. const UChar* codeUnits,
  297. int32_t length,
  298. UChar32 codePoint,
  299. UConverterCallbackReason reason,
  300. UErrorCode * err);
  301. /**
  302. * DO NOT CALL THIS FUNCTION DIRECTLY!
  303. * This From Unicode callback will Substitute the ILLEGAL SEQUENCE with the
  304. * hexadecimal representation of the illegal codepoints
  305. *
  306. * @param context The function currently recognizes the callback options:
  307. * <ul>
  308. * <li>UCNV_ESCAPE_ICU: Substitues the ILLEGAL SEQUENCE with the hexadecimal
  309. * representation in the format %UXXXX, e.g. "%uFFFE%u00AC%uC8FE").
  310. * In the Event the converter doesn't support the characters {%,U}[A-F][0-9],
  311. * it will substitute the illegal sequence with the substitution characters.
  312. * Note that codeUnit(32bit int eg: unit of a surrogate pair) is represented as
  313. * %UD84D%UDC56</li>
  314. * <li>UCNV_ESCAPE_JAVA: Substitues the ILLEGAL SEQUENCE with the hexadecimal
  315. * representation in the format \\uXXXX, e.g. "\\uFFFE\\u00AC\\uC8FE").
  316. * In the Event the converter doesn't support the characters {\,u}[A-F][0-9],
  317. * it will substitute the illegal sequence with the substitution characters.
  318. * Note that codeUnit(32bit int eg: unit of a surrogate pair) is represented as
  319. * \\uD84D\\uDC56</li>
  320. * <li>UCNV_ESCAPE_C: Substitues the ILLEGAL SEQUENCE with the hexadecimal
  321. * representation in the format \\uXXXX, e.g. "\\uFFFE\\u00AC\\uC8FE").
  322. * In the Event the converter doesn't support the characters {\,u,U}[A-F][0-9],
  323. * it will substitute the illegal sequence with the substitution characters.
  324. * Note that codeUnit(32bit int eg: unit of a surrogate pair) is represented as
  325. * \\U00023456</li>
  326. * <li>UCNV_ESCAPE_XML_DEC: Substitues the ILLEGAL SEQUENCE with the decimal
  327. * representation in the format \htmlonly&amp;#DDDDDDDD;, e.g. "&amp;#65534;&amp;#172;&amp;#51454;")\endhtmlonly.
  328. * In the Event the converter doesn't support the characters {&amp;,#}[0-9],
  329. * it will substitute the illegal sequence with the substitution characters.
  330. * Note that codeUnit(32bit int eg: unit of a surrogate pair) is represented as
  331. * &amp;#144470; and Zero padding is ignored.</li>
  332. * <li>UCNV_ESCAPE_XML_HEX:Substitues the ILLEGAL SEQUENCE with the decimal
  333. * representation in the format \htmlonly&amp;#xXXXX; e.g. "&amp;#xFFFE;&amp;#x00AC;&amp;#xC8FE;")\endhtmlonly.
  334. * In the Event the converter doesn't support the characters {&,#,x}[0-9],
  335. * it will substitute the illegal sequence with the substitution characters.
  336. * Note that codeUnit(32bit int eg: unit of a surrogate pair) is represented as
  337. * \htmlonly&amp;#x23456;\endhtmlonly</li>
  338. * </ul>
  339. * @param fromUArgs Information about the conversion in progress
  340. * @param codeUnits Points to 'length' UChars of the concerned Unicode sequence
  341. * @param length Size (in bytes) of the concerned codepage sequence
  342. * @param codePoint Single UChar32 (UTF-32) containing the concerend Unicode codepoint.
  343. * @param reason Defines the reason the callback was invoked
  344. * @param err Return value will be set to success if the callback was handled,
  345. * otherwise this value will be set to a failure status.
  346. * @stable ICU 2.0
  347. */
  348. U_STABLE void U_EXPORT2 UCNV_FROM_U_CALLBACK_ESCAPE (
  349. const void *context,
  350. UConverterFromUnicodeArgs *fromUArgs,
  351. const UChar* codeUnits,
  352. int32_t length,
  353. UChar32 codePoint,
  354. UConverterCallbackReason reason,
  355. UErrorCode * err);
  356. /**
  357. * DO NOT CALL THIS FUNCTION DIRECTLY!
  358. * This To Unicode callback skips any ILLEGAL_SEQUENCE, or
  359. * skips only UNASSINGED_SEQUENCE depending on the context parameter
  360. * simply ignoring those characters.
  361. *
  362. * @param context The function currently recognizes the callback options:
  363. * UCNV_SKIP_STOP_ON_ILLEGAL: STOPS at the ILLEGAL_SEQUENCE,
  364. * returning the error code back to the caller immediately.
  365. * NULL: Skips any ILLEGAL_SEQUENCE
  366. * @param toUArgs Information about the conversion in progress
  367. * @param codeUnits Points to 'length' bytes of the concerned codepage sequence
  368. * @param length Size (in bytes) of the concerned codepage sequence
  369. * @param reason Defines the reason the callback was invoked
  370. * @param err Return value will be set to success if the callback was handled,
  371. * otherwise this value will be set to a failure status.
  372. * @stable ICU 2.0
  373. */
  374. U_STABLE void U_EXPORT2 UCNV_TO_U_CALLBACK_SKIP (
  375. const void *context,
  376. UConverterToUnicodeArgs *toUArgs,
  377. const char* codeUnits,
  378. int32_t length,
  379. UConverterCallbackReason reason,
  380. UErrorCode * err);
  381. /**
  382. * DO NOT CALL THIS FUNCTION DIRECTLY!
  383. * This To Unicode callback will Substitute the ILLEGAL SEQUENCE,or
  384. * UNASSIGNED_SEQUENCE depending on context parameter, with the
  385. * Unicode substitution character, U+FFFD.
  386. *
  387. * @param context The function currently recognizes the callback options:
  388. * UCNV_SUB_STOP_ON_ILLEGAL: STOPS at the ILLEGAL_SEQUENCE,
  389. * returning the error code back to the caller immediately.
  390. * NULL: Substitutes any ILLEGAL_SEQUENCE
  391. * @param toUArgs Information about the conversion in progress
  392. * @param codeUnits Points to 'length' bytes of the concerned codepage sequence
  393. * @param length Size (in bytes) of the concerned codepage sequence
  394. * @param reason Defines the reason the callback was invoked
  395. * @param err Return value will be set to success if the callback was handled,
  396. * otherwise this value will be set to a failure status.
  397. * @stable ICU 2.0
  398. */
  399. U_STABLE void U_EXPORT2 UCNV_TO_U_CALLBACK_SUBSTITUTE (
  400. const void *context,
  401. UConverterToUnicodeArgs *toUArgs,
  402. const char* codeUnits,
  403. int32_t length,
  404. UConverterCallbackReason reason,
  405. UErrorCode * err);
  406. /**
  407. * DO NOT CALL THIS FUNCTION DIRECTLY!
  408. * This To Unicode callback will Substitute the ILLEGAL SEQUENCE with the
  409. * hexadecimal representation of the illegal bytes
  410. * (in the format %XNN, e.g. "%XFF%X0A%XC8%X03").
  411. *
  412. * @param context This function currently recognizes the callback options:
  413. * UCNV_ESCAPE_ICU, UCNV_ESCAPE_JAVA, UCNV_ESCAPE_C, UCNV_ESCAPE_XML_DEC,
  414. * UCNV_ESCAPE_XML_HEX and UCNV_ESCAPE_UNICODE.
  415. * @param toUArgs Information about the conversion in progress
  416. * @param codeUnits Points to 'length' bytes of the concerned codepage sequence
  417. * @param length Size (in bytes) of the concerned codepage sequence
  418. * @param reason Defines the reason the callback was invoked
  419. * @param err Return value will be set to success if the callback was handled,
  420. * otherwise this value will be set to a failure status.
  421. * @stable ICU 2.0
  422. */
  423. U_STABLE void U_EXPORT2 UCNV_TO_U_CALLBACK_ESCAPE (
  424. const void *context,
  425. UConverterToUnicodeArgs *toUArgs,
  426. const char* codeUnits,
  427. int32_t length,
  428. UConverterCallbackReason reason,
  429. UErrorCode * err);
  430. #endif
  431. #endif
  432. /*UCNV_ERR_H*/