From 8287d20061887e9fd33e038e8f9bf86cf13f2780 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Thu, 13 Mar 2025 01:08:11 +0100
Subject: Store and reload sized binary structures.

---
 src/common/szbin.h | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 113 insertions(+), 2 deletions(-)

diff --git a/src/common/szbin.h b/src/common/szbin.h
index 23aac67..182739a 100644
--- a/src/common/szbin.h
+++ b/src/common/szbin.h
@@ -25,6 +25,7 @@
 #define _COMMON_SZBIN_H
 
 
+#include <assert.h>
 #include <malloc.h>
 #include <string.h>
 
@@ -72,11 +73,22 @@ typedef struct _sized_binary_t
     while (0)
 
 
+#define setup_sized_binary_from_static_string(sb, s)    \
+    do                                                  \
+    {                                                   \
+        (sb)->static_data = s;                          \
+        (sb)->size = strlen(s) + 1;                     \
+    }                                                   \
+    while (0)
+
+
 #define dup_into_sized_binary(sb, d, s)     \
     do                                      \
     {                                       \
-        setup_sized_binary(sb, s);          \
-        memcpy((sb)->data, d, s);           \
+        size_t __size_once;                 \
+        __size_once = s;                    \
+        setup_sized_binary(sb, __size_once);\
+        memcpy((sb)->data, d, __size_once); \
     }                                       \
     while (0)
 
@@ -143,6 +155,11 @@ typedef struct _sized_binary_t
     })
 
 
+
+/**
+ * Conservations et rechargements.
+ */
+
 #define load_sized_binary(sb, f)                                        \
     ({                                                                  \
         uleb128_t __sz;                                                 \
@@ -159,6 +176,23 @@ typedef struct _sized_binary_t
     })
 
 
+#define load_sized_binary_as_string(sb, f)                              \
+    ({                                                                  \
+        uleb128_t __sz;                                                 \
+        bool __ret;                                                     \
+        __ret = load_uleb128(&__sz, f);                                 \
+        if (__ret)                                                      \
+        {                                                               \
+            setup_sized_binary(sb, __sz + 1);                           \
+            __ret = safe_read(f, (sb)->data, __sz);                     \
+            if (!__ret)                                                 \
+                exit_sized_binary(sb);                                  \
+            (sb)->data[__sz] = '\0';                                    \
+        }                                                               \
+        __ret;                                                          \
+    })
+
+
 #define store_sized_binary(sb, f)                                       \
     ({                                                                  \
         bool __ret;                                                     \
@@ -169,5 +203,82 @@ typedef struct _sized_binary_t
     })
 
 
+#define store_sized_binary_as_string(sb, f)                             \
+    ({                                                                  \
+        bool __ret;                                                     \
+        size_t __length;                                                \
+        assert((sb)->size >= 1);                                        \
+        __length = (sb)->size - 1;                                      \
+        assert((sb)->static_data[__length] == '\0');                    \
+        __ret = store_uleb128((const uleb128_t []){ __length }, f);     \
+        if (__ret)                                                      \
+            __ret = safe_write(f, (sb)->static_data, __length);         \
+        __ret;                                                          \
+    })
+
+
+#define unpack_sized_binary(sb, p, m)                                   \
+    ({                                                                  \
+        uleb128_t __sz;                                                 \
+        bool __ret;                                                     \
+        __ret = unpack_uleb128(&__sz, p, m);                            \
+        if (__ret)                                                      \
+        {                                                               \
+            setup_sized_binary(sb, __sz);                               \
+            memcpy((sb)->data, *p, (sb)->size);                         \
+            *((uint8_t **)p) += __sz;                                   \
+        }                                                               \
+        __ret;                                                          \
+    })
+
+
+#define unpack_sized_binary_as_string(sb, p, m)                         \
+    ({                                                                  \
+        uleb128_t __sz;                                                 \
+        bool __ret;                                                     \
+        __ret = unpack_uleb128(&__sz, p, m);                            \
+        if (__ret)                                                      \
+        {                                                               \
+            setup_sized_binary(sb, __sz + 1);                           \
+            memcpy((sb)->data, *p, __sz);                               \
+            (sb)->data[__sz] = '\0';                                    \
+            *((uint8_t **)p) += __sz;                                   \
+        }                                                               \
+        __ret;                                                          \
+    })
+
+
+#define pack_sized_binary(sb, l)                                        \
+    ({                                                                  \
+        uint8_t *__result;                                              \
+        size_t __pos;                                                   \
+        __result = pack_uleb128((const uleb128_t []){ (sb)->size }, l); \
+        __pos = *(l);                                                   \
+        *(l) += (sb)->size;                                             \
+        __result = realloc(__result, *(l) * sizeof(uint8_t));           \
+        memcpy(&__result[__pos], (sb)->static_data,                     \
+               ((sb)->size * sizeof(uint8_t)));                         \
+        __result;                                                       \
+    })
+
+
+#define pack_sized_binary_as_string(sb, l)                              \
+    ({                                                                  \
+        uint8_t *__result;                                              \
+        size_t __length;                                                \
+        size_t __pos;                                                   \
+        assert((sb)->size >= 1);                                        \
+        __length = (sb)->size - 1;                                      \
+        assert((sb)->static_data[__length] == '\0');                    \
+        __result = pack_uleb128((const uleb128_t []){ __length }, l);   \
+        __pos = *(l);                                                   \
+        *(l) += __length;                                               \
+        __result = realloc(__result, *(l) * sizeof(uint8_t));           \
+        memcpy(&__result[__pos], (sb)->static_data,                     \
+               (__length * sizeof(uint8_t)));                           \
+        __result;                                                       \
+    })
+
+
 
 #endif  /* _COMMON_SZBIN_H */
-- 
cgit v0.11.2-87-g4458