From 51598ba379d31998705f4f991d1606053500f942 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Mon, 17 Jun 2024 00:49:20 +0200 Subject: implement serialization Signed-off-by: Lizzy Fleckenstein --- include/ser.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/str.h | 3 +++ 2 files changed, 48 insertions(+) create mode 100644 include/ser.h (limited to 'include') diff --git a/include/ser.h b/include/ser.h new file mode 100644 index 0000000..fc3451e --- /dev/null +++ b/include/ser.h @@ -0,0 +1,45 @@ +#ifndef SER_H +#define SER_H + +#include +#include +#include +#include "str.h" + +// ser + +void ser_bytes(strbuf *w, size_t len, uint8_t *x); + +void ser_str(strbuf *w, str x); + +void ser_u8(strbuf *w, uint8_t x); +void ser_i8(strbuf *w, int8_t x); + +void ser_u16(strbuf *w, uint16_t x); +void ser_i16(strbuf *w, int16_t x); + +void ser_u32(strbuf *w, uint32_t x); +void ser_i32(strbuf *w, int32_t x); + +void ser_u64(strbuf *w, uint64_t x); +void ser_i64(strbuf *w, int64_t x); + +// deser + +bool deser_bytes(str *r, size_t len, uint8_t *buf); + +bool deser_str(str *r, str *buf); // returns slice! + +bool deser_u8(str *r, uint8_t *buf); +bool deser_i8(str *r, int8_t *buf); + +bool deser_u16(str *r, uint16_t *buf); +bool deser_i16(str *r, int16_t *buf); + +bool deser_u32(str *r, uint32_t *buf); +bool deser_i32(str *r, int32_t *buf); + +bool deser_u64(str *r, uint64_t *buf); +bool deser_i64(str *r, int64_t *buf); + +#endif diff --git a/include/str.h b/include/str.h index f4054a1..b208515 100644 --- a/include/str.h +++ b/include/str.h @@ -19,6 +19,9 @@ typedef array(char) str; #define S(X) ((str) { len(X)-1, X }) #define NILS ((str) { 0, NULL }) +typedef struct { size_t cap; str buf; } strbuf; +#define NILSBUF ((strbuf) { 0, NILS }) + // compares two strings by length and ASCII values. return value: // < 0 if s1 < s2 // = 0 if s1 = s2 -- cgit v1.2.3