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