Remove *xattr syscalls.
[glibc.git] / iconvdata / euc-jisx0213.c
blob8a41756a354e1535d6faa36ea899da6e5d4d4648
1 /* Conversion from and to EUC-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
19 02111-1307 USA. */
21 #include <dlfcn.h>
22 #include <stdint.h>
23 #include <gconv.h>
25 /* The structure of EUC-JISX0213 is as follows:
27 0x00..0x7F: ASCII
29 0x8E{A1..FE}: JISX0201 Katakana, with prefix 0x8E, offset by +0x80.
31 0x8F{A1..FE}{A1..FE}: JISX0213 plane 2, with prefix 0x8F, offset by +0x8080.
33 0x{A1..FE}{A1..FE}: JISX0213 plane 1, offset by +0x8080.
35 Note that some JISX0213 characters are not contained in Unicode 3.2
36 and are therefore best represented as sequences of Unicode characters.
39 #include "jisx0213.h"
41 /* Definitions used in the body of the `gconv' function. */
42 #define CHARSET_NAME "EUC-JISX0213//"
43 #define FROM_LOOP from_euc_jisx0213
44 #define TO_LOOP to_euc_jisx0213
45 #define DEFINE_INIT 1
46 #define DEFINE_FINI 1
47 #define FROM_LOOP_MIN_NEEDED_FROM 1
48 #define FROM_LOOP_MAX_NEEDED_FROM 3
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 3
55 #define PREPARE_LOOP \
56 int saved_state; \
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) \
64 if (Save) \
65 saved_state = *statep; \
66 else \
67 *statep = saved_state
70 /* During UCS-4 to EUC-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
75 flushing. */
76 #define EMIT_SHIFT_TO_INIT \
77 if (data->__statep->__count != 0) \
78 { \
79 if (FROM_DIRECTION) \
80 /* We don't use shift states in the FROM_DIRECTION. */ \
81 data->__statep->__count = 0; \
82 else \
83 { \
84 if (__builtin_expect (outbuf + 2 <= outend, 1)) \
85 { \
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; \
91 } \
92 else \
93 /* We don't have enough room in the output buffer. */ \
94 status = __GCONV_FULL_OUTPUT; \
95 } \
99 /* First define the conversion function from EUC-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
105 #define BODY \
107 uint32_t ch = *inptr; \
109 if (ch < 0x80) \
110 /* Plain ASCII character. */ \
111 ++inptr; \
112 else if ((ch >= 0xa1 && ch <= 0xfe) || ch == 0x8e || ch == 0x8f) \
114 /* Two or three byte character. */ \
115 uint32_t ch2; \
117 if (__builtin_expect (inptr + 1 >= inend, 0)) \
119 /* The second byte is not available. */ \
120 result = __GCONV_INCOMPLETE_INPUT; \
121 break; \
124 ch2 = inptr[1]; \
126 /* The second byte must be >= 0xa1 and <= 0xfe. */ \
127 if (__builtin_expect (ch2 < 0xa1 || ch2 > 0xfe, 0)) \
129 /* This is an illegal character. */ \
130 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
133 if (ch == 0x8e) \
135 /* Half-width katakana. */ \
136 if (__builtin_expect (ch2 > 0xdf, 0)) \
137 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
139 ch = ch2 + 0xfec0; \
140 inptr += 2; \
142 else \
144 const unsigned char *endp; \
146 if (ch == 0x8f) \
148 /* JISX 0213 plane 2. */ \
149 uint32_t ch3; \
151 if (__builtin_expect (inptr + 2 >= inend, 0)) \
153 /* The third byte is not available. */ \
154 result = __GCONV_INCOMPLETE_INPUT; \
155 break; \
158 ch3 = inptr[2]; \
159 endp = inptr + 3; \
161 ch = jisx0213_to_ucs4 (0x200 - 0x80 + ch2, ch3 ^ 0x80); \
163 else \
165 /* JISX 0213 plane 1. */ \
166 endp = inptr + 2; \
168 ch = jisx0213_to_ucs4 (0x100 - 0x80 + ch, ch2 ^ 0x80); \
171 if (ch == 0) \
172 /* This is an illegal character. */ \
173 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
175 if (ch < 0x80) \
177 /* It's a combining character. */ \
178 uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0]; \
179 uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1]; \
181 /* See whether we have room for two characters. */ \
182 if (outptr + 8 <= outend) \
184 inptr = endp; \
185 put32 (outptr, u1); \
186 outptr += 4; \
187 put32 (outptr, u2); \
188 outptr += 4; \
189 continue; \
191 else \
193 result = __GCONV_FULL_OUTPUT; \
194 break; \
198 inptr = endp; \
201 else \
203 /* This is illegal. */ \
204 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
207 put32 (outptr, ch); \
208 outptr += 4; \
210 #define LOOP_NEED_FLAGS
211 #define EXTRA_LOOP_DECLS , int *statep
212 #include <iconv/loop.c>
215 /* Next, define the other direction, from UCS-4 to EUC-JISX0213. */
217 /* Composition tables for each of the relevant combining characters. */
218 static const struct
220 uint16_t base;
221 uint16_t composed;
222 } comp_table_data[] =
224 #define COMP_TABLE_IDX_02E5 0
225 #define COMP_TABLE_LEN_02E5 1
226 { 0xabe4, 0xabe5 }, /* 0x12B65 = 0x12B64 U+02E5 */
227 #define COMP_TABLE_IDX_02E9 (COMP_TABLE_IDX_02E5 + COMP_TABLE_LEN_02E5)
228 #define COMP_TABLE_LEN_02E9 1
229 { 0xabe0, 0xabe6 }, /* 0x12B66 = 0x12B60 U+02E9 */
230 #define COMP_TABLE_IDX_0300 (COMP_TABLE_IDX_02E9 + COMP_TABLE_LEN_02E9)
231 #define COMP_TABLE_LEN_0300 5
232 { 0xa9dc, 0xabc4 }, /* 0x12B44 = 0x1295C U+0300 */
233 { 0xabb8, 0xabc8 }, /* 0x12B48 = 0x12B38 U+0300 */
234 { 0xabb7, 0xabca }, /* 0x12B4A = 0x12B37 U+0300 */
235 { 0xabb0, 0xabcc }, /* 0x12B4C = 0x12B30 U+0300 */
236 { 0xabc3, 0xabce }, /* 0x12B4E = 0x12B43 U+0300 */
237 #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300)
238 #define COMP_TABLE_LEN_0301 4
239 { 0xabb8, 0xabc9 }, /* 0x12B49 = 0x12B38 U+0301 */
240 { 0xabb7, 0xabcb }, /* 0x12B4B = 0x12B37 U+0301 */
241 { 0xabb0, 0xabcd }, /* 0x12B4D = 0x12B30 U+0301 */
242 { 0xabc3, 0xabcf }, /* 0x12B4F = 0x12B43 U+0301 */
243 #define COMP_TABLE_IDX_309A (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301)
244 #define COMP_TABLE_LEN_309A 14
245 { 0xa4ab, 0xa4f7 }, /* 0x12477 = 0x1242B U+309A */
246 { 0xa4ad, 0xa4f8 }, /* 0x12478 = 0x1242D U+309A */
247 { 0xa4af, 0xa4f9 }, /* 0x12479 = 0x1242F U+309A */
248 { 0xa4b1, 0xa4fa }, /* 0x1247A = 0x12431 U+309A */
249 { 0xa4b3, 0xa4fb }, /* 0x1247B = 0x12433 U+309A */
250 { 0xa5ab, 0xa5f7 }, /* 0x12577 = 0x1252B U+309A */
251 { 0xa5ad, 0xa5f8 }, /* 0x12578 = 0x1252D U+309A */
252 { 0xa5af, 0xa5f9 }, /* 0x12579 = 0x1252F U+309A */
253 { 0xa5b1, 0xa5fa }, /* 0x1257A = 0x12531 U+309A */
254 { 0xa5b3, 0xa5fb }, /* 0x1257B = 0x12533 U+309A */
255 { 0xa5bb, 0xa5fc }, /* 0x1257C = 0x1253B U+309A */
256 { 0xa5c4, 0xa5fd }, /* 0x1257D = 0x12544 U+309A */
257 { 0xa5c8, 0xa5fe }, /* 0x1257E = 0x12548 U+309A */
258 { 0xa6f5, 0xa6f8 }, /* 0x12678 = 0x12675 U+309A */
261 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
262 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
263 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
264 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
265 #define LOOPFCT TO_LOOP
266 #define BODY \
268 uint32_t ch = get32 (inptr); \
270 if ((*statep >> 3) != 0) \
272 /* Attempt to combine the last character with this one. */ \
273 uint16_t lasttwo = *statep >> 3; \
274 unsigned int idx; \
275 unsigned int len; \
277 if (ch == 0x02e5) \
278 idx = COMP_TABLE_IDX_02E5, len = COMP_TABLE_LEN_02E5; \
279 else if (ch == 0x02e9) \
280 idx = COMP_TABLE_IDX_02E9, len = COMP_TABLE_LEN_02E9; \
281 else if (ch == 0x0300) \
282 idx = COMP_TABLE_IDX_0300, len = COMP_TABLE_LEN_0300; \
283 else if (ch == 0x0301) \
284 idx = COMP_TABLE_IDX_0301, len = COMP_TABLE_LEN_0301; \
285 else if (ch == 0x309a) \
286 idx = COMP_TABLE_IDX_309A, len = COMP_TABLE_LEN_309A; \
287 else \
288 goto not_combining; \
290 do \
291 if (comp_table_data[idx].base == lasttwo) \
292 break; \
293 while (++idx, --len > 0); \
295 if (len > 0) \
297 /* Output the combined character. */ \
298 if (__builtin_expect (outptr + 1 >= outend, 0)) \
300 result = __GCONV_FULL_OUTPUT; \
301 break; \
303 lasttwo = comp_table_data[idx].composed; \
304 *outptr++ = (lasttwo >> 8) & 0xff; \
305 *outptr++ = lasttwo & 0xff; \
306 *statep = 0; \
307 inptr += 4; \
308 continue; \
311 not_combining: \
312 /* Output the buffered character. */ \
313 if (__builtin_expect (outptr + 1 >= outend, 0)) \
315 result = __GCONV_FULL_OUTPUT; \
316 break; \
318 *outptr++ = (lasttwo >> 8) & 0xff; \
319 *outptr++ = lasttwo & 0xff; \
320 *statep = 0; \
321 continue; \
324 if (ch < 0x80) \
325 /* Plain ASCII character. */ \
326 *outptr++ = ch; \
327 else if (ch >= 0xff61 && ch <= 0xff9f) \
329 /* Half-width katakana. */ \
330 if (__builtin_expect (outptr + 1 >= outend, 0)) \
332 result = __GCONV_FULL_OUTPUT; \
333 break; \
335 *outptr++ = 0x8e; \
336 *outptr++ = ch - 0xfec0; \
338 else \
340 uint32_t jch = ucs4_to_jisx0213 (ch); \
341 if (jch == 0) \
343 UNICODE_TAG_HANDLER (ch, 4); \
345 /* Illegal character. */ \
346 STANDARD_TO_LOOP_ERR_HANDLER (4); \
349 if (jch & 0x0080) \
351 /* A possible match in comp_table_data. We have to buffer it. */\
353 /* We know it's a JISX 0213 plane 1 character. */ \
354 assert ((jch & 0x8000) == 0); \
356 *statep = (jch | 0x8080) << 3; \
357 inptr += 4; \
358 continue; \
361 if (jch & 0x8000) \
363 /* JISX 0213 plane 2. */ \
364 if (__builtin_expect (outptr + 2 >= outend, 0)) \
366 result = __GCONV_FULL_OUTPUT; \
367 break; \
369 *outptr++ = 0x8f; \
371 else \
373 /* JISX 0213 plane 1. */ \
374 if (__builtin_expect (outptr + 1 >= outend, 0)) \
376 result = __GCONV_FULL_OUTPUT; \
377 break; \
380 *outptr++ = (jch >> 8) | 0x80; \
381 *outptr++ = (jch & 0xff) | 0x80; \
384 inptr += 4; \
386 #define LOOP_NEED_FLAGS
387 #define EXTRA_LOOP_DECLS , int *statep
388 #include <iconv/loop.c>
391 /* Now define the toplevel functions. */
392 #include <iconv/skeleton.c>