portable: release 0.90
[got-portable.git] / include / got_compat2.h
blob68c02fe36df0f00abffe293ccae6d27b225b6297
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 #include <limits.h>
21 #include <sys/uio.h>
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... */
53 #include <endian.h>
54 #include <grp.h>
55 #include <stdlib.h>
56 #endif
58 #ifndef __GNUC__
59 #define __attribute__(a)
60 #ifndef __bounded__
61 #define __bounded__(a, b, c)
62 #endif
63 #endif
66 /* For flock. */
67 #ifndef O_EXLOCK
68 #define O_EXLOCK 0
69 #endif
71 #ifndef HAVE_FLOCK
72 #define LOCK_SH 0
73 #define LOCK_EX 0
74 #define LOCK_NB 0
75 #define flock(fd, op) (0)
76 #else
77 #include <sys/file.h>
78 #endif
80 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
81 #ifndef WAIT_ANY
82 #define WAIT_ANY (-1)
83 #endif
85 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
86 * using EAI_NONAME.
88 #ifndef EAI_NODATA
89 #define EAI_NODATA EAI_NONAME
90 #endif
92 #ifndef __dead
93 #define __dead __attribute__ ((__noreturn__))
94 #endif
96 #ifndef __unused
97 #define __unused __attribute__ ((__unused__))
98 #endif
100 #ifndef __OpenBSD__
101 #define pledge(s, p) (0)
102 #define unveil(s, p) (0)
103 #endif
105 #ifndef __FreeBSD__
106 #define cap_enter() (0)
107 #endif
109 #ifndef HAVE_B64_NTOP
110 #undef b64_ntop
111 #undef b64_pton
112 int b64_ntop(u_char const *, size_t, char *, size_t);
113 int b64_pton(char const *, u_char *, size_t);
114 #endif
116 #ifndef HAVE_SETRESGID
117 #define setresgid(a, b, c) (0)
118 #endif
120 #ifndef HAVE_SETRESUID
121 #define setresuid(a, b, c) (0)
122 #endif
124 #ifndef HAVE_LINUX_LANDLOCK_H
125 #define landlock_no_fs() (0)
126 #else
127 int landlock_no_fs(void);
128 #endif
130 #ifndef INFTIM
131 #define INFTIM -1
132 #endif
134 #ifndef HAVE_BSD_UUID
135 #include <uuid/uuid.h>
136 #define uuid_s_ok 0
137 #define uuid_s_bad_version 1
138 #define uuid_s_invalid_string_uuid 2
139 #define uuid_s_no_memory 3
141 /* Length of a node address (an IEEE 802 address). */
142 #define _UUID_NODE_LEN 6
144 struct uuid {
145 uint32_t time_low;
146 uint16_t time_mid;
147 uint16_t time_hi_and_version;
148 uint8_t clock_seq_hi_and_reserved;
149 uint8_t clock_seq_low;
150 uint8_t node[_UUID_NODE_LEN];
153 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
154 int32_t uuid_is_nil(struct uuid *, uint32_t *);
155 void uuid_create(uuid_t *, uint32_t *);
156 void uuid_create_nil(struct uuid *, uint32_t *);
157 void uuid_from_string(const char *, uuid_t *, uint32_t *);
158 void uuid_to_string(uuid_t *, char **, uint32_t *);
159 #else
160 #include <uuid.h>
161 #endif
163 #ifdef HAVE_STDINT_H
164 #include <stdint.h>
165 #else
166 #include <inttypes.h>
167 #endif
169 #ifdef HAVE_QUEUE_H
170 #include <sys/queue.h>
171 #endif
173 #ifndef HAVE_TREE_H
174 #include "compat/tree.h"
175 #else
176 #include <sys/tree.h>
177 #endif
179 #ifdef HAVE_UTIL_H
180 #include <util.h>
181 #endif
183 #ifdef HAVE_LIBUTIL_H
184 #include <libutil.h>
185 #endif
187 #ifndef IOV_MAX
188 # define IOV_MAX 1024
189 #endif
191 #ifndef HAVE_IMSG
192 #include "compat/imsg.h"
193 #else
194 #include <imsg.h>
195 #endif
197 #ifndef HAVE_SIPHASH
198 #include "compat/siphash.h"
199 #else
200 #include <siphash.h>
201 #endif
203 /* Include Apple-specific headers. Mostly for crypto.*/
204 #if defined(__APPLE__)
205 #define COMMON_DIGEST_FOR_OPENSSL
206 #include <CommonCrypto/CommonDigest.h>
208 #define SHA512_BLOCK_LENGTH 128
209 typedef struct _SHA2_CTX {
210 union {
211 u_int32_t st32[8];
212 u_int64_t st64[8];
213 } state;
214 u_int64_t bitcount[2];
215 u_int8_t buffer[SHA512_BLOCK_LENGTH];
216 } SHA2_CTX;
217 #endif
219 #if defined(__APPLE__) || defined(__NetBSD__)
220 #define SHA256Init SHA256_Init
221 #define SHA256Update SHA256_Update
222 #define SHA256Final SHA256_Final
223 #endif
225 #ifndef __APPLE__
226 #ifdef HAVE_SHA_H
227 # include <sha.h>
228 #endif
229 #ifdef HAVE_SHA1_H
230 # include <sha1.h>
231 #endif
232 #ifdef HAVE_SHA2_H
233 # include <sha2.h>
234 #else
235 # include "sha2.h"
236 #endif
237 #ifdef HAVE_SHA256_H
238 # include <sha256.h>
239 #endif
240 #endif
242 /* Catch-all for systems where the header files don't exist and/or the below
243 * still are not defined.
245 #ifndef SHA256_DIGEST_LENGTH
246 #define SHA256_DIGEST_LENGTH 32
247 #endif
249 #ifndef SHA256_DIGEST_STRING_LENGTH
250 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
251 #endif
253 #if defined(__DragonFly__)
254 #include <openssl/sha.h>
255 #endif
257 #ifndef SHA1_DIGEST_LENGTH
258 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
259 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
261 #define SHA1_CTX SHA_CTX
262 #define SHA1Init SHA1_Init
263 #define SHA1Update SHA1_Update
264 #define SHA1Final SHA1_Final
265 #endif
268 * The following SA_LEN/SS_LEN dance comes from various source, notably
269 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
270 * has tweaked a lot of the following macros to suit the needs of
271 * got-portable.
274 /* From OpenNTPD portable */
275 #if !defined(SA_LEN)
276 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
277 # define SA_LEN(x) ((x)->sa_len)
278 # else
279 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
280 sizeof(struct sockaddr_in6) : \
281 sizeof(struct sockaddr_in))
282 # endif
284 #endif
286 /* From OpenBGPD portable */
287 #if !defined(SS_LEN)
288 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
289 # define SS_LEN(x) ((x)->ss_len)
290 # else
291 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
292 # endif
293 #endif
295 /* SOCK_NONBLOCK isn't available across BSDs... */
296 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
297 #define SOCK_NONBLOCK 00004000
298 #endif
300 #ifndef HAVE_ASPRINTF
301 /* asprintf.c */
302 int asprintf(char **, const char *, ...);
303 int vasprintf(char **, const char *, va_list);
304 #endif
306 #ifndef HAVE_EXPLICIT_BZERO
307 /* explicit_bzero.c */
308 void explicit_bzero(void *, size_t);
309 #endif
311 #ifndef HAVE_GETDTABLECOUNT
312 /* getdtablecount.c */
313 int getdtablecount(void);
314 #endif
316 #ifndef HAVE_CLOSEFROM
317 /* closefrom.c */
318 void closefrom(int);
319 #endif
321 #ifndef HAVE_STRSEP
322 /* strsep.c */
323 char *strsep(char **, const char *);
324 #endif
326 #ifndef HAVE_STRTONUM
327 /* strtonum.c */
328 long long strtonum(const char *, long long, long long, const char **);
329 #endif
331 #ifndef HAVE_STRLCPY
332 /* strlcpy.c */
333 size_t strlcpy(char *, const char *, size_t);
334 #endif
336 #ifndef HAVE_STRLCAT
337 /* strlcat.c */
338 size_t strlcat(char *, const char *, size_t);
339 #endif
341 #ifndef HAVE_STRNLEN
342 /* strnlen.c */
343 size_t strnlen(const char *, size_t);
344 #endif
346 #ifndef HAVE_STRNDUP
347 /* strndup.c */
348 char *strndup(const char *, size_t);
349 #endif
351 #ifndef HAVE_GETPROGNAME
352 /* getprogname.c */
353 const char *getprogname(void);
354 #endif
356 #ifndef HAVE_GETLINE
357 /* getline.c */
358 ssize_t getline(char **, size_t *, FILE *);
359 #endif
361 #ifndef HAVE_FREEZERO
362 /* freezero.c */
363 void freezero(void *, size_t);
364 #endif
366 #ifndef HAVE_GETDTABLECOUNT
367 /* getdtablecount.c */
368 int getdtablecount(void);
369 #endif
371 #ifndef HAVE_REALLOCARRAY
372 /* reallocarray.c */
373 void *reallocarray(void *, size_t, size_t);
374 #endif
376 #ifndef HAVE_RECALLOCARRAY
377 /* recallocarray.c */
378 void *recallocarray(void *, size_t, size_t, size_t);
379 #endif
381 #ifndef HAVE_SETPROCTITLE
382 /* setproctitle.c */
383 void setproctitle(const char *, ...);
384 #endif
386 #ifndef HAVE_FMT_SCALED
387 /* fmt_scaled.c */
388 int fmt_scaled(long long, char *);
389 int scan_scaled(char *, long long *);
390 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
391 #endif
393 #if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
394 /* getopt.c */
395 extern int BSDopterr;
396 extern int BSDoptind;
397 extern int BSDoptopt;
398 extern int BSDoptreset;
399 extern char *BSDoptarg;
400 int BSDgetopt(int, char *const *, const char *);
401 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
402 #define opterr BSDopterr
403 #define optind BSDoptind
404 #define optopt BSDoptopt
405 #define optreset BSDoptreset
406 #define optarg BSDoptarg
407 #endif
409 /* Check for some of the non-portable timespec*() functions.
410 * This should largely come from libbsd for systems which
411 * aren't BSD, but this will depend on how old the library
412 * is.
414 #ifndef timespecisset
415 #define timespecisset(tsp) \
416 ((tsp)->tv_sec || (tsp)->tv_nsec)
417 #endif
419 #ifndef timespecsub
420 #define timespecsub(tsp, usp, vsp) \
421 do { \
422 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
423 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
424 if ((vsp)->tv_nsec < 0) { \
425 (vsp)->tv_sec--; \
426 (vsp)->tv_nsec += 1000000000L; \
428 } while (0)
429 #endif
431 #ifndef timespeccmp
432 #define timespeccmp(tvp, uvp, cmp) \
433 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
434 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
435 ((tvp)->tv_sec cmp (uvp)->tv_sec))
436 #endif
438 #ifndef HAVE_MERGESORT
439 /* mergesort.c */
440 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
441 #endif
442 #endif