encoding.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Summary: interface for the encoding conversion functions
  3. * Description: interface for the encoding conversion functions needed for
  4. * XML basic encoding and iconv() support.
  5. *
  6. * Related specs are
  7. * rfc2044 (UTF-8 and UTF-16) F. Yergeau Alis Technologies
  8. * [ISO-10646] UTF-8 and UTF-16 in Annexes
  9. * [ISO-8859-1] ISO Latin-1 characters codes.
  10. * [UNICODE] The Unicode Consortium, "The Unicode Standard --
  11. * Worldwide Character Encoding -- Version 1.0", Addison-
  12. * Wesley, Volume 1, 1991, Volume 2, 1992. UTF-8 is
  13. * described in Unicode Technical Report #4.
  14. * [US-ASCII] Coded Character Set--7-bit American Standard Code for
  15. * Information Interchange, ANSI X3.4-1986.
  16. *
  17. * Copy: See Copyright for the status of this software.
  18. *
  19. * Author: Daniel Veillard
  20. */
  21. #ifndef __XML_CHAR_ENCODING_H__
  22. #define __XML_CHAR_ENCODING_H__
  23. #include <libxml/xmlversion.h>
  24. #ifdef LIBXML_ICONV_ENABLED
  25. #include <iconv.h>
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. typedef enum {
  31. XML_ENC_ERR_SUCCESS = 0,
  32. XML_ENC_ERR_SPACE = -1,
  33. XML_ENC_ERR_INPUT = -2,
  34. XML_ENC_ERR_PARTIAL = -3,
  35. XML_ENC_ERR_INTERNAL = -4,
  36. XML_ENC_ERR_MEMORY = -5
  37. } xmlCharEncError;
  38. /*
  39. * xmlCharEncoding:
  40. *
  41. * Predefined values for some standard encodings.
  42. * Libxml does not do beforehand translation on UTF8 and ISOLatinX.
  43. * It also supports ASCII, ISO-8859-1, and UTF16 (LE and BE) by default.
  44. *
  45. * Anything else would have to be translated to UTF8 before being
  46. * given to the parser itself. The BOM for UTF16 and the encoding
  47. * declaration are looked at and a converter is looked for at that
  48. * point. If not found the parser stops here as asked by the XML REC. A
  49. * converter can be registered by the user using xmlRegisterCharEncodingHandler
  50. * but the current form doesn't allow stateful transcoding (a serious
  51. * problem agreed !). If iconv has been found it will be used
  52. * automatically and allow stateful transcoding, the simplest is then
  53. * to be sure to enable iconv and to provide iconv libs for the encoding
  54. * support needed.
  55. *
  56. * Note that the generic "UTF-16" is not a predefined value. Instead, only
  57. * the specific UTF-16LE and UTF-16BE are present.
  58. */
  59. typedef enum {
  60. XML_CHAR_ENCODING_ERROR= -1, /* No char encoding detected */
  61. XML_CHAR_ENCODING_NONE= 0, /* No char encoding detected */
  62. XML_CHAR_ENCODING_UTF8= 1, /* UTF-8 */
  63. XML_CHAR_ENCODING_UTF16LE= 2, /* UTF-16 little endian */
  64. XML_CHAR_ENCODING_UTF16BE= 3, /* UTF-16 big endian */
  65. XML_CHAR_ENCODING_UCS4LE= 4, /* UCS-4 little endian */
  66. XML_CHAR_ENCODING_UCS4BE= 5, /* UCS-4 big endian */
  67. XML_CHAR_ENCODING_EBCDIC= 6, /* EBCDIC uh! */
  68. XML_CHAR_ENCODING_UCS4_2143=7, /* UCS-4 unusual ordering */
  69. XML_CHAR_ENCODING_UCS4_3412=8, /* UCS-4 unusual ordering */
  70. XML_CHAR_ENCODING_UCS2= 9, /* UCS-2 */
  71. XML_CHAR_ENCODING_8859_1= 10,/* ISO-8859-1 ISO Latin 1 */
  72. XML_CHAR_ENCODING_8859_2= 11,/* ISO-8859-2 ISO Latin 2 */
  73. XML_CHAR_ENCODING_8859_3= 12,/* ISO-8859-3 */
  74. XML_CHAR_ENCODING_8859_4= 13,/* ISO-8859-4 */
  75. XML_CHAR_ENCODING_8859_5= 14,/* ISO-8859-5 */
  76. XML_CHAR_ENCODING_8859_6= 15,/* ISO-8859-6 */
  77. XML_CHAR_ENCODING_8859_7= 16,/* ISO-8859-7 */
  78. XML_CHAR_ENCODING_8859_8= 17,/* ISO-8859-8 */
  79. XML_CHAR_ENCODING_8859_9= 18,/* ISO-8859-9 */
  80. XML_CHAR_ENCODING_2022_JP= 19,/* ISO-2022-JP */
  81. XML_CHAR_ENCODING_SHIFT_JIS=20,/* Shift_JIS */
  82. XML_CHAR_ENCODING_EUC_JP= 21,/* EUC-JP */
  83. XML_CHAR_ENCODING_ASCII= 22 /* pure ASCII */
  84. } xmlCharEncoding;
  85. /**
  86. * xmlCharEncodingInputFunc:
  87. * @out: a pointer to an array of bytes to store the UTF-8 result
  88. * @outlen: the length of @out
  89. * @in: a pointer to an array of chars in the original encoding
  90. * @inlen: the length of @in
  91. *
  92. * Take a block of chars in the original encoding and try to convert
  93. * it to an UTF-8 block of chars out.
  94. *
  95. * Returns the number of bytes written, -1 if lack of space, or -2
  96. * if the transcoding failed.
  97. * The value of @inlen after return is the number of octets consumed
  98. * if the return value is positive, else unpredictiable.
  99. * The value of @outlen after return is the number of octets consumed.
  100. */
  101. typedef int (* xmlCharEncodingInputFunc)(unsigned char *out, int *outlen,
  102. const unsigned char *in, int *inlen);
  103. /**
  104. * xmlCharEncodingOutputFunc:
  105. * @out: a pointer to an array of bytes to store the result
  106. * @outlen: the length of @out
  107. * @in: a pointer to an array of UTF-8 chars
  108. * @inlen: the length of @in
  109. *
  110. * Take a block of UTF-8 chars in and try to convert it to another
  111. * encoding.
  112. * Note: a first call designed to produce heading info is called with
  113. * in = NULL. If stateful this should also initialize the encoder state.
  114. *
  115. * Returns the number of bytes written, -1 if lack of space, or -2
  116. * if the transcoding failed.
  117. * The value of @inlen after return is the number of octets consumed
  118. * if the return value is positive, else unpredictiable.
  119. * The value of @outlen after return is the number of octets produced.
  120. */
  121. typedef int (* xmlCharEncodingOutputFunc)(unsigned char *out, int *outlen,
  122. const unsigned char *in, int *inlen);
  123. /*
  124. * Block defining the handlers for non UTF-8 encodings.
  125. * If iconv is supported, there are two extra fields.
  126. */
  127. typedef struct _xmlCharEncodingHandler xmlCharEncodingHandler;
  128. typedef xmlCharEncodingHandler *xmlCharEncodingHandlerPtr;
  129. struct _xmlCharEncodingHandler {
  130. char *name;
  131. xmlCharEncodingInputFunc input;
  132. xmlCharEncodingOutputFunc output;
  133. #ifdef LIBXML_ICONV_ENABLED
  134. iconv_t iconv_in;
  135. iconv_t iconv_out;
  136. #endif /* LIBXML_ICONV_ENABLED */
  137. #ifdef LIBXML_ICU_ENABLED
  138. struct _uconv_t *uconv_in;
  139. struct _uconv_t *uconv_out;
  140. #endif /* LIBXML_ICU_ENABLED */
  141. };
  142. /*
  143. * Interfaces for encoding handlers.
  144. */
  145. XML_DEPRECATED
  146. XMLPUBFUN void
  147. xmlInitCharEncodingHandlers (void);
  148. XML_DEPRECATED
  149. XMLPUBFUN void
  150. xmlCleanupCharEncodingHandlers (void);
  151. XMLPUBFUN void
  152. xmlRegisterCharEncodingHandler (xmlCharEncodingHandlerPtr handler);
  153. XMLPUBFUN int
  154. xmlLookupCharEncodingHandler (xmlCharEncoding enc,
  155. xmlCharEncodingHandlerPtr *out);
  156. XMLPUBFUN int
  157. xmlOpenCharEncodingHandler (const char *name,
  158. int output,
  159. xmlCharEncodingHandlerPtr *out);
  160. XMLPUBFUN xmlCharEncodingHandlerPtr
  161. xmlGetCharEncodingHandler (xmlCharEncoding enc);
  162. XMLPUBFUN xmlCharEncodingHandlerPtr
  163. xmlFindCharEncodingHandler (const char *name);
  164. XMLPUBFUN xmlCharEncodingHandlerPtr
  165. xmlNewCharEncodingHandler (const char *name,
  166. xmlCharEncodingInputFunc input,
  167. xmlCharEncodingOutputFunc output);
  168. /*
  169. * Interfaces for encoding names and aliases.
  170. */
  171. XMLPUBFUN int
  172. xmlAddEncodingAlias (const char *name,
  173. const char *alias);
  174. XMLPUBFUN int
  175. xmlDelEncodingAlias (const char *alias);
  176. XMLPUBFUN const char *
  177. xmlGetEncodingAlias (const char *alias);
  178. XMLPUBFUN void
  179. xmlCleanupEncodingAliases (void);
  180. XMLPUBFUN xmlCharEncoding
  181. xmlParseCharEncoding (const char *name);
  182. XMLPUBFUN const char *
  183. xmlGetCharEncodingName (xmlCharEncoding enc);
  184. /*
  185. * Interfaces directly used by the parsers.
  186. */
  187. XMLPUBFUN xmlCharEncoding
  188. xmlDetectCharEncoding (const unsigned char *in,
  189. int len);
  190. /** DOC_DISABLE */
  191. struct _xmlBuffer;
  192. /** DOC_ENABLE */
  193. XMLPUBFUN int
  194. xmlCharEncOutFunc (xmlCharEncodingHandler *handler,
  195. struct _xmlBuffer *out,
  196. struct _xmlBuffer *in);
  197. XMLPUBFUN int
  198. xmlCharEncInFunc (xmlCharEncodingHandler *handler,
  199. struct _xmlBuffer *out,
  200. struct _xmlBuffer *in);
  201. XML_DEPRECATED
  202. XMLPUBFUN int
  203. xmlCharEncFirstLine (xmlCharEncodingHandler *handler,
  204. struct _xmlBuffer *out,
  205. struct _xmlBuffer *in);
  206. XMLPUBFUN int
  207. xmlCharEncCloseFunc (xmlCharEncodingHandler *handler);
  208. /*
  209. * Export a few useful functions
  210. */
  211. #ifdef LIBXML_OUTPUT_ENABLED
  212. XMLPUBFUN int
  213. UTF8Toisolat1 (unsigned char *out,
  214. int *outlen,
  215. const unsigned char *in,
  216. int *inlen);
  217. #endif /* LIBXML_OUTPUT_ENABLED */
  218. XMLPUBFUN int
  219. isolat1ToUTF8 (unsigned char *out,
  220. int *outlen,
  221. const unsigned char *in,
  222. int *inlen);
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif /* __XML_CHAR_ENCODING_H__ */