1 /* Conversion module for ISO-2022-KR.
2 Copyright (C) 1998, 1999, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
31 /* The shift sequences for this charset (it does not use ESC). */
35 /* Definitions used in the body of the `gconv' function. */
36 #define CHARSET_NAME "ISO-2022-KR//"
39 #define FROM_LOOP from_iso2022kr_loop
40 #define TO_LOOP to_iso2022kr_loop
41 #define MIN_NEEDED_FROM 1
42 #define MAX_NEEDED_FROM 3
43 #define MIN_NEEDED_TO 4
44 #define MAX_NEEDED_TO 4
45 #define PREPARE_LOOP \
47 int *setp = &data->__statep->__count; \
48 if (!FROM_DIRECTION && !data->__internal_use \
49 && data->__invocation_counter == 0) \
51 /* Emit the designator sequence. */ \
52 if (outbuf + 4 > outend) \
53 return __GCONV_FULL_OUTPUT; \
60 #define EXTRA_LOOP_ARGS , setp
63 /* The COUNT element of the state keeps track of the currently selected
64 character set. The possible values are: */
72 /* Since this is a stateful encoding we have to provide code which resets
73 the output state to the initial state. This has to be done during the
75 #define EMIT_SHIFT_TO_INIT \
76 if (data->__statep->__count != ASCII_set) \
80 /* It's easy, we don't have to emit anything, we just reset the \
81 state for the input. */ \
82 data->__statep->__count &= 7; \
83 data->__statep->__count |= ASCII_set; \
87 unsigned char *outbuf = data->__outbuf; \
89 /* We are not in the initial state. To switch back we have \
91 if (__builtin_expect (outbuf == data->__outbufend, 0)) \
92 /* We don't have enough room in the output buffer. */ \
93 status = __GCONV_FULL_OUTPUT; \
96 /* Write out the shift sequence. */ \
98 data->__outbuf = outbuf; \
99 data->__statep->__count = ASCII_set; \
105 /* Since we might have to reset input pointer we must be able to save
106 and retore the state. */
107 #define SAVE_RESET_STATE(Save) \
114 /* First define the conversion function from ISO-2022-KR to UCS4. */
115 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
116 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
117 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
118 #define LOOPFCT FROM_LOOP
121 uint32_t ch = *inptr; \
123 /* This is a 7bit character set, disallow all 8bit characters. */ \
124 if (__builtin_expect (ch, 0) > 0x7f) \
126 if (! ignore_errors_p ()) \
128 result = __GCONV_ILLEGAL_INPUT; \
137 /* Recognize escape sequences. */ \
138 if (__builtin_expect (ch, 0) == ESC) \
140 /* We don't really have to handle escape sequences since all the \
141 switching is done using the SI and SO bytes. But we have to \
142 recognize `Esc $ ) C' since this is a kind of flag for this \
143 encoding. We simply ignore it. */ \
144 if ((NEED_LENGTH_TEST && __builtin_expect (inptr + 1 > inend, 0)) \
145 || (inptr[1] == '$' \
146 && ((NEED_LENGTH_TEST \
147 && __builtin_expect (inptr + 2 > inend, 0)) \
148 || (inptr[2] == ')' \
149 && __builtin_expect (inptr + 3 > inend, 0))))) \
152 result = __GCONV_EMPTY_INPUT; \
155 if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \
157 /* Yeah, yeah, we know this is ISO 2022-KR. */ \
162 else if (__builtin_expect (ch, 0) == SO) \
164 /* Switch to use KSC. */ \
169 else if (__builtin_expect (ch, 0) == SI) \
171 /* Switch to use ASCII. */ \
177 if (set == ASCII_set) \
179 /* Almost done, just advance the input pointer. */ \
184 assert (set == KSC5601_set); \
186 /* Use the KSC 5601 table. */ \
187 ch = ksc5601_to_ucs4 (&inptr, \
188 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
190 if (NEED_LENGTH_TEST && __builtin_expect (ch, 1) == 0) \
192 result = __GCONV_EMPTY_INPUT; \
195 else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR) \
197 if (! ignore_errors_p ()) \
199 /* This is an illegal character. */ \
200 result = __GCONV_ILLEGAL_INPUT; \
210 put32 (outptr, ch); \
213 #define EXTRA_LOOP_DECLS , int *setp
214 #define INIT_PARAMS int set = *setp
215 #define UPDATE_PARAMS *setp = set
216 #include <iconv/loop.c>
219 /* Next, define the other direction. */
220 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
221 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
222 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
223 #define LOOPFCT TO_LOOP
227 size_t written = 0; \
229 ch = get32 (inptr); \
231 /* First see whether we can write the character using the currently \
232 selected character set. */ \
235 if (set != ASCII_set) \
239 if (NEED_LENGTH_TEST && outptr == outend) \
241 result = __GCONV_FULL_OUTPUT; \
253 written = ucs4_to_ksc5601 (ch, buf, 2); \
255 if (__builtin_expect (written, 0) == __UNKNOWN_10646_CHAR) \
257 /* Illegal character. */ \
258 if (! ignore_errors_p ()) \
260 result = __GCONV_ILLEGAL_INPUT; \
268 assert (written == 2); \
270 /* We use KSC 5601. */ \
271 if (set != KSC5601_set) \
277 if (NEED_LENGTH_TEST && __builtin_expect (outptr + 2 > outend, 0))\
279 result = __GCONV_FULL_OUTPUT; \
283 *outptr++ = buf[0]; \
284 *outptr++ = buf[1]; \
288 /* Now that we wrote the output increment the input pointer. */ \
291 #define EXTRA_LOOP_DECLS , int *setp
292 #define INIT_PARAMS int set = *setp
293 #define UPDATE_PARAMS *setp = set
294 #include <iconv/loop.c>
297 /* Now define the toplevel functions. */
298 #include <iconv/skeleton.c>