Add prototypes for gconv_init and gconv_env.
[kopensolaris-gnu/glibc.git] / iconvdata / utf-16.c
1 /* Conversion module for UTF-16.
2    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
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
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
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
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
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>
28
29 /* This is the Byte Order Mark character (BOM).  */
30 #define BOM     0xfeff
31 /* And in the other byte order.  */
32 #define BOM_OE  0xfffe
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;               \
47   int swap = ((struct utf16_data *) step->__data)->swap;                      \
48   if (FROM_DIRECTION && var == UTF_16)                                        \
49     {                                                                         \
50       if (data->__invocation_counter == 0)                                    \
51         {                                                                     \
52           /* We have to find out which byte order the file is encoded in.  */ \
53           if (inptr + 2 > inend)                                              \
54             return __GCONV_EMPTY_INPUT;                                       \
55                                                                               \
56           if (get16u (inptr) == BOM)                                          \
57             /* Simply ignore the BOM character.  */                           \
58             inptr += 2;                                                       \
59           else if (get16u (inptr) == BOM_OE)                                  \
60             {                                                                 \
61               ((struct utf16_data *) step->__data)->swap = 1;                 \
62               inptr += 2;                                                     \
63             }                                                                 \
64         }                                                                     \
65     }                                                                         \
66   else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use          \
67            && data->__invocation_counter == 0)                                \
68     {                                                                         \
69       /* Emit the Byte Order Mark.  */                                        \
70       if (__builtin_expect (outbuf + 2 > outend, 0))                          \
71         return __GCONV_FULL_OUTPUT;                                           \
72                                                                               \
73       put16u (outbuf, BOM);                                                   \
74       outbuf += 2;                                                            \
75     }
76 #define EXTRA_LOOP_ARGS         , var, swap
77
78
79 /* Direction of the transformation.  */
80 enum direction
81 {
82   illegal_dir,
83   to_utf16,
84   from_utf16
85 };
86
87 enum variant
88 {
89   illegal_var,
90   UTF_16,
91   UTF_16LE,
92   UTF_16BE
93 };
94
95 struct utf16_data
96 {
97   enum direction dir;
98   enum variant var;
99   int swap;
100 };
101
102
103 extern int gconv_init (struct __gconv_step *step);
104 int
105 gconv_init (struct __gconv_step *step)
106 {
107   /* Determine which direction.  */
108   struct utf16_data *new_data;
109   enum direction dir = illegal_dir;
110   enum variant var = illegal_var;
111   int result;
112
113   if (__strcasecmp (step->__from_name, "UTF-16//") == 0)
114     {
115       dir = from_utf16;
116       var = UTF_16;
117     }
118   else if (__strcasecmp (step->__to_name, "UTF-16//") == 0)
119     {
120       dir = to_utf16;
121       var = UTF_16;
122     }
123   else if (__strcasecmp (step->__from_name, "UTF-16BE//") == 0)
124     {
125       dir = from_utf16;
126       var = UTF_16BE;
127     }
128   else if (__strcasecmp (step->__to_name, "UTF-16BE//") == 0)
129     {
130       dir = to_utf16;
131       var = UTF_16BE;
132     }
133   else if (__strcasecmp (step->__from_name, "UTF-16LE//") == 0)
134     {
135       dir = from_utf16;
136       var = UTF_16LE;
137     }
138   else if (__strcasecmp (step->__to_name, "UTF-16LE//") == 0)
139     {
140       dir = to_utf16;
141       var = UTF_16LE;
142     }
143
144   result = __GCONV_NOCONV;
145   if (__builtin_expect (dir, to_utf16) != illegal_dir)
146     {
147       new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data));
148
149       result = __GCONV_NOMEM;
150       if (new_data != NULL)
151         {
152           new_data->dir = dir;
153           new_data->var = var;
154           new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN)
155                             || (var == UTF_16BE
156                                 && BYTE_ORDER == LITTLE_ENDIAN));
157           step->__data = new_data;
158
159           if (dir == from_utf16)
160             {
161               step->__min_needed_from = MIN_NEEDED_FROM;
162               step->__max_needed_from = MIN_NEEDED_FROM;
163               step->__min_needed_to = MIN_NEEDED_TO;
164               step->__max_needed_to = MIN_NEEDED_TO;
165             }
166           else
167             {
168               step->__min_needed_from = MIN_NEEDED_TO;
169               step->__max_needed_from = MIN_NEEDED_TO;
170               step->__min_needed_to = MIN_NEEDED_FROM;
171               step->__max_needed_to = MIN_NEEDED_FROM;
172             }
173
174           step->__stateful = 0;
175
176           result = __GCONV_OK;
177         }
178     }
179
180   return result;
181 }
182
183
184 extern void gconv_end (struct __gconv_step *data);
185 void
186 gconv_end (struct __gconv_step *data)
187 {
188   free (data->__data);
189 }
190
191
192 /* Convert from the internal (UCS4-like) format to UTF-16.  */
193 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
194 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
195 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
196 #define LOOPFCT                 TO_LOOP
197 #define BODY \
198   {                                                                           \
199     uint32_t c = get32 (inptr);                                               \
200                                                                               \
201     if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0))                      \
202       {                                                                       \
203         /* Surrogate characters in UCS-4 input are not valid.                 \
204            We must catch this.  If we let surrogates pass through,            \
205            attackers could make a security hole exploit by                    \
206            synthesizing any desired plane 1-16 character.  */                 \
207         if (! ignore_errors_p ())                                             \
208           {                                                                   \
209             result = __GCONV_ILLEGAL_INPUT;                                   \
210             break;                                                            \
211           }                                                                   \
212         inptr += 4;                                                           \
213         ++*irreversible;                                                      \
214         continue;                                                             \
215       }                                                                       \
216                                                                               \
217     if (swap)                                                                 \
218       {                                                                       \
219         if (__builtin_expect (c, 0) >= 0x10000)                               \
220           {                                                                   \
221             if (__builtin_expect (c, 0) >= 0x110000)                          \
222               {                                                               \
223                 STANDARD_ERR_HANDLER (4);                                     \
224               }                                                               \
225                                                                               \
226             /* Generate a surrogate character.  */                            \
227             if (__builtin_expect (outptr + 4 > outend, 0))                    \
228               {                                                               \
229                 /* Overflow in the output buffer.  */                         \
230                 result = __GCONV_FULL_OUTPUT;                                 \
231                 break;                                                        \
232               }                                                               \
233                                                                               \
234             put16 (outptr, bswap_16 (0xd7c0 + (c >> 10)));                    \
235             outptr += 2;                                                      \
236             put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff)));                  \
237           }                                                                   \
238         else                                                                  \
239           put16 (outptr, bswap_16 (c));                                       \
240       }                                                                       \
241     else                                                                      \
242       {                                                                       \
243         if (__builtin_expect (c, 0) >= 0x10000)                               \
244           {                                                                   \
245             if (__builtin_expect (c, 0) >= 0x110000)                          \
246               {                                                               \
247                 STANDARD_ERR_HANDLER (4);                                     \
248               }                                                               \
249                                                                               \
250             /* Generate a surrogate character.  */                            \
251             if (__builtin_expect (outptr + 4 > outend, 0))                    \
252               {                                                               \
253                 /* Overflow in the output buffer.  */                         \
254                 result = __GCONV_FULL_OUTPUT;                                 \
255                 break;                                                        \
256               }                                                               \
257                                                                               \
258             put16 (outptr, 0xd7c0 + (c >> 10));                               \
259             outptr += 2;                                                      \
260             put16 (outptr, 0xdc00 + (c & 0x3ff));                             \
261           }                                                                   \
262         else                                                                  \
263           put16 (outptr, c);                                                  \
264       }                                                                       \
265     outptr += 2;                                                              \
266     inptr += 4;                                                               \
267   }
268 #define LOOP_NEED_FLAGS
269 #define EXTRA_LOOP_DECLS \
270         , enum variant var, int swap
271 #include <iconv/loop.c>
272
273
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 \
280   {                                                                           \
281     uint16_t u1 = get16 (inptr);                                              \
282                                                                               \
283     if (swap)                                                                 \
284       {                                                                       \
285         u1 = bswap_16 (u1);                                                   \
286                                                                               \
287         if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff)                 \
288           {                                                                   \
289             /* No surrogate.  */                                              \
290             put32 (outptr, u1);                                               \
291             inptr += 2;                                                       \
292           }                                                                   \
293         else                                                                  \
294           {                                                                   \
295             uint16_t u2;                                                      \
296                                                                               \
297             /* It's a surrogate character.  At least the first word says      \
298                it is.  */                                                     \
299             if (__builtin_expect (inptr + 4 > inend, 0))                      \
300               {                                                               \
301                 /* We don't have enough input for another complete input      \
302                    character.  */                                             \
303                 result = __GCONV_INCOMPLETE_INPUT;                            \
304                 break;                                                        \
305               }                                                               \
306                                                                               \
307             inptr += 2;                                                       \
308             u2 = bswap_16 (get16 (inptr));                                    \
309             if (__builtin_expect (u2, 0xdc00) < 0xdc00                        \
310                 || __builtin_expect (u2, 0xdc00) >= 0xdfff)                   \
311               {                                                               \
312                 /* This is no valid second word for a surrogate.  */          \
313                 if (! ignore_errors_p ())                                     \
314                   {                                                           \
315                     inptr -= 2;                                               \
316                     result = __GCONV_ILLEGAL_INPUT;                           \
317                     break;                                                    \
318                   }                                                           \
319                                                                               \
320                 ++*irreversible;                                              \
321                 continue;                                                     \
322               }                                                               \
323                                                                               \
324             put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00));            \
325             inptr += 2;                                                       \
326           }                                                                   \
327       }                                                                       \
328     else                                                                      \
329       {                                                                       \
330         if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff)                 \
331           {                                                                   \
332             /* No surrogate.  */                                              \
333             put32 (outptr, u1);                                               \
334             inptr += 2;                                                       \
335           }                                                                   \
336         else                                                                  \
337           {                                                                   \
338             uint16_t u2;                                                      \
339                                                                               \
340             /* It's a surrogate character.  At least the first word says      \
341                it is.  */                                                     \
342             if (__builtin_expect (inptr + 4 > inend, 0))                      \
343               {                                                               \
344                 /* We don't have enough input for another complete input      \
345                    character.  */                                             \
346                 result = __GCONV_INCOMPLETE_INPUT;                            \
347                 break;                                                        \
348               }                                                               \
349                                                                               \
350             inptr += 2;                                                       \
351             u2 = get16 (inptr);                                               \
352             if (__builtin_expect (u2, 0xdc00) < 0xdc00                        \
353                 || __builtin_expect (u2, 0xdc00) >= 0xdfff)                   \
354               {                                                               \
355                 /* This is no valid second word for a surrogate.  */          \
356                 if (! ignore_errors_p ())                                     \
357                   {                                                           \
358                     inptr -= 2;                                               \
359                     result = __GCONV_ILLEGAL_INPUT;                           \
360                     break;                                                    \
361                   }                                                           \
362                                                                               \
363                 ++*irreversible;                                              \
364                 continue;                                                     \
365               }                                                               \
366                                                                               \
367             put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00));            \
368             inptr += 2;                                                       \
369           }                                                                   \
370       }                                                                       \
371     outptr += 4;                                                              \
372   }
373 #define LOOP_NEED_FLAGS
374 #define EXTRA_LOOP_DECLS \
375         , enum variant var, int swap
376 #include <iconv/loop.c>
377
378
379 /* Now define the toplevel functions.  */
380 #include <iconv/skeleton.c>