1 /* Conversion module for Unicode
2 Copyright (C) 1999, 2000-2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
14 Lesser General Public License for more details.
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
29 /* This is the Byte Order Mark character (BOM). */
31 /* And in the other endian format. */
35 /* Definitions used in the body of the `gconv' function. */
36 #define FROM_LOOP from_unicode_loop
37 #define TO_LOOP to_unicode_loop
40 #define MIN_NEEDED_FROM 2
41 #define MIN_NEEDED_TO 4
42 #define FROM_DIRECTION (dir == from_unicode)
43 #define PREPARE_LOOP \
44 enum direction dir = ((struct unicode_data *) step->__data)->dir; \
48 if (data->__invocation_counter == 0) \
50 /* We have to find out which byte order the file is encoded in. */ \
51 if (inptr + 2 > inend) \
52 return (inptr == inend \
53 ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT); \
55 if (get16u (inptr) == BOM) \
56 /* Simply ignore the BOM character. */ \
57 *inptrp = inptr += 2; \
58 else if (get16u (inptr) == BOM_OE) \
60 ((struct unicode_data *) step->__data)->swap = 1; \
61 *inptrp = inptr += 2; \
65 else if (!data->__internal_use && data->__invocation_counter == 0) \
67 /* Emit the Byte Order Mark. */ \
68 if (__builtin_expect (outbuf + 2 > outend, 0)) \
69 return __GCONV_FULL_OUTPUT; \
71 put16u (outbuf, BOM); \
74 swap = ((struct unicode_data *) step->__data)->swap;
75 #define EXTRA_LOOP_ARGS , swap
78 /* Direction of the transformation. */
93 extern int gconv_init (struct __gconv_step
*step
);
95 gconv_init (struct __gconv_step
*step
)
97 /* Determine which direction. */
98 struct unicode_data
*new_data
;
99 enum direction dir
= illegal_dir
;
102 if (strcmp (step
->__from_name
, "UNICODE//") == 0)
107 new_data
= (struct unicode_data
*) malloc (sizeof (struct unicode_data
));
109 result
= __GCONV_NOMEM
;
110 if (new_data
!= NULL
)
114 step
->__data
= new_data
;
116 if (dir
== from_unicode
)
118 step
->__min_needed_from
= MIN_NEEDED_FROM
;
119 step
->__max_needed_from
= MIN_NEEDED_FROM
;
120 step
->__min_needed_to
= MIN_NEEDED_TO
;
121 step
->__max_needed_to
= MIN_NEEDED_TO
;
125 step
->__min_needed_from
= MIN_NEEDED_TO
;
126 step
->__max_needed_from
= MIN_NEEDED_TO
;
127 step
->__min_needed_to
= MIN_NEEDED_FROM
;
128 step
->__max_needed_to
= MIN_NEEDED_FROM
;
131 step
->__stateful
= 0;
140 extern void gconv_end (struct __gconv_step
*data
);
142 gconv_end (struct __gconv_step
*data
)
148 /* Convert from the internal (UCS4-like) format to UCS2. */
149 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
150 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
151 #define LOOPFCT TO_LOOP
154 uint32_t c = get32 (inptr); \
156 if (__builtin_expect (c >= 0x10000, 0)) \
158 UNICODE_TAG_HANDLER (c, 4); \
159 STANDARD_TO_LOOP_ERR_HANDLER (4); \
161 else if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0)) \
163 /* Surrogate characters in UCS-4 input are not valid. \
164 We must catch this, because the UCS-2 output might be \
165 interpreted as UTF-16 by other programs. If we let \
166 surrogates pass through, attackers could make a security \
167 hole exploit by synthesizing any desired plane 1-16 \
169 result = __GCONV_ILLEGAL_INPUT; \
170 if (! ignore_errors_p ()) \
184 #define LOOP_NEED_FLAGS
185 #define EXTRA_LOOP_DECLS \
187 #include <iconv/loop.c>
190 /* Convert from UCS2 to the internal (UCS4-like) format. */
191 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
192 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
193 #define LOOPFCT FROM_LOOP
196 uint16_t u1 = get16 (inptr); \
199 u1 = bswap_16 (u1); \
201 if (__builtin_expect (u1 >= 0xd800 && u1 < 0xe000, 0)) \
203 /* Surrogate characters in UCS-2 input are not valid. Reject \
204 them. (Catching this here is not security relevant.) */ \
205 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
208 put32 (outptr, u1); \
213 #define LOOP_NEED_FLAGS
214 #define EXTRA_LOOP_DECLS \
216 #include <iconv/loop.c>
219 /* Now define the toplevel functions. */
220 #include <iconv/skeleton.c>