1 /* Generic conversion to and from 8bit charsets,
2 converting from UCS using gaps.
3 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
34 /* Now we can include the tables. */
37 /* We use three objects to describe the operation mode. */
38 static int from_8bit_object;
39 static int to_8bit_object;
43 gconv_init (struct gconv_step *step)
45 /* Determine which direction. */
46 if (strcasestr (step->from_name, NAME) != NULL)
47 step->data = &from_8bit_object;
48 else if (strcasestr (step->to_name, NAME) != NULL)
49 step->data = &to_8bit_object;
58 gconv_end (struct gconv_step *data)
65 gconv (struct gconv_step *step, struct gconv_step_data *data,
66 const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
68 struct gconv_step *next_step = step + 1;
69 struct gconv_step_data *next_data = data + 1;
70 gconv_fct fct = next_step->fct;
74 /* If the function is called with no input this means we have to reset
75 to the initial state. The possibly partly converted input is
81 /* Call the steps down the chain if there are any. */
86 struct gconv_step *next_step = step + 1;
87 struct gconv_step_data *next_data = data + 1;
89 result = (*fct) (next_step, next_data, NULL, 0, written, 1);
91 /* Clear output buffer. */
92 data->outbufavail = 0;
103 if (step->data == &from_8bit_object)
105 size_t inchars = *inbufsize;
106 size_t outwchars = data->outbufavail;
107 char *outbuf = data->outbuf;
111 && (outwchars + sizeof (wchar_t) <= data->outbufsize))
113 wchar_t ch = to_ucs4[(unsigned int) inbuf[cnt]];
115 if (ch == L'\0' && inbuf[cnt] != '\0')
117 /* This is an illegal character. */
118 result = GCONV_ILLEGAL_INPUT;
122 *((wchar_t *) (outbuf + outwchars)) = ch;
124 outwchars += sizeof (wchar_t);
128 data->outbufavail = outwchars;
132 size_t inwchars = *inbufsize;
133 size_t outchars = data->outbufavail;
134 char *outbuf = data->outbuf;
137 while (inwchars >= cnt + sizeof (wchar_t)
138 && outchars < data->outbufsize)
140 const struct gap *rp = from_idx;
141 unsigned int ch = *((wchar_t *) (inbuf + cnt));
147 /* No valid character. */
150 res = from_ucs4[ch + rp->idx];
151 if (res == '\0' && ch != 0)
152 /* No valid character. */
155 outbuf[outchars] = res;
158 cnt += sizeof (wchar_t);
161 data->outbufavail = outchars;
163 if (outchars < data->outbufsize)
165 /* If there is still room in the output buffer something
166 is wrong with the input. */
167 if (inwchars >= cnt + sizeof (wchar_t))
169 /* An error occurred. */
170 result = GCONV_ILLEGAL_INPUT;
175 /* There are some unprocessed bytes at the end of the
177 result = GCONV_INCOMPLETE_INPUT;
183 if (result != GCONV_OK)
188 /* This is the last step. */
189 result = (*inbufsize > (step->data == &from_8bit_object
190 ? 0 : sizeof (wchar_t) - 1)
191 ? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
196 result = GCONV_EMPTY_INPUT;
198 if (data->outbufavail > 0)
200 /* Call the functions below in the chain. */
201 size_t newavail = data->outbufavail;
203 result = (*fct) (next_step, next_data, data->outbuf, &newavail,
206 /* Correct the output buffer. */
207 if (newavail != data->outbufavail && newavail > 0)
209 memmove (data->outbuf,
210 &data->outbuf[data->outbufavail - newavail],
212 data->outbufavail = newavail;
216 while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
219 if (written != NULL && data->is_last)