Update.
[glibc.git] / iconvdata / iso-2022-kr.c
blobab03472f266349e2725f7dd0dae61b014c161ca1
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. */
21 #include <gconv.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include "ksc5601.h"
26 #include <assert.h>
28 /* This makes obvious what everybody knows: 0x1b is the Esc character. */
29 #define ESC 0x1b
31 /* The shift sequences for this charset (it does not use ESC). */
32 #define SI 0x0f
33 #define SO 0x0e
35 /* Definitions used in the body of the `gconv' function. */
36 #define CHARSET_NAME "ISO-2022-KR//"
37 #define DEFINE_INIT 1
38 #define DEFINE_FINI 1
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 \
46 int save_set; \
47 int *setp = &data->__statep->__count; \
48 if (!FROM_DIRECTION && !data->__internal_use \
49 && data->__invocation_counter == 0) \
50 { \
51 /* Emit the designator sequence. */ \
52 if (outbuf + 4 > outend) \
53 return __GCONV_FULL_OUTPUT; \
55 *outbuf++ = ESC; \
56 *outbuf++ = '$'; \
57 *outbuf++ = ')'; \
58 *outbuf++ = 'C'; \
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: */
65 enum
67 ASCII_set = 0,
68 KSC5601_set = 8
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
74 flushing. */
75 #define EMIT_SHIFT_TO_INIT \
76 if (data->__statep->__count != ASCII_set) \
77 { \
78 if (FROM_DIRECTION) \
79 { \
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; \
84 } \
85 else \
86 { \
87 unsigned char *outbuf = data->__outbuf; \
89 /* We are not in the initial state. To switch back we have \
90 to emit `SI'. */ \
91 if (outbuf == data->__outbufend) \
92 /* We don't have enough room in the output buffer. */ \
93 status = __GCONV_FULL_OUTPUT; \
94 else \
95 { \
96 /* Write out the shift sequence. */ \
97 *outbuf++ = SI; \
98 if (data->__is_last) \
99 *written += 1; \
100 data->__outbuf = outbuf; \
101 data->__statep->__count = ASCII_set; \
107 /* Since we might have to reset input pointer we must be able to save
108 and retore the state. */
109 #define SAVE_RESET_STATE(Save) \
110 if (Save) \
111 save_set = *setp; \
112 else \
113 *setp = save_set
116 /* First define the conversion function from ISO-2022-KR to UCS4. */
117 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
118 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
119 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
120 #define LOOPFCT FROM_LOOP
121 #define BODY \
123 uint32_t ch = *inptr; \
125 /* This is a 7bit character set, disallow all 8bit characters. */ \
126 if (ch > 0x7f) \
128 result = __GCONV_ILLEGAL_INPUT; \
129 break; \
132 /* Recognize escape sequences. */ \
133 if (ch == ESC) \
135 /* We don't really have to handle escape sequences since all the \
136 switching is done using the SI and SO bytes. But we have to \
137 recognize `Esc $ ) C' since this is a kind of flag for this \
138 encoding. We simply ignore it. */ \
139 if ((NEED_LENGTH_TEST && inptr + 1 > inend) \
140 || (inptr[1] == '$' \
141 && ((NEED_LENGTH_TEST && inptr + 2 > inend) \
142 || (inptr[2] == ')' && inptr + 3 > inend)))) \
145 result = __GCONV_EMPTY_INPUT; \
146 break; \
148 if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \
150 /* Yeah, yeah, we know this is ISO 2022-KR. */ \
151 inptr += 4; \
152 continue; \
155 else if (ch == SO) \
157 /* Switch to use KSC. */ \
158 ++inptr; \
159 set = KSC5601_set; \
160 continue; \
162 else if (ch == SI) \
164 /* Switch to use ASCII. */ \
165 ++inptr; \
166 set = ASCII_set; \
167 continue; \
170 if (set == ASCII_set) \
172 /* Almost done, just advance the input pointer. */ \
173 ++inptr; \
175 else \
177 assert (set == KSC5601_set); \
179 /* Use the KSC 5601 table. */ \
180 ch = ksc5601_to_ucs4 (&inptr, \
181 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
183 if (NEED_LENGTH_TEST && ch == 0) \
185 result = __GCONV_EMPTY_INPUT; \
186 break; \
188 else if (ch == __UNKNOWN_10646_CHAR) \
190 result = __GCONV_ILLEGAL_INPUT; \
191 break; \
195 put32 (outptr, ch); \
196 outptr += 4; \
198 #define EXTRA_LOOP_DECLS , int *setp
199 #define INIT_PARAMS int set = *setp
200 #define UPDATE_PARAMS *setp = set
201 #include <iconv/loop.c>
204 /* Next, define the other direction. */
205 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
206 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
207 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
208 #define LOOPFCT TO_LOOP
209 #define BODY \
211 uint32_t ch; \
212 size_t written = 0; \
214 ch = get32 (inptr); \
216 /* First see whether we can write the character using the currently \
217 selected character set. */ \
218 if (ch < 0x80) \
220 if (set != ASCII_set) \
222 *outptr++ = SI; \
223 set = ASCII_set; \
224 if (NEED_LENGTH_TEST && outptr == outend) \
226 result = __GCONV_FULL_OUTPUT; \
227 break; \
231 *outptr++ = ch; \
232 written = 1; \
234 else \
236 char buf[2]; \
238 written = ucs4_to_ksc5601 (ch, buf, 2); \
240 if (written == __UNKNOWN_10646_CHAR) \
242 /* Illegal character. */ \
243 result = __GCONV_ILLEGAL_INPUT; \
244 break; \
246 assert (written == 2); \
248 /* We use KSC 5601. */ \
249 if (set != KSC5601_set) \
251 *outptr++ = SO; \
252 set = KSC5601_set; \
255 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
257 result = __GCONV_FULL_OUTPUT; \
258 break; \
261 *outptr++ = buf[0]; \
262 *outptr++ = buf[1]; \
265 /* Now that we wrote the output increment the input pointer. */ \
266 inptr += 4; \
268 #define EXTRA_LOOP_DECLS , int *setp
269 #define INIT_PARAMS int set = *setp
270 #define UPDATE_PARAMS *setp = set
271 #include <iconv/loop.c>
274 /* Now define the toplevel functions. */
275 #include <iconv/skeleton.c>