summaryrefslogtreecommitdiffstats
path: root/third_party/msgpack/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /third_party/msgpack/src
parentInitial commit. (diff)
downloadthunderbird-upstream.tar.xz
thunderbird-upstream.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--third_party/msgpack/src/objectc.c547
-rw-r--r--third_party/msgpack/src/unpack.c676
-rw-r--r--third_party/msgpack/src/version.c22
-rw-r--r--third_party/msgpack/src/vrefbuffer.c232
-rw-r--r--third_party/msgpack/src/zone.c222
5 files changed, 1699 insertions, 0 deletions
diff --git a/third_party/msgpack/src/objectc.c b/third_party/msgpack/src/objectc.c
new file mode 100644
index 0000000000..2722b63415
--- /dev/null
+++ b/third_party/msgpack/src/objectc.c
@@ -0,0 +1,547 @@
+/*
+ * MessagePack for C dynamic typing routine
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+#include "msgpack/object.h"
+#include "msgpack/pack.h"
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+
+#if defined(_MSC_VER)
+#if _MSC_VER >= 1800
+#include <inttypes.h>
+#else
+#define PRIu64 "I64u"
+#define PRIi64 "I64i"
+#define PRIi8 "i"
+#endif
+#else
+#include <inttypes.h>
+#endif
+
+
+int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
+{
+ switch(d.type) {
+ case MSGPACK_OBJECT_NIL:
+ return msgpack_pack_nil(pk);
+
+ case MSGPACK_OBJECT_BOOLEAN:
+ if(d.via.boolean) {
+ return msgpack_pack_true(pk);
+ } else {
+ return msgpack_pack_false(pk);
+ }
+
+ case MSGPACK_OBJECT_POSITIVE_INTEGER:
+ return msgpack_pack_uint64(pk, d.via.u64);
+
+ case MSGPACK_OBJECT_NEGATIVE_INTEGER:
+ return msgpack_pack_int64(pk, d.via.i64);
+
+ case MSGPACK_OBJECT_FLOAT32:
+ return msgpack_pack_float(pk, (float)d.via.f64);
+
+ case MSGPACK_OBJECT_FLOAT64:
+ return msgpack_pack_double(pk, d.via.f64);
+
+ case MSGPACK_OBJECT_STR:
+ {
+ int ret = msgpack_pack_str(pk, d.via.str.size);
+ if(ret < 0) { return ret; }
+ return msgpack_pack_str_body(pk, d.via.str.ptr, d.via.str.size);
+ }
+
+ case MSGPACK_OBJECT_BIN:
+ {
+ int ret = msgpack_pack_bin(pk, d.via.bin.size);
+ if(ret < 0) { return ret; }
+ return msgpack_pack_bin_body(pk, d.via.bin.ptr, d.via.bin.size);
+ }
+
+ case MSGPACK_OBJECT_EXT:
+ {
+ int ret = msgpack_pack_ext(pk, d.via.ext.size, d.via.ext.type);
+ if(ret < 0) { return ret; }
+ return msgpack_pack_ext_body(pk, d.via.ext.ptr, d.via.ext.size);
+ }
+
+ case MSGPACK_OBJECT_ARRAY:
+ {
+ int ret = msgpack_pack_array(pk, d.via.array.size);
+ if(ret < 0) {
+ return ret;
+ }
+ else {
+ msgpack_object* o = d.via.array.ptr;
+ msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
+ for(; o != oend; ++o) {
+ ret = msgpack_pack_object(pk, *o);
+ if(ret < 0) { return ret; }
+ }
+
+ return 0;
+ }
+ }
+
+ case MSGPACK_OBJECT_MAP:
+ {
+ int ret = msgpack_pack_map(pk, d.via.map.size);
+ if(ret < 0) {
+ return ret;
+ }
+ else {
+ msgpack_object_kv* kv = d.via.map.ptr;
+ msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size;
+ for(; kv != kvend; ++kv) {
+ ret = msgpack_pack_object(pk, kv->key);
+ if(ret < 0) { return ret; }
+ ret = msgpack_pack_object(pk, kv->val);
+ if(ret < 0) { return ret; }
+ }
+
+ return 0;
+ }
+ }
+
+ default:
+ return -1;
+ }
+}
+
+
+static void msgpack_object_bin_print(FILE* out, const char *ptr, size_t size)
+{
+ size_t i;
+ for (i = 0; i < size; ++i) {
+ if (ptr[i] == '"') {
+ fputs("\\\"", out);
+ } else if (isprint((unsigned char)ptr[i])) {
+ fputc(ptr[i], out);
+ } else {
+ fprintf(out, "\\x%02x", (unsigned char)ptr[i]);
+ }
+ }
+}
+
+static int msgpack_object_bin_print_buffer(char *buffer, size_t buffer_size, const char *ptr, size_t size)
+{
+ size_t i;
+ char *aux_buffer = buffer;
+ size_t aux_buffer_size = buffer_size;
+ int ret;
+
+ for (i = 0; i < size; ++i) {
+ if (ptr[i] == '"') {
+ ret = snprintf(aux_buffer, aux_buffer_size, "\\\"");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ } else if (isprint((unsigned char)ptr[i])) {
+ if (aux_buffer_size > 0) {
+ memcpy(aux_buffer, ptr + i, 1);
+ aux_buffer = aux_buffer + 1;
+ aux_buffer_size = aux_buffer_size - 1;
+ }
+ } else {
+ ret = snprintf(aux_buffer, aux_buffer_size, "\\x%02x", (unsigned char)ptr[i]);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ }
+ }
+
+ return buffer_size - aux_buffer_size;
+}
+
+
+void msgpack_object_print(FILE* out, msgpack_object o)
+{
+ switch(o.type) {
+ case MSGPACK_OBJECT_NIL:
+ fprintf(out, "nil");
+ break;
+
+ case MSGPACK_OBJECT_BOOLEAN:
+ fprintf(out, (o.via.boolean ? "true" : "false"));
+ break;
+
+ case MSGPACK_OBJECT_POSITIVE_INTEGER:
+#if defined(PRIu64)
+ fprintf(out, "%" PRIu64, o.via.u64);
+#else
+ if (o.via.u64 > ULONG_MAX)
+ fprintf(out, "over 4294967295");
+ else
+ fprintf(out, "%lu", (unsigned long)o.via.u64);
+#endif
+ break;
+
+ case MSGPACK_OBJECT_NEGATIVE_INTEGER:
+#if defined(PRIi64)
+ fprintf(out, "%" PRIi64, o.via.i64);
+#else
+ if (o.via.i64 > LONG_MAX)
+ fprintf(out, "over +2147483647");
+ else if (o.via.i64 < LONG_MIN)
+ fprintf(out, "under -2147483648");
+ else
+ fprintf(out, "%ld", (signed long)o.via.i64);
+#endif
+ break;
+
+ case MSGPACK_OBJECT_FLOAT32:
+ case MSGPACK_OBJECT_FLOAT64:
+ fprintf(out, "%f", o.via.f64);
+ break;
+
+ case MSGPACK_OBJECT_STR:
+ fprintf(out, "\"");
+ fwrite(o.via.str.ptr, o.via.str.size, 1, out);
+ fprintf(out, "\"");
+ break;
+
+ case MSGPACK_OBJECT_BIN:
+ fprintf(out, "\"");
+ msgpack_object_bin_print(out, o.via.bin.ptr, o.via.bin.size);
+ fprintf(out, "\"");
+ break;
+
+ case MSGPACK_OBJECT_EXT:
+#if defined(PRIi8)
+ fprintf(out, "(ext: %" PRIi8 ")", o.via.ext.type);
+#else
+ fprintf(out, "(ext: %d)", (int)o.via.ext.type);
+#endif
+ fprintf(out, "\"");
+ msgpack_object_bin_print(out, o.via.ext.ptr, o.via.ext.size);
+ fprintf(out, "\"");
+ break;
+
+ case MSGPACK_OBJECT_ARRAY:
+ fprintf(out, "[");
+ if(o.via.array.size != 0) {
+ msgpack_object* p = o.via.array.ptr;
+ msgpack_object* const pend = o.via.array.ptr + o.via.array.size;
+ msgpack_object_print(out, *p);
+ ++p;
+ for(; p < pend; ++p) {
+ fprintf(out, ", ");
+ msgpack_object_print(out, *p);
+ }
+ }
+ fprintf(out, "]");
+ break;
+
+ case MSGPACK_OBJECT_MAP:
+ fprintf(out, "{");
+ if(o.via.map.size != 0) {
+ msgpack_object_kv* p = o.via.map.ptr;
+ msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size;
+ msgpack_object_print(out, p->key);
+ fprintf(out, "=>");
+ msgpack_object_print(out, p->val);
+ ++p;
+ for(; p < pend; ++p) {
+ fprintf(out, ", ");
+ msgpack_object_print(out, p->key);
+ fprintf(out, "=>");
+ msgpack_object_print(out, p->val);
+ }
+ }
+ fprintf(out, "}");
+ break;
+
+ default:
+ // FIXME
+#if defined(PRIu64)
+ fprintf(out, "#<UNKNOWN %i %" PRIu64 ">", o.type, o.via.u64);
+#else
+ if (o.via.u64 > ULONG_MAX)
+ fprintf(out, "#<UNKNOWN %i over 4294967295>", o.type);
+ else
+ fprintf(out, "#<UNKNOWN %i %lu>", o.type, (unsigned long)o.via.u64);
+#endif
+
+ }
+}
+
+int msgpack_object_print_buffer(char *buffer, size_t buffer_size, msgpack_object o)
+{
+ char *aux_buffer = buffer;
+ size_t aux_buffer_size = buffer_size;
+ int ret;
+ switch(o.type) {
+ case MSGPACK_OBJECT_NIL:
+ ret = snprintf(aux_buffer, aux_buffer_size, "nil");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ case MSGPACK_OBJECT_BOOLEAN:
+ ret = snprintf(aux_buffer, aux_buffer_size, (o.via.boolean ? "true" : "false"));
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ case MSGPACK_OBJECT_POSITIVE_INTEGER:
+#if defined(PRIu64)
+ ret = snprintf(aux_buffer, aux_buffer_size, "%" PRIu64, o.via.u64);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+#else
+ if (o.via.u64 > ULONG_MAX) {
+ ret = snprintf(aux_buffer, aux_buffer_size, "over 4294967295");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ } else {
+ ret = snprintf(aux_buffer, aux_buffer_size, "%lu", (unsigned long)o.via.u64);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ }
+#endif
+ break;
+
+ case MSGPACK_OBJECT_NEGATIVE_INTEGER:
+#if defined(PRIi64)
+ ret = snprintf(aux_buffer, aux_buffer_size, "%" PRIi64, o.via.i64);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+#else
+ if (o.via.i64 > LONG_MAX) {
+ ret = snprintf(aux_buffer, aux_buffer_size, "over +2147483647");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ } else if (o.via.i64 < LONG_MIN) {
+ ret = snprintf(aux_buffer, aux_buffer_size, "under -2147483648");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ } else {
+ ret = snprintf(aux_buffer, aux_buffer_size, "%ld", (signed long)o.via.i64);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ }
+#endif
+ break;
+
+ case MSGPACK_OBJECT_FLOAT32:
+ case MSGPACK_OBJECT_FLOAT64:
+ ret = snprintf(aux_buffer, aux_buffer_size, "%f", o.via.f64);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ case MSGPACK_OBJECT_STR:
+ ret = snprintf(aux_buffer, aux_buffer_size, "\"");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = snprintf(aux_buffer, aux_buffer_size, "%.*s", (int)o.via.str.size, o.via.str.ptr);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = snprintf(aux_buffer, aux_buffer_size, "\"");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ case MSGPACK_OBJECT_BIN:
+ ret = snprintf(aux_buffer, aux_buffer_size, "\"");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+
+ ret = msgpack_object_bin_print_buffer(aux_buffer, aux_buffer_size, o.via.bin.ptr, o.via.bin.size);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+
+ ret = snprintf(aux_buffer, aux_buffer_size, "\"");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ case MSGPACK_OBJECT_EXT:
+#if defined(PRIi8)
+ ret = snprintf(aux_buffer, aux_buffer_size, "(ext: %" PRIi8 ")", o.via.ext.type);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+#else
+ ret = snprintf(aux_buffer, aux_buffer_size, "(ext: %d)", (int)o.via.ext.type);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+#endif
+ ret = snprintf(aux_buffer, aux_buffer_size, "\"");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+
+ ret = msgpack_object_bin_print_buffer(aux_buffer, aux_buffer_size, o.via.ext.ptr, o.via.ext.size);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+
+ ret = snprintf(aux_buffer, aux_buffer_size, "\"");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ case MSGPACK_OBJECT_ARRAY:
+ ret = snprintf(aux_buffer, aux_buffer_size, "[");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ if(o.via.array.size != 0) {
+ msgpack_object* p = o.via.array.ptr;
+ msgpack_object* const pend = o.via.array.ptr + o.via.array.size;
+ ret = msgpack_object_print_buffer(aux_buffer, aux_buffer_size, *p);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ++p;
+ for(; p < pend; ++p) {
+ ret = snprintf(aux_buffer, aux_buffer_size, ", ");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = msgpack_object_print_buffer(aux_buffer, aux_buffer_size, *p);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ }
+ }
+ ret = snprintf(aux_buffer, aux_buffer_size, "]");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ case MSGPACK_OBJECT_MAP:
+ ret = snprintf(aux_buffer, aux_buffer_size, "{");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ if(o.via.map.size != 0) {
+ msgpack_object_kv* p = o.via.map.ptr;
+ msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size;
+ ret = msgpack_object_print_buffer(aux_buffer, aux_buffer_size, p->key);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = snprintf(aux_buffer, aux_buffer_size, "=>");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = msgpack_object_print_buffer(aux_buffer, aux_buffer_size, p->val);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ++p;
+ for(; p < pend; ++p) {
+ ret = snprintf(aux_buffer, aux_buffer_size, ", ");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = msgpack_object_print_buffer(aux_buffer, aux_buffer_size, p->key);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = snprintf(aux_buffer, aux_buffer_size, "=>");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ ret = msgpack_object_print_buffer(aux_buffer, aux_buffer_size, p->val);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ }
+ }
+ ret = snprintf(aux_buffer, aux_buffer_size, "}");
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ break;
+
+ default:
+ // FIXME
+#if defined(PRIu64)
+ ret = snprintf(aux_buffer, aux_buffer_size, "#<UNKNOWN %i %" PRIu64 ">", o.type, o.via.u64);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+#else
+ if (o.via.u64 > ULONG_MAX) {
+ ret = snprintf(aux_buffer, aux_buffer_size, "#<UNKNOWN %i over 4294967295>", o.type);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ } else {
+ ret = snprintf(aux_buffer, aux_buffer_size, "#<UNKNOWN %i %lu>", o.type, (unsigned long)o.via.u64);
+ aux_buffer = aux_buffer + ret;
+ aux_buffer_size = aux_buffer_size - ret;
+ }
+#endif
+ }
+
+ return buffer_size - aux_buffer_size;
+}
+
+
+bool msgpack_object_equal(const msgpack_object x, const msgpack_object y)
+{
+ if(x.type != y.type) { return false; }
+
+ switch(x.type) {
+ case MSGPACK_OBJECT_NIL:
+ return true;
+
+ case MSGPACK_OBJECT_BOOLEAN:
+ return x.via.boolean == y.via.boolean;
+
+ case MSGPACK_OBJECT_POSITIVE_INTEGER:
+ return x.via.u64 == y.via.u64;
+
+ case MSGPACK_OBJECT_NEGATIVE_INTEGER:
+ return x.via.i64 == y.via.i64;
+
+ case MSGPACK_OBJECT_FLOAT32:
+ case MSGPACK_OBJECT_FLOAT64:
+ return x.via.f64 == y.via.f64;
+
+ case MSGPACK_OBJECT_STR:
+ return x.via.str.size == y.via.str.size &&
+ memcmp(x.via.str.ptr, y.via.str.ptr, x.via.str.size) == 0;
+
+ case MSGPACK_OBJECT_BIN:
+ return x.via.bin.size == y.via.bin.size &&
+ memcmp(x.via.bin.ptr, y.via.bin.ptr, x.via.bin.size) == 0;
+
+ case MSGPACK_OBJECT_EXT:
+ return x.via.ext.size == y.via.ext.size &&
+ x.via.ext.type == y.via.ext.type &&
+ memcmp(x.via.ext.ptr, y.via.ext.ptr, x.via.ext.size) == 0;
+
+ case MSGPACK_OBJECT_ARRAY:
+ if(x.via.array.size != y.via.array.size) {
+ return false;
+ } else if(x.via.array.size == 0) {
+ return true;
+ } else {
+ msgpack_object* px = x.via.array.ptr;
+ msgpack_object* const pxend = x.via.array.ptr + x.via.array.size;
+ msgpack_object* py = y.via.array.ptr;
+ do {
+ if(!msgpack_object_equal(*px, *py)) {
+ return false;
+ }
+ ++px;
+ ++py;
+ } while(px < pxend);
+ return true;
+ }
+
+ case MSGPACK_OBJECT_MAP:
+ if(x.via.map.size != y.via.map.size) {
+ return false;
+ } else if(x.via.map.size == 0) {
+ return true;
+ } else {
+ msgpack_object_kv* px = x.via.map.ptr;
+ msgpack_object_kv* const pxend = x.via.map.ptr + x.via.map.size;
+ msgpack_object_kv* py = y.via.map.ptr;
+ do {
+ if(!msgpack_object_equal(px->key, py->key) || !msgpack_object_equal(px->val, py->val)) {
+ return false;
+ }
+ ++px;
+ ++py;
+ } while(px < pxend);
+ return true;
+ }
+
+ default:
+ return false;
+ }
+}
diff --git a/third_party/msgpack/src/unpack.c b/third_party/msgpack/src/unpack.c
new file mode 100644
index 0000000000..a40f2aab71
--- /dev/null
+++ b/third_party/msgpack/src/unpack.c
@@ -0,0 +1,676 @@
+/*
+ * MessagePack for C unpacking routine
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+#include "msgpack/unpack.h"
+#include "msgpack/unpack_define.h"
+#include "msgpack/util.h"
+#include <stdlib.h>
+
+#ifdef _msgpack_atomic_counter_header
+#include _msgpack_atomic_counter_header
+#endif
+
+
+typedef struct {
+ msgpack_zone* z;
+ bool referenced;
+} unpack_user;
+
+
+#define msgpack_unpack_struct(name) \
+ struct template ## name
+
+#define msgpack_unpack_func(ret, name) \
+ ret template ## name
+
+#define msgpack_unpack_callback(name) \
+ template_callback ## name
+
+#define msgpack_unpack_object msgpack_object
+
+#define msgpack_unpack_user unpack_user
+
+
+struct template_context;
+typedef struct template_context template_context;
+
+static void template_init(template_context* ctx);
+
+static msgpack_object template_data(template_context* ctx);
+
+static int template_execute(
+ template_context* ctx, const char* data, size_t len, size_t* off);
+
+
+static inline msgpack_object template_callback_root(unpack_user* u)
+{
+ msgpack_object o;
+ MSGPACK_UNUSED(u);
+ o.type = MSGPACK_OBJECT_NIL;
+ return o;
+}
+
+static inline int template_callback_uint8(unpack_user* u, uint8_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = d;
+ return 0;
+}
+
+static inline int template_callback_uint16(unpack_user* u, uint16_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = d;
+ return 0;
+}
+
+static inline int template_callback_uint32(unpack_user* u, uint32_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = d;
+ return 0;
+}
+
+static inline int template_callback_uint64(unpack_user* u, uint64_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = d;
+ return 0;
+}
+
+static inline int template_callback_int8(unpack_user* u, int8_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ if(d >= 0) {
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = (uint64_t)d;
+ return 0;
+ }
+ else {
+ o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
+ o->via.i64 = d;
+ return 0;
+ }
+}
+
+static inline int template_callback_int16(unpack_user* u, int16_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ if(d >= 0) {
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = (uint64_t)d;
+ return 0;
+ }
+ else {
+ o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
+ o->via.i64 = d;
+ return 0;
+ }
+}
+
+static inline int template_callback_int32(unpack_user* u, int32_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ if(d >= 0) {
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = (uint64_t)d;
+ return 0;
+ }
+ else {
+ o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
+ o->via.i64 = d;
+ return 0;
+ }
+}
+
+static inline int template_callback_int64(unpack_user* u, int64_t d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ if(d >= 0) {
+ o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
+ o->via.u64 = (uint64_t)d;
+ return 0;
+ }
+ else {
+ o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
+ o->via.i64 = d;
+ return 0;
+ }
+}
+
+static inline int template_callback_float(unpack_user* u, float d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_FLOAT32;
+ o->via.f64 = d;
+ return 0;
+}
+
+static inline int template_callback_double(unpack_user* u, double d, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_FLOAT64;
+ o->via.f64 = d;
+ return 0;
+}
+
+static inline int template_callback_nil(unpack_user* u, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_NIL;
+ return 0;
+}
+
+static inline int template_callback_true(unpack_user* u, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_BOOLEAN;
+ o->via.boolean = true;
+ return 0;
+}
+
+static inline int template_callback_false(unpack_user* u, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ o->type = MSGPACK_OBJECT_BOOLEAN;
+ o->via.boolean = false;
+ return 0;
+}
+
+static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
+{
+ unsigned int size;
+ o->type = MSGPACK_OBJECT_ARRAY;
+ o->via.array.size = 0;
+ size = n*sizeof(msgpack_object);
+ if (size / sizeof(msgpack_object) != n) {
+ // integer overflow
+ return MSGPACK_UNPACK_NOMEM_ERROR;
+ }
+ o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, size);
+ if(o->via.array.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
+ return 0;
+}
+
+static inline int template_callback_array_item(unpack_user* u, msgpack_object* c, msgpack_object o)
+{
+ MSGPACK_UNUSED(u);
+#if defined(__GNUC__) && !defined(__clang__)
+ memcpy(&c->via.array.ptr[c->via.array.size], &o, sizeof(msgpack_object));
+#else /* __GNUC__ && !__clang__ */
+ c->via.array.ptr[c->via.array.size] = o;
+#endif /* __GNUC__ && !__clang__ */
+ ++c->via.array.size;
+ return 0;
+}
+
+static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_object* o)
+{
+ unsigned int size;
+ o->type = MSGPACK_OBJECT_MAP;
+ o->via.map.size = 0;
+ size = n*sizeof(msgpack_object_kv);
+ if (size / sizeof(msgpack_object_kv) != n) {
+ // integer overflow
+ return MSGPACK_UNPACK_NOMEM_ERROR;
+ }
+ o->via.map.ptr = (msgpack_object_kv*)msgpack_zone_malloc(u->z, size);
+ if(o->via.map.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
+ return 0;
+}
+
+static inline int template_callback_map_item(unpack_user* u, msgpack_object* c, msgpack_object k, msgpack_object v)
+{
+ MSGPACK_UNUSED(u);
+#if defined(__GNUC__) && !defined(__clang__)
+ memcpy(&c->via.map.ptr[c->via.map.size].key, &k, sizeof(msgpack_object));
+ memcpy(&c->via.map.ptr[c->via.map.size].val, &v, sizeof(msgpack_object));
+#else /* __GNUC__ && !__clang__ */
+ c->via.map.ptr[c->via.map.size].key = k;
+ c->via.map.ptr[c->via.map.size].val = v;
+#endif /* __GNUC__ && !__clang__ */
+ ++c->via.map.size;
+ return 0;
+}
+
+static inline int template_callback_str(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ MSGPACK_UNUSED(b);
+ o->type = MSGPACK_OBJECT_STR;
+ o->via.str.ptr = p;
+ o->via.str.size = l;
+ u->referenced = true;
+ return 0;
+}
+
+static inline int template_callback_bin(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o)
+{
+ MSGPACK_UNUSED(u);
+ MSGPACK_UNUSED(b);
+ o->type = MSGPACK_OBJECT_BIN;
+ o->via.bin.ptr = p;
+ o->via.bin.size = l;
+ u->referenced = true;
+ return 0;
+}
+
+static inline int template_callback_ext(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o)
+{
+ if (l == 0) {
+ return MSGPACK_UNPACK_PARSE_ERROR;
+ }
+ MSGPACK_UNUSED(u);
+ MSGPACK_UNUSED(b);
+ o->type = MSGPACK_OBJECT_EXT;
+ o->via.ext.type = *p;
+ o->via.ext.ptr = p + 1;
+ o->via.ext.size = l - 1;
+ u->referenced = true;
+ return 0;
+}
+
+#include "msgpack/unpack_template.h"
+
+
+#define CTX_CAST(m) ((template_context*)(m))
+#define CTX_REFERENCED(mpac) CTX_CAST((mpac)->ctx)->user.referenced
+
+#define COUNTER_SIZE (sizeof(_msgpack_atomic_counter_t))
+
+
+static inline void init_count(void* buffer)
+{
+ *(volatile _msgpack_atomic_counter_t*)buffer = 1;
+}
+
+static inline void decr_count(void* buffer)
+{
+ // atomic if(--*(_msgpack_atomic_counter_t*)buffer == 0) { free(buffer); }
+ if(_msgpack_sync_decr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer) == 0) {
+ free(buffer);
+ }
+}
+
+static inline void incr_count(void* buffer)
+{
+ // atomic ++*(_msgpack_atomic_counter_t*)buffer;
+ _msgpack_sync_incr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer);
+}
+
+static inline _msgpack_atomic_counter_t get_count(void* buffer)
+{
+ return *(volatile _msgpack_atomic_counter_t*)buffer;
+}
+
+bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size)
+{
+ char* buffer;
+ void* ctx;
+ msgpack_zone* z;
+
+ if(initial_buffer_size < COUNTER_SIZE) {
+ initial_buffer_size = COUNTER_SIZE;
+ }
+
+ buffer = (char*)malloc(initial_buffer_size);
+ if(buffer == NULL) {
+ return false;
+ }
+
+ ctx = malloc(sizeof(template_context));
+ if(ctx == NULL) {
+ free(buffer);
+ return false;
+ }
+
+ z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
+ if(z == NULL) {
+ free(ctx);
+ free(buffer);
+ return false;
+ }
+
+ mpac->buffer = buffer;
+ mpac->used = COUNTER_SIZE;
+ mpac->free = initial_buffer_size - mpac->used;
+ mpac->off = COUNTER_SIZE;
+ mpac->parsed = 0;
+ mpac->initial_buffer_size = initial_buffer_size;
+ mpac->z = z;
+ mpac->ctx = ctx;
+
+ init_count(mpac->buffer);
+
+ template_init(CTX_CAST(mpac->ctx));
+ CTX_CAST(mpac->ctx)->user.z = mpac->z;
+ CTX_CAST(mpac->ctx)->user.referenced = false;
+
+ return true;
+}
+
+void msgpack_unpacker_destroy(msgpack_unpacker* mpac)
+{
+ msgpack_zone_free(mpac->z);
+ free(mpac->ctx);
+ decr_count(mpac->buffer);
+}
+
+msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size)
+{
+ msgpack_unpacker* mpac = (msgpack_unpacker*)malloc(sizeof(msgpack_unpacker));
+ if(mpac == NULL) {
+ return NULL;
+ }
+
+ if(!msgpack_unpacker_init(mpac, initial_buffer_size)) {
+ free(mpac);
+ return NULL;
+ }
+
+ return mpac;
+}
+
+void msgpack_unpacker_free(msgpack_unpacker* mpac)
+{
+ msgpack_unpacker_destroy(mpac);
+ free(mpac);
+}
+
+bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
+{
+ if(mpac->used == mpac->off && get_count(mpac->buffer) == 1
+ && !CTX_REFERENCED(mpac)) {
+ // rewind buffer
+ mpac->free += mpac->used - COUNTER_SIZE;
+ mpac->used = COUNTER_SIZE;
+ mpac->off = COUNTER_SIZE;
+
+ if(mpac->free >= size) {
+ return true;
+ }
+ }
+
+ if(mpac->off == COUNTER_SIZE) {
+ char* tmp;
+ size_t next_size = (mpac->used + mpac->free) * 2; // include COUNTER_SIZE
+ while(next_size < size + mpac->used) {
+ size_t tmp_next_size = next_size * 2;
+ if (tmp_next_size <= next_size) {
+ next_size = size + mpac->used;
+ break;
+ }
+ next_size = tmp_next_size;
+ }
+
+ tmp = (char*)realloc(mpac->buffer, next_size);
+ if(tmp == NULL) {
+ return false;
+ }
+
+ mpac->buffer = tmp;
+ mpac->free = next_size - mpac->used;
+
+ } else {
+ char* tmp;
+ size_t next_size = mpac->initial_buffer_size; // include COUNTER_SIZE
+ size_t not_parsed = mpac->used - mpac->off;
+ while(next_size < size + not_parsed + COUNTER_SIZE) {
+ size_t tmp_next_size = next_size * 2;
+ if (tmp_next_size <= next_size) {
+ next_size = size + not_parsed + COUNTER_SIZE;
+ break;
+ }
+ next_size = tmp_next_size;
+ }
+
+ tmp = (char*)malloc(next_size);
+ if(tmp == NULL) {
+ return false;
+ }
+
+ init_count(tmp);
+
+ memcpy(tmp+COUNTER_SIZE, mpac->buffer+mpac->off, not_parsed);
+
+ if(CTX_REFERENCED(mpac)) {
+ if(!msgpack_zone_push_finalizer(mpac->z, decr_count, mpac->buffer)) {
+ free(tmp);
+ return false;
+ }
+ CTX_REFERENCED(mpac) = false;
+ } else {
+ decr_count(mpac->buffer);
+ }
+
+ mpac->buffer = tmp;
+ mpac->used = not_parsed + COUNTER_SIZE;
+ mpac->free = next_size - mpac->used;
+ mpac->off = COUNTER_SIZE;
+ }
+
+ return true;
+}
+
+int msgpack_unpacker_execute(msgpack_unpacker* mpac)
+{
+ size_t off = mpac->off;
+ int ret = template_execute(CTX_CAST(mpac->ctx),
+ mpac->buffer, mpac->used, &mpac->off);
+ if(mpac->off > off) {
+ mpac->parsed += mpac->off - off;
+ }
+ return ret;
+}
+
+msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac)
+{
+ return template_data(CTX_CAST(mpac->ctx));
+}
+
+msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
+{
+ msgpack_zone* r;
+ msgpack_zone* old;
+
+ if(!msgpack_unpacker_flush_zone(mpac)) {
+ return NULL;
+ }
+
+ r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
+ if(r == NULL) {
+ return NULL;
+ }
+
+ old = mpac->z;
+ mpac->z = r;
+ CTX_CAST(mpac->ctx)->user.z = mpac->z;
+
+ return old;
+}
+
+void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac)
+{
+ msgpack_zone_clear(mpac->z);
+}
+
+bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac)
+{
+ if(CTX_REFERENCED(mpac)) {
+ if(!msgpack_zone_push_finalizer(mpac->z, decr_count, mpac->buffer)) {
+ return false;
+ }
+ CTX_REFERENCED(mpac) = false;
+
+ incr_count(mpac->buffer);
+ }
+
+ return true;
+}
+
+void msgpack_unpacker_reset(msgpack_unpacker* mpac)
+{
+ template_init(CTX_CAST(mpac->ctx));
+ // don't reset referenced flag
+ mpac->parsed = 0;
+}
+
+static inline msgpack_unpack_return unpacker_next(msgpack_unpacker* mpac,
+ msgpack_unpacked* result)
+{
+ int ret;
+
+ msgpack_unpacked_destroy(result);
+
+ ret = msgpack_unpacker_execute(mpac);
+
+ if(ret < 0) {
+ result->zone = NULL;
+ memset(&result->data, 0, sizeof(msgpack_object));
+ return ret;
+ }
+
+ if(ret == 0) {
+ return MSGPACK_UNPACK_CONTINUE;
+ }
+ result->zone = msgpack_unpacker_release_zone(mpac);
+ result->data = msgpack_unpacker_data(mpac);
+
+ return MSGPACK_UNPACK_SUCCESS;
+}
+
+msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac,
+ msgpack_unpacked* result)
+{
+ int ret;
+
+ ret = unpacker_next(mpac, result);
+ if (ret == MSGPACK_UNPACK_SUCCESS) {
+ msgpack_unpacker_reset(mpac);
+ }
+
+ return ret;
+}
+
+msgpack_unpack_return
+msgpack_unpacker_next_with_size(msgpack_unpacker* mpac,
+ msgpack_unpacked* result, size_t *p_bytes)
+{
+ int ret;
+
+ ret = unpacker_next(mpac, result);
+ if (ret == MSGPACK_UNPACK_SUCCESS || ret == MSGPACK_UNPACK_CONTINUE) {
+ *p_bytes = mpac->parsed;
+ }
+
+ if (ret == MSGPACK_UNPACK_SUCCESS) {
+ msgpack_unpacker_reset(mpac);
+ }
+
+ return ret;
+}
+
+msgpack_unpack_return
+msgpack_unpack(const char* data, size_t len, size_t* off,
+ msgpack_zone* result_zone, msgpack_object* result)
+{
+ size_t noff = 0;
+ if(off != NULL) { noff = *off; }
+
+ if(len <= noff) {
+ // FIXME
+ return MSGPACK_UNPACK_CONTINUE;
+ }
+ else {
+ int e;
+ template_context ctx;
+ template_init(&ctx);
+
+ ctx.user.z = result_zone;
+ ctx.user.referenced = false;
+
+ e = template_execute(&ctx, data, len, &noff);
+ if(e < 0) {
+ return e;
+ }
+
+ if(off != NULL) { *off = noff; }
+
+ if(e == 0) {
+ return MSGPACK_UNPACK_CONTINUE;
+ }
+
+ *result = template_data(&ctx);
+
+ if(noff < len) {
+ return MSGPACK_UNPACK_EXTRA_BYTES;
+ }
+
+ return MSGPACK_UNPACK_SUCCESS;
+ }
+}
+
+msgpack_unpack_return
+msgpack_unpack_next(msgpack_unpacked* result,
+ const char* data, size_t len, size_t* off)
+{
+ size_t noff = 0;
+ msgpack_unpacked_destroy(result);
+
+ if(off != NULL) { noff = *off; }
+
+ if(len <= noff) {
+ return MSGPACK_UNPACK_CONTINUE;
+ }
+
+ if (!result->zone) {
+ result->zone = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
+ }
+
+ if (!result->zone) {
+ return MSGPACK_UNPACK_NOMEM_ERROR;
+ }
+ else {
+ int e;
+ template_context ctx;
+ template_init(&ctx);
+
+ ctx.user.z = result->zone;
+ ctx.user.referenced = false;
+
+ e = template_execute(&ctx, data, len, &noff);
+
+ if(off != NULL) { *off = noff; }
+
+ if(e < 0) {
+ msgpack_zone_free(result->zone);
+ result->zone = NULL;
+ return e;
+ }
+
+ if(e == 0) {
+ return MSGPACK_UNPACK_CONTINUE;
+ }
+
+ result->data = template_data(&ctx);
+
+ return MSGPACK_UNPACK_SUCCESS;
+ }
+}
+
+#if defined(MSGPACK_OLD_COMPILER_BUS_ERROR_WORKAROUND)
+// FIXME: Dirty hack to avoid a bus error caused by OS X's old gcc.
+static void dummy_function_to_avoid_bus_error()
+{
+}
+#endif
diff --git a/third_party/msgpack/src/version.c b/third_party/msgpack/src/version.c
new file mode 100644
index 0000000000..83f75108eb
--- /dev/null
+++ b/third_party/msgpack/src/version.c
@@ -0,0 +1,22 @@
+#include "msgpack.h"
+
+const char* msgpack_version(void)
+{
+ return MSGPACK_VERSION;
+}
+
+int msgpack_version_major(void)
+{
+ return MSGPACK_VERSION_MAJOR;
+}
+
+int msgpack_version_minor(void)
+{
+ return MSGPACK_VERSION_MINOR;
+}
+
+int msgpack_version_revision(void)
+{
+ return MSGPACK_VERSION_REVISION;
+}
+
diff --git a/third_party/msgpack/src/vrefbuffer.c b/third_party/msgpack/src/vrefbuffer.c
new file mode 100644
index 0000000000..2834747640
--- /dev/null
+++ b/third_party/msgpack/src/vrefbuffer.c
@@ -0,0 +1,232 @@
+/*
+ * MessagePack for C zero-copy buffer implementation
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+#include "msgpack/vrefbuffer.h"
+#include <stdlib.h>
+#include <string.h>
+
+#define MSGPACK_PACKER_MAX_BUFFER_SIZE 9
+
+struct msgpack_vrefbuffer_chunk {
+ struct msgpack_vrefbuffer_chunk* next;
+ /* data ... */
+};
+
+bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
+ size_t ref_size, size_t chunk_size)
+{
+ size_t nfirst;
+ struct iovec* array;
+ msgpack_vrefbuffer_chunk* chunk;
+
+ vbuf->chunk_size = chunk_size;
+ vbuf->ref_size =
+ ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ?
+ ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ;
+
+ nfirst = (sizeof(struct iovec) < 72/2) ?
+ 72 / sizeof(struct iovec) : 8;
+
+ array = (struct iovec*)malloc(
+ sizeof(struct iovec) * nfirst);
+ if(array == NULL) {
+ return false;
+ }
+
+ vbuf->tail = array;
+ vbuf->end = array + nfirst;
+ vbuf->array = array;
+
+ chunk = (msgpack_vrefbuffer_chunk*)malloc(
+ sizeof(msgpack_vrefbuffer_chunk) + chunk_size);
+ if(chunk == NULL) {
+ free(array);
+ return false;
+ }
+ else {
+ msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
+
+ ib->free = chunk_size;
+ ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
+ ib->head = chunk;
+ chunk->next = NULL;
+
+ return true;
+ }
+}
+
+void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
+{
+ msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head;
+ while(true) {
+ msgpack_vrefbuffer_chunk* n = c->next;
+ free(c);
+ if(n != NULL) {
+ c = n;
+ } else {
+ break;
+ }
+ }
+ free(vbuf->array);
+}
+
+void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf)
+{
+ msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head->next;
+ msgpack_vrefbuffer_chunk* n;
+ while(c != NULL) {
+ n = c->next;
+ free(c);
+ c = n;
+ }
+
+ {
+ msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
+ msgpack_vrefbuffer_chunk* chunk = ib->head;
+ chunk->next = NULL;
+ ib->free = vbuf->chunk_size;
+ ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
+
+ vbuf->tail = vbuf->array;
+ }
+}
+
+int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
+ const char* buf, size_t len)
+{
+ if(vbuf->tail == vbuf->end) {
+ const size_t nused = (size_t)(vbuf->tail - vbuf->array);
+ const size_t nnext = nused * 2;
+
+ struct iovec* nvec = (struct iovec*)realloc(
+ vbuf->array, sizeof(struct iovec)*nnext);
+ if(nvec == NULL) {
+ return -1;
+ }
+
+ vbuf->array = nvec;
+ vbuf->end = nvec + nnext;
+ vbuf->tail = nvec + nused;
+ }
+
+ vbuf->tail->iov_base = (char*)buf;
+ vbuf->tail->iov_len = len;
+ ++vbuf->tail;
+
+ return 0;
+}
+
+int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
+ const char* buf, size_t len)
+{
+ msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
+ char* m;
+
+ if(ib->free < len) {
+ msgpack_vrefbuffer_chunk* chunk;
+ size_t sz = vbuf->chunk_size;
+ if(sz < len) {
+ sz = len;
+ }
+
+ chunk = (msgpack_vrefbuffer_chunk*)malloc(
+ sizeof(msgpack_vrefbuffer_chunk) + sz);
+ if(chunk == NULL) {
+ return -1;
+ }
+
+ chunk->next = ib->head;
+ ib->head = chunk;
+ ib->free = sz;
+ ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
+ }
+
+ m = ib->ptr;
+ memcpy(m, buf, len);
+ ib->free -= len;
+ ib->ptr += len;
+
+ if(vbuf->tail != vbuf->array && m ==
+ (const char*)((vbuf->tail-1)->iov_base) + (vbuf->tail-1)->iov_len) {
+ (vbuf->tail-1)->iov_len += len;
+ return 0;
+ } else {
+ return msgpack_vrefbuffer_append_ref(vbuf, m, len);
+ }
+}
+
+int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
+{
+ size_t sz = vbuf->chunk_size;
+
+ msgpack_vrefbuffer_chunk* empty = (msgpack_vrefbuffer_chunk*)malloc(
+ sizeof(msgpack_vrefbuffer_chunk) + sz);
+ if(empty == NULL) {
+ return -1;
+ }
+
+ empty->next = NULL;
+
+ {
+ const size_t nused = (size_t)(vbuf->tail - vbuf->array);
+ if(to->tail + nused < vbuf->end) {
+ struct iovec* nvec;
+ const size_t tosize = (size_t)(to->tail - to->array);
+ const size_t reqsize = nused + tosize;
+ size_t nnext = (size_t)(to->end - to->array) * 2;
+ while(nnext < reqsize) {
+ size_t tmp_nnext = nnext * 2;
+ if (tmp_nnext <= nnext) {
+ nnext = reqsize;
+ break;
+ }
+ nnext = tmp_nnext;
+ }
+
+ nvec = (struct iovec*)realloc(
+ to->array, sizeof(struct iovec)*nnext);
+ if(nvec == NULL) {
+ free(empty);
+ return -1;
+ }
+
+ to->array = nvec;
+ to->end = nvec + nnext;
+ to->tail = nvec + tosize;
+ }
+
+ memcpy(to->tail, vbuf->array, sizeof(struct iovec)*nused);
+
+ to->tail += nused;
+ vbuf->tail = vbuf->array;
+
+ {
+ msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
+ msgpack_vrefbuffer_inner_buffer* const toib = &to->inner_buffer;
+
+ msgpack_vrefbuffer_chunk* last = ib->head;
+ while(last->next != NULL) {
+ last = last->next;
+ }
+ last->next = toib->head;
+ toib->head = ib->head;
+
+ if(toib->free < ib->free) {
+ toib->free = ib->free;
+ toib->ptr = ib->ptr;
+ }
+
+ ib->head = empty;
+ ib->free = sz;
+ ib->ptr = ((char*)empty) + sizeof(msgpack_vrefbuffer_chunk);
+ }
+ }
+
+ return 0;
+}
diff --git a/third_party/msgpack/src/zone.c b/third_party/msgpack/src/zone.c
new file mode 100644
index 0000000000..372a1f5b80
--- /dev/null
+++ b/third_party/msgpack/src/zone.c
@@ -0,0 +1,222 @@
+/*
+ * MessagePack for C memory pool implementation
+ *
+ * Copyright (C) 2008-2009 FURUHASHI Sadayuki
+ *
+ * Distributed under the Boost Software License, Version 1.0.
+ * (See accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+#include "msgpack/zone.h"
+#include <stdlib.h>
+#include <string.h>
+
+struct msgpack_zone_chunk {
+ struct msgpack_zone_chunk* next;
+ /* data ... */
+};
+
+static inline bool init_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_size)
+{
+ msgpack_zone_chunk* chunk = (msgpack_zone_chunk*)malloc(
+ sizeof(msgpack_zone_chunk) + chunk_size);
+ if(chunk == NULL) {
+ return false;
+ }
+
+ cl->head = chunk;
+ cl->free = chunk_size;
+ cl->ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk);
+ chunk->next = NULL;
+
+ return true;
+}
+
+static inline void destroy_chunk_list(msgpack_zone_chunk_list* cl)
+{
+ msgpack_zone_chunk* c = cl->head;
+ while(true) {
+ msgpack_zone_chunk* n = c->next;
+ free(c);
+ if(n != NULL) {
+ c = n;
+ } else {
+ break;
+ }
+ }
+}
+
+static inline void clear_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_size)
+{
+ msgpack_zone_chunk* c = cl->head;
+ while(true) {
+ msgpack_zone_chunk* n = c->next;
+ if(n != NULL) {
+ free(c);
+ c = n;
+ } else {
+ cl->head = c;
+ break;
+ }
+ }
+ cl->head->next = NULL;
+ cl->free = chunk_size;
+ cl->ptr = ((char*)cl->head) + sizeof(msgpack_zone_chunk);
+}
+
+void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
+{
+ msgpack_zone_chunk_list* const cl = &zone->chunk_list;
+ msgpack_zone_chunk* chunk;
+
+ size_t sz = zone->chunk_size;
+
+ while(sz < size) {
+ size_t tmp_sz = sz * 2;
+ if (tmp_sz <= sz) {
+ sz = size;
+ break;
+ }
+ sz = tmp_sz;
+ }
+
+ chunk = (msgpack_zone_chunk*)malloc(
+ sizeof(msgpack_zone_chunk) + sz);
+ if (chunk == NULL) {
+ return NULL;
+ }
+ else {
+ char* ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk);
+ chunk->next = cl->head;
+ cl->head = chunk;
+ cl->free = sz - size;
+ cl->ptr = ptr + size;
+
+ return ptr;
+ }
+}
+
+
+static inline void init_finalizer_array(msgpack_zone_finalizer_array* fa)
+{
+ fa->tail = NULL;
+ fa->end = NULL;
+ fa->array = NULL;
+}
+
+static inline void call_finalizer_array(msgpack_zone_finalizer_array* fa)
+{
+ msgpack_zone_finalizer* fin = fa->tail;
+ for(; fin != fa->array; --fin) {
+ (*(fin-1)->func)((fin-1)->data);
+ }
+}
+
+static inline void destroy_finalizer_array(msgpack_zone_finalizer_array* fa)
+{
+ call_finalizer_array(fa);
+ free(fa->array);
+}
+
+static inline void clear_finalizer_array(msgpack_zone_finalizer_array* fa)
+{
+ call_finalizer_array(fa);
+ fa->tail = fa->array;
+}
+
+bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
+ void (*func)(void* data), void* data)
+{
+ msgpack_zone_finalizer_array* const fa = &zone->finalizer_array;
+ msgpack_zone_finalizer* tmp;
+
+ const size_t nused = (size_t)(fa->end - fa->array);
+
+ size_t nnext;
+ if(nused == 0) {
+ nnext = (sizeof(msgpack_zone_finalizer) < 72/2) ?
+ 72 / sizeof(msgpack_zone_finalizer) : 8;
+
+ } else {
+ nnext = nused * 2;
+ }
+
+ tmp = (msgpack_zone_finalizer*)realloc(fa->array,
+ sizeof(msgpack_zone_finalizer) * nnext);
+ if(tmp == NULL) {
+ return false;
+ }
+
+ fa->array = tmp;
+ fa->end = tmp + nnext;
+ fa->tail = tmp + nused;
+
+ fa->tail->func = func;
+ fa->tail->data = data;
+
+ ++fa->tail;
+
+ return true;
+}
+
+
+bool msgpack_zone_is_empty(msgpack_zone* zone)
+{
+ msgpack_zone_chunk_list* const cl = &zone->chunk_list;
+ msgpack_zone_finalizer_array* const fa = &zone->finalizer_array;
+ return cl->free == zone->chunk_size && cl->head->next == NULL &&
+ fa->tail == fa->array;
+}
+
+
+void msgpack_zone_destroy(msgpack_zone* zone)
+{
+ destroy_finalizer_array(&zone->finalizer_array);
+ destroy_chunk_list(&zone->chunk_list);
+}
+
+void msgpack_zone_clear(msgpack_zone* zone)
+{
+ clear_finalizer_array(&zone->finalizer_array);
+ clear_chunk_list(&zone->chunk_list, zone->chunk_size);
+}
+
+bool msgpack_zone_init(msgpack_zone* zone, size_t chunk_size)
+{
+ zone->chunk_size = chunk_size;
+
+ if(!init_chunk_list(&zone->chunk_list, chunk_size)) {
+ return false;
+ }
+
+ init_finalizer_array(&zone->finalizer_array);
+
+ return true;
+}
+
+msgpack_zone* msgpack_zone_new(size_t chunk_size)
+{
+ msgpack_zone* zone = (msgpack_zone*)malloc(
+ sizeof(msgpack_zone));
+ if(zone == NULL) {
+ return NULL;
+ }
+
+ zone->chunk_size = chunk_size;
+
+ if(!init_chunk_list(&zone->chunk_list, chunk_size)) {
+ free(zone);
+ return NULL;
+ }
+
+ init_finalizer_array(&zone->finalizer_array);
+
+ return zone;
+}
+
+void msgpack_zone_free(msgpack_zone* zone)
+{
+ if(zone == NULL) { return; }
+ msgpack_zone_destroy(zone);
+ free(zone);
+}