Home | History | Annotate | Line # | Download | only in gen
vis.c revision 1.67
      1 /*	$NetBSD: vis.c,v 1.67 2015/05/23 11:47:56 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.67 2015/05/23 11:47:56 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 		/* We cannot encode these characters in VIS_CSTYLE
    220 		 * because they special meaning */
    221 		case L'n':
    222 		case L'r':
    223 		case L'b':
    224 		case L'a':
    225 		case L'v':
    226 		case L't':
    227 		case L'f':
    228 		case L's':
    229 		case L'0':
    230 		case L'M':
    231 		case L'^':
    232 		case L'$': /* vis(1) -l */
    233 			break;
    234 		default:
    235 			if (iswgraph(c) && !iswoctal(c)) {
    236 				*dst++ = L'\\';
    237 				*dst++ = c;
    238 				return dst;
    239 			}
    240 		}
    241 	}
    242 	if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
    243 		*dst++ = L'\\';
    244 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
    245 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
    246 		*dst++ =			     (c	      & 07) + L'0';
    247 	} else {
    248 		if ((flags & VIS_NOSLASH) == 0)
    249 			*dst++ = L'\\';
    250 
    251 		if (c & 0200) {
    252 			c &= 0177;
    253 			*dst++ = L'M';
    254 		}
    255 
    256 		if (iswcntrl(c)) {
    257 			*dst++ = L'^';
    258 			if (c == 0177)
    259 				*dst++ = L'?';
    260 			else
    261 				*dst++ = c + L'@';
    262 		} else {
    263 			*dst++ = L'-';
    264 			*dst++ = c;
    265 		}
    266 	}
    267 
    268 	return dst;
    269 }
    270 
    271 /*
    272  * This is do_vis, the central code of vis.
    273  * dst:	      Pointer to the destination buffer
    274  * c:	      Character to encode
    275  * flags:     Flags word
    276  * nextc:     The character following 'c'
    277  * extra:     Pointer to the list of extra characters to be
    278  *	      backslash-protected.
    279  */
    280 static wchar_t *
    281 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
    282 {
    283 	int iswextra, i, shft;
    284 	uint64_t bmsk, wmsk;
    285 
    286 	iswextra = wcschr(extra, c) != NULL;
    287 	if (!iswextra && (iswgraph(c) || iswwhite(c) ||
    288 	    ((flags & VIS_SAFE) && iswsafe(c)))) {
    289 		*dst++ = c;
    290 		return dst;
    291 	}
    292 
    293 	/* See comment in istrsenvisx() output loop, below. */
    294 	wmsk = 0;
    295 	for (i = sizeof(wmsk) - 1; i >= 0; i--) {
    296 		shft = i * NBBY;
    297 		bmsk = (uint64_t)0xffLL << shft;
    298 		wmsk |= bmsk;
    299 		if ((c & wmsk) || i == 0)
    300 			dst = do_mbyte(dst, (wint_t)(
    301 			    (uint64_t)(c & bmsk) >> shft),
    302 			    flags, nextc, iswextra);
    303 	}
    304 
    305 	return dst;
    306 }
    307 
    308 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
    309 
    310 /*
    311  * Return the appropriate encoding function depending on the flags given.
    312  */
    313 static visfun_t
    314 getvisfun(int flags)
    315 {
    316 	if (flags & VIS_HTTPSTYLE)
    317 		return do_hvis;
    318 	if (flags & VIS_MIMESTYLE)
    319 		return do_mvis;
    320 	return do_svis;
    321 }
    322 
    323 /*
    324  * Expand list of extra characters to not visually encode.
    325  */
    326 static wchar_t *
    327 makeextralist(int flags, const char *src)
    328 {
    329 	wchar_t *dst, *d;
    330 	size_t len;
    331 	const wchar_t *s;
    332 
    333 	len = strlen(src);
    334 	if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
    335 		return NULL;
    336 
    337 	if ((flags & VIS_NOLOCALE) || mbstowcs(dst, src, len) == (size_t)-1) {
    338 		size_t i;
    339 		for (i = 0; i < len; i++)
    340 			dst[i] = (wchar_t)(u_char)src[i];
    341 		d = dst + len;
    342 	} else
    343 		d = dst + wcslen(dst);
    344 
    345 	if (flags & VIS_GLOB)
    346 		for (s = char_glob; *s; *d++ = *s++)
    347 			continue;
    348 
    349 	if (flags & VIS_SHELL)
    350 		for (s = char_shell; *s; *d++ = *s++)
    351 			continue;
    352 
    353 	if (flags & VIS_SP) *d++ = L' ';
    354 	if (flags & VIS_TAB) *d++ = L'\t';
    355 	if (flags & VIS_NL) *d++ = L'\n';
    356 	if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
    357 	*d = L'\0';
    358 
    359 	return dst;
    360 }
    361 
    362 /*
    363  * istrsenvisx()
    364  * 	The main internal function.
    365  *	All user-visible functions call this one.
    366  */
    367 static int
    368 istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
    369     int flags, const char *mbextra, int *cerr_ptr)
    370 {
    371 	wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
    372 	size_t len, olen;
    373 	uint64_t bmsk, wmsk;
    374 	wint_t c;
    375 	visfun_t f;
    376 	int clen = 0, cerr, error = -1, i, shft;
    377 	ssize_t mbslength, maxolen;
    378 
    379 	_DIAGASSERT(mbdst != NULL);
    380 	_DIAGASSERT(mbsrc != NULL || mblength == 0);
    381 	_DIAGASSERT(mbextra != NULL);
    382 
    383 	/*
    384 	 * Input (mbsrc) is a char string considered to be multibyte
    385 	 * characters.  The input loop will read this string pulling
    386 	 * one character, possibly multiple bytes, from mbsrc and
    387 	 * converting each to wchar_t in src.
    388 	 *
    389 	 * The vis conversion will be done using the wide char
    390 	 * wchar_t string.
    391 	 *
    392 	 * This will then be converted back to a multibyte string to
    393 	 * return to the caller.
    394 	 */
    395 
    396 	/* Allocate space for the wide char strings */
    397 	psrc = pdst = extra = NULL;
    398 	if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
    399 		return -1;
    400 	if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
    401 		goto out;
    402 	dst = pdst;
    403 	src = psrc;
    404 
    405 	if (flags & VIS_NOLOCALE) {
    406 		/* Do one byte at a time conversion */
    407 		cerr = 1;
    408 	} else {
    409 		/* Use caller's multibyte conversion error flag. */
    410 		cerr = cerr_ptr ? *cerr_ptr : 0;
    411 	}
    412 
    413 	/*
    414 	 * Input loop.
    415 	 * Handle up to mblength characters (not bytes).  We do not
    416 	 * stop at NULs because we may be processing a block of data
    417 	 * that includes NULs.
    418 	 */
    419 	mbslength = (ssize_t)mblength;
    420 	/*
    421 	 * When inputing a single character, must also read in the
    422 	 * next character for nextc, the look-ahead character.
    423 	 */
    424 	if (mbslength == 1)
    425 		mbslength++;
    426 	while (mbslength > 0) {
    427 		/* Convert one multibyte character to wchar_t. */
    428 		if (!cerr)
    429 			clen = mbtowc(src, mbsrc, MB_LEN_MAX);
    430 		if (cerr || clen < 0) {
    431 			/* Conversion error, process as a byte instead. */
    432 			*src = (wint_t)(u_char)*mbsrc;
    433 			clen = 1;
    434 			cerr = 1;
    435 		}
    436 		if (clen == 0)
    437 			/*
    438 			 * NUL in input gives 0 return value. process
    439 			 * as single NUL byte and keep going.
    440 			 */
    441 			clen = 1;
    442 		/* Advance buffer character pointer. */
    443 		src++;
    444 		/* Advance input pointer by number of bytes read. */
    445 		mbsrc += clen;
    446 		/* Decrement input byte count. */
    447 		mbslength -= clen;
    448 	}
    449 	len = src - psrc;
    450 	src = psrc;
    451 	/*
    452 	 * In the single character input case, we will have actually
    453 	 * processed two characters, c and nextc.  Reset len back to
    454 	 * just a single character.
    455 	 */
    456 	if (mblength < len)
    457 		len = mblength;
    458 
    459 	/* Convert extra argument to list of characters for this mode. */
    460 	extra = makeextralist(flags, mbextra);
    461 	if (!extra) {
    462 		if (dlen && *dlen == 0) {
    463 			errno = ENOSPC;
    464 			goto out;
    465 		}
    466 		*mbdst = '\0';		/* can't create extra, return "" */
    467 		error = 0;
    468 		goto out;
    469 	}
    470 
    471 	/* Look up which processing function to call. */
    472 	f = getvisfun(flags);
    473 
    474 	/*
    475 	 * Main processing loop.
    476 	 * Call do_Xvis processing function one character at a time
    477 	 * with next character available for look-ahead.
    478 	 */
    479 	for (start = dst; len > 0; len--) {
    480 		c = *src++;
    481 		dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
    482 		if (dst == NULL) {
    483 			errno = ENOSPC;
    484 			goto out;
    485 		}
    486 	}
    487 
    488 	/* Terminate the string in the buffer. */
    489 	*dst = L'\0';
    490 
    491 	/*
    492 	 * Output loop.
    493 	 * Convert wchar_t string back to multibyte output string.
    494 	 * If we have hit a multi-byte conversion error on input,
    495 	 * output byte-by-byte here.  Else use wctomb().
    496 	 */
    497 	len = wcslen(start);
    498 	maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
    499 	olen = 0;
    500 	for (dst = start; len > 0; len--) {
    501 		if (!cerr)
    502 			clen = wctomb(mbdst, *dst);
    503 		if (cerr || clen < 0) {
    504 			/*
    505 			 * Conversion error, process as a byte(s) instead.
    506 			 * Examine each byte and higher-order bytes for
    507 			 * data.  E.g.,
    508 			 *	0x000000000000a264 -> a2 64
    509 			 *	0x000000001f00a264 -> 1f 00 a2 64
    510 			 */
    511 			clen = 0;
    512 			wmsk = 0;
    513 			for (i = sizeof(wmsk) - 1; i >= 0; i--) {
    514 				shft = i * NBBY;
    515 				bmsk = (uint64_t)0xffLL << shft;
    516 				wmsk |= bmsk;
    517 				if ((*dst & wmsk) || i == 0)
    518 					mbdst[clen++] = (char)(
    519 					    (uint64_t)(*dst & bmsk) >>
    520 					    shft);
    521 			}
    522 			cerr = 1;
    523 		}
    524 		/* If this character would exceed our output limit, stop. */
    525 		if (olen + clen > (size_t)maxolen)
    526 			break;
    527 		/* Advance output pointer by number of bytes written. */
    528 		mbdst += clen;
    529 		/* Advance buffer character pointer. */
    530 		dst++;
    531 		/* Incrment output character count. */
    532 		olen += clen;
    533 	}
    534 
    535 	/* Terminate the output string. */
    536 	*mbdst = '\0';
    537 
    538 	if (flags & VIS_NOLOCALE) {
    539 		/* Pass conversion error flag out. */
    540 		if (cerr_ptr)
    541 			*cerr_ptr = cerr;
    542 	}
    543 
    544 	free(extra);
    545 	free(pdst);
    546 	free(psrc);
    547 
    548 	return (int)olen;
    549 out:
    550 	free(extra);
    551 	free(pdst);
    552 	free(psrc);
    553 	return error;
    554 }
    555 
    556 static int
    557 istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
    558     int flags, const char *mbextra, int *cerr_ptr)
    559 {
    560 	return istrsenvisx(mbdst, dlen, mbsrc,
    561 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
    562 }
    563 
    564 #endif
    565 
    566 #if !HAVE_SVIS
    567 /*
    568  *	The "svis" variants all take an "extra" arg that is a pointer
    569  *	to a NUL-terminated list of characters to be encoded, too.
    570  *	These functions are useful e. g. to encode strings in such a
    571  *	way so that they are not interpreted by a shell.
    572  */
    573 
    574 char *
    575 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
    576 {
    577 	char cc[2];
    578 	int ret;
    579 
    580 	cc[0] = c;
    581 	cc[1] = nextc;
    582 
    583 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL);
    584 	if (ret < 0)
    585 		return NULL;
    586 	return mbdst + ret;
    587 }
    588 
    589 char *
    590 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
    591 {
    592 	char cc[2];
    593 	int ret;
    594 
    595 	cc[0] = c;
    596 	cc[1] = nextc;
    597 
    598 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL);
    599 	if (ret < 0)
    600 		return NULL;
    601 	return mbdst + ret;
    602 }
    603 
    604 int
    605 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
    606 {
    607 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL);
    608 }
    609 
    610 int
    611 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
    612 {
    613 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL);
    614 }
    615 
    616 int
    617 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
    618 {
    619 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
    620 }
    621 
    622 int
    623 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    624     const char *mbextra)
    625 {
    626 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
    627 }
    628 
    629 int
    630 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    631     const char *mbextra, int *cerr_ptr)
    632 {
    633 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
    634 }
    635 #endif
    636 
    637 #if !HAVE_VIS
    638 /*
    639  * vis - visually encode characters
    640  */
    641 char *
    642 vis(char *mbdst, int c, int flags, int nextc)
    643 {
    644 	char cc[2];
    645 	int ret;
    646 
    647 	cc[0] = c;
    648 	cc[1] = nextc;
    649 
    650 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL);
    651 	if (ret < 0)
    652 		return NULL;
    653 	return mbdst + ret;
    654 }
    655 
    656 char *
    657 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
    658 {
    659 	char cc[2];
    660 	int ret;
    661 
    662 	cc[0] = c;
    663 	cc[1] = nextc;
    664 
    665 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL);
    666 	if (ret < 0)
    667 		return NULL;
    668 	return mbdst + ret;
    669 }
    670 
    671 /*
    672  * strvis - visually encode characters from src into dst
    673  *
    674  *	Dst must be 4 times the size of src to account for possible
    675  *	expansion.  The length of dst, not including the trailing NULL,
    676  *	is returned.
    677  */
    678 
    679 int
    680 strvis(char *mbdst, const char *mbsrc, int flags)
    681 {
    682 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL);
    683 }
    684 
    685 int
    686 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
    687 {
    688 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
    689 }
    690 
    691 /*
    692  * strvisx - visually encode characters from src into dst
    693  *
    694  *	Dst must be 4 times the size of src to account for possible
    695  *	expansion.  The length of dst, not including the trailing NULL,
    696  *	is returned.
    697  *
    698  *	Strvisx encodes exactly len characters from src into dst.
    699  *	This is useful for encoding a block of data.
    700  */
    701 
    702 int
    703 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
    704 {
    705 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL);
    706 }
    707 
    708 int
    709 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
    710 {
    711 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL);
    712 }
    713 
    714 int
    715 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
    716     int *cerr_ptr)
    717 {
    718 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
    719 }
    720 #endif
    721