Use '%z' instead of '%Z' on printf functions
[glibc.git] / iconvdata / utf-16.c
blob5c02022fe535e3f79ff0af017ece9c588b1c7498
1 /* Conversion module for UTF-16.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <byteswap.h>
20 #include <dlfcn.h>
21 #include <gconv.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
27 /* This is the Byte Order Mark character (BOM). */
28 #define BOM 0xfeff
29 /* And in the other byte order. */
30 #define BOM_OE 0xfffe
33 /* Definitions used in the body of the `gconv' function. */
34 #define FROM_LOOP from_utf16_loop
35 #define TO_LOOP to_utf16_loop
36 #define DEFINE_INIT 0
37 #define DEFINE_FINI 0
38 #define MIN_NEEDED_FROM 2
39 #define MAX_NEEDED_FROM 4
40 #define MIN_NEEDED_TO 4
41 #define ONE_DIRECTION 0
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 if (__glibc_unlikely (data->__invocation_counter == 0)) \
47 { \
48 if (var == UTF_16) \
49 { \
50 if (FROM_DIRECTION) \
51 { \
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); \
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 } \
66 } \
67 else if (!FROM_DIRECTION && !data->__internal_use) \
68 { \
69 /* Emit the Byte Order Mark. */ \
70 if (__glibc_unlikely (outbuf + 2 > outend)) \
71 return __GCONV_FULL_OUTPUT; \
73 put16u (outbuf, BOM); \
74 outbuf += 2; \
75 } \
76 } \
77 else if ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN) \
78 || (var == UTF_16BE && BYTE_ORDER == LITTLE_ENDIAN)) \
79 data->__flags |= __GCONV_SWAP; \
80 } \
81 const int swap = data->__flags & __GCONV_SWAP;
82 #define EXTRA_LOOP_ARGS , swap
85 /* Direction of the transformation. */
86 enum direction
88 illegal_dir,
89 to_utf16,
90 from_utf16
93 enum variant
95 illegal_var,
96 UTF_16,
97 UTF_16LE,
98 UTF_16BE
101 struct utf16_data
103 enum direction dir;
104 enum variant var;
108 extern int gconv_init (struct __gconv_step *step);
110 gconv_init (struct __gconv_step *step)
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;
118 if (__strcasecmp (step->__from_name, "UTF-16//") == 0)
120 dir = from_utf16;
121 var = UTF_16;
123 else if (__strcasecmp (step->__to_name, "UTF-16//") == 0)
125 dir = to_utf16;
126 var = UTF_16;
128 else if (__strcasecmp (step->__from_name, "UTF-16BE//") == 0)
130 dir = from_utf16;
131 var = UTF_16BE;
133 else if (__strcasecmp (step->__to_name, "UTF-16BE//") == 0)
135 dir = to_utf16;
136 var = UTF_16BE;
138 else if (__strcasecmp (step->__from_name, "UTF-16LE//") == 0)
140 dir = from_utf16;
141 var = UTF_16LE;
143 else if (__strcasecmp (step->__to_name, "UTF-16LE//") == 0)
145 dir = to_utf16;
146 var = UTF_16LE;
149 result = __GCONV_NOCONV;
150 if (__builtin_expect (dir, to_utf16) != illegal_dir)
152 new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data));
154 result = __GCONV_NOMEM;
155 if (new_data != NULL)
157 new_data->dir = dir;
158 new_data->var = var;
159 step->__data = new_data;
161 if (dir == from_utf16)
163 step->__min_needed_from = MIN_NEEDED_FROM;
164 step->__max_needed_from = MAX_NEEDED_FROM;
165 step->__min_needed_to = MIN_NEEDED_TO;
166 step->__max_needed_to = MIN_NEEDED_TO;
168 else
170 step->__min_needed_from = MIN_NEEDED_TO;
171 step->__max_needed_from = MIN_NEEDED_TO;
172 step->__min_needed_to = MIN_NEEDED_FROM;
173 step->__max_needed_to = MAX_NEEDED_FROM;
176 step->__stateful = 0;
178 result = __GCONV_OK;
182 return result;
186 extern void gconv_end (struct __gconv_step *data);
187 void
188 gconv_end (struct __gconv_step *data)
190 free (data->__data);
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 \
201 uint32_t c = get32 (inptr); \
203 if (__glibc_unlikely (c >= 0xd800 && c < 0xe000)) \
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. */ \
209 result = __GCONV_ILLEGAL_INPUT; \
210 if (! ignore_errors_p ()) \
211 break; \
212 inptr += 4; \
213 ++*irreversible; \
214 continue; \
217 if (swap) \
219 if (__glibc_unlikely (c >= 0x10000)) \
221 if (__glibc_unlikely (c >= 0x110000)) \
223 STANDARD_TO_LOOP_ERR_HANDLER (4); \
226 /* Generate a surrogate character. */ \
227 if (__glibc_unlikely (outptr + 4 > outend)) \
229 /* Overflow in the output buffer. */ \
230 result = __GCONV_FULL_OUTPUT; \
231 break; \
234 put16 (outptr, bswap_16 (0xd7c0 + (c >> 10))); \
235 outptr += 2; \
236 put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff))); \
238 else \
239 put16 (outptr, bswap_16 (c)); \
241 else \
243 if (__glibc_unlikely (c >= 0x10000)) \
245 if (__glibc_unlikely (c >= 0x110000)) \
247 STANDARD_TO_LOOP_ERR_HANDLER (4); \
250 /* Generate a surrogate character. */ \
251 if (__glibc_unlikely (outptr + 4 > outend)) \
253 /* Overflow in the output buffer. */ \
254 result = __GCONV_FULL_OUTPUT; \
255 break; \
258 put16 (outptr, 0xd7c0 + (c >> 10)); \
259 outptr += 2; \
260 put16 (outptr, 0xdc00 + (c & 0x3ff)); \
262 else \
263 put16 (outptr, c); \
265 outptr += 2; \
266 inptr += 4; \
268 #define LOOP_NEED_FLAGS
269 #define EXTRA_LOOP_DECLS \
270 , int swap
271 #include <iconv/loop.c>
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 \
281 uint16_t u1 = get16 (inptr); \
283 if (swap) \
285 u1 = bswap_16 (u1); \
287 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
289 /* No surrogate. */ \
290 put32 (outptr, u1); \
291 inptr += 2; \
293 else \
295 uint16_t u2; \
297 if (__glibc_unlikely (u1 >= 0xdc00)) \
299 /* This is no valid first word for a surrogate. */ \
300 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
303 /* It's a surrogate character. At least the first word says \
304 it is. */ \
305 if (__glibc_unlikely (inptr + 4 > inend)) \
307 /* We don't have enough input for another complete input \
308 character. */ \
309 result = __GCONV_INCOMPLETE_INPUT; \
310 break; \
313 inptr += 2; \
314 u2 = bswap_16 (get16 (inptr)); \
315 if (__builtin_expect (u2 < 0xdc00, 0) \
316 || __builtin_expect (u2 > 0xdfff, 0)) \
318 /* This is no valid second word for a surrogate. */ \
319 inptr -= 2; \
320 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
323 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
324 inptr += 2; \
327 else \
329 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
331 /* No surrogate. */ \
332 put32 (outptr, u1); \
333 inptr += 2; \
335 else \
337 if (__glibc_unlikely (u1 >= 0xdc00)) \
339 /* This is no valid first word for a surrogate. */ \
340 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
343 /* It's a surrogate character. At least the first word says \
344 it is. */ \
345 if (__glibc_unlikely (inptr + 4 > inend)) \
347 /* We don't have enough input for another complete input \
348 character. */ \
349 result = __GCONV_INCOMPLETE_INPUT; \
350 break; \
353 inptr += 2; \
354 uint16_t u2 = get16 (inptr); \
355 if (__builtin_expect (u2 < 0xdc00, 0) \
356 || __builtin_expect (u2 > 0xdfff, 0)) \
358 /* This is no valid second word for a surrogate. */ \
359 inptr -= 2; \
360 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
363 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
364 inptr += 2; \
367 outptr += 4; \
369 #define LOOP_NEED_FLAGS
370 #define EXTRA_LOOP_DECLS \
371 , int swap
372 #include <iconv/loop.c>
375 /* Now define the toplevel functions. */
376 #include <iconv/skeleton.c>