New version from translator.
[glibc.git] / iconvdata / utf-16.c
bloba7a01a83022ac152d86eafca2e1d72fd4af9aa75
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. */
21 #include <byteswap.h>
22 #include <gconv.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
28 /* This is the Byte Order Mark character (BOM). */
29 #define BOM 0xfeff
30 /* And in the other byte order. */
31 #define BOM_OE 0xfffe
34 /* Definitions used in the body of the `gconv' function. */
35 #define FROM_LOOP from_utf16_loop
36 #define TO_LOOP to_utf16_loop
37 #define DEFINE_INIT 0
38 #define DEFINE_FINI 0
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) \
48 { \
49 if (data->__invocation_counter == 0) \
50 { \
51 /* We have to find out which byte order the file is encoded in. */ \
52 if (inptr + 2 > inend) \
53 return __GCONV_EMPTY_INPUT; \
55 if (get16u (inptr) == BOM) \
56 /* Simply ignore the BOM character. */ \
57 inptr += 2; \
58 else if (get16u (inptr) == BOM_OE) \
59 { \
60 ((struct utf16_data *) step->__data)->swap = 1; \
61 inptr += 2; \
62 } \
63 } \
64 } \
65 else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \
66 && data->__invocation_counter == 0) \
67 { \
68 /* Emit the Byte Order Mark. */ \
69 if (outbuf + 2 > outend) \
70 return __GCONV_FULL_OUTPUT; \
72 put16u (outbuf, BOM); \
73 outbuf += 2; \
75 #define EXTRA_LOOP_ARGS , var, data, swap
78 /* Direction of the transformation. */
79 enum direction
81 illegal_dir,
82 to_utf16,
83 from_utf16
86 enum variant
88 illegal_var,
89 UTF_16,
90 UTF_16LE,
91 UTF_16BE
94 struct utf16_data
96 enum direction dir;
97 enum variant var;
98 int swap;
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;
109 int result;
111 if (__strcasecmp (step->__from_name, "UTF-16") == 0)
113 dir = from_utf16;
114 var = UTF_16;
116 else if (__strcasecmp (step->__to_name, "UTF-16") == 0)
118 dir = to_utf16;
119 var = UTF_16;
121 else if (__strcasecmp (step->__from_name, "UTF-16BE") == 0)
123 dir = from_utf16;
124 var = UTF_16BE;
126 else if (__strcasecmp (step->__to_name, "UTF-16BE") == 0)
128 dir = to_utf16;
129 var = UTF_16BE;
131 else if (__strcasecmp (step->__from_name, "UTF-16LE") == 0)
133 dir = from_utf16;
134 var = UTF_16LE;
136 else if (__strcasecmp (step->__to_name, "UTF-16LE") == 0)
138 dir = to_utf16;
139 var = UTF_16LE;
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)
150 new_data->dir = dir;
151 new_data->var = var;
152 new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN)
153 || (var == UTF_16BE
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;
164 else
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;
174 result = __GCONV_OK;
178 return result;
182 void
183 gconv_end (struct __gconv_step *data)
185 free (data->__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
194 #define BODY \
196 uint32_t c = get32 (inptr); \
198 if (swap) \
200 if (c >= 0x10000) \
202 if (c >= 0x110000) \
204 result = __GCONV_ILLEGAL_INPUT; \
205 break; \
208 /* Generate a surrogate character. */ \
209 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
211 /* Overflow in the output buffer. */ \
212 result = __GCONV_FULL_OUTPUT; \
213 break; \
216 put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \
217 outptr += 2; \
218 put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \
220 else \
221 put16 (outptr, bswap_16 (c)); \
223 else \
225 if (c >= 0x10000) \
227 if (c >= 0x110000) \
229 result = __GCONV_ILLEGAL_INPUT; \
230 break; \
233 /* Generate a surrogate character. */ \
234 if (NEED_LENGTH_TEST && outptr + 4 > outend) \
236 /* Overflow in the output buffer. */ \
237 result = __GCONV_FULL_OUTPUT; \
238 break; \
241 put16 (outptr, 0xd7c0 + (c >> 10)); \
242 outptr += 2; \
243 put16 (outptr, 0xdc00 + (c & 0x3ff)); \
245 else \
246 put16 (outptr, c); \
248 outptr += 2; \
249 inptr += 4; \
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
261 #define BODY \
263 uint16_t u1 = get16 (inptr); \
265 if (swap) \
267 u1 = bswap_16 (u1); \
269 if (u1 < 0xd800 || u1 > 0xdfff) \
271 /* No surrogate. */ \
272 put32 (outptr, u1); \
273 inptr += 2; \
275 else \
277 uint16_t u2; \
279 /* It's a surrogate character. At least the first word says \
280 it is. */ \
281 if (NEED_LENGTH_TEST && inptr + 4 > inend) \
283 /* We don't have enough input for another complete input \
284 character. */ \
285 result = __GCONV_INCOMPLETE_INPUT; \
286 break; \
289 inptr += 2; \
290 u2 = bswap_16 (get16 (inptr)); \
291 if (u2 < 0xdc00 || u2 >= 0xdfff) \
293 /* This is no valid second word for a surrogate. */ \
294 result = __GCONV_ILLEGAL_INPUT; \
295 inptr -= 2; \
296 break; \
299 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
300 inptr += 2; \
303 else \
305 if (u1 < 0xd800 || u1 > 0xdfff) \
307 /* No surrogate. */ \
308 put32 (outptr, u1); \
309 inptr += 2; \
311 else \
313 uint16_t u2; \
315 /* It's a surrogate character. At least the first word says \
316 it is. */ \
317 if (NEED_LENGTH_TEST && inptr + 4 > inend) \
319 /* We don't have enough input for another complete input \
320 character. */ \
321 result = __GCONV_INCOMPLETE_INPUT; \
322 break; \
325 inptr += 2; \
326 u2 = get16 (inptr); \
327 if (u2 < 0xdc00 || u2 >= 0xdfff) \
329 /* This is no valid second word for a surrogate. */ \
330 result = __GCONV_ILLEGAL_INPUT; \
331 inptr -= 2; \
332 break; \
335 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
336 inptr += 2; \
339 outptr += 4; \
341 #define EXTRA_LOOP_DECLS \
342 , enum variant var, struct __gconv_step_data *step_data, int swap
343 #include <iconv/loop.c>
346 /* Now define the toplevel functions. */
347 #include <iconv/skeleton.c>