Home | History | Annotate | Line # | Download | only in gen
unvis.c revision 1.24
      1 /*	$NetBSD: unvis.c,v 1.24 2003/08/07 16:42:59 agc 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 #include <sys/cdefs.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.24 2003/08/07 16:42:59 agc Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #define __LIBC12_SOURCE__
     42 
     43 #include "namespace.h"
     44 #include <sys/types.h>
     45 
     46 #include <assert.h>
     47 #include <ctype.h>
     48 #include <stdio.h>
     49 #include <vis.h>
     50 
     51 #ifdef __weak_alias
     52 __weak_alias(strunvis,_strunvis)
     53 __weak_alias(unvis,_unvis)
     54 #endif
     55 
     56 #ifdef __warn_references
     57 __warn_references(unvis,
     58     "warning: reference to compatibility unvis(); include <vis.h> for correct reference")
     59 #endif
     60 
     61 #if !HAVE_VIS
     62 /*
     63  * decode driven by state machine
     64  */
     65 #define	S_GROUND	0	/* haven't seen escape char */
     66 #define	S_START		1	/* start decoding special sequence */
     67 #define	S_META		2	/* metachar started (M) */
     68 #define	S_META1		3	/* metachar more, regular char (-) */
     69 #define	S_CTRL		4	/* control char started (^) */
     70 #define	S_OCTAL2	5	/* octal digit 2 */
     71 #define	S_OCTAL3	6	/* octal digit 3 */
     72 #define S_HEX1		7	/* hex digit */
     73 #define S_HEX2		8	/* hex digit 2 */
     74 
     75 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
     76 #define xtod(c)		(isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
     77 
     78 int
     79 unvis(cp, c, astate, flag)
     80 	char *cp;
     81 	int c;
     82 	int *astate, flag;
     83 {
     84 	return __unvis13(cp, (int)c, astate, flag);
     85 }
     86 
     87 /*
     88  * unvis - decode characters previously encoded by vis
     89  */
     90 int
     91 __unvis13(cp, c, astate, flag)
     92 	char *cp;
     93 	int c;
     94 	int *astate, flag;
     95 {
     96 
     97 	_DIAGASSERT(cp != NULL);
     98 	_DIAGASSERT(astate != NULL);
     99 
    100 	if (flag & UNVIS_END) {
    101 		if (*astate == S_OCTAL2 || *astate == S_OCTAL3
    102 		    || *astate == S_HEX2) {
    103 			*astate = S_GROUND;
    104 			return (UNVIS_VALID);
    105 		}
    106 		return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
    107 	}
    108 
    109 	switch (*astate) {
    110 
    111 	case S_GROUND:
    112 		*cp = 0;
    113 		if (c == '\\') {
    114 			*astate = S_START;
    115 			return (0);
    116 		}
    117 		if ((flag & VIS_HTTPSTYLE) && c == '%') {
    118 			*astate = S_HEX1;
    119 			return (0);
    120 		}
    121 		*cp = c;
    122 		return (UNVIS_VALID);
    123 
    124 	case S_START:
    125 		switch(c) {
    126 		case '\\':
    127 			*cp = c;
    128 			*astate = S_GROUND;
    129 			return (UNVIS_VALID);
    130 		case '0': case '1': case '2': case '3':
    131 		case '4': case '5': case '6': case '7':
    132 			*cp = (c - '0');
    133 			*astate = S_OCTAL2;
    134 			return (0);
    135 		case 'M':
    136 			*cp = (char)0200;
    137 			*astate = S_META;
    138 			return (0);
    139 		case '^':
    140 			*astate = S_CTRL;
    141 			return (0);
    142 		case 'n':
    143 			*cp = '\n';
    144 			*astate = S_GROUND;
    145 			return (UNVIS_VALID);
    146 		case 'r':
    147 			*cp = '\r';
    148 			*astate = S_GROUND;
    149 			return (UNVIS_VALID);
    150 		case 'b':
    151 			*cp = '\b';
    152 			*astate = S_GROUND;
    153 			return (UNVIS_VALID);
    154 		case 'a':
    155 			*cp = '\007';
    156 			*astate = S_GROUND;
    157 			return (UNVIS_VALID);
    158 		case 'v':
    159 			*cp = '\v';
    160 			*astate = S_GROUND;
    161 			return (UNVIS_VALID);
    162 		case 't':
    163 			*cp = '\t';
    164 			*astate = S_GROUND;
    165 			return (UNVIS_VALID);
    166 		case 'f':
    167 			*cp = '\f';
    168 			*astate = S_GROUND;
    169 			return (UNVIS_VALID);
    170 		case 's':
    171 			*cp = ' ';
    172 			*astate = S_GROUND;
    173 			return (UNVIS_VALID);
    174 		case 'E':
    175 			*cp = '\033';
    176 			*astate = S_GROUND;
    177 			return (UNVIS_VALID);
    178 		case '\n':
    179 			/*
    180 			 * hidden newline
    181 			 */
    182 			*astate = S_GROUND;
    183 			return (UNVIS_NOCHAR);
    184 		case '$':
    185 			/*
    186 			 * hidden marker
    187 			 */
    188 			*astate = S_GROUND;
    189 			return (UNVIS_NOCHAR);
    190 		}
    191 		*astate = S_GROUND;
    192 		return (UNVIS_SYNBAD);
    193 
    194 	case S_META:
    195 		if (c == '-')
    196 			*astate = S_META1;
    197 		else if (c == '^')
    198 			*astate = S_CTRL;
    199 		else {
    200 			*astate = S_GROUND;
    201 			return (UNVIS_SYNBAD);
    202 		}
    203 		return (0);
    204 
    205 	case S_META1:
    206 		*astate = S_GROUND;
    207 		*cp |= c;
    208 		return (UNVIS_VALID);
    209 
    210 	case S_CTRL:
    211 		if (c == '?')
    212 			*cp |= 0177;
    213 		else
    214 			*cp |= c & 037;
    215 		*astate = S_GROUND;
    216 		return (UNVIS_VALID);
    217 
    218 	case S_OCTAL2:	/* second possible octal digit */
    219 		if (isoctal(c)) {
    220 			/*
    221 			 * yes - and maybe a third
    222 			 */
    223 			*cp = (*cp << 3) + (c - '0');
    224 			*astate = S_OCTAL3;
    225 			return (0);
    226 		}
    227 		/*
    228 		 * no - done with current sequence, push back passed char
    229 		 */
    230 		*astate = S_GROUND;
    231 		return (UNVIS_VALIDPUSH);
    232 
    233 	case S_OCTAL3:	/* third possible octal digit */
    234 		*astate = S_GROUND;
    235 		if (isoctal(c)) {
    236 			*cp = (*cp << 3) + (c - '0');
    237 			return (UNVIS_VALID);
    238 		}
    239 		/*
    240 		 * we were done, push back passed char
    241 		 */
    242 		return (UNVIS_VALIDPUSH);
    243 	case S_HEX1:
    244 		if (isxdigit(c)) {
    245 			*cp = xtod(c);
    246 			*astate = S_HEX2;
    247 			return (0);
    248 		}
    249 		/*
    250 		 * no - done with current sequence, push back passed char
    251 		 */
    252 		*astate = S_GROUND;
    253 		return (UNVIS_VALIDPUSH);
    254 	case S_HEX2:
    255                 *astate = S_GROUND;
    256                 if (isxdigit(c)) {
    257                         *cp = xtod(c) | (*cp << 4);
    258 			return (UNVIS_VALID);
    259 		}
    260                 return (UNVIS_VALIDPUSH);
    261 	default:
    262 		/*
    263 		 * decoder in unknown state - (probably uninitialized)
    264 		 */
    265 		*astate = S_GROUND;
    266 		return (UNVIS_SYNBAD);
    267 	}
    268 }
    269 
    270 /*
    271  * strunvis - decode src into dst
    272  *
    273  *	Number of chars decoded into dst is returned, -1 on error.
    274  *	Dst is null terminated.
    275  */
    276 
    277 int
    278 strunvisx(dst, src, flag)
    279 	char *dst;
    280 	const char *src;
    281 	int flag;
    282 {
    283 	char c;
    284 	char *start = dst;
    285 	int state = 0;
    286 
    287 	_DIAGASSERT(src != NULL);
    288 	_DIAGASSERT(dst != NULL);
    289 
    290 	while ((c = *src++) != '\0') {
    291 	again:
    292 		switch (__unvis13(dst, c, &state, flag)) {
    293 		case UNVIS_VALID:
    294 			dst++;
    295 			break;
    296 		case UNVIS_VALIDPUSH:
    297 			dst++;
    298 			goto again;
    299 		case 0:
    300 		case UNVIS_NOCHAR:
    301 			break;
    302 		default:
    303 			return (-1);
    304 		}
    305 	}
    306 	if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID)
    307 		dst++;
    308 	*dst = '\0';
    309 	return (dst - start);
    310 }
    311 
    312 int
    313 strunvis(dst, src)
    314 	char *dst;
    315 	const char *src;
    316 {
    317 	return strunvisx(dst, src, 0);
    318 }
    319 #endif
    320