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