1 /* $NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 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.62 2014/09/08 17:35:01 christos Exp $");
61 #endif /* LIBC_SCCS and not lint */
63 __FBSDID("$FreeBSD$");
64 #define _DIAGASSERT(x) assert(x)
67 #include "namespace.h"
68 #include <sys/types.h>
69 #include <sys/param.h>
79 __weak_alias(strvisx,_strvisx)
82 #if !HAVE_VIS || !HAVE_SVIS
89 * The reason for going through the trouble to deal with character encodings
90 * in vis(3), is that we use this to safe encode output of commands. This
91 * safe encoding varies depending on the character set. For example if we
92 * display ps output in French, we don't want to display French characters
96 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
101 #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
102 #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n')
103 #define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r')
104 #define xtoa(c) L"0123456789abcdef"[c]
105 #define XTOA(c) L"0123456789ABCDEF"[c]
109 #if !HAVE_NBTOOL_CONFIG_H
112 * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
113 * integral type and it is probably wrong, since currently the maximum
114 * number of bytes and character needs is 6. Until this is fixed, the
115 * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
116 * the assertion is commented out.
120 * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel
124 #define CTASSERT(x) _CTASSERT(x, __LINE__)
125 #define _CTASSERT(x, y) __CTASSERT(x, y)
126 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
128 #endif /* __FreeBSD__ */
129 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
130 #endif /* !__NetBSD__ */
134 * This is do_hvis, for HTTP style (RFC 1808)
137 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
141 || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
143 || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
145 dst = do_svis(dst, c, flags, nextc, extra);
148 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
149 *dst++ = xtoa((unsigned int)c & 0xf);
156 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
157 * NB: No handling of long lines or CRLF.
160 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
163 /* Space at the end of the line */
164 ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
166 (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
167 /* Specific char to be escaped */
168 wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
170 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
171 *dst++ = XTOA((unsigned int)c & 0xf);
173 dst = do_svis(dst, c, flags, nextc, extra);
178 * Output single byte of multibyte character.
181 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
183 if (flags & VIS_CSTYLE) {
186 *dst++ = L'\\'; *dst++ = L'n';
189 *dst++ = L'\\'; *dst++ = L'r';
192 *dst++ = L'\\'; *dst++ = L'b';
195 *dst++ = L'\\'; *dst++ = L'a';
198 *dst++ = L'\\'; *dst++ = L'v';
201 *dst++ = L'\\'; *dst++ = L't';
204 *dst++ = L'\\'; *dst++ = L'f';
207 *dst++ = L'\\'; *dst++ = L's';
210 *dst++ = L'\\'; *dst++ = L'0';
211 if (iswoctal(nextc)) {
224 if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
226 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
227 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
228 *dst++ = (c & 07) + L'0';
230 if ((flags & VIS_NOSLASH) == 0)
254 * This is do_vis, the central code of vis.
255 * dst: Pointer to the destination buffer
256 * c: Character to encode
258 * nextc: The character following 'c'
259 * extra: Pointer to the list of extra characters to be
260 * backslash-protected.
263 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
265 int iswextra, i, shft;
268 iswextra = wcschr(extra, c) != NULL;
269 if (!iswextra && (iswgraph(c) || iswwhite(c) ||
270 ((flags & VIS_SAFE) && iswsafe(c)))) {
275 /* See comment in istrsenvisx() output loop, below. */
277 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
279 bmsk = (uint64_t)0xffLL << shft;
281 if ((c & wmsk) || i == 0)
282 dst = do_mbyte(dst, (wint_t)(
283 (uint64_t)(c & bmsk) >> shft),
284 flags, nextc, iswextra);
290 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
293 * Return the appropriate encoding function depending on the flags given.
298 if (flags & VIS_HTTPSTYLE)
300 if (flags & VIS_MIMESTYLE)
306 * Expand list of extra characters to not visually encode.
309 makeextralist(int flags, const char *src)
315 if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
318 if (mbstowcs(dst, src, len) == (size_t)-1) {
320 for (i = 0; i < len; i++)
321 dst[i] = (wint_t)(u_char)src[i];
324 d = dst + wcslen(dst);
326 if (flags & VIS_GLOB) {
333 if (flags & VIS_SP) *d++ = L' ';
334 if (flags & VIS_TAB) *d++ = L'\t';
335 if (flags & VIS_NL) *d++ = L'\n';
336 if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
344 * The main internal function.
345 * All user-visible functions call this one.
348 istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
349 int flags, const char *mbextra, int *cerr_ptr)
351 wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
356 int clen = 0, cerr = 0, error = -1, i, shft;
357 ssize_t mbslength, maxolen;
359 _DIAGASSERT(mbdst != NULL);
360 _DIAGASSERT(mbsrc != NULL || mblength == 0);
361 _DIAGASSERT(mbextra != NULL);
364 * Input (mbsrc) is a char string considered to be multibyte
365 * characters. The input loop will read this string pulling
366 * one character, possibly multiple bytes, from mbsrc and
367 * converting each to wchar_t in src.
369 * The vis conversion will be done using the wide char
372 * This will then be converted back to a multibyte string to
373 * return to the caller.
376 /* Allocate space for the wide char strings */
377 psrc = pdst = extra = NULL;
378 if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
380 if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
385 /* Use caller's multibyte conversion error flag. */
391 * Handle up to mblength characters (not bytes). We do not
392 * stop at NULs because we may be processing a block of data
393 * that includes NULs.
395 mbslength = (ssize_t)mblength;
397 * When inputing a single character, must also read in the
398 * next character for nextc, the look-ahead character.
402 while (mbslength > 0) {
403 /* Convert one multibyte character to wchar_t. */
405 clen = mbtowc(src, mbsrc, MB_LEN_MAX);
406 if (cerr || clen < 0) {
407 /* Conversion error, process as a byte instead. */
408 *src = (wint_t)(u_char)*mbsrc;
414 * NUL in input gives 0 return value. process
415 * as single NUL byte and keep going.
418 /* Advance buffer character pointer. */
420 /* Advance input pointer by number of bytes read. */
422 /* Decrement input byte count. */
428 * In the single character input case, we will have actually
429 * processed two characters, c and nextc. Reset len back to
430 * just a single character.
435 /* Convert extra argument to list of characters for this mode. */
436 extra = makeextralist(flags, mbextra);
438 if (dlen && *dlen == 0) {
442 *mbdst = '\0'; /* can't create extra, return "" */
447 /* Look up which processing function to call. */
448 f = getvisfun(flags);
451 * Main processing loop.
452 * Call do_Xvis processing function one character at a time
453 * with next character available for look-ahead.
455 for (start = dst; len > 0; len--) {
457 dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
464 /* Terminate the string in the buffer. */
469 * Convert wchar_t string back to multibyte output string.
470 * If we have hit a multi-byte conversion error on input,
471 * output byte-by-byte here. Else use wctomb().
474 maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
476 for (dst = start; len > 0; len--) {
478 clen = wctomb(mbdst, *dst);
479 if (cerr || clen < 0) {
481 * Conversion error, process as a byte(s) instead.
482 * Examine each byte and higher-order bytes for
484 * 0x000000000000a264 -> a2 64
485 * 0x000000001f00a264 -> 1f 00 a2 64
489 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
491 bmsk = (uint64_t)0xffLL << shft;
493 if ((*dst & wmsk) || i == 0)
494 mbdst[clen++] = (char)(
495 (uint64_t)(*dst & bmsk) >>
500 /* If this character would exceed our output limit, stop. */
501 if (olen + clen > (size_t)maxolen)
503 /* Advance output pointer by number of bytes written. */
505 /* Advance buffer character pointer. */
507 /* Incrment output character count. */
511 /* Terminate the output string. */
514 /* Pass conversion error flag out. */
531 istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
532 int flags, const char *mbextra, int *cerr_ptr)
534 return istrsenvisx(mbdst, dlen, mbsrc,
535 mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
542 * The "svis" variants all take an "extra" arg that is a pointer
543 * to a NUL-terminated list of characters to be encoded, too.
544 * These functions are useful e. g. to encode strings in such a
545 * way so that they are not interpreted by a shell.
549 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
557 ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL);
564 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
572 ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL);
579 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
581 return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL);
585 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
587 return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL);
591 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
593 return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
597 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
600 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
604 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
605 const char *mbextra, int *cerr_ptr)
607 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
613 * vis - visually encode characters
616 vis(char *mbdst, int c, int flags, int nextc)
624 ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL);
631 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
639 ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL);
646 * strvis - visually encode characters from src into dst
648 * Dst must be 4 times the size of src to account for possible
649 * expansion. The length of dst, not including the trailing NULL,
654 strvis(char *mbdst, const char *mbsrc, int flags)
656 return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL);
660 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
662 return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
666 * strvisx - visually encode characters from src into dst
668 * Dst must be 4 times the size of src to account for possible
669 * expansion. The length of dst, not including the trailing NULL,
672 * Strvisx encodes exactly len characters from src into dst.
673 * This is useful for encoding a block of data.
677 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
679 return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL);
683 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
685 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL);
689 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
692 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);