From dc65de866caa7de5511973ef97227afa1f9898ea Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 19 May 2020 21:30:12 +0200
Subject: [PATCH] Correct endianness

---
 inc/common/common.h             | 50 ++++++++++++++++--------
 inc/lektor/module/module_sdl2.h |  2 +-
 inc/lektor/module/module_x11.h  |  2 +-
 meson.build                     |  8 ++--
 src/common.c                    | 26 ++++++++++++-
 src/main/server.c               |  8 ++--
 src/module/module_sdl2.c        | 52 +++++++++++++------------
 src/module/module_x11.c         | 68 +++++++++++++--------------------
 8 files changed, 122 insertions(+), 94 deletions(-)

diff --git a/inc/common/common.h b/inc/common/common.h
index faf50a37..dd483dfc 100644
--- a/inc/common/common.h
+++ b/inc/common/common.h
@@ -4,11 +4,6 @@
 #include <unistd.h>
 #include <stdint.h>
 
-/* May be usefull (gcc, see something else for clang):
-   __FLOAT_WORD_ORDER__
-   __ORDER_LITTLE_ENDIAN__
-   __ORDER_BIG_ENDIAN__ */
-
 #define not_implemented() __not_implemented(__func__,__FILE__,__LINE__)
 extern void __not_implemented(const char *func, char *file, int line);
 
@@ -22,23 +17,46 @@ void *safe_zero_malloc(size_t size);
 
 int is_utf8(const char *string);
 
+/* Same as `be_uint64_t` but for Big-endian doubles. The result is a
+   correct endian number. Restriction: bytes must be of length 8.
+   Use the be_uint64_t defined function. */
+double be_double_t(const uint8_t bytes[]);
+
+/* Same as `be_double_t` but for floats. Restriction: bytes must
+   be of length 4. The result is a correct endian number Use the
+   be_uint32_t defined function. */
+float be_float_t(const uint8_t bytes[]);
+
+#if defined (_LITTLE_ENDIAN)
+
+/* Destination: Little-endian */
+#define be_uint32_t be_uint32_t_to_le
+#define be_uint64_t be_uint64_t_to_le
+
 /* Read `bytes` as the big endian representation of a 32-bit unsigned integer.
-   `n` is the number of bytes in `bytes`
+   `n` is the number of bytes in `bytes`. The result is a Little-endian.
    Returns 0 if n is 0.
    Restriction: n <= 4 */
-uint32_t be_uint32_t(const uint8_t bytes[], size_t n);
+uint32_t be_uint32_t_to_le(const uint8_t bytes[], size_t n);
 
-/* Same as `be_uint32_t` but for 64-bit unsigned integers.
-   Restriction: n <= 8 */
-uint64_t be_uint64_t(const uint8_t bytes[], size_t n);
+/* Same as `be_uint32_t` but for 64-bit unsigned integers. The result is a
+   Little-endian. Restriction: n <= 8 */
+uint64_t be_uint64_t_to_le(const uint8_t bytes[], size_t n);
 
-/* Same as `be_uint64_t` but for Big-endian doubles.
-   Restriction: bytes must be of length 8. */
-double be_double_t(const uint8_t bytes[]);
+#else
+#if defined(_BIG_ENDIAN)
 
-/* Same as `be_double_t` but for floats. Restriction: bytes must
-   be of length 4. */
-float be_float_t(const uint8_t bytes[]);
+/* Destination: Big-endian */
+#error "You must define functions to read big endian and store them as big endian."
+#define be_uint32_t be_uint32_t_to_be
+#define be_uint64_t be_uint64_t_to_be
+
+/* Same as the be_to_le versions */
+uint32_t be_uint32_t_to_be(const uint8_t bytes[], size_t n);
+uint64_t be_uint64_t_to_be(const uint8_t bytes[], size_t n);
+
+#endif /* _BIG_ENDIAN    */
+#endif /* _LITTLE_ENDIAN */
 
 /* Trim the string `str` of the char `c` from the right and the left.
    The string may not be null terminated.
diff --git a/inc/lektor/module/module_sdl2.h b/inc/lektor/module/module_sdl2.h
index 30dea574..9a3b7917 100644
--- a/inc/lektor/module/module_sdl2.h
+++ b/inc/lektor/module/module_sdl2.h
@@ -4,7 +4,7 @@
 #include <sqlite3.h>
 #include <lektor/window.h>
 
-#if defined(__STATIC_MODULES)
+#if defined(_STATIC_MODULES)
 /* The only function with a setted filename */
 int load_sdl2(void *mod, struct lkt_state *srv, void *handle);
 #endif
diff --git a/inc/lektor/module/module_x11.h b/inc/lektor/module/module_x11.h
index f1cee93f..14c9fb7e 100644
--- a/inc/lektor/module/module_x11.h
+++ b/inc/lektor/module/module_x11.h
@@ -4,7 +4,7 @@
 #include <sqlite3.h>
 #include <lektor/window.h>
 
