Update.
[glibc.git] / iconvdata / utf-16.c
blob642340611d287e6c9f01e707bb62ed6d215b3b1f
1 /* Conversion module for UTF-16.
2 Copyright (C) 1999, 2000-2002 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
19 02111-1307 USA. */
21 #include <byteswap.h>
22 #include <dlfcn.h>
23 #include <gconv.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
29 /* This is the Byte Order Mark character (BOM). */
30 #define BOM 0xfeff
31 /* And in the other byte order. */
32 #define BOM_OE 0xfffe
35 /* Definitions used in the body of the `gconv' function. */
36 #define FROM_LOOP from_utf16_loop
37 #define TO_LOOP to_utf16_loop
38 #define DEFINE_INIT 0
39 #define DEFINE_FINI 0
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 int swap; \
48 if (FROM_DIRECTION && var == UTF_16) \
49 { \
50 if (data->__invocation_counter == 0) \
51 { \
52 /* We have to find out which byte order the file is encoded in. */ \
53 if (inptr + 2 > inend) \
54 return __GCONV_EMPTY_INPUT; \
56 if (get16u (inptr) == BOM) \
57 /* Simply ignore the BOM character. */ \
58 *inptrp = inptr += 2; \
59 else if (get16u (inptr) == BOM_OE) \
60 { \
61 ((struct utf16_data *) step->__data)->swap = 1; \
62 *inptrp = inptr += 2; \
63 } \
64 } \
65 } \
66 else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \
67 && data->__invocation_counter == 0) \
68 { \
69 /* Emit the Byte Order Mark. */ \
70 if (__builtin_expect (outbuf + 2 > outend, 0)) \
71 return __GCONV_FULL_OUTPUT; \
73 put16u (outbuf, BOM); \
74 outbuf += 2; \
75 } \
76 swap = ((struct utf16_data *) step->__data)->swap;
77 #define EXTRA_LOOP_ARGS , var, swap
80 /* Direction of the transformation. */
81 enum direction
83 illegal_dir,
84 to_utf16,
85 from_utf16
88 enum variant
90 illegal_var,
91 UTF_16,
92 UTF_16LE,
93 UTF_16BE
96 struct utf16_data
98 enum direction dir;
99 enum variant var;
100 int swap;
104 extern int gconv_init (struct __gconv_step *step);
106 gconv_init (struct __gconv_step *step)
108 /* Determine which direction. */
109 struct utf16_data *new_data;
110 enum direction dir = illegal_dir;
111 enum variant var = illegal_var;
112 int result;
114 if (__strcasecmp (step->__from_name, "UTF-16//") == 0)
116 dir = from_utf16;
117 var = UTF_16;
119 else if (__strcasecmp (step->__to_name, "UTF-16//") == 0)
121 dir = to_utf16;
122 var = UTF_16;
124 else if (__strcasecmp (step->__from_name, "UTF-16BE//") == 0)
126 dir = from_utf16;
127 var = UTF_16BE;
129 else if (__strcasecmp (step->__to_name, "UTF-16BE//") == 0)
131 dir = to_utf16;
132 var = UTF_16BE;
134 else if (__strcasecmp (step->__from_name, "UTF-16LE//") == 0)
136 dir = from_utf16;
137 var = UTF_16LE;
139 else if (__strcasecmp (step->__to_name, "UTF-16LE//") == 0)
141 dir = to_utf16;
142 var = UTF_16LE;
145 result = __GCONV_NOCONV;
146 if (__builtin_expect (dir, to_utf16) != illegal_dir)
148 new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data));
150 result = __GCONV_NOMEM;
151 if (new_data != NULL)
153 new_data->dir = dir;
154 new_data->var = var;
155 new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN)
156 || (var == UTF_16BE
157 && BYTE_ORDER == LITTLE_ENDIAN));
158 step->__data = new_data;
160 if (dir == from_utf16)
162 step->__min_needed_from = MIN_NEEDED_FROM;
163 step->__max_needed_from = MAX_NEEDED_FROM;
164 step->__min_needed_to = MIN_NEEDED_TO;
165 step->__max_needed_to = MIN_NEEDED_TO;
167 else
169 step->__min_needed_from = MIN_NEEDED_TO;
170 step->__max_needed_from = MIN_NEEDED_TO;
171 step->__min_needed_to = MIN_NEEDED_FROM;
172 step->__max_needed_to = MAX_NEEDED_FROM;
175 step->__stateful = 0;
177 result = __GCONV_OK;
181 return result;
185 extern void gconv_end (struct __gconv_step *data);
186 void
187 gconv_end (struct __gconv_step *data)
189 free (data->__data);
193 /* Convert from the internal (UCS4-like) format to UTF-16. */
194 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
195 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
196 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
197 #define LOOPFCT TO_LOOP
198 #define BODY \
200 uint32_t c = get32 (inptr); \
202 if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0)) \
204 /* Surrogate characters in UCS-4 input are not valid. \
205 We must catch this. If we let surrogates pass through, \
206 attackers could make a security hole exploit by \
207 synthesizing any desired plane 1-16 character. */ \
208 result = __GCONV_ILLEGAL_INPUT; \
209 if (! ignore_errors_p ()) \
210 break; \
211 inptr += 4; \
212 ++*irreversible; \
213 continue; \
216 if (swap) \
218 if (__builtin_expect (c >= 0x10000, 0)) \
220 if (__builtin_expect (c >= 0x110000, 0)) \
222 STANDARD_TO_LOOP_ERR_HANDLER (4); \
225 /* Generate a surrogate character. */ \
226 if (__builtin_expect (outptr + 4 > outend, 0)) \
228 /* Overflow in the output buffer. */ \
229 result = __GCONV_FULL_OUTPUT; \
230 break; \
233 put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \
234 outptr += 2; \
235 put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \
237 else \
238 put16 (outptr, bswap_16 (c)); \
240 else \
242 if (__builtin_expect (c >= 0x10000, 0)) \
244 if (__builtin_expect (c >= 0x110000, 0)) \
246 STANDARD_TO_LOOP_ERR_HANDLER (4); \
249 /* Generate a surrogate character. */ \
250 if (__builtin_expect (outptr + 4 > outend, 0)) \
252 /* Overflow in the output buffer. */ \
253 result = __GCONV_FULL_OUTPUT; \
254 break; \
257 put16 (outptr, 0xd7c0 + (c >> 10)); \
258 outptr += 2; \
259 put16 (outptr, 0xdc00 + (c & 0x3ff)); \
261 else \
262 put16 (outptr, c); \
264 outptr += 2; \
265 inptr += 4; \
267 #define LOOP_NEED_FLAGS
268 #define EXTRA_LOOP_DECLS \
269 , enum variant var, int swap
270 #include <iconv/loop.c>
273 /* Convert from UTF-16 to the internal (UCS4-like) format. */
274 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
275 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
276 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
277 #define LOOPFCT FROM_LOOP
278 #define BODY \
280 uint16_t u1 = get16 (inptr); \
282 if (swap) \
284 u1 = bswap_16 (u1); \
286 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
288 /* No surrogate. */ \
289 put32 (outptr, u1); \
290 inptr += 2; \
292 else \
294 uint16_t u2; \
296 /* It's a surrogate character. At least the first word says \
297 it is. */ \
298 if (__builtin_expect (inptr + 4 > inend, 0)) \
300 /* We don't have enough input for another complete input \
301 character. */ \
302 result = __GCONV_INCOMPLETE_INPUT; \
303 break; \
306 inptr += 2; \
307 u2 = bswap_16 (get16 (inptr)); \
308 if (__builtin_expect (u2 < 0xdc00, 0) \
309 || __builtin_expect (u2 == 0xdfff, 0)) \
311 /* This is no valid second word for a surrogate. */ \
312 inptr -= 2; \
313 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
316 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
317 inptr += 2; \
320 else \
322 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
324 /* No surrogate. */ \
325 put32 (outptr, u1); \
326 inptr += 2; \
328 else \
330 uint16_t u2; \
332 /* It's a surrogate character. At least the first word says \
333 it is. */ \
334 if (__builtin_expect (inptr + 4 > inend, 0)) \
336 /* We don't have enough input for another complete input \
337 character. */ \
338 result = __GCONV_INCOMPLETE_INPUT; \
339 break; \
342 inptr += 2; \
343 u2 = get16 (inptr); \
344 if (__builtin_expect (u2 < 0xdc00, 0) \
345 || __builtin_expect (u2 >= 0xdfff, 0)) \
347 /* This is no valid second word for a surrogate. */ \
348 inptr -= 2; \
349 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
352 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
353 inptr += 2; \
356 outptr += 4; \
358 #define LOOP_NEED_FLAGS
359 #define EXTRA_LOOP_DECLS \
360 , enum variant var, int swap
361 #include <iconv/loop.c>
364 /* Now define the toplevel functions. */
365 #include <iconv/skeleton.c>