1 /* $NetBSD: vis.c,v 1.74 2017/11/27 16:37:21 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 "namespace.h"
59 #include <sys/types.h>
60 #include <sys/param.h>
70 __weak_alias(strvisx,_strvisx)
73 #if !HAVE_VIS || !HAVE_SVIS
80 * The reason for going through the trouble to deal with character encodings
81 * in vis(3), is that we use this to safe encode output of commands. This
82 * safe encoding varies depending on the character set. For example if we
83 * display ps output in French, we don't want to display French characters
87 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
92 #if defined(LC_C_LOCALE)
93 #define iscgraph(c) isgraph_l(c, LC_C_LOCALE)
95 /* Keep it simple for now, no locale stuff */
96 #define iscgraph(c) isgraph(c)
104 ol = setlocale(LC_CTYPE, "C");
107 setlocale(LC_CTYPE, ol);
113 #define ISGRAPH(flags, c) \
114 (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c))
116 #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
117 #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n')
118 #define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r')
119 #define xtoa(c) L"0123456789abcdef"[c]
120 #define XTOA(c) L"0123456789ABCDEF"[c]
124 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
125 static const wchar_t char_glob[] = L"*?[#";
127 #if !HAVE_NBTOOL_CONFIG_H
130 * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
131 * integral type and it is probably wrong, since currently the maximum
132 * number of bytes and character needs is 6. Until this is fixed, the
133 * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
134 * the assertion is commented out.
136 #if defined(__FreeBSD__) || defined(__DragonFly__)
138 * On FreeBSD and DragonFly, including <sys/systm.h> for CTASSERT only
139 * works in kernel mode.
142 #define CTASSERT(x) _CTASSERT(x, __LINE__)
143 #define _CTASSERT(x, y) __CTASSERT(x, y)
144 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
146 #endif /* __FreeBSD__ || __DragonFly__ */
147 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
148 #endif /* !__NetBSD__ */
152 * This is do_hvis, for HTTP style (RFC 1808)
155 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
159 || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
161 || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
163 dst = do_svis(dst, c, flags, nextc, extra);
166 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
167 *dst++ = xtoa((unsigned int)c & 0xf);
174 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
175 * NB: No handling of long lines or CRLF.
178 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
181 /* Space at the end of the line */
182 ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
184 (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
185 /* Specific char to be escaped */
186 wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
188 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
189 *dst++ = XTOA((unsigned int)c & 0xf);
191 dst = do_svis(dst, c, flags, nextc, extra);
196 * Output single byte of multibyte character.
199 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
201 if (flags & VIS_CSTYLE) {
204 *dst++ = L'\\'; *dst++ = L'n';
207 *dst++ = L'\\'; *dst++ = L'r';
210 *dst++ = L'\\'; *dst++ = L'b';
213 *dst++ = L'\\'; *dst++ = L'a';
216 *dst++ = L'\\'; *dst++ = L'v';
219 *dst++ = L'\\'; *dst++ = L't';
222 *dst++ = L'\\'; *dst++ = L'f';
225 *dst++ = L'\\'; *dst++ = L's';
228 *dst++ = L'\\'; *dst++ = L'0';
229 if (iswoctal(nextc)) {
234 /* We cannot encode these characters in VIS_CSTYLE
235 * because they special meaning */
247 case L'$': /* vis(1) -l */
250 if (ISGRAPH(flags, c) && !iswoctal(c)) {
257 if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
259 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
260 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
261 *dst++ = (c & 07) + L'0';
263 if ((flags & VIS_NOSLASH) == 0)
287 * This is do_vis, the central code of vis.
288 * dst: Pointer to the destination buffer
289 * c: Character to encode
291 * nextc: The character following 'c'
292 * extra: Pointer to the list of extra characters to be
293 * backslash-protected.
296 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
298 int iswextra, i, shft;
301 iswextra = wcschr(extra, c) != NULL;
302 if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) ||
303 ((flags & VIS_SAFE) && iswsafe(c)))) {
308 /* See comment in istrsenvisx() output loop, below. */
310 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
312 bmsk = (uint64_t)0xffLL << shft;
314 if ((c & wmsk) || i == 0)
315 dst = do_mbyte(dst, (wint_t)(
316 (uint64_t)(c & bmsk) >> shft),
317 flags, nextc, iswextra);
323 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
326 * Return the appropriate encoding function depending on the flags given.
331 if (flags & VIS_HTTPSTYLE)
333 if (flags & VIS_MIMESTYLE)
339 * Expand list of extra characters to not visually encode.
342 makeextralist(int flags, const char *src)
349 if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
352 if ((flags & VIS_NOLOCALE) || mbstowcs(dst, src, len) == (size_t)-1) {
354 for (i = 0; i < len; i++)
355 dst[i] = (wchar_t)(u_char)src[i];
358 d = dst + wcslen(dst);
360 if (flags & VIS_GLOB)
361 for (s = char_glob; *s; *d++ = *s++)
364 if (flags & VIS_SHELL)
365 for (s = char_shell; *s; *d++ = *s++)
368 if (flags & VIS_SP) *d++ = L' ';
369 if (flags & VIS_TAB) *d++ = L'\t';
370 if (flags & VIS_NL) *d++ = L'\n';
371 if (flags & VIS_DQ) *d++ = L'"';
372 if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
380 * The main internal function.
381 * All user-visible functions call this one.
384 istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength,
385 int flags, const char *mbextra, int *cerr_ptr)
387 wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
392 int clen = 0, cerr, error = -1, i, shft;
394 ssize_t mbslength, maxolen;
396 _DIAGASSERT(mbdstp != NULL);
397 _DIAGASSERT(mbsrc != NULL || mblength == 0);
398 _DIAGASSERT(mbextra != NULL);
400 mbslength = (ssize_t)mblength;
402 * When inputing a single character, must also read in the
403 * next character for nextc, the look-ahead character.
409 * Input (mbsrc) is a char string considered to be multibyte
410 * characters. The input loop will read this string pulling
411 * one character, possibly multiple bytes, from mbsrc and
412 * converting each to wchar_t in src.
414 * The vis conversion will be done using the wide char
417 * This will then be converted back to a multibyte string to
418 * return to the caller.
421 /* Allocate space for the wide char strings */
422 psrc = pdst = extra = NULL;
424 if ((psrc = calloc(mbslength + 1, sizeof(*psrc))) == NULL)
426 if ((pdst = calloc((16 * mbslength) + 1, sizeof(*pdst))) == NULL)
428 if (*mbdstp == NULL) {
429 if ((mdst = calloc((16 * mbslength) + 1, sizeof(*mdst))) == NULL)
438 if (flags & VIS_NOLOCALE) {
439 /* Do one byte at a time conversion */
442 /* Use caller's multibyte conversion error flag. */
443 cerr = cerr_ptr ? *cerr_ptr : 0;
448 * Handle up to mblength characters (not bytes). We do not
449 * stop at NULs because we may be processing a block of data
450 * that includes NULs.
452 while (mbslength > 0) {
453 /* Convert one multibyte character to wchar_t. */
455 clen = mbtowc(src, mbsrc, MB_LEN_MAX);
456 if (cerr || clen < 0) {
457 /* Conversion error, process as a byte instead. */
458 *src = (wint_t)(u_char)*mbsrc;
464 * NUL in input gives 0 return value. process
465 * as single NUL byte and keep going.
469 /* Advance buffer character pointer. */
471 /* Advance input pointer by number of bytes read. */
473 /* Decrement input byte count. */
480 * In the single character input case, we will have actually
481 * processed two characters, c and nextc. Reset len back to
482 * just a single character.
487 /* Convert extra argument to list of characters for this mode. */
488 extra = makeextralist(flags, mbextra);
490 if (dlen && *dlen == 0) {
494 *mbdst = '\0'; /* can't create extra, return "" */
499 /* Look up which processing function to call. */
500 f = getvisfun(flags);
503 * Main processing loop.
504 * Call do_Xvis processing function one character at a time
505 * with next character available for look-ahead.
507 for (start = dst; len > 0; len--) {
509 dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
516 /* Terminate the string in the buffer. */
521 * Convert wchar_t string back to multibyte output string.
522 * If we have hit a multi-byte conversion error on input,
523 * output byte-by-byte here. Else use wctomb().
526 maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
528 for (dst = start; len > 0; len--) {
530 clen = wctomb(mbdst, *dst);
531 if (cerr || clen < 0) {
533 * Conversion error, process as a byte(s) instead.
534 * Examine each byte and higher-order bytes for
536 * 0x000000000000a264 -> a2 64
537 * 0x000000001f00a264 -> 1f 00 a2 64
541 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
543 bmsk = (uint64_t)0xffLL << shft;
545 if ((*dst & wmsk) || i == 0)
546 mbdst[clen++] = (char)(
547 (uint64_t)(*dst & bmsk) >>
552 /* If this character would exceed our output limit, stop. */
553 if (olen + clen > (size_t)maxolen)
555 /* Advance output pointer by number of bytes written. */
557 /* Advance buffer character pointer. */
559 /* Incrment output character count. */
563 /* Terminate the output string. */
566 if (flags & VIS_NOLOCALE) {
567 /* Pass conversion error flag out. */
586 istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc,
587 int flags, const char *mbextra, int *cerr_ptr)
589 return istrsenvisx(mbdstp, dlen, mbsrc,
590 mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
597 * The "svis" variants all take an "extra" arg that is a pointer
598 * to a NUL-terminated list of characters to be encoded, too.
599 * These functions are useful e. g. to encode strings in such a
600 * way so that they are not interpreted by a shell.
604 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
612 ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL);
619 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
627 ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL);
634 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
636 return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL);
640 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
642 return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL);
646 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
648 return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
652 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
655 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
659 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
660 const char *mbextra, int *cerr_ptr)
662 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
668 * vis - visually encode characters
671 vis(char *mbdst, int c, int flags, int nextc)
679 ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL);
686 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
694 ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL);
701 * strvis - visually encode characters from src into dst
703 * Dst must be 4 times the size of src to account for possible
704 * expansion. The length of dst, not including the trailing NULL,
709 strvis(char *mbdst, const char *mbsrc, int flags)
711 return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL);
715 strnvis(char *dst, const char *src, size_t len, int flag)
717 return istrsenvisxl(&dst, &len, src, flag, "", NULL);
721 stravis(char **mbdstp, const char *mbsrc, int flags)
724 return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL);
728 * strvisx - visually encode characters from src into dst
730 * Dst must be 4 times the size of src to account for possible
731 * expansion. The length of dst, not including the trailing NULL,
734 * Strvisx encodes exactly len characters from src into dst.
735 * This is useful for encoding a block of data.
739 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
741 return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL);
745 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
747 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL);
751 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
754 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);