7dd72840eb7d982e743927dbb169b47c3d35d88e
[kopensolaris-gnu/glibc.git] / iconvdata / ibm939.c
1 /* Conversion to and from IBM939.
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 "ibm939.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    "IBM939//"
33 #define FROM_LOOP       from_ibm939
34 #define TO_LOOP         to_ibm939
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-939 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 = __ibm939sb_to_ucs4_idx;                           \
61     const struct gap *rp2 = __ibm939db_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 IBM939 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 = __ibm939sb_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             if (res == 0xa5)                                                  \
124               res = 0x5c;                                                     \
125             put32 (outptr, res);                                              \
126             outptr += 4;                                                      \
127             ++inptr;                                                          \
128           }                                                                   \
129       }                                                                       \
130     else if (curcs == db)                                                     \
131       {                                                                       \
132         /* Use the IBM939 table for double byte.  */                          \
133         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
134           {                                                                   \
135             /* The second character is not available.  Store the              \
136                intermediate result. */                                        \
137             result = __GCONV_INCOMPLETE_INPUT;                                \
138             break;                                                            \
139           }                                                                   \
140                                                                               \
141         ch = (ch * 0x100) + inptr[1];                                         \
142         while (ch > rp2->end)                                                 \
143           ++rp2;                                                              \
144                                                                               \
145         if (__builtin_expect (rp2 == NULL, 0)                                 \
146             || __builtin_expect (ch < rp2->start, 0)                          \
147             || (res = __ibm939db_to_ucs4[ch + rp2->idx],                      \
148                 __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
149           {                                                                   \
150             /* This is an illegal character.  */                              \
151             if (! ignore_errors_p ())                                         \
152               {                                                               \
153                 result = __GCONV_ILLEGAL_INPUT;                               \
154                 break;                                                        \
155               }                                                               \
156             ++*irreversible;                                                  \
157             inptr += 2;                                                       \
158             continue;                                                         \
159           }                                                                   \
160         else                                                                  \
161           {                                                                   \
162             put32 (outptr, res);                                              \
163             outptr += 4;                                                      \
164             inptr += 2;                                                       \
165           }                                                                   \
166       }                                                                       \
167   }
168 #define LOOP_NEED_FLAGS
169 #include <iconv/loop.c>
170
171 /* Next, define the other direction */
172 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
173 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
174 #define INIT_PARAMS             int curcs = init;
175 #define LOOPFCT                 TO_LOOP
176 #define BODY \
177   {                                                                           \
178     uint32_t ch = get32 (inptr);                                              \
179     const struct gap *rp1 = __ucs4_to_ibm939sb_idx;                           \
180     const struct gap *rp2 = __ucs4_to_ibm939db_idx;                           \
181     const char *cp;                                                           \
182                                                                               \
183     if (__builtin_expect (ch, 0) >= 0xffff)                                   \
184       {                                                                       \
185         rp1 = NULL;                                                           \
186         rp2 = NULL;                                                           \
187       }                                                                       \
188     else                                                                      \
189       while (ch > rp1->end)                                                   \
190         ++rp1;                                                                \
191                                                                               \
192     /* Use the UCS4 table for single byte.  */                                \
193     if (__builtin_expect (rp1 == NULL, 0)                                     \
194         || __builtin_expect (ch < rp1->start, 0)                              \
195         || (cp = __ucs4_to_ibm939sb[ch + rp1->idx],                           \
196             __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
197       {                                                                       \
198         /* Use the UCS4 table for double byte.  */                            \
199         while (ch > rp2->end)                                                 \
200           ++rp2;                                                              \
201                                                                               \
202         if (__builtin_expect (rp2 == NULL, 0)                                 \
203             || __builtin_expect (ch < rp2->start, 0)                          \
204             || (cp = __ucs4_to_ibm939db[ch + rp2->idx],                       \
205                 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0'))        \
206           {                                                                   \
207             /* This is an illegal character.  */                              \
208             if (! ignore_errors_p ())                                         \
209               {                                                               \
210                 result = __GCONV_ILLEGAL_INPUT;                               \
211                 break;                                                        \
212               }                                                               \
213             ++*irreversible;                                                  \
214           }                                                                   \
215         else                                                                  \
216           {                                                                   \
217             if (curcs == init || curcs == sb)                                 \
218               {                                                               \
219                 if (__builtin_expect (outptr+1 > outend, 0))                  \
220                   {                                                           \
221                     result = __GCONV_FULL_OUTPUT;                             \
222                     break;                                                    \
223                   }                                                           \
224                 *outptr++ = SO;                                               \
225                 curcs = db;                                                   \
226               }                                                               \
227                                                                               \
228             if (__builtin_expect (outptr + 2 > outend, 0))                    \
229               {                                                               \
230                 result = __GCONV_FULL_OUTPUT;                                 \
231                 break;                                                        \
232               }                                                               \
233             *outptr++ = cp[0];                                                \
234             *outptr++ = cp[1];                                                \
235           }                                                                   \
236       }                                                                       \
237     else                                                                      \
238       {                                                                       \
239         if (curcs == db)                                                      \
240           {                                                                   \
241             if (__builtin_expect (outptr + 1 > outend, 0))                    \
242               {                                                               \
243                 result = __GCONV_FULL_OUTPUT;                                 \
244                 break;                                                        \
245               }                                                               \
246             *outptr++ = SI;                                                   \
247           }                                                                   \
248                                                                               \
249         if (__builtin_expect (outptr + 1 > outend, 0))                        \
250           {                                                                   \
251             result = __GCONV_FULL_OUTPUT;                                     \
252             break;                                                            \
253           }                                                                   \
254         if (ch == 0x7e)                                                       \
255           *outptr++ = 0xa0;                                                   \
256         else if (ch == 0x5c)                                                  \
257           *outptr++ = 0xb2;                                                   \
258         else                                                                  \
259           *outptr++ = cp[0];                                                  \
260         curcs = sb;                                                           \
261       }                                                                       \
262                                                                               \
263     /* Now that we wrote the output increment the input pointer.  */          \
264     inptr += 4;                                                               \
265   }
266 #define LOOP_NEED_FLAGS
267 #include <iconv/loop.c>
268
269 /* Now define the toplevel functions. */
270 #include <iconv/skeleton.c>