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. */
29 /* Direction of the transformation. */
30 static int to_eucjp_object;
31 static int from_eucjp_object;
35 gconv_init (struct gconv_step *step)
37 /* Determine which direction. */
38 if (strcasestr (step->from_name, "EUC-JP") != NULL)
39 step->data = &from_eucjp_object;
40 else if (strcasestr (step->to_name, "EUC-JP") != NULL)
41 step->data = &to_eucjp_object;
50 gconv_end (struct gconv_step *data)
57 gconv (struct gconv_step *step, struct gconv_step_data *data,
58 const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
60 struct gconv_step *next_step = step + 1;
61 struct gconv_step_data *next_data = data + 1;
62 gconv_fct fct = next_step->fct;
66 /* If the function is called with no input this means we have to reset
67 to the initial state. The possibly partly converted input is
73 /* Call the steps down the chain if there are any. */
78 struct gconv_step *next_step = step + 1;
79 struct gconv_step_data *next_data = data + 1;
81 result = (*fct) (next_step, next_data, NULL, 0, written, 1);
83 /* Clear output buffer. */
84 data->outbufavail = 0;
95 if (step->data == &from_eucjp_object)
97 size_t inchars = *inbufsize;
98 size_t outwchars = data->outbufavail;
99 char *outbuf = data->outbuf;
103 && (outwchars + sizeof (wchar_t) <= data->outbufsize))
105 int inchar = (unsigned char) inbuf[cnt];
109 ch = (wchar_t) inchar;
110 else if ((inchar <= 0xa0 || inchar > 0xfe)
111 && inchar != 0x8e && inchar != 0x8f)
112 /* This is illegal. */
116 /* Two or more byte character. First test whether the
117 next character is also available. */
120 if (cnt + 1 >= inchars)
122 /* The second character is not available. Store
123 the intermediate result. */
124 result = GCONV_INCOMPLETE_INPUT;
128 inchar2 = (unsigned char) inbuf[++cnt];
130 /* All second bytes of a multibyte character must be
134 /* This is an illegal character. */
136 result = GCONV_ILLEGAL_INPUT;
140 if (inchar == '\x8e')
141 /* This is code set 2: half-width katakana. */
142 ch = jisx0201_to_ucs4 (inchar2);
143 else if (inchar == '\x8f')
145 /* This is code set 3: JIS X 0212-1990. */
146 const char *endp = &inbuf[cnt];
148 ch = jisx0212_to_ucs4 (&endp, 1 + inchars - cnt,
154 /* This is code set 1: JIS X 0208. */
155 const char *endp = &inbuf[cnt - 1];
157 ch = jisx0208_to_ucs4 (&endp, 2 + inchars - cnt,
163 if (ch == UNKNOWN_10646_CHAR)
170 if (ch == L'\0' && inbuf[cnt] != '\0')
172 /* This is an illegal character. */
173 result = GCONV_ILLEGAL_INPUT;
177 *((wchar_t *) (outbuf + outwchars)) = ch;
179 outwchars += sizeof (wchar_t);
183 data->outbufavail = outwchars;
187 size_t inwchars = *inbufsize;
188 size_t outchars = data->outbufavail;
189 char *outbuf = data->outbuf;
193 while (inwchars >= cnt + sizeof (wchar_t)
194 && outchars < data->outbufsize)
196 wchar_t ch = *((wchar_t *) (inbuf + cnt));
199 /* It's plain ASCII. */
200 outbuf[outchars] = ch;
203 /* Try the JIS character sets. */
206 found = ucs4_to_jisx0201 (ch, &outbuf[outchars]);
208 if (found == UNKNOWN_10646_CHAR)
210 /* No JIS 0201 character. */
211 found = ucs4_to_jisx0208 (ch, &outbuf[outchars],
216 /* We ran out of space. */
220 else if (found != UNKNOWN_10646_CHAR)
222 /* It's a JIS 0208 character, adjust it for
224 outbuf[outchars++] += 0x80;
225 outbuf[outchars] += 0x80;
229 /* No JIS 0208 character. */
230 found = ucs4_to_jisx0212 (ch, &outbuf[outchars],
236 /* We ran out of space. */
240 else if (found != UNKNOWN_10646_CHAR)
242 /* It's a JIS 0212 character, adjust it for
244 outbuf[outchars++] += 0x80;
245 outbuf[outchars] += 0x80;
248 /* Illegal character. */
256 cnt += sizeof (wchar_t);
259 data->outbufavail = outchars;
261 if (outchars + extra < data->outbufsize)
263 /* If there is still room in the output buffer something
264 is wrong with the input. */
265 if (inwchars >= cnt + sizeof (wchar_t))
267 /* An error occurred. */
268 result = GCONV_ILLEGAL_INPUT;
273 /* There are some unprocessed bytes at the end of the
275 result = GCONV_INCOMPLETE_INPUT;
281 if (result != GCONV_OK)
286 /* This is the last step. */
287 result = (*inbufsize > (step->data == &from_eucjp_object
288 ? 0 : sizeof (wchar_t) - 1)
289 ? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
294 result = GCONV_EMPTY_INPUT;
296 if (data->outbufavail > 0)
298 /* Call the functions below in the chain. */
299 size_t newavail = data->outbufavail;
301 result = (*fct) (next_step, next_data, data->outbuf, &newavail,
304 /* Correct the output buffer. */
305 if (newavail != data->outbufavail && newavail > 0)
307 memmove (data->outbuf,
308 &data->outbuf[data->outbufavail - newavail],
310 data->outbufavail = newavail;
314 while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
317 if (written != NULL && data->is_last)