Update.
[glibc.git] / iconvdata / utf-16.c
blob4b7fefaf2892e3f5af4df78b698d018861c8fcf6
1 /* Conversion module for UTF-16.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
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 <byteswap.h>
22 #include <dlfcn.h>
23 #include <gconv.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
29 /* This is the Byte Order Mark character (BOM). */
30 #define BOM 0xfeff
31 /* And in the other byte order. */
32 #define BOM_OE 0xfffe
35 /* Definitions used in the body of the `gconv' function. */
36 #define FROM_LOOP from_utf16_loop
37 #define TO_LOOP to_utf16_loop
38 #define DEFINE_INIT 0
39 #define DEFINE_FINI 0
40 #define MIN_NEEDED_FROM 2
41 #define MAX_NEEDED_FROM 4
42 #define MIN_NEEDED_TO 4
43 #define FROM_DIRECTION (dir == from_utf16)
44 #define PREPARE_LOOP \
45 enum direction dir = ((struct utf16_data *) step->__data)->dir; \
46 enum variant var = ((struct utf16_data *) step->__data)->var; \
47 int swap = ((struct utf16_data *) step->__data)->swap; \
48 if (FROM_DIRECTION || var == UTF_16) \
49 { \
50 if (data->__invocation_counter == 0) \
51 { \
52 /* We have to find out which byte order the file is encoded in. */ \
53 if (inptr + 2 > inend) \
54 return __GCONV_EMPTY_INPUT; \
56 if (get16u (inptr) == BOM) \
57 /* Simply ignore the BOM character. */ \
58 inptr += 2; \
59 else if (get16u (inptr) == BOM_OE) \
60 { \
61 ((struct utf16_data *) step->__data)->swap = 1; \
62 inptr += 2; \
63 } \
64 } \
65 } \
66 else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \
67 && data->__invocation_counter == 0) \
68 { \
69 /* Emit the Byte Order Mark. */ \
70 if (__builtin_expect (outbuf + 2 > outend, 0)) \
71 return __GCONV_FULL_OUTPUT; \
73 put16u (outbuf, BOM); \
74 outbuf += 2; \
76 #define EXTRA_LOOP_ARGS , var, swap
79 /* Direction of the transformation. */
80 enum direction
82 illegal_dir,
83 to_utf16,
84 from_utf16
87 enum variant
89 illegal_var,
90 UTF_16,
91 UTF_16LE,
92 UTF_16BE
95 struct utf16_data
97 enum direction dir;
98 enum variant var;
99 int swap;
104 gconv_init (struct __gconv_step *step)
106 /* Determine which direction. */
107 struct utf16_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-16") == 0)
114 dir = from_utf16;
115 var = UTF_16;
117 else if (__strcasecmp (step->__to_name, "UTF-16") == 0)
119 dir = to_utf16;
120 var = UTF_16;
122 else if (__strcasecmp (step->__from_name, "UTF-16BE") == 0)
124 dir = from_utf16;
125 var = UTF_16BE;
127 else if (__strcasecmp (step->__to_name, "UTF-16BE") == 0)
129 dir = to_utf16;
130 var = UTF_16BE;
132 else if (__strcasecmp (step->__from_name, "UTF-16LE") == 0)
134 dir = from_utf16;
135 var = UTF_16LE;
137 else if (__strcasecmp (step->__to_name, "UTF-16LE") == 0)
139 dir = to_utf16;
140 var = UTF_16LE;
143 result = __GCONV_NOCONV;
144 if (__builtin_expect (dir, to_utf16) != illegal_dir)
146 new_data = (struct utf16_data *) malloc (sizeof (struct utf16_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_16LE && BYTE_ORDER == BIG_ENDIAN)
154 || (var == UTF_16BE
155 && BYTE_ORDER == LITTLE_ENDIAN));
156 step->__data = new_data;
158 if (dir == from_utf16)
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 void
184 gconv_end (struct __gconv_step *data)
186 free (data->__data);
190 /* Convert from the internal (UCS4-like) format to UTF-16. */
191 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
192 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
193 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
194 #define LOOPFCT TO_LOOP
195 #define BODY \
197 uint32_t c = get32 (inptr); \
199 if (swap) \
201 if (__builtin_expect (c, 0) >= 0x10000) \
203 if (__builtin_expect (c, 0) >= 0x110000) \
205 STANDARD_ERR_HANDLER (4); \
208 /* Generate a surrogate character. */ \
209 if (__builtin_expect (outptr + 4 > outend, 0)) \
211 /* Overflow in the output buffer. */ \
212 result = __GCONV_FULL_OUTPUT; \
213 break; \
216 put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \
217 outptr += 2; \
218 put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \
220 else \
221 put16 (outptr, bswap_16 (c)); \
223 else \
225 if (__builtin_expect (c, 0) >= 0x10000) \
227 if (__builtin_expect (c, 0) >= 0x110000) \
229 STANDARD_ERR_HANDLER (4); \
232 /* Generate a surrogate character. */ \
233 if (__builtin_expect (outptr + 4 > outend, 0)) \
235 /* Overflow in the output buffer. */ \
236 result = __GCONV_FULL_OUTPUT; \
237 break; \
240 put16 (outptr, 0xd7c0 + (c >> 10)); \
241 outptr += 2; \
242 put16 (outptr, 0xdc00 + (c & 0x3ff)); \
244 else \
245 put16 (outptr, c); \
247 outptr += 2; \
248 inptr += 4; \
250 #define LOOP_NEED_FLAGS
251 #define EXTRA_LOOP_DECLS \
252 , enum variant var, int swap
253 #include <iconv/loop.c>
256 /* Convert from UTF-16 to the internal (UCS4-like) format. */
257 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
258 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
259 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
260 #define LOOPFCT FROM_LOOP
261 #define BODY \
263 uint16_t u1 = get16 (inptr); \
265 if (swap) \
267 u1 = bswap_16 (u1); \
269 if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff) \
271 /* No surrogate. */ \
272 put32 (outptr, u1); \
273 inptr += 2; \
275 else \
277 uint16_t u2; \
279 /* It's a surrogate character. At least the first word says \
280 it is. */ \
281 if (__builtin_expect (inptr + 4 > inend, 0)) \
283 /* We don't have enough input for another complete input \
284 character. */ \
285 result = __GCONV_INCOMPLETE_INPUT; \
286 break; \
289 inptr += 2; \
290 u2 = bswap_16 (get16 (inptr)); \
291 if (__builtin_expect (u2, 0xdc00) < 0xdc00 \
292 || __builtin_expect (u2, 0xdc00) >= 0xdfff) \
294 /* This is no valid second word for a surrogate. */ \
295 if (! ignore_errors_p ()) \
297 inptr -= 2; \
298 result = __GCONV_ILLEGAL_INPUT; \
299 break; \
302 ++*irreversible; \
303 continue; \
306 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
307 inptr += 2; \
310 else \
312 if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff) \
314 /* No surrogate. */ \
315 put32 (outptr, u1); \
316 inptr += 2; \
318 else \
320 uint16_t u2; \
322 /* It's a surrogate character. At least the first word says \
323 it is. */ \
324 if (__builtin_expect (inptr + 4 > inend, 0)) \
326 /* We don't have enough input for another complete input \
327 character. */ \
328 result = __GCONV_INCOMPLETE_INPUT; \
329 break; \
332 inptr += 2; \
333 u2 = get16 (inptr); \
334 if (__builtin_expect (u2, 0xdc00) < 0xdc00 \
335 || __builtin_expect (u2, 0xdc00) >= 0xdfff) \
337 /* This is no valid second word for a surrogate. */ \
338 if (! ignore_errors_p ()) \
340 inptr -= 2; \
341 result = __GCONV_ILLEGAL_INPUT; \
342 break; \
345 ++*irreversible; \
346 continue; \
349 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
350 inptr += 2; \
353 outptr += 4; \
355 #define LOOP_NEED_FLAGS
356 #define EXTRA_LOOP_DECLS \
357 , enum variant var, int swap
358 #include <iconv/loop.c>
361 /* Now define the toplevel functions. */
362 #include <iconv/skeleton.c>