Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.64
      1 /*	$NetBSD: vis.c,v 1.64 2014/09/26 05:01:44 christos 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.64 2014/09/26 05:01:44 christos 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 #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]
    106 
    107 #define MAXEXTRAS	30
    108 
    109 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
    110 static const wchar_t char_glob[] = L"*?[#";
    111 
    112 #if !HAVE_NBTOOL_CONFIG_H
    113 #ifndef __NetBSD__
    114 /*
    115  * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
    116  * integral type and it is probably wrong, since currently the maximum
    117  * number of bytes and character needs is 6. Until this is fixed, the
    118  * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
    119  * the assertion is commented out.
    120  */
    121 #ifdef __FreeBSD__
    122 /*
    123  * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel
    124  * mode.
    125  */
    126 #ifndef CTASSERT
    127 #define CTASSERT(x)             _CTASSERT(x, __LINE__)
    128 #define _CTASSERT(x, y)         __CTASSERT(x, y)
    129 #define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
    130 #endif
    131 #endif /* __FreeBSD__ */
    132 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
    133 #endif /* !__NetBSD__ */
    134 #endif
    135 
    136 /*
    137  * This is do_hvis, for HTTP style (RFC 1808)
    138  */
    139 static wchar_t *
    140 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
    141 {
    142 	if (iswalnum(c)
    143 	    /* safe */
    144 	    || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
    145 	    /* extra */
    146 	    || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
    147 	    || c == L',')
    148 		dst = do_svis(dst, c, flags, nextc, extra);
    149 	else {
    150 		*dst++ = L'%';
    151 		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
    152 		*dst++ = xtoa((unsigned int)c & 0xf);
    153 	}
    154 
    155 	return dst;
    156 }
    157 
    158 /*
    159  * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
    160  * NB: No handling of long lines or CRLF.
    161  */
    162 static wchar_t *
    163 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
    164 {
    165 	if ((c != L'\n') &&
    166 	    /* Space at the end of the line */
    167 	    ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
    168 	    /* Out of range */
    169 	    (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
    170 	    /* Specific char to be escaped */
    171 	    wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
    172 		*dst++ = L'=';
    173 		*dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
    174 		*dst++ = XTOA((unsigned int)c & 0xf);
    175 	} else
    176 		dst = do_svis(dst, c, flags, nextc, extra);
    177 	return dst;
    178 }
    179 
    180 /*
    181  * Output single byte of multibyte character.
    182  */
    183 static wchar_t *
    184 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
    185 {
    186 	if (flags & VIS_CSTYLE) {
    187 		switch (c) {
    188 		case L'\n':
    189 			*dst++ = L'\\'; *dst++ = L'n';
    190 			return dst;
    191 		case L'\r':
    192 			*dst++ = L'\\'; *dst++ = L'r';
    193 			return dst;
    194 		case L'\b':
    195 			*dst++ = L'\\'; *dst++ = L'b';
    196 			return dst;
    197 		case BELL:
    198 			*dst++ = L'\\'; *dst++ = L'a';
    199 			return dst;
    200 		case L'\v':
    201 			*dst++ = L'\\'; *dst++ = L'v';
    202 			return dst;
    203 		case L'\t':
    204 			*dst++ = L'\\'; *dst++ = L't';
    205 			return dst;
    206 		case L'\f':
    207 			*dst++ = L'\\'; *dst++ = L'f';
    208 			return dst;
    209 		case L' ':
    210 			*dst++ = L'\\'; *dst++ = L's';
    211 			return dst;
    212 		case L'\0':
    213 			*dst++ = L'\\'; *dst++ = L'0';
    214 			if (iswoctal(nextc)) {
    215 				*dst++ = L'0';
    216 				*dst++ = L'0';
    217 			}
    218 			return dst;
    219 		default:
    220 			if (iswgraph(c)) {
    221 				*dst++ = L'\\';
    222 				*dst++ = c;
    223 				return dst;
    224 			}
    225 		}
    226 	}
    227 	if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
    228 		*dst++ = L'\\';
    229 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
    230 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
    231 		*dst++ =			     (c	      & 07) + L'0';
    232 	} else {
    233 		if ((flags & VIS_NOSLASH) == 0)
    234 			*dst++ = L'\\';
    235 
    236 		if (c & 0200) {
    237 			c &= 0177;
    238 			*dst++ = L'M';
    239 		}
    240 
    241 		if (iswcntrl(c)) {
    242 			*dst++ = L'^';
    243 			if (c == 0177)
    244 				*dst++ = L'?';
    245 			else
    246 				*dst++ = c + L'@';
    247 		} else {
    248 			*dst++ = L'-';
    249 			*dst++ = c;
    250 		}
    251 	}
    252 
    253 	return dst;
    254 }
    255 
    256 /*
    257  * This is do_vis, the central code of vis.
    258  * dst:	      Pointer to the destination buffer
    259  * c:	      Character to encode
    260  * flags:     Flags word
    261  * nextc:     The character following 'c'
    262  * extra:     Pointer to the list of extra characters to be
    263  *	      backslash-protected.
    264  */
    265 static wchar_t *
    266 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
    267 {
    268 	int iswextra, i, shft;
    269 	uint64_t bmsk, wmsk;
    270 
    271 	iswextra = wcschr(extra, c) != NULL;
    272 	if (!iswextra && (iswgraph(c) || iswwhite(c) ||
    273 	    ((flags & VIS_SAFE) && iswsafe(c)))) {
    274 		*dst++ = c;
    275 		return dst;
    276 	}
    277 
    278 	/* See comment in istrsenvisx() output loop, below. */
    279 	wmsk = 0;
    280 	for (i = sizeof(wmsk) - 1; i >= 0; i--) {
    281 		shft = i * NBBY;
    282 		bmsk = (uint64_t)0xffLL << shft;
    283 		wmsk |= bmsk;
    284 		if ((c & wmsk) || i == 0)
    285 			dst = do_mbyte(dst, (wint_t)(
    286 			    (uint64_t)(c & bmsk) >> shft),
    287 			    flags, nextc, iswextra);
    288 	}
    289 
    290 	return dst;
    291 }
    292 
    293 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
    294 
    295 /*
    296  * Return the appropriate encoding function depending on the flags given.
    297  */
    298 static visfun_t
    299 getvisfun(int flags)
    300 {
    301 	if (flags & VIS_HTTPSTYLE)
    302 		return do_hvis;
    303 	if (flags & VIS_MIMESTYLE)
    304 		return do_mvis;
    305 	return do_svis;
    306 }
    307 
    308 /*
    309  * Expand list of extra characters to not visually encode.
    310  */
    311 static wchar_t *
    312 makeextralist(int flags, const char *src)
    313 {
    314 	wchar_t *dst, *d;
    315 	size_t len;
    316 	const wchar_t *s;
    317 
    318 	len = strlen(src);
    319 	if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
    320 		return NULL;
    321 
    322 	if (mbstowcs(dst, src, len) == (size_t)-1) {
    323 		size_t i;
    324 		for (i = 0; i < len; i++)
    325 			dst[i] = (wchar_t)(u_char)src[i];
    326 		d = dst + len;
    327 	} else
    328 		d = dst + wcslen(dst);
    329 
    330 	if (flags & VIS_GLOB)
    331 		for (s = char_glob; *s; *d++ = *s++)
    332 			continue;
    333 
    334 	if (flags & VIS_SHELL)
    335 		for (s = char_shell; *s; *d++ = *s++)
    336 			continue;
    337 
    338 	if (flags & VIS_SP) *d++ = L' ';
    339 	if (flags & VIS_TAB) *d++ = L'\t';
    340 	if (flags & VIS_NL) *d++ = L'\n';
    341 	if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
    342 	*d = L'\0';
    343 
    344 	return dst;
    345 }
    346 
    347 /*
    348  * istrsenvisx()
    349  * 	The main internal function.
    350  *	All user-visible functions call this one.
    351  */
    352 static int
    353 istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
    354     int flags, const char *mbextra, int *cerr_ptr)
    355 {
    356 	wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
    357 	size_t len, olen;
    358 	uint64_t bmsk, wmsk;
    359 	wint_t c;
    360 	visfun_t f;
    361 	int clen = 0, cerr = 0, error = -1, i, shft;
    362 	ssize_t mbslength, maxolen;
    363 
    364 	_DIAGASSERT(mbdst != NULL);
    365 	_DIAGASSERT(mbsrc != NULL || mblength == 0);
    366 	_DIAGASSERT(mbextra != NULL);
    367 
    368 	/*
    369 	 * Input (mbsrc) is a char string considered to be multibyte
    370 	 * characters.  The input loop will read this string pulling
    371 	 * one character, possibly multiple bytes, from mbsrc and
    372 	 * converting each to wchar_t in src.
    373 	 *
    374 	 * The vis conversion will be done using the wide char
    375 	 * wchar_t string.
    376 	 *
    377 	 * This will then be converted back to a multibyte string to
    378 	 * return to the caller.
    379 	 */
    380 
    381 	/* Allocate space for the wide char strings */
    382 	psrc = pdst = extra = NULL;
    383 	if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
    384 		return -1;
    385 	if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
    386 		goto out;
    387 	dst = pdst;
    388 	src = psrc;
    389 
    390 	/* Use caller's multibyte conversion error flag. */
    391 	if (cerr_ptr)
    392 		cerr = *cerr_ptr;
    393 
    394 	/*
    395 	 * Input loop.
    396 	 * Handle up to mblength characters (not bytes).  We do not
    397 	 * stop at NULs because we may be processing a block of data
    398 	 * that includes NULs.
    399 	 */
    400 	mbslength = (ssize_t)mblength;
    401 	/*
    402 	 * When inputing a single character, must also read in the
    403 	 * next character for nextc, the look-ahead character.
    404 	 */
    405 	if (mbslength == 1)
    406 		mbslength++;
    407 	while (mbslength > 0) {
    408 		/* Convert one multibyte character to wchar_t. */
    409 		if (!cerr)
    410 			clen = mbtowc(src, mbsrc, MB_LEN_MAX);
    411 		if (cerr || clen < 0) {
    412 			/* Conversion error, process as a byte instead. */
    413 			*src = (wint_t)(u_char)*mbsrc;
    414 			clen = 1;
    415 			cerr = 1;
    416 		}
    417 		if (clen == 0)
    418 			/*
    419 			 * NUL in input gives 0 return value. process
    420 			 * as single NUL byte and keep going.
    421 			 */
    422 			clen = 1;
    423 		/* Advance buffer character pointer. */
    424 		src++;
    425 		/* Advance input pointer by number of bytes read. */
    426 		mbsrc += clen;
    427 		/* Decrement input byte count. */
    428 		mbslength -= clen;
    429 	}
    430 	len = src - psrc;
    431 	src = psrc;
    432 	/*
    433 	 * In the single character input case, we will have actually
    434 	 * processed two characters, c and nextc.  Reset len back to
    435 	 * just a single character.
    436 	 */
    437 	if (mblength < len)
    438 		len = mblength;
    439 
    440 	/* Convert extra argument to list of characters for this mode. */
    441 	extra = makeextralist(flags, mbextra);
    442 	if (!extra) {
    443 		if (dlen && *dlen == 0) {
    444 			errno = ENOSPC;
    445 			goto out;
    446 		}
    447 		*mbdst = '\0';		/* can't create extra, return "" */
    448 		error = 0;
    449 		goto out;
    450 	}
    451 
    452 	/* Look up which processing function to call. */
    453 	f = getvisfun(flags);
    454 
    455 	/*
    456 	 * Main processing loop.
    457 	 * Call do_Xvis processing function one character at a time
    458 	 * with next character available for look-ahead.
    459 	 */
    460 	for (start = dst; len > 0; len--) {
    461 		c = *src++;
    462 		dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
    463 		if (dst == NULL) {
    464 			errno = ENOSPC;
    465 			goto out;
    466 		}
    467 	}
    468 
    469 	/* Terminate the string in the buffer. */
    470 	*dst = L'\0';
    471 
    472 	/*
    473 	 * Output loop.
    474 	 * Convert wchar_t string back to multibyte output string.
    475 	 * If we have hit a multi-byte conversion error on input,
    476 	 * output byte-by-byte here.  Else use wctomb().
    477 	 */
    478 	len = wcslen(start);
    479 	maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
    480 	olen = 0;
    481 	for (dst = start; len > 0; len--) {
    482 		if (!cerr)
    483 			clen = wctomb(mbdst, *dst);
    484 		if (cerr || clen < 0) {
    485 			/*
    486 			 * Conversion error, process as a byte(s) instead.
    487 			 * Examine each byte and higher-order bytes for
    488 			 * data.  E.g.,
    489 			 *	0x000000000000a264 -> a2 64
    490 			 *	0x000000001f00a264 -> 1f 00 a2 64
    491 			 */
    492 			clen = 0;
    493 			wmsk = 0;
    494 			for (i = sizeof(wmsk) - 1; i >= 0; i--) {
    495 				shft = i * NBBY;
    496 				bmsk = (uint64_t)0xffLL << shft;
    497 				wmsk |= bmsk;
    498 				if ((*dst & wmsk) || i == 0)
    499 					mbdst[clen++] = (char)(
    500 					    (uint64_t)(*dst & bmsk) >>
    501 					    shft);
    502 			}
    503 			cerr = 1;
    504 		}
    505 		/* If this character would exceed our output limit, stop. */
    506 		if (olen + clen > (size_t)maxolen)
    507 			break;
    508 		/* Advance output pointer by number of bytes written. */
    509 		mbdst += clen;
    510 		/* Advance buffer character pointer. */
    511 		dst++;
    512 		/* Incrment output character count. */
    513 		olen += clen;
    514 	}
    515 
    516 	/* Terminate the output string. */
    517 	*mbdst = '\0';
    518 
    519 	/* Pass conversion error flag out. */
    520 	if (cerr_ptr)
    521 		*cerr_ptr = cerr;
    522 
    523 	free(extra);
    524 	free(pdst);
    525 	free(psrc);
    526 
    527 	return (int)olen;
    528 out:
    529 	free(extra);
    530 	free(pdst);
    531 	free(psrc);
    532 	return error;
    533 }
    534 
    535 static int
    536 istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
    537     int flags, const char *mbextra, int *cerr_ptr)
    538 {
    539 	return istrsenvisx(mbdst, dlen, mbsrc,
    540 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
    541 }
    542 
    543 #endif
    544 
    545 #if !HAVE_SVIS
    546 /*
    547  *	The "svis" variants all take an "extra" arg that is a pointer
    548  *	to a NUL-terminated list of characters to be encoded, too.
    549  *	These functions are useful e. g. to encode strings in such a
    550  *	way so that they are not interpreted by a shell.
    551  */
    552 
    553 char *
    554 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
    555 {
    556 	char cc[2];
    557 	int ret;
    558 
    559 	cc[0] = c;
    560 	cc[1] = nextc;
    561 
    562 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL);
    563 	if (ret < 0)
    564 		return NULL;
    565 	return mbdst + ret;
    566 }
    567 
    568 char *
    569 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
    570 {
    571 	char cc[2];
    572 	int ret;
    573 
    574 	cc[0] = c;
    575 	cc[1] = nextc;
    576 
    577 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL);
    578 	if (ret < 0)
    579 		return NULL;
    580 	return mbdst + ret;
    581 }
    582 
    583 int
    584 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
    585 {
    586 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL);
    587 }
    588 
    589 int
    590 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
    591 {
    592 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL);
    593 }
    594 
    595 int
    596 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
    597 {
    598 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
    599 }
    600 
    601 int
    602 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    603     const char *mbextra)
    604 {
    605 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
    606 }
    607 
    608 int
    609 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    610     const char *mbextra, int *cerr_ptr)
    611 {
    612 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
    613 }
    614 #endif
    615 
    616 #if !HAVE_VIS
    617 /*
    618  * vis - visually encode characters
    619  */
    620 char *
    621 vis(char *mbdst, int c, int flags, int nextc)
    622 {
    623 	char cc[2];
    624 	int ret;
    625 
    626 	cc[0] = c;
    627 	cc[1] = nextc;
    628 
    629 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL);
    630 	if (ret < 0)
    631 		return NULL;
    632 	return mbdst + ret;
    633 }
    634 
    635 char *
    636 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
    637 {
    638 	char cc[2];
    639 	int ret;
    640 
    641 	cc[0] = c;
    642 	cc[1] = nextc;
    643 
    644 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL);
    645 	if (ret < 0)
    646 		return NULL;
    647 	return mbdst + ret;
    648 }
    649 
    650 /*
    651  * strvis - visually encode characters from src into dst
    652  *
    653  *	Dst must be 4 times the size of src to account for possible
    654  *	expansion.  The length of dst, not including the trailing NULL,
    655  *	is returned.
    656  */
    657 
    658 int
    659 strvis(char *mbdst, const char *mbsrc, int flags)
    660 {
    661 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL);
    662 }
    663 
    664 int
    665 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
    666 {
    667 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
    668 }
    669 
    670 /*
    671  * strvisx - visually encode characters from src into dst
    672  *
    673  *	Dst must be 4 times the size of src to account for possible
    674  *	expansion.  The length of dst, not including the trailing NULL,
    675  *	is returned.
    676  *
    677  *	Strvisx encodes exactly len characters from src into dst.
    678  *	This is useful for encoding a block of data.
    679  */
    680 
    681 int
    682 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
    683 {
    684 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL);
    685 }
    686 
    687 int
    688 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
    689 {
    690 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL);
    691 }
    692 
    693 int
    694 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    695     int *cerr_ptr)
    696 {
    697 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
    698 }
    699 #endif
    700