citrus_iso2022.c revision 1.1.2.2 1 1.1.2.2 nathanw /* $NetBSD: citrus_iso2022.c,v 1.1.2.2 2002/03/22 20:42:03 nathanw Exp $ */
2 1.1.2.2 nathanw
3 1.1.2.2 nathanw /*-
4 1.1.2.2 nathanw * Copyright (c)1999, 2002 Citrus Project,
5 1.1.2.2 nathanw * All rights reserved.
6 1.1.2.2 nathanw *
7 1.1.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.1.2.2 nathanw * are met:
10 1.1.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.1.2.2 nathanw *
16 1.1.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 nathanw * SUCH DAMAGE.
27 1.1.2.2 nathanw *
28 1.1.2.2 nathanw * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/iso2022.c,v 1.23 2001/06/21 01:51:44 yamt Exp $
29 1.1.2.2 nathanw */
30 1.1.2.2 nathanw
31 1.1.2.2 nathanw #include <sys/cdefs.h>
32 1.1.2.2 nathanw #if defined(LIBC_SCCS) && !defined(lint)
33 1.1.2.2 nathanw __RCSID("$NetBSD: citrus_iso2022.c,v 1.1.2.2 2002/03/22 20:42:03 nathanw Exp $");
34 1.1.2.2 nathanw #endif /* LIBC_SCCS and not lint */
35 1.1.2.2 nathanw
36 1.1.2.2 nathanw #include <assert.h>
37 1.1.2.2 nathanw #include <errno.h>
38 1.1.2.2 nathanw #include <string.h>
39 1.1.2.2 nathanw #include <stdio.h>
40 1.1.2.2 nathanw #include <stdlib.h>
41 1.1.2.2 nathanw #include <stddef.h>
42 1.1.2.2 nathanw #include <locale.h>
43 1.1.2.2 nathanw #include <wchar.h>
44 1.1.2.2 nathanw #include <sys/types.h>
45 1.1.2.2 nathanw #include <limits.h>
46 1.1.2.2 nathanw #include "citrus_module.h"
47 1.1.2.2 nathanw #include "citrus_ctype.h"
48 1.1.2.2 nathanw #include "citrus_iso2022.h"
49 1.1.2.2 nathanw
50 1.1.2.2 nathanw
51 1.1.2.2 nathanw /* ----------------------------------------------------------------------
52 1.1.2.2 nathanw * private stuffs used by templates
53 1.1.2.2 nathanw */
54 1.1.2.2 nathanw
55 1.1.2.2 nathanw
56 1.1.2.2 nathanw /*
57 1.1.2.2 nathanw * wchar_t mappings:
58 1.1.2.2 nathanw * ASCII (ESC ( B) 00000000 00000000 00000000 0xxxxxxx
59 1.1.2.2 nathanw * iso-8859-1 (ESC , A) 00000000 00000000 00000000 1xxxxxxx
60 1.1.2.2 nathanw * 94 charset (ESC ( F) 0fffffff 00000000 00000000 0xxxxxxx
61 1.1.2.2 nathanw * 94 charset (ESC ( M F) 0fffffff 1mmmmmmm 00000000 0xxxxxxx
62 1.1.2.2 nathanw * 96 charset (ESC , F) 0fffffff 00000000 00000000 1xxxxxxx
63 1.1.2.2 nathanw * 96 charset (ESC , M F) 0fffffff 1mmmmmmm 00000000 1xxxxxxx
64 1.1.2.2 nathanw * 94x94 charset (ESC $ ( F) 0fffffff 00000000 0xxxxxxx 0xxxxxxx
65 1.1.2.2 nathanw * 96x96 charset (ESC $ , F) 0fffffff 00000000 0xxxxxxx 1xxxxxxx
66 1.1.2.2 nathanw * 94x94 charset (ESC & V ESC $ ( F)
67 1.1.2.2 nathanw * 0fffffff 1vvvvvvv 0xxxxxxx 0xxxxxxx
68 1.1.2.2 nathanw * 94x94x94 charset (ESC $ ( F) 0fffffff 0xxxxxxx 0xxxxxxx 0xxxxxxx
69 1.1.2.2 nathanw * 96x96x96 charset (ESC $ , F) 0fffffff 0xxxxxxx 0xxxxxxx 1xxxxxxx
70 1.1.2.2 nathanw */
71 1.1.2.2 nathanw
72 1.1.2.2 nathanw typedef struct {
73 1.1.2.2 nathanw u_char type;
74 1.1.2.2 nathanw #define CS94 (0U)
75 1.1.2.2 nathanw #define CS96 (1U)
76 1.1.2.2 nathanw #define CS94MULTI (2U)
77 1.1.2.2 nathanw #define CS96MULTI (3U)
78 1.1.2.2 nathanw
79 1.1.2.2 nathanw u_char final;
80 1.1.2.2 nathanw u_char interm;
81 1.1.2.2 nathanw u_char vers;
82 1.1.2.2 nathanw } _ISO2022Charset;
83 1.1.2.2 nathanw
84 1.1.2.2 nathanw typedef struct {
85 1.1.2.2 nathanw _ISO2022Charset g[4];
86 1.1.2.2 nathanw /* need 3 bits to hold -1, 0, ..., 3 */
87 1.1.2.2 nathanw int gl:3,
88 1.1.2.2 nathanw gr:3,
89 1.1.2.2 nathanw singlegl:3,
90 1.1.2.2 nathanw singlegr:3;
91 1.1.2.2 nathanw char ch[7]; /* longest escape sequence (ESC & V ESC $ ( F) */
92 1.1.2.2 nathanw int chlen;
93 1.1.2.2 nathanw } _ISO2022State __attribute__((__packed__));
94 1.1.2.2 nathanw
95 1.1.2.2 nathanw typedef struct {
96 1.1.2.2 nathanw _ISO2022Charset *recommend[4];
97 1.1.2.2 nathanw size_t recommendsize[4];
98 1.1.2.2 nathanw _ISO2022Charset initg[4];
99 1.1.2.2 nathanw int maxcharset;
100 1.1.2.2 nathanw int flags;
101 1.1.2.2 nathanw #define F_8BIT 0x0001
102 1.1.2.2 nathanw #define F_NOOLD 0x0002
103 1.1.2.2 nathanw #define F_SI 0x0010 /*0F*/
104 1.1.2.2 nathanw #define F_SO 0x0020 /*0E*/
105 1.1.2.2 nathanw #define F_LS0 0x0010 /*0F*/
106 1.1.2.2 nathanw #define F_LS1 0x0020 /*0E*/
107 1.1.2.2 nathanw #define F_LS2 0x0040 /*ESC n*/
108 1.1.2.2 nathanw #define F_LS3 0x0080 /*ESC o*/
109 1.1.2.2 nathanw #define F_LS1R 0x0100 /*ESC ~*/
110 1.1.2.2 nathanw #define F_LS2R 0x0200 /*ESC }*/
111 1.1.2.2 nathanw #define F_LS3R 0x0400 /*ESC |*/
112 1.1.2.2 nathanw #define F_SS2 0x0800 /*ESC N*/
113 1.1.2.2 nathanw #define F_SS3 0x1000 /*ESC O*/
114 1.1.2.2 nathanw #define F_SS2R 0x2000 /*8E*/
115 1.1.2.2 nathanw #define F_SS3R 0x4000 /*8F*/
116 1.1.2.2 nathanw } _ISO2022EncodingInfo;
117 1.1.2.2 nathanw typedef struct {
118 1.1.2.2 nathanw _ISO2022EncodingInfo ei;
119 1.1.2.2 nathanw struct {
120 1.1.2.2 nathanw /* for future multi-locale facility */
121 1.1.2.2 nathanw _ISO2022State s_mblen;
122 1.1.2.2 nathanw _ISO2022State s_mbrlen;
123 1.1.2.2 nathanw _ISO2022State s_mbrtowc;
124 1.1.2.2 nathanw _ISO2022State s_mbtowc;
125 1.1.2.2 nathanw _ISO2022State s_mbsrtowcs;
126 1.1.2.2 nathanw _ISO2022State s_wcrtomb;
127 1.1.2.2 nathanw _ISO2022State s_wcsrtombs;
128 1.1.2.2 nathanw _ISO2022State s_wcstombs;
129 1.1.2.2 nathanw _ISO2022State s_wctomb;
130 1.1.2.2 nathanw } states;
131 1.1.2.2 nathanw } _ISO2022CTypeInfo;
132 1.1.2.2 nathanw
133 1.1.2.2 nathanw #define _TO_EI(_cl_) ((_ISO2022EncodingInfo *)(_cl_))
134 1.1.2.2 nathanw #define _TO_CEI(_cl_) ((_ISO2022CTypeInfo *)(_cl_))
135 1.1.2.2 nathanw #define _TO_STATE(_ps_) ((_ISO2022State *)(_ps_))
136 1.1.2.2 nathanw #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
137 1.1.2.2 nathanw #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
138 1.1.2.2 nathanw
139 1.1.2.2 nathanw #define _FUNCNAME(m) _citrus_ISO2022_##m
140 1.1.2.2 nathanw #define _ENCODING_INFO _ISO2022EncodingInfo
141 1.1.2.2 nathanw #define _CTYPE_INFO _ISO2022CTypeInfo
142 1.1.2.2 nathanw #define _ENCODING_STATE _ISO2022State
143 1.1.2.2 nathanw #define _ENCODING_MB_CUR_MAX(_cl_) MB_LEN_MAX
144 1.1.2.2 nathanw #define _ENCODING_IS_STATE_DEPENDENT 1
145 1.1.2.2 nathanw
146 1.1.2.2 nathanw
147 1.1.2.2 nathanw #define _ISO2022INVALID (wchar_t)-1
148 1.1.2.2 nathanw
149 1.1.2.2 nathanw static __inline int isc0(__uint8_t x) { return ((x & 0x1f) == x); }
150 1.1.2.2 nathanw static __inline int isc1(__uint8_t x) { return (0x80 <= x && x <= 0x9f); }
151 1.1.2.2 nathanw static __inline int iscntl(__uint8_t x) { return (isc0(x) || isc1(x) || x == 0x7f); }
152 1.1.2.2 nathanw static __inline int is94(__uint8_t x) { return (0x21 <= x && x <= 0x7e); }
153 1.1.2.2 nathanw static __inline int is96(__uint8_t x) { return (0x20 <= x && x <= 0x7f); }
154 1.1.2.2 nathanw static __inline int isecma(__uint8_t x) { return (0x30 <= x && x <= 0x7f); }
155 1.1.2.2 nathanw static __inline int isinterm(__uint8_t x) { return (0x20 <= x && x <= 0x2f); }
156 1.1.2.2 nathanw static __inline int isthree(__uint8_t x) { return (0x60 <= x && x <= 0x6f); }
157 1.1.2.2 nathanw
158 1.1.2.2 nathanw static __inline int
159 1.1.2.2 nathanw getcs(const char * __restrict p, _ISO2022Charset * __restrict cs)
160 1.1.2.2 nathanw {
161 1.1.2.2 nathanw
162 1.1.2.2 nathanw _DIAGASSERT(p != NULL);
163 1.1.2.2 nathanw _DIAGASSERT(cs != NULL);
164 1.1.2.2 nathanw
165 1.1.2.2 nathanw if (!strncmp(p, "94$", 3) && p[3] && !p[4]) {
166 1.1.2.2 nathanw cs->final = (u_char)(p[3] & 0xff);
167 1.1.2.2 nathanw cs->interm = '\0';
168 1.1.2.2 nathanw cs->vers = '\0';
169 1.1.2.2 nathanw cs->type = CS94MULTI;
170 1.1.2.2 nathanw } else if (!strncmp(p, "96$", 3) && p[3] && !p[4]) {
171 1.1.2.2 nathanw cs->final = (u_char)(p[3] & 0xff);
172 1.1.2.2 nathanw cs->interm = '\0';
173 1.1.2.2 nathanw cs->vers = '\0';
174 1.1.2.2 nathanw cs->type = CS96MULTI;
175 1.1.2.2 nathanw } else if (!strncmp(p, "94", 2) && p[2] && !p[3]) {
176 1.1.2.2 nathanw cs->final = (u_char)(p[2] & 0xff);
177 1.1.2.2 nathanw cs->interm = '\0';
178 1.1.2.2 nathanw cs->vers = '\0';
179 1.1.2.2 nathanw cs->type = CS94;
180 1.1.2.2 nathanw } else if (!strncmp(p, "96", 2) && p[2] && !p[3]) {
181 1.1.2.2 nathanw cs->final = (u_char )(p[2] & 0xff);
182 1.1.2.2 nathanw cs->interm = '\0';
183 1.1.2.2 nathanw cs->vers = '\0';
184 1.1.2.2 nathanw cs->type = CS96;
185 1.1.2.2 nathanw } else {
186 1.1.2.2 nathanw return 1;
187 1.1.2.2 nathanw }
188 1.1.2.2 nathanw
189 1.1.2.2 nathanw return 0;
190 1.1.2.2 nathanw }
191 1.1.2.2 nathanw
192 1.1.2.2 nathanw
193 1.1.2.2 nathanw #define _NOTMATCH 0
194 1.1.2.2 nathanw #define _MATCH 1
195 1.1.2.2 nathanw #define _PARSEFAIL 2
196 1.1.2.2 nathanw
197 1.1.2.2 nathanw static __inline int
198 1.1.2.2 nathanw get_recommend(_ISO2022EncodingInfo * __restrict ei,
199 1.1.2.2 nathanw const char * __restrict token)
200 1.1.2.2 nathanw {
201 1.1.2.2 nathanw int i;
202 1.1.2.2 nathanw _ISO2022Charset cs;
203 1.1.2.2 nathanw
204 1.1.2.2 nathanw if (!strchr("0123", token[0]) || token[1] != '=')
205 1.1.2.2 nathanw return (_NOTMATCH);
206 1.1.2.2 nathanw
207 1.1.2.2 nathanw if (getcs(&token[2], &cs) == 0)
208 1.1.2.2 nathanw ;
209 1.1.2.2 nathanw else if (!strcmp(&token[2], "94")) {
210 1.1.2.2 nathanw cs.final = (u_char)(token[4]);
211 1.1.2.2 nathanw cs.interm = '\0';
212 1.1.2.2 nathanw cs.vers = '\0';
213 1.1.2.2 nathanw cs.type = CS94;
214 1.1.2.2 nathanw } else if (!strcmp(&token[2], "96")) {
215 1.1.2.2 nathanw cs.final = (u_char)(token[4]);
216 1.1.2.2 nathanw cs.interm = '\0';
217 1.1.2.2 nathanw cs.vers = '\0';
218 1.1.2.2 nathanw cs.type = CS96;
219 1.1.2.2 nathanw } else if (!strcmp(&token[2], "94$")) {
220 1.1.2.2 nathanw cs.final = (u_char)(token[5]);
221 1.1.2.2 nathanw cs.interm = '\0';
222 1.1.2.2 nathanw cs.vers = '\0';
223 1.1.2.2 nathanw cs.type = CS94MULTI;
224 1.1.2.2 nathanw } else if (!strcmp(&token[2], "96$")) {
225 1.1.2.2 nathanw cs.final = (u_char)(token[5]);
226 1.1.2.2 nathanw cs.interm = '\0';
227 1.1.2.2 nathanw cs.vers = '\0';
228 1.1.2.2 nathanw cs.type = CS96MULTI;
229 1.1.2.2 nathanw } else {
230 1.1.2.2 nathanw return (_PARSEFAIL);
231 1.1.2.2 nathanw }
232 1.1.2.2 nathanw
233 1.1.2.2 nathanw i = token[0] - '0';
234 1.1.2.2 nathanw ei->recommendsize[i] += 1;
235 1.1.2.2 nathanw if (!ei->recommend[i]) {
236 1.1.2.2 nathanw ei->recommend[i] = malloc(sizeof(_ISO2022Charset));
237 1.1.2.2 nathanw } else {
238 1.1.2.2 nathanw ei->recommend[i] =
239 1.1.2.2 nathanw realloc(ei->recommend[i],
240 1.1.2.2 nathanw sizeof(_ISO2022Charset)* (ei->recommendsize[i]));
241 1.1.2.2 nathanw }
242 1.1.2.2 nathanw if (!ei->recommend[i])
243 1.1.2.2 nathanw return (_PARSEFAIL);
244 1.1.2.2 nathanw
245 1.1.2.2 nathanw (ei->recommend[i] + (ei->recommendsize[i] - 1))->final = cs.final;
246 1.1.2.2 nathanw (ei->recommend[i] + (ei->recommendsize[i] - 1))->interm = cs.interm;
247 1.1.2.2 nathanw (ei->recommend[i] + (ei->recommendsize[i] - 1))->vers = cs.vers;
248 1.1.2.2 nathanw (ei->recommend[i] + (ei->recommendsize[i] - 1))->type = cs.type;
249 1.1.2.2 nathanw
250 1.1.2.2 nathanw return (_MATCH);
251 1.1.2.2 nathanw }
252 1.1.2.2 nathanw
253 1.1.2.2 nathanw static __inline int
254 1.1.2.2 nathanw get_initg(_ISO2022EncodingInfo * __restrict ei,
255 1.1.2.2 nathanw const char * __restrict token)
256 1.1.2.2 nathanw {
257 1.1.2.2 nathanw _ISO2022Charset cs;
258 1.1.2.2 nathanw
259 1.1.2.2 nathanw if (strncmp("INIT", &token[0], 4) ||
260 1.1.2.2 nathanw !strchr("0123", token[4]) ||
261 1.1.2.2 nathanw token[5] != '=')
262 1.1.2.2 nathanw return (_NOTMATCH);
263 1.1.2.2 nathanw
264 1.1.2.2 nathanw if (getcs(&token[6], &cs) != 0)
265 1.1.2.2 nathanw return (_PARSEFAIL);
266 1.1.2.2 nathanw
267 1.1.2.2 nathanw ei->initg[token[4] - '0'].type = cs.type;
268 1.1.2.2 nathanw ei->initg[token[4] - '0'].final = cs.final;
269 1.1.2.2 nathanw ei->initg[token[4] - '0'].interm = cs.interm;
270 1.1.2.2 nathanw ei->initg[token[4] - '0'].vers = cs.vers;
271 1.1.2.2 nathanw
272 1.1.2.2 nathanw return (_MATCH);
273 1.1.2.2 nathanw }
274 1.1.2.2 nathanw
275 1.1.2.2 nathanw static __inline int
276 1.1.2.2 nathanw get_max(_ISO2022EncodingInfo * __restrict ei,
277 1.1.2.2 nathanw const char * __restrict token)
278 1.1.2.2 nathanw {
279 1.1.2.2 nathanw if (!strcmp(token, "MAX1")) {
280 1.1.2.2 nathanw ei->maxcharset = 1;
281 1.1.2.2 nathanw } else if (!strcmp(token, "MAX2")) {
282 1.1.2.2 nathanw ei->maxcharset = 2;
283 1.1.2.2 nathanw } else if (!strcmp(token, "MAX3")) {
284 1.1.2.2 nathanw ei->maxcharset = 3;
285 1.1.2.2 nathanw } else
286 1.1.2.2 nathanw return (_NOTMATCH);
287 1.1.2.2 nathanw
288 1.1.2.2 nathanw return (_MATCH);
289 1.1.2.2 nathanw }
290 1.1.2.2 nathanw
291 1.1.2.2 nathanw
292 1.1.2.2 nathanw static __inline int
293 1.1.2.2 nathanw get_flags(_ISO2022EncodingInfo * __restrict ei,
294 1.1.2.2 nathanw const char * __restrict token)
295 1.1.2.2 nathanw {
296 1.1.2.2 nathanw int i;
297 1.1.2.2 nathanw static struct {
298 1.1.2.2 nathanw const char *tag;
299 1.1.2.2 nathanw int flag;
300 1.1.2.2 nathanw } const tags[] = {
301 1.1.2.2 nathanw { "DUMMY", 0 },
302 1.1.2.2 nathanw { "8BIT", F_8BIT },
303 1.1.2.2 nathanw { "NOOLD", F_NOOLD },
304 1.1.2.2 nathanw { "SI", F_SI },
305 1.1.2.2 nathanw { "SO", F_SO },
306 1.1.2.2 nathanw { "LS0", F_LS0 },
307 1.1.2.2 nathanw { "LS1", F_LS1 },
308 1.1.2.2 nathanw { "LS2", F_LS2 },
309 1.1.2.2 nathanw { "LS3", F_LS3 },
310 1.1.2.2 nathanw { "LS1R", F_LS1R },
311 1.1.2.2 nathanw { "LS2R", F_LS2R },
312 1.1.2.2 nathanw { "LS3R", F_LS3R },
313 1.1.2.2 nathanw { "SS2", F_SS2 },
314 1.1.2.2 nathanw { "SS3", F_SS3 },
315 1.1.2.2 nathanw { "SS2R", F_SS2R },
316 1.1.2.2 nathanw { "SS3R", F_SS3R },
317 1.1.2.2 nathanw { NULL, 0 }
318 1.1.2.2 nathanw };
319 1.1.2.2 nathanw
320 1.1.2.2 nathanw for (i = 0; tags[i].tag; i++) {
321 1.1.2.2 nathanw if (!strcmp(token, tags[i].tag)) {
322 1.1.2.2 nathanw ei->flags |= tags[i].flag;
323 1.1.2.2 nathanw return (_MATCH);
324 1.1.2.2 nathanw }
325 1.1.2.2 nathanw }
326 1.1.2.2 nathanw
327 1.1.2.2 nathanw return (_NOTMATCH);
328 1.1.2.2 nathanw }
329 1.1.2.2 nathanw
330 1.1.2.2 nathanw
331 1.1.2.2 nathanw static __inline int
332 1.1.2.2 nathanw _citrus_ISO2022_parse_variable(_ISO2022EncodingInfo * __restrict ei,
333 1.1.2.2 nathanw const void * __restrict var, size_t lenvar)
334 1.1.2.2 nathanw {
335 1.1.2.2 nathanw char const *v, *e;
336 1.1.2.2 nathanw char buf[20];
337 1.1.2.2 nathanw int i, len, ret;
338 1.1.2.2 nathanw
339 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
340 1.1.2.2 nathanw
341 1.1.2.2 nathanw
342 1.1.2.2 nathanw /*
343 1.1.2.2 nathanw * parse VARIABLE section.
344 1.1.2.2 nathanw */
345 1.1.2.2 nathanw
346 1.1.2.2 nathanw if (!var)
347 1.1.2.2 nathanw return (EFTYPE);
348 1.1.2.2 nathanw
349 1.1.2.2 nathanw v = (const char *) var;
350 1.1.2.2 nathanw
351 1.1.2.2 nathanw /* initialize structure */
352 1.1.2.2 nathanw ei->maxcharset = 0;
353 1.1.2.2 nathanw for (i = 0; i < 4; i++) {
354 1.1.2.2 nathanw ei->recommend[i] = NULL;
355 1.1.2.2 nathanw ei->recommendsize[i] = 0;
356 1.1.2.2 nathanw }
357 1.1.2.2 nathanw ei->flags = 0;
358 1.1.2.2 nathanw
359 1.1.2.2 nathanw while (*v) {
360 1.1.2.2 nathanw while (*v == ' ' || *v == '\t')
361 1.1.2.2 nathanw ++v;
362 1.1.2.2 nathanw
363 1.1.2.2 nathanw /* find the token */
364 1.1.2.2 nathanw e = v;
365 1.1.2.2 nathanw while (*e && *e != ' ' && *e != '\t')
366 1.1.2.2 nathanw ++e;
367 1.1.2.2 nathanw if (*e) {
368 1.1.2.2 nathanw len = e-v;
369 1.1.2.2 nathanw if (len>=sizeof(buf))
370 1.1.2.2 nathanw goto parsefail;
371 1.1.2.2 nathanw sprintf(buf, "%.*s", len, v);
372 1.1.2.2 nathanw ++e;
373 1.1.2.2 nathanw }
374 1.1.2.2 nathanw
375 1.1.2.2 nathanw if ((ret = get_recommend(ei, buf)) != _NOTMATCH)
376 1.1.2.2 nathanw ;
377 1.1.2.2 nathanw else if ((ret = get_initg(ei, buf)) != _NOTMATCH)
378 1.1.2.2 nathanw ;
379 1.1.2.2 nathanw else if ((ret = get_max(ei, buf)) != _NOTMATCH)
380 1.1.2.2 nathanw ;
381 1.1.2.2 nathanw else if ((ret = get_flags(ei, buf)) != _NOTMATCH)
382 1.1.2.2 nathanw ;
383 1.1.2.2 nathanw else
384 1.1.2.2 nathanw ret = _PARSEFAIL;
385 1.1.2.2 nathanw if (ret==_PARSEFAIL)
386 1.1.2.2 nathanw goto parsefail;
387 1.1.2.2 nathanw v = e;
388 1.1.2.2 nathanw
389 1.1.2.2 nathanw }
390 1.1.2.2 nathanw
391 1.1.2.2 nathanw return (0);
392 1.1.2.2 nathanw
393 1.1.2.2 nathanw parsefail:
394 1.1.2.2 nathanw free(ei->recommend[0]);
395 1.1.2.2 nathanw free(ei->recommend[1]);
396 1.1.2.2 nathanw free(ei->recommend[2]);
397 1.1.2.2 nathanw free(ei->recommend[3]);
398 1.1.2.2 nathanw
399 1.1.2.2 nathanw return (EFTYPE);
400 1.1.2.2 nathanw }
401 1.1.2.2 nathanw
402 1.1.2.2 nathanw static __inline void
403 1.1.2.2 nathanw /*ARGSUSED*/
404 1.1.2.2 nathanw _citrus_ISO2022_init_state(_ISO2022EncodingInfo * __restrict ei,
405 1.1.2.2 nathanw _ISO2022State * __restrict s)
406 1.1.2.2 nathanw {
407 1.1.2.2 nathanw int i;
408 1.1.2.2 nathanw
409 1.1.2.2 nathanw memset(s, 0, sizeof(*s));
410 1.1.2.2 nathanw s->gl = 0;
411 1.1.2.2 nathanw s->gr = (ei->flags & F_8BIT) ? 1 : -1;
412 1.1.2.2 nathanw
413 1.1.2.2 nathanw for (i = 0; i < 4; i++) {
414 1.1.2.2 nathanw if (ei->initg[i].final) {
415 1.1.2.2 nathanw s->g[i].type = ei->initg[i].type;
416 1.1.2.2 nathanw s->g[i].final = ei->initg[i].final;
417 1.1.2.2 nathanw s->g[i].interm = ei->initg[i].interm;
418 1.1.2.2 nathanw }
419 1.1.2.2 nathanw }
420 1.1.2.2 nathanw s->singlegl = s->singlegr = -1;
421 1.1.2.2 nathanw }
422 1.1.2.2 nathanw
423 1.1.2.2 nathanw static __inline void
424 1.1.2.2 nathanw /*ARGSUSED*/
425 1.1.2.2 nathanw _citrus_ISO2022_pack_state(_ISO2022EncodingInfo * __restrict ei,
426 1.1.2.2 nathanw void * __restrict pspriv,
427 1.1.2.2 nathanw const _ISO2022State * __restrict s)
428 1.1.2.2 nathanw {
429 1.1.2.2 nathanw memcpy(pspriv, (const void *)s, sizeof(*s));
430 1.1.2.2 nathanw }
431 1.1.2.2 nathanw
432 1.1.2.2 nathanw static __inline void
433 1.1.2.2 nathanw /*ARGSUSED*/
434 1.1.2.2 nathanw _citrus_ISO2022_unpack_state(_ISO2022EncodingInfo * __restrict ei,
435 1.1.2.2 nathanw _ISO2022State * __restrict s,
436 1.1.2.2 nathanw const void * __restrict pspriv)
437 1.1.2.2 nathanw {
438 1.1.2.2 nathanw memcpy((void *)s, pspriv, sizeof(*s));
439 1.1.2.2 nathanw }
440 1.1.2.2 nathanw
441 1.1.2.2 nathanw static int
442 1.1.2.2 nathanw /*ARGSUSED*/
443 1.1.2.2 nathanw _citrus_ISO2022_stdencoding_init(_ISO2022EncodingInfo * __restrict ei,
444 1.1.2.2 nathanw const void * __restrict var, size_t lenvar)
445 1.1.2.2 nathanw {
446 1.1.2.2 nathanw
447 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
448 1.1.2.2 nathanw
449 1.1.2.2 nathanw return _citrus_ISO2022_parse_variable(ei, var, lenvar);
450 1.1.2.2 nathanw }
451 1.1.2.2 nathanw
452 1.1.2.2 nathanw static void
453 1.1.2.2 nathanw /*ARGSUSED*/
454 1.1.2.2 nathanw _citrus_ISO2022_stdencoding_uninit(_ISO2022EncodingInfo *ei)
455 1.1.2.2 nathanw {
456 1.1.2.2 nathanw }
457 1.1.2.2 nathanw
458 1.1.2.2 nathanw #define ESC '\033'
459 1.1.2.2 nathanw #define ECMA -1
460 1.1.2.2 nathanw #define INTERM -2
461 1.1.2.2 nathanw #define OECMA -3
462 1.1.2.2 nathanw static struct seqtable {
463 1.1.2.2 nathanw int type;
464 1.1.2.2 nathanw int csoff;
465 1.1.2.2 nathanw int finaloff;
466 1.1.2.2 nathanw int intermoff;
467 1.1.2.2 nathanw int versoff;
468 1.1.2.2 nathanw int len;
469 1.1.2.2 nathanw int chars[10];
470 1.1.2.2 nathanw } seqtable[] = {
471 1.1.2.2 nathanw /* G0 94MULTI special */
472 1.1.2.2 nathanw { CS94MULTI, -1, 2, -1, -1, 3, { ESC, '$', OECMA }, },
473 1.1.2.2 nathanw /* G0 94MULTI special with version identification */
474 1.1.2.2 nathanw { CS94MULTI, -1, 5, -1, 2, 6, { ESC, '&', ECMA, ESC, '$', OECMA }, },
475 1.1.2.2 nathanw /* G? 94 */
476 1.1.2.2 nathanw { CS94, 1, 2, -1, -1, 3, { ESC, CS94, ECMA, }, },
477 1.1.2.2 nathanw /* G? 94 with 2nd intermediate char */
478 1.1.2.2 nathanw { CS94, 1, 3, 2, -1, 4, { ESC, CS94, INTERM, ECMA, }, },
479 1.1.2.2 nathanw /* G? 96 */
480 1.1.2.2 nathanw { CS96, 1, 2, -1, -1, 3, { ESC, CS96, ECMA, }, },
481 1.1.2.2 nathanw /* G? 96 with 2nd intermediate char */
482 1.1.2.2 nathanw { CS96, 1, 3, 2, -1, 4, { ESC, CS96, INTERM, ECMA, }, },
483 1.1.2.2 nathanw /* G? 94MULTI */
484 1.1.2.2 nathanw { CS94MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS94, ECMA, }, },
485 1.1.2.2 nathanw /* G? 96MULTI */
486 1.1.2.2 nathanw { CS96MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS96, ECMA, }, },
487 1.1.2.2 nathanw /* G? 94MULTI with version specification */
488 1.1.2.2 nathanw { CS94MULTI, 5, 6, -1, 2, 7, { ESC, '&', ECMA, ESC, '$', CS94, ECMA, }, },
489 1.1.2.2 nathanw /* LS2/3 */
490 1.1.2.2 nathanw { -1, -1, -1, -1, -1, 2, { ESC, 'n', }, },
491 1.1.2.2 nathanw { -1, -1, -1, -1, -1, 2, { ESC, 'o', }, },
492 1.1.2.2 nathanw /* LS1/2/3R */
493 1.1.2.2 nathanw { -1, -1, -1, -1, -1, 2, { ESC, '~', }, },
494 1.1.2.2 nathanw { -1, -1, -1, -1, -1, 2, { ESC, /*{*/ '}', }, },
495 1.1.2.2 nathanw { -1, -1, -1, -1, -1, 2, { ESC, '|', }, },
496 1.1.2.2 nathanw /* SS2/3 */
497 1.1.2.2 nathanw { -1, -1, -1, -1, -1, 2, { ESC, 'N', }, },
498 1.1.2.2 nathanw { -1, -1, -1, -1, -1, 2, { ESC, 'O', }, },
499 1.1.2.2 nathanw /* end of records */
500 1.1.2.2 nathanw { 0, }
501 1.1.2.2 nathanw };
502 1.1.2.2 nathanw
503 1.1.2.2 nathanw static int
504 1.1.2.2 nathanw seqmatch(const char * __restrict s, size_t n,
505 1.1.2.2 nathanw const struct seqtable * __restrict sp)
506 1.1.2.2 nathanw {
507 1.1.2.2 nathanw const int *p;
508 1.1.2.2 nathanw
509 1.1.2.2 nathanw _DIAGASSERT(s != NULL);
510 1.1.2.2 nathanw _DIAGASSERT(sp != NULL);
511 1.1.2.2 nathanw
512 1.1.2.2 nathanw p = sp->chars;
513 1.1.2.2 nathanw while (p - sp->chars < n && p - sp->chars < sp->len) {
514 1.1.2.2 nathanw switch (*p) {
515 1.1.2.2 nathanw case ECMA:
516 1.1.2.2 nathanw if (!isecma(*s))
517 1.1.2.2 nathanw goto terminate;
518 1.1.2.2 nathanw break;
519 1.1.2.2 nathanw case OECMA:
520 1.1.2.2 nathanw if (*s && strchr("@AB", *s))
521 1.1.2.2 nathanw break;
522 1.1.2.2 nathanw else
523 1.1.2.2 nathanw goto terminate;
524 1.1.2.2 nathanw case INTERM:
525 1.1.2.2 nathanw if (!isinterm(*s))
526 1.1.2.2 nathanw goto terminate;
527 1.1.2.2 nathanw break;
528 1.1.2.2 nathanw case CS94:
529 1.1.2.2 nathanw if (*s && strchr("()*+", *s))
530 1.1.2.2 nathanw break;
531 1.1.2.2 nathanw else
532 1.1.2.2 nathanw goto terminate;
533 1.1.2.2 nathanw case CS96:
534 1.1.2.2 nathanw if (*s && strchr(",-./", *s))
535 1.1.2.2 nathanw break;
536 1.1.2.2 nathanw else
537 1.1.2.2 nathanw goto terminate;
538 1.1.2.2 nathanw default:
539 1.1.2.2 nathanw if (*s != *p)
540 1.1.2.2 nathanw goto terminate;
541 1.1.2.2 nathanw break;
542 1.1.2.2 nathanw }
543 1.1.2.2 nathanw
544 1.1.2.2 nathanw p++;
545 1.1.2.2 nathanw s++;
546 1.1.2.2 nathanw }
547 1.1.2.2 nathanw
548 1.1.2.2 nathanw terminate:
549 1.1.2.2 nathanw return p - sp->chars;
550 1.1.2.2 nathanw }
551 1.1.2.2 nathanw
552 1.1.2.2 nathanw static wchar_t
553 1.1.2.2 nathanw _ISO2022_sgetwchar(_ISO2022EncodingInfo * __restrict ei,
554 1.1.2.2 nathanw const char * __restrict string, size_t n,
555 1.1.2.2 nathanw const char ** __restrict result,
556 1.1.2.2 nathanw _ISO2022State * __restrict psenc)
557 1.1.2.2 nathanw {
558 1.1.2.2 nathanw wchar_t wchar = 0;
559 1.1.2.2 nathanw int cur;
560 1.1.2.2 nathanw struct seqtable *sp;
561 1.1.2.2 nathanw int nmatch;
562 1.1.2.2 nathanw int i;
563 1.1.2.2 nathanw
564 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
565 1.1.2.2 nathanw _DIAGASSERT(state != NULL);
566 1.1.2.2 nathanw _DIAGASSERT(string != NULL);
567 1.1.2.2 nathanw /* result may be NULL */
568 1.1.2.2 nathanw
569 1.1.2.2 nathanw while (1) {
570 1.1.2.2 nathanw /* SI/SO */
571 1.1.2.2 nathanw if (1 <= n && string[0] == '\017') {
572 1.1.2.2 nathanw psenc->gl = 0;
573 1.1.2.2 nathanw string++;
574 1.1.2.2 nathanw n--;
575 1.1.2.2 nathanw continue;
576 1.1.2.2 nathanw }
577 1.1.2.2 nathanw if (1 <= n && string[0] == '\016') {
578 1.1.2.2 nathanw psenc->gl = 1;
579 1.1.2.2 nathanw string++;
580 1.1.2.2 nathanw n--;
581 1.1.2.2 nathanw continue;
582 1.1.2.2 nathanw }
583 1.1.2.2 nathanw
584 1.1.2.2 nathanw /* SS2/3R */
585 1.1.2.2 nathanw if (1 <= n && string[0] && strchr("\217\216", string[0])) {
586 1.1.2.2 nathanw psenc->singlegl = psenc->singlegr =
587 1.1.2.2 nathanw (string[0] - '\216') + 2;
588 1.1.2.2 nathanw string++;
589 1.1.2.2 nathanw n--;
590 1.1.2.2 nathanw continue;
591 1.1.2.2 nathanw }
592 1.1.2.2 nathanw
593 1.1.2.2 nathanw /* eat the letter if this is not ESC */
594 1.1.2.2 nathanw if (1 <= n && string[0] != '\033')
595 1.1.2.2 nathanw break;
596 1.1.2.2 nathanw
597 1.1.2.2 nathanw /* look for a perfect match from escape sequences */
598 1.1.2.2 nathanw for (sp = &seqtable[0]; sp->len; sp++) {
599 1.1.2.2 nathanw nmatch = seqmatch(string, n, sp);
600 1.1.2.2 nathanw if (sp->len == nmatch && n >= sp->len)
601 1.1.2.2 nathanw break;
602 1.1.2.2 nathanw }
603 1.1.2.2 nathanw
604 1.1.2.2 nathanw if (!sp->len)
605 1.1.2.2 nathanw goto notseq;
606 1.1.2.2 nathanw
607 1.1.2.2 nathanw if (sp->type != -1) {
608 1.1.2.2 nathanw if (sp->csoff == -1)
609 1.1.2.2 nathanw i = 0;
610 1.1.2.2 nathanw else {
611 1.1.2.2 nathanw switch (sp->type) {
612 1.1.2.2 nathanw case CS94:
613 1.1.2.2 nathanw case CS94MULTI:
614 1.1.2.2 nathanw i = string[sp->csoff] - '(';
615 1.1.2.2 nathanw break;
616 1.1.2.2 nathanw case CS96:
617 1.1.2.2 nathanw case CS96MULTI:
618 1.1.2.2 nathanw i = string[sp->csoff] - ',';
619 1.1.2.2 nathanw break;
620 1.1.2.2 nathanw }
621 1.1.2.2 nathanw }
622 1.1.2.2 nathanw psenc->g[i].type = sp->type;
623 1.1.2.2 nathanw psenc->g[i].final = '\0';
624 1.1.2.2 nathanw psenc->g[i].interm = '\0';
625 1.1.2.2 nathanw psenc->g[i].vers = '\0';
626 1.1.2.2 nathanw /* sp->finaloff must not be -1 */
627 1.1.2.2 nathanw if (sp->finaloff != -1)
628 1.1.2.2 nathanw psenc->g[i].final = string[sp->finaloff];
629 1.1.2.2 nathanw if (sp->intermoff != -1)
630 1.1.2.2 nathanw psenc->g[i].interm = string[sp->intermoff];
631 1.1.2.2 nathanw if (sp->versoff != -1)
632 1.1.2.2 nathanw psenc->g[i].vers = string[sp->versoff];
633 1.1.2.2 nathanw
634 1.1.2.2 nathanw string += sp->len;
635 1.1.2.2 nathanw n -= sp->len;
636 1.1.2.2 nathanw continue;
637 1.1.2.2 nathanw }
638 1.1.2.2 nathanw
639 1.1.2.2 nathanw /* LS2/3 */
640 1.1.2.2 nathanw if (2 <= n && string[0] == '\033'
641 1.1.2.2 nathanw && string[1] && strchr("no", string[1])) {
642 1.1.2.2 nathanw psenc->gl = string[1] - 'n' + 2;
643 1.1.2.2 nathanw string += 2;
644 1.1.2.2 nathanw n -= 2;
645 1.1.2.2 nathanw continue;
646 1.1.2.2 nathanw }
647 1.1.2.2 nathanw
648 1.1.2.2 nathanw /* LS1/2/3R */
649 1.1.2.2 nathanw /* XXX: { for vi showmatch */
650 1.1.2.2 nathanw if (2 <= n && string[0] == '\033'
651 1.1.2.2 nathanw && string[1] && strchr("~}|", string[1])) {
652 1.1.2.2 nathanw psenc->gr = 3 - (string[1] - '|');
653 1.1.2.2 nathanw string += 2;
654 1.1.2.2 nathanw n -= 2;
655 1.1.2.2 nathanw continue;
656 1.1.2.2 nathanw }
657 1.1.2.2 nathanw
658 1.1.2.2 nathanw /* SS2/3 */
659 1.1.2.2 nathanw if (2 <= n && string[0] == '\033'
660 1.1.2.2 nathanw && string[1] && strchr("NO", string[1])) {
661 1.1.2.2 nathanw psenc->singlegl = (string[1] - 'N') + 2;
662 1.1.2.2 nathanw string += 2;
663 1.1.2.2 nathanw n -= 2;
664 1.1.2.2 nathanw continue;
665 1.1.2.2 nathanw }
666 1.1.2.2 nathanw
667 1.1.2.2 nathanw notseq:
668 1.1.2.2 nathanw /*
669 1.1.2.2 nathanw * if we've got an unknown escape sequence, eat the ESC at the
670 1.1.2.2 nathanw * head. otherwise, wait till full escape sequence comes.
671 1.1.2.2 nathanw */
672 1.1.2.2 nathanw for (sp = &seqtable[0]; sp->len; sp++) {
673 1.1.2.2 nathanw nmatch = seqmatch(string, n, sp);
674 1.1.2.2 nathanw if (!nmatch)
675 1.1.2.2 nathanw continue;
676 1.1.2.2 nathanw
677 1.1.2.2 nathanw /*
678 1.1.2.2 nathanw * if we are in the middle of escape sequence,
679 1.1.2.2 nathanw * we still need to wait for more characters to come
680 1.1.2.2 nathanw */
681 1.1.2.2 nathanw if (n < sp->len) {
682 1.1.2.2 nathanw if (nmatch == n) {
683 1.1.2.2 nathanw if (result)
684 1.1.2.2 nathanw *result = string;
685 1.1.2.2 nathanw return (_ISO2022INVALID);
686 1.1.2.2 nathanw }
687 1.1.2.2 nathanw } else {
688 1.1.2.2 nathanw if (nmatch == sp->len) {
689 1.1.2.2 nathanw /* this case should not happen */
690 1.1.2.2 nathanw goto eat;
691 1.1.2.2 nathanw }
692 1.1.2.2 nathanw }
693 1.1.2.2 nathanw }
694 1.1.2.2 nathanw
695 1.1.2.2 nathanw break;
696 1.1.2.2 nathanw }
697 1.1.2.2 nathanw
698 1.1.2.2 nathanw eat:
699 1.1.2.2 nathanw /* no letter to eat */
700 1.1.2.2 nathanw if (n < 1) {
701 1.1.2.2 nathanw if (result)
702 1.1.2.2 nathanw *result = string;
703 1.1.2.2 nathanw return (_ISO2022INVALID);
704 1.1.2.2 nathanw }
705 1.1.2.2 nathanw
706 1.1.2.2 nathanw /* normal chars. always eat C0/C1 as is. */
707 1.1.2.2 nathanw if (iscntl(*string & 0xff))
708 1.1.2.2 nathanw cur = -1;
709 1.1.2.2 nathanw else if (*string & 0x80) {
710 1.1.2.2 nathanw cur = (psenc->singlegr == -1)
711 1.1.2.2 nathanw ? psenc->gr : psenc->singlegr;
712 1.1.2.2 nathanw } else {
713 1.1.2.2 nathanw cur = (psenc->singlegl == -1)
714 1.1.2.2 nathanw ? psenc->gl : psenc->singlegl;
715 1.1.2.2 nathanw }
716 1.1.2.2 nathanw
717 1.1.2.2 nathanw if (cur == -1) {
718 1.1.2.2 nathanw asis:
719 1.1.2.2 nathanw wchar = *string++ & 0xff;
720 1.1.2.2 nathanw if (result)
721 1.1.2.2 nathanw *result = string;
722 1.1.2.2 nathanw /* reset single shift state */
723 1.1.2.2 nathanw psenc->singlegr = psenc->singlegl = -1;
724 1.1.2.2 nathanw return wchar;
725 1.1.2.2 nathanw }
726 1.1.2.2 nathanw
727 1.1.2.2 nathanw /* length error check */
728 1.1.2.2 nathanw switch (psenc->g[cur].type) {
729 1.1.2.2 nathanw case CS94MULTI:
730 1.1.2.2 nathanw case CS96MULTI:
731 1.1.2.2 nathanw if (!isthree(psenc->g[cur].final)) {
732 1.1.2.2 nathanw if (2 <= n
733 1.1.2.2 nathanw && (string[0] & 0x80) == (string[1] & 0x80))
734 1.1.2.2 nathanw break;
735 1.1.2.2 nathanw } else {
736 1.1.2.2 nathanw if (3 <= n
737 1.1.2.2 nathanw && (string[0] & 0x80) == (string[1] & 0x80)
738 1.1.2.2 nathanw && (string[0] & 0x80) == (string[2] & 0x80))
739 1.1.2.2 nathanw break;
740 1.1.2.2 nathanw }
741 1.1.2.2 nathanw
742 1.1.2.2 nathanw /* we still need to wait for more characters to come */
743 1.1.2.2 nathanw if (result)
744 1.1.2.2 nathanw *result = string;
745 1.1.2.2 nathanw return (_ISO2022INVALID);
746 1.1.2.2 nathanw
747 1.1.2.2 nathanw case CS94:
748 1.1.2.2 nathanw case CS96:
749 1.1.2.2 nathanw if (1 <= n)
750 1.1.2.2 nathanw break;
751 1.1.2.2 nathanw
752 1.1.2.2 nathanw /* we still need to wait for more characters to come */
753 1.1.2.2 nathanw if (result)
754 1.1.2.2 nathanw *result = string;
755 1.1.2.2 nathanw return (_ISO2022INVALID);
756 1.1.2.2 nathanw }
757 1.1.2.2 nathanw
758 1.1.2.2 nathanw /* range check */
759 1.1.2.2 nathanw switch (psenc->g[cur].type) {
760 1.1.2.2 nathanw case CS94:
761 1.1.2.2 nathanw if (!(is94(string[0] & 0x7f)))
762 1.1.2.2 nathanw goto asis;
763 1.1.2.2 nathanw case CS96:
764 1.1.2.2 nathanw if (!(is96(string[0] & 0x7f)))
765 1.1.2.2 nathanw goto asis;
766 1.1.2.2 nathanw break;
767 1.1.2.2 nathanw case CS94MULTI:
768 1.1.2.2 nathanw if (!(is94(string[0] & 0x7f) && is94(string[1] & 0x7f)))
769 1.1.2.2 nathanw goto asis;
770 1.1.2.2 nathanw break;
771 1.1.2.2 nathanw case CS96MULTI:
772 1.1.2.2 nathanw if (!(is96(string[0] & 0x7f) && is96(string[1] & 0x7f)))
773 1.1.2.2 nathanw goto asis;
774 1.1.2.2 nathanw break;
775 1.1.2.2 nathanw }
776 1.1.2.2 nathanw
777 1.1.2.2 nathanw /* extract the character. */
778 1.1.2.2 nathanw switch (psenc->g[cur].type) {
779 1.1.2.2 nathanw case CS94:
780 1.1.2.2 nathanw /* special case for ASCII. */
781 1.1.2.2 nathanw if (psenc->g[cur].final == 'B' && !psenc->g[cur].interm) {
782 1.1.2.2 nathanw wchar = *string++;
783 1.1.2.2 nathanw wchar &= 0x7f;
784 1.1.2.2 nathanw break;
785 1.1.2.2 nathanw }
786 1.1.2.2 nathanw wchar = psenc->g[cur].final;
787 1.1.2.2 nathanw wchar = (wchar << 8);
788 1.1.2.2 nathanw wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
789 1.1.2.2 nathanw wchar = (wchar << 8);
790 1.1.2.2 nathanw wchar = (wchar << 8) | (*string++ & 0x7f);
791 1.1.2.2 nathanw break;
792 1.1.2.2 nathanw case CS96:
793 1.1.2.2 nathanw /* special case for ISO-8859-1. */
794 1.1.2.2 nathanw if (psenc->g[cur].final == 'A' && !psenc->g[cur].interm) {
795 1.1.2.2 nathanw wchar = *string++;
796 1.1.2.2 nathanw wchar &= 0x7f;
797 1.1.2.2 nathanw wchar |= 0x80;
798 1.1.2.2 nathanw break;
799 1.1.2.2 nathanw }
800 1.1.2.2 nathanw wchar = psenc->g[cur].final;
801 1.1.2.2 nathanw wchar = (wchar << 8);
802 1.1.2.2 nathanw wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
803 1.1.2.2 nathanw wchar = (wchar << 8);
804 1.1.2.2 nathanw wchar = (wchar << 8) | (*string++ & 0x7f);
805 1.1.2.2 nathanw wchar |= 0x80;
806 1.1.2.2 nathanw break;
807 1.1.2.2 nathanw case CS94MULTI:
808 1.1.2.2 nathanw case CS96MULTI:
809 1.1.2.2 nathanw wchar = psenc->g[cur].final;
810 1.1.2.2 nathanw wchar = (wchar << 8);
811 1.1.2.2 nathanw if (isthree(psenc->g[cur].final))
812 1.1.2.2 nathanw wchar |= (*string++ & 0x7f);
813 1.1.2.2 nathanw wchar = (wchar << 8) | (*string++ & 0x7f);
814 1.1.2.2 nathanw wchar = (wchar << 8) | (*string++ & 0x7f);
815 1.1.2.2 nathanw if (psenc->g[cur].type == CS96MULTI)
816 1.1.2.2 nathanw wchar |= 0x80;
817 1.1.2.2 nathanw break;
818 1.1.2.2 nathanw }
819 1.1.2.2 nathanw
820 1.1.2.2 nathanw if (result)
821 1.1.2.2 nathanw *result = string;
822 1.1.2.2 nathanw /* reset single shift state */
823 1.1.2.2 nathanw psenc->singlegr = psenc->singlegl = -1;
824 1.1.2.2 nathanw return wchar;
825 1.1.2.2 nathanw }
826 1.1.2.2 nathanw
827 1.1.2.2 nathanw
828 1.1.2.2 nathanw
829 1.1.2.2 nathanw static int
830 1.1.2.2 nathanw _citrus_ISO2022_mbrtowc_priv(_ISO2022EncodingInfo * __restrict ei,
831 1.1.2.2 nathanw wchar_t * __restrict pwc,
832 1.1.2.2 nathanw const char ** __restrict s,
833 1.1.2.2 nathanw size_t n, _ISO2022State * __restrict psenc,
834 1.1.2.2 nathanw size_t * __restrict nresult)
835 1.1.2.2 nathanw {
836 1.1.2.2 nathanw wchar_t wchar;
837 1.1.2.2 nathanw const char *s0, *p, *result;
838 1.1.2.2 nathanw int c;
839 1.1.2.2 nathanw int chlenbak;
840 1.1.2.2 nathanw
841 1.1.2.2 nathanw _DIAGASSERT(nresult != 0);
842 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
843 1.1.2.2 nathanw _DIAGASSERT(psenc != NULL);
844 1.1.2.2 nathanw _DIAGASSERT(s != NULL);
845 1.1.2.2 nathanw
846 1.1.2.2 nathanw s0 = *s;
847 1.1.2.2 nathanw c = 0;
848 1.1.2.2 nathanw chlenbak = psenc->chlen;
849 1.1.2.2 nathanw
850 1.1.2.2 nathanw /*
851 1.1.2.2 nathanw * if we have something in buffer, use that.
852 1.1.2.2 nathanw * otherwise, skip here
853 1.1.2.2 nathanw */
854 1.1.2.2 nathanw if (psenc->chlen < 0 || psenc->chlen > sizeof(psenc->ch)) {
855 1.1.2.2 nathanw /* illgeal state */
856 1.1.2.2 nathanw _citrus_ISO2022_init_state(ei, psenc);
857 1.1.2.2 nathanw goto encoding_error;
858 1.1.2.2 nathanw }
859 1.1.2.2 nathanw if (psenc->chlen == 0)
860 1.1.2.2 nathanw goto emptybuf;
861 1.1.2.2 nathanw
862 1.1.2.2 nathanw /* buffer is not empty */
863 1.1.2.2 nathanw p = psenc->ch;
864 1.1.2.2 nathanw while (psenc->chlen < sizeof(psenc->ch) && n >= 0) {
865 1.1.2.2 nathanw if (n > 0) {
866 1.1.2.2 nathanw psenc->ch[psenc->chlen++] = *s0++;
867 1.1.2.2 nathanw n--;
868 1.1.2.2 nathanw }
869 1.1.2.2 nathanw
870 1.1.2.2 nathanw wchar = _ISO2022_sgetwchar(ei, p, psenc->chlen - (p-psenc->ch),
871 1.1.2.2 nathanw &result, psenc);
872 1.1.2.2 nathanw if (wchar != _ISO2022INVALID) {
873 1.1.2.2 nathanw c += result - p;
874 1.1.2.2 nathanw if (psenc->chlen > c)
875 1.1.2.2 nathanw memmove(psenc->ch, result, psenc->chlen - c);
876 1.1.2.2 nathanw if (psenc->chlen < c)
877 1.1.2.2 nathanw psenc->chlen = 0;
878 1.1.2.2 nathanw else
879 1.1.2.2 nathanw psenc->chlen -= c;
880 1.1.2.2 nathanw goto output;
881 1.1.2.2 nathanw }
882 1.1.2.2 nathanw
883 1.1.2.2 nathanw c += result - p;
884 1.1.2.2 nathanw p = result;
885 1.1.2.2 nathanw
886 1.1.2.2 nathanw if (n == 0)
887 1.1.2.2 nathanw goto restart;
888 1.1.2.2 nathanw }
889 1.1.2.2 nathanw
890 1.1.2.2 nathanw /* escape sequence too long? */
891 1.1.2.2 nathanw goto encoding_error;
892 1.1.2.2 nathanw
893 1.1.2.2 nathanw emptybuf:
894 1.1.2.2 nathanw wchar = _ISO2022_sgetwchar(ei, s0, n, &result, psenc);
895 1.1.2.2 nathanw if (wchar != _ISO2022INVALID) {
896 1.1.2.2 nathanw c += result - s0;
897 1.1.2.2 nathanw psenc->chlen = 0;
898 1.1.2.2 nathanw s0 = result;
899 1.1.2.2 nathanw goto output;
900 1.1.2.2 nathanw }
901 1.1.2.2 nathanw if (result > s0 && n > result - s0) {
902 1.1.2.2 nathanw c += (result - s0);
903 1.1.2.2 nathanw n -= (result - s0);
904 1.1.2.2 nathanw s0 = result;
905 1.1.2.2 nathanw goto emptybuf;
906 1.1.2.2 nathanw }
907 1.1.2.2 nathanw n += c;
908 1.1.2.2 nathanw if (n < sizeof(psenc->ch)) {
909 1.1.2.2 nathanw memcpy(psenc->ch, s0 - c, n);
910 1.1.2.2 nathanw psenc->chlen = n;
911 1.1.2.2 nathanw s0 = result;
912 1.1.2.2 nathanw goto restart;
913 1.1.2.2 nathanw }
914 1.1.2.2 nathanw
915 1.1.2.2 nathanw /* escape sequence too long? */
916 1.1.2.2 nathanw
917 1.1.2.2 nathanw encoding_error:
918 1.1.2.2 nathanw psenc->chlen = 0;
919 1.1.2.2 nathanw *nresult = (size_t)-1;
920 1.1.2.2 nathanw return (EILSEQ);
921 1.1.2.2 nathanw
922 1.1.2.2 nathanw output:
923 1.1.2.2 nathanw *s = s0;
924 1.1.2.2 nathanw if (pwc)
925 1.1.2.2 nathanw *pwc = wchar;
926 1.1.2.2 nathanw
927 1.1.2.2 nathanw if (!wchar)
928 1.1.2.2 nathanw *nresult = 0;
929 1.1.2.2 nathanw else
930 1.1.2.2 nathanw *nresult = c - chlenbak;
931 1.1.2.2 nathanw
932 1.1.2.2 nathanw return (0);
933 1.1.2.2 nathanw
934 1.1.2.2 nathanw restart:
935 1.1.2.2 nathanw *s = s0;
936 1.1.2.2 nathanw *nresult = (size_t)-2;
937 1.1.2.2 nathanw
938 1.1.2.2 nathanw return (0);
939 1.1.2.2 nathanw }
940 1.1.2.2 nathanw
941 1.1.2.2 nathanw static int
942 1.1.2.2 nathanw recommendation(_ISO2022EncodingInfo * __restrict ei,
943 1.1.2.2 nathanw _ISO2022Charset * __restrict cs)
944 1.1.2.2 nathanw {
945 1.1.2.2 nathanw int i, j;
946 1.1.2.2 nathanw _ISO2022Charset *recommend;
947 1.1.2.2 nathanw
948 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
949 1.1.2.2 nathanw _DIAGASSERT(cs != NULL);
950 1.1.2.2 nathanw
951 1.1.2.2 nathanw /* first, try a exact match. */
952 1.1.2.2 nathanw for (i = 0; i < 4; i++) {
953 1.1.2.2 nathanw recommend = ei->recommend[i];
954 1.1.2.2 nathanw for (j = 0; j < ei->recommendsize[i]; j++) {
955 1.1.2.2 nathanw if (cs->type != recommend[j].type)
956 1.1.2.2 nathanw continue;
957 1.1.2.2 nathanw if (cs->final != recommend[j].final)
958 1.1.2.2 nathanw continue;
959 1.1.2.2 nathanw if (cs->interm != recommend[j].interm)
960 1.1.2.2 nathanw continue;
961 1.1.2.2 nathanw
962 1.1.2.2 nathanw return i;
963 1.1.2.2 nathanw }
964 1.1.2.2 nathanw }
965 1.1.2.2 nathanw
966 1.1.2.2 nathanw /* then, try a wildcard match over final char. */
967 1.1.2.2 nathanw for (i = 0; i < 4; i++) {
968 1.1.2.2 nathanw recommend = ei->recommend[i];
969 1.1.2.2 nathanw for (j = 0; j < ei->recommendsize[i]; j++) {
970 1.1.2.2 nathanw if (cs->type != recommend[j].type)
971 1.1.2.2 nathanw continue;
972 1.1.2.2 nathanw if (cs->final && (cs->final != recommend[j].final))
973 1.1.2.2 nathanw continue;
974 1.1.2.2 nathanw if (cs->interm && (cs->interm != recommend[j].interm))
975 1.1.2.2 nathanw continue;
976 1.1.2.2 nathanw
977 1.1.2.2 nathanw return i;
978 1.1.2.2 nathanw }
979 1.1.2.2 nathanw }
980 1.1.2.2 nathanw
981 1.1.2.2 nathanw /* there's no recommendation. make a guess. */
982 1.1.2.2 nathanw if (ei->maxcharset == 0) {
983 1.1.2.2 nathanw return 0;
984 1.1.2.2 nathanw } else {
985 1.1.2.2 nathanw switch (cs->type) {
986 1.1.2.2 nathanw case CS94:
987 1.1.2.2 nathanw case CS94MULTI:
988 1.1.2.2 nathanw return 0;
989 1.1.2.2 nathanw case CS96:
990 1.1.2.2 nathanw case CS96MULTI:
991 1.1.2.2 nathanw return 1;
992 1.1.2.2 nathanw }
993 1.1.2.2 nathanw }
994 1.1.2.2 nathanw return 0;
995 1.1.2.2 nathanw }
996 1.1.2.2 nathanw
997 1.1.2.2 nathanw static int
998 1.1.2.2 nathanw _ISO2022_sputwchar(_ISO2022EncodingInfo * __restrict ei, wchar_t c,
999 1.1.2.2 nathanw char * __restrict string, size_t n,
1000 1.1.2.2 nathanw char ** __restrict result,
1001 1.1.2.2 nathanw _ISO2022State * __restrict psenc)
1002 1.1.2.2 nathanw {
1003 1.1.2.2 nathanw int i = 0, len;
1004 1.1.2.2 nathanw _ISO2022Charset cs;
1005 1.1.2.2 nathanw char *p;
1006 1.1.2.2 nathanw char tmp[MB_LEN_MAX];
1007 1.1.2.2 nathanw int target;
1008 1.1.2.2 nathanw u_char mask;
1009 1.1.2.2 nathanw int bit8;
1010 1.1.2.2 nathanw
1011 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
1012 1.1.2.2 nathanw _DIAGASSERT(string != NULL);
1013 1.1.2.2 nathanw /* result may be NULL */
1014 1.1.2.2 nathanw /* state appears to be unused */
1015 1.1.2.2 nathanw
1016 1.1.2.2 nathanw if (iscntl(c & 0xff)) {
1017 1.1.2.2 nathanw /* go back to ASCII on control chars */
1018 1.1.2.2 nathanw cs.type = CS94;
1019 1.1.2.2 nathanw cs.final = 'B';
1020 1.1.2.2 nathanw cs.interm = '\0';
1021 1.1.2.2 nathanw } else if (!(c & ~0xff)) {
1022 1.1.2.2 nathanw if (c & 0x80) {
1023 1.1.2.2 nathanw /* special treatment for ISO-8859-1 */
1024 1.1.2.2 nathanw cs.type = CS96;
1025 1.1.2.2 nathanw cs.final = 'A';
1026 1.1.2.2 nathanw cs.interm = '\0';
1027 1.1.2.2 nathanw } else {
1028 1.1.2.2 nathanw /* special treatment for ASCII */
1029 1.1.2.2 nathanw cs.type = CS94;
1030 1.1.2.2 nathanw cs.final = 'B';
1031 1.1.2.2 nathanw cs.interm = '\0';
1032 1.1.2.2 nathanw }
1033 1.1.2.2 nathanw } else {
1034 1.1.2.2 nathanw cs.final = (c >> 24) & 0x7f;
1035 1.1.2.2 nathanw if ((c >> 16) & 0x80)
1036 1.1.2.2 nathanw cs.interm = (c >> 16) & 0x7f;
1037 1.1.2.2 nathanw else
1038 1.1.2.2 nathanw cs.interm = '\0';
1039 1.1.2.2 nathanw if (c & 0x80)
1040 1.1.2.2 nathanw cs.type = (c & 0x00007f00) ? CS96MULTI : CS96;
1041 1.1.2.2 nathanw else
1042 1.1.2.2 nathanw cs.type = (c & 0x00007f00) ? CS94MULTI : CS94;
1043 1.1.2.2 nathanw }
1044 1.1.2.2 nathanw target = recommendation(ei, &cs);
1045 1.1.2.2 nathanw p = tmp;
1046 1.1.2.2 nathanw bit8 = ei->flags & F_8BIT;
1047 1.1.2.2 nathanw
1048 1.1.2.2 nathanw /* designate the charset onto the target plane(G0/1/2/3). */
1049 1.1.2.2 nathanw if (psenc->g[target].type == cs.type
1050 1.1.2.2 nathanw && psenc->g[target].final == cs.final
1051 1.1.2.2 nathanw && psenc->g[target].interm == cs.interm)
1052 1.1.2.2 nathanw goto planeok;
1053 1.1.2.2 nathanw
1054 1.1.2.2 nathanw *p++ = '\033';
1055 1.1.2.2 nathanw if (cs.type == CS94MULTI || cs.type == CS96MULTI)
1056 1.1.2.2 nathanw *p++ = '$';
1057 1.1.2.2 nathanw if (target == 0 && cs.type == CS94MULTI && strchr("@AB", cs.final)
1058 1.1.2.2 nathanw && !cs.interm && !(ei->flags & F_NOOLD))
1059 1.1.2.2 nathanw ;
1060 1.1.2.2 nathanw else if (cs.type == CS94 || cs.type == CS94MULTI)
1061 1.1.2.2 nathanw *p++ = "()*+"[target];
1062 1.1.2.2 nathanw else
1063 1.1.2.2 nathanw *p++ = ",-./"[target];
1064 1.1.2.2 nathanw if (cs.interm)
1065 1.1.2.2 nathanw *p++ = cs.interm;
1066 1.1.2.2 nathanw *p++ = cs.final;
1067 1.1.2.2 nathanw
1068 1.1.2.2 nathanw psenc->g[target].type = cs.type;
1069 1.1.2.2 nathanw psenc->g[target].final = cs.final;
1070 1.1.2.2 nathanw psenc->g[target].interm = cs.interm;
1071 1.1.2.2 nathanw
1072 1.1.2.2 nathanw planeok:
1073 1.1.2.2 nathanw
1074 1.1.2.2 nathanw /* invoke the plane onto GL or GR. */
1075 1.1.2.2 nathanw if (psenc->gl == target)
1076 1.1.2.2 nathanw goto sideok;
1077 1.1.2.2 nathanw if (bit8 && psenc->gr == target)
1078 1.1.2.2 nathanw goto sideok;
1079 1.1.2.2 nathanw
1080 1.1.2.2 nathanw if (target == 0 && (ei->flags & F_LS0)) {
1081 1.1.2.2 nathanw *p++ = '\017';
1082 1.1.2.2 nathanw psenc->gl = 0;
1083 1.1.2.2 nathanw } else if (target == 1 && (ei->flags & F_LS1)) {
1084 1.1.2.2 nathanw *p++ = '\016';
1085 1.1.2.2 nathanw psenc->gl = 1;
1086 1.1.2.2 nathanw } else if (target == 2 && (ei->flags & F_LS2)) {
1087 1.1.2.2 nathanw *p++ = '\033';
1088 1.1.2.2 nathanw *p++ = 'n';
1089 1.1.2.2 nathanw psenc->gl = 2;
1090 1.1.2.2 nathanw } else if (target == 3 && (ei->flags & F_LS3)) {
1091 1.1.2.2 nathanw *p++ = '\033';
1092 1.1.2.2 nathanw *p++ = 'o';
1093 1.1.2.2 nathanw psenc->gl = 3;
1094 1.1.2.2 nathanw } else if (bit8 && target == 1 && (ei->flags & F_LS1R)) {
1095 1.1.2.2 nathanw *p++ = '\033';
1096 1.1.2.2 nathanw *p++ = '~';
1097 1.1.2.2 nathanw psenc->gr = 1;
1098 1.1.2.2 nathanw } else if (bit8 && target == 2 && (ei->flags & F_LS2R)) {
1099 1.1.2.2 nathanw *p++ = '\033';
1100 1.1.2.2 nathanw /*{*/
1101 1.1.2.2 nathanw *p++ = '}';
1102 1.1.2.2 nathanw psenc->gr = 2;
1103 1.1.2.2 nathanw } else if (bit8 && target == 3 && (ei->flags & F_LS3R)) {
1104 1.1.2.2 nathanw *p++ = '\033';
1105 1.1.2.2 nathanw *p++ = '|';
1106 1.1.2.2 nathanw psenc->gr = 3;
1107 1.1.2.2 nathanw } else if (target == 2 && (ei->flags & F_SS2)) {
1108 1.1.2.2 nathanw *p++ = '\033';
1109 1.1.2.2 nathanw *p++ = 'N';
1110 1.1.2.2 nathanw psenc->singlegl = 2;
1111 1.1.2.2 nathanw } else if (target == 3 && (ei->flags & F_SS3)) {
1112 1.1.2.2 nathanw *p++ = '\033';
1113 1.1.2.2 nathanw *p++ = 'O';
1114 1.1.2.2 nathanw psenc->singlegl = 3;
1115 1.1.2.2 nathanw } else if (bit8 && target == 2 && (ei->flags & F_SS2R)) {
1116 1.1.2.2 nathanw *p++ = '\216';
1117 1.1.2.2 nathanw *p++ = 'N';
1118 1.1.2.2 nathanw psenc->singlegl = psenc->singlegr = 2;
1119 1.1.2.2 nathanw } else if (bit8 && target == 3 && (ei->flags & F_SS3R)) {
1120 1.1.2.2 nathanw *p++ = '\217';
1121 1.1.2.2 nathanw *p++ = 'O';
1122 1.1.2.2 nathanw psenc->singlegl = psenc->singlegr = 3;
1123 1.1.2.2 nathanw } else
1124 1.1.2.2 nathanw abort();
1125 1.1.2.2 nathanw
1126 1.1.2.2 nathanw sideok:
1127 1.1.2.2 nathanw if (psenc->singlegl == target)
1128 1.1.2.2 nathanw mask = 0x00;
1129 1.1.2.2 nathanw else if (psenc->singlegr == target)
1130 1.1.2.2 nathanw mask = 0x80;
1131 1.1.2.2 nathanw else if (psenc->gl == target)
1132 1.1.2.2 nathanw mask = 0x00;
1133 1.1.2.2 nathanw else if ((ei->flags & F_8BIT) && psenc->gr == target)
1134 1.1.2.2 nathanw mask = 0x80;
1135 1.1.2.2 nathanw else
1136 1.1.2.2 nathanw abort();
1137 1.1.2.2 nathanw
1138 1.1.2.2 nathanw switch (cs.type) {
1139 1.1.2.2 nathanw case CS94:
1140 1.1.2.2 nathanw case CS96:
1141 1.1.2.2 nathanw i = 1;
1142 1.1.2.2 nathanw break;
1143 1.1.2.2 nathanw case CS94MULTI:
1144 1.1.2.2 nathanw case CS96MULTI:
1145 1.1.2.2 nathanw i = isthree(cs.final) ? 3 : 2;
1146 1.1.2.2 nathanw break;
1147 1.1.2.2 nathanw }
1148 1.1.2.2 nathanw while (i-- > 0)
1149 1.1.2.2 nathanw *p++ = ((c >> (i << 3)) & 0x7f) | mask;
1150 1.1.2.2 nathanw
1151 1.1.2.2 nathanw /* reset single shift state */
1152 1.1.2.2 nathanw psenc->singlegl = psenc->singlegr = -1;
1153 1.1.2.2 nathanw
1154 1.1.2.2 nathanw len = p - tmp;
1155 1.1.2.2 nathanw if (n < len) {
1156 1.1.2.2 nathanw if (result)
1157 1.1.2.2 nathanw *result = (char *)0;
1158 1.1.2.2 nathanw } else {
1159 1.1.2.2 nathanw if (result)
1160 1.1.2.2 nathanw *result = string + len;
1161 1.1.2.2 nathanw memcpy(string, tmp, len);
1162 1.1.2.2 nathanw }
1163 1.1.2.2 nathanw return len;
1164 1.1.2.2 nathanw }
1165 1.1.2.2 nathanw
1166 1.1.2.2 nathanw static int
1167 1.1.2.2 nathanw _citrus_ISO2022_wcrtomb_priv(_ISO2022EncodingInfo * __restrict ei,
1168 1.1.2.2 nathanw char * __restrict s, size_t n, wchar_t wc,
1169 1.1.2.2 nathanw _ISO2022State * __restrict psenc,
1170 1.1.2.2 nathanw size_t * __restrict nresult)
1171 1.1.2.2 nathanw {
1172 1.1.2.2 nathanw char buf[MB_LEN_MAX];
1173 1.1.2.2 nathanw char *result;
1174 1.1.2.2 nathanw int len;
1175 1.1.2.2 nathanw
1176 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
1177 1.1.2.2 nathanw _DIAGASSERT(nresult != 0);
1178 1.1.2.2 nathanw _DIAGASSERT(s != NULL);
1179 1.1.2.2 nathanw
1180 1.1.2.2 nathanw /* XXX state will be modified after this operation... */
1181 1.1.2.2 nathanw len = _ISO2022_sputwchar(ei, wc, buf, sizeof(buf), &result, psenc);
1182 1.1.2.2 nathanw if (sizeof(buf) < len || n < len) {
1183 1.1.2.2 nathanw /* XXX should recover state? */
1184 1.1.2.2 nathanw goto ilseq;
1185 1.1.2.2 nathanw }
1186 1.1.2.2 nathanw
1187 1.1.2.2 nathanw memcpy(s, buf, len);
1188 1.1.2.2 nathanw *nresult = (size_t)len;
1189 1.1.2.2 nathanw return (0);
1190 1.1.2.2 nathanw
1191 1.1.2.2 nathanw ilseq:
1192 1.1.2.2 nathanw /* bound check failure */
1193 1.1.2.2 nathanw *nresult = (size_t)-1;
1194 1.1.2.2 nathanw return (EILSEQ);
1195 1.1.2.2 nathanw }
1196 1.1.2.2 nathanw
1197 1.1.2.2 nathanw /* ----------------------------------------------------------------------
1198 1.1.2.2 nathanw * public interface for ctype
1199 1.1.2.2 nathanw */
1200 1.1.2.2 nathanw
1201 1.1.2.2 nathanw _CITRUS_CTYPE_DECLS(ISO2022);
1202 1.1.2.2 nathanw _CITRUS_CTYPE_DEF_OPS(ISO2022);
1203 1.1.2.2 nathanw
1204 1.1.2.2 nathanw #include "citrus_ctype_template.h"
1205