blob: c74b25f3cbb6756d662e4ba278d1b94e851e6370 [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
Owen Taylor3473f882001-02-23 17:55:21 +000030#ifdef __cplusplus
31extern "C" {
32#endif
33
William M. Brack60f394e2003-11-16 06:25:42 +000034/*
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000035 * xmlCharEncoding:
36 *
Daniel Veillard61f26172002-03-12 18:46:39 +000037 * Predefined values for some standard encodings.
William M. Brackf9415e42003-11-28 09:39:10 +000038 * Libxml does not do beforehand translation on UTF8 and ISOLatinX.
39 * It also supports ASCII, ISO-8859-1, and UTF16 (LE and BE) by default.
Owen Taylor3473f882001-02-23 17:55:21 +000040 *
41 * Anything else would have to be translated to UTF8 before being
42 * given to the parser itself. The BOM for UTF16 and the encoding
43 * declaration are looked at and a converter is looked for at that
William M. Brackf9415e42003-11-28 09:39:10 +000044 * point. If not found the parser stops here as asked by the XML REC. A
45 * converter can be registered by the user using xmlRegisterCharEncodingHandler
Daniel Veillardcbaf3992001-12-31 16:16:02 +000046 * but the current form doesn't allow stateful transcoding (a serious
Owen Taylor3473f882001-02-23 17:55:21 +000047 * problem agreed !). If iconv has been found it will be used
48 * automatically and allow stateful transcoding, the simplest is then
William M. Brackf9415e42003-11-28 09:39:10 +000049 * to be sure to enable iconv and to provide iconv libs for the encoding
Owen Taylor3473f882001-02-23 17:55:21 +000050 * support needed.
William M. Brackf9415e42003-11-28 09:39:10 +000051 *
52 * Note that the generic "UTF-16" is not a predefined value. Instead, only
53 * the specific UTF-16LE and UTF-16BE are present.
Owen Taylor3473f882001-02-23 17:55:21 +000054 */
55typedef enum {
56 XML_CHAR_ENCODING_ERROR= -1, /* No char encoding detected */
57 XML_CHAR_ENCODING_NONE= 0, /* No char encoding detected */
58 XML_CHAR_ENCODING_UTF8= 1, /* UTF-8 */
59 XML_CHAR_ENCODING_UTF16LE= 2, /* UTF-16 little endian */
60 XML_CHAR_ENCODING_UTF16BE= 3, /* UTF-16 big endian */
61 XML_CHAR_ENCODING_UCS4LE= 4, /* UCS-4 little endian */
62 XML_CHAR_ENCODING_UCS4BE= 5, /* UCS-4 big endian */
63 XML_CHAR_ENCODING_EBCDIC= 6, /* EBCDIC uh! */
64 XML_CHAR_ENCODING_UCS4_2143=7, /* UCS-4 unusual ordering */
65 XML_CHAR_ENCODING_UCS4_3412=8, /* UCS-4 unusual ordering */
66 XML_CHAR_ENCODING_UCS2= 9, /* UCS-2 */
67 XML_CHAR_ENCODING_8859_1= 10,/* ISO-8859-1 ISO Latin 1 */
68 XML_CHAR_ENCODING_8859_2= 11,/* ISO-8859-2 ISO Latin 2 */
69 XML_CHAR_ENCODING_8859_3= 12,/* ISO-8859-3 */
70 XML_CHAR_ENCODING_8859_4= 13,/* ISO-8859-4 */
71 XML_CHAR_ENCODING_8859_5= 14,/* ISO-8859-5 */
72 XML_CHAR_ENCODING_8859_6= 15,/* ISO-8859-6 */
73 XML_CHAR_ENCODING_8859_7= 16,/* ISO-8859-7 */
74 XML_CHAR_ENCODING_8859_8= 17,/* ISO-8859-8 */
75 XML_CHAR_ENCODING_8859_9= 18,/* ISO-8859-9 */
76 XML_CHAR_ENCODING_2022_JP= 19,/* ISO-2022-JP */
77 XML_CHAR_ENCODING_SHIFT_JIS=20,/* Shift_JIS */
78 XML_CHAR_ENCODING_EUC_JP= 21,/* EUC-JP */
79 XML_CHAR_ENCODING_ASCII= 22 /* pure ASCII */
80} xmlCharEncoding;
81
82/**
83 * xmlCharEncodingInputFunc:
Daniel Veillardcbaf3992001-12-31 16:16:02 +000084 * @out: a pointer to an array of bytes to store the UTF-8 result
Daniel Veillard60087f32001-10-10 09:45:09 +000085 * @outlen: the length of @out
Daniel Veillardcbaf3992001-12-31 16:16:02 +000086 * @in: a pointer to an array of chars in the original encoding
Daniel Veillard60087f32001-10-10 09:45:09 +000087 * @inlen: the length of @in
Owen Taylor3473f882001-02-23 17:55:21 +000088 *
89 * Take a block of chars in the original encoding and try to convert
90 * it to an UTF-8 block of chars out.
91 *
William M. Brackf9415e42003-11-28 09:39:10 +000092 * Returns the number of bytes written, -1 if lack of space, or -2
Owen Taylor3473f882001-02-23 17:55:21 +000093 * if the transcoding failed.
94 * The value of @inlen after return is the number of octets consumed
William M. Brackf9415e42003-11-28 09:39:10 +000095 * if the return value is positive, else unpredictiable.
Daniel Veillardcbaf3992001-12-31 16:16:02 +000096 * The value of @outlen after return is the number of octets consumed.
Owen Taylor3473f882001-02-23 17:55:21 +000097 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +000098typedef int (* xmlCharEncodingInputFunc)(unsigned char *out, int *outlen,
99 const unsigned char *in, int *inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000100
101
102/**
103 * xmlCharEncodingOutputFunc:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000104 * @out: a pointer to an array of bytes to store the result
Daniel Veillard60087f32001-10-10 09:45:09 +0000105 * @outlen: the length of @out
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000106 * @in: a pointer to an array of UTF-8 chars
Daniel Veillard60087f32001-10-10 09:45:09 +0000107 * @inlen: the length of @in
Owen Taylor3473f882001-02-23 17:55:21 +0000108 *
William M. Brackf9415e42003-11-28 09:39:10 +0000109 * Take a block of UTF-8 chars in and try to convert it to another
Owen Taylor3473f882001-02-23 17:55:21 +0000110 * encoding.
111 * Note: a first call designed to produce heading info is called with
Daniel Veillard61f26172002-03-12 18:46:39 +0000112 * in = NULL. If stateful this should also initialize the encoder state.
Owen Taylor3473f882001-02-23 17:55:21 +0000113 *
William M. Brackf9415e42003-11-28 09:39:10 +0000114 * Returns the number of bytes written, -1 if lack of space, or -2
Owen Taylor3473f882001-02-23 17:55:21 +0000115 * if the transcoding failed.
116 * The value of @inlen after return is the number of octets consumed
William M. Brackf9415e42003-11-28 09:39:10 +0000117 * if the return value is positive, else unpredictiable.
Daniel Veillard36711902004-02-11 13:25:26 +0000118 * The value of @outlen after return is the number of octets produced.
Owen Taylor3473f882001-02-23 17:55:21 +0000119 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000120typedef int (* xmlCharEncodingOutputFunc)(unsigned char *out, int *outlen,
121 const unsigned char *in, int *inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000122
123
124/*
125 * Block defining the handlers for non UTF-8 encodings.
William M. Brackf9415e42003-11-28 09:39:10 +0000126 * If iconv is supported, there are two extra fields.
Owen Taylor3473f882001-02-23 17:55:21 +0000127 */
128
129typedef struct _xmlCharEncodingHandler xmlCharEncodingHandler;
130typedef xmlCharEncodingHandler *xmlCharEncodingHandlerPtr;
131struct _xmlCharEncodingHandler {
132 char *name;
133 xmlCharEncodingInputFunc input;
134 xmlCharEncodingOutputFunc output;
135#ifdef LIBXML_ICONV_ENABLED
136 iconv_t iconv_in;
137 iconv_t iconv_out;
138#endif /* LIBXML_ICONV_ENABLED */
139};
140
Daniel Veillard6c4ffaf2002-02-11 08:54:05 +0000141#ifdef __cplusplus
142}
143#endif
144#include <libxml/tree.h>
145#ifdef __cplusplus
146extern "C" {
147#endif
148
Owen Taylor3473f882001-02-23 17:55:21 +0000149/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000150 * Interfaces for encoding handlers.
Owen Taylor3473f882001-02-23 17:55:21 +0000151 */
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000152XMLPUBFUN void XMLCALL
153 xmlInitCharEncodingHandlers (void);
154XMLPUBFUN void XMLCALL
155 xmlCleanupCharEncodingHandlers (void);
156XMLPUBFUN void XMLCALL
157 xmlRegisterCharEncodingHandler (xmlCharEncodingHandlerPtr handler);
158XMLPUBFUN xmlCharEncodingHandlerPtr XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000159 xmlGetCharEncodingHandler (xmlCharEncoding enc);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000160XMLPUBFUN xmlCharEncodingHandlerPtr XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000161 xmlFindCharEncodingHandler (const char *name);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000162XMLPUBFUN xmlCharEncodingHandlerPtr XMLCALL
Daniel Veillard6f46f6c2002-08-01 12:22:24 +0000163 xmlNewCharEncodingHandler (const char *name,
164 xmlCharEncodingInputFunc input,
165 xmlCharEncodingOutputFunc output);
Owen Taylor3473f882001-02-23 17:55:21 +0000166
167/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000168 * Interfaces for encoding names and aliases.
Owen Taylor3473f882001-02-23 17:55:21 +0000169 */
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000170XMLPUBFUN int XMLCALL
171 xmlAddEncodingAlias (const char *name,
Owen Taylor3473f882001-02-23 17:55:21 +0000172 const char *alias);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000173XMLPUBFUN int XMLCALL
174 xmlDelEncodingAlias (const char *alias);
175XMLPUBFUN const char * XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000176 xmlGetEncodingAlias (const char *alias);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000177XMLPUBFUN void XMLCALL
178 xmlCleanupEncodingAliases (void);
179XMLPUBFUN xmlCharEncoding XMLCALL
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000180 xmlParseCharEncoding (const char *name);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000181XMLPUBFUN const char * XMLCALL
Owen Taylor3473f882001-02-23 17:55:21 +0000182 xmlGetCharEncodingName (xmlCharEncoding enc);
183
184/*
185 * Interfaces directly used by the parsers.
186 */
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000187XMLPUBFUN xmlCharEncoding XMLCALL
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000188 xmlDetectCharEncoding (const unsigned char *in,
Owen Taylor3473f882001-02-23 17:55:21 +0000189 int len);
190
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000191XMLPUBFUN int XMLCALL
192 xmlCharEncOutFunc (xmlCharEncodingHandler *handler,
Owen Taylor3473f882001-02-23 17:55:21 +0000193 xmlBufferPtr out,
194 xmlBufferPtr in);
195
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000196XMLPUBFUN int XMLCALL
197 xmlCharEncInFunc (xmlCharEncodingHandler *handler,
Owen Taylor3473f882001-02-23 17:55:21 +0000198 xmlBufferPtr out,
199 xmlBufferPtr in);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000200XMLPUBFUN int XMLCALL
201 xmlCharEncFirstLine (xmlCharEncodingHandler *handler,
Owen Taylor3473f882001-02-23 17:55:21 +0000202 xmlBufferPtr out,
203 xmlBufferPtr in);
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000204XMLPUBFUN int XMLCALL
205 xmlCharEncCloseFunc (xmlCharEncodingHandler *handler);
Owen Taylor3473f882001-02-23 17:55:21 +0000206
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000207/*
208 * Export a few useful functions
209 */
William M. Brack21e4ef22005-01-02 09:53:13 +0000210#ifdef LIBXML_OUTPUT_ENABLED
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000211XMLPUBFUN int XMLCALL
212 UTF8Toisolat1 (unsigned char *out,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000213 int *outlen,
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000214 const unsigned char *in,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000215 int *inlen);
William M. Brack21e4ef22005-01-02 09:53:13 +0000216#endif /* LIBXML_OUTPUT_ENABLED */
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000217XMLPUBFUN int XMLCALL
218 isolat1ToUTF8 (unsigned char *out,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000219 int *outlen,
Daniel Veillard963d2ae2002-01-20 22:08:18 +0000220 const unsigned char *in,
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000221 int *inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000222#ifdef __cplusplus
223}
224#endif
225
226#endif /* __XML_CHAR_ENCODING_H__ */