]>
Commit | Line | Data |
---|---|---|
7cdd956e | 1 | /* Conversion module for UTF-16. |
b721a2c0 | 2 | Copyright (C) 1999, 2000, 2001 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; \ | |
b721a2c0 | 47 | int swap; \ |
59a8849d | 48 | if (FROM_DIRECTION && var == UTF_16) \ |
8d617a71 UD |
49 | { \ |
50 | if (data->__invocation_counter == 0) \ | |
51 | { \ | |
52 | /* We have to find out which byte order the file is encoded in. */ \ | |
fd1b5c0f | 53 | if (inptr + 2 > inend) \ |
8d617a71 UD |
54 | return __GCONV_EMPTY_INPUT; \ |
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 | } \ | |
65 | } \ | |
66 | else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \ | |
67 | && data->__invocation_counter == 0) \ | |
7cdd956e UD |
68 | { \ |
69 | /* Emit the Byte Order Mark. */ \ | |
ee4ce289 | 70 | if (__builtin_expect (outbuf + 2 > outend, 0)) \ |
7cdd956e UD |
71 | return __GCONV_FULL_OUTPUT; \ |
72 | \ | |
77e1d15a | 73 | put16u (outbuf, BOM); \ |
7cdd956e | 74 | outbuf += 2; \ |
b721a2c0 UD |
75 | } \ |
76 | swap = ((struct utf16_data *) step->__data)->swap; | |
55985355 | 77 | #define EXTRA_LOOP_ARGS , var, swap |
7cdd956e UD |
78 | |
79 | ||
80 | /* Direction of the transformation. */ | |
81 | enum direction | |
82 | { | |
83 | illegal_dir, | |
84 | to_utf16, | |
85 | from_utf16 | |
86 | }; | |
87 | ||
88 | enum variant | |
89 | { | |
90 | illegal_var, | |
91 | UTF_16, | |
92 | UTF_16LE, | |
93 | UTF_16BE | |
94 | }; | |
95 | ||
96 | struct utf16_data | |
97 | { | |
98 | enum direction dir; | |
99 | enum variant var; | |
8d617a71 | 100 | int swap; |
7cdd956e UD |
101 | }; |
102 | ||
103 | ||
8c0b7170 | 104 | extern int gconv_init (struct __gconv_step *step); |
7cdd956e UD |
105 | int |
106 | gconv_init (struct __gconv_step *step) | |
107 | { | |
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; | |
113 | ||
755104ed | 114 | if (__strcasecmp (step->__from_name, "UTF-16//") == 0) |
7cdd956e UD |
115 | { |
116 | dir = from_utf16; | |
117 | var = UTF_16; | |
118 | } | |
755104ed | 119 | else if (__strcasecmp (step->__to_name, "UTF-16//") == 0) |
7cdd956e UD |
120 | { |
121 | dir = to_utf16; | |
122 | var = UTF_16; | |
123 | } | |
755104ed | 124 | else if (__strcasecmp (step->__from_name, "UTF-16BE//") == 0) |
7cdd956e UD |
125 | { |
126 | dir = from_utf16; | |
127 | var = UTF_16BE; | |
128 | } | |
755104ed | 129 | else if (__strcasecmp (step->__to_name, "UTF-16BE//") == 0) |
7cdd956e UD |
130 | { |
131 | dir = to_utf16; | |
132 | var = UTF_16BE; | |
133 | } | |
755104ed | 134 | else if (__strcasecmp (step->__from_name, "UTF-16LE//") == 0) |
7cdd956e UD |
135 | { |
136 | dir = from_utf16; | |
137 | var = UTF_16LE; | |
138 | } | |
755104ed | 139 | else if (__strcasecmp (step->__to_name, "UTF-16LE//") == 0) |
7cdd956e UD |
140 | { |
141 | dir = to_utf16; | |
142 | var = UTF_16LE; | |
143 | } | |
144 | ||
145 | result = __GCONV_NOCONV; | |
ee4ce289 | 146 | if (__builtin_expect (dir, to_utf16) != illegal_dir) |
7cdd956e UD |
147 | { |
148 | new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data)); | |
149 | ||
150 | result = __GCONV_NOMEM; | |
151 | if (new_data != NULL) | |
152 | { | |
153 | new_data->dir = dir; | |
154 | new_data->var = var; | |
8d617a71 UD |
155 | new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN) |
156 | || (var == UTF_16BE | |
157 | && BYTE_ORDER == LITTLE_ENDIAN)); | |
7cdd956e UD |
158 | step->__data = new_data; |
159 | ||
bc4831b9 | 160 | if (dir == from_utf16) |
7cdd956e UD |
161 | { |
162 | step->__min_needed_from = MIN_NEEDED_FROM; | |
b721a2c0 | 163 | step->__max_needed_from = MAX_NEEDED_FROM; |
7cdd956e UD |
164 | step->__min_needed_to = MIN_NEEDED_TO; |
165 | step->__max_needed_to = MIN_NEEDED_TO; | |
166 | } | |
167 | else | |
168 | { | |
169 | step->__min_needed_from = MIN_NEEDED_TO; | |
170 | step->__max_needed_from = MIN_NEEDED_TO; | |
171 | step->__min_needed_to = MIN_NEEDED_FROM; | |
b721a2c0 | 172 | step->__max_needed_to = MAX_NEEDED_FROM; |
7cdd956e UD |
173 | } |
174 | ||
175 | step->__stateful = 0; | |
176 | ||
177 | result = __GCONV_OK; | |
178 | } | |
179 | } | |
180 | ||
181 | return result; | |
182 | } | |
183 | ||
184 | ||
8c0b7170 | 185 | extern void gconv_end (struct __gconv_step *data); |
7cdd956e UD |
186 | void |
187 | gconv_end (struct __gconv_step *data) | |
188 | { | |
189 | free (data->__data); | |
190 | } | |
191 | ||
192 | ||
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 \ | |
199 | { \ | |
77e1d15a | 200 | uint32_t c = get32 (inptr); \ |
7cdd956e | 201 | \ |
755104ed UD |
202 | if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0)) \ |
203 | { \ | |
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 | if (! ignore_errors_p ()) \ | |
209 | { \ | |
210 | result = __GCONV_ILLEGAL_INPUT; \ | |
211 | break; \ | |
212 | } \ | |
213 | inptr += 4; \ | |
214 | ++*irreversible; \ | |
215 | continue; \ | |
216 | } \ | |
217 | \ | |
8d617a71 | 218 | if (swap) \ |
7cdd956e | 219 | { \ |
ee4ce289 | 220 | if (__builtin_expect (c, 0) >= 0x10000) \ |
7cdd956e | 221 | { \ |
ee4ce289 | 222 | if (__builtin_expect (c, 0) >= 0x110000) \ |
7cdd956e | 223 | { \ |
d6204268 | 224 | STANDARD_ERR_HANDLER (4); \ |
7cdd956e UD |
225 | } \ |
226 | \ | |
227 | /* Generate a surrogate character. */ \ | |
55985355 | 228 | if (__builtin_expect (outptr + 4 > outend, 0)) \ |
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 | { \ | |
ee4ce289 | 244 | if (__builtin_expect (c, 0) >= 0x10000) \ |
7cdd956e | 245 | { \ |
ee4ce289 | 246 | if (__builtin_expect (c, 0) >= 0x110000) \ |
7cdd956e | 247 | { \ |
d6204268 | 248 | STANDARD_ERR_HANDLER (4); \ |
7cdd956e UD |
249 | } \ |
250 | \ | |
251 | /* Generate a surrogate character. */ \ | |
55985355 | 252 | if (__builtin_expect (outptr + 4 > outend, 0)) \ |
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 \ |
55985355 | 271 | , enum variant var, 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 | \ | |
ee4ce289 | 288 | if (__builtin_expect (u1, 0) < 0xd800 || 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 | \ | |
298 | /* It's a surrogate character. At least the first word says \ | |
299 | it is. */ \ | |
55985355 | 300 | if (__builtin_expect (inptr + 4 > inend, 0)) \ |
7cdd956e UD |
301 | { \ |
302 | /* We don't have enough input for another complete input \ | |
303 | character. */ \ | |
304 | result = __GCONV_INCOMPLETE_INPUT; \ | |
305 | break; \ | |
306 | } \ | |
307 | \ | |
77e1d15a UD |
308 | inptr += 2; \ |
309 | u2 = bswap_16 (get16 (inptr)); \ | |
ee4ce289 UD |
310 | if (__builtin_expect (u2, 0xdc00) < 0xdc00 \ |
311 | || __builtin_expect (u2, 0xdc00) >= 0xdfff) \ | |
7cdd956e UD |
312 | { \ |
313 | /* This is no valid second word for a surrogate. */ \ | |
85830c4c UD |
314 | if (! ignore_errors_p ()) \ |
315 | { \ | |
316 | inptr -= 2; \ | |
317 | result = __GCONV_ILLEGAL_INPUT; \ | |
318 | break; \ | |
319 | } \ | |
320 | \ | |
38677ace | 321 | ++*irreversible; \ |
85830c4c | 322 | continue; \ |
7cdd956e UD |
323 | } \ |
324 | \ | |
77e1d15a UD |
325 | put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \ |
326 | inptr += 2; \ | |
7cdd956e UD |
327 | } \ |
328 | } \ | |
329 | else \ | |
330 | { \ | |
ee4ce289 | 331 | if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff) \ |
7cdd956e UD |
332 | { \ |
333 | /* No surrogate. */ \ | |
77e1d15a | 334 | put32 (outptr, u1); \ |
7cdd956e UD |
335 | inptr += 2; \ |
336 | } \ | |
337 | else \ | |
338 | { \ | |
339 | uint16_t u2; \ | |
340 | \ | |
341 | /* It's a surrogate character. At least the first word says \ | |
342 | it is. */ \ | |
55985355 | 343 | if (__builtin_expect (inptr + 4 > inend, 0)) \ |
7cdd956e UD |
344 | { \ |
345 | /* We don't have enough input for another complete input \ | |
346 | character. */ \ | |
347 | result = __GCONV_INCOMPLETE_INPUT; \ | |
348 | break; \ | |
349 | } \ | |
350 | \ | |
77e1d15a UD |
351 | inptr += 2; \ |
352 | u2 = get16 (inptr); \ | |
ee4ce289 UD |
353 | if (__builtin_expect (u2, 0xdc00) < 0xdc00 \ |
354 | || __builtin_expect (u2, 0xdc00) >= 0xdfff) \ | |
7cdd956e UD |
355 | { \ |
356 | /* This is no valid second word for a surrogate. */ \ | |
85830c4c UD |
357 | if (! ignore_errors_p ()) \ |
358 | { \ | |
359 | inptr -= 2; \ | |
360 | result = __GCONV_ILLEGAL_INPUT; \ | |
361 | break; \ | |
362 | } \ | |
363 | \ | |
38677ace | 364 | ++*irreversible; \ |
85830c4c | 365 | continue; \ |
7cdd956e UD |
366 | } \ |
367 | \ | |
77e1d15a UD |
368 | put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \ |
369 | inptr += 2; \ | |
7cdd956e UD |
370 | } \ |
371 | } \ | |
372 | outptr += 4; \ | |
373 | } | |
55985355 | 374 | #define LOOP_NEED_FLAGS |
7cdd956e | 375 | #define EXTRA_LOOP_DECLS \ |
55985355 | 376 | , enum variant var, int swap |
7cdd956e UD |
377 | #include <iconv/loop.c> |
378 | ||
379 | ||
380 | /* Now define the toplevel functions. */ | |
381 | #include <iconv/skeleton.c> |