xref: /freebsd/contrib/libc-vis/vis.c (revision ea2be8ed283597062853935a5a29dfd67973044b)
1ea46e638SKyle Evans /*	$NetBSD: vis.c,v 1.83 2023/08/12 12:48:52 riastradh Exp $	*/
28ccca122SBrooks Davis 
38ccca122SBrooks Davis /*-
48ccca122SBrooks Davis  * Copyright (c) 1989, 1993
58ccca122SBrooks Davis  *	The Regents of the University of California.  All rights reserved.
68ccca122SBrooks Davis  *
78ccca122SBrooks Davis  * Redistribution and use in source and binary forms, with or without
88ccca122SBrooks Davis  * modification, are permitted provided that the following conditions
98ccca122SBrooks Davis  * are met:
108ccca122SBrooks Davis  * 1. Redistributions of source code must retain the above copyright
118ccca122SBrooks Davis  *    notice, this list of conditions and the following disclaimer.
128ccca122SBrooks Davis  * 2. Redistributions in binary form must reproduce the above copyright
138ccca122SBrooks Davis  *    notice, this list of conditions and the following disclaimer in the
148ccca122SBrooks Davis  *    documentation and/or other materials provided with the distribution.
158ccca122SBrooks Davis  * 3. Neither the name of the University nor the names of its contributors
168ccca122SBrooks Davis  *    may be used to endorse or promote products derived from this software
178ccca122SBrooks Davis  *    without specific prior written permission.
188ccca122SBrooks Davis  *
198ccca122SBrooks Davis  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208ccca122SBrooks Davis  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218ccca122SBrooks Davis  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228ccca122SBrooks Davis  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238ccca122SBrooks Davis  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248ccca122SBrooks Davis  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258ccca122SBrooks Davis  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268ccca122SBrooks Davis  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278ccca122SBrooks Davis  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288ccca122SBrooks Davis  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298ccca122SBrooks Davis  * SUCH DAMAGE.
308ccca122SBrooks Davis  */
318ccca122SBrooks Davis 
328ccca122SBrooks Davis /*-
338ccca122SBrooks Davis  * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
348ccca122SBrooks Davis  * All rights reserved.
358ccca122SBrooks Davis  *
368ccca122SBrooks Davis  * Redistribution and use in source and binary forms, with or without
378ccca122SBrooks Davis  * modification, are permitted provided that the following conditions
388ccca122SBrooks Davis  * are met:
398ccca122SBrooks Davis  * 1. Redistributions of source code must retain the above copyright
408ccca122SBrooks Davis  *    notice, this list of conditions and the following disclaimer.
418ccca122SBrooks Davis  * 2. Redistributions in binary form must reproduce the above copyright
428ccca122SBrooks Davis  *    notice, this list of conditions and the following disclaimer in the
438ccca122SBrooks Davis  *    documentation and/or other materials provided with the distribution.
448ccca122SBrooks Davis  *
458ccca122SBrooks Davis  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
468ccca122SBrooks Davis  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
478ccca122SBrooks Davis  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
488ccca122SBrooks Davis  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
498ccca122SBrooks Davis  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
508ccca122SBrooks Davis  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
518ccca122SBrooks Davis  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
528ccca122SBrooks Davis  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
538ccca122SBrooks Davis  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
548ccca122SBrooks Davis  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
558ccca122SBrooks Davis  * POSSIBILITY OF SUCH DAMAGE.
568ccca122SBrooks Davis  */
578ccca122SBrooks Davis 
588ccca122SBrooks Davis #include <sys/cdefs.h>
598ccca122SBrooks Davis #if defined(LIBC_SCCS) && !defined(lint)
60ea46e638SKyle Evans __RCSID("$NetBSD: vis.c,v 1.83 2023/08/12 12:48:52 riastradh Exp $");
618ccca122SBrooks Davis #endif /* LIBC_SCCS and not lint */
628ccca122SBrooks Davis 
638ccca122SBrooks Davis #include "namespace.h"
648ccca122SBrooks Davis #include <sys/types.h>
65778c12a6SBrooks Davis #include <sys/param.h>
668ccca122SBrooks Davis 
678ccca122SBrooks Davis #include <assert.h>
688ccca122SBrooks Davis #include <vis.h>
698ccca122SBrooks Davis #include <errno.h>
7009078445SKyle Evans #include <stdint.h>
718ccca122SBrooks Davis #include <stdlib.h>
72778c12a6SBrooks Davis #include <wchar.h>
73778c12a6SBrooks Davis #include <wctype.h>
748ccca122SBrooks Davis 
758ccca122SBrooks Davis #ifdef __weak_alias
768ccca122SBrooks Davis __weak_alias(strvisx,_strvisx)
778ccca122SBrooks Davis #endif
788ccca122SBrooks Davis 
798ccca122SBrooks Davis #if !HAVE_VIS || !HAVE_SVIS
808ccca122SBrooks Davis #include <ctype.h>
818ccca122SBrooks Davis #include <limits.h>
828ccca122SBrooks Davis #include <stdio.h>
838ccca122SBrooks Davis #include <string.h>
848ccca122SBrooks Davis 
85*db94e7c3SBrooks Davis #define	_DIAGASSERT(x)	assert(x)
86*db94e7c3SBrooks Davis 
87778c12a6SBrooks Davis /*
88778c12a6SBrooks Davis  * The reason for going through the trouble to deal with character encodings
89778c12a6SBrooks Davis  * in vis(3), is that we use this to safe encode output of commands. This
90778c12a6SBrooks Davis  * safe encoding varies depending on the character set. For example if we
91778c12a6SBrooks Davis  * display ps output in French, we don't want to display French characters
92778c12a6SBrooks Davis  * as M-foo.
93778c12a6SBrooks Davis  */
94778c12a6SBrooks Davis 
95778c12a6SBrooks Davis static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
968ccca122SBrooks Davis 
978ccca122SBrooks Davis #undef BELL
98778c12a6SBrooks Davis #define BELL L'\a'
998ccca122SBrooks Davis 
1008dfeba04SBrooks Davis #if defined(LC_C_LOCALE)
1018dfeba04SBrooks Davis #define iscgraph(c)      isgraph_l(c, LC_C_LOCALE)
1028dfeba04SBrooks Davis #else
1038dfeba04SBrooks Davis /* Keep it simple for now, no locale stuff */
1048dfeba04SBrooks Davis #define iscgraph(c)	isgraph(c)
1058dfeba04SBrooks Davis #ifdef notyet
1068dfeba04SBrooks Davis #include <locale.h>
1078dfeba04SBrooks Davis static int
iscgraph(int c)1088dfeba04SBrooks Davis iscgraph(int c) {
1098dfeba04SBrooks Davis 	int rv;
1108dfeba04SBrooks Davis 	char *ol;
1118dfeba04SBrooks Davis 
1128dfeba04SBrooks Davis 	ol = setlocale(LC_CTYPE, "C");
1138dfeba04SBrooks Davis 	rv = isgraph(c);
1148dfeba04SBrooks Davis 	if (ol)
1158dfeba04SBrooks Davis 		setlocale(LC_CTYPE, ol);
1168dfeba04SBrooks Davis 	return rv;
1178dfeba04SBrooks Davis }
1188dfeba04SBrooks Davis #endif
1198dfeba04SBrooks Davis #endif
1208dfeba04SBrooks Davis 
1218dfeba04SBrooks Davis #define ISGRAPH(flags, c) \
1228dfeba04SBrooks Davis     (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c))
1238dfeba04SBrooks Davis 
124778c12a6SBrooks Davis #define iswoctal(c)	(((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
125778c12a6SBrooks Davis #define iswwhite(c)	(c == L' ' || c == L'\t' || c == L'\n')
126778c12a6SBrooks Davis #define iswsafe(c)	(c == L'\b' || c == BELL || c == L'\r')
127778c12a6SBrooks Davis #define xtoa(c)		L"0123456789abcdef"[c]
128778c12a6SBrooks Davis #define XTOA(c)		L"0123456789ABCDEF"[c]
1298ccca122SBrooks Davis 
1308dfeba04SBrooks Davis #define MAXEXTRAS	30
1318dfeba04SBrooks Davis 
1328dfeba04SBrooks Davis static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
1338dfeba04SBrooks Davis static const wchar_t char_glob[] = L"*?[#";
1348ccca122SBrooks Davis 
135778c12a6SBrooks Davis #if !HAVE_NBTOOL_CONFIG_H
136778c12a6SBrooks Davis #ifndef __NetBSD__
137778c12a6SBrooks Davis /*
138778c12a6SBrooks Davis  * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
139778c12a6SBrooks Davis  * integral type and it is probably wrong, since currently the maximum
140778c12a6SBrooks Davis  * number of bytes and character needs is 6. Until this is fixed, the
141778c12a6SBrooks Davis  * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
142778c12a6SBrooks Davis  * the assertion is commented out.
143778c12a6SBrooks Davis  */
144778c12a6SBrooks Davis #ifdef __FreeBSD__
145778c12a6SBrooks Davis /*
146778c12a6SBrooks Davis  * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel
147778c12a6SBrooks Davis  * mode.
148778c12a6SBrooks Davis  */
149778c12a6SBrooks Davis #ifndef CTASSERT
150778c12a6SBrooks Davis #define CTASSERT(x)             _CTASSERT(x, __LINE__)
151778c12a6SBrooks Davis #define _CTASSERT(x, y)         __CTASSERT(x, y)
152778c12a6SBrooks Davis #define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
153778c12a6SBrooks Davis #endif
154778c12a6SBrooks Davis #endif /* __FreeBSD__ */
155778c12a6SBrooks Davis CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
156778c12a6SBrooks Davis #endif /* !__NetBSD__ */
157778c12a6SBrooks Davis #endif
1588ccca122SBrooks Davis 
1598ccca122SBrooks Davis /*
1608ccca122SBrooks Davis  * This is do_hvis, for HTTP style (RFC 1808)
1618ccca122SBrooks Davis  */
162778c12a6SBrooks Davis static wchar_t *
do_hvis(wchar_t * dst,wint_t c,int flags,wint_t nextc,const wchar_t * extra)163778c12a6SBrooks Davis do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
1648ccca122SBrooks Davis {
165778c12a6SBrooks Davis 	if (iswalnum(c)
1668ccca122SBrooks Davis 	    /* safe */
167778c12a6SBrooks Davis 	    || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
1688ccca122SBrooks Davis 	    /* extra */
169778c12a6SBrooks Davis 	    || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
170778c12a6SBrooks Davis 	    || c == L',')
171778c12a6SBrooks Davis 		dst = do_svis(dst, c, flags, nextc, extra);
172778c12a6SBrooks Davis 	else {
173778c12a6SBrooks Davis 		*dst++ = L'%';
1748ccca122SBrooks Davis 		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
1758ccca122SBrooks Davis 		*dst++ = xtoa((unsigned int)c & 0xf);
1768ccca122SBrooks Davis 	}
1778ccca122SBrooks Davis 
1788ccca122SBrooks Davis 	return dst;
1798ccca122SBrooks Davis }
1808ccca122SBrooks Davis 
1818ccca122SBrooks Davis /*
1828ccca122SBrooks Davis  * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
1838ccca122SBrooks Davis  * NB: No handling of long lines or CRLF.
1848ccca122SBrooks Davis  */
185778c12a6SBrooks Davis static wchar_t *
do_mvis(wchar_t * dst,wint_t c,int flags,wint_t nextc,const wchar_t * extra)186778c12a6SBrooks Davis do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
1878ccca122SBrooks Davis {
188778c12a6SBrooks Davis 	if ((c != L'\n') &&
1898ccca122SBrooks Davis 	    /* Space at the end of the line */
190778c12a6SBrooks Davis 	    ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
1918ccca122SBrooks Davis 	    /* Out of range */
192778c12a6SBrooks Davis 	    (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
1938ccca122SBrooks Davis 	    /* Specific char to be escaped */
194778c12a6SBrooks Davis 	    wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
195778c12a6SBrooks Davis 		*dst++ = L'=';
1968ccca122SBrooks Davis 		*dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
1978ccca122SBrooks Davis 		*dst++ = XTOA((unsigned int)c & 0xf);
198778c12a6SBrooks Davis 	} else
199778c12a6SBrooks Davis 		dst = do_svis(dst, c, flags, nextc, extra);
200778c12a6SBrooks Davis 	return dst;
2018ccca122SBrooks Davis }
202778c12a6SBrooks Davis 
203778c12a6SBrooks Davis /*
204778c12a6SBrooks Davis  * Output single byte of multibyte character.
205778c12a6SBrooks Davis  */
206778c12a6SBrooks Davis static wchar_t *
do_mbyte(wchar_t * dst,wint_t c,int flags,wint_t nextc,int iswextra)207778c12a6SBrooks Davis do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
208778c12a6SBrooks Davis {
209778c12a6SBrooks Davis 	if (flags & VIS_CSTYLE) {
210778c12a6SBrooks Davis 		switch (c) {
211778c12a6SBrooks Davis 		case L'\n':
212778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L'n';
213778c12a6SBrooks Davis 			return dst;
214778c12a6SBrooks Davis 		case L'\r':
215778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L'r';
216778c12a6SBrooks Davis 			return dst;
217778c12a6SBrooks Davis 		case L'\b':
218778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L'b';
219778c12a6SBrooks Davis 			return dst;
220778c12a6SBrooks Davis 		case BELL:
221778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L'a';
222778c12a6SBrooks Davis 			return dst;
223778c12a6SBrooks Davis 		case L'\v':
224778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L'v';
225778c12a6SBrooks Davis 			return dst;
226778c12a6SBrooks Davis 		case L'\t':
227778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L't';
228778c12a6SBrooks Davis 			return dst;
229778c12a6SBrooks Davis 		case L'\f':
230778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L'f';
231778c12a6SBrooks Davis 			return dst;
232778c12a6SBrooks Davis 		case L' ':
233778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L's';
234778c12a6SBrooks Davis 			return dst;
235778c12a6SBrooks Davis 		case L'\0':
236778c12a6SBrooks Davis 			*dst++ = L'\\'; *dst++ = L'0';
237778c12a6SBrooks Davis 			if (iswoctal(nextc)) {
238778c12a6SBrooks Davis 				*dst++ = L'0';
239778c12a6SBrooks Davis 				*dst++ = L'0';
240778c12a6SBrooks Davis 			}
241778c12a6SBrooks Davis 			return dst;
2428dfeba04SBrooks Davis 		/* We cannot encode these characters in VIS_CSTYLE
2438dfeba04SBrooks Davis 		 * because they special meaning */
2448dfeba04SBrooks Davis 		case L'n':
2458dfeba04SBrooks Davis 		case L'r':
2468dfeba04SBrooks Davis 		case L'b':
2478dfeba04SBrooks Davis 		case L'a':
2488dfeba04SBrooks Davis 		case L'v':
2498dfeba04SBrooks Davis 		case L't':
2508dfeba04SBrooks Davis 		case L'f':
2518dfeba04SBrooks Davis 		case L's':
2528dfeba04SBrooks Davis 		case L'0':
2538dfeba04SBrooks Davis 		case L'M':
2548dfeba04SBrooks Davis 		case L'^':
2558dfeba04SBrooks Davis 		case L'$': /* vis(1) -l */
2568dfeba04SBrooks Davis 			break;
257778c12a6SBrooks Davis 		default:
2588dfeba04SBrooks Davis 			if (ISGRAPH(flags, c) && !iswoctal(c)) {
259778c12a6SBrooks Davis 				*dst++ = L'\\';
260778c12a6SBrooks Davis 				*dst++ = c;
261778c12a6SBrooks Davis 				return dst;
262778c12a6SBrooks Davis 			}
263778c12a6SBrooks Davis 		}
264778c12a6SBrooks Davis 	}
265778c12a6SBrooks Davis 	if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
266778c12a6SBrooks Davis 		*dst++ = L'\\';
267778c12a6SBrooks Davis 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
268778c12a6SBrooks Davis 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
269778c12a6SBrooks Davis 		*dst++ =			     (c	      & 07) + L'0';
270778c12a6SBrooks Davis 	} else {
271778c12a6SBrooks Davis 		if ((flags & VIS_NOSLASH) == 0)
272778c12a6SBrooks Davis 			*dst++ = L'\\';
273778c12a6SBrooks Davis 
274778c12a6SBrooks Davis 		if (c & 0200) {
275778c12a6SBrooks Davis 			c &= 0177;
276778c12a6SBrooks Davis 			*dst++ = L'M';
277778c12a6SBrooks Davis 		}
278778c12a6SBrooks Davis 
279778c12a6SBrooks Davis 		if (iswcntrl(c)) {
280778c12a6SBrooks Davis 			*dst++ = L'^';
281778c12a6SBrooks Davis 			if (c == 0177)
282778c12a6SBrooks Davis 				*dst++ = L'?';
283778c12a6SBrooks Davis 			else
284778c12a6SBrooks Davis 				*dst++ = c + L'@';
285778c12a6SBrooks Davis 		} else {
286778c12a6SBrooks Davis 			*dst++ = L'-';
287778c12a6SBrooks Davis 			*dst++ = c;
288778c12a6SBrooks Davis 		}
289778c12a6SBrooks Davis 	}
290778c12a6SBrooks Davis 
2918ccca122SBrooks Davis 	return dst;
2928ccca122SBrooks Davis }
2938ccca122SBrooks Davis 
2948ccca122SBrooks Davis /*
2958ccca122SBrooks Davis  * This is do_vis, the central code of vis.
2968ccca122SBrooks Davis  * dst:	      Pointer to the destination buffer
2978ccca122SBrooks Davis  * c:	      Character to encode
298778c12a6SBrooks Davis  * flags:     Flags word
2998ccca122SBrooks Davis  * nextc:     The character following 'c'
3008ccca122SBrooks Davis  * extra:     Pointer to the list of extra characters to be
3018ccca122SBrooks Davis  *	      backslash-protected.
3028ccca122SBrooks Davis  */
303778c12a6SBrooks Davis static wchar_t *
do_svis(wchar_t * dst,wint_t c,int flags,wint_t nextc,const wchar_t * extra)304778c12a6SBrooks Davis do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
3058ccca122SBrooks Davis {
306778c12a6SBrooks Davis 	int iswextra, i, shft;
307778c12a6SBrooks Davis 	uint64_t bmsk, wmsk;
3088ccca122SBrooks Davis 
309778c12a6SBrooks Davis 	iswextra = wcschr(extra, c) != NULL;
3108dfeba04SBrooks Davis 	if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) ||
311778c12a6SBrooks Davis 	    ((flags & VIS_SAFE) && iswsafe(c)))) {
3128ccca122SBrooks Davis 		*dst++ = c;
3138ccca122SBrooks Davis 		return dst;
3148ccca122SBrooks Davis 	}
315778c12a6SBrooks Davis 
316778c12a6SBrooks Davis 	/* See comment in istrsenvisx() output loop, below. */
317778c12a6SBrooks Davis 	wmsk = 0;
318778c12a6SBrooks Davis 	for (i = sizeof(wmsk) - 1; i >= 0; i--) {
319778c12a6SBrooks Davis 		shft = i * NBBY;
320778c12a6SBrooks Davis 		bmsk = (uint64_t)0xffLL << shft;
321778c12a6SBrooks Davis 		wmsk |= bmsk;
322778c12a6SBrooks Davis 		if ((c & wmsk) || i == 0)
323778c12a6SBrooks Davis 			dst = do_mbyte(dst, (wint_t)(
324778c12a6SBrooks Davis 			    (uint64_t)(c & bmsk) >> shft),
325778c12a6SBrooks Davis 			    flags, nextc, iswextra);
3268ccca122SBrooks Davis 	}
3278ccca122SBrooks Davis 
3288ccca122SBrooks Davis 	return dst;
3298ccca122SBrooks Davis }
3308ccca122SBrooks Davis 
331778c12a6SBrooks Davis typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
3328ccca122SBrooks Davis 
3338ccca122SBrooks Davis /*
3348ccca122SBrooks Davis  * Return the appropriate encoding function depending on the flags given.
3358ccca122SBrooks Davis  */
3368ccca122SBrooks Davis static visfun_t
getvisfun(int flags)337778c12a6SBrooks Davis getvisfun(int flags)
3388ccca122SBrooks Davis {
339778c12a6SBrooks Davis 	if (flags & VIS_HTTPSTYLE)
3408ccca122SBrooks Davis 		return do_hvis;
341778c12a6SBrooks Davis 	if (flags & VIS_MIMESTYLE)
3428ccca122SBrooks Davis 		return do_mvis;
3438ccca122SBrooks Davis 	return do_svis;
3448ccca122SBrooks Davis }
3458ccca122SBrooks Davis 
3468ccca122SBrooks Davis /*
347778c12a6SBrooks Davis  * Expand list of extra characters to not visually encode.
3488ccca122SBrooks Davis  */
349778c12a6SBrooks Davis static wchar_t *
makeextralist(int flags,const char * src)350778c12a6SBrooks Davis makeextralist(int flags, const char *src)
3518ccca122SBrooks Davis {
352778c12a6SBrooks Davis 	wchar_t *dst, *d;
353778c12a6SBrooks Davis 	size_t len;
3548dfeba04SBrooks Davis 	const wchar_t *s;
355f0a6d7e2SBryan Drewery 	mbstate_t mbstate;
3568ccca122SBrooks Davis 
357f0a6d7e2SBryan Drewery 	bzero(&mbstate, sizeof(mbstate));
358778c12a6SBrooks Davis 	len = strlen(src);
359778c12a6SBrooks Davis 	if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
3608ccca122SBrooks Davis 		return NULL;
361778c12a6SBrooks Davis 
362ea46e638SKyle Evans 	memset(&mbstate, 0, sizeof(mbstate));
363ea46e638SKyle Evans 	if ((flags & VIS_NOLOCALE)
364ea46e638SKyle Evans 	    || mbsrtowcs(dst, &src, len, &mbstate) == (size_t)-1) {
365778c12a6SBrooks Davis 		size_t i;
366778c12a6SBrooks Davis 		for (i = 0; i < len; i++)
3678dfeba04SBrooks Davis 			dst[i] = (wchar_t)(u_char)src[i];
368778c12a6SBrooks Davis 		d = dst + len;
369778c12a6SBrooks Davis 	} else
370778c12a6SBrooks Davis 		d = dst + wcslen(dst);
371778c12a6SBrooks Davis 
3728dfeba04SBrooks Davis 	if (flags & VIS_GLOB)
3738dfeba04SBrooks Davis 		for (s = char_glob; *s; *d++ = *s++)
3748dfeba04SBrooks Davis 			continue;
3758dfeba04SBrooks Davis 
3768dfeba04SBrooks Davis 	if (flags & VIS_SHELL)
3778dfeba04SBrooks Davis 		for (s = char_shell; *s; *d++ = *s++)
3788dfeba04SBrooks Davis 			continue;
379778c12a6SBrooks Davis 
380778c12a6SBrooks Davis 	if (flags & VIS_SP) *d++ = L' ';
381778c12a6SBrooks Davis 	if (flags & VIS_TAB) *d++ = L'\t';
382778c12a6SBrooks Davis 	if (flags & VIS_NL) *d++ = L'\n';
383ff88ef41SBrooks Davis 	if (flags & VIS_DQ) *d++ = L'"';
384778c12a6SBrooks Davis 	if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
385778c12a6SBrooks Davis 	*d = L'\0';
386778c12a6SBrooks Davis 
3878ccca122SBrooks Davis 	return dst;
3888ccca122SBrooks Davis }
3898ccca122SBrooks Davis 
3908ccca122SBrooks Davis /*
391778c12a6SBrooks Davis  * istrsenvisx()
392778c12a6SBrooks Davis  * 	The main internal function.
393778c12a6SBrooks Davis  *	All user-visible functions call this one.
3948ccca122SBrooks Davis  */
3958ccca122SBrooks Davis static int
istrsenvisx(char ** mbdstp,size_t * dlen,const char * mbsrc,size_t mblength,int flags,const char * mbextra,int * cerr_ptr)3968dfeba04SBrooks Davis istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength,
397778c12a6SBrooks Davis     int flags, const char *mbextra, int *cerr_ptr)
3988ccca122SBrooks Davis {
399ea46e638SKyle Evans 	char mbbuf[MB_LEN_MAX];
400778c12a6SBrooks Davis 	wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
401778c12a6SBrooks Davis 	size_t len, olen;
402778c12a6SBrooks Davis 	uint64_t bmsk, wmsk;
403778c12a6SBrooks Davis 	wint_t c;
4048ccca122SBrooks Davis 	visfun_t f;
4058dfeba04SBrooks Davis 	int clen = 0, cerr, error = -1, i, shft;
4062f489a50SKyle Evans 	char *mbdst, *mbwrite, *mdst;
407ea46e638SKyle Evans 	size_t mbslength;
4082f489a50SKyle Evans 	size_t maxolen;
409f0a6d7e2SBryan Drewery 	mbstate_t mbstate;
4108ccca122SBrooks Davis 
4118dfeba04SBrooks Davis 	_DIAGASSERT(mbdstp != NULL);
412ac25e238SBrooks Davis 	_DIAGASSERT(mbsrc != NULL || mblength == 0);
413778c12a6SBrooks Davis 	_DIAGASSERT(mbextra != NULL);
414778c12a6SBrooks Davis 
415ea46e638SKyle Evans 	mbslength = mblength;
416ff88ef41SBrooks Davis 	/*
417ff88ef41SBrooks Davis 	 * When inputing a single character, must also read in the
418ff88ef41SBrooks Davis 	 * next character for nextc, the look-ahead character.
419ff88ef41SBrooks Davis 	 */
420ff88ef41SBrooks Davis 	if (mbslength == 1)
421ff88ef41SBrooks Davis 		mbslength++;
422ff88ef41SBrooks Davis 
423778c12a6SBrooks Davis 	/*
424778c12a6SBrooks Davis 	 * Input (mbsrc) is a char string considered to be multibyte
425778c12a6SBrooks Davis 	 * characters.  The input loop will read this string pulling
426778c12a6SBrooks Davis 	 * one character, possibly multiple bytes, from mbsrc and
427778c12a6SBrooks Davis 	 * converting each to wchar_t in src.
428778c12a6SBrooks Davis 	 *
429778c12a6SBrooks Davis 	 * The vis conversion will be done using the wide char
430778c12a6SBrooks Davis 	 * wchar_t string.
431778c12a6SBrooks Davis 	 *
432778c12a6SBrooks Davis 	 * This will then be converted back to a multibyte string to
433778c12a6SBrooks Davis 	 * return to the caller.
434778c12a6SBrooks Davis 	 */
435778c12a6SBrooks Davis 
436ea46e638SKyle Evans 	/*
437ea46e638SKyle Evans 	 * Guarantee the arithmetic on input to calloc won't overflow.
438ea46e638SKyle Evans 	 */
439ea46e638SKyle Evans 	if (mbslength > (SIZE_MAX - 1)/16) {
440ea46e638SKyle Evans 		errno = ENOMEM;
441ea46e638SKyle Evans 		return -1;
442ea46e638SKyle Evans 	}
443ea46e638SKyle Evans 
444778c12a6SBrooks Davis 	/* Allocate space for the wide char strings */
445778c12a6SBrooks Davis 	psrc = pdst = extra = NULL;
4468dfeba04SBrooks Davis 	mdst = NULL;
447ff88ef41SBrooks Davis 	if ((psrc = calloc(mbslength + 1, sizeof(*psrc))) == NULL)
4488ccca122SBrooks Davis 		return -1;
449ff88ef41SBrooks Davis 	if ((pdst = calloc((16 * mbslength) + 1, sizeof(*pdst))) == NULL)
450778c12a6SBrooks Davis 		goto out;
4518dfeba04SBrooks Davis 	if (*mbdstp == NULL) {
452ff88ef41SBrooks Davis 		if ((mdst = calloc((16 * mbslength) + 1, sizeof(*mdst))) == NULL)
4538dfeba04SBrooks Davis 			goto out;
4548dfeba04SBrooks Davis 		*mbdstp = mdst;
4558dfeba04SBrooks Davis 	}
4568dfeba04SBrooks Davis 
4578dfeba04SBrooks Davis 	mbdst = *mbdstp;
458778c12a6SBrooks Davis 	dst = pdst;
459778c12a6SBrooks Davis 	src = psrc;
460778c12a6SBrooks Davis 
4618dfeba04SBrooks Davis 	if (flags & VIS_NOLOCALE) {
4628dfeba04SBrooks Davis 		/* Do one byte at a time conversion */
4638dfeba04SBrooks Davis 		cerr = 1;
4648dfeba04SBrooks Davis 	} else {
465778c12a6SBrooks Davis 		/* Use caller's multibyte conversion error flag. */
4668dfeba04SBrooks Davis 		cerr = cerr_ptr ? *cerr_ptr : 0;
4678dfeba04SBrooks Davis 	}
468778c12a6SBrooks Davis 
469778c12a6SBrooks Davis 	/*
470778c12a6SBrooks Davis 	 * Input loop.
471778c12a6SBrooks Davis 	 * Handle up to mblength characters (not bytes).  We do not
472778c12a6SBrooks Davis 	 * stop at NULs because we may be processing a block of data
473778c12a6SBrooks Davis 	 * that includes NULs.
474778c12a6SBrooks Davis 	 */
475ea46e638SKyle Evans 	memset(&mbstate, 0, sizeof(mbstate));
476778c12a6SBrooks Davis 	while (mbslength > 0) {
477778c12a6SBrooks Davis 		/* Convert one multibyte character to wchar_t. */
478ea46e638SKyle Evans 		if (!cerr) {
479ea46e638SKyle Evans 			clen = mbrtowc(src, mbsrc,
480ea46e638SKyle Evans 			    (mbslength < MB_LEN_MAX
481ea46e638SKyle Evans 				? mbslength
482ea46e638SKyle Evans 				: MB_LEN_MAX),
4831a2f06d0SAlex Richardson 			    &mbstate);
484ea46e638SKyle Evans 			assert(clen < 0 || (size_t)clen <= mbslength);
485ea46e638SKyle Evans 			assert(clen <= MB_LEN_MAX);
486ea46e638SKyle Evans 		}
487778c12a6SBrooks Davis 		if (cerr || clen < 0) {
488778c12a6SBrooks Davis 			/* Conversion error, process as a byte instead. */
489778c12a6SBrooks Davis 			*src = (wint_t)(u_char)*mbsrc;
490778c12a6SBrooks Davis 			clen = 1;
491778c12a6SBrooks Davis 			cerr = 1;
4928ccca122SBrooks Davis 		}
493ff88ef41SBrooks Davis 		if (clen == 0) {
494778c12a6SBrooks Davis 			/*
495778c12a6SBrooks Davis 			 * NUL in input gives 0 return value. process
496778c12a6SBrooks Davis 			 * as single NUL byte and keep going.
497778c12a6SBrooks Davis 			 */
498778c12a6SBrooks Davis 			clen = 1;
499ff88ef41SBrooks Davis 		}
500ea46e638SKyle Evans 		/*
501ea46e638SKyle Evans 		 * Let n := MIN(mbslength, MB_LEN_MAX).  We have:
502ea46e638SKyle Evans 		 *
503ea46e638SKyle Evans 		 *	mbslength >= 1
504ea46e638SKyle Evans 		 *	mbrtowc(..., n, &mbstate) <= n,
505ea46e638SKyle Evans 		 *		by the contract of mbrtowc
506ea46e638SKyle Evans 		 *
507ea46e638SKyle Evans 		 *  clen is either
508ea46e638SKyle Evans 		 *  (a) mbrtowc(..., n, &mbstate), in which case
509ea46e638SKyle Evans 		 *      clen <= n <= mbslength; or
510ea46e638SKyle Evans 		 *  (b) 1, in which case clen = 1 <= mbslength.
511ea46e638SKyle Evans 		 */
512ea46e638SKyle Evans 		assert(clen > 0);
513ea46e638SKyle Evans 		assert((size_t)clen <= mbslength);
514778c12a6SBrooks Davis 		/* Advance buffer character pointer. */
515778c12a6SBrooks Davis 		src++;
516778c12a6SBrooks Davis 		/* Advance input pointer by number of bytes read. */
517778c12a6SBrooks Davis 		mbsrc += clen;
518778c12a6SBrooks Davis 		/* Decrement input byte count. */
519778c12a6SBrooks Davis 		mbslength -= clen;
5208ccca122SBrooks Davis 	}
521778c12a6SBrooks Davis 	len = src - psrc;
522778c12a6SBrooks Davis 	src = psrc;
523ff88ef41SBrooks Davis 
524778c12a6SBrooks Davis 	/*
525778c12a6SBrooks Davis 	 * In the single character input case, we will have actually
526778c12a6SBrooks Davis 	 * processed two characters, c and nextc.  Reset len back to
527778c12a6SBrooks Davis 	 * just a single character.
528778c12a6SBrooks Davis 	 */
529778c12a6SBrooks Davis 	if (mblength < len)
530778c12a6SBrooks Davis 		len = mblength;
531778c12a6SBrooks Davis 
532778c12a6SBrooks Davis 	/* Convert extra argument to list of characters for this mode. */
533778c12a6SBrooks Davis 	extra = makeextralist(flags, mbextra);
534778c12a6SBrooks Davis 	if (!extra) {
5358ccca122SBrooks Davis 		if (dlen && *dlen == 0) {
5368ccca122SBrooks Davis 			errno = ENOSPC;
537778c12a6SBrooks Davis 			goto out;
5388ccca122SBrooks Davis 		}
539778c12a6SBrooks Davis 		*mbdst = '\0';	/* can't create extra, return "" */
540778c12a6SBrooks Davis 		error = 0;
541778c12a6SBrooks Davis 		goto out;
5428ccca122SBrooks Davis 	}
5438ccca122SBrooks Davis 
544778c12a6SBrooks Davis 	/* Look up which processing function to call. */
545778c12a6SBrooks Davis 	f = getvisfun(flags);
5468ccca122SBrooks Davis 
547778c12a6SBrooks Davis 	/*
548778c12a6SBrooks Davis 	 * Main processing loop.
549778c12a6SBrooks Davis 	 * Call do_Xvis processing function one character at a time
550778c12a6SBrooks Davis 	 * with next character available for look-ahead.
551778c12a6SBrooks Davis 	 */
5528ccca122SBrooks Davis 	for (start = dst; len > 0; len--) {
5538ccca122SBrooks Davis 		c = *src++;
554778c12a6SBrooks Davis 		dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
5558ccca122SBrooks Davis 		if (dst == NULL) {
5568ccca122SBrooks Davis 			errno = ENOSPC;
557778c12a6SBrooks Davis 			goto out;
5588ccca122SBrooks Davis 		}
5598ccca122SBrooks Davis 	}
560778c12a6SBrooks Davis 
561778c12a6SBrooks Davis 	/* Terminate the string in the buffer. */
562778c12a6SBrooks Davis 	*dst = L'\0';
563778c12a6SBrooks Davis 
564778c12a6SBrooks Davis 	/*
565778c12a6SBrooks Davis 	 * Output loop.
566778c12a6SBrooks Davis 	 * Convert wchar_t string back to multibyte output string.
567778c12a6SBrooks Davis 	 * If we have hit a multi-byte conversion error on input,
568778c12a6SBrooks Davis 	 * output byte-by-byte here.  Else use wctomb().
569778c12a6SBrooks Davis 	 */
570778c12a6SBrooks Davis 	len = wcslen(start);
571ea46e638SKyle Evans 	if (dlen) {
572ea46e638SKyle Evans 		maxolen = *dlen;
573ea46e638SKyle Evans 		if (maxolen == 0) {
574ea46e638SKyle Evans 			errno = ENOSPC;
575ea46e638SKyle Evans 			goto out;
576ea46e638SKyle Evans 		}
577ea46e638SKyle Evans 	} else {
578ea46e638SKyle Evans 		if (len > (SIZE_MAX - 1)/MB_LEN_MAX) {
579ea46e638SKyle Evans 			errno = ENOSPC;
580ea46e638SKyle Evans 			goto out;
581ea46e638SKyle Evans 		}
582ea46e638SKyle Evans 		maxolen = len*MB_LEN_MAX + 1;
583ea46e638SKyle Evans 	}
584778c12a6SBrooks Davis 	olen = 0;
585ea46e638SKyle Evans 	memset(&mbstate, 0, sizeof(mbstate));
586778c12a6SBrooks Davis 	for (dst = start; len > 0; len--) {
5872f489a50SKyle Evans 		if (!cerr) {
5882f489a50SKyle Evans 			/*
5892f489a50SKyle Evans 			 * If we have at least MB_CUR_MAX bytes in the buffer,
5902f489a50SKyle Evans 			 * we'll just do the conversion in-place into mbdst.  We
5912f489a50SKyle Evans 			 * need to be a little more conservative when we get to
5922f489a50SKyle Evans 			 * the end of the buffer, as we may not have MB_CUR_MAX
5932f489a50SKyle Evans 			 * bytes but we may not need it.
5942f489a50SKyle Evans 			 */
5952f489a50SKyle Evans 			if (maxolen - olen > MB_CUR_MAX)
5962f489a50SKyle Evans 				mbwrite = mbdst;
5972f489a50SKyle Evans 			else
5982f489a50SKyle Evans 				mbwrite = mbbuf;
5992f489a50SKyle Evans 			clen = wcrtomb(mbwrite, *dst, &mbstate);
6002f489a50SKyle Evans 			if (clen > 0 && mbwrite != mbdst) {
6012f489a50SKyle Evans 				/*
6022f489a50SKyle Evans 				 * Don't break past our output limit, noting
6032f489a50SKyle Evans 				 * that maxolen includes the nul terminator so
6042f489a50SKyle Evans 				 * we can't write past maxolen - 1 here.
6052f489a50SKyle Evans 				 */
6062f489a50SKyle Evans 				if (olen + clen >= maxolen) {
6072f489a50SKyle Evans 					errno = ENOSPC;
6082f489a50SKyle Evans 					goto out;
6092f489a50SKyle Evans 				}
6102f489a50SKyle Evans 
6112f489a50SKyle Evans 				memcpy(mbdst, mbwrite, clen);
6122f489a50SKyle Evans 			}
6132f489a50SKyle Evans 		}
614778c12a6SBrooks Davis 		if (cerr || clen < 0) {
615778c12a6SBrooks Davis 			/*
616778c12a6SBrooks Davis 			 * Conversion error, process as a byte(s) instead.
617778c12a6SBrooks Davis 			 * Examine each byte and higher-order bytes for
618778c12a6SBrooks Davis 			 * data.  E.g.,
619778c12a6SBrooks Davis 			 *	0x000000000000a264 -> a2 64
620778c12a6SBrooks Davis 			 *	0x000000001f00a264 -> 1f 00 a2 64
621778c12a6SBrooks Davis 			 */
622778c12a6SBrooks Davis 			clen = 0;
623778c12a6SBrooks Davis 			wmsk = 0;
624778c12a6SBrooks Davis 			for (i = sizeof(wmsk) - 1; i >= 0; i--) {
625778c12a6SBrooks Davis 				shft = i * NBBY;
626778c12a6SBrooks Davis 				bmsk = (uint64_t)0xffLL << shft;
627778c12a6SBrooks Davis 				wmsk |= bmsk;
6282f489a50SKyle Evans 				if ((*dst & wmsk) || i == 0) {
6292f489a50SKyle Evans 					if (olen + clen + 1 >= maxolen) {
6302f489a50SKyle Evans 						errno = ENOSPC;
6312f489a50SKyle Evans 						goto out;
6322f489a50SKyle Evans 					}
6332f489a50SKyle Evans 
634778c12a6SBrooks Davis 					mbdst[clen++] = (char)(
635778c12a6SBrooks Davis 					    (uint64_t)(*dst & bmsk) >>
636778c12a6SBrooks Davis 					    shft);
6378ccca122SBrooks Davis 				}
6382f489a50SKyle Evans 			}
639778c12a6SBrooks Davis 			cerr = 1;
640778c12a6SBrooks Davis 		}
6412f489a50SKyle Evans 
6422f489a50SKyle Evans 		/*
6432f489a50SKyle Evans 		 * We'll be dereferencing mbdst[clen] after this to write the
6442f489a50SKyle Evans 		 * nul terminator; the above paths should have checked for a
6452f489a50SKyle Evans 		 * possible overflow already.
6462f489a50SKyle Evans 		 */
6472f489a50SKyle Evans 		assert(olen + clen < maxolen);
6482f489a50SKyle Evans 
649778c12a6SBrooks Davis 		/* Advance output pointer by number of bytes written. */
650778c12a6SBrooks Davis 		mbdst += clen;
651778c12a6SBrooks Davis 		/* Advance buffer character pointer. */
652778c12a6SBrooks Davis 		dst++;
653778c12a6SBrooks Davis 		/* Incrment output character count. */
654778c12a6SBrooks Davis 		olen += clen;
655778c12a6SBrooks Davis 	}
656778c12a6SBrooks Davis 
657778c12a6SBrooks Davis 	/* Terminate the output string. */
658ea46e638SKyle Evans 	assert(olen < maxolen);
659778c12a6SBrooks Davis 	*mbdst = '\0';
660778c12a6SBrooks Davis 
6618dfeba04SBrooks Davis 	if (flags & VIS_NOLOCALE) {
662778c12a6SBrooks Davis 		/* Pass conversion error flag out. */
663778c12a6SBrooks Davis 		if (cerr_ptr)
664778c12a6SBrooks Davis 			*cerr_ptr = cerr;
6658dfeba04SBrooks Davis 	}
666778c12a6SBrooks Davis 
667778c12a6SBrooks Davis 	free(extra);
668778c12a6SBrooks Davis 	free(pdst);
669778c12a6SBrooks Davis 	free(psrc);
670778c12a6SBrooks Davis 
671778c12a6SBrooks Davis 	return (int)olen;
672778c12a6SBrooks Davis out:
673778c12a6SBrooks Davis 	free(extra);
674778c12a6SBrooks Davis 	free(pdst);
675778c12a6SBrooks Davis 	free(psrc);
6768dfeba04SBrooks Davis 	free(mdst);
677778c12a6SBrooks Davis 	return error;
678778c12a6SBrooks Davis }
679ac25e238SBrooks Davis 
680ac25e238SBrooks Davis static int
istrsenvisxl(char ** mbdstp,size_t * dlen,const char * mbsrc,int flags,const char * mbextra,int * cerr_ptr)6818dfeba04SBrooks Davis istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc,
682ac25e238SBrooks Davis     int flags, const char *mbextra, int *cerr_ptr)
683ac25e238SBrooks Davis {
6848dfeba04SBrooks Davis 	return istrsenvisx(mbdstp, dlen, mbsrc,
685ac25e238SBrooks Davis 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
686ac25e238SBrooks Davis }
687ac25e238SBrooks Davis 
688778c12a6SBrooks Davis #endif
689778c12a6SBrooks Davis 
690778c12a6SBrooks Davis #if !HAVE_SVIS
691778c12a6SBrooks Davis /*
692778c12a6SBrooks Davis  *	The "svis" variants all take an "extra" arg that is a pointer
693778c12a6SBrooks Davis  *	to a NUL-terminated list of characters to be encoded, too.
694778c12a6SBrooks Davis  *	These functions are useful e. g. to encode strings in such a
695778c12a6SBrooks Davis  *	way so that they are not interpreted by a shell.
696778c12a6SBrooks Davis  */
697778c12a6SBrooks Davis 
698778c12a6SBrooks Davis char *
svis(char * mbdst,int c,int flags,int nextc,const char * mbextra)699778c12a6SBrooks Davis svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
700778c12a6SBrooks Davis {
701778c12a6SBrooks Davis 	char cc[2];
702778c12a6SBrooks Davis 	int ret;
703778c12a6SBrooks Davis 
704778c12a6SBrooks Davis 	cc[0] = c;
705778c12a6SBrooks Davis 	cc[1] = nextc;
706778c12a6SBrooks Davis 
7078dfeba04SBrooks Davis 	ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL);
708778c12a6SBrooks Davis 	if (ret < 0)
709778c12a6SBrooks Davis 		return NULL;
710778c12a6SBrooks Davis 	return mbdst + ret;
711778c12a6SBrooks Davis }
712778c12a6SBrooks Davis 
713778c12a6SBrooks Davis char *
snvis(char * mbdst,size_t dlen,int c,int flags,int nextc,const char * mbextra)714778c12a6SBrooks Davis snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
715778c12a6SBrooks Davis {
716778c12a6SBrooks Davis 	char cc[2];
717778c12a6SBrooks Davis 	int ret;
718778c12a6SBrooks Davis 
719778c12a6SBrooks Davis 	cc[0] = c;
720778c12a6SBrooks Davis 	cc[1] = nextc;
721778c12a6SBrooks Davis 
7228dfeba04SBrooks Davis 	ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL);
723778c12a6SBrooks Davis 	if (ret < 0)
724778c12a6SBrooks Davis 		return NULL;
725778c12a6SBrooks Davis 	return mbdst + ret;
7268ccca122SBrooks Davis }
7278ccca122SBrooks Davis 
7288ccca122SBrooks Davis int
strsvis(char * mbdst,const char * mbsrc,int flags,const char * mbextra)729778c12a6SBrooks Davis strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
7308ccca122SBrooks Davis {
7318dfeba04SBrooks Davis 	return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL);
7328ccca122SBrooks Davis }
7338ccca122SBrooks Davis 
7348ccca122SBrooks Davis int
strsnvis(char * mbdst,size_t dlen,const char * mbsrc,int flags,const char * mbextra)735778c12a6SBrooks Davis strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
7368ccca122SBrooks Davis {
7378dfeba04SBrooks Davis 	return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL);
738778c12a6SBrooks Davis }
739778c12a6SBrooks Davis 
740778c12a6SBrooks Davis int
strsvisx(char * mbdst,const char * mbsrc,size_t len,int flags,const char * mbextra)741778c12a6SBrooks Davis strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
742778c12a6SBrooks Davis {
7438dfeba04SBrooks Davis 	return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
744778c12a6SBrooks Davis }
745778c12a6SBrooks Davis 
746778c12a6SBrooks Davis int
strsnvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags,const char * mbextra)747778c12a6SBrooks Davis strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
748778c12a6SBrooks Davis     const char *mbextra)
749778c12a6SBrooks Davis {
7508dfeba04SBrooks Davis 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
751778c12a6SBrooks Davis }
752778c12a6SBrooks Davis 
753778c12a6SBrooks Davis int
strsenvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags,const char * mbextra,int * cerr_ptr)754778c12a6SBrooks Davis strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
755778c12a6SBrooks Davis     const char *mbextra, int *cerr_ptr)
756778c12a6SBrooks Davis {
7578dfeba04SBrooks Davis 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
7588ccca122SBrooks Davis }
7598ccca122SBrooks Davis #endif
7608ccca122SBrooks Davis 
7618ccca122SBrooks Davis #if !HAVE_VIS
7628ccca122SBrooks Davis /*
7638ccca122SBrooks Davis  * vis - visually encode characters
7648ccca122SBrooks Davis  */
765778c12a6SBrooks Davis char *
vis(char * mbdst,int c,int flags,int nextc)766778c12a6SBrooks Davis vis(char *mbdst, int c, int flags, int nextc)
7678ccca122SBrooks Davis {
768778c12a6SBrooks Davis 	char cc[2];
769778c12a6SBrooks Davis 	int ret;
7708ccca122SBrooks Davis 
771778c12a6SBrooks Davis 	cc[0] = c;
772778c12a6SBrooks Davis 	cc[1] = nextc;
7738ccca122SBrooks Davis 
7748dfeba04SBrooks Davis 	ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL);
775778c12a6SBrooks Davis 	if (ret < 0)
7768ccca122SBrooks Davis 		return NULL;
777778c12a6SBrooks Davis 	return mbdst + ret;
7788ccca122SBrooks Davis }
7798ccca122SBrooks Davis 
7808ccca122SBrooks Davis char *
nvis(char * mbdst,size_t dlen,int c,int flags,int nextc)781778c12a6SBrooks Davis nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
7828ccca122SBrooks Davis {
783778c12a6SBrooks Davis 	char cc[2];
784778c12a6SBrooks Davis 	int ret;
7858ccca122SBrooks Davis 
786778c12a6SBrooks Davis 	cc[0] = c;
787778c12a6SBrooks Davis 	cc[1] = nextc;
7888ccca122SBrooks Davis 
7898dfeba04SBrooks Davis 	ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL);
790778c12a6SBrooks Davis 	if (ret < 0)
791778c12a6SBrooks Davis 		return NULL;
792778c12a6SBrooks Davis 	return mbdst + ret;
793778c12a6SBrooks Davis }
7948ccca122SBrooks Davis 
7958ccca122SBrooks Davis /*
796778c12a6SBrooks Davis  * strvis - visually encode characters from src into dst
797778c12a6SBrooks Davis  *
798778c12a6SBrooks Davis  *	Dst must be 4 times the size of src to account for possible
799778c12a6SBrooks Davis  *	expansion.  The length of dst, not including the trailing NULL,
800778c12a6SBrooks Davis  *	is returned.
801778c12a6SBrooks Davis  */
802778c12a6SBrooks Davis 
803778c12a6SBrooks Davis int
strvis(char * mbdst,const char * mbsrc,int flags)804778c12a6SBrooks Davis strvis(char *mbdst, const char *mbsrc, int flags)
805778c12a6SBrooks Davis {
8068dfeba04SBrooks Davis 	return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL);
807778c12a6SBrooks Davis }
808778c12a6SBrooks Davis 
809778c12a6SBrooks Davis int
strnvis(char * mbdst,size_t dlen,const char * mbsrc,int flags)810778c12a6SBrooks Davis strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
811778c12a6SBrooks Davis {
8128dfeba04SBrooks Davis 	return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, "", NULL);
8138dfeba04SBrooks Davis }
8148dfeba04SBrooks Davis 
8158dfeba04SBrooks Davis int
stravis(char ** mbdstp,const char * mbsrc,int flags)8168dfeba04SBrooks Davis stravis(char **mbdstp, const char *mbsrc, int flags)
8178dfeba04SBrooks Davis {
8188dfeba04SBrooks Davis 	*mbdstp = NULL;
8198dfeba04SBrooks Davis 	return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL);
820778c12a6SBrooks Davis }
821778c12a6SBrooks Davis 
822778c12a6SBrooks Davis /*
823778c12a6SBrooks Davis  * strvisx - visually encode characters from src into dst
8248ccca122SBrooks Davis  *
8258ccca122SBrooks Davis  *	Dst must be 4 times the size of src to account for possible
8268ccca122SBrooks Davis  *	expansion.  The length of dst, not including the trailing NULL,
8278ccca122SBrooks Davis  *	is returned.
8288ccca122SBrooks Davis  *
829778c12a6SBrooks Davis  *	Strvisx encodes exactly len characters from src into dst.
8308ccca122SBrooks Davis  *	This is useful for encoding a block of data.
8318ccca122SBrooks Davis  */
8328ccca122SBrooks Davis 
833778c12a6SBrooks Davis int
strvisx(char * mbdst,const char * mbsrc,size_t len,int flags)834778c12a6SBrooks Davis strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
835778c12a6SBrooks Davis {
8368dfeba04SBrooks Davis 	return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL);
8378ccca122SBrooks Davis }
8388ccca122SBrooks Davis 
8398ccca122SBrooks Davis int
strnvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags)840778c12a6SBrooks Davis strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
8418ccca122SBrooks Davis {
8428dfeba04SBrooks Davis 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL);
8438ccca122SBrooks Davis }
8448ccca122SBrooks Davis 
8458ccca122SBrooks Davis int
strenvisx(char * mbdst,size_t dlen,const char * mbsrc,size_t len,int flags,int * cerr_ptr)846778c12a6SBrooks Davis strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
847778c12a6SBrooks Davis     int *cerr_ptr)
8488ccca122SBrooks Davis {
8498dfeba04SBrooks Davis 	return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
8508ccca122SBrooks Davis }
8518ccca122SBrooks Davis #endif
852