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