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