Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.63
      1 /*	$NetBSD: vis.c,v 1.63 2014/09/26 01:21:07 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.63 2014/09/26 01:21:07 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 
    317 	len = strlen(src);
    318 	if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
    319 		return NULL;
    320 
    321 	if (mbstowcs(dst, src, len) == (size_t)-1) {
    322 		size_t i;
    323 		for (i = 0; i < len; i++)
    324 			dst[i] = (wchar_t)(u_char)src[i];
    325 		d = dst + len;
    326 	} else
    327 		d = dst + wcslen(dst);
    328 
    329 	if (flags & VIS_GLOB)
    330 		for (const wchar_t *s = char_glob; *s; *d++ = *s++)
    331 			continue;
    332 
    333 	if (flags & VIS_SHELL)
    334 		for (const wchar_t *s = char_shell; *s; *d++ = *s++)
    335 			continue;
    336 
    337 	if (flags & VIS_SP) *d++ = L' ';
    338 	if (flags & VIS_TAB) *d++ = L'\t';
    339 	if (flags & VIS_NL) *d++ = L'\n';
    340 	if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
    341 	*d = L'\0';
    342 
    343 	return dst;
    344 }
    345 
    346 /*
    347  * istrsenvisx()
    348  * 	The main internal function.
    349  *	All user-visible functions call this one.
    350  */
    351 static int
    352 istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
    353     int flags, const char *mbextra, int *cerr_ptr)
    354 {
    355 	wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
    356 	size_t len, olen;
    357 	uint64_t bmsk, wmsk;
    358 	wint_t c;
    359 	visfun_t f;
    360 	int clen = 0, cerr = 0, error = -1, i, shft;
    361 	ssize_t mbslength, maxolen;
    362 
    363 	_DIAGASSERT(mbdst != NULL);
    364 	_DIAGASSERT(mbsrc != NULL || mblength == 0);
    365 	_DIAGASSERT(mbextra != NULL);
    366 
    367 	/*
    368 	 * Input (mbsrc) is a char string considered to be multibyte
    369 	 * characters.  The input loop will read this string pulling
    370 	 * one character, possibly multiple bytes, from mbsrc and
    371 	 * converting each to wchar_t in src.
    372 	 *
    373 	 * The vis conversion will be done using the wide char
    374 	 * wchar_t string.
    375 	 *
    376 	 * This will then be converted back to a multibyte string to
    377 	 * return to the caller.
    378 	 */
    379 
    380 	/* Allocate space for the wide char strings */
    381 	psrc = pdst = extra = NULL;
    382 	if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
    383 		return -1;
    384 	if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
    385 		goto out;
    386 	dst = pdst;
    387 	src = psrc;
    388 
    389 	/* Use caller's multibyte conversion error flag. */
    390 	if (cerr_ptr)
    391 		cerr = *cerr_ptr;
    392 
    393 	/*
    394 	 * Input loop.
    395 	 * Handle up to mblength characters (not bytes).  We do not
    396 	 * stop at NULs because we may be processing a block of data
    397 	 * that includes NULs.
    398 	 */
    399 	mbslength = (ssize_t)mblength;
    400 	/*
    401 	 * When inputing a single character, must also read in the
    402 	 * next character for nextc, the look-ahead character.
    403 	 */
    404 	if (mbslength == 1)
    405 		mbslength++;
    406 	while (mbslength > 0) {
    407 		/* Convert one multibyte character to wchar_t. */
    408 		if (!cerr)
    409 			clen = mbtowc(src, mbsrc, MB_LEN_MAX);
    410 		if (cerr || clen < 0) {
    411 			/* Conversion error, process as a byte instead. */
    412 			*src = (wint_t)(u_char)*mbsrc;
    413 			clen = 1;
    414 			cerr = 1;
    415 		}
    416 		if (clen == 0)
    417 			/*
    418 			 * NUL in input gives 0 return value. process
    419 			 * as single NUL byte and keep going.
    420 			 */
    421 			clen = 1;
    422 		/* Advance buffer character pointer. */
    423 		src++;
    424 		/* Advance input pointer by number of bytes read. */
    425 		mbsrc += clen;
    426 		/* Decrement input byte count. */
    427 		mbslength -= clen;
    428 	}
    429 	len = src - psrc;
    430 	src = psrc;
    431 	/*
    432 	 * In the single character input case, we will have actually
    433 	 * processed two characters, c and nextc.  Reset len back to
    434 	 * just a single character.
    435 	 */
    436 	if (mblength < len)
    437 		len = mblength;
    438 
    439 	/* Convert extra argument to list of characters for this mode. */
    440 	extra = makeextralist(flags, mbextra);
    441 	if (!extra) {
    442 		if (dlen && *dlen == 0) {
    443 			errno = ENOSPC;
    444 			goto out;
    445 		}
    446 		*mbdst = '\0';		/* can't create extra, return "" */
    447 		error = 0;
    448 		goto out;
    449 	}
    450 
    451 	/* Look up which processing function to call. */
    452 	f = getvisfun(flags);
    453 
    454 	/*
    455 	 * Main processing loop.
    456 	 * Call do_Xvis processing function one character at a time
    457 	 * with next character available for look-ahead.
    458 	 */
    459 	for (start = dst; len > 0; len--) {
    460 		c = *src++;
    461 		dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
    462 		if (dst == NULL) {
    463 			errno = ENOSPC;
    464 			goto out;
    465 		}
    466 	}
    467 
    468 	/* Terminate the string in the buffer. */
    469 	*dst = L'\0';
    470 
    471 	/*
    472 	 * Output loop.
    473 	 * Convert wchar_t string back to multibyte output string.
    474 	 * If we have hit a multi-byte conversion error on input,
    475 	 * output byte-by-byte here.  Else use wctomb().
    476 	 */
    477 	len = wcslen(start);
    478 	maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
    479 	olen = 0;
    480 	for (dst = start; len > 0; len--) {
    481 		if (!cerr)
    482 			clen = wctomb(mbdst, *dst);
    483 		if (cerr || clen < 0) {
    484 			/*
    485 			 * Conversion error, process as a byte(s) instead.
    486 			 * Examine each byte and higher-order bytes for
    487 			 * data.  E.g.,
    488 			 *	0x000000000000a264 -> a2 64
    489 			 *	0x000000001f00a264 -> 1f 00 a2 64
    490 			 */
    491 			clen = 0;
    492 			wmsk = 0;
    493 			for (i = sizeof(wmsk) - 1; i >= 0; i--) {
    494 				shft = i * NBBY;
    495 				bmsk = (uint64_t)0xffLL << shft;
    496 				wmsk |= bmsk;
    497 				if ((*dst & wmsk) || i == 0)
    498 					mbdst[clen++] = (char)(
    499 					    (uint64_t)(*dst & bmsk) >>
    500 					    shft);
    501 			}
    502 			cerr = 1;
    503 		}
    504 		/* If this character would exceed our output limit, stop. */
    505 		if (olen + clen > (size_t)maxolen)
    506 			break;
    507 		/* Advance output pointer by number of bytes written. */
    508 		mbdst += clen;
    509 		/* Advance buffer character pointer. */
    510 		dst++;
    511 		/* Incrment output character count. */
    512 		olen += clen;
    513 	}
    514 
    515 	/* Terminate the output string. */
    516 	*mbdst = '\0';
    517 
    518 	/* Pass conversion error flag out. */
    519 	if (cerr_ptr)
    520 		*cerr_ptr = cerr;
    521 
    522 	free(extra);
    523 	free(pdst);
    524 	free(psrc);
    525 
    526 	return (int)olen;
    527 out:
    528 	free(extra);
    529 	free(pdst);
    530 	free(psrc);
    531 	return error;
    532 }
    533 
    534 static int
    535 istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
    536     int flags, const char *mbextra, int *cerr_ptr)
    537 {
    538 	return istrsenvisx(mbdst, dlen, mbsrc,
    539 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
    540 }
    541 
    542 #endif
    543 
    544 #if !HAVE_SVIS
    545 /*
    546  *	The "svis" variants all take an "extra" arg that is a pointer
    547  *	to a NUL-terminated list of characters to be encoded, too.
    548  *	These functions are useful e. g. to encode strings in such a
    549  *	way so that they are not interpreted by a shell.
    550  */
    551 
    552 char *
    553 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
    554 {
    555 	char cc[2];
    556 	int ret;
    557 
    558 	cc[0] = c;
    559 	cc[1] = nextc;
    560 
    561 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL);
    562 	if (ret < 0)
    563 		return NULL;
    564 	return mbdst + ret;
    565 }
    566 
    567 char *
    568 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
    569 {
    570 	char cc[2];
    571 	int ret;
    572 
    573 	cc[0] = c;
    574 	cc[1] = nextc;
    575 
    576 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL);
    577 	if (ret < 0)
    578 		return NULL;
    579 	return mbdst + ret;
    580 }
    581 
    582 int
    583 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
    584 {
    585 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL);
    586 }
    587 
    588 int
    589 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
    590 {
    591 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL);
    592 }
    593 
    594 int
    595 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
    596 {
    597 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
    598 }
    599 
    600 int
    601 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    602     const char *mbextra)
    603 {
    604 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
    605 }
    606 
    607 int
    608 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    609     const char *mbextra, int *cerr_ptr)
    610 {
    611 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
    612 }
    613 #endif
    614 
    615 #if !HAVE_VIS
    616 /*
    617  * vis - visually encode characters
    618  */
    619 char *
    620 vis(char *mbdst, int c, int flags, int nextc)
    621 {
    622 	char cc[2];
    623 	int ret;
    624 
    625 	cc[0] = c;
    626 	cc[1] = nextc;
    627 
    628 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL);
    629 	if (ret < 0)
    630 		return NULL;
    631 	return mbdst + ret;
    632 }
    633 
    634 char *
    635 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
    636 {
    637 	char cc[2];
    638 	int ret;
    639 
    640 	cc[0] = c;
    641 	cc[1] = nextc;
    642 
    643 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL);
    644 	if (ret < 0)
    645 		return NULL;
    646 	return mbdst + ret;
    647 }
    648 
    649 /*
    650  * strvis - visually encode characters from src into dst
    651  *
    652  *	Dst must be 4 times the size of src to account for possible
    653  *	expansion.  The length of dst, not including the trailing NULL,
    654  *	is returned.
    655  */
    656 
    657 int
    658 strvis(char *mbdst, const char *mbsrc, int flags)
    659 {
    660 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL);
    661 }
    662 
    663 int
    664 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
    665 {
    666 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
    667 }
    668 
    669 /*
    670  * strvisx - visually encode characters from src into dst
    671  *
    672  *	Dst must be 4 times the size of src to account for possible
    673  *	expansion.  The length of dst, not including the trailing NULL,
    674  *	is returned.
    675  *
    676  *	Strvisx encodes exactly len characters from src into dst.
    677  *	This is useful for encoding a block of data.
    678  */
    679 
    680 int
    681 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
    682 {
    683 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL);
    684 }
    685 
    686 int
    687 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
    688 {
    689 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL);
    690 }
    691 
    692 int
    693 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    694     int *cerr_ptr)
    695 {
    696 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
    697 }
    698 #endif
    699