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