From: =?utf-8?q?Timo_R=C3=B6hling?= <roehling@debian.org>
Date: Mon, 26 Sep 2022 22:30:21 +0200
Subject: Big endian fix

---
 tiny_gltf.h | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/tiny_gltf.h b/tiny_gltf.h
index 15f63c4..1cc8e36 100644
--- a/tiny_gltf.h
+++ b/tiny_gltf.h
@@ -1651,13 +1651,9 @@ class TinyGLTF {
 //#include <wordexp.h>
 #endif
 
-#if defined(__sparcv9) || defined(__powerpc__)
-// Big endian
-#else
-#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
 #define TINYGLTF_LITTLE_ENDIAN 1
 #endif
-#endif
 
 namespace {
 #ifdef TINYGLTF_USE_RAPIDJSON
@@ -7708,7 +7704,8 @@ static bool WriteBinaryGltfStream(std::ostream &stream,
                                   const std::string &content,
                                   const std::vector<unsigned char> &binBuffer) {
   const std::string header = "glTF";
-  const int version = 2;
+  uint32_t version = 2;
+  swap4(&version);
 
   const uint32_t content_size = uint32_t(content.size());
   const uint32_t binBuffer_size = uint32_t(binBuffer.size());
@@ -7720,17 +7717,20 @@ static bool WriteBinaryGltfStream(std::ostream &stream,
 
   // 12 bytes for header, JSON content length, 8 bytes for JSON chunk info.
   // Chunk data must be located at 4-byte boundary, which may require padding
-  const uint32_t length =
+  uint32_t length =
       12 + 8 + content_size + content_padding_size +
       (binBuffer_size ? (8 + binBuffer_size + bin_padding_size) : 0);
+  swap4(&length);
 
   stream.write(header.c_str(), std::streamsize(header.size()));
   stream.write(reinterpret_cast<const char *>(&version), sizeof(version));
   stream.write(reinterpret_cast<const char *>(&length), sizeof(length));
 
   // JSON chunk info, then JSON data
-  const uint32_t model_length = uint32_t(content.size()) + content_padding_size;
-  const uint32_t model_format = 0x4E4F534A;
+  uint32_t model_length = uint32_t(content.size()) + content_padding_size;
+  swap4(&model_length);
+  uint32_t model_format = 0x4E4F534A;
+  swap4(&model_format);
   stream.write(reinterpret_cast<const char *>(&model_length),
                sizeof(model_length));
   stream.write(reinterpret_cast<const char *>(&model_format),
@@ -7744,8 +7744,10 @@ static bool WriteBinaryGltfStream(std::ostream &stream,
   }
   if (binBuffer.size() > 0) {
     // BIN chunk info, then BIN data
-    const uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size;
-    const uint32_t bin_format = 0x004e4942;
+    uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size;
+    swap4(&bin_length);
+    uint32_t bin_format = 0x004e4942;
+    swap4(&bin_format);
     stream.write(reinterpret_cast<const char *>(&bin_length),
                  sizeof(bin_length));
     stream.write(reinterpret_cast<const char *>(&bin_format),
