2 * Copyright (c) 2023 Thomas Adam <thomas@xteddy.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef _GOT_COMPAT_H_2
18 #define _GOT_COMPAT_H_2
29 #if defined(__FreeBSD__)
30 #include <sys/endian.h>
31 #include <sys/capsicum.h>
32 #elif defined(__APPLE__)
33 #include <machine/endian.h>
34 #include <libkern/OSByteOrder.h>
35 #include "compat/bsd-poll.h"
37 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
39 #define htobe16(x) OSSwapHostToBigInt16(x)
40 #define htole16(x) OSSwapHostToLittleInt16(x)
41 #define be16toh(x) OSSwapBigToHostInt16(x)
42 #define le16toh(x) OSSwapLittleToHostInt16(x)
44 #define htobe32(x) OSSwapHostToBigInt32(x)
45 #define htole32(x) OSSwapHostToLittleInt32(x)
46 #define be32toh(x) OSSwapBigToHostInt32(x)
47 #define le32toh(x) OSSwapLittleToHostInt32(x)
49 #define htobe64(x) OSSwapHostToBigInt64(x)
50 #define htole64(x) OSSwapHostToLittleInt64(x)
51 #define be64toh(x) OSSwapBigToHostInt64(x)
52 #define le64toh(x) OSSwapLittleToHostInt64(x)
54 #define st_atim st_atimespec
55 #define st_ctim st_ctimespec
56 #define st_mtim st_mtimespec
58 #else /* Linux, etc... */
65 #define __attribute__(a)
67 #define __bounded__(a, b, c)
73 # define UID_MAX UINT_MAX /* max value for a uid_t */
76 # define GID_MAX UINT_MAX /* max value for a gid_t */
88 #define flock(fd, op) (0)
93 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
98 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
102 #define EAI_NODATA EAI_NONAME
106 #define __dead __attribute__ ((__noreturn__))
110 #define __unused __attribute__ ((__unused__))
114 #define pledge(s, p) (0)
115 #define unveil(s, p) (0)
119 #define cap_enter() (0)
122 #ifndef HAVE_B64_NTOP
125 int b64_ntop(u_char
const *, size_t, char *, size_t);
126 int b64_pton(char const *, u_char
*, size_t);
129 #ifndef HAVE_SETRESGID
130 #define setresgid(a, b, c) (0)
133 #ifndef HAVE_SETRESUID
134 #define setresuid(a, b, c) (0)
137 #ifndef HAVE_LINUX_LANDLOCK_H
138 #define landlock_no_fs() (0)
140 int landlock_no_fs(void);
147 #ifndef HAVE_BSD_UUID
148 #include <uuid/uuid.h>
150 #define uuid_s_bad_version 1
151 #define uuid_s_invalid_string_uuid 2
152 #define uuid_s_no_memory 3
154 /* Length of a node address (an IEEE 802 address). */
155 #define _UUID_NODE_LEN 6
160 uint16_t time_hi_and_version
;
161 uint8_t clock_seq_hi_and_reserved
;
162 uint8_t clock_seq_low
;
163 uint8_t node
[_UUID_NODE_LEN
];
166 int32_t uuid_equal(struct uuid
*, struct uuid
*, uint32_t *);
167 int32_t uuid_is_nil(struct uuid
*, uint32_t *);
168 void uuid_create(uuid_t
*, uint32_t *);
169 void uuid_create_nil(struct uuid
*, uint32_t *);
170 void uuid_from_string(const char *, uuid_t
*, uint32_t *);
171 void uuid_to_string(uuid_t
*, char **, uint32_t *);
177 #include <sys/queue.h>
181 #include "compat/tree.h"
183 #include <sys/tree.h>
190 #ifdef HAVE_LIBUTIL_H
195 # define IOV_MAX 1024
199 #include "compat/imsg.h"
205 #include "compat/siphash.h"
210 /* Include Apple-specific headers. Mostly for crypto.*/
211 #if defined(__APPLE__)
212 #define COMMON_DIGEST_FOR_OPENSSL
213 #include <CommonCrypto/CommonDigest.h>
215 #define SHA512_BLOCK_LENGTH 128
216 typedef struct _SHA2_CTX
{
221 u_int64_t bitcount
[2];
222 u_int8_t buffer
[SHA512_BLOCK_LENGTH
];
226 #if defined(__APPLE__) || defined(__NetBSD__)
227 #define SHA256Init SHA256_Init
228 #define SHA256Update SHA256_Update
229 #define SHA256Final SHA256_Final
249 /* Catch-all for systems where the header files don't exist and/or the below
250 * still are not defined.
252 #ifndef SHA256_DIGEST_LENGTH
253 #define SHA256_DIGEST_LENGTH 32
256 #ifndef SHA256_DIGEST_STRING_LENGTH
257 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
260 #if defined(__DragonFly__)
261 #include <openssl/sha.h>
264 #ifndef SHA1_DIGEST_LENGTH
265 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
266 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
268 #define SHA1_CTX SHA_CTX
269 #define SHA1Init SHA1_Init
270 #define SHA1Update SHA1_Update
271 #define SHA1Final SHA1_Final
275 * The following SA_LEN/SS_LEN dance comes from various source, notably
276 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
277 * has tweaked a lot of the following macros to suit the needs of
281 /* From OpenNTPD portable */
283 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
284 # define SA_LEN(x) ((x)->sa_len)
286 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
287 sizeof(struct sockaddr_in6) : \
288 sizeof(struct sockaddr_in))
293 /* From OpenBGPD portable */
295 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
296 # define SS_LEN(x) ((x)->ss_len)
298 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
302 /* SOCK_NONBLOCK isn't available across BSDs... */
303 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
304 #define SOCK_NONBLOCK 00004000
307 #ifndef HAVE_ASPRINTF
309 int asprintf(char **, const char *, ...);
310 int vasprintf(char **, const char *, va_list);
313 #ifndef HAVE_EXPLICIT_BZERO
314 /* explicit_bzero.c */
315 void explicit_bzero(void *, size_t);
318 #ifndef HAVE_GETDTABLECOUNT
319 /* getdtablecount.c */
320 int getdtablecount(void);
323 #ifndef HAVE_CLOSEFROM
330 char *strsep(char **, const char *);
333 #ifndef HAVE_STRTONUM
335 long long strtonum(const char *, long long, long long, const char **);
340 size_t strlcpy(char *, const char *, size_t);
345 size_t strlcat(char *, const char *, size_t);
350 size_t strnlen(const char *, size_t);
355 char *strndup(const char *, size_t);
358 #ifndef HAVE_GETPROGNAME
360 const char *getprogname(void);
365 ssize_t
getline(char **, size_t *, FILE *);
368 #ifndef HAVE_FREEZERO
370 void freezero(void *, size_t);
373 #ifndef HAVE_GETDTABLECOUNT
374 /* getdtablecount.c */
375 int getdtablecount(void);
378 #ifndef HAVE_REALLOCARRAY
380 void *reallocarray(void *, size_t, size_t);
383 #ifndef HAVE_RECALLOCARRAY
384 /* recallocarray.c */
385 void *recallocarray(void *, size_t, size_t, size_t);
388 #ifndef HAVE_SETPROCTITLE
390 void setproctitle(const char *, ...);
393 #ifndef HAVE_FMT_SCALED
395 int fmt_scaled(long long, char *);
396 int scan_scaled(char *, long long *);
397 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
400 #if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
402 extern int BSDopterr
;
403 extern int BSDoptind
;
404 extern int BSDoptopt
;
405 extern int BSDoptreset
;
406 extern char *BSDoptarg
;
407 int BSDgetopt(int, char *const *, const char *);
408 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
409 #define opterr BSDopterr
410 #define optind BSDoptind
411 #define optopt BSDoptopt
412 #define optreset BSDoptreset
413 #define optarg BSDoptarg
416 /* Check for some of the non-portable timespec*() functions.
417 * This should largely come from libbsd for systems which
418 * aren't BSD, but this will depend on how old the library
421 #ifndef timespecisset
422 #define timespecisset(tsp) \
423 ((tsp)->tv_sec || (tsp)->tv_nsec)
427 #define timespecsub(tsp, usp, vsp) \
429 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
430 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
431 if ((vsp)->tv_nsec < 0) { \
433 (vsp)->tv_nsec += 1000000000L; \
439 #define timespeccmp(tvp, uvp, cmp) \
440 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
441 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
442 ((tvp)->tv_sec cmp (uvp)->tv_sec))
445 #ifndef HAVE_MERGESORT
447 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));