xref: /netbsd-src/lib/libc/gen/vis.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: vis.c,v 1.66 2014/09/26 15:58:59 roy 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.66 2014/09/26 15:58:59 roy 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 (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 = 0, 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 	/* Use caller's multibyte conversion error flag. */
406 	if (cerr_ptr)
407 		cerr = *cerr_ptr;
408 
409 	/*
410 	 * Input loop.
411 	 * Handle up to mblength characters (not bytes).  We do not
412 	 * stop at NULs because we may be processing a block of data
413 	 * that includes NULs.
414 	 */
415 	mbslength = (ssize_t)mblength;
416 	/*
417 	 * When inputing a single character, must also read in the
418 	 * next character for nextc, the look-ahead character.
419 	 */
420 	if (mbslength == 1)
421 		mbslength++;
422 	while (mbslength > 0) {
423 		/* Convert one multibyte character to wchar_t. */
424 		if (!cerr)
425 			clen = mbtowc(src, mbsrc, MB_LEN_MAX);
426 		if (cerr || clen < 0) {
427 			/* Conversion error, process as a byte instead. */
428 			*src = (wint_t)(u_char)*mbsrc;
429 			clen = 1;
430 			cerr = 1;
431 		}
432 		if (clen == 0)
433 			/*
434 			 * NUL in input gives 0 return value. process
435 			 * as single NUL byte and keep going.
436 			 */
437 			clen = 1;
438 		/* Advance buffer character pointer. */
439 		src++;
440 		/* Advance input pointer by number of bytes read. */
441 		mbsrc += clen;
442 		/* Decrement input byte count. */
443 		mbslength -= clen;
444 	}
445 	len = src - psrc;
446 	src = psrc;
447 	/*
448 	 * In the single character input case, we will have actually
449 	 * processed two characters, c and nextc.  Reset len back to
450 	 * just a single character.
451 	 */
452 	if (mblength < len)
453 		len = mblength;
454 
455 	/* Convert extra argument to list of characters for this mode. */
456 	extra = makeextralist(flags, mbextra);
457 	if (!extra) {
458 		if (dlen && *dlen == 0) {
459 			errno = ENOSPC;
460 			goto out;
461 		}
462 		*mbdst = '\0';		/* can't create extra, return "" */
463 		error = 0;
464 		goto out;
465 	}
466 
467 	/* Look up which processing function to call. */
468 	f = getvisfun(flags);
469 
470 	/*
471 	 * Main processing loop.
472 	 * Call do_Xvis processing function one character at a time
473 	 * with next character available for look-ahead.
474 	 */
475 	for (start = dst; len > 0; len--) {
476 		c = *src++;
477 		dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
478 		if (dst == NULL) {
479 			errno = ENOSPC;
480 			goto out;
481 		}
482 	}
483 
484 	/* Terminate the string in the buffer. */
485 	*dst = L'\0';
486 
487 	/*
488 	 * Output loop.
489 	 * Convert wchar_t string back to multibyte output string.
490 	 * If we have hit a multi-byte conversion error on input,
491 	 * output byte-by-byte here.  Else use wctomb().
492 	 */
493 	len = wcslen(start);
494 	maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
495 	olen = 0;
496 	for (dst = start; len > 0; len--) {
497 		if (!cerr)
498 			clen = wctomb(mbdst, *dst);
499 		if (cerr || clen < 0) {
500 			/*
501 			 * Conversion error, process as a byte(s) instead.
502 			 * Examine each byte and higher-order bytes for
503 			 * data.  E.g.,
504 			 *	0x000000000000a264 -> a2 64
505 			 *	0x000000001f00a264 -> 1f 00 a2 64
506 			 */
507 			clen = 0;
508 			wmsk = 0;
509 			for (i = sizeof(wmsk) - 1; i >= 0; i--) {
510 				shft = i * NBBY;
511 				bmsk = (uint64_t)0xffLL << shft;
512 				wmsk |= bmsk;
513 				if ((*dst & wmsk) || i == 0)
514 					mbdst[clen++] = (char)(
515 					    (uint64_t)(*dst & bmsk) >>
516 					    shft);
517 			}
518 			cerr = 1;
519 		}
520 		/* If this character would exceed our output limit, stop. */
521 		if (olen + clen > (size_t)maxolen)
522 			break;
523 		/* Advance output pointer by number of bytes written. */
524 		mbdst += clen;
525 		/* Advance buffer character pointer. */
526 		dst++;
527 		/* Incrment output character count. */
528 		olen += clen;
529 	}
530 
531 	/* Terminate the output string. */
532 	*mbdst = '\0';
533 
534 	/* Pass conversion error flag out. */
535 	if (cerr_ptr)
536 		*cerr_ptr = cerr;
537 
538 	free(extra);
539 	free(pdst);
540 	free(psrc);
541 
542 	return (int)olen;
543 out:
544 	free(extra);
545 	free(pdst);
546 	free(psrc);
547 	return error;
548 }
549 
550 static int
551 istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
552     int flags, const char *mbextra, int *cerr_ptr)
553 {
554 	return istrsenvisx(mbdst, dlen, mbsrc,
555 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
556 }
557 
558 #endif
559 
560 #if !HAVE_SVIS
561 /*
562  *	The "svis" variants all take an "extra" arg that is a pointer
563  *	to a NUL-terminated list of characters to be encoded, too.
564  *	These functions are useful e. g. to encode strings in such a
565  *	way so that they are not interpreted by a shell.
566  */
567 
568 char *
569 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
570 {
571 	char cc[2];
572 	int ret;
573 
574 	cc[0] = c;
575 	cc[1] = nextc;
576 
577 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL);
578 	if (ret < 0)
579 		return NULL;
580 	return mbdst + ret;
581 }
582 
583 char *
584 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
585 {
586 	char cc[2];
587 	int ret;
588 
589 	cc[0] = c;
590 	cc[1] = nextc;
591 
592 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL);
593 	if (ret < 0)
594 		return NULL;
595 	return mbdst + ret;
596 }
597 
598 int
599 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
600 {
601 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL);
602 }
603 
604 int
605 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
606 {
607 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL);
608 }
609 
610 int
611 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
612 {
613 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
614 }
615 
616 int
617 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
618     const char *mbextra)
619 {
620 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
621 }
622 
623 int
624 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
625     const char *mbextra, int *cerr_ptr)
626 {
627 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
628 }
629 #endif
630 
631 #if !HAVE_VIS
632 /*
633  * vis - visually encode characters
634  */
635 char *
636 vis(char *mbdst, int c, int flags, int nextc)
637 {
638 	char cc[2];
639 	int ret;
640 
641 	cc[0] = c;
642 	cc[1] = nextc;
643 
644 	ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL);
645 	if (ret < 0)
646 		return NULL;
647 	return mbdst + ret;
648 }
649 
650 char *
651 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
652 {
653 	char cc[2];
654 	int ret;
655 
656 	cc[0] = c;
657 	cc[1] = nextc;
658 
659 	ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL);
660 	if (ret < 0)
661 		return NULL;
662 	return mbdst + ret;
663 }
664 
665 /*
666  * strvis - visually encode characters from src into dst
667  *
668  *	Dst must be 4 times the size of src to account for possible
669  *	expansion.  The length of dst, not including the trailing NULL,
670  *	is returned.
671  */
672 
673 int
674 strvis(char *mbdst, const char *mbsrc, int flags)
675 {
676 	return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL);
677 }
678 
679 int
680 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
681 {
682 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
683 }
684 
685 /*
686  * strvisx - visually encode characters from src into dst
687  *
688  *	Dst must be 4 times the size of src to account for possible
689  *	expansion.  The length of dst, not including the trailing NULL,
690  *	is returned.
691  *
692  *	Strvisx encodes exactly len characters from src into dst.
693  *	This is useful for encoding a block of data.
694  */
695 
696 int
697 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
698 {
699 	return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL);
700 }
701 
702 int
703 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
704 {
705 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL);
706 }
707 
708 int
709 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
710     int *cerr_ptr)
711 {
712 	return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
713 }
714 #endif
715