chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / iconvdata / ibm930.c
1 /* Conversion from and to IBM930.
2    Copyright (C) 2000-2002, 2008 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 "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 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
95 /* First, define the conversion function from IBM-930 to UCS4.  */
96 #define MIN_NEEDED_INPUT        FROM_LOOP_MIN_NEEDED_FROM
97 #define MAX_NEEDED_INPUT        FROM_LOOP_MAX_NEEDED_FROM
98 #define MIN_NEEDED_OUTPUT       FROM_LOOP_MIN_NEEDED_TO
99 #define MAX_NEEDED_OUTPUT       FROM_LOOP_MAX_NEEDED_TO
100 #define LOOPFCT                 FROM_LOOP
101 #define BODY \
102   {                                                                           \
103     uint32_t ch = *inptr;                                                     \
104     uint32_t res;                                                             \
105                                                                               \
106     if (__builtin_expect (ch, 0) == SO)                                       \
107       {                                                                       \
108         /* Shift OUT, change to DBCS converter.  */                           \
109         if (curcs == db)                                                      \
110           {                                                                   \
111             result = __GCONV_ILLEGAL_INPUT;                                   \
112             break;                                                            \
113           }                                                                   \
114         curcs = db;                                                           \
115         ++inptr;                                                              \
116         continue;                                                             \
117       }                                                                       \
118     else if (__builtin_expect (ch, 0) == SI)                                  \
119       {                                                                       \
120         /* Shift IN, change to SBCS converter */                              \
121         if (curcs == sb)                                                      \
122           {                                                                   \
123             result = __GCONV_ILLEGAL_INPUT;                                   \
124             break;                                                            \
125           }                                                                   \
126         curcs = sb;                                                           \
127         ++inptr;                                                              \
128         continue;                                                             \
129       }                                                                       \
130                                                                               \
131     if (curcs == sb)                                                          \
132       {                                                                       \
133         /* Use the IBM930 table for single byte.  */                          \
134         res = __ibm930sb_to_ucs4[ch];                                         \
135         if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0')             \
136           {                                                                   \
137             /* This is an illegal character.  */                              \
138             STANDARD_FROM_LOOP_ERR_HANDLER (1);                               \
139           }                                                                   \
140         else                                                                  \
141           {                                                                   \
142             put32 (outptr, res);                                              \
143             outptr += 4;                                                      \
144           }                                                                   \
145         ++inptr;                                                              \
146       }                                                                       \
147     else                                                                      \
148       {                                                                       \
149         /* Use the IBM930 table for double byte. */                           \
150         const struct gap *rp2 = __ibm930db_to_ucs4_idx;                       \
151                                                                               \
152         assert (curcs == db);                                                 \
153                                                                               \
154         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
155           {                                                                   \
156             /* The second character is not available.  Store the              \
157                intermediate result. */                                        \
158             result = __GCONV_INCOMPLETE_INPUT;                                \
159             break;                                                            \
160           }                                                                   \
161                                                                               \
162         ch = (ch * 0x100) + inptr[1];                                         \
163         while (ch > rp2->end)                                                 \
164           ++rp2;                                                              \
165                                                                               \
166         if (__builtin_expect (ch < rp2->start, 0)                             \
167             || (res = __ibm930db_to_ucs4[ch + rp2->idx],                      \
168                 __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
169           {                                                                   \
170             /* This is an illegal character.  */                              \
171             STANDARD_FROM_LOOP_ERR_HANDLER (2);                               \
172           }                                                                   \
173         else                                                                  \
174           {                                                                   \
175             put32 (outptr, res);                                              \
176             outptr += 4;                                                      \
177           }                                                                   \
178         inptr += 2;                                                           \
179       }                                                                       \
180   }
181 #define LOOP_NEED_FLAGS
182 #define EXTRA_LOOP_DECLS        , int *curcsp
183 #define INIT_PARAMS             int curcs = *curcsp & ~7
184 #define UPDATE_PARAMS           *curcsp = curcs
185 #include <iconv/loop.c>
186
187 /* Next, define the other direction.  */
188 #define MIN_NEEDED_INPUT        TO_LOOP_MIN_NEEDED_FROM
189 #define MAX_NEEDED_INPUT        TO_LOOP_MAX_NEEDED_FROM
190 #define MIN_NEEDED_OUTPUT       TO_LOOP_MIN_NEEDED_TO
191 #define MAX_NEEDED_OUTPUT       TO_LOOP_MAX_NEEDED_TO
192 #define LOOPFCT                 TO_LOOP
193 #define BODY \
194   {                                                                           \
195     uint32_t ch = get32 (inptr);                                              \
196     const struct gap *rp1 = __ucs4_to_ibm930sb_idx;                           \
197     const struct gap *rp2 = __ucs4_to_ibm930db_idx;                           \
198     const char *cp;                                                           \
199                                                                               \
200     if (__builtin_expect (ch >= 0xffff, 0))                                   \
201       {                                                                       \
202         UNICODE_TAG_HANDLER (ch, 4);                                          \
203                                                                               \
204         STANDARD_TO_LOOP_ERR_HANDLER (4);                                     \
205       }                                                                       \
206                                                                               \
207     while (ch > rp1->end)                                                     \
208       ++rp1;                                                                  \
209                                                                               \
210     /* Use the UCS4 table for single byte.  */                                \
211     if (__builtin_expect (ch < rp1->start, 0)                                 \
212         || (cp = __ucs4_to_ibm930sb[ch + rp1->idx],                           \
213             __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
214       {                                                                       \
215         /* Use the UCS4 table for double byte. */                             \
216         while (ch > rp2->end)                                                 \
217           ++rp2;                                                              \
218                                                                               \
219         if (__builtin_expect (ch < rp2->start, 0)                             \
220             || (cp = __ucs4_to_ibm930db[ch + rp2->idx],                       \
221                 __builtin_expect (cp[0], L'\1')== L'\0' && ch != '\0'))       \
222           {                                                                   \
223             /* This is an illegal character.  */                              \
224             STANDARD_TO_LOOP_ERR_HANDLER (4);                                 \
225           }                                                                   \
226         else                                                                  \
227           {                                                                   \
228             if (curcs == sb)                                                  \
229               {                                                               \
230                 if (__builtin_expect (outptr + 1 > outend, 0))                \
231                   {                                                           \
232                     result = __GCONV_FULL_OUTPUT;                             \
233                     break;                                                    \
234                   }                                                           \
235                 *outptr++ = SO;                                               \
236                 curcs = db;                                                   \
237               }                                                               \
238                                                                               \
239             if (__builtin_expect (outptr + 2 > outend, 0))                    \
240               {                                                               \
241                 result = __GCONV_FULL_OUTPUT;                                 \
242                 break;                                                        \
243               }                                                               \
244             *outptr++ = cp[0];                                                \
245             *outptr++ = cp[1];                                                \
246           }                                                                   \
247       }                                                                       \
248     else                                                                      \
249       {                                                                       \
250         if (curcs == db)                                                      \
251           {                                                                   \
252             if (__builtin_expect (outptr + 1 > outend, 0))                    \
253               {                                                               \
254                 result = __GCONV_FULL_OUTPUT;                                 \
255                 break;                                                        \
256               }                                                               \
257             *outptr++ = SI;                                                   \
258           }                                                                   \
259                                                                               \
260         if (__builtin_expect (outptr + 1 > outend, 0))                        \
261           {                                                                   \
262             result = __GCONV_FULL_OUTPUT;                                     \
263             break;                                                            \
264           }                                                                   \
265         if (ch == 0x7e)                                                       \
266           *outptr++ = 0xa1;                                                   \
267         else if (ch == 0x5c)                                                  \
268           *outptr++ = 0x5b;                                                   \
269         else                                                                  \
270           *outptr++ = cp[0];                                                  \
271         curcs = sb;                                                           \
272       }                                                                       \
273                                                                               \
274     /* Now that we wrote the output increment the input pointer.  */          \
275     inptr += 4;                                                               \
276   }
277 #define LOOP_NEED_FLAGS
278 #define EXTRA_LOOP_DECLS        , int *curcsp
279 #define INIT_PARAMS             int curcs = *curcsp & ~7
280 #define REINIT_PARAMS           curcs = *curcsp & ~7
281 #define UPDATE_PARAMS           *curcsp = curcs
282 #include <iconv/loop.c>
283
284 /* Now define the toplevel functions.  */
285 #include <iconv/skeleton.c>