]>
Commit | Line | Data |
---|---|---|
7cdd956e | 1 | /* Conversion module for UTF-16. |
dc4bb1c2 | 2 | Copyright (C) 1999, 2000-2002, 2003, 2005 Free Software Foundation, Inc. |
7cdd956e UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
7cdd956e UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
7cdd956e | 15 | |
41bdb6e2 AJ |
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. */ | |
7cdd956e UD |
20 | |
21 | #include <byteswap.h> | |
55985355 | 22 | #include <dlfcn.h> |
7cdd956e UD |
23 | #include <gconv.h> |
24 | #include <stddef.h> | |
25 | #include <stdint.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | ||
29 | /* This is the Byte Order Mark character (BOM). */ | |
30 | #define BOM 0xfeff | |
8d617a71 UD |
31 | /* And in the other byte order. */ |
32 | #define BOM_OE 0xfffe | |
7cdd956e UD |
33 | |
34 | ||
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; \ | |
dc4bb1c2 | 47 | if (__builtin_expect (data->__invocation_counter == 0, 0) && var == UTF_16) \ |
8d617a71 | 48 | { \ |
dc4bb1c2 | 49 | if (FROM_DIRECTION) \ |
8d617a71 UD |
50 | { \ |
51 | /* We have to find out which byte order the file is encoded in. */ \ | |
fd1b5c0f | 52 | if (inptr + 2 > inend) \ |
eb9dc2a2 UD |
53 | return (inptr == inend \ |
54 | ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT); \ | |
8d617a71 | 55 | \ |
77e1d15a | 56 | if (get16u (inptr) == BOM) \ |
8d617a71 | 57 | /* Simply ignore the BOM character. */ \ |
b721a2c0 | 58 | *inptrp = inptr += 2; \ |
77e1d15a | 59 | else if (get16u (inptr) == BOM_OE) \ |
8d617a71 UD |
60 | { \ |
61 | ((struct utf16_data *) step->__data)->swap = 1; \ | |
b721a2c0 | 62 | *inptrp = inptr += 2; \ |
8d617a71 UD |
63 | } \ |
64 | } \ | |
dc4bb1c2 UD |
65 | else if (!FROM_DIRECTION && !data->__internal_use) \ |
66 | { \ | |
67 | /* Emit the Byte Order Mark. */ \ | |
68 | if (__builtin_expect (outbuf + 2 > outend, 0)) \ | |
69 | return __GCONV_FULL_OUTPUT; \ | |
7cdd956e | 70 | \ |
dc4bb1c2 UD |
71 | put16u (outbuf, BOM); \ |
72 | outbuf += 2; \ | |
73 | } \ | |
b721a2c0 | 74 | } \ |
dc4bb1c2 UD |
75 | int swap = ((struct utf16_data *) step->__data)->swap; |
76 | #define EXTRA_LOOP_ARGS , swap | |
7cdd956e UD |
77 | |
78 | ||
79 | /* Direction of the transformation. */ | |
80 | enum direction | |
81 | { | |
82 | illegal_dir, | |
83 | to_utf16, | |
84 | from_utf16 | |
85 | }; | |
86 | ||
87 | enum variant | |
88 | { | |
89 | illegal_var, | |
90 | UTF_16, | |
91 | UTF_16LE, | |
92 | UTF_16BE | |
93 | }; | |
94 | ||
95 | struct utf16_data | |
96 | { | |
97 | enum direction dir; | |
98 | enum variant var; | |
8d617a71 | 99 | int swap; |
7cdd956e UD |
100 | }; |
101 | ||
102 | ||
8c0b7170 | 103 | extern int gconv_init (struct __gconv_step *step); |
7cdd956e UD |
104 | int |
105 | gconv_init (struct __gconv_step *step) | |
106 | { | |
107 | /* Determine which direction. */ | |
108 | struct utf16_data *new_data; | |
109 | enum direction dir = illegal_dir; | |
110 | enum variant var = illegal_var; | |
111 | int result; | |
112 | ||
755104ed | 113 | if (__strcasecmp (step->__from_name, "UTF-16//") == 0) |
7cdd956e UD |
114 | { |
115 | dir = from_utf16; | |
116 | var = UTF_16; | |
117 | } | |
755104ed | 118 | else if (__strcasecmp (step->__to_name, "UTF-16//") == 0) |
7cdd956e UD |
119 | { |
120 | dir = to_utf16; | |
121 | var = UTF_16; | |
122 | } | |
755104ed | 123 | else if (__strcasecmp (step->__from_name, "UTF-16BE//") == 0) |
7cdd956e UD |
124 | { |
125 | dir = from_utf16; | |
126 | var = UTF_16BE; | |
127 | } | |
755104ed | 128 | else if (__strcasecmp (step->__to_name, "UTF-16BE//") == 0) |
7cdd956e UD |
129 | { |
130 | dir = to_utf16; | |
131 | var = UTF_16BE; | |
132 | } | |
755104ed | 133 | else if (__strcasecmp (step->__from_name, "UTF-16LE//") == 0) |
7cdd956e UD |
134 | { |
135 | dir = from_utf16; | |
136 | var = UTF_16LE; | |
137 | } | |
755104ed | 138 | else if (__strcasecmp (step->__to_name, "UTF-16LE//") == 0) |
7cdd956e UD |
139 | { |
140 | dir = to_utf16; | |
141 | var = UTF_16LE; | |
142 | } | |
143 | ||
144 | result = __GCONV_NOCONV; | |
ee4ce289 | 145 | if (__builtin_expect (dir, to_utf16) != illegal_dir) |
7cdd956e UD |
146 | { |
147 | new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data)); | |
148 | ||
149 | result = __GCONV_NOMEM; | |
150 | if (new_data != NULL) | |
151 | { | |
152 | new_data->dir = dir; | |
153 | new_data->var = var; | |
8d617a71 UD |
154 | new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN) |
155 | || (var == UTF_16BE | |
156 | && BYTE_ORDER == LITTLE_ENDIAN)); | |
7cdd956e UD |
157 | step->__data = new_data; |
158 | ||
bc4831b9 | 159 | if (dir == from_utf16) |
7cdd956e UD |
160 | { |
161 | step->__min_needed_from = MIN_NEEDED_FROM; | |
b721a2c0 | 162 | step->__max_needed_from = MAX_NEEDED_FROM; |
7cdd956e UD |
163 | step->__min_needed_to = MIN_NEEDED_TO; |
164 | step->__max_needed_to = MIN_NEEDED_TO; | |
165 | } | |
166 | else | |
167 | { | |
168 | step->__min_needed_from = MIN_NEEDED_TO; | |
169 | step->__max_needed_from = MIN_NEEDED_TO; | |
170 | step->__min_needed_to = MIN_NEEDED_FROM; | |
b721a2c0 | 171 | step->__max_needed_to = MAX_NEEDED_FROM; |
7cdd956e UD |
172 | } |
173 | ||
174 | step->__stateful = 0; | |
175 | ||
176 | result = __GCONV_OK; | |
177 | } | |
178 | } | |
179 | ||
180 | return result; | |
181 | } | |
182 | ||
183 | ||
8c0b7170 | 184 | extern void gconv_end (struct __gconv_step *data); |
7cdd956e UD |
185 | void |
186 | gconv_end (struct __gconv_step *data) | |
187 | { | |
188 | free (data->__data); | |
189 | } | |
190 | ||
191 | ||
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 | |
197 | #define BODY \ | |
198 | { \ | |
77e1d15a | 199 | uint32_t c = get32 (inptr); \ |
7cdd956e | 200 | \ |
755104ed UD |
201 | if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0)) \ |
202 | { \ | |
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. */ \ | |
e438a468 | 207 | result = __GCONV_ILLEGAL_INPUT; \ |
755104ed | 208 | if (! ignore_errors_p ()) \ |
e438a468 | 209 | break; \ |
755104ed UD |
210 | inptr += 4; \ |
211 | ++*irreversible; \ | |
212 | continue; \ | |
213 | } \ | |
214 | \ | |
8d617a71 | 215 | if (swap) \ |
7cdd956e | 216 | { \ |
db2d05f9 | 217 | if (__builtin_expect (c >= 0x10000, 0)) \ |
7cdd956e | 218 | { \ |
db2d05f9 | 219 | if (__builtin_expect (c >= 0x110000, 0)) \ |
7cdd956e | 220 | { \ |
e438a468 | 221 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
7cdd956e UD |
222 | } \ |
223 | \ | |
224 | /* Generate a surrogate character. */ \ | |
55985355 | 225 | if (__builtin_expect (outptr + 4 > outend, 0)) \ |
7cdd956e UD |
226 | { \ |
227 | /* Overflow in the output buffer. */ \ | |
228 | result = __GCONV_FULL_OUTPUT; \ | |
229 | break; \ | |
230 | } \ | |
231 | \ | |
77e1d15a | 232 | put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \ |
7cdd956e | 233 | outptr += 2; \ |
77e1d15a | 234 | put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \ |
7cdd956e UD |
235 | } \ |
236 | else \ | |
77e1d15a | 237 | put16 (outptr, bswap_16 (c)); \ |
7cdd956e UD |
238 | } \ |
239 | else \ | |
240 | { \ | |
db2d05f9 | 241 | if (__builtin_expect (c >= 0x10000, 0)) \ |
7cdd956e | 242 | { \ |
db2d05f9 | 243 | if (__builtin_expect (c >= 0x110000, 0)) \ |
7cdd956e | 244 | { \ |
e438a468 | 245 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
7cdd956e UD |
246 | } \ |
247 | \ | |
248 | /* Generate a surrogate character. */ \ | |
55985355 | 249 | if (__builtin_expect (outptr + 4 > outend, 0)) \ |
7cdd956e UD |
250 | { \ |
251 | /* Overflow in the output buffer. */ \ | |
252 | result = __GCONV_FULL_OUTPUT; \ | |
253 | break; \ | |
254 | } \ | |
255 | \ | |
77e1d15a | 256 | put16 (outptr, 0xd7c0 + (c >> 10)); \ |
7cdd956e | 257 | outptr += 2; \ |
77e1d15a | 258 | put16 (outptr, 0xdc00 + (c & 0x3ff)); \ |
7cdd956e UD |
259 | } \ |
260 | else \ | |
77e1d15a | 261 | put16 (outptr, c); \ |
7cdd956e UD |
262 | } \ |
263 | outptr += 2; \ | |
264 | inptr += 4; \ | |
265 | } | |
55985355 | 266 | #define LOOP_NEED_FLAGS |
7cdd956e | 267 | #define EXTRA_LOOP_DECLS \ |
dc4bb1c2 | 268 | , int swap |
7cdd956e UD |
269 | #include <iconv/loop.c> |
270 | ||
271 | ||
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 | |
277 | #define BODY \ | |
278 | { \ | |
77e1d15a | 279 | uint16_t u1 = get16 (inptr); \ |
7cdd956e | 280 | \ |
8d617a71 | 281 | if (swap) \ |
7cdd956e UD |
282 | { \ |
283 | u1 = bswap_16 (u1); \ | |
284 | \ | |
db2d05f9 | 285 | if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \ |
7cdd956e UD |
286 | { \ |
287 | /* No surrogate. */ \ | |
ee4ce289 | 288 | put32 (outptr, u1); \ |
7cdd956e UD |
289 | inptr += 2; \ |
290 | } \ | |
291 | else \ | |
292 | { \ | |
293 | uint16_t u2; \ | |
294 | \ | |
295 | /* It's a surrogate character. At least the first word says \ | |
296 | it is. */ \ | |
55985355 | 297 | if (__builtin_expect (inptr + 4 > inend, 0)) \ |
7cdd956e UD |
298 | { \ |
299 | /* We don't have enough input for another complete input \ | |
300 | character. */ \ | |
301 | result = __GCONV_INCOMPLETE_INPUT; \ | |
302 | break; \ | |
303 | } \ | |
304 | \ | |
77e1d15a UD |
305 | inptr += 2; \ |
306 | u2 = bswap_16 (get16 (inptr)); \ | |
db2d05f9 | 307 | if (__builtin_expect (u2 < 0xdc00, 0) \ |
5be072b6 | 308 | || __builtin_expect (u2 > 0xdfff, 0)) \ |
7cdd956e UD |
309 | { \ |
310 | /* This is no valid second word for a surrogate. */ \ | |
e438a468 UD |
311 | inptr -= 2; \ |
312 | STANDARD_FROM_LOOP_ERR_HANDLER (2); \ | |
7cdd956e UD |
313 | } \ |
314 | \ | |
77e1d15a UD |
315 | put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \ |
316 | inptr += 2; \ | |
7cdd956e UD |
317 | } \ |
318 | } \ | |
319 | else \ | |
320 | { \ | |
db2d05f9 | 321 | if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \ |
7cdd956e UD |
322 | { \ |
323 | /* No surrogate. */ \ | |
77e1d15a | 324 | put32 (outptr, u1); \ |
7cdd956e UD |
325 | inptr += 2; \ |
326 | } \ | |
327 | else \ | |
328 | { \ | |
7cdd956e UD |
329 | /* It's a surrogate character. At least the first word says \ |
330 | it is. */ \ | |
55985355 | 331 | if (__builtin_expect (inptr + 4 > inend, 0)) \ |
7cdd956e UD |
332 | { \ |
333 | /* We don't have enough input for another complete input \ | |
334 | character. */ \ | |
335 | result = __GCONV_INCOMPLETE_INPUT; \ | |
336 | break; \ | |
337 | } \ | |
338 | \ | |
77e1d15a | 339 | inptr += 2; \ |
dc4bb1c2 | 340 | uint16_t u2 = get16 (inptr); \ |
db2d05f9 | 341 | if (__builtin_expect (u2 < 0xdc00, 0) \ |
5be072b6 | 342 | || __builtin_expect (u2 > 0xdfff, 0)) \ |
7cdd956e UD |
343 | { \ |
344 | /* This is no valid second word for a surrogate. */ \ | |
e438a468 UD |
345 | inptr -= 2; \ |
346 | STANDARD_FROM_LOOP_ERR_HANDLER (2); \ | |
7cdd956e UD |
347 | } \ |
348 | \ | |
77e1d15a UD |
349 | put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \ |
350 | inptr += 2; \ | |
7cdd956e UD |
351 | } \ |
352 | } \ | |
353 | outptr += 4; \ | |
354 | } | |
55985355 | 355 | #define LOOP_NEED_FLAGS |
7cdd956e | 356 | #define EXTRA_LOOP_DECLS \ |
dc4bb1c2 | 357 | , int swap |
7cdd956e UD |
358 | #include <iconv/loop.c> |
359 | ||
360 | ||
361 | /* Now define the toplevel functions. */ | |
362 | #include <iconv/skeleton.c> |