diff --git a/capn-list.c b/capn-list.c index e46a8da..3ef7ada 100644 --- a/capn-list.c +++ b/capn-list.c @@ -2,10 +2,11 @@ #define CAT2(A,B) A ## B #define CAT(A,B) CAT2(A, B) #define UINT_T CAT(CAT(uint, SZ), _t) -#define FLIP CAT(capn_flip_, SZ) +#define FLIP CAT(capn_flip, SZ) -int CAT(capn_read_,SZ) (const struct capn_ptr *p, int off, UINT_T *to, int sz) { +int CAT(capn_read,SZ) (const struct CAT(capn_list,SZ) *list, int off, UINT_T *to, int sz) { int i; + const struct capn_ptr *p = &list->p; if (off + sz > p->size) { sz = p->size - off; @@ -42,8 +43,9 @@ int CAT(capn_read_,SZ) (const struct capn_ptr *p, int off, UINT_T *to, int sz) { } } -int CAT(capn_write_,SZ) (struct capn_ptr *p, int off, const UINT_T *from, int sz) { +int CAT(capn_write,SZ) (struct CAT(capn_list,SZ) *list, int off, const UINT_T *from, int sz) { int i; + struct capn_ptr *p = &list->p; if (off + sz > p->size) { sz = p->size - off; diff --git a/capn.c b/capn.c index 6ec98d7..a982fe8 100644 --- a/capn.c +++ b/capn.c @@ -51,8 +51,8 @@ static uint64_t lookup_far(struct capn_segment **s, char **d, uint64_t val) { return 0; } - far = capn_flip_64(*(uint64_t*) p); - tag = capn_flip_64(*(uint64_t*) (p+8)); + far = capn_flip64(*(uint64_t*) p); + tag = capn_flip64(*(uint64_t*) (p+8)); /* the far tag should not be another double, and the tag * should be struct/list and have no offset */ @@ -72,12 +72,12 @@ static uint64_t lookup_far(struct capn_segment **s, char **d, uint64_t val) { } *d = (*s)->data + off; - return capn_flip_64(*(uint64_t*) *d); + return capn_flip64(*(uint64_t*) *d); } } static char *struct_ptr(struct capn_segment *s, char *d) { - uint64_t val = capn_flip_64(*(uint64_t*)d); + uint64_t val = capn_flip64(*(uint64_t*)d); uint16_t datasz; if ((val&3) == FAR_PTR) { @@ -133,7 +133,7 @@ struct capn_ptr capn_read_ptr(const struct capn_ptr *p, int off) { goto err; } - val = capn_flip_64(*(uint64_t*) d); + val = capn_flip64(*(uint64_t*) d); ret.seg = p->seg; if ((val&3) == FAR_PTR) { @@ -193,7 +193,7 @@ struct capn_ptr capn_read_ptr(const struct capn_ptr *p, int off) { goto err; } - val = capn_flip_64(*(uint64_t*) d); + val = capn_flip64(*(uint64_t*) d); d += 8; e = d + ret.size * 8; @@ -255,15 +255,15 @@ static uint64_t ptr_value(const struct capn_ptr *p, int off) { break; } - return capn_flip_64(val); + return capn_flip64(val); } static void write_far_ptr(char *d, struct capn_segment *s, char *tgt) { - *(uint64_t*) d = capn_flip_64(FAR_PTR | U64(tgt - s->data) | (U64(s->id) << 32)); + *(uint64_t*) d = capn_flip64(FAR_PTR | U64(tgt - s->data) | (U64(s->id) << 32)); } static void write_double_far(char *d, struct capn_segment *s, char *tgt) { - *(uint64_t*) d = capn_flip_64(FAR_PTR | 4 | U64(tgt - s->data) | (U64(s->id) << 32)); + *(uint64_t*) d = capn_flip64(FAR_PTR | 4 | U64(tgt - s->data) | (U64(s->id) << 32)); } static void write_ptr_tag(char *d, const struct capn_ptr *p, int off) { @@ -351,7 +351,7 @@ static int write_ptr(struct capn_segment *s, char *d, const struct capn_ptr *p) } } -int capn_write_ptr(struct capn_ptr *p, int off, struct capn_ptr *tgt) { +int capn_write_ptr(struct capn_ptr *p, int off, const struct capn_ptr *tgt) { struct capn_ptr inner; switch (p->type) { @@ -445,9 +445,10 @@ int capn_copy(struct capn_ptr *t, const struct capn_ptr *f) { } } -int capn_read_1(const struct capn_ptr *p, int off, uint8_t *data, int sz) { +int capn_read1(const struct capn_list1 *list, int off, uint8_t *data, int sz) { /* Note we only support aligned reads */ int bsz; + const struct capn_ptr *p = &list->p; if (p->type != CAPN_BIT_LIST || (off & 7) != 0) return -1; @@ -463,9 +464,10 @@ int capn_read_1(const struct capn_ptr *p, int off, uint8_t *data, int sz) { } } -int capn_write_1(struct capn_ptr *p, int off, const uint8_t *data, int sz) { +int capn_write1(struct capn_list1 *list, int off, const uint8_t *data, int sz) { /* Note we only support aligned writes */ int bsz; + const struct capn_ptr *p = &list->p; if (p->type != CAPN_BIT_LIST || (off & 7) != 0) return -1; @@ -600,3 +602,59 @@ char *capn_to_string(const struct capn_ptr *p, int *psz) { if (psz) *psz = p->size - 1; return p->data; } + +struct capn_text capn_read_text(const struct capn_ptr *p, int off) { + struct capn_text ret; + if (p->type == CAPN_LIST && p->datasz == 1 && p->size && p->data[p->size - 1] == 0) { + ret.seg = p->seg; + ret.str = p->data; + ret.size = p->size - 1; + } else { + ret.seg = NULL; + ret.str = NULL; + ret.size = 0; + } + return ret; +} + +struct capn_data capn_read_data(const struct capn_ptr *p, int off) { + struct capn_data ret; + if (p->type == CAPN_LIST && p->datasz == 1) { + ret.seg = p->seg; + ret.data = (uint8_t*) p->data; + ret.size = p->size; + } else { + ret.seg = NULL; + ret.data = NULL; + ret.size = 0; + } + return ret; +} + +int capn_write_text(struct capn_ptr *p, int off, struct capn_text tgt) { + struct capn_ptr m; + if (tgt.str) { + m.type = CAPN_LIST; + m.size = (tgt.size >= 0 ? tgt.size : strlen(tgt.str)) + 1; + m.seg = tgt.seg; + m.data = (char*)tgt.str; + m.datasz = 1; + } else { + m.type = CAPN_NULL; + } + return capn_write_ptr(p, off, &m); +} + +int capn_write_data(struct capn_ptr *p, int off, struct capn_data tgt) { + struct capn_ptr m; + if (tgt.data) { + m.type = CAPN_LIST; + m.data = (char*)tgt.data; + m.size = tgt.size; + m.datasz = 1; + m.seg = tgt.seg; + } else { + m.type = CAPN_NULL; + } + return capn_write_ptr(p, off, &m); +} diff --git a/capn.h b/capn.h index 40096ee..9f228b3 100644 --- a/capn.h +++ b/capn.h @@ -58,17 +58,23 @@ struct capn_ptr { uint32_t ptrsz; }; -struct capn_string { - struct capn_ptr p; - const char *str; +struct capn_text { int size; + const char *str; + struct capn_segment *seg; }; -struct capn_data{struct capn_ptr p;}; -struct capn_list_8{struct capn_ptr p;}; -struct capn_list_16{struct capn_ptr p;}; -struct capn_list_32{struct capn_ptr p;}; -struct capn_list_64{struct capn_ptr p;}; +struct capn_data { + int size; + const uint8_t *data; + struct capn_segment *seg; +}; + +struct capn_list1{struct capn_ptr p;}; +struct capn_list8{struct capn_ptr p;}; +struct capn_list16{struct capn_ptr p;}; +struct capn_list32{struct capn_ptr p;}; +struct capn_list64{struct capn_ptr p;}; /* capn_read|write_ptr functions read/write ptrs to list/structs * off is the list index or pointer index in a struct @@ -77,14 +83,17 @@ struct capn_list_64{struct capn_ptr p;}; * Both of these will use/return inner pointers for composite lists. */ struct capn_ptr capn_read_ptr(const struct capn_ptr *p, int off); -int capn_write_ptr(struct capn_ptr *p, int off, struct capn_ptr *tgt); +int capn_write_ptr(struct capn_ptr *p, int off, const struct capn_ptr *tgt); /* capn_to_string returns a pointer to a string * Use this instead of accessing the data directly as these checks that the * string is null terminated, the list type, etc. * psz is filled out with the string length if non NULL */ -char *capn_to_string(const struct capn_string *p, int *psz); +struct capn_text capn_read_text(const struct capn_ptr *p, int off); +struct capn_data capn_read_data(const struct capn_ptr *p, int off); +int capn_write_text(struct capn_ptr *p, int off, struct capn_text tgt); +int capn_write_data(struct capn_ptr *p, int off, struct capn_data tgt); /* capn_copy copies data from 'from' to 'to' * returns 0 on success, non-zero on error (type mismatch, allocation error, @@ -99,11 +108,11 @@ int capn_copy(struct capn_ptr *to, const struct capn_ptr *from); * The function returns the number of elements read or -1 on an error. * off must be byte aligned for capn_read_1 */ -int capn_read_1(const struct capn_list_1 *p, int off, uint8_t *data, int sz); -int capn_read_8(const struct capn_list_8 *p, int off, uint8_t *data, int sz); -int capn_read_16(const struct capn_list_16 *p, int off, uint16_t *data, int sz); -int capn_read_32(const struct capn_list_32 *p, int off, uint32_t *data, int sz); -int capn_read_64(const struct capn_list_64 *p, int off, uint64_t *data, int sz); +int capn_read_1(const struct capn_list1 *p, int off, uint8_t *data, int sz); +int capn_read8(const struct capn_list8 *p, int off, uint8_t *data, int sz); +int capn_read16(const struct capn_list16 *p, int off, uint16_t *data, int sz); +int capn_read32(const struct capn_list32 *p, int off, uint32_t *data, int sz); +int capn_read64(const struct capn_list64 *p, int off, uint64_t *data, int sz); /* capn_write_* function write data to a list * off specifies how far into the list to start @@ -111,11 +120,11 @@ int capn_read_64(const struct capn_list_64 *p, int off, uint64_t *data, int sz); * The function returns the number of elemnts written or -1 on an error. * off must be byte aligned for capn_read_1 */ -int capn_write_1(struct capn_list_1 *p, int off, const uint8_t *data, int sz); -int capn_write_8(struct capn_list_8 *p, int off, const uint8_t *data, int sz); -int capn_write_16(struct capn_list_16 *p, int off, const uint16_t *data, int sz); -int capn_write_32(struct capn_list_32 *p, int off, const uint32_t *data, int sz); -int capn_write_64(struct capn_list_64 *p, int off, const uint64_t *data, int sz); +int capn_write_1(struct capn_list1 *p, int off, const uint8_t *data, int sz); +int capn_write8(struct capn_list8 *p, int off, const uint8_t *data, int sz); +int capn_write16(struct capn_list16 *p, int off, const uint16_t *data, int sz); +int capn_write32(struct capn_list32 *p, int off, const uint32_t *data, int sz); +int capn_write64(struct capn_list64 *p, int off, const uint64_t *data, int sz); /* capn_new_* functions create a new object * datasz is in bytes, ptrs is # of pointers, sz is # of elements in the list @@ -134,14 +143,14 @@ struct capn_ptr capn_new_string(struct capn_segment *seg, const char *str, int s * Data must be xored with the default value * These are static in order to be inlined. */ -static uint8_t capn_get_8(const struct capn_ptr *p, int off); -static uint16_t capn_get_16(const struct capn_ptr *p, int off); -static uint32_t capn_get_32(const struct capn_ptr *p, int off); -static uint64_t capn_get_64(const struct capn_ptr *p, int off); -static int capn_set_8(struct capn_ptr *p, int off, uint8_t val); -static int capn_set_16(struct capn_ptr *p, int off, uint16_t val); -static int capn_set_32(struct capn_ptr *p, int off, uint32_t val); -static int capn_set_64(struct capn_ptr *p, int off, uint64_t val); +static uint8_t capn_get8(const struct capn_ptr *p, int off); +static uint16_t capn_get16(const struct capn_ptr *p, int off); +static uint32_t capn_get32(const struct capn_ptr *p, int off); +static uint64_t capn_get64(const struct capn_ptr *p, int off); +static int capn_set8(struct capn_ptr *p, int off, uint8_t val); +static int capn_set16(struct capn_ptr *p, int off, uint16_t val); +static int capn_set32(struct capn_ptr *p, int off, uint32_t val); +static int capn_set64(struct capn_ptr *p, int off, uint64_t val); @@ -150,94 +159,100 @@ static int capn_set_64(struct capn_ptr *p, int off, uint64_t val); #define T(IDX) s.v[IDX] = (uint8_t) (v >> (8*IDX)) -static uint8_t capn_flip_8(uint8_t v) { +static uint8_t capn_flip8(uint8_t v) { return v; } -static uint16_t capn_flip_16(uint16_t v) { +static uint16_t capn_flip16(uint16_t v) { union { uint16_t u; uint8_t v[2]; } s; T(0); T(1); return s.u; } -static uint32_t capn_flip_32(uint32_t v) { +static uint32_t capn_flip32(uint32_t v) { union { uint32_t u; uint16_t v[2]; } s; T(0); T(1); T(2); T(3); return s.u; } -static uint64_t capn_flip_64(uint64_t v) { +static uint64_t capn_flip64(uint64_t v) { union { uint64_t u; uint32_t v[2]; } s; T(0); T(1); T(2); T(3); T(4); T(5); T(6); T(7); return s.u; } #undef T -static uint8_t capn_get_8(const struct capn_ptr *p, int off) { - return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip_8(*(uint8_t*) p->data) : 0; +static uint8_t capn_get8(const struct capn_ptr *p, int off) { + return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip8(*(uint8_t*) p->data) : 0; } -static int capn_set_8(struct capn_ptr *p, int off, uint8_t val) { +static int capn_set8(struct capn_ptr *p, int off, uint8_t val) { if (p->type == CAPN_STRUCT && off < p->datasz) { - *(uint8_t*) p->data = capn_flip_8(val); + *(uint8_t*) p->data = capn_flip8(val); return 0; } else { return -1; } } -static uint16_t capn_get_16(const struct capn_ptr *p, int off) { - return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip_16(*(uint16_t*) p->data) : 0; +static uint16_t capn_get16(const struct capn_ptr *p, int off) { + return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip16(*(uint16_t*) p->data) : 0; } -static int capn_set_16(struct capn_ptr *p, int off, uint16_t val) { +static int capn_set16(struct capn_ptr *p, int off, uint16_t val) { if (p->type == CAPN_STRUCT && off < p->datasz) { - *(uint16_t*) p->data = capn_flip_16(val); + *(uint16_t*) p->data = capn_flip16(val); return 0; } else { return -1; } } -static uint32_t capn_get_32(const struct capn_ptr *p, int off) { - return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip_32(*(uint32_t*) p->data) : 0; +static uint32_t capn_get32(const struct capn_ptr *p, int off) { + return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip32(*(uint32_t*) p->data) : 0; } -static int capn_set_32(struct capn_ptr *p, int off, uint32_t val) { +static int capn_set32(struct capn_ptr *p, int off, uint32_t val) { if (p->type == CAPN_STRUCT && off < p->datasz) { - *(uint32_t*) p->data = capn_flip_32(val); + *(uint32_t*) p->data = capn_flip32(val); return 0; } else { return -1; } } -static uint64_t capn_get_64(const struct capn_ptr *p, int off) { - return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip_64(*(uint64_t*) p->data) : 0; +static uint64_t capn_get64(const struct capn_ptr *p, int off) { + return (p->type == CAPN_STRUCT && off < p->datasz) ? capn_flip64(*(uint64_t*) p->data) : 0; } -static int capn_set_64(struct capn_ptr *p, int off, uint64_t val) { +static int capn_set64(struct capn_ptr *p, int off, uint64_t val) { if (p->type == CAPN_STRUCT && off < p->datasz) { - *(uint64_t*) p->data = capn_flip_64(val); + *(uint64_t*) p->data = capn_flip64(val); return 0; } else { return -1; } } -static float capn_get_float(const struct capn_ptr *p, int off) { +static float capn_get_float(const struct capn_ptr *p, int off, float def) { union { float f; uint32_t u;} u; - u.u = capn_get_32(p, off); + u.f = def; + u.u ^= capn_get32(p, off); return u.f; } -static int capn_set_float(struct capn_ptr *p, int off, float f) { +static int capn_set_float(struct capn_ptr *p, int off, float f, float def) { union { float f; uint32_t u;} u; + union { float f; uint32_t u;} d; u.f = f; - return capn_set_32(p, off, u.u); + d.f = def; + return capn_set32(p, off, u.u ^ d.u); } -static double capn_get_double(const struct capn_ptr *p, int off) { +static double capn_get_double(const struct capn_ptr *p, int off, double def) { union { double f; uint64_t u;} u; - u.u = capn_get_64(p, off); + u.f = def; + u.u ^= capn_get64(p, off); return u.f; } -static int capn_set_double(struct capn_ptr *p, int off, double f) { +static int capn_set_double(struct capn_ptr *p, int off, double f, double def) { union { double f; uint64_t u;} u; + union { double f; uint64_t u;} d; + d.f = f; u.f = f; - return capn_set_64(p, off, u.u); + return capn_set64(p, off, u.u ^ d.u); } #endif