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