1 /* Mapping tables for EUC-JP handling.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 /* Direction of the transformation. */
45 gconv_init (struct gconv_step *step)
47 /* Determine which direction. */
48 struct eucjp_data *new_data;
52 if (strcasestr (step->from_name, "EUC-JP") != NULL)
54 else if (strcasestr (step->to_name, "EUC-JP") != NULL)
59 result = GCONV_NOCONV;
62 = (struct eucjp_data *) malloc (sizeof (struct eucjp_data)))
66 step->data = new_data;
75 gconv_end (struct gconv_step *data)
82 gconv (struct gconv_step *step, struct gconv_step_data *data,
83 const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
85 struct gconv_step *next_step = step + 1;
86 struct gconv_step_data *next_data = data + 1;
87 gconv_fct fct = next_step->fct;
91 /* If the function is called with no input this means we have to reset
92 to the initial state. The possibly partly converted input is
98 /* Call the steps down the chain if there are any. */
103 struct gconv_step *next_step = step + 1;
104 struct gconv_step_data *next_data = data + 1;
106 result = (*fct) (next_step, next_data, NULL, 0, written, 1);
108 /* Clear output buffer. */
109 data->outbufavail = 0;
114 enum direction dir = ((struct eucjp_data *) step->data)->dir;
122 if (dir == from_eucjp)
124 size_t inchars = *inbufsize;
125 size_t outwchars = data->outbufavail;
126 char *outbuf = data->outbuf;
130 && (outwchars + sizeof (wchar_t) <= data->outbufsize))
132 int inchar = (unsigned char) inbuf[cnt];
136 ch = (wchar_t) inchar;
137 else if ((inchar <= 0xa0 || inchar > 0xfe)
138 && inchar != 0x8e && inchar != 0x8f)
139 /* This is illegal. */
143 /* Two or more byte character. First test whether the
144 next character is also available. */
147 if (cnt + 1 >= inchars)
149 /* The second character is not available. Store
150 the intermediate result. */
151 result = GCONV_INCOMPLETE_INPUT;
155 inchar2 = (unsigned char) inbuf[++cnt];
157 /* All second bytes of a multibyte character must be
161 /* This is an illegal character. */
163 result = GCONV_ILLEGAL_INPUT;
167 if (inchar == '\x8e')
168 /* This is code set 2: half-width katakana. */
169 ch = jisx0201_to_ucs4 (inchar2);
170 else if (inchar == '\x8f')
172 /* This is code set 3: JIS X 0212-1990. */
173 const char *endp = &inbuf[cnt];
175 ch = jisx0212_to_ucs4 (&endp, 1 + inchars - cnt,
181 /* This is code set 1: JIS X 0208. */
182 const char *endp = &inbuf[cnt - 1];
184 ch = jisx0208_to_ucs4 (&endp, 2 + inchars - cnt,
190 if (ch == UNKNOWN_10646_CHAR)
197 if (ch == L'\0' && inbuf[cnt] != '\0')
199 /* This is an illegal character. */
200 result = GCONV_ILLEGAL_INPUT;
204 *((wchar_t *) (outbuf + outwchars)) = ch;
206 outwchars += sizeof (wchar_t);
210 data->outbufavail = outwchars;
214 size_t inwchars = *inbufsize;
215 size_t outchars = data->outbufavail;
216 char *outbuf = data->outbuf;
220 while (inwchars >= cnt + sizeof (wchar_t)
221 && outchars < data->outbufsize)
223 wchar_t ch = *((wchar_t *) (inbuf + cnt));
226 /* It's plain ASCII. */
227 outbuf[outchars] = ch;
230 /* Try the JIS character sets. */
233 found = ucs4_to_jisx0201 (ch, &outbuf[outchars]);
235 if (found == UNKNOWN_10646_CHAR)
237 /* No JIS 0201 character. */
238 found = ucs4_to_jisx0208 (ch, &outbuf[outchars],
243 /* We ran out of space. */
247 else if (found != UNKNOWN_10646_CHAR)
249 /* It's a JIS 0208 character, adjust it for
251 outbuf[outchars++] += 0x80;
252 outbuf[outchars] += 0x80;
256 /* No JIS 0208 character. */
257 found = ucs4_to_jisx0212 (ch, &outbuf[outchars],
263 /* We ran out of space. */
267 else if (found != UNKNOWN_10646_CHAR)
269 /* It's a JIS 0212 character, adjust it for
271 outbuf[outchars++] += 0x80;
272 outbuf[outchars] += 0x80;
275 /* Illegal character. */
283 cnt += sizeof (wchar_t);
286 data->outbufavail = outchars;
288 if (outchars + extra < data->outbufsize)
290 /* If there is still room in the output buffer something
291 is wrong with the input. */
292 if (inwchars >= cnt + sizeof (wchar_t))
294 /* An error occurred. */
295 result = GCONV_ILLEGAL_INPUT;
300 /* There are some unprocessed bytes at the end of the
302 result = GCONV_INCOMPLETE_INPUT;
308 if (result != GCONV_OK)
313 /* This is the last step. */
314 result = (*inbufsize > (dir == from_eucjp
315 ? 0 : sizeof (wchar_t) - 1)
316 ? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
321 result = GCONV_EMPTY_INPUT;
323 if (data->outbufavail > 0)
325 /* Call the functions below in the chain. */
326 size_t newavail = data->outbufavail;
328 result = (*fct) (next_step, next_data, data->outbuf, &newavail,
331 /* Correct the output buffer. */
332 if (newavail != data->outbufavail && newavail > 0)
334 memmove (data->outbuf,
335 &data->outbuf[data->outbufavail - newavail],
337 data->outbufavail = newavail;
341 while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
344 if (written != NULL && data->is_last)