xref: /dragonfly/lib/libc/gen/vis.c (revision 207ba670)
1 /*	$NetBSD: vis.c,v 1.74 2017/11/27 16:37:21 christos Exp $	*/
2 /*	$FreeBSD: head/contrib/libc-vis/vis.c 326307 2017-11-28 01:35:28Z brooks $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56  * POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 #include "namespace.h"
60 #include <sys/types.h>
61 #include <sys/param.h>
62 
63 #include <assert.h>
64 #include <vis.h>
65 #include <errno.h>
66 #include <stdlib.h>
67 #include <wchar.h>
68 #include <wctype.h>
69 #include "un-namespace.h"
70 
71 #ifdef __weak_alias
72 __weak_alias(strvisx,_strvisx)
73 #endif
74 
75 #if !HAVE_VIS || !HAVE_SVIS
76 #include "namespace.h"
77 #include <ctype.h>
78 #include <limits.h>
79 #include <stdio.h>
80 #include <string.h>
81 #include "un-namespace.h"
82 
83 /*
84  * The reason for going through the trouble to deal with character encodings
85  * in vis(3), is that we use this to safe encode output of commands. This
86  * safe encoding varies depending on the character set. For example if we
87  * display ps output in French, we don't want to display French characters
88  * as M-foo.
89  */
90 
91 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
92 
93 #undef BELL
94 #define BELL L'\a'
95 
96 #if defined(LC_C_LOCALE)
97 #define iscgraph(c)      isgraph_l(c, LC_C_LOCALE)
98 #else
99 /* Keep it simple for now, no locale stuff */
100 #define iscgraph(c)	isgraph(c)
101 #ifdef notyet
102 #include <locale.h>
103 static int
104 iscgraph(int c) {
105 	int rv;
106 	char *ol;
107 
108 	ol = setlocale(LC_CTYPE, "C");
109 	rv = isgraph(c);
110 	if (ol)
111 		setlocale(LC_CTYPE, ol);
112 	return rv;
113 }
114 #endif
115 #endif
116 
117 #define ISGRAPH(flags, c) \
118     (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c))
119 
120 #define iswoctal(c)	(((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
121 #define iswwhite(c)	(c == L' ' || c == L'\t' || c == L'\n')
122 #define iswsafe(c)	(c == L'\b' || c == BELL || c == L'\r')
123 #define xtoa(c)		L"0123456789abcdef"[c]
124 #define XTOA(c)		L"0123456789ABCDEF"[c]
125 
126 #define MAXEXTRAS	30
127 
128 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
129 static const wchar_t char_glob[] = L"*?[#";
130 
131 #if !HAVE_NBTOOL_CONFIG_H
132 #ifndef __NetBSD__
133 /*
134  * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
135  * integral type and it is probably wrong, since currently the maximum
136  * number of bytes and character needs is 6. Until this is fixed, the
137  * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
138  * the assertion is commented out.
139  */
140 #if defined(__FreeBSD__) || defined(__DragonFly__)
141 /*
142  * On FreeBSD and DragonFly, including <sys/systm.h> for CTASSERT only
143  * works in kernel mode.
144  */
145 #ifndef CTASSERT
146 #define CTASSERT(x)             _CTASSERT(x, __LINE__)
147 #define _CTASSERT(x, y)         __CTASSERT(x, y)
148 #define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
149 #endif
150 #endif /* __FreeBSD__ || __DragonFly__ */
151 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
152 #endif /* !__NetBSD__ */
153 #endif
154 
155 /*
156  * This is do_hvis, for HTTP style (RFC 1808)
157  */
158 static wchar_t *
159 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
160 {
161 	if (iswalnum(c)
162 	    /* safe */
163 	    || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
164 	    /* extra */
165 	    || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
166 	    || c == L',')
167 		dst = do_svis(dst, c, flags, nextc, extra);
168 	else {
169 		*dst++ = L'%';
170 		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
171 		*dst++ = xtoa((unsigned int)c & 0xf);
172 	}
173 
174 	return dst;
175 }
176 
177 /*
178  * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
179  * NB: No handling of long lines or CRLF.
180  */
181 static wchar_t *
182 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
183 {
184 	if ((c != L'\n') &&
185 	    /* Space at the end of the line */
186 	    ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
187 	    /* Out of range */
188 	    (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
189 	    /* Specific char to be escaped */
190 	    wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
191 		*dst++ = L'=';
192 		*dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
193 		*dst++ = XTOA((unsigned int)c & 0xf);
194 	} else
195 		dst = do_svis(dst, c, flags, nextc, extra);
196 	return dst;
197 }
198 
199 /*
200  * Output single byte of multibyte character.
201  */
202 static wchar_t *
203 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
204 {
205 	if (flags & VIS_CSTYLE) {
206 		switch (c) {
207 		case L'\n':
208 			*dst++ = L'\\'; *dst++ = L'n';
209 			return dst;
210 		case L'\r':
211 			*dst++ = L'\\'; *dst++ = L'r';
212 			return dst;
213 		case L'\b':
214 			*dst++ = L'\\'; *dst++ = L'b';
215 			return dst;
216 		case BELL:
217 			*dst++ = L'\\'; *dst++ = L'a';
218 			return dst;
219 		case L'\v':
220 			*dst++ = L'\\'; *dst++ = L'v';
221 			return dst;
222 		case L'\t':
223 			*dst++ = L'\\'; *dst++ = L't';
224 			return dst;
225 		case L'\f':
226 			*dst++ = L'\\'; *dst++ = L'f';
227 			return dst;
228 		case L' ':
229 			*dst++ = L'\\'; *dst++ = L's';
230 			return dst;
231 		case L'\0':
232 			*dst++ = L'\\'; *dst++ = L'0';
233 			if (iswoctal(nextc)) {
234 				*dst++ = L'0';
235 				*dst++ = L'0';
236 			}
237 			return dst;
238 		/* We cannot encode these characters in VIS_CSTYLE
239 		 * because they special meaning */
240 		case L'n':
241 		case L'r':
242 		case L'b':
243 		case L'a':
244 		case L'v':
245 		case L't':
246 		case L'f':
247 		case L's':
248 		case L'x':
249 		case L'0':
250 		case L'E':
251 		case L'F':
252 		case L'M':
253 		case L'-':
254 		case L'^':
255 		case L'$': /* vis(1) -l */
256 			break;
257 		default:
258 			if (ISGRAPH(flags, c) && !iswoctal(c)) {
259 				*dst++ = L'\\';
260 				*dst++ = c;
261 				return dst;
262 			}
263 		}
264 	}
265 	if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
266 		*dst++ = L'\\';
267 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
268 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
269 		*dst++ =			     (c	      & 07) + L'0';
270 	} else {
271 		if ((flags & VIS_NOSLASH) == 0)
272 			*dst++ = L'\\';
273 
274 		if (c & 0200) {
275 			c &= 0177;
276 			*dst++ = L'M';
277 		}
278 
279 		if (iswcntrl(c)) {
280 			*dst++ = L'^';
281 			if (c == 0177)
282 				*dst++ = L'?';
283 			else
284 				*dst++ = c + L'@';
285 		} else {
286 			*dst++ = L'-';
287 			*dst++ = c;
288 		}
289 	}
290 
291 	return dst;
292 }
293 
294 /*
295  * This is do_vis, the central code of vis.
296  * dst:	      Pointer to the destination buffer
297  * c:	      Character to encode
298  * flags:     Flags word
299  * nextc:     The character following 'c'
300  * extra:     Pointer to the list of extra characters to be
301  *	      backslash-protected.
302  */
303 static wchar_t *
304 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
305 {
306 	int iswextra, i, shft;
307 	uint64_t bmsk, wmsk;
308 
309 	iswextra = wcschr(extra, c) != NULL;
310 	if (((flags & VIS_ALL) == 0) &&
311 	    !iswextra &&
312 	    (ISGRAPH(flags, c) || iswwhite(c) ||
313 	     ((flags & VIS_SAFE) && iswsafe(c)))) {
314 		*dst++ = c;
315 		return dst;
316 	}
317 
318 	/* See comment in istrsenvisx() output loop, below. */
319 	wmsk = 0;
320 	for (i = sizeof(wmsk) - 1; i >= 0; i--) {
321 		shft = i * NBBY;
322 		bmsk = (uint64_t)0xffLL << shft;
323 		wmsk |= bmsk;
324 		if ((c & wmsk) || i == 0)
325 			dst = do_mbyte(dst, (wint_t)(
326 			    (uint64_t)(c & bmsk) >> shft),
327 			    flags, nextc, iswextra);
328 	}
329 
330 	return dst;
331 }
332 
333 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
334 
335 /*
336  * Return the appropriate encoding function depending on the flags given.
337  */
338 static visfun_t
339 getvisfun(int flags)
340 {
341 	if (flags & VIS_HTTPSTYLE)
342 		return do_hvis;
343 	if (flags & VIS_MIMESTYLE)
344 		return do_mvis;
345 	return do_svis;
346 }
347 
348 /*
349  * Expand list of extra characters to not visually encode.
350  */
351 static wchar_t *
352 makeextralist(int flags, const char *src)
353 {
354 	wchar_t *dst, *d;
355 	size_t len;
356 	const wchar_t *s;
357 	mbstate_t mbstate;
358 
359 	bzero(&mbstate, sizeof(mbstate));
360 	len = strlen(src);
361 	if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
362 		return NULL;
363 
364 	if ((flags & VIS_NOLOCALE) || 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 	ssize_t mbslength, maxolen;
407 	mbstate_t mbstate;
408 
409 	_DIAGASSERT(mbdstp != NULL);
410 	_DIAGASSERT(mbsrc != NULL || mblength == 0);
411 	_DIAGASSERT(mbextra != NULL);
412 
413 	mbslength = (ssize_t)mblength;
414 	/*
415 	 * When inputing a single character, must also read in the
416 	 * next character for nextc, the look-ahead character.
417 	 */
418 	if (mbslength == 1)
419 		mbslength++;
420 
421 	/*
422 	 * Input (mbsrc) is a char string considered to be multibyte
423 	 * characters.  The input loop will read this string pulling
424 	 * one character, possibly multiple bytes, from mbsrc and
425 	 * converting each to wchar_t in src.
426 	 *
427 	 * The vis conversion will be done using the wide char
428 	 * wchar_t string.
429 	 *
430 	 * This will then be converted back to a multibyte string to
431 	 * return to the caller.
432 	 */
433 
434 	/* Allocate space for the wide char strings */
435 	psrc = pdst = extra = NULL;
436 	mdst = NULL;
437 	if ((psrc = calloc(mbslength + 1, sizeof(*psrc))) == NULL)
438 		return -1;
439 	if ((pdst = calloc((16 * mbslength) + 1, sizeof(*pdst))) == NULL)
440 		goto out;
441 	if (*mbdstp == NULL) {
442 		if ((mdst = calloc((16 * mbslength) + 1, sizeof(*mdst))) == NULL)
443 			goto out;
444 		*mbdstp = mdst;
445 	}
446 
447 	mbdst = *mbdstp;
448 	dst = pdst;
449 	src = psrc;
450 
451 	if (flags & VIS_NOLOCALE) {
452 		/* Do one byte at a time conversion */
453 		cerr = 1;
454 	} else {
455 		/* Use caller's multibyte conversion error flag. */
456 		cerr = cerr_ptr ? *cerr_ptr : 0;
457 	}
458 
459 	/*
460 	 * Input loop.
461 	 * Handle up to mblength characters (not bytes).  We do not
462 	 * stop at NULs because we may be processing a block of data
463 	 * that includes NULs.
464 	 */
465 	bzero(&mbstate, sizeof(mbstate));
466 	while (mbslength > 0) {
467 		/* Convert one multibyte character to wchar_t. */
468 		if (!cerr)
469 			clen = mbrtowc(src, mbsrc, MB_LEN_MAX, &mbstate);
470 		if (cerr || clen < 0) {
471 			/* Conversion error, process as a byte instead. */
472 			*src = (wint_t)(u_char)*mbsrc;
473 			clen = 1;
474 			cerr = 1;
475 		}
476 		if (clen == 0) {
477 			/*
478 			 * NUL in input gives 0 return value. process
479 			 * as single NUL byte and keep going.
480 			 */
481 			clen = 1;
482 		}
483 		/* Advance buffer character pointer. */
484 		src++;
485 		/* Advance input pointer by number of bytes read. */
486 		mbsrc += clen;
487 		/* Decrement input byte count. */
488 		mbslength -= clen;
489 	}
490 	len = src - psrc;
491 	src = psrc;
492 
493 	/*
494 	 * In the single character input case, we will have actually
495 	 * processed two characters, c and nextc.  Reset len back to
496 	 * just a single character.
497 	 */
498 	if (mblength < len)
499 		len = mblength;
500 
501 	/* Convert extra argument to list of characters for this mode. */
502 	extra = makeextralist(flags, mbextra);
503 	if (!extra) {
504 		if (dlen && *dlen == 0) {
505 			errno = ENOSPC;
506 			goto out;
507 		}
508 		*mbdst = '\0';	/* can't create extra, return "" */
509 		error = 0;
510 		goto out;
511 	}
512 
513 	/* Look up which processing function to call. */
514 	f = getvisfun(flags);
515 
516 	/*
517 	 * Main processing loop.
518 	 * Call do_Xvis processing function one character at a time
519 	 * with next character available for look-ahead.
520 	 */
521 	for (start = dst; len > 0; len--) {
522 		c = *src++;
523 		dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
524 		if (dst == NULL) {
525 			errno = ENOSPC;
526 			goto out;
527 		}
528 	}
529 
530 	/* Terminate the string in the buffer. */
531 	*dst = L'\0';
532 
533 	/*
534 	 * Output loop.
535 	 * Convert wchar_t string back to multibyte output string.
536 	 * If we have hit a multi-byte conversion error on input,
537 	 * output byte-by-byte here.  Else use wctomb().
538 	 */
539 	len = wcslen(start);
540 	maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
541 	olen = 0;
542 	bzero(&mbstate, sizeof(mbstate));
543 	for (dst = start; len > 0; len--) {
544 		if (!cerr)
545 			clen = wcrtomb(mbdst, *dst, &mbstate);
546 		if (cerr || clen < 0) {
547 			/*
548 			 * Conversion error, process as a byte(s) instead.
549 			 * Examine each byte and higher-order bytes for
550 			 * data.  E.g.,
551 			 *	0x000000000000a264 -> a2 64
552 			 *	0x000000001f00a264 -> 1f 00 a2 64
553 			 */
554 			clen = 0;
555 			wmsk = 0;
556 			for (i = sizeof(wmsk) - 1; i >= 0; i--) {
557 				shft = i * NBBY;
558 				bmsk = (uint64_t)0xffLL << shft;
559 				wmsk |= bmsk;
560 				if ((*dst & wmsk) || i == 0)
561 					mbdst[clen++] = (char)(
562 					    (uint64_t)(*dst & bmsk) >>
563 					    shft);
564 			}
565 			cerr = 1;
566 		}
567 		/* If this character would exceed our output limit, stop. */
568 		if (olen + clen > (size_t)maxolen)
569 			break;
570 		/* Advance output pointer by number of bytes written. */
571 		mbdst += clen;
572 		/* Advance buffer character pointer. */
573 		dst++;
574 		/* Incrment output character count. */
575 		olen += clen;
576 	}
577 
578 	/* Terminate the output string. */
579 	*mbdst = '\0';
580 
581 	if (flags & VIS_NOLOCALE) {
582 		/* Pass conversion error flag out. */
583 		if (cerr_ptr)
584 			*cerr_ptr = cerr;
585 	}
586 
587 	free(extra);
588 	free(pdst);
589 	free(psrc);
590 
591 	return (int)olen;
592 out:
593 	free(extra);
594 	free(pdst);
595 	free(psrc);
596 	free(mdst);
597 	return error;
598 }
599 
600 static int
601 istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc,
602     int flags, const char *mbextra, int *cerr_ptr)
603 {
604 	return istrsenvisx(mbdstp, dlen, mbsrc,
605 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
606 }
607 
608 #endif
609 
610 #if !HAVE_SVIS
611 /*
612  *	The "svis" variants all take an "extra" arg that is a pointer
613  *	to a NUL-terminated list of characters to be encoded, too.
614  *	These functions are useful e. g. to encode strings in such a
615  *	way so that they are not interpreted by a shell.
616  */
617 
618 char *
619 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
620 {
621 	char cc[2];
622 	int ret;
623 
624 	cc[0] = c;
625 	cc[1] = nextc;
626 
627 	ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL);
628 	if (ret < 0)
629 		return NULL;
630 	return mbdst + ret;
631 }
632 
633 char *
634 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
635 {
636 	char cc[2];
637 	int ret;
638 
639 	cc[0] = c;
640 	cc[1] = nextc;
641 
642 	ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL);
643 	if (ret < 0)
644 		return NULL;
645 	return mbdst + ret;
646 }
647 
648 int
649 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
650 {
651 	return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL);
652 }
653 
654 int
655 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
656 {
657 	return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL);
658 }
659 
660 int
661 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
662 {
663 	return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
664 }
665 
666 int
667 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
668     const char *mbextra)
669 {
670 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
671 }
672 
673 int
674 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
675     const char *mbextra, int *cerr_ptr)
676 {
677 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
678 }
679 #endif
680 
681 #if !HAVE_VIS
682 /*
683  * vis - visually encode characters
684  */
685 char *
686 vis(char *mbdst, int c, int flags, int nextc)
687 {
688 	char cc[2];
689 	int ret;
690 
691 	cc[0] = c;
692 	cc[1] = nextc;
693 
694 	ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL);
695 	if (ret < 0)
696 		return NULL;
697 	return mbdst + ret;
698 }
699 
700 char *
701 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
702 {
703 	char cc[2];
704 	int ret;
705 
706 	cc[0] = c;
707 	cc[1] = nextc;
708 
709 	ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL);
710 	if (ret < 0)
711 		return NULL;
712 	return mbdst + ret;
713 }
714 
715 /*
716  * strvis - visually encode characters from src into dst
717  *
718  *	Dst must be 4 times the size of src to account for possible
719  *	expansion.  The length of dst, not including the trailing NULL,
720  *	is returned.
721  */
722 
723 int
724 strvis(char *mbdst, const char *mbsrc, int flags)
725 {
726 	return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL);
727 }
728 
729 int
730 strnvis(char *dst, const char *src, size_t len, int flag)
731 {
732 	return istrsenvisxl(&dst, &len, src, flag, "", NULL);
733 }
734 
735 int
736 stravis(char **mbdstp, const char *mbsrc, int flags)
737 {
738 	*mbdstp = NULL;
739 	return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL);
740 }
741 
742 /*
743  * strvisx - visually encode characters from src into dst
744  *
745  *	Dst must be 4 times the size of src to account for possible
746  *	expansion.  The length of dst, not including the trailing NULL,
747  *	is returned.
748  *
749  *	Strvisx encodes exactly len characters from src into dst.
750  *	This is useful for encoding a block of data.
751  */
752 
753 int
754 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
755 {
756 	return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL);
757 }
758 
759 int
760 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
761 {
762 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL);
763 }
764 
765 int
766 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
767     int *cerr_ptr)
768 {
769 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
770 }
771 #endif
772