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 > inbufend) \
53 return __GCONV_EMPTY_INPUT; \
55 if (*(uint16_t *) inptr == BOM) \
56 /* Simply ignore the BOM character. */ \
58 else if (*(uint16_t *) 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 (outbuf + 2 > outend) \
70 return __GCONV_FULL_OUTPUT; \
72 *(uint16_t *) 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 (dir != 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 = *((uint32_t *) inptr); \
204 result = __GCONV_ILLEGAL_INPUT; \
208 /* Generate a surrogate character. */ \
209 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
211 /* Overflow in the output buffer. */ \
212 result = __GCONV_FULL_OUTPUT; \
216 *((uint16_t *) outptr) = bswap_16 (0xd7c0 + (c >> 10)); \
218 *((uint16_t *) outptr) = bswap_16 (0xdc00 + (c & 0x3ff)); \
221 *((uint16_t *) outptr) = bswap_16 (c); \
229 result = __GCONV_ILLEGAL_INPUT; \
233 /* Generate a surrogate character. */ \
234 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
236 /* Overflow in the output buffer. */ \
237 result = __GCONV_FULL_OUTPUT; \
241 *((uint16_t *) outptr) = 0xd7c0 + (c >> 10); \
243 *((uint16_t *) outptr) = 0xdc00 + (c & 0x3ff); \
246 *((uint16_t *) outptr) = c; \
251 #define EXTRA_LOOP_DECLS \
252 , enum variant var, struct __gconv_step_data *step_data, 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
263 uint16_t u1 = *(uint16_t *) inptr; \
267 u1 = bswap_16 (u1); \
269 if (u1 < 0xd800 || u1 > 0xdfff) \
271 /* No surrogate. */ \
272 *((uint32_t *) outptr) = u1; \
279 /* It's a surrogate character. At least the first word says \
281 if (NEED_LENGTH_TEST && inptr + 4 > inend) \
283 /* We don't have enough input for another complete input \
285 result = __GCONV_INCOMPLETE_INPUT; \
289 u2 = bswap_16 (((uint16_t *) inptr)[1]); \
290 if (u2 < 0xdc00 || u2 >= 0xdfff) \
292 /* This is no valid second word for a surrogate. */ \
293 result = __GCONV_ILLEGAL_INPUT; \
297 *((uint32_t *) outptr) = ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00); \
303 if (u1 < 0xd800 || u1 > 0xdfff) \
305 /* No surrogate. */ \
306 *((uint32_t *) outptr) = u1; \
313 /* It's a surrogate character. At least the first word says \
315 if (NEED_LENGTH_TEST && inptr + 4 > inend) \
317 /* We don't have enough input for another complete input \
319 result = __GCONV_INCOMPLETE_INPUT; \
323 u2 = ((uint16_t *) inptr)[1]; \
324 if (u2 < 0xdc00 || u2 >= 0xdfff) \
326 /* This is no valid second word for a surrogate. */ \
327 result = __GCONV_ILLEGAL_INPUT; \
331 *((uint32_t *) outptr) = ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00); \
337 #define EXTRA_LOOP_DECLS \
338 , enum variant var, struct __gconv_step_data *step_data, int swap
339 #include <iconv/loop.c>
342 /* Now define the toplevel functions. */
343 #include <iconv/skeleton.c>