From d359dd9981f4137e8873a7a571e5c54c5b2449c4 Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Wed, 2 Feb 2022 08:14:56 +0100
Subject: linux and freebsd compatibility

---
 ecp/build.sh                                 | 20 +++++++----
 ecp/src/platform/posix/transport.c           |  1 +
 ecp/util/util.c                              | 11 +++---
 ext/libressl/include/compat/machine/endian.h | 51 ++++++++++++++++++++++++++++
 ext/libressl/include/compat/stdlib.h         | 47 +++++++++++++++++++++++++
 ext/libressl/ssl_common.mk                   |  2 ++
 6 files changed, 120 insertions(+), 12 deletions(-)
 create mode 100644 ext/libressl/include/compat/machine/endian.h
 create mode 100644 ext/libressl/include/compat/stdlib.h

diff --git a/ecp/build.sh b/ecp/build.sh
index acb1a14..d28c794 100755
--- a/ecp/build.sh
+++ b/ecp/build.sh
@@ -5,24 +5,30 @@ if [ -z $1 ]; then
 else
     ARG=$1
 fi
+
+
+if [ -z $MAKE ]; then
+    MAKE=make
+fi
+
 PLATFORM=posix
 
 cd src/ecp
 if [ "$ARG" != "clean" ]; then
-    make platform=$PLATFORM clean
+    $MAKE platform=$PLATFORM clean
 fi
-make platform=$PLATFORM $ARG || exit
+$MAKE platform=$PLATFORM $ARG || exit
 if [ "$ARG" ==  "all" ]; then
-    make platform=$PLATFORM install
+    $MAKE platform=$PLATFORM install
 fi
 
 cd ../../util
 if [ "$ARG" != "clean" ]; then
-    make platform=$PLATFORM clean
+    $MAKE platform=$PLATFORM clean
 fi
-make platform=$PLATFORM $ARG || exit
+$MAKE platform=$PLATFORM $ARG || exit
 cd ../test
 if [ "$ARG" != "clean" ]; then
-    make platform=$PLATFORM clean
+    $MAKE platform=$PLATFORM clean
 fi
-make platform=$PLATFORM $ARG || exit
+$MAKE platform=$PLATFORM $ARG || exit
diff --git a/ecp/src/platform/posix/transport.c b/ecp/src/platform/posix/transport.c
index 2032ce2..c41f0df 100644
--- a/ecp/src/platform/posix/transport.c
+++ b/ecp/src/platform/posix/transport.c
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <poll.h>
diff --git a/ecp/util/util.c b/ecp/util/util.c
index 4f4adc2..7ce8da5 100644
--- a/ecp/util/util.c
+++ b/ecp/util/util.c
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include "core.h"
 #include "cr.h"
