From 71faa885925d5d04d0c9c4adf6102d70d24a304b Mon Sep 17 00:00:00 2001 From: "git@daemon.de" Date: Sat, 8 Mar 2014 19:27:52 +0100 Subject: [PATCH] portability fixes (compiles under macosx 5.8 as well, probably on newer ones as well) --- configure.ac | 1 + include/pcp/config.h.in | 3 +++ include/pcp/platform.h | 28 ++++++++++++++++++++++------ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index fc28e95..1fbc169 100755 --- a/configure.ac +++ b/configure.ac @@ -62,6 +62,7 @@ AC_TYPE_SIZE_T # Checks for library functions. AC_CHECK_FUNCS( \ arc4random_buf \ + arc4random \ fread \ fopen \ free \ diff --git a/include/pcp/config.h.in b/include/pcp/config.h.in index e1afb04..0dd45ca 100644 --- a/include/pcp/config.h.in +++ b/include/pcp/config.h.in @@ -1,5 +1,8 @@ /* include/pcp/config.h.in. Generated from configure.ac by autoheader. */ +/* Define to 1 if you have the `arc4random' function. */ +#undef HAVE_ARC4RANDOM + /* Define to 1 if you have the `arc4random_buf' function. */ #undef HAVE_ARC4RANDOM_BUF diff --git a/include/pcp/platform.h b/include/pcp/platform.h index 3bae160..7b6e357 100644 --- a/include/pcp/platform.h +++ b/include/pcp/platform.h @@ -33,11 +33,14 @@ # include # ifndef HAVE_BE32TOH # /* openbsd, use aliases */ +# define be16toh betoh16 # define be32toh betoh32 # define be64toh betoh64 # endif # else /* no sys/endian.h */ # ifdef __CPU_IS_BIG_ENDIAN +# define be16toh(x) (x) +# define htobe16(x) (x) # define be32toh(x) (x) # define htobe32(x) (x) # define be64toh(x) (x) @@ -52,6 +55,8 @@ # error Need either netinet/in.h or arpa/inet.h for ntohl() and htonl() # endif # endif +# define be16toh(x) ((u_int16_t)ntohl((u_int16_t)(x))) +# define htobe16(x) ((u_int16_t)htonl((u_int16_t)(x))) # define be32toh(x) ((u_int32_t)ntohl((u_int32_t)(x))) # define htobe32(x) ((u_int32_t)htonl((u_int32_t)(x))) # define be64toh(x) ((u_int64_t)ntohl((u_int64_t)(x))) @@ -61,20 +66,18 @@ #endif /* HAVE_ENDIAN_H */ -#ifndef HAVE_ARC4RANDOM_BUF -/* shitty OS. we're using libsodium's implementation */ - +#ifndef HAVE_ARC4RANDOM #include - static inline u_int32_t arc4random() { return randombytes_random(); } +#endif +#ifndef HAVE_ARC4RANDOM_BUF +#include static inline void arc4random_buf(void *buf, size_t nbytes) { randombytes((unsigned char*)buf, nbytes); } - - #endif @@ -140,6 +143,19 @@ int vasprintf(char **ret, const char *format, va_list args) { #endif +#ifndef HAVE_STRNLEN +static inline size_t +strnlen(const char *msg, size_t maxlen) +{ + size_t i; + + for (i=0; i