2dc6356a |
1 | /* |
2 | * fromucs.c - convert Unicode to other character sets. |
3 | */ |
4 | |
5 | #include "charset.h" |
6 | #include "internal.h" |
7 | |
8 | struct charset_emit_param { |
9 | char *output; |
10 | int outlen; |
11 | const char *errstr; |
12 | int errlen; |
13 | int stopped; |
14 | }; |
15 | |
16 | static void charset_emit(void *ctx, long int output) |
17 | { |
18 | struct charset_emit_param *param = (struct charset_emit_param *)ctx; |
19 | char outval; |
20 | char const *p; |
21 | int outlen; |
22 | |
23 | if (output == ERROR) { |
24 | p = param->errstr; |
25 | outlen = param->errlen; |
26 | } else { |
27 | outval = output; |
28 | p = &outval; |
29 | outlen = 1; |
30 | } |
31 | |
32 | if (param->outlen >= outlen) { |
33 | while (outlen > 0) { |
34 | *param->output++ = *p++; |
35 | param->outlen--; |
36 | outlen--; |
37 | } |
38 | } else { |
39 | param->stopped = 1; |
40 | } |
41 | } |
42 | |
43 | int charset_from_unicode(wchar_t **input, int *inlen, char *output, int outlen, |
44 | int charset, charset_state *state, |
45 | const char *errstr, int errlen) |
46 | { |
47 | charset_spec const *spec = charset_find_spec(charset); |
48 | charset_state localstate; |
49 | struct charset_emit_param param; |
50 | |
51 | param.output = output; |
52 | param.outlen = outlen; |
53 | param.stopped = 0; |
54 | |
55 | /* |
56 | * charset_emit will expect a valid errstr. |
57 | */ |
58 | if (!errstr) { |
59 | /* *shrug* this is good enough, and consistent across all SBCS... */ |
60 | param.errstr = "."; |
61 | param.errlen = 1; |
62 | } |
63 | param.errstr = errstr; |
64 | param.errlen = errlen; |
65 | |
66 | if (!state) { |
67 | localstate.s0 = 0; |
68 | } else { |
69 | localstate = *state; /* structure copy */ |
70 | } |
71 | state = &localstate; |
72 | |
73 | while (*inlen > 0) { |
74 | int lenbefore = param.output - output; |
75 | spec->write(spec, **input, &localstate, charset_emit, ¶m); |
76 | if (param.stopped) { |
77 | /* |
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. |
82 | */ |
83 | return lenbefore; |
84 | } |
85 | if (state) |
86 | *state = localstate; /* structure copy */ |
87 | (*input)++; |
88 | (*inlen)--; |
89 | } |
90 | return param.output - output; |
91 | } |