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