Fix handling of state. Optimize a bit.
[kopensolaris-gnu/glibc.git] / iconvdata / ibm930.c
1 /* Conversion from and to IBM930.
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 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 <dlfcn.h>
22 #include <stdint.h>
23 #include <wchar.h>
24 #include <byteswap.h>
25 #include "ibm930.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    "IBM930//"
33 #define FROM_LOOP       from_ibm930
34 #define TO_LOOP         to_ibm930
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           unsigned char *outbuf = data->__outbuf;                             \
60                                                                               \
61           /* We are not in the initial state.  To switch back we have         \
62              to emit `SI'.  */                                                \
63           if (__builtin_expect (outbuf >= data->__outbufend, 0))              \
64             /* We don't have enough room in the output buffer.  */            \
65             status = __GCONV_FULL_OUTPUT;                                     \
66           else                                                                \
67             {                                                                 \
68               /* Write out the shift sequence.  */                            \
69               *outbuf++ = SI;                                                 \
70               data->__outbuf = outbuf;                                        \
71               data->__statep->__count &= 7;                                   \
72             }                                                                 \
73         }                                                                     \
74     }
75
76
77 /* Since we might have to reset input pointer we must be able to save
78    and retore the state.  */
79 #define SAVE_RESET_STATE(Save) \
80   if (Save)                                                                   \
81     save_curcs = *curcsp;                                                     \
82   else                                                                        \
83     *curcsp = save_curcs
84
85
86 /* Current codeset type.  */
87 enum
88 {
89   sb = 0,
90   db = 64
91 };
92
93
94 /* First, define the conversion function from IBM-930 to UCS4.  */
95 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
96 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
97 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
98 #define LOOPFCT                 FROM_LOOP
99 #define BODY \
100   {                                                                           \
101     uint32_t ch = *inptr;                                                     \
102     uint32_t res;                                                             \
103                                                                               \
104     if (__builtin_expect (ch, 0) == SO)                                       \
105       {                                                                       \
106         /* Shift OUT, change to DBCS converter.  */                           \
107         if (curcs == db)                                                      \
108           {                                                                   \
109             result = __GCONV_ILLEGAL_INPUT;                                   \
110             break;                                                            \
111           }                                                                   \
112         curcs = db;                                                           \
113         ++inptr;                                                              \
114         continue;                                                             \
115       }                                                                       \
116     else if (__builtin_expect (ch, 0) == SI)                                  \
117       {                                                                       \
118         /* Shift IN, change to SBCS converter */                              \
119         if (curcs == sb)                                                      \
120           {                                                                   \
121             result = __GCONV_ILLEGAL_INPUT;                                   \
122             break;                                                            \
123           }                                                                   \
124         curcs = sb;                                                           \
125         ++inptr;                                                              \
126         continue;                                                             \
127       }                                                                       \
128                                                                               \
129     if (curcs == sb)                                                          \
130       {                                                                       \
131         /* Use the IBM930 table for single byte.  */                          \
132         res = __ibm930sb_to_ucs4[ch];                                         \
133         if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0')             \
134           {                                                                   \
135             /* This is an illegal character.  */                              \
136             if (! ignore_errors_p ())                                         \
137               {                                                               \
138                 result = __GCONV_ILLEGAL_INPUT;                               \
139                 break;                                                        \
140               }                                                               \
141             ++*irreversible;                                                  \
142           }                                                                   \
143         else                                                                  \
144           {                                                                   \
145             put32 (outptr, res);                                              \
146             outptr += 4;                                                      \
147           }                                                                   \
148         ++inptr;                                                              \
149       }                                                                       \
150     else                                                                      \
151       {                                                                       \
152         /* Use the IBM930 table for double byte. */                           \
153         const struct gap *rp2 = __ibm930db_to_ucs4_idx;                       \
154                                                                               \
155         assert (curcs == db);                                                 \
156                                                                               \
157         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
158           {                                                                   \
159             /* The second character is not available.  Store the              \
160                intermediate result. */                                        \
161             result = __GCONV_INCOMPLETE_INPUT;                                \
162             break;                                                            \
163           }                                                                   \
164                                                                               \
165         ch = (ch * 0x100) + inptr[1];                                         \
166         while (ch > rp2->end)                                                 \
167           ++rp2;                                                              \
168                                                                               \
169         if (__builtin_expect (ch < rp2->start, 0)                             \
170             || (res = __ibm930db_to_ucs4[ch + rp2->idx],                      \
171                 __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
172           {                                                                   \
173             /* This is an illegal character.  */                              \
174             if (! ignore_errors_p ())                                         \
175               {                                                               \
176                 result = __GCONV_ILLEGAL_INPUT;                               \
177                 break;                                                        \
178               }                                                               \
179             ++*irreversible;                                                  \
180           }                                                                   \
181         else                                                                  \
182           {                                                                   \
183             put32 (outptr, res);                                              \
184             outptr += 4;                                                      \
185           }                                                                   \
186         inptr += 2;                                                           \
187       }                                                                       \
188   }
189 #define LOOP_NEED_FLAGS
190 #define EXTRA_LOOP_DECLS        , int *curcsp
191 #define INIT_PARAMS             int curcs = *curcsp & ~7
192 #define UPDATE_PARAMS           *curcsp = curcs
193 #include <iconv/loop.c>
194
195 /* Next, define the other direction.  */
196 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
197 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
198 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
199 #define LOOPFCT                 TO_LOOP
200 #define BODY \
201   {                                                                           \
202     uint32_t ch = get32 (inptr);                                              \
203     const struct gap *rp1 = __ucs4_to_ibm930sb_idx;                           \
204     const struct gap *rp2 = __ucs4_to_ibm930db_idx;                           \
205     const char *cp;                                                           \
206                                                                               \
207     if (__builtin_expect (ch, 0) >= 0xffff)                                   \
208       {                                                                       \
209         if (! ignore_errors_p ())                                             \
210           {                                                                   \
211             result = __GCONV_ILLEGAL_INPUT;                                   \
212             break;                                                            \
213           }                                                                   \
214         ++*irreversible;                                                      \
215         inptr += 4;                                                           \
216         continue;                                                             \
217       }                                                                       \
218                                                                               \
219     while (ch > rp1->end)                                                     \
220       ++rp1;                                                                  \
221                                                                               \
222     /* Use the UCS4 table for single byte.  */                                \
223     if (__builtin_expect (ch < rp1->start, 0)                         \
224         || (cp = __ucs4_to_ibm930sb[ch + rp1->idx],                           \
225             __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
226       {                                                                       \
227         /* Use the UCS4 table for double byte. */                             \
228         while (ch > rp2->end)                                                 \
229           ++rp2;                                                              \
230                                                                               \
231         if (__builtin_expect (ch < rp2->start, 0)                             \
232             || (cp = __ucs4_to_ibm930db[ch + rp2->idx],                       \
233                 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0'))        \
234           {                                                                   \
235             /* This is an illegal character.  */                              \
236             if (! ignore_errors_p ())                                         \
237               {                                                               \
238                 result = __GCONV_ILLEGAL_INPUT;                               \
239                 break;                                                        \
240               }                                                               \
241             ++*irreversible;                                                  \
242           }                                                                   \
243         else                                                                  \
244           {                                                                   \
245             if (curcs == sb)                                                  \
246               {                                                               \
247                 if (__builtin_expect (outptr+1 > outend, 0))                  \
248                   {                                                           \
249                     result = __GCONV_FULL_OUTPUT;                             \
250                     break;                                                    \
251                   }                                                           \
252                 *outptr++ = SO;                                               \
253                 curcs = db;                                                   \
254               }                                                               \
255                                                                               \
256             if (__builtin_expect (outptr + 2 > outend, 0))                    \
257               {                                                               \
258                 result = __GCONV_FULL_OUTPUT;                                 \
259                 break;                                                        \
260               }                                                               \
261             *outptr++ = cp[0];                                                \
262             *outptr++ = cp[1];                                                \
263           }                                                                   \
264       }                                                                       \
265     else                                                                      \
266       {                                                                       \
267         if (curcs == db)                                                      \
268           {                                                                   \
269             if (__builtin_expect (outptr + 1 > outend, 0))                    \
270               {                                                               \
271                 result = __GCONV_FULL_OUTPUT;                                 \
272                 break;                                                        \
273               }                                                               \
274             *outptr++ = SI;                                                   \
275           }                                                                   \
276                                                                               \
277         if (__builtin_expect (outptr + 1 > outend, 0))                        \
278           {                                                                   \
279             result = __GCONV_FULL_OUTPUT;                                     \
280             break;                                                            \
281           }                                                                   \
282         if (ch == 0x7e)                                                       \
283           *outptr++ = 0xa1;                                                   \
284         else if (ch == 0x5c)                                                  \
285           *outptr++ = 0x5b;                                                   \
286         else                                                                  \
287           *outptr++ = cp[0];                                                  \
288         curcs = sb;                                                           \
289       }                                                                       \
290                                                                               \
291     /* Now that we wrote the output increment the input pointer.  */          \
292     inptr += 4;                                                               \
293   }
294 #define LOOP_NEED_FLAGS
295 #define EXTRA_LOOP_DECLS        , int *curcsp
296 #define INIT_PARAMS             int curcs = *curcsp & ~7
297 #define UPDATE_PARAMS           *curcsp = curcs
298 #include <iconv/loop.c>
299
300 /* Now define the toplevel functions.  */
301 #include <iconv/skeleton.c>