-/******************* common useful tools **********************************/
-
-/**
- * translate a pointer to some integer
- * @param ptr the pointer to translate
- * @return an integer
- */
-static inline uint32_t ptr2id(void *ptr)
-{
- return (uint32_t)(((intptr_t)ptr) >> 6);
-}
-
-/******************* serialisation part **********************************/
-
-struct readbuf
-{
- char *head, *end;
-};
-
-#define WRITEBUF_COUNT_MAX 32
-struct writebuf
-{
- struct iovec iovec[WRITEBUF_COUNT_MAX];
- uint32_t uints[WRITEBUF_COUNT_MAX];
- int count;
-};
-
-static char *readbuf_get(struct readbuf *rb, uint32_t length)
-{
- char *before = rb->head;
- char *after = before + length;
- if (after > rb->end)
- return 0;
- rb->head = after;
- return before;
-}
-
-static int readbuf_char(struct readbuf *rb, char *value)
-{
- if (rb->head >= rb->end)
- return 0;
- *value = *rb->head++;
- return 1;
-}
-
-static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
-{
- char *after = rb->head + sizeof *value;
- if (after > rb->end)
- return 0;
- memcpy(value, rb->head, sizeof *value);
- rb->head = after;
- *value = le32toh(*value);
- return 1;
-}
-
-static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
-{
- uint32_t len;
- if (!readbuf_uint32(rb, &len) || !len)
- return 0;
- if (length)
- *length = (size_t)(len - 1);
- return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
-}
-
-static int readbuf_object(struct readbuf *rb, struct json_object **object)
-{
- const char *string;
- struct json_object *o;
- int rc = readbuf_string(rb, &string, NULL);
- if (rc) {
- o = json_tokener_parse(string);
- if (o == NULL && strcmp(string, "null"))
- o = json_object_new_string(string);
- *object = o;
- }
- return rc;
-}
-
-static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
-{
- int i = wb->count;
- if (i == WRITEBUF_COUNT_MAX)
- return 0;
- wb->iovec[i].iov_base = (void*)value;
- wb->iovec[i].iov_len = length;
- wb->count = i + 1;
- return 1;
-}
-
-static int writebuf_char(struct writebuf *wb, char value)
-{
- int i = wb->count;
- if (i == WRITEBUF_COUNT_MAX)
- return 0;
- *(char*)&wb->uints[i] = value;
- wb->iovec[i].iov_base = &wb->uints[i];
- wb->iovec[i].iov_len = 1;
- wb->count = i + 1;
- return 1;
-}
-
-static int writebuf_uint32(struct writebuf *wb, uint32_t value)
-{
- int i = wb->count;
- if (i == WRITEBUF_COUNT_MAX)
- return 0;
- wb->uints[i] = htole32(value);
- wb->iovec[i].iov_base = &wb->uints[i];
- wb->iovec[i].iov_len = sizeof wb->uints[i];
- wb->count = i + 1;
- return 1;
-}
-
-static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
-{
- uint32_t len = (uint32_t)++length;
- return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
-}
-
-static int writebuf_string(struct writebuf *wb, const char *value)
-{
- return writebuf_string_length(wb, value, strlen(value));
-}
-
-static int writebuf_object(struct writebuf *wb, struct json_object *object)
-{
- const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
- return string != NULL && writebuf_string(wb, string);
-}