blob: ce0ab755303a936d6cd8ce0efa6cfdd48f0d974a [file] [log] [blame]
Daniel Veillard891e4041998-10-19 00:43:02 +00001/*
2 * encoding.h : interface for the encoding conversion functions needed for
3 * XML
4 *
5 * Related specs:
6 * rfc2044 (UTF-8 and UTF-16) F. Yergeau Alis Technologies
7 * [ISO-10646] UTF-8 and UTF-16 in Annexes
8 * [ISO-8859-1] ISO Latin-1 characters codes.
9 * [UNICODE] The Unicode Consortium, "The Unicode Standard --
10 * Worldwide Character Encoding -- Version 1.0", Addison-
11 * Wesley, Volume 1, 1991, Volume 2, 1992. UTF-8 is
12 * described in Unicode Technical Report #4.
13 * [US-ASCII] Coded Character Set--7-bit American Standard Code for
14 * Information Interchange, ANSI X3.4-1986.
15 *
Daniel Veillard891e4041998-10-19 00:43:02 +000016 * See Copyright for the status of this software.
17 *
Daniel Veillard891e4041998-10-19 00:43:02 +000018 * Daniel.Veillard@w3.org
19 */
20
Daniel Veillard27d88741999-05-29 11:51:49 +000021#ifndef __XML_CHAR_ENCODING_H__
22#define __XML_CHAR_ENCODING_H__
Daniel Veillard891e4041998-10-19 00:43:02 +000023
Daniel Veillard361d8452000-04-03 19:48:13 +000024#include <libxml/xmlversion.h>
Daniel Veillard496a1cf2000-05-03 14:20:55 +000025#ifdef LIBXML_ICONV_ENABLED
26#include <iconv.h>
27#endif
28#include <libxml/tree.h>
29
Daniel Veillard891e4041998-10-19 00:43:02 +000030#ifdef __cplusplus
31extern "C" {
32#endif
33
Daniel Veillard14fff061999-06-22 21:49:07 +000034/**
35 * Predefined values for some standard encodings
Daniel Veillard496a1cf2000-05-03 14:20:55 +000036 * Libxml don't do beforehand translation on UTF8, ISOLatinX
37 * It also support UTF16 (LE and BE) by default.
38 *
39 * Anything else would have to be translated to UTF8 before being
40 * given to the parser itself. The BOM for UTF16 and the encoding
41 * declaration are looked at and a converter is looked for at that
42 * point. If not found the parser stops here as asked by the XML REC
43 * Converter can be registered by the user using xmlRegisterCharEncodingHandler
44 * but the currentl form doesn't allow stateful transcoding (a serious
45 * problem agreed !). If iconv has been found it will be used
46 * automatically and allow stateful transcoding, the simplest is then
47 * to be sure to enable icon and to provide iconv libs for the encoding
48 * support needed.
Daniel Veillard14fff061999-06-22 21:49:07 +000049 */
Daniel Veillard27d88741999-05-29 11:51:49 +000050typedef enum {
51 XML_CHAR_ENCODING_ERROR= -1, /* No char encoding detected */
52 XML_CHAR_ENCODING_NONE= 0, /* No char encoding detected */
53 XML_CHAR_ENCODING_UTF8= 1, /* UTF-8 */
54 XML_CHAR_ENCODING_UTF16LE= 2, /* UTF-16 little endian */
55 XML_CHAR_ENCODING_UTF16BE= 3, /* UTF-16 big endian */
56 XML_CHAR_ENCODING_UCS4LE= 4, /* UCS-4 little endian */
57 XML_CHAR_ENCODING_UCS4BE= 5, /* UCS-4 big endian */
58 XML_CHAR_ENCODING_EBCDIC= 6, /* EBCDIC uh! */
59 XML_CHAR_ENCODING_UCS4_2143=7, /* UCS-4 unusual ordering */
60 XML_CHAR_ENCODING_UCS4_3412=8, /* UCS-4 unusual ordering */
61 XML_CHAR_ENCODING_UCS2= 9, /* UCS-2 */
62 XML_CHAR_ENCODING_8859_1= 10,/* ISO-8859-1 ISO Latin 1 */
63 XML_CHAR_ENCODING_8859_2= 11,/* ISO-8859-2 ISO Latin 2 */
64 XML_CHAR_ENCODING_8859_3= 12,/* ISO-8859-3 */
65 XML_CHAR_ENCODING_8859_4= 13,/* ISO-8859-4 */
66 XML_CHAR_ENCODING_8859_5= 14,/* ISO-8859-5 */
67 XML_CHAR_ENCODING_8859_6= 15,/* ISO-8859-6 */
68 XML_CHAR_ENCODING_8859_7= 16,/* ISO-8859-7 */
69 XML_CHAR_ENCODING_8859_8= 17,/* ISO-8859-8 */
70 XML_CHAR_ENCODING_8859_9= 18,/* ISO-8859-9 */
71 XML_CHAR_ENCODING_2022_JP= 19,/* ISO-2022-JP */
72 XML_CHAR_ENCODING_SHIFT_JIS=20,/* Shift_JIS */
Daniel Veillardb96e6431999-08-29 21:02:19 +000073 XML_CHAR_ENCODING_EUC_JP= 21 /* EUC-JP */
Daniel Veillard27d88741999-05-29 11:51:49 +000074} xmlCharEncoding;
75
Daniel Veillard14fff061999-06-22 21:49:07 +000076/**
77 * xmlCharEncodingInputFunc:
78 * @out: a pointer ot an array of bytes to store the UTF-8 result
79 * @outlen: the lenght of @out
80 * @in: a pointer ot an array of chars in the original encoding
81 * @inlen: the lenght of @in
82 *
83 * Take a block of chars in the original encoding and try to convert
84 * it to an UTF-8 block of chars out.
85 *
Daniel Veillard496a1cf2000-05-03 14:20:55 +000086 * Returns the number of byte written, or -1 by lack of space, or -2
87 * if the transcoding failed.
88 * The value of @inlen after return is the number of octets consumed
89 * as the return value is positive, else unpredictiable.
90 * The value of @outlen after return is the number of ocetes consumed.
Daniel Veillard14fff061999-06-22 21:49:07 +000091 */
Daniel Veillard496a1cf2000-05-03 14:20:55 +000092typedef int (* xmlCharEncodingInputFunc)(unsigned char* out, int *outlen,
Daniel Veillardcf461992000-03-14 18:30:20 +000093 const unsigned char* in, int *inlen);
Daniel Veillard14fff061999-06-22 21:49:07 +000094
95
96/**
Daniel Veillardcf461992000-03-14 18:30:20 +000097 * xmlCharEncodingOutputFunc:
Daniel Veillard14fff061999-06-22 21:49:07 +000098 * @out: a pointer ot an array of bytes to store the result
99 * @outlen: the lenght of @out
100 * @in: a pointer ot an array of UTF-8 chars
101 * @inlen: the lenght of @in
102 *
103 * Take a block of UTF-8 chars in and try to convert it to an other
104 * encoding.
Daniel Veillardbe803962000-06-28 23:40:59 +0000105 * Note: a first call designed to produce heading info is called with
106 * in = NULL. If stateful this should also initialize the encoder state
Daniel Veillard14fff061999-06-22 21:49:07 +0000107 *
108 * Returns the number of byte written, or -1 by lack of space, or -2
109 * if the transcoding failed.
Daniel Veillard496a1cf2000-05-03 14:20:55 +0000110 * The value of @inlen after return is the number of octets consumed
111 * as the return value is positive, else unpredictiable.
112 * The value of @outlen after return is the number of ocetes consumed.
Daniel Veillard14fff061999-06-22 21:49:07 +0000113 */
Daniel Veillard496a1cf2000-05-03 14:20:55 +0000114typedef int (* xmlCharEncodingOutputFunc)(unsigned char* out, int *outlen,
Daniel Veillardcf461992000-03-14 18:30:20 +0000115 const unsigned char* in, int *inlen);
Daniel Veillard14fff061999-06-22 21:49:07 +0000116
Daniel Veillard496a1cf2000-05-03 14:20:55 +0000117
Daniel Veillard14fff061999-06-22 21:49:07 +0000118/*
119 * Block defining the handlers for non UTF-8 encodings.
Daniel Veillard496a1cf2000-05-03 14:20:55 +0000120 * If iconv is supported, there is two extra fields
Daniel Veillard14fff061999-06-22 21:49:07 +0000121 */
122
Daniel Veillard71b656e2000-01-05 14:46:17 +0000123typedef struct _xmlCharEncodingHandler xmlCharEncodingHandler;
124typedef xmlCharEncodingHandler *xmlCharEncodingHandlerPtr;
125struct _xmlCharEncodingHandler {
Daniel Veillard14fff061999-06-22 21:49:07 +0000126 char *name;
127 xmlCharEncodingInputFunc input;
Daniel Veillard496a1cf2000-05-03 14:20:55 +0000128 xmlCharEncodingOutputFunc output;
129#ifdef LIBXML_ICONV_ENABLED
130 iconv_t iconv_in;
131 iconv_t iconv_out;
132#endif /* LIBXML_ICONV_ENABLED */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000133};
Daniel Veillard14fff061999-06-22 21:49:07 +0000134
Daniel Veillarda819dac1999-11-24 18:04:22 +0000135void xmlInitCharEncodingHandlers (void);
136void xmlCleanupCharEncodingHandlers (void);
137void xmlRegisterCharEncodingHandler (xmlCharEncodingHandlerPtr handler);
Daniel Veillardcf461992000-03-14 18:30:20 +0000138xmlCharEncoding xmlDetectCharEncoding (const unsigned char* in,
139 int len);
Daniel Veillarda819dac1999-11-24 18:04:22 +0000140xmlCharEncoding xmlParseCharEncoding (const char* name);
Daniel Veillardbe803962000-06-28 23:40:59 +0000141const char* xmlGetCharEncodingName (xmlCharEncoding enc);
Daniel Veillard14fff061999-06-22 21:49:07 +0000142xmlCharEncodingHandlerPtr xmlGetCharEncodingHandler(xmlCharEncoding enc);
143xmlCharEncodingHandlerPtr xmlFindCharEncodingHandler(const char *name);
Daniel Veillardcf461992000-03-14 18:30:20 +0000144int xmlCheckUTF8 (const unsigned char *utf);
Daniel Veillard14fff061999-06-22 21:49:07 +0000145
Daniel Veillard496a1cf2000-05-03 14:20:55 +0000146int xmlCharEncOutFunc (xmlCharEncodingHandler *handler,
147 xmlBufferPtr out,
148 xmlBufferPtr in);
149
150int xmlCharEncInFunc (xmlCharEncodingHandler *handler,
151 xmlBufferPtr out,
152 xmlBufferPtr in);
Daniel Veillardbe803962000-06-28 23:40:59 +0000153int xmlCharEncFirstLine (xmlCharEncodingHandler *handler,
154 xmlBufferPtr out,
155 xmlBufferPtr in);
Daniel Veillard496a1cf2000-05-03 14:20:55 +0000156int xmlCharEncCloseFunc (xmlCharEncodingHandler *handler);
Daniel Veillard27d88741999-05-29 11:51:49 +0000157
Daniel Veillard891e4041998-10-19 00:43:02 +0000158#ifdef __cplusplus
159}
160#endif
161
Daniel Veillard27d88741999-05-29 11:51:49 +0000162#endif /* __XML_CHAR_ENCODING_H__ */