Add __builtin_expect in many places.
[platform/upstream/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 <gconv.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 /* This is the Byte Order Mark character (BOM).  */
29 #define BOM     0xfeff
30 /* And in the other byte order.  */
31 #define BOM_OE  0xfffe
32
33
34 /* Definitions used in the body of the `gconv' function.  */
35 #define FROM_LOOP               from_utf16_loop
36 #define TO_LOOP                 to_utf16_loop
37 #define DEFINE_INIT             0
38 #define DEFINE_FINI             0
39 #define MIN_NEEDED_FROM         2
40 #define MAX_NEEDED_FROM         4
41 #define MIN_NEEDED_TO           4
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   int swap = ((struct utf16_data *) step->__data)->swap;                      \
47   if (FROM_DIRECTION || var == UTF_16)                                        \
48     {                                                                         \
49       if (data->__invocation_counter == 0)                                    \
50         {                                                                     \
51           /* We have to find out which byte order the file is encoded in.  */ \
52           if (inptr + 2 > inend)                                              \
53             return __GCONV_EMPTY_INPUT;                                       \
54                                                                               \
55           if (get16u (inptr) == BOM)                                          \
56             /* Simply ignore the BOM character.  */                           \
57             inptr += 2;                                                       \
58           else if (get16u (inptr) == BOM_OE)                                  \
59             {                                                                 \
60               ((struct utf16_data *) step->__data)->swap = 1;                 \
61               inptr += 2;                                                     \
62             }                                                                 \
63         }                                                                     \
64     }                                                                         \
65   else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use          \
66            && data->__invocation_counter == 0)                                \
67     {                                                                         \
68       /* Emit the Byte Order Mark.  */                                        \
69       if (__builtin_expect (outbuf + 2 > outend, 0))                          \
70         return __GCONV_FULL_OUTPUT;                                           \
71                                                                               \
72       put16u (outbuf, BOM);                                                   \
73       outbuf += 2;                                                            \
74     }
75 #define EXTRA_LOOP_ARGS         , var, data, swap
76
77
78 /* Direction of the transformation.  */
79 enum direction
80 {
81   illegal_dir,
82   to_utf16,
83   from_utf16
84 };
85
86 enum variant
87 {
88   illegal_var,
89   UTF_16,
90   UTF_16LE,
91   UTF_16BE
92 };
93
94 struct utf16_data
95 {
96   enum direction dir;
97   enum variant var;
98   int swap;
99 };
100
101
102 int
103 gconv_init (struct __gconv_step *step)
104 {
105   /* Determine which direction.  */
106   struct utf16_data *new_data;
107   enum direction dir = illegal_dir;
108   enum variant var = illegal_var;
109   int result;
110
111   if (__strcasecmp (step->__from_name, "UTF-16") == 0)
112     {
113       dir = from_utf16;
114       var = UTF_16;
115     }
116   else if (__strcasecmp (step->__to_name, "UTF-16") == 0)
117     {
118       dir = to_utf16;
119       var = UTF_16;
120     }
121   else if (__strcasecmp (step->__from_name, "UTF-16BE") == 0)
122     {
123       dir = from_utf16;
124       var = UTF_16BE;
125     }
126   else if (__strcasecmp (step->__to_name, "UTF-16BE") == 0)
127     {
128       dir = to_utf16;
129       var = UTF_16BE;
130     }
131   else if (__strcasecmp (step->__from_name, "UTF-16LE") == 0)
132     {
133       dir = from_utf16;
134       var = UTF_16LE;
135     }
136   else if (__strcasecmp (step->__to_name, "UTF-16LE") == 0)
137     {
138       dir = to_utf16;
139       var = UTF_16LE;
140     }
141
142   result = __GCONV_NOCONV;
143   if (__builtin_expect (dir, to_utf16) != illegal_dir)
144     {
145       new_data = (struct utf16_data *) malloc (sizeof (struct utf16_data));
146
147       result = __GCONV_NOMEM;
148       if (new_data != NULL)
149         {
150           new_data->dir = dir;
151           new_data->var = var;
152           new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN)
153                             || (var == UTF_16BE
154                                 && BYTE_ORDER == LITTLE_ENDIAN));
155           step->__data = new_data;
156
157           if (dir == from_utf16)
158             {
159               step->__min_needed_from = MIN_NEEDED_FROM;
160               step->__max_needed_from = MIN_NEEDED_FROM;
161               step->__min_needed_to = MIN_NEEDED_TO;
162               step->__max_needed_to = MIN_NEEDED_TO;
163             }
164           else
165             {
166               step->__min_needed_from = MIN_NEEDED_TO;
167               step->__max_needed_from = MIN_NEEDED_TO;
168               step->__min_needed_to = MIN_NEEDED_FROM;
169               step->__max_needed_to = MIN_NEEDED_FROM;
170             }
171
172           step->__stateful = 0;
173
174           result = __GCONV_OK;
175         }
176     }
177
178   return result;
179 }
180
181
182 void
183 gconv_end (struct __gconv_step *data)
184 {
185   free (data->__data);
186 }
187
188
189 /* Convert from the internal (UCS4-like) format to UTF-16.  */
190 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
191 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
192 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
193 #define LOOPFCT                 TO_LOOP
194 #define BODY \
195   {                                                                           \
196     uint32_t c = get32 (inptr);                                               \
197                                                                               \
198     if (swap)                                                                 \
199       {                                                                       \
200         if (__builtin_expect (c, 0) >= 0x10000)                               \
201           {                                                                   \
202             if (__builtin_expect (c, 0) >= 0x110000)                          \
203               {                                                               \
204                 if (! ignore_errors_p ())                                     \
205                   {                                                           \
206                     /* This is an illegal character.  */                      \
207                     result = __GCONV_ILLEGAL_INPUT;                           \
208                     break;                                                    \
209                   }                                                           \
210                                                                               \
211                 ++*converted;                                                 \
212                 inptr += 4;                                                   \
213                 continue;                                                     \
214               }                                                               \
215                                                                               \
216             /* Generate a surrogate character.  */                            \
217             if (NEED_LENGTH_TEST && __builtin_expect (outptr + 4 > outend, 0))\
218               {                                                               \
219                 /* Overflow in the output buffer.  */                         \
220                 result = __GCONV_FULL_OUTPUT;                                 \
221                 break;                                                        \
222               }                                                               \
223                                                                               \
224             put16 (outptr, bswap_16 (0xd7c0 + (c >> 10)));                    \
225             outptr += 2;                                                      \
226             put16 (outptr, bswap_16 (0xdc00 + (c & 0x3ff)));                  \
227           }                                                                   \
228         else                                                                  \
229           put16 (outptr, bswap_16 (c));                                       \
230       }                                                                       \
231     else                                                                      \
232       {                                                                       \
233         if (__builtin_expect (c, 0) >= 0x10000)                               \
234           {                                                                   \
235             if (__builtin_expect (c, 0) >= 0x110000)                          \
236               {                                                               \
237                 if (! ignore_errors_p ())                                     \
238                   {                                                           \
239                     /* This is an illegal character.  */                      \
240                     result = __GCONV_ILLEGAL_INPUT;                           \
241                     break;                                                    \
242                   }                                                           \
243                                                                               \
244                 ++*converted;                                                 \
245                 inptr += 4;                                                   \
246                 continue;                                                     \
247               }                                                               \
248                                                                               \
249             /* Generate a surrogate character.  */                            \
250             if (NEED_LENGTH_TEST && __builtin_expect (outptr + 4 > outend, 0))\
251               {                                                               \
252                 /* Overflow in the output buffer.  */                         \
253                 result = __GCONV_FULL_OUTPUT;                                 \
254                 break;                                                        \
255               }                                                               \
256                                                                               \
257             put16 (outptr, 0xd7c0 + (c >> 10));                               \
258             outptr += 2;                                                      \
259             put16 (outptr, 0xdc00 + (c & 0x3ff));                             \
260           }                                                                   \
261         else                                                                  \
262           put16 (outptr, c);                                                  \
263       }                                                                       \
264     outptr += 2;                                                              \
265     inptr += 4;                                                               \
266   }
267 #define EXTRA_LOOP_DECLS \
268         , enum variant var, struct __gconv_step_data *step_data, int swap
269 #include <iconv/loop.c>
270
271
272 /* Convert from UTF-16 to the internal (UCS4-like) format.  */
273 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
274 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
275 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
276 #define LOOPFCT                 FROM_LOOP
277 #define BODY \
278   {                                                                           \
279     uint16_t u1 = get16 (inptr);                                              \
280                                                                               \
281     if (swap)                                                                 \
282       {                                                                       \
283         u1 = bswap_16 (u1);                                                   \
284                                                                               \
285         if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff)                 \
286           {                                                                   \
287             /* No surrogate.  */                                              \
288             put32 (outptr, u1);                                               \
289             inptr += 2;                                                       \
290           }                                                                   \
291         else                                                                  \
292           {                                                                   \
293             uint16_t u2;                                                      \
294                                                                               \
295             /* It's a surrogate character.  At least the first word says      \
296                it is.  */                                                     \
297             if (NEED_LENGTH_TEST && __builtin_expect (inptr + 4 > inend, 0))  \
298               {                                                               \
299                 /* We don't have enough input for another complete input      \
300                    character.  */                                             \
301                 result = __GCONV_INCOMPLETE_INPUT;                            \
302                 break;                                                        \
303               }                                                               \
304                                                                               \
305             inptr += 2;                                                       \
306             u2 = bswap_16 (get16 (inptr));                                    \
307             if (__builtin_expect (u2, 0xdc00) < 0xdc00                        \
308                 || __builtin_expect (u2, 0xdc00) >= 0xdfff)                   \
309               {                                                               \
310                 /* This is no valid second word for a surrogate.  */          \
311                 if (! ignore_errors_p ())                                     \
312                   {                                                           \
313                     inptr -= 2;                                               \
314                     result = __GCONV_ILLEGAL_INPUT;                           \
315                     break;                                                    \
316                   }                                                           \
317                                                                               \
318                 ++*converted;                                                 \
319                 continue;                                                     \
320               }                                                               \
321                                                                               \
322             put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00));            \
323             inptr += 2;                                                       \
324           }                                                                   \
325       }                                                                       \
326     else                                                                      \
327       {                                                                       \
328         if (__builtin_expect (u1, 0) < 0xd800 || u1 > 0xdfff)                 \
329           {                                                                   \
330             /* No surrogate.  */                                              \
331             put32 (outptr, u1);                                               \
332             inptr += 2;                                                       \
333           }                                                                   \
334         else                                                                  \
335           {                                                                   \
336             uint16_t u2;                                                      \
337                                                                               \
338             /* It's a surrogate character.  At least the first word says      \
339                it is.  */                                                     \
340             if (NEED_LENGTH_TEST && __builtin_expect (inptr + 4 > inend, 0))  \
341               {                                                               \
342                 /* We don't have enough input for another complete input      \
343                    character.  */                                             \
344                 result = __GCONV_INCOMPLETE_INPUT;                            \
345                 break;                                                        \
346               }                                                               \
347                                                                               \
348             inptr += 2;                                                       \
349             u2 = get16 (inptr);                                               \
350             if (__builtin_expect (u2, 0xdc00) < 0xdc00                        \
351                 || __builtin_expect (u2, 0xdc00) >= 0xdfff)                   \
352               {                                                               \
353                 /* This is no valid second word for a surrogate.  */          \
354                 if (! ignore_errors_p ())                                     \
355                   {                                                           \
356                     inptr -= 2;                                               \
357                     result = __GCONV_ILLEGAL_INPUT;                           \
358                     break;                                                    \
359                   }                                                           \
360                                                                               \
361                 ++*converted;                                                 \
362                 continue;                                                     \
363               }                                                               \
364                                                                               \
365             put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00));            \
366             inptr += 2;                                                       \
367           }                                                                   \
368       }                                                                       \
369     outptr += 4;                                                              \
370   }
371 #define EXTRA_LOOP_DECLS \
372         , enum variant var, struct __gconv_step_data *step_data, int swap
373 #include <iconv/loop.c>
374
375
376 /* Now define the toplevel functions.  */
377 #include <iconv/skeleton.c>