update pack/unpack routines

git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@100 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
frsyuki 2009-02-15 09:10:02 +00:00
parent 81a771094d
commit a114f4a5a5
32 changed files with 503 additions and 366 deletions

View file

@ -1,7 +1,7 @@
/*
* MessagePack unpacking routine template
*
* Copyright (C) 2008 FURUHASHI Sadayuki
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View file

@ -1,7 +1,7 @@
/*
* MessagePack packing routine template
*
* Copyright (C) 2008 FURUHASHI Sadayuki
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -278,49 +278,49 @@ do { \
#ifdef msgpack_pack_inline_func_fastint
msgpack_pack_inline_func_fastint(uint8)(msgpack_pack_user x, uint8_t d)
msgpack_pack_inline_func_fastint(_uint8)(msgpack_pack_user x, uint8_t d)
{
const unsigned char buf[2] = {0xcc, d};
msgpack_pack_append_buffer(x, buf, 2);
}
msgpack_pack_inline_func_fastint(uint16)(msgpack_pack_user x, uint16_t d)
msgpack_pack_inline_func_fastint(_uint16)(msgpack_pack_user x, uint16_t d)
{
const unsigned char buf[3] = {0xcd, STORE_BE16(d)};
msgpack_pack_append_buffer(x, buf, 3);
}
msgpack_pack_inline_func_fastint(uint32)(msgpack_pack_user x, uint32_t d)
msgpack_pack_inline_func_fastint(_uint32)(msgpack_pack_user x, uint32_t d)
{
const unsigned char buf[5] = {0xce, STORE_BE32(d)};
msgpack_pack_append_buffer(x, buf, 5);
}
msgpack_pack_inline_func_fastint(uint64)(msgpack_pack_user x, uint64_t d)
msgpack_pack_inline_func_fastint(_uint64)(msgpack_pack_user x, uint64_t d)
{
const unsigned char buf[9] = {0xcf, STORE_BE64(d)};
msgpack_pack_append_buffer(x, buf, 9);
}
msgpack_pack_inline_func_fastint(int8)(msgpack_pack_user x, int8_t d)
msgpack_pack_inline_func_fastint(_int8)(msgpack_pack_user x, int8_t d)
{
const unsigned char buf[2] = {0xd0, d};
msgpack_pack_append_buffer(x, buf, 2);
}
msgpack_pack_inline_func_fastint(int16)(msgpack_pack_user x, int16_t d)
msgpack_pack_inline_func_fastint(_int16)(msgpack_pack_user x, int16_t d)
{
const unsigned char buf[3] = {0xd1, STORE_BE16(d)};
msgpack_pack_append_buffer(x, buf, 3);
}
msgpack_pack_inline_func_fastint(int32)(msgpack_pack_user x, int32_t d)
msgpack_pack_inline_func_fastint(_int32)(msgpack_pack_user x, int32_t d)
{
const unsigned char buf[5] = {0xd2, STORE_BE32(d)};
msgpack_pack_append_buffer(x, buf, 5);
}
msgpack_pack_inline_func_fastint(int64)(msgpack_pack_user x, int64_t d)
msgpack_pack_inline_func_fastint(_int64)(msgpack_pack_user x, int64_t d)
{
const unsigned char buf[9] = {0xd3, STORE_BE64(d)};
msgpack_pack_append_buffer(x, buf, 9);
@ -330,42 +330,42 @@ msgpack_pack_inline_func_fastint(int64)(msgpack_pack_user x, int64_t d)
#endif
msgpack_pack_inline_func(uint8)(msgpack_pack_user x, uint8_t d)
msgpack_pack_inline_func(_uint8)(msgpack_pack_user x, uint8_t d)
{
msgpack_pack_real_uint8(x, d);
}
msgpack_pack_inline_func(uint16)(msgpack_pack_user x, uint16_t d)
msgpack_pack_inline_func(_uint16)(msgpack_pack_user x, uint16_t d)
{
msgpack_pack_real_uint16(x, d);
}
msgpack_pack_inline_func(uint32)(msgpack_pack_user x, uint32_t d)
msgpack_pack_inline_func(_uint32)(msgpack_pack_user x, uint32_t d)
{
msgpack_pack_real_uint32(x, d);
}
msgpack_pack_inline_func(uint64)(msgpack_pack_user x, uint64_t d)
msgpack_pack_inline_func(_uint64)(msgpack_pack_user x, uint64_t d)
{
msgpack_pack_real_uint64(x, d);
}
msgpack_pack_inline_func(int8)(msgpack_pack_user x, int8_t d)
msgpack_pack_inline_func(_int8)(msgpack_pack_user x, int8_t d)
{
msgpack_pack_real_int8(x, d);
}
msgpack_pack_inline_func(int16)(msgpack_pack_user x, int16_t d)
msgpack_pack_inline_func(_int16)(msgpack_pack_user x, int16_t d)
{
msgpack_pack_real_int16(x, d);
}
msgpack_pack_inline_func(int32)(msgpack_pack_user x, int32_t d)
msgpack_pack_inline_func(_int32)(msgpack_pack_user x, int32_t d)
{
msgpack_pack_real_int32(x, d);
}
msgpack_pack_inline_func(int64)(msgpack_pack_user x, int64_t d)
msgpack_pack_inline_func(_int64)(msgpack_pack_user x, int64_t d)
{
msgpack_pack_real_int64(x, d);
}
@ -373,7 +373,7 @@ msgpack_pack_inline_func(int64)(msgpack_pack_user x, int64_t d)
#ifdef msgpack_pack_inline_func_cint
msgpack_pack_inline_func_cint(short)(msgpack_pack_user x, short d)
msgpack_pack_inline_func_cint(_short)(msgpack_pack_user x, short d)
{
#if defined(SIZEOF_SHORT) || defined(SHRT_MAX)
#if SIZEOF_SHORT == 2 || SHRT_MAX == 0x7fff
@ -394,7 +394,7 @@ if(sizeof(short) == 2) {
#endif
}
msgpack_pack_inline_func_cint(int)(msgpack_pack_user x, int d)
msgpack_pack_inline_func_cint(_int)(msgpack_pack_user x, int d)
{
#if defined(SIZEOF_INT) || defined(INT_MAX)
#if SIZEOF_INT == 2 || INT_MAX == 0x7fff
@ -415,7 +415,7 @@ if(sizeof(int) == 2) {
#endif
}
msgpack_pack_inline_func_cint(long)(msgpack_pack_user x, long d)
msgpack_pack_inline_func_cint(_long)(msgpack_pack_user x, long d)
{
#if defined(SIZEOF_LONG) || defined(LONG_MAX)
#if SIZEOF_LONG == 2 || LONG_MAX == 0x7fffL
@ -436,7 +436,7 @@ if(sizeof(long) == 2) {
#endif
}
msgpack_pack_inline_func_cint(long_long)(msgpack_pack_user x, long long d)
msgpack_pack_inline_func_cint(_long_long)(msgpack_pack_user x, long long d)
{
#if defined(SIZEOF_LONG_LONG) || defined(LLONG_MAX)
#if SIZEOF_LONG_LONG == 2 || LLONG_MAX == 0x7fffL
@ -457,7 +457,7 @@ if(sizeof(long long) == 2) {
#endif
}
msgpack_pack_inline_func_cint(unsigned_short)(msgpack_pack_user x, unsigned short d)
msgpack_pack_inline_func_cint(_unsigned_short)(msgpack_pack_user x, unsigned short d)
{
#if defined(SIZEOF_SHORT) || defined(USHRT_MAX)
#if SIZEOF_SHORT == 2 || USHRT_MAX == 0xffffU
@ -478,7 +478,7 @@ if(sizeof(unsigned short) == 2) {
#endif
}
msgpack_pack_inline_func_cint(unsigned_int)(msgpack_pack_user x, unsigned int d)
msgpack_pack_inline_func_cint(_unsigned_int)(msgpack_pack_user x, unsigned int d)
{
#if defined(SIZEOF_INT) || defined(UINT_MAX)
#if SIZEOF_INT == 2 || UINT_MAX == 0xffffU
@ -499,7 +499,7 @@ if(sizeof(unsigned int) == 2) {
#endif
}
msgpack_pack_inline_func_cint(unsigned_long)(msgpack_pack_user x, unsigned long d)
msgpack_pack_inline_func_cint(_unsigned_long)(msgpack_pack_user x, unsigned long d)
{
#if defined(SIZEOF_LONG) || defined(ULONG_MAX)
#if SIZEOF_LONG == 2 || ULONG_MAX == 0xffffUL
@ -520,7 +520,7 @@ if(sizeof(unsigned int) == 2) {
#endif
}
msgpack_pack_inline_func_cint(unsigned_long_long)(msgpack_pack_user x, unsigned long long d)
msgpack_pack_inline_func_cint(_unsigned_long_long)(msgpack_pack_user x, unsigned long long d)
{
#if defined(SIZEOF_LONG_LONG) || defined(ULLONG_MAX)
#if SIZEOF_LONG_LONG == 2 || ULLONG_MAX == 0xffffUL
@ -550,17 +550,19 @@ if(sizeof(unsigned long long) == 2) {
* Float
*/
msgpack_pack_inline_func(float)(msgpack_pack_user x, float d)
msgpack_pack_inline_func(_float)(msgpack_pack_user x, float d)
{
uint32_t n = *((uint32_t*)&d); // FIXME
const unsigned char buf[5] = {0xca, STORE_BE32(n)};
union { char buf[4]; uint32_t num; } f;
*((float*)&f.buf) = d; // FIXME
const unsigned char buf[5] = {0xca, STORE_BE32(f.num)};
msgpack_pack_append_buffer(x, buf, 5);
}
msgpack_pack_inline_func(double)(msgpack_pack_user x, double d)
msgpack_pack_inline_func(_double)(msgpack_pack_user x, double d)
{
uint64_t n = *((uint64_t*)&d); // FIXME
const unsigned char buf[9] = {0xcb, STORE_BE64(n)};
union { char buf[8]; uint64_t num; } f;
*((double*)&f.buf) = d; // FIXME
const unsigned char buf[9] = {0xcb, STORE_BE64(f.num)};
msgpack_pack_append_buffer(x, buf, 9);
}
@ -569,7 +571,7 @@ msgpack_pack_inline_func(double)(msgpack_pack_user x, double d)
* Nil
*/
msgpack_pack_inline_func(nil)(msgpack_pack_user x)
msgpack_pack_inline_func(_nil)(msgpack_pack_user x)
{
static const unsigned char d = 0xc0;
msgpack_pack_append_buffer(x, &d, 1);
@ -580,13 +582,13 @@ msgpack_pack_inline_func(nil)(msgpack_pack_user x)
* Boolean
*/
msgpack_pack_inline_func(true)(msgpack_pack_user x)
msgpack_pack_inline_func(_true)(msgpack_pack_user x)
{
static const unsigned char d = 0xc3;
msgpack_pack_append_buffer(x, &d, 1);
}
msgpack_pack_inline_func(false)(msgpack_pack_user x)
msgpack_pack_inline_func(_false)(msgpack_pack_user x)
{
static const unsigned char d = 0xc2;
msgpack_pack_append_buffer(x, &d, 1);
@ -597,7 +599,7 @@ msgpack_pack_inline_func(false)(msgpack_pack_user x)
* Array
*/
msgpack_pack_inline_func(array)(msgpack_pack_user x, unsigned int n)
msgpack_pack_inline_func(_array)(msgpack_pack_user x, unsigned int n)
{
if(n < 16) {
unsigned char d = 0x90 | n;
@ -618,7 +620,7 @@ msgpack_pack_inline_func(array)(msgpack_pack_user x, unsigned int n)
* Map
*/
msgpack_pack_inline_func(map)(msgpack_pack_user x, unsigned int n)
msgpack_pack_inline_func(_map)(msgpack_pack_user x, unsigned int n)
{
if(n < 16) {
unsigned char d = 0x80 | n;
@ -639,7 +641,7 @@ msgpack_pack_inline_func(map)(msgpack_pack_user x, unsigned int n)
* Raw
*/
msgpack_pack_inline_func(raw)(msgpack_pack_user x, size_t l)
msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l)
{
if(l < 32) {
unsigned char d = 0xa0 | l;
@ -655,7 +657,7 @@ msgpack_pack_inline_func(raw)(msgpack_pack_user x, size_t l)
}
}
msgpack_pack_inline_func(raw_body)(msgpack_pack_user x, const void* b, size_t l)
msgpack_pack_inline_func(_raw_body)(msgpack_pack_user x, const void* b, size_t l)
{
msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
}

View file

@ -1,7 +1,7 @@
/*
* MessagePack unpacking routine template
*
* Copyright (C) 2008 FURUHASHI Sadayuki
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View file

@ -1,7 +1,7 @@
/*
* MessagePack unpacking routine template
*
* Copyright (C) 2008 FURUHASHI Sadayuki
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,38 +41,38 @@
#endif
msgpack_unpack_struct_decl(stack) {
msgpack_unpack_struct_decl(_stack) {
msgpack_unpack_object obj;
size_t count;
unsigned int ct;
msgpack_unpack_object map_key;
};
msgpack_unpack_struct_decl(context) {
msgpack_unpack_struct_decl(_context) {
msgpack_unpack_user user; // must be first
unsigned int cs;
unsigned int trail;
unsigned int top;
msgpack_unpack_struct(stack) stack[MSGPACK_MAX_STACK_SIZE];
msgpack_unpack_struct(_stack) stack[MSGPACK_MAX_STACK_SIZE];
};
msgpack_unpack_func(void, init)(msgpack_unpack_struct(context)* ctx)
msgpack_unpack_func(void, _init)(msgpack_unpack_struct(_context)* ctx)
{
/*memset(ctx, 0, sizeof( msgpack_unpack_struct(context) )); FIXME needed? */
/*memset(ctx, 0, sizeof( msgpack_unpack_struct(_context) )); FIXME needed? */
ctx->cs = CS_HEADER;
ctx->trail = 0;
ctx->top = 0;
ctx->stack[0].obj = msgpack_unpack_callback(init)(&ctx->user);
ctx->stack[0].obj = msgpack_unpack_callback(_init)(&ctx->user);
}
msgpack_unpack_func(msgpack_unpack_object, data)(msgpack_unpack_struct(context)* unpacker)
msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context)* unpacker)
{
return (unpacker)->stack[0].obj;
}
msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const char* data, size_t len, size_t* off)
msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
{
assert(len >= *off);
@ -83,11 +83,11 @@ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const cha
unsigned int trail = ctx->trail;
unsigned int cs = ctx->cs;
unsigned int top = ctx->top;
msgpack_unpack_struct(stack)* stack = ctx->stack;
msgpack_unpack_struct(_stack)* stack = ctx->stack;
msgpack_unpack_user* user = &ctx->user;
msgpack_unpack_object obj;
msgpack_unpack_struct(stack)* c = NULL;
msgpack_unpack_struct(_stack)* c = NULL;
int ret;
@ -139,19 +139,19 @@ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const cha
case CS_HEADER:
switch(*p) {
case 0x00 ... 0x7f: // Positive Fixnum
push_fixed_value(uint8, *(uint8_t*)p);
push_fixed_value(_uint8, *(uint8_t*)p);
case 0xe0 ... 0xff: // Negative Fixnum
push_fixed_value(int8, *(int8_t*)p);
push_fixed_value(_int8, *(int8_t*)p);
case 0xc0 ... 0xdf: // Variable
switch(*p) {
case 0xc0: // nil
push_simple_value(nil);
push_simple_value(_nil);
//case 0xc1: // string
// again_terminal_trail(NEXT_CS(p), p+1);
case 0xc2: // false
push_simple_value(false);
push_simple_value(_false);
case 0xc3: // true
push_simple_value(true);
push_simple_value(_true);
//case 0xc4:
//case 0xc5:
//case 0xc6:
@ -188,9 +188,9 @@ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const cha
case 0xa0 ... 0xbf: // FixRaw
again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero);
case 0x90 ... 0x9f: // FixArray
start_container(array, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM);
start_container(_array, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM);
case 0x80 ... 0x8f: // FixMap
start_container(map, ((unsigned int)*p) & 0x0f, CT_MAP_KEY);
start_container(_map, ((unsigned int)*p) & 0x0f, CT_MAP_KEY);
default:
goto _failed;
@ -208,28 +208,30 @@ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const cha
//case CS_
//case CS_
case CS_FLOAT: {
uint32_t x = PTR_CAST_32(n); // FIXME
push_fixed_value(float, *((float*)&x)); }
union { uint32_t num; char buf[4]; } f;
f.num = PTR_CAST_32(n); // FIXME
push_fixed_value(_float, *((float*)f.buf)); }
case CS_DOUBLE: {
uint64_t x = PTR_CAST_64(n); // FIXME
push_fixed_value(double, *((double*)&x)); }
union { uint64_t num; char buf[8]; } f;
f.num = PTR_CAST_64(n); // FIXME
push_fixed_value(_double, *((double*)f.buf)); }
case CS_UINT_8:
push_fixed_value(uint8, (uint8_t)PTR_CAST_8(n));
push_fixed_value(_uint8, (uint8_t)PTR_CAST_8(n));
case CS_UINT_16:
push_fixed_value(uint16, (uint16_t)PTR_CAST_16(n));
push_fixed_value(_uint16, (uint16_t)PTR_CAST_16(n));
case CS_UINT_32:
push_fixed_value(uint32, (uint32_t)PTR_CAST_32(n));
push_fixed_value(_uint32, (uint32_t)PTR_CAST_32(n));
case CS_UINT_64:
push_fixed_value(uint64, (uint64_t)PTR_CAST_64(n));
push_fixed_value(_uint64, (uint64_t)PTR_CAST_64(n));
case CS_INT_8:
push_fixed_value(int8, (int8_t)PTR_CAST_8(n));
push_fixed_value(_int8, (int8_t)PTR_CAST_8(n));
case CS_INT_16:
push_fixed_value(int16, (int16_t)PTR_CAST_16(n));
push_fixed_value(_int16, (int16_t)PTR_CAST_16(n));
case CS_INT_32:
push_fixed_value(int32, (int32_t)PTR_CAST_32(n));
push_fixed_value(_int32, (int32_t)PTR_CAST_32(n));
case CS_INT_64:
push_fixed_value(int64, (int64_t)PTR_CAST_64(n));
push_fixed_value(_int64, (int64_t)PTR_CAST_64(n));
//case CS_
//case CS_
@ -240,7 +242,7 @@ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const cha
//case ACS_BIG_INT_VALUE:
//_big_int_zero:
// // FIXME
// push_variable_value(big_int, data, n, trail);
// push_variable_value(_big_int, data, n, trail);
//case CS_BIG_FLOAT_16:
// again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint16_t)PTR_CAST_16(n), _big_float_zero);
@ -249,7 +251,7 @@ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const cha
//case ACS_BIG_FLOAT_VALUE:
//_big_float_zero:
// // FIXME
// push_variable_value(big_float, data, n, trail);
// push_variable_value(_big_float, data, n, trail);
case CS_RAW_16:
again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint16_t)PTR_CAST_16(n), _raw_zero);
@ -257,17 +259,17 @@ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const cha
again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint32_t)PTR_CAST_32(n), _raw_zero);
case ACS_RAW_VALUE:
_raw_zero:
push_variable_value(raw, data, n, trail);
push_variable_value(_raw, data, n, trail);
case CS_ARRAY_16:
start_container(array, (uint16_t)PTR_CAST_16(n), CT_ARRAY_ITEM);
start_container(_array, (uint16_t)PTR_CAST_16(n), CT_ARRAY_ITEM);
case CS_ARRAY_32:
start_container(array, (uint32_t)PTR_CAST_32(n), CT_ARRAY_ITEM);
start_container(_array, (uint32_t)PTR_CAST_32(n), CT_ARRAY_ITEM);
case CS_MAP_16:
start_container(map, (uint16_t)PTR_CAST_16(n), CT_MAP_KEY);
start_container(_map, (uint16_t)PTR_CAST_16(n), CT_MAP_KEY);
case CS_MAP_32:
start_container(map, (uint32_t)PTR_CAST_32(n), CT_MAP_KEY);
start_container(_map, (uint32_t)PTR_CAST_32(n), CT_MAP_KEY);
default:
goto _failed;
@ -279,7 +281,7 @@ _push:
c = &stack[top-1];
switch(c->ct) {
case CT_ARRAY_ITEM:
msgpack_unpack_callback(array_item)(user, &c->obj, obj);
msgpack_unpack_callback(_array_item)(user, &c->obj, obj);
if(--c->count == 0) {
obj = c->obj;
--top;
@ -292,7 +294,7 @@ _push:
c->ct = CT_MAP_VALUE;
goto _header_again;
case CT_MAP_VALUE:
msgpack_unpack_callback(map_item)(user, &c->obj, c->map_key, obj);
msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj);
if(--c->count == 0) {
obj = c->obj;
--top;