1 /* Conversion module for ISO-2022-KR.
2 Copyright (C) 1998 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 (we 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 set = data->statep->count; \
48 if (!FROM_DIRECTION && !data->internal_use && data->invocation_counter == 0)\
50 /* Emit the designator sequence. */ \
51 if (outptr + 4 > outend) \
52 return GCONV_FULL_OUTPUT; \
59 #define EXTRA_LOOP_ARGS , set
62 /* The COUNT element of the state keeps track of the currently selected
63 character set. The possible values are: */
71 /* Since this is a stateful encoding we have to provide code which resets
72 the output state to the initial state. This has to be done during the
74 #define EMIT_SHIFT_TO_INIT \
75 if (data->statep->count != 0) \
77 if (step->data == &from_object) \
78 /* It's easy, we don't have to emit anything, we just reset the \
79 state for the input. */ \
80 data->statep->count = 0; \
83 char *outbuf = data->outbuf; \
85 /* We are not in the initial state. To switch back we have \
87 if (outbuf == data->outbufend) \
88 /* We don't have enough room in the output buffer. */ \
89 status = GCONV_FULL_OUTPUT; \
92 /* Write out the shift sequence. */ \
94 data->outbuf = outbuf; \
95 data->statep->count = 0; \
101 /* Since we might have to reset input pointer we must be able to save
102 and retore the state. */
103 #define SAVE_RESET_STATE(Save) \
110 /* First define the conversion function from ISO-2022-JP to UCS4. */
111 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
112 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
113 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
114 #define LOOPFCT FROM_LOOP
117 uint32_t ch = *inptr; \
119 /* This is a 7bit character set, disallow all 8bit characters. */ \
122 result = GCONV_ILLEGAL_INPUT; \
126 /* Recognize escape sequences. */ \
129 /* We don't really have to handle escape sequences since all the \
130 switching is done using the SI and SO bytes. Butwe have to \
131 recognize `Esc $ ) C' since this is a kind of flag for this \
132 encoding. We simply ignore it. */ \
133 if (inptr + 1 > inend \
134 || (inptr[1] == '$' \
135 && (inptr + 2 > inend \
136 || (inptr[2] == ')' && inptr + 3 > inend)))) \
139 result = GCONV_EMPTY_INPUT; \
142 if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \
144 /* Yeah, yeah, we know this is ISO 2022-KR. */ \
151 /* Switch to use KSC. */ \
158 /* Switch to use ASCII. */ \
164 if (set == ASCII_set || ch < 0x21 || ch == 0x7f) \
165 /* Almost done, just advance the input pointer. */ \
169 assert (set == KSC5601_set); \
171 /* Use the KSC 5601 table. */ \
172 ch = ksc5601_to_ucs4 (&inptr, \
173 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
175 if (NEED_LENGTH_TEST && ch == 0) \
177 result = GCONV_EMPTY_INPUT; \
180 else if (ch == UNKNOWN_10646_CHAR) \
182 result = GCONV_ILLEGAL_INPUT; \
187 *((uint32_t *) outptr)++ = ch; \
189 #define EXTRA_LOOP_DECLS , int set
190 #include <iconv/loop.c>
193 /* Next, define the other direction. */
194 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
195 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
196 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
197 #define LOOPFCT TO_LOOP
201 size_t written = 0; \
203 ch = *((uint32_t *) inptr); \
205 /* First see whether we can write the character using the currently \
206 selected character set. */ \
207 if (set == ASCII_set || (ch >= 0x01 && (ch < 0x21 || ch == 0x7f))) \
209 /* Please note that the NUL byte is *not* matched if we are not \
210 currently using the ASCII charset. This is because we must \
211 switch to the initial state whenever a NUL byte is written. */ \
220 assert (set == KSC5601_set); \
222 written = ucs4_to_ksc5601 (ch, outptr, \
223 (NEED_LENGTH_TEST ? outend - outptr : 2)); \
225 if (NEED_LENGTH_TEST && written == 0) \
227 result = GCONV_FULL_OUTPUT; \
230 if (written == UNKNOWN_10646_CHAR) \
232 /* Either this is an unknown character or we have to switch \
233 the currently selected character set. The character sets \
234 do not code entirely separate parts of ISO 10646 and \
235 therefore there is no single correct result. If we choose \
236 the character set to use wrong we might be end up with \
237 using yet another character set for the next character \
238 though the current and the next could be encoded with one \
239 character set. We leave this kind of optimization for \
240 later and now simply use a fixed order in which we test for \
245 /* We must encode using ASCII. First write out the \
246 escape sequence. */ \
250 if (NEED_LENGTH_TEST && outptr == outend) \
252 result = GCONV_FULL_OUTPUT; \
262 written = ucs4_to_ksc5601 (ch, buf, 2); \
263 if (written != UNKNOWN_10646_CHAR) \
265 /* We use KSC 5601. */ \
269 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
271 result = GCONV_FULL_OUTPUT; \
275 *outptr++ = buf[0]; \
276 *outptr++ = buf[1]; \
280 result = GCONV_ILLEGAL_INPUT; \
287 /* Now that we wrote the output increment the input pointer. */ \
290 #define EXTRA_LOOP_DECLS , int set
291 #include <iconv/loop.c>
294 /* Now define the toplevel functions. */
295 #include <iconv/skeleton.c>