2 * charset.h - header file for general character set conversion
6 #ifndef charset_charset_h
7 #define charset_charset_h
12 * Enumeration that lists all the multibyte or single-byte
13 * character sets known to this library.
16 CS_NONE
, /* used for reporting errors, etc */
17 CS_ASCII
, /* ordinary US-ASCII is worth having! */
19 CS_ISO8859_1_X11
, /* X font encoding with VT100 glyphs */
102 * This macro is used to initialise a charset_state structure:
104 * charset_state mystate = CHARSET_INIT_STATE;
106 #define CHARSET_INIT_STATE { 0L, 0L } /* a suitable initialiser */
109 * This external variable contains the same data, but is provided
110 * for easy structure-copy assignment:
112 * mystate = charset_init_state;
114 extern const charset_state charset_init_state
;
117 * Routine to convert a MB/SB character set to Unicode.
119 * This routine accepts some number of bytes, updates a state
120 * variable, and outputs some number of Unicode characters. There
121 * are no guarantees. You can't even guarantee that at most one
122 * Unicode character will be output per byte you feed in; for
123 * example, suppose you're reading UTF-8, you've seen E1 80, and
124 * then you suddenly see FE. Now you need to output _two_ error
125 * characters - one for the incomplete sequence E1 80, and one for
126 * the completely invalid UTF-8 byte FE.
128 * Returns the number of wide characters output; will never output
129 * more than the size of the buffer (as specified on input).
130 * Advances the `input' pointer and decrements `inlen', to indicate
131 * how far along the input string it got.
133 * The sequence of `errlen' wide characters pointed to by `errstr'
134 * will be used to indicate a conversion error. If `errstr' is
135 * NULL, `errlen' will be ignored, and the library will choose
136 * something sensible to do on its own. For Unicode, this will be
137 * U+FFFD (REPLACEMENT CHARACTER).
139 * `output' may be NULL, in which case the entire translation will
140 * be performed in theory (e.g. a dry run to work out how much
141 * space needs to be allocated for the real thing). `outlen' may
142 * also be negative, indicating an unlimited buffer length
143 * (although this is almost certainly unwise if `output' is _not_
147 int charset_to_unicode(const char **input
, int *inlen
,
148 wchar_t *output
, int outlen
,
149 int charset
, charset_state
*state
,
150 const wchar_t *errstr
, int errlen
);
153 * Routine to convert Unicode to an MB/SB character set.
155 * This routine accepts some number of Unicode characters, updates
156 * a state variable, and outputs some number of bytes.
158 * Returns the number of bytes output; will never output more than
159 * the size of the buffer (as specified on input), and will never
160 * output a partial MB character. Advances the `input' pointer and
161 * decrements `inlen', to indicate how far along the input string
164 * If `error' is non-NULL and a character is found which cannot be
165 * expressed in the output charset, conversion will terminate at
166 * that character (so `input' points to the offending character)
167 * and `*error' will be set to TRUE; if `error' is non-NULL and no
168 * difficult characters are encountered, `*error' will be set to
169 * FALSE. If `error' is NULL, difficult characters will simply be
172 * If `input' is NULL, this routine will output the necessary bytes
173 * to reset the encoding state in any way which might be required
174 * at the end of an output piece of text.
176 * `output' may be NULL, in which case the entire translation will
177 * be performed in theory (e.g. a dry run to work out how much
178 * space needs to be allocated for the real thing). `outlen' may
179 * also be negative, indicating an unlimited buffer length
180 * (although this is almost certainly unwise if `output' is _not_
184 int charset_from_unicode(const wchar_t **input
, int *inlen
,
185 char *output
, int outlen
,
186 int charset
, charset_state
*state
, int *error
);
189 * Convert X11 encoding names to and from our charset identifiers.
191 const char *charset_to_xenc(int charset
);
192 int charset_from_xenc(const char *name
);
195 * Convert MIME encoding names to and from our charset identifiers.
197 const char *charset_to_mimeenc(int charset
);
198 int charset_from_mimeenc(const char *name
);
201 * Convert our own encoding names to and from our charset
204 const char *charset_to_localenc(int charset
);
205 int charset_from_localenc(const char *name
);
206 int charset_localenc_nth(int n
);
209 * Convert Mac OS script/region/font to our charset identifiers.
211 int charset_from_macenc(int script
, int region
, int sysvers
,
212 const char *fontname
);
215 * Convert GNU Emacs coding system symbol to and from our charset
218 const char *charset_to_emacsenc(int charset
);
219 int charset_from_emacsenc(const char *name
);
222 * Upgrade a charset identifier to a superset charset which is
223 * often confused with it. For example, people whose MUAs report
224 * their mail as ASCII or ISO8859-1 often in practice turn out to
225 * be using CP1252 quote characters, so when parsing incoming mail
226 * it is prudent to treat ASCII and ISO8859-1 as aliases for CP1252
227 * - and since it's a superset of both, this will cause no
228 * genuinely correct mail to be parsed wrongly.
230 int charset_upgrade(int charset
);
233 * This function returns TRUE if the input charset is a vaguely
234 * sensible superset of ASCII. That is, it returns FALSE for 7-bit
235 * encoding formats such as HZ and UTF-7.
237 int charset_contains_ascii(int charset
);
240 * This function tries to deduce the CS_* identifier of the charset
241 * used in the current C locale. It falls back to CS_ASCII if it
242 * can't figure it out at all, so it will always return a valid
245 * (Note that you should have already called setlocale(LC_CTYPE,
246 * "") to guarantee that this function will do the right thing.)
248 int charset_from_locale(void);
250 #endif /* charset_charset_h */