blob: 7967cc66a965f9381e2c569d153943fb2b7593c8 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardbe586972003-11-18 20:56:51 +00002 * Summary: interface for the encoding conversion functions
3 * Description: interface for the encoding conversion functions needed for
4 * XML basic encoding and iconv() support.
Owen Taylor3473f882001-02-23 17:55:21 +00005 *
Daniel Veillardbe586972003-11-18 20:56:51 +00006 * Related specs are
Owen Taylor3473f882001-02-23 17:55:21 +00007 * 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 *
Daniel Veillardbe586972003-11-18 20:56:51 +000017 * Copy: See Copyright for the status of this software.
Owen Taylor3473f882001-02-23 17:55:21 +000018 *
Daniel Veillardbe586972003-11-18 20:56:51 +000019 * Author: Daniel Veillard
Owen Taylor3473f882001-02-23 17:55:21 +000020 */
21
22#ifndef __XML_CHAR_ENCODING_H__
23#define __XML_CHAR_ENCODING_H__
24
25#include <libxml/xmlversion.h>
Igor Zlatkovic7ae91bc2002-11-08 17:18:52 +000026
Owen Taylor3473f882001-02-23 17:55:21 +000027#ifdef LIBXML_ICONV_ENABLED
28#include <iconv.h>
29#endif
Giuseppe Iuculano48f7dcb2010-11-04 17:42:42 +010030#ifdef LIBXML_ICU_ENABLED
31#include <unicode/ucnv.h>
32#endif
Owen Taylor3473f882001-02-23 17:55:21 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
William M. Brack60f394e2003-11-16 06:25:42 +000037/*
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000038 * xmlCharEncoding:
39 *
Daniel Veillard61f26172002-03-12 18:46:39 +000040 * Predefined values for some standard encodings.
William M. Brackf9415e42003-11-28 09:39:10 +000041 * Libxml does not do beforehand translation on UTF8 and ISOLatinX.
42 * It also supports ASCII, ISO-8859-1, and UTF16 (LE and BE) by default.
Owen Taylor3473f882001-02-23 17:55:21 +000043 *
44 * Anything else would have to be translated to UTF8 before being
45 * given to the parser itself. The BOM for UTF16 and the encoding
46 * declaration are looked at and a converter is looked for at that
William M. Brackf9415e42003-11-28 09:39:10 +000047 * point. If not found the parser stops here as asked by the XML REC. A
48 * converter can be registered by the user using xmlRegisterCharEncodingHandler
Daniel Veillardcbaf3992001-12-31 16:16:02 +000049 * but the current form doesn't allow stateful transcoding (a serious
Owen Taylor3473f882001-02-23 17:55:21 +000050 * problem agreed !). If iconv has been found it will be used
51 * automatically and allow stateful transcoding, the simplest is then
William M. Brackf9415e42003-11-28 09:39:10 +000052 * to be sure to enable iconv and to provide iconv libs for the encoding
Owen Taylor3473f882001-02-23 17:55:21 +000053 * support needed.
William M. Brackf9415e42003-11-28 09:39:10 +000054 *
55 * Note that the generic "UTF-16" is not a predefined value. Instead, only
56 * the specific UTF-16LE and UTF-16BE are present.
Owen Taylor3473f882001-02-23 17:55:21 +000057 */
58typedef enum {
59 XML_CHAR_ENCODING_ERROR= -1, /* No char encoding detected */
60 XML_CHAR_ENCODING_NONE= 0, /* No char encoding detected */
61 XML_CHAR_ENCODING_UTF8= 1, /* UTF-8 */
62 XML_CHAR_ENCODING_UTF16LE= 2, /* UTF-16 little endian */
63 XML_CHAR_ENCODING_UTF16BE= 3, /* UTF-16 big endian */
64 XML_CHAR_ENCODING_UCS4LE= 4, /* UCS-4 little endian */
65 XML_CHAR_ENCODING_UCS4BE= 5, /* UCS-4 big endian */
66 XML_CHAR_ENCODING_EBCDIC= 6, /* EBCDIC uh! */
67 XML_CHAR_ENCODING_UCS4_2143=7, /* UCS-4 unusual ordering */
68 XML_CHAR_ENCODING_UCS4_3412=8, /* UCS-4 unusual ordering */
69 XML_CHAR_ENCODING_UCS2= 9, /* UCS-2 */
70 XML_CHAR_ENCODING_8859_1= 10,/* ISO-8859-1 ISO Latin 1 */
71 XML_CHAR_ENCODING_8859_2= 11,/* ISO-8859-2 ISO Latin 2 */
72 XML_CHAR_ENCODING_8859_3= 12,/* ISO-8859-3 */
73 XML_CHAR_ENCODING_8859_4= 13,/* ISO-8859-4 */
74 XML_CHAR_ENCODING_8859_5= 14,/* ISO-8859-5 */
75 XML_CHAR_ENCODING_8859_6= 15,/* ISO-8859-6 */
76 XML_CHAR_ENCODING_8859_7= 16,/* ISO-8859-7 */
77 XML_CHAR_ENCODING_8859_8= 17,/* ISO-8859-8 */
78 XML_CHAR_ENCODING_8859_9= 18,/* ISO-8859-9 */
79 XML_CHAR_ENCODING_2022_JP= 19,/* ISO-2022-JP */
80 XML_CHAR_ENCODING_SHIFT_JIS=20,/* Shift_JIS */
81 XML_CHAR_ENCODING_EUC_JP= 21,/* EUC-JP */
82 XML_CHAR_ENCODING_ASCII= 22 /* pure ASCII */
83} xmlCharEncoding;
84
85/**
86 * xmlCharEncodingInputFunc:
Daniel Veillardcbaf3992001-12-31 16:16:02 +000087 * @out: a pointer to an array of bytes to store the UTF-8 result
Daniel Veillard60087f32001-10-10 09:45:09 +000088 * @outlen: the length of @out
Daniel Veillardcbaf3992001-12-31 16:16:02 +000089 * @in: a pointer to an array of chars in the original encoding
Daniel Veillard60087f32001-10-10 09:45:09 +000090 * @inlen: the length of @in
Owen Taylor3473f882001-02-23 17:55:21 +000091 *
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 *
William M. Brackf9415e42003-11-28 09:39:10 +000095 * Returns the number of bytes written, -1 if lack of space, or -2
Owen Taylor3473f882001-02-23 17:55:21 +000096 * if the transcoding failed.
97 * The value of @inlen after return is the number of octets consumed
William M. Brackf9415e42003-11-28 09:39:10 +000098 * if the return value is positive, else unpredictiable.
Daniel Veillardcbaf3992001-12-31 16:16:02 +000099 * The value of @outlen after return is the number of octets consumed.
Owen Taylor3473f882001-02-23 17:55:21 +0000100 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000101typedef int (* xmlCharEncodingInputFunc)(unsigned char *out, int *outlen,
102 const unsigned char *in, int *inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000103
104
105/**
106 * xmlCharEncodingOutputFunc:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000107 * @out: a pointer to an array of bytes to store the result
Daniel Veillard60087f32001-10-10 09:45:09 +0000108 * @outlen: the length of @out
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000109 * @in: a pointer to an array of UTF-8 chars
Daniel Veillard60087f32001-10-10 09:45:09 +0000110 * @inlen: the length of @in
Owen Taylor3473f882001-02-23 17:55:21 +0000111 *
William M. Brackf9415e42003-11-28 09:39:10 +0000112 * Take a block of UTF-8 chars in and try to convert it to another
Owen Taylor3473f882001-02-23 17:55:21 +0000113 * encoding.
114 * Note: a first call designed to produce heading info is called with
Daniel Veillard61f26172002-03-12 18:46:39 +0000115 * in = NULL. If stateful this should also initialize the encoder state.
Owen Taylor3473f882001-02-23 17:55:21 +0000116 *
William M. Brackf9415e42003-11-28 09:39:10 +0000117 * Returns the number of bytes written, -1 if lack of space, or -2
Owen Taylor3473f882001-02-23 17:55:21 +0000118 * if the transcoding failed.
119 * The value of @inlen after return is the number of octets consumed
William M. Brackf9415e42003-11-28 09:39:10 +0000120 * if the return value is positive, else unpredictiable.
Daniel Veillard36711902004-02-11 13:25:26 +0000121 * The value of @outlen after return is the number of octets produced.
Owen Taylor3473f882001-02-23 17:55:21 +0000122 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000123typedef int (* xmlCharEncodingOutputFunc)(unsigned char *out, int *outlen,
124 const unsigned char *in, int *inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000125
126
127/*
128 * Block defining the handlers for non UTF-8 encodings.
William M. Brackf9415e42003-11-28 09:39:10 +0000129 * If iconv is supported, there are two extra fields.
Owen Taylor3473f882001-02-23 17:55:21 +0000130 */
Giuseppe Iuculano48f7dcb2010-11-04 17:42:42 +0100131#ifdef LIBXML_ICU_ENABLED
132struct _uconv_t {
133 UConverter *uconv; /* for conversion between an encoding and UTF-16 */
134 UConverter *utf8; /* for conversion between UTF-8 and UTF-16 */
135};
136typedef struct _uconv_t uconv_t;
137#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000138
139typedef struct _xmlCharEncodingHandler xmlCharEncodingHandler;
140typedef xmlCharEncodingHandler *xmlCharEncodingHandlerPtr;
141struct _xmlCharEncodingHandler {
142 char *name;
143 xmlCharEncodingInputFunc input;
144 xmlCharEncodingOutputFunc output;
145#ifdef LIBXML_ICONV_ENABLED
146 iconv_t iconv_in;
147 iconv_t iconv_out;
148#endif /* LIBXML_ICONV_ENABLED */
Giuseppe Iuculano48f7dcb2010-11-04 17:42:42 +0100149#ifdef LIBXML_ICU_ENABLED
150 uconv_t *uconv_in;
151 uconv_t *uconv_out;
152#endif /* LIBXML_ICU_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000153};
154
Daniel Veillard6c4ffaf2002-02-11 08:54:05 +0000155#ifdef __cplusplus
156}
157#endif
158#include <libxml/tree.h>
159#ifdef __cplusplus
160extern "C" {
161#endif
162
Owen Taylor3473f882001-02-23 17:55:21 +0000163/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000164 * Interfaces for encoding handlers.
Owen Taylor3473f882001-02-23 17:55:21 +0000165 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800166XMLPUBFUN void XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000167 xmlInitCharEncodingHandlers (void);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800168XMLPUBFUN void XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000169 xmlCleanupCharEncodingHandlers (void);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800170XMLPUBFUN void XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000171 xmlRegisterCharEncodingHandler (xmlCharEncodingHandlerPtr handler);
172XMLPUBFUN xmlCharEncodingHandlerPtr XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000173 xmlGetCharEncodingHandler (xmlCharEncoding enc);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000174XMLPUBFUN xmlCharEncodingHandlerPtr XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000175 xmlFindCharEncodingHandler (const char *name);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000176XMLPUBFUN xmlCharEncodingHandlerPtr XMLCALL
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800177 xmlNewCharEncodingHandler (const char *name,
178 xmlCharEncodingInputFunc input,
179 xmlCharEncodingOutputFunc output);
Owen Taylor3473f882001-02-23 17:55:21 +0000180
181/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000182 * Interfaces for encoding names and aliases.
Owen Taylor3473f882001-02-23 17:55:21 +0000183 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800184XMLPUBFUN int XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000185 xmlAddEncodingAlias (const char *name,
Owen Taylor3473f882001-02-23 17:55:21 +0000186 const char *alias);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800187XMLPUBFUN int XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000188 xmlDelEncodingAlias (const char *alias);
189XMLPUBFUN const char * XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000190 xmlGetEncodingAlias (const char *alias);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800191XMLPUBFUN void XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000192 xmlCleanupEncodingAliases (void);
193XMLPUBFUN xmlCharEncoding XMLCALL
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000194 xmlParseCharEncoding (const char *name);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000195XMLPUBFUN const char * XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000196 xmlGetCharEncodingName (xmlCharEncoding enc);
197
198/*
199 * Interfaces directly used by the parsers.
200 */
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000201XMLPUBFUN xmlCharEncoding XMLCALL
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000202 xmlDetectCharEncoding (const unsigned char *in,
Owen Taylor3473f882001-02-23 17:55:21 +0000203 int len);
204
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800205XMLPUBFUN int XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000206 xmlCharEncOutFunc (xmlCharEncodingHandler *handler,
Owen Taylor3473f882001-02-23 17:55:21 +0000207 xmlBufferPtr out,
208 xmlBufferPtr in);
209
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800210XMLPUBFUN int XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000211 xmlCharEncInFunc (xmlCharEncodingHandler *handler,
Owen Taylor3473f882001-02-23 17:55:21 +0000212 xmlBufferPtr out,
213 xmlBufferPtr in);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000214XMLPUBFUN int XMLCALL
215 xmlCharEncFirstLine (xmlCharEncodingHandler *handler,
Owen Taylor3473f882001-02-23 17:55:21 +0000216 xmlBufferPtr out,
217 xmlBufferPtr in);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800218XMLPUBFUN int XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000219 xmlCharEncCloseFunc (xmlCharEncodingHandler *handler);
Owen Taylor3473f882001-02-23 17:55:21 +0000220
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000221/*
222 * Export a few useful functions
223 */
William M. Brack21e4ef22005-01-02 09:53:13 +0000224#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800225XMLPUBFUN int XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000226 UTF8Toisolat1 (unsigned char *out,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000227 int *outlen,
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000228 const unsigned char *in,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000229 int *inlen);
William M. Brack21e4ef22005-01-02 09:53:13 +0000230#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800231XMLPUBFUN int XMLCALL
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000232 isolat1ToUTF8 (unsigned char *out,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000233 int *outlen,
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000234 const unsigned char *in,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000235 int *inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000236#ifdef __cplusplus
237}
238#endif
239
240#endif /* __XML_CHAR_ENCODING_H__ */