Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.79
      1 /*	$NetBSD: vis.c,v 1.79 2023/08/12 12:47:17 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     18  *
     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
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     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.
     44  *
     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.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 #if defined(LIBC_SCCS) && !defined(lint)
     60 __RCSID("$NetBSD: vis.c,v 1.79 2023/08/12 12:47:17 riastradh Exp $");
     61 #endif /* LIBC_SCCS and not lint */
     62 #ifdef __FBSDID
     63 __FBSDID("$FreeBSD$");
     64 #define	_DIAGASSERT(x)	assert(x)
     65 #endif
     66 
     67 #include "namespace.h"
     68 #include <sys/types.h>
     69 #include <sys/param.h>
     70 
     71 #include <assert.h>
     72 #include <vis.h>
     73 #include <errno.h>
     74 #include <stdlib.h>
     75 #include <wchar.h>
     76 #include <wctype.h>
     77 
     78 #ifdef __weak_alias
     79 __weak_alias(strvisx,_strvisx)
     80 #endif
     81 
     82 #if !HAVE_VIS || !HAVE_SVIS
     83 #include <ctype.h>
     84 #include <limits.h>
     85 #include <stdio.h>
     86 #include <string.h>
     87 
     88 /*
     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
     93  * as M-foo.
     94  */
     95 
     96 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
     97 
     98 #undef BELL
     99 #define BELL L'\a'
    100 
    101 #if defined(LC_C_LOCALE)
    102 #define iscgraph(c)      isgraph_l(c, LC_C_LOCALE)
    103 #else
    104 /* Keep it simple for now, no locale stuff */
    105 #define iscgraph(c)	isgraph(c)
    106 #ifdef notyet
    107 #include <locale.h>
    108 static int
    109 iscgraph(int c) {
    110 	int rv;
    111 	char *ol;
    112 
    113 	ol = setlocale(LC_CTYPE, "C");
    114 	rv = isgraph(c);
    115 	if (ol)
    116 		setlocale(LC_CTYPE, ol);
    117 	return rv;
    118 }
    119 #endif
    120 #endif
    121 
    122 #define ISGRAPH(flags, c) \
    123     (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c))
    124 
    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]
    130 
    131 #define MAXEXTRAS	30
    132 
    133 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
    134 static const wchar_t char_glob[] = L"*?[#";
    135 
    136 #if !HAVE_NBTOOL_CONFIG_H
    137 #ifndef __NetBSD__
    138 /*
    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.
    144  */
    145 #ifdef __FreeBSD__
    146 /*
    147  * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel
    148  * mode.
    149  */
    150 #ifndef CTASSERT
    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]
    154 #endif
    155 #endif /* __FreeBSD__ */
    156 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
    157 #endif /* !__NetBSD__ */
    158 #endif
    159 
    160 /*
    161  * This is do_hvis, for HTTP style (RFC 1808)
    162  */
    163 static wchar_t *
    164 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
    165 {
    166 	if (iswalnum(c)
    167 	    /* safe */
    168 	    || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
    169 	    /* extra */
    170 	    || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
    171 	    || c == L',')
    172 		dst = do_svis(dst, c, flags, nextc, extra);
    173 	else {
    174 		*dst++ = L'%';
    175 		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
    176 		*dst++ = xtoa((unsigned int)c & 0xf);
    177 	}
    178 
    179 	return dst;
    180 }
    181 
    182 /*
    183  * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
    184  * NB: No handling of long lines or CRLF.
    185  */
    186 static wchar_t *
    187 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
    188 {
    189 	if ((c != L'\n') &&
    190 	    /* Space at the end of the line */
    191 	    ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
    192 	    /* Out of range */
    193 	    (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
    194 	    /* Specific char to be escaped */
    195 	    wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
    196 		*dst++ = L'=';
    197 		*dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
    198 		*dst++ = XTOA((unsigned int)c & 0xf);
    199 	} else
    200 		dst = do_svis(dst, c, flags, nextc, extra);
    201 	return dst;
    202 }
    203 
    204 /*
    205  * Output single byte of multibyte character.
    206  */
    207 static wchar_t *
    208 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
    209 {
    210 	if (flags & VIS_CSTYLE) {
    211 		switch (c) {
    212 		case L'\n':
    213 			*dst++ = L'\\'; *dst++ = L'n';
    214 			return dst;
    215 		case L'\r':
    216 			*dst++ = L'\\'; *dst++ = L'r';
    217 			return dst;
    218 		case L'\b':
    219 			*dst++ = L'\\'; *dst++ = L'b';
    220 			return dst;
    221 		case BELL:
    222 			*dst++ = L'\\'; *dst++ = L'a';
    223 			return dst;
    224 		case L'\v':
    225 			*dst++ = L'\\'; *dst++ = L'v';
    226 			return dst;
    227 		case L'\t':
    228 			*dst++ = L'\\'; *dst++ = L't';
    229 			return dst;
    230 		case L'\f':
    231 			*dst++ = L'\\'; *dst++ = L'f';
    232 			return dst;
    233 		case L' ':
    234 			*dst++ = L'\\'; *dst++ = L's';
    235 			return dst;
    236 		case L'\0':
    237 			*dst++ = L'\\'; *dst++ = L'0';
    238 			if (iswoctal(nextc)) {
    239 				*dst++ = L'0';
    240 				*dst++ = L'0';
    241 			}
    242 			return dst;
    243 		/* We cannot encode these characters in VIS_CSTYLE
    244 		 * because they special meaning */
    245 		case L'n':
    246 		case L'r':
    247 		case L'b':
    248 		case L'a':
    249 		case L'v':
    250 		case L't':
    251 		case L'f':
    252 		case L's':
    253 		case L'0':
    254 		case L'M':
    255 		case L'^':
    256 		case L'$': /* vis(1) -l */
    257 			break;
    258 		default:
    259 			if (ISGRAPH(flags, c) && !iswoctal(c)) {
    260 				*dst++ = L'\\';
    261 				*dst++ = c;
    262 				return dst;
    263 			}
    264 		}
    265 	}
    266 	if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
    267 		*dst++ = L'\\';
    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';
    271 	} else {
    272 		if ((flags & VIS_NOSLASH) == 0)
    273 			*dst++ = L'\\';
    274 
    275 		if (c & 0200) {
    276 			c &= 0177;
    277 			*dst++ = L'M';
    278 		}
    279 
    280 		if (iswcntrl(c)) {
    281 			*dst++ = L'^';
    282 			if (c == 0177)
    283 				*dst++ = L'?';
    284 			else
    285 				*dst++ = c + L'@';
    286 		} else {
    287 			*dst++ = L'-';
    288 			*dst++ = c;
    289 		}
    290 	}
    291 
    292 	return dst;
    293 }
    294 
    295 /*
    296  * This is do_vis, the central code of vis.
    297  * dst:	      Pointer to the destination buffer
    298  * c:	      Character to encode
    299  * flags:     Flags word
    300  * nextc:     The character following 'c'
    301  * extra:     Pointer to the list of extra characters to be
    302  *	      backslash-protected.
    303  */
    304 static wchar_t *
    305 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
    306 {
    307 	int iswextra, i, shft;
    308 	uint64_t bmsk, wmsk;
    309 
    310 	iswextra = wcschr(extra, c) != NULL;
    311 	if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) ||
    312 	    ((flags & VIS_SAFE) && iswsafe(c)))) {
    313 		*dst++ = c;
    314 		return dst;
    315 	}
    316 
    317 	/* See comment in istrsenvisx() output loop, below. */
    318 	wmsk = 0;
    319 	for (i = sizeof(wmsk) - 1; i >= 0; i--) {
    320 		shft = i * NBBY;
    321 		bmsk = (uint64_t)0xffLL << shft;
    322 		wmsk |= bmsk;
    323 		if ((c & wmsk) || i == 0)
    324 			dst = do_mbyte(dst, (wint_t)(
    325 			    (uint64_t)(c & bmsk) >> shft),
    326 			    flags, nextc, iswextra);
    327 	}
    328 
    329 	return dst;
    330 }
    331 
    332 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
    333 
    334 /*
    335  * Return the appropriate encoding function depending on the flags given.
    336  */
    337 static visfun_t
    338 getvisfun(int flags)
    339 {
    340 	if (flags & VIS_HTTPSTYLE)
    341 		return do_hvis;
    342 	if (flags & VIS_MIMESTYLE)
    343 		return do_mvis;
    344 	return do_svis;
    345 }
    346 
    347 /*
    348  * Expand list of extra characters to not visually encode.
    349  */
    350 static wchar_t *
    351 makeextralist(int flags, const char *src)
    352 {
    353 	wchar_t *dst, *d;
    354 	size_t len;
    355 	const wchar_t *s;
    356 	mbstate_t mbstate;
    357 
    358 	len = strlen(src);
    359 	if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
    360 		return NULL;
    361 
    362 	memset(&mbstate, 0, sizeof(mbstate));
    363 	if ((flags & VIS_NOLOCALE)
    364 	    || mbsrtowcs(dst, &src, len, &mbstate) == (size_t)-1) {
    365 		size_t i;
    366 		for (i = 0; i < len; i++)
    367 			dst[i] = (wchar_t)(u_char)src[i];
    368 		d = dst + len;
    369 	} else
    370 		d = dst + wcslen(dst);
    371 
    372 	if (flags & VIS_GLOB)
    373 		for (s = char_glob; *s; *d++ = *s++)
    374 			continue;
    375 
    376 	if (flags & VIS_SHELL)
    377 		for (s = char_shell; *s; *d++ = *s++)
    378 			continue;
    379 
    380 	if (flags & VIS_SP) *d++ = L' ';
    381 	if (flags & VIS_TAB) *d++ = L'\t';
    382 	if (flags & VIS_NL) *d++ = L'\n';
    383 	if (flags & VIS_DQ) *d++ = L'"';
    384 	if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
    385 	*d = L'\0';
    386 
    387 	return dst;
    388 }
    389 
    390 /*
    391  * istrsenvisx()
    392  * 	The main internal function.
    393  *	All user-visible functions call this one.
    394  */
    395 static int
    396 istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength,
    397     int flags, const char *mbextra, int *cerr_ptr)
    398 {
    399 	wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
    400 	size_t len, olen;
    401 	uint64_t bmsk, wmsk;
    402 	wint_t c;
    403 	visfun_t f;
    404 	int clen = 0, cerr, error = -1, i, shft;
    405 	char *mbdst, *mdst;
    406 	size_t mbslength;
    407 	size_t maxolen;
    408 	mbstate_t mbstate;
    409 
    410 	_DIAGASSERT(mbdstp != NULL);
    411 	_DIAGASSERT(mbsrc != NULL || mblength == 0);
    412 	_DIAGASSERT(mbextra != NULL);
    413 
    414 	mbslength = mblength;
    415 	/*
    416 	 * When inputing a single character, must also read in the
    417 	 * next character for nextc, the look-ahead character.
    418 	 */
    419 	if (mbslength == 1)
    420 		mbslength++;
    421 
    422 	/*
    423 	 * Input (mbsrc) is a char string considered to be multibyte
    424 	 * characters.  The input loop will read this string pulling
    425 	 * one character, possibly multiple bytes, from mbsrc and
    426 	 * converting each to wchar_t in src.
    427 	 *
    428 	 * The vis conversion will be done using the wide char
    429 	 * wchar_t string.
    430 	 *
    431 	 * This will then be converted back to a multibyte string to
    432 	 * return to the caller.
    433 	 */
    434 
    435 	/*
    436 	 * Guarantee the arithmetic on input to calloc won't overflow.
    437 	 */
    438 	if (mbslength > (SIZE_MAX - 1)/16) {
    439 		errno = ENOMEM;
    440 		return -1;
    441 	}
    442 
    443 	/* Allocate space for the wide char strings */
    444 	psrc = pdst = extra = NULL;
    445 	mdst = NULL;
    446 	if ((psrc = calloc(mbslength + 1, sizeof(*psrc))) == NULL)
    447 		return -1;
    448 	if ((pdst = calloc((16 * mbslength) + 1, sizeof(*pdst))) == NULL)
    449 		goto out;
    450 	if (*mbdstp == NULL) {
    451 		if ((mdst = calloc((16 * mbslength) + 1, sizeof(*mdst))) == NULL)
    452 			goto out;
    453 		*mbdstp = mdst;
    454 	}
    455 
    456 	mbdst = *mbdstp;
    457 	dst = pdst;
    458 	src = psrc;
    459 
    460 	if (flags & VIS_NOLOCALE) {
    461 		/* Do one byte at a time conversion */
    462 		cerr = 1;
    463 	} else {
    464 		/* Use caller's multibyte conversion error flag. */
    465 		cerr = cerr_ptr ? *cerr_ptr : 0;
    466 	}
    467 
    468 	/*
    469 	 * Input loop.
    470 	 * Handle up to mblength characters (not bytes).  We do not
    471 	 * stop at NULs because we may be processing a block of data
    472 	 * that includes NULs.
    473 	 */
    474 	memset(&mbstate, 0, sizeof(mbstate));
    475 	while (mbslength > 0) {
    476 		/* Convert one multibyte character to wchar_t. */
    477 		if (!cerr) {
    478 			clen = mbrtowc(src, mbsrc,
    479 			    (mbslength < MB_LEN_MAX
    480 				? mbslength
    481 				: MB_LEN_MAX),
    482 			    &mbstate);
    483 			assert(clen < 0 || (size_t)clen <= mbslength);
    484 			assert(clen <= MB_LEN_MAX);
    485 		}
    486 		if (cerr || clen < 0) {
    487 			/* Conversion error, process as a byte instead. */
    488 			*src = (wint_t)(u_char)*mbsrc;
    489 			clen = 1;
    490 			cerr = 1;
    491 		}
    492 		if (clen == 0) {
    493 			/*
    494 			 * NUL in input gives 0 return value. process
    495 			 * as single NUL byte and keep going.
    496 			 */
    497 			clen = 1;
    498 		}
    499 		/*
    500 		 * Let n := MIN(mbslength, MB_LEN_MAX).  We have:
    501 		 *
    502 		 *	mbslength >= 1
    503 		 *	mbrtowc(..., n, &mbstate) <= n,
    504 		 *		by the contract of mbrtowc
    505 		 *
    506 		 *  clen is either
    507 		 *  (a) mbrtowc(..., n, &mbstate), in which case
    508 		 *      clen <= n <= mbslength; or
    509 		 *  (b) 1, in which case clen = 1 <= mbslength.
    510 		 */
    511 		assert(clen > 0);
    512 		assert((size_t)clen <= mbslength);
    513 		/* Advance buffer character pointer. */
    514 		src++;
    515 		/* Advance input pointer by number of bytes read. */
    516 		mbsrc += clen;
    517 		/* Decrement input byte count. */
    518 		mbslength -= clen;
    519 	}
    520 	len = src - psrc;
    521 	src = psrc;
    522 
    523 	/*
    524 	 * In the single character input case, we will have actually
    525 	 * processed two characters, c and nextc.  Reset len back to
    526 	 * just a single character.
    527 	 */
    528 	if (mblength < len)
    529 		len = mblength;
    530 
    531 	/* Convert extra argument to list of characters for this mode. */
    532 	extra = makeextralist(flags, mbextra);
    533 	if (!extra) {
    534 		if (dlen && *dlen == 0) {
    535 			errno = ENOSPC;
    536 			goto out;
    537 		}
    538 		*mbdst = '\0';	/* can't create extra, return "" */
    539 		error = 0;
    540 		goto out;
    541 	}
    542 
    543 	/* Look up which processing function to call. */
    544 	f = getvisfun(flags);
    545 
    546 	/*
    547 	 * Main processing loop.
    548 	 * Call do_Xvis processing function one character at a time
    549 	 * with next character available for look-ahead.
    550 	 */
    551 	for (start = dst; len > 0; len--) {
    552 		c = *src++;
    553 		dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
    554 		if (dst == NULL) {
    555 			errno = ENOSPC;
    556 			goto out;
    557 		}
    558 	}
    559 
    560 	/* Terminate the string in the buffer. */
    561 	*dst = L'\0';
    562 
    563 	/*
    564 	 * Output loop.
    565 	 * Convert wchar_t string back to multibyte output string.
    566 	 * If we have hit a multi-byte conversion error on input,
    567 	 * output byte-by-byte here.  Else use wctomb().
    568 	 */
    569 	len = wcslen(start);
    570 	maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
    571 	olen = 0;
    572 	memset(&mbstate, 0, sizeof(mbstate));
    573 	for (dst = start; len > 0; len--) {
    574 		if (!cerr)
    575 			clen = wcrtomb(mbdst, *dst, &mbstate);
    576 		if (cerr || clen < 0) {
    577 			/*
    578 			 * Conversion error, process as a byte(s) instead.
    579 			 * Examine each byte and higher-order bytes for
    580 			 * data.  E.g.,
    581 			 *	0x000000000000a264 -> a2 64
    582 			 *	0x000000001f00a264 -> 1f 00 a2 64
    583 			 */
    584 			clen = 0;
    585 			wmsk = 0;
    586 			for (i = sizeof(wmsk) - 1; i >= 0; i--) {
    587 				shft = i * NBBY;
    588 				bmsk = (uint64_t)0xffLL << shft;
    589 				wmsk |= bmsk;
    590 				if ((*dst & wmsk) || i == 0)
    591 					mbdst[clen++] = (char)(
    592 					    (uint64_t)(*dst & bmsk) >>
    593 					    shft);
    594 			}
    595 			cerr = 1;
    596 		}
    597 		/* If this character would exceed our output limit, stop. */
    598 		if (olen + clen > maxolen)
    599 			break;
    600 		/* Advance output pointer by number of bytes written. */
    601 		mbdst += clen;
    602 		/* Advance buffer character pointer. */
    603 		dst++;
    604 		/* Incrment output character count. */
    605 		olen += clen;
    606 	}
    607 
    608 	/* Terminate the output string. */
    609 	*mbdst = '\0';
    610 
    611 	if (flags & VIS_NOLOCALE) {
    612 		/* Pass conversion error flag out. */
    613 		if (cerr_ptr)
    614 			*cerr_ptr = cerr;
    615 	}
    616 
    617 	free(extra);
    618 	free(pdst);
    619 	free(psrc);
    620 
    621 	return (int)olen;
    622 out:
    623 	free(extra);
    624 	free(pdst);
    625 	free(psrc);
    626 	free(mdst);
    627 	return error;
    628 }
    629 
    630 static int
    631 istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc,
    632     int flags, const char *mbextra, int *cerr_ptr)
    633 {
    634 	return istrsenvisx(mbdstp, dlen, mbsrc,
    635 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
    636 }
    637 
    638 #endif
    639 
    640 #if !HAVE_SVIS
    641 /*
    642  *	The "svis" variants all take an "extra" arg that is a pointer
    643  *	to a NUL-terminated list of characters to be encoded, too.
    644  *	These functions are useful e. g. to encode strings in such a
    645  *	way so that they are not interpreted by a shell.
    646  */
    647 
    648 char *
    649 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
    650 {
    651 	char cc[2];
    652 	int ret;
    653 
    654 	cc[0] = c;
    655 	cc[1] = nextc;
    656 
    657 	ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL);
    658 	if (ret < 0)
    659 		return NULL;
    660 	return mbdst + ret;
    661 }
    662 
    663 char *
    664 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
    665 {
    666 	char cc[2];
    667 	int ret;
    668 
    669 	cc[0] = c;
    670 	cc[1] = nextc;
    671 
    672 	ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL);
    673 	if (ret < 0)
    674 		return NULL;
    675 	return mbdst + ret;
    676 }
    677 
    678 int
    679 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
    680 {
    681 	return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL);
    682 }
    683 
    684 int
    685 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
    686 {
    687 	return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL);
    688 }
    689 
    690 int
    691 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
    692 {
    693 	return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
    694 }
    695 
    696 int
    697 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    698     const char *mbextra)
    699 {
    700 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
    701 }
    702 
    703 int
    704 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    705     const char *mbextra, int *cerr_ptr)
    706 {
    707 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
    708 }
    709 #endif
    710 
    711 #if !HAVE_VIS
    712 /*
    713  * vis - visually encode characters
    714  */
    715 char *
    716 vis(char *mbdst, int c, int flags, int nextc)
    717 {
    718 	char cc[2];
    719 	int ret;
    720 
    721 	cc[0] = c;
    722 	cc[1] = nextc;
    723 
    724 	ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL);
    725 	if (ret < 0)
    726 		return NULL;
    727 	return mbdst + ret;
    728 }
    729 
    730 char *
    731 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
    732 {
    733 	char cc[2];
    734 	int ret;
    735 
    736 	cc[0] = c;
    737 	cc[1] = nextc;
    738 
    739 	ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL);
    740 	if (ret < 0)
    741 		return NULL;
    742 	return mbdst + ret;
    743 }
    744 
    745 /*
    746  * strvis - visually encode characters from src into dst
    747  *
    748  *	Dst must be 4 times the size of src to account for possible
    749  *	expansion.  The length of dst, not including the trailing NULL,
    750  *	is returned.
    751  */
    752 
    753 int
    754 strvis(char *mbdst, const char *mbsrc, int flags)
    755 {
    756 	return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL);
    757 }
    758 
    759 int
    760 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
    761 {
    762 	return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, "", NULL);
    763 }
    764 
    765 int
    766 stravis(char **mbdstp, const char *mbsrc, int flags)
    767 {
    768 	*mbdstp = NULL;
    769 	return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL);
    770 }
    771 
    772 /*
    773  * strvisx - visually encode characters from src into dst
    774  *
    775  *	Dst must be 4 times the size of src to account for possible
    776  *	expansion.  The length of dst, not including the trailing NULL,
    777  *	is returned.
    778  *
    779  *	Strvisx encodes exactly len characters from src into dst.
    780  *	This is useful for encoding a block of data.
    781  */
    782 
    783 int
    784 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
    785 {
    786 	return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL);
    787 }
    788 
    789 int
    790 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
    791 {
    792 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL);
    793 }
    794 
    795 int
    796 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    797     int *cerr_ptr)
    798 {
    799 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
    800 }
    801 #endif
    802