Update interfaces of functions. Insert appropriate error handling
[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 int
104 gconv_init (struct __gconv_step *step)
105 {
106   /* Determine which direction.  */
107   struct utf16_data *new_data;
108   enum direction dir = illegal_dir;
109   enum variant var = illegal_var;
110   int result;
111
112   if (__strcasecmp (step->__from_name, "UTF-16") == 0)
113     {
114       dir = from_utf16;
115       var = UTF_16;
116     }
117   else if (__strcasecmp (step->__to_name, "UTF-16") == 0)
118     {
119       dir = to_utf16;
120       var = UTF_16;
121     }
122   else if (__strcasecmp (step->__from_name, "UTF-16BE") == 0)
123     {
124       dir = from_utf16;
125       var = UTF_16BE;
126     }
127   else if (__strcasecmp (step->__to_name, "UTF-16BE") == 0)
128     {
129       dir = to_utf16;
130       var = UTF_16BE;
131     }
132   else if (__strcasecmp (step->__from_name, "UTF-16LE") == 0)
133     {
134       dir = from_utf16;
135       var = UTF_16LE;
136     }
137   else if (__strcasecmp (step->__to_name, "UTF-16LE") == 0)
138     {
139       dir = to_utf16;
140       var = UTF_16LE;
141     }
142
143   result = __GCONV_NOCONV;
144   if (__builtin_expect (dir, to_utf16) != illegal_dir)
145     {
146       new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data));
147
148       result = __GCONV_NOMEM;
149       if (new_data != NULL)
150         {
151           new_data->dir = dir;
152           new_data->var = var;
153           new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN)
154                             || (var == UTF_16BE
155                                 && BYTE_ORDER == LITTLE_ENDIAN));
156           step->__data = new_data;
157
158           if (dir == from_utf16)
159             {
160               step->__min_needed_from = MIN_NEEDED_FROM;
161               step->__max_needed_from = MIN_NEEDED_FROM;
162               step->__min_needed_to = MIN_NEEDED_TO;
163               step->__max_needed_to = MIN_NEEDED_TO;
164             }
165           else
166             {
167               step->__min_needed_from = MIN_NEEDED_TO;
168               step->__max_needed_from = MIN_NEEDED_TO;
169               step->__min_needed_to = MIN_NEEDED_FROM;
170               step->__max_needed_to = MIN_NEEDED_FROM;
171             }
172
173           step->__stateful = 0;
174
175           result = __GCONV_OK;
176         }
177     }
178
179   return result;
180 }
181
182
183 void
184 gconv_end (struct __gconv_step *data)
185 {
186   free (data->__data);
187 }
188
189
190 /* Convert from the internal (UCS4-like) format to UTF-16.  */
191 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
192 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
193 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
194 #define LOOPFCT                 TO_LOOP
195 #define BODY \
196   {                                                                           \
197     uint32_t c = get32 (inptr);                                               \
198                                                                               \
199     if (swap)                                                                 \
200       {                                                                       \
201         if (__builtin_expect (c, 0) >= 0x10000)                               \
202           {                                                                   \
203             if (__builtin_expect (c, 0) >= 0x110000)                          \
204               {                                                               \
205                 if (step_data->__trans.__trans_fct != NULL)                   \
206                   {                                                           \
207                     result = DL_CALL_FCT (step_data->__trans.__trans_fct,     \
208                                           (step, step_data, *inptrp, &inptr,  \
209                                            inend, *outptrp, &outptr, outend,  \
210                                            irreversible));                    \
211                     if (result != __GCONV_OK)                                 \
212                       break;                                                  \
213                   }                                                           \
214                 else if (! ignore_errors_p ())                                \
215                   {                                                           \
216                     /* This is an illegal character.  */                      \
217                     result = __GCONV_ILLEGAL_INPUT;                           \
218                     break;                                                    \
219                   }                                                           \
220                 else                                                          \
221                   {                                                           \
222                     ++*irreversible;                                          \
223                     inptr += 4;                                               \
224                   }                                                           \
225                 continue;                                                     \
226               }                                                               \
227                                                                               \
228             /* Generate a surrogate character.  */                            \
229             if (__builtin_expect (outptr + 4 > outend, 0))                    \
230               {                                                               \
231                 /* Overflow in the output buffer.  */                         \
232                 result = __GCONV_FULL_OUTPUT;                                 \
233                 break;                                                        \
234               }                                                               \
235                                                                               \
236             put16 (outptr, bswap_16 (0xd7c0 + (c >> 10)));                    \
237             outptr += 2;                                                      \
238             put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff)));                  \
239           }                                                                   \
240         else                                                                  \
241           put16 (outptr, bswap_16 (c));                                       \
242       }                                                                       \
243     else                                                                      \
244       {                                                                       \
245         if (__builtin_expect (c, 0) >= 0x10000)                               \
246           {                                                                   \
247             if (__builtin_expect (c, 0) >= 0x110000)                          \
248               {                                                               \
249                 if (step_data->__trans.__trans_fct != NULL)                   \
250                   {                                                           \
251                     result = DL_CALL_FCT (step_data->__trans.__trans_fct,     \
252                                           (step, step_data, *inptrp, &inptr,  \
253                                            inend, *outptrp, &outptr, outend,  \
254                                            irreversible));                    \
255                     if (result != __GCONV_OK)                                 \
256                       break;                                                  \
257                   }                                                           \
258                 else if (! ignore_errors_p ())                                \
259                   {                                                           \
260                     /* This is an illegal character.  */                      \
261                     result = __GCONV_ILLEGAL_INPUT;                           \
262                     break;                                                    \
263                   }                                                           \
264                 else                                                          \
265                   {                                                           \
266                     ++*irreversible;                                          \
267                     inptr += 4;                                               \
268                   }                                                           \
269                 continue;                                                     \
270               }                                                               \
271                                                                               \
272             /* Generate a surrogate character.  */                            \
273             if (__builtin_expect (outptr + 4 > outend, 0))                    \
274               {                                                               \
275                 /* Overflow in the output buffer.  */                         \
276                 result = __GCONV_FULL_OUTPUT;                                 \
277                 break;                                                        \
278               }                                                               \
279                                                                               \
280             put16 (outptr, 0xd7c0 + (c >> 10));                               \
281             outptr += 2;                                                      \
282             put16 (outptr, 0xdc00 + (c & 0x3ff));                             \
283           }                                                                   \
284         else                                                                  \
285           put16 (outptr, c);                                                  \
286       }                                                                       \
287     outptr += 2;                                                              \
288     inptr += 4;                                                               \
289   }
290 #define LOOP_NEED_FLAGS
291 #define EXTRA_LOOP_DECLS \
292         , enum variant var, int swap
293 #include <iconv/loop.c>
294
295
296 /* Convert from UTF-16 to the internal (UCS4-like) format.  */
297 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
298 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
299 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
300 #define LOOPFCT                 FROM_LOOP
301 #define BODY \
302   {                                                                           \
303     uint16_t u1 = get16 (inptr);                                              \
304                                                                               \
305     if (swap)                                                                 \
306       {                                                                       \
307         u1 = bswap_16 (u1);                                                   \
308                                                                               \
309         if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff)                 \
310           {                                                                   \
311             /* No surrogate.  */                                              \
312             put32 (outptr, u1);                                               \
313             inptr += 2;                                                       \
314           }                                                                   \
315         else                                                                  \
316           {                                                                   \
317             uint16_t u2;                                                      \
318                                                                               \
319             /* It's a surrogate character.  At least the first word says      \
320                it is.  */                                                     \
321             if (__builtin_expect (inptr + 4 > inend, 0))                      \
322               {                                                               \
323                 /* We don't have enough input for another complete input      \
324                    character.  */                                             \
325                 result = __GCONV_INCOMPLETE_INPUT;                            \
326                 break;                                                        \
327               }                                                               \
328                                                                               \
329             inptr += 2;                                                       \
330             u2 = bswap_16 (get16 (inptr));                                    \
331             if (__builtin_expect (u2, 0xdc00) < 0xdc00                        \
332                 || __builtin_expect (u2, 0xdc00) >= 0xdfff)                   \
333               {                                                               \
334                 /* This is no valid second word for a surrogate.  */          \
335                 if (! ignore_errors_p ())                                     \
336                   {                                                           \
337                     inptr -= 2;                                               \
338                     result = __GCONV_ILLEGAL_INPUT;                           \
339                     break;                                                    \
340                   }                                                           \
341                                                                               \
342                 ++*irreversible;                                              \
343                 continue;                                                     \
344               }                                                               \
345                                                                               \
346             put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00));            \
347             inptr += 2;                                                       \
348           }                                                                   \
349       }                                                                       \
350     else                                                                      \
351       {                                                                       \
352         if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff)                 \
353           {                                                                   \
354             /* No surrogate.  */                                              \
355             put32 (outptr, u1);                                               \
356             inptr += 2;                                                       \
357           }                                                                   \
358         else                                                                  \
359           {                                                                   \
360             uint16_t u2;                                                      \
361                                                                               \
362             /* It's a surrogate character.  At least the first word says      \
363                it is.  */                                                     \
364             if (__builtin_expect (inptr + 4 > inend, 0))                      \
365               {                                                               \
366                 /* We don't have enough input for another complete input      \
367                    character.  */                                             \
368                 result = __GCONV_INCOMPLETE_INPUT;                            \
369                 break;                                                        \
370               }                                                               \
371                                                                               \
372             inptr += 2;                                                       \
373             u2 = get16 (inptr);                                               \
374             if (__builtin_expect (u2, 0xdc00) < 0xdc00                        \
375                 || __builtin_expect (u2, 0xdc00) >= 0xdfff)                   \
376               {                                                               \
377                 /* This is no valid second word for a surrogate.  */          \
378                 if (! ignore_errors_p ())                                     \
379                   {                                                           \
380                     inptr -= 2;                                               \
381                     result = __GCONV_ILLEGAL_INPUT;                           \
382                     break;                                                    \
383                   }                                                           \
384                                                                               \
385                 ++*irreversible;                                              \
386                 continue;                                                     \
387               }                                                               \
388                                                                               \
389             put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00));            \
390             inptr += 2;                                                       \
391           }                                                                   \
392       }                                                                       \
393     outptr += 4;                                                              \
394   }
395 #define LOOP_NEED_FLAGS
396 #define EXTRA_LOOP_DECLS \
397         , enum variant var, int swap
398 #include <iconv/loop.c>
399
400
401 /* Now define the toplevel functions.  */
402 #include <iconv/skeleton.c>