1 /* $NetBSD: vis.c,v 1.1.1.1 2011/04/13 18:15:44 elric Exp $ */
3 /* NetBSD: vis.c,v 1.37 2008/07/25 22:29:23 dsl Exp */
6 * Copyright (c) 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
48 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
49 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
51 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
62 #include <krb5/roken.h>
64 #define _DIAGASSERT(X)
67 #include <sys/cdefs.h>
68 #if defined(LIBC_SCCS) && !defined(lint)
69 __RCSID("NetBSD: vis.c,v 1.37 2008/07/25 22:29:23 dsl Exp");
70 #endif /* LIBC_SCCS and not lint */
72 #include "namespace.h"
75 #include <sys/types.h>
87 __weak_alias(strsvis
,_strsvis
)
88 __weak_alias(strsvisx
,_strsvisx
)
89 __weak_alias(strvis
,_strvis
)
90 __weak_alias(strvisx
,_strvisx
)
91 __weak_alias(svis
,_svis
)
92 __weak_alias(vis
,_vis
)
96 #if !HAVE_VIS || !HAVE_SVIS
102 static char *do_svis(char *, int, int, int, const char *);
105 #if defined(__STDC__)
111 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
112 rk_vis (char *, int, int, int);
113 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
114 rk_svis (char *, int, int, int, const char *);
115 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
116 rk_strvis (char *, const char *, int);
117 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
118 rk_strsvis (char *, const char *, int, const char *);
119 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
120 rk_strvisx (char *, const char *, size_t, int);
121 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
122 rk_strsvisx (char *, const char *, size_t, int, const char *);
125 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
126 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
127 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
128 #define xtoa(c) "0123456789abcdef"[c]
132 #define MAKEEXTRALIST(flag, extra, orig_str) \
134 const char *orig = orig_str; \
135 const char *o = orig; \
139 extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
141 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
144 if (flag & VIS_SP) *e++ = ' '; \
145 if (flag & VIS_TAB) *e++ = '\t'; \
146 if (flag & VIS_NL) *e++ = '\n'; \
147 if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
149 } while (/*CONSTCOND*/0)
152 * This is do_hvis, for HTTP style (RFC 1808)
155 do_hvis(char *dst
, int c
, int flag
, int nextc
, const char *extra
)
157 if (!isascii(c
) || !isalnum(c
) || strchr("$-_.+!*'(),", c
) != NULL
) {
159 *dst
++ = xtoa(((unsigned int)c
>> 4) & 0xf);
160 *dst
++ = xtoa((unsigned int)c
& 0xf);
162 dst
= do_svis(dst
, c
, flag
, nextc
, extra
);
168 * This is do_vis, the central code of vis.
169 * dst: Pointer to the destination buffer
170 * c: Character to encode
172 * nextc: The character following 'c'
173 * extra: Pointer to the list of extra characters to be
174 * backslash-protected.
177 do_svis(char *dst
, int c
, int flag
, int nextc
, const char *extra
)
180 isextra
= strchr(extra
, c
) != NULL
;
181 if (!isextra
&& isascii(c
) && (isgraph(c
) || iswhite(c
) ||
182 ((flag
& VIS_SAFE
) && issafe(c
)))) {
186 if (flag
& VIS_CSTYLE
) {
189 *dst
++ = '\\'; *dst
++ = 'n';
192 *dst
++ = '\\'; *dst
++ = 'r';
195 *dst
++ = '\\'; *dst
++ = 'b';
198 *dst
++ = '\\'; *dst
++ = 'a';
201 *dst
++ = '\\'; *dst
++ = 'v';
204 *dst
++ = '\\'; *dst
++ = 't';
207 *dst
++ = '\\'; *dst
++ = 'f';
210 *dst
++ = '\\'; *dst
++ = 's';
213 *dst
++ = '\\'; *dst
++ = '0';
214 if (isoctal(nextc
)) {
221 *dst
++ = '\\'; *dst
++ = c
;
226 if (isextra
|| ((c
& 0177) == ' ') || (flag
& VIS_OCTAL
)) {
228 *dst
++ = (u_char
)(((unsigned int)(u_char
)c
>> 6) & 03) + '0';
229 *dst
++ = (u_char
)(((unsigned int)(u_char
)c
>> 3) & 07) + '0';
230 *dst
++ = (u_char
)( c
& 07) + '0';
232 if ((flag
& VIS_NOSLASH
) == 0) *dst
++ = '\\';
234 c
&= 0177; *dst
++ = 'M';
243 *dst
++ = '-'; *dst
++ = c
;
251 * svis - visually encode characters, also encoding the characters
252 * pointed to by `extra'
254 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
255 rk_svis(char *dst
, int c
, int flag
, int nextc
, const char *extra
)
259 _DIAGASSERT(dst
!= NULL
);
260 _DIAGASSERT(extra
!= NULL
);
261 MAKEEXTRALIST(flag
, nextra
, extra
);
263 *dst
= '\0'; /* can't create nextra, return "" */
266 if (flag
& VIS_HTTPSTYLE
)
267 dst
= do_hvis(dst
, c
, flag
, nextc
, nextra
);
269 dst
= do_svis(dst
, c
, flag
, nextc
, nextra
);
277 * strsvis, strsvisx - visually encode characters from src into dst
279 * Extra is a pointer to a \0-terminated list of characters to
280 * be encoded, too. These functions are useful e. g. to
281 * encode strings in such a way so that they are not interpreted
284 * Dst must be 4 times the size of src to account for possible
285 * expansion. The length of dst, not including the trailing NULL,
288 * Strsvisx encodes exactly len bytes from src into dst.
289 * This is useful for encoding a block of data.
292 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
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 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
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 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
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 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
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 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
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
);