]>
Commit | Line | Data |
---|---|---|
8babd571 | 1 | /* Conversion module for ISO-2022-KR. |
4b1b449d UD |
2 | Copyright (C) 1998, 1999, 2000-2002, 2007, 2008 |
3 | Free Software Foundation, Inc. | |
8babd571 UD |
4 | This file is part of the GNU C Library. |
5 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. | |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
8babd571 UD |
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 | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
8babd571 | 16 | |
41bdb6e2 | 17 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
18 | License along with the GNU C Library; if not, see |
19 | <http://www.gnu.org/licenses/>. */ | |
8babd571 | 20 | |
55985355 | 21 | #include <dlfcn.h> |
8babd571 UD |
22 | #include <gconv.h> |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
25 | #include "ksc5601.h" | |
26 | ||
2fb2e75a UD |
27 | #include <assert.h> |
28 | ||
8babd571 | 29 | /* This makes obvious what everybody knows: 0x1b is the Esc character. */ |
2fb2e75a UD |
30 | #define ESC 0x1b |
31 | ||
c9fc0e22 | 32 | /* The shift sequences for this charset (it does not use ESC). */ |
8babd571 UD |
33 | #define SI 0x0f |
34 | #define SO 0x0e | |
35 | ||
36 | /* Definitions used in the body of the `gconv' function. */ | |
2fb2e75a | 37 | #define CHARSET_NAME "ISO-2022-KR//" |
8babd571 UD |
38 | #define DEFINE_INIT 1 |
39 | #define DEFINE_FINI 1 | |
40 | #define FROM_LOOP from_iso2022kr_loop | |
41 | #define TO_LOOP to_iso2022kr_loop | |
42 | #define MIN_NEEDED_FROM 1 | |
0e15c4b6 | 43 | #define MAX_NEEDED_FROM 4 |
8babd571 UD |
44 | #define MIN_NEEDED_TO 4 |
45 | #define MAX_NEEDED_TO 4 | |
46 | #define PREPARE_LOOP \ | |
2fb2e75a | 47 | int save_set; \ |
aa831d6d | 48 | int *setp = &data->__statep->__count; \ |
d64b6ad0 UD |
49 | if (!FROM_DIRECTION && !data->__internal_use \ |
50 | && data->__invocation_counter == 0) \ | |
e3e0a182 UD |
51 | { \ |
52 | /* Emit the designator sequence. */ \ | |
c9fc0e22 | 53 | if (outbuf + 4 > outend) \ |
d64b6ad0 | 54 | return __GCONV_FULL_OUTPUT; \ |
e3e0a182 | 55 | \ |
c9fc0e22 UD |
56 | *outbuf++ = ESC; \ |
57 | *outbuf++ = '$'; \ | |
58 | *outbuf++ = ')'; \ | |
59 | *outbuf++ = 'C'; \ | |
e3e0a182 | 60 | } |
66175fa8 | 61 | #define EXTRA_LOOP_ARGS , setp |
2fb2e75a | 62 | |
8babd571 UD |
63 | |
64 | /* The COUNT element of the state keeps track of the currently selected | |
65 | character set. The possible values are: */ | |
66 | enum | |
67 | { | |
68 | ASCII_set = 0, | |
fd1b5c0f | 69 | KSC5601_set = 8 |
8babd571 UD |
70 | }; |
71 | ||
72 | ||
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 \ | |
aa831d6d | 77 | if (data->__statep->__count != ASCII_set) \ |
8babd571 | 78 | { \ |
66175fa8 | 79 | if (FROM_DIRECTION) \ |
fd1b5c0f UD |
80 | { \ |
81 | /* It's easy, we don't have to emit anything, we just reset the \ | |
82 | state for the input. */ \ | |
83 | data->__statep->__count &= 7; \ | |
84 | data->__statep->__count |= ASCII_set; \ | |
85 | } \ | |
8babd571 UD |
86 | else \ |
87 | { \ | |
8babd571 | 88 | /* We are not in the initial state. To switch back we have \ |
66175fa8 | 89 | to emit `SI'. */ \ |
2373b30e | 90 | if (__builtin_expect (outbuf == outend, 0)) \ |
8babd571 | 91 | /* We don't have enough room in the output buffer. */ \ |
d64b6ad0 | 92 | status = __GCONV_FULL_OUTPUT; \ |
8babd571 UD |
93 | else \ |
94 | { \ | |
95 | /* Write out the shift sequence. */ \ | |
66175fa8 | 96 | *outbuf++ = SI; \ |
aa831d6d | 97 | data->__statep->__count = ASCII_set; \ |
8babd571 UD |
98 | } \ |
99 | } \ | |
100 | } | |
101 | ||
102 | ||
103 | /* Since we might have to reset input pointer we must be able to save | |
104 | and retore the state. */ | |
105 | #define SAVE_RESET_STATE(Save) \ | |
106 | if (Save) \ | |
66175fa8 | 107 | save_set = *setp; \ |
8babd571 | 108 | else \ |
66175fa8 | 109 | *setp = save_set |
8babd571 UD |
110 | |
111 | ||
922903d2 | 112 | /* First define the conversion function from ISO-2022-KR to UCS4. */ |
8babd571 UD |
113 | #define MIN_NEEDED_INPUT MIN_NEEDED_FROM |
114 | #define MAX_NEEDED_INPUT MAX_NEEDED_FROM | |
115 | #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO | |
116 | #define LOOPFCT FROM_LOOP | |
117 | #define BODY \ | |
118 | { \ | |
119 | uint32_t ch = *inptr; \ | |
120 | \ | |
121 | /* This is a 7bit character set, disallow all 8bit characters. */ \ | |
db2d05f9 | 122 | if (__builtin_expect (ch > 0x7f, 0)) \ |
e438a468 | 123 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
8babd571 UD |
124 | \ |
125 | /* Recognize escape sequences. */ \ | |
89301d68 | 126 | if (__builtin_expect (ch, 0) == ESC) \ |
8babd571 UD |
127 | { \ |
128 | /* We don't really have to handle escape sequences since all the \ | |
66175fa8 | 129 | switching is done using the SI and SO bytes. But we have to \ |
8babd571 UD |
130 | recognize `Esc $ ) C' since this is a kind of flag for this \ |
131 | encoding. We simply ignore it. */ \ | |
0e15c4b6 | 132 | if (__builtin_expect (inptr + 2 > inend, 0) \ |
8babd571 | 133 | || (inptr[1] == '$' \ |
0e15c4b6 | 134 | && (__builtin_expect (inptr + 3 > inend, 0) \ |
89301d68 | 135 | || (inptr[2] == ')' \ |
0e15c4b6 | 136 | && __builtin_expect (inptr + 4 > inend, 0))))) \ |
8babd571 | 137 | { \ |
c7c3b0e9 | 138 | result = __GCONV_INCOMPLETE_INPUT; \ |
8babd571 UD |
139 | break; \ |
140 | } \ | |
141 | if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \ | |
142 | { \ | |
143 | /* Yeah, yeah, we know this is ISO 2022-KR. */ \ | |
144 | inptr += 4; \ | |
145 | continue; \ | |
146 | } \ | |
147 | } \ | |
89301d68 | 148 | else if (__builtin_expect (ch, 0) == SO) \ |
8babd571 UD |
149 | { \ |
150 | /* Switch to use KSC. */ \ | |
151 | ++inptr; \ | |
152 | set = KSC5601_set; \ | |
153 | continue; \ | |
154 | } \ | |
89301d68 | 155 | else if (__builtin_expect (ch, 0) == SI) \ |
8babd571 UD |
156 | { \ |
157 | /* Switch to use ASCII. */ \ | |
158 | ++inptr; \ | |
159 | set = ASCII_set; \ | |
160 | continue; \ | |
161 | } \ | |
162 | \ | |
66175fa8 UD |
163 | if (set == ASCII_set) \ |
164 | { \ | |
66175fa8 UD |
165 | /* Almost done, just advance the input pointer. */ \ |
166 | ++inptr; \ | |
167 | } \ | |
8babd571 UD |
168 | else \ |
169 | { \ | |
170 | assert (set == KSC5601_set); \ | |
171 | \ | |
172 | /* Use the KSC 5601 table. */ \ | |
55985355 | 173 | ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0); \ |
8babd571 | 174 | \ |
e438a468 | 175 | if (__builtin_expect (ch == 0, 0)) \ |
8babd571 | 176 | { \ |
c7c3b0e9 | 177 | result = __GCONV_INCOMPLETE_INPUT; \ |
8babd571 UD |
178 | break; \ |
179 | } \ | |
e438a468 | 180 | else if (__builtin_expect (ch == __UNKNOWN_10646_CHAR, 0)) \ |
8babd571 | 181 | { \ |
e438a468 | 182 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
8babd571 UD |
183 | } \ |
184 | } \ | |
185 | \ | |
77e1d15a UD |
186 | put32 (outptr, ch); \ |
187 | outptr += 4; \ | |
8babd571 | 188 | } |
55985355 | 189 | #define LOOP_NEED_FLAGS |
66175fa8 UD |
190 | #define EXTRA_LOOP_DECLS , int *setp |
191 | #define INIT_PARAMS int set = *setp | |
192 | #define UPDATE_PARAMS *setp = set | |
8babd571 UD |
193 | #include <iconv/loop.c> |
194 | ||
195 | ||
196 | /* Next, define the other direction. */ | |
197 | #define MIN_NEEDED_INPUT MIN_NEEDED_TO | |
198 | #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM | |
199 | #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM | |
200 | #define LOOPFCT TO_LOOP | |
201 | #define BODY \ | |
202 | { \ | |
278bfa00 | 203 | uint32_t ch = get32 (inptr); \ |
8babd571 UD |
204 | \ |
205 | /* First see whether we can write the character using the currently \ | |
206 | selected character set. */ \ | |
66175fa8 | 207 | if (ch < 0x80) \ |
8babd571 | 208 | { \ |
66175fa8 | 209 | if (set != ASCII_set) \ |
8babd571 | 210 | { \ |
66175fa8 UD |
211 | *outptr++ = SI; \ |
212 | set = ASCII_set; \ | |
55985355 | 213 | if (__builtin_expect (outptr == outend, 0)) \ |
66175fa8 | 214 | { \ |
d64b6ad0 | 215 | result = __GCONV_FULL_OUTPUT; \ |
66175fa8 UD |
216 | break; \ |
217 | } \ | |
8babd571 | 218 | } \ |
66175fa8 UD |
219 | \ |
220 | *outptr++ = ch; \ | |
8babd571 UD |
221 | } \ |
222 | else \ | |
223 | { \ | |
085a4412 | 224 | unsigned char buf[2]; \ |
278bfa00 UD |
225 | /* Fake initialization to keep gcc quiet. */ \ |
226 | asm ("" : "=m" (buf)); \ | |
8babd571 | 227 | \ |
278bfa00 | 228 | size_t written = ucs4_to_ksc5601 (ch, buf, 2); \ |
89301d68 | 229 | if (__builtin_expect (written, 0) == __UNKNOWN_10646_CHAR) \ |
8babd571 | 230 | { \ |
601d2942 UD |
231 | UNICODE_TAG_HANDLER (ch, 4); \ |
232 | \ | |
66175fa8 | 233 | /* Illegal character. */ \ |
e438a468 | 234 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
66175fa8 | 235 | } \ |
85830c4c | 236 | else \ |
66175fa8 | 237 | { \ |
85830c4c UD |
238 | assert (written == 2); \ |
239 | \ | |
240 | /* We use KSC 5601. */ \ | |
241 | if (set != KSC5601_set) \ | |
242 | { \ | |
243 | *outptr++ = SO; \ | |
244 | set = KSC5601_set; \ | |
245 | } \ | |
246 | \ | |
55985355 | 247 | if (__builtin_expect (outptr + 2 > outend, 0)) \ |
85830c4c UD |
248 | { \ |
249 | result = __GCONV_FULL_OUTPUT; \ | |
250 | break; \ | |
251 | } \ | |
66175fa8 | 252 | \ |
85830c4c UD |
253 | *outptr++ = buf[0]; \ |
254 | *outptr++ = buf[1]; \ | |
255 | } \ | |
8babd571 UD |
256 | } \ |
257 | \ | |
258 | /* Now that we wrote the output increment the input pointer. */ \ | |
259 | inptr += 4; \ | |
260 | } | |
55985355 | 261 | #define LOOP_NEED_FLAGS |
66175fa8 UD |
262 | #define EXTRA_LOOP_DECLS , int *setp |
263 | #define INIT_PARAMS int set = *setp | |
4b1b449d | 264 | #define REINIT_PARAMS set = *setp |
66175fa8 | 265 | #define UPDATE_PARAMS *setp = set |
8babd571 UD |
266 | #include <iconv/loop.c> |
267 | ||
268 | ||
269 | /* Now define the toplevel functions. */ | |
270 | #include <iconv/skeleton.c> |