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