a0062c884785b52bd6382ea08922048ae4ac280f
[kopensolaris-gnu/glibc.git] / iconvdata / euc-jisx0213.c
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.
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, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <dlfcn.h>
22 #include <stdint.h>
23 #include <gconv.h>
24
25 /* The structure of EUC-JISX0213 is as follows:
26
27    0x00..0x7F: ASCII
28
29    0x8E{A1..FE}: JISX0201 Katakana, with prefix 0x8E, offset by +0x80.
30
31    0x8F{A1..FE}{A1..FE}: JISX0213 plane 2, with prefix 0x8F, offset by +0x8080.
32
33    0x{A1..FE}{A1..FE}: JISX0213 plane 1, offset by +0x8080.
34
35    Note that some JISX0213 characters are not contained in Unicode 3.2
36    and are therefore best represented as sequences of Unicode characters.
37 */
38
39 #include "jisx0213.h"
40
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 MIN_NEEDED_FROM         1
48 #define MAX_NEEDED_FROM         3
49 #define MIN_NEEDED_TO           4
50 #define PREPARE_LOOP \
51   int saved_state;                                                            \
52   int *statep = &data->__statep->__count;
53 #define EXTRA_LOOP_ARGS         , statep
54
55
56 /* Since we might have to reset input pointer we must be able to save
57    and restore the state.  */
58 #define SAVE_RESET_STATE(Save) \
59   if (Save)                                                                   \
60     saved_state = *statep;                                                    \
61   else                                                                        \
62     *statep = saved_state
63
64
65 /* During UCS-4 to EUC-JISX0213 conversion, the COUNT element of the state
66    contains the last two bytes to be output, shifted by 3 bits.  */
67
68 /* Since this is a stateful encoding we have to provide code which resets
69    the output state to the initial state.  This has to be done during the
70    flushing.  */
71 #define EMIT_SHIFT_TO_INIT \
72   if (data->__statep->__count != 0)                                           \
73     {                                                                         \
74       if (FROM_DIRECTION)                                                     \
75         /* We don't use shift states in the FROM_DIRECTION.  */               \
76         data->__statep->__count = 0;                                          \
77       else                                                                    \
78         {                                                                     \
79           if (__builtin_expect (outbuf + 2 <= outend, 1))                     \
80             {                                                                 \
81               /* Write out the last character.  */                            \
82               uint32_t lasttwo = data->__statep->__count >> 3;                \
83               *outbuf++ = (lasttwo >> 8) & 0xff;                              \
84               *outbuf++ = lasttwo & 0xff;                                     \
85               data->__statep->__count = 0;                                    \
86             }                                                                 \
87           else                                                                \
88             /* We don't have enough room in the output buffer.  */            \
89             status = __GCONV_FULL_OUTPUT;                                     \
90         }                                                                     \
91     }
92
93
94 /* First define the conversion function from EUC-JISX0213 to UCS-4.  */
95 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
96 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
97 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
98 #define LOOPFCT                 FROM_LOOP
99 #define BODY \
100   {                                                                           \
101     uint32_t ch = *inptr;                                                     \
102                                                                               \
103     if (ch < 0x80)                                                            \
104       /* Plain ASCII character.  */                                           \
105       ++inptr;                                                                \
106     else if ((ch >= 0xa1 && ch <= 0xfe) || ch == 0x8e || ch == 0x8f)          \
107       {                                                                       \
108         /* Two or three byte character.  */                                   \
109         uint32_t ch2;                                                         \
110                                                                               \
111         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
112           {                                                                   \
113             /* The second byte is not available.  */                          \
114             result = __GCONV_INCOMPLETE_INPUT;                                \
115             break;                                                            \
116           }                                                                   \
117                                                                               \
118         ch2 = inptr[1];                                                       \
119                                                                               \
120         /* The second byte must be >= 0xa1 and <= 0xfe.  */                   \
121         if (__builtin_expect (ch2 < 0xa1 || ch2 > 0xfe, 0))                   \
122           {                                                                   \
123             /* This is an illegal character.  */                              \
124             if (! ignore_errors_p ())                                         \
125               {                                                               \
126                 result = __GCONV_ILLEGAL_INPUT;                               \
127                 break;                                                        \
128               }                                                               \
129                                                                               \
130             ++inptr;                                                          \
131             ++*irreversible;                                                  \
132             break;                                                            \
133           }                                                                   \
134                                                                               \
135         if (ch == 0x8e)                                                       \
136           {                                                                   \
137             /* Half-width katakana.  */                                       \
138             if (__builtin_expect (ch2 > 0xdf, 0))                             \
139               {                                                               \
140                 /* This is an illegal character.  */                          \
141                 if (! ignore_errors_p ())                                     \
142                   {                                                           \
143                     result = __GCONV_ILLEGAL_INPUT;                           \
144                     break;                                                    \
145                   }                                                           \
146                                                                               \
147                 ++inptr;                                                      \
148                 ++*irreversible;                                              \
149                 break;                                                        \
150               }                                                               \
151                                                                               \
152             ch = ch2 + 0xfec0;                                                \
153             inptr += 2;                                                       \
154           }                                                                   \
155         else                                                                  \
156           {                                                                   \
157             const unsigned char *endp;                                        \
158                                                                               \
159             if (ch == 0x8f)                                                   \
160               {                                                               \
161                 /* JISX 0213 plane 2.  */                                     \
162                 uint32_t ch3;                                                 \
163                                                                               \
164                 if (__builtin_expect (inptr + 2 >= inend, 0))                 \
165                   {                                                           \
166                     /* The third byte is not available.  */                   \
167                     result = __GCONV_INCOMPLETE_INPUT;                        \
168                     break;                                                    \
169                   }                                                           \
170                                                                               \
171                 ch3 = inptr[2];                                               \
172                 endp = inptr + 3;                                             \
173                                                                               \
174                 ch = jisx0213_to_ucs4 (0x200 - 0x80 + ch2, ch3 ^ 0x80);       \
175               }                                                               \
176             else                                                              \
177               {                                                               \
178                 /* JISX 0213 plane 1.  */                                     \
179                 endp = inptr + 2;                                             \
180                                                                               \
181                 ch = jisx0213_to_ucs4 (0x100 - 0x80 + ch, ch2 ^ 0x80);        \
182               }                                                               \
183                                                                               \
184             if (ch == 0)                                                      \
185               {                                                               \
186                 /* This is an illegal character.  */                          \
187                 if (! ignore_errors_p ())                                     \
188                   {                                                           \
189                     result = __GCONV_ILLEGAL_INPUT;                           \
190                     break;                                                    \
191                   }                                                           \
192                                                                               \
193                 ++inptr;                                                      \
194                 ++*irreversible;                                              \
195                 break;                                                        \
196               }                                                               \
197                                                                               \
198             inptr = endp;                                                     \
199                                                                               \
200             if (ch < 0x80)                                                    \
201               {                                                               \
202                 /* It's a combining character.  */                            \
203                 uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0];         \
204                 uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1];         \
205                                                                               \
206                 /* See whether we have room for two characters.  */           \
207                 if (outptr + 8 <= outend)                                     \
208                   {                                                           \
209                     put32 (outptr, u1);                                       \
210                     outptr += 4;                                              \
211                     put32 (outptr, u2);                                       \
212                     outptr += 4;                                              \
213                     continue;                                                 \
214                   }                                                           \
215                 else                                                          \
216                   {                                                           \
217                     result = __GCONV_FULL_OUTPUT;                             \
218                     break;                                                    \
219                   }                                                           \
220               }                                                               \
221           }                                                                   \
222       }                                                                       \
223     else                                                                      \
224       {                                                                       \
225         /* This is illegal.  */                                               \
226         if (! ignore_errors_p ())                                             \
227           {                                                                   \
228             result = __GCONV_ILLEGAL_INPUT;                                   \
229             break;                                                            \
230           }                                                                   \
231                                                                               \
232         ++inptr;                                                              \
233         ++*irreversible;                                                      \
234         continue;                                                             \
235       }                                                                       \
236                                                                               \
237     put32 (outptr, ch);                                                       \
238     outptr += 4;                                                              \
239   }
240 #define LOOP_NEED_FLAGS
241 #define EXTRA_LOOP_DECLS        , int *statep
242 #include <iconv/loop.c>
243
244
245 /* Next, define the other direction, from UCS-4 to EUC-JISX0213.  */
246
247 /* Composition tables for each of the relevant combining characters.  */
248 static const struct
249 {
250   uint16_t base;
251   uint16_t composed;
252 } comp_table_data[] =
253 {
254 #define COMP_TABLE_IDX_02E5 0
255 #define COMP_TABLE_LEN_02E5 1
256   { 0xabe4, 0xabe5 }, /* 0x12B65 = 0x12B64 U+02E5 */
257 #define COMP_TABLE_IDX_02E9 (COMP_TABLE_IDX_02E5 + COMP_TABLE_LEN_02E5)
258 #define COMP_TABLE_LEN_02E9 1
259   { 0xabe0, 0xabe6 }, /* 0x12B66 = 0x12B60 U+02E9 */
260 #define COMP_TABLE_IDX_0300 (COMP_TABLE_IDX_02E9 + COMP_TABLE_LEN_02E9)
261 #define COMP_TABLE_LEN_0300 5
262   { 0xa9dc, 0xabc4 }, /* 0x12B44 = 0x1295C U+0300 */
263   { 0xabb8, 0xabc8 }, /* 0x12B48 = 0x12B38 U+0300 */
264   { 0xabb7, 0xabca }, /* 0x12B4A = 0x12B37 U+0300 */
265   { 0xabb0, 0xabcc }, /* 0x12B4C = 0x12B30 U+0300 */
266   { 0xabc3, 0xabce }, /* 0x12B4E = 0x12B43 U+0300 */
267 #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300)
268 #define COMP_TABLE_LEN_0301 4
269   { 0xabb8, 0xabc9 }, /* 0x12B49 = 0x12B38 U+0301 */
270   { 0xabb7, 0xabcb }, /* 0x12B4B = 0x12B37 U+0301 */
271   { 0xabb0, 0xabcd }, /* 0x12B4D = 0x12B30 U+0301 */
272   { 0xabc3, 0xabcf }, /* 0x12B4F = 0x12B43 U+0301 */
273 #define COMP_TABLE_IDX_309A (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301)
274 #define COMP_TABLE_LEN_309A 14
275   { 0xa4ab, 0xa4f7 }, /* 0x12477 = 0x1242B U+309A */
276   { 0xa4ad, 0xa4f8 }, /* 0x12478 = 0x1242D U+309A */
277   { 0xa4af, 0xa4f9 }, /* 0x12479 = 0x1242F U+309A */
278   { 0xa4b1, 0xa4fa }, /* 0x1247A = 0x12431 U+309A */
279   { 0xa4b3, 0xa4fb }, /* 0x1247B = 0x12433 U+309A */
280   { 0xa5ab, 0xa5f7 }, /* 0x12577 = 0x1252B U+309A */
281   { 0xa5ad, 0xa5f8 }, /* 0x12578 = 0x1252D U+309A */
282   { 0xa5af, 0xa5f9 }, /* 0x12579 = 0x1252F U+309A */
283   { 0xa5b1, 0xa5fa }, /* 0x1257A = 0x12531 U+309A */
284   { 0xa5b3, 0xa5fb }, /* 0x1257B = 0x12533 U+309A */
285   { 0xa5bb, 0xa5fc }, /* 0x1257C = 0x1253B U+309A */
286   { 0xa5c4, 0xa5fd }, /* 0x1257D = 0x12544 U+309A */
287   { 0xa5c8, 0xa5fe }, /* 0x1257E = 0x12548 U+309A */
288   { 0xa6f5, 0xa6f8 }, /* 0x12678 = 0x12675 U+309A */
289 };
290
291 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
292 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
293 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
294 #define LOOPFCT                 TO_LOOP
295 #define BODY \
296   {                                                                           \
297     uint32_t ch = get32 (inptr);                                              \
298                                                                               \
299     if ((*statep >> 3) != 0)                                                  \
300       {                                                                       \
301         /* Attempt to combine the last character with this one.  */           \
302         uint16_t lasttwo = *statep >> 3;                                      \
303         unsigned int idx;                                                     \
304         unsigned int len;                                                     \
305                                                                               \
306         if (ch == 0x02e5)                                                     \
307           idx = COMP_TABLE_IDX_02E5, len = COMP_TABLE_LEN_02E5;               \
308         else if (ch == 0x02e9)                                                \
309           idx = COMP_TABLE_IDX_02E9, len = COMP_TABLE_LEN_02E9;               \
310         else if (ch == 0x0300)                                                \
311           idx = COMP_TABLE_IDX_0300, len = COMP_TABLE_LEN_0300;               \
312         else if (ch == 0x0301)                                                \
313           idx = COMP_TABLE_IDX_0301, len = COMP_TABLE_LEN_0301;               \
314         else if (ch == 0x309a)                                                \
315           idx = COMP_TABLE_IDX_309A, len = COMP_TABLE_LEN_309A;               \
316         else                                                                  \
317           goto not_combining;                                                 \
318                                                                               \
319         do                                                                    \
320           if (comp_table_data[idx].base == lasttwo)                           \
321             break;                                                            \
322         while (++idx, --len > 0);                                             \
323                                                                               \
324         if (len > 0)                                                          \
325           {                                                                   \
326             /* Output the combined character.  */                             \
327             if (__builtin_expect (outptr + 1 >= outend, 0))                   \
328               {                                                               \
329                 result = __GCONV_FULL_OUTPUT;                                 \
330                 break;                                                        \
331               }                                                               \
332             lasttwo = comp_table_data[idx].composed;                          \
333             *outptr++ = (lasttwo >> 8) & 0xff;                                \
334             *outptr++ = lasttwo & 0xff;                                       \
335             *statep = 0;                                                      \
336             inptr += 4;                                                       \
337             continue;                                                         \
338           }                                                                   \
339                                                                               \
340       not_combining:                                                          \
341         /* Output the buffered character.  */                                 \
342         if (__builtin_expect (outptr + 1 >= outend, 0))                       \
343           {                                                                   \
344             result = __GCONV_FULL_OUTPUT;                                     \
345             break;                                                            \
346           }                                                                   \
347         *outptr++ = (lasttwo >> 8) & 0xff;                                    \
348         *outptr++ = lasttwo & 0xff;                                           \
349         *statep = 0;                                                          \
350         continue;                                                             \
351       }                                                                       \
352                                                                               \
353     if (ch < 0x80)                                                            \
354       /* Plain ASCII character.  */                                           \
355       *outptr++ = ch;                                                         \
356     else if (ch >= 0xff61 && ch <= 0xff9f)                                    \
357       {                                                                       \
358         /* Half-width katakana.  */                                           \
359         if (__builtin_expect (outptr + 1 >= outend, 0))                       \
360           {                                                                   \
361             result = __GCONV_FULL_OUTPUT;                                     \
362             break;                                                            \
363           }                                                                   \
364         *outptr++ = 0x8e;                                                     \
365         *outptr++ = ch - 0xfec0;                                              \
366       }                                                                       \
367     else                                                                      \
368       {                                                                       \
369         uint32_t jch = ucs4_to_jisx0213 (ch);                                 \
370         if (jch == 0)                                                         \
371           {                                                                   \
372             UNICODE_TAG_HANDLER (ch, 4);                                      \
373                                                                               \
374             /* Illegal character.  */                                         \
375             STANDARD_ERR_HANDLER (4);                                         \
376           }                                                                   \
377                                                                               \
378         if (jch & 0x0080)                                                     \
379           {                                                                   \
380             /* A possible match in comp_table_data.  We have to buffer it.  */\
381                                                                               \
382             /* We know it's a JISX 0213 plane 1 character.  */                \
383             assert ((jch & 0x8000) == 0);                                     \
384                                                                               \
385             *statep = (jch | 0x8080) << 3;                                    \
386             inptr += 4;                                                       \
387             continue;                                                         \
388           }                                                                   \
389                                                                               \
390         if (jch & 0x8000)                                                     \
391           {                                                                   \
392             /* JISX 0213 plane 2.  */                                         \
393             if (__builtin_expect (outptr + 2 >= outend, 0))                   \
394               {                                                               \
395                 result = __GCONV_FULL_OUTPUT;                                 \
396                 break;                                                        \
397               }                                                               \
398             *outptr++ = 0x8f;                                                 \
399           }                                                                   \
400         else                                                                  \
401           {                                                                   \
402             /* JISX 0213 plane 1.  */                                         \
403             if (__builtin_expect (outptr + 1 >= outend, 0))                   \
404               {                                                               \
405                 result = __GCONV_FULL_OUTPUT;                                 \
406                 break;                                                        \
407               }                                                               \
408           }                                                                   \
409         *outptr++ = (jch >> 8) | 0x80;                                        \
410         *outptr++ = (jch & 0xff) | 0x80;                                      \
411       }                                                                       \
412                                                                               \
413     inptr += 4;                                                               \
414   }
415 #define LOOP_NEED_FLAGS
416 #define EXTRA_LOOP_DECLS        , int *statep
417 #include <iconv/loop.c>
418
419
420 /* Now define the toplevel functions.  */
421 #include <iconv/skeleton.c>