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