Include libcharset into both the Timber and Halibut checkouts.
[sgt/charset] / charset.h
CommitLineData
c6d25d8d 1/*
2 * charset.h - header file for general character set conversion
3 * routines.
4 */
5
6#ifndef charset_charset_h
7#define charset_charset_h
8
9#include <stddef.h>
10
11/*
12 * Enumeration that lists all the multibyte or single-byte
13 * character sets known to this library.
14 */
15typedef enum {
16 CS_NONE, /* used for reporting errors, etc */
17 CS_ASCII, /* ordinary US-ASCII is worth having! */
18 CS_ISO8859_1,
19 CS_ISO8859_1_X11, /* X font encoding with VT100 glyphs */
20 CS_ISO8859_2,
21 CS_ISO8859_3,
22 CS_ISO8859_4,
23 CS_ISO8859_5,
24 CS_ISO8859_6,
25 CS_ISO8859_7,
26 CS_ISO8859_8,
27 CS_ISO8859_9,
28 CS_ISO8859_10,
29 CS_ISO8859_11,
30 CS_ISO8859_13,
31 CS_ISO8859_14,
32 CS_ISO8859_15,
33 CS_ISO8859_16,
34 CS_CP437,
35 CS_CP850,
36 CS_CP1250,
37 CS_CP1251,
38 CS_CP1252,
39 CS_CP1253,
40 CS_CP1254,
41 CS_CP1255,
42 CS_CP1256,
43 CS_CP1257,
44 CS_CP1258,
45 CS_KOI8_R,
46 CS_KOI8_U,
47 CS_KOI8_RU,
48 CS_MAC_ROMAN,
49 CS_MAC_TURKISH,
50 CS_MAC_CROATIAN,
51 CS_MAC_ICELAND,
52 CS_MAC_ROMANIAN,
53 CS_MAC_GREEK,
54 CS_MAC_CYRILLIC,
55 CS_MAC_THAI,
56 CS_MAC_CENTEURO,
57 CS_MAC_SYMBOL,
58 CS_MAC_DINGBATS,
59 CS_MAC_ROMAN_OLD,
60 CS_MAC_CROATIAN_OLD,
61 CS_MAC_ICELAND_OLD,
62 CS_MAC_ROMANIAN_OLD,
63 CS_MAC_GREEK_OLD,
64 CS_MAC_CYRILLIC_OLD,
65 CS_MAC_UKRAINE,
66 CS_MAC_VT100,
67 CS_MAC_VT100_OLD,
68 CS_VISCII,
69 CS_HP_ROMAN8,
70 CS_DEC_MCS,
71 CS_UTF8,
72 CS_UTF7,
73 CS_UTF7_CONSERVATIVE,
74 CS_UTF16,
75 CS_UTF16BE,
76 CS_UTF16LE,
77 CS_EUC_JP,
78 CS_EUC_CN,
79 CS_EUC_KR,
80 CS_ISO2022_JP,
81 CS_ISO2022_KR,
82 CS_BIG5,
83 CS_SHIFT_JIS,
84 CS_HZ,
85 CS_CP949,
86} charset_t;
87
88typedef struct {
89 unsigned long s0, s1;
90} charset_state;
91
92/*
93 * This macro is used to initialise a charset_state structure:
94 *
95 * charset_state mystate = CHARSET_INIT_STATE;
96 */
97#define CHARSET_INIT_STATE { 0L, 0L } /* a suitable initialiser */
98
99/*
100 * This external variable contains the same data, but is provided
101 * for easy structure-copy assignment:
102 *
103 * mystate = charset_init_state;
104 */
105extern const charset_state charset_init_state;
106
107/*
108 * Routine to convert a MB/SB character set to Unicode.
109 *
110 * This routine accepts some number of bytes, updates a state
111 * variable, and outputs some number of Unicode characters. There
112 * are no guarantees. You can't even guarantee that at most one
113 * Unicode character will be output per byte you feed in; for
114 * example, suppose you're reading UTF-8, you've seen E1 80, and
115 * then you suddenly see FE. Now you need to output _two_ error
116 * characters - one for the incomplete sequence E1 80, and one for
117 * the completely invalid UTF-8 byte FE.
118 *
119 * Returns the number of wide characters output; will never output
120 * more than the size of the buffer (as specified on input).
121 * Advances the `input' pointer and decrements `inlen', to indicate
122 * how far along the input string it got.
123 *
124 * The sequence of `errlen' wide characters pointed to by `errstr'
125 * will be used to indicate a conversion error. If `errstr' is
126 * NULL, `errlen' will be ignored, and the library will choose
127 * something sensible to do on its own. For Unicode, this will be
128 * U+FFFD (REPLACEMENT CHARACTER).
129 */
130
131int charset_to_unicode(const char **input, int *inlen,
132 wchar_t *output, int outlen,
133 int charset, charset_state *state,
134 const wchar_t *errstr, int errlen);
135
136/*
137 * Routine to convert Unicode to an MB/SB character set.
138 *
139 * This routine accepts some number of Unicode characters, updates
140 * a state variable, and outputs some number of bytes.
141 *
142 * Returns the number of bytes output; will never output more than
143 * the size of the buffer (as specified on input), and will never
144 * output a partial MB character. Advances the `input' pointer and
145 * decrements `inlen', to indicate how far along the input string
146 * it got.
147 *
148 * If `error' is non-NULL and a character is found which cannot be
149 * expressed in the output charset, conversion will terminate at
150 * that character (so `input' points to the offending character)
151 * and `*error' will be set to TRUE; if `error' is non-NULL and no
152 * difficult characters are encountered, `*error' will be set to
153 * FALSE. If `error' is NULL, difficult characters will simply be
154 * ignored.
155 *
156 * If `input' is NULL, this routine will output the necessary bytes
157 * to reset the encoding state in any way which might be required
158 * at the end of an output piece of text.
159 */
160
161int charset_from_unicode(const wchar_t **input, int *inlen,
162 char *output, int outlen,
163 int charset, charset_state *state, int *error);
164
165/*
166 * Convert X11 encoding names to and from our charset identifiers.
167 */
168const char *charset_to_xenc(int charset);
169int charset_from_xenc(const char *name);
170
171/*
172 * Convert MIME encoding names to and from our charset identifiers.
173 */
174const char *charset_to_mimeenc(int charset);
175int charset_from_mimeenc(const char *name);
176
177/*
178 * Convert our own encoding names to and from our charset
179 * identifiers.
180 */
181const char *charset_to_localenc(int charset);
182int charset_from_localenc(const char *name);
183int charset_localenc_nth(int n);
184
185/*
186 * Convert Mac OS script/region/font to our charset identifiers.
187 */
188int charset_from_macenc(int script, int region, int sysvers,
189 const char *fontname);
190
191/*
192 * Upgrade a charset identifier to a superset charset which is
193 * often confused with it. For example, people whose MUAs report
194 * their mail as ASCII or ISO8859-1 often in practice turn out to
195 * be using CP1252 quote characters, so when parsing incoming mail
196 * it is prudent to treat ASCII and ISO8859-1 as aliases for CP1252
197 * - and since it's a superset of both, this will cause no
198 * genuinely correct mail to be parsed wrongly.
199 */
200int charset_upgrade(int charset);
201
202/*
203 * This function returns TRUE if the input charset is a vaguely
204 * sensible superset of ASCII. That is, it returns FALSE for 7-bit
205 * encoding formats such as HZ and UTF-7.
206 */
207int charset_contains_ascii(int charset);
208
209#endif /* charset_charset_h */