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. */
35 /* Now we can include the tables. */
38 /* Direction of the transformation. */
53 gconv_init (struct gconv_step
*step
, struct gconv_step_data
*data
)
55 /* Determine which direction. */
56 struct s_8bit_data
*new_data
;
60 if (strcasestr (step
->from_name
, NAME
) != NULL
)
62 else if (strcasestr (step
->to_name
, NAME
) != NULL
)
67 result
= GCONV_NOCONV
;
70 = (struct s_8bit_data
*) malloc (sizeof (struct s_8bit_data
)))
74 data
->data
= new_data
;
83 gconv_end (struct gconv_step_data
*data
)
90 gconv (struct gconv_step
*step
, struct gconv_step_data
*data
,
91 const char *inbuf
, size_t *inbufsize
, size_t *written
, int do_flush
)
93 struct gconv_step
*next_step
= step
+ 1;
94 struct gconv_step_data
*next_data
= data
+ 1;
95 gconv_fct fct
= next_step
->fct
;
99 /* If the function is called with no input this means we have to reset
100 to the initial state. The possibly partly converted input is
106 /* Call the steps down the chain if there are any. */
111 struct gconv_step
*next_step
= step
+ 1;
112 struct gconv_step_data
*next_data
= data
+ 1;
114 result
= (*fct
) (next_step
, next_data
, NULL
, 0, written
, 1);
116 /* Clear output buffer. */
117 data
->outbufavail
= 0;
122 enum direction dir
= ((struct s_8bit_data
*) data
->data
)->dir
;
130 if (dir
== from_8bit
)
132 size_t inchars
= *inbufsize
;
133 size_t outwchars
= data
->outbufavail
;
134 char *outbuf
= data
->outbuf
;
138 && (outwchars
+ sizeof (wchar_t) <= data
->outbufsize
))
140 wchar_t ch
= to_ucs4
[(unsigned int) inbuf
[cnt
]];
142 if (ch
== L
'\0' && inbuf
[cnt
] != '\0')
144 /* This is an illegal character. */
145 result
= GCONV_ILLEGAL_INPUT
;
149 *((wchar_t *) (outbuf
+ outwchars
)) = ch
;
151 outwchars
+= sizeof (wchar_t);
155 data
->outbufavail
= outwchars
;
159 size_t inwchars
= *inbufsize
;
160 size_t outchars
= data
->outbufavail
;
161 char *outbuf
= data
->outbuf
;
164 while (inwchars
>= cnt
+ sizeof (wchar_t)
165 && outchars
< data
->outbufsize
)
167 const struct gap
*rp
= from_idx
;
168 unsigned int ch
= *((wchar_t *) (inbuf
+ cnt
));
174 /* No valid character. */
177 res
= from_ucs4
[ch
+ rp
->idx
];
178 if (res
== '\0' && ch
!= 0)
179 /* No valid character. */
182 outbuf
[outchars
] = res
;
185 cnt
+= sizeof (wchar_t);
188 data
->outbufavail
= outchars
;
190 if (outchars
< data
->outbufsize
)
192 /* If there is still room in the output buffer something
193 is wrong with the input. */
194 if (inwchars
>= cnt
+ sizeof (wchar_t))
196 /* An error occurred. */
197 result
= GCONV_ILLEGAL_INPUT
;
202 /* There are some unprocessed bytes at the end of the
204 result
= GCONV_INCOMPLETE_INPUT
;
210 if (result
!= GCONV_OK
)
215 /* This is the last step. */
216 result
= (*inbufsize
> (dir
== from_8bit
217 ? 0 : sizeof (wchar_t) - 1)
218 ? GCONV_FULL_OUTPUT
: GCONV_EMPTY_INPUT
);
223 result
= GCONV_EMPTY_INPUT
;
225 if (data
->outbufavail
> 0)
227 /* Call the functions below in the chain. */
228 size_t newavail
= data
->outbufavail
;
230 result
= (*fct
) (next_step
, next_data
, data
->outbuf
, &newavail
,
233 /* Correct the output buffer. */
234 if (newavail
!= data
->outbufavail
&& newavail
> 0)
236 memmove (data
->outbuf
,
237 &data
->outbuf
[data
->outbufavail
- newavail
],
239 data
->outbufavail
= newavail
;
243 while (*inbufsize
> 0 && result
== GCONV_EMPTY_INPUT
);
246 if (written
!= NULL
&& data
->is_last
)