1 /* Generic conversion to and from 8bit charsets.
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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. */
25 /* Direction of the transformation. */
40 gconv_init (struct gconv_step *step)
42 /* Determine which direction. */
43 struct s_8bit_data *new_data;
47 if (strcasestr (step->from_name, NAME) != NULL)
49 else if (strcasestr (step->to_name, NAME) != NULL)
54 result = GCONV_NOCONV;
57 = (struct s_8bit_data *) malloc (sizeof (struct s_8bit_data)))
61 step->data = new_data;
70 gconv_end (struct gconv_step *data)
77 gconv (struct gconv_step *step, struct gconv_step_data *data,
78 const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
80 struct gconv_step *next_step = step + 1;
81 struct gconv_step_data *next_data = data + 1;
82 gconv_fct fct = next_step->fct;
86 /* If the function is called with no input this means we have to reset
87 to the initial state. The possibly partly converted input is
93 /* Call the steps down the chain if there are any. */
98 struct gconv_step *next_step = step + 1;
99 struct gconv_step_data *next_data = data + 1;
101 result = (*fct) (next_step, next_data, NULL, 0, written, 1);
103 /* Clear output buffer. */
104 data->outbufavail = 0;
109 enum direction dir = ((struct s_8bit_data *) step->data)->dir;
117 if (dir == from_8bit)
119 size_t inchars = *inbufsize;
120 size_t outwchars = data->outbufavail;
121 char *outbuf = data->outbuf;
125 && (outwchars + sizeof (wchar_t) <= data->outbufsize))
127 wchar_t ch = to_ucs4[(unsigned int) inbuf[cnt]];
129 if (ch == L'\0' && inbuf[cnt] != '\0')
131 /* This is an illegal character. */
132 result = GCONV_ILLEGAL_INPUT;
136 *((wchar_t *) (outbuf + outwchars)) = ch;
138 outwchars += sizeof (wchar_t);
142 data->outbufavail = outwchars;
146 size_t inwchars = *inbufsize;
147 size_t outchars = data->outbufavail;
148 char *outbuf = data->outbuf;
151 while (inwchars >= cnt + sizeof (wchar_t)
152 && outchars < data->outbufsize)
154 int ch = *((wchar_t *) (inbuf + cnt));
156 if (ch >= sizeof (from_ucs4) / sizeof (from_ucs4[0])
157 || ch < 0 || (from_ucs4[ch] == '\0' && ch != 0))
160 outbuf[outchars] = from_ucs4[ch];
163 cnt += sizeof (wchar_t);
166 data->outbufavail = outchars;
168 if (outchars < data->outbufsize)
170 /* If there is still room in the output buffer something
171 is wrong with the input. */
172 if (inwchars >= cnt + sizeof (wchar_t))
174 /* An error occurred. */
175 result = GCONV_ILLEGAL_INPUT;
180 /* There are some unprocessed bytes at the end of the
182 result = GCONV_INCOMPLETE_INPUT;
188 if (result != GCONV_OK)
193 /* This is the last step. */
194 result = (*inbufsize > (dir == from_8bit
195 ? 0 : sizeof (wchar_t) - 1)
196 ? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
201 result = GCONV_EMPTY_INPUT;
203 if (data->outbufavail > 0)
205 /* Call the functions below in the chain. */
206 size_t newavail = data->outbufavail;
208 result = (*fct) (next_step, next_data, data->outbuf, &newavail,
211 /* Correct the output buffer. */
212 if (newavail != data->outbufavail && newavail > 0)
214 memmove (data->outbuf,
215 &data->outbuf[data->outbufavail - newavail],
217 data->outbufavail = newavail;
221 while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
224 if (written != NULL && data->is_last)