Include <string.h>.
[glibc.git] / iconvdata / iso-2022-kr.c
blob9846d920d90499bd054e63d4cab77fb94c6e7b25
1 /* Conversion module for ISO-2022-KR.
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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 <gconv.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "ksc5601.h"
27 #include <assert.h>
29 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
30 #define ESC 0x1b
32 /* The shift sequences for this charset (it does not use ESC). */
33 #define SI 0x0f
34 #define SO 0x0e
36 /* Definitions used in the body of the `gconv' function. */
37 #define CHARSET_NAME "ISO-2022-KR//"
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
43 #define MAX_NEEDED_FROM 3
44 #define MIN_NEEDED_TO 4
45 #define MAX_NEEDED_TO 4
46 #define PREPARE_LOOP \
47 int save_set; \
48 int *setp = &data->__statep->__count; \
49 if (!FROM_DIRECTION && !data->__internal_use \
50 && data->__invocation_counter == 0) \
51 { \
52 /* Emit the designator sequence. */ \
53 if (outbuf + 4 > outend) \
54 return __GCONV_FULL_OUTPUT; \
56 *outbuf++ = ESC; \
57 *outbuf++ = '$'; \
58 *outbuf++ = ')'; \
59 *outbuf++ = 'C'; \
61 #define EXTRA_LOOP_ARGS , setp
64 /* The COUNT element of the state keeps track of the currently selected
65 character set. The possible values are: */
66 enum
68 ASCII_set = 0,
69 KSC5601_set = 8
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 != ASCII_set) \
78 { \
79 if (FROM_DIRECTION) \
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 } \
86 else \
87 { \
88 /* We are not in the initial state. To switch back we have \
89 to emit `SI'. */ \
90 if (__builtin_expect (outbuf == outend, 0)) \
91 /* We don't have enough room in the output buffer. */ \
92 status = __GCONV_FULL_OUTPUT; \
93 else \
94 { \
95 /* Write out the shift sequence. */ \
96 *outbuf++ = SI; \
97 data->__statep->__count = ASCII_set; \
98 } \
99 } \
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) \
107 save_set = *setp; \
108 else \
109 *setp = save_set
112 /* First define the conversion function from ISO-2022-KR to UCS4. */
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 \
119 uint32_t ch = *inptr; \
121 /* This is a 7bit character set, disallow all 8bit characters. */ \
122 if (__builtin_expect (ch > 0x7f, 0)) \
124 if (! ignore_errors_p ()) \
126 result = __GCONV_ILLEGAL_INPUT; \
127 break; \
130 ++inptr; \
131 ++*irreversible; \
132 continue; \
135 /* Recognize escape sequences. */ \
136 if (__builtin_expect (ch, 0) == ESC) \
138 /* We don't really have to handle escape sequences since all the \
139 switching is done using the SI and SO bytes. But we have to \
140 recognize `Esc $ ) C' since this is a kind of flag for this \
141 encoding. We simply ignore it. */ \
142 if (__builtin_expect (inptr + 1 > inend, 0) \
143 || (inptr[1] == '$' \
144 && (__builtin_expect (inptr + 2 > inend, 0) \
145 || (inptr[2] == ')' \
146 && __builtin_expect (inptr + 3 > inend, 0))))) \
149 result = __GCONV_INCOMPLETE_INPUT; \
150 break; \
152 if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \
154 /* Yeah, yeah, we know this is ISO 2022-KR. */ \
155 inptr += 4; \
156 continue; \
159 else if (__builtin_expect (ch, 0) == SO) \
161 /* Switch to use KSC. */ \
162 ++inptr; \
163 set = KSC5601_set; \
164 continue; \
166 else if (__builtin_expect (ch, 0) == SI) \
168 /* Switch to use ASCII. */ \
169 ++inptr; \
170 set = ASCII_set; \
171 continue; \
174 if (set == ASCII_set) \
176 /* Almost done, just advance the input pointer. */ \
177 ++inptr; \
179 else \
181 assert (set == KSC5601_set); \
183 /* Use the KSC 5601 table. */ \
184 ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0); \
186 if (__builtin_expect (ch, 1) == 0) \
188 result = __GCONV_INCOMPLETE_INPUT; \
189 break; \
191 else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
193 if (! ignore_errors_p ()) \
195 /* This is an illegal character. */ \
196 result = __GCONV_ILLEGAL_INPUT; \
197 break; \
200 ++*irreversible; \
201 ++inptr; \
202 continue; \
206 put32 (outptr, ch); \
207 outptr += 4; \
209 #define LOOP_NEED_FLAGS
210 #define EXTRA_LOOP_DECLS , int *setp
211 #define INIT_PARAMS int set = *setp
212 #define UPDATE_PARAMS *setp = set
213 #include <iconv/loop.c>
216 /* Next, define the other direction. */
217 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
218 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
219 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
220 #define LOOPFCT TO_LOOP
221 #define BODY \
223 uint32_t ch; \
225 ch = get32 (inptr); \
227 /* First see whether we can write the character using the currently \
228 selected character set. */ \
229 if (ch < 0x80) \
231 if (set != ASCII_set) \
233 *outptr++ = SI; \
234 set = ASCII_set; \
235 if (__builtin_expect (outptr == outend, 0)) \
237 result = __GCONV_FULL_OUTPUT; \
238 break; \
242 *outptr++ = ch; \
244 else \
246 char buf[2]; \
247 size_t written; \
249 written = ucs4_to_ksc5601 (ch, buf, 2); \
251 if (__builtin_expect (written, 0) == __UNKNOWN_10646_CHAR) \
253 UNICODE_TAG_HANDLER (ch, 4); \
255 /* Illegal character. */ \
256 STANDARD_ERR_HANDLER (4); \
258 else \
260 assert (written == 2); \
262 /* We use KSC 5601. */ \
263 if (set != KSC5601_set) \
265 *outptr++ = SO; \
266 set = KSC5601_set; \
269 if (__builtin_expect (outptr + 2 > outend, 0)) \
271 result = __GCONV_FULL_OUTPUT; \
272 break; \
275 *outptr++ = buf[0]; \
276 *outptr++ = buf[1]; \
280 /* Now that we wrote the output increment the input pointer. */ \
281 inptr += 4; \
283 #define LOOP_NEED_FLAGS
284 #define EXTRA_LOOP_DECLS , int *setp
285 #define INIT_PARAMS int set = *setp
286 #define UPDATE_PARAMS *setp = set
287 #include <iconv/loop.c>
290 /* Now define the toplevel functions. */
291 #include <iconv/skeleton.c>