commit fce7e20ce7fcfac913de50b6e5280f12ec3f88b0
parent 170eee68bed454db05c0f713765196284663c4f2
Author: Alexandre Bique <bique.alexandre@gmail.com>
Date: Wed, 12 Oct 2016 07:21:13 +0200
Update the serialize helper to support more types
Diffstat:
2 files changed, 125 insertions(+), 4 deletions(-)
diff --git a/include/clap/helpers/serialize.c b/include/clap/helpers/serialize.c
@@ -36,12 +36,42 @@ clap_serializer_bool(struct clap_serializer *s, bool value)
if (!s->out || s->out + 2 > s->out_end)
return false;
s->out[0] = CLAP_SERIALIZE_BOOL;
+ s->out[1] = value ? true : false; /* ensure bit 0 */
+ s->out += 2;
+ return true;
+}
+
+static inline bool
+clap_serializer_int8(struct clap_serializer *s, int8_t value)
+{
+ if (!s->out || s->out + 2 > s->out_end)
+ return false;
+ s->out[0] = CLAP_SERIALIZE_INT8;
s->out[1] = value;
s->out += 2;
return true;
}
static inline void
+clap_serializer_int16_priv(struct clap_serializer *s, int16_t value)
+{
+ s->out[1] = value >> 8;
+ s->out[2] = value;
+}
+
+static inline bool
+clap_serializer_int16(struct clap_serializer *s, int16_t value)
+{
+ if (!s->out || s->out + 3 >= s->out_end)
+ return false;
+
+ *s->out = CLAP_SERIALIZE_INT16;
+ clap_serializer_int16_priv(s, value);
+ s->out += 3;
+ return true;
+}
+
+static inline void
clap_serializer_int32_priv(struct clap_serializer *s, int32_t value)
{
s->out[1] = value >> 24;
@@ -62,6 +92,31 @@ clap_serializer_int32(struct clap_serializer *s, int32_t value)
return true;
}
+static inline void
+clap_serializer_int64_priv(struct clap_serializer *s, int64_t value)
+{
+ s->out[1] = value >> 56;
+ s->out[2] = value >> 48;
+ s->out[3] = value >> 40;
+ s->out[4] = value >> 32;
+ s->out[5] = value >> 24;
+ s->out[6] = value >> 16;
+ s->out[7] = value >> 8;
+ s->out[8] = value;
+}
+
+static inline bool
+clap_serializer_int64(struct clap_serializer *s, int64_t value)
+{
+ if (!s->out || s->out + 9 >= s->out_end)
+ return false;
+
+ *s->out = CLAP_SERIALIZE_INT64;
+ clap_serializer_int64_priv(s, value);
+ s->out += 9;
+ return true;
+}
+
static inline bool
clap_serializer_float(struct clap_serializer *s, float value)
{
@@ -81,6 +136,24 @@ clap_serializer_float(struct clap_serializer *s, float value)
}
static inline bool
+clap_serializer_float(struct clap_serializer *s, double value)
+{
+ if (!s->out || s->out + 9 >= s->out_end)
+ return false;
+
+ union {
+ double f;
+ int64_t i;
+ } u;
+
+ u.f = value;
+ *s->out = CLAP_SERIALIZE_DOUBLE;
+ clap_serializer_int64_priv(s, u.i);
+ s->out += 9;
+ return true;
+}
+
+static inline bool
clap_serializer_str(struct clap_serializer *s,
const char *str,
int32_t len)
@@ -118,6 +191,7 @@ clap_deserialize(struct clap_deserializer *d)
return true;
case CLAP_SERIALIZE_BOOL:
+ case CLAP_SERIALIZE_INT8:
if (d->in + 5 > d->in_end) {
d->type = CLAP_SERIALIZE_EOB;
return false;
@@ -126,16 +200,36 @@ clap_deserialize(struct clap_deserializer *d)
d->in += 2;
return true;
+ case CLAP_SERIALIZE_INT16:
+ if (d->in + 3 > d->in_end) {
+ d->type = CLAP_SERIALIZE_EOB;
+ return false;
+ }
+ d->i16 = (d->in[3] << 8) | d->in[4];
+ d->in += 3;
+ return true;
+
case CLAP_SERIALIZE_INT32:
case CLAP_SERIALIZE_FLOAT:
if (d->in + 5 > d->in_end) {
d->type = CLAP_SERIALIZE_EOB;
return false;
}
- d->i = (d->in[1] << 24) | (d->in[2] << 16) | (d->in[3] << 8) | d->in[4];
+ d->i32 = (d->in[1] << 24) | (d->in[2] << 16) | (d->in[3] << 8) | d->in[4];
d->in += 5;
return true;
+ case CLAP_SERIALIZE_INT64:
+ case CLAP_SERIALIZE_DOUBLE:
+ if (d->in + 9 > d->in_end) {
+ d->type = CLAP_SERIALIZE_EOB;
+ return false;
+ }
+ d->i64 = (d->in[1] << 56) | (d->in[2] << 48) | (d->in[3] << 40) | (d->in[4] << 32) |
+ (d->in[1] << 24) | (d->in[2] << 16) | (d->in[3] << 8) | d->in[4];
+ d->in += 9;
+ return true;
+
case CLAP_SERIALIZE_STR:
if (d->in + 5 > d->in_end) {
d->type = CLAP_SERIALIZE_EOB;
diff --git a/include/clap/helpers/serialize.h b/include/clap/helpers/serialize.h
@@ -17,8 +17,14 @@ enum clap_serialize_type
CLAP_SERIALIZE_END = 'e',
CLAP_SERIALIZE_STR = 's',
CLAP_SERIALIZE_BOOL = 'b',
+ CLAP_SERIALIZE_INT8 = 'c',
+ CLAP_SERIALIZE_INT16 = 'j',
CLAP_SERIALIZE_INT32 = 'i',
+ CLAP_SERIALIZE_INT64 = 'l',
+
+ /* IEEE 754 format */
CLAP_SERIALIZE_FLOAT = 'f',
+ CLAP_SERIALIZE_DOUBLE = 'g',
};
struct clap_serializer
@@ -40,24 +46,41 @@ clap_serializer_array(struct clap_serializer *s);
static inline bool
clap_serializer_end(struct clap_serializer *s);
-/* Pushes a string. Returns false on EOB. */
+/* Pushes a string. Returns false on EOB.
+ * The string might not be null terminated. */
static inline bool
clap_serializer_str(struct clap_serializer *s,
const char *str,
int32_t len);
-/* Pushes an 32 bits integer. Returns false on EOB. */
+/* Pushes an 8 bits integer. Returns false on EOB. */
static inline bool
clap_serializer_bool(struct clap_serializer *s, bool value);
+/* Pushes an 8 bits integer. Returns false on EOB. */
+static inline bool
+clap_serializer_int8(struct clap_serializer *s, int16_t value);
+
+/* Pushes an 16 bits integer. Returns false on EOB. */
+static inline bool
+clap_serializer_int16(struct clap_serializer *s, int16_t value);
+
/* Pushes an 32 bits integer. Returns false on EOB. */
static inline bool
clap_serializer_int32(struct clap_serializer *s, int32_t value);
+/* Pushes an 64 bits integer. Returns false on EOB. */
+static inline bool
+clap_serializer_int64(struct clap_serializer *s, int64_t value);
+
/* Pushes a float. Returns false on EOB. */
static inline bool
clap_serializer_float(struct clap_serializer *s, float value);
+/* Pushes a double. Returns false on EOB. */
+static inline bool
+clap_serializer_double(struct clap_serializer *s, double value);
+
struct clap_deserializer
{
/* input buffer */
@@ -68,8 +91,12 @@ struct clap_deserializer
enum clap_serialize_type type;
union {
bool b;
- int i;
+ int8_t i8;
+ int16_t i16;
+ int32_t i32;
+ int64_t i64;
float f;
+ double d;
const char *s;
};
int32_t slen;