encoding.h 8.3 KB

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