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