1 /* $NetBSD: vis.c,v 1.22 2002/03/23 17:38:27 christos Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: vis.c,v 1.22 2002/03/23 17:38:27 christos Exp $");
40 #endif /* LIBC_SCCS and not lint */
42 #include <sys/types.h>
49 __weak_alias(strsvis
,_strsvis
)
50 __weak_alias(strsvisx
,_strsvisx
)
51 __weak_alias(strvis
,_strvis
)
52 __weak_alias(strvisx
,_strvisx
)
53 __weak_alias(svis
,_svis
)
54 __weak_alias(vis
,_vis
)
74 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
75 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
76 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
77 #define xtoa(c) "0123456789abcdef"[c]
82 #define MAKEEXTRALIST(flag, extra, orig) \
84 const char *o = orig; \
88 extra = alloca((size_t)((o - orig) + MAXEXTRAS)); \
89 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
92 if (flag & VIS_SP) *e++ = ' '; \
93 if (flag & VIS_TAB) *e++ = '\t'; \
94 if (flag & VIS_NL) *e++ = '\n'; \
95 if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
97 } while (/*CONSTCOND*/0)
101 * This is HVIS, the macro of vis used to HTTP style (RFC 1808)
103 #define HVIS(dst, c, flag, nextc, extra) \
105 if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \
107 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf); \
108 *dst++ = xtoa((unsigned int)c & 0xf); \
110 SVIS(dst, c, flag, nextc, extra); \
112 while (/*CONSTCOND*/0)
115 * This is SVIS, the central macro of vis.
116 * dst: Pointer to the destination buffer
117 * c: Character to encode
119 * nextc: The character following 'c'
120 * extra: Pointer to the list of extra characters to be
121 * backslash-protected.
123 #define SVIS(dst, c, flag, nextc, extra) \
126 isextra = strchr(extra, c) != NULL; \
127 if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) || \
128 ((flag & VIS_SAFE) && issafe(c)))) { \
133 if (flag & VIS_CSTYLE) { \
136 isc = 1; *dst++ = '\\'; *dst++ = 'n'; \
139 isc = 1; *dst++ = '\\'; *dst++ = 'r'; \
142 isc = 1; *dst++ = '\\'; *dst++ = 'b'; \
145 isc = 1; *dst++ = '\\'; *dst++ = 'a'; \
148 isc = 1; *dst++ = '\\'; *dst++ = 'v'; \
151 isc = 1; *dst++ = '\\'; *dst++ = 't'; \
154 isc = 1; *dst++ = '\\'; *dst++ = 'f'; \
157 isc = 1; *dst++ = '\\'; *dst++ = 's'; \
160 isc = 1; *dst++ = '\\'; *dst++ = '0'; \
161 if (isoctal(nextc)) { \
168 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \
170 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \
171 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \
172 *dst++ = (c & 07) + '0'; \
174 if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \
176 c &= 0177; *dst++ = 'M'; \
185 *dst++ = '-'; *dst++ = c; \
188 } while (/*CONSTCOND*/0)
192 * svis - visually encode characters, also encoding the characters
193 * pointed to by `extra'
196 svis(dst
, c
, flag
, nextc
, extra
)
202 _DIAGASSERT(dst
!= NULL
);
203 _DIAGASSERT(extra
!= NULL
);
204 MAKEEXTRALIST(flag
, nextra
, extra
);
205 if (flag
& VIS_HTTPSTYLE
)
206 HVIS(dst
, c
, flag
, nextc
, nextra
);
208 SVIS(dst
, c
, flag
, nextc
, nextra
);
215 * strsvis, strsvisx - visually encode characters from src into dst
217 * Extra is a pointer to a \0-terminated list of characters to
218 * be encoded, too. These functions are useful e. g. to
219 * encode strings in such a way so that they are not interpreted
222 * Dst must be 4 times the size of src to account for possible
223 * expansion. The length of dst, not including the trailing NULL,
226 * Strsvisx encodes exactly len bytes from src into dst.
227 * This is useful for encoding a block of data.
230 strsvis(dst
, src
, flag
, extra
)
240 _DIAGASSERT(dst
!= NULL
);
241 _DIAGASSERT(src
!= NULL
);
242 _DIAGASSERT(extra
!= NULL
);
243 MAKEEXTRALIST(flag
, nextra
, extra
);
244 if (flag
& VIS_HTTPSTYLE
) {
245 for (start
= dst
; (c
= *src
++) != '\0'; /* empty */)
246 HVIS(dst
, c
, flag
, *src
, nextra
);
248 for (start
= dst
; (c
= *src
++) != '\0'; /* empty */)
249 SVIS(dst
, c
, flag
, *src
, nextra
);
252 return (dst
- start
);
257 strsvisx(dst
, src
, len
, flag
, extra
)
268 _DIAGASSERT(dst
!= NULL
);
269 _DIAGASSERT(src
!= NULL
);
270 _DIAGASSERT(extra
!= NULL
);
271 MAKEEXTRALIST(flag
, nextra
, extra
);
273 if (flag
& VIS_HTTPSTYLE
) {
274 for (start
= dst
; len
> 0; len
--) {
276 HVIS(dst
, c
, flag
, len
? *src
: '\0', nextra
);
279 for (start
= dst
; len
> 0; len
--) {
281 SVIS(dst
, c
, flag
, len
? *src
: '\0', nextra
);
285 return (dst
- start
);
290 * vis - visually encode characters
293 vis(dst
, c
, flag
, nextc
)
300 _DIAGASSERT(dst
!= NULL
);
302 MAKEEXTRALIST(flag
, extra
, "");
303 if (flag
& VIS_HTTPSTYLE
)
304 HVIS(dst
, c
, flag
, nextc
, extra
);
306 SVIS(dst
, c
, flag
, nextc
, extra
);
313 * strvis, strvisx - visually encode characters from src into dst
315 * Dst must be 4 times the size of src to account for possible
316 * expansion. The length of dst, not including the trailing NULL,
319 * Strvisx encodes exactly len bytes from src into dst.
320 * This is useful for encoding a block of data.
323 strvis(dst
, src
, flag
)
330 MAKEEXTRALIST(flag
, extra
, "");
331 return (strsvis(dst
, src
, flag
, extra
));
336 strvisx(dst
, src
, len
, flag
)
344 MAKEEXTRALIST(flag
, extra
, "");
345 return (strsvisx(dst
, src
, len
, flag
, extra
));