@@ -9,7 +10,7 @@
 int ecp_util_key_save(ECPContext *ctx, ECPDHKey *key, char *filename) {
     int fd;
     ssize_t rv;
-    
+
     if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR;
     rv = write(fd, ecp_cr_dh_pub_get_buf(&key->public), ECP_ECDH_SIZE_KEY);
     if (rv != ECP_ECDH_SIZE_KEY) {
@@ -29,7 +30,7 @@ int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename) {
     int fd;
     ssize_t rv;
     unsigned char buf[ECP_ECDH_SIZE_KEY];
-    
+
     if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR;
     rv = read(fd, buf, ECP_ECDH_SIZE_KEY);
     if (rv != ECP_ECDH_SIZE_KEY) {
@@ -44,7 +45,7 @@ int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename) {
     close(fd);
 
     ecp_cr_dh_pub_from_buf(&key->public, buf);
-    
+
     key->valid = 1;
     return ECP_OK;
 }
@@ -52,7 +53,7 @@ int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename) {
 int ecp_util_node_save(ECPContext *ctx, ECPNode *node, char *filename) {
     int fd;
     ssize_t rv;
-    
+
     if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR;
     rv = write(fd, ecp_cr_dh_pub_get_buf(&node->public), ECP_ECDH_SIZE_KEY);
     if (rv != ECP_ECDH_SIZE_KEY) {
@@ -72,7 +73,7 @@ int ecp_util_node_load(ECPContext *ctx, ECPNode *node, char *filename) {
     int fd;
     ssize_t rv;
     unsigned char buf[ECP_ECDH_SIZE_KEY];
-    
+
     if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR;
     rv = read(fd, buf, ECP_ECDH_SIZE_KEY);
     if (rv != ECP_ECDH_SIZE_KEY) {
diff --git a/ext/libressl/include/compat/machine/endian.h b/ext/libressl/include/compat/machine/endian.h
new file mode 100644
index 0000000..4dcb60d
--- /dev/null
+++ b/ext/libressl/include/compat/machine/endian.h
@@ -0,0 +1,51 @@
+/*
+ * Public domain
+ * machine/endian.h compatibility shim
+ */
+
+#ifndef LIBCRYPTOCOMPAT_BYTE_ORDER_H_
+#define LIBCRYPTOCOMPAT_BYTE_ORDER_H_
+
+#if defined(_WIN32)
+
+#define LITTLE_ENDIAN  1234
+#define BIG_ENDIAN 4321
+#define PDP_ENDIAN	3412
+
+/*
+ * Use GCC and Visual Studio compiler defines to determine endian.
+ */
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define BYTE_ORDER LITTLE_ENDIAN
+#else
+#define BYTE_ORDER BIG_ENDIAN
+#endif
+
+#elif defined(__linux__) || defined(__midipix__)
+#include <endian.h>
+
+#elif defined(__sun) || defined(_AIX) || defined(__hpux)
+#include <sys/types.h>
+#include <arpa/nameser_compat.h>
+
+#elif defined(__sgi)
+#include <standards.h>
+#include <sys/endian.h>
+
+#else
+#include_next <machine/endian.h>
+
+#endif
+
+#ifndef __STRICT_ALIGNMENT
+#define __STRICT_ALIGNMENT
+#if defined(__i386)    || defined(__i386__)    || \
+    defined(__x86_64)  || defined(__x86_64__)  || \
+    defined(__s390__)  || defined(__s390x__)   || \
+    defined(__aarch64__)                       || \
+    ((defined(__arm__) || defined(__arm)) && __ARM_ARCH >= 6)
+#undef __STRICT_ALIGNMENT
+#endif
+#endif
+
+#endif
diff --git a/ext/libressl/include/compat/stdlib.h b/ext/libressl/include/compat/stdlib.h
new file mode 100644
index 0000000..2eaea24
--- /dev/null
+++ b/ext/libressl/include/compat/stdlib.h
@@ -0,0 +1,47 @@
+/*
+ * stdlib.h compatibility shim
+ * Public domain
+ */
+
+#ifdef _MSC_VER
+#if _MSC_VER >= 1900
+#include <../ucrt/stdlib.h>
+#else
+#include <../include/stdlib.h>
+#endif
+#else
+#include_next <stdlib.h>
+#endif
+
+#ifndef LIBCRYPTOCOMPAT_STDLIB_H
+#define LIBCRYPTOCOMPAT_STDLIB_H
+
+#include <sys/types.h>
+#include <stdint.h>
+
+#ifndef HAVE_ARC4RANDOM_BUF
+uint32_t arc4random(void);
+void arc4random_buf(void *_buf, size_t n);
+uint32_t arc4random_uniform(uint32_t upper_bound);
+#endif
+
+#ifndef HAVE_FREEZERO
+void freezero(void *ptr, size_t sz);
+#endif
+
+#ifndef HAVE_GETPROGNAME
+const char * getprogname(void);
+#endif
+
+void *reallocarray(void *, size_t, size_t);
+
+#ifndef HAVE_RECALLOCARRAY
+void *recallocarray(void *, size_t, size_t, size_t);
+#endif
+
+#ifndef HAVE_STRTONUM
+long long strtonum(const char *nptr, long long minval,
+		long long maxval, const char **errstr);
+#endif
+
+#endif
diff --git a/ext/libressl/ssl_common.mk b/ext/libressl/ssl_common.mk
index fab0710..bac4e9f 100644
--- a/ext/libressl/ssl_common.mk
+++ b/ext/libressl/ssl_common.mk
@@ -1,6 +1,8 @@
 -include common.mk
 
 CFLAGS += -I../../include/compat -I../../include
+# exclude from include/compat/stdlib.h
+CFLAGS += -DHAVE_FREEZERO -DHAVE_GETPROGNAME -DHAVE_RECALLOCARRAY -DHAVE_STRTONUM
 # exclude from include/compat/string.h
 CFLAGS += -DHAVE_STRCASECMP -DHAVE_STRLCPY -DHAVE_STRLCAT -DHAVE_STRNDUP -DHAVE_STRNLEN -DHAVE_STRSEP -DHAVE_MEMMEM
 # exclude from include/compat/unistd.h
-- 
cgit v1.2.3