]>
git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/vis.c
1 /* $NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
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 */
63 __FBSDID("$FreeBSD$");
64 #define _DIAGASSERT(x) assert(x)
67 #include <sys/types.h>
68 #include <sys/param.h>
77 #if !HAVE_VIS || !HAVE_SVIS
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
91 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
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]
104 _Static_assert(MB_LEN_MAX
<= sizeof(uint64_t), "MB_LEN_MAX is less than 64-bits");
107 * This is do_hvis, for HTTP style (RFC 1808)
110 do_hvis(wchar_t *dst
, wint_t c
, int flags
, wint_t nextc
, const wchar_t *extra
)
114 || c
== L
'$' || c
== L
'-' || c
== L
'_' || c
== L
'.' || c
== L
'+'
116 || c
== L
'!' || c
== L
'*' || c
== L
'\'' || c
== L
'(' || c
== L
')'
118 dst
= do_svis(dst
, c
, flags
, nextc
, extra
);
121 *dst
++ = xtoa(((unsigned int)c
>> 4) & 0xf);
122 *dst
++ = xtoa((unsigned int)c
& 0xf);
129 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
130 * NB: No handling of long lines or CRLF.
133 do_mvis(wchar_t *dst
, wint_t c
, int flags
, wint_t nextc
, const wchar_t *extra
)
136 /* Space at the end of the line */
137 ((iswspace(c
) && (nextc
== L
'\r' || nextc
== L
'\n')) ||
139 (!iswspace(c
) && (c
< 33 || (c
> 60 && c
< 62) || c
> 126)) ||
140 /* Specific char to be escaped */
141 wcschr(L
"#$@[\\]^`{|}~", c
) != NULL
)) {
143 *dst
++ = XTOA(((unsigned int)c
>> 4) & 0xf);
144 *dst
++ = XTOA((unsigned int)c
& 0xf);
146 dst
= do_svis(dst
, c
, flags
, nextc
, extra
);
151 * Output single byte of multibyte character.
154 do_mbyte(wchar_t *dst
, wint_t c
, int flags
, wint_t nextc
, int iswextra
)
156 if (flags
& VIS_CSTYLE
) {
159 *dst
++ = L
'\\'; *dst
++ = L
'n';
162 *dst
++ = L
'\\'; *dst
++ = L
'r';
165 *dst
++ = L
'\\'; *dst
++ = L
'b';
168 *dst
++ = L
'\\'; *dst
++ = L
'a';
171 *dst
++ = L
'\\'; *dst
++ = L
'v';
174 *dst
++ = L
'\\'; *dst
++ = L
't';
177 *dst
++ = L
'\\'; *dst
++ = L
'f';
180 *dst
++ = L
'\\'; *dst
++ = L
's';
183 *dst
++ = L
'\\'; *dst
++ = L
'0';
184 if (iswoctal(nextc
)) {
197 if (iswextra
|| ((c
& 0177) == L
' ') || (flags
& VIS_OCTAL
)) {
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';
203 if ((flags
& VIS_NOSLASH
) == 0)
227 * This is do_vis, the central code of vis.
228 * dst: Pointer to the destination buffer
229 * c: Character to encode
231 * nextc: The character following 'c'
232 * extra: Pointer to the list of extra characters to be
233 * backslash-protected.
236 do_svis(wchar_t *dst
, wint_t c
, int flags
, wint_t nextc
, const wchar_t *extra
)
238 int iswextra
, i
, shft
;
241 iswextra
= wcschr(extra
, c
) != NULL
;
242 if (!iswextra
&& (iswgraph(c
) || iswwhite(c
) ||
243 ((flags
& VIS_SAFE
) && iswsafe(c
)))) {
248 /* See comment in istrsenvisx() output loop, below. */
250 for (i
= sizeof(wmsk
) - 1; i
>= 0; i
--) {
252 bmsk
= (uint64_t)0xffLL
<< shft
;
254 if ((c
& wmsk
) || i
== 0)
255 dst
= do_mbyte(dst
, (wint_t)(
256 (uint64_t)(c
& bmsk
) >> shft
),
257 flags
, nextc
, iswextra
);
263 typedef wchar_t *(*visfun_t
)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
266 * Return the appropriate encoding function depending on the flags given.
271 if (flags
& VIS_HTTPSTYLE
)
273 if (flags
& VIS_MIMESTYLE
)
279 * Expand list of extra characters to not visually encode.
282 makeextralist(int flags
, const char *src
)
288 if ((dst
= calloc(len
+ MAXEXTRAS
, sizeof(*dst
))) == NULL
)
291 if (mbstowcs(dst
, src
, len
) == (size_t)-1) {
293 for (i
= 0; i
< len
; i
++)
294 dst
[i
] = (wint_t)(u_char
)src
[i
];
297 d
= dst
+ wcslen(dst
);
299 if (flags
& VIS_GLOB
) {
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
'\\';
317 * The main internal function.
318 * All user-visible functions call this one.
321 istrsenvisx(char *mbdst
, size_t *dlen
, const char *mbsrc
, size_t mblength
,
322 int flags
, const char *mbextra
, int *cerr_ptr
)
324 wchar_t *dst
, *src
, *pdst
, *psrc
, *start
, *extra
;
329 int clen
= 0, cerr
= 0, error
= -1, i
, shft
;
330 ssize_t mbslength
, maxolen
;
332 _DIAGASSERT(mbdst
!= NULL
);
333 _DIAGASSERT(mbsrc
!= NULL
|| mblength
== 0);
334 _DIAGASSERT(mbextra
!= NULL
);
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.
342 * The vis conversion will be done using the wide char
345 * This will then be converted back to a multibyte string to
346 * return to the caller.
349 /* Allocate space for the wide char strings */
350 psrc
= pdst
= extra
= NULL
;
351 if ((psrc
= calloc(mblength
+ 1, sizeof(*psrc
))) == NULL
)
353 if ((pdst
= calloc((4 * mblength
) + 1, sizeof(*pdst
))) == NULL
)
358 /* Use caller's multibyte conversion error flag. */
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.
368 mbslength
= (ssize_t
)mblength
;
370 * When inputing a single character, must also read in the
371 * next character for nextc, the look-ahead character.
375 while (mbslength
> 0) {
376 /* Convert one multibyte character to wchar_t. */
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
;
387 * NUL in input gives 0 return value. process
388 * as single NUL byte and keep going.
391 /* Advance buffer character pointer. */
393 /* Advance input pointer by number of bytes read. */
395 /* Decrement input byte count. */
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.
408 /* Convert extra argument to list of characters for this mode. */
409 extra
= makeextralist(flags
, mbextra
);
411 if (dlen
&& *dlen
== 0) {
415 *mbdst
= '\0'; /* can't create extra, return "" */
420 /* Look up which processing function to call. */
421 f
= getvisfun(flags
);
424 * Main processing loop.
425 * Call do_Xvis processing function one character at a time
426 * with next character available for look-ahead.
428 for (start
= dst
; len
> 0; len
--) {
430 dst
= (*f
)(dst
, c
, flags
, len
>= 1 ? *src
: L
'\0', extra
);
437 /* Terminate the string in the buffer. */
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().
447 maxolen
= dlen
? *dlen
: (wcslen(start
) * MB_LEN_MAX
+ 1);
449 for (dst
= start
; len
> 0; len
--) {
451 clen
= wctomb(mbdst
, *dst
);
452 if (cerr
|| clen
< 0) {
454 * Conversion error, process as a byte(s) instead.
455 * Examine each byte and higher-order bytes for
457 * 0x000000000000a264 -> a2 64
458 * 0x000000001f00a264 -> 1f 00 a2 64
462 for (i
= sizeof(wmsk
) - 1; i
>= 0; i
--) {
464 bmsk
= (uint64_t)0xffLL
<< shft
;
466 if ((*dst
& wmsk
) || i
== 0)
467 mbdst
[clen
++] = (char)(
468 (uint64_t)(*dst
& bmsk
) >>
473 /* If this character would exceed our output limit, stop. */
474 if (olen
+ clen
> (size_t)maxolen
)
476 /* Advance output pointer by number of bytes written. */
478 /* Advance buffer character pointer. */
480 /* Incrment output character count. */
484 /* Terminate the output string. */
487 /* Pass conversion error flag out. */
504 istrsenvisxl(char *mbdst
, size_t *dlen
, const char *mbsrc
,
505 int flags
, const char *mbextra
, int *cerr_ptr
)
507 return istrsenvisx(mbdst
, dlen
, mbsrc
,
508 mbsrc
!= NULL
? strlen(mbsrc
) : 0, flags
, mbextra
, cerr_ptr
);
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.
522 svis(char *mbdst
, int c
, int flags
, int nextc
, const char *mbextra
)
530 ret
= istrsenvisx(mbdst
, NULL
, cc
, 1, flags
, mbextra
, NULL
);
537 snvis(char *mbdst
, size_t dlen
, int c
, int flags
, int nextc
, const char *mbextra
)
545 ret
= istrsenvisx(mbdst
, &dlen
, cc
, 1, flags
, mbextra
, NULL
);
552 strsvis(char *mbdst
, const char *mbsrc
, int flags
, const char *mbextra
)
554 return istrsenvisxl(mbdst
, NULL
, mbsrc
, flags
, mbextra
, NULL
);
558 strsnvis(char *mbdst
, size_t dlen
, const char *mbsrc
, int flags
, const char *mbextra
)
560 return istrsenvisxl(mbdst
, &dlen
, mbsrc
, flags
, mbextra
, NULL
);
564 strsvisx(char *mbdst
, const char *mbsrc
, size_t len
, int flags
, const char *mbextra
)
566 return istrsenvisx(mbdst
, NULL
, mbsrc
, len
, flags
, mbextra
, NULL
);
570 strsnvisx(char *mbdst
, size_t dlen
, const char *mbsrc
, size_t len
, int flags
,
573 return istrsenvisx(mbdst
, &dlen
, mbsrc
, len
, flags
, mbextra
, NULL
);
577 strsenvisx(char *mbdst
, size_t dlen
, const char *mbsrc
, size_t len
, int flags
,
578 const char *mbextra
, int *cerr_ptr
)
580 return istrsenvisx(mbdst
, &dlen
, mbsrc
, len
, flags
, mbextra
, cerr_ptr
);
586 * vis - visually encode characters
589 vis(char *mbdst
, int c
, int flags
, int nextc
)
597 ret
= istrsenvisx(mbdst
, NULL
, cc
, 1, flags
, "", NULL
);
604 nvis(char *mbdst
, size_t dlen
, int c
, int flags
, int nextc
)
612 ret
= istrsenvisx(mbdst
, &dlen
, cc
, 1, flags
, "", NULL
);
619 * strvis - visually encode characters from src into dst
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,
627 strvis(char *mbdst
, const char *mbsrc
, int flags
)
629 return istrsenvisxl(mbdst
, NULL
, mbsrc
, flags
, "", NULL
);
633 strnvis(char *mbdst
, size_t dlen
, const char *mbsrc
, int flags
)
635 return istrsenvisxl(mbdst
, &dlen
, mbsrc
, flags
, "", NULL
);
639 * strvisx - visually encode characters from src into dst
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,
645 * Strvisx encodes exactly len characters from src into dst.
646 * This is useful for encoding a block of data.
650 strvisx(char *mbdst
, const char *mbsrc
, size_t len
, int flags
)
652 return istrsenvisx(mbdst
, NULL
, mbsrc
, len
, flags
, "", NULL
);
656 strnvisx(char *mbdst
, size_t dlen
, const char *mbsrc
, size_t len
, int flags
)
658 return istrsenvisx(mbdst
, &dlen
, mbsrc
, len
, flags
, "", NULL
);
662 strenvisx(char *mbdst
, size_t dlen
, const char *mbsrc
, size_t len
, int flags
,
665 return istrsenvisx(mbdst
, &dlen
, mbsrc
, len
, flags
, "", cerr_ptr
);