lobase

Linux port of OpenBSDs userland.
Log | Files | Refs | README

commit 2eb043784fdc12b618a76fe0d929cd2d77a9d724
parent 7e765cdd84cb177ed08550be48ae94ef235c335b
Author: Duncaen <mail@duncano.de>
Date:   Wed,  8 Mar 2017 19:26:38 +0100

lib/libopenbsd: move headers to include and move some code into subdirs

Diffstat:
include/compat.h | 154+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
include/readpassphrase.h | 37+++++++++++++++++++++++++++++++++++++
include/unistd.h | 1+
include/vis.h | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/Makefile | 82++++++++++++++-----------------------------------------------------------------
lib/libopenbsd/chacha.h | 48------------------------------------------------
lib/libopenbsd/config.mk.in | 4----
lib/libopenbsd/crypt/chacha-merged.c | 270-------------------------------------------------------------------------------
lib/libopenbsd/crypt/chacha.c | 77-----------------------------------------------------------------------------
lib/libopenbsd/crypt/chacha_private.h | 2+-
lib/libopenbsd/gen/unvis.3 | 195+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/gen/unvis.c | 286+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/gen/vis.3 | 395+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/gen/vis.c | 243+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/getfsstat | 0
lib/libopenbsd/getfsstat.2 | 149-------------------------------------------------------------------------------
lib/libopenbsd/getfsstat.c | 120-------------------------------------------------------------------------------
lib/libopenbsd/getmntinfo.c | 2++
lib/libopenbsd/include/namespace.h | 164+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/openbsd.h | 162-------------------------------------------------------------------------------
lib/libopenbsd/pledge-noop.c | 2--
lib/libopenbsd/readpassphrase.h | 37-------------------------------------
lib/libopenbsd/stdlib/malloc.3 | 526+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/string/strlcpy.3 | 188+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/libopenbsd/unvis.3 | 195-------------------------------------------------------------------------------
lib/libopenbsd/unvis.c | 286-------------------------------------------------------------------------------
lib/libopenbsd/util.h | 2--
lib/libopenbsd/vis.3 | 395-------------------------------------------------------------------------------
lib/libopenbsd/vis.c | 243-------------------------------------------------------------------------------
lib/libopenbsd/vis.h | 91-------------------------------------------------------------------------------
30 files changed, 2297 insertions(+), 2150 deletions(-)

