Fix build errors with -DNDEBUG.
[glibc.git] / iconvdata / ibm937.c
blob88344a445b158f3c3ceff8e8e36c8e51706a51f6
1 /* Conversion from and to IBM937.
2 Copyright (C) 2000-2016 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, see
18 <http://www.gnu.org/licenses/>. */
20 /* IBM937 is designed for the representation of Traditional Chinese
21 using a stateful EBCDIC encoding scheme. It is also known as
22 CCSID 937 or CP937. See:
23 https://www-01.ibm.com/software/globalization/ccsid/ccsid937.html */
25 #include <dlfcn.h>
26 #include <stdint.h>
27 #include <wchar.h>
28 #include <byteswap.h>
29 #include "ibm937.h"
31 /* The shift sequences for this charset (it does not use ESC). */
32 #define SI 0x0F /* Shift In, host code to turn DBCS off. */
33 #define SO 0x0E /* Shift Out, host code to turn DBCS on. */
35 /* Definitions used in the body of the `gconv' function. */
36 #define CHARSET_NAME "IBM937//"
37 #define FROM_LOOP from_ibm937
38 #define TO_LOOP to_ibm937
39 #define ONE_DIRECTION 0
40 #define FROM_LOOP_MIN_NEEDED_FROM 1
41 #define FROM_LOOP_MAX_NEEDED_FROM 2
42 #define FROM_LOOP_MIN_NEEDED_TO 4
43 #define FROM_LOOP_MAX_NEEDED_TO 4
44 #define TO_LOOP_MIN_NEEDED_FROM 4
45 #define TO_LOOP_MAX_NEEDED_FROM 4
46 #define TO_LOOP_MIN_NEEDED_TO 1
47 #define TO_LOOP_MAX_NEEDED_TO 3
48 #define PREPARE_LOOP \
49 int save_curcs; \
50 int *curcsp = &data->__statep->__count;
51 #define EXTRA_LOOP_ARGS , curcsp
53 /* Definitions of initialization and destructor function. */
54 #define DEFINE_INIT 1
55 #define DEFINE_FINI 1
58 /* Since this is a stateful encoding we have to provide code which resets
59 the output state to the initial state. This has to be done during the
60 flushing. */
61 #define EMIT_SHIFT_TO_INIT \
62 if ((data->__statep->__count & ~7) != sb) \
63 { \
64 if (FROM_DIRECTION) \
65 data->__statep->__count &= 7; \
66 else \
67 { \
68 /* We are not in the initial state. To switch back we have \
69 to emit `SI'. */ \
70 if (__glibc_unlikely (outbuf >= outend)) \
71 /* We don't have enough room in the output buffer. */ \
72 status = __GCONV_FULL_OUTPUT; \
73 else \
74 { \
75 /* Write out the shift sequence. */ \
76 *outbuf++ = SI; \
77 data->__statep->__count &= 7; \
78 } \
79 } \
83 /* Since we might have to reset input pointer we must be able to save
84 and retore the state. */
85 #define SAVE_RESET_STATE(Save) \
86 if (Save) \
87 save_curcs = *curcsp; \
88 else \
89 *curcsp = save_curcs
92 /* Current codeset type. */
93 enum
95 sb = 0,
96 db = 64
99 /* First, define the conversion function from IBM-937 to UCS4. */
100 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
101 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
102 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
103 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
104 #define LOOPFCT FROM_LOOP
105 #define BODY \
107 uint32_t ch = *inptr; \
108 uint32_t res; \
110 if (__builtin_expect (ch, 0) == SO) \
112 /* Shift OUT, change to DBCS converter. */ \
113 if (curcs == db) \
115 result = __GCONV_ILLEGAL_INPUT; \
116 break; \
118 curcs = db; \
119 ++inptr; \
120 continue; \
122 else if (__builtin_expect (ch, 0) == SI) \
124 /* Shift IN, change to SBCS converter. */ \
125 if (curcs == sb) \
127 result = __GCONV_ILLEGAL_INPUT; \
128 break; \
130 curcs = sb; \
131 ++inptr; \
132 continue; \
135 if (curcs == sb) \
137 /* Use the IBM937 table for single byte. */ \
138 res = __ibm937sb_to_ucs4[ch]; \
139 if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \
141 /* This is an illegal character. */ \
142 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
144 else \
146 put32 (outptr, res); \
147 outptr += 4; \
149 ++inptr; \
151 else \
153 const struct gap *rp2 = __ibm937db_to_ucs4_idx; \
155 assert (curcs == db); \
157 /* Use the IBM937 table for double byte. */ \
158 if (__glibc_unlikely (inptr + 1 >= inend)) \
160 /* The second character is not available. \
161 Store the intermediate result. */ \
162 result = __GCONV_INCOMPLETE_INPUT; \
163 break; \
166 ch = (ch * 0x100) + inptr[1]; \
167 while (ch > rp2->end) \
168 ++rp2; \
170 if (__builtin_expect (rp2->start == 0xffff, 0) \
171 || __builtin_expect (ch < rp2->start, 0) \
172 || (res = __ibm937db_to_ucs4[ch + rp2->idx], \
173 __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \
175 /* This is an illegal character. */ \
176 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
178 else \
180 put32 (outptr, res); \
181 outptr += 4; \
183 inptr += 2; \
186 #define LOOP_NEED_FLAGS
187 #define EXTRA_LOOP_DECLS , int *curcsp
188 #define INIT_PARAMS int curcs = *curcsp & ~7
189 #define UPDATE_PARAMS *curcsp = curcs
190 #include <iconv/loop.c>
192 /* Next, define the other direction. */
193 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
194 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
195 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
196 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
197 #define LOOPFCT TO_LOOP
198 #define BODY \
200 uint32_t ch = get32 (inptr); \
201 const struct gap *rp1 = __ucs4_to_ibm937sb_idx; \
202 const struct gap *rp2 = __ucs4_to_ibm937db_idx; \
203 const char *cp; \
205 if (__glibc_unlikely (ch >= 0xffff)) \
207 UNICODE_TAG_HANDLER (ch, 4); \
209 STANDARD_TO_LOOP_ERR_HANDLER (4); \
212 while (ch > rp1->end) \
213 ++rp1; \
215 /* Use the UCS4 table for single byte. */ \
216 if (__builtin_expect (ch < rp1->start, 0) \
217 || (cp = __ucs4_to_ibm937sb[ch + rp1->idx], \
218 __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0')) \
220 /* Use the UCS4 table for double byte. */ \
221 while (ch > rp2->end) \
222 ++rp2; \
224 if (__builtin_expect (ch < rp2->start, 0) \
225 || (cp = __ucs4_to_ibm937db[ch + rp2->idx], \
226 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0')) \
228 /* This is an illegal character. */ \
229 STANDARD_TO_LOOP_ERR_HANDLER (4); \
231 else \
233 if (curcs == sb) \
235 if (__glibc_unlikely (outptr + 1 > outend)) \
237 result = __GCONV_FULL_OUTPUT; \
238 break; \
240 *outptr++ = SO; \
241 curcs = db; \
244 if (__glibc_unlikely (outptr + 2 > outend)) \
246 result = __GCONV_FULL_OUTPUT; \
247 break; \
249 *outptr++ = cp[0]; \
250 *outptr++ = cp[1]; \
253 else \
255 if (curcs == db) \
257 if (__glibc_unlikely (outptr + 1 > outend)) \
259 result = __GCONV_FULL_OUTPUT; \
260 break; \
262 *outptr++ = SI; \
263 curcs = sb; \
266 if (__glibc_unlikely (outptr + 1 > outend)) \
268 result = __GCONV_FULL_OUTPUT; \
269 break; \
271 *outptr++ = cp[0]; \
274 /* Now that we wrote the output increment the input pointer. */ \
275 inptr += 4; \
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>
284 /* Now define the toplevel functions. */
285 #include <iconv/skeleton.c>