1 /* Conversion to and from ISO 8859-1.
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. */
25 /* Direction of the transformation. */
40 gconv_init (struct gconv_step
*step
, struct gconv_step_data
*data
)
42 /* Determine which direction. */
43 struct iso88591_data
*new_data
;
47 if (__strcasestr (step
->from_name
, "ISO-8859-1") != NULL
)
49 else if (__strcasestr (step
->to_name
, "ISO-8859-1") != NULL
)
54 result
= GCONV_NOCONV
;
57 = (struct iso88591_data
*) malloc (sizeof (struct iso88591_data
)))
61 data
->data
= new_data
;
70 gconv_end (struct gconv_step_data
*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 iso88591_data
*) data
->data
)->dir
;
117 if (dir
== from_iso88591
)
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 *) (outbuf
+ outwchars
)) = inbuf
[cnt
];
129 outwchars
+= sizeof (wchar_t);
133 data
->outbufavail
= outwchars
;
137 size_t inwchars
= *inbufsize
;
138 size_t outchars
= data
->outbufavail
;
139 char *outbuf
= data
->outbuf
;
142 while (inwchars
>= cnt
+ sizeof (wchar_t)
143 && outchars
< data
->outbufsize
)
145 if (*((wchar_t *) (inbuf
+ cnt
)) >= L
'\0'
146 && *((wchar_t *) (inbuf
+ cnt
)) <= L
'\377')
147 outbuf
[outchars
] = *((wchar_t *) (inbuf
+ cnt
));
149 /* Here is where the transliteration would enter the
155 cnt
+= sizeof (wchar_t);
158 data
->outbufavail
= outchars
;
160 if (outchars
< data
->outbufsize
)
162 /* If there is still room in the output buffer something
163 is wrong with the input. */
164 if (inwchars
>= cnt
+ sizeof (wchar_t))
166 /* An error occurred. */
167 result
= GCONV_ILLEGAL_INPUT
;
172 /* There are some unprocessed bytes at the end of the
174 result
= GCONV_INCOMPLETE_INPUT
;
180 if (result
!= GCONV_OK
)
185 /* This is the last step. */
186 result
= (*inbufsize
> (dir
== from_iso88591
187 ? 0 : sizeof (wchar_t) - 1)
188 ? GCONV_FULL_OUTPUT
: GCONV_EMPTY_INPUT
);
193 result
= GCONV_EMPTY_INPUT
;
195 if (data
->outbufavail
> 0)
197 /* Call the functions below in the chain. */
198 size_t newavail
= data
->outbufavail
;
200 result
= (*fct
) (next_step
, next_data
, data
->outbuf
, &newavail
,
203 /* Correct the output buffer. */
204 if (newavail
!= data
->outbufavail
&& newavail
> 0)
206 memmove (data
->outbuf
,
207 &data
->outbuf
[data
->outbufavail
- newavail
],
209 data
->outbufavail
= newavail
;
213 while (*inbufsize
> 0 && result
== GCONV_EMPTY_INPUT
);
216 if (written
!= NULL
&& data
->is_last
)