From 8f3444c08141520a0977adce643ce0eb7f0324cd Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 15 Feb 2009 09:10:00 +0000 Subject: [PATCH] ruby binding: fix Fixnum serialization bug on x86_64 git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@88 5a5092ae-2292-43ba-b2d5-dcab9c1a2731 --- configure.in | 2 +- msgpack/pack_template.h | 141 +++++++++++++++++++++++++++++--- ruby/gem/lib/msgpack/version.rb | 2 +- ruby/pack.c | 6 +- 4 files changed, 134 insertions(+), 17 deletions(-) diff --git a/configure.in b/configure.in index bd348851..63762b0c 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ AC_INIT(msgpack/unpack_template.h) -AM_INIT_AUTOMAKE(msgpack, 0.2.0) +AM_INIT_AUTOMAKE(msgpack, 0.2.1) AC_CONFIG_HEADER(config.h) AC_PROG_LIBTOOL diff --git a/msgpack/pack_template.h b/msgpack/pack_template.h index 1dbf6fd0..04f8c983 100644 --- a/msgpack/pack_template.h +++ b/msgpack/pack_template.h @@ -68,15 +68,14 @@ * Integer */ -// wrapper -msgpack_pack_inline_func(int)(msgpack_pack_user x, int d) +static inline void msgpack_pack_compress_int32(msgpack_pack_user x, uint32_t d) { - if(d < -32) { - if(d < -32768) { + if(d < -(1<<5)) { + if(d < -(1<<15)) { // signed 32 const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; msgpack_pack_append_buffer(x, buf, 5); - } else if(d < -128) { + } else if(d < -(1<<7)) { // signed 16 const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; msgpack_pack_append_buffer(x, buf, 3); @@ -85,15 +84,15 @@ msgpack_pack_inline_func(int)(msgpack_pack_user x, int d) const unsigned char buf[2] = {0xd0, (uint8_t)d}; msgpack_pack_append_buffer(x, buf, 2); } - } else if(d < 128) { + } else if(d < (1<<7)) { // fixnum msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); } else { - if(d < 256) { + if(d < (1<<8)) { // unsigned 8 const unsigned char buf[2] = {0xcc, (uint8_t)d}; msgpack_pack_append_buffer(x, buf, 2); - } else if(d < 65536) { + } else if(d < (1<<16)) { // unsigned 16 const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; msgpack_pack_append_buffer(x, buf, 3); @@ -105,11 +104,10 @@ msgpack_pack_inline_func(int)(msgpack_pack_user x, int d) } } -// wrapper -msgpack_pack_inline_func(unsigned_int)(msgpack_pack_user x, unsigned int d) +static inline void msgpack_pack_compress_uint32(msgpack_pack_user x, uint32_t d) { - if(d < 256) { - if(d < 128) { + if(d < (1<<8)) { + if(d < (1<<7)) { // fixnum msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); } else { @@ -118,7 +116,7 @@ msgpack_pack_inline_func(unsigned_int)(msgpack_pack_user x, unsigned int d) msgpack_pack_append_buffer(x, buf, 2); } } else { - if(d < 65536) { + if(d < (1<<16)) { // unsigned 16 const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; msgpack_pack_append_buffer(x, buf, 3); @@ -130,6 +128,123 @@ msgpack_pack_inline_func(unsigned_int)(msgpack_pack_user x, unsigned int d) } } +static inline void msgpack_pack_compress_int64(msgpack_pack_user x, int64_t d) +{ + if(d < -(1LL<<5)) { + if(d < -(1LL<<15)) { + if(d < -(1LL<<31)) { + // signed 64 + const unsigned char buf[9] = {0xd3, STORE_BE64(d)}; + msgpack_pack_append_buffer(x, buf, 9); + } else { + // signed 32 + const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; + msgpack_pack_append_buffer(x, buf, 5); + } + } else { + if(d < -(1<<7)) { + // signed 16 + const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; + msgpack_pack_append_buffer(x, buf, 3); + } else { + // signed 8 + const unsigned char buf[2] = {0xd0, (uint8_t)d}; + msgpack_pack_append_buffer(x, buf, 2); + } + } + } else if(d < (1<<7)) { + // fixnum + msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); + } else { + if(d < (1LL<<16)) { + if(d < (1<<8)) { + // unsigned 8 + const unsigned char buf[2] = {0xcc, (uint8_t)d}; + msgpack_pack_append_buffer(x, buf, 2); + } else { + // unsigned 16 + const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; + msgpack_pack_append_buffer(x, buf, 3); + } + } else { + if(d < (1LL<<32)) { + // unsigned 32 + const unsigned char buf[5] = {0xce, STORE_BE32(d)}; + msgpack_pack_append_buffer(x, buf, 5); + } else { + // unsigned 64 + const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; + msgpack_pack_append_buffer(x, buf, 9); + } + } + } +} + +static inline void msgpack_pack_compress_uint64(msgpack_pack_user x, uint64_t d) +{ + if(d < (1ULL<<8)) { + if(d < (1<<7)) { + // fixnum + msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); + } else { + // unsigned 8 + const unsigned char buf[2] = {0xcc, (uint8_t)d}; + msgpack_pack_append_buffer(x, buf, 2); + } + } else { + if(d < (1ULL<<16)) { + // signed 16 + const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; + msgpack_pack_append_buffer(x, buf, 3); + } else if(d < (1ULL<<32)) { + // signed 32 + const unsigned char buf[5] = {0xce, STORE_BE32(d)}; + msgpack_pack_append_buffer(x, buf, 5); + } else { + // signed 64 + const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; + msgpack_pack_append_buffer(x, buf, 9); + } + } +} + +msgpack_pack_inline_func(int)(msgpack_pack_user x, int d) +{ +#if SIZEOF_INT == 8 + msgpack_pack_compress_int64(x, d); +#else + msgpack_pack_compress_int32(x, d); +#endif +} + +msgpack_pack_inline_func(unsigned_int)(msgpack_pack_user x, unsigned int d) +{ +#if SIZEOF_INT == 8 + msgpack_pack_compress_uint64(x, d); +#else + msgpack_pack_compress_uint32(x, d); +#endif +} + +msgpack_pack_inline_func(long)(msgpack_pack_user x, long d) +{ +#if SIZEOF_LONG == 8 + msgpack_pack_compress_int64(x, d); +#else + msgpack_pack_compress_int32(x, d); +#endif +} + +msgpack_pack_inline_func(unsigned_long)(msgpack_pack_user x, unsigned long d) +{ +#if SIZEOF_LONG == 8 + msgpack_pack_compress_uint64(x, d); +#else + msgpack_pack_compress_uint32(x, d); +#endif +} + + msgpack_pack_inline_func(uint8)(msgpack_pack_user x, uint8_t d) { if(d < 128) { diff --git a/ruby/gem/lib/msgpack/version.rb b/ruby/gem/lib/msgpack/version.rb index b2a5db65..229c7462 100644 --- a/ruby/gem/lib/msgpack/version.rb +++ b/ruby/gem/lib/msgpack/version.rb @@ -2,7 +2,7 @@ module MessagePack module VERSION #:nodoc: MAJOR = 0 MINOR = 2 - TINY = 0 + TINY = 1 STRING = [MAJOR, MINOR, TINY].join('.') end diff --git a/ruby/pack.c b/ruby/pack.c index e62419d7..5262024d 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -20,7 +20,7 @@ #include #define msgpack_pack_inline_func(name) \ - static void msgpack_pack_##name + static inline void msgpack_pack_##name #define msgpack_pack_user VALUE @@ -30,6 +30,8 @@ /* static void msgpack_pack_int(VALUE x, int d); static void msgpack_pack_unsigned_int(VALUE x, unsigned int d); +static void msgpack_pack_long(VALUE x, long d); +static void msgpack_pack_unsigned_long(VALUE x, unsigned long d); static void msgpack_pack_uint8(VALUE x, uint8_t d); static void msgpack_pack_uint16(VALUE x, uint16_t d); static void msgpack_pack_uint32(VALUE x, uint32_t d); @@ -93,7 +95,7 @@ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - msgpack_pack_int(out, FIX2INT(self)); + msgpack_pack_long(out, FIX2LONG(self)); return out; }