rename to "[lib]capnp_c", use lib/ subdir
This commit is contained in:
parent
d3b1cad50f
commit
811d3046e1
12 changed files with 28 additions and 27 deletions
160
lib/capn-list.inc
Normal file
160
lib/capn-list.inc
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
/* capn-list.inc
|
||||
*
|
||||
* Copyright (C) 2013 James McKaskill
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
#define CAT2(A,B) A ## B
|
||||
#define CAT(A,B) CAT2(A, B)
|
||||
#define UINT_T CAT(CAT(uint, SZ), _t)
|
||||
#define LIST_T CAT(capn_list, SZ)
|
||||
#define FLIP CAT(capn_flip, SZ)
|
||||
|
||||
UINT_T CAT(capn_get,SZ) (LIST_T l, int off) {
|
||||
char *d;
|
||||
capn_ptr p = l.p;
|
||||
if (off >= p.len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (p.type) {
|
||||
case CAPN_LIST:
|
||||
if (p.datasz < SZ/8)
|
||||
return 0;
|
||||
d = p.data + off * (p.datasz + 8*p.ptrs);
|
||||
return FLIP(*(UINT_T*)d);
|
||||
|
||||
case CAPN_PTR_LIST:
|
||||
d = struct_ptr(p.seg, p.data + 8*off, SZ/8);
|
||||
if (d) {
|
||||
return FLIP(*(UINT_T*)d);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int CAT(capn_getv,SZ) (LIST_T l, int off, UINT_T *to, int sz) {
|
||||
int i;
|
||||
capn_ptr p = l.p;
|
||||
if (off + sz > p.len) {
|
||||
sz = p.len - off;
|
||||
}
|
||||
|
||||
switch (p.type) {
|
||||
case CAPN_LIST:
|
||||
if (p.datasz == SZ/8 && !p.ptrs && (SZ == 8 || CAPN_LITTLE)) {
|
||||
memcpy(to, p.data + off, sz * (SZ/8));
|
||||
return sz;
|
||||
} else if (p.datasz < SZ/8) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < sz; i++) {
|
||||
char *d = p.data + (i + off) * (p.datasz + 8*p.ptrs);
|
||||
to[i] = FLIP(*(UINT_T*)d);
|
||||
}
|
||||
return sz;
|
||||
|
||||
case CAPN_PTR_LIST:
|
||||
for (i = 0; i < sz; i++) {
|
||||
char *d = struct_ptr(p.seg, p.data + 8*(i+off), SZ/8);
|
||||
if (d) {
|
||||
to[i] = FLIP(*(UINT_T*)d);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int CAT(capn_set,SZ) (LIST_T l, int off, UINT_T v) {
|
||||
char *d;
|
||||
capn_ptr p = l.p;
|
||||
if (off >= p.len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (p.type) {
|
||||
case CAPN_LIST:
|
||||
if (p.datasz < SZ/8)
|
||||
return -1;
|
||||
d = p.data + off * (p.datasz + 8*p.ptrs);
|
||||
*(UINT_T*) d = FLIP(v);
|
||||
return 0;
|
||||
|
||||
case CAPN_PTR_LIST:
|
||||
d = struct_ptr(p.seg, p.data + 8*off, SZ/8);
|
||||
if (!d) {
|
||||
return -1;
|
||||
}
|
||||
*(UINT_T*) d = FLIP(v);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int CAT(capn_setv,SZ) (LIST_T l, int off, const UINT_T *from, int sz) {
|
||||
int i;
|
||||
capn_ptr p = l.p;
|
||||
if (off + sz > p.len) {
|
||||
sz = p.len - off;
|
||||
}
|
||||
|
||||
switch (p.type) {
|
||||
case CAPN_LIST:
|
||||
if (p.datasz == SZ/8 && !p.ptrs && (SZ == 8 || CAPN_LITTLE)) {
|
||||
memcpy(p.data + off, from, sz * (SZ/8));
|
||||
return sz;
|
||||
} else if (p.datasz < SZ/8) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < sz; i++) {
|
||||
char *d = p.data + (i + off) * (p.datasz + 8*p.ptrs);
|
||||
*(UINT_T*) d = FLIP(from[i]);
|
||||
}
|
||||
return sz;
|
||||
|
||||
case CAPN_PTR_LIST:
|
||||
for (i = 0; i < sz; i++) {
|
||||
char *d = struct_ptr(p.seg, p.data + 8*(i+off), SZ/8);
|
||||
if (d) {
|
||||
*(UINT_T*) d = FLIP(from[i]);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
LIST_T CAT(capn_new_list,SZ) (struct capn_segment *seg, int sz) {
|
||||
LIST_T l = {{CAPN_LIST}};
|
||||
l.p.seg = seg;
|
||||
l.p.len = sz;
|
||||
l.p.datasz = SZ/8;
|
||||
new_object(&l.p, sz*(SZ/8));
|
||||
return l;
|
||||
}
|
||||
|
||||
#undef CAT2
|
||||
#undef CAT
|
||||
#undef UINT_T
|
||||
#undef LIST_T
|
||||
#undef FLIP
|
||||
|
||||
394
lib/capn-malloc.c
Normal file
394
lib/capn-malloc.c
Normal file
|
|
@ -0,0 +1,394 @@
|
|||
/* vim: set sw=8 ts=8 sts=8 noet: */
|
||||
/* capn-malloc.c
|
||||
*
|
||||
* Copyright (C) 2013 James McKaskill
|
||||
* Copyright (C) 2014 Steve Dee
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
#include "capnp_c.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
struct check_segment_alignment {
|
||||
unsigned int foo : (sizeof(struct capn_segment)&7) ? -1 : 1;
|
||||
};
|
||||
|
||||
static struct capn_segment *create(void *u, uint32_t id, int sz) {
|
||||
struct capn_segment *s;
|
||||
sz += sizeof(*s);
|
||||
if (sz < 4096) {
|
||||
sz = 4096;
|
||||
} else {
|
||||
sz = (sz + 4095) & ~4095;
|
||||
}
|
||||
s = (struct capn_segment*) calloc(1, sz);
|
||||
s->data = (char*) (s+1);
|
||||
s->cap = sz - sizeof(*s);
|
||||
s->user = s;
|
||||
return s;
|
||||
}
|
||||
|
||||
static struct capn_segment *create_local(void *u, int sz) {
|
||||
return create(u, 0, sz);
|
||||
}
|
||||
|
||||
void capn_init_malloc(struct capn *c) {
|
||||
memset(c, 0, sizeof(*c));
|
||||
c->create = &create;
|
||||
c->create_local = &create_local;
|
||||
}
|
||||
|
||||
void capn_free(struct capn *c) {
|
||||
struct capn_segment *s = c->seglist;
|
||||
while (s != NULL) {
|
||||
struct capn_segment *n = s->next;
|
||||
free(s->user);
|
||||
s = n;
|
||||
}
|
||||
capn_reset_copy(c);
|
||||
}
|
||||
|
||||
void capn_reset_copy(struct capn *c) {
|
||||
struct capn_segment *s = c->copylist;
|
||||
while (s != NULL) {
|
||||
struct capn_segment *n = s->next;
|
||||
free(s->user);
|
||||
s = n;
|
||||
}
|
||||
c->copy = NULL;
|
||||
c->copylist = NULL;
|
||||
}
|
||||
|
||||
#define ZBUF_SZ 4096
|
||||
|
||||
static int read_fp(void *p, size_t sz, FILE *f, struct capn_stream *z, uint8_t* zbuf, int packed) {
|
||||
if (f && packed) {
|
||||
z->next_out = (uint8_t*) p;
|
||||
z->avail_out = sz;
|
||||
|
||||
while (z->avail_out && capn_inflate(z) == CAPN_NEED_MORE) {
|
||||
int r;
|
||||
memmove(zbuf, z->next_in, z->avail_in);
|
||||
r = fread(zbuf+z->avail_in, 1, ZBUF_SZ - z->avail_in, f);
|
||||
if (r <= 0)
|
||||
return -1;
|
||||
z->avail_in += r;
|
||||
}
|
||||
return 0;
|
||||
|
||||
} else if (f && !packed) {
|
||||
return fread(p, sz, 1, f) != 1;
|
||||
|
||||
} else if (packed) {
|
||||
z->next_out = (uint8_t*) p;
|
||||
z->avail_out = sz;
|
||||
return capn_inflate(z) != 0;
|
||||
|
||||
} else {
|
||||
if (z->avail_in < sz)
|
||||
return -1;
|
||||
memcpy(p, z->next_in, sz);
|
||||
z->next_in += sz;
|
||||
z->avail_in -= sz;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int init_fp(struct capn *c, FILE *f, struct capn_stream *z, int packed) {
|
||||
/*
|
||||
* Initialize 'c' from the contents of 'f', assuming the message has been
|
||||
* serialized with the standard framing format. From https://capnproto.org/encoding.html:
|
||||
*
|
||||
* When transmitting over a stream, the following should be sent. All integers are unsigned and little-endian.
|
||||
* (4 bytes) The number of segments, minus one (since there is always at least one segment).
|
||||
* (N * 4 bytes) The size of each segment, in words.
|
||||
* (0 or 4 bytes) Padding up to the next word boundary.
|
||||
* The content of each segment, in order.
|
||||
*/
|
||||
|
||||
struct capn_segment *s = NULL;
|
||||
uint32_t i, segnum, total = 0;
|
||||
uint32_t hdr[1024];
|
||||
uint8_t zbuf[ZBUF_SZ];
|
||||
char *data = NULL;
|
||||
|
||||
capn_init_malloc(c);
|
||||
|
||||
/* Read the first four bytes to know how many headers we have */
|
||||
if (read_fp(&segnum, 4, f, z, zbuf, packed))
|
||||
goto err;
|
||||
|
||||
segnum = capn_flip32(segnum);
|
||||
if (segnum > 1023)
|
||||
goto err;
|
||||
segnum++; /* The wire encoding was zero-based */
|
||||
|
||||
/* Read the header list */
|
||||
if (read_fp(hdr, 8 * (segnum/2) + 4, f, z, zbuf, packed))
|
||||
goto err;
|
||||
|
||||
for (i = 0; i < segnum; i++) {
|
||||
uint32_t n = capn_flip32(hdr[i]);
|
||||
if (n > INT_MAX/8 || n > UINT32_MAX/8 || UINT32_MAX - total < n*8)
|
||||
goto err;
|
||||
hdr[i] = n*8;
|
||||
total += hdr[i];
|
||||
}
|
||||
|
||||
/* Allocate space for the data and the capn_segment structs */
|
||||
s = (struct capn_segment*) calloc(1, total + (sizeof(*s) * segnum));
|
||||
if (!s)
|
||||
goto err;
|
||||
|
||||
/* Now read the data and setup the capn_segment structs */
|
||||
data = (char*) (s+segnum);
|
||||
if (read_fp(data, total, f, z, zbuf, packed))
|
||||
goto err;
|
||||
|
||||
for (i = 0; i < segnum; i++) {
|
||||
s[i].len = s[i].cap = hdr[i];
|
||||
s[i].data = data;
|
||||
data += s[i].len;
|
||||
capn_append_segment(c, &s[i]);
|
||||
}
|
||||
|
||||
/* Set the entire region to be freed on the last segment */
|
||||
s[segnum-1].user = s;
|
||||
|
||||
return 0;
|
||||
|
||||
err:
|
||||
memset(c, 0, sizeof(*c));
|
||||
free(s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int capn_init_fp(struct capn *c, FILE *f, int packed) {
|
||||
struct capn_stream z;
|
||||
memset(&z, 0, sizeof(z));
|
||||
return init_fp(c, f, &z, packed);
|
||||
}
|
||||
|
||||
int capn_init_mem(struct capn *c, const uint8_t *p, size_t sz, int packed) {
|
||||
struct capn_stream z;
|
||||
memset(&z, 0, sizeof(z));
|
||||
z.next_in = p;
|
||||
z.avail_in = sz;
|
||||
return init_fp(c, NULL, &z, packed);
|
||||
}
|
||||
|
||||
static void header_calc(struct capn *c, uint32_t *headerlen, size_t *headersz)
|
||||
{
|
||||
/* segnum == 1:
|
||||
* [segnum][segsiz]
|
||||
* segnum == 2:
|
||||
* [segnum][segsiz][segsiz][zeroes]
|
||||
* segnum == 3:
|
||||
* [segnum][segsiz][segsiz][segsiz]
|
||||
* segnum == 4:
|
||||
* [segnum][segsiz][segsiz][segsiz][segsiz][zeroes]
|
||||
*/
|
||||
*headerlen = ((2 + c->segnum) / 2) * 2;
|
||||
*headersz = 4 * *headerlen;
|
||||
}
|
||||
|
||||
static int header_render(struct capn *c, struct capn_segment *seg, uint32_t *header, uint32_t headerlen, size_t *datasz)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
header[0] = capn_flip32(c->segnum - 1);
|
||||
header[headerlen-1] = 0; /* Zero out the spare position in the header sizes */
|
||||
for (i = 0; i < c->segnum; i++, seg = seg->next) {
|
||||
if (0 == seg)
|
||||
return -1;
|
||||
*datasz += seg->len;
|
||||
header[1 + i] = capn_flip32(seg->len / 8);
|
||||
}
|
||||
if (0 != seg)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int capn_write_mem_packed(struct capn *c, uint8_t *p, size_t sz)
|
||||
{
|
||||
struct capn_segment *seg;
|
||||
struct capn_ptr root;
|
||||
uint32_t headerlen;
|
||||
size_t headersz, datasz = 0;
|
||||
uint32_t *header;
|
||||
struct capn_stream z;
|
||||
int ret;
|
||||
|
||||
root = capn_root(c);
|
||||
header_calc(c, &headerlen, &headersz);
|
||||
header = (uint32_t*) p + headersz + 2; /* must reserve two bytes for worst case expansion */
|
||||
|
||||
if (sz < headersz*2 + 2) /* We must have space for temporary writing of header to deflate */
|
||||
return -1;
|
||||
|
||||
ret = header_render(c, root.seg, header, headerlen, &datasz);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
|
||||
memset(&z, 0, sizeof(z));
|
||||
z.next_in = (uint8_t *)header;
|
||||
z.avail_in = headersz;
|
||||
z.next_out = p;
|
||||
z.avail_out = sz;
|
||||
|
||||
// pack the headers
|
||||
ret = capn_deflate(&z);
|
||||
if (ret != 0 || z.avail_in != 0)
|
||||
return -1;
|
||||
|
||||
for (seg = root.seg; seg; seg = seg->next) {
|
||||
z.next_in = (uint8_t *)seg->data;
|
||||
z.avail_in = seg->len;
|
||||
ret = capn_deflate(&z);
|
||||
if (ret != 0 || z.avail_in != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sz - z.avail_out;
|
||||
}
|
||||
|
||||
int
|
||||
capn_write_mem(struct capn *c, uint8_t *p, size_t sz, int packed)
|
||||
{
|
||||
struct capn_segment *seg;
|
||||
struct capn_ptr root;
|
||||
uint32_t headerlen;
|
||||
size_t headersz, datasz = 0;
|
||||
uint32_t *header;
|
||||
int ret;
|
||||
|
||||
if (c->segnum == 0)
|
||||
return -1;
|
||||
|
||||
if (packed)
|
||||
return capn_write_mem_packed(c, p, sz);
|
||||
|
||||
root = capn_root(c);
|
||||
header_calc(c, &headerlen, &headersz);
|
||||
header = (uint32_t*) p;
|
||||
|
||||
if (sz < headersz)
|
||||
return -1;
|
||||
|
||||
ret = header_render(c, root.seg, header, headerlen, &datasz);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
|
||||
if (sz < headersz + datasz)
|
||||
return -1;
|
||||
|
||||
p += headersz;
|
||||
|
||||
for (seg = root.seg; seg; seg = seg->next) {
|
||||
memcpy(p, seg->data, seg->len);
|
||||
p += seg->len;
|
||||
}
|
||||
|
||||
return headersz+datasz;
|
||||
}
|
||||
|
||||
static int _write_fd(ssize_t (*write_fd)(int fd, void *p, size_t count), int fd, void *p, size_t count)
|
||||
{
|
||||
ssize_t ret;
|
||||
size_t sent = 0;
|
||||
|
||||
while (sent < count) {
|
||||
ret = write_fd(fd, ((uint8_t*)p)+sent, count-sent);
|
||||
if (ret < 0) {
|
||||
if (errno == EAGAIN || errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
sent += ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int capn_write_fd(struct capn *c, ssize_t (*write_fd)(int fd, void *p, size_t count), int fd, int packed)
|
||||
{
|
||||
unsigned char buf[4096];
|
||||
struct capn_segment *seg;
|
||||
struct capn_ptr root;
|
||||
uint32_t headerlen;
|
||||
size_t headersz, datasz = 0;
|
||||
int ret;
|
||||
struct capn_stream z;
|
||||
unsigned char *p;
|
||||
|
||||
if (c->segnum == 0)
|
||||
return -1;
|
||||
|
||||
root = capn_root(c);
|
||||
header_calc(c, &headerlen, &headersz);
|
||||
|
||||
if (sizeof(buf) < headersz)
|
||||
return -1;
|
||||
|
||||
ret = header_render(c, root.seg, (uint32_t*)buf, headerlen, &datasz);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
|
||||
if (packed) {
|
||||
const int headerrem = sizeof(buf) - headersz;
|
||||
const int maxpack = headersz + 2;
|
||||
if (headerrem < maxpack)
|
||||
return -1;
|
||||
|
||||
memset(&z, 0, sizeof(z));
|
||||
z.next_in = buf;
|
||||
z.avail_in = headersz;
|
||||
z.next_out = buf + headersz;
|
||||
z.avail_out = headerrem;
|
||||
ret = capn_deflate(&z);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
|
||||
p = buf + headersz;
|
||||
headersz = headerrem - z.avail_out;
|
||||
} else {
|
||||
p = buf;
|
||||
}
|
||||
|
||||
ret = _write_fd(write_fd, fd, p, headersz);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
datasz = headersz;
|
||||
for (seg = root.seg; seg; seg = seg->next) {
|
||||
size_t bufsz;
|
||||
if (packed) {
|
||||
memset(&z, 0, sizeof(z));
|
||||
z.next_in = (uint8_t*)seg->data;
|
||||
z.avail_in = seg->len;
|
||||
z.next_out = buf;
|
||||
z.avail_out = sizeof(buf);
|
||||
ret = capn_deflate(&z);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
p = buf;
|
||||
bufsz = sizeof(buf) - z.avail_out;
|
||||
} else {
|
||||
p = (uint8_t*)seg->data;
|
||||
bufsz = seg->len;
|
||||
}
|
||||
ret = _write_fd(write_fd, fd, p, bufsz);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
datasz += bufsz;
|
||||
}
|
||||
|
||||
return datasz;
|
||||
}
|
||||
201
lib/capn-stream.c
Normal file
201
lib/capn-stream.c
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
/* vim: set sw=8 ts=8 sts=8 noet: */
|
||||
/* capn-stream.c
|
||||
*
|
||||
* Copyright (C) 2013 James McKaskill
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
#include "capnp_c.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifndef min
|
||||
static unsigned min(unsigned a, unsigned b) { return (a < b) ? a : b; }
|
||||
#endif
|
||||
|
||||
int capn_deflate(struct capn_stream* s) {
|
||||
if (s->avail_in % 8) {
|
||||
return CAPN_MISALIGNED;
|
||||
}
|
||||
|
||||
while (s->avail_in) {
|
||||
int i;
|
||||
size_t sz;
|
||||
uint8_t hdr = 0;
|
||||
uint8_t *p;
|
||||
|
||||
if (!s->avail_out)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
if (s->raw > 0) {
|
||||
sz = min(s->raw, min(s->avail_in, s->avail_out));
|
||||
memcpy(s->next_out, s->next_in, sz);
|
||||
s->next_out += sz;
|
||||
s->next_in += sz;
|
||||
s->avail_out -= sz;
|
||||
s->avail_in -= sz;
|
||||
s->raw -= sz;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (s->avail_in < 8)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
sz = 0;
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (s->next_in[i]) {
|
||||
sz ++;
|
||||
hdr |= 1 << i;
|
||||
}
|
||||
}
|
||||
|
||||
switch (sz) {
|
||||
case 0:
|
||||
if (s->avail_out < 2)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
s->next_out[0] = 0;
|
||||
for (sz = 1; sz < min(s->avail_in/8, 256); sz++) {
|
||||
if (((uint64_t*) s->next_in)[sz] != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
s->next_out[1] = (uint8_t) (sz-1);
|
||||
s->next_in += sz*8;
|
||||
s->avail_in -= sz*8;
|
||||
s->next_out += 2;
|
||||
s->avail_out -= 2;
|
||||
continue;
|
||||
|
||||
case 8:
|
||||
if (s->avail_out < 10)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
s->next_out[0] = 0xFF;
|
||||
memcpy(s->next_out+1, s->next_in, 8);
|
||||
s->next_in += 8;
|
||||
s->avail_in -= 8;
|
||||
|
||||
s->raw = min(s->avail_in, 256*8);
|
||||
if ((p = (uint8_t*) memchr(s->next_in, 0, s->raw)) != NULL) {
|
||||
s->raw = (p - s->next_in) & ~7;
|
||||
}
|
||||
|
||||
s->next_out[9] = (uint8_t) (s->raw/8);
|
||||
s->next_out += 10;
|
||||
s->avail_out -= 10;
|
||||
continue;
|
||||
|
||||
default:
|
||||
if (s->avail_out < 1U + sz)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
*(s->next_out++) = hdr;
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (s->next_in[i]) {
|
||||
*(s->next_out++) = s->next_in[i];
|
||||
}
|
||||
}
|
||||
s->avail_out -= sz + 1;
|
||||
s->next_in += 8;
|
||||
s->avail_in -= 8;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int capn_inflate(struct capn_stream* s) {
|
||||
if (s->avail_out % 8) {
|
||||
return CAPN_MISALIGNED;
|
||||
}
|
||||
|
||||
while (s->avail_out) {
|
||||
int i;
|
||||
size_t sz;
|
||||
uint8_t hdr;
|
||||
|
||||
if (s->zeros > 0) {
|
||||
sz = min(s->avail_out, s->zeros);
|
||||
memset(s->next_out, 0, sz);
|
||||
s->next_out += sz;
|
||||
s->avail_out -= sz;
|
||||
s->zeros -= sz;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (s->raw > 0) {
|
||||
if (s->avail_in == 0)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
sz = min(min(s->avail_out, s->raw), s->avail_in);
|
||||
memcpy(s->next_out, s->next_in, sz);
|
||||
s->next_in += sz;
|
||||
s->next_out += sz;
|
||||
s->avail_in -= sz;
|
||||
s->avail_out -= sz;
|
||||
s->raw -= sz;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (s->avail_in == 0)
|
||||
return 0;
|
||||
else if (s->avail_in < 2)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
switch (s->next_in[0]) {
|
||||
case 0xFF:
|
||||
/* 0xFF is followed by 8 bytes raw, followed by
|
||||
* a byte with length in words to read raw */
|
||||
if (s->avail_in < 10)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
memcpy(s->next_out, s->next_in+1, 8);
|
||||
s->raw = s->next_in[9] * 8;
|
||||
s->next_in += 10;
|
||||
s->avail_in -= 10;
|
||||
s->next_out += 8;
|
||||
s->avail_out -= 8;
|
||||
continue;
|
||||
|
||||
case 0x00:
|
||||
/* 0x00 is followed by a single byte indicating
|
||||
* the count of consecutive zero value words
|
||||
* minus 1 */
|
||||
s->zeros = (s->next_in[1] + 1) * 8;
|
||||
s->next_in += 2;
|
||||
s->avail_in -= 2;
|
||||
continue;
|
||||
|
||||
default:
|
||||
hdr = s->next_in[0];
|
||||
sz = 0;
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (hdr & (1 << i))
|
||||
sz++;
|
||||
}
|
||||
if (s->avail_in < 1U + sz)
|
||||
return CAPN_NEED_MORE;
|
||||
|
||||
s->next_in += 1;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (hdr & (1 << i)) {
|
||||
*(s->next_out++) = *(s->next_in++);
|
||||
} else {
|
||||
*(s->next_out++) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
s->avail_out -= 8;
|
||||
s->avail_in -= 1 + sz;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
1108
lib/capn.c
Normal file
1108
lib/capn.c
Normal file
File diff suppressed because it is too large
Load diff
422
lib/capnp_c.h
Normal file
422
lib/capnp_c.h
Normal file
|
|
@ -0,0 +1,422 @@
|
|||
/* vim: set sw=8 ts=8 sts=8 noet: */
|
||||
/* capnp_c.h
|
||||
*
|
||||
* Copyright (C) 2013 James McKaskill
|
||||
* Copyright (C) 2014 Steve Dee
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
#ifndef CAPNP_C_H
|
||||
#define CAPNP_C_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
|
||||
#define CAPN_INLINE static inline
|
||||
#else
|
||||
#define CAPN_INLINE static
|
||||
#endif
|
||||
|
||||
#define CAPN_VERSION 1
|
||||
|
||||
/* struct capn is a common structure shared between segments in the same
|
||||
* session/context so that far pointers between segments will be created.
|
||||
*
|
||||
* lookup is used to lookup segments by id when derefencing a far pointer
|
||||
*
|
||||
* create is used to create or lookup an alternate segment that has at least
|
||||
* sz available (ie returned seg->len + sz <= seg->cap)
|
||||
*
|
||||
* create_local is used to create a segment for the copy tree and should be
|
||||
* allocated in the local memory space.
|
||||
*
|
||||
* Allocated segments must be zero initialized.
|
||||
*
|
||||
* create and lookup can be NULL if you don't need multiple segments and don't
|
||||
* want to support copying
|
||||
*
|
||||
* seglist and copylist are linked lists which can be used to free up segments
|
||||
* on cleanup, but should not be modified by the user.
|
||||
*
|
||||
* lookup, create, create_local, and user can be set by the user. Other values
|
||||
* should be zero initialized.
|
||||
*/
|
||||
struct capn {
|
||||
/* user settable */
|
||||
struct capn_segment *(*lookup)(void* /*user*/, uint32_t /*id */);
|
||||
struct capn_segment *(*create)(void* /*user*/, uint32_t /*id */, int /*sz*/);
|
||||
struct capn_segment *(*create_local)(void* /*user*/, int /*sz*/);
|
||||
void *user;
|
||||
/* zero initialized, user should not modify */
|
||||
uint32_t segnum;
|
||||
struct capn_tree *copy;
|
||||
struct capn_tree *segtree;
|
||||
struct capn_segment *seglist, *lastseg;
|
||||
struct capn_segment *copylist;
|
||||
};
|
||||
|
||||
/* struct capn_tree is a rb tree header used internally for the segment id
|
||||
* lookup and copy tree */
|
||||
struct capn_tree {
|
||||
struct capn_tree *parent, *link[2];
|
||||
unsigned int red : 1;
|
||||
};
|
||||
|
||||
struct capn_tree *capn_tree_insert(struct capn_tree *root, struct capn_tree *n);
|
||||
|
||||
/* struct capn_segment contains the information about a single segment.
|
||||
*
|
||||
* capn points to a struct capn that is shared between segments in the
|
||||
* same session
|
||||
*
|
||||
* id specifies the segment id, used for far pointers
|
||||
*
|
||||
* data specifies the segment data. This should not move after creation.
|
||||
*
|
||||
* len specifies the current segment length. This is 0 for a blank
|
||||
* segment.
|
||||
*
|
||||
* cap specifies the segment capacity.
|
||||
*
|
||||
* When creating new structures len will be incremented until it reaces cap,
|
||||
* at which point a new segment will be requested via capn->create. The
|
||||
* create callback can either create a new segment or expand an existing
|
||||
* one by incrementing cap and returning the expanded segment.
|
||||
*
|
||||
* data, len, and cap must all by 8 byte aligned
|
||||
*
|
||||
* data, len, cap, and user should all set by the user. Other values
|
||||
* should be zero initialized.
|
||||
*/
|
||||
struct capn_segment {
|
||||
struct capn_tree hdr;
|
||||
struct capn_segment *next;
|
||||
struct capn *capn;
|
||||
uint32_t id;
|
||||
/* user settable */
|
||||
char *data;
|
||||
size_t len, cap;
|
||||
void *user;
|
||||
};
|
||||
|
||||
enum CAPN_TYPE {
|
||||
CAPN_NULL = 0,
|
||||
CAPN_STRUCT = 1,
|
||||
CAPN_LIST = 2,
|
||||
CAPN_PTR_LIST = 3,
|
||||
CAPN_BIT_LIST = 4,
|
||||
CAPN_FAR_POINTER = 5,
|
||||
};
|
||||
|
||||
struct capn_ptr {
|
||||
unsigned int type : 4;
|
||||
unsigned int has_ptr_tag : 1;
|
||||
unsigned int is_list_member : 1;
|
||||
unsigned int is_composite_list : 1;
|
||||
unsigned int datasz : 19;
|
||||
unsigned int ptrs : 16;
|
||||
int len;
|
||||
char *data;
|
||||
struct capn_segment *seg;
|
||||
};
|
||||
|
||||
struct capn_text {
|
||||
int len;
|
||||
const char *str;
|
||||
struct capn_segment *seg;
|
||||
};
|
||||
|
||||
typedef struct capn_ptr capn_ptr;
|
||||
typedef struct capn_text capn_text;
|
||||
typedef struct {capn_ptr p;} capn_data;
|
||||
typedef struct {capn_ptr p;} capn_list1;
|
||||
typedef struct {capn_ptr p;} capn_list8;
|
||||
typedef struct {capn_ptr p;} capn_list16;
|
||||
typedef struct {capn_ptr p;} capn_list32;
|
||||
typedef struct {capn_ptr p;} capn_list64;
|
||||
|
||||
struct capn_msg {
|
||||
struct capn_segment *seg;
|
||||
uint64_t iface;
|
||||
uint16_t method;
|
||||
capn_ptr args;
|
||||
};
|
||||
|
||||
/* capn_append_segment appends a segment to a session */
|
||||
void capn_append_segment(struct capn*, struct capn_segment*);
|
||||
|
||||
capn_ptr capn_root(struct capn *c);
|
||||
void capn_resolve(capn_ptr *p);
|
||||
|
||||
#define capn_len(list) ((list).p.type == CAPN_FAR_POINTER ? (capn_resolve(&(list).p), (list).p.len) : (list).p.len)
|
||||
|
||||
/* capn_getp|setp functions get/set ptrs in list/structs
|
||||
* off is the list index or pointer index in a struct
|
||||
* capn_setp will copy the data, create far pointers, etc if the target
|
||||
* is in a different segment/context.
|
||||
* Both of these will use/return inner pointers for composite lists.
|
||||
*/
|
||||
capn_ptr capn_getp(capn_ptr p, int off, int resolve);
|
||||
int capn_setp(capn_ptr p, int off, capn_ptr tgt);
|
||||
|
||||
capn_text capn_get_text(capn_ptr p, int off, capn_text def);
|
||||
capn_data capn_get_data(capn_ptr p, int off);
|
||||
int capn_set_text(capn_ptr p, int off, capn_text tgt);
|
||||
|
||||
/* capn_get* functions get data from a list
|
||||
* The length of the list is given by p->size
|
||||
* off specifies how far into the list to start
|
||||
* sz indicates the number of elements to get
|
||||
* The function returns the number of elements read or -1 on an error.
|
||||
* off must be byte aligned for capn_getv1
|
||||
*/
|
||||
int capn_get1(capn_list1 p, int off);
|
||||
uint8_t capn_get8(capn_list8 p, int off);
|
||||
uint16_t capn_get16(capn_list16 p, int off);
|
||||
uint32_t capn_get32(capn_list32 p, int off);
|
||||
uint64_t capn_get64(capn_list64 p, int off);
|
||||
int capn_getv1(capn_list1 p, int off, uint8_t *data, int sz);
|
||||
int capn_getv8(capn_list8 p, int off, uint8_t *data, int sz);
|
||||
int capn_getv16(capn_list16 p, int off, uint16_t *data, int sz);
|
||||
int capn_getv32(capn_list32 p, int off, uint32_t *data, int sz);
|
||||
int capn_getv64(capn_list64 p, int off, uint64_t *data, int sz);
|
||||
|
||||
/* capn_set* functions set data in a list
|
||||
* off specifies how far into the list to start
|
||||
* sz indicates the number of elements to write
|
||||
* The function returns the number of elemnts written or -1 on an error.
|
||||
* off must be byte aligned for capn_setv1
|
||||
*/
|
||||
int capn_set1(capn_list1 p, int off, int v);
|
||||
int capn_set8(capn_list8 p, int off, uint8_t v);
|
||||
int capn_set16(capn_list16 p, int off, uint16_t v);
|
||||
int capn_set32(capn_list32 p, int off, uint32_t v);
|
||||
int capn_set64(capn_list64 p, int off, uint64_t v);
|
||||
int capn_setv1(capn_list1 p, int off, const uint8_t *data, int sz);
|
||||
int capn_setv8(capn_list8 p, int off, const uint8_t *data, int sz);
|
||||
int capn_setv16(capn_list16 p, int off, const uint16_t *data, int sz);
|
||||
int capn_setv32(capn_list32 p, int off, const uint32_t *data, int sz);
|
||||
int capn_setv64(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
|
||||
* On an error a CAPN_NULL pointer is returned
|
||||
*/
|
||||
capn_ptr capn_new_struct(struct capn_segment *seg, int datasz, int ptrs);
|
||||
capn_ptr capn_new_interface(struct capn_segment *seg, int datasz, int ptrs);
|
||||
capn_ptr capn_new_ptr_list(struct capn_segment *seg, int sz);
|
||||
capn_ptr capn_new_list(struct capn_segment *seg, int sz, int datasz, int ptrs);
|
||||
capn_list1 capn_new_list1(struct capn_segment *seg, int sz);
|
||||
capn_list8 capn_new_list8(struct capn_segment *seg, int sz);
|
||||
capn_list16 capn_new_list16(struct capn_segment *seg, int sz);
|
||||
capn_list32 capn_new_list32(struct capn_segment *seg, int sz);
|
||||
capn_list64 capn_new_list64(struct capn_segment *seg, int sz);
|
||||
|
||||
/* capn_read|write* functions read/write struct values
|
||||
* off is the offset into the structure in bytes
|
||||
* Rarely should these be called directly, instead use the generated code.
|
||||
* Data must be xored with the default value
|
||||
* These are inlined
|
||||
*/
|
||||
CAPN_INLINE uint8_t capn_read8(capn_ptr p, int off);
|
||||
CAPN_INLINE uint16_t capn_read16(capn_ptr p, int off);
|
||||
CAPN_INLINE uint32_t capn_read32(capn_ptr p, int off);
|
||||
CAPN_INLINE uint64_t capn_read64(capn_ptr p, int off);
|
||||
CAPN_INLINE int capn_write1(capn_ptr p, int off, int val);
|
||||
CAPN_INLINE int capn_write8(capn_ptr p, int off, uint8_t val);
|
||||
CAPN_INLINE int capn_write16(capn_ptr p, int off, uint16_t val);
|
||||
CAPN_INLINE int capn_write32(capn_ptr p, int off, uint32_t val);
|
||||
CAPN_INLINE int capn_write64(capn_ptr p, int off, uint64_t val);
|
||||
|
||||
/* capn_init_malloc inits the capn struct with a create function which
|
||||
* allocates segments on the heap using malloc
|
||||
*
|
||||
* capn_init_(fp|mem) inits by reading segments in from the file/memory buffer
|
||||
* in serialized form (optionally packed). It will then setup the create
|
||||
* function ala capn_init_malloc so that further segments can be created.
|
||||
*
|
||||
* capn_free frees all the segment headers and data created by the create
|
||||
* function setup by capn_init_*
|
||||
*/
|
||||
void capn_init_malloc(struct capn *c);
|
||||
int capn_init_fp(struct capn *c, FILE *f, int packed);
|
||||
int capn_init_mem(struct capn *c, const uint8_t *p, size_t sz, int packed);
|
||||
|
||||
/* capn_write_(fp|mem) writes segments to the file/memory buffer in
|
||||
* serialized form and returns the number of bytes written.
|
||||
*/
|
||||
/* TODO */
|
||||
/*int capn_write_fp(struct capn *c, FILE *f, int packed);*/
|
||||
int capn_write_fd(struct capn *c, ssize_t (*write_fd)(int fd, void *p, size_t count), int fd, int packed);
|
||||
int capn_write_mem(struct capn *c, uint8_t *p, size_t sz, int packed);
|
||||
|
||||
void capn_free(struct capn *c);
|
||||
void capn_reset_copy(struct capn *c);
|
||||
|
||||
/* capn_stream encapsulates the needed fields for capn_(deflate|inflate) in a
|
||||
* similar manner to z_stream from zlib
|
||||
*
|
||||
* The user should set next_in, avail_in, next_out, avail_out to the
|
||||
* available in/out buffers before calling capn_(deflate|inflate).
|
||||
*
|
||||
* Other fields should be zero initialized.
|
||||
*/
|
||||
struct capn_stream {
|
||||
const uint8_t *next_in;
|
||||
size_t avail_in;
|
||||
uint8_t *next_out;
|
||||
size_t avail_out;
|
||||
unsigned zeros, raw;
|
||||
};
|
||||
|
||||
#define CAPN_MISALIGNED -1
|
||||
#define CAPN_NEED_MORE -2
|
||||
|
||||
/* capn_deflate deflates a stream to the packed format
|
||||
* capn_inflate inflates a stream from the packed format
|
||||
*
|
||||
* Returns:
|
||||
* CAPN_MISALIGNED - if the unpacked data is not 8 byte aligned
|
||||
* CAPN_NEED_MORE - more packed data/room is required (out for inflate, in for
|
||||
* deflate)
|
||||
* 0 - success, all output for inflate, all input for deflate processed
|
||||
*/
|
||||
int capn_deflate(struct capn_stream*);
|
||||
int capn_inflate(struct capn_stream*);
|
||||
|
||||
/* Inline functions */
|
||||
|
||||
|
||||
CAPN_INLINE uint8_t capn_flip8(uint8_t v) {
|
||||
return v;
|
||||
}
|
||||
CAPN_INLINE uint16_t capn_flip16(uint16_t v) {
|
||||
union { uint16_t u; uint8_t v[2]; } s;
|
||||
s.v[0] = (uint8_t)v;
|
||||
s.v[1] = (uint8_t)(v>>8);
|
||||
return s.u;
|
||||
}
|
||||
CAPN_INLINE uint32_t capn_flip32(uint32_t v) {
|
||||
union { uint32_t u; uint8_t v[4]; } s;
|
||||
s.v[0] = (uint8_t)v;
|
||||
s.v[1] = (uint8_t)(v>>8);
|
||||
s.v[2] = (uint8_t)(v>>16);
|
||||
s.v[3] = (uint8_t)(v>>24);
|
||||
return s.u;
|
||||
}
|
||||
CAPN_INLINE uint64_t capn_flip64(uint64_t v) {
|
||||
union { uint64_t u; uint8_t v[8]; } s;
|
||||
s.v[0] = (uint8_t)v;
|
||||
s.v[1] = (uint8_t)(v>>8);
|
||||
s.v[2] = (uint8_t)(v>>16);
|
||||
s.v[3] = (uint8_t)(v>>24);
|
||||
s.v[4] = (uint8_t)(v>>32);
|
||||
s.v[5] = (uint8_t)(v>>40);
|
||||
s.v[6] = (uint8_t)(v>>48);
|
||||
s.v[7] = (uint8_t)(v>>56);
|
||||
return s.u;
|
||||
}
|
||||
|
||||
CAPN_INLINE int capn_write1(capn_ptr p, int off, int val) {
|
||||
if (off >= p.datasz*8) {
|
||||
return -1;
|
||||
} else if (val) {
|
||||
((uint8_t*)p.data)[off/8] |= 1 << (off%8);
|
||||
return 0;
|
||||
} else {
|
||||
((uint8_t*)p.data)[off/8] &= ~(1 << (off%8));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
CAPN_INLINE uint8_t capn_read8(capn_ptr p, int off) {
|
||||
return off+1 <= p.datasz ? capn_flip8(*(uint8_t*) (p.data+off)) : 0;
|
||||
}
|
||||
CAPN_INLINE int capn_write8(capn_ptr p, int off, uint8_t val) {
|
||||
if (off+1 <= p.datasz) {
|
||||
*(uint8_t*) (p.data+off) = capn_flip8(val);
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
CAPN_INLINE uint16_t capn_read16(capn_ptr p, int off) {
|
||||
return off+2 <= p.datasz ? capn_flip16(*(uint16_t*) (p.data+off)) : 0;
|
||||
}
|
||||
CAPN_INLINE int capn_write16(capn_ptr p, int off, uint16_t val) {
|
||||
if (off+2 <= p.datasz) {
|
||||
*(uint16_t*) (p.data+off) = capn_flip16(val);
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
CAPN_INLINE uint32_t capn_read32(capn_ptr p, int off) {
|
||||
return off+4 <= p.datasz ? capn_flip32(*(uint32_t*) (p.data+off)) : 0;
|
||||
}
|
||||
CAPN_INLINE int capn_write32(capn_ptr p, int off, uint32_t val) {
|
||||
if (off+4 <= p.datasz) {
|
||||
*(uint32_t*) (p.data+off) = capn_flip32(val);
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
CAPN_INLINE uint64_t capn_read64(capn_ptr p, int off) {
|
||||
return off+8 <= p.datasz ? capn_flip64(*(uint64_t*) (p.data+off)) : 0;
|
||||
}
|
||||
CAPN_INLINE int capn_write64(capn_ptr p, int off, uint64_t val) {
|
||||
if (off+8 <= p.datasz) {
|
||||
*(uint64_t*) (p.data+off) = capn_flip64(val);
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
union capn_conv_f32 {
|
||||
uint32_t u;
|
||||
float f;
|
||||
};
|
||||
|
||||
union capn_conv_f64 {
|
||||
uint64_t u;
|
||||
double f;
|
||||
};
|
||||
|
||||
CAPN_INLINE float capn_to_f32(uint32_t v) {
|
||||
union capn_conv_f32 u;
|
||||
u.u = v;
|
||||
return u.f;
|
||||
}
|
||||
CAPN_INLINE double capn_to_f64(uint64_t v) {
|
||||
union capn_conv_f64 u;
|
||||
u.u = v;
|
||||
return u.f;
|
||||
}
|
||||
CAPN_INLINE uint32_t capn_from_f32(float v) {
|
||||
union capn_conv_f32 u;
|
||||
u.f = v;
|
||||
return u.u;
|
||||
}
|
||||
CAPN_INLINE uint64_t capn_from_f64(double v) {
|
||||
union capn_conv_f64 u;
|
||||
u.f = v;
|
||||
return u.u;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue