2 * toucs.c - convert charsets to Unicode.
\r
6 #include "internal.h"
\r
8 struct unicode_emit_param {
\r
11 const wchar_t *errstr;
\r
16 static void unicode_emit(void *ctx, long int output)
\r
18 struct unicode_emit_param *param = (struct unicode_emit_param *)ctx;
\r
23 if (output == ERROR) {
\r
24 if (param->errstr) {
\r
26 outlen = param->errlen;
\r
28 outval = 0xFFFD; /* U+FFFD REPLACEMENT CHARACTER */
\r
38 if (param->outlen >= outlen) {
\r
39 while (outlen > 0) {
\r
40 *param->output++ = *p++;
\r
49 int charset_to_unicode(char **input, int *inlen, wchar_t *output, int outlen,
\r
50 int charset, charset_state *state,
\r
51 const wchar_t *errstr, int errlen)
\r
53 charset_spec const *spec = charset_find_spec(charset);
\r
54 charset_state localstate;
\r
55 struct unicode_emit_param param;
\r
57 param.output = output;
\r
58 param.outlen = outlen;
\r
59 param.errstr = errstr;
\r
60 param.errlen = errlen;
\r
66 localstate = *state; /* structure copy */
\r
69 while (*inlen > 0) {
\r
70 int lenbefore = param.output - output;
\r
71 spec->read(spec, (unsigned char)**input, &localstate,
\r
72 unicode_emit, ¶m);
\r
73 if (param.stopped) {
\r
75 * The emit function has _tried_ to output some
\r
76 * characters, but ran up against the end of the
\r
77 * buffer. Leave immediately, and return what happened
\r
78 * _before_ attempting to process this character.
\r
83 *state = localstate; /* structure copy */
\r
88 return param.output - output;
\r