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