xref: /freebsd/contrib/libfido2/openbsd-compat/openbsd-compat.h (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*
2  * Copyright (c) 2018-2021 Yubico AB. All rights reserved.
3  * Use of this source code is governed by a BSD-style
4  * license that can be found in the LICENSE file.
5  * SPDX-License-Identifier: BSD-2-Clause
6  */
7 
8 #ifndef _OPENBSD_COMPAT_H
9 #define _OPENBSD_COMPAT_H
10 
11 #if defined(_MSC_VER)
12 #include "types.h"
13 #endif
14 
15 #if defined(HAVE_ENDIAN_H)
16 #include <endian.h>
17 #endif
18 
19 #if defined(__APPLE__) && !defined(HAVE_ENDIAN_H)
20 #include <libkern/OSByteOrder.h>
21 #define be16toh(x) OSSwapBigToHostInt16((x))
22 #define htobe16(x) OSSwapHostToBigInt16((x))
23 #define be32toh(x) OSSwapBigToHostInt32((x))
24 #define htobe32(x) OSSwapHostToBigInt32((x))
25 #define htole32(x) OSSwapHostToLittleInt32((x))
26 #define htole64(x) OSSwapHostToLittleInt64((x))
27 #endif /* __APPLE__ && !HAVE_ENDIAN_H */
28 
29 #if defined(_WIN32) && !defined(HAVE_ENDIAN_H)
30 #include <stdint.h>
31 #include <winsock2.h>
32 #if !defined(_MSC_VER)
33 #include <sys/param.h>
34 #endif
35 #define be16toh(x) ntohs((x))
36 #define htobe16(x) htons((x))
37 #define be32toh(x) ntohl((x))
38 #define htobe32(x) htonl((x))
39 uint32_t htole32(uint32_t);
40 uint64_t htole64(uint64_t);
41 #endif /* _WIN32 && !HAVE_ENDIAN_H */
42 
43 #if (defined(__FreeBSD__) || defined(__MidnightBSD__)) && !defined(HAVE_ENDIAN_H)
44 #include <sys/endian.h>
45 #endif
46 
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #if !defined(HAVE_STRLCAT)
51 size_t strlcat(char *, const char *, size_t);
52 #endif
53 
54 #if !defined(HAVE_STRLCPY)
55 size_t strlcpy(char *, const char *, size_t);
56 #endif
57 
58 #if !defined(HAVE_STRSEP)
59 char *strsep(char **, const char *);
60 #endif
61 
62 #if !defined(HAVE_RECALLOCARRAY)
63 void *recallocarray(void *, size_t, size_t, size_t);
64 #endif
65 
66 #if !defined(HAVE_EXPLICIT_BZERO)
67 void explicit_bzero(void *, size_t);
68 #endif
69 
70 #if !defined(HAVE_FREEZERO)
71 void freezero(void *, size_t);
72 #endif
73 
74 #if !defined(HAVE_GETPAGESIZE)
75 int getpagesize(void);
76 #endif
77 
78 #if !defined(HAVE_TIMINGSAFE_BCMP)
79 int timingsafe_bcmp(const void *, const void *, size_t);
80 #endif
81 
82 #if !defined(HAVE_READPASSPHRASE)
83 #include "readpassphrase.h"
84 #else
85 #include <readpassphrase.h>
86 #endif
87 
88 #include <openssl/opensslv.h>
89 
90 #if !defined(HAVE_ERR_H)
91 #include "err.h"
92 #else
93 #include <err.h>
94 #endif
95 
96 #if !defined(HAVE_GETOPT)
97 #include "getopt.h"
98 #else
99 #include <unistd.h>
100 #endif
101 
102 #if !defined(HAVE_GETLINE)
103 #include <stdio.h>
104 ssize_t getline(char **, size_t *, FILE *);
105 #endif
106 
107 #if defined(_MSC_VER)
108 #define strerror_r(e, b, l) strerror_s((b), (l), (e))
109 #endif
110 
111 #include "time.h"
112 
113 #if !defined(HAVE_POSIX_IOCTL)
114 #define IOCTL_REQ(x)	(x)
115 #else
116 #define IOCTL_REQ(x)	((int)(x))
117 #endif
118 
119 #if !defined(HAVE_ASPRINTF)
120 int asprintf(char **, const char *, ...);
121 #endif
122 
123 #endif /* !_OPENBSD_COMPAT_H */
124