diff --git a/include/compat.h b/include/compat.h @@ -0,0 +1,154 @@ +#ifndef _COMPAT_H_ +#define _COMPAT_H_ + +#define FAIL_INSTEAD_OF_TRYING_FALLBACK + +#define __warn_references(x,y) + +#ifndef ALIGN +/* XXX: x86_64 only, see sys/arch/$arch/include/_types.h */ +#define ALIGNBYTES (sizeof(long) - 1) +#define ALIGN(p) (((size_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) +#endif + +#define __UNUSED __attribute__ ((unused)) + +#define __dead __attribute__((__noreturn__)) + +#define __BEGIN_DECLS +#define __END_DECLS + +#ifndef __CONCAT +#define __CONCAT(x,y) x ## y +#endif +#ifndef __STRING +#define __STRING(x) #x +#endif + +#undef __weak_alias +#define __weak_alias(new, old) \ + extern __typeof(old) new __attribute__((weak, alias(#old))) + +#ifndef SA_LEN +#define SA_LEN(X) \ + (((struct sockaddr*)(X))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : \ + ((struct sockaddr*)(X))->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr)) +#endif + +#define _PW_BUF_LEN 1024 +#define _GR_BUF_LEN 1024 + +#define NOFILE_MAX NOFILE + +#if !defined(HAVE_ATTRIBUTE__BOUNDED__) +#define __bounded__(x, y, z) +#endif + +#if !defined(DEF_WEAK) +#define DEF_WEAK(x) +#endif + +#if !defined(DEF_STRONG) +#define DEF_STRONG(x) +#endif + +#if !defined(PROTO_NORMAL) +#define PROTO_NORMAL(x) +#endif + +/* sys/sys/param.h */ +/* + * File system parameters and macros. + * + * The file system is made out of blocks of at most MAXBSIZE units, with + * smaller units (fragments) only in the last direct block. MAXBSIZE + * primarily determines the size of buffers in the buffer pool. It may be + * made larger without any effect on existing file systems; however making + * it smaller makes some file systems unmountable. + */ +#define MAXBSIZE (64 * 1024) + +#ifndef NL_TEXTMAX +#define NL_TEXTMAX 2048 +#endif + +#ifndef howmany +#define howmany(n,d) (((n)+((d)-1))/(d)) +#endif + +/* API definitions lifted from OpenBSD src/include */ + +/* pwd.h */ +#define _PW_NAME_LEN 63 + +/* stdlib.h */ + +/* unistd.h */ +#ifndef HAVE_EXECVPE +int execvpe(const char *, char *const *, char *const *); +#endif /* !HAVE_EXECVPE */ +#ifndef HAVE_SETRESUID +/* int setresuid(uid_t, uid_t, uid_t); */ +#endif /* !HAVE_SETRESUID */ + +#ifndef HAVE_PLEDGE +int pledge(const char *, const char *[]); +#endif /* !HAVE_PLEDGE */ + +#ifndef HAVE_SETPROGNAME +const char * getprogname(void); +void setprogname(const char *progname); +#endif /* !HAVE_SETPROGNAME */ + +/* pwcache.c */ +/* char *user_from_uid(uid_t, int); */ +/* char *group_from_gid(gid_t gid, int); */ + +/* getbsize.c */ +char *getbsize(int *, long *); + +#define strtoq strtoll + +/* inttypes.h */ +/* +intmax_t strtoimax(const char *, char **, int); +uintmax_t strtoumax(const char *, char **, int); +*/ + +/* #define d_namlen d_reclen */ +#if !defined(_DIRENT_HAVE_NAMLEN) +#define D_NAMLEN(x) \ + strnlen((x)->d_name, (x)->d_reclen) +#else +#define D_NAMLEN(x) \ + (x)->d_namlen +#endif + +/* XXX: read dynamically? */ +#define UID_MAX 60000 +#define GID_MAX 60000 + +/* sys/sys/stat.h */ +#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 00777 */ +#define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) /* 00666 */ +#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) + +#define S_ISTXT S_ISVTX + +/* lib/libc/include/thread_private.h */ +#define _MUTEX_LOCK(mutex) do {} while (0) +#define _MUTEX_UNLOCK(mutex) do {} while (0) + +#define SHA512_Update SHA512Update +#define SHA512_CTX SHA2_CTX +#define SHA512_Init SHA512Init +#define SHA512_Final SHA512Final + +/* setproctitle.c */ +void setproctitle(const char *, ...); + +/* pw_dup.c */ +struct passwd *pw_dup(const struct passwd *); + +int issetugid(void); +#endif diff --git a/include/readpassphrase.h b/include/readpassphrase.h @@ -0,0 +1,37 @@ +/* $OpenBSD: readpassphrase.h,v 1.5 2003/06/17 21:56:23 millert Exp $ */ + +/* + * Copyright (c) 2000, 2002 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ + +/* OPENBSD ORIGINAL: include/readpassphrase.h */ + +#ifndef HAVE_READPASSPHRASE + +#define RPP_ECHO_OFF 0x00 /* Turn off echo (default). */ +#define RPP_ECHO_ON 0x01 /* Leave echo on. */ +#define RPP_REQUIRE_TTY 0x02 /* Fail if there is no tty. */ +#define RPP_FORCELOWER 0x04 /* Force input to lower case. */ +#define RPP_FORCEUPPER 0x08 /* Force input to upper case. */ +#define RPP_SEVENBIT 0x10 /* Strip the high bit from input. */ +#define RPP_STDIN 0x20 /* Read from stdin, not /dev/tty */ + +char * readpassphrase(const char *, char *, size_t, int); + +#endif diff --git a/include/unistd.h b/include/unistd.h @@ -51,6 +51,7 @@ int getresuid(uid_t *, uid_t *, uid_t *); void *setmode(const char *); int setresgid(gid_t, gid_t, gid_t); int setresuid(uid_t, uid_t, uid_t); +int pledge(const char *, const char *[]); #endif /* _COMPAT_UNISTD_H_ */ diff --git a/include/vis.h b/include/vis.h @@ -0,0 +1,91 @@ +/* $OpenBSD: vis.h,v 1.15 2015/07/20 01:52:27 millert Exp $ */ +/* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */ + +/*- + * Copyright (c) 1990 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. + * 3. 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. + * + * @(#)vis.h 5.9 (Berkeley) 4/3/91 + */ + +#ifndef _VIS_H_ +#define _VIS_H_ + +/* + * to select alternate encoding format + */ +#define VIS_OCTAL 0x01 /* use octal \ddd format */ +#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropriate */ + +/* + * to alter set of characters encoded (default is to encode all + * non-graphic except space, tab, and newline). + */ +#define VIS_SP 0x04 /* also encode space */ +#define VIS_TAB 0x08 /* also encode tab */ +#define VIS_NL 0x10 /* also encode newline */ +#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) +#define VIS_SAFE 0x20 /* only encode "unsafe" characters */ +#define VIS_DQ 0x200 /* backslash-escape double quotes */ +#define VIS_ALL 0x400 /* encode all characters */ + +/* + * other + */ +#define VIS_NOSLASH 0x40 /* inhibit printing '\' */ +#define VIS_GLOB 0x100 /* encode glob(3) magics and '#' */ + +/* + * unvis return codes + */ +#define UNVIS_VALID 1 /* character valid */ +#define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */ +#define UNVIS_NOCHAR 3 /* valid sequence, no character produced */ +#define UNVIS_SYNBAD -1 /* unrecognized escape sequence */ +#define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */ + +/* + * unvis flags + */ +#define UNVIS_END 1 /* no more characters */ + +__BEGIN_DECLS +char *vis(char *, int, int, int); +int strvis(char *, const char *, int); +int stravis(char **, const char *, int); +int strnvis(char *, const char *, size_t, int) + __attribute__ ((__bounded__(__string__,1,3))); +int strvisx(char *, const char *, size_t, int) + __attribute__ ((__bounded__(__string__,1,3))); +int strunvis(char *, const char *); +int unvis(char *, char, int *, int); +ssize_t strnunvis(char *, const char *, size_t) + __attribute__ ((__bounded__(__string__,1,3))); + +__END_DECLS + +#endif /* !_VIS_H_ */ diff --git a/lib/libopenbsd/Makefile b/lib/libopenbsd/Makefile @@ -1,85 +1,31 @@ -LIB=openbsd -CPPFLAGS += -I. -Ihidden -include openbsd.h -I.. -include config.h +.TOPDIR?=../.. +.CURDIR?=. -GEN_SRCS = hash/md5hl.c hash/rmd160hl.c hash/sha1hl.c hash/sha224hl.c \ - hash/sha256hl.c hash/sha384hl.c hash/sha512hl.c +LIB=openbsd -CLEANFILES+=$(GEN_SRCS) +# CPPFLAGS += -include include/compat.h -include config.h -SRCS = closefrom.c \ +SRCS+= closefrom.c \ heapsort.c merge.c pledge-noop.c progname.c \ qsort.c radixsort.c random.c readpassphrase.c \ setproctitle.c strtoimax.c strtonum.c strtoumax.c \ - vis.c unvis.c pwcache.c pw_dup.c getbsize.c \ - strmode.c crypt/arc4random.c crypt/arc4random_uniform.c \ - crypt/chacha.c crypt/blowfish.c hash/md5.c hash/rmd160.c hash/sha1.c \ - hash/sha2.c $(GEN_SRCS) - -SRCS += gen/errc.c gen/fts.c gen/warnc.c gen/verrc.c gen/vwarnc.c gen/setmode.c\ - gen/basename.c gen/dirname.c -SRCS += stdio/fgetwln.c -SRCS += net/base64.c - -SRCS += regex/regcomp.c regex/regerror.c regex/regexec.c regex/regfree.c - -include config.mk - -ifneq ($(HAVE_EXPLICIT_BZERO),1) - SRCS += string/explicit_bzero.c -endif + pwcache.c pw_dup.c getbsize.c \ + strmode.c -ifneq ($(HAVE_REALLOCARRAY),1) - SRCS += stdlib/reallocarray.c -endif -ifneq ($(HAVE_STRLCPY),1) - SRCS += string/strlcpy.c -endif +LIBCSRCDIR=${.CURDIR} +include ${LIBCSRCDIR}/Makefile.inc -ifneq ($(HAVE_STRLCAT),1) - SRCS += string/strlcat.c -endif +include ${.TOPDIR}/config.mk ifneq ($(HAVE_ISSETUGID),1) - SRCS += issetugid.c -endif - -ifneq ($(HAVE_FGETLN),1) - SRCS += stdio/fgetln.c +SRCS+= issetugid.c endif ifneq ($(HAVE_GETENTROPY),1) - SRCS += getentropy_linux.c +# XXX: fix fstatfs +SRCS+= getentropy_linux.c getentropy_linux.o : CPPFLAGS += -D_GNU_SOURCE endif -ifneq ($(HAVE_GETDTABLECOUNT),1) - SRCS += sys/getdtablecount.c -endif - -ifneq ($(HAVE_GETFSSTAT),1) - SRCS += sys/getfsstat.c - SRCS += sys/statfs.c -endif - -ifneq ($(HAVE_GETMNTINFO),1) - SRCS += gen/getmntinfo.c -endif - -$(GEN_SRCS) : % : hash/helper.c - -hash/sha1hl.c: - sed -e 's/hashinc/sha1.h/g;s/HASH/SHA1/g' hash/helper.c > $@ - -hash/sha%hl.c: - sed -e 's/hashinc/sha2.h/g;s/HASH_CTX/SHA2_CTX/g' \ - -e 's/HASH/SHA$*/g' hash/helper.c > $@ - -hash/md5hl.c: - sed -e "s/hashinc/md5.h/g;s/HASH/MD5/g" hash/helper.c > $@ - -hash/rmd160hl.c: - sed -e "s/hashinc/rmd160.h/g;s/HASH/RMD160/g" hash/helper.c > $@ - - -include bsd.lib.mk +include ${.TOPDIR}/mk/bsd.lib.mk diff --git a/lib/libopenbsd/chacha.h b/lib/libopenbsd/chacha.h @@ -1,48 +0,0 @@ -/* $OpenBSD: chacha.h,v 1.6 2014/07/25 14:04:51 jsing Exp $ */ -/* - * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef HEADER_CHACHA_H -#define HEADER_CHACHA_H - -#include <stddef.h> -#include <stdint.h> - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - unsigned int input[16]; - unsigned char ks[64]; - unsigned char unused; -} ChaCha_ctx; - -void ChaCha_set_key(ChaCha_ctx *ctx, const unsigned char *key, - unsigned int keybits); -void ChaCha_set_iv(ChaCha_ctx *ctx, const unsigned char *iv, - const unsigned char *counter); -void ChaCha(ChaCha_ctx *ctx, unsigned char *out, const unsigned char *in, - size_t len); - -void CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len, - const unsigned char key[32], const unsigned char iv[8], uint64_t counter); - -#ifdef __cplusplus -} -#endif - -#endif /* HEADER_CHACHA_H */ diff --git a/lib/libopenbsd/config.mk.in b/lib/libopenbsd/config.mk.in @@ -1,4 +0,0 @@ -HAVE_STRLCAT = @HAVE_STRLCAT@ -HAVE_STRLCPY = @HAVE_STRLCPY@ -HAVE_GETENTROPY = @HAVE_GETENTROPY@ -include ../config.mk diff --git a/lib/libopenbsd/crypt/chacha-merged.c b/lib/libopenbsd/crypt/chacha-merged.c @@ -1,270 +0,0 @@ -/* $OpenBSD: chacha-merged.c,v 1.6 2014/06/24 18:12:09 jsing Exp $ */ -/* -chacha-merged.c version 20080118 -D. J. Bernstein -Public domain. -*/ - -#include <sys/types.h> - -#include <stdint.h> - -#define CHACHA_MINKEYLEN 16 -#define CHACHA_NONCELEN 8 -#define CHACHA_CTRLEN 8 -#define CHACHA_STATELEN (CHACHA_NONCELEN+CHACHA_CTRLEN) -#define CHACHA_BLOCKLEN 64 - -struct chacha_ctx { - u_int input[16]; - uint8_t ks[CHACHA_BLOCKLEN]; - uint8_t unused; -}; - -static inline void chacha_keysetup(struct chacha_ctx *x, const u_char *k, - u_int kbits) - __attribute__((__bounded__(__minbytes__, 2, CHACHA_MINKEYLEN))); -static inline void chacha_ivsetup(struct chacha_ctx *x, const u_char *iv, - const u_char *ctr) - __attribute__((__bounded__(__minbytes__, 2, CHACHA_NONCELEN))) - __attribute__((__bounded__(__minbytes__, 3, CHACHA_CTRLEN))); -static inline void chacha_encrypt_bytes(struct chacha_ctx *x, const u_char *m, - u_char *c, u_int bytes) - __attribute__((__bounded__(__buffer__, 2, 4))) - __attribute__((__bounded__(__buffer__, 3, 4))); - -typedef unsigned char u8; -typedef unsigned int u32; - -typedef struct chacha_ctx chacha_ctx; - -#define U8C(v) (v##U) -#define U32C(v) (v##U) - -#define U8V(v) ((u8)(v) & U8C(0xFF)) -#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF)) - -#define ROTL32(v, n) \ - (U32V((v) << (n)) | ((v) >> (32 - (n)))) - -#define U8TO32_LITTLE(p) \ - (((u32)((p)[0])) | \ - ((u32)((p)[1]) << 8) | \ - ((u32)((p)[2]) << 16) | \ - ((u32)((p)[3]) << 24)) - -#define U32TO8_LITTLE(p, v) \ - do { \ - (p)[0] = U8V((v)); \ - (p)[1] = U8V((v) >> 8); \ - (p)[2] = U8V((v) >> 16); \ - (p)[3] = U8V((v) >> 24); \ - } while (0) - -#define ROTATE(v,c) (ROTL32(v,c)) -#define XOR(v,w) ((v) ^ (w)) -#define PLUS(v,w) (U32V((v) + (w))) -#define PLUSONE(v) (PLUS((v),1)) - -#define QUARTERROUND(a,b,c,d) \ - a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \ - c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \ - a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \ - c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); - -static const char sigma[16] = "expand 32-byte k"; -static const char tau[16] = "expand 16-byte k"; - -static inline void -chacha_keysetup(chacha_ctx *x, const u8 *k, u32 kbits) -{ - const char *constants; - - x->input[4] = U8TO32_LITTLE(k + 0); - x->input[5] = U8TO32_LITTLE(k + 4); - x->input[6] = U8TO32_LITTLE(k + 8); - x->input[7] = U8TO32_LITTLE(k + 12); - if (kbits == 256) { /* recommended */ - k += 16; - constants = sigma; - } else { /* kbits == 128 */ - constants = tau; - } - x->input[8] = U8TO32_LITTLE(k + 0); - x->input[9] = U8TO32_LITTLE(k + 4); - x->input[10] = U8TO32_LITTLE(k + 8); - x->input[11] = U8TO32_LITTLE(k + 12); - x->input[0] = U8TO32_LITTLE(constants + 0); - x->input[1] = U8TO32_LITTLE(constants + 4); - x->input[2] = U8TO32_LITTLE(constants + 8); - x->input[3] = U8TO32_LITTLE(constants + 12); -} - -static inline void -chacha_ivsetup(chacha_ctx *x, const u8 *iv, const u8 *counter) -{ - x->input[12] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 0); - x->input[13] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 4); - x->input[14] = U8TO32_LITTLE(iv + 0); - x->input[15] = U8TO32_LITTLE(iv + 4); -} - -static inline void -chacha_encrypt_bytes(chacha_ctx *x, const u8 *m, u8 *c, u32 bytes) -{ - u32 x0, x1, x2, x3, x4, x5, x6, x7; - u32 x8, x9, x10, x11, x12, x13, x14, x15; - u32 j0, j1, j2, j3, j4, j5, j6, j7; - u32 j8, j9, j10, j11, j12, j13, j14, j15; - u8 *ctarget = NULL; - u8 tmp[64]; - u_int i; - - if (!bytes) - return; - - j0 = x->input[0]; - j1 = x->input[1]; - j2 = x->input[2]; - j3 = x->input[3]; - j4 = x->input[4]; - j5 = x->input[5]; - j6 = x->input[6]; - j7 = x->input[7]; - j8 = x->input[8]; - j9 = x->input[9]; - j10 = x->input[10]; - j11 = x->input[11]; - j12 = x->input[12]; - j13 = x->input[13]; - j14 = x->input[14]; - j15 = x->input[15]; - - for (;;) { - if (bytes < 64) { - for (i = 0; i < bytes; ++i) - tmp[i] = m[i]; - m = tmp; - ctarget = c; - c = tmp; - } - x0 = j0; - x1 = j1; - x2 = j2; - x3 = j3; - x4 = j4; - x5 = j5; - x6 = j6; - x7 = j7; - x8 = j8; - x9 = j9; - x10 = j10; - x11 = j11; - x12 = j12; - x13 = j13; - x14 = j14; - x15 = j15; - for (i = 20; i > 0; i -= 2) { - QUARTERROUND(x0, x4, x8, x12) - QUARTERROUND(x1, x5, x9, x13) - QUARTERROUND(x2, x6, x10, x14) - QUARTERROUND(x3, x7, x11, x15) - QUARTERROUND(x0, x5, x10, x15) - QUARTERROUND(x1, x6, x11, x12) - QUARTERROUND(x2, x7, x8, x13) - QUARTERROUND(x3, x4, x9, x14) - } - x0 = PLUS(x0, j0); - x1 = PLUS(x1, j1); - x2 = PLUS(x2, j2); - x3 = PLUS(x3, j3); - x4 = PLUS(x4, j4); - x5 = PLUS(x5, j5); - x6 = PLUS(x6, j6); - x7 = PLUS(x7, j7); - x8 = PLUS(x8, j8); - x9 = PLUS(x9, j9); - x10 = PLUS(x10, j10); - x11 = PLUS(x11, j11); - x12 = PLUS(x12, j12); - x13 = PLUS(x13, j13); - x14 = PLUS(x14, j14); - x15 = PLUS(x15, j15); - - if (bytes < 64) { - U32TO8_LITTLE(x->ks + 0, x0); - U32TO8_LITTLE(x->ks + 4, x1); - U32TO8_LITTLE(x->ks + 8, x2); - U32TO8_LITTLE(x->ks + 12, x3); - U32TO8_LITTLE(x->ks + 16, x4); - U32TO8_LITTLE(x->ks + 20, x5); - U32TO8_LITTLE(x->ks + 24, x6); - U32TO8_LITTLE(x->ks + 28, x7); - U32TO8_LITTLE(x->ks + 32, x8); - U32TO8_LITTLE(x->ks + 36, x9); - U32TO8_LITTLE(x->ks + 40, x10); - U32TO8_LITTLE(x->ks + 44, x11); - U32TO8_LITTLE(x->ks + 48, x12); - U32TO8_LITTLE(x->ks + 52, x13); - U32TO8_LITTLE(x->ks + 56, x14); - U32TO8_LITTLE(x->ks + 60, x15); - } - - x0 = XOR(x0, U8TO32_LITTLE(m + 0)); - x1 = XOR(x1, U8TO32_LITTLE(m + 4)); - x2 = XOR(x2, U8TO32_LITTLE(m + 8)); - x3 = XOR(x3, U8TO32_LITTLE(m + 12)); - x4 = XOR(x4, U8TO32_LITTLE(m + 16)); - x5 = XOR(x5, U8TO32_LITTLE(m + 20)); - x6 = XOR(x6, U8TO32_LITTLE(m + 24)); - x7 = XOR(x7, U8TO32_LITTLE(m + 28)); - x8 = XOR(x8, U8TO32_LITTLE(m + 32)); - x9 = XOR(x9, U8TO32_LITTLE(m + 36)); - x10 = XOR(x10, U8TO32_LITTLE(m + 40)); - x11 = XOR(x11, U8TO32_LITTLE(m + 44)); - x12 = XOR(x12, U8TO32_LITTLE(m + 48)); - x13 = XOR(x13, U8TO32_LITTLE(m + 52)); - x14 = XOR(x14, U8TO32_LITTLE(m + 56)); - x15 = XOR(x15, U8TO32_LITTLE(m + 60)); - - j12 = PLUSONE(j12); - if (!j12) { - j13 = PLUSONE(j13); - /* - * Stopping at 2^70 bytes per nonce is the user's - * responsibility. - */ - } - - U32TO8_LITTLE(c + 0, x0); - U32TO8_LITTLE(c + 4, x1); - U32TO8_LITTLE(c + 8, x2); - U32TO8_LITTLE(c + 12, x3); - U32TO8_LITTLE(c + 16, x4); - U32TO8_LITTLE(c + 20, x5); - U32TO8_LITTLE(c + 24, x6); - U32TO8_LITTLE(c + 28, x7); - U32TO8_LITTLE(c + 32, x8); - U32TO8_LITTLE(c + 36, x9); - U32TO8_LITTLE(c + 40, x10); - U32TO8_LITTLE(c + 44, x11); - U32TO8_LITTLE(c + 48, x12); - U32TO8_LITTLE(c + 52, x13); - U32TO8_LITTLE(c + 56, x14); - U32TO8_LITTLE(c + 60, x15); - - if (bytes <= 64) { - if (bytes < 64) { - for (i = 0; i < bytes; ++i) - ctarget[i] = c[i]; - } - x->input[12] = j12; - x->input[13] = j13; - x->unused = 64 - bytes; - return; - } - bytes -= 64; - c += 64; - m += 64; - } -} diff --git a/lib/libopenbsd/crypt/chacha.c b/lib/libopenbsd/crypt/chacha.c @@ -1,77 +0,0 @@ -/* $OpenBSD: chacha.c,v 1.6 2014/07/08 14:30:23 bcook Exp $ */ -/* - * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <stdint.h> - -#include <chacha.h> - -#include "chacha-merged.c" - -void -ChaCha_set_key(ChaCha_ctx *ctx, const unsigned char *key, uint32_t keybits) -{ - chacha_keysetup((chacha_ctx *)ctx, key, keybits); - ctx->unused = 0; -} - -void -ChaCha_set_iv(ChaCha_ctx *ctx, const unsigned char *iv, - const unsigned char *counter) -{ - chacha_ivsetup((chacha_ctx *)ctx, iv, counter); - ctx->unused = 0; -} - -void -ChaCha(ChaCha_ctx *ctx, unsigned char *out, const unsigned char *in, size_t len) -{ - unsigned char *k; - int i, l; - - /* Consume remaining keystream, if any exists. */ - if (ctx->unused > 0) { - k = ctx->ks + 64 - ctx->unused; - l = (len > ctx->unused) ? ctx->unused : len; - for (i = 0; i < l; i++) - *(out++) = *(in++) ^ *(k++); - ctx->unused -= l; - len -= l; - } - - chacha_encrypt_bytes((chacha_ctx *)ctx, in, out, (uint32_t)len); -} - -void -CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len, - const unsigned char key[32], const unsigned char iv[8], uint64_t counter) -{ - struct chacha_ctx ctx; - - /* - * chacha_ivsetup expects the counter to be in u8. Rather than - * converting size_t to u8 and then back again, pass a counter of - * NULL and manually assign it afterwards. - */ - chacha_keysetup(&ctx, key, 256); - chacha_ivsetup(&ctx, iv, NULL); - if (counter != 0) { - ctx.input[12] = (uint32_t)counter; - ctx.input[13] = (uint32_t)(counter >> 32); - } - - chacha_encrypt_bytes(&ctx, in, out, (uint32_t)len); -} diff --git a/lib/libopenbsd/crypt/chacha_private.h b/lib/libopenbsd/crypt/chacha_private.h @@ -4,7 +4,7 @@ D. J. Bernstein Public domain. */ -/* $OpenBSD$ */ +/* $OpenBSD: chacha_private.h,v 1.2 2013/10/04 07:02:27 djm Exp $ */ typedef unsigned char u8; typedef unsigned int u32; diff --git a/lib/libopenbsd/gen/unvis.3 b/lib/libopenbsd/gen/unvis.3 @@ -0,0 +1,195 @@ +.\" $OpenBSD: unvis.3,v 1.18 2014/11/09 04:34:20 guenther Exp $ +.\" +.\" 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. +.\" 3. 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. +.\" +.Dd $Mdocdate: November 9 2014 $ +.Dt UNVIS 3 +.Os +.Sh NAME +.Nm unvis , +.Nm strunvis , +.Nm strnunvis +.Nd decode a visual representation of characters +.Sh SYNOPSIS +.In vis.h +.Ft int +.Fn unvis "char *cp" "char c" "int *astate" "int flag" +.Ft int +.Fn strunvis "char *dst" "const char *src" +.Ft ssize_t +.Fn strnunvis "char *dst" "const char *src" "size_t size" +.Sh DESCRIPTION +The +.Fn unvis , +.Fn strunvis +and +.Fn strnunvis +functions are used to decode a visual representation of characters, +as produced by the +.Xr vis 3 +function, back into the original form. +.Fn 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 . +.Pp +.Fn strunvis +decodes the characters pointed to by +.Fa src +into the buffer pointed to by +.Fa dst . +.Pp +.Fn strnunvis +decodes the characters pointed to by +.Fa src +into the buffer pointed to by +.Fa dst , +writing a maximum of +.Fa size +bytes. +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). +.Fn strunvis +terminates the destination string with a trailing NUL byte; +.Fn strnunvis +does so if +.Fa size +is larger than 0. +.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. +.Sh RETURN VALUES +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 +.Fa cp . +.It Dv UNVIS_VALIDPUSH +A valid character has been recognized and is available at the location +pointed to by +.Fa 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 +.Fn strunvis +function returns the number of bytes written (not counting +the trailing NUL byte) or \-1 if an error occurred. +.Pp +The +.Fn strnunvis +function returns the number of bytes (not counting the trailing NUL byte) +that would be needed to fully convert the input string, or \-1 if an +error occurred. +.Sh EXAMPLES +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 unvis 1 , +.Xr vis 1 , +.Xr vis 3 +.Sh HISTORY +The +.Fn unvis +function first appeared in +.Bx 4.4 . diff --git a/lib/libopenbsd/gen/unvis.c b/lib/libopenbsd/gen/unvis.c @@ -0,0 +1,286 @@ +/* $OpenBSD: unvis.c,v 1.17 2015/09/13 11:32:51 guenther Exp $ */ +/*- + * 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. + * 3. 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. + */ + +#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 isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') + +/* + * unvis - decode characters previously encoded by vis + */ +int +unvis(char *cp, char 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) { + + case S_GROUND: + *cp = 0; + if (c == '\\') { + *astate = S_START; + return (0); + } + *cp = c; + return (UNVIS_VALID); + + case S_START: + switch(c) { + case '-': + *cp = 0; + *astate = S_GROUND; + return (0); + case '\\': + 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 = (char) 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); + + default: + /* + * decoder in unknown state - (probably uninitialized) + */ + *astate = S_GROUND; + return (UNVIS_SYNBAD); + } +} +DEF_WEAK(unvis); + +/* + * 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: + *dst = '\0'; + return (-1); + } + } + if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) + dst++; + *dst = '\0'; + return (dst - start); +} + +ssize_t +strnunvis(char *dst, const char *src, size_t sz) +{ + char c, p; + char *start = dst, *end = dst + sz - 1; + int state = 0; + + if (sz > 0) + *end = '\0'; + while ((c = *src++)) { + again: + switch (unvis(&p, c, &state, 0)) { + case UNVIS_VALID: + if (dst < end) + *dst = p; + dst++; + break; + case UNVIS_VALIDPUSH: + if (dst < end) + *dst = p; + dst++; + goto again; + case 0: + case UNVIS_NOCHAR: + break; + default: + if (dst <= end) + *dst = '\0'; + return (-1); + } + } + if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) { + if (dst < end) + *dst = p; + dst++; + } + if (dst <= end) + *dst = '\0'; + return (dst - start); +} + diff --git a/lib/libopenbsd/gen/vis.3 b/lib/libopenbsd/gen/vis.3 @@ -0,0 +1,395 @@ +.\" $OpenBSD: vis.3,v 1.35 2015/07/20 01:52:28 millert Exp $ +.\" +.\" 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. +.\" 3. 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. +.\" +.Dd $Mdocdate: July 20 2015 $ +.Dt VIS 3 +.Os +.Sh NAME +.Nm vis , +.Nm strvis , +.Nm strnvis , +.Nm strvisx , +.Nm stravis +.Nd visually encode characters +.Sh SYNOPSIS +.In stdlib.h +.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 strnvis "char *dst" "const char *src" "size_t dstsize" "int flag" +.Ft int +.Fn strvisx "char *dst" "const char *src" "size_t srclen" "int flag" +.Ft int +.Fn stravis "char **outp" "const char *src" "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. +.Fa dst +will be NUL-terminated and must be at least 5 bytes long +(maximum encoding requires 4 bytes plus the NUL). +The additional character, +.Fa nextc , +is only used when selecting the +.Dv VIS_CSTYLE +encoding format (explained below). +.Pp +The +.Fn strvis , +.Fn strnvis +and +.Fn strvisx +functions copy into +.Fa dst +a visual representation of +the string +.Fa src . +.Pp +The +.Fn strvis +function encodes characters from +.Fa src +up to the first NUL, into a buffer +.Fa dst +(which must be at least 4 * strlen(src) + 1 long). +.Pp +The +.Fn strnvis +function encodes characters from +.Fa src +up to the first NUL or the end of the buffer +.Fa dst , +as indicated by +.Fa dstsize . +.Pp +The +.Fn strvisx +function encodes exactly +.Fa srclen +characters from +.Fa src +into a buffer +.Fa dst +(which must be at least 4 * srclen + 1 long). +This +is useful for encoding a block of data that may contain NULs. +.Pp +The +.Fn stravis +function writes a visual representation of the string +.Fa src +into a newly allocated string +.Fa outp ; +it does not attempt to +.Xr realloc 3 +.Fa outp . +.Fa outp +should be passed to +.Xr free 3 +to release the allocated storage when it is no longer needed. +.Fn stravis +checks for integer overflow when allocating memory. +.Pp +All forms NUL-terminate +.Fa dst , +except for +.Fn strnvis +when +.Fa dstsize +is zero, in which case +.Fa dst +is not touched. +.Pp +The +.Fa flag +parameter is used for altering the default range of +characters considered for encoding and for altering the visual +representation. +.Ss Encodings +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_ALL +Encode all characters, whether visible or not. +.It Dv VIS_DQ +Also encode double quote characters +.Pf ( Ql \&" ) . +.It Dv VIS_GLOB +Also encode magic characters recognized by +.Xr glob 3 +.Pf ( Ql * , +.Ql \&? , +.Ql \&[ ) +and +.Ql # . +.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 | VIS_TAB | VIS_NL . +.It Dv VIS_SAFE +Only encode +.Dq unsafe +characters. +These are 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 three forms of encoding. +All forms use the backslash +.Ql \e +character to introduce a special +sequence; two backslashes are used to represent a real backslash. +These are the visual formats: +.Bl -tag -width VIS_CSTYLE +.It (default) +Use an +.Ql M +to represent meta characters (characters with the 8th +bit set), and use a caret +.Ql ^ +to represent control characters (see +.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. +.It Dv \e-C +Represents character +.Ql C . +Only used with +.Dv VIS_ALL . +.El +.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: +.Bd -unfilled -offset indent +.Li \ea Tn - BEL No (007) +.Li \eb Tn - BS No (010) +.Li \ef Tn - NP No (014) +.Li \en Tn - NL No (012) +.Li \er Tn - CR No (015) +.Li \es Tn - SP No (040) +.Li \et Tn - HT No (011) +.Li \ev Tn - VT No (013) +.Li \e0 Tn - NUL No (000) +.Ed +.Pp +When using this format, the +.Fa nextc +parameter is looked at to determine +if a 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_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 RETURN VALUES +.Fn vis +returns a pointer to the terminating NUL character of the string +.Fa dst . +.Pp +.Fn strvis +and +.Fn strvisx +return the number of characters in +.Fa dst +(not including the trailing NUL). +.Pp +.Fn strnvis +returns the length that +.Fa dst +would become if it were of unlimited size (similar to +.Xr snprintf 3 +or +.Xr strlcpy 3 ) . +This can be used to detect truncation, but it also means that +the return value of +.Fn strnvis +must not be used without checking it against +.Fa dstsize . +.Pp +Upon successful completion, +.Fn stravis +returns the number of characters in +.Pf * Fa outp +(not including the trailing NUL). +Otherwise, +.Fn stravis +returns -1 and sets +.Va errno +to +.Er ENOMEM . +.Sh EXAMPLES +.Fn strvis +has unusual storage requirements that can lead to stack or heap corruption +if the destination is not carefully constructed. +A common mistake is to use the same size for the source and destination +when the destination actually needs up to 4 * strlen(source) + 1 bytes. +.Pp +If the length of a string to be encoded is not known at compile time, use +.Fn stravis : +.Bd -literal -offset indent +char *src, *dst; + +\&... +if (stravis(&dst, src, VIS_OCTAL) == -1) + err(1, "stravis"); + +\&... +free(dst); +.Ed +.Pp +To encode a fixed size buffer, +.Fn strnvis +can be used with a fixed size target buffer: +.Bd -literal -offset indent +char src[MAXPATHLEN]; +char dst[4 * MAXPATHLEN + 1]; + +\&... +if (strnvis(dst, src, sizeof(dst), VIS_OCTAL) >= sizeof(dst)) + err(1, "strnvis"); +.Ed +.Sh SEE ALSO +.Xr unvis 1 , +.Xr vis 1 , +.Xr free 3 , +.Xr snprintf 3 , +.Xr strlcpy 3 , +.Xr unvis 3 +.Sh HISTORY +The +.Fn vis , +.Fn strvis +and +.Fn strvisx +functions first appeared in +.Bx 4.4 , +.Fn strnvis +in +.Ox 2.9 +and +.Fn stravis +in +.Ox 5.7 . +.Pp +The +.Dv VIS_ALL +flag first appeared in +.Ox 4.9 . diff --git a/lib/libopenbsd/gen/vis.c b/lib/libopenbsd/gen/vis.c @@ -0,0 +1,243 @@ +/* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */ +/*- + * 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. + * 3. 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. + */ + +#include <sys/types.h> +#include <errno.h> +#include <ctype.h> +#include <limits.h> +#include <string.h> +#include <stdlib.h> +#include <vis.h> + +#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') +#define isvisible(c,flag) \ + (((c) == '\\' || (flag & VIS_ALL) == 0) && \ + (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ + (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ + (flag & VIS_GLOB) == 0) && isgraph((u_char)(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' || \ + isgraph((u_char)(c)))))) + +/* + * vis - visually encode characters + */ +char * +vis(char *dst, int c, int flag, int nextc) +{ + if (isvisible(c, flag)) { + if ((c == '"' && (flag & VIS_DQ) != 0) || + (c == '\\' && (flag & VIS_NOSLASH) == 0)) + *dst++ = '\\'; + *dst++ = c; + *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) == ' ') || (flag & VIS_OCTAL) || + ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) { + *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((u_char)c)) { + *dst++ = '^'; + if (c == 0177) + *dst++ = '?'; + else + *dst++ = c + '@'; + } else { + *dst++ = '-'; + *dst++ = c; + } +done: + *dst = '\0'; + return (dst); +} +DEF_WEAK(vis); + +/* + * strvis, strnvis, 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 NULL, + * is returned. + * + * Strnvis will write no more than siz-1 bytes (and will NULL terminate). + * The number of bytes needed to fully encode the string is returned. + * + * Strvisx encodes exactly len bytes from src into dst. + * This is useful for encoding a block of data. + */ +int +strvis(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); +} +DEF_WEAK(strvis); + +int +strnvis(char *dst, const char *src, size_t siz, int flag) +{ + char *start, *end; + char tbuf[5]; + int c, i; + + i = 0; + for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { + if (isvisible(c, flag)) { + if ((c == '"' && (flag & VIS_DQ) != 0) || + (c == '\\' && (flag & VIS_NOSLASH) == 0)) { + /* need space for the extra '\\' */ + if (dst + 1 >= end) { + i = 2; + break; + } + *dst++ = '\\'; + } + i = 1; + *dst++ = c; + src++; + } else { + i = vis(tbuf, c, flag, *++src) - tbuf; + if (dst + i <= end) { + memcpy(dst, tbuf, i); + dst += i; + } else { + src--; + break; + } + } + } + if (siz > 0) + *dst = '\0'; + if (dst + i > end) { + /* adjust return value for truncation */ + while ((c = *src)) + dst += vis(tbuf, c, flag, *++src) - tbuf; + } + return (dst - start); +} + +int +stravis(char **outp, const char *src, int flag) +{ + char *buf; + int len, serrno; + + buf = reallocarray(NULL, 4, strlen(src) + 1); + if (buf == NULL) + return -1; + len = strvis(buf, src, flag); + serrno = errno; + *outp = realloc(buf, len + 1); + if (*outp == NULL) { + *outp = buf; + errno = serrno; + } + return (len); +} + +int +strvisx(char *dst, const char *src, size_t len, int flag) +{ + char 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); +} diff --git a/lib/libopenbsd/getfsstat b/lib/libopenbsd/getfsstat Binary files differ. diff --git a/lib/libopenbsd/getfsstat.2 b/lib/libopenbsd/getfsstat.2 @@ -1,149 +0,0 @@ -.\" $OpenBSD: getfsstat.2,v 1.20 2014/12/16 00:06:49 schwarze Exp $ -.\" $NetBSD: getfsstat.2,v 1.6 1995/06/29 11:40:44 cgd Exp $ -.\" -.\" 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. -.\" 3. 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. -.\" -.\" @(#)getfsstat.2 8.1 (Berkeley) 6/9/93 -.\" -.Dd $Mdocdate: December 16 2014 $ -.Dt GETFSSTAT 2 -.Os -.Sh NAME -.Nm getfsstat -.Nd get list of all mounted file systems -.Sh SYNOPSIS -.In sys/param.h -.In sys/mount.h -.Ft int -.Fn getfsstat "struct statfs *buf" "size_t bufsize" "int flags" -.Sh DESCRIPTION -.Fn getfsstat -returns information about all mounted file systems. -.Fa buf -is a pointer to an array of -.Xr statfs 2 -structures defined as follows: -.Bd -literal -typedef struct { int32_t val[2]; } fsid_t; - -#define MFSNAMELEN 16 /* length of fs type name, including nul */ -#define MNAMELEN 90 /* length of buffer for returned name */ - -struct statfs { - u_int32_t f_flags; /* copy of mount flags */ - u_int32_t f_bsize; /* file system block size */ - u_int32_t f_iosize; /* optimal transfer block size */ - - /* unit is f_bsize */ - u_int64_t f_blocks; /* total data blocks in file system */ - u_int64_t f_bfree; /* free blocks in fs */ - int64_t f_bavail; /* free blocks avail to non-superuser */ - - u_int64_t f_files; /* total file nodes in file system */ - u_int64_t f_ffree; /* free file nodes in fs */ - int64_t f_favail; /* free file nodes avail to non-root */ - - u_int64_t f_syncwrites; /* count of sync writes since mount */ - u_int64_t f_syncreads; /* count of sync reads since mount */ - u_int64_t f_asyncwrites; /* count of async writes since mount */ - u_int64_t f_asyncreads; /* count of async reads since mount */ - - fsid_t f_fsid; /* file system id */ - u_int32_t f_namemax; /* maximum filename length */ - uid_t f_owner; /* user that mounted the file system */ - u_int64_t f_ctime; /* last mount [-u] time */ - - char f_fstypename[MFSNAMELEN]; /* fs type name */ - char f_mntonname[MNAMELEN]; /* directory on which mounted */ - char f_mntfromname[MNAMELEN]; /* mounted file system */ - char f_mntfromspec[MNAMELEN]; /* special for mount request */ - union mount_info mount_info; /* per-filesystem mount options */ -}; -.Ed -.Pp -The buffer is filled with an array of -.Fa statfs -structures, one for each mounted file system -up to the size specified by -.Fa bufsize . -.Pp -If -.Fa buf -is -.Dv NULL , -.Fn getfsstat -returns just the number of mounted file systems. -.Pp -Normally -.Fa flags -should be specified as -.Dv MNT_WAIT . -If -.Fa flags -is set to -.Dv MNT_NOWAIT , -.Fn getfsstat -will return the information it has available without requesting -an update from each file system. -Thus, some of the information will be out of date, but -.Fn getfsstat -will not block waiting for information from a file system that is -unable to respond. -If no flags are provided, -.Dv MNT_WAIT -is assumed. -.Pp -Note that -.Fa f_fsid -will be empty unless the user is the superuser. -.Sh RETURN VALUES -Upon successful completion, the number of -.Fa statfs -structures is returned. -Otherwise, \-1 is returned and the global variable -.Va errno -is set to indicate the error. -.Sh ERRORS -.Fn getfsstat -fails if one or more of the following are true: -.Bl -tag -width Er -.It Bq Er EFAULT -.Fa buf -points to an invalid address. -.It Bq Er EIO -An I/O error occurred while reading from or writing to the file system. -.El -.Sh SEE ALSO -.Xr statfs 2 , -.Xr fstab 5 , -.Xr mount 8 -.Sh HISTORY -The -.Fn getfsstat -function first appeared in -.Bx 4.4 . diff --git a/lib/libopenbsd/getfsstat.c b/lib/libopenbsd/getfsstat.c @@ -1,120 +0,0 @@ -#include <stdio.h> -#include <stdint.h> -#include <errno.h> -#include <string.h> -#include <sys/statfs.h> -#include <sys/types.h> -#include <mntent.h> - -typedef struct { int32_t val[2]; } obsd_fsid_t; - -#define MFSNAMELEN 16 /* length of fs type name, including nul */ -#define MNAMELEN 90 /* length of buffer for returned name */ - -/* per-filesystem mount options */ -union mount_info { - struct ufs_args ufs_args; - struct mfs_args mfs_args; - struct nfs_args nfs_args; - struct iso_args iso_args; - struct msdosfs_args msdosfs_args; - struct ntfs_args ntfs_args; - struct tmpfs_args tmpfs_args; - char __align[160]; /* 64-bit alignment and room to grow */ -}; - - -struct obsd_statfs { - u_int32_t f_flags; /* copy of mount flags */ - u_int32_t f_bsize; /* file system block size */ - u_int32_t f_iosize; /* optimal transfer block size */ - - /* unit is f_bsize */ - u_int64_t f_blocks; /* total data blocks in file system */ - u_int64_t f_bfree; /* free blocks in fs */ - int64_t f_bavail; /* free blocks avail to non-superuser */ - - u_int64_t f_files; /* total file nodes in file system */ - u_int64_t f_ffree; /* free file nodes in fs */ - int64_t f_favail; /* free file nodes avail to non-root */ - - u_int64_t f_syncwrites; /* count of sync writes since mount */ - u_int64_t f_syncreads; /* count of sync reads since mount */ - u_int64_t f_asyncwrites; /* count of async writes since mount */ - u_int64_t f_asyncreads; /* count of async reads since mount */ - - obsd_fsid_t f_fsid; /* file system id */ - u_int32_t f_namemax; /* maximum filename length */ - uid_t f_owner; /* user that mounted the file system */ - u_int64_t f_ctime; /* last mount [-u] time */ - - char f_fstypename[MFSNAMELEN]; /* fs type name */ - char f_mntonname[MNAMELEN]; /* directory on which mounted */ - char f_mntfromname[MNAMELEN]; /* mounted file system */ - char f_mntfromspec[MNAMELEN]; /* special for mount request */ - union mount_info mount_info; /* per-filesystem mount options */ -}; - -static char linebuf[256]; - -static int -nummounts(FILE *f) -{ - int rv; - - rv = 0; - - for (;;) { - fgets(linebuf, sizeof linebuf, f); - if (ferror(f)) { - rv = -1; - break; - } - if (!strchr(linebuf, '\n')) { - errno = ERANGE; - rv = -1; - break; - } - if (feof(f)) - break; - rv++; - } - - return rv; -} - -int -statfs(const char *path, struct statfs *buf) -{ - static struct ln_statfs fs; -} - -int -getfsstat(struct statfs *buf, size_t bufsize, int flags) -{ - static struct mntent mnt; - FILE *f; - int i; - - if (!(f = fopen("/etc/mtab", "r"))) - return -1; - - if(!buf) { - i = nummounts(f); - fclose(f); - return i; - } - - for (i = 0; i < bufsize; i++) { - getmntent_r(f, &mnt, linebuf, sizeof linebuf); - } - - fclose(f); - return i; -} - -int -main() -{ - printf("num %d error %s\n", getfsstat(0, 0, 0), strerror(errno)); -} diff --git a/lib/libopenbsd/getmntinfo.c b/lib/libopenbsd/getmntinfo.c @@ -30,7 +30,9 @@ #include <sys/types.h> #include <sys/mount.h> + #include <stdlib.h> +#include <mntent.h> /* * Return information about mounted filesystems. diff --git a/lib/libopenbsd/include/namespace.h b/lib/libopenbsd/include/namespace.h @@ -0,0 +1,164 @@ +/* $OpenBSD: namespace.h,v 1.10 2016/05/07 19:05:22 guenther Exp $ */ + +#ifndef _LIBC_NAMESPACE_H_ +#define _LIBC_NAMESPACE_H_ + +/* + * Copyright (c) 2015 Philip Guenther <guenther@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * The goal: calls from inside libc to other libc functions should be via + * identifiers that are of hidden visibility and--to avoid confusion--are + * in the reserved namespace. By doing this these calls are protected + * from overriding by applications and on many platforms can avoid creation + * or use of GOT or PLT entries. I've chosen a prefix of underbar-C-underbar + * ("_libc_") for this. These will not be declared directly; instead, the + * gcc "asm labels" extension will be used rename the function. + * + * For syscalls which are cancellation points, such as wait4(), there + * are identifiers that do not provide cancellation: + * _libc_wait4 hidden alias, for use internal to libc only + * _thread_sys_wait4 global name, for use outside libc only + * ...and identifiers that do provide cancellation: + * wait4 weak alias, for general use + * _libc_wait4_cancel hidden alias, for use internal to libc only + * Inside libc, the bare name ("wait4") binds to the version *without* + * cancellation; the few times where cancellation is desired it can be + * obtained by calling CANCEL(x) instead of just x. + * + * Some other calls need to be wrapped for reasons other than cancellation, + * such as to provide functionality beyond the underlying syscall (e.g., + * sigaction). For these, there are identifiers for the raw call, without + * the wrapping: + * _libc_sigaction hidden alias, for use internal to libc only + * _thread_sys_sigaction global name, for use outside libc only + * ...and identifiers that do provide the libc wrapping: + * sigaction weak alias, for general use + * _libc_sigaction_wrap hidden alias, for use internal to libc only + * Inside libc, the bare name ("sigaction") binds to the wrapper; when the + * raw version is necessary it can be obtained by calling HIDDEN(x) instead of + * just x. + * + * For syscalls which are not cancellation points, such as getpid(), + * the identifiers are just: + * _libc_getpid hidden alias, for use internal to libc only + * _thread_sys_getpid global name, for use outside libc only + * getpid weak alias, for use outside libc only + * + * By using gcc's "asm label" extension, we can usually avoid having + * to type those prefixes in the .h and .c files. However, for those + * cases where a non-default binding is necessary we can use these macros + * to get the desired identifier: + * + * CANCEL(x) + * This expands to the internal, hidden name of a cancellation + * wrapper: _libc_x_cancel. ex: CANCEL(fsync)(fd) + * + * WRAP(x) + * This expands to the internal, hidden name of a non-cancellation + * wrapper: _libc_x_wrap. ex: WRAP(sigprocmask)(set) + * + * + * In order to actually set up the desired asm labels, we use these in + * the internal .h files: + * PROTO_NORMAL(x) Symbols used both internally and externally + * This makes gcc convert use of x to use _libc_x instead + * ex: PROTO_NORMAL(getpid) + * + * PROTO_STD_DEPRECATED(x) Standard C symbols that we don't want to use + * This just marks the symbol as deprecated, with no renaming. + * ex: PROTO_STD_DEPRECATED(strcpy) + * + * PROTO_DEPRECATED(x) Symbols not in ISO C that we don't want to use + * This marks the symbol as both weak and deprecated, with no renaming + * ex: PROTO_DEPRECATED(creat) + * + * PROTO_CANCEL(x) Functions that have cancellation wrappers + * Like PROTO_NORMAL(x), but also declares _libc_x_cancel + * ex: PROTO_CANCEL(wait4) + * + * PROTO_WRAP(x) Functions that have wrappers for other reasons + * Like PROTO_NORMAL(x), but also declares _libc_x_wrap. Internal + * calls that want the wrapper's processing should invoke WRAP(x)(...) + * ex: PROTO_WRAP(sigaction) + * + * + * Finally, to create the expected aliases, we use these in the .c files + * where the definitions are: + * DEF_STRONG(x) Symbols reserved to or specified by ISO C + * This defines x as a strong alias for _libc_x; this must only + * be used for symbols that are reserved by the C standard + * (or reserved in the external identifier namespace). + * Matches with PROTO_NORMAL() + * ex: DEF_STRONG(fopen) + * + * DEF_WEAK(x) Symbols used internally and not in ISO C + * This defines x as a weak alias for _libc_x + * Matches with PROTO_NORMAL() + * ex: DEF_WEAK(lseek) + * + * DEF_CANCEL(x) Symbols that have a cancellation wrapper + * This defines x as a weak alias for _libc_x_cancel. + * Matches with PROTO_CANCEL() + * ex: DEF_CANCEL(read) + * + * DEF_WRAP(x) + * This defines x as a weak alias for _libc_x_wrap. + * Matches with PROTO_WRAP() + * ex: DEF_WRAP(sigaction) + * + * DEF_SYS(x) + * This defines _thread_sys_x as a strong alias for _libc_x. This should + * only be needed for syscalls that have C instead of asm stubs. + * Matches with PROTO_NORMAL(), PROTO_CANCEL(), or PROTO_WRAP() + * ex: DEF_SYS(pread) + * + * MAKE_CLONE(dst, src) Symbols that are exact clones of other symbols + * This declares _libc_dst as being the same type as dst, and makes + * _libc_dst a strong, hidden alias for _libc_src. You still need to + * DEF_STRONG(dst) or DEF_WEAK(dst) to alias dst itself + * ex: MAKE_CLONE(SHA224Pad, SHA256Pad) + */ + +/* #include <sys/cdefs.h> /1* for __dso_hidden and __{weak,strong}_alias *1/ */ +#define __dso_hidden __attribute__((__visibility__("hidden"))) + +#define HIDDEN(x) _libc_##x +/* #define CANCEL(x) _libc_##x##_cancel */ +/* #define WRAP(x) _libc_##x##_wrap */ +#define HIDDEN_STRING(x) "_libc_" __STRING(x) +/* #define CANCEL_STRING(x) "_libc_" __STRING(x) "_cancel" */ +/* #define WRAP_STRING(x) "_libc_" __STRING(x) "_wrap" */ + +/* #define PROTO_NORMAL(x) __dso_hidden typeof(x) x asm(HIDDEN_STRING(x)) */ +/* #define PROTO_STD_DEPRECATED(x) typeof(x) x __attribute__((deprecated)) */ +/* #define PROTO_DEPRECATED(x) typeof(x) x __attribute__((deprecated, weak)) */ +/* #define PROTO_CANCEL(x) __dso_hidden typeof(x) HIDDEN(x), \ */ +/* x asm(CANCEL_STRING(x)) */ +/* #define PROTO_WRAP(x) PROTO_NORMAL(x), WRAP(x) */ + +/* #define DEF_STRONG(x) __strong_alias(x, HIDDEN(x)) */ +/* #define DEF_WEAK(x) __weak_alias(x, HIDDEN(x)) */ +/* #define DEF_CANCEL(x) __weak_alias(x, CANCEL(x)) */ +/* #define DEF_WRAP(x) __weak_alias(x, WRAP(x)) */ +/* #define DEF_SYS(x) __strong_alias(_thread_sys_##x, HIDDEN(x)) */ + +#define MAKE_CLONE(dst, src) __dso_hidden typeof(dst) (dst) \ + __attribute__((alias (__STRING(src)))) + + +#endif /* _LIBC_NAMESPACE_H_ */ + diff --git a/lib/libopenbsd/openbsd.h b/lib/libopenbsd/openbsd.h @@ -1,162 +0,0 @@ -#ifndef _OPENBSD_H -#define _OPENBSD_H -/* #define _GNU_SOURCE */ -/* #include <stdarg.h> */ -/* #include <stdint.h> */ -/* #include <limits.h> */ -/* #include <sys/types.h> */ -/* #undef _GNU_SOURCE */ -/* #undef _FEATURES_H */ - -#define FAIL_INSTEAD_OF_TRYING_FALLBACK - -#ifndef ALIGN -/* XXX: x86_64 only, see sys/arch/$arch/include/_types.h */ -#define ALIGNBYTES (sizeof(long) - 1) -#define ALIGN(p) (((size_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) -#endif - -#define __UNUSED __attribute__ ((unused)) - -#define __dead __attribute__((__noreturn__)) - -#define __BEGIN_DECLS -#define __END_DECLS - -#ifndef __CONCAT -#define __CONCAT(x,y) x ## y -#endif -#ifndef __STRING -#define __STRING(x) #x -#endif - -#undef __weak_alias -#define __weak_alias(new, old) \ - extern __typeof(old) new __attribute__((weak, alias(#old))) - -#define MAKE_CLONE(new, old) \ - extern __typeof(old) new __attribute__((weak, alias(#old))) - -#ifndef SA_LEN -#define SA_LEN(X) \ - (((struct sockaddr*)(X))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : \ - ((struct sockaddr*)(X))->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr)) -#endif - -#define _PW_BUF_LEN 1024 -#define _GR_BUF_LEN 1024 - -#define NOFILE_MAX NOFILE - -#if !defined(HAVE_ATTRIBUTE__BOUNDED__) -#define __bounded__(x, y, z) -#endif - -#if !defined(DEF_WEAK) -#define DEF_WEAK(x) -#endif - -#if !defined(DEF_STRONG) -#define DEF_STRONG(x) -#endif - -#if !defined(PROTO_NORMAL) -#define PROTO_NORMAL(x) -#endif - -/* sys/sys/param.h */ -/* - * File system parameters and macros. - * - * The file system is made out of blocks of at most MAXBSIZE units, with - * smaller units (fragments) only in the last direct block. MAXBSIZE - * primarily determines the size of buffers in the buffer pool. It may be - * made larger without any effect on existing file systems; however making - * it smaller makes some file systems unmountable. - */ -#define MAXBSIZE (64 * 1024) - -#ifndef NL_TEXTMAX -#define NL_TEXTMAX 2048 -#endif - -#ifndef howmany -#define howmany(n,d) (((n)+((d)-1))/(d)) -#endif - -/* API definitions lifted from OpenBSD src/include */ - -/* pwd.h */ -#define _PW_NAME_LEN 63 - -/* stdlib.h */ - -/* unistd.h */ -#ifndef HAVE_EXECVPE -int execvpe(const char *, char *const *, char *const *); -#endif /* !HAVE_EXECVPE */ -#ifndef HAVE_SETRESUID -/* int setresuid(uid_t, uid_t, uid_t); */ -#endif /* !HAVE_SETRESUID */ - -#ifndef HAVE_PLEDGE -int pledge(const char *, const char *[]); -#endif /* !HAVE_PLEDGE */ - -#ifndef HAVE_SETPROGNAME -const char * getprogname(void); -void setprogname(const char *progname); -#endif /* !HAVE_SETPROGNAME */ - -/* pwcache.c */ -/* char *user_from_uid(uid_t, int); */ -/* char *group_from_gid(gid_t gid, int); */ - -/* getbsize.c */ -char *getbsize(int *, long *); - -#define strtoq strtoll - -/* inttypes.h */ -/* -intmax_t strtoimax(const char *, char **, int); -uintmax_t strtoumax(const char *, char **, int); -*/ - -/* #define d_namlen d_reclen */ -#if !defined(_DIRENT_HAVE_NAMLEN) -#define D_NAMLEN(x) \ - strnlen((x)->d_name, (x)->d_reclen) -#else -#define D_NAMLEN(x) \ - (x)->d_namlen -#endif - -/* XXX: read dynamically? */ -#define UID_MAX 60000 -#define GID_MAX 60000 - -/* sys/sys/stat.h */ -#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 00777 */ -#define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) /* 00666 */ -#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) - -#define S_ISTXT S_ISVTX - -/* lib/libc/include/thread_private.h */ -#define _MUTEX_LOCK(mutex) do {} while (0) -#define _MUTEX_UNLOCK(mutex) do {} while (0) - -#define SHA512_Update SHA512Update -#define SHA512_CTX SHA2_CTX -#define SHA512_Init SHA512Init -#define SHA512_Final SHA512Final - -/* setproctitle.c */ -void setproctitle(const char *, ...); - -/* pw_dup.c */ -struct passwd *pw_dup(const struct passwd *); - -int issetugid(void); -#endif diff --git a/lib/libopenbsd/pledge-noop.c b/lib/libopenbsd/pledge-noop.c @@ -1,5 +1,3 @@ -#include "openbsd.h" - int pledge(__UNUSED const char *promises, __UNUSED const char *paths[]) { diff --git a/lib/libopenbsd/readpassphrase.h b/lib/libopenbsd/readpassphrase.h @@ -1,37 +0,0 @@ -/* $OpenBSD: readpassphrase.h,v 1.5 2003/06/17 21:56:23 millert Exp $ */ - -/* - * Copyright (c) 2000, 2002 Todd C. Miller <Todd.Miller@courtesan.com> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Sponsored in part by the Defense Advanced Research Projects - * Agency (DARPA) and Air Force Research Laboratory, Air Force - * Materiel Command, USAF, under agreement number F39502-99-1-0512. - */ - -/* OPENBSD ORIGINAL: include/readpassphrase.h */ - -#ifndef HAVE_READPASSPHRASE - -#define RPP_ECHO_OFF 0x00 /* Turn off echo (default). */ -#define RPP_ECHO_ON 0x01 /* Leave echo on. */ -#define RPP_REQUIRE_TTY 0x02 /* Fail if there is no tty. */ -#define RPP_FORCELOWER 0x04 /* Force input to lower case. */ -#define RPP_FORCEUPPER 0x08 /* Force input to upper case. */ -#define RPP_SEVENBIT 0x10 /* Strip the high bit from input. */ -#define RPP_STDIN 0x20 /* Read from stdin, not /dev/tty */ - -char * readpassphrase(const char *, char *, size_t, int); - -#endif diff --git a/lib/libopenbsd/stdlib/malloc.3 b/lib/libopenbsd/stdlib/malloc.3 @@ -0,0 +1,526 @@ +.\" +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. +.\" 3. 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. +.\" +.\" $OpenBSD: malloc.3,v 1.101 2017/02/12 10:46:09 otto Exp $ +.\" +.Dd $Mdocdate: February 12 2017 $ +.Dt MALLOC 3 +.Os +.Sh NAME +.Nm malloc , +.Nm calloc , +.Nm reallocarray , +.Nm realloc , +.Nm free +.Nd memory allocation and deallocation +.Sh SYNOPSIS +.In stdlib.h +.Ft void * +.Fn malloc "size_t size" +.Ft void * +.Fn calloc "size_t nmemb" "size_t size" +.Ft void * +.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" +.Ft void * +.Fn realloc "void *ptr" "size_t size" +.Ft void +.Fn free "void *ptr" +.Vt char *malloc_options ; +.Sh DESCRIPTION +The +.Fn malloc +function allocates uninitialized space for an object of +the specified +.Fa size . +.Fn malloc +maintains multiple lists of free blocks according to size, allocating +space from the appropriate list. +The allocated space is suitably aligned (after possible pointer coercion) for +storage of any type of object. +.Pp +The +.Fn calloc +function allocates space for an array of +.Fa nmemb +objects, each of the specified +.Fa size . +The space is initialized to zero. +.Pp +The +.Fn realloc +function changes the size of the object pointed to by +.Fa ptr +to +.Fa size +bytes and returns a pointer to the (possibly moved) object. +The contents of the object are unchanged up to the lesser +of the new and old sizes. +If the new size is larger, the value of the newly allocated portion +of the object is indeterminate and uninitialized. +If the space cannot be allocated, the object +pointed to by +.Fa ptr +is unchanged. +If +.Fa ptr +is +.Dv NULL , +.Fn realloc +behaves like +.Fn malloc +and allocates a new object. +.Pp +The +.Fn reallocarray +function is similar to +.Fn realloc +except it operates on +.Fa nmemb +members of size +.Fa size +and checks for integer overflow in the calculation +.Fa nmemb +* +.Fa size . +.Pp +The +.Fn free +function causes the space pointed to by +.Fa ptr +to be either placed on a list of free pages to make it available for future +allocation or, if required, to be returned to the kernel using +.Xr munmap 2 . +If +.Fa ptr +is a +.Dv NULL +pointer, no action occurs. +If +.Fa ptr +was previously freed by +.Fn free , +.Fn realloc , +or +.Fn reallocarray , +the behavior is undefined and the double free is a security concern. +.Sh RETURN VALUES +Upon successful completion, the functions +.Fn malloc , +.Fn calloc , +.Fn realloc , +and +.Fn reallocarray +return a pointer to the allocated space; otherwise, a +.Dv NULL +pointer is returned and +.Va errno +is set to +.Er ENOMEM . +.Pp +If +.Fa size +or +.Fa nmemb +is equal to 0, a unique pointer to an access protected, +zero sized object is returned. +Access via this pointer will generate a +.Dv SIGSEGV +exception. +.Pp +If multiplying +.Fa nmemb +and +.Fa size +results in integer overflow, +.Fn calloc +and +.Fn reallocarray +return +.Dv NULL +and set +.Va errno +to +.Er ENOMEM . +.Sh IDIOMS +Consider +.Fn calloc +or the extension +.Fn reallocarray +when there is multiplication in the +.Fa size +argument of +.Fn malloc +or +.Fn realloc . +For example, avoid this common idiom as it may lead to integer overflow: +.Bd -literal -offset indent +if ((p = malloc(num * size)) == NULL) + err(1, NULL); +.Ed +.Pp +A drop-in replacement is the +.Ox +extension +.Fn reallocarray : +.Bd -literal -offset indent +if ((p = reallocarray(NULL, num, size)) == NULL) + err(1, NULL); +.Ed +.Pp +Alternatively, +.Fn calloc +may be used at the cost of initialization overhead. +.Pp +When using +.Fn realloc , +be careful to avoid the following idiom: +.Bd -literal -offset indent +size += 50; +if ((p = realloc(p, size)) == NULL) + return (NULL); +.Ed +.Pp +Do not adjust the variable describing how much memory has been allocated +until the allocation has been successful. +This can cause aberrant program behavior if the incorrect size value is used. +In most cases, the above sample will also result in a leak of memory. +As stated earlier, a return value of +.Dv NULL +indicates that the old object still remains allocated. +Better code looks like this: +.Bd -literal -offset indent +newsize = size + 50; +if ((newp = realloc(p, newsize)) == NULL) { + free(p); + p = NULL; + size = 0; + return (NULL); +} +p = newp; +size = newsize; +.Ed +.Pp +As with +.Fn malloc , +it is important to ensure the new size value will not overflow; +i.e. avoid allocations like the following: +.Bd -literal -offset indent +if ((newp = realloc(p, num * size)) == NULL) { + ... +.Ed +.Pp +Instead, use +.Fn reallocarray : +.Bd -literal -offset indent +if ((newp = reallocarray(p, num, size)) == NULL) { + ... +.Ed +.Pp +Calling +.Fn realloc +with a +.Dv NULL +.Fa ptr +is equivalent to calling +.Fn malloc . +Instead of this idiom: +.Bd -literal -offset indent +if (p == NULL) + newp = malloc(newsize); +else + newp = realloc(p, newsize); +.Ed +.Pp +Use the following: +.Bd -literal -offset indent +newp = realloc(p, newsize); +.Ed +.Sh ENVIRONMENT +.Bl -tag -width "/etc/malloc.conf" +.It Ev MALLOC_OPTIONS +String of flags documented in +.Xr malloc.conf 5 . +.El +.Sh FILES +.Bl -tag -width "/etc/malloc.conf" +.It Pa /etc/malloc.conf +Symbolic link to filename containing option flags. +.El +.Sh EXAMPLES +If +.Fn malloc +must be used with multiplication, be sure to test for overflow: +.Bd -literal -offset indent +size_t num, size; +\&... + +/* Check for size_t overflow */ +if (size && num > SIZE_MAX / size) + errc(1, EOVERFLOW, "overflow"); + +if ((p = malloc(size * num)) == NULL) + err(1, NULL); +.Ed +.Pp +The above test is not sufficient in all cases. +For example, multiplying ints requires a different set of checks: +.Bd -literal -offset indent +int num, size; +\&... + +/* Avoid invalid requests */ +if (size < 0 || num < 0) + errc(1, EOVERFLOW, "overflow"); + +/* Check for signed int overflow */ +if (size && num > INT_MAX / size) + errc(1, EOVERFLOW, "overflow"); + +if ((p = malloc(size * num)) == NULL) + err(1, NULL); +.Ed +.Pp +Assuming the implementation checks for integer overflow as +.Ox +does, it is much easier to use +.Fn calloc +or +.Fn reallocarray . +.Pp +The above examples could be simplified to: +.Bd -literal -offset indent +if ((p = reallocarray(NULL, num, size)) == NULL) + err(1, NULL); +.Ed +.Pp +or at the cost of initialization: +.Bd -literal -offset indent +if ((p = calloc(num, size)) == NULL) + err(1, NULL); +.Ed +.Sh DIAGNOSTICS +If +.Fn malloc , +.Fn calloc , +.Fn realloc , +.Fn reallocarray , +or +.Fn free +detect an error condition, +a message will be printed to file descriptor +2 (not using stdio). +Errors will result in the process being aborted. +.Pp +Here is a brief description of the error messages and what they mean: +.Bl -tag -width Ds +.It Dq out of memory +If the +.Cm X +option is specified it is an error for +.Fn malloc , +.Fn calloc , +.Fn realloc , +or +.Fn reallocarray +to return +.Dv NULL . +.It Dq malloc init mmap failed +This is a rather weird condition that is most likely to indicate a +seriously overloaded system or a ulimit restriction. +.It Dq bogus pointer (double free?) +An attempt to +.Fn free , +.Fn realloc , +or +.Fn reallocarray +an unallocated pointer was made. +.It Dq chunk is already free +There was an attempt to free a chunk that had already been freed. +.It Dq use after free +A chunk has been modified after it was freed. +.It Dq modified chunk-pointer +The pointer passed to +.Fn free , +.Fn realloc , +or +.Fn reallocarray +has been modified. +.It Dq chunk canary corrupted address offset@length +A byte after the requested size has been overwritten, +indicating a heap overflow. +The offset at which corruption was detected is printed before the @, +and the requested length of the allocation after the @. +.It Dq recursive call +An attempt was made to call recursively into these functions, i.e., from a +signal handler. +This behavior is not supported. +In particular, signal handlers should +.Em not +use any of the +.Fn malloc +functions nor utilize any other functions which may call +.Fn malloc +(e.g., +.Xr stdio 3 +routines). +.It Dq unknown char in MALLOC_OPTIONS +We found something we didn't understand. +.It Dq malloc cache overflow/underflow +The internal malloc page cache has been corrupted. +.It Dq malloc free slot lost +The internal malloc page cache has been corrupted. +.It Dq guard size +An inconsistent guard size was detected. +.It any other error +.Fn malloc +detected an internal error; +consult sources and/or wizards. +.El +.Sh SEE ALSO +.Xr brk 2 , +.Xr mmap 2 , +.Xr munmap 2 , +.Xr alloca 3 , +.Xr getpagesize 3 , +.Xr posix_memalign 3 , +.Xr sysconf 3 , +.Xr malloc.conf 5 +.Sh STANDARDS +The +.Fn malloc , +.Fn calloc , +.Fn realloc , +and +.Fn free +functions conform to +.St -ansiC . +.Pp +If +.Fa size +or +.Fa nmemb +are 0, the return value is implementation defined; +other conforming implementations may return +.Dv NULL +in this case. +.Pp +The +.Ev MALLOC_OPTIONS +environment variable, the file +.Pa /etc/malloc.conf , +and the +.Sx DIAGNOSTICS +output are extensions to the standard. +.Sh HISTORY +A +.Fn free +internal kernel function and a predecessor to +.Fn malloc , +.Fn alloc , +first appeared in +.At v1 . +C library functions +.Fn alloc +and +.Fn free +appeared in +.At v6 . +The functions +.Fn malloc , +.Fn calloc , +and +.Fn realloc +first appeared in +.At v7 . +.Pp +A new implementation by Chris Kingsley was introduced in +.Bx 4.2 , +followed by a complete rewrite by Poul-Henning Kamp which appeared in +.Fx 2.2 +and was included in +.Ox 2.0 . +These implementations were all +.Xr sbrk 2 +based. +In +.Ox 3.8 , +Thierry Deval rewrote +.Nm +to use the +.Xr mmap 2 +system call, +making the page addresses returned by +.Nm +random. +A rewrite by Otto Moerbeek introducing a new central data structure and more +randomization appeared in +.Ox 4.4 . +.Pp +The +.Fn reallocarray +function appeared in +.Ox 5.6 . +.Sh CAVEATS +When using +.Fn malloc , +be wary of signed integer and +.Vt size_t +overflow especially when there is multiplication in the +.Fa size +argument. +.Pp +Signed integer overflow will cause undefined behavior which compilers +typically handle by wrapping back around to negative numbers. +Depending on the input, this can result in allocating more or less +memory than intended. +.Pp +An unsigned overflow has defined behavior which will wrap back around and +return less memory than intended. +.Pp +A signed or unsigned integer overflow is a +.Em security +risk if less memory is returned than intended. +Subsequent code may corrupt the heap by writing beyond the memory that was +allocated. +An attacker may be able to leverage this heap corruption to execute arbitrary +code. +.Pp +Consider using +.Fn calloc +or +.Fn reallocarray +instead of using multiplication in +.Fn malloc +and +.Fn realloc +to avoid these problems on +.Ox . diff --git a/lib/libopenbsd/string/strlcpy.3 b/lib/libopenbsd/string/strlcpy.3 @@ -0,0 +1,188 @@ +.\" $OpenBSD: strlcpy.3,v 1.26 2013/09/30 12:02:35 millert Exp $ +.\" +.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com> +.\" +.\" Permission to use, copy, modify, and distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.Dd $Mdocdate: September 30 2013 $ +.Dt STRLCPY 3 +.Os +.Sh NAME +.Nm strlcpy , +.Nm strlcat +.Nd size-bounded string copying and concatenation +.Sh SYNOPSIS +.In string.h +.Ft size_t +.Fn strlcpy "char *dst" "const char *src" "size_t dstsize" +.Ft size_t +.Fn strlcat "char *dst" "const char *src" "size_t dstsize" +.Sh DESCRIPTION +The +.Fn strlcpy +and +.Fn strlcat +functions copy and concatenate strings with the +same input parameters and output result as +.Xr snprintf 3 . +They are designed to be safer, more consistent, and less error +prone replacements for the easily misused functions +.Xr strncpy 3 +and +.Xr strncat 3 . +.Pp +.Fn strlcpy +and +.Fn strlcat +take the full size of the destination buffer and guarantee +NUL-termination if there is room. +Note that room for the NUL should be included in +.Fa dstsize . +.Pp +.Fn strlcpy +copies up to +.Fa dstsize +\- 1 characters from the string +.Fa src +to +.Fa dst , +NUL-terminating the result if +.Fa dstsize +is not 0. +.Pp +.Fn strlcat +appends string +.Fa src +to the end of +.Fa dst . +It will append at most +.Fa dstsize +\- strlen(dst) \- 1 characters. +It will then NUL-terminate, unless +.Fa dstsize +is 0 or the original +.Fa dst +string was longer than +.Fa dstsize +(in practice this should not happen +as it means that either +.Fa dstsize +is incorrect or that +.Fa dst +is not a proper string). +.Pp +If the +.Fa src +and +.Fa dst +strings overlap, the behavior is undefined. +.Sh RETURN VALUES +Besides quibbles over the return type +.Pf ( Va size_t +versus +.Va int ) +and signal handler safety +.Pf ( Xr snprintf 3 +is not entirely safe on some systems), the +following two are equivalent: +.Bd -literal -offset indent +n = strlcpy(dst, src, len); +n = snprintf(dst, len, "%s", src); +.Ed +.Pp +Like +.Xr snprintf 3 , +the +.Fn strlcpy +and +.Fn strlcat +functions return the total length of the string they tried to create. +For +.Fn strlcpy +that means the length of +.Fa src . +For +.Fn strlcat +that means the initial length of +.Fa dst +plus +the length of +.Fa src . +.Pp +If the return value is +.Cm >= +.Va dstsize , +the output string has been truncated. +It is the caller's responsibility to handle this. +.Sh EXAMPLES +The following code fragment illustrates the simple case: +.Bd -literal -offset indent +char *s, *p, buf[BUFSIZ]; + +\&... + +(void)strlcpy(buf, s, sizeof(buf)); +(void)strlcat(buf, p, sizeof(buf)); +.Ed +.Pp +To detect truncation, perhaps while building a pathname, something +like the following might be used: +.Bd -literal -offset indent +char *dir, *file, pname[PATH_MAX]; + +\&... + +if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname)) + goto toolong; +if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) + goto toolong; +.Ed +.Pp +Since it is known how many characters were copied the first time, things +can be sped up a bit by using a copy instead of an append: +.Bd -literal -offset indent +char *dir, *file, pname[PATH_MAX]; +size_t n; + +\&... + +n = strlcpy(pname, dir, sizeof(pname)); +if (n >= sizeof(pname)) + goto toolong; +if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n) + goto toolong; +.Ed +.Pp +However, one may question the validity of such optimizations, as they +defeat the whole purpose of +.Fn strlcpy +and +.Fn strlcat . +As a matter of fact, the first version of this manual page got it wrong. +.Sh SEE ALSO +.Xr snprintf 3 , +.Xr strncat 3 , +.Xr strncpy 3 , +.Xr wcslcpy 3 +.Sh HISTORY +.Fn strlcpy +and +.Fn strlcat +first appeared in +.Ox 2.4 . +.Sh AUTHORS +.Fn strlcpy +and +.Fn strlcat +were created by +.An Todd C. Miller Aq Mt Todd.Miller@courtesan.com . diff --git a/lib/libopenbsd/unvis.3 b/lib/libopenbsd/unvis.3 @@ -1,195 +0,0 @@ -.\" $OpenBSD: unvis.3,v 1.18 2014/11/09 04:34:20 guenther Exp $ -.\" -.\" 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. -.\" 3. 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. -.\" -.Dd $Mdocdate: November 9 2014 $ -.Dt UNVIS 3 -.Os -.Sh NAME -.Nm unvis , -.Nm strunvis , -.Nm strnunvis -.Nd decode a visual representation of characters -.Sh SYNOPSIS -.In vis.h -.Ft int -.Fn unvis "char *cp" "char c" "int *astate" "int flag" -.Ft int -.Fn strunvis "char *dst" "const char *src" -.Ft ssize_t -.Fn strnunvis "char *dst" "const char *src" "size_t size" -.Sh DESCRIPTION -The -.Fn unvis , -.Fn strunvis -and -.Fn strnunvis -functions are used to decode a visual representation of characters, -as produced by the -.Xr vis 3 -function, back into the original form. -.Fn 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 . -.Pp -.Fn strunvis -decodes the characters pointed to by -.Fa src -into the buffer pointed to by -.Fa dst . -.Pp -.Fn strnunvis -decodes the characters pointed to by -.Fa src -into the buffer pointed to by -.Fa dst , -writing a maximum of -.Fa size -bytes. -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). -.Fn strunvis -terminates the destination string with a trailing NUL byte; -.Fn strnunvis -does so if -.Fa size -is larger than 0. -.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. -.Sh RETURN VALUES -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 -.Fa cp . -.It Dv UNVIS_VALIDPUSH -A valid character has been recognized and is available at the location -pointed to by -.Fa 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 -.Fn strunvis -function returns the number of bytes written (not counting -the trailing NUL byte) or \-1 if an error occurred. -.Pp -The -.Fn strnunvis -function returns the number of bytes (not counting the trailing NUL byte) -that would be needed to fully convert the input string, or \-1 if an -error occurred. -.Sh EXAMPLES -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 unvis 1 , -.Xr vis 1 , -.Xr vis 3 -.Sh HISTORY -The -.Fn unvis -function first appeared in -.Bx 4.4 . diff --git a/lib/libopenbsd/unvis.c b/lib/libopenbsd/unvis.c @@ -1,286 +0,0 @@ -/* $OpenBSD: unvis.c,v 1.17 2015/09/13 11:32:51 guenther Exp $ */ -/*- - * 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. - * 3. 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. - */ - -#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 isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') - -/* - * unvis - decode characters previously encoded by vis - */ -int -unvis(char *cp, char 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) { - - case S_GROUND: - *cp = 0; - if (c == '\\') { - *astate = S_START; - return (0); - } - *cp = c; - return (UNVIS_VALID); - - case S_START: - switch(c) { - case '-': - *cp = 0; - *astate = S_GROUND; - return (0); - case '\\': - 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 = (char) 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); - - default: - /* - * decoder in unknown state - (probably uninitialized) - */ - *astate = S_GROUND; - return (UNVIS_SYNBAD); - } -} -DEF_WEAK(unvis); - -/* - * 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: - *dst = '\0'; - return (-1); - } - } - if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) - dst++; - *dst = '\0'; - return (dst - start); -} - -ssize_t -strnunvis(char *dst, const char *src, size_t sz) -{ - char c, p; - char *start = dst, *end = dst + sz - 1; - int state = 0; - - if (sz > 0) - *end = '\0'; - while ((c = *src++)) { - again: - switch (unvis(&p, c, &state, 0)) { - case UNVIS_VALID: - if (dst < end) - *dst = p; - dst++; - break; - case UNVIS_VALIDPUSH: - if (dst < end) - *dst = p; - dst++; - goto again; - case 0: - case UNVIS_NOCHAR: - break; - default: - if (dst <= end) - *dst = '\0'; - return (-1); - } - } - if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) { - if (dst < end) - *dst = p; - dst++; - } - if (dst <= end) - *dst = '\0'; - return (dst - start); -} - diff --git a/lib/libopenbsd/util.h b/lib/libopenbsd/util.h @@ -1,2 +0,0 @@ - -void logwtmp(const char *, const char *, const char *); diff --git a/lib/libopenbsd/vis.3 b/lib/libopenbsd/vis.3 @@ -1,395 +0,0 @@ -.\" $OpenBSD: vis.3,v 1.35 2015/07/20 01:52:28 millert Exp $ -.\" -.\" 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. -.\" 3. 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. -.\" -.Dd $Mdocdate: July 20 2015 $ -.Dt VIS 3 -.Os -.Sh NAME -.Nm vis , -.Nm strvis , -.Nm strnvis , -.Nm strvisx , -.Nm stravis -.Nd visually encode characters -.Sh SYNOPSIS -.In stdlib.h -.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 strnvis "char *dst" "const char *src" "size_t dstsize" "int flag" -.Ft int -.Fn strvisx "char *dst" "const char *src" "size_t srclen" "int flag" -.Ft int -.Fn stravis "char **outp" "const char *src" "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. -.Fa dst -will be NUL-terminated and must be at least 5 bytes long -(maximum encoding requires 4 bytes plus the NUL). -The additional character, -.Fa nextc , -is only used when selecting the -.Dv VIS_CSTYLE -encoding format (explained below). -.Pp -The -.Fn strvis , -.Fn strnvis -and -.Fn strvisx -functions copy into -.Fa dst -a visual representation of -the string -.Fa src . -.Pp -The -.Fn strvis -function encodes characters from -.Fa src -up to the first NUL, into a buffer -.Fa dst -(which must be at least 4 * strlen(src) + 1 long). -.Pp -The -.Fn strnvis -function encodes characters from -.Fa src -up to the first NUL or the end of the buffer -.Fa dst , -as indicated by -.Fa dstsize . -.Pp -The -.Fn strvisx -function encodes exactly -.Fa srclen -characters from -.Fa src -into a buffer -.Fa dst -(which must be at least 4 * srclen + 1 long). -This -is useful for encoding a block of data that may contain NULs. -.Pp -The -.Fn stravis -function writes a visual representation of the string -.Fa src -into a newly allocated string -.Fa outp ; -it does not attempt to -.Xr realloc 3 -.Fa outp . -.Fa outp -should be passed to -.Xr free 3 -to release the allocated storage when it is no longer needed. -.Fn stravis -checks for integer overflow when allocating memory. -.Pp -All forms NUL-terminate -.Fa dst , -except for -.Fn strnvis -when -.Fa dstsize -is zero, in which case -.Fa dst -is not touched. -.Pp -The -.Fa flag -parameter is used for altering the default range of -characters considered for encoding and for altering the visual -representation. -.Ss Encodings -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_ALL -Encode all characters, whether visible or not. -.It Dv VIS_DQ -Also encode double quote characters -.Pf ( Ql \&" ) . -.It Dv VIS_GLOB -Also encode magic characters recognized by -.Xr glob 3 -.Pf ( Ql * , -.Ql \&? , -.Ql \&[ ) -and -.Ql # . -.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 | VIS_TAB | VIS_NL . -.It Dv VIS_SAFE -Only encode -.Dq unsafe -characters. -These are 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 three forms of encoding. -All forms use the backslash -.Ql \e -character to introduce a special -sequence; two backslashes are used to represent a real backslash. -These are the visual formats: -.Bl -tag -width VIS_CSTYLE -.It (default) -Use an -.Ql M -to represent meta characters (characters with the 8th -bit set), and use a caret -.Ql ^ -to represent control characters (see -.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. -.It Dv \e-C -Represents character -.Ql C . -Only used with -.Dv VIS_ALL . -.El -.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: -.Bd -unfilled -offset indent -.Li \ea Tn - BEL No (007) -.Li \eb Tn - BS No (010) -.Li \ef Tn - NP No (014) -.Li \en Tn - NL No (012) -.Li \er Tn - CR No (015) -.Li \es Tn - SP No (040) -.Li \et Tn - HT No (011) -.Li \ev Tn - VT No (013) -.Li \e0 Tn - NUL No (000) -.Ed -.Pp -When using this format, the -.Fa nextc -parameter is looked at to determine -if a 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_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 RETURN VALUES -.Fn vis -returns a pointer to the terminating NUL character of the string -.Fa dst . -.Pp -.Fn strvis -and -.Fn strvisx -return the number of characters in -.Fa dst -(not including the trailing NUL). -.Pp -.Fn strnvis -returns the length that -.Fa dst -would become if it were of unlimited size (similar to -.Xr snprintf 3 -or -.Xr strlcpy 3 ) . -This can be used to detect truncation, but it also means that -the return value of -.Fn strnvis -must not be used without checking it against -.Fa dstsize . -.Pp -Upon successful completion, -.Fn stravis -returns the number of characters in -.Pf * Fa outp -(not including the trailing NUL). -Otherwise, -.Fn stravis -returns -1 and sets -.Va errno -to -.Er ENOMEM . -.Sh EXAMPLES -.Fn strvis -has unusual storage requirements that can lead to stack or heap corruption -if the destination is not carefully constructed. -A common mistake is to use the same size for the source and destination -when the destination actually needs up to 4 * strlen(source) + 1 bytes. -.Pp -If the length of a string to be encoded is not known at compile time, use -.Fn stravis : -.Bd -literal -offset indent -char *src, *dst; - -\&... -if (stravis(&dst, src, VIS_OCTAL) == -1) - err(1, "stravis"); - -\&... -free(dst); -.Ed -.Pp -To encode a fixed size buffer, -.Fn strnvis -can be used with a fixed size target buffer: -.Bd -literal -offset indent -char src[MAXPATHLEN]; -char dst[4 * MAXPATHLEN + 1]; - -\&... -if (strnvis(dst, src, sizeof(dst), VIS_OCTAL) >= sizeof(dst)) - err(1, "strnvis"); -.Ed -.Sh SEE ALSO -.Xr unvis 1 , -.Xr vis 1 , -.Xr free 3 , -.Xr snprintf 3 , -.Xr strlcpy 3 , -.Xr unvis 3 -.Sh HISTORY -The -.Fn vis , -.Fn strvis -and -.Fn strvisx -functions first appeared in -.Bx 4.4 , -.Fn strnvis -in -.Ox 2.9 -and -.Fn stravis -in -.Ox 5.7 . -.Pp -The -.Dv VIS_ALL -flag first appeared in -.Ox 4.9 . diff --git a/lib/libopenbsd/vis.c b/lib/libopenbsd/vis.c @@ -1,243 +0,0 @@ -/* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */ -/*- - * 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. - * 3. 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. - */ - -#include <sys/types.h> -#include <errno.h> -#include <ctype.h> -#include <limits.h> -#include <string.h> -#include <stdlib.h> -#include <vis.h> - -#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') -#define isvisible(c,flag) \ - (((c) == '\\' || (flag & VIS_ALL) == 0) && \ - (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ - (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ - (flag & VIS_GLOB) == 0) && isgraph((u_char)(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' || \ - isgraph((u_char)(c)))))) - -/* - * vis - visually encode characters - */ -char * -vis(char *dst, int c, int flag, int nextc) -{ - if (isvisible(c, flag)) { - if ((c == '"' && (flag & VIS_DQ) != 0) || - (c == '\\' && (flag & VIS_NOSLASH) == 0)) - *dst++ = '\\'; - *dst++ = c; - *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) == ' ') || (flag & VIS_OCTAL) || - ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) { - *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((u_char)c)) { - *dst++ = '^'; - if (c == 0177) - *dst++ = '?'; - else - *dst++ = c + '@'; - } else { - *dst++ = '-'; - *dst++ = c; - } -done: - *dst = '\0'; - return (dst); -} -DEF_WEAK(vis); - -/* - * strvis, strnvis, 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 NULL, - * is returned. - * - * Strnvis will write no more than siz-1 bytes (and will NULL terminate). - * The number of bytes needed to fully encode the string is returned. - * - * Strvisx encodes exactly len bytes from src into dst. - * This is useful for encoding a block of data. - */ -int -strvis(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); -} -DEF_WEAK(strvis); - -int -strnvis(char *dst, const char *src, size_t siz, int flag) -{ - char *start, *end; - char tbuf[5]; - int c, i; - - i = 0; - for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { - if (isvisible(c, flag)) { - if ((c == '"' && (flag & VIS_DQ) != 0) || - (c == '\\' && (flag & VIS_NOSLASH) == 0)) { - /* need space for the extra '\\' */ - if (dst + 1 >= end) { - i = 2; - break; - } - *dst++ = '\\'; - } - i = 1; - *dst++ = c; - src++; - } else { - i = vis(tbuf, c, flag, *++src) - tbuf; - if (dst + i <= end) { - memcpy(dst, tbuf, i); - dst += i; - } else { - src--; - break; - } - } - } - if (siz > 0) - *dst = '\0'; - if (dst + i > end) { - /* adjust return value for truncation */ - while ((c = *src)) - dst += vis(tbuf, c, flag, *++src) - tbuf; - } - return (dst - start); -} - -int -stravis(char **outp, const char *src, int flag) -{ - char *buf; - int len, serrno; - - buf = reallocarray(NULL, 4, strlen(src) + 1); - if (buf == NULL) - return -1; - len = strvis(buf, src, flag); - serrno = errno; - *outp = realloc(buf, len + 1); - if (*outp == NULL) { - *outp = buf; - errno = serrno; - } - return (len); -} - -int -strvisx(char *dst, const char *src, size_t len, int flag) -{ - char 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); -} diff --git a/lib/libopenbsd/vis.h b/lib/libopenbsd/vis.h @@ -1,91 +0,0 @@ -/* $OpenBSD: vis.h,v 1.15 2015/07/20 01:52:27 millert Exp $ */ -/* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */ - -/*- - * Copyright (c) 1990 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. - * 3. 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. - * - * @(#)vis.h 5.9 (Berkeley) 4/3/91 - */ - -#ifndef _VIS_H_ -#define _VIS_H_ - -/* - * to select alternate encoding format - */ -#define VIS_OCTAL 0x01 /* use octal \ddd format */ -#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropriate */ - -/* - * to alter set of characters encoded (default is to encode all - * non-graphic except space, tab, and newline). - */ -#define VIS_SP 0x04 /* also encode space */ -#define VIS_TAB 0x08 /* also encode tab */ -#define VIS_NL 0x10 /* also encode newline */ -#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) -#define VIS_SAFE 0x20 /* only encode "unsafe" characters */ -#define VIS_DQ 0x200 /* backslash-escape double quotes */ -#define VIS_ALL 0x400 /* encode all characters */ - -/* - * other - */ -#define VIS_NOSLASH 0x40 /* inhibit printing '\' */ -#define VIS_GLOB 0x100 /* encode glob(3) magics and '#' */ - -/* - * unvis return codes - */ -#define UNVIS_VALID 1 /* character valid */ -#define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */ -#define UNVIS_NOCHAR 3 /* valid sequence, no character produced */ -#define UNVIS_SYNBAD -1 /* unrecognized escape sequence */ -#define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */ - -/* - * unvis flags - */ -#define UNVIS_END 1 /* no more characters */ - -__BEGIN_DECLS -char *vis(char *, int, int, int); -int strvis(char *, const char *, int); -int stravis(char **, const char *, int); -int strnvis(char *, const char *, size_t, int) - __attribute__ ((__bounded__(__string__,1,3))); -int strvisx(char *, const char *, size_t, int) - __attribute__ ((__bounded__(__string__,1,3))); -int strunvis(char *, const char *); -int unvis(char *, char, int *, int); -ssize_t strnunvis(char *, const char *, size_t) - __attribute__ ((__bounded__(__string__,1,3))); - -__END_DECLS - -#endif /* !_VIS_H_ */