Update.
[glibc.git] / iconvdata / unicode.c
blob7798ae330b64b5012ebde4b34c25af549fda726e
1 /* Conversion module for Unicode
2 Copyright (C) 1999, 2000, 2001 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
19 02111-1307 USA. */
21 #include <byteswap.h>
22 #include <dlfcn.h>
23 #include <gconv.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
29 /* This is the Byte Order Mark character (BOM). */
30 #define BOM 0xfeff
31 /* And in the other endian format. */
32 #define BOM_OE 0xfffe
35 /* Definitions used in the body of the `gconv' function. */
36 #define FROM_LOOP from_unicode_loop
37 #define TO_LOOP to_unicode_loop
38 #define DEFINE_INIT 0
39 #define DEFINE_FINI 0
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; \
45 int swap; \
46 if (FROM_DIRECTION) \
47 { \
48 if (data->__invocation_counter == 0) \
49 { \
50 /* We have to find out which byte order the file is encoded in. */ \
51 if (inptr + 2 > inend) \
52 return __GCONV_EMPTY_INPUT; \
54 if (get16u (inptr) == BOM) \
55 /* Simply ignore the BOM character. */ \
56 *inptrp = inptr += 2; \
57 else if (get16u (inptr) == BOM_OE) \
58 { \
59 ((struct unicode_data *) step->__data)->swap = 1; \
60 *inptrp = inptr += 2; \
61 } \
62 } \
63 } \
64 else if (!data->__internal_use && data->__invocation_counter == 0) \
65 { \
66 /* Emit the Byte Order Mark. */ \
67 if (__builtin_expect (outbuf + 2 > outend, 0)) \
68 return __GCONV_FULL_OUTPUT; \
70 put16u (outbuf, BOM); \
71 outbuf += 2; \
72 } \
73 swap = ((struct unicode_data *) step->__data)->swap;
74 #define EXTRA_LOOP_ARGS , swap
77 /* Direction of the transformation. */
78 enum direction
80 illegal_dir,
81 to_unicode,
82 from_unicode
85 struct unicode_data
87 enum direction dir;
88 int swap;
92 extern int gconv_init (struct __gconv_step *step);
93 int
94 gconv_init (struct __gconv_step *step)
96 /* Determine which direction. */
97 struct unicode_data *new_data;
98 enum direction dir = illegal_dir;
99 int result;
101 if (strcmp (step->__from_name, "UNICODE//") == 0)
102 dir = from_unicode;
103 else
104 dir = to_unicode;
106 new_data = (struct unicode_data *) malloc (sizeof (struct unicode_data));
108 result = __GCONV_NOMEM;
109 if (new_data != NULL)
111 new_data->dir = dir;
112 new_data->swap = 0;
113 step->__data = new_data;
115 if (dir == from_unicode)
117 step->__min_needed_from = MIN_NEEDED_FROM;
118 step->__max_needed_from = MIN_NEEDED_FROM;
119 step->__min_needed_to = MIN_NEEDED_TO;
120 step->__max_needed_to = MIN_NEEDED_TO;
122 else
124 step->__min_needed_from = MIN_NEEDED_TO;
125 step->__max_needed_from = MIN_NEEDED_TO;
126 step->__min_needed_to = MIN_NEEDED_FROM;
127 step->__max_needed_to = MIN_NEEDED_FROM;
130 step->__stateful = 0;
132 result = __GCONV_OK;
135 return result;
139 extern void gconv_end (struct __gconv_step *data);
140 void
141 gconv_end (struct __gconv_step *data)
143 free (data->__data);
147 /* Convert from the internal (UCS4-like) format to UCS2. */
148 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
149 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
150 #define LOOPFCT TO_LOOP
151 #define BODY \
153 uint32_t c = get32 (inptr); \
155 if (__builtin_expect (c >= 0x10000, 0)) \
157 UNICODE_TAG_HANDLER (c, 4); \
158 STANDARD_ERR_HANDLER (4); \
160 else if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0)) \
162 /* Surrogate characters in UCS-4 input are not valid. \
163 We must catch this, because the UCS-2 output might be \
164 interpreted as UTF-16 by other programs. If we let \
165 surrogates pass through, attackers could make a security \
166 hole exploit by synthesizing any desired plane 1-16 \
167 character. */ \
168 if (! ignore_errors_p ()) \
170 result = __GCONV_ILLEGAL_INPUT; \
171 break; \
173 inptr += 4; \
174 ++*irreversible; \
175 continue; \
177 else \
179 put16 (outptr, c); \
180 outptr += 2; \
183 inptr += 4; \
185 #define LOOP_NEED_FLAGS
186 #define EXTRA_LOOP_DECLS \
187 , int swap
188 #include <iconv/loop.c>
191 /* Convert from UCS2 to the internal (UCS4-like) format. */
192 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
193 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
194 #define LOOPFCT FROM_LOOP
195 #define BODY \
197 uint16_t u1 = get16 (inptr); \
199 if (swap) \
200 u1 = bswap_16 (u1); \
202 if (__builtin_expect (u1 >= 0xd800 && u1 < 0xe000, 0)) \
204 /* Surrogate characters in UCS-2 input are not valid. Reject \
205 them. (Catching this here is not security relevant.) */ \
206 if (! ignore_errors_p ()) \
208 result = __GCONV_ILLEGAL_INPUT; \
209 break; \
211 inptr += 2; \
212 ++*irreversible; \
213 continue; \
216 put32 (outptr, u1); \
218 inptr += 2; \
219 outptr += 4; \
221 #define LOOP_NEED_FLAGS
222 #define EXTRA_LOOP_DECLS \
223 , int swap
224 #include <iconv/loop.c>
227 /* Now define the toplevel functions. */
228 #include <iconv/skeleton.c>