diff options
Diffstat (limited to 'src/ser.c')
-rw-r--r-- | src/ser.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/ser.c b/src/ser.c new file mode 100644 index 0000000..998c81b --- /dev/null +++ b/src/ser.c @@ -0,0 +1,119 @@ +#include <stdlib.h> +#include <string.h> +#include <endian.h> +#include "ser.h" + +void ser_bytes(strbuf *w, size_t len, uint8_t *x) +{ + while (w->buf.len + len > w->cap) + w->buf.data = realloc(w->buf.data, w->cap = w->cap ? w->cap * 2 : 1); + memcpy(w->buf.data, x, len); + w->buf.len += len; +} + +void ser_str(strbuf *w, str x) +{ + ser_u16(w, x.len); + ser_bytes(w, x.len, (uint8_t *) x.data); +} + +void ser_u8(strbuf *w, uint8_t x) +{ + ser_bytes(w, 1, &x); +} + +void ser_u16(strbuf *w, uint16_t x) +{ + x = htole16(x); + ser_bytes(w, 2, (uint8_t *) &x); +} + +void ser_u32(strbuf *w, uint32_t x) +{ + x = htole32(x); + ser_bytes(w, 4, (uint8_t *) &x); +} + +void ser_u64(strbuf *w, uint64_t x) +{ + x = htole64(x); + ser_bytes(w, 8, (uint8_t *) &x); +} + +#define SER_SIGN(N) void ser_i##N(strbuf *w, int##N##_t x) { ser_u##N(w, x); }; + +SER_SIGN(16) +SER_SIGN(32) +SER_SIGN(64) + +#undef SER_SIGN + +bool deser_bytes(str *r, size_t len, uint8_t *buf) +{ + if (len > r->len) + return false; + + memcpy(buf, r->data, len); + *r = str_advance(*r, len); + return true; +} + +bool deser_str(str *r, str *buf) +{ + uint16_t len; + if (!deser_u16(r, &len)) + return false; + + if (len > r->len) + return false; + + *buf = (str) { len, r->data }; + *r = str_advance(*r, len); + return true; +} + +bool deser_u8(str *r, uint8_t *buf) +{ + return deser_bytes(r, 1, buf); +} + +bool deser_u16(str *r, uint16_t *buf) +{ + if (!deser_bytes(r, 2, (uint8_t *) buf)) + return false; + *buf = le16toh(*buf); + return true; +} + +bool deser_u32(str *r, uint32_t *buf) +{ + if (!deser_bytes(r, 4, (uint8_t *) buf)) + return false; + *buf = le32toh(*buf); + return true; +} + +bool deser_u64(str *r, uint64_t *buf) +{ + if (!deser_bytes(r, 8, (uint8_t *) buf)) + return false; + *buf = le64toh(*buf); + return true; +} + +#define DESER_SIGN(N) \ + bool deser_i##N(str *r, int##N##_t *buf) \ + { \ + uint##N##_t x; \ + if (!deser_u##N(r, &x)) \ + return false; \ + *buf = x; \ + return true; \ + } + +DESER_SIGN(8) +DESER_SIGN(16) +DESER_SIGN(32) +DESER_SIGN(64) + +#undef DESER_SIGN |