unvis.c revision 1.45 1 1.45 rillig /* $NetBSD: unvis.c,v 1.45 2022/04/19 20:32:15 rillig Exp $ */
2 1.4 cgd
3 1.1 cgd /*-
4 1.4 cgd * Copyright (c) 1989, 1993
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.24 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 christos #include <sys/cdefs.h>
33 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
34 1.4 cgd #if 0
35 1.4 cgd static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
36 1.4 cgd #else
37 1.45 rillig __RCSID("$NetBSD: unvis.c,v 1.45 2022/04/19 20:32:15 rillig Exp $");
38 1.4 cgd #endif
39 1.1 cgd #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.7 jtc #include "namespace.h"
42 1.1 cgd #include <sys/types.h>
43 1.15 lukem
44 1.15 lukem #include <assert.h>
45 1.1 cgd #include <ctype.h>
46 1.33 christos #include <stdint.h>
47 1.15 lukem #include <stdio.h>
48 1.34 christos #include <errno.h>
49 1.1 cgd #include <vis.h>
50 1.7 jtc
51 1.7 jtc #ifdef __weak_alias
52 1.34 christos __weak_alias(strnunvisx,_strnunvisx)
53 1.21 tv #endif
54 1.1 cgd
55 1.23 pooka #if !HAVE_VIS
56 1.1 cgd /*
57 1.1 cgd * decode driven by state machine
58 1.1 cgd */
59 1.1 cgd #define S_GROUND 0 /* haven't seen escape char */
60 1.1 cgd #define S_START 1 /* start decoding special sequence */
61 1.1 cgd #define S_META 2 /* metachar started (M) */
62 1.1 cgd #define S_META1 3 /* metachar more, regular char (-) */
63 1.1 cgd #define S_CTRL 4 /* control char started (^) */
64 1.1 cgd #define S_OCTAL2 5 /* octal digit 2 */
65 1.1 cgd #define S_OCTAL3 6 /* octal digit 3 */
66 1.37 christos #define S_HEX 7 /* mandatory hex digit */
67 1.37 christos #define S_HEX1 8 /* http hex digit */
68 1.37 christos #define S_HEX2 9 /* http hex digit 2 */
69 1.37 christos #define S_MIME1 10 /* mime hex digit 1 */
70 1.37 christos #define S_MIME2 11 /* mime hex digit 2 */
71 1.37 christos #define S_EATCRNL 12 /* mime eating CRNL */
72 1.37 christos #define S_AMP 13 /* seen & */
73 1.37 christos #define S_NUMBER 14 /* collecting number */
74 1.37 christos #define S_STRING 15 /* collecting string */
75 1.1 cgd
76 1.1 cgd #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
77 1.37 christos #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
78 1.37 christos #define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
79 1.9 thorpej
80 1.1 cgd /*
81 1.31 christos * RFC 1866
82 1.31 christos */
83 1.31 christos static const struct nv {
84 1.41 matt char name[7];
85 1.31 christos uint8_t value;
86 1.31 christos } nv[] = {
87 1.31 christos { "AElig", 198 }, /* capital AE diphthong (ligature) */
88 1.31 christos { "Aacute", 193 }, /* capital A, acute accent */
89 1.31 christos { "Acirc", 194 }, /* capital A, circumflex accent */
90 1.31 christos { "Agrave", 192 }, /* capital A, grave accent */
91 1.31 christos { "Aring", 197 }, /* capital A, ring */
92 1.31 christos { "Atilde", 195 }, /* capital A, tilde */
93 1.31 christos { "Auml", 196 }, /* capital A, dieresis or umlaut mark */
94 1.31 christos { "Ccedil", 199 }, /* capital C, cedilla */
95 1.31 christos { "ETH", 208 }, /* capital Eth, Icelandic */
96 1.31 christos { "Eacute", 201 }, /* capital E, acute accent */
97 1.31 christos { "Ecirc", 202 }, /* capital E, circumflex accent */
98 1.31 christos { "Egrave", 200 }, /* capital E, grave accent */
99 1.31 christos { "Euml", 203 }, /* capital E, dieresis or umlaut mark */
100 1.31 christos { "Iacute", 205 }, /* capital I, acute accent */
101 1.31 christos { "Icirc", 206 }, /* capital I, circumflex accent */
102 1.31 christos { "Igrave", 204 }, /* capital I, grave accent */
103 1.31 christos { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */
104 1.31 christos { "Ntilde", 209 }, /* capital N, tilde */
105 1.31 christos { "Oacute", 211 }, /* capital O, acute accent */
106 1.31 christos { "Ocirc", 212 }, /* capital O, circumflex accent */
107 1.31 christos { "Ograve", 210 }, /* capital O, grave accent */
108 1.31 christos { "Oslash", 216 }, /* capital O, slash */
109 1.31 christos { "Otilde", 213 }, /* capital O, tilde */
110 1.31 christos { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */
111 1.31 christos { "THORN", 222 }, /* capital THORN, Icelandic */
112 1.31 christos { "Uacute", 218 }, /* capital U, acute accent */
113 1.31 christos { "Ucirc", 219 }, /* capital U, circumflex accent */
114 1.31 christos { "Ugrave", 217 }, /* capital U, grave accent */
115 1.31 christos { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */
116 1.31 christos { "Yacute", 221 }, /* capital Y, acute accent */
117 1.31 christos { "aacute", 225 }, /* small a, acute accent */
118 1.31 christos { "acirc", 226 }, /* small a, circumflex accent */
119 1.31 christos { "acute", 180 }, /* acute accent */
120 1.31 christos { "aelig", 230 }, /* small ae diphthong (ligature) */
121 1.31 christos { "agrave", 224 }, /* small a, grave accent */
122 1.31 christos { "amp", 38 }, /* ampersand */
123 1.31 christos { "aring", 229 }, /* small a, ring */
124 1.31 christos { "atilde", 227 }, /* small a, tilde */
125 1.31 christos { "auml", 228 }, /* small a, dieresis or umlaut mark */
126 1.31 christos { "brvbar", 166 }, /* broken (vertical) bar */
127 1.31 christos { "ccedil", 231 }, /* small c, cedilla */
128 1.31 christos { "cedil", 184 }, /* cedilla */
129 1.31 christos { "cent", 162 }, /* cent sign */
130 1.31 christos { "copy", 169 }, /* copyright sign */
131 1.31 christos { "curren", 164 }, /* general currency sign */
132 1.31 christos { "deg", 176 }, /* degree sign */
133 1.31 christos { "divide", 247 }, /* divide sign */
134 1.31 christos { "eacute", 233 }, /* small e, acute accent */
135 1.31 christos { "ecirc", 234 }, /* small e, circumflex accent */
136 1.31 christos { "egrave", 232 }, /* small e, grave accent */
137 1.31 christos { "eth", 240 }, /* small eth, Icelandic */
138 1.31 christos { "euml", 235 }, /* small e, dieresis or umlaut mark */
139 1.31 christos { "frac12", 189 }, /* fraction one-half */
140 1.31 christos { "frac14", 188 }, /* fraction one-quarter */
141 1.31 christos { "frac34", 190 }, /* fraction three-quarters */
142 1.31 christos { "gt", 62 }, /* greater than */
143 1.31 christos { "iacute", 237 }, /* small i, acute accent */
144 1.31 christos { "icirc", 238 }, /* small i, circumflex accent */
145 1.31 christos { "iexcl", 161 }, /* inverted exclamation mark */
146 1.31 christos { "igrave", 236 }, /* small i, grave accent */
147 1.31 christos { "iquest", 191 }, /* inverted question mark */
148 1.31 christos { "iuml", 239 }, /* small i, dieresis or umlaut mark */
149 1.31 christos { "laquo", 171 }, /* angle quotation mark, left */
150 1.31 christos { "lt", 60 }, /* less than */
151 1.31 christos { "macr", 175 }, /* macron */
152 1.31 christos { "micro", 181 }, /* micro sign */
153 1.31 christos { "middot", 183 }, /* middle dot */
154 1.31 christos { "nbsp", 160 }, /* no-break space */
155 1.31 christos { "not", 172 }, /* not sign */
156 1.31 christos { "ntilde", 241 }, /* small n, tilde */
157 1.31 christos { "oacute", 243 }, /* small o, acute accent */
158 1.31 christos { "ocirc", 244 }, /* small o, circumflex accent */
159 1.31 christos { "ograve", 242 }, /* small o, grave accent */
160 1.31 christos { "ordf", 170 }, /* ordinal indicator, feminine */
161 1.31 christos { "ordm", 186 }, /* ordinal indicator, masculine */
162 1.31 christos { "oslash", 248 }, /* small o, slash */
163 1.31 christos { "otilde", 245 }, /* small o, tilde */
164 1.31 christos { "ouml", 246 }, /* small o, dieresis or umlaut mark */
165 1.31 christos { "para", 182 }, /* pilcrow (paragraph sign) */
166 1.31 christos { "plusmn", 177 }, /* plus-or-minus sign */
167 1.31 christos { "pound", 163 }, /* pound sterling sign */
168 1.31 christos { "quot", 34 }, /* double quote */
169 1.31 christos { "raquo", 187 }, /* angle quotation mark, right */
170 1.31 christos { "reg", 174 }, /* registered sign */
171 1.31 christos { "sect", 167 }, /* section sign */
172 1.31 christos { "shy", 173 }, /* soft hyphen */
173 1.31 christos { "sup1", 185 }, /* superscript one */
174 1.31 christos { "sup2", 178 }, /* superscript two */
175 1.31 christos { "sup3", 179 }, /* superscript three */
176 1.31 christos { "szlig", 223 }, /* small sharp s, German (sz ligature) */
177 1.31 christos { "thorn", 254 }, /* small thorn, Icelandic */
178 1.31 christos { "times", 215 }, /* multiply sign */
179 1.31 christos { "uacute", 250 }, /* small u, acute accent */
180 1.31 christos { "ucirc", 251 }, /* small u, circumflex accent */
181 1.31 christos { "ugrave", 249 }, /* small u, grave accent */
182 1.31 christos { "uml", 168 }, /* umlaut (dieresis) */
183 1.31 christos { "uuml", 252 }, /* small u, dieresis or umlaut mark */
184 1.31 christos { "yacute", 253 }, /* small y, acute accent */
185 1.31 christos { "yen", 165 }, /* yen sign */
186 1.31 christos { "yuml", 255 }, /* small y, dieresis or umlaut mark */
187 1.31 christos };
188 1.31 christos
189 1.31 christos /*
190 1.1 cgd * unvis - decode characters previously encoded by vis
191 1.1 cgd */
192 1.1 cgd int
193 1.29 christos unvis(char *cp, int c, int *astate, int flag)
194 1.1 cgd {
195 1.25 rillig unsigned char uc = (unsigned char)c;
196 1.31 christos unsigned char st, ia, is, lc;
197 1.31 christos
198 1.31 christos /*
199 1.31 christos * Bottom 8 bits of astate hold the state machine state.
200 1.31 christos * Top 8 bits hold the current character in the http 1866 nv string decoding
201 1.31 christos */
202 1.31 christos #define GS(a) ((a) & 0xff)
203 1.32 christos #define SS(a, b) (((uint32_t)(a) << 24) | (b))
204 1.32 christos #define GI(a) ((uint32_t)(a) >> 24)
205 1.1 cgd
206 1.15 lukem _DIAGASSERT(cp != NULL);
207 1.15 lukem _DIAGASSERT(astate != NULL);
208 1.31 christos st = GS(*astate);
209 1.15 lukem
210 1.1 cgd if (flag & UNVIS_END) {
211 1.31 christos switch (st) {
212 1.31 christos case S_OCTAL2:
213 1.31 christos case S_OCTAL3:
214 1.31 christos case S_HEX2:
215 1.31 christos *astate = SS(0, S_GROUND);
216 1.29 christos return UNVIS_VALID;
217 1.31 christos case S_GROUND:
218 1.31 christos return UNVIS_NOCHAR;
219 1.31 christos default:
220 1.31 christos return UNVIS_SYNBAD;
221 1.27 lukem }
222 1.1 cgd }
223 1.1 cgd
224 1.31 christos switch (st) {
225 1.1 cgd
226 1.1 cgd case S_GROUND:
227 1.1 cgd *cp = 0;
228 1.31 christos if ((flag & VIS_NOESCAPE) == 0 && c == '\\') {
229 1.31 christos *astate = SS(0, S_START);
230 1.29 christos return UNVIS_NOCHAR;
231 1.27 lukem }
232 1.31 christos if ((flag & VIS_HTTP1808) && c == '%') {
233 1.31 christos *astate = SS(0, S_HEX1);
234 1.31 christos return UNVIS_NOCHAR;
235 1.31 christos }
236 1.31 christos if ((flag & VIS_HTTP1866) && c == '&') {
237 1.31 christos *astate = SS(0, S_AMP);
238 1.29 christos return UNVIS_NOCHAR;
239 1.29 christos }
240 1.29 christos if ((flag & VIS_MIMESTYLE) && c == '=') {
241 1.31 christos *astate = SS(0, S_MIME1);
242 1.29 christos return UNVIS_NOCHAR;
243 1.22 christos }
244 1.1 cgd *cp = c;
245 1.29 christos return UNVIS_VALID;
246 1.1 cgd
247 1.1 cgd case S_START:
248 1.1 cgd switch(c) {
249 1.1 cgd case '\\':
250 1.1 cgd *cp = c;
251 1.31 christos *astate = SS(0, S_GROUND);
252 1.29 christos return UNVIS_VALID;
253 1.1 cgd case '0': case '1': case '2': case '3':
254 1.1 cgd case '4': case '5': case '6': case '7':
255 1.1 cgd *cp = (c - '0');
256 1.31 christos *astate = SS(0, S_OCTAL2);
257 1.29 christos return UNVIS_NOCHAR;
258 1.1 cgd case 'M':
259 1.13 christos *cp = (char)0200;
260 1.31 christos *astate = SS(0, S_META);
261 1.29 christos return UNVIS_NOCHAR;
262 1.1 cgd case '^':
263 1.31 christos *astate = SS(0, S_CTRL);
264 1.29 christos return UNVIS_NOCHAR;
265 1.1 cgd case 'n':
266 1.1 cgd *cp = '\n';
267 1.31 christos *astate = SS(0, S_GROUND);
268 1.29 christos return UNVIS_VALID;
269 1.1 cgd case 'r':
270 1.1 cgd *cp = '\r';
271 1.31 christos *astate = SS(0, S_GROUND);
272 1.29 christos return UNVIS_VALID;
273 1.1 cgd case 'b':
274 1.1 cgd *cp = '\b';
275 1.31 christos *astate = SS(0, S_GROUND);
276 1.29 christos return UNVIS_VALID;
277 1.1 cgd case 'a':
278 1.1 cgd *cp = '\007';
279 1.31 christos *astate = SS(0, S_GROUND);
280 1.29 christos return UNVIS_VALID;
281 1.1 cgd case 'v':
282 1.1 cgd *cp = '\v';
283 1.31 christos *astate = SS(0, S_GROUND);
284 1.29 christos return UNVIS_VALID;
285 1.1 cgd case 't':
286 1.1 cgd *cp = '\t';
287 1.31 christos *astate = SS(0, S_GROUND);
288 1.29 christos return UNVIS_VALID;
289 1.1 cgd case 'f':
290 1.1 cgd *cp = '\f';
291 1.31 christos *astate = SS(0, S_GROUND);
292 1.29 christos return UNVIS_VALID;
293 1.1 cgd case 's':
294 1.1 cgd *cp = ' ';
295 1.31 christos *astate = SS(0, S_GROUND);
296 1.29 christos return UNVIS_VALID;
297 1.1 cgd case 'E':
298 1.1 cgd *cp = '\033';
299 1.31 christos *astate = SS(0, S_GROUND);
300 1.29 christos return UNVIS_VALID;
301 1.37 christos case 'x':
302 1.37 christos *astate = SS(0, S_HEX);
303 1.37 christos return UNVIS_NOCHAR;
304 1.1 cgd case '\n':
305 1.1 cgd /*
306 1.1 cgd * hidden newline
307 1.1 cgd */
308 1.31 christos *astate = SS(0, S_GROUND);
309 1.31 christos return UNVIS_NOCHAR;
310 1.44 roy case '$':
311 1.44 roy /*
312 1.44 roy * hidden marker
313 1.44 roy */
314 1.44 roy *astate = SS(0, S_GROUND);
315 1.44 roy return UNVIS_NOCHAR;
316 1.42 roy default:
317 1.42 roy if (isgraph(c)) {
318 1.42 roy *cp = c;
319 1.42 roy *astate = SS(0, S_GROUND);
320 1.42 roy return UNVIS_VALID;
321 1.42 roy }
322 1.1 cgd }
323 1.31 christos goto bad;
324 1.27 lukem
325 1.1 cgd case S_META:
326 1.1 cgd if (c == '-')
327 1.31 christos *astate = SS(0, S_META1);
328 1.1 cgd else if (c == '^')
329 1.31 christos *astate = SS(0, S_CTRL);
330 1.31 christos else
331 1.31 christos goto bad;
332 1.29 christos return UNVIS_NOCHAR;
333 1.27 lukem
334 1.1 cgd case S_META1:
335 1.31 christos *astate = SS(0, S_GROUND);
336 1.1 cgd *cp |= c;
337 1.29 christos return UNVIS_VALID;
338 1.27 lukem
339 1.1 cgd case S_CTRL:
340 1.1 cgd if (c == '?')
341 1.1 cgd *cp |= 0177;
342 1.1 cgd else
343 1.1 cgd *cp |= c & 037;
344 1.31 christos *astate = SS(0, S_GROUND);
345 1.29 christos return UNVIS_VALID;
346 1.1 cgd
347 1.1 cgd case S_OCTAL2: /* second possible octal digit */
348 1.25 rillig if (isoctal(uc)) {
349 1.27 lukem /*
350 1.27 lukem * yes - and maybe a third
351 1.1 cgd */
352 1.1 cgd *cp = (*cp << 3) + (c - '0');
353 1.31 christos *astate = SS(0, S_OCTAL3);
354 1.29 christos return UNVIS_NOCHAR;
355 1.27 lukem }
356 1.27 lukem /*
357 1.27 lukem * no - done with current sequence, push back passed char
358 1.1 cgd */
359 1.31 christos *astate = SS(0, S_GROUND);
360 1.29 christos return UNVIS_VALIDPUSH;
361 1.1 cgd
362 1.1 cgd case S_OCTAL3: /* third possible octal digit */
363 1.31 christos *astate = SS(0, S_GROUND);
364 1.25 rillig if (isoctal(uc)) {
365 1.1 cgd *cp = (*cp << 3) + (c - '0');
366 1.29 christos return UNVIS_VALID;
367 1.1 cgd }
368 1.1 cgd /*
369 1.1 cgd * we were done, push back passed char
370 1.1 cgd */
371 1.29 christos return UNVIS_VALIDPUSH;
372 1.26 lukem
373 1.37 christos case S_HEX:
374 1.37 christos if (!isxdigit(uc))
375 1.37 christos goto bad;
376 1.37 christos /*FALLTHROUGH*/
377 1.22 christos case S_HEX1:
378 1.25 rillig if (isxdigit(uc)) {
379 1.25 rillig *cp = xtod(uc);
380 1.31 christos *astate = SS(0, S_HEX2);
381 1.29 christos return UNVIS_NOCHAR;
382 1.22 christos }
383 1.27 lukem /*
384 1.27 lukem * no - done with current sequence, push back passed char
385 1.22 christos */
386 1.31 christos *astate = SS(0, S_GROUND);
387 1.29 christos return UNVIS_VALIDPUSH;
388 1.26 lukem
389 1.22 christos case S_HEX2:
390 1.27 lukem *astate = S_GROUND;
391 1.27 lukem if (isxdigit(uc)) {
392 1.27 lukem *cp = xtod(uc) | (*cp << 4);
393 1.29 christos return UNVIS_VALID;
394 1.29 christos }
395 1.29 christos return UNVIS_VALIDPUSH;
396 1.29 christos
397 1.29 christos case S_MIME1:
398 1.29 christos if (uc == '\n' || uc == '\r') {
399 1.31 christos *astate = SS(0, S_EATCRNL);
400 1.29 christos return UNVIS_NOCHAR;
401 1.29 christos }
402 1.29 christos if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
403 1.29 christos *cp = XTOD(uc);
404 1.31 christos *astate = SS(0, S_MIME2);
405 1.29 christos return UNVIS_NOCHAR;
406 1.29 christos }
407 1.31 christos goto bad;
408 1.29 christos
409 1.29 christos case S_MIME2:
410 1.29 christos if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
411 1.31 christos *astate = SS(0, S_GROUND);
412 1.29 christos *cp = XTOD(uc) | (*cp << 4);
413 1.29 christos return UNVIS_VALID;
414 1.29 christos }
415 1.31 christos goto bad;
416 1.29 christos
417 1.29 christos case S_EATCRNL:
418 1.29 christos switch (uc) {
419 1.29 christos case '\r':
420 1.29 christos case '\n':
421 1.29 christos return UNVIS_NOCHAR;
422 1.29 christos case '=':
423 1.31 christos *astate = SS(0, S_MIME1);
424 1.29 christos return UNVIS_NOCHAR;
425 1.29 christos default:
426 1.29 christos *cp = uc;
427 1.31 christos *astate = SS(0, S_GROUND);
428 1.29 christos return UNVIS_VALID;
429 1.22 christos }
430 1.26 lukem
431 1.31 christos case S_AMP:
432 1.31 christos *cp = 0;
433 1.31 christos if (uc == '#') {
434 1.31 christos *astate = SS(0, S_NUMBER);
435 1.31 christos return UNVIS_NOCHAR;
436 1.31 christos }
437 1.31 christos *astate = SS(0, S_STRING);
438 1.31 christos /*FALLTHROUGH*/
439 1.31 christos
440 1.31 christos case S_STRING:
441 1.31 christos ia = *cp; /* index in the array */
442 1.31 christos is = GI(*astate); /* index in the string */
443 1.31 christos lc = is == 0 ? 0 : nv[ia].name[is - 1]; /* last character */
444 1.31 christos
445 1.31 christos if (uc == ';')
446 1.31 christos uc = '\0';
447 1.31 christos
448 1.31 christos for (; ia < __arraycount(nv); ia++) {
449 1.31 christos if (is != 0 && nv[ia].name[is - 1] != lc)
450 1.31 christos goto bad;
451 1.31 christos if (nv[ia].name[is] == uc)
452 1.31 christos break;
453 1.31 christos }
454 1.31 christos
455 1.36 martin if (ia == __arraycount(nv))
456 1.31 christos goto bad;
457 1.31 christos
458 1.31 christos if (uc != 0) {
459 1.31 christos *cp = ia;
460 1.31 christos *astate = SS(is + 1, S_STRING);
461 1.31 christos return UNVIS_NOCHAR;
462 1.31 christos }
463 1.31 christos
464 1.31 christos *cp = nv[ia].value;
465 1.31 christos *astate = SS(0, S_GROUND);
466 1.31 christos return UNVIS_VALID;
467 1.31 christos
468 1.31 christos case S_NUMBER:
469 1.31 christos if (uc == ';')
470 1.31 christos return UNVIS_VALID;
471 1.31 christos if (!isdigit(uc))
472 1.31 christos goto bad;
473 1.31 christos *cp += (*cp * 10) + uc - '0';
474 1.31 christos return UNVIS_NOCHAR;
475 1.31 christos
476 1.27 lukem default:
477 1.31 christos bad:
478 1.27 lukem /*
479 1.27 lukem * decoder in unknown state - (probably uninitialized)
480 1.1 cgd */
481 1.31 christos *astate = SS(0, S_GROUND);
482 1.29 christos return UNVIS_SYNBAD;
483 1.1 cgd }
484 1.1 cgd }
485 1.1 cgd
486 1.1 cgd /*
487 1.34 christos * strnunvisx - decode src into dst
488 1.1 cgd *
489 1.1 cgd * Number of chars decoded into dst is returned, -1 on error.
490 1.1 cgd * Dst is null terminated.
491 1.1 cgd */
492 1.1 cgd
493 1.1 cgd int
494 1.34 christos strnunvisx(char *dst, size_t dlen, const char *src, int flag)
495 1.1 cgd {
496 1.12 perry char c;
497 1.38 christos char t = '\0', *start = dst;
498 1.1 cgd int state = 0;
499 1.15 lukem
500 1.15 lukem _DIAGASSERT(src != NULL);
501 1.15 lukem _DIAGASSERT(dst != NULL);
502 1.34 christos #define CHECKSPACE() \
503 1.34 christos do { \
504 1.34 christos if (dlen-- == 0) { \
505 1.34 christos errno = ENOSPC; \
506 1.34 christos return -1; \
507 1.34 christos } \
508 1.45 rillig } while (0)
509 1.1 cgd
510 1.6 christos while ((c = *src++) != '\0') {
511 1.26 lukem again:
512 1.34 christos switch (unvis(&t, c, &state, flag)) {
513 1.1 cgd case UNVIS_VALID:
514 1.34 christos CHECKSPACE();
515 1.34 christos *dst++ = t;
516 1.1 cgd break;
517 1.1 cgd case UNVIS_VALIDPUSH:
518 1.34 christos CHECKSPACE();
519 1.34 christos *dst++ = t;
520 1.1 cgd goto again;
521 1.1 cgd case 0:
522 1.1 cgd case UNVIS_NOCHAR:
523 1.1 cgd break;
524 1.34 christos case UNVIS_SYNBAD:
525 1.34 christos errno = EINVAL;
526 1.34 christos return -1;
527 1.1 cgd default:
528 1.39 christos _DIAGASSERT(/*CONSTCOND*/0);
529 1.34 christos errno = EINVAL;
530 1.34 christos return -1;
531 1.1 cgd }
532 1.1 cgd }
533 1.34 christos if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
534 1.34 christos CHECKSPACE();
535 1.34 christos *dst++ = t;
536 1.34 christos }
537 1.34 christos CHECKSPACE();
538 1.1 cgd *dst = '\0';
539 1.29 christos return (int)(dst - start);
540 1.22 christos }
541 1.22 christos
542 1.22 christos int
543 1.34 christos strunvisx(char *dst, const char *src, int flag)
544 1.34 christos {
545 1.35 mrg return strnunvisx(dst, (size_t)~0, src, flag);
546 1.34 christos }
547 1.34 christos
548 1.34 christos int
549 1.31 christos strunvis(char *dst, const char *src)
550 1.22 christos {
551 1.35 mrg return strnunvisx(dst, (size_t)~0, src, 0);
552 1.34 christos }
553 1.34 christos
554 1.34 christos int
555 1.34 christos strnunvis(char *dst, size_t dlen, const char *src)
556 1.34 christos {
557 1.34 christos return strnunvisx(dst, dlen, src, 0);
558 1.1 cgd }
559 1.20 tv #endif
560