1 /* Conversion module for ISO-2022-KR.
2 Copyright (C) 1998-2013 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, see
18 <http://www.gnu.org/licenses/>. */
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 4
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 /* We are not in the initial state. To switch back we have \
89 if (__builtin_expect (outbuf == outend, 0)) \
90 /* We don't have enough room in the output buffer. */ \
91 status = __GCONV_FULL_OUTPUT; \
94 /* Write out the shift sequence. */ \
96 data->__statep->__count = ASCII_set; \
102 /* Since we might have to reset input pointer we must be able to save
103 and retore the state. */
104 #define SAVE_RESET_STATE(Save) \
111 /* First define the conversion function from ISO-2022-KR to UCS4. */
112 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
113 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
114 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
115 #define LOOPFCT FROM_LOOP
118 uint32_t ch = *inptr; \
120 /* This is a 7bit character set, disallow all 8bit characters. */ \
121 if (__builtin_expect (ch > 0x7f, 0)) \
122 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
124 /* Recognize escape sequences. */ \
125 if (__builtin_expect (ch, 0) == ESC) \
127 /* We don't really have to handle escape sequences since all the \
128 switching is done using the SI and SO bytes. But we have to \
129 recognize `Esc $ ) C' since this is a kind of flag for this \
130 encoding. We simply ignore it. */ \
131 if (__builtin_expect (inptr + 2 > inend, 0) \
132 || (inptr[1] == '$' \
133 && (__builtin_expect (inptr + 3 > inend, 0) \
134 || (inptr[2] == ')' \
135 && __builtin_expect (inptr + 4 > inend, 0))))) \
137 result = __GCONV_INCOMPLETE_INPUT; \
140 if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \
142 /* Yeah, yeah, we know this is ISO 2022-KR. */ \
147 else if (__builtin_expect (ch, 0) == SO) \
149 /* Switch to use KSC. */ \
154 else if (__builtin_expect (ch, 0) == SI) \
156 /* Switch to use ASCII. */ \
162 if (set == ASCII_set) \
164 /* Almost done, just advance the input pointer. */ \
169 assert (set == KSC5601_set); \
171 /* Use the KSC 5601 table. */ \
172 ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0); \
174 if (__builtin_expect (ch == 0, 0)) \
176 result = __GCONV_INCOMPLETE_INPUT; \
179 else if (__builtin_expect (ch == __UNKNOWN_10646_CHAR, 0)) \
181 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
185 put32 (outptr, ch); \
188 #define LOOP_NEED_FLAGS
189 #define EXTRA_LOOP_DECLS , int *setp
190 #define INIT_PARAMS int set = *setp
191 #define UPDATE_PARAMS *setp = set
192 #include <iconv/loop.c>
195 /* Next, define the other direction. */
196 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
197 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
198 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
199 #define LOOPFCT TO_LOOP
202 uint32_t ch = get32 (inptr); \
204 /* First see whether we can write the character using the currently \
205 selected character set. */ \
208 if (set != ASCII_set) \
212 if (__builtin_expect (outptr == outend, 0)) \
214 result = __GCONV_FULL_OUTPUT; \
223 unsigned char buf[2]; \
224 /* Fake initialization to keep gcc quiet. */ \
225 asm ("" : "=m" (buf)); \
227 size_t written = ucs4_to_ksc5601 (ch, buf, 2); \
228 if (__builtin_expect (written, 0) == __UNKNOWN_10646_CHAR) \
230 UNICODE_TAG_HANDLER (ch, 4); \
232 /* Illegal character. */ \
233 STANDARD_TO_LOOP_ERR_HANDLER (4); \
237 assert (written == 2); \
239 /* We use KSC 5601. */ \
240 if (set != KSC5601_set) \
246 if (__builtin_expect (outptr + 2 > outend, 0)) \
248 result = __GCONV_FULL_OUTPUT; \
252 *outptr++ = buf[0]; \
253 *outptr++ = buf[1]; \
257 /* Now that we wrote the output increment the input pointer. */ \
260 #define LOOP_NEED_FLAGS
261 #define EXTRA_LOOP_DECLS , int *setp
262 #define INIT_PARAMS int set = *setp
263 #define REINIT_PARAMS set = *setp
264 #define UPDATE_PARAMS *setp = set
265 #include <iconv/loop.c>
268 /* Now define the toplevel functions. */
269 #include <iconv/skeleton.c>