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. */
44 euckr_from_ucs4(wchar_t ch, unsigned char *cp)
50 if (ucs4_to_ksc5601 (ch, &idx))
53 *cp = (unsigned char) (idx/256);
54 *(cp+1) = (unsigned char) (idx & 0xff) ;
56 /* think about 0x5c ; '\' */
59 *cp = (unsigned char) (0x7f & ch) ;
60 *(cp+1) = (unsigned char) 0;
66 gconv_init (struct gconv_step *step, struct gconv_step_data *data)
68 /* Determine which direction. */
69 struct euckr_data *new_data;
73 if (strcasestr (step->from_name, "EUC-KR") != NULL)
75 else if (strcasestr (step->to_name, "EUC-KR") != NULL)
80 result = GCONV_NOCONV;
83 = (struct euckr_data *) malloc (sizeof (struct euckr_data)))
87 data->data = new_data;
96 gconv_end (struct gconv_step_data *data)
103 gconv (struct gconv_step *step, struct gconv_step_data *data,
104 const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
106 struct gconv_step *next_step = step + 1;
107 struct gconv_step_data *next_data = data + 1;
108 gconv_fct fct = next_step->fct;
112 /* If the function is called with no input this means we have to reset
113 to the initial state. The possibly partly converted input is
119 /* Call the steps down the chain if there are any. */
124 struct gconv_step *next_step = step + 1;
125 struct gconv_step_data *next_data = data + 1;
127 result = (*fct) (next_step, next_data, NULL, 0, written, 1);
129 /* Clear output buffer. */
130 data->outbufavail = 0;
135 enum direction dir = ((struct euckr_data *) data->data)->dir;
143 if (dir == from_euckr)
145 size_t inchars = *inbufsize;
146 size_t outwchars = data->outbufavail;
147 char *outbuf = data->outbuf;
151 && (outwchars + sizeof (wchar_t) <= data->outbufsize))
153 int inchar = (unsigned char) inbuf[cnt];
157 half-width Korean Currency WON sign
161 else if (inchar <= 0x7f)
162 ch = (wchar_t) inchar;
166 ch = (wchar_t) inchar;
169 /* 0xfe(->0x7e : row 94) and 0xc9(->0x59 : row 41) are user-defined areas */
171 else if ( inchar <= 0xa0 || inchar >= 0xfe || inchar == 0xc9)
172 /* This is illegal. */
176 /* Two-byte character. First test whether the next
177 character is also available. */
180 if (cnt + 1 >= inchars)
182 /* The second character is not available. Store
183 the intermediate result. */
184 result = GCONV_INCOMPLETE_INPUT;
188 inchar2 = (unsigned char) inbuf[++cnt];
190 ch = ksc5601_to_ucs4 ((uint16_t) (inchar * 256 + inchar2)
192 if (ch == UNKNOWN_10646_CHAR)
199 if (ch == L'\0' && inbuf[cnt] != '\0')
201 /* This is an illegal character. */
202 result = GCONV_ILLEGAL_INPUT;
206 *((wchar_t *) (outbuf + outwchars)) = ch;
208 outwchars += sizeof (wchar_t);
212 data->outbufavail = outwchars;
216 size_t inwchars = *inbufsize;
217 size_t outchars = data->outbufavail;
218 char *outbuf = data->outbuf;
222 while (inwchars >= cnt + sizeof (wchar_t)
223 && outchars < data->outbufsize)
225 wchar_t ch = *((wchar_t *) (inbuf + cnt));
228 /* decomposing Hangul syllables not available in KS C 5601 into Jamos
229 should be considered either here or in euckr_from_ucs4() */
231 euckr_from_ucs4(ch,cp) ;
233 if (cp[0] == '\0' && ch != 0)
234 /* Illegal character. */
237 outbuf[outchars] = cp[0];
238 /* Now test for a possible second byte and write this
242 if (outchars + 1 >= data->outbufsize)
244 /* The result does not fit into the buffer. */
248 outbuf[++outchars] = cp[1];
253 cnt += sizeof (wchar_t);
256 data->outbufavail = outchars;
258 if (outchars + extra < data->outbufsize)
260 /* If there is still room in the output buffer something
261 is wrong with the input. */
262 if (inwchars >= cnt + sizeof (wchar_t))
264 /* An error occurred. */
265 result = GCONV_ILLEGAL_INPUT;
270 /* There are some unprocessed bytes at the end of the
272 result = GCONV_INCOMPLETE_INPUT;
278 if (result != GCONV_OK)
283 /* This is the last step. */
284 result = (*inbufsize > (dir == from_euckr
285 ? 0 : sizeof (wchar_t) - 1)
286 ? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
291 result = GCONV_EMPTY_INPUT;
293 if (data->outbufavail > 0)
295 /* Call the functions below in the chain. */
296 size_t newavail = data->outbufavail;
298 result = (*fct) (next_step, next_data, data->outbuf, &newavail,
301 /* Correct the output buffer. */
302 if (newavail != data->outbufavail && newavail > 0)
304 memmove (data->outbuf,
305 &data->outbuf[data->outbufavail - newavail],
307 data->outbufavail = newavail;
311 while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
314 if (written != NULL && data->is_last)