48fdee3cdcc4b116948a6fe3867aab3726b25433
[kopensolaris-gnu/glibc.git] / iconvdata / euc-kr.c
1 /* Mapping tables for EUC-KR handling.
2    Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jungshik Shin <jshin@pantheon.yale.edu>
5    and Ulrich Drepper <drepper@cygnus.com>, 1998.
6
7    The GNU C Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The GNU C Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the GNU C Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <stdint.h>
23 #include <ksc5601.h>
24
25
26 static inline void
27 euckr_from_ucs4 (uint32_t ch, unsigned char *cp)
28 {
29   if (ch > 0x7f)
30     {
31       if (__builtin_expect (ucs4_to_ksc5601 (ch, cp, 2), 0)
32           != __UNKNOWN_10646_CHAR)
33         {
34           cp[0] |= 0x80;
35           cp[1] |= 0x80;
36         }
37       else
38         cp[0] = '\0';
39     }
40   /* XXX Think about 0x5c ; '\'.  */
41   else
42     {
43       cp[0] = (unsigned char) ch;
44       cp[1] = '\0';
45     }
46 }
47
48
49 /* Definitions used in the body of the `gconv' function.  */
50 #define CHARSET_NAME            "EUC-KR//"
51 #define FROM_LOOP               from_euc_kr
52 #define TO_LOOP                 to_euc_kr
53 #define DEFINE_INIT             1
54 #define DEFINE_FINI             1
55 #define MIN_NEEDED_FROM         1
56 #define MAX_NEEDED_FROM         2
57 #define MIN_NEEDED_TO           4
58
59
60 /* First define the conversion function from EUC-KR to UCS4.  */
61 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
62 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
63 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
64 #define LOOPFCT                 FROM_LOOP
65 #define BODY \
66   {                                                                           \
67     uint32_t ch = *inptr;                                                     \
68                                                                               \
69     /* Half-width Korean Currency WON sign                                    \
70                                                                               \
71        if (inchar == 0x5c)                                                    \
72          ch =  0x20a9;                                                        \
73        else if (inchar <= 0x7f)                                               \
74          ch = (uint32_t) inchar;                                              \
75     */                                                                        \
76                                                                               \
77     if (ch <= 0x7f)                                                           \
78       /* Plain ASCII.  */                                                     \
79       ++inptr;                                                                \
80     /* 0xfe(->0x7e : row 94) and 0xc9(->0x59 : row 41) are                    \
81        user-defined areas.  */                                                \
82     else if (__builtin_expect (ch, 0xa1) <= 0xa0                              \
83              || __builtin_expect (ch, 0xa1) > 0xfe                            \
84              || __builtin_expect (ch, 0xa1) == 0xc9)                          \
85       {                                                                       \
86         /* This is illegal.  */                                               \
87         if (! ignore_errors_p ())                                             \
88           {                                                                   \
89             result = __GCONV_ILLEGAL_INPUT;                                   \
90             break;                                                            \
91           }                                                                   \
92                                                                               \
93         ++inptr;                                                              \
94         ++*converted;                                                         \
95         continue;                                                             \
96       }                                                                       \
97     else                                                                      \
98       {                                                                       \
99         /* Two-byte character.  First test whether the next character         \
100            is also available.  */                                             \
101         ch = ksc5601_to_ucs4 (&inptr,                                         \
102                               NEED_LENGTH_TEST ? inptr - inend : 2, 0x80);    \
103         if (NEED_LENGTH_TEST && __builtin_expect (ch, 1) == 0)                \
104           {                                                                   \
105             /* The second character is not available.  */                     \
106             result = __GCONV_INCOMPLETE_INPUT;                                \
107             break;                                                            \
108           }                                                                   \
109         if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR)                 \
110           {                                                                   \
111             /* This is an illegal character.  */                              \
112             if (! ignore_errors_p ())                                         \
113               {                                                               \
114                 /* This is an illegal character.  */                          \
115                 result = __GCONV_ILLEGAL_INPUT;                               \
116                 break;                                                        \
117               }                                                               \
118                                                                               \
119             inptr += 2;                                                       \
120             ++*converted;                                                     \
121             continue;                                                         \
122           }                                                                   \
123       }                                                                       \
124                                                                               \
125     put32 (outptr, ch);                                                       \
126     outptr += 4;                                                              \
127   }
128 #include <iconv/loop.c>
129
130
131 /* Next, define the other direction.  */
132 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
133 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
134 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
135 #define LOOPFCT                 TO_LOOP
136 #define BODY \
137   {                                                                           \
138     uint32_t ch = get32 (inptr);                                              \
139     unsigned char cp[2];                                                      \
140                                                                               \
141     /* Decomposing Hangul syllables not available in KS C 5601 into           \
142        Jamos should be considered either here or in euckr_from_ucs4() */      \
143     euckr_from_ucs4 (ch, cp) ;                                                \
144                                                                               \
145     if (__builtin_expect (cp[0], '\1') == '\0' && ch != 0)                    \
146       {                                                                       \
147         /* Illegal character.  */                                             \
148         if (! ignore_errors_p ())                                             \
149           {                                                                   \
150             result = __GCONV_ILLEGAL_INPUT;                                   \
151             break;                                                            \
152           }                                                                   \
153                                                                               \
154         inptr += 4;                                                           \
155         ++*converted;                                                         \
156         continue;                                                             \
157       }                                                                       \
158                                                                               \
159     *outptr++ = cp[0];                                                        \
160     /* Now test for a possible second byte and write this if possible.  */    \
161     if (cp[1] != '\0')                                                        \
162       {                                                                       \
163         if (NEED_LENGTH_TEST && __builtin_expect (outptr >= outend, 0))       \
164           {                                                                   \
165             /* The result does not fit into the buffer.  */                   \
166             --outptr;                                                         \
167             result = __GCONV_FULL_OUTPUT;                                     \
168             break;                                                            \
169           }                                                                   \
170         *outptr++ = cp[1];                                                    \
171       }                                                                       \
172                                                                               \
173     inptr += 4;                                                               \
174   }
175 #include <iconv/loop.c>
176
177
178 /* Now define the toplevel functions.  */
179 #include <iconv/skeleton.c>