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