messagepack implementation for embedded systems (mbed / arduino)

Dependents:   hello_message_pack

msgpack-c & msgpack-c++ https://github.com/msgpack/msgpack-c implementation for embedded systems (mbed / Arduino)

Committer:
hideakitai
Date:
Mon Feb 22 01:43:48 2016 +0000
Revision:
4:bd0c06dd6e92
Parent:
0:3f9dbf1e2cb0
fix throw error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hideakitai 0:3f9dbf1e2cb0 1 /*
hideakitai 0:3f9dbf1e2cb0 2 * MessagePack unpacking routine template
hideakitai 0:3f9dbf1e2cb0 3 *
hideakitai 0:3f9dbf1e2cb0 4 * Copyright (C) 2008-2010 FURUHASHI Sadayuki
hideakitai 0:3f9dbf1e2cb0 5 *
hideakitai 0:3f9dbf1e2cb0 6 * Distributed under the Boost Software License, Version 1.0.
hideakitai 0:3f9dbf1e2cb0 7 * (See accompanying file LICENSE_1_0.txt or copy at
hideakitai 0:3f9dbf1e2cb0 8 * http://www.boost.org/LICENSE_1_0.txt)
hideakitai 0:3f9dbf1e2cb0 9 */
hideakitai 0:3f9dbf1e2cb0 10
hideakitai 0:3f9dbf1e2cb0 11 #ifndef msgpack_unpack_func
hideakitai 0:3f9dbf1e2cb0 12 #error msgpack_unpack_func template is not defined
hideakitai 0:3f9dbf1e2cb0 13 #endif
hideakitai 0:3f9dbf1e2cb0 14
hideakitai 0:3f9dbf1e2cb0 15 #ifndef msgpack_unpack_callback
hideakitai 0:3f9dbf1e2cb0 16 #error msgpack_unpack_callback template is not defined
hideakitai 0:3f9dbf1e2cb0 17 #endif
hideakitai 0:3f9dbf1e2cb0 18
hideakitai 0:3f9dbf1e2cb0 19 #ifndef msgpack_unpack_struct
hideakitai 0:3f9dbf1e2cb0 20 #error msgpack_unpack_struct template is not defined
hideakitai 0:3f9dbf1e2cb0 21 #endif
hideakitai 0:3f9dbf1e2cb0 22
hideakitai 0:3f9dbf1e2cb0 23 #ifndef msgpack_unpack_struct_decl
hideakitai 0:3f9dbf1e2cb0 24 #define msgpack_unpack_struct_decl(name) msgpack_unpack_struct(name)
hideakitai 0:3f9dbf1e2cb0 25 #endif
hideakitai 0:3f9dbf1e2cb0 26
hideakitai 0:3f9dbf1e2cb0 27 #ifndef msgpack_unpack_object
hideakitai 0:3f9dbf1e2cb0 28 #error msgpack_unpack_object type is not defined
hideakitai 0:3f9dbf1e2cb0 29 #endif
hideakitai 0:3f9dbf1e2cb0 30
hideakitai 0:3f9dbf1e2cb0 31 #ifndef msgpack_unpack_user
hideakitai 0:3f9dbf1e2cb0 32 #error msgpack_unpack_user type is not defined
hideakitai 0:3f9dbf1e2cb0 33 #endif
hideakitai 0:3f9dbf1e2cb0 34
hideakitai 0:3f9dbf1e2cb0 35 #ifndef USE_CASE_RANGE
hideakitai 0:3f9dbf1e2cb0 36 #if !defined(_MSC_VER)
hideakitai 0:3f9dbf1e2cb0 37 #define USE_CASE_RANGE
hideakitai 0:3f9dbf1e2cb0 38 #endif
hideakitai 0:3f9dbf1e2cb0 39 #endif
hideakitai 0:3f9dbf1e2cb0 40
hideakitai 0:3f9dbf1e2cb0 41 msgpack_unpack_struct_decl(_stack) {
hideakitai 0:3f9dbf1e2cb0 42 msgpack_unpack_object obj;
hideakitai 0:3f9dbf1e2cb0 43 size_t count;
hideakitai 0:3f9dbf1e2cb0 44 unsigned int ct;
hideakitai 0:3f9dbf1e2cb0 45 msgpack_unpack_object map_key;
hideakitai 0:3f9dbf1e2cb0 46 };
hideakitai 0:3f9dbf1e2cb0 47
hideakitai 0:3f9dbf1e2cb0 48 msgpack_unpack_struct_decl(_context) {
hideakitai 0:3f9dbf1e2cb0 49 msgpack_unpack_user user;
hideakitai 0:3f9dbf1e2cb0 50 unsigned int cs;
hideakitai 0:3f9dbf1e2cb0 51 unsigned int trail;
hideakitai 0:3f9dbf1e2cb0 52 unsigned int top;
hideakitai 0:3f9dbf1e2cb0 53 /*
hideakitai 0:3f9dbf1e2cb0 54 msgpack_unpack_struct(_stack)* stack;
hideakitai 0:3f9dbf1e2cb0 55 unsigned int stack_size;
hideakitai 0:3f9dbf1e2cb0 56 msgpack_unpack_struct(_stack) embed_stack[MSGPACK_EMBED_STACK_SIZE];
hideakitai 0:3f9dbf1e2cb0 57 */
hideakitai 0:3f9dbf1e2cb0 58 msgpack_unpack_struct(_stack) stack[MSGPACK_EMBED_STACK_SIZE];
hideakitai 0:3f9dbf1e2cb0 59 };
hideakitai 0:3f9dbf1e2cb0 60
hideakitai 0:3f9dbf1e2cb0 61
hideakitai 0:3f9dbf1e2cb0 62 msgpack_unpack_func(void, _init)(msgpack_unpack_struct(_context)* ctx)
hideakitai 0:3f9dbf1e2cb0 63 {
hideakitai 0:3f9dbf1e2cb0 64 ctx->cs = MSGPACK_CS_HEADER;
hideakitai 0:3f9dbf1e2cb0 65 ctx->trail = 0;
hideakitai 0:3f9dbf1e2cb0 66 ctx->top = 0;
hideakitai 0:3f9dbf1e2cb0 67 /*
hideakitai 0:3f9dbf1e2cb0 68 ctx->stack = ctx->embed_stack;
hideakitai 0:3f9dbf1e2cb0 69 ctx->stack_size = MSGPACK_EMBED_STACK_SIZE;
hideakitai 0:3f9dbf1e2cb0 70 */
hideakitai 0:3f9dbf1e2cb0 71 ctx->stack[0].obj = msgpack_unpack_callback(_root)(&ctx->user);
hideakitai 0:3f9dbf1e2cb0 72 }
hideakitai 0:3f9dbf1e2cb0 73
hideakitai 0:3f9dbf1e2cb0 74 /*
hideakitai 0:3f9dbf1e2cb0 75 msgpack_unpack_func(void, _destroy)(msgpack_unpack_struct(_context)* ctx)
hideakitai 0:3f9dbf1e2cb0 76 {
hideakitai 0:3f9dbf1e2cb0 77 if(ctx->stack_size != MSGPACK_EMBED_STACK_SIZE) {
hideakitai 0:3f9dbf1e2cb0 78 free(ctx->stack);
hideakitai 0:3f9dbf1e2cb0 79 }
hideakitai 0:3f9dbf1e2cb0 80 }
hideakitai 0:3f9dbf1e2cb0 81 */
hideakitai 0:3f9dbf1e2cb0 82
hideakitai 0:3f9dbf1e2cb0 83 msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context)* ctx)
hideakitai 0:3f9dbf1e2cb0 84 {
hideakitai 0:3f9dbf1e2cb0 85 return (ctx)->stack[0].obj;
hideakitai 0:3f9dbf1e2cb0 86 }
hideakitai 0:3f9dbf1e2cb0 87
hideakitai 0:3f9dbf1e2cb0 88
hideakitai 0:3f9dbf1e2cb0 89 msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
hideakitai 0:3f9dbf1e2cb0 90 {
hideakitai 0:3f9dbf1e2cb0 91 assert(len >= *off);
hideakitai 0:3f9dbf1e2cb0 92 {
hideakitai 0:3f9dbf1e2cb0 93 const unsigned char* p = (unsigned char*)data + *off;
hideakitai 0:3f9dbf1e2cb0 94 const unsigned char* const pe = (unsigned char*)data + len;
hideakitai 0:3f9dbf1e2cb0 95 const void* n = NULL;
hideakitai 0:3f9dbf1e2cb0 96
hideakitai 0:3f9dbf1e2cb0 97 unsigned int trail = ctx->trail;
hideakitai 0:3f9dbf1e2cb0 98 unsigned int cs = ctx->cs;
hideakitai 0:3f9dbf1e2cb0 99 unsigned int top = ctx->top;
hideakitai 0:3f9dbf1e2cb0 100 msgpack_unpack_struct(_stack)* stack = ctx->stack;
hideakitai 0:3f9dbf1e2cb0 101 /*
hideakitai 0:3f9dbf1e2cb0 102 unsigned int stack_size = ctx->stack_size;
hideakitai 0:3f9dbf1e2cb0 103 */
hideakitai 0:3f9dbf1e2cb0 104 msgpack_unpack_user* user = &ctx->user;
hideakitai 0:3f9dbf1e2cb0 105
hideakitai 0:3f9dbf1e2cb0 106 msgpack_unpack_object obj;
hideakitai 0:3f9dbf1e2cb0 107 msgpack_unpack_struct(_stack)* c = NULL;
hideakitai 0:3f9dbf1e2cb0 108
hideakitai 0:3f9dbf1e2cb0 109 int ret;
hideakitai 0:3f9dbf1e2cb0 110
hideakitai 0:3f9dbf1e2cb0 111 #define push_simple_value(func) \
hideakitai 0:3f9dbf1e2cb0 112 if(msgpack_unpack_callback(func)(user, &obj) < 0) { goto _failed; } \
hideakitai 0:3f9dbf1e2cb0 113 goto _push
hideakitai 0:3f9dbf1e2cb0 114 #define push_fixed_value(func, arg) \
hideakitai 0:3f9dbf1e2cb0 115 if(msgpack_unpack_callback(func)(user, arg, &obj) < 0) { goto _failed; } \
hideakitai 0:3f9dbf1e2cb0 116 goto _push
hideakitai 0:3f9dbf1e2cb0 117 #define push_variable_value(func, base, pos, len) \
hideakitai 0:3f9dbf1e2cb0 118 if(msgpack_unpack_callback(func)(user, \
hideakitai 0:3f9dbf1e2cb0 119 (const char*)base, (const char*)pos, len, &obj) < 0) { goto _failed; } \
hideakitai 0:3f9dbf1e2cb0 120 goto _push
hideakitai 0:3f9dbf1e2cb0 121
hideakitai 0:3f9dbf1e2cb0 122 #define again_fixed_trail(_cs, trail_len) \
hideakitai 0:3f9dbf1e2cb0 123 trail = trail_len; \
hideakitai 0:3f9dbf1e2cb0 124 cs = _cs; \
hideakitai 0:3f9dbf1e2cb0 125 goto _fixed_trail_again
hideakitai 0:3f9dbf1e2cb0 126 #define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \
hideakitai 0:3f9dbf1e2cb0 127 trail = trail_len; \
hideakitai 0:3f9dbf1e2cb0 128 if(trail == 0) { goto ifzero; } \
hideakitai 0:3f9dbf1e2cb0 129 cs = _cs; \
hideakitai 0:3f9dbf1e2cb0 130 goto _fixed_trail_again
hideakitai 0:3f9dbf1e2cb0 131
hideakitai 0:3f9dbf1e2cb0 132 #define start_container(func, count_, ct_) \
hideakitai 0:3f9dbf1e2cb0 133 if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ \
hideakitai 0:3f9dbf1e2cb0 134 if(msgpack_unpack_callback(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
hideakitai 0:3f9dbf1e2cb0 135 if((count_) == 0) { obj = stack[top].obj; goto _push; } \
hideakitai 0:3f9dbf1e2cb0 136 stack[top].ct = ct_; \
hideakitai 0:3f9dbf1e2cb0 137 stack[top].count = count_; \
hideakitai 0:3f9dbf1e2cb0 138 ++top; \
hideakitai 0:3f9dbf1e2cb0 139 /*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \
hideakitai 0:3f9dbf1e2cb0 140 /*printf("stack push %d\n", top);*/ \
hideakitai 0:3f9dbf1e2cb0 141 /* FIXME \
hideakitai 0:3f9dbf1e2cb0 142 if(top >= stack_size) { \
hideakitai 0:3f9dbf1e2cb0 143 if(stack_size == MSGPACK_EMBED_STACK_SIZE) { \
hideakitai 0:3f9dbf1e2cb0 144 size_t csize = sizeof(msgpack_unpack_struct(_stack)) * MSGPACK_EMBED_STACK_SIZE; \
hideakitai 0:3f9dbf1e2cb0 145 size_t nsize = csize * 2; \
hideakitai 0:3f9dbf1e2cb0 146 msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)malloc(nsize); \
hideakitai 0:3f9dbf1e2cb0 147 if(tmp == NULL) { goto _failed; } \
hideakitai 0:3f9dbf1e2cb0 148 memcpy(tmp, ctx->stack, csize); \
hideakitai 0:3f9dbf1e2cb0 149 ctx->stack = stack = tmp; \
hideakitai 0:3f9dbf1e2cb0 150 ctx->stack_size = stack_size = MSGPACK_EMBED_STACK_SIZE * 2; \
hideakitai 0:3f9dbf1e2cb0 151 } else { \
hideakitai 0:3f9dbf1e2cb0 152 size_t nsize = sizeof(msgpack_unpack_struct(_stack)) * ctx->stack_size * 2; \
hideakitai 0:3f9dbf1e2cb0 153 msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)realloc(ctx->stack, nsize); \
hideakitai 0:3f9dbf1e2cb0 154 if(tmp == NULL) { goto _failed; } \
hideakitai 0:3f9dbf1e2cb0 155 ctx->stack = stack = tmp; \
hideakitai 0:3f9dbf1e2cb0 156 ctx->stack_size = stack_size = stack_size * 2; \
hideakitai 0:3f9dbf1e2cb0 157 } \
hideakitai 0:3f9dbf1e2cb0 158 } \
hideakitai 0:3f9dbf1e2cb0 159 */ \
hideakitai 0:3f9dbf1e2cb0 160 goto _header_again
hideakitai 0:3f9dbf1e2cb0 161
hideakitai 0:3f9dbf1e2cb0 162 #define NEXT_CS(p) \
hideakitai 0:3f9dbf1e2cb0 163 ((unsigned int)*p & 0x1f)
hideakitai 0:3f9dbf1e2cb0 164
hideakitai 0:3f9dbf1e2cb0 165 #ifdef USE_CASE_RANGE
hideakitai 0:3f9dbf1e2cb0 166 #define SWITCH_RANGE_BEGIN switch(*p) {
hideakitai 0:3f9dbf1e2cb0 167 #define SWITCH_RANGE(FROM, TO) case FROM ... TO:
hideakitai 0:3f9dbf1e2cb0 168 #define SWITCH_RANGE_DEFAULT default:
hideakitai 0:3f9dbf1e2cb0 169 #define SWITCH_RANGE_END }
hideakitai 0:3f9dbf1e2cb0 170 #else
hideakitai 0:3f9dbf1e2cb0 171 #define SWITCH_RANGE_BEGIN { if(0) {
hideakitai 0:3f9dbf1e2cb0 172 #define SWITCH_RANGE(FROM, TO) } else if(FROM <= *p && *p <= TO) {
hideakitai 0:3f9dbf1e2cb0 173 #define SWITCH_RANGE_DEFAULT } else {
hideakitai 0:3f9dbf1e2cb0 174 #define SWITCH_RANGE_END } }
hideakitai 0:3f9dbf1e2cb0 175 #endif
hideakitai 0:3f9dbf1e2cb0 176
hideakitai 0:3f9dbf1e2cb0 177 if(p == pe) { goto _out; }
hideakitai 0:3f9dbf1e2cb0 178 do {
hideakitai 0:3f9dbf1e2cb0 179 switch(cs) {
hideakitai 0:3f9dbf1e2cb0 180 case MSGPACK_CS_HEADER:
hideakitai 0:3f9dbf1e2cb0 181 SWITCH_RANGE_BEGIN
hideakitai 0:3f9dbf1e2cb0 182 SWITCH_RANGE(0x00, 0x7f) // Positive Fixnum
hideakitai 0:3f9dbf1e2cb0 183 push_fixed_value(_uint8, *(uint8_t*)p);
hideakitai 0:3f9dbf1e2cb0 184 SWITCH_RANGE(0xe0, 0xff) // Negative Fixnum
hideakitai 0:3f9dbf1e2cb0 185 push_fixed_value(_int8, *(int8_t*)p);
hideakitai 0:3f9dbf1e2cb0 186 SWITCH_RANGE(0xc0, 0xdf) // Variable
hideakitai 0:3f9dbf1e2cb0 187 switch(*p) {
hideakitai 0:3f9dbf1e2cb0 188 case 0xc0: // nil
hideakitai 0:3f9dbf1e2cb0 189 push_simple_value(_nil);
hideakitai 0:3f9dbf1e2cb0 190 //case 0xc1: // string
hideakitai 0:3f9dbf1e2cb0 191 // again_terminal_trail(NEXT_CS(p), p+1);
hideakitai 0:3f9dbf1e2cb0 192 case 0xc2: // false
hideakitai 0:3f9dbf1e2cb0 193 push_simple_value(_false);
hideakitai 0:3f9dbf1e2cb0 194 case 0xc3: // true
hideakitai 0:3f9dbf1e2cb0 195 push_simple_value(_true);
hideakitai 0:3f9dbf1e2cb0 196 case 0xc4: // bin 8
hideakitai 0:3f9dbf1e2cb0 197 case 0xc5: // bin 16
hideakitai 0:3f9dbf1e2cb0 198 case 0xc6: // bin 32
hideakitai 0:3f9dbf1e2cb0 199 again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
hideakitai 0:3f9dbf1e2cb0 200 case 0xc7: // ext 8
hideakitai 0:3f9dbf1e2cb0 201 case 0xc8: // ext 16
hideakitai 0:3f9dbf1e2cb0 202 case 0xc9: // ext 32
hideakitai 0:3f9dbf1e2cb0 203 again_fixed_trail(NEXT_CS(p), 1 << ((((unsigned int)*p) + 1) & 0x03));
hideakitai 0:3f9dbf1e2cb0 204 case 0xca: // float
hideakitai 0:3f9dbf1e2cb0 205 case 0xcb: // double
hideakitai 0:3f9dbf1e2cb0 206 case 0xcc: // unsigned int 8
hideakitai 0:3f9dbf1e2cb0 207 case 0xcd: // unsigned int 16
hideakitai 0:3f9dbf1e2cb0 208 case 0xce: // unsigned int 32
hideakitai 0:3f9dbf1e2cb0 209 case 0xcf: // unsigned int 64
hideakitai 0:3f9dbf1e2cb0 210 case 0xd0: // signed int 8
hideakitai 0:3f9dbf1e2cb0 211 case 0xd1: // signed int 16
hideakitai 0:3f9dbf1e2cb0 212 case 0xd2: // signed int 32
hideakitai 0:3f9dbf1e2cb0 213 case 0xd3: // signed int 64
hideakitai 0:3f9dbf1e2cb0 214 again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
hideakitai 0:3f9dbf1e2cb0 215 case 0xd4: // fixext 1
hideakitai 0:3f9dbf1e2cb0 216 case 0xd5: // fixext 2
hideakitai 0:3f9dbf1e2cb0 217 case 0xd6: // fixext 4
hideakitai 0:3f9dbf1e2cb0 218 case 0xd7: // fixext 8
hideakitai 0:3f9dbf1e2cb0 219 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE,
hideakitai 0:3f9dbf1e2cb0 220 (1 << (((unsigned int)*p) & 0x03)) + 1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 221 case 0xd8: // fixext 16
hideakitai 0:3f9dbf1e2cb0 222 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 16+1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 223
hideakitai 0:3f9dbf1e2cb0 224 case 0xd9: // str 8
hideakitai 0:3f9dbf1e2cb0 225 case 0xda: // str 16
hideakitai 0:3f9dbf1e2cb0 226 case 0xdb: // str 32
hideakitai 0:3f9dbf1e2cb0 227 again_fixed_trail(NEXT_CS(p), 1 << ((((unsigned int)*p) & 0x03) - 1));
hideakitai 0:3f9dbf1e2cb0 228 case 0xdc: // array 16
hideakitai 0:3f9dbf1e2cb0 229 case 0xdd: // array 32
hideakitai 0:3f9dbf1e2cb0 230 case 0xde: // map 16
hideakitai 0:3f9dbf1e2cb0 231 case 0xdf: // map 32
hideakitai 0:3f9dbf1e2cb0 232 again_fixed_trail(NEXT_CS(p), 2u << (((unsigned int)*p) & 0x01));
hideakitai 0:3f9dbf1e2cb0 233 default:
hideakitai 0:3f9dbf1e2cb0 234 goto _failed;
hideakitai 0:3f9dbf1e2cb0 235 }
hideakitai 0:3f9dbf1e2cb0 236 SWITCH_RANGE(0xa0, 0xbf) // FixStr
hideakitai 0:3f9dbf1e2cb0 237 again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, ((unsigned int)*p & 0x1f), _str_zero);
hideakitai 0:3f9dbf1e2cb0 238 SWITCH_RANGE(0x90, 0x9f) // FixArray
hideakitai 0:3f9dbf1e2cb0 239 start_container(_array, ((unsigned int)*p) & 0x0f, MSGPACK_CT_ARRAY_ITEM);
hideakitai 0:3f9dbf1e2cb0 240 SWITCH_RANGE(0x80, 0x8f) // FixMap
hideakitai 0:3f9dbf1e2cb0 241 start_container(_map, ((unsigned int)*p) & 0x0f, MSGPACK_CT_MAP_KEY);
hideakitai 0:3f9dbf1e2cb0 242
hideakitai 0:3f9dbf1e2cb0 243 SWITCH_RANGE_DEFAULT
hideakitai 0:3f9dbf1e2cb0 244 goto _failed;
hideakitai 0:3f9dbf1e2cb0 245 SWITCH_RANGE_END
hideakitai 0:3f9dbf1e2cb0 246 // end MSGPACK_CS_HEADER
hideakitai 0:3f9dbf1e2cb0 247
hideakitai 0:3f9dbf1e2cb0 248
hideakitai 0:3f9dbf1e2cb0 249 _fixed_trail_again:
hideakitai 0:3f9dbf1e2cb0 250 ++p;
hideakitai 0:3f9dbf1e2cb0 251
hideakitai 0:3f9dbf1e2cb0 252 default:
hideakitai 0:3f9dbf1e2cb0 253 if((size_t)(pe - p) < trail) { goto _out; }
hideakitai 0:3f9dbf1e2cb0 254 n = p; p += trail - 1;
hideakitai 0:3f9dbf1e2cb0 255 switch(cs) {
hideakitai 0:3f9dbf1e2cb0 256 //case MSGPACK_CS_
hideakitai 0:3f9dbf1e2cb0 257 //case MSGPACK_CS_
hideakitai 0:3f9dbf1e2cb0 258 case MSGPACK_CS_FLOAT: {
hideakitai 0:3f9dbf1e2cb0 259 union { uint32_t i; float f; } mem;
hideakitai 0:3f9dbf1e2cb0 260 _msgpack_load32(uint32_t, n, &mem.i);
hideakitai 0:3f9dbf1e2cb0 261 push_fixed_value(_float, mem.f); }
hideakitai 0:3f9dbf1e2cb0 262 case MSGPACK_CS_DOUBLE: {
hideakitai 0:3f9dbf1e2cb0 263 union { uint64_t i; double f; } mem;
hideakitai 0:3f9dbf1e2cb0 264 _msgpack_load64(uint64_t, n, &mem.i);
hideakitai 0:3f9dbf1e2cb0 265 #if defined(TARGET_OS_IPHONE)
hideakitai 0:3f9dbf1e2cb0 266 // ok
hideakitai 0:3f9dbf1e2cb0 267 #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi
hideakitai 0:3f9dbf1e2cb0 268 // https://github.com/msgpack/msgpack-perl/pull/1
hideakitai 0:3f9dbf1e2cb0 269 mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
hideakitai 0:3f9dbf1e2cb0 270 #endif
hideakitai 0:3f9dbf1e2cb0 271 push_fixed_value(_double, mem.f); }
hideakitai 0:3f9dbf1e2cb0 272 case MSGPACK_CS_UINT_8:
hideakitai 0:3f9dbf1e2cb0 273 push_fixed_value(_uint8, *(uint8_t*)n);
hideakitai 0:3f9dbf1e2cb0 274 case MSGPACK_CS_UINT_16:{
hideakitai 0:3f9dbf1e2cb0 275 uint16_t tmp;
hideakitai 0:3f9dbf1e2cb0 276 _msgpack_load16(uint16_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 277 push_fixed_value(_uint16, tmp);
hideakitai 0:3f9dbf1e2cb0 278 }
hideakitai 0:3f9dbf1e2cb0 279 case MSGPACK_CS_UINT_32:{
hideakitai 0:3f9dbf1e2cb0 280 uint32_t tmp;
hideakitai 0:3f9dbf1e2cb0 281 _msgpack_load32(uint32_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 282 push_fixed_value(_uint32, tmp);
hideakitai 0:3f9dbf1e2cb0 283 }
hideakitai 0:3f9dbf1e2cb0 284 case MSGPACK_CS_UINT_64:{
hideakitai 0:3f9dbf1e2cb0 285 uint64_t tmp;
hideakitai 0:3f9dbf1e2cb0 286 _msgpack_load64(uint64_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 287 push_fixed_value(_uint64, tmp);
hideakitai 0:3f9dbf1e2cb0 288 }
hideakitai 0:3f9dbf1e2cb0 289 case MSGPACK_CS_INT_8:
hideakitai 0:3f9dbf1e2cb0 290 push_fixed_value(_int8, *(int8_t*)n);
hideakitai 0:3f9dbf1e2cb0 291 case MSGPACK_CS_INT_16:{
hideakitai 0:3f9dbf1e2cb0 292 int16_t tmp;
hideakitai 0:3f9dbf1e2cb0 293 _msgpack_load16(int16_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 294 push_fixed_value(_int16, tmp);
hideakitai 0:3f9dbf1e2cb0 295 }
hideakitai 0:3f9dbf1e2cb0 296 case MSGPACK_CS_INT_32:{
hideakitai 0:3f9dbf1e2cb0 297 int32_t tmp;
hideakitai 0:3f9dbf1e2cb0 298 _msgpack_load32(int32_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 299 push_fixed_value(_int32, tmp);
hideakitai 0:3f9dbf1e2cb0 300 }
hideakitai 0:3f9dbf1e2cb0 301 case MSGPACK_CS_INT_64:{
hideakitai 0:3f9dbf1e2cb0 302 int64_t tmp;
hideakitai 0:3f9dbf1e2cb0 303 _msgpack_load64(int64_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 304 push_fixed_value(_int64, tmp);
hideakitai 0:3f9dbf1e2cb0 305 }
hideakitai 0:3f9dbf1e2cb0 306 case MSGPACK_CS_FIXEXT_1:
hideakitai 0:3f9dbf1e2cb0 307 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 1+1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 308 case MSGPACK_CS_FIXEXT_2:
hideakitai 0:3f9dbf1e2cb0 309 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 2+1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 310 case MSGPACK_CS_FIXEXT_4:
hideakitai 0:3f9dbf1e2cb0 311 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 4+1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 312 case MSGPACK_CS_FIXEXT_8:
hideakitai 0:3f9dbf1e2cb0 313 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 8+1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 314 case MSGPACK_CS_FIXEXT_16:
hideakitai 0:3f9dbf1e2cb0 315 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, 16+1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 316 case MSGPACK_CS_STR_8:
hideakitai 0:3f9dbf1e2cb0 317 again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, *(uint8_t*)n, _str_zero);
hideakitai 0:3f9dbf1e2cb0 318 case MSGPACK_CS_BIN_8:
hideakitai 0:3f9dbf1e2cb0 319 again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, *(uint8_t*)n, _bin_zero);
hideakitai 0:3f9dbf1e2cb0 320 case MSGPACK_CS_EXT_8:
hideakitai 0:3f9dbf1e2cb0 321 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, (*(uint8_t*)n) + 1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 322 case MSGPACK_CS_STR_16:{
hideakitai 0:3f9dbf1e2cb0 323 uint16_t tmp;
hideakitai 0:3f9dbf1e2cb0 324 _msgpack_load16(uint16_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 325 again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, tmp, _str_zero);
hideakitai 0:3f9dbf1e2cb0 326 }
hideakitai 0:3f9dbf1e2cb0 327 case MSGPACK_CS_BIN_16:{
hideakitai 0:3f9dbf1e2cb0 328 uint16_t tmp;
hideakitai 0:3f9dbf1e2cb0 329 _msgpack_load16(uint16_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 330 again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, tmp, _bin_zero);
hideakitai 0:3f9dbf1e2cb0 331 }
hideakitai 0:3f9dbf1e2cb0 332 case MSGPACK_CS_EXT_16:{
hideakitai 0:3f9dbf1e2cb0 333 uint16_t tmp;
hideakitai 0:3f9dbf1e2cb0 334 _msgpack_load16(uint16_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 335 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, tmp + 1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 336 }
hideakitai 0:3f9dbf1e2cb0 337 case MSGPACK_CS_STR_32:{
hideakitai 0:3f9dbf1e2cb0 338 uint32_t tmp;
hideakitai 0:3f9dbf1e2cb0 339 _msgpack_load32(uint32_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 340 again_fixed_trail_if_zero(MSGPACK_ACS_STR_VALUE, tmp, _str_zero);
hideakitai 0:3f9dbf1e2cb0 341 }
hideakitai 0:3f9dbf1e2cb0 342 case MSGPACK_CS_BIN_32:{
hideakitai 0:3f9dbf1e2cb0 343 uint32_t tmp;
hideakitai 0:3f9dbf1e2cb0 344 _msgpack_load32(uint32_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 345 again_fixed_trail_if_zero(MSGPACK_ACS_BIN_VALUE, tmp, _bin_zero);
hideakitai 0:3f9dbf1e2cb0 346 }
hideakitai 0:3f9dbf1e2cb0 347 case MSGPACK_CS_EXT_32:{
hideakitai 0:3f9dbf1e2cb0 348 uint32_t tmp;
hideakitai 0:3f9dbf1e2cb0 349 _msgpack_load32(uint32_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 350 again_fixed_trail_if_zero(MSGPACK_ACS_EXT_VALUE, tmp + 1, _ext_zero);
hideakitai 0:3f9dbf1e2cb0 351 }
hideakitai 0:3f9dbf1e2cb0 352 case MSGPACK_ACS_STR_VALUE:
hideakitai 0:3f9dbf1e2cb0 353 _str_zero:
hideakitai 0:3f9dbf1e2cb0 354 push_variable_value(_str, data, n, trail);
hideakitai 0:3f9dbf1e2cb0 355 case MSGPACK_ACS_BIN_VALUE:
hideakitai 0:3f9dbf1e2cb0 356 _bin_zero:
hideakitai 0:3f9dbf1e2cb0 357 push_variable_value(_bin, data, n, trail);
hideakitai 0:3f9dbf1e2cb0 358 case MSGPACK_ACS_EXT_VALUE:
hideakitai 0:3f9dbf1e2cb0 359 _ext_zero:
hideakitai 0:3f9dbf1e2cb0 360 push_variable_value(_ext, data, n, trail);
hideakitai 0:3f9dbf1e2cb0 361
hideakitai 0:3f9dbf1e2cb0 362 case MSGPACK_CS_ARRAY_16:{
hideakitai 0:3f9dbf1e2cb0 363 uint16_t tmp;
hideakitai 0:3f9dbf1e2cb0 364 _msgpack_load16(uint16_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 365 start_container(_array, tmp, MSGPACK_CT_ARRAY_ITEM);
hideakitai 0:3f9dbf1e2cb0 366 }
hideakitai 0:3f9dbf1e2cb0 367 case MSGPACK_CS_ARRAY_32:{
hideakitai 0:3f9dbf1e2cb0 368 /* FIXME security guard */
hideakitai 0:3f9dbf1e2cb0 369 uint32_t tmp;
hideakitai 0:3f9dbf1e2cb0 370 _msgpack_load32(uint32_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 371 start_container(_array, tmp, MSGPACK_CT_ARRAY_ITEM);
hideakitai 0:3f9dbf1e2cb0 372 }
hideakitai 0:3f9dbf1e2cb0 373
hideakitai 0:3f9dbf1e2cb0 374 case MSGPACK_CS_MAP_16:{
hideakitai 0:3f9dbf1e2cb0 375 uint16_t tmp;
hideakitai 0:3f9dbf1e2cb0 376 _msgpack_load16(uint16_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 377 start_container(_map, tmp, MSGPACK_CT_MAP_KEY);
hideakitai 0:3f9dbf1e2cb0 378 }
hideakitai 0:3f9dbf1e2cb0 379 case MSGPACK_CS_MAP_32:{
hideakitai 0:3f9dbf1e2cb0 380 /* FIXME security guard */
hideakitai 0:3f9dbf1e2cb0 381 uint32_t tmp;
hideakitai 0:3f9dbf1e2cb0 382 _msgpack_load32(uint32_t,n,&tmp);
hideakitai 0:3f9dbf1e2cb0 383 start_container(_map, tmp, MSGPACK_CT_MAP_KEY);
hideakitai 0:3f9dbf1e2cb0 384 }
hideakitai 0:3f9dbf1e2cb0 385
hideakitai 0:3f9dbf1e2cb0 386 default:
hideakitai 0:3f9dbf1e2cb0 387 goto _failed;
hideakitai 0:3f9dbf1e2cb0 388 }
hideakitai 0:3f9dbf1e2cb0 389 }
hideakitai 0:3f9dbf1e2cb0 390
hideakitai 0:3f9dbf1e2cb0 391 _push:
hideakitai 0:3f9dbf1e2cb0 392 if(top == 0) { goto _finish; }
hideakitai 0:3f9dbf1e2cb0 393 c = &stack[top-1];
hideakitai 0:3f9dbf1e2cb0 394 switch(c->ct) {
hideakitai 0:3f9dbf1e2cb0 395 case MSGPACK_CT_ARRAY_ITEM:
hideakitai 0:3f9dbf1e2cb0 396 if(msgpack_unpack_callback(_array_item)(user, &c->obj, obj) < 0) { goto _failed; }
hideakitai 0:3f9dbf1e2cb0 397 if(--c->count == 0) {
hideakitai 0:3f9dbf1e2cb0 398 obj = c->obj;
hideakitai 0:3f9dbf1e2cb0 399 --top;
hideakitai 0:3f9dbf1e2cb0 400 /*printf("stack pop %d\n", top);*/
hideakitai 0:3f9dbf1e2cb0 401 goto _push;
hideakitai 0:3f9dbf1e2cb0 402 }
hideakitai 0:3f9dbf1e2cb0 403 goto _header_again;
hideakitai 0:3f9dbf1e2cb0 404 case MSGPACK_CT_MAP_KEY:
hideakitai 0:3f9dbf1e2cb0 405 c->map_key = obj;
hideakitai 0:3f9dbf1e2cb0 406 c->ct = MSGPACK_CT_MAP_VALUE;
hideakitai 0:3f9dbf1e2cb0 407 goto _header_again;
hideakitai 0:3f9dbf1e2cb0 408 case MSGPACK_CT_MAP_VALUE:
hideakitai 0:3f9dbf1e2cb0 409 if(msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
hideakitai 0:3f9dbf1e2cb0 410 if(--c->count == 0) {
hideakitai 0:3f9dbf1e2cb0 411 obj = c->obj;
hideakitai 0:3f9dbf1e2cb0 412 --top;
hideakitai 0:3f9dbf1e2cb0 413 /*printf("stack pop %d\n", top);*/
hideakitai 0:3f9dbf1e2cb0 414 goto _push;
hideakitai 0:3f9dbf1e2cb0 415 }
hideakitai 0:3f9dbf1e2cb0 416 c->ct = MSGPACK_CT_MAP_KEY;
hideakitai 0:3f9dbf1e2cb0 417 goto _header_again;
hideakitai 0:3f9dbf1e2cb0 418
hideakitai 0:3f9dbf1e2cb0 419 default:
hideakitai 0:3f9dbf1e2cb0 420 goto _failed;
hideakitai 0:3f9dbf1e2cb0 421 }
hideakitai 0:3f9dbf1e2cb0 422
hideakitai 0:3f9dbf1e2cb0 423 _header_again:
hideakitai 0:3f9dbf1e2cb0 424 cs = MSGPACK_CS_HEADER;
hideakitai 0:3f9dbf1e2cb0 425 ++p;
hideakitai 0:3f9dbf1e2cb0 426 } while(p != pe);
hideakitai 0:3f9dbf1e2cb0 427 goto _out;
hideakitai 0:3f9dbf1e2cb0 428
hideakitai 0:3f9dbf1e2cb0 429
hideakitai 0:3f9dbf1e2cb0 430 _finish:
hideakitai 0:3f9dbf1e2cb0 431 stack[0].obj = obj;
hideakitai 0:3f9dbf1e2cb0 432 ++p;
hideakitai 0:3f9dbf1e2cb0 433 ret = 1;
hideakitai 0:3f9dbf1e2cb0 434 /*printf("-- finish --\n"); */
hideakitai 0:3f9dbf1e2cb0 435 goto _end;
hideakitai 0:3f9dbf1e2cb0 436
hideakitai 0:3f9dbf1e2cb0 437 _failed:
hideakitai 0:3f9dbf1e2cb0 438 /*printf("** FAILED **\n"); */
hideakitai 0:3f9dbf1e2cb0 439 ret = -1;
hideakitai 0:3f9dbf1e2cb0 440 goto _end;
hideakitai 0:3f9dbf1e2cb0 441
hideakitai 0:3f9dbf1e2cb0 442 _out:
hideakitai 0:3f9dbf1e2cb0 443 ret = 0;
hideakitai 0:3f9dbf1e2cb0 444 goto _end;
hideakitai 0:3f9dbf1e2cb0 445
hideakitai 0:3f9dbf1e2cb0 446 _end:
hideakitai 0:3f9dbf1e2cb0 447 ctx->cs = cs;
hideakitai 0:3f9dbf1e2cb0 448 ctx->trail = trail;
hideakitai 0:3f9dbf1e2cb0 449 ctx->top = top;
hideakitai 0:3f9dbf1e2cb0 450 *off = (size_t)(p - (const unsigned char*)data);
hideakitai 0:3f9dbf1e2cb0 451
hideakitai 0:3f9dbf1e2cb0 452 return ret;
hideakitai 0:3f9dbf1e2cb0 453 }
hideakitai 0:3f9dbf1e2cb0 454 }
hideakitai 0:3f9dbf1e2cb0 455
hideakitai 0:3f9dbf1e2cb0 456 #undef msgpack_unpack_func
hideakitai 0:3f9dbf1e2cb0 457 #undef msgpack_unpack_callback
hideakitai 0:3f9dbf1e2cb0 458 #undef msgpack_unpack_struct
hideakitai 0:3f9dbf1e2cb0 459 #undef msgpack_unpack_object
hideakitai 0:3f9dbf1e2cb0 460 #undef msgpack_unpack_user
hideakitai 0:3f9dbf1e2cb0 461
hideakitai 0:3f9dbf1e2cb0 462 #undef push_simple_value
hideakitai 0:3f9dbf1e2cb0 463 #undef push_fixed_value
hideakitai 0:3f9dbf1e2cb0 464 #undef push_variable_value
hideakitai 0:3f9dbf1e2cb0 465 #undef again_fixed_trail
hideakitai 0:3f9dbf1e2cb0 466 #undef again_fixed_trail_if_zero
hideakitai 0:3f9dbf1e2cb0 467 #undef start_container
hideakitai 0:3f9dbf1e2cb0 468
hideakitai 0:3f9dbf1e2cb0 469 #undef NEXT_CS
hideakitai 0:3f9dbf1e2cb0 470
hideakitai 0:3f9dbf1e2cb0 471 #undef SWITCH_RANGE_BEGIN
hideakitai 0:3f9dbf1e2cb0 472 #undef SWITCH_RANGE
hideakitai 0:3f9dbf1e2cb0 473 #undef SWITCH_RANGE_DEFAULT
hideakitai 0:3f9dbf1e2cb0 474 #undef SWITCH_RANGE_END
hideakitai 0:3f9dbf1e2cb0 475