Update.
[glibc.git] / iconvdata / ibm937.c
blobedfbf6683c20fc55d0fdeb3a947317b0ecede5e0
1 /* Conversion to and from IBM937.
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.
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.
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.
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. */
21 #include <dlfcn.h>
22 #include <stdint.h>
23 #include <wchar.h>
24 #include <byteswap.h>
25 #include "ibm937.h"
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. */
31 /* Definitions used in the body of the `gconv' function. */
32 #define CHARSET_NAME "IBM937//"
33 #define FROM_LOOP from_ibm937
34 #define TO_LOOP to_ibm937
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
44 /* Definitions of initialization and destructor function. */
45 #define DEFINE_INIT 1
46 #define DEFINE_FINI 1
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 /* We are not in the initial state. To switch back we have \
60 to emit `SI'. */ \
61 if (__builtin_expect (outbuf >= outend, 0)) \
62 /* We don't have enough room in the output buffer. */ \
63 status = __GCONV_FULL_OUTPUT; \
64 else \
65 { \
66 /* Write out the shift sequence. */ \
67 *outbuf++ = SI; \
68 data->__statep->__count &= 7; \
69 } \
70 } \
74 /* Since we might have to reset input pointer we must be able to save
75 and retore the state. */
76 #define SAVE_RESET_STATE(Save) \
77 if (Save) \
78 save_curcs = *curcsp; \
79 else \
80 *curcsp = save_curcs
83 /* Current codeset type. */
84 enum
86 sb = 0,
87 db = 64
90 /* First, define the conversion function from IBM-937 to UCS4. */
91 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
92 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
93 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
94 #define LOOPFCT FROM_LOOP
95 #define BODY \
96 { \
97 uint32_t ch = *inptr; \
99 if (__builtin_expect (ch, 0) == SO) \
101 /* Shift OUT, change to DBCS converter. */ \
102 if (curcs == db) \
104 result = __GCONV_ILLEGAL_INPUT; \
105 break; \
107 curcs = db; \
108 ++inptr; \
109 continue; \
111 else if (__builtin_expect (ch, 0) == SI) \
113 /* Shift IN, change to SBCS converter. */ \
114 if (curcs == sb) \
116 result = __GCONV_ILLEGAL_INPUT; \
117 break; \
119 curcs = sb; \
120 ++inptr; \
121 continue; \
124 if (curcs == sb) \
126 /* Use the UCS4 table for single byte. */ \
127 ch = __ibm937sb_to_ucs4[ch]; \
128 if (__builtin_expect (ch, L'\1') == L'\0' && *inptr != '\0') \
130 /* This is an illegal character. */ \
131 if (! ignore_errors_p ()) \
133 result = __GCONV_ILLEGAL_INPUT; \
134 break; \
136 ++*irreversible; \
138 else \
140 put32 (outptr, ch); \
141 outptr += 4; \
143 ++inptr; \
145 else \
147 /* Use the IBM937 table for double byte. */ \
149 assert (curcs == db); \
151 ch = ibm937db_to_ucs4(inptr[0], inptr[1]); \
152 if (__builtin_expect (ch, L'\1') == L'\0' && *inptr != '\0') \
154 /* This is an illegal character. */ \
155 if (! ignore_errors_p ()) \
157 result = __GCONV_ILLEGAL_INPUT; \
158 break; \
160 ++*irreversible; \
162 else \
164 put32 (outptr, ch); \
165 outptr += 4; \
167 inptr += 2; \
170 #define LOOP_NEED_FLAGS
171 #define EXTRA_LOOP_DECLS , int *curcsp
172 #define INIT_PARAMS int curcs = *curcsp & ~7
173 #define UPDATE_PARAMS *curcsp = curcs
174 #include <iconv/loop.c>
176 /* Next, define the other direction. */
177 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
178 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
179 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
180 #define LOOPFCT TO_LOOP
181 #define BODY \
183 uint32_t ch = get32 (inptr); \
184 const char *cp; \
186 /* Use the UCS4 table for single byte. */ \
187 cp = __ucs4_to_ibm937sb[ch]; \
188 if (__builtin_expect (ch >= (sizeof (__ucs4_to_ibm937sb) \
189 / sizeof (__ucs4_to_ibm937sb[0])), 0) \
190 || (__builtin_expect (cp[0], '\1') == '\0' && ch != 0)) \
192 /* Use the UCS4 table for double byte. */ \
193 cp = __ucs4_to_ibm937db[ch]; \
194 if (__builtin_expect (ch >= (sizeof (__ucs4_to_ibm937db) \
195 / sizeof (__ucs4_to_ibm937db[0])), 0) \
196 || __builtin_expect (cp[0], '\1') == '\0') \
198 UNICODE_TAG_HANDLER (ch, 4); \
200 /* This is an illegal character. */ \
201 if (! ignore_errors_p ()) \
203 result = __GCONV_ILLEGAL_INPUT; \
204 break; \
206 ++*irreversible; \
208 else \
210 if (curcs == sb) \
212 *outptr++ = SO; \
213 curcs = db; \
215 if (__builtin_expect (outptr + 1 >= outend, 0)) \
217 result = __GCONV_FULL_OUTPUT; \
218 break; \
220 *outptr++ = cp[0]; \
221 *outptr++ = cp[1]; \
224 else \
226 if (curcs == db) \
228 *outptr++ = SI; \
229 curcs = sb; \
230 if (__builtin_expect (outptr == outend, 0)) \
232 result = __GCONV_FULL_OUTPUT; \
233 break; \
236 *outptr++ = cp[0]; \
239 /* Now that we wrote the output increment the input pointer. */ \
240 inptr += 4; \
242 #define LOOP_NEED_FLAGS
243 #define EXTRA_LOOP_DECLS , int *curcsp
244 #define INIT_PARAMS int curcs = *curcsp & ~7
245 #define UPDATE_PARAMS *curcsp = curcs
246 #include <iconv/loop.c>
248 /* Now define the toplevel functions. */
249 #include <iconv/skeleton.c>