Update.
[glibc.git] / iconvdata / utf-32.c
blob2e245c91735c83d020521b65b3970b8c940544fc
1 /* Conversion module for UTF-32.
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <byteswap.h>
21 #include <dlfcn.h>
22 #include <gconv.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
28 /* This is the Byte Order Mark character (BOM). */
29 #define BOM 0x0000feffu
30 /* And in the other byte order. */
31 #define BOM_OE 0xfffe0000u
34 /* Definitions used in the body of the `gconv' function. */
35 #define FROM_LOOP from_utf32_loop
36 #define TO_LOOP to_utf32_loop
37 #define DEFINE_INIT 0
38 #define DEFINE_FINI 0
39 #define MIN_NEEDED_FROM 4
40 #define MIN_NEEDED_TO 4
41 #define FROM_DIRECTION (dir == from_utf32)
42 #define PREPARE_LOOP \
43 enum direction dir = ((struct utf32_data *) step->__data)->dir; \
44 enum variant var = ((struct utf32_data *) step->__data)->var; \
45 int swap; \
46 if (FROM_DIRECTION && var == UTF_32) \
47 { \
48 if (data->__invocation_counter == 0) \
49 { \
50 /* We have to find out which byte order the file is encoded in. */ \
51 if (inptr + 4 > inend) \
52 return __GCONV_EMPTY_INPUT; \
54 if (get32u (inptr) == BOM) \
55 /* Simply ignore the BOM character. */ \
56 *inptrp = inptr += 4; \
57 else if (get32u (inptr) == BOM_OE) \
58 { \
59 ((struct utf32_data *) step->__data)->swap = 1; \
60 *inptrp = inptr += 4; \
61 } \
62 } \
63 } \
64 else if (!FROM_DIRECTION && var == UTF_32 && !data->__internal_use \
65 && data->__invocation_counter == 0) \
66 { \
67 /* Emit the Byte Order Mark. */ \
68 if (__builtin_expect (outbuf + 4 > outend, 0)) \
69 return __GCONV_FULL_OUTPUT; \
71 put32u (outbuf, BOM); \
72 outbuf += 4; \
73 } \
74 swap = ((struct utf32_data *) step->__data)->swap;
75 #define EXTRA_LOOP_ARGS , var, swap
78 /* Direction of the transformation. */
79 enum direction
81 illegal_dir,
82 to_utf32,
83 from_utf32
86 enum variant
88 illegal_var,
89 UTF_32,
90 UTF_32LE,
91 UTF_32BE
94 struct utf32_data
96 enum direction dir;
97 enum variant var;
98 int swap;
102 extern int gconv_init (struct __gconv_step *step);
104 gconv_init (struct __gconv_step *step)
106 /* Determine which direction. */
107 struct utf32_data *new_data;
108 enum direction dir = illegal_dir;
109 enum variant var = illegal_var;
110 int result;
112 if (__strcasecmp (step->__from_name, "UTF-32//") == 0)
114 dir = from_utf32;
115 var = UTF_32;
117 else if (__strcasecmp (step->__to_name, "UTF-32//") == 0)
119 dir = to_utf32;
120 var = UTF_32;
122 else if (__strcasecmp (step->__from_name, "UTF-32BE//") == 0)
124 dir = from_utf32;
125 var = UTF_32BE;
127 else if (__strcasecmp (step->__to_name, "UTF-32BE//") == 0)
129 dir = to_utf32;
130 var = UTF_32BE;
132 else if (__strcasecmp (step->__from_name, "UTF-32LE//") == 0)
134 dir = from_utf32;
135 var = UTF_32LE;
137 else if (__strcasecmp (step->__to_name, "UTF-32LE//") == 0)
139 dir = to_utf32;
140 var = UTF_32LE;
143 result = __GCONV_NOCONV;
144 if (__builtin_expect (dir, to_utf32) != illegal_dir)
146 new_data = (struct utf32_data *) malloc (sizeof (struct utf32_data));
148 result = __GCONV_NOMEM;
149 if (new_data != NULL)
151 new_data->dir = dir;
152 new_data->var = var;
153 new_data->swap = ((var == UTF_32LE && BYTE_ORDER == BIG_ENDIAN)
154 || (var == UTF_32BE
155 && BYTE_ORDER == LITTLE_ENDIAN));
156 step->__data = new_data;
158 if (dir == from_utf32)
160 step->__min_needed_from = MIN_NEEDED_FROM;
161 step->__max_needed_from = MIN_NEEDED_FROM;
162 step->__min_needed_to = MIN_NEEDED_TO;
163 step->__max_needed_to = MIN_NEEDED_TO;
165 else
167 step->__min_needed_from = MIN_NEEDED_TO;
168 step->__max_needed_from = MIN_NEEDED_TO;
169 step->__min_needed_to = MIN_NEEDED_FROM;
170 step->__max_needed_to = MIN_NEEDED_FROM;
173 step->__stateful = 0;
175 result = __GCONV_OK;
179 return result;
183 extern void gconv_end (struct __gconv_step *data);
184 void
185 gconv_end (struct __gconv_step *data)
187 free (data->__data);
191 /* Convert from the internal (UCS4-like) format to UTF-32. */
192 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
193 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
194 #define LOOPFCT TO_LOOP
195 #define BODY \
197 uint32_t c = get32 (inptr); \
199 if (__builtin_expect (c >= 0x110000, 0)) \
201 STANDARD_ERR_HANDLER (4); \
203 else if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0)) \
205 /* Surrogate characters in UCS-4 input are not valid. \
206 We must catch this. If we let surrogates pass through, \
207 attackers could make a security hole exploit by \
208 generating "irregular UTF-32" sequences. */ \
209 if (! ignore_errors_p ()) \
211 result = __GCONV_ILLEGAL_INPUT; \
212 break; \
214 inptr += 4; \
215 ++*irreversible; \
216 continue; \
219 if (swap) \
220 put32 (outptr, bswap_32 (c)); \
221 else \
222 put32 (outptr, c); \
224 outptr += 4; \
225 inptr += 4; \
227 #define LOOP_NEED_FLAGS
228 #define EXTRA_LOOP_DECLS \
229 , enum variant var, int swap
230 #include <iconv/loop.c>
233 /* Convert from UTF-32 to the internal (UCS4-like) format. */
234 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
235 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
236 #define LOOPFCT FROM_LOOP
237 #define BODY \
239 uint32_t u1 = get32 (inptr); \
241 if (swap) \
242 u1 = bswap_32 (u1); \
244 if (__builtin_expect (u1 >= 0x110000, 0)) \
246 /* This is illegal. */ \
247 if (! ignore_errors_p ()) \
249 /* This is an illegal character. */ \
250 result = __GCONV_ILLEGAL_INPUT; \
251 break; \
254 inptr += 4; \
255 ++*irreversible; \
256 continue; \
259 put32 (outptr, u1); \
260 inptr += 4; \
261 outptr += 4; \
263 #define LOOP_NEED_FLAGS
264 #define EXTRA_LOOP_DECLS \
265 , enum variant var, int swap
266 #include <iconv/loop.c>
269 /* Now define the toplevel functions. */
270 #include <iconv/skeleton.c>