2 * toucs.c - convert charsets to Unicode.
8 struct unicode_emit_param
{
12 const wchar_t *errstr
;
17 static void unicode_emit(void *ctx
, long int output
)
19 struct unicode_emit_param
*param
= (struct unicode_emit_param
*)ctx
;
24 if (output
== ERROR
) {
27 outlen
= param
->errlen
;
29 outval
= 0xFFFD; /* U+FFFD REPLACEMENT CHARACTER */
39 if (param
->outlen
< 0 || param
->outlen
>= outlen
) {
42 *param
->output
++ = *p
++;
43 if (param
->outlen
> 0)
53 int charset_to_unicode(const char **input
, int *inlen
,
54 wchar_t *output
, int outlen
,
55 int charset
, charset_state
*state
,
56 const wchar_t *errstr
, int errlen
)
58 charset_spec
const *spec
= charset_find_spec(charset
);
59 charset_state localstate
= CHARSET_INIT_STATE
;
60 struct unicode_emit_param param
;
62 param
.output
= output
;
63 param
.outlen
= outlen
;
64 param
.errstr
= errstr
;
65 param
.errlen
= errlen
;
70 localstate
= *state
; /* structure copy */
73 int lenbefore
= param
.writtenlen
;
74 spec
->read(spec
, (unsigned char)**input
, &localstate
,
75 unicode_emit
, ¶m
);
78 * The emit function has _tried_ to output some
79 * characters, but ran up against the end of the
80 * buffer. Leave immediately, and return what happened
81 * _before_ attempting to process this character.
86 *state
= localstate
; /* structure copy */
91 return param
.writtenlen
;