implement gotwebd test harness
[got-portable.git] / include / got_compat2.h
blobfb06ed9608e05a21544619ffe1cf9cfe3f7a25cf
1 /*
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
20 #ifdef HAVE_STDINT_H
21 #include <stdint.h>
22 #else
23 #include <inttypes.h>
24 #endif
26 #include <limits.h>
27 #include <sys/uio.h>
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... */
59 #include <endian.h>
60 #include <grp.h>
61 #include <stdlib.h>
62 #endif
64 #ifndef __GNUC__
65 #define __attribute__(a)
66 #endif
69 #ifndef UID_MAX
70 # define UID_MAX UINT_MAX /* max value for a uid_t */
71 #endif
72 #ifndef GID_MAX
73 # define GID_MAX UINT_MAX /* max value for a gid_t */
74 #endif
76 /* For flock. */
77 #ifndef O_EXLOCK
78 #define O_EXLOCK 0
79 #endif
81 #ifndef HAVE_FLOCK
82 #define LOCK_SH 0
83 #define LOCK_EX 0
84 #define LOCK_NB 0
85 #define flock(fd, op) (0)
86 #else
87 #include <sys/file.h>
88 #endif
90 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
91 #ifndef WAIT_ANY
92 #define WAIT_ANY (-1)
93 #endif
95 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
96 * using EAI_NONAME.
98 #ifndef EAI_NODATA
99 #define EAI_NODATA EAI_NONAME
100 #endif
102 #ifndef __dead
103 #define __dead __attribute__ ((__noreturn__))
104 #endif
106 #ifndef __unused
107 #define __unused __attribute__ ((__unused__))
108 #endif
110 #if !defined(__bounded__) && !defined(__OpenBSD__)
111 #define __bounded__(a, b, c)
112 #endif
114 #ifndef __OpenBSD__
115 #define pledge(s, p) (0)
116 #define unveil(s, p) (0)
117 #endif
119 #ifndef __FreeBSD__
120 #define cap_enter() (0)
121 #endif
123 #ifndef HAVE_B64_NTOP
124 #undef b64_ntop
125 #undef b64_pton
126 int b64_ntop(u_char const *, size_t, char *, size_t);
127 int b64_pton(char const *, u_char *, size_t);
128 #endif
130 #ifndef HAVE_SETRESGID
131 #define setresgid(a, b, c) (0)
132 #endif
134 #ifndef HAVE_SETRESUID
135 #define setresuid(a, b, c) (0)
136 #endif
138 #ifndef HAVE_LINUX_LANDLOCK_H
139 #define landlock_no_fs() (0)
140 #else
141 int landlock_no_fs(void);
142 #endif
144 #ifndef INFTIM
145 #define INFTIM -1
146 #endif
148 #ifndef HAVE_BSD_UUID
149 #include <uuid/uuid.h>
150 #define uuid_s_ok 0
151 #define uuid_s_bad_version 1
152 #define uuid_s_invalid_string_uuid 2
153 #define uuid_s_no_memory 3
155 /* Length of a node address (an IEEE 802 address). */
156 #define _UUID_NODE_LEN 6
158 struct uuid {
159 uint32_t time_low;
160 uint16_t time_mid;
161 uint16_t time_hi_and_version;
162 uint8_t clock_seq_hi_and_reserved;
163 uint8_t clock_seq_low;
164 uint8_t node[_UUID_NODE_LEN];
167 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
168 int32_t uuid_is_nil(struct uuid *, uint32_t *);
169 void uuid_create(uuid_t *, uint32_t *);
170 void uuid_create_nil(struct uuid *, uint32_t *);
171 void uuid_from_string(const char *, uuid_t *, uint32_t *);
172 void uuid_to_string(uuid_t *, char **, uint32_t *);
173 #else
174 #include <uuid.h>
175 #endif
177 #ifdef HAVE_QUEUE_H
178 #include <sys/queue.h>
179 #endif
181 #ifndef HAVE_TREE_H
182 #include "compat/tree.h"
183 #else
184 #include <sys/tree.h>
185 #endif
187 #ifdef HAVE_UTIL_H
188 #include <util.h>
189 #endif
191 #ifdef HAVE_LIBUTIL_H
192 #include <libutil.h>
193 #endif
195 #ifndef IOV_MAX
196 # define IOV_MAX 1024
197 #endif
199 #ifndef HAVE_IMSG
200 #include "compat/imsg.h"
201 #else
202 #include <imsg.h>
203 #endif
205 #ifndef HAVE_SIPHASH
206 #include "compat/siphash.h"
207 #else
208 #include <siphash.h>
209 #endif
211 /* Include Apple-specific headers. Mostly for crypto.*/
212 #if defined(__APPLE__)
213 #define COMMON_DIGEST_FOR_OPENSSL
214 #include <CommonCrypto/CommonDigest.h>
215 #endif
217 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
218 #define SHA256Init SHA256_Init
219 #define SHA256Update SHA256_Update
220 #define SHA256Final SHA256_Final
221 #define SHA2_CTX SHA256_CTX
222 #endif
224 #ifndef __APPLE__
225 #ifdef HAVE_SHA_H
226 # include <sha.h>
227 #endif
228 #ifdef HAVE_SHA1_H
229 # include <sha1.h>
230 #endif
231 #ifdef HAVE_SHA2_H
232 # include <sha2.h>
233 #endif
234 #ifdef HAVE_SHA256_H
235 # include <sha256.h>
236 #endif
237 #endif
239 /* Catch-all for systems where the header files don't exist and/or the below
240 * still are not defined.
242 #ifndef SHA256_DIGEST_LENGTH
243 #define SHA256_DIGEST_LENGTH 32
244 #endif
246 #ifndef SHA256_DIGEST_STRING_LENGTH
247 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
248 #endif
250 #if defined(__DragonFly__)
251 #include <openssl/sha.h>
252 #endif
254 #ifndef SHA1_DIGEST_LENGTH
255 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
256 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
258 #define SHA1_CTX SHA_CTX
259 #define SHA1Init SHA1_Init
260 #define SHA1Update SHA1_Update
261 #define SHA1Final SHA1_Final
262 #endif
264 /* SOCK_NONBLOCK isn't available across BSDs... */
265 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
266 #define SOCK_NONBLOCK 00004000
267 #endif
269 #ifndef HAVE_ASPRINTF
270 /* asprintf.c */
271 int asprintf(char **, const char *, ...);
272 int vasprintf(char **, const char *, va_list);
273 #endif
275 #ifndef HAVE_EXPLICIT_BZERO
276 /* explicit_bzero.c */
277 void explicit_bzero(void *, size_t);
278 #endif
280 #ifndef HAVE_GETDTABLECOUNT
281 /* getdtablecount.c */
282 int getdtablecount(void);
283 #endif
285 #ifndef HAVE_CLOSEFROM
286 /* closefrom.c */
287 void closefrom(int);
288 #endif
290 #ifndef HAVE_STRSEP
291 /* strsep.c */
292 char *strsep(char **, const char *);
293 #endif
295 #ifndef HAVE_STRTONUM
296 /* strtonum.c */
297 long long strtonum(const char *, long long, long long, const char **);
298 #endif
300 #ifndef HAVE_STRLCPY
301 /* strlcpy.c */
302 size_t strlcpy(char *, const char *, size_t);
303 #endif
305 #ifndef HAVE_STRLCAT
306 /* strlcat.c */
307 size_t strlcat(char *, const char *, size_t);
308 #endif
310 #ifndef HAVE_STRNLEN
311 /* strnlen.c */
312 size_t strnlen(const char *, size_t);
313 #endif
315 #ifndef HAVE_STRNDUP
316 /* strndup.c */
317 char *strndup(const char *, size_t);
318 #endif
320 #ifndef HAVE_GETPROGNAME
321 /* getprogname.c */
322 const char *getprogname(void);
323 #endif
325 #ifndef HAVE_GETLINE
326 /* getline.c */
327 ssize_t getline(char **, size_t *, FILE *);
328 #endif
330 #ifndef HAVE_FREEZERO
331 /* freezero.c */
332 void freezero(void *, size_t);
333 #endif
335 #ifndef HAVE_GETDTABLECOUNT
336 /* getdtablecount.c */
337 int getdtablecount(void);
338 #endif
340 #ifndef HAVE_REALLOCARRAY
341 /* reallocarray.c */
342 void *reallocarray(void *, size_t, size_t);
343 #endif
345 #ifndef HAVE_RECALLOCARRAY
346 /* recallocarray.c */
347 void *recallocarray(void *, size_t, size_t, size_t);
348 #endif
350 #ifndef HAVE_SETPROCTITLE
351 /* setproctitle.c */
352 void setproctitle(const char *, ...);
353 #endif
355 #ifndef HAVE_FMT_SCALED
356 /* fmt_scaled.c */
357 int fmt_scaled(long long, char *);
358 int scan_scaled(char *, long long *);
359 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
360 #endif
362 #if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
363 /* getopt.c */
364 extern int BSDopterr;
365 extern int BSDoptind;
366 extern int BSDoptopt;
367 extern int BSDoptreset;
368 extern char *BSDoptarg;
369 int BSDgetopt(int, char *const *, const char *);
370 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
371 #define opterr BSDopterr
372 #define optind BSDoptind
373 #define optopt BSDoptopt
374 #define optreset BSDoptreset
375 #define optarg BSDoptarg
376 #endif
378 /* Check for some of the non-portable timespec*() functions.
379 * This should largely come from libbsd for systems which
380 * aren't BSD, but this will depend on how old the library
381 * is.
383 #ifndef timespecisset
384 #define timespecisset(tsp) \
385 ((tsp)->tv_sec || (tsp)->tv_nsec)
386 #endif
388 #ifndef timespecsub
389 #define timespecsub(tsp, usp, vsp) \
390 do { \
391 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
392 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
393 if ((vsp)->tv_nsec < 0) { \
394 (vsp)->tv_sec--; \
395 (vsp)->tv_nsec += 1000000000L; \
397 } while (0)
398 #endif
400 #ifndef timespeccmp
401 #define timespeccmp(tvp, uvp, cmp) \
402 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
403 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
404 ((tvp)->tv_sec cmp (uvp)->tv_sec))
405 #endif
407 #ifndef HAVE_MERGESORT
408 /* mergesort.c */
409 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
410 #endif
411 #endif