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. */
28 /* This is the Byte Order Mark character (BOM). */
30 /* And in the other byte order. */
34 /* Definitions used in the body of the `gconv' function. */
35 #define FROM_LOOP from_utf16_loop
36 #define TO_LOOP to_utf16_loop
39 #define MIN_NEEDED_FROM 2
40 #define MAX_NEEDED_FROM 4
41 #define MIN_NEEDED_TO 4
42 #define FROM_DIRECTION (dir == from_utf16)
43 #define PREPARE_LOOP \
44 enum direction dir = ((struct utf16_data *) step->__data)->dir; \
45 enum variant var = ((struct utf16_data *) step->__data)->var; \
46 int swap = ((struct utf16_data *) step->__data)->swap; \
47 if (FROM_DIRECTION || var == UTF_16) \
49 if (data->__invocation_counter == 0) \
51 /* We have to find out which byte order the file is encoded in. */ \
52 if (inptr + 2 > inend) \
53 return __GCONV_EMPTY_INPUT; \
55 if (get16u (inptr) == BOM) \
56 /* Simply ignore the BOM character. */ \
58 else if (get16u (inptr) == BOM_OE) \
60 ((struct utf16_data *) step->__data)->swap = 1; \
65 else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \
66 && data->__invocation_counter == 0) \
68 /* Emit the Byte Order Mark. */ \
69 if (__builtin_expect (outbuf + 2 > outend, 0)) \
70 return __GCONV_FULL_OUTPUT; \
72 put16u (outbuf, BOM); \
75 #define EXTRA_LOOP_ARGS , var, data, swap
78 /* Direction of the transformation. */
103 gconv_init (struct __gconv_step *step)
105 /* Determine which direction. */
106 struct utf16_data *new_data;
107 enum direction dir = illegal_dir;
108 enum variant var = illegal_var;
111 if (__strcasecmp (step->__from_name, "UTF-16") == 0)
116 else if (__strcasecmp (step->__to_name, "UTF-16") == 0)
121 else if (__strcasecmp (step->__from_name, "UTF-16BE") == 0)
126 else if (__strcasecmp (step->__to_name, "UTF-16BE") == 0)
131 else if (__strcasecmp (step->__from_name, "UTF-16LE") == 0)
136 else if (__strcasecmp (step->__to_name, "UTF-16LE") == 0)
142 result = __GCONV_NOCONV;
143 if (__builtin_expect (dir, to_utf16) != illegal_dir)
145 new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data));
147 result = __GCONV_NOMEM;
148 if (new_data != NULL)
152 new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN)
154 && BYTE_ORDER == LITTLE_ENDIAN));
155 step->__data = new_data;
157 if (dir == from_utf16)
159 step->__min_needed_from = MIN_NEEDED_FROM;
160 step->__max_needed_from = MIN_NEEDED_FROM;
161 step->__min_needed_to = MIN_NEEDED_TO;
162 step->__max_needed_to = MIN_NEEDED_TO;
166 step->__min_needed_from = MIN_NEEDED_TO;
167 step->__max_needed_from = MIN_NEEDED_TO;
168 step->__min_needed_to = MIN_NEEDED_FROM;
169 step->__max_needed_to = MIN_NEEDED_FROM;
172 step->__stateful = 0;
183 gconv_end (struct __gconv_step *data)
189 /* Convert from the internal (UCS4-like) format to UTF-16. */
190 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
191 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
192 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
193 #define LOOPFCT TO_LOOP
196 uint32_t c = get32 (inptr); \
200 if (__builtin_expect (c, 0) >= 0x10000) \
202 if (__builtin_expect (c, 0) >= 0x110000) \
204 if (! ignore_errors_p ()) \
206 /* This is an illegal character. */ \
207 result = __GCONV_ILLEGAL_INPUT; \
216 /* Generate a surrogate character. */ \
217 if (NEED_LENGTH_TEST && __builtin_expect (outptr + 4 > outend, 0))\
219 /* Overflow in the output buffer. */ \
220 result = __GCONV_FULL_OUTPUT; \
224 put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \
226 put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \
229 put16 (outptr, bswap_16 (c)); \
233 if (__builtin_expect (c, 0) >= 0x10000) \
235 if (__builtin_expect (c, 0) >= 0x110000) \
237 if (! ignore_errors_p ()) \
239 /* This is an illegal character. */ \
240 result = __GCONV_ILLEGAL_INPUT; \
249 /* Generate a surrogate character. */ \
250 if (NEED_LENGTH_TEST && __builtin_expect (outptr + 4 > outend, 0))\
252 /* Overflow in the output buffer. */ \
253 result = __GCONV_FULL_OUTPUT; \
257 put16 (outptr, 0xd7c0 + (c >> 10)); \
259 put16 (outptr, 0xdc00 + (c & 0x3ff)); \
267 #define EXTRA_LOOP_DECLS \
268 , enum variant var, struct __gconv_step_data *step_data, int swap
269 #include <iconv/loop.c>
272 /* Convert from UTF-16 to the internal (UCS4-like) format. */
273 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
274 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
275 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
276 #define LOOPFCT FROM_LOOP
279 uint16_t u1 = get16 (inptr); \
283 u1 = bswap_16 (u1); \
285 if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff) \
287 /* No surrogate. */ \
288 put32 (outptr, u1); \
295 /* It's a surrogate character. At least the first word says \
297 if (NEED_LENGTH_TEST && __builtin_expect (inptr + 4 > inend, 0)) \
299 /* We don't have enough input for another complete input \
301 result = __GCONV_INCOMPLETE_INPUT; \
306 u2 = bswap_16 (get16 (inptr)); \
307 if (__builtin_expect (u2, 0xdc00) < 0xdc00 \
308 || __builtin_expect (u2, 0xdc00) >= 0xdfff) \
310 /* This is no valid second word for a surrogate. */ \
311 if (! ignore_errors_p ()) \
314 result = __GCONV_ILLEGAL_INPUT; \
322 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
328 if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff) \
330 /* No surrogate. */ \
331 put32 (outptr, u1); \
338 /* It's a surrogate character. At least the first word says \
340 if (NEED_LENGTH_TEST && __builtin_expect (inptr + 4 > inend, 0)) \
342 /* We don't have enough input for another complete input \
344 result = __GCONV_INCOMPLETE_INPUT; \
349 u2 = get16 (inptr); \
350 if (__builtin_expect (u2, 0xdc00) < 0xdc00 \
351 || __builtin_expect (u2, 0xdc00) >= 0xdfff) \
353 /* This is no valid second word for a surrogate. */ \
354 if (! ignore_errors_p ()) \
357 result = __GCONV_ILLEGAL_INPUT; \
365 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
371 #define EXTRA_LOOP_DECLS \
372 , enum variant var, struct __gconv_step_data *step_data, int swap
373 #include <iconv/loop.c>
376 /* Now define the toplevel functions. */
377 #include <iconv/skeleton.c>