Commit de6135680657b19bd3f633d765f835878886e491

jeremy 2024-12-03T15:48:38

wip serialize_u8

diff --git a/libkc3/deserialize.c b/libkc3/deserialize.c
new file mode 100644
index 0000000..7ce6227
--- /dev/null
+++ b/libkc3/deserialize.c
@@ -0,0 +1,40 @@
+/* kc3_main
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include "assert.h"
+#include "deserialize.h"
+
+sw deserialize_bool (s_buf *buf, bool *dest);
+sw  deserialize_character (s_buf *buf, character *c);
+sw deserialize_list (s_buf *buf, s_list **list);
+sw deserialize_tag (s_buf *buf,  s_tag *tag);
+sw deserialize_tuple (s_buf *buf, s_tuple *tuple);
+sw deserialize_s8 (s_buf *buf, s8 *x);
+sw deserialize_s16 (s_buf *buf, s16 *x);
+sw deserialize_s32 (s_buf *buf, s32 *x);
+sw deserialize_s64 (s_buf *buf, s64 *x);
+sw deserialize_str (s_buf *buf, s_str *str);
+sw deserialize_sw (s_buf *buf, sw *x);
+
+u8 * deserialize_u8 (s_buf *buf, u8 *x)
+{
+  if (buf_read_u8(&buf, x) <= 0)
+    return NULL;
+  return x;
+}
+
+sw deserialize_u16 (s_buf *buf, u16 *x);
+sw deserialize_u32 (s_buf *buf, u32 *x);
+sw deserialize_u64 (s_buf *buf, u64 *x);
+sw deserialize_uw (s_buf *buf, uw *x);
+
+#endif /* LIBKC3_DESERIALIZE_H */
diff --git a/libkc3/deserialize.h b/libkc3/deserialize.h
new file mode 100644
index 0000000..645f489
--- /dev/null
+++ b/libkc3/deserialize.h
@@ -0,0 +1,41 @@
+/* kc3_main
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+/**
+ * @file deserialize.h
+ * @brief Deserialization from blob to tag.
+ *
+ * Serialization is transforming a binary blob into memory data.
+ */
+#ifndef LIBKC3_DESERIALIZE_H
+#define LIBKC3_DESERIALIZE_H
+
+#include "types.h"
+
+sw deserialize_bool (s_buf *buf, bool *dest);
+sw  deserialize_character (s_buf *buf, character *c);
+sw deserialize_list (s_buf *buf, s_list **list);
+sw deserialize_tag (s_buf *buf,  s_tag *tag);
+sw deserialize_tuple (s_buf *buf, s_tuple *tuple);
+sw deserialize_s8 (s_buf *buf, s8 *x);
+sw deserialize_s16 (s_buf *buf, s16 *x);
+sw deserialize_s32 (s_buf *buf, s32 *x);
+sw deserialize_s64 (s_buf *buf, s64 *x);
+sw deserialize_str (s_buf *buf, s_str *str);
+sw deserialize_sw (s_buf *buf, sw *x);
+sw deserialize_u8 (s_buf *buf, u8 *x);
+sw deserialize_u16 (s_buf *buf, u16 *x);
+sw deserialize_u32 (s_buf *buf, u32 *x);
+sw deserialize_u64 (s_buf *buf, u64 *x);
+sw deserialize_uw (s_buf *buf, uw *x);
+
+#endif /* LIBKC3_DESERIALIZE_H */
diff --git a/libkc3/serialize.c b/libkc3/serialize.c
index c8fcb2d..fd2d192 100644
--- a/libkc3/serialize.c
+++ b/libkc3/serialize.c
@@ -11,6 +11,8 @@
  * THIS SOFTWARE.
  */
 #include "assert.h"
+#include "alloc.h"
+#include "buf.h"
 #include "serialize.h"
 
 void serialize_clean (s_serialize *serialize)
@@ -19,6 +21,12 @@ void serialize_clean (s_serialize *serialize)
   (void) serialize;
 }
 
+void serialize_delete (s_serialize *serialize)
+{
+  serialize_clean(serialize);
+  free(serialize);
+}
+
 s_serialize * serialize_init (s_serialize *serialize)
 {
   s_serialize tmp = {0};
@@ -26,6 +34,15 @@ s_serialize * serialize_init (s_serialize *serialize)
   return serialize;
 }
 
+s_serialize * serialize_new ()
+{
+  s_serialize *serialize;
+  if (! (serialize = alloc(sizeof(s_serialize))))
+    return NULL;
+  serialize_init(serialize);
+  return serialize;
+}
+
 s_str * serialize_to_buf (const s_serialize *serialize, s_buf *buf);
 
 s_str * serialize_to_str (const s_serialize *serialize, s_str *dest);
@@ -38,8 +55,16 @@ s_serialize * serialize_character (s_serialize *serialize,
 s_serialize * serialize_list (s_serialize *serialize,
                               const s_list *list);
 
-s_serialize * serialize_tag (s_serialize *serialize,
-                             const s_tag *tag);
+s_serialize * serialize_tag (s_serialize *serialize, const s_tag *tag)
+{
+  switch (tag->type){
+  case TAG_U8: return serialize_u8(serialize, tag->data.u8);
+  default:     break;
+  }
+  err_puts("serialize_tag: not implemented");
+  assert(! "serialize_tag: not implemented");
+  return NULL;
+}
 
 s_serialize * serialize_tuple (s_serialize *serialize,
                                const s_tuple *tuple);
@@ -58,8 +83,9 @@ s_serialize * serialize_sw (s_serialize *serialize, sw x);
 
 s_serialize * serialize_u8 (s_serialize *serialize, u8 x)
 {
+  sw r;
   assert(serialize);
-  if ((r = buf_write_u8(serialize->buf, x)) < 0)
+  if ((r = buf_write_u8(&serialize->buf, x)) < 0)
     return NULL;
   return serialize;
 }