Add _nl_domain_bindings.
[glibc/history.git] / iconvdata / 8bit-generic.c
blobfe0a09bd3eff335302da261cb330974f112e6169
1 /* Generic conversion to and from 8bit charsets.
2 Copyright (C) 1997 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. */
21 #include <gconv.h>
22 #include <stdlib.h>
23 #include <string.h>
25 /* Direction of the transformation. */
26 enum direction
28 illegal,
29 to_8bit,
30 from_8bit
33 struct s_8bit_data
35 enum direction dir;
39 int
40 gconv_init (struct gconv_step *step, struct gconv_step_data *data)
42 /* Determine which direction. */
43 struct s_8bit_data *new_data;
44 enum direction dir;
45 int result;
47 if (__strcasestr (step->from_name, NAME) != NULL)
48 dir = from_8bit;
49 else if (__strcasestr (step->to_name, NAME) != NULL)
50 dir = to_8bit;
51 else
52 dir = illegal;
54 result = GCONV_NOCONV;
55 if (dir != illegal
56 && ((new_data
57 = (struct s_8bit_data *) malloc (sizeof (struct s_8bit_data)))
58 != NULL))
60 new_data->dir = dir;
61 data->data = new_data;
62 result = GCONV_OK;
65 return result;
69 void
70 gconv_end (struct gconv_step_data *data)
72 free (data->data);
76 int
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;
83 size_t do_write;
84 int result;
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
88 dropped. */
89 if (do_flush)
91 do_write = 0;
93 /* Call the steps down the chain if there are any. */
94 if (data->is_last)
95 result = GCONV_OK;
96 else
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;
107 else
109 enum direction dir = ((struct s_8bit_data *) data->data)->dir;
111 do_write = 0;
115 result = GCONV_OK;
117 if (dir == from_8bit)
119 size_t inchars = *inbufsize;
120 size_t outwchars = data->outbufavail;
121 char *outbuf = data->outbuf;
122 size_t cnt = 0;
124 while (cnt < inchars
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;
133 break;
136 *((wchar_t *) (outbuf + outwchars)) = ch;
137 ++do_write;
138 outwchars += sizeof (wchar_t);
139 ++cnt;
141 *inbufsize -= cnt;
142 data->outbufavail = outwchars;
144 else
146 size_t inwchars = *inbufsize;
147 size_t outchars = data->outbufavail;
148 char *outbuf = data->outbuf;
149 size_t cnt = 0;
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))
158 break;
160 outbuf[outchars] = from_ucs4[ch];
161 ++do_write;
162 ++outchars;
163 cnt += sizeof (wchar_t);
165 *inbufsize -= cnt;
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;
176 break;
178 if (inwchars != cnt)
180 /* There are some unprocessed bytes at the end of the
181 input buffer. */
182 result = GCONV_INCOMPLETE_INPUT;
183 break;
188 if (result != GCONV_OK)
189 break;
191 if (data->is_last)
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);
197 break;
200 /* Status so far. */
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,
209 written, 0);
211 /* Correct the output buffer. */
212 if (newavail != data->outbufavail && newavail > 0)
214 memmove (data->outbuf,
215 &data->outbuf[data->outbufavail - newavail],
216 newavail);
217 data->outbufavail = newavail;
221 while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
224 if (written != NULL && data->is_last)
225 *written = do_write;
227 return result;