-#if defined(__STATIC_MODULES)
+#if defined(_STATIC_MODULES)
 /* The only function with a setted filename */
 int load_x11(void *mod, struct lkt_state *srv, void *handle);
 #endif
diff --git a/meson.build b/meson.build
index 35025c61..c5955d01 100644
--- a/meson.build
+++ b/meson.build
@@ -25,16 +25,16 @@ endif
 if archi == 'unknown'
   archi = run_command('arch').stdout().strip()
 endif
-add_global_arguments('-D_REENTRANT', '-D' + archi + '_ARCH', language: 'c')
+add_global_arguments('-D_REENTRANT', '-D' + archi + '_ARCH', '-D_' + build_machine.endian().to_upper() + '_ENDIAN', language: 'c')
 
 ## Static modules ?
 if get_option('static_modules').enabled()
-  add_global_arguments('-D__STATIC_MODULES', language: 'c')
+  add_global_arguments('-D_STATIC_MODULES', language: 'c')
   if get_option('module_sdl').enabled()
-    add_global_arguments('-D__STATIC_SDL2', language: 'c')
+    add_global_arguments('-D_STATIC_SDL2', language: 'c')
   endif
   if get_option('module_x11').enabled()
-    add_global_arguments('-D__STATIC_X11', language: 'c')
+    add_global_arguments('-D_STATIC_X11', language: 'c')
   endif
 endif
 
diff --git a/src/common.c b/src/common.c
index 59f748da..f1eb6241 100644
--- a/src/common.c
+++ b/src/common.c
@@ -24,8 +24,10 @@ __unused(void *dummy, ...)
     (void) dummy;
 }
 
+/* Endian stuff */
+
 uint32_t
-be_uint32_t(const uint8_t bytes[], size_t n)
+be_uint32_t_to_le(const uint8_t bytes[], size_t n)
 {
     uint32_t res = 0;
     for (size_t i = 0; i < n; i++)
@@ -45,7 +47,7 @@ be_float_t(const uint8_t bytes[])
 }
 
 uint64_t
-be_uint64_t(const uint8_t bytes[], size_t n)
+be_uint64_t_to_le(const uint8_t bytes[], size_t n)
 {
     uint64_t res = 0;
     for (size_t i = 0; i < n; i++)
@@ -64,6 +66,26 @@ be_double_t(const uint8_t bytes[])
     return ret._double;
 }
 
