1 /* Conversion from and to Shift_JISX0213.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Bruno Haible <bruno@clisp.org>, 2002.
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
25 /* The structure of Shift_JISX0213 is as follows:
27 0x00..0x7F: ISO646-JP, an ASCII variant
29 0x{A1..DF}: JISX0201 Katakana.
31 0x{81..9F,E0..EF}{40..7E,80..FC}: JISX0213 plane 1.
33 0x{F0..FC}{40..7E,80..FC}: JISX0213 plane 2, with irregular row mapping.
35 Note that some JISX0213 characters are not contained in Unicode 3.2
36 and are therefore best represented as sequences of Unicode characters.
41 /* Definitions used in the body of the `gconv' function. */
42 #define CHARSET_NAME "SHIFT_JISX0213//"
43 #define FROM_LOOP from_shift_jisx0213
44 #define TO_LOOP to_shift_jisx0213
47 #define FROM_LOOP_MIN_NEEDED_FROM 1
48 #define FROM_LOOP_MAX_NEEDED_FROM 2
49 #define FROM_LOOP_MIN_NEEDED_TO 4
50 #define FROM_LOOP_MAX_NEEDED_TO 8
51 #define TO_LOOP_MIN_NEEDED_FROM 4
52 #define TO_LOOP_MAX_NEEDED_FROM 4
53 #define TO_LOOP_MIN_NEEDED_TO 1
54 #define TO_LOOP_MAX_NEEDED_TO 2
55 #define PREPARE_LOOP \
57 int *statep = &data->__statep->__count;
58 #define EXTRA_LOOP_ARGS , statep
61 /* Since we might have to reset input pointer we must be able to save
62 and restore the state. */
63 #define SAVE_RESET_STATE(Save) \
65 saved_state = *statep; \
70 /* During UCS-4 to Shift_JISX0213 conversion, the COUNT element of the state
71 contains the last two bytes to be output, shifted by 3 bits. */
73 /* Since this is a stateful encoding we have to provide code which resets
74 the output state to the initial state. This has to be done during the
76 #define EMIT_SHIFT_TO_INIT \
77 if (data->__statep->__count != 0) \
80 /* We don't use shift states in the FROM_DIRECTION. */ \
81 data->__statep->__count = 0; \
84 if (__builtin_expect (outbuf + 2 <= outend, 1)) \
86 /* Write out the last character. */ \
87 uint32_t lasttwo = data->__statep->__count >> 3; \
88 *outbuf++ = (lasttwo >> 8) & 0xff; \
89 *outbuf++ = lasttwo & 0xff; \
90 data->__statep->__count = 0; \
93 /* We don't have enough room in the output buffer. */ \
94 status = __GCONV_FULL_OUTPUT; \
99 /* First define the conversion function from Shift_JISX0213 to UCS-4. */
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
107 uint32_t ch = *inptr; \
111 /* Plain ISO646-JP character. */ \
112 if (__builtin_expect (ch == 0x5c, 0)) \
114 else if (__builtin_expect (ch == 0x7e, 0)) \
118 else if (ch >= 0xa1 && ch <= 0xdf) \
120 /* Half-width katakana. */ \
124 else if ((ch >= 0x81 && ch <= 0x9f) || (ch >= 0xe0 && ch <= 0xfc)) \
126 /* Two byte character. */ \
129 if (__builtin_expect (inptr + 1 >= inend, 0)) \
131 /* The second byte is not available. */ \
132 result = __GCONV_INCOMPLETE_INPUT; \
138 /* The second byte must be in the range 0x{40..7E,80..FC}. */ \
139 if (__builtin_expect (ch2 < 0x40 || ch2 == 0x7f || ch2 > 0xfc, 0)) \
141 /* This is an illegal character. */ \
142 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
145 /* Convert to row and column. */ \
154 /* Now 0 <= ch <= 0x3b, 0 <= ch2 <= 0xbb. */ \
161 /* Handling of JISX 0213 plane 2 rows. */ \
164 else if (ch >= 0x63 || ch == 0x5f) \
170 ch = jisx0213_to_ucs4 (0x121 + ch, ch2); \
174 /* This is an illegal character. */ \
175 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
180 /* It's a combining character. */ \
181 uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0]; \
182 uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1]; \
184 /* See whether we have room for two characters. */ \
185 if (outptr + 8 <= outend) \
188 put32 (outptr, u1); \
190 put32 (outptr, u2); \
196 result = __GCONV_FULL_OUTPUT; \
205 /* This is illegal. */ \
206 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
209 put32 (outptr, ch); \
212 #define LOOP_NEED_FLAGS
213 #define EXTRA_LOOP_DECLS , int *statep
214 #include <iconv/loop.c>
217 /* Next, define the other direction, from UCS-4 to Shift_JISX0213. */
219 /* Composition tables for each of the relevant combining characters. */
224 } comp_table_data
[] =
226 #define COMP_TABLE_IDX_02E5 0
227 #define COMP_TABLE_LEN_02E5 1
228 { 0x8684, 0x8685 }, /* 0x12B65 = 0x12B64 U+02E5 */
229 #define COMP_TABLE_IDX_02E9 (COMP_TABLE_IDX_02E5 + COMP_TABLE_LEN_02E5)
230 #define COMP_TABLE_LEN_02E9 1
231 { 0x8680, 0x8686 }, /* 0x12B66 = 0x12B60 U+02E9 */
232 #define COMP_TABLE_IDX_0300 (COMP_TABLE_IDX_02E9 + COMP_TABLE_LEN_02E9)
233 #define COMP_TABLE_LEN_0300 5
234 { 0x857b, 0x8663 }, /* 0x12B44 = 0x1295C U+0300 */
235 { 0x8657, 0x8667 }, /* 0x12B48 = 0x12B38 U+0300 */
236 { 0x8656, 0x8669 }, /* 0x12B4A = 0x12B37 U+0300 */
237 { 0x864f, 0x866b }, /* 0x12B4C = 0x12B30 U+0300 */
238 { 0x8662, 0x866d }, /* 0x12B4E = 0x12B43 U+0300 */
239 #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300)
240 #define COMP_TABLE_LEN_0301 4
241 { 0x8657, 0x8668 }, /* 0x12B49 = 0x12B38 U+0301 */
242 { 0x8656, 0x866a }, /* 0x12B4B = 0x12B37 U+0301 */
243 { 0x864f, 0x866c }, /* 0x12B4D = 0x12B30 U+0301 */
244 { 0x8662, 0x866e }, /* 0x12B4F = 0x12B43 U+0301 */
245 #define COMP_TABLE_IDX_309A (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301)
246 #define COMP_TABLE_LEN_309A 14
247 { 0x82a9, 0x82f5 }, /* 0x12477 = 0x1242B U+309A */
248 { 0x82ab, 0x82f6 }, /* 0x12478 = 0x1242D U+309A */
249 { 0x82ad, 0x82f7 }, /* 0x12479 = 0x1242F U+309A */
250 { 0x82af, 0x82f8 }, /* 0x1247A = 0x12431 U+309A */
251 { 0x82b1, 0x82f9 }, /* 0x1247B = 0x12433 U+309A */
252 { 0x834a, 0x8397 }, /* 0x12577 = 0x1252B U+309A */
253 { 0x834c, 0x8398 }, /* 0x12578 = 0x1252D U+309A */
254 { 0x834e, 0x8399 }, /* 0x12579 = 0x1252F U+309A */
255 { 0x8350, 0x839a }, /* 0x1257A = 0x12531 U+309A */
256 { 0x8352, 0x839b }, /* 0x1257B = 0x12533 U+309A */
257 { 0x835a, 0x839c }, /* 0x1257C = 0x1253B U+309A */
258 { 0x8363, 0x839d }, /* 0x1257D = 0x12544 U+309A */
259 { 0x8367, 0x839e }, /* 0x1257E = 0x12548 U+309A */
260 { 0x83f3, 0x83f6 }, /* 0x12678 = 0x12675 U+309A */
263 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
264 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
265 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
266 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
267 #define LOOPFCT TO_LOOP
270 uint32_t ch = get32 (inptr); \
272 if ((*statep >> 3) != 0) \
274 /* Attempt to combine the last character with this one. */ \
275 uint16_t lasttwo = *statep >> 3; \
280 idx = COMP_TABLE_IDX_02E5, len = COMP_TABLE_LEN_02E5; \
281 else if (ch == 0x02e9) \
282 idx = COMP_TABLE_IDX_02E9, len = COMP_TABLE_LEN_02E9; \
283 else if (ch == 0x0300) \
284 idx = COMP_TABLE_IDX_0300, len = COMP_TABLE_LEN_0300; \
285 else if (ch == 0x0301) \
286 idx = COMP_TABLE_IDX_0301, len = COMP_TABLE_LEN_0301; \
287 else if (ch == 0x309a) \
288 idx = COMP_TABLE_IDX_309A, len = COMP_TABLE_LEN_309A; \
290 goto not_combining; \
293 if (comp_table_data[idx].base == lasttwo) \
295 while (++idx, --len > 0); \
299 /* Output the combined character. */ \
300 if (__builtin_expect (outptr + 1 >= outend, 0)) \
302 result = __GCONV_FULL_OUTPUT; \
305 lasttwo = comp_table_data[idx].composed; \
306 *outptr++ = (lasttwo >> 8) & 0xff; \
307 *outptr++ = lasttwo & 0xff; \
314 /* Output the buffered character. */ \
315 if (__builtin_expect (outptr + 1 >= outend, 0)) \
317 result = __GCONV_FULL_OUTPUT; \
320 *outptr++ = (lasttwo >> 8) & 0xff; \
321 *outptr++ = lasttwo & 0xff; \
327 /* Plain ISO646-JP character. */ \
329 else if (ch == 0xa5) \
331 else if (ch == 0x203e) \
333 else if (ch >= 0xff61 && ch <= 0xff9f) \
334 /* Half-width katakana. */ \
335 *outptr++ = ch - 0xfec0; \
338 unsigned int s1, s2; \
339 uint32_t jch = ucs4_to_jisx0213 (ch); \
342 UNICODE_TAG_HANDLER (ch, 4); \
344 /* Illegal character. */ \
345 STANDARD_TO_LOOP_ERR_HANDLER (4); \
348 /* Convert it to shifted representation. */ \
355 /* Handling of JISX 0213 plane 2 rows. */ \
356 if (s1 >= 0xcd) /* rows 0x26E..0x27E */ \
358 else if (s1 >= 0x8b || s1 == 0x87) /* rows 0x228, 0x22C..0x22F */ \
360 else /* rows 0x221, 0x223..0x225 */ \
362 /* Now 0x5e <= s1 <= 0x77. */ \
378 /* A possible match in comp_table_data. We have to buffer it. */\
380 /* We know it's a JISX 0213 plane 1 character. */ \
381 assert ((jch & 0x8000) == 0); \
383 *statep = ((s1 << 8) | s2) << 3; \
388 /* Output the shifted representation. */ \
389 if (__builtin_expect (outptr + 1 >= outend, 0)) \
391 result = __GCONV_FULL_OUTPUT; \
400 #define LOOP_NEED_FLAGS
401 #define EXTRA_LOOP_DECLS , int *statep
402 #include <iconv/loop.c>
405 /* Now define the toplevel functions. */
406 #include <iconv/skeleton.c>