]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconvdata/ibm933.c
Comment on IBM930, IBM933, IBM935, IBM937, IBM939.
[thirdparty/glibc.git] / iconvdata / ibm933.c
1 /* Conversion from and to IBM933.
2 Copyright (C) 2000-2015 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, see
18 <http://www.gnu.org/licenses/>. */
19
20 /* IBM933 is designed for the representation of Korean using a stateful
21 EBCDIC encoding scheme. It is also known as CCSID 933 or CP933. See:
22 https://www-01.ibm.com/software/globalization/ccsid/ccsid933.html */
23
24 #include <dlfcn.h>
25 #include <stdint.h>
26 #include <wchar.h>
27 #include <byteswap.h>
28 #include "ibm933.h"
29
30 /* The shift sequences for this charset (it does not use ESC). */
31 #define SI 0x0F /* Shift In, host code to turn DBCS off. */
32 #define SO 0x0E /* Shift Out, host code to turn DBCS on. */
33
34 /* Definitions used in the body of the `gconv' function. */
35 #define CHARSET_NAME "IBM933//"
36 #define FROM_LOOP from_ibm933
37 #define TO_LOOP to_ibm933
38 #define ONE_DIRECTION 0
39 #define FROM_LOOP_MIN_NEEDED_FROM 1
40 #define FROM_LOOP_MAX_NEEDED_FROM 2
41 #define FROM_LOOP_MIN_NEEDED_TO 4
42 #define FROM_LOOP_MAX_NEEDED_TO 4
43 #define TO_LOOP_MIN_NEEDED_FROM 4
44 #define TO_LOOP_MAX_NEEDED_FROM 4
45 #define TO_LOOP_MIN_NEEDED_TO 1
46 #define TO_LOOP_MAX_NEEDED_TO 3
47 #define PREPARE_LOOP \
48 int save_curcs; \
49 int *curcsp = &data->__statep->__count;
50 #define EXTRA_LOOP_ARGS , curcsp
51
52 /* Definitions of initialization and destructor function. */
53 #define DEFINE_INIT 1
54 #define DEFINE_FINI 1
55
56
57 /* Since this is a stateful encoding we have to provide code which resets
58 the output state to the initial state. This has to be done during the
59 flushing. */
60 #define EMIT_SHIFT_TO_INIT \
61 if ((data->__statep->__count & ~7) != sb) \
62 { \
63 if (FROM_DIRECTION) \
64 data->__statep->__count &= 7; \
65 else \
66 { \
67 /* We are not in the initial state. To switch back we have \
68 to emit `SI'. */ \
69 if (__glibc_unlikely (outbuf >= outend)) \
70 /* We don't have enough room in the output buffer. */ \
71 status = __GCONV_FULL_OUTPUT; \
72 else \
73 { \
74 /* Write out the shift sequence. */ \
75 *outbuf++ = SI; \
76 data->__statep->__count &= 7; \
77 } \
78 } \
79 }
80
81
82 /* Since we might have to reset input pointer we must be able to save
83 and retore the state. */
84 #define SAVE_RESET_STATE(Save) \
85 if (Save) \
86 save_curcs = *curcsp; \
87 else \
88 *curcsp = save_curcs
89
90
91 /* Current codeset type. */
92 enum
93 {
94 sb = 0,
95 db = 64
96 };
97
98 /* First, define the conversion function from IBM-933 to UCS4. */
99 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
100 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
101 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
102 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
103 #define LOOPFCT FROM_LOOP
104 #define BODY \
105 { \
106 uint32_t ch = *inptr; \
107 uint32_t res; \
108 \
109 if (__builtin_expect (ch, 0) == SO) \
110 { \
111 /* Shift OUT, change to DBCS converter. */ \
112 if (curcs == db) \
113 { \
114 result = __GCONV_ILLEGAL_INPUT; \
115 break; \
116 } \
117 curcs = db; \
118 ++inptr; \
119 continue; \
120 } \
121 else if (__builtin_expect (ch, 0) == SI) \
122 { \
123 /* Shift IN, change to SBCS converter. */ \
124 if (curcs == sb) \
125 { \
126 result = __GCONV_ILLEGAL_INPUT; \
127 break; \
128 } \
129 curcs = sb; \
130 ++inptr; \
131 continue; \
132 } \
133 \
134 if (curcs == sb) \
135 { \
136 /* Use the IBM933 table for single byte. */ \
137 res = __ibm933sb_to_ucs4[ch]; \
138 if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \
139 { \
140 /* This is an illegal character. */ \
141 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
142 } \
143 else \
144 { \
145 put32 (outptr, res); \
146 outptr += 4; \
147 } \
148 ++inptr; \
149 } \
150 else \
151 { \
152 const struct gap *rp2 = __ibm933db_to_ucs4_idx; \
153 \
154 assert (curcs == db); \
155 \
156 /* Use the IBM933 table for double byte. */ \
157 if (__glibc_unlikely (inptr + 1 >= inend)) \
158 { \
159 /* The second character is not available. Store the \
160 intermediate result. */ \
161 result = __GCONV_INCOMPLETE_INPUT; \
162 break; \
163 } \
164 \
165 ch = (ch * 0x100) + inptr[1]; \
166 while (ch > rp2->end) \
167 ++rp2; \
168 \
169 if (__builtin_expect (rp2->start == 0xffff, 0) \
170 || __builtin_expect (ch < rp2->start, 0) \
171 || (res = __ibm933db_to_ucs4[ch + rp2->idx], \
172 __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \
173 { \
174 /* This is an illegal character. */ \
175 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
176 } \
177 else \
178 { \
179 put32 (outptr, res); \
180 outptr += 4; \
181 inptr += 2; \
182 } \
183 } \
184 }
185 #define LOOP_NEED_FLAGS
186 #define EXTRA_LOOP_DECLS , int *curcsp
187 #define INIT_PARAMS int curcs = *curcsp & ~7
188 #define UPDATE_PARAMS *curcsp = curcs
189 #include <iconv/loop.c>
190
191 /* Next, define the other direction. */
192 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
193 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
194 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
195 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
196 #define LOOPFCT TO_LOOP
197 #define BODY \
198 { \
199 uint32_t ch = get32 (inptr); \
200 const struct gap *rp1 = __ucs4_to_ibm933sb_idx; \
201 const struct gap *rp2 = __ucs4_to_ibm933db_idx; \
202 const char *cp; \
203 \
204 if (__glibc_unlikely (ch >= 0xffff)) \
205 { \
206 UNICODE_TAG_HANDLER (ch, 4); \
207 \
208 STANDARD_TO_LOOP_ERR_HANDLER (4); \
209 } \
210 \
211 while (ch > rp1->end) \
212 ++rp1; \
213 \
214 /* Use the UCS4 table for single byte. */ \
215 if (__builtin_expect (ch < rp1->start, 0) \
216 || (cp = __ucs4_to_ibm933sb[ch + rp1->idx], \
217 __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0')) \
218 { \
219 /* Use the UCS4 table for double byte. */ \
220 while (ch > rp2->end) \
221 ++rp2; \
222 \
223 if (__builtin_expect (ch < rp2->start, 0) \
224 || (cp = __ucs4_to_ibm933db[ch + rp2->idx], \
225 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0')) \
226 { \
227 /* This is an illegal character. */ \
228 STANDARD_TO_LOOP_ERR_HANDLER (4); \
229 } \
230 else \
231 { \
232 if (curcs == sb) \
233 { \
234 if (__glibc_unlikely (outptr + 1 > outend)) \
235 { \
236 result = __GCONV_FULL_OUTPUT; \
237 break; \
238 } \
239 *outptr++ = SO; \
240 curcs = db; \
241 } \
242 \
243 if (__glibc_unlikely (outptr + 2 > outend)) \
244 { \
245 result = __GCONV_FULL_OUTPUT; \
246 break; \
247 } \
248 *outptr++ = cp[0]; \
249 *outptr++ = cp[1]; \
250 } \
251 } \
252 else \
253 { \
254 if (curcs == db) \
255 { \
256 if (__glibc_unlikely (outptr + 1 > outend)) \
257 { \
258 result = __GCONV_FULL_OUTPUT; \
259 break; \
260 } \
261 *outptr++ = SI; \
262 } \
263 \
264 if (__glibc_unlikely (outptr + 1 > outend)) \
265 { \
266 result = __GCONV_FULL_OUTPUT; \
267 break; \
268 } \
269 *outptr++ = cp[0]; \
270 curcs = sb; \
271 } \
272 \
273 /* Now that we wrote the output increment the input pointer. */ \
274 inptr += 4; \
275 }
276 #define LOOP_NEED_FLAGS
277 #define EXTRA_LOOP_DECLS , int *curcsp
278 #define INIT_PARAMS int curcs = *curcsp & ~7
279 #define REINIT_PARAMS curcs = *curcsp & ~7
280 #define UPDATE_PARAMS *curcsp = curcs
281 #include <iconv/loop.c>
282
283 /* Now define the toplevel functions. */
284 #include <iconv/skeleton.c>