+++ /dev/null
-/*\r
- * toucs.c - convert charsets to Unicode.\r
- */\r
-\r
-#include "charset.h"\r
-#include "internal.h"\r
-\r
-struct unicode_emit_param {\r
- wchar_t *output;\r
- int outlen;\r
- const wchar_t *errstr;\r
- int errlen;\r
- int stopped;\r
-};\r
-\r
-static void unicode_emit(void *ctx, long int output)\r
-{\r
- struct unicode_emit_param *param = (struct unicode_emit_param *)ctx;\r
- wchar_t outval;\r
- wchar_t const *p;\r
- int outlen;\r
-\r
- if (output == ERROR) {\r
- if (param->errstr) {\r
- p = param->errstr;\r
- outlen = param->errlen;\r
- } else {\r
- outval = 0xFFFD; /* U+FFFD REPLACEMENT CHARACTER */\r
- p = &outval;\r
- outlen = 1;\r
- }\r
- } else {\r
- outval = output;\r
- p = &outval;\r
- outlen = 1;\r
- }\r
-\r
- if (param->outlen >= outlen) {\r
- while (outlen > 0) {\r
- *param->output++ = *p++;\r
- param->outlen--;\r
- outlen--;\r
- }\r
- } else {\r
- param->stopped = 1;\r
- }\r
-}\r
-\r
-int charset_to_unicode(char **input, int *inlen, wchar_t *output, int outlen,\r
- int charset, charset_state *state,\r
- const wchar_t *errstr, int errlen)\r
-{\r
- charset_spec const *spec = charset_find_spec(charset);\r
- charset_state localstate;\r
- struct unicode_emit_param param;\r
-\r
- param.output = output;\r
- param.outlen = outlen;\r
- param.errstr = errstr;\r
- param.errlen = errlen;\r
- param.stopped = 0;\r
-\r
- if (!state) {\r
- localstate.s0 = 0;\r
- } else {\r
- localstate = *state; /* structure copy */\r
- }\r
-\r
- while (*inlen > 0) {\r
- int lenbefore = param.output - output;\r
- spec->read(spec, (unsigned char)**input, &localstate,\r
- unicode_emit, ¶m);\r
- if (param.stopped) {\r
- /*\r
- * The emit function has _tried_ to output some\r
- * characters, but ran up against the end of the\r
- * buffer. Leave immediately, and return what happened\r
- * _before_ attempting to process this character.\r
- */\r
- return lenbefore;\r
- }\r
- if (state)\r
- *state = localstate; /* structure copy */\r
- (*input)++;\r
- (*inlen)--;\r
- }\r
-\r
- return param.output - output;\r
-}\r