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