unvis.c revision 1.35 1 1.35 mrg /* $NetBSD: unvis.c,v 1.35 2011/03/13 07:40:44 mrg 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.35 mrg __RCSID("$NetBSD: unvis.c,v 1.35 2011/03/13 07:40:44 mrg 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.29 christos #define S_HEX1 7 /* http hex digit */
67 1.29 christos #define S_HEX2 8 /* http hex digit 2 */
68 1.29 christos #define S_MIME1 9 /* mime hex digit 1 */
69 1.29 christos #define S_MIME2 10 /* mime hex digit 2 */
70 1.29 christos #define S_EATCRNL 11 /* mime eating CRNL */
71 1.31 christos #define S_AMP 12 /* seen & */
72 1.31 christos #define S_NUMBER 13 /* collecting number */
73 1.31 christos #define S_STRING 14 /* collecting string */
74 1.1 cgd
75 1.1 cgd #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
76 1.22 christos #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
77 1.29 christos #define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
78 1.9 thorpej
79 1.1 cgd /*
80 1.31 christos * RFC 1866
81 1.31 christos */
82 1.31 christos static const struct nv {
83 1.31 christos const char *name;
84 1.31 christos uint8_t value;
85 1.31 christos } nv[] = {
86 1.31 christos { "AElig", 198 }, /* capital AE diphthong (ligature) */
87 1.31 christos { "Aacute", 193 }, /* capital A, acute accent */
88 1.31 christos { "Acirc", 194 }, /* capital A, circumflex accent */
89 1.31 christos { "Agrave", 192 }, /* capital A, grave accent */
90 1.31 christos { "Aring", 197 }, /* capital A, ring */
91 1.31 christos { "Atilde", 195 }, /* capital A, tilde */
92 1.31 christos { "Auml", 196 }, /* capital A, dieresis or umlaut mark */
93 1.31 christos { "Ccedil", 199 }, /* capital C, cedilla */
94 1.31 christos { "ETH", 208 }, /* capital Eth, Icelandic */
95 1.31 christos { "Eacute", 201 }, /* capital E, acute accent */
96 1.31 christos { "Ecirc", 202 }, /* capital E, circumflex accent */
97 1.31 christos { "Egrave", 200 }, /* capital E, grave accent */
98 1.31 christos { "Euml", 203 }, /* capital E, dieresis or umlaut mark */
99 1.31 christos { "Iacute", 205 }, /* capital I, acute accent */
100 1.31 christos { "Icirc", 206 }, /* capital I, circumflex accent */
101 1.31 christos { "Igrave", 204 }, /* capital I, grave accent */
102 1.31 christos { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */
103 1.31 christos { "Ntilde", 209 }, /* capital N, tilde */
104 1.31 christos { "Oacute", 211 }, /* capital O, acute accent */
105 1.31 christos { "Ocirc", 212 }, /* capital O, circumflex accent */
106 1.31 christos { "Ograve", 210 }, /* capital O, grave accent */
107 1.31 christos { "Oslash", 216 }, /* capital O, slash */
108 1.31 christos { "Otilde", 213 }, /* capital O, tilde */
109 1.31 christos { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */
110 1.31 christos { "THORN", 222 }, /* capital THORN, Icelandic */
111 1.31 christos { "Uacute", 218 }, /* capital U, acute accent */
112 1.31 christos { "Ucirc", 219 }, /* capital U, circumflex accent */
113 1.31 christos { "Ugrave", 217 }, /* capital U, grave accent */
114 1.31 christos { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */
115 1.31 christos { "Yacute", 221 }, /* capital Y, acute accent */
116 1.31 christos { "aacute", 225 }, /* small a, acute accent */
117 1.31 christos { "acirc", 226 }, /* small a, circumflex accent */
118 1.31 christos { "acute", 180 }, /* acute accent */
119 1.31 christos { "aelig", 230 }, /* small ae diphthong (ligature) */
120 1.31 christos { "agrave", 224 }, /* small a, grave accent */
121 1.31 christos { "amp", 38 }, /* ampersand */
122 1.31 christos { "aring", 229 }, /* small a, ring */
123 1.31 christos { "atilde", 227 }, /* small a, tilde */
124 1.31 christos { "auml", 228 }, /* small a, dieresis or umlaut mark */
125 1.31 christos { "brvbar", 166 }, /* broken (vertical) bar */
126 1.31 christos { "ccedil", 231 }, /* small c, cedilla */
127 1.31 christos { "cedil", 184 }, /* cedilla */
128 1.31 christos { "cent", 162 }, /* cent sign */
129 1.31 christos { "copy", 169 }, /* copyright sign */
130 1.31 christos { "curren", 164 }, /* general currency sign */
131 1.31 christos { "deg", 176 }, /* degree sign */
132 1.31 christos { "divide", 247 }, /* divide sign */
133 1.31 christos { "eacute", 233 }, /* small e, acute accent */
134 1.31 christos { "ecirc", 234 }, /* small e, circumflex accent */
135 1.31 christos { "egrave", 232 }, /* small e, grave accent */
136 1.31 christos { "eth", 240 }, /* small eth, Icelandic */
137 1.31 christos { "euml", 235 }, /* small e, dieresis or umlaut mark */
138 1.31 christos { "frac12", 189 }, /* fraction one-half */
139 1.31 christos { "frac14", 188 }, /* fraction one-quarter */
140 1.31 christos { "frac34", 190 }, /* fraction three-quarters */
141 1.31 christos { "gt", 62 }, /* greater than */
142 1.31 christos { "iacute", 237 }, /* small i, acute accent */
143 1.31 christos { "icirc", 238 }, /* small i, circumflex accent */
144 1.31 christos { "iexcl", 161 }, /* inverted exclamation mark */
145 1.31 christos { "igrave", 236 }, /* small i, grave accent */
146 1.31 christos { "iquest", 191 }, /* inverted question mark */
147 1.31 christos { "iuml", 239 }, /* small i, dieresis or umlaut mark */
148 1.31 christos { "laquo", 171 }, /* angle quotation mark, left */
149 1.31 christos { "lt", 60 }, /* less than */
150 1.31 christos { "macr", 175 }, /* macron */
151 1.31 christos { "micro", 181 }, /* micro sign */
152 1.31 christos { "middot", 183 }, /* middle dot */
153 1.31 christos { "nbsp", 160 }, /* no-break space */
154 1.31 christos { "not", 172 }, /* not sign */
155 1.31 christos { "ntilde", 241 }, /* small n, tilde */
156 1.31 christos { "oacute", 243 }, /* small o, acute accent */
157 1.31 christos { "ocirc", 244 }, /* small o, circumflex accent */
158 1.31 christos { "ograve", 242 }, /* small o, grave accent */
159 1.31 christos { "ordf", 170 }, /* ordinal indicator, feminine */
160 1.31 christos { "ordm", 186 }, /* ordinal indicator, masculine */
161 1.31 christos { "oslash", 248 }, /* small o, slash */
162 1.31 christos { "otilde", 245 }, /* small o, tilde */
163 1.31 christos { "ouml", 246 }, /* small o, dieresis or umlaut mark */
164 1.31 christos { "para", 182 }, /* pilcrow (paragraph sign) */
165 1.31 christos { "plusmn", 177 }, /* plus-or-minus sign */
166 1.31 christos { "pound", 163 }, /* pound sterling sign */
167 1.31 christos { "quot", 34 }, /* double quote */
168 1.31 christos { "raquo", 187 }, /* angle quotation mark, right */
169 1.31 christos { "reg", 174 }, /* registered sign */
170 1.31 christos { "sect", 167 }, /* section sign */
171 1.31 christos { "shy", 173 }, /* soft hyphen */
172 1.31 christos { "sup1", 185 }, /* superscript one */
173 1.31 christos { "sup2", 178 }, /* superscript two */
174 1.31 christos { "sup3", 179 }, /* superscript three */
175 1.31 christos { "szlig", 223 }, /* small sharp s, German (sz ligature) */
176 1.31 christos { "thorn", 254 }, /* small thorn, Icelandic */
177 1.31 christos { "times", 215 }, /* multiply sign */
178 1.31 christos { "uacute", 250 }, /* small u, acute accent */
179 1.31 christos { "ucirc", 251 }, /* small u, circumflex accent */
180 1.31 christos { "ugrave", 249 }, /* small u, grave accent */
181 1.31 christos { "uml", 168 }, /* umlaut (dieresis) */
182 1.31 christos { "uuml", 252 }, /* small u, dieresis or umlaut mark */
183 1.31 christos { "yacute", 253 }, /* small y, acute accent */
184 1.31 christos { "yen", 165 }, /* yen sign */
185 1.31 christos { "yuml", 255 }, /* small y, dieresis or umlaut mark */
186 1.31 christos };
187 1.31 christos
188 1.31 christos /*
189 1.1 cgd * unvis - decode characters previously encoded by vis
190 1.1 cgd */
191 1.1 cgd int
192 1.29 christos unvis(char *cp, int c, int *astate, int flag)
193 1.1 cgd {
194 1.25 rillig unsigned char uc = (unsigned char)c;
195 1.31 christos unsigned char st, ia, is, lc;
196 1.31 christos
197 1.31 christos /*
198 1.31 christos * Bottom 8 bits of astate hold the state machine state.
199 1.31 christos * Top 8 bits hold the current character in the http 1866 nv string decoding
200 1.31 christos */
201 1.31 christos #define GS(a) ((a) & 0xff)
202 1.32 christos #define SS(a, b) (((uint32_t)(a) << 24) | (b))
203 1.32 christos #define GI(a) ((uint32_t)(a) >> 24)
204 1.1 cgd
205 1.15 lukem _DIAGASSERT(cp != NULL);
206 1.15 lukem _DIAGASSERT(astate != NULL);
207 1.31 christos st = GS(*astate);
208 1.15 lukem
209 1.1 cgd if (flag & UNVIS_END) {
210 1.31 christos switch (st) {
211 1.31 christos case S_OCTAL2:
212 1.31 christos case S_OCTAL3:
213 1.31 christos case S_HEX2:
214 1.31 christos *astate = SS(0, S_GROUND);
215 1.29 christos return UNVIS_VALID;
216 1.31 christos case S_GROUND:
217 1.31 christos return UNVIS_NOCHAR;
218 1.31 christos default:
219 1.31 christos return UNVIS_SYNBAD;
220 1.27 lukem }
221 1.1 cgd }
222 1.1 cgd
223 1.31 christos switch (st) {
224 1.1 cgd
225 1.1 cgd case S_GROUND:
226 1.1 cgd *cp = 0;
227 1.31 christos if ((flag & VIS_NOESCAPE) == 0 && c == '\\') {
228 1.31 christos *astate = SS(0, S_START);
229 1.29 christos return UNVIS_NOCHAR;
230 1.27 lukem }
231 1.31 christos if ((flag & VIS_HTTP1808) && c == '%') {
232 1.31 christos *astate = SS(0, S_HEX1);
233 1.31 christos return UNVIS_NOCHAR;
234 1.31 christos }
235 1.31 christos if ((flag & VIS_HTTP1866) && c == '&') {
236 1.31 christos *astate = SS(0, S_AMP);
237 1.29 christos return UNVIS_NOCHAR;
238 1.29 christos }
239 1.29 christos if ((flag & VIS_MIMESTYLE) && c == '=') {
240 1.31 christos *astate = SS(0, S_MIME1);
241 1.29 christos return UNVIS_NOCHAR;
242 1.22 christos }
243 1.1 cgd *cp = c;
244 1.29 christos return UNVIS_VALID;
245 1.1 cgd
246 1.1 cgd case S_START:
247 1.1 cgd switch(c) {
248 1.1 cgd case '\\':
249 1.1 cgd *cp = c;
250 1.31 christos *astate = SS(0, S_GROUND);
251 1.29 christos return UNVIS_VALID;
252 1.1 cgd case '0': case '1': case '2': case '3':
253 1.1 cgd case '4': case '5': case '6': case '7':
254 1.1 cgd *cp = (c - '0');
255 1.31 christos *astate = SS(0, S_OCTAL2);
256 1.29 christos return UNVIS_NOCHAR;
257 1.1 cgd case 'M':
258 1.13 christos *cp = (char)0200;
259 1.31 christos *astate = SS(0, S_META);
260 1.29 christos return UNVIS_NOCHAR;
261 1.1 cgd case '^':
262 1.31 christos *astate = SS(0, S_CTRL);
263 1.29 christos return UNVIS_NOCHAR;
264 1.1 cgd case 'n':
265 1.1 cgd *cp = '\n';
266 1.31 christos *astate = SS(0, S_GROUND);
267 1.29 christos return UNVIS_VALID;
268 1.1 cgd case 'r':
269 1.1 cgd *cp = '\r';
270 1.31 christos *astate = SS(0, S_GROUND);
271 1.29 christos return UNVIS_VALID;
272 1.1 cgd case 'b':
273 1.1 cgd *cp = '\b';
274 1.31 christos *astate = SS(0, S_GROUND);
275 1.29 christos return UNVIS_VALID;
276 1.1 cgd case 'a':
277 1.1 cgd *cp = '\007';
278 1.31 christos *astate = SS(0, S_GROUND);
279 1.29 christos return UNVIS_VALID;
280 1.1 cgd case 'v':
281 1.1 cgd *cp = '\v';
282 1.31 christos *astate = SS(0, S_GROUND);
283 1.29 christos return UNVIS_VALID;
284 1.1 cgd case 't':
285 1.1 cgd *cp = '\t';
286 1.31 christos *astate = SS(0, S_GROUND);
287 1.29 christos return UNVIS_VALID;
288 1.1 cgd case 'f':
289 1.1 cgd *cp = '\f';
290 1.31 christos *astate = SS(0, S_GROUND);
291 1.29 christos return UNVIS_VALID;
292 1.1 cgd case 's':
293 1.1 cgd *cp = ' ';
294 1.31 christos *astate = SS(0, S_GROUND);
295 1.29 christos return UNVIS_VALID;
296 1.1 cgd case 'E':
297 1.1 cgd *cp = '\033';
298 1.31 christos *astate = SS(0, S_GROUND);
299 1.29 christos return UNVIS_VALID;
300 1.1 cgd case '\n':
301 1.1 cgd /*
302 1.1 cgd * hidden newline
303 1.1 cgd */
304 1.31 christos *astate = SS(0, S_GROUND);
305 1.31 christos return UNVIS_NOCHAR;
306 1.1 cgd case '$':
307 1.1 cgd /*
308 1.1 cgd * hidden marker
309 1.1 cgd */
310 1.31 christos *astate = SS(0, S_GROUND);
311 1.31 christos return UNVIS_NOCHAR;
312 1.1 cgd }
313 1.31 christos goto bad;
314 1.27 lukem
315 1.1 cgd case S_META:
316 1.1 cgd if (c == '-')
317 1.31 christos *astate = SS(0, S_META1);
318 1.1 cgd else if (c == '^')
319 1.31 christos *astate = SS(0, S_CTRL);
320 1.31 christos else
321 1.31 christos goto bad;
322 1.29 christos return UNVIS_NOCHAR;
323 1.27 lukem
324 1.1 cgd case S_META1:
325 1.31 christos *astate = SS(0, S_GROUND);
326 1.1 cgd *cp |= c;
327 1.29 christos return UNVIS_VALID;
328 1.27 lukem
329 1.1 cgd case S_CTRL:
330 1.1 cgd if (c == '?')
331 1.1 cgd *cp |= 0177;
332 1.1 cgd else
333 1.1 cgd *cp |= c & 037;
334 1.31 christos *astate = SS(0, S_GROUND);
335 1.29 christos return UNVIS_VALID;
336 1.1 cgd
337 1.1 cgd case S_OCTAL2: /* second possible octal digit */
338 1.25 rillig if (isoctal(uc)) {
339 1.27 lukem /*
340 1.27 lukem * yes - and maybe a third
341 1.1 cgd */
342 1.1 cgd *cp = (*cp << 3) + (c - '0');
343 1.31 christos *astate = SS(0, S_OCTAL3);
344 1.29 christos return UNVIS_NOCHAR;
345 1.27 lukem }
346 1.27 lukem /*
347 1.27 lukem * no - done with current sequence, push back passed char
348 1.1 cgd */
349 1.31 christos *astate = SS(0, S_GROUND);
350 1.29 christos return UNVIS_VALIDPUSH;
351 1.1 cgd
352 1.1 cgd case S_OCTAL3: /* third possible octal digit */
353 1.31 christos *astate = SS(0, S_GROUND);
354 1.25 rillig if (isoctal(uc)) {
355 1.1 cgd *cp = (*cp << 3) + (c - '0');
356 1.29 christos return UNVIS_VALID;
357 1.1 cgd }
358 1.1 cgd /*
359 1.1 cgd * we were done, push back passed char
360 1.1 cgd */
361 1.29 christos return UNVIS_VALIDPUSH;
362 1.26 lukem
363 1.22 christos case S_HEX1:
364 1.25 rillig if (isxdigit(uc)) {
365 1.25 rillig *cp = xtod(uc);
366 1.31 christos *astate = SS(0, S_HEX2);
367 1.29 christos return UNVIS_NOCHAR;
368 1.22 christos }
369 1.27 lukem /*
370 1.27 lukem * no - done with current sequence, push back passed char
371 1.22 christos */
372 1.31 christos *astate = SS(0, S_GROUND);
373 1.29 christos return UNVIS_VALIDPUSH;
374 1.26 lukem
375 1.22 christos case S_HEX2:
376 1.27 lukem *astate = S_GROUND;
377 1.27 lukem if (isxdigit(uc)) {
378 1.27 lukem *cp = xtod(uc) | (*cp << 4);
379 1.29 christos return UNVIS_VALID;
380 1.29 christos }
381 1.29 christos return UNVIS_VALIDPUSH;
382 1.29 christos
383 1.29 christos case S_MIME1:
384 1.29 christos if (uc == '\n' || uc == '\r') {
385 1.31 christos *astate = SS(0, S_EATCRNL);
386 1.29 christos return UNVIS_NOCHAR;
387 1.29 christos }
388 1.29 christos if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
389 1.29 christos *cp = XTOD(uc);
390 1.31 christos *astate = SS(0, S_MIME2);
391 1.29 christos return UNVIS_NOCHAR;
392 1.29 christos }
393 1.31 christos goto bad;
394 1.29 christos
395 1.29 christos case S_MIME2:
396 1.29 christos if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
397 1.31 christos *astate = SS(0, S_GROUND);
398 1.29 christos *cp = XTOD(uc) | (*cp << 4);
399 1.29 christos return UNVIS_VALID;
400 1.29 christos }
401 1.31 christos goto bad;
402 1.29 christos
403 1.29 christos case S_EATCRNL:
404 1.29 christos switch (uc) {
405 1.29 christos case '\r':
406 1.29 christos case '\n':
407 1.29 christos return UNVIS_NOCHAR;
408 1.29 christos case '=':
409 1.31 christos *astate = SS(0, S_MIME1);
410 1.29 christos return UNVIS_NOCHAR;
411 1.29 christos default:
412 1.29 christos *cp = uc;
413 1.31 christos *astate = SS(0, S_GROUND);
414 1.29 christos return UNVIS_VALID;
415 1.22 christos }
416 1.26 lukem
417 1.31 christos case S_AMP:
418 1.31 christos *cp = 0;
419 1.31 christos if (uc == '#') {
420 1.31 christos *astate = SS(0, S_NUMBER);
421 1.31 christos return UNVIS_NOCHAR;
422 1.31 christos }
423 1.31 christos *astate = SS(0, S_STRING);
424 1.31 christos /*FALLTHROUGH*/
425 1.31 christos
426 1.31 christos case S_STRING:
427 1.31 christos ia = *cp; /* index in the array */
428 1.31 christos is = GI(*astate); /* index in the string */
429 1.31 christos lc = is == 0 ? 0 : nv[ia].name[is - 1]; /* last character */
430 1.31 christos
431 1.31 christos if (uc == ';')
432 1.31 christos uc = '\0';
433 1.31 christos
434 1.31 christos for (; ia < __arraycount(nv); ia++) {
435 1.31 christos if (is != 0 && nv[ia].name[is - 1] != lc)
436 1.31 christos goto bad;
437 1.31 christos if (nv[ia].name[is] == uc)
438 1.31 christos break;
439 1.31 christos }
440 1.31 christos
441 1.31 christos if (*cp == __arraycount(nv))
442 1.31 christos goto bad;
443 1.31 christos
444 1.31 christos if (uc != 0) {
445 1.31 christos *cp = ia;
446 1.31 christos *astate = SS(is + 1, S_STRING);
447 1.31 christos return UNVIS_NOCHAR;
448 1.31 christos }
449 1.31 christos
450 1.31 christos *cp = nv[ia].value;
451 1.31 christos *astate = SS(0, S_GROUND);
452 1.31 christos return UNVIS_VALID;
453 1.31 christos
454 1.31 christos case S_NUMBER:
455 1.31 christos if (uc == ';')
456 1.31 christos return UNVIS_VALID;
457 1.31 christos if (!isdigit(uc))
458 1.31 christos goto bad;
459 1.31 christos *cp += (*cp * 10) + uc - '0';
460 1.31 christos return UNVIS_NOCHAR;
461 1.31 christos
462 1.27 lukem default:
463 1.31 christos bad:
464 1.27 lukem /*
465 1.27 lukem * decoder in unknown state - (probably uninitialized)
466 1.1 cgd */
467 1.31 christos *astate = SS(0, S_GROUND);
468 1.29 christos return UNVIS_SYNBAD;
469 1.1 cgd }
470 1.1 cgd }
471 1.1 cgd
472 1.1 cgd /*
473 1.34 christos * strnunvisx - decode src into dst
474 1.1 cgd *
475 1.1 cgd * Number of chars decoded into dst is returned, -1 on error.
476 1.1 cgd * Dst is null terminated.
477 1.1 cgd */
478 1.1 cgd
479 1.1 cgd int
480 1.34 christos strnunvisx(char *dst, size_t dlen, const char *src, int flag)
481 1.1 cgd {
482 1.12 perry char c;
483 1.34 christos char t, *start = dst;
484 1.1 cgd int state = 0;
485 1.15 lukem
486 1.15 lukem _DIAGASSERT(src != NULL);
487 1.15 lukem _DIAGASSERT(dst != NULL);
488 1.34 christos #define CHECKSPACE() \
489 1.34 christos do { \
490 1.34 christos if (dlen-- == 0) { \
491 1.34 christos errno = ENOSPC; \
492 1.34 christos return -1; \
493 1.34 christos } \
494 1.34 christos } while (/*CONSTCOND*/0)
495 1.1 cgd
496 1.6 christos while ((c = *src++) != '\0') {
497 1.26 lukem again:
498 1.34 christos switch (unvis(&t, c, &state, flag)) {
499 1.1 cgd case UNVIS_VALID:
500 1.34 christos CHECKSPACE();
501 1.34 christos *dst++ = t;
502 1.1 cgd break;
503 1.1 cgd case UNVIS_VALIDPUSH:
504 1.34 christos CHECKSPACE();
505 1.34 christos *dst++ = t;
506 1.1 cgd goto again;
507 1.1 cgd case 0:
508 1.1 cgd case UNVIS_NOCHAR:
509 1.1 cgd break;
510 1.34 christos case UNVIS_SYNBAD:
511 1.34 christos errno = EINVAL;
512 1.34 christos return -1;
513 1.1 cgd default:
514 1.34 christos _DIAGASSERT(0);
515 1.34 christos errno = EINVAL;
516 1.34 christos return -1;
517 1.1 cgd }
518 1.1 cgd }
519 1.34 christos if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
520 1.34 christos CHECKSPACE();
521 1.34 christos *dst++ = t;
522 1.34 christos }
523 1.34 christos CHECKSPACE();
524 1.1 cgd *dst = '\0';
525 1.29 christos return (int)(dst - start);
526 1.22 christos }
527 1.22 christos
528 1.22 christos int
529 1.34 christos strunvisx(char *dst, const char *src, int flag)
530 1.34 christos {
531 1.35 mrg return strnunvisx(dst, (size_t)~0, src, flag);
532 1.34 christos }
533 1.34 christos
534 1.34 christos int
535 1.31 christos strunvis(char *dst, const char *src)
536 1.22 christos {
537 1.35 mrg return strnunvisx(dst, (size_t)~0, src, 0);
538 1.34 christos }
539 1.34 christos
540 1.34 christos int
541 1.34 christos strnunvis(char *dst, size_t dlen, const char *src)
542 1.34 christos {
543 1.34 christos return strnunvisx(dst, dlen, src, 0);
544 1.1 cgd }
545 1.20 tv #endif
546