Update.
[platform/upstream/glibc.git] / iconvdata / ibm933.c
1 /* Conversion from and to IBM933.
2    Copyright (C) 2000 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 "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
36 /* Definitions of initialization and destructor function.  */
37 #define DEFINE_INIT     1
38 #define DEFINE_FINI     1
39
40 #define MIN_NEEDED_FROM 1
41 #define MIN_NEEDED_TO   4
42
43 /* Current codeset type.  */
44 enum
45 {
46   init = 0,
47   sb,
48   db
49 };
50
51 /* First, define the conversion function from IBM-933 to UCS4.  */
52 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
53 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
54 #define INIT_PARAMS             int curcs = init;
55 #define LOOPFCT                 FROM_LOOP
56 #define BODY \
57   {                                                                           \
58     uint32_t ch = *inptr;                                                     \
59     uint32_t res;                                                             \
60     const struct gap *rp1 = __ibm933sb_to_ucs4_idx;                           \
61     const struct gap *rp2 = __ibm933db_to_ucs4_idx;                           \
62                                                                               \
63     if (__builtin_expect (ch, 0) == SO)                                       \
64       {                                                                       \
65         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
66           {                                                                   \
67             result = __GCONV_INCOMPLETE_INPUT;                                \
68             break;                                                            \
69           }                                                                   \
70                                                                               \
71         /* Shift OUT, change to DBCS converter.  */                           \
72         if (curcs == db)                                                      \
73           {                                                                   \
74             result = __GCONV_ILLEGAL_INPUT;                                   \
75             break;                                                            \
76           }                                                                   \
77         curcs = db;                                                           \
78         ++inptr;                                                              \
79         ch = *inptr;                                                          \
80       }                                                                       \
81     else if (__builtin_expect (ch, 0) == SI)                                  \
82       {                                                                       \
83         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
84           {                                                                   \
85             result = __GCONV_INCOMPLETE_INPUT;                                \
86             break;                                                            \
87           }                                                                   \
88                                                                               \
89         /* Shift IN, change to SBCS converter.  */                            \
90         if (curcs == sb)                                                      \
91           {                                                                   \
92             result = __GCONV_ILLEGAL_INPUT;                                   \
93             break;                                                            \
94           }                                                                   \
95         curcs = sb;                                                           \
96         ++inptr;                                                              \
97         ch = *inptr;                                                          \
98       }                                                                       \
99                                                                               \
100     if (curcs == sb || curcs == init)                                         \
101       {                                                                       \
102         /* Use the IBM933 table for single byte.  */                          \
103         while (ch > rp1->end)                                                 \
104           ++rp1;                                                              \
105                                                                               \
106         if (__builtin_expect (rp1 == NULL, 0)                                 \
107             || __builtin_expect (ch < rp1->start, 0)                          \
108             || (res = __ibm933sb_to_ucs4[ch + rp1->idx],                      \
109                 __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
110           {                                                                   \
111             /* This is an illegal character.  */                              \
112             if (! ignore_errors_p ())                                         \
113               {                                                               \
114                 result = __GCONV_ILLEGAL_INPUT;                               \
115                 break;                                                        \
116               }                                                               \
117             ++*irreversible;                                                  \
118             ++inptr;                                                          \
119             continue;                                                         \
120           }                                                                   \
121         else                                                                  \
122           {                                                                   \
123             put32 (outptr, res);                                              \
124             outptr += 4;                                                      \
125             ++inptr;                                                          \
126           }                                                                   \
127       }                                                                       \
128     else if (curcs == db)                                                     \
129       {                                                                       \
130         /* Use the IBM933 table for double byte.  */                          \
131         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
132           {                                                                   \
133             /* The second character is not available.  Store the              \
134                intermediate result. */                                        \
135             result = __GCONV_INCOMPLETE_INPUT;                                \
136             break;                                                            \
137           }                                                                   \
138                                                                               \
139         ch = (ch * 0x100) + inptr[1];                                         \
140         while (ch > rp2->end)                                                 \
141           ++rp2;                                                              \
142                                                                               \
143         if (__builtin_expect (rp2 == NULL, 0)                                 \
144             || __builtin_expect (ch < rp2->start, 0)                          \
145             || (res = __ibm933db_to_ucs4[ch + rp2->idx],                      \
146                 __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
147           {                                                                   \
148             /* This is an illegal character.  */                              \
149             if (! ignore_errors_p ())                                         \
150               {                                                               \
151                 result = __GCONV_ILLEGAL_INPUT;                               \
152                 break;                                                        \
153               }                                                               \
154             ++*irreversible;                                                  \
155             inptr += 2;                                                       \
156             continue;                                                         \
157           }                                                                   \
158         else                                                                  \
159           {                                                                   \
160             put32 (outptr, res);                                              \
161             outptr += 4;                                                      \
162             inptr += 2;                                                       \
163           }                                                                   \
164       }                                                                       \
165   }
166 #define LOOP_NEED_FLAGS
167 #include <iconv/loop.c>
168
169 /* Next, define the other direction.  */
170 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
171 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
172 #define INIT_PARAMS             int curcs = init;
173 #define LOOPFCT                 TO_LOOP
174 #define BODY \
175   {                                                                           \
176     uint32_t ch = get32 (inptr);                                              \
177     const struct gap *rp1 = __ucs4_to_ibm933sb_idx;                           \
178     const struct gap *rp2 = __ucs4_to_ibm933db_idx;                           \
179     const char *cp;                                                           \
180                                                                               \
181     if (__builtin_expect (ch, 0) >= 0xffff)                                   \
182       {                                                                       \
183         rp1 = NULL;                                                           \
184         rp2 = NULL;                                                           \
185       }                                                                       \
186     else                                                                      \
187       while (ch > rp1->end)                                                   \
188         ++rp1;                                                                \
189                                                                               \
190     /* Use the UCS4 table for single byte.  */                                \
191     if (__builtin_expect (rp1 == NULL, 0)                                     \
192         || __builtin_expect (ch < rp1->start, 0)                              \
193         || (cp = __ucs4_to_ibm933sb[ch + rp1->idx],                           \
194             __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
195       {                                                                       \
196         /* Use the UCS4 table for double byte.  */                            \
197         while (ch > rp2->end)                                                 \
198           ++rp2;                                                              \
199                                                                               \
200         if (__builtin_expect (rp2 == NULL, 0)                                 \
201             || __builtin_expect (ch < rp2->start, 0)                          \
202             || (cp = __ucs4_to_ibm933db[ch + rp2->idx],                       \
203                 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0'))        \
204           {                                                                   \
205             /* This is an illegal character.  */                              \
206             if (! ignore_errors_p ())                                         \
207               {                                                               \
208                 result = __GCONV_ILLEGAL_INPUT;                               \
209                 break;                                                        \
210               }                                                               \
211             ++*irreversible;                                                  \
212           }                                                                   \
213         else                                                                  \
214           {                                                                   \
215             if (curcs == init || curcs == sb)                                 \
216               {                                                               \
217                 if (__builtin_expect (outptr+1 > outend, 0))                  \
218                   {                                                           \
219                     result = __GCONV_FULL_OUTPUT;                             \
220                     break;                                                    \
221                   }                                                           \
222                 *outptr++ = SO;                                               \
223                 curcs = db;                                                   \
224               }                                                               \
225                                                                               \
226             if (__builtin_expect (outptr + 2 > outend, 0))                    \
227               {                                                               \
228                 result = __GCONV_FULL_OUTPUT;                                 \
229                 break;                                                        \
230               }                                                               \
231             *outptr++ = cp[0];                                                \
232             *outptr++ = cp[1];                                                \
233           }                                                                   \
234       }                                                                       \
235     else                                                                      \
236       {                                                                       \
237         if (curcs == db)                                                      \
238           {                                                                   \
239             if (__builtin_expect (outptr + 1 > outend, 0))                    \
240               {                                                               \
241                 result = __GCONV_FULL_OUTPUT;                                 \
242                 break;                                                        \
243               }                                                               \
244             *outptr++ = SI;                                                   \
245           }                                                                   \
246                                                                               \
247         if (__builtin_expect (outptr + 1 > outend, 0))                        \
248           {                                                                   \
249             result = __GCONV_FULL_OUTPUT;                                     \
250             break;                                                            \
251           }                                                                   \
252         *outptr++ = cp[0];                                                    \
253         curcs = sb;                                                           \
254       }                                                                       \
255                                                                               \
256     /* Now that we wrote the output increment the input pointer.  */          \
257     inptr += 4;                                                               \
258   }
259 #define LOOP_NEED_FLAGS
260 #include <iconv/loop.c>
261
262 /* Now define the toplevel functions.  */
263 #include <iconv/skeleton.c>