diff options
author | brooks <brooks@FreeBSD.org> | 2012-12-18 16:37:24 +0000 |
---|---|---|
committer | brooks <brooks@FreeBSD.org> | 2012-12-18 16:37:24 +0000 |
commit | 25561669c9e3d0a05c2bc54cce35d2f8211c4962 (patch) | |
tree | af2fc41b0960db64adb617ce885a81fe748f415c /lib | |
parent | 84cec43191f814fd6b8dda3470035312d342caff (diff) | |
parent | 8b7c283f17726c97bbffe5e3826bf4ff2b1599cf (diff) | |
download | FreeBSD-src-25561669c9e3d0a05c2bc54cce35d2f8211c4962.zip FreeBSD-src-25561669c9e3d0a05c2bc54cce35d2f8211c4962.tar.gz |
Replace our implementation of the vis(3) and unvis(3) APIs with
NetBSD's. This output size limited versions of vis and unvis functions
as well as a set of vis variants that allow arbitrary characters to be
specified for encoding.
Finally, MIME Quoted-Printable encoding as described in RFC 2045 is
supported.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/gen/Makefile.inc | 7 | ||||
-rw-r--r-- | lib/libc/gen/Symbol.map | 17 | ||||
-rw-r--r-- | lib/libc/gen/unvis-compat.c | 46 | ||||
-rw-r--r-- | lib/libc/gen/unvis.3 | 200 | ||||
-rw-r--r-- | lib/libc/gen/unvis.c | 293 | ||||
-rw-r--r-- | lib/libc/gen/vis.3 | 308 | ||||
-rw-r--r-- | lib/libc/gen/vis.c | 201 |
7 files changed, 65 insertions, 1007 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 906f4ce..abface8 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -32,13 +32,16 @@ SRCS+= __getosreldate.c __xuname.c \ sigsetops.c sleep.c srand48.c statvfs.c stringlist.c strtofflags.c \ sysconf.c sysctl.c sysctlbyname.c sysctlnametomib.c \ syslog.c telldir.c termios.c time.c times.c timezone.c tls.c \ - ttyname.c ttyslot.c ualarm.c ulimit.c uname.c unvis.c \ - usleep.c utime.c utxdb.c valloc.c vis.c wait.c wait3.c waitpid.c \ + ttyname.c ttyslot.c ualarm.c ulimit.c uname.c unvis-compat.c \ + usleep.c utime.c utxdb.c valloc.c wait.c wait3.c waitpid.c \ waitid.c wordexp.c .PATH: ${.CURDIR}/../../contrib/libc-pwcache SRCS+= pwcache.c pwcache.h +.PATH: ${.CURDIR}/../../contrib/libc-vis +SRCS+= unvis.c vis.c + MISRCS+=modf.c CANCELPOINTS_SRCS=sem.c sem_new.c diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index f5d4f71..0c6f48e 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -298,9 +298,6 @@ FBSD_1.0 { ualarm; ulimit; uname; - unvis; - strunvis; - strunvisx; usleep; utime; valloc; @@ -388,9 +385,23 @@ FBSD_1.3 { __FreeBSD_libc_enter_restricted_mode; getcontextx; gid_from_group; + nvis; pwcache_userdb; pwcache_groupdb; + snvis; + strnunvis; + strnunvisx; + strunvis; + strunvisx; + strnvis; + strnvisx; + strsnvis; + strsnvisx; + strsvis; + strsvisx; + svis; uid_from_user; + unvis; waitid; }; diff --git a/lib/libc/gen/unvis-compat.c b/lib/libc/gen/unvis-compat.c new file mode 100644 index 0000000..080143e --- /dev/null +++ b/lib/libc/gen/unvis-compat.c @@ -0,0 +1,46 @@ +/*- + * Copyright (c) 2012 SRI International + * All rights reserved. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <vis.h> + +#define _UNVIS_END 1 + +int +__unvis_44bsd(char *cp, int c, int *astate, int flag) +{ + + if (flag & _UNVIS_END) + flag = (flag & ~_UNVIS_END) ^ UNVIS_END; + return unvis(cp, c, astate, flag); +} + +__sym_compat(unvis, __vis_44bsd, FBSD_1.0); diff --git a/lib/libc/gen/unvis.3 b/lib/libc/gen/unvis.3 deleted file mode 100644 index 9164f79..0000000 --- a/lib/libc/gen/unvis.3 +++ /dev/null @@ -1,200 +0,0 @@ -.\" Copyright (c) 1989, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 4. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93 -.\" $FreeBSD$ -.\" -.Dd December 11, 1993 -.Dt UNVIS 3 -.Os -.Sh NAME -.Nm unvis , -.Nm strunvis -.Nd decode a visual representation of characters -.Sh LIBRARY -.Lb libc -.Sh SYNOPSIS -.In vis.h -.Ft int -.Fn unvis "char *cp" "int c" "int *astate" "int flag" -.Ft int -.Fn strunvis "char *dst" "const char *src" -.Ft int -.Fn strunvisx "char *dst" "const char *src" "int flag" -.Sh DESCRIPTION -The -.Fn unvis , -.Fn strunvis -and -.Fn strunvisx -functions -are used to decode a visual representation of characters, as produced -by the -.Xr vis 3 -function, back into -the original form. -Unvis is called with successive characters in -.Fa c -until a valid -sequence is recognized, at which time the decoded character is -available at the character pointed to by -.Fa cp . -Strunvis decodes the -characters pointed to by -.Fa src -into the buffer pointed to by -.Fa dst . -.Pp -The -.Fn strunvis -function -simply copies -.Fa src -to -.Fa dst , -decoding any escape sequences along the way, -and returns the number of characters placed into -.Fa dst , -or \-1 if an -invalid escape sequence was detected. -The size of -.Fa dst -should be -equal to the size of -.Fa src -(that is, no expansion takes place during -decoding). -.Pp -The -.Fn strunvisx -function does the same as the -.Fn strunvis -function, -but it allows you to add a flag that specifies the style the string -.Fa src -is encoded with. -Currently, the only supported flag is -.Dv VIS_HTTPSTYLE . -.Pp -The -.Fn unvis -function -implements a state machine that can be used to decode an arbitrary -stream of bytes. -All state associated with the bytes being decoded -is stored outside the -.Fn unvis -function (that is, a pointer to the state is passed in), so -calls decoding different streams can be freely intermixed. -To -start decoding a stream of bytes, first initialize an integer -to zero. -Call -.Fn unvis -with each successive byte, along with a pointer -to this integer, and a pointer to a destination character. -The -.Fn unvis -function -has several return codes that must be handled properly. -They are: -.Bl -tag -width UNVIS_VALIDPUSH -.It Li \&0 (zero) -Another character is necessary; nothing has been recognized yet. -.It Dv UNVIS_VALID -A valid character has been recognized and is available at the location -pointed to by cp. -.It Dv UNVIS_VALIDPUSH -A valid character has been recognized and is available at the location -pointed to by cp; however, the character currently passed in should -be passed in again. -.It Dv UNVIS_NOCHAR -A valid sequence was detected, but no character was produced. -This -return code is necessary to indicate a logical break between characters. -.It Dv UNVIS_SYNBAD -An invalid escape sequence was detected, or the decoder is in an -unknown state. -The decoder is placed into the starting state. -.El -.Pp -When all bytes in the stream have been processed, call -.Fn unvis -one more time with -.Fa flag -set to -.Dv UNVIS_END -to extract any remaining character (the character passed in is ignored). -.Pp -The -.Fa flag -argument is also used to specify the encoding style of the source. -If set to -.Dv VIS_HTTPSTYLE , -.Fn unvis -will decode URI strings as specified in RFC 1808. -.Pp -The following code fragment illustrates a proper use of -.Fn unvis . -.Bd -literal -offset indent -int state = 0; -char out; - -while ((ch = getchar()) != EOF) { -again: - switch(unvis(&out, ch, &state, 0)) { - case 0: - case UNVIS_NOCHAR: - break; - case UNVIS_VALID: - (void) putchar(out); - break; - case UNVIS_VALIDPUSH: - (void) putchar(out); - goto again; - case UNVIS_SYNBAD: - (void)fprintf(stderr, "bad sequence!\en"); - exit(1); - } -} -if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID) - (void) putchar(out); -.Ed -.Sh SEE ALSO -.Xr vis 1 , -.Xr vis 3 -.Rs -.%A R. Fielding -.%T Relative Uniform Resource Locators -.%O RFC1808 -.Re -.Sh HISTORY -The -.Fn unvis -function -first appeared in -.Bx 4.4 . diff --git a/lib/libc/gen/unvis.c b/lib/libc/gen/unvis.c deleted file mode 100644 index 9069e55..0000000 --- a/lib/libc/gen/unvis.c +++ /dev/null @@ -1,293 +0,0 @@ -/*- - * Copyright (c) 1989, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/types.h> -#include <ctype.h> -#include <vis.h> - -/* - * decode driven by state machine - */ -#define S_GROUND 0 /* haven't seen escape char */ -#define S_START 1 /* start decoding special sequence */ -#define S_META 2 /* metachar started (M) */ -#define S_META1 3 /* metachar more, regular char (-) */ -#define S_CTRL 4 /* control char started (^) */ -#define S_OCTAL2 5 /* octal digit 2 */ -#define S_OCTAL3 6 /* octal digit 3 */ -#define S_HEX2 7 /* hex digit 2 */ - -#define S_HTTP 0x080 /* %HEXHEX escape */ - -#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') -#define ishex(c) ((((u_char)(c)) >= '0' && ((u_char)(c)) <= '9') || (((u_char)(c)) >= 'a' && ((u_char)(c)) <= 'f')) - -/* - * unvis - decode characters previously encoded by vis - */ -int -unvis(char *cp, int c, int *astate, int flag) -{ - - if (flag & UNVIS_END) { - if (*astate == S_OCTAL2 || *astate == S_OCTAL3) { - *astate = S_GROUND; - return (UNVIS_VALID); - } - return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD); - } - - switch (*astate & ~S_HTTP) { - - case S_GROUND: - *cp = 0; - if (c == '\\') { - *astate = S_START; - return (0); - } - if (flag & VIS_HTTPSTYLE && c == '%') { - *astate = S_START | S_HTTP; - return (0); - } - *cp = c; - return (UNVIS_VALID); - - case S_START: - if (*astate & S_HTTP) { - if (ishex(tolower(c))) { - *cp = isdigit(c) ? (c - '0') : (tolower(c) - 'a'); - *astate = S_HEX2; - return (0); - } - } - switch(c) { - case '\\': - *cp = c; - *astate = S_GROUND; - return (UNVIS_VALID); - case '0': case '1': case '2': case '3': - case '4': case '5': case '6': case '7': - *cp = (c - '0'); - *astate = S_OCTAL2; - return (0); - case 'M': - *cp = 0200; - *astate = S_META; - return (0); - case '^': - *astate = S_CTRL; - return (0); - case 'n': - *cp = '\n'; - *astate = S_GROUND; - return (UNVIS_VALID); - case 'r': - *cp = '\r'; - *astate = S_GROUND; - return (UNVIS_VALID); - case 'b': - *cp = '\b'; - *astate = S_GROUND; - return (UNVIS_VALID); - case 'a': - *cp = '\007'; - *astate = S_GROUND; - return (UNVIS_VALID); - case 'v': - *cp = '\v'; - *astate = S_GROUND; - return (UNVIS_VALID); - case 't': - *cp = '\t'; - *astate = S_GROUND; - return (UNVIS_VALID); - case 'f': - *cp = '\f'; - *astate = S_GROUND; - return (UNVIS_VALID); - case 's': - *cp = ' '; - *astate = S_GROUND; - return (UNVIS_VALID); - case 'E': - *cp = '\033'; - *astate = S_GROUND; - return (UNVIS_VALID); - case '\n': - /* - * hidden newline - */ - *astate = S_GROUND; - return (UNVIS_NOCHAR); - case '$': - /* - * hidden marker - */ - *astate = S_GROUND; - return (UNVIS_NOCHAR); - } - *astate = S_GROUND; - return (UNVIS_SYNBAD); - - case S_META: - if (c == '-') - *astate = S_META1; - else if (c == '^') - *astate = S_CTRL; - else { - *astate = S_GROUND; - return (UNVIS_SYNBAD); - } - return (0); - - case S_META1: - *astate = S_GROUND; - *cp |= c; - return (UNVIS_VALID); - - case S_CTRL: - if (c == '?') - *cp |= 0177; - else - *cp |= c & 037; - *astate = S_GROUND; - return (UNVIS_VALID); - - case S_OCTAL2: /* second possible octal digit */ - if (isoctal(c)) { - /* - * yes - and maybe a third - */ - *cp = (*cp << 3) + (c - '0'); - *astate = S_OCTAL3; - return (0); - } - /* - * no - done with current sequence, push back passed char - */ - *astate = S_GROUND; - return (UNVIS_VALIDPUSH); - - case S_OCTAL3: /* third possible octal digit */ - *astate = S_GROUND; - if (isoctal(c)) { - *cp = (*cp << 3) + (c - '0'); - return (UNVIS_VALID); - } - /* - * we were done, push back passed char - */ - return (UNVIS_VALIDPUSH); - - case S_HEX2: /* second mandatory hex digit */ - if (ishex(tolower(c))) { - *cp = (isdigit(c) ? (*cp << 4) + (c - '0') : (*cp << 4) + (tolower(c) - 'a' + 10)); - } - *astate = S_GROUND; - return (UNVIS_VALID); - - default: - /* - * decoder in unknown state - (probably uninitialized) - */ - *astate = S_GROUND; - return (UNVIS_SYNBAD); - } -} - -/* - * strunvis - decode src into dst - * - * Number of chars decoded into dst is returned, -1 on error. - * Dst is null terminated. - */ - -int -strunvis(char *dst, const char *src) -{ - char c; - char *start = dst; - int state = 0; - - while ( (c = *src++) ) { - again: - switch (unvis(dst, c, &state, 0)) { - case UNVIS_VALID: - dst++; - break; - case UNVIS_VALIDPUSH: - dst++; - goto again; - case 0: - case UNVIS_NOCHAR: - break; - default: - return (-1); - } - } - if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) - dst++; - *dst = '\0'; - return (dst - start); -} - -int -strunvisx(char *dst, const char *src, int flag) -{ - char c; - char *start = dst; - int state = 0; - - while ( (c = *src++) ) { - again: - switch (unvis(dst, c, &state, flag)) { - case UNVIS_VALID: - dst++; - break; - case UNVIS_VALIDPUSH: - dst++; - goto again; - case 0: - case UNVIS_NOCHAR: - break; - default: - return (-1); - } - } - if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) - dst++; - *dst = '\0'; - return (dst - start); -} diff --git a/lib/libc/gen/vis.3 b/lib/libc/gen/vis.3 deleted file mode 100644 index 8ec16eb..0000000 --- a/lib/libc/gen/vis.3 +++ /dev/null @@ -1,308 +0,0 @@ -.\" Copyright (c) 1989, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 4. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" From: @(#)vis.3 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD$ -.\" -.Dd April 9, 2006 -.Dt VIS 3 -.Os -.Sh NAME -.Nm vis -.Nd visually encode characters -.Sh LIBRARY -.Lb libc -.Sh SYNOPSIS -.In vis.h -.Ft char * -.Fn vis "char *dst" "int c" "int flag" "int nextc" -.Ft int -.Fn strvis "char *dst" "const char *src" "int flag" -.Ft int -.Fn strvisx "char *dst" "const char *src" "size_t len" "int flag" -.Sh DESCRIPTION -The -.Fn vis -function -copies into -.Fa dst -a string which represents the character -.Fa c . -If -.Fa c -needs no encoding, it is copied in unaltered. -The string is -null terminated, and a pointer to the end of the string is -returned. -The maximum length of any encoding is four -characters (not including the trailing -.Dv NUL ) ; -thus, when -encoding a set of characters into a buffer, the size of the buffer should -be four times the number of characters encoded, plus one for the trailing -.Dv NUL . -The -.Fa flag -argument is used for altering the default range of -characters considered for encoding and for altering the visual -representation. -The additional character, -.Fa nextc , -is only used when selecting the -.Dv VIS_CSTYLE -encoding format (explained below). -.Pp -The -.Fn strvis -and -.Fn strvisx -functions copy into -.Fa dst -a visual representation of -the string -.Fa src . -The -.Fn strvis -function encodes characters from -.Fa src -up to the -first -.Dv NUL . -The -.Fn strvisx -function encodes exactly -.Fa len -characters from -.Fa src -(this -is useful for encoding a block of data that may contain -.Dv NUL Ns 's ) . -Both forms -.Dv NUL -terminate -.Fa dst . -The size of -.Fa dst -must be four times the number -of characters encoded from -.Fa src -(plus one for the -.Dv NUL ) . -Both -forms return the number of characters in dst (not including -the trailing -.Dv NUL ) . -.Pp -The encoding is a unique, invertible representation composed entirely of -graphic characters; it can be decoded back into the original form using -the -.Xr unvis 3 -or -.Xr strunvis 3 -functions. -.Pp -There are two parameters that can be controlled: the range of -characters that are encoded, and the type -of representation used. -By default, all non-graphic characters -except space, tab, and newline are encoded. -(See -.Xr isgraph 3 . ) -The following flags -alter this: -.Bl -tag -width VIS_WHITEX -.It Dv VIS_GLOB -Also encode magic characters -.Ql ( * , -.Ql \&? , -.Ql \&[ -and -.Ql # ) -recognized by -.Xr glob 3 . -.It Dv VIS_SP -Also encode space. -.It Dv VIS_TAB -Also encode tab. -.It Dv VIS_NL -Also encode newline. -.It Dv VIS_WHITE -Synonym for -.Dv VIS_SP -\&| -.Dv VIS_TAB -\&| -.Dv VIS_NL . -.It Dv VIS_SAFE -Only encode "unsafe" characters. -Unsafe means control -characters which may cause common terminals to perform -unexpected functions. -Currently this form allows space, -tab, newline, backspace, bell, and return - in addition -to all graphic characters - unencoded. -.El -.Pp -There are four forms of encoding. -Most forms use the backslash character -.Ql \e -to introduce a special -sequence; two backslashes are used to represent a real backslash. -These are the visual formats: -.Bl -tag -width VIS_HTTPSTYLE -.It (default) -Use an -.Ql M -to represent meta characters (characters with the 8th -bit set), and use caret -.Ql ^ -to represent control characters see -.Pf ( Xr iscntrl 3 ) . -The following formats are used: -.Bl -tag -width xxxxx -.It Dv \e^C -Represents the control character -.Ql C . -Spans characters -.Ql \e000 -through -.Ql \e037 , -and -.Ql \e177 -(as -.Ql \e^? ) . -.It Dv \eM-C -Represents character -.Ql C -with the 8th bit set. -Spans characters -.Ql \e241 -through -.Ql \e376 . -.It Dv \eM^C -Represents control character -.Ql C -with the 8th bit set. -Spans characters -.Ql \e200 -through -.Ql \e237 , -and -.Ql \e377 -(as -.Ql \eM^? ) . -.It Dv \e040 -Represents -.Tn ASCII -space. -.It Dv \e240 -Represents Meta-space. -.El -.Pp -.It Dv VIS_CSTYLE -Use C-style backslash sequences to represent standard non-printable -characters. -The following sequences are used to represent the indicated characters: -.Pp -.Bl -tag -width ".Li \e0" -offset indent -compact -.It Li \ea -.Dv BEL No (007) -.It Li \eb -.Dv BS No (010) -.It Li \ef -.Dv NP No (014) -.It Li \en -.Dv NL No (012) -.It Li \er -.Dv CR No (015) -.It Li \es -.Dv SP No (040) -.It Li \et -.Dv HT No (011) -.It Li \ev -.Dv VT No (013) -.It Li \e0 -.Dv NUL No (000) -.El -.Pp -When using this format, the -.Fa nextc -argument is looked at to determine -if a -.Dv NUL -character can be encoded as -.Ql \e0 -instead of -.Ql \e000 . -If -.Fa nextc -is an octal digit, the latter representation is used to -avoid ambiguity. -.It Dv VIS_HTTPSTYLE -Use URI encoding as described in RFC 1808. -The form is -.Ql %dd -where -.Ar d -represents a hexadecimal digit. -.It Dv VIS_OCTAL -Use a three digit octal sequence. -The form is -.Ql \eddd -where -.Ar d -represents an octal digit. -.El -.Pp -There is one additional flag, -.Dv VIS_NOSLASH , -which inhibits the -doubling of backslashes and the backslash before the default -format (that is, control characters are represented by -.Ql ^C -and -meta characters as -.Ql M-C ) . -With this flag set, the encoding is -ambiguous and non-invertible. -.Sh SEE ALSO -.Xr unvis 1 , -.Xr unvis 3 -.Rs -.%A R. Fielding -.%T Relative Uniform Resource Locators -.%O RFC1808 -.Re -.Sh HISTORY -These functions first appeared in -.Bx 4.4 . -.Sh BUGS -The -.Nm -family of functions do not recognize multibyte characters, and thus -may consider them to be non-printable when they are in fact printable -(and vice versa.) diff --git a/lib/libc/gen/vis.c b/lib/libc/gen/vis.c deleted file mode 100644 index 362f4dc..0000000 --- a/lib/libc/gen/vis.c +++ /dev/null @@ -1,201 +0,0 @@ -/*- - * Copyright (c) 1989, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 7/19/93"; -#endif /* LIBC_SCCS and not lint */ -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/types.h> -#include <limits.h> -#include <ctype.h> -#include <stdio.h> -#include <vis.h> - -#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') - -/* - * vis - visually encode characters - */ -char * -vis(dst, c, flag, nextc) - char *dst; - int c, nextc; - int flag; -{ - c = (unsigned char)c; - - if (flag & VIS_HTTPSTYLE) { - /* Described in RFC 1808 */ - if (!(isalnum(c) /* alpha-numeric */ - /* safe */ - || c == '$' || c == '-' || c == '_' || c == '.' || c == '+' - /* extra */ - || c == '!' || c == '*' || c == '\'' || c == '(' - || c == ')' || c == ',')) { - *dst++ = '%'; - snprintf(dst, 4, (c < 16 ? "0%X" : "%X"), c); - dst += 2; - goto done; - } - } - - if ((flag & VIS_GLOB) && - (c == '*' || c == '?' || c == '[' || c == '#')) - ; - else if (isgraph(c) || - ((flag & VIS_SP) == 0 && c == ' ') || - ((flag & VIS_TAB) == 0 && c == '\t') || - ((flag & VIS_NL) == 0 && c == '\n') || - ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) { - *dst++ = c; - if (c == '\\' && (flag & VIS_NOSLASH) == 0) - *dst++ = '\\'; - *dst = '\0'; - return (dst); - } - - if (flag & VIS_CSTYLE) { - switch(c) { - case '\n': - *dst++ = '\\'; - *dst++ = 'n'; - goto done; - case '\r': - *dst++ = '\\'; - *dst++ = 'r'; - goto done; - case '\b': - *dst++ = '\\'; - *dst++ = 'b'; - goto done; - case '\a': - *dst++ = '\\'; - *dst++ = 'a'; - goto done; - case '\v': - *dst++ = '\\'; - *dst++ = 'v'; - goto done; - case '\t': - *dst++ = '\\'; - *dst++ = 't'; - goto done; - case '\f': - *dst++ = '\\'; - *dst++ = 'f'; - goto done; - case ' ': - *dst++ = '\\'; - *dst++ = 's'; - goto done; - case '\0': - *dst++ = '\\'; - *dst++ = '0'; - if (isoctal(nextc)) { - *dst++ = '0'; - *dst++ = '0'; - } - goto done; - } - } - if (((c & 0177) == ' ') || isgraph(c) || (flag & VIS_OCTAL)) { - *dst++ = '\\'; - *dst++ = ((u_char)c >> 6 & 07) + '0'; - *dst++ = ((u_char)c >> 3 & 07) + '0'; - *dst++ = ((u_char)c & 07) + '0'; - goto done; - } - if ((flag & VIS_NOSLASH) == 0) - *dst++ = '\\'; - if (c & 0200) { - c &= 0177; - *dst++ = 'M'; - } - if (iscntrl(c)) { - *dst++ = '^'; - if (c == 0177) - *dst++ = '?'; - else - *dst++ = c + '@'; - } else { - *dst++ = '-'; - *dst++ = c; - } -done: - *dst = '\0'; - return (dst); -} - -/* - * strvis, strvisx - visually encode characters from src into dst - * - * Dst must be 4 times the size of src to account for possible - * expansion. The length of dst, not including the trailing NUL, - * is returned. - * - * Strvisx encodes exactly len bytes from src into dst. - * This is useful for encoding a block of data. - */ -int -strvis(dst, src, flag) - char *dst; - const char *src; - int flag; -{ - char c; - char *start; - - for (start = dst; (c = *src); ) - dst = vis(dst, c, flag, *++src); - *dst = '\0'; - return (dst - start); -} - -int -strvisx(dst, src, len, flag) - char *dst; - const char *src; - size_t len; - int flag; -{ - int c; - char *start; - - for (start = dst; len > 1; len--) { - c = *src; - dst = vis(dst, c, flag, *++src); - } - if (len) - dst = vis(dst, *src, flag, '\0'); - *dst = '\0'; - - return (dst - start); -} |