From 77f88857fb809ba6ffa92029255f69c20572e6d2 Mon Sep 17 00:00:00 2001
From: Sergey Matveev <stargrave@stargrave.org>
Date: Sun, 19 Mar 2023 19:43:22 +0300
Subject: [PATCH] Get rid of endian.h dependency

It is not POSIX. It is placed in different places in GNU and BSD systems.
Moreover not every system has leXXdec-style functions.
---
 cmd/zstd/enzstd.c | 6 ++++--
 cmd/zstd/unzstd.c | 8 +++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/cmd/zstd/enzstd.c b/cmd/zstd/enzstd.c
index 63e5574..6dbbe98 100644
--- a/cmd/zstd/enzstd.c
+++ b/cmd/zstd/enzstd.c
@@ -18,7 +18,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/endian.h>
 
 #include <zstd.h>
 
@@ -70,7 +69,10 @@ main(int argc, char **argv)
             perror("can not fread(stdin)");
             goto Exit;
         }
-        srcWantSize = (size_t)be64dec(src);
+        srcWantSize = ((uint64_t)(src[0]) << 56) | ((uint64_t)(src[1]) << 48) |
+                      ((uint64_t)(src[2]) << 40) | ((uint64_t)(src[3]) << 32) |
+                      ((uint64_t)(src[4]) << 24) | ((uint64_t)(src[5]) << 16) |
+                      ((uint64_t)(src[6]) << 8) | (uint64_t)(src[7]);
         if (srcWantSize > srcSize) {
             free(src);
             srcSize = srcWantSize;
diff --git a/cmd/zstd/unzstd.c b/cmd/zstd/unzstd.c
index c87dcd6..823515c 100644
--- a/cmd/zstd/unzstd.c
+++ b/cmd/zstd/unzstd.c
@@ -23,7 +23,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/endian.h>
 
 #include <zstd.h>
 
@@ -34,6 +33,13 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <sysexits.h>
 #endif // __FreeBSD__
 
+static uint32_t
+le32dec(const char buf[4])
+{
+    return (uint32_t)(buf[3]) << 24 | (uint32_t)(buf[2]) << 16 |
+           (uint32_t)(buf[1]) << 8 | (uint32_t)(buf[0]);
+}
+
 int
 main(int argc, char **argv)
 {
-- 
2.51.0