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
23 #if defined(__FreeBSD__)
24 #include <sys/endian.h>
25 #include <sys/capsicum.h>
26 #elif defined(__APPLE__)
27 #include <machine/endian.h>
28 #include <libkern/OSByteOrder.h>
29 #include "compat/bsd-poll.h"
31 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
33 #define htobe16(x) OSSwapHostToBigInt16(x)
34 #define htole16(x) OSSwapHostToLittleInt16(x)
35 #define be16toh(x) OSSwapBigToHostInt16(x)
36 #define le16toh(x) OSSwapLittleToHostInt16(x)
38 #define htobe32(x) OSSwapHostToBigInt32(x)
39 #define htole32(x) OSSwapHostToLittleInt32(x)
40 #define be32toh(x) OSSwapBigToHostInt32(x)
41 #define le32toh(x) OSSwapLittleToHostInt32(x)
43 #define htobe64(x) OSSwapHostToBigInt64(x)
44 #define htole64(x) OSSwapHostToLittleInt64(x)
45 #define be64toh(x) OSSwapBigToHostInt64(x)
46 #define le64toh(x) OSSwapLittleToHostInt64(x)
48 #define st_atim st_atimespec
49 #define st_ctim st_ctimespec
50 #define st_mtim st_mtimespec
52 #else /* Linux, etc... */
59 #define __attribute__(a)
61 #define __bounded__(a, b, c)
67 # define UID_MAX UINT_MAX /* max value for a uid_t */
70 # define GID_MAX UINT_MAX /* max value for a gid_t */
82 #define flock(fd, op) (0)
87 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
92 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
96 #define EAI_NODATA EAI_NONAME
100 #define __dead __attribute__ ((__noreturn__))
104 #define __unused __attribute__ ((__unused__))
108 #define pledge(s, p) (0)
109 #define unveil(s, p) (0)
113 #define cap_enter() (0)
116 #ifndef HAVE_B64_NTOP
119 int b64_ntop(u_char
const *, size_t, char *, size_t);
120 int b64_pton(char const *, u_char
*, size_t);
123 #ifndef HAVE_SETRESGID
124 #define setresgid(a, b, c) (0)
127 #ifndef HAVE_SETRESUID
128 #define setresuid(a, b, c) (0)
131 #ifndef HAVE_LINUX_LANDLOCK_H
132 #define landlock_no_fs() (0)
134 int landlock_no_fs(void);
141 #ifndef HAVE_BSD_UUID
142 #include <uuid/uuid.h>
144 #define uuid_s_bad_version 1
145 #define uuid_s_invalid_string_uuid 2
146 #define uuid_s_no_memory 3
148 /* Length of a node address (an IEEE 802 address). */
149 #define _UUID_NODE_LEN 6
154 uint16_t time_hi_and_version
;
155 uint8_t clock_seq_hi_and_reserved
;
156 uint8_t clock_seq_low
;
157 uint8_t node
[_UUID_NODE_LEN
];
160 int32_t uuid_equal(struct uuid
*, struct uuid
*, uint32_t *);
161 int32_t uuid_is_nil(struct uuid
*, uint32_t *);
162 void uuid_create(uuid_t
*, uint32_t *);
163 void uuid_create_nil(struct uuid
*, uint32_t *);
164 void uuid_from_string(const char *, uuid_t
*, uint32_t *);
165 void uuid_to_string(uuid_t
*, char **, uint32_t *);
173 #include <inttypes.h>
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 *));