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