1 /* Conversion module for UTF-16.
2 Copyright (C) 1999, 2000-2002, 2003, 2005 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 /* This is the Byte Order Mark character (BOM). */
31 /* And in the other byte order. */
35 /* Definitions used in the body of the `gconv' function. */
36 #define FROM_LOOP from_utf16_loop
37 #define TO_LOOP to_utf16_loop
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 if (__builtin_expect (data->__invocation_counter == 0, 0) && var == UTF_16) \
51 /* We have to find out which byte order the file is encoded in. */ \
52 if (inptr + 2 > inend) \
53 return (inptr == inend \
54 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT); \
56 if (get16u (inptr) == BOM) \
57 /* Simply ignore the BOM character. */ \
58 *inptrp = inptr += 2; \
59 else if (get16u (inptr) == BOM_OE) \
61 ((struct utf16_data *) step->__data)->swap = 1; \
62 *inptrp = inptr += 2; \
65 else if (!FROM_DIRECTION && !data->__internal_use) \
67 /* Emit the Byte Order Mark. */ \
68 if (__builtin_expect (outbuf + 2 > outend, 0)) \
69 return __GCONV_FULL_OUTPUT; \
71 put16u (outbuf, BOM); \
75 int swap = ((struct utf16_data *) step->__data)->swap;
76 #define EXTRA_LOOP_ARGS , swap
79 /* Direction of the transformation. */
103 extern int gconv_init (struct __gconv_step
*step
);
105 gconv_init (struct __gconv_step
*step
)
107 /* Determine which direction. */
108 struct utf16_data
*new_data
;
109 enum direction dir
= illegal_dir
;
110 enum variant var
= illegal_var
;
113 if (__strcasecmp (step
->__from_name
, "UTF-16//") == 0)
118 else if (__strcasecmp (step
->__to_name
, "UTF-16//") == 0)
123 else if (__strcasecmp (step
->__from_name
, "UTF-16BE//") == 0)
128 else if (__strcasecmp (step
->__to_name
, "UTF-16BE//") == 0)
133 else if (__strcasecmp (step
->__from_name
, "UTF-16LE//") == 0)
138 else if (__strcasecmp (step
->__to_name
, "UTF-16LE//") == 0)
144 result
= __GCONV_NOCONV
;
145 if (__builtin_expect (dir
, to_utf16
) != illegal_dir
)
147 new_data
= (struct utf16_data
*) malloc (sizeof (struct utf16_data
));
149 result
= __GCONV_NOMEM
;
150 if (new_data
!= NULL
)
154 new_data
->swap
= ((var
== UTF_16LE
&& BYTE_ORDER
== BIG_ENDIAN
)
156 && BYTE_ORDER
== LITTLE_ENDIAN
));
157 step
->__data
= new_data
;
159 if (dir
== from_utf16
)
161 step
->__min_needed_from
= MIN_NEEDED_FROM
;
162 step
->__max_needed_from
= MAX_NEEDED_FROM
;
163 step
->__min_needed_to
= MIN_NEEDED_TO
;
164 step
->__max_needed_to
= MIN_NEEDED_TO
;
168 step
->__min_needed_from
= MIN_NEEDED_TO
;
169 step
->__max_needed_from
= MIN_NEEDED_TO
;
170 step
->__min_needed_to
= MIN_NEEDED_FROM
;
171 step
->__max_needed_to
= MAX_NEEDED_FROM
;
174 step
->__stateful
= 0;
184 extern void gconv_end (struct __gconv_step
*data
);
186 gconv_end (struct __gconv_step
*data
)
192 /* Convert from the internal (UCS4-like) format to UTF-16. */
193 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
194 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
195 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
196 #define LOOPFCT TO_LOOP
199 uint32_t c = get32 (inptr); \
201 if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0)) \
203 /* Surrogate characters in UCS-4 input are not valid. \
204 We must catch this. If we let surrogates pass through, \
205 attackers could make a security hole exploit by \
206 synthesizing any desired plane 1-16 character. */ \
207 result = __GCONV_ILLEGAL_INPUT; \
208 if (! ignore_errors_p ()) \
217 if (__builtin_expect (c >= 0x10000, 0)) \
219 if (__builtin_expect (c >= 0x110000, 0)) \
221 STANDARD_TO_LOOP_ERR_HANDLER (4); \
224 /* Generate a surrogate character. */ \
225 if (__builtin_expect (outptr + 4 > outend, 0)) \
227 /* Overflow in the output buffer. */ \
228 result = __GCONV_FULL_OUTPUT; \
232 put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \
234 put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \
237 put16 (outptr, bswap_16 (c)); \
241 if (__builtin_expect (c >= 0x10000, 0)) \
243 if (__builtin_expect (c >= 0x110000, 0)) \
245 STANDARD_TO_LOOP_ERR_HANDLER (4); \
248 /* Generate a surrogate character. */ \
249 if (__builtin_expect (outptr + 4 > outend, 0)) \
251 /* Overflow in the output buffer. */ \
252 result = __GCONV_FULL_OUTPUT; \
256 put16 (outptr, 0xd7c0 + (c >> 10)); \
258 put16 (outptr, 0xdc00 + (c & 0x3ff)); \
266 #define LOOP_NEED_FLAGS
267 #define EXTRA_LOOP_DECLS \
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 < 0xd800, 1) || u1 > 0xdfff) \
287 /* No surrogate. */ \
288 put32 (outptr, u1); \
295 /* It's a surrogate character. At least the first word says \
297 if (__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, 0) \
308 || __builtin_expect (u2 > 0xdfff, 0)) \
310 /* This is no valid second word for a surrogate. */ \
312 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
315 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
321 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
323 /* No surrogate. */ \
324 put32 (outptr, u1); \
329 /* It's a surrogate character. At least the first word says \
331 if (__builtin_expect (inptr + 4 > inend, 0)) \
333 /* We don't have enough input for another complete input \
335 result = __GCONV_INCOMPLETE_INPUT; \
340 uint16_t u2 = get16 (inptr); \
341 if (__builtin_expect (u2 < 0xdc00, 0) \
342 || __builtin_expect (u2 > 0xdfff, 0)) \
344 /* This is no valid second word for a surrogate. */ \
346 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
349 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
355 #define LOOP_NEED_FLAGS
356 #define EXTRA_LOOP_DECLS \
358 #include <iconv/loop.c>
361 /* Now define the toplevel functions. */
362 #include <iconv/skeleton.c>