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