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