Update.
[glibc.git] / iconvdata / iso-2022-kr.c
blobb6aee4d933756a2d1c33e996520eaa603384f2d9
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. */
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 && data->invocation_counter == 0)\
49 { \
50 /* Emit the designator sequence. */ \
51 if (outbuf + 4 > outend) \
52 return GCONV_FULL_OUTPUT; \
54 *outbuf++ = ESC; \
55 *outbuf++ = '$'; \
56 *outbuf++ = ')'; \
57 *outbuf++ = 'C'; \
59 #define EXTRA_LOOP_ARGS , setp
62 /* The COUNT element of the state keeps track of the currently selected
63 character set. The possible values are: */
64 enum
66 ASCII_set = 0,
67 KSC5601_set
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
73 flushing. */
74 #define EMIT_SHIFT_TO_INIT \
75 if (data->statep->count != ASCII_set) \
76 { \
77 if (FROM_DIRECTION) \
78 /* It's easy, we don't have to emit anything, we just reset the \
79 state for the input. */ \
80 data->statep->count = ASCII_set; \
81 else \
82 { \
83 char *outbuf = data->outbuf; \
85 /* We are not in the initial state. To switch back we have \
86 to emit `SI'. */ \
87 if (outbuf == data->outbufend) \
88 /* We don't have enough room in the output buffer. */ \
89 status = GCONV_FULL_OUTPUT; \
90 else \
91 { \
92 /* Write out the shift sequence. */ \
93 *outbuf++ = SI; \
94 data->outbuf = outbuf; \
95 data->statep->count = ASCII_set; \
96 } \
97 } \
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) \
104 if (Save) \
105 save_set = *setp; \
106 else \
107 *setp = save_set
110 /* First define the conversion function from ISO-2022-KR 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
115 #define BODY \
117 uint32_t ch = *inptr; \
119 /* This is a 7bit character set, disallow all 8bit characters. */ \
120 if (ch > 0x7f) \
122 result = GCONV_ILLEGAL_INPUT; \
123 break; \
126 /* Recognize escape sequences. */ \
127 if (ch == ESC) \
129 /* We don't really have to handle escape sequences since all the \
130 switching is done using the SI and SO bytes. But we 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; \
140 break; \
142 if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \
144 /* Yeah, yeah, we know this is ISO 2022-KR. */ \
145 inptr += 4; \
146 continue; \
149 else if (ch == SO) \
151 /* Switch to use KSC. */ \
152 ++inptr; \
153 set = KSC5601_set; \
154 continue; \
156 else if (ch == SI) \
158 /* Switch to use ASCII. */ \
159 ++inptr; \
160 set = ASCII_set; \
161 continue; \
164 if (set == ASCII_set) \
166 if (ch >= 0x80) \
168 result = GCONV_ILLEGAL_INPUT; \
169 break; \
171 /* Almost done, just advance the input pointer. */ \
172 ++inptr; \
174 else \
176 assert (set == KSC5601_set); \
178 /* Use the KSC 5601 table. */ \
179 ch = ksc5601_to_ucs4 (&inptr, \
180 NEED_LENGTH_TEST ? inend - inptr : 2, 0); \
182 if (NEED_LENGTH_TEST && ch == 0) \
184 result = GCONV_EMPTY_INPUT; \
185 break; \
187 else if (ch == UNKNOWN_10646_CHAR) \
189 result = GCONV_ILLEGAL_INPUT; \
190 break; \
194 *((uint32_t *) outptr)++ = ch; \
196 #define EXTRA_LOOP_DECLS , int *setp
197 #define INIT_PARAMS int set = *setp
198 #define UPDATE_PARAMS *setp = set
199 #include <iconv/loop.c>
202 /* Next, define the other direction. */
203 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
204 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
205 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
206 #define LOOPFCT TO_LOOP
207 #define BODY \
209 uint32_t ch; \
210 size_t written = 0; \
212 ch = *((uint32_t *) inptr); \
214 /* First see whether we can write the character using the currently \
215 selected character set. */ \
216 if (ch < 0x80) \
218 if (set != ASCII_set) \
220 *outptr++ = SI; \
221 set = ASCII_set; \
222 if (NEED_LENGTH_TEST && outptr == outend) \
224 result = GCONV_FULL_OUTPUT; \
225 break; \
229 *outptr++ = ch; \
230 written = 1; \
232 else \
234 char buf[2]; \
236 written = ucs4_to_ksc5601 (ch, buf, 2); \
238 if (written == UNKNOWN_10646_CHAR) \
240 /* Illegal character. */ \
241 result = GCONV_ILLEGAL_INPUT; \
242 break; \
244 assert (written == 2); \
246 /* We use KSC 5601. */ \
247 if (set != KSC5601_set) \
249 *outptr++ = SO; \
250 set = KSC5601_set; \
253 if (NEED_LENGTH_TEST && outptr + 2 > outend) \
255 result = GCONV_FULL_OUTPUT; \
256 break; \
259 *outptr++ = buf[0]; \
260 *outptr++ = buf[1]; \
263 /* Now that we wrote the output increment the input pointer. */ \
264 inptr += 4; \
266 #define EXTRA_LOOP_DECLS , int *setp
267 #define INIT_PARAMS int set = *setp
268 #define UPDATE_PARAMS *setp = set
269 #include <iconv/loop.c>
272 /* Now define the toplevel functions. */
273 #include <iconv/skeleton.c>