Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.28
      1  1.28  christos /*	$NetBSD: vis.c,v 1.28 2005/04/17 17:27:11 christos Exp $	*/
      2   1.6       cgd 
      3   1.1       cgd /*-
      4   1.6       cgd  * Copyright (c) 1989, 1993
      5  1.16  wennmach  *	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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17  1.16  wennmach  *	This product includes software developed by the University of
     18  1.16  wennmach  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.26       agc /*-
     37  1.26       agc  * Copyright (c) 1999 The NetBSD Foundation, Inc.
     38  1.26       agc  *
     39  1.26       agc  * Redistribution and use in source and binary forms, with or without
     40  1.26       agc  * modification, are permitted provided that the following conditions
     41  1.26       agc  * are met:
     42  1.26       agc  * 1. Redistributions of source code must retain the above copyright
     43  1.26       agc  *    notice, this list of conditions and the following disclaimer.
     44  1.26       agc  * 2. Redistributions in binary form must reproduce the above copyright
     45  1.26       agc  *    notice, this list of conditions and the following disclaimer in the
     46  1.26       agc  *    documentation and/or other materials provided with the distribution.
     47  1.26       agc  * 3. All advertising materials mentioning features or use of this software
     48  1.26       agc  *    must display the following acknowledgement:
     49  1.26       agc  *	This product includes software developed by the University of
     50  1.26       agc  *	California, Berkeley and its contributors.
     51  1.26       agc  * 4. Neither the name of the University nor the names of its contributors
     52  1.26       agc  *    may be used to endorse or promote products derived from this software
     53  1.26       agc  *    without specific prior written permission.
     54  1.26       agc  *
     55  1.26       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  1.26       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  1.26       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  1.26       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  1.26       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  1.26       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  1.26       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  1.26       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  1.26       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  1.26       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  1.26       agc  * SUCH DAMAGE.
     66  1.26       agc  */
     67  1.26       agc 
     68   1.7  christos #include <sys/cdefs.h>
     69  1.21        tv #if defined(LIBC_SCCS) && !defined(lint)
     70  1.28  christos __RCSID("$NetBSD: vis.c,v 1.28 2005/04/17 17:27:11 christos Exp $");
     71  1.21        tv #endif /* LIBC_SCCS and not lint */
     72   1.1       cgd 
     73   1.8       jtc #include "namespace.h"
     74   1.1       cgd #include <sys/types.h>
     75  1.12     lukem 
     76  1.12     lukem #include <assert.h>
     77   1.1       cgd #include <vis.h>
     78  1.22  christos #include <stdlib.h>
     79   1.8       jtc 
     80   1.8       jtc #ifdef __weak_alias
     81  1.18   mycroft __weak_alias(strsvis,_strsvis)
     82  1.18   mycroft __weak_alias(strsvisx,_strsvisx)
     83  1.18   mycroft __weak_alias(strvis,_strvis)
     84  1.18   mycroft __weak_alias(strvisx,_strvisx)
     85  1.18   mycroft __weak_alias(svis,_svis)
     86  1.18   mycroft __weak_alias(vis,_vis)
     87  1.20        tv #endif
     88  1.20        tv 
     89  1.24     pooka #if !HAVE_VIS || !HAVE_SVIS
     90  1.20        tv #include <ctype.h>
     91  1.20        tv #include <limits.h>
     92  1.20        tv #include <stdio.h>
     93  1.20        tv #include <string.h>
     94   1.1       cgd 
     95  1.15  wennmach #undef BELL
     96  1.15  wennmach #define BELL '\a'
     97  1.15  wennmach 
     98  1.16  wennmach #define isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
     99  1.16  wennmach #define iswhite(c)	(c == ' ' || c == '\t' || c == '\n')
    100  1.16  wennmach #define issafe(c)	(c == '\b' || c == BELL || c == '\r')
    101  1.22  christos #define xtoa(c)		"0123456789abcdef"[c]
    102  1.16  wennmach 
    103  1.27     enami #define MAXEXTRAS	5
    104  1.15  wennmach 
    105  1.15  wennmach 
    106  1.22  christos #define MAKEEXTRALIST(flag, extra, orig)				      \
    107  1.16  wennmach do {									      \
    108  1.22  christos 	const char *o = orig;						      \
    109  1.27     enami 	char *e;							      \
    110  1.22  christos 	while (*o++)							      \
    111  1.22  christos 		continue;						      \
    112  1.27     enami 	extra = alloca((size_t)((o - orig) + MAXEXTRAS));		      \
    113  1.22  christos 	for (o = orig, e = extra; (*e++ = *o++) != '\0';)		      \
    114  1.22  christos 		continue;						      \
    115  1.22  christos 	e--;								      \
    116  1.27     enami 	if (flag & VIS_SP) *e++ = ' ';					      \
    117  1.22  christos 	if (flag & VIS_TAB) *e++ = '\t';				      \
    118  1.27     enami 	if (flag & VIS_NL) *e++ = '\n';					      \
    119  1.27     enami 	if ((flag & VIS_NOSLASH) == 0) *e++ = '\\';			      \
    120  1.22  christos 	*e = '\0';							      \
    121  1.19   mycroft } while (/*CONSTCOND*/0)
    122  1.15  wennmach 
    123  1.22  christos 
    124  1.22  christos /*
    125  1.22  christos  * This is HVIS, the macro of vis used to HTTP style (RFC 1808)
    126  1.22  christos  */
    127  1.22  christos #define HVIS(dst, c, flag, nextc, extra)				      \
    128  1.27     enami do									      \
    129  1.22  christos 	if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \
    130  1.22  christos 		*dst++ = '%';						      \
    131  1.22  christos 		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);		      \
    132  1.22  christos 		*dst++ = xtoa((unsigned int)c & 0xf);			      \
    133  1.22  christos 	} else {							      \
    134  1.22  christos 		SVIS(dst, c, flag, nextc, extra);			      \
    135  1.22  christos 	}								      \
    136  1.22  christos while (/*CONSTCOND*/0)
    137  1.27     enami 
    138  1.15  wennmach /*
    139  1.15  wennmach  * This is SVIS, the central macro of vis.
    140  1.16  wennmach  * dst:	      Pointer to the destination buffer
    141  1.16  wennmach  * c:	      Character to encode
    142  1.15  wennmach  * flag:      Flag word
    143  1.15  wennmach  * nextc:     The character following 'c'
    144  1.15  wennmach  * extra:     Pointer to the list of extra characters to be
    145  1.16  wennmach  *	      backslash-protected.
    146  1.15  wennmach  */
    147  1.16  wennmach #define SVIS(dst, c, flag, nextc, extra)				      \
    148  1.16  wennmach do {									      \
    149  1.25       dsl 	int isextra;							      \
    150  1.16  wennmach 	isextra = strchr(extra, c) != NULL;				      \
    151  1.17  wennmach 	if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||	      \
    152  1.17  wennmach 	    ((flag & VIS_SAFE) && issafe(c)))) {			      \
    153  1.16  wennmach 		*dst++ = c;						      \
    154  1.16  wennmach 		break;							      \
    155  1.16  wennmach 	}								      \
    156  1.16  wennmach 	if (flag & VIS_CSTYLE) {					      \
    157  1.16  wennmach 		switch (c) {						      \
    158  1.16  wennmach 		case '\n':						      \
    159  1.25       dsl 			*dst++ = '\\'; *dst++ = 'n';			      \
    160  1.25       dsl 			continue;					      \
    161  1.16  wennmach 		case '\r':						      \
    162  1.25       dsl 			*dst++ = '\\'; *dst++ = 'r';			      \
    163  1.25       dsl 			continue;					      \
    164  1.16  wennmach 		case '\b':						      \
    165  1.25       dsl 			*dst++ = '\\'; *dst++ = 'b';			      \
    166  1.25       dsl 			continue;					      \
    167  1.16  wennmach 		case BELL:						      \
    168  1.25       dsl 			*dst++ = '\\'; *dst++ = 'a';			      \
    169  1.25       dsl 			continue;					      \
    170  1.16  wennmach 		case '\v':						      \
    171  1.25       dsl 			*dst++ = '\\'; *dst++ = 'v';			      \
    172  1.25       dsl 			continue;					      \
    173  1.16  wennmach 		case '\t':						      \
    174  1.25       dsl 			*dst++ = '\\'; *dst++ = 't';			      \
    175  1.25       dsl 			continue;					      \
    176  1.16  wennmach 		case '\f':						      \
    177  1.25       dsl 			*dst++ = '\\'; *dst++ = 'f';			      \
    178  1.25       dsl 			continue;					      \
    179  1.16  wennmach 		case ' ':						      \
    180  1.25       dsl 			*dst++ = '\\'; *dst++ = 's';			      \
    181  1.25       dsl 			continue;					      \
    182  1.16  wennmach 		case '\0':						      \
    183  1.25       dsl 			*dst++ = '\\'; *dst++ = '0';			      \
    184  1.16  wennmach 			if (isoctal(nextc)) {				      \
    185  1.16  wennmach 				*dst++ = '0';				      \
    186  1.16  wennmach 				*dst++ = '0';				      \
    187  1.16  wennmach 			}						      \
    188  1.25       dsl 			continue;					      \
    189  1.25       dsl 		default:						      \
    190  1.25       dsl 			if (isgraph(c)) {				      \
    191  1.25       dsl 				*dst++ = '\\'; *dst++ = c;		      \
    192  1.25       dsl 				continue;				      \
    193  1.25       dsl 			}						      \
    194  1.16  wennmach 		}							      \
    195  1.16  wennmach 	}								      \
    196  1.17  wennmach 	if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {	      \
    197  1.16  wennmach 		*dst++ = '\\';						      \
    198  1.16  wennmach 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';    \
    199  1.16  wennmach 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';    \
    200  1.16  wennmach 		*dst++ =			     (c	      & 07) + '0';    \
    201  1.16  wennmach 	} else {							      \
    202  1.16  wennmach 		if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';		      \
    203  1.16  wennmach 		if (c & 0200) {						      \
    204  1.16  wennmach 			c &= 0177; *dst++ = 'M';			      \
    205  1.16  wennmach 		}							      \
    206  1.16  wennmach 		if (iscntrl(c)) {					      \
    207  1.16  wennmach 			*dst++ = '^';					      \
    208  1.16  wennmach 			if (c == 0177)					      \
    209  1.16  wennmach 				*dst++ = '?';				      \
    210  1.16  wennmach 			else						      \
    211  1.16  wennmach 				*dst++ = c + '@';			      \
    212  1.16  wennmach 		} else {						      \
    213  1.16  wennmach 			*dst++ = '-'; *dst++ = c;			      \
    214  1.16  wennmach 		}							      \
    215  1.16  wennmach 	}								      \
    216  1.19   mycroft } while (/*CONSTCOND*/0)
    217  1.15  wennmach 
    218  1.15  wennmach 
    219  1.15  wennmach /*
    220  1.17  wennmach  * svis - visually encode characters, also encoding the characters
    221  1.17  wennmach  * 	  pointed to by `extra'
    222  1.15  wennmach  */
    223  1.15  wennmach char *
    224  1.15  wennmach svis(dst, c, flag, nextc, extra)
    225  1.16  wennmach 	char *dst;
    226  1.16  wennmach 	int c, flag, nextc;
    227  1.16  wennmach 	const char *extra;
    228  1.15  wennmach {
    229  1.22  christos 	char *nextra;
    230  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    231  1.16  wennmach 	_DIAGASSERT(extra != NULL);
    232  1.22  christos 	MAKEEXTRALIST(flag, nextra, extra);
    233  1.22  christos 	if (flag & VIS_HTTPSTYLE)
    234  1.22  christos 		HVIS(dst, c, flag, nextc, nextra);
    235  1.22  christos 	else
    236  1.22  christos 		SVIS(dst, c, flag, nextc, nextra);
    237  1.16  wennmach 	*dst = '\0';
    238  1.16  wennmach 	return(dst);
    239  1.15  wennmach }
    240  1.15  wennmach 
    241  1.15  wennmach 
    242  1.15  wennmach /*
    243  1.15  wennmach  * strsvis, strsvisx - visually encode characters from src into dst
    244  1.15  wennmach  *
    245  1.16  wennmach  *	Extra is a pointer to a \0-terminated list of characters to
    246  1.17  wennmach  *	be encoded, too. These functions are useful e. g. to
    247  1.17  wennmach  *	encode strings in such a way so that they are not interpreted
    248  1.16  wennmach  *	by a shell.
    249  1.27     enami  *
    250  1.16  wennmach  *	Dst must be 4 times the size of src to account for possible
    251  1.16  wennmach  *	expansion.  The length of dst, not including the trailing NULL,
    252  1.27     enami  *	is returned.
    253  1.15  wennmach  *
    254  1.16  wennmach  *	Strsvisx encodes exactly len bytes from src into dst.
    255  1.16  wennmach  *	This is useful for encoding a block of data.
    256  1.15  wennmach  */
    257  1.15  wennmach int
    258  1.25       dsl strsvis(dst, csrc, flag, extra)
    259  1.16  wennmach 	char *dst;
    260  1.25       dsl 	const char *csrc;
    261  1.16  wennmach 	int flag;
    262  1.16  wennmach 	const char *extra;
    263  1.15  wennmach {
    264  1.25       dsl 	int c;
    265  1.16  wennmach 	char *start;
    266  1.22  christos 	char *nextra;
    267  1.25       dsl 	const unsigned char *src = (const unsigned char *)csrc;
    268  1.15  wennmach 
    269  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    270  1.16  wennmach 	_DIAGASSERT(src != NULL);
    271  1.16  wennmach 	_DIAGASSERT(extra != NULL);
    272  1.22  christos 	MAKEEXTRALIST(flag, nextra, extra);
    273  1.22  christos 	if (flag & VIS_HTTPSTYLE) {
    274  1.22  christos 		for (start = dst; (c = *src++) != '\0'; /* empty */)
    275  1.22  christos 			HVIS(dst, c, flag, *src, nextra);
    276  1.22  christos 	} else {
    277  1.22  christos 		for (start = dst; (c = *src++) != '\0'; /* empty */)
    278  1.22  christos 			SVIS(dst, c, flag, *src, nextra);
    279  1.22  christos 	}
    280  1.16  wennmach 	*dst = '\0';
    281  1.16  wennmach 	return (dst - start);
    282  1.15  wennmach }
    283  1.15  wennmach 
    284  1.15  wennmach 
    285  1.15  wennmach int
    286  1.25       dsl strsvisx(dst, csrc, len, flag, extra)
    287  1.16  wennmach 	char *dst;
    288  1.25       dsl 	const char *csrc;
    289  1.16  wennmach 	size_t len;
    290  1.16  wennmach 	int flag;
    291  1.16  wennmach 	const char *extra;
    292  1.15  wennmach {
    293  1.28  christos 	unsigned char c;
    294  1.16  wennmach 	char *start;
    295  1.22  christos 	char *nextra;
    296  1.25       dsl 	const unsigned char *src = (const unsigned char *)csrc;
    297  1.15  wennmach 
    298  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    299  1.16  wennmach 	_DIAGASSERT(src != NULL);
    300  1.16  wennmach 	_DIAGASSERT(extra != NULL);
    301  1.22  christos 	MAKEEXTRALIST(flag, nextra, extra);
    302  1.16  wennmach 
    303  1.22  christos 	if (flag & VIS_HTTPSTYLE) {
    304  1.22  christos 		for (start = dst; len > 0; len--) {
    305  1.22  christos 			c = *src++;
    306  1.22  christos 			HVIS(dst, c, flag, len ? *src : '\0', nextra);
    307  1.22  christos 		}
    308  1.22  christos 	} else {
    309  1.22  christos 		for (start = dst; len > 0; len--) {
    310  1.22  christos 			c = *src++;
    311  1.22  christos 			SVIS(dst, c, flag, len ? *src : '\0', nextra);
    312  1.22  christos 		}
    313  1.16  wennmach 	}
    314  1.16  wennmach 	*dst = '\0';
    315  1.16  wennmach 	return (dst - start);
    316  1.15  wennmach }
    317  1.24     pooka #endif
    318  1.15  wennmach 
    319  1.24     pooka #if !HAVE_VIS
    320   1.1       cgd /*
    321   1.1       cgd  * vis - visually encode characters
    322   1.1       cgd  */
    323   1.1       cgd char *
    324   1.1       cgd vis(dst, c, flag, nextc)
    325  1.16  wennmach 	char *dst;
    326  1.16  wennmach 	int c, flag, nextc;
    327  1.27     enami 
    328  1.15  wennmach {
    329  1.22  christos 	char *extra;
    330  1.28  christos 	unsigned char uc = (unsigned char)c;
    331  1.15  wennmach 
    332  1.16  wennmach 	_DIAGASSERT(dst != NULL);
    333  1.15  wennmach 
    334  1.22  christos 	MAKEEXTRALIST(flag, extra, "");
    335  1.22  christos 	if (flag & VIS_HTTPSTYLE)
    336  1.28  christos 		HVIS(dst, uc, flag, nextc, extra);
    337  1.22  christos 	else
    338  1.28  christos 		SVIS(dst, uc, flag, nextc, extra);
    339  1.16  wennmach 	*dst = '\0';
    340  1.16  wennmach 	return (dst);
    341   1.1       cgd }
    342   1.1       cgd 
    343  1.15  wennmach 
    344   1.1       cgd /*
    345   1.1       cgd  * strvis, strvisx - visually encode characters from src into dst
    346  1.27     enami  *
    347  1.16  wennmach  *	Dst must be 4 times the size of src to account for possible
    348  1.16  wennmach  *	expansion.  The length of dst, not including the trailing NULL,
    349  1.27     enami  *	is returned.
    350   1.1       cgd  *
    351  1.16  wennmach  *	Strvisx encodes exactly len bytes from src into dst.
    352  1.16  wennmach  *	This is useful for encoding a block of data.
    353   1.1       cgd  */
    354   1.1       cgd int
    355   1.1       cgd strvis(dst, src, flag)
    356  1.16  wennmach 	char *dst;
    357  1.16  wennmach 	const char *src;
    358  1.16  wennmach 	int flag;
    359  1.15  wennmach {
    360  1.22  christos 	char *extra;
    361  1.15  wennmach 
    362  1.22  christos 	MAKEEXTRALIST(flag, extra, "");
    363  1.16  wennmach 	return (strsvis(dst, src, flag, extra));
    364   1.1       cgd }
    365   1.1       cgd 
    366  1.15  wennmach 
    367   1.1       cgd int
    368   1.1       cgd strvisx(dst, src, len, flag)
    369  1.16  wennmach 	char *dst;
    370  1.16  wennmach 	const char *src;
    371  1.16  wennmach 	size_t len;
    372  1.16  wennmach 	int flag;
    373  1.15  wennmach {
    374  1.22  christos 	char *extra;
    375   1.1       cgd 
    376  1.22  christos 	MAKEEXTRALIST(flag, extra, "");
    377  1.16  wennmach 	return (strsvisx(dst, src, len, flag, extra));
    378   1.1       cgd }
    379  1.20        tv #endif
    380