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