mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-10-19 21:16:55 +02:00
Compare commits
69 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
54796231c7 | ||
![]() |
7aed0edf73 | ||
![]() |
fafcc397ac | ||
![]() |
48107fc8c4 | ||
![]() |
e7cf8c5785 | ||
![]() |
25d35625eb | ||
![]() |
500b3080a2 | ||
![]() |
1eba406021 | ||
![]() |
8ad7570c20 | ||
![]() |
43a8270317 | ||
![]() |
6a71b24b63 | ||
![]() |
7389fe8d24 | ||
![]() |
2716dfd0b7 | ||
![]() |
54f8745657 | ||
![]() |
428be9e030 | ||
![]() |
c7a5d780ae | ||
![]() |
1808d64b77 | ||
![]() |
beafad2657 | ||
![]() |
6145b56178 | ||
![]() |
731b0a7739 | ||
![]() |
50b50a4330 | ||
![]() |
25e88f6479 | ||
![]() |
04a8fb2469 | ||
![]() |
4f68a88f55 | ||
![]() |
8f59221c4f | ||
![]() |
72a82ee262 | ||
![]() |
3c305f2873 | ||
![]() |
25278891d8 | ||
![]() |
e35d9141dc | ||
![]() |
4feda87049 | ||
![]() |
d563a17430 | ||
![]() |
785cf9d1e9 | ||
![]() |
15bd284b29 | ||
![]() |
a9fc285988 | ||
![]() |
c0d86a6412 | ||
![]() |
1fb25b7dca | ||
![]() |
31f034e386 | ||
![]() |
2374f409de | ||
![]() |
a4e0db2b97 | ||
![]() |
43d34c9d3b | ||
![]() |
1c3ff61699 | ||
![]() |
edea268ce9 | ||
![]() |
e832b7687e | ||
![]() |
c4fca5bb4f | ||
![]() |
a1f79978e8 | ||
![]() |
4676026286 | ||
![]() |
25f9b30678 | ||
![]() |
18ebabf223 | ||
![]() |
4ab11c7f48 | ||
![]() |
766c883e30 | ||
![]() |
a4de4d95a6 | ||
![]() |
233cab9d64 | ||
![]() |
2462cd8888 | ||
![]() |
d54ceb37ce | ||
![]() |
847e682f8d | ||
![]() |
68f980c90d | ||
![]() |
37a9b56c05 | ||
![]() |
45dd5229ea | ||
![]() |
01f0d1ea1e | ||
![]() |
9c85d828a1 | ||
![]() |
eac4ce0c67 | ||
![]() |
5ecff0c903 | ||
![]() |
8c5a83d678 | ||
![]() |
d5b04ab19c | ||
![]() |
cfeafeabad | ||
![]() |
3d6b6ead64 | ||
![]() |
eb445425ff | ||
![]() |
59f6a95609 | ||
![]() |
3548c5f6bf |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
ChangeLog
|
||||
*~
|
||||
*.pc
|
||||
*.la
|
||||
*.lo
|
||||
@@ -7,6 +8,8 @@ ChangeLog
|
||||
*.a
|
||||
*.log
|
||||
*.trs
|
||||
*.gcda
|
||||
*.gcno
|
||||
.dirstamp
|
||||
.deps/
|
||||
.libs/
|
||||
@@ -17,6 +20,6 @@ autom4te.cache/
|
||||
build-aux/
|
||||
configure
|
||||
config.*
|
||||
format.ld
|
||||
libtool
|
||||
m4/
|
||||
stamp-h1
|
||||
|
@@ -1,9 +1,19 @@
|
||||
image: debian:stretch
|
||||
image: debian:buster
|
||||
|
||||
test:
|
||||
before_script:
|
||||
- apt update -qq
|
||||
- apt install -qq -y --no-install-recommends git gcc make autoconf automake libtool
|
||||
before_script:
|
||||
- apt-get update -qq
|
||||
- apt-get install -qq -y --no-install-recommends
|
||||
git gcc make autoconf automake libtool libmd-dev gcovr
|
||||
|
||||
unit-tests:
|
||||
stage: test
|
||||
script:
|
||||
- ./autogen && ./configure
|
||||
- make check
|
||||
|
||||
coverage:
|
||||
stage: test
|
||||
script:
|
||||
- ./autogen && ./configure --disable-static
|
||||
- make check CFLAGS="--coverage -O0 -ggdb" LDFLAGS="--coverage -O0 -ggdb"
|
||||
- gcovr -s -e test/
|
||||
|
26
COPYING
26
COPYING
@@ -3,12 +3,11 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
|
||||
Files:
|
||||
*
|
||||
Copyright:
|
||||
Copyright © 2004-2006, 2008-2018 Guillem Jover <guillem@hadrons.org>
|
||||
Copyright © 2004-2006, 2008-2021 Guillem Jover <guillem@hadrons.org>
|
||||
License: BSD-3-clause
|
||||
|
||||
Files:
|
||||
man/arc4random.3bsd
|
||||
man/tree.3bsd
|
||||
Copyright:
|
||||
Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
|
||||
All rights reserved.
|
||||
@@ -105,6 +104,7 @@ Files:
|
||||
man/getbsize.3bsd
|
||||
man/heapsort.3bsd
|
||||
man/nlist.3bsd
|
||||
man/pwcache.3bsd
|
||||
man/queue.3bsd
|
||||
man/radixsort.3bsd
|
||||
man/reallocarray.3bsd
|
||||
@@ -121,6 +121,7 @@ Files:
|
||||
src/heapsort.c
|
||||
src/merge.c
|
||||
src/nlist.c
|
||||
src/pwcache.c
|
||||
src/radixsort.c
|
||||
src/setmode.c
|
||||
src/strmode.c
|
||||
@@ -131,6 +132,7 @@ Files:
|
||||
Copyright:
|
||||
Copyright © 1980, 1982, 1986, 1989-1994
|
||||
The Regents of the University of California. All rights reserved.
|
||||
Copyright © 1992 Keith Muller.
|
||||
Copyright © 2001 Mike Barcroft <mike@FreeBSD.org>
|
||||
.
|
||||
Some code is derived from software contributed to Berkeley by
|
||||
@@ -275,8 +277,6 @@ Files:
|
||||
man/getpeereid.3bsd
|
||||
man/pidfile.3bsd
|
||||
src/expand_number.c
|
||||
src/hash/sha512.h
|
||||
src/hash/sha512c.c
|
||||
src/pidfile.c
|
||||
src/reallocf.c
|
||||
src/timeconv.c
|
||||
@@ -347,6 +347,7 @@ License: BSD-2-clause-verbatim
|
||||
Files:
|
||||
include/bsd/sys/tree.h
|
||||
man/fparseln.3bsd
|
||||
man/tree.3bsd
|
||||
src/fparseln.c
|
||||
Copyright:
|
||||
Copyright © 1997 Christos Zoulas.
|
||||
@@ -368,6 +369,7 @@ Files:
|
||||
src/arc4random_unix.h
|
||||
src/arc4random_win.h
|
||||
src/closefrom.c
|
||||
src/freezero.c
|
||||
src/getentropy_aix.c
|
||||
src/getentropy_bsd.c
|
||||
src/getentropy_hpux.c
|
||||
@@ -378,6 +380,7 @@ Files:
|
||||
src/getentropy_win.c
|
||||
src/readpassphrase.c
|
||||
src/reallocarray.c
|
||||
src/recallocarray.c
|
||||
src/strlcat.c
|
||||
src/strlcpy.c
|
||||
Copyright:
|
||||
@@ -389,7 +392,7 @@ Copyright:
|
||||
Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
Copyright © 2004 Ted Unangst
|
||||
Copyright © 2008 Damien Miller <djm@openbsd.org>
|
||||
Copyright © 2008 Otto Moerbeek <otto@drijf.net>
|
||||
Copyright © 2008, 2010-2011, 2016-2017 Otto Moerbeek <otto@drijf.net>
|
||||
Copyright © 2013 Markus Friedl <markus@openbsd.org>
|
||||
Copyright © 2014 Bob Beck <beck@obtuse.com>
|
||||
Copyright © 2014 Brent Cook <bcook@openbsd.org>
|
||||
@@ -453,17 +456,6 @@ License: Expat
|
||||
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Files:
|
||||
include/bsd/md5.h
|
||||
src/hash/md5.c
|
||||
Copyright:
|
||||
None
|
||||
License: public-domain-Colin-Plumb
|
||||
This code implements the MD5 message-digest algorithm.
|
||||
The algorithm is due to Ron Rivest. This code was
|
||||
written by Colin Plumb in 1993, no copyright is claimed.
|
||||
This code is in the public domain; do with it what you wish.
|
||||
|
||||
Files:
|
||||
src/explicit_bzero.c
|
||||
src/chacha_private.h
|
||||
@@ -474,8 +466,6 @@ License: public-domain
|
||||
|
||||
Files:
|
||||
man/mdX.3bsd
|
||||
src/hash/md5hl.c
|
||||
src/hash/helper.c
|
||||
Copyright:
|
||||
None
|
||||
License: Beerware
|
||||
|
3
README
3
README
@@ -5,6 +5,9 @@ and lacking on others like GNU systems, thus making it easier to port
|
||||
projects with strong BSD origins, without needing to embed the same
|
||||
code over and over again on each project.
|
||||
|
||||
A BSD compatible message-digest library is required, on systems where
|
||||
this is not provided by its libc or libmd libraries, the canonical
|
||||
implementation to use is <https://www.hadrons.org/software/libmd/>.
|
||||
|
||||
Releases
|
||||
--------
|
||||
|
100
configure.ac
100
configure.ac
@@ -12,8 +12,8 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])],
|
||||
[AC_SUBST([AM_DEFAULT_VERBOSITY], [1])])
|
||||
|
||||
LIBBSD_ABI_MAJOR=0
|
||||
LIBBSD_ABI_MINOR=10
|
||||
LIBBSD_ABI_PATCH=0
|
||||
LIBBSD_ABI_MINOR=11
|
||||
LIBBSD_ABI_PATCH=4
|
||||
|
||||
LIBBSD_ABI="$LIBBSD_ABI_MAJOR:$LIBBSD_ABI_MINOR:$LIBBSD_ABI_PATCH"
|
||||
AC_SUBST([LIBBSD_ABI])
|
||||
@@ -27,15 +27,71 @@ AC_SYS_LARGEFILE
|
||||
|
||||
LT_INIT
|
||||
|
||||
AC_CACHE_CHECK([if ld supports --version-script flag],
|
||||
[libbsd_cv_version_script], [
|
||||
echo "{ global: symbol; local: *; };" >conftest.map
|
||||
save_LDFLAGS=$LDFLAGS
|
||||
LDFLAGS="$LDFLAGS -Wl,--version-script=conftest.map"
|
||||
AC_LINK_IFELSE([
|
||||
AC_LANG_PROGRAM([], [])
|
||||
], [
|
||||
libbsd_cv_version_script=yes
|
||||
], [
|
||||
libbsd_cv_version_script=no
|
||||
])
|
||||
LDFLAGS="$save_LDFLAGS"
|
||||
rm -f conftest.map
|
||||
]
|
||||
)
|
||||
AM_CONDITIONAL([HAVE_LINKER_VERSION_SCRIPT],
|
||||
[test "x$libbsd_cv_version_script" = "xyes"])
|
||||
|
||||
# Checks for programs.
|
||||
AC_PROG_CC
|
||||
AC_PROG_SED
|
||||
AC_PROG_INSTALL
|
||||
AC_PROG_LN_S
|
||||
|
||||
# Set default compiler variables
|
||||
if test "$user_CFLAGS" = unset && test "$GCC" = yes; then
|
||||
CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter"
|
||||
fi
|
||||
AS_IF([test "$user_CFLAGS" = unset], [
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wall])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wextra])
|
||||
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wbad-function-cast])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wc99-c11-compat])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wcast-align])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wdeclaration-after-statement])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wdocumentation])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-branches])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-cond])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wformat -Wformat-security])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wformat=2])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Winit-self])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-not-parentheses])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-op])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-declarations])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-format-attribute])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-prototypes])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wnested-externs])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wno-missing-field-initializers])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wno-nonnull-compare])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wno-tautological-constant-out-of-range-compare])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wno-unused-parameter])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wnull-dereference])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wold-style-definition])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wpointer-arith])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wregister])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wrestrict])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wshadow])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wshift-negative-value])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wsizeof-array-argument])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wstrict-prototypes])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wswitch-bool])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wvla])
|
||||
LIBBSD_CHECK_COMPILER_FLAG([-Wwrite-strings])
|
||||
|
||||
CFLAGS="$CFLAGS $LIBBSD_COMPILER_FLAGS"
|
||||
])
|
||||
|
||||
# Checks for libraries.
|
||||
AC_CHECK_LIB([testu01], [unif01_CreateExternGenBits],
|
||||
@@ -44,6 +100,27 @@ AC_SUBST([TESTU01_LIBS])
|
||||
AM_CONDITIONAL([HAVE_LIBTESTU01],
|
||||
[test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
|
||||
|
||||
saved_LIBS="$LIBS"
|
||||
AC_SEARCH_LIBS([MD5Update], [md], [
|
||||
AS_IF([test "x$ac_cv_search_MD5Update" != "xnone required"], [
|
||||
MD5_LIBS="$MD5_LIBS $ac_cv_search_MD5Update"
|
||||
need_transparent_libmd=yes
|
||||
])
|
||||
], [
|
||||
AC_MSG_ERROR([cannot find required MD5 functions in libc or libmd])
|
||||
])
|
||||
AC_SEARCH_LIBS([SHA512Update], [md], [
|
||||
AS_IF([test "x$ac_cv_search_SHA512Update" != "xnone required"], [
|
||||
LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_SHA512Update"
|
||||
])
|
||||
], [
|
||||
AC_MSG_ERROR([cannot find required SHA-2 functions in libc or libmd])
|
||||
])
|
||||
LIBS="$saved_LIBS"
|
||||
|
||||
AM_CONDITIONAL([NEED_TRANSPARENT_LIBMD],
|
||||
[test "x$need_transparent_libmd" = "xyes"])
|
||||
|
||||
is_windows=no
|
||||
AS_CASE([$host_os],
|
||||
[*-gnu*], [
|
||||
@@ -51,10 +128,9 @@ AS_CASE([$host_os],
|
||||
saved_LIBS="$LIBS"
|
||||
AC_SEARCH_LIBS([clock_gettime], [rt], [
|
||||
AS_IF([test "x$ac_cv_search_clock_gettime" != "xnone required"], [
|
||||
CLOCK_GETTIME_LIBS="$ac_cv_search_clock_gettime"
|
||||
LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_clock_gettime"
|
||||
])
|
||||
])
|
||||
AC_SUBST([CLOCK_GETTIME_LIBS])
|
||||
LIBS="$saved_LIBS"
|
||||
],
|
||||
[*-musl*], [
|
||||
@@ -68,7 +144,7 @@ AS_CASE([$host_os],
|
||||
AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
|
||||
|
||||
# Checks for header files.
|
||||
AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h grp.h])
|
||||
AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h pwd.h grp.h])
|
||||
|
||||
# Checks for typedefs, structures, and compiler characteristics.
|
||||
AC_C_INLINE
|
||||
@@ -96,7 +172,7 @@ AC_CACHE_CHECK(
|
||||
[[
|
||||
static int rc = 1;
|
||||
static void init(int argc) { if (argc == 1) rc = 0; }
|
||||
void (*init_func)(int argc) __attribute__((__section__(".init_array"))) = init;
|
||||
void (*init_func)(int argc) __attribute__((__section__(".init_array"), __used__)) = init;
|
||||
int main() { return rc; }
|
||||
]]
|
||||
)],
|
||||
@@ -162,8 +238,7 @@ AC_LINK_IFELSE(
|
||||
[AC_DEFINE([HAVE___REGISTER_ATFORK], [1],
|
||||
[Define to 1 if you have __register_atfork])
|
||||
AC_MSG_RESULT([yes])],
|
||||
[ARC4RANDOM_ATFORK_LIBS="-pthread"
|
||||
AC_SUBST([ARC4RANDOM_ATFORK_LIBS])
|
||||
[LIBBSD_LIBS="$LIBBSD_LIBS -pthread"
|
||||
AC_MSG_RESULT([no])
|
||||
])
|
||||
|
||||
@@ -172,6 +247,9 @@ AC_CHECK_FUNCS([clearenv dirfd fopencookie __fpurge \
|
||||
pstat_getproc sysconf])
|
||||
AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xtrue"])
|
||||
|
||||
AC_SUBST([MD5_LIBS])
|
||||
AC_SUBST([LIBBSD_LIBS])
|
||||
|
||||
AC_CONFIG_FILES([
|
||||
Makefile
|
||||
include/Makefile
|
||||
|
@@ -14,10 +14,12 @@ nobase_include_HEADERS = \
|
||||
bsd/bsd.h \
|
||||
bsd/err.h \
|
||||
bsd/getopt.h \
|
||||
bsd/grp.h \
|
||||
bsd/inttypes.h \
|
||||
bsd/libutil.h \
|
||||
bsd/md5.h \
|
||||
bsd/nlist.h \
|
||||
bsd/pwd.h \
|
||||
bsd/readpassphrase.h \
|
||||
bsd/stdio.h \
|
||||
bsd/stdlib.h \
|
||||
|
51
include/bsd/grp.h
Normal file
51
include/bsd/grp.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright © 2021 Guillem Jover <guillem@hadrons.org>
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR 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.
|
||||
*/
|
||||
|
||||
#ifdef LIBBSD_OVERLAY
|
||||
#include <sys/cdefs.h>
|
||||
#if __has_include_next(<grp.h>)
|
||||
#include_next <grp.h>
|
||||
#endif
|
||||
#else
|
||||
#include <bsd/sys/cdefs.h>
|
||||
#if __has_include(<grp.h>)
|
||||
#include <grp.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef LIBBSD_GRP_H
|
||||
#define LIBBSD_GRP_H
|
||||
|
||||
#define _GR_BUF_LEN (1024 + 200 * sizeof(char *))
|
||||
|
||||
__BEGIN_DECLS
|
||||
int
|
||||
gid_from_group(const char *, gid_t *);
|
||||
const char *
|
||||
group_from_gid(gid_t, int);
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
@@ -1,58 +1,31 @@
|
||||
/* $OpenBSD: md5.h,v 1.16 2004/06/22 01:57:30 jfb Exp $ */
|
||||
|
||||
/*
|
||||
* This code implements the MD5 message-digest algorithm.
|
||||
* The algorithm is due to Ron Rivest. This code was
|
||||
* written by Colin Plumb in 1993, no copyright is claimed.
|
||||
* This code is in the public domain; do with it what you wish.
|
||||
* Copyright © 2021 Guillem Jover <guillem@hadrons.org>
|
||||
*
|
||||
* Equivalent code is available from RSA Data Security, Inc.
|
||||
* This code has been tested against that, and is equivalent,
|
||||
* except that you don't need to include two pages of legalese
|
||||
* with every copy.
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR 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.
|
||||
*/
|
||||
|
||||
#ifndef LIBBSD_MD5_H
|
||||
#define LIBBSD_MD5_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MD5_BLOCK_LENGTH 64
|
||||
#define MD5_DIGEST_LENGTH 16
|
||||
#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
|
||||
|
||||
typedef struct MD5Context {
|
||||
uint32_t state[4]; /* state */
|
||||
uint64_t count; /* number of bits, mod 2^64 */
|
||||
uint8_t buffer[MD5_BLOCK_LENGTH]; /* input buffer */
|
||||
} MD5_CTX;
|
||||
|
||||
#ifdef LIBBSD_OVERLAY
|
||||
#include <sys/cdefs.h>
|
||||
#include_next <md5.h>
|
||||
#else
|
||||
#include <bsd/sys/cdefs.h>
|
||||
#include <md5.h>
|
||||
#endif
|
||||
#include <sys/types.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
void MD5Init(MD5_CTX *);
|
||||
void MD5Update(MD5_CTX *, const uint8_t *, size_t)
|
||||
__attribute__((__bounded__(__string__,2,3)));
|
||||
void MD5Pad(MD5_CTX *);
|
||||
void MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *)
|
||||
__attribute__((__bounded__(__minbytes__,1,MD5_DIGEST_LENGTH)));
|
||||
void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH])
|
||||
__attribute__((__bounded__(__minbytes__,1,4)))
|
||||
__attribute__((__bounded__(__minbytes__,2,MD5_BLOCK_LENGTH)));
|
||||
char *MD5End(MD5_CTX *, char *)
|
||||
__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
|
||||
char *MD5File(const char *, char *)
|
||||
__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
|
||||
char *MD5FileChunk(const char *, char *, off_t, off_t)
|
||||
__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
|
||||
char *MD5Data(const uint8_t *, size_t, char *)
|
||||
__attribute__((__bounded__(__string__,1,2)))
|
||||
__attribute__((__bounded__(__minbytes__,3,MD5_DIGEST_STRING_LENGTH)));
|
||||
__END_DECLS
|
||||
|
||||
#endif /* LIBBSD_MD5_H */
|
||||
|
51
include/bsd/pwd.h
Normal file
51
include/bsd/pwd.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright © 2021 Guillem Jover <guillem@hadrons.org>
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR 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.
|
||||
*/
|
||||
|
||||
#ifdef LIBBSD_OVERLAY
|
||||
#include <sys/cdefs.h>
|
||||
#if __has_include_next(<pwd.h>)
|
||||
#include_next <pwd.h>
|
||||
#endif
|
||||
#else
|
||||
#include <bsd/sys/cdefs.h>
|
||||
#if __has_include(<pwd.h>)
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef LIBBSD_PWD_H
|
||||
#define LIBBSD_PWD_H
|
||||
|
||||
#define _PW_BUF_LEN 1024 /* length of getpw*_r buffer */
|
||||
|
||||
__BEGIN_DECLS
|
||||
int
|
||||
uid_from_user(const char *, uid_t *);
|
||||
const char *
|
||||
user_from_uid(uid_t, int);
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
@@ -62,7 +62,7 @@ int dehumanize_number(const char *str, int64_t *size);
|
||||
const char *getprogname(void);
|
||||
void setprogname(const char *);
|
||||
|
||||
int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
|
||||
int heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
|
||||
int mergesort(void *base, size_t nmemb, size_t size,
|
||||
int (*cmp)(const void *, const void *));
|
||||
int radixsort(const unsigned char **base, int nmemb,
|
||||
@@ -75,6 +75,8 @@ void *reallocf(void *ptr, size_t size);
|
||||
(defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 26) || !defined(_GNU_SOURCE)))
|
||||
void *reallocarray(void *ptr, size_t nmemb, size_t size);
|
||||
#endif
|
||||
void *recallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size);
|
||||
void freezero(void *ptr, size_t size);
|
||||
|
||||
long long strtonum(const char *nptr, long long minval, long long maxval,
|
||||
const char **errstr);
|
||||
|
@@ -187,10 +187,10 @@
|
||||
# else
|
||||
# ifndef __cplusplus
|
||||
# define __offsetof(type, field) \
|
||||
((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
|
||||
((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field))
|
||||
# else
|
||||
# define __offsetof(type, field) \
|
||||
(__offsetof__ (reinterpret_cast <__size_t> \
|
||||
(__offsetof__ (reinterpret_cast <size_t> \
|
||||
(&reinterpret_cast <const volatile char &> \
|
||||
(static_cast<type *> (0)->field))))
|
||||
# endif
|
||||
@@ -243,15 +243,15 @@
|
||||
#endif
|
||||
|
||||
#ifndef __DECONST
|
||||
#define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var))
|
||||
#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
|
||||
#endif
|
||||
|
||||
#ifndef __DEVOLATILE
|
||||
#define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var))
|
||||
#define __DEVOLATILE(type, var) ((type)(uintptr_t)(volatile void *)(var))
|
||||
#endif
|
||||
|
||||
#ifndef __DEQUALIFY
|
||||
#define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
|
||||
#define __DEQUALIFY(type, var) ((type)(uintptr_t)(const volatile void *)(var))
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@@ -1,4 +1,6 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* Copyright (c) 1991, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
@@ -10,7 +12,7 @@
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* 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.
|
||||
*
|
||||
@@ -80,17 +82,25 @@
|
||||
*
|
||||
* For details on the use of these macros, see the queue(3) manual page.
|
||||
*
|
||||
* Below is a summary of implemented functions where:
|
||||
* + means the macro is available
|
||||
* - means the macro is not available
|
||||
* s means the macro is available but is slow (runs in O(n) time)
|
||||
*
|
||||
* SLIST LIST STAILQ TAILQ
|
||||
* _HEAD + + + +
|
||||
* _CLASS_HEAD + + + +
|
||||
* _HEAD_INITIALIZER + + + +
|
||||
* _ENTRY + + + +
|
||||
* _CLASS_ENTRY + + + +
|
||||
* _INIT + + + +
|
||||
* _EMPTY + + + +
|
||||
* _END + + + +
|
||||
* _FIRST + + + +
|
||||
* _NEXT + + + +
|
||||
* _PREV - + - +
|
||||
* _LAST - - + +
|
||||
* _LAST_FAST - - - +
|
||||
* _FOREACH + + + +
|
||||
* _FOREACH_FROM + + + +
|
||||
* _FOREACH_SAFE + + + +
|
||||
@@ -103,14 +113,20 @@
|
||||
* _INSERT_BEFORE - + - +
|
||||
* _INSERT_AFTER + + + +
|
||||
* _INSERT_TAIL - - + +
|
||||
* _CONCAT - - + +
|
||||
* _CONCAT s s + +
|
||||
* _REMOVE_AFTER + - + -
|
||||
* _REMOVE_HEAD + - + -
|
||||
* _REMOVE + + + +
|
||||
* _REMOVE s + s +
|
||||
* _SWAP + + + +
|
||||
*
|
||||
*/
|
||||
#ifdef QUEUE_MACRO_DEBUG
|
||||
#warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
|
||||
#define QUEUE_MACRO_DEBUG_TRACE
|
||||
#define QUEUE_MACRO_DEBUG_TRASH
|
||||
#endif
|
||||
|
||||
#ifdef QUEUE_MACRO_DEBUG_TRACE
|
||||
/* Store the last 2 places the queue element or head was altered */
|
||||
struct qm_trace {
|
||||
unsigned long lastline;
|
||||
@@ -120,9 +136,7 @@ struct qm_trace {
|
||||
};
|
||||
|
||||
#define TRACEBUF struct qm_trace trace;
|
||||
#define TRACEBUF_INITIALIZER { __FILE__, __LINE__, NULL, 0 } ,
|
||||
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
|
||||
#define QMD_SAVELINK(name, link) void **name = (void *)&(link)
|
||||
#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
|
||||
|
||||
#define QMD_TRACE_HEAD(head) do { \
|
||||
(head)->trace.prevline = (head)->trace.lastline; \
|
||||
@@ -138,14 +152,31 @@ struct qm_trace {
|
||||
(elem)->trace.lastfile = __FILE__; \
|
||||
} while (0)
|
||||
|
||||
#else
|
||||
#else /* !QUEUE_MACRO_DEBUG_TRACE */
|
||||
#define QMD_TRACE_ELEM(elem)
|
||||
#define QMD_TRACE_HEAD(head)
|
||||
#define QMD_SAVELINK(name, link)
|
||||
#define TRACEBUF
|
||||
#define TRACEBUF_INITIALIZER
|
||||
#endif /* QUEUE_MACRO_DEBUG_TRACE */
|
||||
|
||||
#ifdef QUEUE_MACRO_DEBUG_TRASH
|
||||
#define QMD_SAVELINK(name, link) void **name = (void *)&(link)
|
||||
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
|
||||
#define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
|
||||
#else /* !QUEUE_MACRO_DEBUG_TRASH */
|
||||
#define QMD_SAVELINK(name, link)
|
||||
#define TRASHIT(x)
|
||||
#endif /* QUEUE_MACRO_DEBUG */
|
||||
#define QMD_IS_TRASHED(x) 0
|
||||
#endif /* QUEUE_MACRO_DEBUG_TRASH */
|
||||
|
||||
#ifdef __cplusplus
|
||||
/*
|
||||
* In C++ there can be structure lists and class lists:
|
||||
*/
|
||||
#define QUEUE_TYPEOF(type) type
|
||||
#else
|
||||
#define QUEUE_TYPEOF(type) struct type
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Singly-linked List declarations.
|
||||
@@ -155,6 +186,11 @@ struct name { \
|
||||
struct type *slh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define SLIST_CLASS_HEAD(name, type) \
|
||||
struct name { \
|
||||
class type *slh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define SLIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
@@ -163,9 +199,37 @@ struct { \
|
||||
struct type *sle_next; /* next element */ \
|
||||
}
|
||||
|
||||
#define SLIST_CLASS_ENTRY(type) \
|
||||
struct { \
|
||||
class type *sle_next; /* next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Singly-linked List functions.
|
||||
*/
|
||||
#if (defined(_KERNEL) && defined(INVARIANTS))
|
||||
#define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
|
||||
if (*(prevp) != (elm)) \
|
||||
panic("Bad prevptr *(%p) == %p != %p", \
|
||||
(prevp), *(prevp), (elm)); \
|
||||
} while (0)
|
||||
#else
|
||||
#define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
|
||||
#endif
|
||||
|
||||
#define SLIST_CONCAT(head1, head2, type, field) do { \
|
||||
QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
|
||||
if (curelm == NULL) { \
|
||||
if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
|
||||
SLIST_INIT(head2); \
|
||||
} else if (SLIST_FIRST(head2) != NULL) { \
|
||||
while (SLIST_NEXT(curelm, field) != NULL) \
|
||||
curelm = SLIST_NEXT(curelm, field); \
|
||||
SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
|
||||
SLIST_INIT(head2); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
|
||||
|
||||
#define SLIST_FIRST(head) ((head)->slh_first)
|
||||
@@ -217,7 +281,7 @@ struct { \
|
||||
SLIST_REMOVE_HEAD((head), field); \
|
||||
} \
|
||||
else { \
|
||||
struct type *curelm = SLIST_FIRST((head)); \
|
||||
QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
|
||||
while (SLIST_NEXT(curelm, field) != (elm)) \
|
||||
curelm = SLIST_NEXT(curelm, field); \
|
||||
SLIST_REMOVE_AFTER(curelm, field); \
|
||||
@@ -234,12 +298,20 @@ struct { \
|
||||
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
|
||||
QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
|
||||
*(prevp) = SLIST_NEXT(elm, field); \
|
||||
TRASHIT((elm)->field.sle_next); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_SWAP(head1, head2, type) do { \
|
||||
struct type *swap_first = SLIST_FIRST(head1); \
|
||||
QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
|
||||
SLIST_FIRST(head1) = SLIST_FIRST(head2); \
|
||||
SLIST_FIRST(head2) = swap_first; \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_END(head) NULL
|
||||
|
||||
/*
|
||||
* Singly-linked Tail queue declarations.
|
||||
*/
|
||||
@@ -249,6 +321,12 @@ struct name { \
|
||||
struct type **stqh_last;/* addr of last next element */ \
|
||||
}
|
||||
|
||||
#define STAILQ_CLASS_HEAD(name, type) \
|
||||
struct name { \
|
||||
class type *stqh_first; /* first element */ \
|
||||
class type **stqh_last; /* addr of last next element */ \
|
||||
}
|
||||
|
||||
#define STAILQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).stqh_first }
|
||||
|
||||
@@ -257,6 +335,11 @@ struct { \
|
||||
struct type *stqe_next; /* next element */ \
|
||||
}
|
||||
|
||||
#define STAILQ_CLASS_ENTRY(type) \
|
||||
struct { \
|
||||
class type *stqe_next; /* next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Singly-linked Tail queue functions.
|
||||
*/
|
||||
@@ -317,7 +400,8 @@ struct { \
|
||||
|
||||
#define STAILQ_LAST(head, type, field) \
|
||||
(STAILQ_EMPTY((head)) ? NULL : \
|
||||
__containerof((head)->stqh_last, struct type, field.stqe_next))
|
||||
__containerof((head)->stqh_last, \
|
||||
QUEUE_TYPEOF(type), field.stqe_next))
|
||||
|
||||
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
|
||||
|
||||
@@ -327,7 +411,7 @@ struct { \
|
||||
STAILQ_REMOVE_HEAD((head), field); \
|
||||
} \
|
||||
else { \
|
||||
struct type *curelm = STAILQ_FIRST((head)); \
|
||||
QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
|
||||
while (STAILQ_NEXT(curelm, field) != (elm)) \
|
||||
curelm = STAILQ_NEXT(curelm, field); \
|
||||
STAILQ_REMOVE_AFTER(head, curelm, field); \
|
||||
@@ -348,8 +432,8 @@ struct { \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_SWAP(head1, head2, type) do { \
|
||||
struct type *swap_first = STAILQ_FIRST(head1); \
|
||||
struct type **swap_last = (head1)->stqh_last; \
|
||||
QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
|
||||
QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
|
||||
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
|
||||
(head1)->stqh_last = (head2)->stqh_last; \
|
||||
STAILQ_FIRST(head2) = swap_first; \
|
||||
@@ -360,6 +444,8 @@ struct { \
|
||||
(head2)->stqh_last = &STAILQ_FIRST(head2); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_END(head) NULL
|
||||
|
||||
|
||||
/*
|
||||
* List declarations.
|
||||
@@ -369,6 +455,11 @@ struct name { \
|
||||
struct type *lh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define LIST_CLASS_HEAD(name, type) \
|
||||
struct name { \
|
||||
class type *lh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define LIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
@@ -378,11 +469,23 @@ struct { \
|
||||
struct type **le_prev; /* address of previous next element */ \
|
||||
}
|
||||
|
||||
#define LIST_CLASS_ENTRY(type) \
|
||||
struct { \
|
||||
class type *le_next; /* next element */ \
|
||||
class type **le_prev; /* address of previous next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* List functions.
|
||||
*/
|
||||
|
||||
#if (defined(_KERNEL) && defined(INVARIANTS))
|
||||
/*
|
||||
* QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME)
|
||||
*
|
||||
* If the list is non-empty, validates that the first element of the list
|
||||
* points back at 'head.'
|
||||
*/
|
||||
#define QMD_LIST_CHECK_HEAD(head, field) do { \
|
||||
if (LIST_FIRST((head)) != NULL && \
|
||||
LIST_FIRST((head))->field.le_prev != \
|
||||
@@ -390,6 +493,12 @@ struct { \
|
||||
panic("Bad list head %p first->prev != head", (head)); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME)
|
||||
*
|
||||
* If an element follows 'elm' in the list, validates that the next element
|
||||
* points back at 'elm.'
|
||||
*/
|
||||
#define QMD_LIST_CHECK_NEXT(elm, field) do { \
|
||||
if (LIST_NEXT((elm), field) != NULL && \
|
||||
LIST_NEXT((elm), field)->field.le_prev != \
|
||||
@@ -397,6 +506,11 @@ struct { \
|
||||
panic("Bad link elm %p next->prev != elm", (elm)); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME)
|
||||
*
|
||||
* Validates that the previous element (or head of the list) points to 'elm.'
|
||||
*/
|
||||
#define QMD_LIST_CHECK_PREV(elm, field) do { \
|
||||
if (*(elm)->field.le_prev != (elm)) \
|
||||
panic("Bad link elm %p prev->next != elm", (elm)); \
|
||||
@@ -407,6 +521,23 @@ struct { \
|
||||
#define QMD_LIST_CHECK_PREV(elm, field)
|
||||
#endif /* (_KERNEL && INVARIANTS) */
|
||||
|
||||
#define LIST_CONCAT(head1, head2, type, field) do { \
|
||||
QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
|
||||
if (curelm == NULL) { \
|
||||
if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
|
||||
LIST_FIRST(head2)->field.le_prev = \
|
||||
&LIST_FIRST((head1)); \
|
||||
LIST_INIT(head2); \
|
||||
} \
|
||||
} else if (LIST_FIRST(head2) != NULL) { \
|
||||
while (LIST_NEXT(curelm, field) != NULL) \
|
||||
curelm = LIST_NEXT(curelm, field); \
|
||||
LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
|
||||
LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
|
||||
LIST_INIT(head2); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
|
||||
|
||||
#define LIST_FIRST(head) ((head)->lh_first)
|
||||
@@ -464,7 +595,8 @@ struct { \
|
||||
|
||||
#define LIST_PREV(elm, head, type, field) \
|
||||
((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
|
||||
__containerof((elm)->field.le_prev, struct type, field.le_next))
|
||||
__containerof((elm)->field.le_prev, \
|
||||
QUEUE_TYPEOF(type), field.le_next))
|
||||
|
||||
#define LIST_REMOVE(elm, field) do { \
|
||||
QMD_SAVELINK(oldnext, (elm)->field.le_next); \
|
||||
@@ -480,7 +612,7 @@ struct { \
|
||||
} while (0)
|
||||
|
||||
#define LIST_SWAP(head1, head2, type, field) do { \
|
||||
struct type *swap_tmp = LIST_FIRST((head1)); \
|
||||
QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
|
||||
LIST_FIRST((head1)) = LIST_FIRST((head2)); \
|
||||
LIST_FIRST((head2)) = swap_tmp; \
|
||||
if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
|
||||
@@ -489,6 +621,8 @@ struct { \
|
||||
swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
|
||||
} while (0)
|
||||
|
||||
#define LIST_END(head) NULL
|
||||
|
||||
/*
|
||||
* Tail queue declarations.
|
||||
*/
|
||||
@@ -499,6 +633,13 @@ struct name { \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
#define TAILQ_CLASS_HEAD(name, type) \
|
||||
struct name { \
|
||||
class type *tqh_first; /* first element */ \
|
||||
class type **tqh_last; /* addr of last next element */ \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
#define TAILQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
|
||||
|
||||
@@ -509,10 +650,23 @@ struct { \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
#define TAILQ_CLASS_ENTRY(type) \
|
||||
struct { \
|
||||
class type *tqe_next; /* next element */ \
|
||||
class type **tqe_prev; /* address of previous next element */ \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
/*
|
||||
* Tail queue functions.
|
||||
*/
|
||||
#if (defined(_KERNEL) && defined(INVARIANTS))
|
||||
/*
|
||||
* QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
|
||||
*
|
||||
* If the tailq is non-empty, validates that the first element of the tailq
|
||||
* points back at 'head.'
|
||||
*/
|
||||
#define QMD_TAILQ_CHECK_HEAD(head, field) do { \
|
||||
if (!TAILQ_EMPTY(head) && \
|
||||
TAILQ_FIRST((head))->field.tqe_prev != \
|
||||
@@ -520,11 +674,22 @@ struct { \
|
||||
panic("Bad tailq head %p first->prev != head", (head)); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
|
||||
*
|
||||
* Validates that the tail of the tailq is a pointer to pointer to NULL.
|
||||
*/
|
||||
#define QMD_TAILQ_CHECK_TAIL(head, field) do { \
|
||||
if (*(head)->tqh_last != NULL) \
|
||||
panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME)
|
||||
*
|
||||
* If an element follows 'elm' in the tailq, validates that the next element
|
||||
* points back at 'elm.'
|
||||
*/
|
||||
#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
|
||||
if (TAILQ_NEXT((elm), field) != NULL && \
|
||||
TAILQ_NEXT((elm), field)->field.tqe_prev != \
|
||||
@@ -532,6 +697,11 @@ struct { \
|
||||
panic("Bad link elm %p next->prev != elm", (elm)); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME)
|
||||
*
|
||||
* Validates that the previous element (or head of the tailq) points to 'elm.'
|
||||
*/
|
||||
#define QMD_TAILQ_CHECK_PREV(elm, field) do { \
|
||||
if (*(elm)->field.tqe_prev != (elm)) \
|
||||
panic("Bad link elm %p prev->next != elm", (elm)); \
|
||||
@@ -616,7 +786,7 @@ struct { \
|
||||
TAILQ_NEXT((listelm), field) = (elm); \
|
||||
(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
|
||||
QMD_TRACE_ELEM(&(elm)->field); \
|
||||
QMD_TRACE_ELEM(&listelm->field); \
|
||||
QMD_TRACE_ELEM(&(listelm)->field); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
@@ -626,7 +796,7 @@ struct { \
|
||||
*(listelm)->field.tqe_prev = (elm); \
|
||||
(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
|
||||
QMD_TRACE_ELEM(&(elm)->field); \
|
||||
QMD_TRACE_ELEM(&listelm->field); \
|
||||
QMD_TRACE_ELEM(&(listelm)->field); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||
@@ -655,11 +825,25 @@ struct { \
|
||||
#define TAILQ_LAST(head, headname) \
|
||||
(*(((struct headname *)((head)->tqh_last))->tqh_last))
|
||||
|
||||
/*
|
||||
* The FAST function is fast in that it causes no data access other
|
||||
* then the access to the head. The standard LAST function above
|
||||
* will cause a data access of both the element you want and
|
||||
* the previous element. FAST is very useful for instances when
|
||||
* you may want to prefetch the last data element.
|
||||
*/
|
||||
#define TAILQ_LAST_FAST(head, type, field) \
|
||||
(TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next))
|
||||
|
||||
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
||||
|
||||
#define TAILQ_PREV(elm, headname, field) \
|
||||
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
|
||||
|
||||
#define TAILQ_PREV_FAST(elm, head, type, field) \
|
||||
((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \
|
||||
__containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next))
|
||||
|
||||
#define TAILQ_REMOVE(head, elm, field) do { \
|
||||
QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
|
||||
QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
|
||||
@@ -679,8 +863,8 @@ struct { \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_SWAP(head1, head2, type, field) do { \
|
||||
struct type *swap_first = (head1)->tqh_first; \
|
||||
struct type **swap_last = (head1)->tqh_last; \
|
||||
QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
|
||||
QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
|
||||
(head1)->tqh_first = (head2)->tqh_first; \
|
||||
(head1)->tqh_last = (head2)->tqh_last; \
|
||||
(head2)->tqh_first = swap_first; \
|
||||
@@ -695,4 +879,6 @@ struct { \
|
||||
(head2)->tqh_last = &(head2)->tqh_first; \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_END(head) NULL
|
||||
|
||||
#endif /* !LIBBSD_SYS_QUEUE_H */
|
||||
|
@@ -99,8 +99,11 @@
|
||||
* Provide both implementations and default for now on the historical one to
|
||||
* avoid breakage, we will switch to the NetBSD one in libbsd 0.10.0 or so.
|
||||
* Define LIBBSD_NETBSD_VIS to switch to the NetBSD one now.
|
||||
* Define LIBBSD_OPENBSD_VIS to keep using the OpenBSD one.
|
||||
*/
|
||||
#ifndef LIBBSD_NETBSD_VIS
|
||||
#if defined(LIBBSD_OPENBSD_VIS)
|
||||
#undef LIBBSD_NETBSD_VIS
|
||||
#elif !defined(LIBBSD_NETBSD_VIS)
|
||||
#warning "NetBSD added incompatible strnvis() and strnunvis(), please see <bsd/vis.h> for more detils."
|
||||
#endif
|
||||
|
||||
|
2
m4/.gitignore
vendored
Normal file
2
m4/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*.m4
|
||||
!libbsd*.m4
|
24
m4/libbsd-compiler.m4
Normal file
24
m4/libbsd-compiler.m4
Normal file
@@ -0,0 +1,24 @@
|
||||
# Copyright © 2021 Guillem Jover <guillem@hadrons.org>
|
||||
|
||||
# LIBBSD_CHECK_COMPILER_FLAG
|
||||
# -------------------------
|
||||
AC_DEFUN([LIBBSD_CHECK_COMPILER_FLAG], [
|
||||
AS_VAR_PUSHDEF([libbsd_varname_cache], [libbsd_cv_cflags_$1])
|
||||
AC_CACHE_CHECK([whether $CC accepts $1], [libbsd_varname_cache], [
|
||||
m4_define([libbsd_check_flag], m4_bpatsubst([$1], [^-Wno-], [-W]))
|
||||
AS_VAR_COPY([libbsd_save_CFLAGS], [CFLAGS])
|
||||
AS_VAR_SET([CFLAGS], ["-Werror libbsd_check_flag"])
|
||||
AC_COMPILE_IFELSE([
|
||||
AC_LANG_SOURCE([[]])
|
||||
], [
|
||||
AS_VAR_SET([libbsd_varname_cache], [yes])
|
||||
], [
|
||||
AS_VAR_SET([libbsd_varname_cache], [no])
|
||||
])
|
||||
AS_VAR_COPY([CFLAGS], [libbsd_save_CFLAGS])
|
||||
])
|
||||
AS_VAR_IF([libbsd_varname_cache], [yes], [
|
||||
AS_VAR_APPEND([LIBBSD_COMPILER_FLAGS], [" $1"])
|
||||
])
|
||||
AS_VAR_POPDEF([libbsd_varname_cache])
|
||||
])
|
1
man/LIST_CLASS_ENTRY.3bsd
Normal file
1
man/LIST_CLASS_ENTRY.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
1
man/LIST_CLASS_HEAD.3bsd
Normal file
1
man/LIST_CLASS_HEAD.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
@@ -11,9 +11,11 @@ CLEANFILES = \
|
||||
SED_MD5_SUBST = -e 's/mdX/md5/g' -e 's/mdY/md4/g' -e 's/MDX/MD5/g'
|
||||
|
||||
md5.3bsd: $(srcdir)/mdX.3bsd
|
||||
$(AM_V_GEN) sed $(SED_MD5_SUBST) $< > $@
|
||||
$(AM_V_GEN) $(SED) $(SED_MD5_SUBST) $< > $@
|
||||
|
||||
dist_man_MANS = \
|
||||
LIST_CLASS_ENTRY.3bsd \
|
||||
LIST_CLASS_HEAD.3bsd \
|
||||
LIST_EMPTY.3bsd \
|
||||
LIST_ENTRY.3bsd \
|
||||
LIST_FIRST.3bsd \
|
||||
@@ -54,6 +56,8 @@ dist_man_MANS = \
|
||||
RB_REMOVE.3bsd \
|
||||
RB_RIGHT.3bsd \
|
||||
RB_ROOT.3bsd \
|
||||
SLIST_CLASS_ENTRY.3bsd \
|
||||
SLIST_CLASS_HEAD.3bsd \
|
||||
SLIST_EMPTY.3bsd \
|
||||
SLIST_ENTRY.3bsd \
|
||||
SLIST_FIRST.3bsd \
|
||||
@@ -70,6 +74,7 @@ dist_man_MANS = \
|
||||
SLIST_REMOVE.3bsd \
|
||||
SLIST_REMOVE_AFTER.3bsd \
|
||||
SLIST_REMOVE_HEAD.3bsd \
|
||||
SLIST_REMOVE_PREVPTR.3bsd \
|
||||
SLIST_SWAP.3bsd \
|
||||
SPLAY_EMPTY.3bsd \
|
||||
SPLAY_ENTRY.3bsd \
|
||||
@@ -88,6 +93,8 @@ dist_man_MANS = \
|
||||
SPLAY_REMOVE.3bsd \
|
||||
SPLAY_RIGHT.3bsd \
|
||||
SPLAY_ROOT.3bsd \
|
||||
STAILQ_CLASS_ENTRY.3bsd \
|
||||
STAILQ_CLASS_HEAD.3bsd \
|
||||
STAILQ_CONCAT.3bsd \
|
||||
STAILQ_EMPTY.3bsd \
|
||||
STAILQ_ENTRY.3bsd \
|
||||
@@ -108,6 +115,8 @@ dist_man_MANS = \
|
||||
STAILQ_REMOVE_AFTER.3bsd \
|
||||
STAILQ_REMOVE_HEAD.3bsd \
|
||||
STAILQ_SWAP.3bsd \
|
||||
TAILQ_CLASS_ENTRY.3bsd \
|
||||
TAILQ_CLASS_HEAD.3bsd \
|
||||
TAILQ_CONCAT.3bsd \
|
||||
TAILQ_EMPTY.3bsd \
|
||||
TAILQ_ENTRY.3bsd \
|
||||
@@ -165,6 +174,7 @@ dist_man_MANS = \
|
||||
fgetln.3bsd \
|
||||
fgetwln.3bsd \
|
||||
flopen.3bsd \
|
||||
freezero.3bsd \
|
||||
fmtcheck.3bsd \
|
||||
fparseln.3bsd \
|
||||
fpurge.3bsd \
|
||||
@@ -173,6 +183,8 @@ dist_man_MANS = \
|
||||
getmode.3bsd \
|
||||
getpeereid.3bsd \
|
||||
getprogname.3bsd \
|
||||
gid_from_group.3bsd \
|
||||
group_from_gid.3bsd \
|
||||
heapsort.3bsd \
|
||||
humanize_number.3bsd \
|
||||
le16dec.3bsd \
|
||||
@@ -190,11 +202,13 @@ dist_man_MANS = \
|
||||
pidfile_open.3bsd \
|
||||
pidfile_remove.3bsd \
|
||||
pidfile_write.3bsd \
|
||||
pwcache.3bsd \
|
||||
queue.3bsd \
|
||||
radixsort.3bsd \
|
||||
readpassphrase.3bsd \
|
||||
reallocarray.3bsd \
|
||||
reallocf.3bsd \
|
||||
recallocarray.3bsd \
|
||||
setmode.3bsd \
|
||||
setproctitle.3bsd \
|
||||
setproctitle_init.3bsd \
|
||||
@@ -223,6 +237,7 @@ dist_man_MANS = \
|
||||
timercmp.3bsd \
|
||||
timerisset.3bsd \
|
||||
timersub.3bsd \
|
||||
timespec.3bsd \
|
||||
timespecadd.3bsd \
|
||||
timespecclear.3bsd \
|
||||
timespeccmp.3bsd \
|
||||
@@ -230,7 +245,9 @@ dist_man_MANS = \
|
||||
timespecsub.3bsd \
|
||||
timeval.3bsd \
|
||||
tree.3bsd \
|
||||
uid_from_user.3bsd \
|
||||
unvis.3bsd \
|
||||
user_from_uid.3bsd \
|
||||
vis.3bsd \
|
||||
wcslcat.3bsd \
|
||||
wcslcpy.3bsd \
|
||||
|
1
man/SLIST_CLASS_ENTRY.3bsd
Normal file
1
man/SLIST_CLASS_ENTRY.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
1
man/SLIST_CLASS_HEAD.3bsd
Normal file
1
man/SLIST_CLASS_HEAD.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
1
man/SLIST_REMOVE_PREVPTR.3bsd
Normal file
1
man/SLIST_REMOVE_PREVPTR.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
1
man/STAILQ_CLASS_ENTRY.3bsd
Normal file
1
man/STAILQ_CLASS_ENTRY.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
1
man/STAILQ_CLASS_HEAD.3bsd
Normal file
1
man/STAILQ_CLASS_HEAD.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
1
man/TAILQ_CLASS_ENTRY.3bsd
Normal file
1
man/TAILQ_CLASS_ENTRY.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
1
man/TAILQ_CLASS_HEAD.3bsd
Normal file
1
man/TAILQ_CLASS_HEAD.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/queue.3bsd
|
@@ -44,6 +44,10 @@
|
||||
.Nm bitstr_size ,
|
||||
.Nm bit_test
|
||||
.Nd bit-string manipulation macros
|
||||
.Sh LIBRARY
|
||||
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
|
||||
.Lb libbsd
|
||||
.Sh SYNOPSIS
|
||||
.In bitstring.h
|
||||
(See
|
||||
|
@@ -31,6 +31,10 @@
|
||||
.Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec ,
|
||||
.Nm le16enc , le16dec , le32enc , le32dec , le64enc , le64dec
|
||||
.Nd byte order operations
|
||||
.Sh LIBRARY
|
||||
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
|
||||
.Lb libbsd
|
||||
.Sh SYNOPSIS
|
||||
.In sys/endian.h
|
||||
(See
|
||||
|
1
man/freezero.3bsd
Normal file
1
man/freezero.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/reallocarray.3bsd
|
1
man/gid_from_group.3bsd
Normal file
1
man/gid_from_group.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/pwcache.3bsd
|
1
man/group_from_gid.3bsd
Normal file
1
man/group_from_gid.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/pwcache.3bsd
|
61
man/libbsd.7
61
man/libbsd.7
@@ -1,6 +1,6 @@
|
||||
.\" libbsd man page
|
||||
.\"
|
||||
.\" Copyright © 2017-2018 Guillem Jover <guillem@hadrons.org>
|
||||
.\" Copyright © 2017-2021 Guillem Jover <guillem@hadrons.org>
|
||||
.\"
|
||||
.\" Redistribution and use in source and binary forms, with or without
|
||||
.\" modification, are permitted provided that the following conditions
|
||||
@@ -24,7 +24,7 @@
|
||||
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
.\"
|
||||
.Dd May 21, 2018
|
||||
.Dd Feb 13, 2021
|
||||
.Dt LIBBSD 7
|
||||
.Os
|
||||
.Sh NAME
|
||||
@@ -42,7 +42,7 @@ The library can be used in an overlay mode, which is the preferred way, so
|
||||
that the code is portable and requires no modification to the original BSD
|
||||
code.
|
||||
This can be done easily with the
|
||||
.Xr pkg-config 3
|
||||
.Xr pkg-config 1
|
||||
library named
|
||||
.Pa libbsd-overlay .
|
||||
Or by adding the system-specific include directory with the
|
||||
@@ -64,7 +64,7 @@ this is less portable as it makes using
|
||||
mandatory and it will not work on BSD-based systems, and requires
|
||||
modifying original BSD code.
|
||||
This can be done with the
|
||||
.Xr pkg-config 3
|
||||
.Xr pkg-config 1
|
||||
library named
|
||||
.Pa libbsd .
|
||||
The includes in this case should be namespaced with
|
||||
@@ -73,13 +73,13 @@ such as
|
||||
.In bsd/unistd.h .
|
||||
.Pp
|
||||
The package also provides a
|
||||
.Pa bsd-ctor
|
||||
.Nm libbsd-ctor
|
||||
static library that can be used to inject automatic constructors into a
|
||||
program so that the
|
||||
.Fn setproctitle_init 3
|
||||
function gets invoked automatically at startup time.
|
||||
This can be done with the
|
||||
.Xr pkg-config 3
|
||||
.Xr pkg-config 1
|
||||
library named
|
||||
.Pa libbsd-ctor .
|
||||
.Sh HEADERS
|
||||
@@ -94,11 +94,13 @@ be prefixed with
|
||||
.It In bitstring.h
|
||||
.It In err.h
|
||||
.It In getopt.h
|
||||
.It In grp.h
|
||||
.It In inttypes.h
|
||||
.It In libutil.h
|
||||
.It In md5.h
|
||||
.It In netinet/ip_icmp.h
|
||||
.It In nlist.h
|
||||
.It In pwd.h
|
||||
.It In readpassphrase.h
|
||||
.It In stdio.h
|
||||
.It In stdlib.h
|
||||
@@ -107,6 +109,7 @@ be prefixed with
|
||||
.It In sys/bitstring.h
|
||||
.It In sys/cdefs.h
|
||||
.It In sys/endian.h
|
||||
.It In sys/param.h
|
||||
.It In sys/poll.h
|
||||
.It In sys/queue.h
|
||||
.It In sys/time.h
|
||||
@@ -125,6 +128,46 @@ It only works in non-overlay mode.
|
||||
.Bl -tag -width 4m -compact
|
||||
.It In bsd/bsd.h
|
||||
.El
|
||||
.Sh ALTERNATIVES
|
||||
Some functions have different prototypes depending on the BSD where they
|
||||
originated from, and these various implementations provided are selectable
|
||||
at build-time.
|
||||
.Pp
|
||||
This is the list of functions that provide multiple implementations:
|
||||
.Bl -tag -width 4m
|
||||
.It Fn strnvis 3
|
||||
.It Fn strnunvis 3
|
||||
.Nx
|
||||
added
|
||||
.Fn strnvis 3
|
||||
and
|
||||
.Fn strnunvis 3
|
||||
but unfortunately made it incompatible with the existing one in
|
||||
.Ox
|
||||
and Freedesktop's libbsd (the former having existed for over ten years).
|
||||
Despite this incompatibility being reported during development (see
|
||||
http://gnats.netbsd.org/44977) they still shipped it.
|
||||
Even more unfortunately
|
||||
.Fx
|
||||
and later MacOS picked up this incompatible implementation.
|
||||
.Pp
|
||||
Provide both implementations and default for now to the historical one to
|
||||
avoid breakage, but we will switch to the
|
||||
.Nx
|
||||
one in a later release, which is internally consistent with the other
|
||||
.Xr vis 3
|
||||
functions and is now more widespread.
|
||||
Define
|
||||
.Dv LIBBSD_NETBSD_VIS
|
||||
to switch to the
|
||||
.Nx
|
||||
one now.
|
||||
Define
|
||||
.Dv LIBBSD_OPENBSD_VIS
|
||||
to keep using the
|
||||
.Ox
|
||||
one.
|
||||
.El
|
||||
.Sh DEPRECATED
|
||||
Some functions have been deprecated, they will emit warnings at compile time
|
||||
and possibly while being linked at run-time.
|
||||
@@ -180,9 +223,10 @@ for example.
|
||||
.It Fn MD5File 3
|
||||
.It Fn MD5FileChunk 3
|
||||
.It Fn MD5Data 3
|
||||
The set of MD5 digest functions are now provided by the
|
||||
The set of MD5 digest functions are now proxies for the implementations
|
||||
provided by the
|
||||
.Nm libmd
|
||||
companion library, so it is advised to use that instead.
|
||||
companion library, so it is advised to switch to use that directly instead.
|
||||
.It Fn explicit_bzero 3
|
||||
This function is provided by
|
||||
.Nm glibc
|
||||
@@ -215,6 +259,7 @@ This function is provided by
|
||||
.Xr md5 3bsd ,
|
||||
.Xr nlist 3bsd ,
|
||||
.Xr pidfile 3bsd ,
|
||||
.Xr pwcache 3bsd ,
|
||||
.Xr queue 3bsd ,
|
||||
.Xr radixsort 3bsd ,
|
||||
.Xr readpassphrase 3bsd ,
|
||||
|
143
man/pwcache.3bsd
Normal file
143
man/pwcache.3bsd
Normal file
@@ -0,0 +1,143 @@
|
||||
.\" $OpenBSD: pwcache.3,v 1.15 2018/09/13 16:50:54 jmc 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: September 13 2018 $
|
||||
.Dt USER_FROM_UID 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm user_from_uid ,
|
||||
.Nm uid_from_user ,
|
||||
.Nm group_from_gid ,
|
||||
.Nm gid_from_group
|
||||
.Nd cache password and group entries
|
||||
.Sh LIBRARY
|
||||
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
|
||||
.Lb libbsd
|
||||
.Sh SYNOPSIS
|
||||
.In pwd.h
|
||||
(See
|
||||
.Xr libbsd 7
|
||||
for include usage.)
|
||||
.Ft int
|
||||
.Fn uid_from_user "const char *name" "uid_t *uid"
|
||||
.Ft const char *
|
||||
.Fn user_from_uid "uid_t uid" "int nouser"
|
||||
.In grp.h
|
||||
.Ft int
|
||||
.Fn gid_from_group "const char *name" "gid_t *gid"
|
||||
.Ft const char *
|
||||
.Fn group_from_gid "gid_t gid" "int nogroup"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn user_from_uid
|
||||
function returns the user name associated with the argument
|
||||
.Fa uid .
|
||||
The user name is cached so that multiple calls with the same
|
||||
.Fa uid
|
||||
do not require additional calls to
|
||||
.Xr getpwuid 3 .
|
||||
If there is no user associated with the
|
||||
.Fa uid ,
|
||||
a pointer is returned
|
||||
to a string representation of the
|
||||
.Fa uid ,
|
||||
unless the argument
|
||||
.Fa nouser
|
||||
is non-zero, in which case a null pointer is returned.
|
||||
.Pp
|
||||
The
|
||||
.Fn uid_from_user
|
||||
function returns the user ID associated with the argument
|
||||
.Fa name .
|
||||
The user ID is cached so that multiple calls with the same
|
||||
.Fa name
|
||||
do not require additional calls to
|
||||
.Xr getpwnam 3 .
|
||||
If there is no user ID associated with the
|
||||
.Fa name ,
|
||||
the
|
||||
.Fn uid_from_user
|
||||
function returns -1;
|
||||
otherwise it stores the user ID at the location pointed to by
|
||||
.Fa uid
|
||||
and returns 0.
|
||||
.Pp
|
||||
The
|
||||
.Fn group_from_gid
|
||||
function returns the group name associated with the argument
|
||||
.Fa gid .
|
||||
The group name is cached so that multiple calls with the same
|
||||
.Fa gid
|
||||
do not require additional calls to
|
||||
.Xr getgrgid 3 .
|
||||
If there is no group associated with the
|
||||
.Fa gid ,
|
||||
a pointer is returned
|
||||
to a string representation of the
|
||||
.Fa gid ,
|
||||
unless the argument
|
||||
.Fa nogroup
|
||||
is non-zero, in which case a null pointer is returned.
|
||||
.Pp
|
||||
The
|
||||
.Fn gid_from_group
|
||||
function returns the group ID associated with the argument
|
||||
.Fa name .
|
||||
The group ID is cached so that multiple calls with the same
|
||||
.Fa name
|
||||
do not require additional calls to
|
||||
.Xr getgrnam 3 .
|
||||
If there is no group ID associated with the
|
||||
.Fa name ,
|
||||
the
|
||||
.Fn gid_from_group
|
||||
function returns -1;
|
||||
otherwise it stores the group ID at the location pointed to by
|
||||
.Fa gid
|
||||
and returns 0.
|
||||
.Sh SEE ALSO
|
||||
.Xr getgrgid 3 ,
|
||||
.Xr getpwuid 3
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Fn user_from_uid
|
||||
and
|
||||
.Fn group_from_gid
|
||||
functions first appeared in
|
||||
.Bx 4.4 .
|
||||
.Pp
|
||||
The
|
||||
.Fn uid_from_user
|
||||
and
|
||||
.Fn gid_from_group
|
||||
functions were ported from
|
||||
.Nx
|
||||
and first appeared in
|
||||
.Ox 6.4 .
|
157
man/queue.3bsd
157
man/queue.3bsd
@@ -28,35 +28,40 @@
|
||||
.\" @(#)queue.3 8.2 (Berkeley) 1/24/94
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd June 17, 2013
|
||||
.Dd September 8, 2016
|
||||
.Dt QUEUE 3bsd
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm SLIST_CLASS_ENTRY ,
|
||||
.Nm SLIST_CLASS_HEAD ,
|
||||
.Nm SLIST_CONCAT ,
|
||||
.Nm SLIST_EMPTY ,
|
||||
.Nm SLIST_ENTRY ,
|
||||
.Nm SLIST_FIRST ,
|
||||
.Nm SLIST_FOREACH ,
|
||||
.Nm SLIST_FOREACH_FROM ,
|
||||
.Nm SLIST_FOREACH_SAFE ,
|
||||
.Nm SLIST_FOREACH_FROM_SAFE ,
|
||||
.Nm SLIST_FOREACH_SAFE ,
|
||||
.Nm SLIST_HEAD ,
|
||||
.Nm SLIST_HEAD_INITIALIZER ,
|
||||
.Nm SLIST_INIT ,
|
||||
.Nm SLIST_INSERT_AFTER ,
|
||||
.Nm SLIST_INSERT_HEAD ,
|
||||
.Nm SLIST_NEXT ,
|
||||
.Nm SLIST_REMOVE ,
|
||||
.Nm SLIST_REMOVE_AFTER ,
|
||||
.Nm SLIST_REMOVE_HEAD ,
|
||||
.Nm SLIST_REMOVE ,
|
||||
.Nm SLIST_SWAP ,
|
||||
.Nm STAILQ_CLASS_ENTRY ,
|
||||
.Nm STAILQ_CLASS_HEAD ,
|
||||
.Nm STAILQ_CONCAT ,
|
||||
.Nm STAILQ_EMPTY ,
|
||||
.Nm STAILQ_ENTRY ,
|
||||
.Nm STAILQ_FIRST ,
|
||||
.Nm STAILQ_FOREACH ,
|
||||
.Nm STAILQ_FOREACH_FROM ,
|
||||
.Nm STAILQ_FOREACH_SAFE ,
|
||||
.Nm STAILQ_FOREACH_FROM_SAFE ,
|
||||
.Nm STAILQ_FOREACH_SAFE ,
|
||||
.Nm STAILQ_HEAD ,
|
||||
.Nm STAILQ_HEAD_INITIALIZER ,
|
||||
.Nm STAILQ_INIT ,
|
||||
@@ -65,17 +70,20 @@
|
||||
.Nm STAILQ_INSERT_TAIL ,
|
||||
.Nm STAILQ_LAST ,
|
||||
.Nm STAILQ_NEXT ,
|
||||
.Nm STAILQ_REMOVE ,
|
||||
.Nm STAILQ_REMOVE_AFTER ,
|
||||
.Nm STAILQ_REMOVE_HEAD ,
|
||||
.Nm STAILQ_REMOVE ,
|
||||
.Nm STAILQ_SWAP ,
|
||||
.Nm LIST_CLASS_ENTRY ,
|
||||
.Nm LIST_CLASS_HEAD ,
|
||||
.Nm LIST_CONCAT ,
|
||||
.Nm LIST_EMPTY ,
|
||||
.Nm LIST_ENTRY ,
|
||||
.Nm LIST_FIRST ,
|
||||
.Nm LIST_FOREACH ,
|
||||
.Nm LIST_FOREACH_FROM ,
|
||||
.Nm LIST_FOREACH_SAFE ,
|
||||
.Nm LIST_FOREACH_FROM_SAFE ,
|
||||
.Nm LIST_FOREACH_SAFE ,
|
||||
.Nm LIST_HEAD ,
|
||||
.Nm LIST_HEAD_INITIALIZER ,
|
||||
.Nm LIST_INIT ,
|
||||
@@ -86,18 +94,20 @@
|
||||
.Nm LIST_PREV ,
|
||||
.Nm LIST_REMOVE ,
|
||||
.Nm LIST_SWAP ,
|
||||
.Nm TAILQ_CLASS_ENTRY ,
|
||||
.Nm TAILQ_CLASS_HEAD ,
|
||||
.Nm TAILQ_CONCAT ,
|
||||
.Nm TAILQ_EMPTY ,
|
||||
.Nm TAILQ_ENTRY ,
|
||||
.Nm TAILQ_FIRST ,
|
||||
.Nm TAILQ_FOREACH ,
|
||||
.Nm TAILQ_FOREACH_FROM ,
|
||||
.Nm TAILQ_FOREACH_SAFE ,
|
||||
.Nm TAILQ_FOREACH_FROM_SAFE ,
|
||||
.Nm TAILQ_FOREACH_REVERSE ,
|
||||
.Nm TAILQ_FOREACH_REVERSE_FROM ,
|
||||
.Nm TAILQ_FOREACH_REVERSE_SAFE ,
|
||||
.Nm TAILQ_FOREACH_REVERSE_FROM_SAFE ,
|
||||
.Nm TAILQ_FOREACH_REVERSE_SAFE ,
|
||||
.Nm TAILQ_FOREACH_SAFE ,
|
||||
.Nm TAILQ_HEAD ,
|
||||
.Nm TAILQ_HEAD_INITIALIZER ,
|
||||
.Nm TAILQ_INIT ,
|
||||
@@ -112,58 +122,70 @@
|
||||
.Nm TAILQ_SWAP
|
||||
.Nd implementations of singly-linked lists, singly-linked tail queues,
|
||||
lists and tail queues
|
||||
.Sh LIBRARY
|
||||
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
|
||||
.Lb libbsd
|
||||
.Sh SYNOPSIS
|
||||
.In sys/queue.h
|
||||
(See
|
||||
.Xr libbsd 7
|
||||
for include usage.)
|
||||
.\"
|
||||
.Fn SLIST_CLASS_ENTRY "CLASSTYPE"
|
||||
.Fn SLIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
|
||||
.Fn SLIST_CONCAT "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_EMPTY "SLIST_HEAD *head"
|
||||
.Fn SLIST_ENTRY "TYPE"
|
||||
.Fn SLIST_FIRST "SLIST_HEAD *head"
|
||||
.Fn SLIST_FOREACH "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_FOREACH_FROM "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn SLIST_FOREACH_FROM_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn SLIST_FOREACH_SAFE "TYPE *var" "SLIST_HEAD *head" "SLIST_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn SLIST_HEAD "HEADNAME" "TYPE"
|
||||
.Fn SLIST_HEAD_INITIALIZER "SLIST_HEAD head"
|
||||
.Fn SLIST_INIT "SLIST_HEAD *head"
|
||||
.Fn SLIST_INSERT_AFTER "TYPE *listelm" "TYPE *elm" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_INSERT_HEAD "SLIST_HEAD *head" "TYPE *elm" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_NEXT "TYPE *elm" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_REMOVE_AFTER "TYPE *elm" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_REMOVE_HEAD "SLIST_HEAD *head" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_REMOVE "SLIST_HEAD *head" "TYPE *elm" "TYPE" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "SLIST_ENTRY NAME"
|
||||
.Fn SLIST_SWAP "SLIST_HEAD *head1" "SLIST_HEAD *head2" "TYPE"
|
||||
.\"
|
||||
.Fn STAILQ_CLASS_ENTRY "CLASSTYPE"
|
||||
.Fn STAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
|
||||
.Fn STAILQ_CONCAT "STAILQ_HEAD *head1" "STAILQ_HEAD *head2"
|
||||
.Fn STAILQ_EMPTY "STAILQ_HEAD *head"
|
||||
.Fn STAILQ_ENTRY "TYPE"
|
||||
.Fn STAILQ_FIRST "STAILQ_HEAD *head"
|
||||
.Fn STAILQ_FOREACH "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_FOREACH_FROM "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn STAILQ_FOREACH_FROM_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn STAILQ_FOREACH_SAFE "TYPE *var" "STAILQ_HEAD *head" "STAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn STAILQ_HEAD "HEADNAME" "TYPE"
|
||||
.Fn STAILQ_HEAD_INITIALIZER "STAILQ_HEAD head"
|
||||
.Fn STAILQ_INIT "STAILQ_HEAD *head"
|
||||
.Fn STAILQ_INSERT_AFTER "STAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_INSERT_HEAD "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_INSERT_TAIL "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_LAST "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_NEXT "TYPE *elm" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_REMOVE_AFTER "STAILQ_HEAD *head" "TYPE *elm" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_REMOVE_HEAD "STAILQ_HEAD *head" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_REMOVE "STAILQ_HEAD *head" "TYPE *elm" "TYPE" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "STAILQ_ENTRY NAME"
|
||||
.Fn STAILQ_SWAP "STAILQ_HEAD *head1" "STAILQ_HEAD *head2" "TYPE"
|
||||
.\"
|
||||
.Fn LIST_CLASS_ENTRY "CLASSTYPE"
|
||||
.Fn LIST_CLASS_HEAD "HEADNAME" "CLASSTYPE"
|
||||
.Fn LIST_CONCAT "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
|
||||
.Fn LIST_EMPTY "LIST_HEAD *head"
|
||||
.Fn LIST_ENTRY "TYPE"
|
||||
.Fn LIST_FIRST "LIST_HEAD *head"
|
||||
.Fn LIST_FOREACH "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
|
||||
.Fn LIST_FOREACH_FROM "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME"
|
||||
.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn LIST_FOREACH_FROM_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn LIST_FOREACH_SAFE "TYPE *var" "LIST_HEAD *head" "LIST_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn LIST_HEAD "HEADNAME" "TYPE"
|
||||
.Fn LIST_HEAD_INITIALIZER "LIST_HEAD head"
|
||||
.Fn LIST_INIT "LIST_HEAD *head"
|
||||
@@ -175,18 +197,20 @@ for include usage.)
|
||||
.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
|
||||
.Fn LIST_SWAP "LIST_HEAD *head1" "LIST_HEAD *head2" "TYPE" "LIST_ENTRY NAME"
|
||||
.\"
|
||||
.Fn TAILQ_CLASS_ENTRY "CLASSTYPE"
|
||||
.Fn TAILQ_CLASS_HEAD "HEADNAME" "CLASSTYPE"
|
||||
.Fn TAILQ_CONCAT "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TAILQ_ENTRY NAME"
|
||||
.Fn TAILQ_EMPTY "TAILQ_HEAD *head"
|
||||
.Fn TAILQ_ENTRY "TYPE"
|
||||
.Fn TAILQ_FIRST "TAILQ_HEAD *head"
|
||||
.Fn TAILQ_FOREACH "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
|
||||
.Fn TAILQ_FOREACH_FROM "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME"
|
||||
.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn TAILQ_FOREACH_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn TAILQ_FOREACH_REVERSE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
|
||||
.Fn TAILQ_FOREACH_REVERSE_FROM "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME"
|
||||
.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn TAILQ_FOREACH_REVERSE_FROM_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn TAILQ_FOREACH_REVERSE_SAFE "TYPE *var" "TAILQ_HEAD *head" "HEADNAME" "TAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn TAILQ_FOREACH_SAFE "TYPE *var" "TAILQ_HEAD *head" "TAILQ_ENTRY NAME" "TYPE *temp_var"
|
||||
.Fn TAILQ_HEAD "HEADNAME" "TYPE"
|
||||
.Fn TAILQ_HEAD_INITIALIZER "TAILQ_HEAD head"
|
||||
.Fn TAILQ_INIT "TAILQ_HEAD *head"
|
||||
@@ -201,8 +225,18 @@ for include usage.)
|
||||
.Fn TAILQ_SWAP "TAILQ_HEAD *head1" "TAILQ_HEAD *head2" "TYPE" "TAILQ_ENTRY NAME"
|
||||
.\"
|
||||
.Sh DESCRIPTION
|
||||
These macros define and operate on four types of data structures:
|
||||
singly-linked lists, singly-linked tail queues, lists, and tail queues.
|
||||
These macros define and operate on four types of data structures which
|
||||
can be used in both C and C++ source code:
|
||||
.Bl -enum -compact -offset indent
|
||||
.It
|
||||
Lists
|
||||
.It
|
||||
Singly-linked lists
|
||||
.It
|
||||
Singly-linked tail queues
|
||||
.It
|
||||
Tail queues
|
||||
.El
|
||||
All four structures support the following functionality:
|
||||
.Bl -enum -compact -offset indent
|
||||
.It
|
||||
@@ -226,6 +260,8 @@ Singly-linked lists add the following functionality:
|
||||
.Bl -enum -compact -offset indent
|
||||
.It
|
||||
O(n) removal of any entry in the list.
|
||||
.It
|
||||
O(n) concatenation of two lists.
|
||||
.El
|
||||
.Pp
|
||||
Singly-linked tail queues add the following functionality:
|
||||
@@ -273,6 +309,8 @@ Linked lists are the simplest of the doubly linked data structures.
|
||||
They add the following functionality over the above:
|
||||
.Bl -enum -compact -offset indent
|
||||
.It
|
||||
O(n) concatenation of two lists.
|
||||
.It
|
||||
They may be traversed backwards.
|
||||
.El
|
||||
However:
|
||||
@@ -304,24 +342,39 @@ than singly-linked lists.
|
||||
.Pp
|
||||
In the macro definitions,
|
||||
.Fa TYPE
|
||||
is the name of a user defined structure,
|
||||
that must contain a field of type
|
||||
is the name of a user defined structure.
|
||||
The structure must contain a field called
|
||||
.Fa NAME
|
||||
which is of type
|
||||
.Li SLIST_ENTRY ,
|
||||
.Li STAILQ_ENTRY ,
|
||||
.Li LIST_ENTRY ,
|
||||
or
|
||||
.Li TAILQ_ENTRY ,
|
||||
named
|
||||
.Fa NAME .
|
||||
.Li TAILQ_ENTRY .
|
||||
In the macro definitions,
|
||||
.Fa CLASSTYPE
|
||||
is the name of a user defined class.
|
||||
The class must contain a field called
|
||||
.Fa NAME
|
||||
which is of type
|
||||
.Li SLIST_CLASS_ENTRY ,
|
||||
.Li STAILQ_CLASS_ENTRY ,
|
||||
.Li LIST_CLASS_ENTRY ,
|
||||
or
|
||||
.Li TAILQ_CLASS_ENTRY .
|
||||
The argument
|
||||
.Fa HEADNAME
|
||||
is the name of a user defined structure that must be declared
|
||||
using the macros
|
||||
.Li SLIST_HEAD ,
|
||||
.Li SLIST_CLASS_HEAD ,
|
||||
.Li STAILQ_HEAD ,
|
||||
.Li STAILQ_CLASS_HEAD ,
|
||||
.Li LIST_HEAD ,
|
||||
.Li LIST_CLASS_HEAD ,
|
||||
.Li TAILQ_HEAD ,
|
||||
or
|
||||
.Li TAILQ_HEAD .
|
||||
.Li TAILQ_CLASS_HEAD .
|
||||
See the examples below for further explanation of how these
|
||||
macros are used.
|
||||
.Sh SINGLY-LINKED LISTS
|
||||
@@ -363,6 +416,19 @@ evaluates to an initializer for the list
|
||||
.Fa head .
|
||||
.Pp
|
||||
The macro
|
||||
.Nm SLIST_CONCAT
|
||||
concatenates the list headed by
|
||||
.Fa head2
|
||||
onto the end of the one headed by
|
||||
.Fa head1
|
||||
removing all entries from the former.
|
||||
Use of this macro should be avoided as it traverses the entirety of the
|
||||
.Fa head1
|
||||
list.
|
||||
A singly-linked tail queue should be used if this macro is needed in
|
||||
high-usage code paths or to operate on long lists.
|
||||
.Pp
|
||||
The macro
|
||||
.Nm SLIST_EMPTY
|
||||
evaluates to true if there are no elements in the list.
|
||||
.Pp
|
||||
@@ -470,6 +536,9 @@ The macro
|
||||
removes the element
|
||||
.Fa elm
|
||||
from the list.
|
||||
Use of this macro should be avoided as it traverses the entire list.
|
||||
A doubly-linked list should be used if this macro is needed in
|
||||
high-usage code paths or to operate on long lists.
|
||||
.Pp
|
||||
The macro
|
||||
.Nm SLIST_SWAP
|
||||
@@ -686,6 +755,9 @@ The macro
|
||||
removes the element
|
||||
.Fa elm
|
||||
from the tail queue.
|
||||
Use of this macro should be avoided as it traverses the entire list.
|
||||
A doubly-linked tail queue should be used if this macro is needed in
|
||||
high-usage code paths or to operate on long tail queues.
|
||||
.Pp
|
||||
The macro
|
||||
.Nm STAILQ_SWAP
|
||||
@@ -785,6 +857,19 @@ evaluates to an initializer for the list
|
||||
.Fa head .
|
||||
.Pp
|
||||
The macro
|
||||
.Nm LIST_CONCAT
|
||||
concatenates the list headed by
|
||||
.Fa head2
|
||||
onto the end of the one headed by
|
||||
.Fa head1
|
||||
removing all entries from the former.
|
||||
Use of this macro should be avoided as it traverses the entirety of the
|
||||
.Fa head1
|
||||
list.
|
||||
A tail queue should be used if this macro is needed in
|
||||
high-usage code paths or to operate on long lists.
|
||||
.Pp
|
||||
The macro
|
||||
.Nm LIST_EMPTY
|
||||
evaluates to true if there are no elements in the list.
|
||||
.Pp
|
||||
@@ -1206,6 +1291,26 @@ while (n1 != NULL) {
|
||||
}
|
||||
TAILQ_INIT(&head);
|
||||
.Ed
|
||||
.Sh DIAGNOSTICS
|
||||
When debugging
|
||||
.Nm queue(3) ,
|
||||
it can be useful to trace queue changes.
|
||||
To enable tracing, define the macro
|
||||
.Va QUEUE_MACRO_DEBUG_TRACE
|
||||
at compile time.
|
||||
.Pp
|
||||
It can also be useful to trash pointers that have been unlinked from a queue,
|
||||
to detect use after removal.
|
||||
To enable pointer trashing, define the macro
|
||||
.Va QUEUE_MACRO_DEBUG_TRASH
|
||||
at compile time.
|
||||
The macro
|
||||
.Fn QMD_IS_TRASHED "void *ptr"
|
||||
returns true if
|
||||
.Fa ptr
|
||||
has been trashed by the
|
||||
.Va QUEUE_MACRO_DEBUG_TRASH
|
||||
option.
|
||||
.Sh SEE ALSO
|
||||
.Xr tree 3bsd
|
||||
.Sh HISTORY
|
||||
|
@@ -30,13 +30,15 @@
|
||||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
.\" SUCH DAMAGE.
|
||||
.\"
|
||||
.\" $OpenBSD: malloc.3,v 1.78 2014/05/01 18:41:59 jmc Exp $
|
||||
.\" $OpenBSD: malloc.3,v 1.126 2019/09/14 13:16:50 otto Exp $
|
||||
.\"
|
||||
.Dd $Mdocdate: May 1 2014 $
|
||||
.Dd $Mdocdate: September 14 2019 $
|
||||
.Dt REALLOCARRAY 3bsd
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm reallocarray
|
||||
.Nm reallocarray ,
|
||||
.Nm recallocarray ,
|
||||
.Nm freezero
|
||||
.Nd memory allocation and deallocation
|
||||
.Sh LIBRARY
|
||||
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||
@@ -49,59 +51,244 @@
|
||||
for include usage.)
|
||||
.Ft void *
|
||||
.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
|
||||
.Ft void *
|
||||
.Fn recallocarray "void *ptr" "size_t oldnmemb" "size_t nmemb" "size_t size"
|
||||
.Ft void
|
||||
.Fn freezero "void *ptr" "size_t size"
|
||||
.Sh DESCRIPTION
|
||||
.Pp
|
||||
When using
|
||||
.Fn malloc
|
||||
be careful to avoid the following idiom:
|
||||
.Bd -literal -offset indent
|
||||
if ((p = malloc(num * size)) == NULL)
|
||||
err(1, "malloc");
|
||||
.Ed
|
||||
.Pp
|
||||
The multiplication may lead to an integer overflow, which can
|
||||
be avoided using the extension
|
||||
.Fn reallocarray ,
|
||||
as follows:
|
||||
.Bd -literal -offset indent
|
||||
if ((p = reallocarray(NULL, num, size)) == NULL)
|
||||
err(1, "malloc");
|
||||
.Ed
|
||||
.Pp
|
||||
Alternatively
|
||||
.Fn calloc
|
||||
is a more portable solution which comes with the cost of clearing memory.
|
||||
.Pp
|
||||
If
|
||||
.Fn malloc
|
||||
must be used, be sure to test for overflow:
|
||||
.Bd -literal -offset indent
|
||||
if (size && num > SIZE_MAX / size) {
|
||||
errno = ENOMEM;
|
||||
err(1, "overflow");
|
||||
}
|
||||
.Ed
|
||||
.Pp
|
||||
The use of
|
||||
Designed for safe allocation of arrays,
|
||||
the
|
||||
.Fn reallocarray
|
||||
or
|
||||
.Fn calloc
|
||||
is strongly encouraged when allocating multiple sized objects
|
||||
in order to avoid possible integer overflows.
|
||||
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
|
||||
Used for the allocation of memory holding sensitive data,
|
||||
the
|
||||
.Fn recallocarray
|
||||
function guarantees that memory becoming unallocated is explicitly
|
||||
.Em discarded ,
|
||||
meaning cached free objects are cleared with
|
||||
.Xr explicit_bzero 3 .
|
||||
.Pp
|
||||
The
|
||||
.Fn recallocarray
|
||||
function is similar to
|
||||
.Fn reallocarray
|
||||
except it ensures newly allocated memory is cleared similar to
|
||||
.Fn calloc .
|
||||
If
|
||||
.Fa ptr
|
||||
is
|
||||
.Dv NULL ,
|
||||
.Fa oldnmemb
|
||||
is ignored and the call is equivalent to
|
||||
.Fn calloc .
|
||||
If
|
||||
.Fa ptr
|
||||
is not
|
||||
.Dv NULL ,
|
||||
.Fa oldnmemb
|
||||
must be a value such that
|
||||
.Fa oldnmemb
|
||||
*
|
||||
.Fa size
|
||||
is the size of the earlier allocation that returned
|
||||
.Fa ptr ,
|
||||
otherwise the behavior is undefined.
|
||||
The
|
||||
.Fn freezero
|
||||
function is similar to the
|
||||
.Fn free
|
||||
function except it ensures memory is explicitly discarded.
|
||||
If
|
||||
.Fa ptr
|
||||
is
|
||||
.Dv NULL ,
|
||||
no action occurs.
|
||||
If
|
||||
.Fa ptr
|
||||
is not
|
||||
.Dv NULL ,
|
||||
the
|
||||
.Fa size
|
||||
argument must be equal to or smaller than the size of the earlier allocation
|
||||
that returned
|
||||
.Fa ptr .
|
||||
.Fn freezero
|
||||
guarantees the memory range starting at
|
||||
.Fa ptr
|
||||
with length
|
||||
.Fa size
|
||||
is discarded while deallocating the whole object originally allocated.
|
||||
.Sh RETURN VALUES
|
||||
The
|
||||
.Fn reallocarray
|
||||
function returns a pointer to the allocated space if successful; otherwise,
|
||||
and
|
||||
.Fn recallocarray
|
||||
functions return a pointer to the allocated space if successful; otherwise,
|
||||
a null pointer is returned and
|
||||
.Va errno
|
||||
is set to
|
||||
.Er ENOMEM .
|
||||
.Pp
|
||||
If multiplying
|
||||
.Fa nmemb
|
||||
and
|
||||
.Fa size
|
||||
results in integer overflow,
|
||||
.Fn reallocarray
|
||||
and
|
||||
.Fn recallocarray
|
||||
return
|
||||
.Dv NULL
|
||||
and set
|
||||
.Va errno
|
||||
to
|
||||
.Er ENOMEM .
|
||||
.Pp
|
||||
If
|
||||
.Fa ptr
|
||||
is not
|
||||
.Dv NULL
|
||||
and multiplying
|
||||
.Fa oldnmemb
|
||||
and
|
||||
.Fa size
|
||||
results in integer overflow
|
||||
.Fn recallocarray
|
||||
returns
|
||||
.Dv NULL
|
||||
and sets
|
||||
.Va errno
|
||||
to
|
||||
.Er EINVAL .
|
||||
.Sh IDIOMS
|
||||
Consider
|
||||
.Fn calloc
|
||||
or the extensions
|
||||
.Fn reallocarray
|
||||
and
|
||||
.Fn recallocarray
|
||||
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
|
||||
.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
|
||||
.Pp
|
||||
The
|
||||
.Fn recallocarray
|
||||
function should be used for resizing objects containing sensitive data like
|
||||
keys.
|
||||
To avoid leaking information,
|
||||
it guarantees memory is cleared before placing it on the internal free list.
|
||||
Deallocation of such an object should be done by calling
|
||||
.Fn freezero .
|
||||
.Sh SEE ALSO
|
||||
.Xr malloc 3 ,
|
||||
.Xr calloc 3 ,
|
||||
.Xr alloca 3
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Fn reallocarray
|
||||
appeared in
|
||||
function appeared in
|
||||
.Ox 5.6 ,
|
||||
glibc 2.26.
|
||||
and glibc 2.26.
|
||||
The
|
||||
.Fn recallocarray
|
||||
function appeared in
|
||||
.Ox 6.1 .
|
||||
The
|
||||
.Fn freezero
|
||||
function appeared in
|
||||
.Ox 6.2 .
|
||||
|
1
man/recallocarray.3bsd
Normal file
1
man/recallocarray.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/reallocarray.3bsd
|
1
man/timespec.3bsd
Normal file
1
man/timespec.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/timeval.3bsd
|
395
man/tree.3bsd
395
man/tree.3bsd
@@ -1,36 +1,29 @@
|
||||
.\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $
|
||||
.\"
|
||||
.\" Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
||||
.\" 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. All advertising materials mentioning features or use of this software
|
||||
.\" must display the following acknowledgement:
|
||||
.\" This product includes software developed by Niels Provos.
|
||||
.\" 4. The name of the author may not be used to endorse or promote products
|
||||
.\" derived from this software without specific prior written permission.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
.\"
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd December 27, 2007
|
||||
.\" $OpenBSD: tree.3,v 1.30 2019/05/10 13:13:14 florian Exp $
|
||||
.\"/*
|
||||
.\" * Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
||||
.\" * 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.
|
||||
.\" *
|
||||
.\" * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
.\" * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
.\" * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
.\" * IN NO EVENT SHALL THE AUTHOR 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 May 10, 2019
|
||||
.Dt TREE 3bsd
|
||||
.Os
|
||||
.Sh NAME
|
||||
@@ -70,98 +63,108 @@
|
||||
.Nm RB_RIGHT ,
|
||||
.Nm RB_PARENT ,
|
||||
.Nm RB_FOREACH ,
|
||||
.Nm RB_FOREACH_SAFE ,
|
||||
.Nm RB_FOREACH_REVERSE ,
|
||||
.Nm RB_FOREACH_REVERSE_SAFE ,
|
||||
.Nm RB_INIT ,
|
||||
.Nm RB_INSERT ,
|
||||
.Nm RB_REMOVE
|
||||
.Nd implementations of splay and red-black trees
|
||||
.Sh LIBRARY
|
||||
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||
.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
|
||||
.Lb libbsd
|
||||
.Sh SYNOPSIS
|
||||
.In sys/tree.h
|
||||
(See
|
||||
.Xr libbsd 7
|
||||
for include usage.)
|
||||
.Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP
|
||||
.Fn SPLAY_GENERATE NAME TYPE FIELD CMP
|
||||
.Fn SPLAY_ENTRY TYPE
|
||||
.Fn SPLAY_HEAD HEADNAME TYPE
|
||||
.Pp
|
||||
.Fn SPLAY_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
|
||||
.Fn SPLAY_GENERATE "NAME" "TYPE" "FIELD" "CMP"
|
||||
.Fn SPLAY_ENTRY "TYPE"
|
||||
.Fn SPLAY_HEAD "HEADNAME" "TYPE"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
|
||||
.Fn SPLAY_ROOT "SPLAY_HEAD *head"
|
||||
.Ft bool
|
||||
.Ft "int"
|
||||
.Fn SPLAY_EMPTY "SPLAY_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Fn SPLAY_NEXT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_MIN NAME "SPLAY_HEAD *head"
|
||||
.Fn SPLAY_MIN "NAME" "SPLAY_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_MAX NAME "SPLAY_HEAD *head"
|
||||
.Fn SPLAY_MAX "NAME" "SPLAY_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Fn SPLAY_FIND "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
|
||||
.Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head"
|
||||
.Fn SPLAY_FOREACH "VARNAME" "NAME" "SPLAY_HEAD *head"
|
||||
.Ft void
|
||||
.Fn SPLAY_INIT "SPLAY_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Fn SPLAY_INSERT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Fn RB_PROTOTYPE NAME TYPE FIELD CMP
|
||||
.Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP
|
||||
.Fn RB_GENERATE NAME TYPE FIELD CMP
|
||||
.Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP
|
||||
.Fn RB_ENTRY TYPE
|
||||
.Fn RB_HEAD HEADNAME TYPE
|
||||
.Fn SPLAY_REMOVE "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
|
||||
.Pp
|
||||
.Fn RB_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
|
||||
.Fn RB_PROTOTYPE_STATIC "NAME" "TYPE" "FIELD" "CMP"
|
||||
.Fn RB_GENERATE "NAME" "TYPE" "FIELD" "CMP"
|
||||
.Fn RB_GENERATE_STATIC "NAME" "TYPE" "FIELD" "CMP"
|
||||
.Fn RB_ENTRY "TYPE"
|
||||
.Fn RB_HEAD "HEADNAME" "TYPE"
|
||||
.Fn RB_INITIALIZER "RB_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_ROOT "RB_HEAD *head"
|
||||
.Ft "bool"
|
||||
.Ft "int"
|
||||
.Fn RB_EMPTY "RB_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Fn RB_NEXT "NAME" "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Fn RB_PREV "NAME" "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_MIN NAME "RB_HEAD *head"
|
||||
.Fn RB_MIN "NAME" "RB_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_MAX NAME "RB_HEAD *head"
|
||||
.Fn RB_MAX "NAME" "RB_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Fn RB_FIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Fn RB_NFIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
|
||||
.Fn RB_FOREACH VARNAME NAME "RB_HEAD *head"
|
||||
.Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head"
|
||||
.Fn RB_FOREACH "VARNAME" "NAME" "RB_HEAD *head"
|
||||
.Fn RB_FOREACH_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
|
||||
.Fn RB_FOREACH_REVERSE "VARNAME" "NAME" "RB_HEAD *head"
|
||||
.Fn RB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
|
||||
.Ft void
|
||||
.Fn RB_INIT "RB_HEAD *head"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Fn RB_INSERT "NAME" "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Ft "struct TYPE *"
|
||||
.Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Fn RB_REMOVE "NAME" "RB_HEAD *head" "struct TYPE *elm"
|
||||
.Sh DESCRIPTION
|
||||
These macros define data structures for different types of trees:
|
||||
splay trees and red-black trees.
|
||||
.Pp
|
||||
In the macro definitions,
|
||||
.Fa TYPE
|
||||
is the name tag of a user defined structure that must contain a field of type
|
||||
.Vt SPLAY_ENTRY ,
|
||||
is the name tag of a user defined structure that must contain a field named
|
||||
.Fa FIELD ,
|
||||
of type
|
||||
.Li SPLAY_ENTRY
|
||||
or
|
||||
.Vt RB_ENTRY ,
|
||||
named
|
||||
.Fa ENTRYNAME .
|
||||
.Li RB_ENTRY .
|
||||
The argument
|
||||
.Fa HEADNAME
|
||||
is the name tag of a user defined structure that must be declared
|
||||
using the macros
|
||||
.Fn SPLAY_HEAD ,
|
||||
.Fn SPLAY_HEAD
|
||||
or
|
||||
.Fn RB_HEAD .
|
||||
The argument
|
||||
@@ -169,46 +172,38 @@ The argument
|
||||
has to be a unique name prefix for every tree that is defined.
|
||||
.Pp
|
||||
The function prototypes are declared with
|
||||
.Fn SPLAY_PROTOTYPE ,
|
||||
.Fn RB_PROTOTYPE ,
|
||||
.Li SPLAY_PROTOTYPE ,
|
||||
.Li RB_PROTOTYPE ,
|
||||
or
|
||||
.Fn RB_PROTOTYPE_STATIC .
|
||||
.Li RB_PROTOTYPE_STATIC .
|
||||
The function bodies are generated with
|
||||
.Fn SPLAY_GENERATE ,
|
||||
.Fn RB_GENERATE ,
|
||||
.Li SPLAY_GENERATE ,
|
||||
.Li RB_GENERATE ,
|
||||
or
|
||||
.Fn RB_GENERATE_STATIC .
|
||||
.Li RB_GENERATE_STATIC .
|
||||
See the examples below for further explanation of how these macros are used.
|
||||
.Sh SPLAY TREES
|
||||
A splay tree is a self-organizing data structure.
|
||||
Every operation on the tree causes a splay to happen.
|
||||
The splay moves the requested
|
||||
node to the root of the tree and partly rebalances it.
|
||||
The splay moves the requested node to the root of the tree and partly
|
||||
rebalances it.
|
||||
.Pp
|
||||
This has the benefit that request locality causes faster lookups as
|
||||
the requested nodes move to the top of the tree.
|
||||
On the other hand, every lookup causes memory writes.
|
||||
.Pp
|
||||
The Balance Theorem bounds the total access time for
|
||||
.Ar m
|
||||
operations and
|
||||
.Ar n
|
||||
inserts on an initially empty tree as
|
||||
.Fn O "\*[lp]m + n\*[rp]lg n" .
|
||||
The
|
||||
amortized cost for a sequence of
|
||||
.Ar m
|
||||
accesses to a splay tree is
|
||||
.Fn O "lg n" .
|
||||
The Balance Theorem bounds the total access time for m operations
|
||||
and n inserts on an initially empty tree as O((m + n)lg n).
|
||||
The amortized cost for a sequence of m accesses to a splay tree is O(lg n).
|
||||
.Pp
|
||||
A splay tree is headed by a structure defined by the
|
||||
.Fn SPLAY_HEAD
|
||||
macro.
|
||||
A
|
||||
.Fa SPLAY_HEAD
|
||||
structure is declared as follows:
|
||||
.Bd -ragged -offset indent
|
||||
.Fn SPLAY_HEAD HEADNAME TYPE
|
||||
.Va head ;
|
||||
.Bd -literal -offset indent
|
||||
SPLAY_HEAD(HEADNAME, TYPE) head;
|
||||
.Ed
|
||||
.Pp
|
||||
where
|
||||
@@ -247,16 +242,15 @@ macro, but should be used only once.
|
||||
Finally,
|
||||
the
|
||||
.Fa CMP
|
||||
argument is the name of a function used to compare tree nodes
|
||||
argument is the name of a function used to compare trees' nodes
|
||||
with each other.
|
||||
The function takes two arguments of type
|
||||
.Vt "struct TYPE *" .
|
||||
.Fa "struct TYPE *" .
|
||||
If the first argument is smaller than the second, the function returns a
|
||||
value smaller than zero.
|
||||
If they are equal, the function returns zero.
|
||||
Otherwise, it should return a value greater than zero.
|
||||
The compare
|
||||
function defines the order of the tree elements.
|
||||
The compare function defines the order of the tree elements.
|
||||
.Pp
|
||||
The
|
||||
.Fn SPLAY_INIT
|
||||
@@ -266,11 +260,8 @@ macro initializes the tree referenced by
|
||||
The splay tree can also be initialized statically by using the
|
||||
.Fn SPLAY_INITIALIZER
|
||||
macro like this:
|
||||
.Bd -ragged -offset indent
|
||||
.Fn SPLAY_HEAD HEADNAME TYPE
|
||||
.Va head
|
||||
=
|
||||
.Fn SPLAY_INITIALIZER &head ;
|
||||
.Bd -literal -offset indent
|
||||
SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);
|
||||
.Ed
|
||||
.Pp
|
||||
The
|
||||
@@ -278,6 +269,11 @@ The
|
||||
macro inserts the new element
|
||||
.Fa elm
|
||||
into the tree.
|
||||
Upon success,
|
||||
.Va NULL
|
||||
is returned.
|
||||
If a matching element already exists in the tree, the insertion is
|
||||
aborted, and a pointer to the existing element is returned.
|
||||
.Pp
|
||||
The
|
||||
.Fn SPLAY_REMOVE
|
||||
@@ -285,6 +281,11 @@ macro removes the element
|
||||
.Fa elm
|
||||
from the tree pointed by
|
||||
.Fa head .
|
||||
Upon success, a pointer to the removed element is returned.
|
||||
.Va NULL
|
||||
is returned if
|
||||
.Fa elm
|
||||
is not present in the tree.
|
||||
.Pp
|
||||
The
|
||||
.Fn SPLAY_FIND
|
||||
@@ -292,7 +293,7 @@ macro can be used to find a particular element in the tree.
|
||||
.Bd -literal -offset indent
|
||||
struct TYPE find, *res;
|
||||
find.key = 30;
|
||||
res = SPLAY_FIND(NAME, head, &find);
|
||||
res = SPLAY_FIND(NAME, &head, &find);
|
||||
.Ed
|
||||
.Pp
|
||||
The
|
||||
@@ -309,8 +310,8 @@ for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
|
||||
Or, for simplicity, one can use the
|
||||
.Fn SPLAY_FOREACH
|
||||
macro:
|
||||
.Bd -ragged -offset indent
|
||||
.Fn SPLAY_FOREACH np NAME head
|
||||
.Bd -literal -offset indent
|
||||
SPLAY_FOREACH(np, NAME, &head)
|
||||
.Ed
|
||||
.Pp
|
||||
The
|
||||
@@ -320,29 +321,28 @@ macro should be used to check whether a splay tree is empty.
|
||||
A red-black tree is a binary search tree with the node color as an
|
||||
extra attribute.
|
||||
It fulfills a set of conditions:
|
||||
.Bl -enum -offset indent
|
||||
.Pp
|
||||
.Bl -enum -compact -offset indent
|
||||
.It
|
||||
Every search path from the root to a leaf consists of the same number of
|
||||
black nodes.
|
||||
every search path from the root to a leaf consists of the same number of
|
||||
black nodes,
|
||||
.It
|
||||
Each red node (except for the root) has a black parent.
|
||||
each red node (except for the root) has a black parent,
|
||||
.It
|
||||
Each leaf node is black.
|
||||
each leaf node is black.
|
||||
.El
|
||||
.Pp
|
||||
Every operation on a red-black tree is bounded as
|
||||
.Fn O "lg n" .
|
||||
The maximum height of a red-black tree is
|
||||
.Fn 2lg "n + 1" .
|
||||
Every operation on a red-black tree is bounded as O(lg n).
|
||||
The maximum height of a red-black tree is 2lg (n+1).
|
||||
.Pp
|
||||
A red-black tree is headed by a structure defined by the
|
||||
.Fn RB_HEAD
|
||||
macro.
|
||||
A
|
||||
.Fa RB_HEAD
|
||||
structure is declared as follows:
|
||||
.Bd -ragged -offset indent
|
||||
.Fn RB_HEAD HEADNAME TYPE
|
||||
.Va head ;
|
||||
.Bd -literal -offset indent
|
||||
RB_HEAD(HEADNAME, TYPE) head;
|
||||
.Ed
|
||||
.Pp
|
||||
where
|
||||
@@ -360,7 +360,7 @@ their prototypes need to be declared with the
|
||||
.Fn RB_PROTOTYPE
|
||||
or
|
||||
.Fn RB_PROTOTYPE_STATIC
|
||||
macro,
|
||||
macros,
|
||||
where
|
||||
.Fa NAME
|
||||
is a unique identifier for this particular tree.
|
||||
@@ -377,7 +377,7 @@ The function bodies are generated with the
|
||||
.Fn RB_GENERATE
|
||||
or
|
||||
.Fn RB_GENERATE_STATIC
|
||||
macro.
|
||||
macros.
|
||||
These macros take the same arguments as the
|
||||
.Fn RB_PROTOTYPE
|
||||
and
|
||||
@@ -387,16 +387,15 @@ macros, but should be used only once.
|
||||
Finally,
|
||||
the
|
||||
.Fa CMP
|
||||
argument is the name of a function used to compare tree nodes
|
||||
argument is the name of a function used to compare trees' nodes
|
||||
with each other.
|
||||
The function takes two arguments of type
|
||||
.Vt "struct TYPE *" .
|
||||
.Fa "struct TYPE *" .
|
||||
If the first argument is smaller than the second, the function returns a
|
||||
value smaller than zero.
|
||||
If they are equal, the function returns zero.
|
||||
Otherwise, it should return a value greater than zero.
|
||||
The compare
|
||||
function defines the order of the tree elements.
|
||||
The compare function defines the order of the tree elements.
|
||||
.Pp
|
||||
The
|
||||
.Fn RB_INIT
|
||||
@@ -406,11 +405,8 @@ macro initializes the tree referenced by
|
||||
The red-black tree can also be initialized statically by using the
|
||||
.Fn RB_INITIALIZER
|
||||
macro like this:
|
||||
.Bd -ragged -offset indent
|
||||
.Fn RB_HEAD HEADNAME TYPE
|
||||
.Va head
|
||||
=
|
||||
.Fn RB_INITIALIZER &head ;
|
||||
.Bd -literal -offset indent
|
||||
RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head);
|
||||
.Ed
|
||||
.Pp
|
||||
The
|
||||
@@ -418,6 +414,11 @@ The
|
||||
macro inserts the new element
|
||||
.Fa elm
|
||||
into the tree.
|
||||
Upon success,
|
||||
.Va NULL
|
||||
is returned.
|
||||
If a matching element already exists in the tree, the insertion is
|
||||
aborted, and a pointer to the existing element is returned.
|
||||
.Pp
|
||||
The
|
||||
.Fn RB_REMOVE
|
||||
@@ -425,16 +426,24 @@ macro removes the element
|
||||
.Fa elm
|
||||
from the tree pointed by
|
||||
.Fa head .
|
||||
.Fn RB_REMOVE
|
||||
returns
|
||||
.Fa elm .
|
||||
.Pp
|
||||
The
|
||||
.Fn RB_FIND
|
||||
and
|
||||
.Fn RB_NFIND
|
||||
macros can be used to find a particular element in the tree.
|
||||
.Fn RB_FIND
|
||||
finds the node with the same key as
|
||||
.Fa elm .
|
||||
.Fn RB_NFIND
|
||||
finds the first node greater than or equal to the search key.
|
||||
.Bd -literal -offset indent
|
||||
struct TYPE find, *res;
|
||||
find.key = 30;
|
||||
res = RB_FIND(NAME, head, &find);
|
||||
res = RB_FIND(NAME, &head, &find);
|
||||
.Ed
|
||||
.Pp
|
||||
The
|
||||
@@ -445,26 +454,119 @@ The
|
||||
and
|
||||
.Fn RB_PREV
|
||||
macros can be used to traverse the tree:
|
||||
.Pp
|
||||
.Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
|
||||
.Bd -literal -offset indent
|
||||
for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))
|
||||
.Ed
|
||||
.Pp
|
||||
Or, for simplicity, one can use the
|
||||
.Fn RB_FOREACH
|
||||
or
|
||||
.Fn RB_FOREACH_REVERSE
|
||||
macro:
|
||||
.Bd -ragged -offset indent
|
||||
.Fn RB_FOREACH np NAME head
|
||||
macros:
|
||||
.Bd -literal -offset indent
|
||||
RB_FOREACH(np, NAME, &head)
|
||||
.Ed
|
||||
.Pp
|
||||
The macros
|
||||
.Fn RB_FOREACH_SAFE
|
||||
and
|
||||
.Fn RB_FOREACH_REVERSE_SAFE
|
||||
traverse the tree referenced by head
|
||||
in a forward or reverse direction respectively,
|
||||
assigning each element in turn to np.
|
||||
However, unlike their unsafe counterparts,
|
||||
they permit both the removal of np
|
||||
as well as freeing it from within the loop safely
|
||||
without interfering with the traversal.
|
||||
.Pp
|
||||
The
|
||||
.Fn RB_EMPTY
|
||||
macro should be used to check whether a red-black tree is empty.
|
||||
.Sh EXAMPLES
|
||||
The following example demonstrates how to declare a red-black tree
|
||||
holding integers.
|
||||
Values are inserted into it and the contents of the tree are printed
|
||||
in order.
|
||||
Lastly, the internal structure of the tree is printed.
|
||||
.Bd -literal -offset 3n
|
||||
#include <sys/tree.h>
|
||||
#include <err.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct node {
|
||||
RB_ENTRY(node) entry;
|
||||
int i;
|
||||
};
|
||||
|
||||
int intcmp(struct node *, struct node *);
|
||||
void print_tree(struct node *);
|
||||
|
||||
int
|
||||
intcmp(struct node *e1, struct node *e2)
|
||||
{
|
||||
return (e1->i < e2->i ? -1 : e1->i > e2->i);
|
||||
}
|
||||
|
||||
RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
|
||||
RB_PROTOTYPE(inttree, node, entry, intcmp)
|
||||
RB_GENERATE(inttree, node, entry, intcmp)
|
||||
|
||||
int testdata[] = {
|
||||
20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18,
|
||||
7, 11, 14
|
||||
};
|
||||
|
||||
void
|
||||
print_tree(struct node *n)
|
||||
{
|
||||
struct node *left, *right;
|
||||
|
||||
if (n == NULL) {
|
||||
printf("nil");
|
||||
return;
|
||||
}
|
||||
left = RB_LEFT(n, entry);
|
||||
right = RB_RIGHT(n, entry);
|
||||
if (left == NULL && right == NULL)
|
||||
printf("%d", n->i);
|
||||
else {
|
||||
printf("%d(", n->i);
|
||||
print_tree(left);
|
||||
printf(",");
|
||||
print_tree(right);
|
||||
printf(")");
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int i;
|
||||
struct node *n;
|
||||
|
||||
for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
|
||||
if ((n = malloc(sizeof(struct node))) == NULL)
|
||||
err(1, NULL);
|
||||
n->i = testdata[i];
|
||||
RB_INSERT(inttree, &head, n);
|
||||
}
|
||||
|
||||
RB_FOREACH(n, inttree, &head) {
|
||||
printf("%d\en", n->i);
|
||||
}
|
||||
print_tree(RB_ROOT(&head));
|
||||
printf("\en");
|
||||
return (0);
|
||||
}
|
||||
.Ed
|
||||
.Sh SEE ALSO
|
||||
.Xr queue 3bsd
|
||||
.Sh NOTES
|
||||
Trying to free a tree in the following way is a common error:
|
||||
.Bd -literal -offset indent
|
||||
SPLAY_FOREACH(var, NAME, head) {
|
||||
SPLAY_REMOVE(NAME, head, var);
|
||||
SPLAY_FOREACH(var, NAME, &head) {
|
||||
SPLAY_REMOVE(NAME, &head, var);
|
||||
free(var);
|
||||
}
|
||||
free(head);
|
||||
@@ -472,36 +574,17 @@ free(head);
|
||||
.Pp
|
||||
Since
|
||||
.Va var
|
||||
is freed, the
|
||||
is free'd, the
|
||||
.Fn FOREACH
|
||||
macro refers to a pointer that may have been reallocated already.
|
||||
Proper code needs a second variable.
|
||||
.Bd -literal -offset indent
|
||||
for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
|
||||
nxt = SPLAY_NEXT(NAME, head, var);
|
||||
SPLAY_REMOVE(NAME, head, var);
|
||||
for (var = SPLAY_MIN(NAME, &head); var != NULL; var = nxt) {
|
||||
nxt = SPLAY_NEXT(NAME, &head, var);
|
||||
SPLAY_REMOVE(NAME, &head, var);
|
||||
free(var);
|
||||
}
|
||||
.Ed
|
||||
.Pp
|
||||
Both
|
||||
.Fn RB_INSERT
|
||||
and
|
||||
.Fn SPLAY_INSERT
|
||||
return
|
||||
.Dv NULL
|
||||
if the element was inserted in the tree successfully, otherwise they
|
||||
return a pointer to the element with the colliding key.
|
||||
.Pp
|
||||
Accordingly,
|
||||
.Fn RB_REMOVE
|
||||
and
|
||||
.Fn SPLAY_REMOVE
|
||||
return the pointer to the removed element otherwise they return
|
||||
.Dv NULL
|
||||
to indicate an error.
|
||||
.Sh SEE ALSO
|
||||
.Xr queue 3bsd
|
||||
.Sh AUTHORS
|
||||
The author of the tree macros is
|
||||
.An Niels Provos .
|
||||
|
1
man/uid_from_user.3bsd
Normal file
1
man/uid_from_user.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/pwcache.3bsd
|
1
man/user_from_uid.3bsd
Normal file
1
man/user_from_uid.3bsd
Normal file
@@ -0,0 +1 @@
|
||||
.so man3/pwcache.3bsd
|
@@ -529,6 +529,7 @@ The destination buffer size is not large enough to perform the conversion.
|
||||
.%O "RFC 2045"
|
||||
.Re
|
||||
.Sh HISTORY
|
||||
.ds doc-operating-system-NetBSD-7.0 7.0
|
||||
The
|
||||
.Fn vis ,
|
||||
.Fn strvis ,
|
||||
|
@@ -15,7 +15,6 @@ AM_CPPFLAGS += \
|
||||
endif
|
||||
|
||||
libbsd_la_included_sources = \
|
||||
hash/helper.c \
|
||||
getentropy_aix.c \
|
||||
getentropy_bsd.c \
|
||||
getentropy_hpux.c \
|
||||
@@ -26,6 +25,7 @@ libbsd_la_included_sources = \
|
||||
getentropy_win.c \
|
||||
$(nil)
|
||||
|
||||
CLEANFILES =
|
||||
EXTRA_DIST = \
|
||||
libbsd.map \
|
||||
libbsd.pc.in \
|
||||
@@ -34,10 +34,6 @@ EXTRA_DIST = \
|
||||
$(libbsd_la_included_sources) \
|
||||
$(nil)
|
||||
|
||||
CLEANFILES = \
|
||||
hash/md5hl.c \
|
||||
$(nil)
|
||||
|
||||
pkgconfigdir = $(libdir)/pkgconfig
|
||||
pkgconfig_DATA = \
|
||||
libbsd.pc \
|
||||
@@ -53,20 +49,19 @@ pkgconfig_DATA += libbsd-ctor.pc
|
||||
lib_LIBRARIES += libbsd-ctor.a
|
||||
endif
|
||||
|
||||
hash/md5hl.c: $(srcdir)/hash/helper.c
|
||||
$(AM_V_at) $(MKDIR_P) hash
|
||||
$(AM_V_GEN) sed -e 's:hashinc:md5.h:g' -e 's:HASH:MD5:g' $< > $@
|
||||
|
||||
libbsd_la_DEPENDENCIES = \
|
||||
$(libbsd_la_included_sources) \
|
||||
libbsd.map
|
||||
libbsd_la_LIBADD = \
|
||||
$(CLOCK_GETTIME_LIBS) \
|
||||
$(ARC4RANDOM_ATFORK_LIBS) \
|
||||
$(MD5_LIBS) \
|
||||
$(LIBBSD_LIBS) \
|
||||
$(nil)
|
||||
libbsd_la_LDFLAGS = \
|
||||
-Wl,--version-script=$(srcdir)/libbsd.map \
|
||||
-version-number $(LIBBSD_ABI)
|
||||
if HAVE_LINKER_VERSION_SCRIPT
|
||||
libbsd_la_LDFLAGS += \
|
||||
-Wl,--version-script=$(srcdir)/libbsd.map
|
||||
endif
|
||||
libbsd_la_SOURCES = \
|
||||
arc4random.c \
|
||||
arc4random.h \
|
||||
@@ -83,6 +78,7 @@ libbsd_la_SOURCES = \
|
||||
expand_number.c \
|
||||
explicit_bzero.c \
|
||||
fgetln.c \
|
||||
freezero.c \
|
||||
fgetwln.c \
|
||||
flopen.c \
|
||||
fmtcheck.c \
|
||||
@@ -91,23 +87,22 @@ libbsd_la_SOURCES = \
|
||||
funopen.c \
|
||||
getbsize.c \
|
||||
getpeereid.c \
|
||||
hash/md5.c \
|
||||
hash/md5hl.c \
|
||||
hash/sha512.h \
|
||||
hash/sha512c.c \
|
||||
heapsort.c \
|
||||
humanize_number.c \
|
||||
inet_net_pton.c \
|
||||
local-elf.h \
|
||||
local-link.h \
|
||||
md5.c \
|
||||
merge.c \
|
||||
nlist.c \
|
||||
pidfile.c \
|
||||
progname.c \
|
||||
pwcache.c \
|
||||
radixsort.c \
|
||||
readpassphrase.c \
|
||||
reallocarray.c \
|
||||
reallocf.c \
|
||||
recallocarray.c \
|
||||
setmode.c \
|
||||
setproctitle.c \
|
||||
strlcat.c \
|
||||
@@ -131,22 +126,51 @@ libbsd_la_SOURCES += \
|
||||
$(nil)
|
||||
endif
|
||||
|
||||
if NEED_TRANSPARENT_LIBMD
|
||||
CLEANFILES += \
|
||||
format.ld \
|
||||
# EOL
|
||||
endif
|
||||
|
||||
libbsd_ctor_a_SOURCES = \
|
||||
setproctitle_ctor.c \
|
||||
$(nil)
|
||||
|
||||
if NEED_TRANSPARENT_LIBMD
|
||||
TRANSPARENT_LIBMD_DEPENDS = format.ld
|
||||
|
||||
format.ld:
|
||||
$(CC) -shared -nostdlib -nostartfiles -x assembler /dev/null -o $@.so
|
||||
objdump -f $@.so | sed -n 's/.*file format \(.*\)/OUTPUT_FORMAT(\1)/;T;p' > $@
|
||||
rm -f $@.so
|
||||
endif
|
||||
|
||||
runtimelibdir = $(libdir)
|
||||
|
||||
install-exec-hook:
|
||||
install-exec-hook: $(TRANSPARENT_LIBMD_DEPENDS)
|
||||
if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
|
||||
$(MKDIR_P) $(DESTDIR)$(runtimelibdir); \
|
||||
mv $(DESTDIR)$(libdir)/libbsd*.so.* \
|
||||
$(DESTDIR)$(runtimelibdir)/; \
|
||||
fi
|
||||
if NEED_TRANSPARENT_LIBMD
|
||||
# The "GNU ld script" magic is required so that GNU ldconfig does not complain
|
||||
# about an unknown format file.
|
||||
soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
|
||||
sorelprefix=`echo $(libdir) | sed -r -e 's:(^/)?[^/]+:..:g'`; \
|
||||
$(RM) $(DESTDIR)$(libdir)/libbsd.so; \
|
||||
(echo '/* GNU ld script'; \
|
||||
echo ' * The MD5 functions are provided by the libmd library. */'; \
|
||||
cat format.ld; \
|
||||
echo "GROUP($(runtimelibdir)/$$soname AS_NEEDED($(MD5_LIBS)))"; \
|
||||
)>$(DESTDIR)$(libdir)/libbsd.so
|
||||
else
|
||||
if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
|
||||
soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
|
||||
sorelprefix=`echo $(libdir) | $(SED) -r -e 's:(^/)?[^/]+:..:g'`; \
|
||||
ln -sf $$sorelprefix$(runtimelibdir)/$$soname \
|
||||
$(DESTDIR)$(libdir)/libbsd.so; \
|
||||
fi
|
||||
endif
|
||||
|
||||
uninstall-hook:
|
||||
rm -f $(DESTDIR)$(runtimelibdir)/libbsd*.so*
|
||||
|
117
src/closefrom.c
117
src/closefrom.c
@@ -1,6 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2004-2005, 2007, 2010, 2012-2014
|
||||
* Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* SPDX-License-Identifier: ISC
|
||||
*
|
||||
* Copyright (c) 2004-2005, 2007, 2010, 2012-2015, 2017-2018
|
||||
* Todd C. Miller <Todd.Miller@sudo.ws>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
@@ -17,19 +19,17 @@
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#ifdef STDC_HEADERS
|
||||
# include <stdlib.h>
|
||||
# include <stddef.h>
|
||||
#else
|
||||
# ifdef HAVE_STDLIB_H
|
||||
# include <stdlib.h>
|
||||
#ifdef __linux__
|
||||
# include <sys/syscall.h>
|
||||
# if defined(__NR_close_range) && !defined(SYS_close_range)
|
||||
# define SYS_close_range __NR_close_range
|
||||
# endif
|
||||
#endif /* STDC_HEADERS */
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#ifdef HAVE_PSTAT_GETPROC
|
||||
# include <sys/param.h>
|
||||
# include <sys/pstat.h>
|
||||
@@ -56,10 +56,6 @@
|
||||
# define OPEN_MAX 256
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_FCNTL_CLOSEM) && !defined(HAVE_DIRFD)
|
||||
# define closefrom closefrom_fallback
|
||||
#endif
|
||||
|
||||
static inline void
|
||||
closefrom_close(int fd)
|
||||
{
|
||||
@@ -71,56 +67,64 @@ closefrom_close(int fd)
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(__linux__) && defined(SYS_close_range)
|
||||
static inline int
|
||||
sys_close_range(unsigned int fd, unsigned int max_fd, unsigned int flags)
|
||||
{
|
||||
return syscall(SYS_close_range, fd, max_fd, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Close all file descriptors greater than or equal to lowfd.
|
||||
* This is the expensive (fallback) method.
|
||||
*/
|
||||
void
|
||||
static void
|
||||
closefrom_fallback(int lowfd)
|
||||
{
|
||||
long fd, maxfd;
|
||||
|
||||
/*
|
||||
* Fall back on sysconf() or getdtablesize(). We avoid checking
|
||||
* resource limits since it is possible to open a file descriptor
|
||||
* and then drop the rlimit such that it is below the open fd.
|
||||
* Fall back on sysconf(_SC_OPEN_MAX) or getdtablesize(). This is
|
||||
* equivalent to checking the RLIMIT_NOFILE soft limit. It is
|
||||
* possible for there to be open file descriptors past this limit
|
||||
* but there is not much we can do about that since the hard limit
|
||||
* may be RLIM_INFINITY (LLONG_MAX or ULLONG_MAX on modern systems).
|
||||
*/
|
||||
#ifdef HAVE_SYSCONF
|
||||
maxfd = sysconf(_SC_OPEN_MAX);
|
||||
#else
|
||||
maxfd = getdtablesize();
|
||||
#endif /* HAVE_SYSCONF */
|
||||
if (maxfd < 0)
|
||||
if (maxfd < OPEN_MAX)
|
||||
maxfd = OPEN_MAX;
|
||||
|
||||
/* Make sure we did not get RLIM_INFINITY as the upper limit. */
|
||||
if (maxfd > INT_MAX)
|
||||
maxfd = INT_MAX;
|
||||
|
||||
for (fd = lowfd; fd < maxfd; fd++)
|
||||
closefrom_close(fd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Close all file descriptors greater than or equal to lowfd.
|
||||
* We try the fast way first, falling back on the slow method.
|
||||
*/
|
||||
#if defined(HAVE_FCNTL_CLOSEM)
|
||||
void
|
||||
closefrom(int lowfd)
|
||||
#if defined(HAVE_PSTAT_GETPROC)
|
||||
static int
|
||||
closefrom_pstat(int lowfd)
|
||||
{
|
||||
if (fcntl(lowfd, F_CLOSEM, 0) == -1)
|
||||
closefrom_fallback(lowfd);
|
||||
}
|
||||
#elif defined(HAVE_PSTAT_GETPROC)
|
||||
void
|
||||
closefrom(int lowfd)
|
||||
{
|
||||
struct pst_status pstat;
|
||||
struct pst_status pst;
|
||||
int fd;
|
||||
|
||||
if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1) {
|
||||
for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
|
||||
/*
|
||||
* EOVERFLOW is not a fatal error for the fields we use.
|
||||
* See the "EOVERFLOW Error" section of pstat_getvminfo(3).
|
||||
*/
|
||||
if (pstat_getproc(&pst, sizeof(pst), 0, getpid()) != -1 ||
|
||||
errno == EOVERFLOW) {
|
||||
for (fd = lowfd; fd <= pst.pst_highestfd; fd++)
|
||||
(void)close(fd);
|
||||
} else {
|
||||
closefrom_fallback(lowfd);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#elif defined(HAVE_DIRFD)
|
||||
static int
|
||||
@@ -135,8 +139,8 @@ closefrom_procfs(int lowfd)
|
||||
int ret = 0;
|
||||
int i;
|
||||
|
||||
/* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */
|
||||
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
|
||||
/* Use /proc/self/fd (or /dev/fd on macOS) if it exists. */
|
||||
# ifdef __APPLE__
|
||||
path = "/dev/fd";
|
||||
# else
|
||||
path = "/proc/self/fd";
|
||||
@@ -180,13 +184,36 @@ closefrom_procfs(int lowfd)
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Close all file descriptors greater than or equal to lowfd.
|
||||
* We try the fast way first, falling back on the slow method.
|
||||
*/
|
||||
void
|
||||
closefrom(int lowfd)
|
||||
{
|
||||
if (closefrom_procfs(lowfd) == 0)
|
||||
return;
|
||||
if (lowfd < 0)
|
||||
lowfd = 0;
|
||||
|
||||
/* Try the fast methods first, if possible. */
|
||||
#if defined(HAVE_FCNTL_CLOSEM)
|
||||
if (fcntl(lowfd, F_CLOSEM, 0) != -1)
|
||||
return;
|
||||
#endif /* HAVE_FCNTL_CLOSEM */
|
||||
#if defined(__linux__) && defined(SYS_close_range)
|
||||
if (sys_close_range(lowfd, UINT_MAX, 0) == 0)
|
||||
return;
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_PSTAT_GETPROC)
|
||||
if (closefrom_pstat(lowfd) != -1)
|
||||
return;
|
||||
#elif defined(HAVE_DIRFD)
|
||||
if (closefrom_procfs(lowfd) != -1)
|
||||
return;
|
||||
#endif /* HAVE_DIRFD */
|
||||
|
||||
/* Do things the slow way. */
|
||||
closefrom_fallback(lowfd);
|
||||
}
|
||||
#endif /* HAVE_FCNTL_CLOSEM */
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: explicit_bzero.c,v 1.3 2014/06/21 02:34:26 matthew Exp $ */
|
||||
/* $OpenBSD: explicit_bzero.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */
|
||||
/*
|
||||
* Public domain.
|
||||
* Written by Matthew Dempsky.
|
||||
@@ -6,6 +6,9 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
__attribute__((__weak__)) void
|
||||
__explicit_bzero_hook(void *, size_t);
|
||||
|
||||
__attribute__((__weak__)) void
|
||||
__explicit_bzero_hook(void *buf, size_t len)
|
||||
{
|
||||
|
@@ -76,8 +76,8 @@ fgetln(FILE *stream, size_t *len)
|
||||
}
|
||||
}
|
||||
libbsd_link_warning(fgetln,
|
||||
"This function cannot be safely ported, use getline(3) "
|
||||
"instead, as it is supported by GNU and POSIX.1-2008.")
|
||||
"The fgetln() function cannot be safely ported, use getline(3) "
|
||||
"instead, as it is supported by GNU and POSIX.1-2008.");
|
||||
#else
|
||||
#error "Function fgetln() needs to be ported."
|
||||
#endif
|
||||
|
@@ -88,5 +88,5 @@ fgetwln(FILE *stream, size_t *lenp)
|
||||
return wused ? fb->wbuf : NULL;
|
||||
}
|
||||
libbsd_link_warning(fgetwln,
|
||||
"This function cannot be safely ported, use fgetwc(3) "
|
||||
"instead, as it is supported by C99 and POSIX.1-2001.")
|
||||
"The fgetwln() function cannot be safely ported, use fgetwc(3) "
|
||||
"instead, as it is supported by C99 and POSIX.1-2001.");
|
||||
|
30
src/freezero.c
Normal file
30
src/freezero.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/* $OpenBSD: malloc.c,v 1.267 2020/11/23 15:42:11 otto Exp $ */
|
||||
/*
|
||||
* Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net>
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void
|
||||
freezero(void *ptr, size_t sz)
|
||||
{
|
||||
/* This is legal. */
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
|
||||
explicit_bzero(ptr, sz);
|
||||
free(ptr);
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2011, 2013, 2017 Guillem Jover
|
||||
* Copyright © 2011, 2013, 2017, 2020 Guillem Jover
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -87,10 +87,10 @@ funopen_close(void *cookie)
|
||||
struct funopen_cookie *cookiewrap = cookie;
|
||||
int rc;
|
||||
|
||||
if (cookiewrap->closefn == NULL)
|
||||
return 0;
|
||||
|
||||
if (cookiewrap->closefn)
|
||||
rc = cookiewrap->closefn(cookiewrap->orig_cookie);
|
||||
else
|
||||
rc = 0;
|
||||
|
||||
free(cookiewrap);
|
||||
|
||||
|
@@ -39,7 +39,7 @@
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
|
||||
#include "hash/sha512.h"
|
||||
#include <sha512.h>
|
||||
|
||||
#include <libperfstat.h>
|
||||
|
||||
@@ -54,9 +54,9 @@
|
||||
HD(b); \
|
||||
} while (0)
|
||||
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
|
||||
|
||||
int getentropy(void *buf, size_t len);
|
||||
|
||||
|
@@ -26,6 +26,8 @@
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
|
||||
int getentropy(void *buf, size_t len);
|
||||
|
||||
/*
|
||||
* Derived from lib/libc/gen/arc4random.c from FreeBSD.
|
||||
*/
|
||||
|
@@ -42,7 +42,7 @@
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "hash/sha512.h"
|
||||
#include <sha512.h>
|
||||
|
||||
#include <sys/vfs.h>
|
||||
|
||||
@@ -59,9 +59,9 @@
|
||||
HD(b); \
|
||||
} while (0)
|
||||
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
|
||||
|
||||
int getentropy(void *buf, size_t len);
|
||||
|
||||
|
@@ -44,7 +44,7 @@
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "hash/sha512.h"
|
||||
#include <sha512.h>
|
||||
|
||||
#ifdef HAVE_GETAUXVAL
|
||||
#include <sys/auxv.h>
|
||||
@@ -62,9 +62,9 @@
|
||||
HD(b); \
|
||||
} while (0)
|
||||
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
|
||||
|
||||
int getentropy(void *buf, size_t len);
|
||||
|
||||
@@ -228,7 +228,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
|
||||
{
|
||||
SHA512_CTX *ctx = data;
|
||||
|
||||
SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
|
||||
SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@@ -48,7 +48,7 @@
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "hash/sha512.h"
|
||||
#include <sha512.h>
|
||||
|
||||
#include <linux/types.h>
|
||||
#include <linux/random.h>
|
||||
@@ -68,9 +68,9 @@
|
||||
HD(b); \
|
||||
} while (0)
|
||||
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
|
||||
|
||||
int getentropy(void *buf, size_t len);
|
||||
|
||||
@@ -333,7 +333,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
|
||||
{
|
||||
SHA512_CTX *ctx = data;
|
||||
|
||||
SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
|
||||
SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -520,17 +520,17 @@ getentropy_fallback(void *buf, size_t len)
|
||||
#ifdef HAVE_GETAUXVAL
|
||||
#ifdef AT_RANDOM
|
||||
/* Not as random as you think but we take what we are given */
|
||||
p = (char *) getauxval(AT_RANDOM);
|
||||
p = (char *) ((intptr_t) getauxval(AT_RANDOM));
|
||||
if (p)
|
||||
HR(p, 16);
|
||||
#endif
|
||||
#ifdef AT_SYSINFO_EHDR
|
||||
p = (char *) getauxval(AT_SYSINFO_EHDR);
|
||||
p = (char *) ((intptr_t) getauxval(AT_SYSINFO_EHDR));
|
||||
if (p)
|
||||
HR(p, pgs);
|
||||
#endif
|
||||
#ifdef AT_BASE
|
||||
p = (char *) getauxval(AT_BASE);
|
||||
p = (char *) ((intptr_t) getauxval(AT_BASE));
|
||||
if (p)
|
||||
HD(p);
|
||||
#endif
|
||||
|
@@ -76,9 +76,9 @@
|
||||
HD(b); \
|
||||
} while (0)
|
||||
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
|
||||
|
||||
int getentropy(void *buf, size_t len);
|
||||
|
||||
|
@@ -62,9 +62,9 @@
|
||||
HD(b); \
|
||||
} while (0)
|
||||
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
|
||||
#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
|
||||
#define HD(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
|
||||
#define HF(x) (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
|
||||
|
||||
int getentropy(void *buf, size_t len);
|
||||
|
||||
@@ -249,7 +249,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
|
||||
{
|
||||
SHA512_CTX *ctx = data;
|
||||
|
||||
SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
|
||||
SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
1
src/hash/.gitignore
vendored
1
src/hash/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
md5hl.c
|
@@ -1,105 +0,0 @@
|
||||
/** $MirOS: src/lib/libc/hash/helper.c,v 1.5 2007/05/07 15:21:18 tg Exp $ */
|
||||
/* $OpenBSD: helper.c,v 1.8 2005/08/08 08:05:35 espie Exp $ */
|
||||
|
||||
/*
|
||||
* ----------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42):
|
||||
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
|
||||
* can do whatever you want with this stuff. If we meet some day, and you think
|
||||
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
#include <hashinc>
|
||||
|
||||
/* ARGSUSED */
|
||||
char *
|
||||
HASHEnd(HASH_CTX *ctx, char *buf)
|
||||
{
|
||||
int i;
|
||||
uint8_t digest[HASH_DIGEST_LENGTH];
|
||||
#ifdef HASH_DIGEST_UPPERCASE
|
||||
static const char hex[] = "0123456789ABCDEF";
|
||||
#else
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
#endif
|
||||
|
||||
if (buf == NULL && (buf = malloc(HASH_DIGEST_STRING_LENGTH)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
HASHFinal(digest, ctx);
|
||||
for (i = 0; i < HASH_DIGEST_LENGTH; i++) {
|
||||
buf[i + i] = hex[digest[i] >> 4];
|
||||
buf[i + i + 1] = hex[digest[i] & 0x0f];
|
||||
}
|
||||
buf[i + i] = '\0';
|
||||
memset(digest, 0, sizeof(digest));
|
||||
return (buf);
|
||||
}
|
||||
|
||||
char *
|
||||
HASHFileChunk(const char *filename, char *buf, off_t off, off_t len)
|
||||
{
|
||||
struct stat sb;
|
||||
unsigned char buffer[BUFSIZ];
|
||||
HASH_CTX ctx;
|
||||
int fd, save_errno;
|
||||
ssize_t nr;
|
||||
|
||||
HASHInit(&ctx);
|
||||
|
||||
if ((fd = open(filename, O_RDONLY)) < 0)
|
||||
return (NULL);
|
||||
if (len == 0) {
|
||||
if (fstat(fd, &sb) == -1) {
|
||||
close(fd);
|
||||
return (NULL);
|
||||
}
|
||||
len = sb.st_size;
|
||||
}
|
||||
if ((len < 0) || (off > 0 && lseek(fd, off, SEEK_SET) < 0)) {
|
||||
close(fd);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
while ((nr = read(fd, buffer,
|
||||
(size_t)(len ? MIN(BUFSIZ, len) : BUFSIZ))) > 0) {
|
||||
HASHUpdate(&ctx, buffer, (size_t)nr);
|
||||
if (len > 0 && (len -= nr) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
save_errno = errno;
|
||||
close(fd);
|
||||
errno = save_errno;
|
||||
return (nr < 0 ? NULL : HASHEnd(&ctx, buf));
|
||||
}
|
||||
|
||||
char *
|
||||
HASHFile(const char *filename, char *buf)
|
||||
{
|
||||
return (HASHFileChunk(filename, buf, (off_t)0, (off_t)0));
|
||||
}
|
||||
|
||||
char *
|
||||
HASHData(const unsigned char *data, size_t len, char *buf)
|
||||
{
|
||||
HASH_CTX ctx;
|
||||
|
||||
HASHInit(&ctx);
|
||||
HASHUpdate(&ctx, data, len);
|
||||
return (HASHEnd(&ctx, buf));
|
||||
}
|
248
src/hash/md5.c
248
src/hash/md5.c
@@ -1,248 +0,0 @@
|
||||
/* $OpenBSD: md5.c,v 1.8 2005/08/08 08:05:35 espie Exp $ */
|
||||
|
||||
/*
|
||||
* This code implements the MD5 message-digest algorithm.
|
||||
* The algorithm is due to Ron Rivest. This code was
|
||||
* written by Colin Plumb in 1993, no copyright is claimed.
|
||||
* This code is in the public domain; do with it what you wish.
|
||||
*
|
||||
* Equivalent code is available from RSA Data Security, Inc.
|
||||
* This code has been tested against that, and is equivalent,
|
||||
* except that you don't need to include two pages of legalese
|
||||
* with every copy.
|
||||
*
|
||||
* To compute the message digest of a chunk of bytes, declare an
|
||||
* MD5Context structure, pass it to MD5Init, call MD5Update as
|
||||
* needed on buffers full of bytes, and then call MD5Final, which
|
||||
* will fill a supplied 16-byte array with the digest.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <md5.h>
|
||||
|
||||
#define PUT_64BIT_LE(cp, value) do { \
|
||||
(cp)[7] = (value) >> 56; \
|
||||
(cp)[6] = (value) >> 48; \
|
||||
(cp)[5] = (value) >> 40; \
|
||||
(cp)[4] = (value) >> 32; \
|
||||
(cp)[3] = (value) >> 24; \
|
||||
(cp)[2] = (value) >> 16; \
|
||||
(cp)[1] = (value) >> 8; \
|
||||
(cp)[0] = (value); } while (0)
|
||||
|
||||
#define PUT_32BIT_LE(cp, value) do { \
|
||||
(cp)[3] = (value) >> 24; \
|
||||
(cp)[2] = (value) >> 16; \
|
||||
(cp)[1] = (value) >> 8; \
|
||||
(cp)[0] = (value); } while (0)
|
||||
|
||||
static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
||||
* initialization constants.
|
||||
*/
|
||||
void
|
||||
MD5Init(MD5_CTX *ctx)
|
||||
{
|
||||
ctx->count = 0;
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xefcdab89;
|
||||
ctx->state[2] = 0x98badcfe;
|
||||
ctx->state[3] = 0x10325476;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update context to reflect the concatenation of another buffer full
|
||||
* of bytes.
|
||||
*/
|
||||
void
|
||||
MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
|
||||
{
|
||||
size_t have, need;
|
||||
|
||||
/* Check how many bytes we already have and how many more we need. */
|
||||
have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
|
||||
need = MD5_BLOCK_LENGTH - have;
|
||||
|
||||
/* Update bitcount */
|
||||
ctx->count += (uint64_t)len << 3;
|
||||
|
||||
if (len >= need) {
|
||||
if (have != 0) {
|
||||
memcpy(ctx->buffer + have, input, need);
|
||||
MD5Transform(ctx->state, ctx->buffer);
|
||||
input += need;
|
||||
len -= need;
|
||||
have = 0;
|
||||
}
|
||||
|
||||
/* Process data in MD5_BLOCK_LENGTH-byte chunks. */
|
||||
while (len >= MD5_BLOCK_LENGTH) {
|
||||
MD5Transform(ctx->state, input);
|
||||
input += MD5_BLOCK_LENGTH;
|
||||
len -= MD5_BLOCK_LENGTH;
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle any remaining bytes of data. */
|
||||
if (len != 0)
|
||||
memcpy(ctx->buffer + have, input, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Pad pad to 64-byte boundary with the bit pattern
|
||||
* 1 0* (64-bit count of bits processed, MSB-first)
|
||||
*/
|
||||
void
|
||||
MD5Pad(MD5_CTX *ctx)
|
||||
{
|
||||
uint8_t count[8];
|
||||
size_t padlen;
|
||||
|
||||
/* Convert count to 8 bytes in little endian order. */
|
||||
PUT_64BIT_LE(count, ctx->count);
|
||||
|
||||
/* Pad out to 56 mod 64. */
|
||||
padlen = MD5_BLOCK_LENGTH -
|
||||
((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
|
||||
if (padlen < 1 + 8)
|
||||
padlen += MD5_BLOCK_LENGTH;
|
||||
MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
|
||||
MD5Update(ctx, count, 8);
|
||||
}
|
||||
|
||||
/*
|
||||
* Final wrapup--call MD5Pad, fill in digest and zero out ctx.
|
||||
*/
|
||||
void
|
||||
MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
|
||||
{
|
||||
int i;
|
||||
|
||||
MD5Pad(ctx);
|
||||
if (digest != NULL) {
|
||||
for (i = 0; i < 4; i++)
|
||||
PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* The four core functions - F1 is optimized somewhat */
|
||||
|
||||
/* #define F1(x, y, z) (x & y | ~x & z) */
|
||||
#define F1(x, y, z) (z ^ (x & (y ^ z)))
|
||||
#define F2(x, y, z) F1(z, x, y)
|
||||
#define F3(x, y, z) (x ^ y ^ z)
|
||||
#define F4(x, y, z) (y ^ (x | ~z))
|
||||
|
||||
/* This is the central step in the MD5 algorithm. */
|
||||
#define MD5STEP(f, w, x, y, z, data, s) \
|
||||
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
|
||||
|
||||
/*
|
||||
* The core of the MD5 algorithm, this alters an existing MD5 hash to
|
||||
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
||||
* the data and converts bytes into longwords for this routine.
|
||||
*/
|
||||
void
|
||||
MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
||||
{
|
||||
uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
memcpy(in, block, sizeof(in));
|
||||
#else
|
||||
for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
|
||||
in[a] = (uint32_t)(
|
||||
(uint32_t)(block[a * 4 + 0]) |
|
||||
(uint32_t)(block[a * 4 + 1]) << 8 |
|
||||
(uint32_t)(block[a * 4 + 2]) << 16 |
|
||||
(uint32_t)(block[a * 4 + 3]) << 24);
|
||||
}
|
||||
#endif
|
||||
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
|
||||
MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
|
||||
|
||||
MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
|
||||
|
||||
MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[2 ] + 0xc4ac5665, 23);
|
||||
|
||||
MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[7 ] + 0x432aff97, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[5 ] + 0xfc93a039, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[3 ] + 0x8f0ccc92, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[1 ] + 0x85845dd1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[8 ] + 0x6fa87e4f, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[6 ] + 0xa3014314, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[4 ] + 0xf7537e82, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[2 ] + 0x2ad7d2bb, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[9 ] + 0xeb86d391, 21);
|
||||
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
}
|
@@ -1,53 +0,0 @@
|
||||
/*-
|
||||
* Copyright 2005 Colin Percival
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#ifndef _SHA512_H_
|
||||
#define _SHA512_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#define SHA512_DIGEST_LENGTH 64
|
||||
|
||||
typedef struct SHA512Context {
|
||||
uint64_t state[8];
|
||||
uint64_t count[2];
|
||||
unsigned char buf[128];
|
||||
} SHA512_CTX;
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
void SHA512_Init(SHA512_CTX *);
|
||||
void SHA512_Update(SHA512_CTX *, const void *, size_t);
|
||||
void SHA512_Final(unsigned char [64], SHA512_CTX *);
|
||||
char *SHA512_End(SHA512_CTX *, char *);
|
||||
char *SHA512_File(const char *, char *);
|
||||
char *SHA512_FileChunk(const char *, char *, off_t, off_t);
|
||||
char *SHA512_Data(const void *, unsigned int, char *);
|
||||
__END_DECLS
|
||||
|
||||
#endif /* !_SHA512_H_ */
|
@@ -1,318 +0,0 @@
|
||||
/*-
|
||||
* Copyright 2005 Colin Percival
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/endian.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "sha512.h"
|
||||
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
|
||||
/* Copy a vector of big-endian uint64_t into a vector of bytes */
|
||||
#define be64enc_vect(dst, src, len) \
|
||||
memcpy((void *)dst, (const void *)src, (size_t)len)
|
||||
|
||||
/* Copy a vector of bytes into a vector of big-endian uint64_t */
|
||||
#define be64dec_vect(dst, src, len) \
|
||||
memcpy((void *)dst, (const void *)src, (size_t)len)
|
||||
|
||||
#else /* BYTE_ORDER != BIG_ENDIAN */
|
||||
|
||||
/*
|
||||
* Encode a length len/4 vector of (uint64_t) into a length len vector of
|
||||
* (unsigned char) in big-endian form. Assumes len is a multiple of 8.
|
||||
*/
|
||||
static void
|
||||
be64enc_vect(unsigned char *dst, const uint64_t *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len / 8; i++)
|
||||
be64enc(dst + i * 8, src[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Decode a big-endian length len vector of (unsigned char) into a length
|
||||
* len/4 vector of (uint64_t). Assumes len is a multiple of 8.
|
||||
*/
|
||||
static void
|
||||
be64dec_vect(uint64_t *dst, const unsigned char *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len / 8; i++)
|
||||
dst[i] = be64dec(src + i * 8);
|
||||
}
|
||||
|
||||
#endif /* BYTE_ORDER != BIG_ENDIAN */
|
||||
|
||||
/* Elementary functions used by SHA512 */
|
||||
#define Ch(x, y, z) ((x & (y ^ z)) ^ z)
|
||||
#define Maj(x, y, z) ((x & (y | z)) | (y & z))
|
||||
#define SHR(x, n) (x >> n)
|
||||
#define ROTR(x, n) ((x >> n) | (x << (64 - n)))
|
||||
#define S0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
|
||||
#define S1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
|
||||
#define s0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
|
||||
#define s1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
|
||||
|
||||
/* SHA512 round function */
|
||||
#define RND(a, b, c, d, e, f, g, h, k) \
|
||||
t0 = h + S1(e) + Ch(e, f, g) + k; \
|
||||
t1 = S0(a) + Maj(a, b, c); \
|
||||
d += t0; \
|
||||
h = t0 + t1;
|
||||
|
||||
/* Adjusted round function for rotating state */
|
||||
#define RNDr(S, W, i, k) \
|
||||
RND(S[(80 - i) % 8], S[(81 - i) % 8], \
|
||||
S[(82 - i) % 8], S[(83 - i) % 8], \
|
||||
S[(84 - i) % 8], S[(85 - i) % 8], \
|
||||
S[(86 - i) % 8], S[(87 - i) % 8], \
|
||||
W[i] + k)
|
||||
|
||||
/*
|
||||
* SHA512 block compression function. The 512-bit state is transformed via
|
||||
* the 512-bit input block to produce a new state.
|
||||
*/
|
||||
static void
|
||||
SHA512_Transform(uint64_t * state, const unsigned char block[128])
|
||||
{
|
||||
uint64_t W[80];
|
||||
uint64_t S[8];
|
||||
uint64_t t0, t1;
|
||||
int i;
|
||||
|
||||
/* 1. Prepare message schedule W. */
|
||||
be64dec_vect(W, block, 128);
|
||||
for (i = 16; i < 80; i++)
|
||||
W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
|
||||
|
||||
/* 2. Initialize working variables. */
|
||||
memcpy(S, state, 64);
|
||||
|
||||
/* 3. Mix. */
|
||||
RNDr(S, W, 0, 0x428a2f98d728ae22ULL);
|
||||
RNDr(S, W, 1, 0x7137449123ef65cdULL);
|
||||
RNDr(S, W, 2, 0xb5c0fbcfec4d3b2fULL);
|
||||
RNDr(S, W, 3, 0xe9b5dba58189dbbcULL);
|
||||
RNDr(S, W, 4, 0x3956c25bf348b538ULL);
|
||||
RNDr(S, W, 5, 0x59f111f1b605d019ULL);
|
||||
RNDr(S, W, 6, 0x923f82a4af194f9bULL);
|
||||
RNDr(S, W, 7, 0xab1c5ed5da6d8118ULL);
|
||||
RNDr(S, W, 8, 0xd807aa98a3030242ULL);
|
||||
RNDr(S, W, 9, 0x12835b0145706fbeULL);
|
||||
RNDr(S, W, 10, 0x243185be4ee4b28cULL);
|
||||
RNDr(S, W, 11, 0x550c7dc3d5ffb4e2ULL);
|
||||
RNDr(S, W, 12, 0x72be5d74f27b896fULL);
|
||||
RNDr(S, W, 13, 0x80deb1fe3b1696b1ULL);
|
||||
RNDr(S, W, 14, 0x9bdc06a725c71235ULL);
|
||||
RNDr(S, W, 15, 0xc19bf174cf692694ULL);
|
||||
RNDr(S, W, 16, 0xe49b69c19ef14ad2ULL);
|
||||
RNDr(S, W, 17, 0xefbe4786384f25e3ULL);
|
||||
RNDr(S, W, 18, 0x0fc19dc68b8cd5b5ULL);
|
||||
RNDr(S, W, 19, 0x240ca1cc77ac9c65ULL);
|
||||
RNDr(S, W, 20, 0x2de92c6f592b0275ULL);
|
||||
RNDr(S, W, 21, 0x4a7484aa6ea6e483ULL);
|
||||
RNDr(S, W, 22, 0x5cb0a9dcbd41fbd4ULL);
|
||||
RNDr(S, W, 23, 0x76f988da831153b5ULL);
|
||||
RNDr(S, W, 24, 0x983e5152ee66dfabULL);
|
||||
RNDr(S, W, 25, 0xa831c66d2db43210ULL);
|
||||
RNDr(S, W, 26, 0xb00327c898fb213fULL);
|
||||
RNDr(S, W, 27, 0xbf597fc7beef0ee4ULL);
|
||||
RNDr(S, W, 28, 0xc6e00bf33da88fc2ULL);
|
||||
RNDr(S, W, 29, 0xd5a79147930aa725ULL);
|
||||
RNDr(S, W, 30, 0x06ca6351e003826fULL);
|
||||
RNDr(S, W, 31, 0x142929670a0e6e70ULL);
|
||||
RNDr(S, W, 32, 0x27b70a8546d22ffcULL);
|
||||
RNDr(S, W, 33, 0x2e1b21385c26c926ULL);
|
||||
RNDr(S, W, 34, 0x4d2c6dfc5ac42aedULL);
|
||||
RNDr(S, W, 35, 0x53380d139d95b3dfULL);
|
||||
RNDr(S, W, 36, 0x650a73548baf63deULL);
|
||||
RNDr(S, W, 37, 0x766a0abb3c77b2a8ULL);
|
||||
RNDr(S, W, 38, 0x81c2c92e47edaee6ULL);
|
||||
RNDr(S, W, 39, 0x92722c851482353bULL);
|
||||
RNDr(S, W, 40, 0xa2bfe8a14cf10364ULL);
|
||||
RNDr(S, W, 41, 0xa81a664bbc423001ULL);
|
||||
RNDr(S, W, 42, 0xc24b8b70d0f89791ULL);
|
||||
RNDr(S, W, 43, 0xc76c51a30654be30ULL);
|
||||
RNDr(S, W, 44, 0xd192e819d6ef5218ULL);
|
||||
RNDr(S, W, 45, 0xd69906245565a910ULL);
|
||||
RNDr(S, W, 46, 0xf40e35855771202aULL);
|
||||
RNDr(S, W, 47, 0x106aa07032bbd1b8ULL);
|
||||
RNDr(S, W, 48, 0x19a4c116b8d2d0c8ULL);
|
||||
RNDr(S, W, 49, 0x1e376c085141ab53ULL);
|
||||
RNDr(S, W, 50, 0x2748774cdf8eeb99ULL);
|
||||
RNDr(S, W, 51, 0x34b0bcb5e19b48a8ULL);
|
||||
RNDr(S, W, 52, 0x391c0cb3c5c95a63ULL);
|
||||
RNDr(S, W, 53, 0x4ed8aa4ae3418acbULL);
|
||||
RNDr(S, W, 54, 0x5b9cca4f7763e373ULL);
|
||||
RNDr(S, W, 55, 0x682e6ff3d6b2b8a3ULL);
|
||||
RNDr(S, W, 56, 0x748f82ee5defb2fcULL);
|
||||
RNDr(S, W, 57, 0x78a5636f43172f60ULL);
|
||||
RNDr(S, W, 58, 0x84c87814a1f0ab72ULL);
|
||||
RNDr(S, W, 59, 0x8cc702081a6439ecULL);
|
||||
RNDr(S, W, 60, 0x90befffa23631e28ULL);
|
||||
RNDr(S, W, 61, 0xa4506cebde82bde9ULL);
|
||||
RNDr(S, W, 62, 0xbef9a3f7b2c67915ULL);
|
||||
RNDr(S, W, 63, 0xc67178f2e372532bULL);
|
||||
RNDr(S, W, 64, 0xca273eceea26619cULL);
|
||||
RNDr(S, W, 65, 0xd186b8c721c0c207ULL);
|
||||
RNDr(S, W, 66, 0xeada7dd6cde0eb1eULL);
|
||||
RNDr(S, W, 67, 0xf57d4f7fee6ed178ULL);
|
||||
RNDr(S, W, 68, 0x06f067aa72176fbaULL);
|
||||
RNDr(S, W, 69, 0x0a637dc5a2c898a6ULL);
|
||||
RNDr(S, W, 70, 0x113f9804bef90daeULL);
|
||||
RNDr(S, W, 71, 0x1b710b35131c471bULL);
|
||||
RNDr(S, W, 72, 0x28db77f523047d84ULL);
|
||||
RNDr(S, W, 73, 0x32caab7b40c72493ULL);
|
||||
RNDr(S, W, 74, 0x3c9ebe0a15c9bebcULL);
|
||||
RNDr(S, W, 75, 0x431d67c49c100d4cULL);
|
||||
RNDr(S, W, 76, 0x4cc5d4becb3e42b6ULL);
|
||||
RNDr(S, W, 77, 0x597f299cfc657e2aULL);
|
||||
RNDr(S, W, 78, 0x5fcb6fab3ad6faecULL);
|
||||
RNDr(S, W, 79, 0x6c44198c4a475817ULL);
|
||||
|
||||
/* 4. Mix local working variables into global state */
|
||||
for (i = 0; i < 8; i++)
|
||||
state[i] += S[i];
|
||||
}
|
||||
|
||||
static unsigned char PAD[128] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/* Add padding and terminating bit-count. */
|
||||
static void
|
||||
SHA512_Pad(SHA512_CTX * ctx)
|
||||
{
|
||||
unsigned char len[16];
|
||||
uint64_t r, plen;
|
||||
|
||||
/*
|
||||
* Convert length to a vector of bytes -- we do this now rather
|
||||
* than later because the length will change after we pad.
|
||||
*/
|
||||
be64enc_vect(len, ctx->count, 16);
|
||||
|
||||
/* Add 1--128 bytes so that the resulting length is 112 mod 128 */
|
||||
r = (ctx->count[1] >> 3) & 0x7f;
|
||||
plen = (r < 112) ? (112 - r) : (240 - r);
|
||||
SHA512_Update(ctx, PAD, (size_t)plen);
|
||||
|
||||
/* Add the terminating bit-count */
|
||||
SHA512_Update(ctx, len, 16);
|
||||
}
|
||||
|
||||
/* SHA-512 initialization. Begins a SHA-512 operation. */
|
||||
void
|
||||
SHA512_Init(SHA512_CTX * ctx)
|
||||
{
|
||||
|
||||
/* Zero bits processed so far */
|
||||
ctx->count[0] = ctx->count[1] = 0;
|
||||
|
||||
/* Magic initialization constants */
|
||||
ctx->state[0] = 0x6a09e667f3bcc908ULL;
|
||||
ctx->state[1] = 0xbb67ae8584caa73bULL;
|
||||
ctx->state[2] = 0x3c6ef372fe94f82bULL;
|
||||
ctx->state[3] = 0xa54ff53a5f1d36f1ULL;
|
||||
ctx->state[4] = 0x510e527fade682d1ULL;
|
||||
ctx->state[5] = 0x9b05688c2b3e6c1fULL;
|
||||
ctx->state[6] = 0x1f83d9abfb41bd6bULL;
|
||||
ctx->state[7] = 0x5be0cd19137e2179ULL;
|
||||
}
|
||||
|
||||
/* Add bytes into the hash */
|
||||
void
|
||||
SHA512_Update(SHA512_CTX * ctx, const void *in, size_t len)
|
||||
{
|
||||
uint64_t bitlen[2];
|
||||
uint64_t r;
|
||||
const unsigned char *src = in;
|
||||
|
||||
/* Number of bytes left in the buffer from previous updates */
|
||||
r = (ctx->count[1] >> 3) & 0x7f;
|
||||
|
||||
/* Convert the length into a number of bits */
|
||||
bitlen[1] = ((uint64_t)len) << 3;
|
||||
bitlen[0] = ((uint64_t)len) >> 61;
|
||||
|
||||
/* Update number of bits */
|
||||
if ((ctx->count[1] += bitlen[1]) < bitlen[1])
|
||||
ctx->count[0]++;
|
||||
ctx->count[0] += bitlen[0];
|
||||
|
||||
/* Handle the case where we don't need to perform any transforms */
|
||||
if (len < 128 - r) {
|
||||
memcpy(&ctx->buf[r], src, len);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Finish the current block */
|
||||
memcpy(&ctx->buf[r], src, 128 - r);
|
||||
SHA512_Transform(ctx->state, ctx->buf);
|
||||
src += 128 - r;
|
||||
len -= 128 - r;
|
||||
|
||||
/* Perform complete blocks */
|
||||
while (len >= 128) {
|
||||
SHA512_Transform(ctx->state, src);
|
||||
src += 128;
|
||||
len -= 128;
|
||||
}
|
||||
|
||||
/* Copy left over data into buffer */
|
||||
memcpy(ctx->buf, src, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-512 finalization. Pads the input data, exports the hash value,
|
||||
* and clears the context state.
|
||||
*/
|
||||
void
|
||||
SHA512_Final(unsigned char digest[64], SHA512_CTX * ctx)
|
||||
{
|
||||
|
||||
/* Add padding */
|
||||
SHA512_Pad(ctx);
|
||||
|
||||
/* Write the hash */
|
||||
be64enc_vect(digest, ctx->state, 64);
|
||||
|
||||
/* Clear the context state */
|
||||
memset((void *)ctx, 0, sizeof(*ctx));
|
||||
}
|
@@ -8,4 +8,5 @@ Description: Utility functions from BSD systems (overlay)
|
||||
Version: @VERSION@
|
||||
URL: https://libbsd.freedesktop.org/
|
||||
Libs: -L${libdir} -lbsd
|
||||
Libs.private: @LIBBSD_LIBS@ @MD5_LIBS@
|
||||
Cflags: -isystem ${includedir}/bsd -DLIBBSD_OVERLAY
|
||||
|
@@ -35,6 +35,8 @@ LIBBSD_0.0 {
|
||||
strunvis;
|
||||
strunvisx;
|
||||
|
||||
/* The following functions are provided via the system libc or libmd
|
||||
* implementations. */
|
||||
MD5Init;
|
||||
MD5Update;
|
||||
MD5Pad;
|
||||
@@ -181,3 +183,15 @@ LIBBSD_0.10.0 {
|
||||
err;
|
||||
errx;
|
||||
} LIBBSD_0.9.1;
|
||||
|
||||
LIBBSD_0.11.0 {
|
||||
strnvisx;
|
||||
|
||||
recallocarray;
|
||||
freezero;
|
||||
|
||||
gid_from_group;
|
||||
group_from_gid;
|
||||
uid_from_user;
|
||||
user_from_uid;
|
||||
} LIBBSD_0.10.0;
|
||||
|
@@ -8,4 +8,5 @@ Description: Utility functions from BSD systems
|
||||
Version: @VERSION@
|
||||
URL: https://libbsd.freedesktop.org/
|
||||
Libs: -L${libdir} -lbsd
|
||||
Libs.private: @LIBBSD_LIBS@ @MD5_LIBS@
|
||||
Cflags: -I${includedir}
|
||||
|
@@ -45,7 +45,7 @@
|
||||
#define ELF_TARG_CLASS ELFCLASS64
|
||||
#define ELF_TARG_DATA ELFDATA2LSB
|
||||
|
||||
#elif defined(__amd64__)
|
||||
#elif defined(__amd64__) || defined(__x86_64__)
|
||||
|
||||
#define ELF_TARG_MACH EM_X86_64
|
||||
#if defined(__ILP32__)
|
||||
|
@@ -27,21 +27,44 @@
|
||||
#ifndef LIBBSD_LOCAL_LINK_H
|
||||
#define LIBBSD_LOCAL_LINK_H
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#define libbsd_link_warning(symbol, msg) \
|
||||
static const char libbsd_emit_link_warning_##symbol[] \
|
||||
__attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg;
|
||||
__attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg
|
||||
|
||||
#ifdef __ELF__
|
||||
# if __has_attribute(symver)
|
||||
/* The symver attribute is supported since gcc 10.x. */
|
||||
#define libbsd_symver_default(alias, symbol, version) \
|
||||
extern __typeof__(symbol) symbol \
|
||||
__attribute__((__symver__(#alias "@@" #version)))
|
||||
#define libbsd_symver_variant(alias, symbol, version) \
|
||||
extern __typeof__(symbol) symbol \
|
||||
__attribute__((__symver__(#alias "@" #version)))
|
||||
|
||||
#define libbsd_symver_weak(alias, symbol, version) \
|
||||
extern __typeof__(symbol) symbol \
|
||||
__attribute__((__symver__(#alias "@" #version), __weak__))
|
||||
# else
|
||||
#define libbsd_symver_default(alias, symbol, version) \
|
||||
__asm__(".symver " #symbol "," #alias "@@" #version)
|
||||
|
||||
#define libbsd_symver_variant(alias, symbol, version) \
|
||||
__asm__(".symver " #symbol "," #alias "@" #version)
|
||||
|
||||
#define libbsd_symver_weak(alias, symbol, version) \
|
||||
libbsd_symver_variant(alias, symbol, version); \
|
||||
extern __typeof__(symbol) alias \
|
||||
__attribute__((__weak__))
|
||||
# endif
|
||||
#else
|
||||
#define libbsd_symver_default(alias, symbol, version) \
|
||||
extern __typeof(symbol) alias __attribute__((__alias__(#symbol)))
|
||||
extern __typeof__(symbol) alias __attribute__((__alias__(#symbol)))
|
||||
|
||||
#define libbsd_symver_variant(alias, symbol, version)
|
||||
|
||||
#define libbsd_symver_weak(alias, symbol, version)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
120
src/md5.c
Normal file
120
src/md5.c
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright © 2021 Guillem Jover <guillem@hadrons.org>
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR 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 <md5.h>
|
||||
#include "local-link.h"
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
void
|
||||
libbsd_MD5Init(MD5_CTX *context)
|
||||
{
|
||||
MD5Init(context);
|
||||
}
|
||||
libbsd_link_warning(MD5Init,
|
||||
"The MD5Init() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5Init, libbsd_MD5Init, LIBBSD_0.0);
|
||||
|
||||
void
|
||||
libbsd_MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
|
||||
{
|
||||
MD5Update(context, data, len);
|
||||
}
|
||||
libbsd_link_warning(MD5Update,
|
||||
"The MD5Update() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5Update, libbsd_MD5Update, LIBBSD_0.0);
|
||||
|
||||
void
|
||||
libbsd_MD5Pad(MD5_CTX *context)
|
||||
{
|
||||
MD5Pad(context);
|
||||
}
|
||||
libbsd_link_warning(MD5Pad,
|
||||
"The MD5Pad() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5Pad, libbsd_MD5Pad, LIBBSD_0.0);
|
||||
|
||||
void
|
||||
libbsd_MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
|
||||
{
|
||||
MD5Final(digest, context);
|
||||
}
|
||||
libbsd_link_warning(MD5Final,
|
||||
"The MD5Final() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5Final, libbsd_MD5Final, LIBBSD_0.0);
|
||||
|
||||
void
|
||||
libbsd_MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
||||
{
|
||||
MD5Transform(state, block);
|
||||
}
|
||||
libbsd_link_warning(MD5Transform,
|
||||
"The MD5Transform() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5Transform, libbsd_MD5Transform, LIBBSD_0.0);
|
||||
|
||||
char *
|
||||
libbsd_MD5End(MD5_CTX *context, char *buf)
|
||||
{
|
||||
return MD5End(context, buf);
|
||||
}
|
||||
libbsd_link_warning(MD5End,
|
||||
"The MD5End() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5End, libbsd_MD5End, LIBBSD_0.0);
|
||||
|
||||
char *
|
||||
libbsd_MD5File(const char *filename, char *buf)
|
||||
{
|
||||
return MD5File(filename, buf);
|
||||
}
|
||||
libbsd_link_warning(MD5File,
|
||||
"The MD5File() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5File, libbsd_MD5File, LIBBSD_0.0);
|
||||
|
||||
char *
|
||||
libbsd_MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
|
||||
{
|
||||
return MD5FileChunk(filename, buf, offset, length);
|
||||
}
|
||||
libbsd_link_warning(MD5FileChunk,
|
||||
"The MD5FileChunk() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5FileChunk, libbsd_MD5FileChunk, LIBBSD_0.0);
|
||||
|
||||
char *
|
||||
libbsd_MD5Data(const uint8_t *data, size_t len, char *buf)
|
||||
{
|
||||
return MD5Data(data, len, buf);
|
||||
}
|
||||
libbsd_link_warning(MD5Data,
|
||||
"The MD5Data() function in libbsd is a deprecated wrapper, "
|
||||
"use libmd instead.");
|
||||
libbsd_symver_weak(MD5Data, libbsd_MD5Data, LIBBSD_0.0);
|
@@ -43,8 +43,6 @@
|
||||
|
||||
#include "local-elf.h"
|
||||
|
||||
#define SIZE_T_MAX 0xffffffffU
|
||||
|
||||
/* Note: This function is used by libkvm0, so we need to export it.
|
||||
* It is not declared in the include files though. */
|
||||
int __fdnlist(int, struct nlist *);
|
||||
@@ -151,7 +149,7 @@ __fdnlist(int fd, struct nlist *list)
|
||||
shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
|
||||
|
||||
/* Make sure it's not too big to mmap */
|
||||
if (shdr_size > SIZE_T_MAX || shdr_size > st.st_size) {
|
||||
if (shdr_size > st.st_size) {
|
||||
errno = EFBIG;
|
||||
return (-1);
|
||||
}
|
||||
@@ -184,7 +182,7 @@ __fdnlist(int fd, struct nlist *list)
|
||||
}
|
||||
|
||||
/* Check for files too large to mmap. */
|
||||
if (symstrsize > SIZE_T_MAX || symstrsize > st.st_size) {
|
||||
if (symstrsize > st.st_size) {
|
||||
errno = EFBIG;
|
||||
goto done;
|
||||
}
|
||||
@@ -237,7 +235,6 @@ __fdnlist(int fd, struct nlist *list)
|
||||
for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
|
||||
char *name;
|
||||
Elf_Word size;
|
||||
struct nlist *p;
|
||||
|
||||
name = strtab + s->st_name;
|
||||
if (name[0] == '\0')
|
||||
|
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Copyright © 2006 Robert Millan
|
||||
* Copyright © 2010-2012 Guillem Jover <guillem@hadrons.org>
|
||||
* Copyright © 2018 Facebook, Inc.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -30,11 +31,16 @@
|
||||
* <https://sourceware.org/ml/libc-alpha/2006-03/msg00125.html>.
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#include <shlwapi.h>
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
|
||||
#ifdef _WIN32
|
||||
#define LIBBSD_IS_PATHNAME_SEPARATOR(c) ((c) == '/' || (c) == '\\')
|
||||
#else
|
||||
#define LIBBSD_IS_PATHNAME_SEPARATOR(c) ((c) == '/')
|
||||
@@ -56,6 +62,70 @@ getprogname(void)
|
||||
/* getexecname(3) returns an absolute pathname, normalize it. */
|
||||
if (__progname == NULL)
|
||||
setprogname(getexecname());
|
||||
#elif defined(_WIN32)
|
||||
if (__progname == NULL) {
|
||||
WCHAR *wpath = NULL;
|
||||
WCHAR *wname = NULL;
|
||||
WCHAR *wext = NULL;
|
||||
DWORD wpathsiz = MAX_PATH / 2;
|
||||
DWORD len, i;
|
||||
char *mbname = NULL;
|
||||
int mbnamesiz;
|
||||
|
||||
/* Use the Unicode version of this function to support long
|
||||
* paths. MAX_PATH isn't actually the maximum length of a
|
||||
* path in this case. */
|
||||
do {
|
||||
WCHAR *wpathnew;
|
||||
|
||||
wpathsiz *= 2;
|
||||
wpathsiz = MIN(wpathsiz, UNICODE_STRING_MAX_CHARS);
|
||||
wpathnew = reallocarray(wpath, wpathsiz, sizeof(*wpath));
|
||||
if (wpathnew == NULL)
|
||||
goto done;
|
||||
wpath = wpathnew;
|
||||
|
||||
len = GetModuleFileNameW(NULL, wpath, wpathsiz);
|
||||
if (wpathsiz == UNICODE_STRING_MAX_CHARS)
|
||||
goto done;
|
||||
} while (wpathsiz == len);
|
||||
if (len == 0)
|
||||
goto done;
|
||||
|
||||
/* GetModuleFileNameW() retrieves an absolute path. Locate the
|
||||
* filename now to only convert necessary characters and save
|
||||
* memory. */
|
||||
wname = wpath;
|
||||
for (i = len; i > 0; i--) {
|
||||
if (LIBBSD_IS_PATHNAME_SEPARATOR(wpath[i - 1])) {
|
||||
wname = wpath + i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove any trailing extension, such as '.exe', to make the
|
||||
* behavior mach the non-Windows systems. */
|
||||
wext = PathFindExtensionW(wname);
|
||||
wext[0] = '\0';
|
||||
|
||||
mbnamesiz = WideCharToMultiByte(CP_UTF8, 0, wname, -1, NULL,
|
||||
0, NULL, NULL);
|
||||
if (mbnamesiz == 0)
|
||||
goto done;
|
||||
mbname = malloc(mbnamesiz);
|
||||
if (mbname == NULL)
|
||||
goto done;
|
||||
mbnamesiz = WideCharToMultiByte(CP_UTF8, 0, wname, -1, mbname,
|
||||
mbnamesiz, NULL, NULL);
|
||||
if (mbnamesiz == 0)
|
||||
goto done;
|
||||
__progname = mbname;
|
||||
mbname = NULL;
|
||||
|
||||
done:
|
||||
free(wpath);
|
||||
free(mbname);
|
||||
}
|
||||
#endif
|
||||
|
||||
return __progname;
|
||||
|
437
src/pwcache.c
Normal file
437
src/pwcache.c
Normal file
@@ -0,0 +1,437 @@
|
||||
/* $OpenBSD: pwcache.c,v 1.15 2018/09/22 02:47:23 millert Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1992 Keith Muller.
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Keith Muller of the University of California, San Diego.
|
||||
*
|
||||
* 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 <assert.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* Constants and data structures used to implement group and password file
|
||||
* caches. Name lengths have been chosen to be as large as those supported
|
||||
* by the passwd and group files as well as the standard archive formats.
|
||||
* CACHE SIZES MUST BE PRIME
|
||||
*/
|
||||
#define UNMLEN 32 /* >= user name found in any protocol */
|
||||
#define GNMLEN 32 /* >= group name found in any protocol */
|
||||
#define UID_SZ 317 /* size of uid to user_name cache */
|
||||
#define UNM_SZ 317 /* size of user_name to uid cache */
|
||||
#define GID_SZ 251 /* size of gid to group_name cache */
|
||||
#define GNM_SZ 251 /* size of group_name to gid cache */
|
||||
#define VALID 1 /* entry and name are valid */
|
||||
#define INVALID 2 /* entry valid, name NOT valid */
|
||||
|
||||
/*
|
||||
* Node structures used in the user, group, uid, and gid caches.
|
||||
*/
|
||||
|
||||
typedef struct uidc {
|
||||
int valid; /* is this a valid or a miss entry */
|
||||
char name[UNMLEN]; /* uid name */
|
||||
uid_t uid; /* cached uid */
|
||||
} UIDC;
|
||||
|
||||
typedef struct gidc {
|
||||
int valid; /* is this a valid or a miss entry */
|
||||
char name[GNMLEN]; /* gid name */
|
||||
gid_t gid; /* cached gid */
|
||||
} GIDC;
|
||||
|
||||
/*
|
||||
* Routines that control user, group, uid and gid caches.
|
||||
* Traditional passwd/group cache routines perform quite poorly with
|
||||
* archives. The chances of hitting a valid lookup with an archive is quite a
|
||||
* bit worse than with files already resident on the file system. These misses
|
||||
* create a MAJOR performance cost. To adress this problem, these routines
|
||||
* cache both hits and misses.
|
||||
*/
|
||||
|
||||
static UIDC **uidtb; /* uid to name cache */
|
||||
static GIDC **gidtb; /* gid to name cache */
|
||||
static UIDC **usrtb; /* user name to uid cache */
|
||||
static GIDC **grptb; /* group name to gid cache */
|
||||
|
||||
static u_int
|
||||
st_hash(const char *name, size_t len, int tabsz)
|
||||
{
|
||||
u_int key = 0;
|
||||
|
||||
assert(name != NULL);
|
||||
|
||||
while (len--) {
|
||||
key += *name++;
|
||||
key = (key << 8) | (key >> 24);
|
||||
}
|
||||
|
||||
return key % tabsz;
|
||||
}
|
||||
|
||||
/*
|
||||
* uidtb_start
|
||||
* creates an an empty uidtb
|
||||
* Return:
|
||||
* 0 if ok, -1 otherwise
|
||||
*/
|
||||
static int
|
||||
uidtb_start(void)
|
||||
{
|
||||
static int fail = 0;
|
||||
|
||||
if (uidtb != NULL)
|
||||
return 0;
|
||||
if (fail)
|
||||
return -1;
|
||||
if ((uidtb = calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
|
||||
++fail;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* gidtb_start
|
||||
* creates an an empty gidtb
|
||||
* Return:
|
||||
* 0 if ok, -1 otherwise
|
||||
*/
|
||||
static int
|
||||
gidtb_start(void)
|
||||
{
|
||||
static int fail = 0;
|
||||
|
||||
if (gidtb != NULL)
|
||||
return 0;
|
||||
if (fail)
|
||||
return -1;
|
||||
if ((gidtb = calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
|
||||
++fail;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* usrtb_start
|
||||
* creates an an empty usrtb
|
||||
* Return:
|
||||
* 0 if ok, -1 otherwise
|
||||
*/
|
||||
static int
|
||||
usrtb_start(void)
|
||||
{
|
||||
static int fail = 0;
|
||||
|
||||
if (usrtb != NULL)
|
||||
return 0;
|
||||
if (fail)
|
||||
return -1;
|
||||
if ((usrtb = calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
|
||||
++fail;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* grptb_start
|
||||
* creates an an empty grptb
|
||||
* Return:
|
||||
* 0 if ok, -1 otherwise
|
||||
*/
|
||||
static int
|
||||
grptb_start(void)
|
||||
{
|
||||
static int fail = 0;
|
||||
|
||||
if (grptb != NULL)
|
||||
return 0;
|
||||
if (fail)
|
||||
return -1;
|
||||
if ((grptb = calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
|
||||
++fail;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* user_from_uid()
|
||||
* caches the name (if any) for the uid. If noname clear, we always
|
||||
* return the stored name (if valid or invalid match).
|
||||
* We use a simple hash table.
|
||||
* Return:
|
||||
* Pointer to stored name (or a empty string)
|
||||
*/
|
||||
const char *
|
||||
user_from_uid(uid_t uid, int noname)
|
||||
{
|
||||
struct passwd pwstore, *pw = NULL;
|
||||
char pwbuf[_PW_BUF_LEN];
|
||||
UIDC **pptr, *ptr = NULL;
|
||||
|
||||
if ((uidtb != NULL) || (uidtb_start() == 0)) {
|
||||
/*
|
||||
* see if we have this uid cached
|
||||
*/
|
||||
pptr = uidtb + (uid % UID_SZ);
|
||||
ptr = *pptr;
|
||||
|
||||
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
|
||||
/*
|
||||
* have an entry for this uid
|
||||
*/
|
||||
if (!noname || (ptr->valid == VALID))
|
||||
return ptr->name;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ptr == NULL)
|
||||
*pptr = ptr = malloc(sizeof(UIDC));
|
||||
}
|
||||
|
||||
getpwuid_r(uid, &pwstore, pwbuf, sizeof(pwbuf), &pw);
|
||||
if (pw == NULL) {
|
||||
/*
|
||||
* no match for this uid in the local password file
|
||||
* a string that is the uid in numeric format
|
||||
*/
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
ptr->uid = uid;
|
||||
(void)snprintf(ptr->name, UNMLEN, "%u", uid);
|
||||
ptr->valid = INVALID;
|
||||
if (noname)
|
||||
return NULL;
|
||||
} else {
|
||||
/*
|
||||
* there is an entry for this uid in the password file
|
||||
*/
|
||||
if (ptr == NULL)
|
||||
return pw->pw_name;
|
||||
ptr->uid = uid;
|
||||
(void)strlcpy(ptr->name, pw->pw_name, sizeof(ptr->name));
|
||||
ptr->valid = VALID;
|
||||
}
|
||||
return ptr->name;
|
||||
}
|
||||
|
||||
/*
|
||||
* group_from_gid()
|
||||
* caches the name (if any) for the gid. If noname clear, we always
|
||||
* return the stored name (if valid or invalid match).
|
||||
* We use a simple hash table.
|
||||
* Return:
|
||||
* Pointer to stored name (or a empty string)
|
||||
*/
|
||||
const char *
|
||||
group_from_gid(gid_t gid, int noname)
|
||||
{
|
||||
struct group grstore, *gr = NULL;
|
||||
char grbuf[_GR_BUF_LEN];
|
||||
GIDC **pptr, *ptr = NULL;
|
||||
|
||||
if ((gidtb != NULL) || (gidtb_start() == 0)) {
|
||||
/*
|
||||
* see if we have this gid cached
|
||||
*/
|
||||
pptr = gidtb + (gid % GID_SZ);
|
||||
ptr = *pptr;
|
||||
|
||||
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
|
||||
/*
|
||||
* have an entry for this gid
|
||||
*/
|
||||
if (!noname || (ptr->valid == VALID))
|
||||
return ptr->name;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ptr == NULL)
|
||||
*pptr = ptr = malloc(sizeof(GIDC));
|
||||
}
|
||||
|
||||
getgrgid_r(gid, &grstore, grbuf, sizeof(grbuf), &gr);
|
||||
if (gr == NULL) {
|
||||
/*
|
||||
* no match for this gid in the local group file, put in
|
||||
* a string that is the gid in numeric format
|
||||
*/
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
ptr->gid = gid;
|
||||
(void)snprintf(ptr->name, GNMLEN, "%u", gid);
|
||||
ptr->valid = INVALID;
|
||||
if (noname)
|
||||
return NULL;
|
||||
} else {
|
||||
/*
|
||||
* there is an entry for this group in the group file
|
||||
*/
|
||||
if (ptr == NULL)
|
||||
return gr->gr_name;
|
||||
ptr->gid = gid;
|
||||
(void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name));
|
||||
ptr->valid = VALID;
|
||||
}
|
||||
return ptr->name;
|
||||
}
|
||||
|
||||
/*
|
||||
* uid_from_user()
|
||||
* caches the uid for a given user name. We use a simple hash table.
|
||||
* Return:
|
||||
* 0 if the user name is found (filling in uid), -1 otherwise
|
||||
*/
|
||||
int
|
||||
uid_from_user(const char *name, uid_t *uid)
|
||||
{
|
||||
struct passwd pwstore, *pw = NULL;
|
||||
char pwbuf[_PW_BUF_LEN];
|
||||
UIDC **pptr, *ptr = NULL;
|
||||
size_t namelen;
|
||||
|
||||
/*
|
||||
* return -1 for mangled names
|
||||
*/
|
||||
if (name == NULL || ((namelen = strlen(name)) == 0))
|
||||
return -1;
|
||||
|
||||
if ((usrtb != NULL) || (usrtb_start() == 0)) {
|
||||
/*
|
||||
* look up in hash table, if found and valid return the uid,
|
||||
* if found and invalid, return a -1
|
||||
*/
|
||||
pptr = usrtb + st_hash(name, namelen, UNM_SZ);
|
||||
ptr = *pptr;
|
||||
|
||||
if ((ptr != NULL) && (ptr->valid > 0) &&
|
||||
strcmp(name, ptr->name) == 0) {
|
||||
if (ptr->valid == INVALID)
|
||||
return -1;
|
||||
*uid = ptr->uid;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ptr == NULL)
|
||||
*pptr = ptr = malloc(sizeof(UIDC));
|
||||
}
|
||||
|
||||
/*
|
||||
* no match, look it up, if no match store it as an invalid entry,
|
||||
* or store the matching uid
|
||||
*/
|
||||
getpwnam_r(name, &pwstore, pwbuf, sizeof(pwbuf), &pw);
|
||||
if (ptr == NULL) {
|
||||
if (pw == NULL)
|
||||
return -1;
|
||||
*uid = pw->pw_uid;
|
||||
return 0;
|
||||
}
|
||||
(void)strlcpy(ptr->name, name, sizeof(ptr->name));
|
||||
if (pw == NULL) {
|
||||
ptr->valid = INVALID;
|
||||
return -1;
|
||||
}
|
||||
ptr->valid = VALID;
|
||||
*uid = ptr->uid = pw->pw_uid;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* gid_from_group()
|
||||
* caches the gid for a given group name. We use a simple hash table.
|
||||
* Return:
|
||||
* 0 if the group name is found (filling in gid), -1 otherwise
|
||||
*/
|
||||
int
|
||||
gid_from_group(const char *name, gid_t *gid)
|
||||
{
|
||||
struct group grstore, *gr = NULL;
|
||||
char grbuf[_GR_BUF_LEN];
|
||||
GIDC **pptr, *ptr = NULL;
|
||||
size_t namelen;
|
||||
|
||||
/*
|
||||
* return -1 for mangled names
|
||||
*/
|
||||
if (name == NULL || ((namelen = strlen(name)) == 0))
|
||||
return -1;
|
||||
|
||||
if ((grptb != NULL) || (grptb_start() == 0)) {
|
||||
/*
|
||||
* look up in hash table, if found and valid return the uid,
|
||||
* if found and invalid, return a -1
|
||||
*/
|
||||
pptr = grptb + st_hash(name, namelen, GID_SZ);
|
||||
ptr = *pptr;
|
||||
|
||||
if ((ptr != NULL) && (ptr->valid > 0) &&
|
||||
strcmp(name, ptr->name) == 0) {
|
||||
if (ptr->valid == INVALID)
|
||||
return -1;
|
||||
*gid = ptr->gid;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ptr == NULL)
|
||||
*pptr = ptr = malloc(sizeof(GIDC));
|
||||
}
|
||||
|
||||
/*
|
||||
* no match, look it up, if no match store it as an invalid entry,
|
||||
* or store the matching gid
|
||||
*/
|
||||
getgrnam_r(name, &grstore, grbuf, sizeof(grbuf), &gr);
|
||||
if (ptr == NULL) {
|
||||
if (gr == NULL)
|
||||
return -1;
|
||||
*gid = gr->gr_gid;
|
||||
return 0;
|
||||
}
|
||||
|
||||
(void)strlcpy(ptr->name, name, sizeof(ptr->name));
|
||||
if (gr == NULL) {
|
||||
ptr->valid = INVALID;
|
||||
return -1;
|
||||
}
|
||||
ptr->valid = VALID;
|
||||
*gid = ptr->gid = gr->gr_gid;
|
||||
return 0;
|
||||
}
|
80
src/recallocarray.c
Normal file
80
src/recallocarray.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */
|
||||
/*
|
||||
* Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
|
||||
*
|
||||
* 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 <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
|
||||
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
|
||||
*/
|
||||
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
|
||||
|
||||
void *
|
||||
recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
|
||||
{
|
||||
size_t oldsize, newsize;
|
||||
void *newptr;
|
||||
|
||||
if (ptr == NULL)
|
||||
return calloc(newnmemb, size);
|
||||
|
||||
if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
|
||||
newnmemb > 0 && SIZE_MAX / newnmemb < size) {
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
newsize = newnmemb * size;
|
||||
|
||||
if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
|
||||
oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
oldsize = oldnmemb * size;
|
||||
|
||||
/*
|
||||
* Don't bother too much if we're shrinking just a bit,
|
||||
* we do not shrink for series of small steps, oh well.
|
||||
*/
|
||||
if (newsize <= oldsize) {
|
||||
size_t d = oldsize - newsize;
|
||||
|
||||
if (d < oldsize / 2 && d < (size_t)getpagesize()) {
|
||||
memset((char *)ptr + newsize, 0, d);
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
|
||||
newptr = malloc(newsize);
|
||||
if (newptr == NULL)
|
||||
return NULL;
|
||||
|
||||
if (newsize > oldsize) {
|
||||
memcpy(newptr, ptr, oldsize);
|
||||
memset((char *)newptr + oldsize, 0, newsize - oldsize);
|
||||
} else
|
||||
memcpy(newptr, ptr, newsize);
|
||||
|
||||
explicit_bzero(ptr, oldsize);
|
||||
free(ptr);
|
||||
|
||||
return newptr;
|
||||
}
|
@@ -222,6 +222,10 @@ setproctitle_init(int argc, char *argv[], char *envp[])
|
||||
#define SPT_MAXTITLE 255
|
||||
#endif
|
||||
|
||||
__printflike(1, 2)
|
||||
void
|
||||
setproctitle_impl(const char *fmt, ...);
|
||||
|
||||
void
|
||||
setproctitle_impl(const char *fmt, ...)
|
||||
{
|
||||
|
@@ -49,4 +49,4 @@
|
||||
* move them from .ctors to .init_array.
|
||||
*/
|
||||
void (*libbsd_init_func)(int argc, char *argv[], char *envp[])
|
||||
__attribute__((__section__(".init_array"))) = setproctitle_init;
|
||||
__attribute__((__section__(".init_array"), __used__)) = setproctitle_init;
|
||||
|
@@ -564,12 +564,16 @@ strunvis(char *dst, const char *src)
|
||||
* NetBSD: 2012, strnunvis(char *dst, size_t dlen, const char *src);
|
||||
*/
|
||||
ssize_t
|
||||
strnunvis_openbsd(char *, const char *, size_t);
|
||||
ssize_t
|
||||
strnunvis_openbsd(char *dst, const char *src, size_t dlen)
|
||||
{
|
||||
return strnunvisx(dst, dlen, src, 0);
|
||||
}
|
||||
libbsd_symver_default(strnunvis, strnunvis_openbsd, LIBBSD_0.2);
|
||||
|
||||
int
|
||||
strnunvis_netbsd(char *, size_t, const char *);
|
||||
int
|
||||
strnunvis_netbsd(char *dst, size_t dlen, const char *src)
|
||||
{
|
||||
|
@@ -733,12 +733,16 @@ strvis(char *mbdst, const char *mbsrc, int flags)
|
||||
* NetBSD: 2012, strnvis(char *dst, size_t dlen, const char *src, int flag);
|
||||
*/
|
||||
int
|
||||
strnvis_openbsd(char *, const char *, size_t, int);
|
||||
int
|
||||
strnvis_openbsd(char *mbdst, const char *mbsrc, size_t dlen, int flags)
|
||||
{
|
||||
return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
|
||||
}
|
||||
libbsd_symver_default(strnvis, strnvis_openbsd, LIBBSD_0.2);
|
||||
|
||||
int
|
||||
strnvis_netbsd(char *, size_t, const char *, int);
|
||||
int
|
||||
strnvis_netbsd(char *mbdst, size_t dlen, const char *mbsrc, int flags)
|
||||
{
|
||||
|
1
test/.gitignore
vendored
1
test/.gitignore
vendored
@@ -15,6 +15,7 @@ overlay
|
||||
proctitle-init
|
||||
proctitle
|
||||
progname
|
||||
pwcache
|
||||
setmode
|
||||
strl
|
||||
strmode
|
||||
|
@@ -41,8 +41,10 @@ check_PROGRAMS = \
|
||||
fpurge \
|
||||
md5 \
|
||||
nlist \
|
||||
proctitle \
|
||||
proctitle-init \
|
||||
progname \
|
||||
pwcache \
|
||||
setmode \
|
||||
strl \
|
||||
strmode \
|
||||
@@ -67,6 +69,11 @@ proctitle_LDFLAGS = \
|
||||
check_PROGRAMS += proctitle
|
||||
endif
|
||||
|
||||
if NEED_TRANSPARENT_LIBMD
|
||||
# On the installed system this is handled via the ld script.
|
||||
md5_LDADD = $(LDADD) $(MD5_LIBS)
|
||||
endif
|
||||
|
||||
fgetln_SOURCES = test-stream.c test-stream.h fgetln.c
|
||||
fgetln_CFLAGS = -Wno-deprecated-declarations
|
||||
fparseln_SOURCES = test-stream.c test-stream.h fparseln.c
|
||||
|
@@ -28,7 +28,7 @@
|
||||
#include <string.h>
|
||||
|
||||
int
|
||||
main()
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
unsigned char array[40];
|
||||
size_t i;
|
||||
|
@@ -31,7 +31,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main()
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int fd;
|
||||
|
@@ -30,7 +30,7 @@
|
||||
#include <string.h>
|
||||
|
||||
int
|
||||
main()
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
unsigned char decstream[] = {
|
||||
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2013 Guillem Jover <guillem@hadrons.org>
|
||||
* Copyright © 2013-2015, 2020 Guillem Jover <guillem@hadrons.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -61,6 +61,7 @@ static const wchar_t *data_wide[] = {
|
||||
|
||||
struct file {
|
||||
FILE *fp;
|
||||
void *line_alloc;
|
||||
const void **lines;
|
||||
|
||||
const void *got_buf;
|
||||
@@ -74,7 +75,7 @@ test_fgetln_single(void)
|
||||
size_t len;
|
||||
int i;
|
||||
|
||||
fp = pipe_feed("%s", (const void **)data_ascii, DATA_LINES);
|
||||
fp = pipe_feed(PIPE_DATA_ASCII, (const void **)data_ascii, DATA_LINES);
|
||||
for (i = 0; i < DATA_LINES; i++) {
|
||||
char *str = fgetln(fp, &len);
|
||||
|
||||
@@ -95,12 +96,14 @@ test_fgetln_multi(void)
|
||||
char *str;
|
||||
|
||||
str = strdup("A\n");
|
||||
assert(str);
|
||||
str[0] += i;
|
||||
|
||||
files[i].line_alloc = str;
|
||||
files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
|
||||
files[i].lines[0] = str;
|
||||
files[i].lines[1] = str;
|
||||
files[i].fp = pipe_feed("%s", files[i].lines, LINE_COUNT);
|
||||
files[i].fp = pipe_feed(PIPE_DATA_ASCII, files[i].lines, LINE_COUNT);
|
||||
}
|
||||
|
||||
for (l = 0; l < LINE_COUNT; l++) {
|
||||
@@ -123,8 +126,11 @@ test_fgetln_multi(void)
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < LINE_COUNT; i++)
|
||||
for (i = 0; i < FILE_COUNT; i++) {
|
||||
free(files[i].line_alloc);
|
||||
free(files[i].lines);
|
||||
pipe_close(files[i].fp);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -134,7 +140,7 @@ test_fgetwln_single(void)
|
||||
size_t len;
|
||||
int i;
|
||||
|
||||
fp = pipe_feed("%ls", (const void **)data_wide, DATA_LINES);
|
||||
fp = pipe_feed(PIPE_DATA_WIDE, (const void **)data_wide, DATA_LINES);
|
||||
for (i = 0; i < DATA_LINES; i++) {
|
||||
wchar_t *wstr;
|
||||
|
||||
@@ -159,10 +165,11 @@ test_fgetwln_multi(void)
|
||||
wstr = wcsdup(L"A\n");
|
||||
wstr[0] += i;
|
||||
|
||||
files[i].line_alloc = wstr;
|
||||
files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
|
||||
files[i].lines[0] = wstr;
|
||||
files[i].lines[1] = wstr;
|
||||
files[i].fp = pipe_feed("%ls", files[i].lines, LINE_COUNT);
|
||||
files[i].fp = pipe_feed(PIPE_DATA_WIDE, files[i].lines, LINE_COUNT);
|
||||
}
|
||||
|
||||
for (l = 0; l < LINE_COUNT; l++) {
|
||||
@@ -185,8 +192,11 @@ test_fgetwln_multi(void)
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < LINE_COUNT; i++)
|
||||
for (i = 0; i < FILE_COUNT; i++) {
|
||||
free(files[i].line_alloc);
|
||||
free(files[i].lines);
|
||||
pipe_close(files[i].fp);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
|
@@ -68,7 +68,7 @@ test_fparseln(const char **data_expect, int flags)
|
||||
FILE *fp;
|
||||
size_t i, len, lineno = 0;
|
||||
|
||||
fp = pipe_feed("%s", (const void **)data_test, TEST_LINES);
|
||||
fp = pipe_feed(PIPE_DATA_ASCII, (const void **)data_test, TEST_LINES);
|
||||
for (i = 0; i < EXPECT_LINES; i++) {
|
||||
char *str = fparseln(fp, &len, &lineno, NULL, flags);
|
||||
|
||||
|
@@ -27,7 +27,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main()
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
static FILE fp_bad;
|
||||
FILE *fp;
|
||||
|
@@ -37,7 +37,7 @@ struct test_cookie {
|
||||
int index;
|
||||
};
|
||||
|
||||
int
|
||||
static int
|
||||
test_readfn(void *cookie, char *buf, int size)
|
||||
{
|
||||
struct test_cookie *tc = cookie;
|
||||
@@ -56,7 +56,7 @@ test_readfn(void *cookie, char *buf, int size)
|
||||
return size;
|
||||
}
|
||||
|
||||
int
|
||||
static int
|
||||
test_writefn(void *cookie, const char *buf, int size)
|
||||
{
|
||||
struct test_cookie *tc = cookie;
|
||||
@@ -75,7 +75,7 @@ test_writefn(void *cookie, const char *buf, int size)
|
||||
return size;
|
||||
}
|
||||
|
||||
off_t
|
||||
static off_t
|
||||
test_seekfn(void *cookie, off_t offset, int whence)
|
||||
{
|
||||
struct test_cookie *tc = cookie;
|
||||
@@ -95,7 +95,7 @@ test_seekfn(void *cookie, off_t offset, int whence)
|
||||
return tc->index;
|
||||
}
|
||||
|
||||
int
|
||||
static int
|
||||
test_closefn(void *cookie)
|
||||
{
|
||||
struct test_cookie *tc = cookie;
|
||||
@@ -114,7 +114,7 @@ main(int argc, char **argv)
|
||||
size_t i;
|
||||
|
||||
/* Test invalid hooks. */
|
||||
fp = funopen(&tc, NULL, NULL, NULL, NULL);
|
||||
fp = funopen(NULL, NULL, NULL, NULL, NULL);
|
||||
assert(fp == NULL);
|
||||
assert(errno == EINVAL);
|
||||
|
||||
|
@@ -28,7 +28,7 @@
|
||||
#include <md5.h>
|
||||
#include <string.h>
|
||||
|
||||
void
|
||||
static void
|
||||
test_md5(const char *digest, const char *string)
|
||||
{
|
||||
char result[MD5_DIGEST_STRING_LENGTH];
|
||||
@@ -37,7 +37,7 @@ test_md5(const char *digest, const char *string)
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
test_md5("d41d8cd98f00b204e9800998ecf8427e", "");
|
||||
test_md5("900150983cd24fb0d6963f7d28e17f72", "abc");
|
||||
|
16
test/nlist.c
16
test/nlist.c
@@ -39,11 +39,11 @@ extern int data_pub_uninit[2048];
|
||||
extern int *data_pub_ptr;
|
||||
|
||||
int *data_pub_ptr = &data_prv_init;
|
||||
int data_pub_init = 10;
|
||||
int data_pub_uninit[2048];
|
||||
int data_pub_init __attribute__((__used__)) = 10;
|
||||
int data_pub_uninit[2048] __attribute__((__used__));
|
||||
|
||||
extern int
|
||||
func_pub(void);
|
||||
func_pub(void) __attribute__((__used__)) ;
|
||||
|
||||
int
|
||||
func_pub(void)
|
||||
@@ -55,11 +55,11 @@ int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
struct nlist nl[] = {
|
||||
{ .n_un.n_name = "main" },
|
||||
{ .n_un.n_name = "func_pub" },
|
||||
{ .n_un.n_name = "data_pub_uninit" },
|
||||
{ .n_un.n_name = "data_pub_init" },
|
||||
{ .n_un.n_name = "data_prv_init" },
|
||||
{ .n_un.n_name = (char *)"main" },
|
||||
{ .n_un.n_name = (char *)"func_pub" },
|
||||
{ .n_un.n_name = (char *)"data_pub_uninit" },
|
||||
{ .n_un.n_name = (char *)"data_pub_init" },
|
||||
{ .n_un.n_name = (char *)"data_prv_init" },
|
||||
{ .n_un.n_name = NULL },
|
||||
};
|
||||
int rc;
|
||||
|
@@ -28,6 +28,9 @@
|
||||
* other headers through magic macros, to check that the overlay is working
|
||||
* properly. */
|
||||
#include <errno.h>
|
||||
#ifdef HAVE_PWD_H
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
#ifdef HAVE_GRP_H
|
||||
#include <grp.h>
|
||||
#endif
|
||||
@@ -42,7 +45,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int
|
||||
main()
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
/* Test that we do not get partial definitions. */
|
||||
fflush(stdout);
|
||||
|
49
test/pwcache.c
Normal file
49
test/pwcache.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright © 2021 Guillem Jover <guillem@hadrons.org>
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
* THE AUTHOR 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 <assert.h>
|
||||
#include <string.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
|
||||
assert(uid_from_user("root", &uid) == 0);
|
||||
assert(uid == 0);
|
||||
|
||||
assert(strcmp(user_from_uid(0, 0), "root") == 0);
|
||||
|
||||
assert(gid_from_group("root", &gid) == 0);
|
||||
assert(gid == 0);
|
||||
|
||||
assert(strcmp(group_from_gid(0, 0), "root") == 0);
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2017 Guillem Jover <guillem@hadrons.org>
|
||||
* Copyright © 2017, 2020 Guillem Jover <guillem@hadrons.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -25,19 +25,31 @@
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
void *set;
|
||||
|
||||
umask(0);
|
||||
|
||||
assert(getmode(setmode("0"), 0) == 0);
|
||||
set = setmode("0");
|
||||
assert(getmode(set, 0) == 0);
|
||||
free(set);
|
||||
|
||||
assert(getmode(setmode("7777"), 0) == 07777);
|
||||
assert(getmode(setmode("1555"), 0) == 01555);
|
||||
set = setmode("7777");
|
||||
assert(getmode(set, 0) == 07777);
|
||||
free(set);
|
||||
|
||||
assert(getmode(setmode("ugo=rwx"), 0) == 0777);
|
||||
set = setmode("1555");
|
||||
assert(getmode(set, 0) == 01555);
|
||||
free(set);
|
||||
|
||||
set = setmode("ugo=rwx");
|
||||
assert(getmode(set, 0) == 0777);
|
||||
free(set);
|
||||
|
||||
/* FIXME: Complete unit tests. */
|
||||
|
||||
|
@@ -35,11 +35,15 @@ main(int argc, char **argv)
|
||||
|
||||
assert(strnstr(large, "", strlen(large)) == large);
|
||||
|
||||
assert(strnstr(large, "far", strlen(large)) == NULL);
|
||||
assert(strnstr(large, "quux", strlen(large)) == NULL);
|
||||
|
||||
assert(strnstr(large, small, 4) == NULL);
|
||||
|
||||
assert(strnstr(large, small, strlen(large)) == (large + 4));
|
||||
|
||||
assert(strnstr("quux", large, strlen("quux")) == NULL);
|
||||
assert(strnstr("foo", large, strlen("foo")) == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -27,12 +27,13 @@
|
||||
#include <sys/wait.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <wchar.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "test-stream.h"
|
||||
|
||||
FILE *
|
||||
pipe_feed(const char *fmt, const void **buf, int buf_nmemb)
|
||||
pipe_feed(enum pipe_data_mode mode, const void **buf, int buf_nmemb)
|
||||
{
|
||||
FILE *fp;
|
||||
int rc;
|
||||
@@ -56,7 +57,10 @@ pipe_feed(const char *fmt, const void **buf, int buf_nmemb)
|
||||
assert(fp);
|
||||
|
||||
for (line = 0; line < buf_nmemb; line++) {
|
||||
rc = fprintf(fp, fmt, buf[line]);
|
||||
if (mode == PIPE_DATA_ASCII)
|
||||
rc = fprintf(fp, "%s", (const char *)buf[line]);
|
||||
else
|
||||
rc = fprintf(fp, "%ls", (const wchar_t *)buf[line]);
|
||||
assert(rc >= 0);
|
||||
}
|
||||
|
||||
|
@@ -29,8 +29,13 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
enum pipe_data_mode {
|
||||
PIPE_DATA_ASCII,
|
||||
PIPE_DATA_WIDE,
|
||||
};
|
||||
|
||||
FILE *
|
||||
pipe_feed(const char *fmt, const void **buf, int buf_nmemb);
|
||||
pipe_feed(enum pipe_data_mode mode, const void **buf, int buf_nmemb);
|
||||
void
|
||||
pipe_close(FILE *fp);
|
||||
|
||||
|
@@ -24,12 +24,11 @@
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#define LIBBSD_OPENBSD_VIS 1
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wcpp"
|
||||
#include <vis.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
|
Reference in New Issue
Block a user