]>
git.ipfire.org Git - thirdparty/glibc.git/blob - iconvdata/utf-16.c
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). */
32 /* Definitions used in the body of the `gconv' function. */
33 #define FROM_LOOP from_utf16_loop
34 #define TO_LOOP to_utf16_loop
37 #define MIN_NEEDED_FROM 2
38 #define MAX_NEEDED_FROM 4
39 #define MIN_NEEDED_TO 4
40 #define FROM_DIRECTION (dir == from_utf16)
41 #define PREPARE_LOOP \
42 enum direction dir = ((struct utf16_data *) step->__data)->dir; \
43 enum variant var = ((struct utf16_data *) step->__data)->var; \
44 if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \
45 && data->__invocation_counter == 0) \
47 /* Emit the Byte Order Mark. */ \
48 if (outbuf + 2 > outend) \
49 return __GCONV_FULL_OUTPUT; \
51 *(uint16_t *) outbuf = BOM; \
54 #define EXTRA_LOOP_ARGS , var, data
57 /* Direction of the transformation. */
81 gconv_init (struct __gconv_step
*step
)
83 /* Determine which direction. */
84 struct utf16_data
*new_data
;
85 enum direction dir
= illegal_dir
;
86 enum variant var
= illegal_var
;
89 if (__strcasecmp (step
->__from_name
, "UTF-16") == 0)
94 else if (__strcasecmp (step
->__to_name
, "UTF-16") == 0)
99 else if (__strcasecmp (step
->__from_name
, "UTF-16BE") == 0)
104 else if (__strcasecmp (step
->__to_name
, "UTF-16BE") == 0)
109 else if (__strcasecmp (step
->__from_name
, "UTF-16LE") == 0)
114 else if (__strcasecmp (step
->__to_name
, "UTF-16LE") == 0)
120 result
= __GCONV_NOCONV
;
121 if (dir
!= illegal_dir
)
123 new_data
= (struct utf16_data
*) malloc (sizeof (struct utf16_data
));
125 result
= __GCONV_NOMEM
;
126 if (new_data
!= NULL
)
130 step
->__data
= new_data
;
132 if (dir
== from_utf16
)
134 step
->__min_needed_from
= MIN_NEEDED_FROM
;
135 step
->__max_needed_from
= MIN_NEEDED_FROM
;
136 step
->__min_needed_to
= MIN_NEEDED_TO
;
137 step
->__max_needed_to
= MIN_NEEDED_TO
;
141 step
->__min_needed_from
= MIN_NEEDED_TO
;
142 step
->__max_needed_from
= MIN_NEEDED_TO
;
143 step
->__min_needed_to
= MIN_NEEDED_FROM
;
144 step
->__max_needed_to
= MIN_NEEDED_FROM
;
147 step
->__stateful
= 0;
158 gconv_end (struct __gconv_step
*data
)
164 /* Convert from the internal (UCS4-like) format to UTF-16. */
165 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
166 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
167 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
168 #define LOOPFCT TO_LOOP
171 uint32_t c = *((uint32_t *) inptr); \
173 if ((__BYTE_ORDER == __LITTLE_ENDIAN && var == UTF_16BE) \
174 || (__BYTE_ORDER == __BIG_ENDIAN && var == UTF_16LE)) \
180 result = __GCONV_ILLEGAL_INPUT; \
184 /* Generate a surrogate character. */ \
185 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
187 /* Overflow in the output buffer. */ \
188 result = __GCONV_FULL_OUTPUT; \
192 *((uint16_t *) outptr) = bswap_16 (0xd7c0 + (c >> 10)); \
194 *((uint16_t *) outptr) = bswap_16 (0xdc00 + (c & 0x3ff)); \
197 *((uint16_t *) outptr) = bswap_16 (c); \
205 result = __GCONV_ILLEGAL_INPUT; \
209 /* Generate a surrogate character. */ \
210 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
212 /* Overflow in the output buffer. */ \
213 result = __GCONV_FULL_OUTPUT; \
217 *((uint16_t *) outptr) = 0xd7c0 + (c >> 10); \
219 *((uint16_t *) outptr) = 0xdc00 + (c & 0x3ff); \
222 *((uint16_t *) outptr) = c; \
227 #define EXTRA_LOOP_DECLS \
228 , enum variant var, struct __gconv_step_data *step_data
229 #include <iconv/loop.c>
232 /* Convert from UTF-16 to the internal (UCS4-like) format. */
233 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
234 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
235 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
236 #define LOOPFCT FROM_LOOP
239 uint16_t u1 = *(uint16_t *) inptr; \
241 if ((__BYTE_ORDER == __LITTLE_ENDIAN && var == UTF_16BE) \
242 || (__BYTE_ORDER == __BIG_ENDIAN && var == UTF_16LE)) \
244 u1 = bswap_16 (u1); \
246 if (u1 < 0xd800 || u1 > 0xdfff) \
248 /* No surrogate. */ \
249 *((uint32_t *) outptr) = u1; \
256 /* It's a surrogate character. At least the first word says \
258 if (NEED_LENGTH_TEST && inptr + 4 > inend) \
260 /* We don't have enough input for another complete input \
262 result = __GCONV_INCOMPLETE_INPUT; \
266 u2 = bswap_16 (((uint16_t *) inptr)[1]); \
267 if (u2 < 0xdc00 || u2 >= 0xdfff) \
269 /* This is no valid second word for a surrogate. */ \
270 result = __GCONV_ILLEGAL_INPUT; \
274 *((uint32_t *) outptr) = ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00); \
280 if (u1 == BOM && var == UTF_16 && !step_data->__internal_use \
281 && step_data->__invocation_counter == 0 && inptr == *inptrp) \
283 /* This is the first word in the file and it is the BOM and \
284 we are converting a file without specified byte order. \
285 Simply sack the BOM. */ \
290 if (u1 < 0xd800 || u1 > 0xdfff) \
292 /* No surrogate. */ \
293 *((uint32_t *) outptr) = u1; \
300 /* It's a surrogate character. At least the first word says \
302 if (NEED_LENGTH_TEST && inptr + 4 > inend) \
304 /* We don't have enough input for another complete input \
306 result = __GCONV_INCOMPLETE_INPUT; \
310 u2 = ((uint16_t *) inptr)[1]; \
311 if (u2 < 0xdc00 || u2 >= 0xdfff) \
313 /* This is no valid second word for a surrogate. */ \
314 result = __GCONV_ILLEGAL_INPUT; \
318 *((uint32_t *) outptr) = ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00); \
324 #define EXTRA_LOOP_DECLS \
325 , enum variant var, struct __gconv_step_data *step_data
326 #include <iconv/loop.c>
329 /* Now define the toplevel functions. */
330 #include <iconv/skeleton.c>