1 /* Conversion module for UTF-16.
2 Copyright (C) 1999-2018 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, see
18 <http://www.gnu.org/licenses/>. */
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 ONE_DIRECTION 0
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 (__glibc_unlikely (data->__invocation_counter == 0)) \
53 /* We have to find out which byte order the file is \
55 if (inptr + 2 > inend) \
56 return (inptr == inend \
57 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT); \
59 if (get16u (inptr) == BOM) \
60 /* Simply ignore the BOM character. */ \
61 *inptrp = inptr += 2; \
62 else if (get16u (inptr) == BOM_OE) \
64 data->__flags |= __GCONV_SWAP; \
65 *inptrp = inptr += 2; \
68 else if (!FROM_DIRECTION && !data->__internal_use) \
70 /* Emit the Byte Order Mark. */ \
71 if (__glibc_unlikely (outbuf + 2 > outend)) \
72 return __GCONV_FULL_OUTPUT; \
74 put16u (outbuf, BOM); \
78 else if ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN) \
79 || (var == UTF_16BE && BYTE_ORDER == LITTLE_ENDIAN)) \
80 data->__flags |= __GCONV_SWAP; \
82 const int swap = data->__flags & __GCONV_SWAP;
83 #define EXTRA_LOOP_ARGS , swap
86 /* Direction of the transformation. */
109 extern int gconv_init (struct __gconv_step
*step
);
111 gconv_init (struct __gconv_step
*step
)
113 /* Determine which direction. */
114 struct utf16_data
*new_data
;
115 enum direction dir
= illegal_dir
;
116 enum variant var
= illegal_var
;
119 if (__strcasecmp (step
->__from_name
, "UTF-16//") == 0)
124 else if (__strcasecmp (step
->__to_name
, "UTF-16//") == 0)
129 else if (__strcasecmp (step
->__from_name
, "UTF-16BE//") == 0)
134 else if (__strcasecmp (step
->__to_name
, "UTF-16BE//") == 0)
139 else if (__strcasecmp (step
->__from_name
, "UTF-16LE//") == 0)
144 else if (__strcasecmp (step
->__to_name
, "UTF-16LE//") == 0)
150 result
= __GCONV_NOCONV
;
151 if (__builtin_expect (dir
, to_utf16
) != illegal_dir
)
153 new_data
= (struct utf16_data
*) malloc (sizeof (struct utf16_data
));
155 result
= __GCONV_NOMEM
;
156 if (new_data
!= NULL
)
160 step
->__data
= new_data
;
162 if (dir
== from_utf16
)
164 step
->__min_needed_from
= MIN_NEEDED_FROM
;
165 step
->__max_needed_from
= MAX_NEEDED_FROM
;
166 step
->__min_needed_to
= MIN_NEEDED_TO
;
167 step
->__max_needed_to
= MIN_NEEDED_TO
;
171 step
->__min_needed_from
= MIN_NEEDED_TO
;
172 step
->__max_needed_from
= MIN_NEEDED_TO
;
173 step
->__min_needed_to
= MIN_NEEDED_FROM
;
174 step
->__max_needed_to
= MAX_NEEDED_FROM
;
177 step
->__stateful
= 0;
187 extern void gconv_end (struct __gconv_step
*data
);
189 gconv_end (struct __gconv_step
*data
)
195 /* Convert from the internal (UCS4-like) format to UTF-16. */
196 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
197 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
198 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
199 #define LOOPFCT TO_LOOP
202 uint32_t c = get32 (inptr); \
204 if (__glibc_unlikely (c >= 0xd800 && c < 0xe000)) \
206 /* Surrogate characters in UCS-4 input are not valid. \
207 We must catch this. If we let surrogates pass through, \
208 attackers could make a security hole exploit by \
209 synthesizing any desired plane 1-16 character. */ \
210 result = __GCONV_ILLEGAL_INPUT; \
211 if (! ignore_errors_p ()) \
220 if (__glibc_unlikely (c >= 0x10000)) \
222 if (__glibc_unlikely (c >= 0x110000)) \
224 STANDARD_TO_LOOP_ERR_HANDLER (4); \
227 /* Generate a surrogate character. */ \
228 if (__glibc_unlikely (outptr + 4 > outend)) \
230 /* Overflow in the output buffer. */ \
231 result = __GCONV_FULL_OUTPUT; \
235 put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \
237 put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \
240 put16 (outptr, bswap_16 (c)); \
244 if (__glibc_unlikely (c >= 0x10000)) \
246 if (__glibc_unlikely (c >= 0x110000)) \
248 STANDARD_TO_LOOP_ERR_HANDLER (4); \
251 /* Generate a surrogate character. */ \
252 if (__glibc_unlikely (outptr + 4 > outend)) \
254 /* Overflow in the output buffer. */ \
255 result = __GCONV_FULL_OUTPUT; \
259 put16 (outptr, 0xd7c0 + (c >> 10)); \
261 put16 (outptr, 0xdc00 + (c & 0x3ff)); \
269 #define LOOP_NEED_FLAGS
270 #define EXTRA_LOOP_DECLS \
272 #include <iconv/loop.c>
275 /* Convert from UTF-16 to the internal (UCS4-like) format. */
276 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
277 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
278 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
279 #define LOOPFCT FROM_LOOP
282 uint16_t u1 = get16 (inptr); \
286 u1 = bswap_16 (u1); \
288 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
290 /* No surrogate. */ \
291 put32 (outptr, u1); \
298 if (__glibc_unlikely (u1 >= 0xdc00)) \
300 /* This is no valid first word for a surrogate. */ \
301 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
304 /* It's a surrogate character. At least the first word says \
306 if (__glibc_unlikely (inptr + 4 > inend)) \
308 /* We don't have enough input for another complete input \
310 result = __GCONV_INCOMPLETE_INPUT; \
315 u2 = bswap_16 (get16 (inptr)); \
316 if (__builtin_expect (u2 < 0xdc00, 0) \
317 || __builtin_expect (u2 > 0xdfff, 0)) \
319 /* This is no valid second word for a surrogate. */ \
321 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
324 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
330 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
332 /* No surrogate. */ \
333 put32 (outptr, u1); \
338 if (__glibc_unlikely (u1 >= 0xdc00)) \
340 /* This is no valid first word for a surrogate. */ \
341 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
344 /* It's a surrogate character. At least the first word says \
346 if (__glibc_unlikely (inptr + 4 > inend)) \
348 /* We don't have enough input for another complete input \
350 result = __GCONV_INCOMPLETE_INPUT; \
355 uint16_t u2 = get16 (inptr); \
356 if (__builtin_expect (u2 < 0xdc00, 0) \
357 || __builtin_expect (u2 > 0xdfff, 0)) \
359 /* This is no valid second word for a surrogate. */ \
361 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
364 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
370 #define LOOP_NEED_FLAGS
371 #define EXTRA_LOOP_DECLS \
373 #include <iconv/loop.c>
376 /* Now define the toplevel functions. */
377 #include <iconv/skeleton.c>