+uint32_t
+be_uint32_t_to_be(const uint8_t bytes[], size_t n)
+{
+    uint32_t res = 0;
+    for (size_t i = 0; i < n; i++)
+        res = (res >> 8u) | bytes[i];
+    return res;
+}
+
+uint64_t
+be_uint64_t_to_be(const uint8_t bytes[], size_t n)
+{
+    uint64_t res = 0;
+    for (size_t i = 0; i < n; i++)
+        res = (res >> 8u) | bytes[i];
+    return res;
+}
+
+/* End of endian stuff */
+
 char *
 trim(char *str, size_t len, char c)
 {
diff --git a/src/main/server.c b/src/main/server.c
index 5f760ed5..0e2f5ded 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -19,19 +19,19 @@
 #include <pwd.h>
 #include <pthread.h>
 
-#ifdef __STATIC_SDL2
+#ifdef _STATIC_SDL2
 #include <lektor/module/module_sdl2.h>
 #endif
-#ifdef __STATIC_X11
+#ifdef _STATIC_X11
 #include <lektor/module/module_x11.h>
 #endif
 
 REG_BEGIN(server_reg)
 REG_ADD(load_repo_https)
-#ifdef __STATIC_SDL2
+#ifdef _STATIC_SDL2
 REG_ADD(load_sdl2)
 #endif
-#ifdef __STATIC_X11
+#ifdef _STATIC_X11
 REG_ADD(load_x11)
 #endif
 REG_END()
diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index faa121c1..45b04f23 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -275,31 +275,6 @@ end:
 
 /* Exported functions */
 
-int
-#if ! defined(__STATIC_MODULES)
-module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
-#else
-load_sdl2(void *arg__, struct lkt_state *srv, void *handle__)
-#endif
-{
-    RETURN_UNLESS(arg__, "Invalid argument", 1);
-    struct lkt_win *win = (struct lkt_win *) arg__;
-    win->srv            = srv;
-
-    win->new            = module_sdl2_new;
-    win->close          = module_sdl2_close;
-    win->free           = module_sdl2_free;
-    win->toggle_pause   = module_sdl2_toggle_pause;
-    win->load_file      = module_sdl2_load_file;
-    win->set_volume     = module_sdl2_set_volume;
-    win->get_duration   = module_sdl2_get_duration;
-    win->get_elapsed    = module_sdl2_get_elapsed;
-    win->handle_events  = module_sdl2_handle_events;
-    win->handle         = handle__;
-
-    return !module_sdl2_new(&srv->win);
-}
-
 bool
 module_sdl2_new(struct lkt_win *const win)
 {
@@ -410,6 +385,33 @@ module_sdl2_handle_events(struct lkt_win *const win, volatile sqlite3 *db,
     return true;
 }
 
+/* The module stuff */
+
+int
+#if ! defined(_STATIC_MODULES)
+module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
+#else
+load_sdl2(void *arg__, struct lkt_state *srv, void *handle__)
+#endif
+{
+    RETURN_UNLESS(arg__, "Invalid argument", 1);
+    struct lkt_win *win = (struct lkt_win *) arg__;
+    win->srv            = srv;
+
+    win->new            = module_sdl2_new;
+    win->close          = module_sdl2_close;
+    win->free           = module_sdl2_free;
+    win->toggle_pause   = module_sdl2_toggle_pause;
+    win->load_file      = module_sdl2_load_file;
+    win->set_volume     = module_sdl2_set_volume;
+    win->get_duration   = module_sdl2_get_duration;
+    win->get_elapsed    = module_sdl2_get_elapsed;
+    win->handle_events  = module_sdl2_handle_events;
+    win->handle         = handle__;
+
+    return !module_sdl2_new(&srv->win);
+}
+
 REG_BEGIN(sdl2_reg)
 REG_ADD_NAMED("new",    module_sdl2_new)
 REG_ADD_NAMED("free",   module_sdl2_free)
diff --git a/src/module/module_x11.c b/src/module/module_x11.c
index 14d7a5ad..fcce96a6 100644
--- a/src/module/module_x11.c
+++ b/src/module/module_x11.c
@@ -39,41 +39,6 @@ struct module_x11_window {
     volatile int mpv_duration;  // Because don't need to be persistent  //
 };
 
-/*
- * NOW: THE ONLY EXPORTED FUNCTION :NOW
- */
-
-int
-#if ! defined(__STATIC_MODULES)
-module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
-#else
-load_x11(void *arg__, struct lkt_state *srv, void *handle__)
-#endif
-{
-    RETURN_UNLESS(arg__, "Invalid arguments", 1);
-    struct lkt_win *win = (struct lkt_win *) arg__;
-    win->srv            = srv;
-
-    win->new            = module_x11_new;
-    win->close          = module_x11_close;
-    win->free           = module_x11_free;
-    win->toggle_pause   = module_x11_toggle_pause;
-    win->load_file      = module_x11_load_file;
-    win->set_volume     = module_x11_set_volume;
-    win->get_duration   = module_x11_get_duration;
-    win->get_elapsed    = module_x11_get_elapsed;
-    win->handle_events  = module_x11_handle_events;
-    win->handle         = handle__;
-
-    return !module_x11_new(&srv->win);
-}
-
-/*
- *
- * NOW: PRIVATE X11 FUNCTIONS :NOW
- *
- */
-
 static inline bool
 lx11_new(struct module_x11_window *ret)
 {
@@ -173,12 +138,6 @@ lx11_handle(struct module_x11_window *win)
     return true;
 }
 
-/*
- *
- * NOW: THE SETTED FUNCTIONS FOR THE WINDOW MODULE :NOW
- *
- */
-
 bool
 module_x11_new(struct lkt_win *const win)
 {
@@ -278,6 +237,33 @@ module_x11_handle_events(struct lkt_win *const win, volatile sqlite3 *db,
                          &xwin->mpv_time_pos, &xwin->mpv_duration);
 }
 
+/* The module stuff */
+
+int
+#if ! defined(_STATIC_MODULES)
+module_set_function(void *arg__, struct lkt_state *srv, void *handle__)
+#else
+load_x11(void *arg__, struct lkt_state *srv, void *handle__)
+#endif
+{
+    RETURN_UNLESS(arg__, "Invalid arguments", 1);
+    struct lkt_win *win = (struct lkt_win *) arg__;
+    win->srv            = srv;
+
+    win->new            = module_x11_new;
+    win->close          = module_x11_close;
+    win->free           = module_x11_free;
+    win->toggle_pause   = module_x11_toggle_pause;
+    win->load_file      = module_x11_load_file;
+    win->set_volume     = module_x11_set_volume;
+    win->get_duration   = module_x11_get_duration;
+    win->get_elapsed    = module_x11_get_elapsed;
+    win->handle_events  = module_x11_handle_events;
+    win->handle         = handle__;
+
+    return !module_x11_new(&srv->win);
+}
+
 REG_BEGIN(x11_reg)
 REG_ADD_NAMED("new",    module_x11_new)
 REG_ADD_NAMED("free",   module_x11_free)
-- 
GitLab