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