1 /* $NetBSD: vis.c,v 1.44 2011/03/12 19:52:48 christos 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.
60 #if defined(LIBC_SCCS) && !defined(lint)
61 __RCSID("$NetBSD: vis.c,v 1.44 2011/03/12 19:52:48 christos Exp $");
62 #endif /* LIBC_SCCS and not lint */
64 #include <sys/types.h>
72 __weak_alias(strvisx,_strvisx)
75 #if !HAVE_VIS || !HAVE_SVIS
81 static char *do_svis(char *, size_t *, int, int, int, const char *);
86 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
87 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
88 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
89 #define xtoa(c) "0123456789abcdef"[c]
90 #define XTOA(c) "0123456789ABCDEF"[c]
94 #define MAKEEXTRALIST(flag, extra, orig_str) \
96 const char *orig = orig_str; \
97 const char *o = orig; \
101 extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
103 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
106 if (flag & VIS_SP) *e++ = ' '; \
107 if (flag & VIS_TAB) *e++ = '\t'; \
108 if (flag & VIS_NL) *e++ = '\n'; \
109 if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
111 } while (/*CONSTCOND*/0)
114 * This is do_hvis, for HTTP style (RFC 1808)
117 do_hvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
120 if ((isascii(c) && isalnum(c))
122 || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
124 || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
126 dst = do_svis(dst, dlen, c, flag, nextc, extra);
134 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
135 *dst++ = xtoa((unsigned int)c & 0xf);
142 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
143 * NB: No handling of long lines or CRLF.
146 do_mvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
149 /* Space at the end of the line */
150 ((isspace(c) && (nextc == '\r' || nextc == '\n')) ||
152 (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
153 /* Specific char to be escaped */
154 strchr("#$@[\\]^`{|}~", c) != NULL)) {
161 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
162 *dst++ = XTOA((unsigned int)c & 0xf);
164 dst = do_svis(dst, dlen, c, flag, nextc, extra);
170 * This is do_vis, the central code of vis.
171 * dst: Pointer to the destination buffer
172 * c: Character to encode
174 * nextc: The character following 'c'
175 * extra: Pointer to the list of extra characters to be
176 * backslash-protected.
179 do_svis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
182 size_t odlen = dlen ? *dlen : 0;
184 isextra = strchr(extra, c) != NULL;
192 } while (/*CONSTCOND*/0)
193 if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
194 ((flag & VIS_SAFE) && issafe(c)))) {
199 if (flag & VIS_CSTYLE) {
203 *dst++ = '\\'; *dst++ = 'n';
206 *dst++ = '\\'; *dst++ = 'r';
209 *dst++ = '\\'; *dst++ = 'b';
212 *dst++ = '\\'; *dst++ = 'a';
215 *dst++ = '\\'; *dst++ = 'v';
218 *dst++ = '\\'; *dst++ = 't';
221 *dst++ = '\\'; *dst++ = 'f';
224 *dst++ = '\\'; *dst++ = 's';
227 *dst++ = '\\'; *dst++ = '0';
228 if (isoctal(nextc)) {
236 *dst++ = '\\'; *dst++ = c;
243 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
246 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
247 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
248 *dst++ = (c & 07) + '0';
250 if ((flag & VIS_NOSLASH) == 0) {
257 c &= 0177; *dst++ = 'M';
269 *dst++ = '-'; *dst++ = c;
278 typedef char *(*visfun_t)(char *, size_t *, int, int, int, const char *);
281 * Return the appropriate encoding function depending on the flags given.
286 if (flag & VIS_HTTPSTYLE)
288 if (flag & VIS_MIMESTYLE)
294 * isnvis - visually encode characters, also encoding the characters
295 * pointed to by `extra'
298 isnvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
303 _DIAGASSERT(dst != NULL);
304 _DIAGASSERT(extra != NULL);
305 MAKEEXTRALIST(flag, nextra, extra);
307 if (dlen && *dlen == 0) {
311 *dst = '\0'; /* can't create nextra, return "" */
315 dst = (*f)(dst, dlen, c, flag, nextc, nextra);
317 if (dst == NULL || (dlen && *dlen == 0)) {
326 svis(char *dst, int c, int flag, int nextc, const char *extra)
328 return isnvis(dst, NULL, c, flag, nextc, extra);
332 snvis(char *dst, size_t dlen, int c, int flag, int nextc, const char *extra)
334 return isnvis(dst, &dlen, c, flag, nextc, extra);
339 * strsvis, strsvisx - visually encode characters from src into dst
341 * Extra is a pointer to a \0-terminated list of characters to
342 * be encoded, too. These functions are useful e. g. to
343 * encode strings in such a way so that they are not interpreted
346 * Dst must be 4 times the size of src to account for possible
347 * expansion. The length of dst, not including the trailing NULL,
350 * Strsvisx encodes exactly len bytes from src into dst.
351 * This is useful for encoding a block of data.
354 istrsnvis(char *dst, size_t *dlen, const char *csrc, int flag, const char *extra)
359 const unsigned char *src = (const unsigned char *)csrc;
362 _DIAGASSERT(dst != NULL);
363 _DIAGASSERT(src != NULL);
364 _DIAGASSERT(extra != NULL);
365 MAKEEXTRALIST(flag, nextra, extra);
367 *dst = '\0'; /* can't create nextra, return "" */
371 for (start = dst; (c = *src++) != '\0'; /* empty */) {
372 dst = (*f)(dst, dlen, c, flag, *src, nextra);
379 if (dlen && *dlen == 0) {
384 return (int)(dst - start);
388 strsvis(char *dst, const char *csrc, int flag, const char *extra)
390 return istrsnvis(dst, NULL, csrc, flag, extra);
394 strsnvis(char *dst, size_t dlen, const char *csrc, int flag, const char *extra)
396 return istrsnvis(dst, &dlen, csrc, flag, extra);
400 istrsnvisx(char *dst, size_t *dlen, const char *csrc, size_t len, int flag,
406 const unsigned char *src = (const unsigned char *)csrc;
409 _DIAGASSERT(dst != NULL);
410 _DIAGASSERT(src != NULL);
411 _DIAGASSERT(extra != NULL);
412 MAKEEXTRALIST(flag, nextra, extra);
414 if (dlen && *dlen == 0) {
418 *dst = '\0'; /* can't create nextra, return "" */
423 for (start = dst; len > 0; len--) {
425 dst = (*f)(dst, dlen, c, flag, len > 1 ? *src : '\0', nextra);
432 if (dlen && *dlen == 0) {
437 return (int)(dst - start);
441 strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
443 return istrsnvisx(dst, NULL, csrc, len, flag, extra);
447 strsnvisx(char *dst, size_t dlen, const char *csrc, size_t len, int flag,
450 return istrsnvisx(dst, &dlen, csrc, len, flag, extra);
456 * vis - visually encode characters
459 invis(char *dst, size_t *dlen, int c, int flag, int nextc)
462 unsigned char uc = (unsigned char)c;
465 _DIAGASSERT(dst != NULL);
467 MAKEEXTRALIST(flag, extra, "");
469 if (dlen && *dlen == 0) {
473 *dst = '\0'; /* can't create extra, return "" */
477 dst = (*f)(dst, dlen, uc, flag, nextc, extra);
479 if (dst == NULL || (dlen && *dlen == 0)) {
488 vis(char *dst, int c, int flag, int nextc)
490 return invis(dst, NULL, c, flag, nextc);
494 nvis(char *dst, size_t dlen, int c, int flag, int nextc)
496 return invis(dst, &dlen, c, flag, nextc);
501 * strvis, strvisx - visually encode characters from src into dst
503 * Dst must be 4 times the size of src to account for possible
504 * expansion. The length of dst, not including the trailing NULL,
507 * Strvisx encodes exactly len bytes from src into dst.
508 * This is useful for encoding a block of data.
511 istrnvis(char *dst, size_t *dlen, const char *src, int flag)
516 MAKEEXTRALIST(flag, extra, "");
518 if (dlen && *dlen == 0) {
522 *dst = '\0'; /* can't create extra, return "" */
525 rv = istrsnvis(dst, dlen, src, flag, extra);
531 strvis(char *dst, const char *src, int flag)
533 return istrnvis(dst, NULL, src, flag);
537 strnvis(char *dst, size_t dlen, const char *src, int flag)
539 return istrnvis(dst, &dlen, src, flag);
543 istrnvisx(char *dst, size_t *dlen, const char *src, size_t len, int flag)
548 MAKEEXTRALIST(flag, extra, "");
550 if (dlen && *dlen == 0) {
554 *dst = '\0'; /* can't create extra, return "" */
557 rv = istrsnvisx(dst, dlen, src, len, flag, extra);
563 strvisx(char *dst, const char *src, size_t len, int flag)
565 return istrnvisx(dst, NULL, src, len, flag);
569 strnvisx(char *dst, size_t dlen, const char *src, size_t len, int flag)
571 return istrnvisx(dst, &dlen, src, len, flag);