1 /* $NetBSD: vis.c,v 1.71 2016/01/14 20:41:23 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.71 2016/01/14 20:41:23 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 #if defined(LC_C_LOCALE)
102 #define iscgraph(c) isgraph_l(c, LC_C_LOCALE)
104 /* Keep it simple for now, no locale stuff */
105 #define iscgraph(c) isgraph(c)
113 ol = setlocale(LC_CTYPE, "C");
116 setlocale(LC_CTYPE, ol);
122 #define ISGRAPH(flags, c) \
123 (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c))
125 #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
126 #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n')
127 #define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r')
128 #define xtoa(c) L"0123456789abcdef"[c]
129 #define XTOA(c) L"0123456789ABCDEF"[c]
133 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
134 static const wchar_t char_glob[] = L"*?[#";
136 #if !HAVE_NBTOOL_CONFIG_H
139 * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
140 * integral type and it is probably wrong, since currently the maximum
141 * number of bytes and character needs is 6. Until this is fixed, the
142 * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
143 * the assertion is commented out.
147 * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel
151 #define CTASSERT(x) _CTASSERT(x, __LINE__)
152 #define _CTASSERT(x, y) __CTASSERT(x, y)
153 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
155 #endif /* __FreeBSD__ */
156 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
157 #endif /* !__NetBSD__ */
161 * This is do_hvis, for HTTP style (RFC 1808)
164 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
168 || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
170 || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
172 dst = do_svis(dst, c, flags, nextc, extra);
175 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
176 *dst++ = xtoa((unsigned int)c & 0xf);
183 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
184 * NB: No handling of long lines or CRLF.
187 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
190 /* Space at the end of the line */
191 ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
193 (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
194 /* Specific char to be escaped */
195 wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
197 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
198 *dst++ = XTOA((unsigned int)c & 0xf);
200 dst = do_svis(dst, c, flags, nextc, extra);
205 * Output single byte of multibyte character.
208 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
210 if (flags & VIS_CSTYLE) {
213 *dst++ = L'\\'; *dst++ = L'n';
216 *dst++ = L'\\'; *dst++ = L'r';
219 *dst++ = L'\\'; *dst++ = L'b';
222 *dst++ = L'\\'; *dst++ = L'a';
225 *dst++ = L'\\'; *dst++ = L'v';
228 *dst++ = L'\\'; *dst++ = L't';
231 *dst++ = L'\\'; *dst++ = L'f';
234 *dst++ = L'\\'; *dst++ = L's';
237 *dst++ = L'\\'; *dst++ = L'0';
238 if (iswoctal(nextc)) {
243 /* We cannot encode these characters in VIS_CSTYLE
244 * because they special meaning */
256 case L'$': /* vis(1) -l */
259 if (ISGRAPH(flags, c) && !iswoctal(c)) {
266 if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
268 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
269 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
270 *dst++ = (c & 07) + L'0';
272 if ((flags & VIS_NOSLASH) == 0)
296 * This is do_vis, the central code of vis.
297 * dst: Pointer to the destination buffer
298 * c: Character to encode
300 * nextc: The character following 'c'
301 * extra: Pointer to the list of extra characters to be
302 * backslash-protected.
305 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
307 int iswextra, i, shft;
310 iswextra = wcschr(extra, c) != NULL;
311 if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) ||
312 ((flags & VIS_SAFE) && iswsafe(c)))) {
317 /* See comment in istrsenvisx() output loop, below. */
319 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
321 bmsk = (uint64_t)0xffLL << shft;
323 if ((c & wmsk) || i == 0)
324 dst = do_mbyte(dst, (wint_t)(
325 (uint64_t)(c & bmsk) >> shft),
326 flags, nextc, iswextra);
332 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
335 * Return the appropriate encoding function depending on the flags given.
340 if (flags & VIS_HTTPSTYLE)
342 if (flags & VIS_MIMESTYLE)
348 * Expand list of extra characters to not visually encode.
351 makeextralist(int flags, const char *src)
358 bzero(&mbstate, sizeof(mbstate));
360 if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
363 if ((flags & VIS_NOLOCALE) || mbsrtowcs(dst, &src, len, &mbstate) == (size_t)-1) {
365 for (i = 0; i < len; i++)
366 dst[i] = (wchar_t)(u_char)src[i];
369 d = dst + wcslen(dst);
371 if (flags & VIS_GLOB)
372 for (s = char_glob; *s; *d++ = *s++)
375 if (flags & VIS_SHELL)
376 for (s = char_shell; *s; *d++ = *s++)
379 if (flags & VIS_SP) *d++ = L' ';
380 if (flags & VIS_TAB) *d++ = L'\t';
381 if (flags & VIS_NL) *d++ = L'\n';
382 if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
390 * The main internal function.
391 * All user-visible functions call this one.
394 istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength,
395 int flags, const char *mbextra, int *cerr_ptr)
397 wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
402 int clen = 0, cerr, error = -1, i, shft;
404 ssize_t mbslength, maxolen;
407 _DIAGASSERT(mbdstp != NULL);
408 _DIAGASSERT(mbsrc != NULL || mblength == 0);
409 _DIAGASSERT(mbextra != NULL);
412 * Input (mbsrc) is a char string considered to be multibyte
413 * characters. The input loop will read this string pulling
414 * one character, possibly multiple bytes, from mbsrc and
415 * converting each to wchar_t in src.
417 * The vis conversion will be done using the wide char
420 * This will then be converted back to a multibyte string to
421 * return to the caller.
424 /* Allocate space for the wide char strings */
425 psrc = pdst = extra = NULL;
427 if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
429 if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
431 if (*mbdstp == NULL) {
432 if ((mdst = calloc((4 * mblength) + 1, sizeof(*mdst))) == NULL)
441 if (flags & VIS_NOLOCALE) {
442 /* Do one byte at a time conversion */
445 /* Use caller's multibyte conversion error flag. */
446 cerr = cerr_ptr ? *cerr_ptr : 0;
451 * Handle up to mblength characters (not bytes). We do not
452 * stop at NULs because we may be processing a block of data
453 * that includes NULs.
455 mbslength = (ssize_t)mblength;
457 * When inputing a single character, must also read in the
458 * next character for nextc, the look-ahead character.
462 bzero(&mbstate, sizeof(mbstate));
463 while (mbslength > 0) {
464 /* Convert one multibyte character to wchar_t. */
466 clen = mbrtowc(src, mbsrc, MB_LEN_MAX, &mbstate);
467 if (cerr || clen < 0) {
468 /* Conversion error, process as a byte instead. */
469 *src = (wint_t)(u_char)*mbsrc;
475 * NUL in input gives 0 return value. process
476 * as single NUL byte and keep going.
479 /* Advance buffer character pointer. */
481 /* Advance input pointer by number of bytes read. */
483 /* Decrement input byte count. */
489 * In the single character input case, we will have actually
490 * processed two characters, c and nextc. Reset len back to
491 * just a single character.
496 /* Convert extra argument to list of characters for this mode. */
497 extra = makeextralist(flags, mbextra);
499 if (dlen && *dlen == 0) {
503 *mbdst = '\0'; /* can't create extra, return "" */
508 /* Look up which processing function to call. */
509 f = getvisfun(flags);
512 * Main processing loop.
513 * Call do_Xvis processing function one character at a time
514 * with next character available for look-ahead.
516 for (start = dst; len > 0; len--) {
518 dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
525 /* Terminate the string in the buffer. */
530 * Convert wchar_t string back to multibyte output string.
531 * If we have hit a multi-byte conversion error on input,
532 * output byte-by-byte here. Else use wctomb().
535 maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
537 bzero(&mbstate, sizeof(mbstate));
538 for (dst = start; len > 0; len--) {
540 clen = wcrtomb(mbdst, *dst, &mbstate);
541 if (cerr || clen < 0) {
543 * Conversion error, process as a byte(s) instead.
544 * Examine each byte and higher-order bytes for
546 * 0x000000000000a264 -> a2 64
547 * 0x000000001f00a264 -> 1f 00 a2 64
551 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
553 bmsk = (uint64_t)0xffLL << shft;
555 if ((*dst & wmsk) || i == 0)
556 mbdst[clen++] = (char)(
557 (uint64_t)(*dst & bmsk) >>
562 /* If this character would exceed our output limit, stop. */
563 if (olen + clen > (size_t)maxolen)
565 /* Advance output pointer by number of bytes written. */
567 /* Advance buffer character pointer. */
569 /* Incrment output character count. */
573 /* Terminate the output string. */
576 if (flags & VIS_NOLOCALE) {
577 /* Pass conversion error flag out. */
596 istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc,
597 int flags, const char *mbextra, int *cerr_ptr)
599 return istrsenvisx(mbdstp, dlen, mbsrc,
600 mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
607 * The "svis" variants all take an "extra" arg that is a pointer
608 * to a NUL-terminated list of characters to be encoded, too.
609 * These functions are useful e. g. to encode strings in such a
610 * way so that they are not interpreted by a shell.
614 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
622 ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL);
629 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
637 ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL);
644 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
646 return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL);
650 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
652 return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL);
656 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
658 return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
662 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
665 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
669 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
670 const char *mbextra, int *cerr_ptr)
672 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
678 * vis - visually encode characters
681 vis(char *mbdst, int c, int flags, int nextc)
689 ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL);
696 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
704 ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL);
711 * strvis - visually encode characters from src into dst
713 * Dst must be 4 times the size of src to account for possible
714 * expansion. The length of dst, not including the trailing NULL,
719 strvis(char *mbdst, const char *mbsrc, int flags)
721 return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL);
725 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
727 return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, "", NULL);
731 stravis(char **mbdstp, const char *mbsrc, int flags)
734 return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL);
738 * strvisx - visually encode characters from src into dst
740 * Dst must be 4 times the size of src to account for possible
741 * expansion. The length of dst, not including the trailing NULL,
744 * Strvisx encodes exactly len characters from src into dst.
745 * This is useful for encoding a block of data.
749 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
751 return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL);
755 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
757 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL);
761 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
764 return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);