]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconvdata/iso-2022-kr.c
Make tests consistently use *.out output files.
[thirdparty/glibc.git] / iconvdata / iso-2022-kr.c
CommitLineData
8babd571 1/* Conversion module for ISO-2022-KR.
d4697bc9 2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
8babd571
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
8babd571
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
8babd571 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
8babd571 19
55985355 20#include <dlfcn.h>
8babd571
UD
21#include <gconv.h>
22#include <stdint.h>
23#include <string.h>
24#include "ksc5601.h"
25
2fb2e75a
UD
26#include <assert.h>
27
8babd571 28/* This makes obvious what everybody knows: 0x1b is the Esc character. */
2fb2e75a
UD
29#define ESC 0x1b
30
c9fc0e22 31/* The shift sequences for this charset (it does not use ESC). */
8babd571
UD
32#define SI 0x0f
33#define SO 0x0e
34
35/* Definitions used in the body of the `gconv' function. */
2fb2e75a 36#define CHARSET_NAME "ISO-2022-KR//"
8babd571
UD
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
0e15c4b6 42#define MAX_NEEDED_FROM 4
8babd571
UD
43#define MIN_NEEDED_TO 4
44#define MAX_NEEDED_TO 4
45#define PREPARE_LOOP \
2fb2e75a 46 int save_set; \
aa831d6d 47 int *setp = &data->__statep->__count; \
d64b6ad0
UD
48 if (!FROM_DIRECTION && !data->__internal_use \
49 && data->__invocation_counter == 0) \
e3e0a182
UD
50 { \
51 /* Emit the designator sequence. */ \
c9fc0e22 52 if (outbuf + 4 > outend) \
d64b6ad0 53 return __GCONV_FULL_OUTPUT; \
e3e0a182 54 \
c9fc0e22
UD
55 *outbuf++ = ESC; \
56 *outbuf++ = '$'; \
57 *outbuf++ = ')'; \
58 *outbuf++ = 'C'; \
e3e0a182 59 }
66175fa8 60#define EXTRA_LOOP_ARGS , setp
2fb2e75a 61
8babd571
UD
62
63/* The COUNT element of the state keeps track of the currently selected
64 character set. The possible values are: */
65enum
66{
67 ASCII_set = 0,
fd1b5c0f 68 KSC5601_set = 8
8babd571
UD
69};
70
71
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 \
aa831d6d 76 if (data->__statep->__count != ASCII_set) \
8babd571 77 { \
66175fa8 78 if (FROM_DIRECTION) \
fd1b5c0f
UD
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 } \
8babd571
UD
85 else \
86 { \
8babd571 87 /* We are not in the initial state. To switch back we have \
66175fa8 88 to emit `SI'. */ \
a1ffb40e 89 if (__glibc_unlikely (outbuf == outend)) \
8babd571 90 /* We don't have enough room in the output buffer. */ \
d64b6ad0 91 status = __GCONV_FULL_OUTPUT; \
8babd571
UD
92 else \
93 { \
94 /* Write out the shift sequence. */ \
66175fa8 95 *outbuf++ = SI; \
aa831d6d 96 data->__statep->__count = ASCII_set; \
8babd571
UD
97 } \
98 } \
99 }
100
101
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) \
105 if (Save) \
66175fa8 106 save_set = *setp; \
8babd571 107 else \
66175fa8 108 *setp = save_set
8babd571
UD
109
110
922903d2 111/* First define the conversion function from ISO-2022-KR to UCS4. */
8babd571
UD
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
116#define BODY \
117 { \
118 uint32_t ch = *inptr; \
119 \
120 /* This is a 7bit character set, disallow all 8bit characters. */ \
a1ffb40e 121 if (__glibc_unlikely (ch > 0x7f)) \
e438a468 122 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
8babd571
UD
123 \
124 /* Recognize escape sequences. */ \
89301d68 125 if (__builtin_expect (ch, 0) == ESC) \
8babd571
UD
126 { \
127 /* We don't really have to handle escape sequences since all the \
66175fa8 128 switching is done using the SI and SO bytes. But we have to \
8babd571
UD
129 recognize `Esc $ ) C' since this is a kind of flag for this \
130 encoding. We simply ignore it. */ \
0e15c4b6 131 if (__builtin_expect (inptr + 2 > inend, 0) \
8babd571 132 || (inptr[1] == '$' \
0e15c4b6 133 && (__builtin_expect (inptr + 3 > inend, 0) \
89301d68 134 || (inptr[2] == ')' \
0e15c4b6 135 && __builtin_expect (inptr + 4 > inend, 0))))) \
8babd571 136 { \
c7c3b0e9 137 result = __GCONV_INCOMPLETE_INPUT; \
8babd571
UD
138 break; \
139 } \
140 if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C') \
141 { \
142 /* Yeah, yeah, we know this is ISO 2022-KR. */ \
143 inptr += 4; \
144 continue; \
145 } \
146 } \
89301d68 147 else if (__builtin_expect (ch, 0) == SO) \
8babd571
UD
148 { \
149 /* Switch to use KSC. */ \
150 ++inptr; \
151 set = KSC5601_set; \
152 continue; \
153 } \
89301d68 154 else if (__builtin_expect (ch, 0) == SI) \
8babd571
UD
155 { \
156 /* Switch to use ASCII. */ \
157 ++inptr; \
158 set = ASCII_set; \
159 continue; \
160 } \
161 \
66175fa8
UD
162 if (set == ASCII_set) \
163 { \
66175fa8
UD
164 /* Almost done, just advance the input pointer. */ \
165 ++inptr; \
166 } \
8babd571
UD
167 else \
168 { \
169 assert (set == KSC5601_set); \
170 \
171 /* Use the KSC 5601 table. */ \
55985355 172 ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0); \
8babd571 173 \
a1ffb40e 174 if (__glibc_unlikely (ch == 0)) \
8babd571 175 { \
c7c3b0e9 176 result = __GCONV_INCOMPLETE_INPUT; \
8babd571
UD
177 break; \
178 } \
a1ffb40e 179 else if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \
8babd571 180 { \
e438a468 181 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
8babd571
UD
182 } \
183 } \
184 \
77e1d15a
UD
185 put32 (outptr, ch); \
186 outptr += 4; \
8babd571 187 }
55985355 188#define LOOP_NEED_FLAGS
66175fa8
UD
189#define EXTRA_LOOP_DECLS , int *setp
190#define INIT_PARAMS int set = *setp
191#define UPDATE_PARAMS *setp = set
8babd571
UD
192#include <iconv/loop.c>
193
194
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
200#define BODY \
201 { \
278bfa00 202 uint32_t ch = get32 (inptr); \
8babd571
UD
203 \
204 /* First see whether we can write the character using the currently \
205 selected character set. */ \
66175fa8 206 if (ch < 0x80) \
8babd571 207 { \
66175fa8 208 if (set != ASCII_set) \
8babd571 209 { \
66175fa8
UD
210 *outptr++ = SI; \
211 set = ASCII_set; \
a1ffb40e 212 if (__glibc_unlikely (outptr == outend)) \
66175fa8 213 { \
d64b6ad0 214 result = __GCONV_FULL_OUTPUT; \
66175fa8
UD
215 break; \
216 } \
8babd571 217 } \
66175fa8
UD
218 \
219 *outptr++ = ch; \
8babd571
UD
220 } \
221 else \
222 { \
085a4412 223 unsigned char buf[2]; \
278bfa00
UD
224 /* Fake initialization to keep gcc quiet. */ \
225 asm ("" : "=m" (buf)); \
8babd571 226 \
278bfa00 227 size_t written = ucs4_to_ksc5601 (ch, buf, 2); \
89301d68 228 if (__builtin_expect (written, 0) == __UNKNOWN_10646_CHAR) \
8babd571 229 { \
601d2942
UD
230 UNICODE_TAG_HANDLER (ch, 4); \
231 \
66175fa8 232 /* Illegal character. */ \
e438a468 233 STANDARD_TO_LOOP_ERR_HANDLER (4); \
66175fa8 234 } \
85830c4c 235 else \
66175fa8 236 { \
85830c4c
UD
237 assert (written == 2); \
238 \
239 /* We use KSC 5601. */ \
240 if (set != KSC5601_set) \
241 { \
242 *outptr++ = SO; \
243 set = KSC5601_set; \
244 } \
245 \
a1ffb40e 246 if (__glibc_unlikely (outptr + 2 > outend)) \
85830c4c
UD
247 { \
248 result = __GCONV_FULL_OUTPUT; \
249 break; \
250 } \
66175fa8 251 \
85830c4c
UD
252 *outptr++ = buf[0]; \
253 *outptr++ = buf[1]; \
254 } \
8babd571
UD
255 } \
256 \
257 /* Now that we wrote the output increment the input pointer. */ \
258 inptr += 4; \
259 }
55985355 260#define LOOP_NEED_FLAGS
66175fa8
UD
261#define EXTRA_LOOP_DECLS , int *setp
262#define INIT_PARAMS int set = *setp
4b1b449d 263#define REINIT_PARAMS set = *setp
66175fa8 264#define UPDATE_PARAMS *setp = set
8babd571
UD
265#include <iconv/loop.c>
266
267
268/* Now define the toplevel functions. */
269#include <iconv/skeleton.c>