1 /* $NetBSD: vis.c,v 1.37 2008/07/25 22:29:23 dsl Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 * POSSIBILITY OF SUCH DAMAGE.
65 #define _DIAGASSERT(X)
68 #include <sys/cdefs.h>
69 #if defined(LIBC_SCCS) && !defined(lint)
70 __RCSID("$NetBSD: vis.c,v 1.37 2008/07/25 22:29:23 dsl Exp $");
71 #endif /* LIBC_SCCS and not lint */
73 #include "namespace.h"
76 #include <sys/types.h>
88 __weak_alias(strsvis
,_strsvis
)
89 __weak_alias(strsvisx
,_strsvisx
)
90 __weak_alias(strvis
,_strvis
)
91 __weak_alias(strvisx
,_strvisx
)
92 __weak_alias(svis
,_svis
)
93 __weak_alias(vis
,_vis
)
97 #if !HAVE_VIS || !HAVE_SVIS
103 static char *do_svis(char *, int, int, int, const char *);
106 #if defined(__STDC__)
112 char * ROKEN_LIB_FUNCTION
113 rk_vis (char *, int, int, int);
114 char * ROKEN_LIB_FUNCTION
115 rk_svis (char *, int, int, int, const char *);
116 int ROKEN_LIB_FUNCTION
117 rk_strvis (char *, const char *, int);
118 int ROKEN_LIB_FUNCTION
119 rk_strsvis (char *, const char *, int, const char *);
120 int ROKEN_LIB_FUNCTION
121 rk_strvisx (char *, const char *, size_t, int);
122 int ROKEN_LIB_FUNCTION
123 rk_strsvisx (char *, const char *, size_t, int, const char *);
126 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
127 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
128 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
129 #define xtoa(c) "0123456789abcdef"[c]
133 #define MAKEEXTRALIST(flag, extra, orig_str) \
135 const char *orig = orig_str; \
136 const char *o = orig; \
140 extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
142 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
145 if (flag & VIS_SP) *e++ = ' '; \
146 if (flag & VIS_TAB) *e++ = '\t'; \
147 if (flag & VIS_NL) *e++ = '\n'; \
148 if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
150 } while (/*CONSTCOND*/0)
153 * This is do_hvis, for HTTP style (RFC 1808)
156 do_hvis(char *dst
, int c
, int flag
, int nextc
, const char *extra
)
158 if (!isascii(c
) || !isalnum(c
) || strchr("$-_.+!*'(),", c
) != NULL
) {
160 *dst
++ = xtoa(((unsigned int)c
>> 4) & 0xf);
161 *dst
++ = xtoa((unsigned int)c
& 0xf);
163 dst
= do_svis(dst
, c
, flag
, nextc
, extra
);
169 * This is do_vis, the central code of vis.
170 * dst: Pointer to the destination buffer
171 * c: Character to encode
173 * nextc: The character following 'c'
174 * extra: Pointer to the list of extra characters to be
175 * backslash-protected.
178 do_svis(char *dst
, int c
, int flag
, int nextc
, const char *extra
)
181 isextra
= strchr(extra
, c
) != NULL
;
182 if (!isextra
&& isascii(c
) && (isgraph(c
) || iswhite(c
) ||
183 ((flag
& VIS_SAFE
) && issafe(c
)))) {
187 if (flag
& VIS_CSTYLE
) {
190 *dst
++ = '\\'; *dst
++ = 'n';
193 *dst
++ = '\\'; *dst
++ = 'r';
196 *dst
++ = '\\'; *dst
++ = 'b';
199 *dst
++ = '\\'; *dst
++ = 'a';
202 *dst
++ = '\\'; *dst
++ = 'v';
205 *dst
++ = '\\'; *dst
++ = 't';
208 *dst
++ = '\\'; *dst
++ = 'f';
211 *dst
++ = '\\'; *dst
++ = 's';
214 *dst
++ = '\\'; *dst
++ = '0';
215 if (isoctal(nextc
)) {
222 *dst
++ = '\\'; *dst
++ = c
;
227 if (isextra
|| ((c
& 0177) == ' ') || (flag
& VIS_OCTAL
)) {
229 *dst
++ = (u_char
)(((u_int32_t
)(u_char
)c
>> 6) & 03) + '0';
230 *dst
++ = (u_char
)(((u_int32_t
)(u_char
)c
>> 3) & 07) + '0';
231 *dst
++ = (c
& 07) + '0';
233 if ((flag
& VIS_NOSLASH
) == 0) *dst
++ = '\\';
235 c
&= 0177; *dst
++ = 'M';
244 *dst
++ = '-'; *dst
++ = c
;
252 * svis - visually encode characters, also encoding the characters
253 * pointed to by `extra'
255 char * ROKEN_LIB_FUNCTION
256 rk_svis(char *dst
, int c
, int flag
, int nextc
, const char *extra
)
260 _DIAGASSERT(dst
!= NULL
);
261 _DIAGASSERT(extra
!= NULL
);
262 MAKEEXTRALIST(flag
, nextra
, extra
);
264 *dst
= '\0'; /* can't create nextra, return "" */
267 if (flag
& VIS_HTTPSTYLE
)
268 dst
= do_hvis(dst
, c
, flag
, nextc
, nextra
);
270 dst
= do_svis(dst
, c
, flag
, nextc
, nextra
);
278 * strsvis, strsvisx - visually encode characters from src into dst
280 * Extra is a pointer to a \0-terminated list of characters to
281 * be encoded, too. These functions are useful e. g. to
282 * encode strings in such a way so that they are not interpreted
285 * Dst must be 4 times the size of src to account for possible
286 * expansion. The length of dst, not including the trailing NULL,
289 * Strsvisx encodes exactly len bytes from src into dst.
290 * This is useful for encoding a block of data.
292 int ROKEN_LIB_FUNCTION
293 rk_strsvis(char *dst
, const char *csrc
, int flag
, const char *extra
)
298 const unsigned char *src
= (const unsigned char *)csrc
;
300 _DIAGASSERT(dst
!= NULL
);
301 _DIAGASSERT(src
!= NULL
);
302 _DIAGASSERT(extra
!= NULL
);
303 MAKEEXTRALIST(flag
, nextra
, extra
);
305 *dst
= '\0'; /* can't create nextra, return "" */
308 if (flag
& VIS_HTTPSTYLE
) {
309 for (start
= dst
; (c
= *src
++) != '\0'; /* empty */)
310 dst
= do_hvis(dst
, c
, flag
, *src
, nextra
);
312 for (start
= dst
; (c
= *src
++) != '\0'; /* empty */)
313 dst
= do_svis(dst
, c
, flag
, *src
, nextra
);
317 return (dst
- start
);
321 int ROKEN_LIB_FUNCTION
322 rk_strsvisx(char *dst
, const char *csrc
, size_t len
, int flag
, const char *extra
)
327 const unsigned char *src
= (const unsigned char *)csrc
;
329 _DIAGASSERT(dst
!= NULL
);
330 _DIAGASSERT(src
!= NULL
);
331 _DIAGASSERT(extra
!= NULL
);
332 MAKEEXTRALIST(flag
, nextra
, extra
);
334 *dst
= '\0'; /* can't create nextra, return "" */
338 if (flag
& VIS_HTTPSTYLE
) {
339 for (start
= dst
; len
> 0; len
--) {
341 dst
= do_hvis(dst
, c
, flag
, len
? *src
: '\0', nextra
);
344 for (start
= dst
; len
> 0; len
--) {
346 dst
= do_svis(dst
, c
, flag
, len
? *src
: '\0', nextra
);
351 return (dst
- start
);
357 * vis - visually encode characters
359 char * ROKEN_LIB_FUNCTION
360 rk_vis(char *dst
, int c
, int flag
, int nextc
)
363 unsigned char uc
= (unsigned char)c
;
365 _DIAGASSERT(dst
!= NULL
);
367 MAKEEXTRALIST(flag
, extra
, "");
369 *dst
= '\0'; /* can't create extra, return "" */
372 if (flag
& VIS_HTTPSTYLE
)
373 dst
= do_hvis(dst
, uc
, flag
, nextc
, extra
);
375 dst
= do_svis(dst
, uc
, flag
, nextc
, extra
);
383 * strvis, strvisx - visually encode characters from src into dst
385 * Dst must be 4 times the size of src to account for possible
386 * expansion. The length of dst, not including the trailing NULL,
389 * Strvisx encodes exactly len bytes from src into dst.
390 * This is useful for encoding a block of data.
392 int ROKEN_LIB_FUNCTION
393 rk_strvis(char *dst
, const char *src
, int flag
)
398 MAKEEXTRALIST(flag
, extra
, "");
400 *dst
= '\0'; /* can't create extra, return "" */
403 rv
= strsvis(dst
, src
, flag
, extra
);
409 int ROKEN_LIB_FUNCTION
410 rk_strvisx(char *dst
, const char *src
, size_t len
, int flag
)
415 MAKEEXTRALIST(flag
, extra
, "");
417 *dst
= '\0'; /* can't create extra, return "" */
420 rv
= strsvisx(dst
, src
, len
, flag
, extra
);