pax_global_header00006660000000000000000000000064130635672230014521gustar00rootroot0000000000000052 comment=e9047afe4c02cd47c510f701deda6f502d7d94a2 libmpack-1.0.5/000077500000000000000000000000001306356722300133065ustar00rootroot00000000000000libmpack-1.0.5/.config/000077500000000000000000000000001306356722300146315ustar00rootroot00000000000000libmpack-1.0.5/.config/amalgamation.mk000066400000000000000000000003051306356722300176120ustar00rootroot00000000000000include .config/release.mk # unset LIB and define TEST_AMALGAMATION so the test harness will include the # amalgamation instead of linking to the library. LIB := XCFLAGS += -DTEST_AMALGAMATION libmpack-1.0.5/.config/armeb.mk000066400000000000000000000021761306356722300162560ustar00rootroot00000000000000unexport CC LINARO_V := 6.2.1-2016.11 LINARO_V_NOPATCH := 6.2-2016.11 LINARO_URL := https://releases.linaro.org/components/toolchain/binaries/$(LINARO_V_NOPATCH)/armeb-linux-gnueabihf/gcc-linaro-$(LINARO_V)-x86_64_armeb-linux-gnueabihf.tar.xz QEMU_URL := https://github.com/qemu/qemu/archive/stable-2.4.tar.gz DEPS := $(shell pwd)/.deps DPREFIX := $(DEPS)/usr RUNNER := $(DPREFIX)/bin/qemu-armeb TPREFIX := $(DPREFIX)/bin/armeb-linux-gnueabihf- C_COMPILER := $(TPREFIX)gcc CC := $(C_COMPILER) AR := $(TPREFIX)ar RANLIB := $(TPREFIX)ranlib LINK := $(CC) XLDFLAGS += -all-static FETCH ?= curl -L -o - $(RUNNER): @echo installing qemu... @rm -rf $(DEPS)/src/qemu @mkdir -p $(DEPS)/src/qemu @$(FETCH) $(QEMU_URL) | tar xfz - --strip-components=1 -C $(DEPS)/src/qemu @cd $(DEPS)/src/qemu && \ CFLAGS= LDFLAGS= \ ./configure --prefix=$(DPREFIX) \ --target-list=armeb-linux-user && \ make install @rm -rf $(DEPS)/src $(C_COMPILER): mkdir -p $(DPREFIX) @echo installing linaro toolchain... @echo fetch $(LINARO_URL) @$(FETCH) $(LINARO_URL) | tar xJf - --strip-components=1 -C $(DPREFIX) libmpack-1.0.5/.config/asan.mk000066400000000000000000000003761306356722300161120ustar00rootroot00000000000000include .config/release.mk XCFLAGS += -g3 XCFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=address XLDFLAGS += -fsanitize=address export ASAN_OPTIONS := log_path=asan:detect_leaks=1 export ASAN_SYMBOLIZER_PATH := $(SYMBOLIZER) libmpack-1.0.5/.config/debug.mk000066400000000000000000000000351306356722300162460ustar00rootroot00000000000000XCFLAGS += -O0 -ggdb -DDEBUG libmpack-1.0.5/.config/msan.mk000066400000000000000000000004331306356722300161200ustar00rootroot00000000000000include .config/release.mk XCFLAGS += -g3 XCFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls \ -fsanitize=memory -fsanitize-memory-track-origins XLDFLAGS += -fsanitize=memory export MSAN_OPTIONS := log_path=msan export MSAN_SYMBOLIZER_PATH := $(SYMBOLIZER) libmpack-1.0.5/.config/release.mk000066400000000000000000000000301306356722300165730ustar00rootroot00000000000000XCFLAGS += -O3 -DNDEBUG libmpack-1.0.5/.config/ubsan.mk000066400000000000000000000005121306356722300162700ustar00rootroot00000000000000include .config/release.mk XCFLAGS += -g3 XCFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls \ -fno-sanitize-recover -fsanitize=undefined \ -fno-sanitize=float-cast-overflow XLDFLAGS += -fsanitize=undefined export UBSAN_OPTIONS := log_path=ubsan export UBSAN_SYMBOLIZER_PATH := $(SYMBOLIZER) libmpack-1.0.5/.gitignore000066400000000000000000000000701306356722300152730ustar00rootroot00000000000000/build/ /.deps/ gmon.out *.c.gcov compile_commands.json libmpack-1.0.5/.travis.yml000066400000000000000000000021651306356722300154230ustar00rootroot00000000000000sudo: false language: c compiler: clang-3.8 env: global: - VERBOSE=1 matrix: - CONFIG=asan - CONFIG=msan - CONFIG=ubsan - CONFIG=debug - CONFIG=release CFLAGS=-Werror - CONFIG=amalgamation CFLAGS=-Werror - CONFIG=release ANSI=1 CFLAGS=-Werror addons: apt: packages: - clang-3.8 sources: - llvm-toolchain-precise-3.8 - ubuntu-toolchain-r-test os: - linux matrix: include: - os: linux compiler: gcc env: CONFIG=release CFLAGS='-m32 -Werror' LDFLAGS=-m32 addons: apt: packages: - gcc-multilib - os: linux compiler: gcc env: - CONFIG=armeb CFLAGS='-DNDEBUG -g --coverage -Werror' LDFLAGS=--coverage before_install: - pip install --user cpp-coveralls 'requests[security]' after_success: - coveralls -b . --gcov $(pwd)/.deps/usr/bin/armeb-linux-gnueabihf-gcov --gcov-options '\-lp' - os: osx env: CONFIG=release compiler: gcc script: make config=${CONFIG} test cache: directories: - .deps/usr after_failure: cat asan.* ubsan.* msan.* || true libmpack-1.0.5/LICENSE-MIT000066400000000000000000000020441306356722300147420ustar00rootroot00000000000000Copyright (c) 2016 Thiago de Arruda Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. libmpack-1.0.5/Makefile000066400000000000000000000100331306356722300147430ustar00rootroot00000000000000# Some parts of this Makefile were taken or adapted from libunibilium: # https://github.com/mauke/unibilium config ?= debug SYSTEM ?= $(shell uname -s) ifeq ($(SYSTEM),Darwin) LIBTOOL ?= glibtool else LIBTOOL ?= libtool endif ifneq ($(VERBOSE),1) LIBTOOL += --quiet endif ifneq "$(strip $(findstring clang,$(CC)))" "" # When CC is set to clang-${VERSION}, it is very likely that other llvm # tools are installed with the same version suffix, so use it for # llvm-symbolizer SYMBOLIZER ?= $(shell realpath $$(which $(subst clang,llvm-symbolizer,$(CC)))) endif SYMBOLIZER ?= /usr/bin/llvm-symbolizer XCFLAGS += -Wall -Wextra -Wconversion -Wstrict-prototypes -pedantic ifeq ($(ANSI),1) # test helper to validate that the library works when compiled as c90 # (without native 32-bit integers, for example) XCFLAGS += -DFORCE_32BIT_INTS -ansi else XCFLAGS += -std=c99 endif NAME := mpack MAJOR := 1 MINOR := 0 PATCH := 3 VERSION := $(MAJOR).$(MINOR).$(PATCH) LT_REVISION=0 LT_CURRENT=0 LT_AGE=0 PREFIX ?= /usr/local LIBDIR ?= $(PREFIX)/lib INCDIR ?= $(PREFIX)/include SRCDIR ?= src TESTDIR ?= test BINDIR ?= build OUTDIR ?= $(BINDIR)/$(config) SRC := core.c conv.c object.c rpc.c SRC := $(addprefix $(SRCDIR)/,$(SRC)) HDRS := $(SRC:.c=.h) OBJ := $(addprefix $(OUTDIR)/,$(SRC:.c=.lo)) LIBRARY := lib$(NAME).la LIB := $(OUTDIR)/$(LIBRARY) TSRC := $(wildcard $(TESTDIR)/*.c) $(TESTDIR)/deps/tap/tap.c TOBJ := $(addprefix $(OUTDIR)/,$(TSRC:.c=.lo)) TEXE := $(OUTDIR)/run-tests AMALG := $(BINDIR)/$(NAME).c AMALG_H := $(AMALG:.c=.h) COVOUT := $(OUTDIR)/gcov.txt PROFOUT := $(OUTDIR)/gprof.txt TEST_FILTER_OUT := --coverage -ansi -std=c99 .PHONY: all all: lib-bin test-bin include .config/$(config).mk .PHONY: tools tools: $(C_COMPILER) $(RUNNER) .PHONY: amalgamation amalgamation: $(AMALG) .PHONY: lib-bin lib-bin: tools $(LIB) .PHONY: test-bin test-bin: lib-bin $(TEXE) .PHONY: test test: test-bin @$(RUNNER) $(TEXE) .PHONY: gdb gdb: test-bin $(LIBTOOL) --mode=execute gdb -x .gdb $(TEXE) .PHONY: coverage coverage: tools $(COVOUT) cat $(COVOUT) .PHONY: profile profile: tools $(PROFOUT) cat $(PROFOUT) .PHONY: compile_commands.json compile_commands.json: rm -f $(BINDIR)/compile_commands.json $(MAKE) config=$(config) clean bear $(MAKE) config=$(config) mv compile_commands.json $(BINDIR) .PHONY: install install: install-inc install-lib $(LIBTOOL) --mode=finish '$(DESTDIR)$(LIBDIR)' .PHONY: install-inc install-inc: $(AMALG_H) mpack.pc.in mkdir -p '$(DESTDIR)$(INCDIR)' install -m644 $(AMALG_H) '$(DESTDIR)$(INCDIR)' mkdir -p '$(DESTDIR)$(LIBDIR)/pkgconfig' sed 's,@VERSION@,$(VERSION),;s,@LIBDIR@,$(LIBDIR),;s,@INCDIR@,$(INCDIR),' '$(DESTDIR)$(LIBDIR)/pkgconfig/mpack.pc' .PHONY: install-lib install-lib: $(LIB) mkdir -p '$(DESTDIR)$(LIBDIR)' $(LIBTOOL) --mode=install cp $(LIB) '$(DESTDIR)$(LIBDIR)/$(LIBRARY)' .PHONY: clean clean: rm -rf $(BINDIR)/$(config) $(TOBJ): XCFLAGS := $(filter-out $(TEST_FILTER_OUT),$(XCFLAGS)) \ -std=gnu99 -Wno-conversion -Wno-unused-parameter $(COVOUT): $(SRC) $(TSRC) find $(OUTDIR) -type f -name '*.gcda' -print0 | xargs -0 rm -f $(MAKE) CFLAGS='-DNDEBUG -g --coverage' LDFLAGS=--coverage config=$(config) test find $(OUTDIR)/src -type f -name '*.o' -print0 | \ xargs -0 gcov -lp > $@ rm *.c.gcov $(PROFOUT): $(SRC) $(TSRC) $(MAKE) CFLAGS=-pg LDFLAGS=-pg config=$(config) test gprof $(OUTDIR)/run-tests gmon.out > $@ rm gmon.out $(OUTDIR)/%.lo: %.c $(AMALG) @echo compile $< =\> $@ @$(LIBTOOL) --mode=compile --tag=CC $(CC) $(XCFLAGS) $(CFLAGS) -o $@ -c $< $(LIB): $(OBJ) @echo link $^ =\> $@ @$(LIBTOOL) --mode=link --tag=CC $(CC) $(XLDFLAGS) $(LDFLAGS) \ -rpath '$(LIBDIR)' \ -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) -o $@ $^ $(TEXE): $(LIB) $(TOBJ) @echo link $^ =\> $@ @$(LIBTOOL) --mode=link --tag=CC $(CC) $(XLDFLAGS) $(LDFLAGS) -lm -g -O \ -o $@ $(LIB) $(TOBJ) $(AMALG_H): $(HDRS) mkdir -p $(BINDIR) cat $^ | sed '/^#include "/d' > $@ $(AMALG): $(AMALG_H) $(SRC) mkdir -p $(BINDIR) cat $^ | sed '/^#include "/d' > $@ libmpack-1.0.5/README.md000066400000000000000000000067321306356722300145750ustar00rootroot00000000000000## libmpack [![Travis Build Status](https://travis-ci.org/libmpack/libmpack.svg?branch=master)](https://travis-ci.org/libmpack/libmpack) [![Coverage Status](https://coveralls.io/repos/libmpack/libmpack/badge.svg?branch=master&service=github)](https://coveralls.io/github/libmpack/libmpack?branch=master) ### Introduction libmpack is a small binary serialization/RPC library that implements both the [msgpack](https://github.com/msgpack/msgpack/blob/master/spec.md) and [msgpack-rpc](https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md) specifications. ### Rationale While there's already a [msgpack-c](https://github.com/msgpack/msgpack-c) implementation, it has a few problems that libmpack aims to address: * It couples msgpack serialization format with a set of predefined C typedefs. This means the user almost always has to recursively convert the allocated structures into some other application-specific format, especially if binding to another language. libmpack serialization/deserialization API is callback-based, making it simple to serialize/deserialize directly from/to application-specific objects. * It is not trivial to simply include its files into another project(eg: a lua C module) since it relies too much on C99 features and compilation with -Wconversion issues a bunch of warnings. You need to build the library and link against it, which can be cumbersome for a simple serialization library that is being embedded into other projects(lua or node.js modules for example). libmpack provides an amalgamation build(single source file containing all code that can be #included) and should compile cleanly as part of any C89 project. It won't produce any warnings for `-Wall`/`-Wextra`/`-Wconversion`. * msgpack-c doesn't work without allocating memory. For example, you can't send/receive simple primitives without a `msgpack_sbuffer_t`(which is then dynamically extended by msgpack internal functions). libmpack does no allocation at all, and provides some helpers to simplify dynamic allocation by the user, if required. * There's no msgpack-rpc implementation for C(much less one that can be reused in other languages). libmpack has a simple and flexible msgpack-rpc implementation that can be used to easily create distributed applications across any kind of transport. Unlike some msgpack-rpc libraries(ruby official implementation for example), libmpack has no coupling with any network sink/source, allowing it to be used with any event loop library or system-specific networking APIs. Here's a few extras that may or not overlap with what msgpack-c provides: * Fully incremental/iterative parse/serialization API with no backtracking, which simplifies working with split buffers. * Portable C89 library with zero system dependencies. In fact, it only uses one function from C standard library: memcpy. libmpack can be used even in OS/kernel development(eg: communicate with userspace using netlink/msgpack-rpc). * Endian aware: it should work unmodified regardless of the system's byte order, and its CI infrastructure automatically tests it on a big endian platform. * Well tested, it should always have about 100% code coverage: https://coveralls.io/github/libmpack/libmpack?branch=master * Relatively small footprint: The amalgamation(headers + code) is less than 2k lines of C. The whole library can be inlined when compiled with -O3(Though this depends on compiler and usage, eg: how many call sites for certain functions). libmpack-1.0.5/mpack.pc.in000066400000000000000000000003461306356722300153350ustar00rootroot00000000000000libdir=@LIBDIR@ includedir=@INCDIR@ version=@VERSION@ Name: mpack Description: small binary serialization library implementing msgpack and msgpack-rpc formats Version: ${version} Libs: -L${libdir} -lmpack Cflags: -I${includedir} libmpack-1.0.5/src/000077500000000000000000000000001306356722300140755ustar00rootroot00000000000000libmpack-1.0.5/src/conv.c000066400000000000000000000221561306356722300152140ustar00rootroot00000000000000#include "conv.h" static int mpack_fits_single(double v); static mpack_value_t mpack_pack_ieee754(double v, unsigned m, unsigned e); static int mpack_is_be(void) FPURE; static double mpack_fmod_pow2_32(double a); #define POW2(n) \ ((double)(1 << (n / 2)) * (double)(1 << (n / 2)) * (double)(1 << (n % 2))) #define MPACK_SWAP_VALUE(val) \ do { \ mpack_uint32_t lo = val.lo; \ val.lo = val.hi; \ val.hi = lo; \ } while (0) MPACK_API mpack_token_t mpack_pack_nil(void) { mpack_token_t rv; rv.type = MPACK_TOKEN_NIL; return rv; } MPACK_API mpack_token_t mpack_pack_boolean(unsigned v) { mpack_token_t rv; rv.type = MPACK_TOKEN_BOOLEAN; rv.data.value.lo = v ? 1 : 0; rv.data.value.hi = 0; return rv; } MPACK_API mpack_token_t mpack_pack_uint(mpack_uintmax_t v) { mpack_token_t rv; rv.data.value.lo = v & 0xffffffff; rv.data.value.hi = (mpack_uint32_t)((v >> 31) >> 1); rv.type = MPACK_TOKEN_UINT; return rv; } MPACK_API mpack_token_t mpack_pack_sint(mpack_sintmax_t v) { if (v < 0) { mpack_token_t rv; mpack_uintmax_t tc = -((mpack_uintmax_t)(v + 1)) + 1; tc = ~tc + 1; rv = mpack_pack_uint(tc); rv.type = MPACK_TOKEN_SINT; return rv; } return mpack_pack_uint((mpack_uintmax_t)v); } MPACK_API mpack_token_t mpack_pack_float_compat(double v) { /* ieee754 single-precision limits to determine if "v" can be fully * represented in 4 bytes */ mpack_token_t rv; if (mpack_fits_single(v)) { rv.length = 4; rv.data.value = mpack_pack_ieee754(v, 23, 8); } else { rv.length = 8; rv.data.value = mpack_pack_ieee754(v, 52, 11); } rv.type = MPACK_TOKEN_FLOAT; return rv; } MPACK_API mpack_token_t mpack_pack_float_fast(double v) { /* ieee754 single-precision limits to determine if "v" can be fully * represented in 4 bytes */ mpack_token_t rv; if (mpack_fits_single(v)) { union { float f; mpack_uint32_t m; } conv; conv.f = (float)v; rv.length = 4; rv.data.value.lo = conv.m; rv.data.value.hi = 0; } else { union { double d; mpack_value_t m; } conv; conv.d = v; rv.length = 8; rv.data.value = conv.m; if (mpack_is_be()) { MPACK_SWAP_VALUE(rv.data.value); } } rv.type = MPACK_TOKEN_FLOAT; return rv; } MPACK_API mpack_token_t mpack_pack_number(double v) { mpack_token_t tok; double vabs; vabs = v < 0 ? -v : v; assert(v <= 9007199254740991. && v >= -9007199254740991.); tok.data.value.hi = (mpack_uint32_t)(vabs / POW2(32)); tok.data.value.lo = (mpack_uint32_t)mpack_fmod_pow2_32(vabs); if (v < 0) { /* Compute the two's complement */ tok.type = MPACK_TOKEN_SINT; tok.data.value.hi = ~tok.data.value.hi; tok.data.value.lo = ~tok.data.value.lo + 1; if (!tok.data.value.lo) tok.data.value.hi++; if (tok.data.value.lo == 0 && tok.data.value.hi == 0) tok.length = 1; else if (tok.data.value.lo < 0x80000000) tok.length = 8; else if (tok.data.value.lo < 0xffff7fff) tok.length = 4; else if (tok.data.value.lo < 0xffffff7f) tok.length = 2; else tok.length = 1; } else { tok.type = MPACK_TOKEN_UINT; if (tok.data.value.hi) tok.length = 8; else if (tok.data.value.lo > 0xffff) tok.length = 4; else if (tok.data.value.lo > 0xff) tok.length = 2; else tok.length = 1; } if (mpack_unpack_number(tok) != v) { return mpack_pack_float(v); } return tok; } MPACK_API mpack_token_t mpack_pack_chunk(const char *p, mpack_uint32_t l) { mpack_token_t rv; rv.type = MPACK_TOKEN_CHUNK; rv.data.chunk_ptr = p; rv.length = l; return rv; } MPACK_API mpack_token_t mpack_pack_str(mpack_uint32_t l) { mpack_token_t rv; rv.type = MPACK_TOKEN_STR; rv.length = l; return rv; } MPACK_API mpack_token_t mpack_pack_bin(mpack_uint32_t l) { mpack_token_t rv; rv.type = MPACK_TOKEN_BIN; rv.length = l; return rv; } MPACK_API mpack_token_t mpack_pack_ext(int t, mpack_uint32_t l) { mpack_token_t rv; rv.type = MPACK_TOKEN_EXT; rv.length = l; rv.data.ext_type = t; return rv; } MPACK_API mpack_token_t mpack_pack_array(mpack_uint32_t l) { mpack_token_t rv; rv.type = MPACK_TOKEN_ARRAY; rv.length = l; return rv; } MPACK_API mpack_token_t mpack_pack_map(mpack_uint32_t l) { mpack_token_t rv; rv.type = MPACK_TOKEN_MAP; rv.length = l; return rv; } MPACK_API bool mpack_unpack_boolean(mpack_token_t t) { return t.data.value.lo || t.data.value.hi; } MPACK_API mpack_uintmax_t mpack_unpack_uint(mpack_token_t t) { return (((mpack_uintmax_t)t.data.value.hi << 31) << 1) | t.data.value.lo; } /* unpack signed integer without relying on two's complement as internal * representation */ MPACK_API mpack_sintmax_t mpack_unpack_sint(mpack_token_t t) { mpack_uint32_t hi = t.data.value.hi; mpack_uint32_t lo = t.data.value.lo; mpack_uintmax_t rv = lo; assert(t.length <= sizeof(mpack_sintmax_t)); if (t.length == 8) { rv |= (((mpack_uintmax_t)hi) << 31) << 1; } /* reverse the two's complement so that lo/hi contain the absolute value. * note that we have to mask ~rv so that it reflects the two's complement * of the appropriate byte length */ rv = (~rv & (((mpack_uintmax_t)1 << ((t.length * 8) - 1)) - 1)) + 1; /* negate and return the absolute value, making sure mpack_sintmax_t can * represent the positive cast. */ return -((mpack_sintmax_t)(rv - 1)) - 1; } MPACK_API double mpack_unpack_float_compat(mpack_token_t t) { mpack_uint32_t sign; mpack_sint32_t exponent, bias; unsigned mantbits; unsigned expbits; double mant; if (t.data.value.lo == 0 && t.data.value.hi == 0) /* nothing to do */ return 0; if (t.length == 4) mantbits = 23, expbits = 8; else mantbits = 52, expbits = 11; bias = (1 << (expbits - 1)) - 1; /* restore sign/exponent/mantissa */ if (mantbits == 52) { sign = t.data.value.hi >> 31; exponent = (t.data.value.hi >> 20) & ((1 << 11) - 1); mant = (t.data.value.hi & ((1 << 20) - 1)) * POW2(32); mant += t.data.value.lo; } else { sign = t.data.value.lo >> 31; exponent = (t.data.value.lo >> 23) & ((1 << 8) - 1); mant = t.data.value.lo & ((1 << 23) - 1); } mant /= POW2(mantbits); if (exponent) mant += 1.0; /* restore leading 1 */ else exponent = 1; /* subnormal */ exponent -= bias; /* restore original value */ while (exponent > 0) mant *= 2.0, exponent--; while (exponent < 0) mant /= 2.0, exponent++; return mant * (sign ? -1 : 1); } MPACK_API double mpack_unpack_float_fast(mpack_token_t t) { if (t.length == 4) { union { float f; mpack_uint32_t m; } conv; conv.m = t.data.value.lo; return conv.f; } else { union { double d; mpack_value_t m; } conv; conv.m = t.data.value; if (mpack_is_be()) { MPACK_SWAP_VALUE(conv.m); } return conv.d; } } MPACK_API double mpack_unpack_number(mpack_token_t t) { double rv; mpack_uint32_t hi, lo; if (t.type == MPACK_TOKEN_FLOAT) return mpack_unpack_float(t); assert(t.type == MPACK_TOKEN_UINT || t.type == MPACK_TOKEN_SINT); hi = t.data.value.hi; lo = t.data.value.lo; if (t.type == MPACK_TOKEN_SINT) { /* same idea as mpack_unpack_sint, except here we shouldn't rely on * mpack_uintmax_t having 64-bits, operating on the 32-bit words separately. */ if (!hi) { assert(t.length <= 4); hi = 0; lo = (~lo & (((mpack_uint32_t)1 << ((t.length * 8) - 1)) - 1)); } else { hi = ~hi; lo = ~lo; } lo++; if (!lo) hi++; } rv = (double)lo + POW2(32) * hi; return t.type == MPACK_TOKEN_SINT ? -rv : rv; } static int mpack_fits_single(double v) { return (float)v == v; } static mpack_value_t mpack_pack_ieee754(double v, unsigned mantbits, unsigned expbits) { mpack_value_t rv = {0, 0}; mpack_sint32_t exponent, bias = (1 << (expbits - 1)) - 1; mpack_uint32_t sign; double mant; if (v == 0) { rv.lo = 0; rv.hi = 0; goto end; } if (v < 0) sign = 1, mant = -v; else sign = 0, mant = v; exponent = 0; while (mant >= 2.0) mant /= 2.0, exponent++; while (mant < 1.0 && exponent > -(bias - 1)) mant *= 2.0, exponent--; if (mant < 1.0) exponent = -bias; /* subnormal value */ else mant = mant - 1.0; /* remove leading 1 */ exponent += bias; mant *= POW2(mantbits); if (mantbits == 52) { rv.hi = (mpack_uint32_t)(mant / POW2(32)); rv.lo = (mpack_uint32_t)(mant - rv.hi * POW2(32)); rv.hi |= ((mpack_uint32_t)exponent << 20) | (sign << 31); } else if (mantbits == 23) { rv.hi = 0; rv.lo = (mpack_uint32_t)mant; rv.lo |= ((mpack_uint32_t)exponent << 23) | (sign << 31); } end: return rv; } static int mpack_is_be(void) { union { mpack_uint32_t i; char c[sizeof(mpack_uint32_t)]; } test; test.i = 1; return test.c[0] == 0; } /* this simplified version of `fmod` that returns the remainder of double * division by 0xffffffff, which is enough for our purposes */ static double mpack_fmod_pow2_32(double a) { return a - ((double)(mpack_uint32_t)(a / POW2(32)) * POW2(32)); } libmpack-1.0.5/src/conv.h000066400000000000000000000043541306356722300152210ustar00rootroot00000000000000#ifndef MPACK_CONV_H #define MPACK_CONV_H #include "core.h" #if ULLONG_MAX == 0xffffffffffffffff typedef long long mpack_sintmax_t; typedef unsigned long long mpack_uintmax_t; #elif UINT64_MAX == 0xffffffffffffffff typedef int64_t mpack_sintmax_t; typedef uint64_t mpack_uintmax_t; #else typedef mpack_sint32_t mpack_sintmax_t; typedef mpack_uint32_t mpack_uintmax_t; #endif #ifndef bool # define bool unsigned #endif MPACK_API mpack_token_t mpack_pack_nil(void) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_boolean(unsigned v) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_uint(mpack_uintmax_t v) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_sint(mpack_sintmax_t v) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_float_compat(double v) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_float_fast(double v) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_number(double v) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_chunk(const char *p, mpack_uint32_t l) FUNUSED FPURE FNONULL; MPACK_API mpack_token_t mpack_pack_str(mpack_uint32_t l) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_bin(mpack_uint32_t l) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_ext(int type, mpack_uint32_t l) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_array(mpack_uint32_t l) FUNUSED FPURE; MPACK_API mpack_token_t mpack_pack_map(mpack_uint32_t l) FUNUSED FPURE; MPACK_API bool mpack_unpack_boolean(mpack_token_t t) FUNUSED FPURE; MPACK_API mpack_uintmax_t mpack_unpack_uint(mpack_token_t t) FUNUSED FPURE; MPACK_API mpack_sintmax_t mpack_unpack_sint(mpack_token_t t) FUNUSED FPURE; MPACK_API double mpack_unpack_float_fast(mpack_token_t t) FUNUSED FPURE; MPACK_API double mpack_unpack_float_compat(mpack_token_t t) FUNUSED FPURE; MPACK_API double mpack_unpack_number(mpack_token_t t) FUNUSED FPURE; /* The mpack_{pack,unpack}_float_fast functions should work in 99% of the * platforms. When compiling for a platform where floats don't use ieee754 as * the internal format, pass * -Dmpack_{pack,unpack}_float=mpack_{pack,unpack}_float_compat to the * compiler.*/ #ifndef mpack_pack_float # define mpack_pack_float mpack_pack_float_fast #endif #ifndef mpack_unpack_float # define mpack_unpack_float mpack_unpack_float_fast #endif #endif /* MPACK_CONV_H */ libmpack-1.0.5/src/core.c000066400000000000000000000414121306356722300151730ustar00rootroot00000000000000#include #include "core.h" #define UNUSED(p) (void)p; #define ADVANCE(buf, buflen) ((*buflen)--, (unsigned char)*((*buf)++)) #define TLEN(val, range_start) ((mpack_uint32_t)(1 << (val - range_start))) #ifndef MIN # define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) #endif static int mpack_rtoken(const char **buf, size_t *buflen, mpack_token_t *tok); static int mpack_rpending(const char **b, size_t *nl, mpack_tokbuf_t *tb); static int mpack_rvalue(mpack_token_type_t t, mpack_uint32_t l, const char **b, size_t *bl, mpack_token_t *tok); static int mpack_rblob(mpack_token_type_t t, mpack_uint32_t l, const char **b, size_t *bl, mpack_token_t *tok); static int mpack_wtoken(const mpack_token_t *tok, char **b, size_t *bl); static int mpack_wpending(char **b, size_t *bl, mpack_tokbuf_t *tb); static int mpack_wpint(char **b, size_t *bl, mpack_value_t v); static int mpack_wnint(char **b, size_t *bl, mpack_value_t v); static int mpack_wfloat(char **b, size_t *bl, const mpack_token_t *v); static int mpack_wstr(char **buf, size_t *buflen, mpack_uint32_t len); static int mpack_wbin(char **buf, size_t *buflen, mpack_uint32_t len); static int mpack_wext(char **buf, size_t *buflen, int type, mpack_uint32_t len); static int mpack_warray(char **buf, size_t *buflen, mpack_uint32_t len); static int mpack_wmap(char **buf, size_t *buflen, mpack_uint32_t len); static int mpack_w1(char **b, size_t *bl, mpack_uint32_t v); static int mpack_w2(char **b, size_t *bl, mpack_uint32_t v); static int mpack_w4(char **b, size_t *bl, mpack_uint32_t v); static mpack_value_t mpack_byte(unsigned char b); static int mpack_value(mpack_token_type_t t, mpack_uint32_t l, mpack_value_t v, mpack_token_t *tok); static int mpack_blob(mpack_token_type_t t, mpack_uint32_t l, int et, mpack_token_t *tok); MPACK_API void mpack_tokbuf_init(mpack_tokbuf_t *tokbuf) { tokbuf->ppos = 0; tokbuf->plen = 0; tokbuf->passthrough = 0; } MPACK_API int mpack_read(mpack_tokbuf_t *tokbuf, const char **buf, size_t *buflen, mpack_token_t *tok) { int status; size_t initial_ppos, ptrlen, advanced; const char *ptr, *ptr_save; assert(*buf && *buflen); if (tokbuf->passthrough) { /* pass data from str/bin/ext directly as a MPACK_TOKEN_CHUNK, adjusting * *buf and *buflen */ tok->type = MPACK_TOKEN_CHUNK; tok->data.chunk_ptr = *buf; tok->length = MIN((mpack_uint32_t)*buflen, tokbuf->passthrough); tokbuf->passthrough -= tok->length; *buf += tok->length; *buflen -= tok->length; goto done; } initial_ppos = tokbuf->ppos; if (tokbuf->plen) { if (!mpack_rpending(buf, buflen, tokbuf)) { return MPACK_EOF; } ptr = tokbuf->pending; ptrlen = tokbuf->ppos; } else { ptr = *buf; ptrlen = *buflen; } ptr_save = ptr; if ((status = mpack_rtoken(&ptr, &ptrlen, tok))) { if (status != MPACK_EOF) return MPACK_ERROR; /* need more data */ assert(!tokbuf->plen); /* read the remainder of *buf to tokbuf->pending so it can be parsed * later with more data. only required when tokbuf->plen == 0 or else * it would have been done already. */ tokbuf->plen = tok->length + 1; assert(tokbuf->plen <= sizeof(tokbuf->pending)); tokbuf->ppos = 0; status = mpack_rpending(buf, buflen, tokbuf); assert(!status); return MPACK_EOF; } advanced = (size_t)(ptr - ptr_save) - initial_ppos; tokbuf->plen = tokbuf->ppos = 0; *buflen -= advanced; *buf += advanced; if (tok->type > MPACK_TOKEN_MAP) { tokbuf->passthrough = tok->length; } done: return MPACK_OK; } MPACK_API int mpack_write(mpack_tokbuf_t *tokbuf, char **buf, size_t *buflen, const mpack_token_t *t) { int status; char *ptr; size_t ptrlen; mpack_token_t tok = tokbuf->plen ? tokbuf->pending_tok : *t; assert(*buf && *buflen); if (tok.type == MPACK_TOKEN_CHUNK) { size_t written, pending, count; if (!tokbuf->plen) tokbuf->ppos = 0; written = tokbuf->ppos; pending = tok.length - written; count = MIN(pending, *buflen); memcpy(*buf, tok.data.chunk_ptr + written, count); *buf += count; *buflen -= count; tokbuf->ppos += count; tokbuf->plen = count == pending ? 0 : tok.length; if (count == pending) { return MPACK_OK; } else { tokbuf->pending_tok = tok; return MPACK_EOF; } } if (tokbuf->plen) return mpack_wpending(buf, buflen, tokbuf); if (*buflen < MPACK_MAX_TOKEN_LEN) { ptr = tokbuf->pending; ptrlen = sizeof(tokbuf->pending); } else { ptr = *buf; ptrlen = *buflen; } if ((status = mpack_wtoken(&tok, &ptr, &ptrlen))) return status; if (*buflen < MPACK_MAX_TOKEN_LEN) { size_t toklen = sizeof(tokbuf->pending) - ptrlen; size_t write_cnt = MIN(toklen, *buflen); memcpy(*buf, tokbuf->pending, write_cnt); *buf += write_cnt; *buflen -= write_cnt; if (write_cnt < toklen) { assert(!*buflen); tokbuf->plen = toklen; tokbuf->ppos = write_cnt; tokbuf->pending_tok = tok; return MPACK_EOF; } } else { *buflen -= (size_t)(ptr - *buf); *buf = ptr; } return MPACK_OK; } static int mpack_rtoken(const char **buf, size_t *buflen, mpack_token_t *tok) { unsigned char t = ADVANCE(buf, buflen); if (t < 0x80) { /* positive fixint */ return mpack_value(MPACK_TOKEN_UINT, 1, mpack_byte(t), tok); } else if (t < 0x90) { /* fixmap */ return mpack_blob(MPACK_TOKEN_MAP, t & 0xf, 0, tok); } else if (t < 0xa0) { /* fixarray */ return mpack_blob(MPACK_TOKEN_ARRAY, t & 0xf, 0, tok); } else if (t < 0xc0) { /* fixstr */ return mpack_blob(MPACK_TOKEN_STR, t & 0x1f, 0, tok); } else if (t < 0xe0) { switch (t) { case 0xc0: /* nil */ return mpack_value(MPACK_TOKEN_NIL, 0, mpack_byte(0), tok); case 0xc2: /* false */ return mpack_value(MPACK_TOKEN_BOOLEAN, 1, mpack_byte(0), tok); case 0xc3: /* true */ return mpack_value(MPACK_TOKEN_BOOLEAN, 1, mpack_byte(1), tok); case 0xc4: /* bin 8 */ case 0xc5: /* bin 16 */ case 0xc6: /* bin 32 */ return mpack_rblob(MPACK_TOKEN_BIN, TLEN(t, 0xc4), buf, buflen, tok); case 0xc7: /* ext 8 */ case 0xc8: /* ext 16 */ case 0xc9: /* ext 32 */ return mpack_rblob(MPACK_TOKEN_EXT, TLEN(t, 0xc7), buf, buflen, tok); case 0xca: /* float 32 */ case 0xcb: /* float 64 */ return mpack_rvalue(MPACK_TOKEN_FLOAT, TLEN(t, 0xc8), buf, buflen, tok); case 0xcc: /* uint 8 */ case 0xcd: /* uint 16 */ case 0xce: /* uint 32 */ case 0xcf: /* uint 64 */ return mpack_rvalue(MPACK_TOKEN_UINT, TLEN(t, 0xcc), buf, buflen, tok); case 0xd0: /* int 8 */ case 0xd1: /* int 16 */ case 0xd2: /* int 32 */ case 0xd3: /* int 64 */ return mpack_rvalue(MPACK_TOKEN_SINT, TLEN(t, 0xd0), buf, buflen, tok); case 0xd4: /* fixext 1 */ case 0xd5: /* fixext 2 */ case 0xd6: /* fixext 4 */ case 0xd7: /* fixext 8 */ case 0xd8: /* fixext 16 */ if (*buflen == 0) { /* require only one extra byte for the type code */ tok->length = 1; return MPACK_EOF; } tok->length = TLEN(t, 0xd4); tok->type = MPACK_TOKEN_EXT; tok->data.ext_type = ADVANCE(buf, buflen); return MPACK_OK; case 0xd9: /* str 8 */ case 0xda: /* str 16 */ case 0xdb: /* str 32 */ return mpack_rblob(MPACK_TOKEN_STR, TLEN(t, 0xd9), buf, buflen, tok); case 0xdc: /* array 16 */ case 0xdd: /* array 32 */ return mpack_rblob(MPACK_TOKEN_ARRAY, TLEN(t, 0xdb), buf, buflen, tok); case 0xde: /* map 16 */ case 0xdf: /* map 32 */ return mpack_rblob(MPACK_TOKEN_MAP, TLEN(t, 0xdd), buf, buflen, tok); default: return MPACK_ERROR; } } else { /* negative fixint */ return mpack_value(MPACK_TOKEN_SINT, 1, mpack_byte(t), tok); } } static int mpack_rpending(const char **buf, size_t *buflen, mpack_tokbuf_t *state) { size_t count; assert(state->ppos < state->plen); count = MIN(state->plen - state->ppos, *buflen); memcpy(state->pending + state->ppos, *buf, count); state->ppos += count; if (state->ppos < state->plen) { /* consume buffer since no token will be parsed yet. */ *buf += *buflen; *buflen = 0; return 0; } return 1; } static int mpack_rvalue(mpack_token_type_t type, mpack_uint32_t remaining, const char **buf, size_t *buflen, mpack_token_t *tok) { if (*buflen < remaining) { tok->length = remaining; return MPACK_EOF; } mpack_value(type, remaining, mpack_byte(0), tok); while (remaining) { mpack_uint32_t byte = ADVANCE(buf, buflen), byte_idx, byte_shift; byte_idx = (mpack_uint32_t)--remaining; byte_shift = (byte_idx % 4) * 8; tok->data.value.lo |= byte << byte_shift; if (remaining == 4) { /* unpacked the first half of a 8-byte value, shift what was parsed to the * "hi" field and reset "lo" for the trailing 4 bytes. */ tok->data.value.hi = tok->data.value.lo; tok->data.value.lo = 0; } } if (type == MPACK_TOKEN_SINT) { mpack_uint32_t hi = tok->data.value.hi; mpack_uint32_t lo = tok->data.value.lo; mpack_uint32_t msb = (tok->length == 8 && hi >> 31) || (tok->length == 4 && lo >> 31) || (tok->length == 2 && lo >> 15) || (tok->length == 1 && lo >> 7); if (!msb) { tok->type = MPACK_TOKEN_UINT; } } return MPACK_OK; } static int mpack_rblob(mpack_token_type_t type, mpack_uint32_t tlen, const char **buf, size_t *buflen, mpack_token_t *tok) { mpack_token_t l; mpack_uint32_t required = tlen + (type == MPACK_TOKEN_EXT ? 1 : 0); if (*buflen < required) { tok->length = required; return MPACK_EOF; } l.data.value.lo = 0; mpack_rvalue(MPACK_TOKEN_UINT, tlen, buf, buflen, &l); tok->type = type; tok->length = l.data.value.lo; if (type == MPACK_TOKEN_EXT) { tok->data.ext_type = ADVANCE(buf, buflen); } return MPACK_OK; } static int mpack_wtoken(const mpack_token_t *tok, char **buf, size_t *buflen) { switch (tok->type) { case MPACK_TOKEN_NIL: return mpack_w1(buf, buflen, 0xc0); case MPACK_TOKEN_BOOLEAN: return mpack_w1(buf, buflen, tok->data.value.lo ? 0xc3 : 0xc2); case MPACK_TOKEN_UINT: return mpack_wpint(buf, buflen, tok->data.value); case MPACK_TOKEN_SINT: return mpack_wnint(buf, buflen, tok->data.value); case MPACK_TOKEN_FLOAT: return mpack_wfloat(buf, buflen, tok); case MPACK_TOKEN_BIN: return mpack_wbin(buf, buflen, tok->length); case MPACK_TOKEN_STR: return mpack_wstr(buf, buflen, tok->length); case MPACK_TOKEN_EXT: return mpack_wext(buf, buflen, tok->data.ext_type, tok->length); case MPACK_TOKEN_ARRAY: return mpack_warray(buf, buflen, tok->length); case MPACK_TOKEN_MAP: return mpack_wmap(buf, buflen, tok->length); default: return MPACK_ERROR; } } static int mpack_wpending(char **buf, size_t *buflen, mpack_tokbuf_t *state) { size_t count; assert(state->ppos < state->plen); count = MIN(state->plen - state->ppos, *buflen); memcpy(*buf, state->pending + state->ppos, count); state->ppos += count; *buf += count; *buflen -= count; if (state->ppos == state->plen) { state->plen = 0; return MPACK_OK; } return MPACK_EOF; } static int mpack_wpint(char **buf, size_t *buflen, mpack_value_t val) { mpack_uint32_t hi = val.hi; mpack_uint32_t lo = val.lo; if (hi) { /* uint 64 */ return mpack_w1(buf, buflen, 0xcf) || mpack_w4(buf, buflen, hi) || mpack_w4(buf, buflen, lo); } else if (lo > 0xffff) { /* uint 32 */ return mpack_w1(buf, buflen, 0xce) || mpack_w4(buf, buflen, lo); } else if (lo > 0xff) { /* uint 16 */ return mpack_w1(buf, buflen, 0xcd) || mpack_w2(buf, buflen, lo); } else if (lo > 0x7f) { /* uint 8 */ return mpack_w1(buf, buflen, 0xcc) || mpack_w1(buf, buflen, lo); } else { return mpack_w1(buf, buflen, lo); } } static int mpack_wnint(char **buf, size_t *buflen, mpack_value_t val) { mpack_uint32_t hi = val.hi; mpack_uint32_t lo = val.lo; if (lo < 0x80000000) { /* int 64 */ return mpack_w1(buf, buflen, 0xd3) || mpack_w4(buf, buflen, hi) || mpack_w4(buf, buflen, lo); } else if (lo < 0xffff7fff) { /* int 32 */ return mpack_w1(buf, buflen, 0xd2) || mpack_w4(buf, buflen, lo); } else if (lo < 0xffffff7f) { /* int 16 */ return mpack_w1(buf, buflen, 0xd1) || mpack_w2(buf, buflen, lo); } else if (lo < 0xffffffe0) { /* int 8 */ return mpack_w1(buf, buflen, 0xd0) || mpack_w1(buf, buflen, lo); } else { /* negative fixint */ return mpack_w1(buf, buflen, (mpack_uint32_t)(0x100 + lo)); } } static int mpack_wfloat(char **buf, size_t *buflen, const mpack_token_t *tok) { if (tok->length == 4) { return mpack_w1(buf, buflen, 0xca) || mpack_w4(buf, buflen, tok->data.value.lo); } else if (tok->length == 8) { return mpack_w1(buf, buflen, 0xcb) || mpack_w4(buf, buflen, tok->data.value.hi) || mpack_w4(buf, buflen, tok->data.value.lo); } else { return MPACK_ERROR; } } static int mpack_wstr(char **buf, size_t *buflen, mpack_uint32_t len) { if (len < 0x20) { return mpack_w1(buf, buflen, 0xa0 | len); } else if (len < 0x100) { return mpack_w1(buf, buflen, 0xd9) || mpack_w1(buf, buflen, len); } else if (len < 0x10000) { return mpack_w1(buf, buflen, 0xda) || mpack_w2(buf, buflen, len); } else { return mpack_w1(buf, buflen, 0xdb) || mpack_w4(buf, buflen, len); } } static int mpack_wbin(char **buf, size_t *buflen, mpack_uint32_t len) { if (len < 0x100) { return mpack_w1(buf, buflen, 0xc4) || mpack_w1(buf, buflen, len); } else if (len < 0x10000) { return mpack_w1(buf, buflen, 0xc5) || mpack_w2(buf, buflen, len); } else { return mpack_w1(buf, buflen, 0xc6) || mpack_w4(buf, buflen, len); } } static int mpack_wext(char **buf, size_t *buflen, int type, mpack_uint32_t len) { mpack_uint32_t t; assert(type >= 0 && type < 0x80); t = (mpack_uint32_t)type; switch (len) { case 1: mpack_w1(buf, buflen, 0xd4); return mpack_w1(buf, buflen, t); case 2: mpack_w1(buf, buflen, 0xd5); return mpack_w1(buf, buflen, t); case 4: mpack_w1(buf, buflen, 0xd6); return mpack_w1(buf, buflen, t); case 8: mpack_w1(buf, buflen, 0xd7); return mpack_w1(buf, buflen, t); case 16: mpack_w1(buf, buflen, 0xd8); return mpack_w1(buf, buflen, t); default: if (len < 0x100) { return mpack_w1(buf, buflen, 0xc7) || mpack_w1(buf, buflen, len) || mpack_w1(buf, buflen, t); } else if (len < 0x10000) { return mpack_w1(buf, buflen, 0xc8) || mpack_w2(buf, buflen, len) || mpack_w1(buf, buflen, t); } else { return mpack_w1(buf, buflen, 0xc9) || mpack_w4(buf, buflen, len) || mpack_w1(buf, buflen, t); } } } static int mpack_warray(char **buf, size_t *buflen, mpack_uint32_t len) { if (len < 0x10) { return mpack_w1(buf, buflen, 0x90 | len); } else if (len < 0x10000) { return mpack_w1(buf, buflen, 0xdc) || mpack_w2(buf, buflen, len); } else { return mpack_w1(buf, buflen, 0xdd) || mpack_w4(buf, buflen, len); } } static int mpack_wmap(char **buf, size_t *buflen, mpack_uint32_t len) { if (len < 0x10) { return mpack_w1(buf, buflen, 0x80 | len); } else if (len < 0x10000) { return mpack_w1(buf, buflen, 0xde) || mpack_w2(buf, buflen, len); } else { return mpack_w1(buf, buflen, 0xdf) || mpack_w4(buf, buflen, len); } } static int mpack_w1(char **b, size_t *bl, mpack_uint32_t v) { (*bl)--; *(*b)++ = (char)(v & 0xff); return MPACK_OK; } static int mpack_w2(char **b, size_t *bl, mpack_uint32_t v) { *bl -= 2; *(*b)++ = (char)((v >> 8) & 0xff); *(*b)++ = (char)(v & 0xff); return MPACK_OK; } static int mpack_w4(char **b, size_t *bl, mpack_uint32_t v) { *bl -= 4; *(*b)++ = (char)((v >> 24) & 0xff); *(*b)++ = (char)((v >> 16) & 0xff); *(*b)++ = (char)((v >> 8) & 0xff); *(*b)++ = (char)(v & 0xff); return MPACK_OK; } static int mpack_value(mpack_token_type_t type, mpack_uint32_t length, mpack_value_t value, mpack_token_t *tok) { tok->type = type; tok->length = length; tok->data.value = value; return MPACK_OK; } static int mpack_blob(mpack_token_type_t type, mpack_uint32_t length, int ext_type, mpack_token_t *tok) { tok->type = type; tok->length = length; tok->data.ext_type = ext_type; return MPACK_OK; } static mpack_value_t mpack_byte(unsigned char byte) { mpack_value_t rv; rv.lo = byte; rv.hi = 0; return rv; } libmpack-1.0.5/src/core.h000066400000000000000000000043561306356722300152060ustar00rootroot00000000000000#ifndef MPACK_CORE_H #define MPACK_CORE_H #ifndef MPACK_API # define MPACK_API extern #endif #include #include #include #ifdef __GNUC__ # define FPURE __attribute__((const)) # define FNONULL __attribute__((nonnull)) # define FNONULL_ARG(x) __attribute__((nonnull x)) # define FUNUSED __attribute__((unused)) #else # define FPURE # define FNONULL # define FNONULL_ARG(x) # define FUNUSED #endif #if UINT_MAX == 0xffffffff typedef int mpack_sint32_t; typedef unsigned int mpack_uint32_t; #elif ULONG_MAX == 0xffffffff typedef long mpack_sint32_t; typedef unsigned long mpack_uint32_t; #else # error "can't find unsigned 32-bit integer type" #endif typedef struct mpack_value_s { mpack_uint32_t lo, hi; } mpack_value_t; enum { MPACK_OK = 0, MPACK_EOF = 1, MPACK_ERROR = 2 }; #define MPACK_MAX_TOKEN_LEN 9 /* 64-bit ints/floats plus type code */ typedef enum { MPACK_TOKEN_NIL = 1, MPACK_TOKEN_BOOLEAN = 2, MPACK_TOKEN_UINT = 3, MPACK_TOKEN_SINT = 4, MPACK_TOKEN_FLOAT = 5, MPACK_TOKEN_CHUNK = 6, MPACK_TOKEN_ARRAY = 7, MPACK_TOKEN_MAP = 8, MPACK_TOKEN_BIN = 9, MPACK_TOKEN_STR = 10, MPACK_TOKEN_EXT = 11 } mpack_token_type_t; typedef struct mpack_token_s { mpack_token_type_t type; /* Type of token */ mpack_uint32_t length; /* Byte length for str/bin/ext/chunk/float/int/uint. Item count for array/map. */ union { mpack_value_t value; /* 32-bit parts of primitives (bool,int,float) */ const char *chunk_ptr; /* Chunk of data from str/bin/ext */ int ext_type; /* Type field for ext tokens */ } data; } mpack_token_t; typedef struct mpack_tokbuf_s { char pending[MPACK_MAX_TOKEN_LEN]; mpack_token_t pending_tok; size_t ppos, plen; mpack_uint32_t passthrough; } mpack_tokbuf_t; #define MPACK_TOKBUF_INITIAL_VALUE { { 0 }, { 0, 0, { { 0, 0 } } }, 0, 0, 0 } MPACK_API void mpack_tokbuf_init(mpack_tokbuf_t *tb) FUNUSED FNONULL; MPACK_API int mpack_read(mpack_tokbuf_t *tb, const char **b, size_t *bl, mpack_token_t *tok) FUNUSED FNONULL; MPACK_API int mpack_write(mpack_tokbuf_t *tb, char **b, size_t *bl, const mpack_token_t *tok) FUNUSED FNONULL; #endif /* MPACK_CORE_H */ libmpack-1.0.5/src/mpack.c000066400000000000000000000001111306356722300153250ustar00rootroot00000000000000#include "core.c" #include "conv.c" #include "object.c" #include "rpc.c" libmpack-1.0.5/src/object.c000066400000000000000000000146611306356722300155170ustar00rootroot00000000000000#include #include "object.h" static int mpack_parser_full(mpack_parser_t *w); static mpack_node_t *mpack_parser_push(mpack_parser_t *w); static mpack_node_t *mpack_parser_pop(mpack_parser_t *w); MPACK_API void mpack_parser_init(mpack_parser_t *parser, mpack_uint32_t capacity) { mpack_tokbuf_init(&parser->tokbuf); parser->data.p = NULL; parser->capacity = capacity ? capacity : MPACK_MAX_OBJECT_DEPTH; parser->size = 0; parser->exiting = 0; memset(parser->items, 0, sizeof(mpack_node_t) * (parser->capacity + 1)); parser->items[0].pos = (size_t)-1; parser->status = 0; } #define MPACK_EXCEPTION_CHECK(parser) \ do { \ if (parser->status == MPACK_EXCEPTION) { \ return MPACK_EXCEPTION; \ } \ } while (0) #define MPACK_WALK(action) \ do { \ mpack_node_t *n; \ \ if (parser->exiting) goto exit; \ if (mpack_parser_full(parser)) return MPACK_NOMEM; \ n = mpack_parser_push(parser); \ action; \ MPACK_EXCEPTION_CHECK(parser); \ parser->exiting = 1; \ return MPACK_EOF; \ \ exit: \ parser->exiting = 0; \ while ((n = mpack_parser_pop(parser))) { \ exit_cb(parser, n); \ MPACK_EXCEPTION_CHECK(parser); \ if (!parser->size) return MPACK_OK; \ } \ \ return MPACK_EOF; \ } while (0) MPACK_API int mpack_parse_tok(mpack_parser_t *parser, mpack_token_t tok, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) { MPACK_EXCEPTION_CHECK(parser); MPACK_WALK({n->tok = tok; enter_cb(parser, n);}); } MPACK_API int mpack_unparse_tok(mpack_parser_t *parser, mpack_token_t *tok, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) { MPACK_EXCEPTION_CHECK(parser); MPACK_WALK({enter_cb(parser, n); *tok = n->tok;}); } MPACK_API int mpack_parse(mpack_parser_t *parser, const char **buf, size_t *buflen, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) { int status = MPACK_EOF; MPACK_EXCEPTION_CHECK(parser); while (*buflen && status) { mpack_token_t tok; mpack_tokbuf_t *tb = &parser->tokbuf; const char *buf_save = *buf; size_t buflen_save = *buflen; if ((status = mpack_read(tb, buf, buflen, &tok)) == MPACK_EOF) continue; else if (status == MPACK_ERROR) goto rollback; do { status = mpack_parse_tok(parser, tok, enter_cb, exit_cb); MPACK_EXCEPTION_CHECK(parser); } while (parser->exiting); if (status != MPACK_NOMEM) continue; rollback: /* restore buf/buflen so the next call will try to read the same token */ *buf = buf_save; *buflen = buflen_save; break; } return status; } MPACK_API int mpack_unparse(mpack_parser_t *parser, char **buf, size_t *buflen, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) { int status = MPACK_EOF; MPACK_EXCEPTION_CHECK(parser); while (*buflen && status) { int write_status; mpack_token_t tok; mpack_tokbuf_t *tb = &parser->tokbuf; if (!tb->plen) parser->status = mpack_unparse_tok(parser, &tok, enter_cb, exit_cb); MPACK_EXCEPTION_CHECK(parser); status = parser->status; if (status == MPACK_NOMEM) break; if (parser->exiting) { write_status = mpack_write(tb, buf, buflen, &tok); status = write_status ? write_status : status; } } return status; } MPACK_API void mpack_parser_copy(mpack_parser_t *dst, mpack_parser_t *src) { mpack_uint32_t i; mpack_uint32_t dst_capacity = dst->capacity; assert(src->capacity <= dst_capacity); /* copy all fields except the stack */ memcpy(dst, src, sizeof(mpack_one_parser_t) - sizeof(mpack_node_t)); /* reset capacity */ dst->capacity = dst_capacity; /* copy the stack */ for (i = 0; i <= src->capacity; i++) { dst->items[i] = src->items[i]; } } static int mpack_parser_full(mpack_parser_t *parser) { return parser->size == parser->capacity; } static mpack_node_t *mpack_parser_push(mpack_parser_t *parser) { mpack_node_t *top; assert(parser->size < parser->capacity); top = parser->items + parser->size + 1; top->data[0].p = NULL; top->data[1].p = NULL; top->pos = 0; top->key_visited = 0; /* increase size and invoke callback, passing parent node if any */ parser->size++; return top; } static mpack_node_t *mpack_parser_pop(mpack_parser_t *parser) { mpack_node_t *top, *parent; assert(parser->size); top = parser->items + parser->size; if (top->tok.type > MPACK_TOKEN_CHUNK && top->pos < top->tok.length) { /* continue processing children */ return NULL; } parent = MPACK_PARENT_NODE(top); if (parent) { /* we use parent->tok.length to keep track of how many children remain. * update it to reflect the processed node. */ if (top->tok.type == MPACK_TOKEN_CHUNK) { parent->pos += top->tok.length; } else if (parent->tok.type == MPACK_TOKEN_MAP) { /* maps allow up to 2^32 - 1 pairs, so to allow this many items in a * 32-bit length variable we use an additional flag to determine if the * key of a certain position was visited */ if (parent->key_visited) { parent->pos++; } parent->key_visited = !parent->key_visited; } else { parent->pos++; } } parser->size--; return top; } libmpack-1.0.5/src/object.h000066400000000000000000000052261306356722300155210ustar00rootroot00000000000000#ifndef MPACK_OBJECT_H #define MPACK_OBJECT_H #include "core.h" #include "conv.h" #ifndef MPACK_MAX_OBJECT_DEPTH # define MPACK_MAX_OBJECT_DEPTH 32 #endif #define MPACK_PARENT_NODE(n) (((n) - 1)->pos == (size_t)-1 ? NULL : (n) - 1) #define MPACK_THROW(parser) \ do { \ parser->status = MPACK_EXCEPTION; \ return; \ } while (0) enum { MPACK_EXCEPTION = -1, MPACK_NOMEM = MPACK_ERROR + 1 }; /* Storing integer in pointers in undefined behavior according to the C * standard. Define a union type to accomodate arbitrary user data associated * with nodes(and with requests in rpc.h). */ typedef union { void *p; mpack_uintmax_t u; mpack_sintmax_t i; double d; } mpack_data_t; typedef struct mpack_node_s { mpack_token_t tok; size_t pos; /* flag to determine if the key was visited when traversing a map */ int key_visited; /* allow 2 instances mpack_data_t per node. the reason is that when * serializing, the user may need to keep track of traversal state besides the * parent node reference */ mpack_data_t data[2]; } mpack_node_t; #define MPACK_PARSER_STRUCT(c) \ struct { \ mpack_data_t data; \ mpack_uint32_t size, capacity; \ int status; \ int exiting; \ mpack_tokbuf_t tokbuf; \ mpack_node_t items[c + 1]; \ } /* Some compilers warn against anonymous structs: * https://github.com/libmpack/libmpack/issues/6 */ typedef MPACK_PARSER_STRUCT(0) mpack_one_parser_t; #define MPACK_PARSER_STRUCT_SIZE(c) \ (sizeof(mpack_node_t) * c + \ sizeof(mpack_one_parser_t)) typedef MPACK_PARSER_STRUCT(MPACK_MAX_OBJECT_DEPTH) mpack_parser_t; typedef void(*mpack_walk_cb)(mpack_parser_t *w, mpack_node_t *n); MPACK_API void mpack_parser_init(mpack_parser_t *p, mpack_uint32_t c) FUNUSED FNONULL; MPACK_API int mpack_parse_tok(mpack_parser_t *walker, mpack_token_t tok, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) FUNUSED FNONULL_ARG((1,3,4)); MPACK_API int mpack_unparse_tok(mpack_parser_t *walker, mpack_token_t *tok, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) FUNUSED FNONULL_ARG((1,2,3,4)); MPACK_API int mpack_parse(mpack_parser_t *parser, const char **b, size_t *bl, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) FUNUSED FNONULL_ARG((1,2,3,4,5)); MPACK_API int mpack_unparse(mpack_parser_t *parser, char **b, size_t *bl, mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) FUNUSED FNONULL_ARG((1,2,3,4,5)); MPACK_API void mpack_parser_copy(mpack_parser_t *d, mpack_parser_t *s) FUNUSED FNONULL; #endif /* MPACK_OBJECT_H */ libmpack-1.0.5/src/rpc.c000066400000000000000000000212661306356722300150340ustar00rootroot00000000000000#include #include "rpc.h" enum { MPACK_RPC_RECEIVE_ARRAY = 1, MPACK_RPC_RECEIVE_TYPE, MPACK_RPC_RECEIVE_ID }; static mpack_rpc_header_t mpack_rpc_request_hdr(void); static mpack_rpc_header_t mpack_rpc_reply_hdr(void); static mpack_rpc_header_t mpack_rpc_notify_hdr(void); static int mpack_rpc_put(mpack_rpc_session_t *s, mpack_rpc_message_t m); static int mpack_rpc_pop(mpack_rpc_session_t *s, mpack_rpc_message_t *m); static void mpack_rpc_reset_hdr(mpack_rpc_header_t *hdr); MPACK_API void mpack_rpc_session_init(mpack_rpc_session_t *session, mpack_uint32_t capacity) { session->capacity = capacity ? capacity : MPACK_RPC_MAX_REQUESTS; session->request_id = 0; mpack_tokbuf_init(&session->reader); mpack_tokbuf_init(&session->writer); mpack_rpc_reset_hdr(&session->receive); mpack_rpc_reset_hdr(&session->send); memset(session->slots, 0, sizeof(struct mpack_rpc_slot_s) * session->capacity); } MPACK_API int mpack_rpc_receive_tok(mpack_rpc_session_t *session, mpack_token_t tok, mpack_rpc_message_t *msg) { int type; if (session->receive.index == 0) { if (tok.type != MPACK_TOKEN_ARRAY) /* not an array */ return MPACK_RPC_EARRAY; if (tok.length < 3 || tok.length > 4) /* invalid array length */ return MPACK_RPC_EARRAYL; session->receive.toks[0] = tok; session->receive.index++; return MPACK_EOF; /* get the type */ } if (session->receive.index == 1) { if (tok.type != MPACK_TOKEN_UINT || tok.length > 1 || tok.data.value.lo > 2) /* invalid type */ return MPACK_RPC_ETYPE; if (tok.data.value.lo < 2 && session->receive.toks[0].length != 4) /* request or response with array length != 4 */ return MPACK_RPC_EARRAYL; if (tok.data.value.lo == 2 && session->receive.toks[0].length != 3) /* notification with array length != 3 */ return MPACK_RPC_EARRAYL; session->receive.toks[1] = tok; session->receive.index++; if (tok.data.value.lo < 2) return MPACK_EOF; type = MPACK_RPC_NOTIFICATION; goto end; } assert(session->receive.index == 2); if (tok.type != MPACK_TOKEN_UINT || tok.length > 4) /* invalid request/response id */ return MPACK_RPC_EMSGID; msg->id = tok.data.value.lo; msg->data.p = NULL; type = (int)session->receive.toks[1].data.value.lo + MPACK_RPC_REQUEST; if (type == MPACK_RPC_RESPONSE && !mpack_rpc_pop(session, msg)) /* response with invalid id */ return MPACK_RPC_ERESPID; end: mpack_rpc_reset_hdr(&session->receive); return type; } MPACK_API int mpack_rpc_request_tok(mpack_rpc_session_t *session, mpack_token_t *tok, mpack_data_t data) { if (session->send.index == 0) { int status; mpack_rpc_message_t msg; do { msg.id = session->request_id; msg.data = data; session->send = mpack_rpc_request_hdr(); session->send.toks[2].type = MPACK_TOKEN_UINT; session->send.toks[2].data.value.lo = msg.id; session->send.toks[2].data.value.hi = 0; *tok = session->send.toks[0]; status = mpack_rpc_put(session, msg); if (status == -1) return MPACK_NOMEM; session->request_id = (session->request_id + 1) % 0xffffffff; } while (!status); session->send.index++; return MPACK_EOF; } if (session->send.index == 1) { *tok = session->send.toks[1]; session->send.index++; return MPACK_EOF; } assert(session->send.index == 2); *tok = session->send.toks[2]; mpack_rpc_reset_hdr(&session->send); return MPACK_OK; } MPACK_API int mpack_rpc_reply_tok(mpack_rpc_session_t *session, mpack_token_t *tok, mpack_uint32_t id) { if (session->send.index == 0) { session->send = mpack_rpc_reply_hdr(); session->send.toks[2].type = MPACK_TOKEN_UINT; session->send.toks[2].data.value.lo = id; session->send.toks[2].data.value.hi = 0; *tok = session->send.toks[0]; session->send.index++; return MPACK_EOF; } if (session->send.index == 1) { *tok = session->send.toks[1]; session->send.index++; return MPACK_EOF; } assert(session->send.index == 2); *tok = session->send.toks[2]; mpack_rpc_reset_hdr(&session->send); return MPACK_OK; } MPACK_API int mpack_rpc_notify_tok(mpack_rpc_session_t *session, mpack_token_t *tok) { if (session->send.index == 0) { session->send = mpack_rpc_notify_hdr(); *tok = session->send.toks[0]; session->send.index++; return MPACK_EOF; } assert(session->send.index == 1); *tok = session->send.toks[1]; mpack_rpc_reset_hdr(&session->send); return MPACK_OK; } MPACK_API int mpack_rpc_receive(mpack_rpc_session_t *session, const char **buf, size_t *buflen, mpack_rpc_message_t *msg) { int status; do { mpack_token_t tok; status = mpack_read(&session->reader, buf, buflen, &tok); if (status) break; status = mpack_rpc_receive_tok(session, tok, msg); if (status >= MPACK_RPC_REQUEST) break; } while (*buflen); return status; } MPACK_API int mpack_rpc_request(mpack_rpc_session_t *session, char **buf, size_t *buflen, mpack_data_t data) { int status = MPACK_EOF; while (status && *buflen) { int write_status; mpack_token_t tok; if (!session->writer.plen) { status = mpack_rpc_request_tok(session, &tok, data); } if (status == MPACK_NOMEM) break; write_status = mpack_write(&session->writer, buf, buflen, &tok); status = write_status ? write_status : status; } return status; } MPACK_API int mpack_rpc_reply(mpack_rpc_session_t *session, char **buf, size_t *buflen, mpack_uint32_t id) { int status = MPACK_EOF; while (status && *buflen) { int write_status; mpack_token_t tok; if (!session->writer.plen) { status = mpack_rpc_reply_tok(session, &tok, id); } write_status = mpack_write(&session->writer, buf, buflen, &tok); status = write_status ? write_status : status; } return status; } MPACK_API int mpack_rpc_notify(mpack_rpc_session_t *session, char **buf, size_t *buflen) { int status = MPACK_EOF; while (status && *buflen) { int write_status; mpack_token_t tok; if (!session->writer.plen) { status = mpack_rpc_notify_tok(session, &tok); } write_status = mpack_write(&session->writer, buf, buflen, &tok); status = write_status ? write_status : status; } return status; } MPACK_API void mpack_rpc_session_copy(mpack_rpc_session_t *dst, mpack_rpc_session_t *src) { mpack_uint32_t i; mpack_uint32_t dst_capacity = dst->capacity; assert(src->capacity <= dst_capacity); /* copy all fields except slots */ memcpy(dst, src, sizeof(mpack_rpc_one_session_t) - sizeof(struct mpack_rpc_slot_s)); /* reset capacity */ dst->capacity = dst_capacity; /* reinsert requests */ memset(dst->slots, 0, sizeof(struct mpack_rpc_slot_s) * dst->capacity); for (i = 0; i < src->capacity; i++) { if (src->slots[i].used) mpack_rpc_put(dst, src->slots[i].msg); } } static mpack_rpc_header_t mpack_rpc_request_hdr(void) { mpack_rpc_header_t hdr; hdr.index = 0; hdr.toks[0].type = MPACK_TOKEN_ARRAY; hdr.toks[0].length = 4; hdr.toks[1].type = MPACK_TOKEN_UINT; hdr.toks[1].data.value.lo = 0; hdr.toks[1].data.value.hi = 0; return hdr; } static mpack_rpc_header_t mpack_rpc_reply_hdr(void) { mpack_rpc_header_t hdr = mpack_rpc_request_hdr(); hdr.toks[1].data.value.lo = 1; hdr.toks[1].data.value.hi = 0; return hdr; } static mpack_rpc_header_t mpack_rpc_notify_hdr(void) { mpack_rpc_header_t hdr = mpack_rpc_request_hdr(); hdr.toks[0].length = 3; hdr.toks[1].data.value.lo = 2; hdr.toks[1].data.value.hi = 0; return hdr; } static int mpack_rpc_put(mpack_rpc_session_t *session, mpack_rpc_message_t msg) { struct mpack_rpc_slot_s *slot = NULL; mpack_uint32_t i; mpack_uint32_t hash = msg.id % session->capacity; for (i = 0; i < session->capacity; i++) { if (!session->slots[hash].used || session->slots[hash].msg.id == msg.id) { slot = session->slots + hash; break; } hash = hash > 0 ? hash - 1 : session->capacity - 1; } if (!slot) return -1; /* no space */ if (slot->msg.id == msg.id && slot->used) return 0; /* duplicate key */ slot->msg = msg; slot->used = 1; return 1; } static int mpack_rpc_pop(mpack_rpc_session_t *session, mpack_rpc_message_t *msg) { struct mpack_rpc_slot_s *slot = NULL; mpack_uint32_t i; mpack_uint32_t hash = msg->id % session->capacity; for (i = 0; i < session->capacity; i++) { if (session->slots[hash].used && session->slots[hash].msg.id == msg->id) { slot = session->slots + hash; break; } hash = hash > 0 ? hash - 1 : session->capacity - 1; } if (!slot) return 0; *msg = slot->msg; slot->used = 0; return 1; } static void mpack_rpc_reset_hdr(mpack_rpc_header_t *hdr) { hdr->index = 0; } libmpack-1.0.5/src/rpc.h000066400000000000000000000046221306356722300150360ustar00rootroot00000000000000#ifndef MPACK_RPC_H #define MPACK_RPC_H #include "core.h" #include "object.h" #ifndef MPACK_RPC_MAX_REQUESTS # define MPACK_RPC_MAX_REQUESTS 32 #endif enum { MPACK_RPC_REQUEST = MPACK_NOMEM + 1, MPACK_RPC_RESPONSE, MPACK_RPC_NOTIFICATION, MPACK_RPC_ERROR }; enum { MPACK_RPC_EARRAY = MPACK_RPC_ERROR, MPACK_RPC_EARRAYL, MPACK_RPC_ETYPE, MPACK_RPC_EMSGID, MPACK_RPC_ERESPID }; typedef struct mpack_rpc_header_s { mpack_token_t toks[3]; int index; } mpack_rpc_header_t; typedef struct mpack_rpc_message_s { mpack_uint32_t id; mpack_data_t data; } mpack_rpc_message_t; struct mpack_rpc_slot_s { int used; mpack_rpc_message_t msg; }; #define MPACK_RPC_SESSION_STRUCT(c) \ struct { \ mpack_tokbuf_t reader, writer; \ mpack_rpc_header_t receive, send; \ mpack_uint32_t request_id, capacity; \ struct mpack_rpc_slot_s slots[c]; \ } /* Some compilers warn against anonymous structs: * https://github.com/libmpack/libmpack/issues/6 */ typedef MPACK_RPC_SESSION_STRUCT(1) mpack_rpc_one_session_t; #define MPACK_RPC_SESSION_STRUCT_SIZE(c) \ (sizeof(struct mpack_rpc_slot_s) * (c - 1) + \ sizeof(mpack_rpc_one_session_t)) typedef MPACK_RPC_SESSION_STRUCT(MPACK_RPC_MAX_REQUESTS) mpack_rpc_session_t; MPACK_API void mpack_rpc_session_init(mpack_rpc_session_t *s, mpack_uint32_t c) FUNUSED FNONULL; MPACK_API int mpack_rpc_receive_tok(mpack_rpc_session_t *s, mpack_token_t t, mpack_rpc_message_t *msg) FUNUSED FNONULL; MPACK_API int mpack_rpc_request_tok(mpack_rpc_session_t *s, mpack_token_t *t, mpack_data_t d) FUNUSED FNONULL_ARG((1,2)); MPACK_API int mpack_rpc_reply_tok(mpack_rpc_session_t *s, mpack_token_t *t, mpack_uint32_t i) FUNUSED FNONULL; MPACK_API int mpack_rpc_notify_tok(mpack_rpc_session_t *s, mpack_token_t *t) FUNUSED FNONULL; MPACK_API int mpack_rpc_receive(mpack_rpc_session_t *s, const char **b, size_t *bl, mpack_rpc_message_t *m) FUNUSED FNONULL; MPACK_API int mpack_rpc_request(mpack_rpc_session_t *s, char **b, size_t *bl, mpack_data_t d) FUNUSED FNONULL_ARG((1,2,3)); MPACK_API int mpack_rpc_reply(mpack_rpc_session_t *s, char **b, size_t *bl, mpack_uint32_t i) FNONULL FUNUSED; MPACK_API int mpack_rpc_notify(mpack_rpc_session_t *s, char **b, size_t *bl) FNONULL FUNUSED; MPACK_API void mpack_rpc_session_copy(mpack_rpc_session_t *d, mpack_rpc_session_t *s) FUNUSED FNONULL; #endif /* MPACK_RPC_H */ libmpack-1.0.5/test/000077500000000000000000000000001306356722300142655ustar00rootroot00000000000000libmpack-1.0.5/test/deps/000077500000000000000000000000001306356722300152205ustar00rootroot00000000000000libmpack-1.0.5/test/deps/tap/000077500000000000000000000000001306356722300160045ustar00rootroot00000000000000libmpack-1.0.5/test/deps/tap/COPYING000066400000000000000000000167431306356722300170520ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. libmpack-1.0.5/test/deps/tap/readme000066400000000000000000000001031306356722300171560ustar00rootroot00000000000000Original repository for libtap: https://github.com/zorgnax/libtap libmpack-1.0.5/test/deps/tap/tap.c000066400000000000000000000204271306356722300167410ustar00rootroot00000000000000/* libtap - Write tests in C Copyright 2012 Jake Gelbman This file is licensed under the LGPL */ #define _DEFAULT_SOURCE 1 #include #include #include #include #include "tap.h" static int expected_tests = NO_PLAN; static int failed_tests; static int current_test; static char *todo_mesg; static char * vstrdupf (const char *fmt, va_list args) { char *str; int size; va_list args2; va_copy(args2, args); if (!fmt) fmt = ""; size = vsnprintf(NULL, 0, fmt, args2) + 2; str = malloc(size); if (!str) { perror("malloc error"); exit(1); } vsprintf(str, fmt, args); va_end(args2); return str; } void tap_plan (int tests, const char *fmt, ...) { expected_tests = tests; if (tests == SKIP_ALL) { char *why; va_list args; va_start(args, fmt); why = vstrdupf(fmt, args); va_end(args); printf("1..0 "); diag("SKIP %s\n", why); exit(0); } if (tests != NO_PLAN) { printf("1..%d\n", tests); } } int vok_at_loc (const char *file, int line, int test, const char *fmt, va_list args) { char *name = vstrdupf(fmt, args); if (!test) { printf("not "); } printf("ok %d", ++current_test); if (*name) printf(" - %s", name); if (todo_mesg) { printf(" # TODO"); if (*todo_mesg) printf(" %s", todo_mesg); } printf("\n"); if (!test) { printf("# Failed "); if (todo_mesg) printf("(TODO) "); printf("test "); if (*name) printf("'%s'\n# ", name); printf("at %s line %d.\n", file, line); if (!todo_mesg) failed_tests++; } free(name); return test; } int ok_at_loc (const char *file, int line, int test, const char *fmt, ...) { va_list args; va_start(args, fmt); vok_at_loc(file, line, test, fmt, args); va_end(args); return test; } static int mystrcmp (const char *a, const char *b) { return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b); } #define eq(a, b) (!mystrcmp(a, b)) #define ne(a, b) (mystrcmp(a, b)) int is_at_loc (const char *file, int line, const char *got, const char *expected, const char *fmt, ...) { int test = eq(got, expected); va_list args; va_start(args, fmt); vok_at_loc(file, line, test, fmt, args); va_end(args); if (!test) { diag(" got: '%s'", got); diag(" expected: '%s'", expected); } return test; } int isnt_at_loc (const char *file, int line, const char *got, const char *expected, const char *fmt, ...) { int test = ne(got, expected); va_list args; va_start(args, fmt); vok_at_loc(file, line, test, fmt, args); va_end(args); if (!test) { diag(" got: '%s'", got); diag(" expected: anything else"); } return test; } int cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b, const char *fmt, ...) { int test = eq(op, "||") ? a || b : eq(op, "&&") ? a && b : eq(op, "|") ? a | b : eq(op, "^") ? a ^ b : eq(op, "&") ? a & b : eq(op, "==") ? a == b : eq(op, "!=") ? a != b : eq(op, "<") ? a < b : eq(op, ">") ? a > b : eq(op, "<=") ? a <= b : eq(op, ">=") ? a >= b : eq(op, "<<") ? a << b : eq(op, ">>") ? a >> b : eq(op, "+") ? a + b : eq(op, "-") ? a - b : eq(op, "*") ? a * b : eq(op, "/") ? a / b : eq(op, "%") ? a % b : diag("unrecognized operator '%s'", op); va_list args; va_start(args, fmt); vok_at_loc(file, line, test, fmt, args); va_end(args); if (!test) { diag(" %d", a); diag(" %s", op); diag(" %d", b); } return test; } static int find_mem_diff (const char *a, const char *b, size_t n, size_t *offset) { size_t i; if (a == b) return 0; if (!a || !b) return 2; for (i = 0; i < n; i++) { if (a[i] != b[i]) { *offset = i; return 1; } } return 0; } int cmp_mem_at_loc (const char *file, int line, const void *got, const void *expected, size_t n, const char *fmt, ...) { size_t offset; int diff = find_mem_diff(got, expected, n, &offset); va_list args; va_start(args, fmt); vok_at_loc(file, line, !diff, fmt, args); va_end(args); if (diff == 1) { diag(" Difference starts at offset %d", offset); diag(" got: 0x%02x", ((unsigned char *)got)[offset]); diag(" expected: 0x%02x", ((unsigned char *)expected)[offset]); } else if (diff == 2) { diag(" got: %s", got ? "not NULL" : "NULL"); diag(" expected: %s", expected ? "not NULL" : "NULL"); } return !diff; } int diag (const char *fmt, ...) { va_list args; char *mesg, *line; int i; va_start(args, fmt); if (!fmt) return 0; mesg = vstrdupf(fmt, args); line = mesg; for (i = 0; *line; i++) { char c = mesg[i]; if (!c || c == '\n') { mesg[i] = '\0'; printf("# %s\n", line); if (!c) break; mesg[i] = c; line = mesg + i + 1; } } free(mesg); va_end(args); return 0; } int exit_status () { int retval = 0; if (expected_tests == NO_PLAN) { printf("1..%d\n", current_test); } else if (current_test != expected_tests) { diag("Looks like you planned %d test%s but ran %d.", expected_tests, expected_tests > 1 ? "s" : "", current_test); retval = 2; } if (failed_tests) { diag("Looks like you failed %d test%s of %d run.", failed_tests, failed_tests > 1 ? "s" : "", current_test); retval = 1; } return retval; } int bail_out (int ignore, const char *fmt, ...) { va_list args; va_start(args, fmt); printf("Bail out! "); vprintf(fmt, args); printf("\n"); va_end(args); exit(255); return 0; } void tap_skip (int n, const char *fmt, ...) { char *why; va_list args; va_start(args, fmt); why = vstrdupf(fmt, args); va_end(args); while (n --> 0) { printf("ok %d ", ++current_test); diag("skip %s\n", why); } free(why); } void tap_todo (int ignore, const char *fmt, ...) { va_list args; va_start(args, fmt); todo_mesg = vstrdupf(fmt, args); va_end(args); } void tap_end_todo () { free(todo_mesg); todo_mesg = NULL; } #ifndef _WIN32 #include #include #include #if defined __APPLE__ || defined BSD #define MAP_ANONYMOUS MAP_ANON #endif /* Create a shared memory int to keep track of whether a piece of code executed dies. to be used in the dies_ok and lives_ok macros. */ int tap_test_died (int status) { static int *test_died = NULL; int prev; if (!test_died) { test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); *test_died = 0; } prev = *test_died; *test_died = status; return prev; } int like_at_loc (int for_match, const char *file, int line, const char *got, const char *expected, const char *fmt, ...) { int test; regex_t re; va_list args; int err = regcomp(&re, expected, REG_EXTENDED); if (err) { char errbuf[256]; regerror(err, &re, errbuf, sizeof errbuf); fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n", expected, errbuf, file, line); exit(255); } err = regexec(&re, got, 0, NULL, 0); regfree(&re); test = for_match ? !err : err; va_start(args, fmt); vok_at_loc(file, line, test, fmt, args); va_end(args); if (!test) { if (for_match) { diag(" '%s'", got); diag(" doesn't match: '%s'", expected); } else { diag(" '%s'", got); diag(" matches: '%s'", expected); } } return test; } #endif libmpack-1.0.5/test/deps/tap/tap.h000066400000000000000000000114001306356722300167350ustar00rootroot00000000000000/* libtap - Write tests in C Copyright 2012 Jake Gelbman This file is licensed under the LGPL */ #ifndef __TAP_H__ #define __TAP_H__ #ifdef __cplusplus extern "C" { #endif #ifndef va_copy #ifdef __va_copy #define va_copy __va_copy #else #define va_copy(d, s) ((d) = (s)) #endif #endif #include #include #include int vok_at_loc (const char *file, int line, int test, const char *fmt, va_list args); int ok_at_loc (const char *file, int line, int test, const char *fmt, ...); int is_at_loc (const char *file, int line, const char *got, const char *expected, const char *fmt, ...); int isnt_at_loc (const char *file, int line, const char *got, const char *expected, const char *fmt, ...); int cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b, const char *fmt, ...); int cmp_mem_at_loc (const char *file, int line, const void *got, const void *expected, size_t n, const char *fmt, ...); int bail_out (int ignore, const char *fmt, ...); void tap_plan (int tests, const char *fmt, ...); int diag (const char *fmt, ...); int exit_status (void); void tap_skip (int n, const char *fmt, ...); void tap_todo (int ignore, const char *fmt, ...); void tap_end_todo (void); #define NO_PLAN -1 #define SKIP_ALL -2 #define ok(...) ok_at_loc(__FILE__, __LINE__, (int) __VA_ARGS__, NULL) #define is(...) is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL) #define isnt(...) isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL) #define cmp_ok(...) cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL) #define cmp_mem(...) cmp_mem_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL); #define plan(...) tap_plan(__VA_ARGS__, NULL) #define done_testing() return exit_status() #define BAIL_OUT(...) bail_out(0, "" __VA_ARGS__, NULL) #define pass(...) ok(1, "" __VA_ARGS__) #define fail(...) ok(0, "" __VA_ARGS__) #define skip(test, ...) do {if (test) {tap_skip(__VA_ARGS__, NULL); break;} #define end_skip } while (0) #define todo(...) tap_todo(0, "" __VA_ARGS__, NULL) #define end_todo tap_end_todo() #define dies_ok(...) dies_ok_common(1, __VA_ARGS__) #define lives_ok(...) dies_ok_common(0, __VA_ARGS__) #ifdef _WIN32 #define like(...) tap_skip(1, "like is not implemented on Windows") #define unlike tap_skip(1, "unlike is not implemented on Windows") #define dies_ok_common(...) \ tap_skip(1, "Death detection is not supported on Windows") #else #define like(...) like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL) #define unlike(...) like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL) int like_at_loc (int for_match, const char *file, int line, const char *got, const char *expected, const char *fmt, ...); #include #include #include int tap_test_died (int status); #define dies_ok_common(for_death, code, ...) \ do { \ int cpid; \ int it_died; \ tap_test_died(1); \ cpid = fork(); \ switch (cpid) { \ case -1: \ perror("fork error"); \ exit(1); \ case 0: \ close(1); \ close(2); \ code \ tap_test_died(0); \ exit(0); \ } \ if (waitpid(cpid, NULL, 0) < 0) { \ perror("waitpid error"); \ exit(1); \ } \ it_died = tap_test_died(0); \ if (!it_died) \ {code} \ ok(for_death ? it_died : !it_died, "" __VA_ARGS__); \ } while (0) #endif #ifdef __cplusplus } #endif #endif libmpack-1.0.5/test/fixtures.c000066400000000000000000001076761306356722300163230ustar00rootroot00000000000000#include #include #include #include "fixtures.h" #include "../src/rpc.h" static void write_size_be(uint8_t *p, size_t s) { if (s <= UINT8_MAX) { p[0] = (uint8_t)s; } else if (s <= UINT16_MAX) { p[0] = (uint8_t)((s & 0xff00) >> 8); p[1] = (uint8_t)(s & 0xff); } else { p[0] = (uint8_t)((s & 0xff000000) >> 24); p[1] = (uint8_t)((s & 0xff0000) >> 16); p[2] = (uint8_t)((s & 0xff00) >> 8); p[3] = (uint8_t)(s & 0xff); } } static char jsbuf[0xffffff]; static uint8_t mpbuf[0xffffff]; static void map_generator(char **js, uint8_t **mp, size_t *mplen, size_t s) { assert(s >= 0x10); *js = jsbuf; *mp = mpbuf; (*js)[0] = '{'; size_t jsoff = 1; size_t mpoff = 1; const char js_item_pattern[] = "\"k%05x\":[1,\"s:2\",[3,4]]"; uint8_t mp_item_pattern[] = { 0xa6, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x93, 0x01, 0xa1, 0x32, 0x92, 0x03, 0x04, }; if (s <= UINT16_MAX) { (*mp)[0] = 0xde; if (s <= 0xff) { /* put an extra byte to the left */ write_size_be(*mp + mpoff++, 0); write_size_be(*mp + mpoff++, s); } else { write_size_be(*mp + mpoff, s); mpoff += 2; } } else { (*mp)[0] = 0xdf; write_size_be(*mp + mpoff, s); mpoff += 4; } for (size_t i = 0; i < s; i++) { char b[sizeof(js_item_pattern) + 1]; snprintf(b, sizeof(b), js_item_pattern, (unsigned int)i); memcpy(*js + jsoff, b, sizeof(b) - 1); jsoff += sizeof(b) - 1; (*js)[jsoff++] = ','; memcpy(*mp + mpoff, mp_item_pattern, sizeof(mp_item_pattern)); memcpy(*mp + mpoff + 2, b + 2, 5); mpoff += sizeof(mp_item_pattern); } (*js)[--jsoff] = '}'; (*js)[++jsoff] = 0; *mplen = mpoff; } static void array_generator(char **js, uint8_t **mp, size_t *mplen, size_t s) { assert(s >= 0x10); *js = jsbuf; *mp = mpbuf; (*js)[0] = '['; size_t jsoff = 1; size_t mpoff = 1; char js_item_pattern[] = "{\"mpack\":true,\"version\":[1,0,0]}"; uint8_t mp_item_pattern[] = { 0x82, 0xa5, 0x6d, 0x70, 0x61, 0x63, 0x6b, 0xc3, 0xa7, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x93, 0x01, 0x00, 0x00}; if (s <= UINT16_MAX) { (*mp)[0] = 0xdc; if (s <= 0xff) { /* put an extra byte to the left */ write_size_be(*mp + mpoff++, 0); write_size_be(*mp + mpoff++, s); } else { write_size_be(*mp + mpoff, s); mpoff += 2; } } else { (*mp)[0] = 0xdd; write_size_be(*mp + mpoff, s); mpoff += 4; } for (size_t i = 0; i < s; i++) { memcpy(*mp + mpoff, mp_item_pattern, sizeof(mp_item_pattern)); mpoff += sizeof(mp_item_pattern); memcpy(*js + jsoff, js_item_pattern, sizeof(js_item_pattern) - 1); jsoff += sizeof(js_item_pattern) - 1; (*js)[jsoff++] = ','; } (*js)[--jsoff] = ']'; (*js)[++jsoff] = 0; *mplen = mpoff; } static void blob_pattern_generator(char **js, uint8_t **mp, size_t *mplen, size_t s, uint8_t code, char prefix) { *js = jsbuf; *mp = mpbuf; char *pattern; size_t mpoff; size_t jsoff = prefix == 'e' ? 6 : 3; assert(s + jsoff + 2 <= sizeof(jsbuf)); (*js)[0] = '"'; (*js)[1] = prefix; (*js)[2] = ':'; if (prefix == 'e') { (*js)[3] = '7'; (*js)[4] = 'f'; (*js)[5] = ':'; } if (s <= UINT8_MAX) { pattern = "blob08"; mpoff = 2; } else if (s <= UINT16_MAX) { pattern = "blob16"; mpoff = 3; code++; } else { pattern = "blob32"; mpoff = 5; code++; code++; } if (prefix == 'e') { mpoff++; } assert(s + mpoff <= sizeof(mpbuf)); (*mp)[0] = code; write_size_be(*mp + 1, s); if (prefix == 'e') { (*mp)[mpoff - 1] = 0x7f; } size_t patlen = strlen(pattern); for (size_t i = 0; i < s; i++) { (*mp)[i + mpoff] = (uint8_t )pattern[i % patlen]; (*js)[i + jsoff] = pattern[i % patlen]; } (*js)[s + jsoff] = '"'; (*js)[s + jsoff + 1] = 0; *mplen = s + mpoff; } static void bin_generator(char **js, uint8_t **mp, size_t *mplen, size_t s) { blob_pattern_generator(js, mp, mplen, s, 0xc4, 'b'); } static void str_generator(char **js, uint8_t **mp, size_t *mplen, size_t s) { assert(s > 31); blob_pattern_generator(js, mp, mplen, s, 0xd9, 's'); } static void ext_generator(char **js, uint8_t **mp, size_t *mplen, size_t s) { assert(s != 1 && s != 2 && s != 4 && s != 8 && s != 16); blob_pattern_generator(js, mp, mplen, s, 0xc7, 'e'); } const struct fixture fixtures[] = { #define F(j, ...) { \ .json = j, \ .msgpack = (uint8_t []){__VA_ARGS__}, \ .msgpacklen = (sizeof((int[]){__VA_ARGS__})/sizeof(int)), \ .generator = NULL, \ .generator_size = 0 \ } #define DF(g, s) { \ .json = NULL, \ .msgpack = NULL, \ .msgpacklen = 0, \ .generator = g, \ .generator_size = s \ } /* positive fixint */ F("0", 0x00), F("1", 0x01), F("2", 0x02), F("3", 0x03), F("4", 0x04), F("5", 0x05), F("6", 0x06), F("7", 0x07), F("8", 0x08), F("9", 0x09), F("10", 0x0a), F("11", 0x0b), F("12", 0x0c), F("13", 0x0d), F("14", 0x0e), F("15", 0x0f), F("16", 0x10), F("17", 0x11), F("18", 0x12), F("19", 0x13), F("20", 0x14), F("21", 0x15), F("22", 0x16), F("23", 0x17), F("24", 0x18), F("25", 0x19), F("26", 0x1a), F("27", 0x1b), F("28", 0x1c), F("29", 0x1d), F("30", 0x1e), F("31", 0x1f), F("32", 0x20), F("33", 0x21), F("34", 0x22), F("35", 0x23), F("36", 0x24), F("37", 0x25), F("38", 0x26), F("39", 0x27), F("40", 0x28), F("41", 0x29), F("42", 0x2a), F("43", 0x2b), F("44", 0x2c), F("45", 0x2d), F("46", 0x2e), F("47", 0x2f), F("48", 0x30), F("49", 0x31), F("50", 0x32), F("51", 0x33), F("52", 0x34), F("53", 0x35), F("54", 0x36), F("55", 0x37), F("56", 0x38), F("57", 0x39), F("58", 0x3a), F("59", 0x3b), F("60", 0x3c), F("61", 0x3d), F("62", 0x3e), F("63", 0x3f), F("64", 0x40), F("65", 0x41), F("66", 0x42), F("67", 0x43), F("68", 0x44), F("69", 0x45), F("70", 0x46), F("71", 0x47), F("72", 0x48), F("73", 0x49), F("74", 0x4a), F("75", 0x4b), F("76", 0x4c), F("77", 0x4d), F("78", 0x4e), F("79", 0x4f), F("80", 0x50), F("81", 0x51), F("82", 0x52), F("83", 0x53), F("84", 0x54), F("85", 0x55), F("86", 0x56), F("87", 0x57), F("88", 0x58), F("89", 0x59), F("90", 0x5a), F("91", 0x5b), F("92", 0x5c), F("93", 0x5d), F("94", 0x5e), F("95", 0x5f), F("96", 0x60), F("97", 0x61), F("98", 0x62), F("99", 0x63), F("100", 0x64), F("101", 0x65), F("102", 0x66), F("103", 0x67), F("104", 0x68), F("105", 0x69), F("106", 0x6a), F("107", 0x6b), F("108", 0x6c), F("109", 0x6d), F("110", 0x6e), F("111", 0x6f), F("112", 0x70), F("113", 0x71), F("114", 0x72), F("115", 0x73), F("116", 0x74), F("117", 0x75), F("118", 0x76), F("119", 0x77), F("120", 0x78), F("121", 0x79), F("122", 0x7a), F("123", 0x7b), F("124", 0x7c), F("125", 0x7d), F("126", 0x7e), F("127", 0x7f), /* fixmap */ F("{}", 0x80), F("{\"k\":\"s:val\"}", 0x81, 0xa1, 0x6b, 0xa3, 0x76, 0x61, 0x6c), F("{\"k1\":1,\"k2\":2}", 0x82, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02), F("{\"k1\":1,\"k2\":2,\"k3\":3}", 0x83, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4}", 0x84, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5}", 0x85, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6}", 0x86, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7}", 0x87, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8}", 0x88, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8,\"k9\":9}", 0x89, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08, 0xa2, 0x6b, 0x39, 0x09), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8,\"k9\":9,\"k10\":10}", 0x8a, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08, 0xa2, 0x6b, 0x39, 0x09, 0xa3, 0x6b, 0x31, 0x30, 0x0a), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8,\"k9\":9,\"k10\":10,\"k11\":11}", 0x8b, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08, 0xa2, 0x6b, 0x39, 0x09, 0xa3, 0x6b, 0x31, 0x30, 0x0a, 0xa3, 0x6b, 0x31, 0x31, 0x0b), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8,\"k9\":9,\"k10\":10,\"k11\":11,\"k12\":12}", 0x8c, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08, 0xa2, 0x6b, 0x39, 0x09, 0xa3, 0x6b, 0x31, 0x30, 0x0a, 0xa3, 0x6b, 0x31, 0x31, 0x0b, 0xa3, 0x6b, 0x31, 0x32, 0x0c), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8,\"k9\":9,\"k10\":10,\"k11\":11,\"k12\":12,\"k13\":13}", 0x8d, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08, 0xa2, 0x6b, 0x39, 0x09, 0xa3, 0x6b, 0x31, 0x30, 0x0a, 0xa3, 0x6b, 0x31, 0x31, 0x0b, 0xa3, 0x6b, 0x31, 0x32, 0x0c, 0xa3, 0x6b, 0x31, 0x33, 0x0d), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8,\"k9\":9,\"k10\":10,\"k11\":11,\"k12\":12,\"k13\":13," "\"k14\":14}", 0x8e, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08, 0xa2, 0x6b, 0x39, 0x09, 0xa3, 0x6b, 0x31, 0x30, 0x0a, 0xa3, 0x6b, 0x31, 0x31, 0x0b, 0xa3, 0x6b, 0x31, 0x32, 0x0c, 0xa3, 0x6b, 0x31, 0x33, 0x0d, 0xa3, 0x6b, 0x31, 0x34, 0x0e), F("{\"k1\":1,\"k2\":2,\"k3\":3,\"k4\":4,\"k5\":5,\"k6\":6,\"k7\":7," "\"k8\":8,\"k9\":9,\"k10\":10,\"k11\":11,\"k12\":12,\"k13\":13," "\"k14\":14,\"k15\":15}", 0x8f, 0xa2, 0x6b, 0x31, 0x01, 0xa2, 0x6b, 0x32, 0x02, 0xa2, 0x6b, 0x33, 0x03, 0xa2, 0x6b, 0x34, 0x04, 0xa2, 0x6b, 0x35, 0x05, 0xa2, 0x6b, 0x36, 0x06, 0xa2, 0x6b, 0x37, 0x07, 0xa2, 0x6b, 0x38, 0x08, 0xa2, 0x6b, 0x39, 0x09, 0xa3, 0x6b, 0x31, 0x30, 0x0a, 0xa3, 0x6b, 0x31, 0x31, 0x0b, 0xa3, 0x6b, 0x31, 0x32, 0x0c, 0xa3, 0x6b, 0x31, 0x33, 0x0d, 0xa3, 0x6b, 0x31, 0x34, 0x0e, 0xa3, 0x6b, 0x31, 0x35, 0x0f), F("{\"k1\":[1,\"s:2\",[3,4]]}", 0x81, 0xa2, 0x6b, 0x31, 0x93, 0x01, 0xa1, 0x32, 0x92, 0x03, 0x04), F("{\"k1\":[1,\"s:2\",[3,4]],\"k2\":[1,\"s:2\",[3,4]]}", 0x82, 0xa2, 0x6b, 0x31, 0x93, 0x01, 0xa1, 0x32, 0x92, 0x03, 0x04, 0xa2, 0x6b, 0x32, 0x93, 0x01, 0xa1, 0x32, 0x92, 0x03, 0x04), /* fixarray */ F("[]", 0x90), F("[1]", 0x91, 0x01), F("[1,2]", 0x92, 0x01, 0x02), F("[1,2,3]", 0x93, 0x01, 0x02, 0x03), F("[1,2,3,4]", 0x94, 0x01, 0x02, 0x03, 0x04), F("[1,2,3,4,5]", 0x95, 0x01, 0x02, 0x03, 0x04, 0x05), F("[1,2,3,4,5,6]", 0x96, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06), F("[1,2,3,4,5,6,7]", 0x97, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07), F("[1,2,3,4,5,6,7,8]", 0x98, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08), F("[1,2,3,4,5,6,7,8,9]", 0x99, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09), F("[1,2,3,4,5,6,7,8,9,10]", 0x9a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a), F("[1,2,3,4,5,6,7,8,9,10,11]", 0x9b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b), F("[1,2,3,4,5,6,7,8,9,10,11,12]", 0x9c, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c), F("[1,2,3,4,5,6,7,8,9,10,11,12,13]", 0x9d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d), F("[1,2,3,4,5,6,7,8,9,10,11,12,13,14]", 0x9e, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e), F("[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]", 0x9f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f), F("[{\"mpack\":true,\"version\":[1,0,0]}]", 0x91, 0x82, 0xa5, 0x6d, 0x70, 0x61, 0x63, 0x6b, 0xc3, 0xa7, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x93, 0x01, 0x00, 0x00), F("[{\"mpack\":true,\"version\":[1,0,0]}," "{\"mpack\":true,\"version\":[1,0,0]}]", 0x92, 0x82, 0xa5, 0x6d, 0x70, 0x61, 0x63, 0x6b, 0xc3, 0xa7, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x93, 0x01, 0x00, 0x00, 0x82, 0xa5, 0x6d, 0x70, 0x61, 0x63, 0x6b, 0xc3, 0xa7, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x93, 0x01, 0x00, 0x00), /* fixstr */ F("\"s:\"", 0xa0), F("\"s:a\"", 0xa1, 0x61), F("\"s:ab\"", 0xa2, 0x61, 0x62), F("\"s:abc\"", 0xa3, 0x61, 0x62, 0x63), F("\"s:abcd\"", 0xa4, 0x61, 0x62, 0x63, 0x64), F("\"s:abcde\"", 0xa5, 0x61, 0x62, 0x63, 0x64, 0x65), F("\"s:abcdef\"", 0xa6, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66), F("\"s:abcdefg\"", 0xa7, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67), F("\"s:abcdefgh\"", 0xa8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68), F("\"s:abcdefghi\"", 0xa9, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69), F("\"s:abcdefghij\"", 0xaa, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a), F("\"s:abcdefghijk\"", 0xab, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b), F("\"s:abcdefghijkl\"", 0xac, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c), F("\"s:abcdefghijklm\"", 0xad, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d), F("\"s:abcdefghijklmn\"", 0xae, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e), F("\"s:abcdefghijklmno\"", 0xaf, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f), F("\"s:abcdefghijklmnop\"", 0xb0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70), F("\"s:abcdefghijklmnopq\"", 0xb1, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71), F("\"s:abcdefghijklmnopqr\"", 0xb2, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72), F("\"s:abcdefghijklmnopqrs\"", 0xb3, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73), F("\"s:abcdefghijklmnopqrst\"", 0xb4, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74), F("\"s:abcdefghijklmnopqrstu\"", 0xb5, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75), F("\"s:abcdefghijklmnopqrstuv\"", 0xb6, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76), F("\"s:abcdefghijklmnopqrstuvw\"", 0xb7, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77), F("\"s:abcdefghijklmnopqrstuvwx\"", 0xb8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78), F("\"s:abcdefghijklmnopqrstuvwxy\"", 0xb9, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79), F("\"s:abcdefghijklmnopqrstuvwxyz\"", 0xba, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a), F("\"s:abcdefghijklmnopqrstuvwxyz0\"", 0xbb, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30), F("\"s:abcdefghijklmnopqrstuvwxyz01\"", 0xbc, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31), F("\"s:abcdefghijklmnopqrstuvwxyz012\"", 0xbd, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32), F("\"s:abcdefghijklmnopqrstuvwxyz0123\"", 0xbe, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33), F("\"s:abcdefghijklmnopqrstuvwxyz01234\"", 0xbf, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34), F("null", 0xc0), F("false", 0xc2), F("true", 0xc3), /* bin 8 */ DF(bin_generator, 0x00), DF(bin_generator, 0x01), DF(bin_generator, 0x7f), DF(bin_generator, 0xff), /* bin 16 */ DF(bin_generator, 0x100), DF(bin_generator, 0x101), DF(bin_generator, 0x7fff), DF(bin_generator, 0xffff), /* bin 32, skipping very large values to avoid memory starvation */ DF(bin_generator, 0x10000), DF(bin_generator, 0x10001), DF(bin_generator, 0x7ffff), DF(bin_generator, 0xfffff), /* ext 8 */ DF(ext_generator, 0x03), DF(ext_generator, 0x07), DF(ext_generator, 0x0f), /* ext 16 */ DF(ext_generator, 0x100), DF(ext_generator, 0x101), DF(ext_generator, 0x7fff), DF(ext_generator, 0xffff), /* ext 32 */ DF(ext_generator, 0x10000), DF(ext_generator, 0x10001), DF(ext_generator, 0x7ffff), DF(ext_generator, 0xfffff), /* float 32(converted to double) */ F("0.0", 0xca, 0x00, 0x00, 0x00, 0x00), F("1.0", 0xca, 0x3f, 0x80, 0x00, 0x00), F("-1.0", 0xca, 0xbf, 0x80, 0x00, 0x00), F("-79.967178344726562", 0xca, 0xc2, 0x9f, 0xef, 0x32), F("-0.00071464438224211335", 0xca, 0xba, 0x3b, 0x56, 0xf9), F("2.460578477798307e+29", 0xca, 0x70, 0x46, 0xc3, 0x92), F("1.3502847071725796e+34", 0xca, 0x78, 0x26, 0x6f, 0x79), F("-2.1088339408059653e-10", 0xca, 0xaf, 0x67, 0xde, 0x66), F("-2.7540517940439024e+30", 0xca, 0xf2, 0x0b, 0x0b, 0x49), F("2.1027235734516251e-23", 0xca, 0x19, 0xcb, 0x5c, 0xea), /* subnormal float 32 */ F("1.4012984643248171e-45", 0xca, 0x00, 0x00, 0x00, 0x01), F("2.8025969286496341e-45", 0xca, 0x00, 0x00, 0x00, 0x02), F("3.5873240686715317e-43", 0xca, 0x00, 0x00, 0x01, 0x00), F("-3.5873240686715317e-43", 0xca, 0x80, 0x00, 0x01, 0x00), /* float 64 */ F("4.2487020772743464e-149", 0xcb, 0x21, 0x21, 0x62, 0x73, 0xa0, 0xc9, 0x61, 0xe4), F("-3.64378489333011e-199", 0xcb, 0x96, 0xbb, 0xe4, 0x30, 0xd8, 0x4f, 0x48, 0x84), F("-3.7908138174749756e+148", 0xcb, 0xde, 0xc7, 0xb7, 0x9e, 0x91, 0x92, 0x99, 0xef), F("1.9423862325881795e+243", 0xcb, 0x72, 0x72, 0x34, 0xc8, 0xe2, 0xd1, 0xbc, 0x6c), F("-4.5970653956423213e-225", 0xcb, 0x91, 0x5b, 0x39, 0xc0, 0xcc, 0xaf, 0xb4, 0xa4), F("1.5856901593454632e-299", 0xcb, 0x01, 0xe5, 0x3d, 0x0e, 0xdf, 0x2f, 0x2b, 0x97), F("-2.8033297048969783e-123", 0xcb, 0xa6, 0x7d, 0xa6, 0x88, 0xd4, 0x2f, 0x74, 0xcc), F("-2.8033297048969783e-123", 0xcb, 0xa6, 0x7d, 0xa6, 0x88, 0xd4, 0x2f, 0x74, 0xcc), /* subnormal float 64 */ F("4.9406564584124654e-324", 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01), F("9.8813129168249309e-324", 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02), F("3.3951932655444357e-313", 0xcb, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00), F("-3.3951932655444357e-313", 0xcb, 0x80, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00), F("-4.9406564584124654e-324", 0xcb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01), F("-9.8813129168249309e-324", 0xcb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02), /* uint 8 */ F("128", 0xcc, 0x80), F("255", 0xcc, 0xff), /* uint 16 */ F("256", 0xcd, 0x01, 0x00), F("65535", 0xcd, 0xff, 0xff), /* uint 32 */ F("65536", 0xce, 0x00, 0x01, 0x00, 0x00), #ifndef FORCE_32BIT_INTS F("4294967295", 0xce, 0xff, 0xff, 0xff, 0xff), /* uint 64 */ F("4294967296", 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00), F("9007199254740991", 0xcf, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), F("9223372036854775807", 0xcf, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), #endif /* int 8 */ F("-128", 0xd0, 0x80), F("-127", 0xd0, 0x81), /* int 16 */ F("-32768", 0xd1, 0x80, 0x00), F("-32767", 0xd1, 0x80, 0x01), /* int 32 */ F("-2147483648", 0xd2, 0x80, 0x00, 0x00, 0x00), F("-2147483647", 0xd2, 0x80, 0x00, 0x00, 0x01), #ifndef FORCE_32BIT_INTS /* int 64 */ F("-2147483649", 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff), F("-2147483650", 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe), F("-4294967296", 0xd3, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00), F("-9007199254740991", 0xd3, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01), F("-9223372036854775808", 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), #endif /* fixext 1 */ F("\"e:79:a\"", 0xd4, 0x79, 0x61), /* fixext 2 */ F("\"e:79:ab\"", 0xd5, 0x79, 0x61, 0x62), /* fixext 4 */ F("\"e:78:abcd\"", 0xd6, 0x78, 0x61, 0x62, 0x63, 0x64), /* fixext 8 */ F("\"e:77:abcdefgh\"", 0xd7, 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68), /* fixext 16 */ F("\"e:76:abcdefghijklmnop\"", 0xd8, 0x76, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70), /* str 8 */ DF(str_generator, 0x20), DF(str_generator, 0x7f), DF(str_generator, 0xff), /* str 16 */ DF(str_generator, 0x100), DF(str_generator, 0x101), DF(str_generator, 0x7fff), DF(str_generator, 0xffff), /* str 32 */ DF(str_generator, 0x10000), DF(str_generator, 0x10001), DF(str_generator, 0x7ffff), DF(str_generator, 0xfffff), /* array 16 */ DF(array_generator, 0x10), DF(array_generator, 0xff), DF(array_generator, 0x100), /* array 32 */ DF(array_generator, 0x10000), /* map 16 */ DF(map_generator, 0x10), DF(map_generator, 0xff), DF(map_generator, 0x100), /* map 32 */ DF(map_generator, 0x10000), /* negative fixint */ F("-32", 0xe0), F("-31", 0xe1), F("-30", 0xe2), F("-29", 0xe3), F("-28", 0xe4), F("-27", 0xe5), F("-26", 0xe6), F("-25", 0xe7), F("-24", 0xe8), F("-23", 0xe9), F("-22", 0xea), F("-21", 0xeb), F("-20", 0xec), F("-19", 0xed), F("-18", 0xee), F("-17", 0xef), F("-16", 0xf0), F("-15", 0xf1), F("-14", 0xf2), F("-13", 0xf3), F("-12", 0xf4), F("-11", 0xf5), F("-10", 0xf6), F("-9", 0xf7), F("-8", 0xf8), F("-7", 0xf9), F("-6", 0xfa), F("-5", 0xfb), F("-4", 0xfc), F("-3", 0xfd), F("-2", 0xfe), F("-1", 0xff), }; const int fixture_count = ARRAY_SIZE(fixtures); const struct fixture number_fixtures[] = { F("0", 0x00), F("1", 0x01), F("2", 0x02), F("3", 0x03), F("4", 0x04), F("5", 0x05), F("6", 0x06), F("7", 0x07), F("8", 0x08), F("9", 0x09), F("10", 0x0a), F("11", 0x0b), F("12", 0x0c), F("13", 0x0d), F("14", 0x0e), F("15", 0x0f), F("16", 0x10), F("17", 0x11), F("18", 0x12), F("19", 0x13), F("20", 0x14), F("21", 0x15), F("22", 0x16), F("23", 0x17), F("24", 0x18), F("25", 0x19), F("26", 0x1a), F("27", 0x1b), F("28", 0x1c), F("29", 0x1d), F("30", 0x1e), F("31", 0x1f), F("32", 0x20), F("33", 0x21), F("34", 0x22), F("35", 0x23), F("36", 0x24), F("37", 0x25), F("38", 0x26), F("39", 0x27), F("40", 0x28), F("41", 0x29), F("42", 0x2a), F("43", 0x2b), F("44", 0x2c), F("45", 0x2d), F("46", 0x2e), F("47", 0x2f), F("48", 0x30), F("49", 0x31), F("50", 0x32), F("51", 0x33), F("52", 0x34), F("53", 0x35), F("54", 0x36), F("55", 0x37), F("56", 0x38), F("57", 0x39), F("58", 0x3a), F("59", 0x3b), F("60", 0x3c), F("61", 0x3d), F("62", 0x3e), F("63", 0x3f), F("64", 0x40), F("65", 0x41), F("66", 0x42), F("67", 0x43), F("68", 0x44), F("69", 0x45), F("70", 0x46), F("71", 0x47), F("72", 0x48), F("73", 0x49), F("74", 0x4a), F("75", 0x4b), F("76", 0x4c), F("77", 0x4d), F("78", 0x4e), F("79", 0x4f), F("80", 0x50), F("81", 0x51), F("82", 0x52), F("83", 0x53), F("84", 0x54), F("85", 0x55), F("86", 0x56), F("87", 0x57), F("88", 0x58), F("89", 0x59), F("90", 0x5a), F("91", 0x5b), F("92", 0x5c), F("93", 0x5d), F("94", 0x5e), F("95", 0x5f), F("96", 0x60), F("97", 0x61), F("98", 0x62), F("99", 0x63), F("100", 0x64), F("101", 0x65), F("102", 0x66), F("103", 0x67), F("104", 0x68), F("105", 0x69), F("106", 0x6a), F("107", 0x6b), F("108", 0x6c), F("109", 0x6d), F("110", 0x6e), F("111", 0x6f), F("112", 0x70), F("113", 0x71), F("114", 0x72), F("115", 0x73), F("116", 0x74), F("117", 0x75), F("118", 0x76), F("119", 0x77), F("120", 0x78), F("121", 0x79), F("122", 0x7a), F("123", 0x7b), F("124", 0x7c), F("125", 0x7d), F("126", 0x7e), F("127", 0x7f), F("-79.967178344726562", 0xca, 0xc2, 0x9f, 0xef, 0x32), F("-0.00071464438224211335", 0xca, 0xba, 0x3b, 0x56, 0xf9), F("128", 0xcc, 0x80), F("255", 0xcc, 0xff), F("256", 0xcd, 0x01, 0x00), F("65535", 0xcd, 0xff, 0xff), F("65536", 0xce, 0x00, 0x01, 0x00, 0x00), F("4294967295", 0xce, 0xff, 0xff, 0xff, 0xff), F("4294967296", 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00), F("9007199254740991", 0xcf, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), F("-128", 0xd0, 0x80), F("-127", 0xd0, 0x81), F("-32768", 0xd1, 0x80, 0x00), F("-32767", 0xd1, 0x80, 0x01), F("-2147483648", 0xd2, 0x80, 0x00, 0x00, 0x00), F("-2147483647", 0xd2, 0x80, 0x00, 0x00, 0x01), F("-2147483649", 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff), F("-2147483650", 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe), F("-4294967296", 0xd3, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00), F("-9007199254740991", 0xd3, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01), F("-32", 0xe0), F("-31", 0xe1), F("-30", 0xe2), F("-29", 0xe3), F("-28", 0xe4), F("-27", 0xe5), F("-26", 0xe6), F("-25", 0xe7), F("-24", 0xe8), F("-23", 0xe9), F("-22", 0xea), F("-21", 0xeb), F("-20", 0xec), F("-19", 0xed), F("-18", 0xee), F("-17", 0xef), F("-16", 0xf0), F("-15", 0xf1), F("-14", 0xf2), F("-13", 0xf3), F("-12", 0xf4), F("-11", 0xf5), F("-10", 0xf6), F("-9", 0xf7), F("-8", 0xf8), F("-7", 0xf9), F("-6", 0xfa), F("-5", 0xfb), F("-4", 0xfc), F("-3", 0xfd), F("-2", 0xfe), F("-1", 0xff), }; const int number_fixture_count = ARRAY_SIZE(number_fixtures); const struct rpc_fixture rpc_fixtures[] = { #define REQ(p, i, m, a) { \ .payload = p, \ .type = MPACK_RPC_REQUEST, \ .id = i, \ .method = m, \ .args = a, \ .error = NULL, \ .result = NULL \ } #define RES(p, i, e, r) { \ .payload = p, \ .type = MPACK_RPC_RESPONSE, \ .id = i, \ .method = NULL, \ .args = NULL, \ .error = e, \ .result = r \ } #define NOT(p, m, a) { \ .payload = p, \ .type = MPACK_RPC_NOTIFICATION, \ .method = m, \ .args = a, \ .error = NULL, \ .result = NULL \ } #define ERR(p, t) { \ .payload = p, \ .type = t, \ .method = NULL, \ .args = NULL, \ .error = NULL, \ .result = NULL \ } #define S(...) SC(0, __VA_ARGS__) #define SC(c, ...) { \ .capacity = c, \ .messages = (struct rpc_message[]){__VA_ARGS__}, \ .count = \ (sizeof((struct rpc_message[]){__VA_ARGS__})/sizeof(struct rpc_message)) \ } S( REQ("->[0, 0, \"add\", [1, 2]]", 0, "\"add\"", "[1, 2]"), RES("<-[1, 0, null, 3]", 0, "null", "3"), REQ("->[0, 1, \"sub\", [5, 4]]", 1, "\"sub\"", "[5, 4]"), RES("<-[1, 1, null, 1]", 1, "null", "1"), ), S( REQ("<-[0, 0, \"add\", [1, 2]]", 0, "\"add\"", "[1, 2]"), REQ("<-[0, 1, \"sub\", [5, 4]]", 1, "\"sub\"", "[5, 4]"), RES("->[1, 0, null, 3]", 0, "null", "3"), RES("->[1, 1, null, 1]", 1, "null", "1"), ), S( REQ("<-[0, 0, \"add\", [1, 2]]", 0, "\"add\"", "[1, 2]"), NOT("<-[2, \"event\", [\"arg\"]]", "\"event\"", "[\"arg\"]"), REQ("<-[0, 1, \"sub\", [5, 4]]", 1, "\"sub\"", "[5, 4]"), RES("->[1, 1, null, 1]", 1, "null", "1"), NOT("<-[2, \"event\", [\"arg\"]]", "\"event\"", "[\"arg\"]"), RES("->[1, 0, null, 3]", 0, "null", "3"), NOT("<-[2, \"event\", [\"arg\"]]", "\"event\"", "[\"arg\"]"), ), /* validation errors */ S(ERR("<-1", MPACK_RPC_EARRAY)), S(ERR("<-[0, 0, \"add\", [1, 2], 3]", MPACK_RPC_EARRAYL)), S(ERR("<-[0, 0, \"add\"]", MPACK_RPC_EARRAYL)), S(ERR("<-[1, 0, \"add\", [1, 2], 3]", MPACK_RPC_EARRAYL)), S(ERR("<-[1, 0, \"add\"]", MPACK_RPC_EARRAYL)), S(ERR("<-[2, 0, \"add\", [1, 2]]", MPACK_RPC_EARRAYL)), S(ERR("<-[2, 0]", MPACK_RPC_EARRAYL)), S(ERR("<-[3, 0, 0]", MPACK_RPC_ETYPE)), S(ERR("<-[-1, 0, 0]", MPACK_RPC_ETYPE)), S(ERR("<-[0, 5.323, 0, 0]", MPACK_RPC_EMSGID)), S(ERR("<-[1, 5.323, 0, 0]", MPACK_RPC_EMSGID)), S(ERR("<-[1, 4294967296, 0, 0]", MPACK_RPC_EMSGID)), S(ERR("<-[1, 0, 0, 0]", MPACK_RPC_ERESPID)), S(ERR("<-[1, 4294967295, 0, 0]", MPACK_RPC_ERESPID)), SC(2, REQ("->[0, 0, \"m\", []]", 0, "\"m\"", "[]"), REQ("->[0, 1, \"m\", []]", 1, "\"m\"", "[]"), ERR("->[0, 2, \"m\", []]", MPACK_NOMEM), ), /* hash table tests */ SC(2, REQ("->[0, 0, \"add\", [1, 2]]", 0, "\"add\"", "[1, 2]"), REQ("->[0, 1, \"sub\", [1, 2]]", 1, "\"sub\"", "[1, 2]"), RES("<-[1, 1, null, -1]", 1, "null", "-1"), RES("<-[1, 0, null, 3]", 0, "null", "3"), ), SC(3, REQ("->[0, 0, \"m\", []]", 0, "\"m\"", "[]"), RES("<-[1, 0, null, null]", 0, "null", "null"), REQ("->[0, 1, \"m\", []]", 1, "\"m\"", "[]"), REQ("->[0, 2, \"m\", []]", 2, "\"m\"", "[]"), RES("<-[1, 2, null, null]", 2, "null", "null"), REQ("->[0, 3, \"m\", []]", 3, "\"m\"", "[]"), RES("<-[1, 3, null, null]", 3, "null", "null"), REQ("->[0, 4, \"m\", []]", 4, "\"m\"", "[]"), REQ("->[0, 5, \"m\", []]", 5, "\"m\"", "[]"), RES("<-[1, 5, null, null]", 5, "null", "null"), ), SC(5, REQ("->[0, 0, \"m\", []]", 0, "\"m\"", "[]"), RES("<-[1, 0, null, null]", 0, "null", "null"), REQ("->[0, 1, \"m\", []]", 1, "\"m\"", "[]"), RES("<-[1, 1, null, null]", 1, "null", "null"), REQ("->[0, 2, \"m\", []]", 2, "\"m\"", "[]"), REQ("->[0, 3, \"m\", []]", 3, "\"m\"", "[]"), RES("<-[1, 3, null, null]", 3, "null", "null"), REQ("->[0, 4, \"m\", []]", 4, "\"m\"", "[]"), RES("<-[1, 4, null, null]", 4, "null", "null"), REQ("->[0, 5, \"m\", []]", 5, "\"m\"", "[]"), RES("<-[1, 5, null, null]", 5, "null", "null"), REQ("->[0, 6, \"m\", []]", 6, "\"m\"", "[]"), RES("<-[1, 6, null, null]", 6, "null", "null"), REQ("->[0, 7, \"m\", []]", 7, "\"m\"", "[]"), REQ("->[0, 8, \"m\", []]", 8, "\"m\"", "[]"), REQ("->[0, 9, \"m\", []]", 9, "\"m\"", "[]"), RES("<-[1, 2, null, null]", 2, "null", "null"), RES("<-[1, 7, null, null]", 7, "null", "null"), RES("<-[1, 9, null, null]", 9, "null", "null"), RES("<-[1, 8, null, null]", 8, "null", "null"), REQ("->[0, 10, \"m\", []]", 10, "\"m\"", "[]"), RES("<-[1, 10, null, null]", 10, "null", "null"), REQ("->[0, 11, \"m\", []]", 11, "\"m\"", "[]"), REQ("->[0, 12, \"m\", []]", 12, "\"m\"", "[]"), RES("<-[1, 12, null, null]", 12, "null", "null"), REQ("->[0, 13, \"m\", []]", 13, "\"m\"", "[]"), RES("<-[1, 13, null, null]", 13, "null", "null"), REQ("->[0, 14, \"m\", []]", 14, "\"m\"", "[]"), REQ("->[0, 15, \"m\", []]", 15, "\"m\"", "[]"), REQ("->[0, 16, \"m\", []]", 16, "\"m\"", "[]"), REQ("->[0, 17, \"m\", []]", 17, "\"m\"", "[]"), RES("<-[1, 15, null, null]", 15, "null", "null"), RES("<-[1, 17, null, null]", 17, "null", "null"), RES("<-[1, 14, null, null]", 14, "null", "null"), RES("<-[1, 11, null, null]", 11, "null", "null"), ), }; const int rpc_fixture_count = ARRAY_SIZE(rpc_fixtures); libmpack-1.0.5/test/fixtures.h000066400000000000000000000020011306356722300163000ustar00rootroot00000000000000#ifndef MPACK_TEST_FIXTURES_H #define MPACK_TEST_FIXTURES_H #include #include #ifndef MIN # define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) #endif #define ARRAY_SIZE(arr) \ ((sizeof(arr)/sizeof((arr)[0])) / \ ((size_t)(!(sizeof(arr) % sizeof((arr)[0]))))) struct fixture { char *json; uint8_t *msgpack; size_t msgpacklen; void(*generator)(char **json, uint8_t **msgpack, size_t *msgpacklen, size_t size); size_t generator_size; }; struct rpc_message { char *payload; int type; uint32_t id; char *method; char *args; char *error; char *result; }; struct rpc_fixture { struct rpc_message *messages; size_t count; size_t capacity; }; extern const int fixture_count; extern const struct fixture fixtures[]; extern const int number_fixture_count; extern const struct fixture number_fixtures[]; extern const int rpc_fixture_count; extern const struct rpc_fixture rpc_fixtures[]; #endif /* MPACK_TEST_FIXTURES_H */ libmpack-1.0.5/test/mpack.c000066400000000000000000000533241306356722300155330ustar00rootroot00000000000000#include #include #include #include #include #include #include #include #include #include #include #include "fixtures.h" #include "tap.h" #ifdef FORCE_32BIT_INTS #define UFORMAT PRIu32 #define SFORMAT PRId32 #undef ULLONG_MAX #undef UINT64_MAX #else #define UFORMAT PRIu64 #define SFORMAT PRId64 #endif #ifdef TEST_AMALGAMATION # define MPACK_API static # include "../build/mpack.c" #else # include "../build/mpack.h" #endif static char buf[0xffffff]; static size_t bufpos; static bool number_conv = false; static bool throw = false; static mpack_data_t d(void *p) { mpack_data_t rv; rv.p = p; return rv; } static void w(const char *fmt, ...) { va_list ap; va_start(ap, fmt); bufpos += (size_t)vsnprintf(buf + bufpos, sizeof(buf) - bufpos, fmt, ap); va_end(ap); } static uint32_t item_count(const char *s) { size_t depth = 1; uint32_t count = 0; while (depth) { int c = *s++; if (c == ' ') continue; else if (c == ']' || c == '}') depth--; else if (c == '[' || c == '{') depth++; else if (depth == 1 && c == ',') count++; else if (count == 0) count = 1; } return count; } static void unparse_enter(mpack_parser_t *parser, mpack_node_t *node) { mpack_node_t *parent = MPACK_PARENT_NODE(node); char *p = parent ? parent->data[0].p : parser->data.p; if (parent && parent->tok.type > MPACK_TOKEN_MAP) { node->tok = mpack_pack_chunk(p, parent->tok.length); p += parent->tok.length; goto end; } while (*p == ' ' || *p == ',' || *p == ':') { p++; } switch (*p) { case 'n': node->tok = mpack_pack_nil(); p += 4; break; case 'f': node->tok = mpack_pack_boolean(false); p += 5; break; case 't': node->tok = mpack_pack_boolean(true); p += 4; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': case '+': case '-': { if (throw) { MPACK_THROW(parser); } char *p2 = p; double d = strtod(p, &p2); size_t l = (size_t)(p2 - p); char tmp[256]; memcpy(tmp, p, l); tmp[l] = 0; p = p2; if (number_conv) { node->tok = mpack_pack_number(d); } else { if (strchr(tmp, '.') || strchr(tmp, 'e')) { node->tok = mpack_pack_float_fast(d); { /* test both pack_float implementations */ mpack_token_t tok = mpack_pack_float_compat(d); (void)(tok); assert(node->tok.data.value.lo == tok.data.value.lo && node->tok.data.value.hi == tok.data.value.hi); } } else { node->tok = mpack_pack_sint((mpack_sintmax_t)strtoll(tmp, NULL, 10)); } } break; } case '"': { p++; char *s2 = strchr(p, '"'); mpack_uint32_t len = (mpack_uint32_t)(s2 - p); switch (*p) { case 's': node->tok = mpack_pack_str(len - 2); p += 2; break; case 'b': node->tok = mpack_pack_bin(len - 2); p += 2; break; case 'e': node->tok = mpack_pack_ext((int)strtol(p + 2, NULL, 16), len - 5); p += 5; break; default: node->tok = mpack_pack_str(len); break; } break; } case '[': p++; node->tok = mpack_pack_array(item_count(p)); break; case '{': p++; node->tok = mpack_pack_map(item_count(p)); break; } end: node->data[0].p = p; if (parent) parent->data[0].p = p; } static void unparse_exit(mpack_parser_t *parser, mpack_node_t *node) { (void)(parser); mpack_node_t *parent = MPACK_PARENT_NODE(node); char *p = node->data[0].p; switch (node->tok.type) { case MPACK_TOKEN_BIN: case MPACK_TOKEN_STR: case MPACK_TOKEN_EXT: case MPACK_TOKEN_ARRAY: case MPACK_TOKEN_MAP: p++; default: break; } node->data[0].p = p; if (parent) parent->data[0].p = p; } static void parse_enter(mpack_parser_t *parser, mpack_node_t *node) { (void)(parser); mpack_node_t *parent = MPACK_PARENT_NODE(node); mpack_token_t *t = &node->tok; mpack_token_t *p = parent ? &parent->tok : NULL; switch (t->type) { case MPACK_TOKEN_NIL: w("null"); break; case MPACK_TOKEN_BOOLEAN: w(mpack_unpack_boolean(*t) ? "true" : "false"); break; case MPACK_TOKEN_UINT: if (throw) MPACK_THROW(parser); if (number_conv) goto nconv; w("%" UFORMAT, mpack_unpack_uint(*t)); break; case MPACK_TOKEN_SINT: if (number_conv) goto nconv; w("%" SFORMAT, mpack_unpack_sint(*t)); break; case MPACK_TOKEN_FLOAT: { if (number_conv) goto nconv; /* test both unpack_float implementations */ double d = mpack_unpack_float_fast(*t),d2 = mpack_unpack_float_compat(*t); (void)(d2); assert(d == d2); w("%.*g", 17, d); if (round(d) == d && (fabs(d) < 10e3)) { /* Need a trailing .0 to be parsed as float in the packer tests */ w(".0"); } break; } case MPACK_TOKEN_CHUNK: w("%.*s", t->length, t->data.chunk_ptr); break; case MPACK_TOKEN_BIN: case MPACK_TOKEN_STR: case MPACK_TOKEN_EXT: w("\""); if (!p || p->type != MPACK_TOKEN_MAP || parent->key_visited) { if (t->type == MPACK_TOKEN_EXT) { w("e:%02x:", t->data.ext_type); } else { w(t->type == MPACK_TOKEN_BIN ? "b:" : "s:"); } } break; case MPACK_TOKEN_ARRAY: w("["); break; case MPACK_TOKEN_MAP: w("{"); break; } return; double d; nconv: d = mpack_unpack_number(*t); w("%.*g", 17, d); } static void parse_exit(mpack_parser_t *parser, mpack_node_t *node) { (void)(parser); mpack_node_t *parent = MPACK_PARENT_NODE(node); mpack_token_t *t = &node->tok; mpack_token_t *p = parent ? &parent->tok : NULL; switch (t->type) { case MPACK_TOKEN_BIN: case MPACK_TOKEN_STR: case MPACK_TOKEN_EXT: w("\""); break; case MPACK_TOKEN_ARRAY: w("]"); break; case MPACK_TOKEN_MAP: w("}"); break; default: break; } if (p && p->type < MPACK_TOKEN_BIN && parent->pos < p->length) { w(p->type == MPACK_TOKEN_MAP ? (parent->key_visited ? ":" : ",") : ","); } } /* Each unpack/pack test is executed multiple times, with each feeding data in * chunks of different sizes. */ static const size_t chunksizes[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, SIZE_MAX}; static void fixture_test(const struct fixture *ff, int fixture_idx) { const struct fixture *f = ff + fixture_idx; char *fjson; uint8_t *fmsgpack; size_t fmsgpacklen; if (f->generator) { f->generator(&fjson, &fmsgpack, &fmsgpacklen, f->generator_size); } else { fjson = f->json; fmsgpack = f->msgpack; fmsgpacklen = f->msgpacklen; } char repr[32]; snprintf(repr, sizeof(repr), "%s", fjson); for (size_t i = 0; i < ARRAY_SIZE(chunksizes); i++) { mpack_parser_t parser; size_t cs = chunksizes[i]; char *b; size_t bl; int s; /* unpack test */ bufpos = 0; mpack_parser_init(&parser, 0); b = (char *)fmsgpack; bl = cs; do { s = mpack_parse(&parser, (const char **)&b, &bl, parse_enter, parse_exit); if (s) { assert(s == MPACK_EOF); bl = cs; } } while (s); is(buf, fjson, cs == SIZE_MAX ? "unpack '%s' in a single step" : "unpack '%s' in steps of %zu", repr, cs); /* pack test */ mpack_parser_init(&parser, 0); b = buf; bl = MIN(cs, sizeof(buf)); parser.data.p = fjson; do { s = mpack_unparse(&parser, &b, &bl, unparse_enter, unparse_exit); if (s) { assert(s == MPACK_EOF); bl = cs; } } while (s); cmp_mem(buf, fmsgpack, fmsgpacklen, cs == SIZE_MAX ? "pack '%s' in a single step" : "pack '%s' in steps of %zu", repr, cs); } } static void signed_positive_packs_with_unsigned_format(void) { mpack_token_t tokbuf[0xff]; size_t tokbufpos = 0; char mpackbuf[256]; char *buf = mpackbuf; size_t buflen = sizeof(mpackbuf); mpack_tokbuf_t writer = MPACK_TOKBUF_INITIAL_VALUE; tokbuf[tokbufpos++] = mpack_pack_sint(0); tokbuf[tokbufpos++] = mpack_pack_sint(1); tokbuf[tokbufpos++] = mpack_pack_sint(0x7f); tokbuf[tokbufpos++] = mpack_pack_sint(0xff); tokbuf[tokbufpos++] = mpack_pack_sint(0xffff); #ifndef FORCE_32BIT_INTS tokbuf[tokbufpos++] = mpack_pack_sint(0xffffffff); tokbuf[tokbufpos++] = mpack_pack_sint(0x7fffffffffffffff); #endif for (size_t i = 0; i < tokbufpos; i++) mpack_write(&writer, &buf, &buflen, tokbuf + i); uint8_t expected[] = { 0x00, 0x01, 0x7f, 0xcc, 0xff, 0xcd, 0xff, 0xff, #ifndef FORCE_32BIT_INTS 0xce, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff #endif }; cmp_mem(mpackbuf, expected, sizeof(mpackbuf) - buflen, "signed positive packs with unsigned format"); } static void positive_signed_format_unpacks_as_unsigned(void) { mpack_tokbuf_t reader; mpack_token_t toks[4]; const uint8_t input[] = { 0xd0, 0x7f, 0xd1, 0x7f, 0xff, 0xd2, 0x7f, 0xff, 0xff, 0xff, #ifndef FORCE_32BIT_INTS 0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff #endif }; const char *inp = (const char *)input; size_t inplen = sizeof(input); mpack_tokbuf_init(&reader); for (size_t i = 0; i < ARRAY_SIZE(toks) && inplen; i++) mpack_read(&reader, &inp, &inplen, toks + i); mpack_uintmax_t expected[] = { 0x7f, 0x7fff, 0x7fffffff, #ifndef FORCE_32BIT_INTS 0x7fffffffffffffff #endif }; mpack_token_type_t expected_types[] = { MPACK_TOKEN_UINT, MPACK_TOKEN_UINT, MPACK_TOKEN_UINT, #ifndef FORCE_32BIT_INTS MPACK_TOKEN_UINT, #endif }; mpack_uintmax_t actual[] = { mpack_unpack_uint(toks[0]), mpack_unpack_uint(toks[1]), mpack_unpack_uint(toks[2]), #ifndef FORCE_32BIT_INTS mpack_unpack_uint(toks[3]) #endif }; mpack_token_type_t actual_types[] = { toks[0].type, toks[1].type, toks[2].type, #ifndef FORCE_32BIT_INTS toks[3].type #endif }; cmp_mem(expected, actual, sizeof(expected), "positive signed format unpacks as unsigned"); cmp_mem(expected_types, actual_types, sizeof(expected_types), "positive signed format unpacks as unsigned(tokens)"); } static void unpacking_c1_returns_eread(void) { const uint8_t input[] = {0xc1}; const char *inp = (const char *)input; size_t inplen = sizeof(input); mpack_parser_t parser; mpack_parser_init(&parser, 0); int res = mpack_parse(&parser, &inp, &inplen, parse_enter, parse_exit); ok(res == MPACK_ERROR, "0xc1 returns MPACK_ERROR"); } static void parsing_very_deep_objects_returns_enomem(void) { bufpos = 0; MPACK_PARSER_STRUCT(2) p2; MPACK_PARSER_STRUCT(3) p3; mpack_parser_init((mpack_parser_t *)&p2, 2); mpack_parser_init((mpack_parser_t *)&p3, 3); const uint8_t input[] = {0x91, 0x91, 0x01}; /* [[1]] */ const char *b = (const char *)input; size_t bl = sizeof(input); ok(mpack_parse((mpack_parser_t *)&p2, &b, &bl, parse_enter, parse_exit) == MPACK_NOMEM && b == (char *)input + 2 && bl == 1, "parsing very deep objects return MPACK_ENOMEM"); mpack_parser_copy((mpack_parser_t *)&p3, (mpack_parser_t *)&p2); ok(mpack_parse((mpack_parser_t *)&p3, &b, &bl, parse_enter, parse_exit) == MPACK_OK && b == (char *)input + 3 && bl == 0); is(buf, "[[1]]"); } static void unparsing_very_deep_objects_returns_enomem(void) { MPACK_PARSER_STRUCT(2) p2; MPACK_PARSER_STRUCT(3) p3; mpack_parser_init((mpack_parser_t *)&p2, 2); mpack_parser_init((mpack_parser_t *)&p3, 3); char input[] = "[[1]]"; uint8_t buf[16]; char *b = (char *)buf; size_t bl = sizeof(buf); p2.data.p = input; ok(mpack_unparse((mpack_parser_t *)&p2, &b, &bl, unparse_enter, unparse_exit) == MPACK_NOMEM && b == (char *)buf + 2 && bl == 14, "unparsing very deep objects return MPACK_ENOMEM"); const uint8_t e2[] = {0x91, 0x91}; cmp_mem(e2, buf, 2); mpack_parser_copy((mpack_parser_t *)&p3, (mpack_parser_t *)&p2); ok(mpack_unparse((mpack_parser_t *)&p3, &b, &bl, unparse_enter, unparse_exit) == MPACK_OK && b == (char *)buf + 3 && bl == 13); const uint8_t e3[] = {0x91, 0x91, 0x01}; cmp_mem(e3, buf, 3); } static void parse_throw(void) { bufpos = 0; mpack_parser_t parser; mpack_parser_init(&parser, 0); const uint8_t input[] = {0x91, 0x91, 0x01}; /* [[1]] */ const char *b = (const char *)input; size_t bl = sizeof(input); ok(mpack_parse((mpack_parser_t *)&parser, &b, &bl, parse_enter, parse_exit) == MPACK_OK); b = (const char *)input; bl = sizeof(input); throw = true; ok(mpack_parse((mpack_parser_t *)&parser, &b, &bl, parse_enter, parse_exit) == MPACK_EXCEPTION); b = (const char *)input; bl = sizeof(input); throw = false; ok(mpack_parse((mpack_parser_t *)&parser, &b, &bl, parse_enter, parse_exit) == MPACK_EXCEPTION, "throw will invalidate the parser"); } static void unparse_throw(void) { mpack_parser_t parser; mpack_parser_init(&parser, 0); char input[] = "[[1]]"; uint8_t buf[16]; char *b = (char *)buf; size_t bl = sizeof(buf); parser.data.p = input; ok(mpack_unparse((mpack_parser_t *)&parser, &b, &bl, unparse_enter, unparse_exit) == MPACK_OK); b = (char *)buf; bl = sizeof(buf); throw = true; ok(mpack_unparse((mpack_parser_t *)&parser, &b, &bl, unparse_enter, unparse_exit) == MPACK_EXCEPTION); b = (char *)buf; bl = sizeof(buf); throw = false; ok(mpack_unparse((mpack_parser_t *)&parser, &b, &bl, unparse_enter, unparse_exit) == MPACK_EXCEPTION, "throw will invalidate the parser"); } static void does_not_write_invalid_tokens(void) { mpack_tokbuf_t writer; mpack_tokbuf_init(&writer); mpack_token_t tok, tok2; tok.type = 9999; char buf[64], *ptr = buf; size_t ptrlen = sizeof(buf); tok2 = mpack_pack_float_compat(5.5); tok2.length = 5; ok(mpack_write(&writer, &ptr, &ptrlen, &tok) == MPACK_ERROR, "does not write invalid tokens 1"); mpack_tokbuf_init(&writer); ok(mpack_write(&writer, &ptr, &ptrlen, &tok2) == MPACK_ERROR, "does not write invalid tokens 2"); } #define MSGPACK_BUFLEN 0xff static void to_msgpack(const char *json, uint8_t **buf) { size_t buflen = MSGPACK_BUFLEN; mpack_parser_t parser; mpack_parser_init(&parser, 0); parser.data.p = (char *)json; if (mpack_unparse(&parser, (char **)buf, &buflen, unparse_enter, unparse_exit) != MPACK_OK) abort(); } static void rpc_copy_session_maintains_state(void) { int d1, d2, d3; mpack_rpc_session_t *s2 = malloc(sizeof(MPACK_RPC_SESSION_STRUCT(2))); mpack_rpc_session_t *s3 = malloc(sizeof(MPACK_RPC_SESSION_STRUCT(3))); mpack_rpc_session_init(s2, 2); mpack_rpc_session_init(s3, 3); mpack_token_t tok; while (mpack_rpc_request_tok(s2, &tok, d(&d1)) != MPACK_OK); while (mpack_rpc_request_tok(s2, &tok, d(&d2)) != MPACK_OK); ok(mpack_rpc_request_tok(s2, &tok, d(NULL)) == MPACK_NOMEM); mpack_rpc_session_copy(s3, s2); while (mpack_rpc_request_tok(s3, &tok, d(&d3)) != MPACK_OK); size_t bl = 0xff; uint8_t buf[0xff]; const char *b = (const char *)buf; mpack_rpc_message_t msg; to_msgpack("[1, 0, null, null]", (uint8_t **)&b); b = (const char *)buf; ok(mpack_rpc_receive(s3, &b, &bl, &msg) == MPACK_RPC_RESPONSE && msg.data.p == &d1); b = (const char *)buf; to_msgpack("[1, 1, null, null]", (uint8_t **)&b); b = (const char *)buf; ok(mpack_rpc_receive(s3, &b, &bl, &msg) == MPACK_RPC_RESPONSE && msg.data.p == &d2); b = (const char *)buf; to_msgpack("[1, 2, null, null]", (uint8_t **)&b); b = (const char *)buf; ok(mpack_rpc_receive(s3, &b, &bl, &msg) == MPACK_RPC_RESPONSE); free(s2); free(s3); } static void rpc_request_id_wrap(void) { mpack_rpc_session_t session; mpack_rpc_session_init(&session, 0); mpack_token_t tok; /* produce request 0 */ while (mpack_rpc_request_tok(&session, &tok, d(NULL)) != MPACK_OK); ok(session.slots[0].used && session.slots[0].msg.id == 0); /* jump request id to 0xffffffff */ session.request_id = 0xffffffff; while (mpack_rpc_request_tok(&session, &tok, d(NULL)) != MPACK_OK); ok(session.slots[31].used && session.slots[31].msg.id == 0xffffffff); /* wrap back to 0 which is taken, resulting in 1 being used instead */ while (mpack_rpc_request_tok(&session, &tok, d(NULL)) != MPACK_OK); ok(session.slots[1].used && session.slots[1].msg.id == 1); } static int reqdata; static void rpc_check_outgoing(mpack_rpc_session_t *session, struct rpc_message *m, size_t cs, bool invert) { uint8_t buf[MSGPACK_BUFLEN]; uint8_t *b = buf; uint8_t rbuf[MSGPACK_BUFLEN]; char *ptr = (char *)rbuf; to_msgpack(m->payload + 2, &b); int status = MPACK_EOF; while (status == MPACK_EOF) { size_t bl = cs; if (m->type == MPACK_RPC_REQUEST || m->payload[3] == '0') { status = mpack_rpc_request(session, &ptr, &bl, d(&reqdata)); } else if (m->type == MPACK_RPC_RESPONSE || m->payload[3] == '1') { status = mpack_rpc_reply(session, &ptr, &bl, m->id); } else if (m->type == MPACK_RPC_NOTIFICATION || m->payload[3] == '2') { status = mpack_rpc_notify(session, &ptr, &bl); } else return; /* inverted error checking, return */ } if (m->type == MPACK_RPC_REQUEST) { to_msgpack(m->method, (uint8_t **)&ptr); to_msgpack(m->args, (uint8_t **)&ptr); } else if (m->type == MPACK_RPC_RESPONSE) { to_msgpack(m->error, (uint8_t **)&ptr); to_msgpack(m->result, (uint8_t **)&ptr); } else if (m->type == MPACK_RPC_NOTIFICATION) { to_msgpack(m->method, (uint8_t **)&ptr); to_msgpack(m->args, (uint8_t **)&ptr); } else { if (!invert) ok(status == m->type, "%s: expected error matched", m->payload); return; } cmp_mem(buf, rbuf, (size_t)((uint8_t *)ptr - rbuf), cs == SIZE_MAX ? "%s: outgoing message matches in a single step" : "%s: outgoing message matches in steps of %zu%s", m->payload, cs, invert ? " (inverted)" : ""); } static const char *get_error_string(int type) { if (type == MPACK_RPC_EARRAY) return "invalid array"; if (type == MPACK_RPC_EARRAYL) return "invalid array length"; else if (type == MPACK_RPC_ETYPE) return "invalid message type"; else if (type == MPACK_RPC_EMSGID) return "invalid message id"; else if (type == MPACK_RPC_ERESPID) return "unmatched response id"; else abort(); } static void rpc_check_incoming(mpack_rpc_session_t *session, struct rpc_message *m, size_t cs, bool invert) { uint8_t buf[MSGPACK_BUFLEN]; uint8_t *b = buf; uint8_t dbuf[MSGPACK_BUFLEN]; uint8_t *db = dbuf; const char *ptr = (const char *)buf; to_msgpack(m->payload + 2, &b); mpack_rpc_message_t msg; int type = MPACK_EOF; while (type == MPACK_EOF) { size_t bl = cs; type = mpack_rpc_receive(session, &ptr, &bl, &msg); } bool result; if (m->type == MPACK_RPC_REQUEST) { result = msg.id == m->id; to_msgpack(m->method, &db); to_msgpack(m->args, &db); } else if (m->type == MPACK_RPC_RESPONSE) { to_msgpack(m->error, &db); to_msgpack(m->result, &db); result = msg.id == m->id; result = msg.data.p == &reqdata; } else if (m->type == MPACK_RPC_NOTIFICATION) { result = true; to_msgpack(m->method, &db); to_msgpack(m->args, &db); } else { if (!invert) ok(type == m->type, "%s: expected error matched(%s)", m->payload, get_error_string(type)); return; } result = result && type == m->type; ok(result && !memcmp(dbuf, ptr, (size_t)(db - dbuf)), cs == SIZE_MAX ? "%s: incoming message matches in a single step" : "%s: incoming message matches in steps of %zu%s", m->payload, cs, invert ? " (inverted)" : ""); } static void rpc_fixture_test(int fixture_idx) { const struct rpc_fixture *fixture = rpc_fixtures + fixture_idx; for (size_t i = 0; i < ARRAY_SIZE(chunksizes); i++) { for (size_t j = 0; j < 2; j++) { /* test the library from both endpoints */ mpack_rpc_session_t session; mpack_rpc_session_init(&session, fixture->capacity); for (size_t k = 0; k < fixture->count; k++) { size_t cs = chunksizes[i]; struct rpc_message *msg = fixture->messages + k; if (msg->payload[0] == '<') { if (j) rpc_check_outgoing(&session, msg, cs, j); else rpc_check_incoming(&session, msg, cs, j); } else if (msg->payload[1] == '>') { if (j) rpc_check_incoming(&session, msg, cs, j); else rpc_check_outgoing(&session, msg, cs, j); } } } } } int main(void) { for (int i = 0; i < fixture_count; i++) { fixture_test(fixtures, i); } signed_positive_packs_with_unsigned_format(); positive_signed_format_unpacks_as_unsigned(); unpacking_c1_returns_eread(); parsing_very_deep_objects_returns_enomem(); unparsing_very_deep_objects_returns_enomem(); parse_throw(); unparse_throw(); does_not_write_invalid_tokens(); rpc_copy_session_maintains_state(); rpc_request_id_wrap(); number_conv = true; /* test using mpack_{pack,unpack}_number to do the numeric conversions */ for (int i = 0; i < rpc_fixture_count; i++) { rpc_fixture_test(i); } for (int i = 0; i < number_fixture_count; i++) { fixture_test(number_fixtures, i); } /* test size macros */ ok(sizeof(MPACK_PARSER_STRUCT(2)) == MPACK_PARSER_STRUCT_SIZE(2)); ok(sizeof(MPACK_PARSER_STRUCT(3)) == MPACK_PARSER_STRUCT_SIZE(3)); ok(sizeof(MPACK_PARSER_STRUCT(9)) == MPACK_PARSER_STRUCT_SIZE(9)); ok(sizeof(MPACK_PARSER_STRUCT(45)) == MPACK_PARSER_STRUCT_SIZE(45)); ok(sizeof(MPACK_RPC_SESSION_STRUCT(2)) == MPACK_RPC_SESSION_STRUCT_SIZE(2)); ok(sizeof(MPACK_RPC_SESSION_STRUCT(3)) == MPACK_RPC_SESSION_STRUCT_SIZE(3)); ok(sizeof(MPACK_RPC_SESSION_STRUCT(9)) == MPACK_RPC_SESSION_STRUCT_SIZE(9)); ok(sizeof(MPACK_RPC_SESSION_STRUCT(45)) == MPACK_RPC_SESSION_STRUCT_SIZE(45)); done_testing(); } libmpack-1.0.5/test/tap.h000077700000000000000000000000001306356722300176712deps/tap/tap.hustar00rootroot00000000000000