1 /* $NetBSD: vis.c,v 1.45 2012/12/14 21:38:18 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.
58 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
60 __RCSID("$NetBSD: vis.c,v 1.45 2012/12/14 21:38:18 christos Exp $");
61 #endif /* LIBC_SCCS and not lint */
62 __FBSDID("$FreeBSD$");
64 #include "namespace.h"
65 #include <sys/types.h>
72 #define _DIAGASSERT(x) assert(x)
75 __weak_alias(strvisx,_strvisx)
78 #if !HAVE_VIS || !HAVE_SVIS
84 static char *do_svis(char *, size_t *, int, int, int, const char *);
89 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
90 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n')
91 #define issafe(c) (c == '\b' || c == BELL || c == '\r')
92 #define xtoa(c) "0123456789abcdef"[c]
93 #define XTOA(c) "0123456789ABCDEF"[c]
97 #define MAKEEXTRALIST(flag, extra, orig_str) \
99 const char *orig = orig_str; \
100 const char *o = orig; \
104 extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \
106 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \
109 if (flag & VIS_GLOB) { \
115 if (flag & VIS_SP) *e++ = ' '; \
116 if (flag & VIS_TAB) *e++ = '\t'; \
117 if (flag & VIS_NL) *e++ = '\n'; \
118 if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \
120 } while (/*CONSTCOND*/0)
123 * This is do_hvis, for HTTP style (RFC 1808)
126 do_hvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
129 if ((isascii(c) && isalnum(c))
131 || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
133 || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
135 dst = do_svis(dst, dlen, c, flag, nextc, extra);
143 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
144 *dst++ = xtoa((unsigned int)c & 0xf);
151 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
152 * NB: No handling of long lines or CRLF.
155 do_mvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
158 /* Space at the end of the line */
159 ((isspace(c) && (nextc == '\r' || nextc == '\n')) ||
161 (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
162 /* Specific char to be escaped */
163 strchr("#$@[\\]^`{|}~", c) != NULL)) {
170 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
171 *dst++ = XTOA((unsigned int)c & 0xf);
173 dst = do_svis(dst, dlen, c, flag, nextc, extra);
179 * This is do_vis, the central code of vis.
180 * dst: Pointer to the destination buffer
181 * c: Character to encode
183 * nextc: The character following 'c'
184 * extra: Pointer to the list of extra characters to be
185 * backslash-protected.
188 do_svis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
191 size_t odlen = dlen ? *dlen : 0;
193 isextra = strchr(extra, c) != NULL;
201 } while (/*CONSTCOND*/0)
202 if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
203 ((flag & VIS_SAFE) && issafe(c)))) {
208 if (flag & VIS_CSTYLE) {
212 *dst++ = '\\'; *dst++ = 'n';
215 *dst++ = '\\'; *dst++ = 'r';
218 *dst++ = '\\'; *dst++ = 'b';
221 *dst++ = '\\'; *dst++ = 'a';
224 *dst++ = '\\'; *dst++ = 'v';
227 *dst++ = '\\'; *dst++ = 't';
230 *dst++ = '\\'; *dst++ = 'f';
233 *dst++ = '\\'; *dst++ = 's';
236 *dst++ = '\\'; *dst++ = '0';
237 if (isoctal(nextc)) {
245 *dst++ = '\\'; *dst++ = c;
252 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
255 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
256 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
257 *dst++ = (c & 07) + '0';
259 if ((flag & VIS_NOSLASH) == 0) {
266 c &= 0177; *dst++ = 'M';
278 *dst++ = '-'; *dst++ = c;
287 typedef char *(*visfun_t)(char *, size_t *, int, int, int, const char *);
290 * Return the appropriate encoding function depending on the flags given.
295 if (flag & VIS_HTTPSTYLE)
297 if (flag & VIS_MIMESTYLE)
303 * isnvis - visually encode characters, also encoding the characters
304 * pointed to by `extra'
307 isnvis(char *dst, size_t *dlen, int c, int flag, int nextc, const char *extra)
312 _DIAGASSERT(dst != NULL);
313 _DIAGASSERT(extra != NULL);
314 MAKEEXTRALIST(flag, nextra, extra);
316 if (dlen && *dlen == 0) {
320 *dst = '\0'; /* can't create nextra, return "" */
324 dst = (*f)(dst, dlen, c, flag, nextc, nextra);
326 if (dst == NULL || (dlen && *dlen == 0)) {
335 svis(char *dst, int c, int flag, int nextc, const char *extra)
337 return isnvis(dst, NULL, c, flag, nextc, extra);
341 snvis(char *dst, size_t dlen, int c, int flag, int nextc, const char *extra)
343 return isnvis(dst, &dlen, c, flag, nextc, extra);
348 * strsvis, strsvisx - visually encode characters from src into dst
350 * Extra is a pointer to a \0-terminated list of characters to
351 * be encoded, too. These functions are useful e. g. to
352 * encode strings in such a way so that they are not interpreted
355 * Dst must be 4 times the size of src to account for possible
356 * expansion. The length of dst, not including the trailing NULL,
359 * Strsvisx encodes exactly len bytes from src into dst.
360 * This is useful for encoding a block of data.
363 istrsnvis(char *dst, size_t *dlen, const char *csrc, int flag, const char *extra)
368 const unsigned char *src = (const unsigned char *)csrc;
371 _DIAGASSERT(dst != NULL);
372 _DIAGASSERT(src != NULL);
373 _DIAGASSERT(extra != NULL);
374 MAKEEXTRALIST(flag, nextra, extra);
376 *dst = '\0'; /* can't create nextra, return "" */
380 for (start = dst; (c = *src++) != '\0'; /* empty */) {
381 dst = (*f)(dst, dlen, c, flag, *src, nextra);
388 if (dlen && *dlen == 0) {
393 return (int)(dst - start);
397 strsvis(char *dst, const char *csrc, int flag, const char *extra)
399 return istrsnvis(dst, NULL, csrc, flag, extra);
403 strsnvis(char *dst, size_t dlen, const char *csrc, int flag, const char *extra)
405 return istrsnvis(dst, &dlen, csrc, flag, extra);
409 istrsnvisx(char *dst, size_t *dlen, const char *csrc, size_t len, int flag,
415 const unsigned char *src = (const unsigned char *)csrc;
418 _DIAGASSERT(dst != NULL);
419 _DIAGASSERT(src != NULL);
420 _DIAGASSERT(extra != NULL);
421 MAKEEXTRALIST(flag, nextra, extra);
423 if (dlen && *dlen == 0) {
427 *dst = '\0'; /* can't create nextra, return "" */
432 for (start = dst; len > 0; len--) {
434 dst = (*f)(dst, dlen, c, flag, len > 1 ? *src : '\0', nextra);
441 if (dlen && *dlen == 0) {
446 return (int)(dst - start);
450 strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
452 return istrsnvisx(dst, NULL, csrc, len, flag, extra);
456 strsnvisx(char *dst, size_t dlen, const char *csrc, size_t len, int flag,
459 return istrsnvisx(dst, &dlen, csrc, len, flag, extra);
465 * vis - visually encode characters
468 invis(char *dst, size_t *dlen, int c, int flag, int nextc)
471 unsigned char uc = (unsigned char)c;
474 _DIAGASSERT(dst != NULL);
476 MAKEEXTRALIST(flag, extra, "");
478 if (dlen && *dlen == 0) {
482 *dst = '\0'; /* can't create extra, return "" */
486 dst = (*f)(dst, dlen, uc, flag, nextc, extra);
488 if (dst == NULL || (dlen && *dlen == 0)) {
497 vis(char *dst, int c, int flag, int nextc)
499 return invis(dst, NULL, c, flag, nextc);
503 nvis(char *dst, size_t dlen, int c, int flag, int nextc)
505 return invis(dst, &dlen, c, flag, nextc);
510 * strvis, strvisx - visually encode characters from src into dst
512 * Dst must be 4 times the size of src to account for possible
513 * expansion. The length of dst, not including the trailing NULL,
516 * Strvisx encodes exactly len bytes from src into dst.
517 * This is useful for encoding a block of data.
520 istrnvis(char *dst, size_t *dlen, const char *src, int flag)
525 MAKEEXTRALIST(flag, extra, "");
527 if (dlen && *dlen == 0) {
531 *dst = '\0'; /* can't create extra, return "" */
534 rv = istrsnvis(dst, dlen, src, flag, extra);
540 strvis(char *dst, const char *src, int flag)
542 return istrnvis(dst, NULL, src, flag);
546 strnvis(char *dst, size_t dlen, const char *src, int flag)
548 return istrnvis(dst, &dlen, src, flag);
552 istrnvisx(char *dst, size_t *dlen, const char *src, size_t len, int flag)
557 MAKEEXTRALIST(flag, extra, "");
559 if (dlen && *dlen == 0) {
563 *dst = '\0'; /* can't create extra, return "" */
566 rv = istrsnvisx(dst, dlen, src, len, flag, extra);
572 strvisx(char *dst, const char *src, size_t len, int flag)
574 return istrnvisx(dst, NULL, src, len, flag);
578 strnvisx(char *dst, size_t dlen, const char *src, size_t len, int flag)
580 return istrnvisx(dst, &dlen, src, len, flag);