Update to LGPL v2.1.
[platform/upstream/glibc.git] / iconvdata / ibm933.c
1 /* Conversion from and to IBM933.
2    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Masahide Washizawa <washi@yamato.ibm.co.jp>, 2000.
5
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.
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    Lesser General Public License for more details.
15
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.  */
20
21 #include <dlfcn.h>
22 #include <stdint.h>
23 #include <wchar.h>
24 #include <byteswap.h>
25 #include "ibm933.h"
26
27 /* The shift sequences for this charset (it does not use ESC).  */
28 #define SI              0x0F  /* Shift In, host code to turn DBCS off.  */
29 #define SO              0x0E  /* Shift Out, host code to turn DBCS on.  */
30
31 /* Definitions used in the body of the `gconv' function.  */
32 #define CHARSET_NAME    "IBM933//"
33 #define FROM_LOOP       from_ibm933
34 #define TO_LOOP         to_ibm933
35 #define MIN_NEEDED_FROM 1
36 #define MAX_NEEDED_FROM 2
37 #define MIN_NEEDED_TO   4
38 #define MAX_NEEDED_TO   4
39 #define PREPARE_LOOP \
40   int save_curcs;                                                             \
41   int *curcsp = &data->__statep->__count;
42 #define EXTRA_LOOP_ARGS         , curcsp
43
44 /* Definitions of initialization and destructor function.  */
45 #define DEFINE_INIT     1
46 #define DEFINE_FINI     1
47
48
49 /* Since this is a stateful encoding we have to provide code which resets
50    the output state to the initial state.  This has to be done during the
51    flushing.  */
52 #define EMIT_SHIFT_TO_INIT \
53   if ((data->__statep->__count & ~7) != sb)                                   \
54     {                                                                         \
55       if (FROM_DIRECTION)                                                     \
56         data->__statep->__count &= 7;                                         \
57       else                                                                    \
58         {                                                                     \
59           /* We are not in the initial state.  To switch back we have         \
60              to emit `SI'.  */                                                \
61           if (__builtin_expect (outbuf >= outend, 0))                         \
62             /* We don't have enough room in the output buffer.  */            \
63             status = __GCONV_FULL_OUTPUT;                                     \
64           else                                                                \
65             {                                                                 \
66               /* Write out the shift sequence.  */                            \
67               *outbuf++ = SI;                                                 \
68               data->__statep->__count &= 7;                                   \
69             }                                                                 \
70         }                                                                     \
71     }
72
73
74 /* Since we might have to reset input pointer we must be able to save
75    and retore the state.  */
76 #define SAVE_RESET_STATE(Save) \
77   if (Save)                                                                   \
78     save_curcs = *curcsp;                                                     \
79   else                                                                        \
80     *curcsp = save_curcs
81
82
83 /* Current codeset type.  */
84 enum
85 {
86   sb = 0,
87   db = 64
88 };
89
90 /* First, define the conversion function from IBM-933 to UCS4.  */
91 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
92 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
93 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
94 #define LOOPFCT                 FROM_LOOP
95 #define BODY \
96   {                                                                           \
97     uint32_t ch = *inptr;                                                     \
98     uint32_t res;                                                             \
99                                                                               \
100     if (__builtin_expect (ch, 0) == SO)                                       \
101       {                                                                       \
102         /* Shift OUT, change to DBCS converter.  */                           \
103         if (curcs == db)                                                      \
104           {                                                                   \
105             result = __GCONV_ILLEGAL_INPUT;                                   \
106             break;                                                            \
107           }                                                                   \
108         curcs = db;                                                           \
109         ++inptr;                                                              \
110         continue;                                                             \
111       }                                                                       \
112     else if (__builtin_expect (ch, 0) == SI)                                  \
113       {                                                                       \
114         /* Shift IN, change to SBCS converter.  */                            \
115         if (curcs == sb)                                                      \
116           {                                                                   \
117             result = __GCONV_ILLEGAL_INPUT;                                   \
118             break;                                                            \
119           }                                                                   \
120         curcs = sb;                                                           \
121         ++inptr;                                                              \
122         continue;                                                             \
123       }                                                                       \
124                                                                               \
125     if (curcs == sb)                                                          \
126       {                                                                       \
127         /* Use the IBM933 table for single byte.  */                          \
128         res = __ibm933sb_to_ucs4[ch];                                         \
129         if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0')             \
130           {                                                                   \
131             /* This is an illegal character.  */                              \
132             if (! ignore_errors_p ())                                         \
133               {                                                               \
134                 result = __GCONV_ILLEGAL_INPUT;                               \
135                 break;                                                        \
136               }                                                               \
137             ++*irreversible;                                                  \
138           }                                                                   \
139         else                                                                  \
140           {                                                                   \
141             put32 (outptr, res);                                              \
142             outptr += 4;                                                      \
143           }                                                                   \
144         ++inptr;                                                              \
145       }                                                                       \
146     else                                                                      \
147       {                                                                       \
148         const struct gap *rp2 = __ibm933db_to_ucs4_idx;                       \
149                                                                               \
150         assert (curcs == db);                                                 \
151                                                                               \
152         /* Use the IBM933 table for double byte.  */                          \
153         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
154           {                                                                   \
155             /* The second character is not available.  Store the              \
156                intermediate result. */                                        \
157             result = __GCONV_INCOMPLETE_INPUT;                                \
158             break;                                                            \
159           }                                                                   \
160                                                                               \
161         ch = (ch * 0x100) + inptr[1];                                         \
162         while (ch > rp2->end)                                                 \
163           ++rp2;                                                              \
164                                                                               \
165         if (__builtin_expect (rp2 == NULL, 0)                                 \
166             || __builtin_expect (ch < rp2->start, 0)                          \
167             || (res = __ibm933db_to_ucs4[ch + rp2->idx],                      \
168                 __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
169           {                                                                   \
170             /* This is an illegal character.  */                              \
171             if (! ignore_errors_p ())                                         \
172               {                                                               \
173                 result = __GCONV_ILLEGAL_INPUT;                               \
174                 break;                                                        \
175               }                                                               \
176             ++*irreversible;                                                  \
177             inptr += 2;                                                       \
178             continue;                                                         \
179           }                                                                   \
180         else                                                                  \
181           {                                                                   \
182             put32 (outptr, res);                                              \
183             outptr += 4;                                                      \
184             inptr += 2;                                                       \
185           }                                                                   \
186       }                                                                       \
187   }
188 #define LOOP_NEED_FLAGS
189 #define EXTRA_LOOP_DECLS        , int *curcsp
190 #define INIT_PARAMS             int curcs = *curcsp & ~7
191 #define UPDATE_PARAMS           *curcsp = curcs
192 #include <iconv/loop.c>
193
194 /* Next, define the other direction.  */
195 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
196 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
197 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
198 #define LOOPFCT                 TO_LOOP
199 #define BODY \
200   {                                                                           \
201     uint32_t ch = get32 (inptr);                                              \
202     const struct gap *rp1 = __ucs4_to_ibm933sb_idx;                           \
203     const struct gap *rp2 = __ucs4_to_ibm933db_idx;                           \
204     const char *cp;                                                           \
205                                                                               \
206     if (__builtin_expect (ch, 0) >= 0xffff)                                   \
207       {                                                                       \
208         UNICODE_TAG_HANDLER (ch, 4);                                          \
209                                                                               \
210         if (! ignore_errors_p ())                                             \
211           {                                                                   \
212             result = __GCONV_ILLEGAL_INPUT;                                   \
213             break;                                                            \
214           }                                                                   \
215         ++*irreversible;                                                      \
216         inptr += 4;                                                           \
217         continue;                                                             \
218       }                                                                       \
219                                                                               \
220     while (ch > rp1->end)                                                     \
221       ++rp1;                                                                  \
222                                                                               \
223     /* Use the UCS4 table for single byte.  */                                \
224     if (__builtin_expect (ch < rp1->start, 0)                         \
225         || (cp = __ucs4_to_ibm933sb[ch + rp1->idx],                           \
226             __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
227       {                                                                       \
228         /* Use the UCS4 table for double byte.  */                            \
229         while (ch > rp2->end)                                                 \
230           ++rp2;                                                              \
231                                                                               \
232         if (__builtin_expect (ch < rp2->start, 0)                             \
233             || (cp = __ucs4_to_ibm933db[ch + rp2->idx],                       \
234                 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0'))        \
235           {                                                                   \
236             /* This is an illegal character.  */                              \
237             if (! ignore_errors_p ())                                         \
238               {                                                               \
239                 result = __GCONV_ILLEGAL_INPUT;                               \
240                 break;                                                        \
241               }                                                               \
242             ++*irreversible;                                                  \
243           }                                                                   \
244         else                                                                  \
245           {                                                                   \
246             if (curcs == sb)                                                  \
247               {                                                               \
248                 if (__builtin_expect (outptr+1 > outend, 0))                  \
249                   {                                                           \
250                     result = __GCONV_FULL_OUTPUT;                             \
251                     break;                                                    \
252                   }                                                           \
253                 *outptr++ = SO;                                               \
254                 curcs = db;                                                   \
255               }                                                               \
256                                                                               \
257             if (__builtin_expect (outptr + 2 > outend, 0))                    \
258               {                                                               \
259                 result = __GCONV_FULL_OUTPUT;                                 \
260                 break;                                                        \
261               }                                                               \
262             *outptr++ = cp[0];                                                \
263             *outptr++ = cp[1];                                                \
264           }                                                                   \
265       }                                                                       \
266     else                                                                      \
267       {                                                                       \
268         if (curcs == db)                                                      \
269           {                                                                   \
270             if (__builtin_expect (outptr + 1 > outend, 0))                    \
271               {                                                               \
272                 result = __GCONV_FULL_OUTPUT;                                 \
273                 break;                                                        \
274               }                                                               \
275             *outptr++ = SI;                                                   \
276           }                                                                   \
277                                                                               \
278         if (__builtin_expect (outptr + 1 > outend, 0))                        \
279           {                                                                   \
280             result = __GCONV_FULL_OUTPUT;                                     \
281             break;                                                            \
282           }                                                                   \
283         *outptr++ = cp[0];                                                    \
284         curcs = sb;                                                           \
285       }                                                                       \
286                                                                               \
287     /* Now that we wrote the output increment the input pointer.  */          \
288     inptr += 4;                                                               \
289   }
290 #define LOOP_NEED_FLAGS
291 #define EXTRA_LOOP_DECLS        , int *curcsp
292 #define INIT_PARAMS             int curcs = *curcsp & ~7
293 #define UPDATE_PARAMS           *curcsp = curcs
294 #include <iconv/loop.c>
295
296 /* Now define the toplevel functions.  */
297 #include <iconv/skeleton.c>