1 /* Mapping tables for EUC-KR handling.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jungshik Shin <jshin@pantheon.yale.edu>, 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. */
28 /* Direction of the transformation. */
29 static int to_euckr_object;
30 static int from_euckr_object;
34 euckr_from_ucs4(wchar_t ch, unsigned char *cp)
40 if (ucs4_to_ksc5601 (ch, &idx))
43 *cp = (unsigned char) (idx/256);
44 *(cp+1) = (unsigned char) (idx & 0xff) ;
46 /* think about 0x5c ; '\' */
49 *cp = (unsigned char) (0x7f & ch) ;
50 *(cp+1) = (unsigned char) 0;
56 gconv_init (struct gconv_step *step)
58 /* Determine which direction. */
59 if (strcasestr (step->from_name, "EUC-KR") != NULL)
60 step->data = &from_euckr_object;
61 else if (strcasestr (step->to_name, "EUC-KR") != NULL)
62 step->data = &to_euckr_object;
71 gconv_end (struct gconv_step *data)
78 gconv (struct gconv_step *step, struct gconv_step_data *data,
79 const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
81 struct gconv_step *next_step = step + 1;
82 struct gconv_step_data *next_data = data + 1;
83 gconv_fct fct = next_step->fct;
87 /* If the function is called with no input this means we have to reset
88 to the initial state. The possibly partly converted input is
94 /* Call the steps down the chain if there are any. */
99 struct gconv_step *next_step = step + 1;
100 struct gconv_step_data *next_data = data + 1;
102 result = (*fct) (next_step, next_data, NULL, 0, written, 1);
104 /* Clear output buffer. */
105 data->outbufavail = 0;
116 if (step->data == &from_euckr_object)
118 size_t inchars = *inbufsize;
119 size_t outwchars = data->outbufavail;
120 char *outbuf = data->outbuf;
124 && (outwchars + sizeof (wchar_t) <= data->outbufsize))
126 int inchar = (unsigned char) inbuf[cnt];
130 half-width Korean Currency WON sign
134 else if (inchar <= 0x7f)
135 ch = (wchar_t) inchar;
139 ch = (wchar_t) inchar;
142 /* 0xfe(->0x7e : row 94) and 0xc9(->0x59 : row 41) are user-defined areas */
144 else if ( inchar <= 0xa0 || inchar > 0xfe || inchar == 0xc9)
145 /* This is illegal. */
149 /* Two-byte character. First test whether the next
150 character is also available. */
153 if (cnt + 1 >= inchars)
155 /* The second character is not available. Store
156 the intermediate result. */
157 result = GCONV_INCOMPLETE_INPUT;
161 inchar2 = (unsigned char) inbuf[++cnt];
163 ch = ksc5601_to_ucs4 ((uint16_t) (inchar * 256 + inchar2)
165 if (ch == UNKNOWN_10646_CHAR)
172 if (ch == L'\0' && inbuf[cnt] != '\0')
174 /* This is an illegal character. */
175 result = GCONV_ILLEGAL_INPUT;
179 *((wchar_t *) (outbuf + outwchars)) = ch;
181 outwchars += sizeof (wchar_t);
185 data->outbufavail = outwchars;
189 size_t inwchars = *inbufsize;
190 size_t outchars = data->outbufavail;
191 char *outbuf = data->outbuf;
195 while (inwchars >= cnt + sizeof (wchar_t)
196 && outchars < data->outbufsize)
198 wchar_t ch = *((wchar_t *) (inbuf + cnt));
201 /* decomposing Hangul syllables not available in KS C 5601 into Jamos
202 should be considered either here or in euckr_from_ucs4() */
204 euckr_from_ucs4(ch,cp) ;
206 if (cp[0] == '\0' && ch != 0)
207 /* Illegal character. */
210 outbuf[outchars] = cp[0];
211 /* Now test for a possible second byte and write this
215 if (outchars + 1 >= data->outbufsize)
217 /* The result does not fit into the buffer. */
221 outbuf[++outchars] = cp[1];
226 cnt += sizeof (wchar_t);
229 data->outbufavail = outchars;
231 if (outchars + extra < data->outbufsize)
233 /* If there is still room in the output buffer something
234 is wrong with the input. */
235 if (inwchars >= cnt + sizeof (wchar_t))
237 /* An error occurred. */
238 result = GCONV_ILLEGAL_INPUT;
243 /* There are some unprocessed bytes at the end of the
245 result = GCONV_INCOMPLETE_INPUT;
251 if (result != GCONV_OK)
256 /* This is the last step. */
257 result = (*inbufsize > (step->data == &from_euckr_object
258 ? 0 : sizeof (wchar_t) - 1)
259 ? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
264 result = GCONV_EMPTY_INPUT;
266 if (data->outbufavail > 0)
268 /* Call the functions below in the chain. */
269 size_t newavail = data->outbufavail;
271 result = (*fct) (next_step, next_data, data->outbuf, &newavail,
274 /* Correct the output buffer. */
275 if (newavail != data->outbufavail && newavail > 0)
277 memmove (data->outbuf,
278 &data->outbuf[data->outbufavail - newavail],
280 data->outbufavail = newavail;
284 while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
287 if (written != NULL && data->is_last)