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