* pack.c: use integer types with explicit size.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26774 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2010-02-27 15:39:20 +00:00
parent a971778b39
commit 68691ab2dd
2 changed files with 85 additions and 110 deletions

View File

@ -1,3 +1,7 @@
Sun Feb 28 00:38:18 2010 Tanaka Akira <akr@fsij.org>
* pack.c: use integer types with explicit size.
Sat Feb 27 15:54:55 2010 Tanaka Akira <akr@fsij.org> Sat Feb 27 15:54:55 2010 Tanaka Akira <akr@fsij.org>
* pack.c: check assuption on QUAD_SIZE and SIZEOF_LONG. * pack.c: check assuption on QUAD_SIZE and SIZEOF_LONG.

187
pack.c
View File

@ -49,6 +49,14 @@
# define NATINT_LEN(type,len) ((int)sizeof(type)) # define NATINT_LEN(type,len) ((int)sizeof(type))
#endif #endif
#if SIZEOF_LONG == 8
# define INT64toNUM(x) LONG2NUM(x)
# define UINT64toNUM(x) ULONG2NUM(x)
#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
# define INT64toNUM(x) LL2NUM(x)
# define UINT64toNUM(x) ULL2NUM(x)
#endif
#define define_swapx(x, xtype) \ #define define_swapx(x, xtype) \
static xtype \ static xtype \
TOKEN_PASTE(swap,x)(xtype z) \ TOKEN_PASTE(swap,x)(xtype z) \
@ -83,12 +91,8 @@ TOKEN_PASTE(swap,x)(xtype z) \
#endif #endif
#ifndef swap64 #ifndef swap64
# if SIZEOF_LONG == 8 # ifdef HAVE_INT64_T
# define byte_in_64bit(n) ((unsigned long)0xff << n) # define byte_in_64bit(n) ((uint64_t)0xff << n)
# elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
# define byte_in_64bit(n) ((LONG_LONG)0xff << n)
# endif
# ifdef byte_in_64bit
# define swap64(x) ((((x)&byte_in_64bit(0))<<56) \ # define swap64(x) ((((x)&byte_in_64bit(0))<<56) \
|(((x)>>56)&0xFF) \ |(((x)>>56)&0xFF) \
|(((x)&byte_in_64bit(8))<<40) \ |(((x)&byte_in_64bit(8))<<40) \
@ -700,49 +704,47 @@ pack_pack(VALUE ary, VALUE fmt)
pack_integer: pack_integer:
switch (integer_size) { switch (integer_size) {
case SIZEOF_SHORT: #ifdef HAVE_INT16_T
case SIZEOF_INT16_T:
while (len-- > 0) { while (len-- > 0) {
short s; int16_t v;
from = NEXTFROM; from = NEXTFROM;
s = (short)num2i32(from); v = (int16_t)num2i32(from);
if (bigendian_p != BIGENDIAN_P()) s = swaps(s); if (bigendian_p != BIGENDIAN_P()) v = swap16(v);
rb_str_buf_cat(res, (char *)&s, sizeof(short)); rb_str_buf_cat(res, (char *)&v, sizeof(int16_t));
}
break;
#if SIZEOF_SHORT != SIZEOF_INT
case SIZEOF_INT:
while (len-- > 0) {
int i;
from = NEXTFROM;
i = (int)num2i32(from);
if (bigendian_p != BIGENDIAN_P()) i = swapi(i);
rb_str_buf_cat(res, (char *)&i, sizeof(int));
} }
break; break;
#endif #endif
#if SIZEOF_INT != SIZEOF_LONG #ifdef HAVE_INT32_T
#if !defined(FORCE_BIG_PACK) || SIZEOF_LONG != QUAD_SIZE case SIZEOF_INT32_T:
case SIZEOF_LONG:
while (len-- > 0) { while (len-- > 0) {
long l; int32_t v;
from = NEXTFROM; from = NEXTFROM;
l = num2i32(from); v = (int32_t)num2i32(from);
if (bigendian_p != BIGENDIAN_P()) l = swapl(l); if (bigendian_p != BIGENDIAN_P()) v = swap32(v);
rb_str_buf_cat(res, (char *)&l, sizeof(long)); rb_str_buf_cat(res, (char *)&v, sizeof(int32_t));
} }
break; break;
#endif #endif
#endif
#if SIZEOF_LONG != QUAD_SIZE || defined(FORCE_BIG_PACK) #if defined(HAVE_INT64_T) && SIZEOF_LONG == SIZEOF_INT64_T && !defined(FORCE_BIG_PACK)
#if QUAD_SIZE % SIZEOF_LONG != 0 case SIZEOF_INT64_T:
while (len-- > 0) {
int64_t v;
from = NEXTFROM;
v = num2i32(from);
if (bigendian_p != BIGENDIAN_P()) v = swap64(v);
rb_str_buf_cat(res, (char *)&v, sizeof(int64_t));
}
break;
#else
# if QUAD_SIZE % SIZEOF_LONG != 0
# error unexpected QUAD_SIZE : SIZEOF_LONG ratio # error unexpected QUAD_SIZE : SIZEOF_LONG ratio
#endif # endif
case QUAD_SIZE: case QUAD_SIZE:
while (len-- > 0) { while (len-- > 0) {
unsigned long tmp[QUAD_SIZE/SIZEOF_LONG]; unsigned long tmp[QUAD_SIZE/SIZEOF_LONG];
@ -763,7 +765,7 @@ pack_pack(VALUE ary, VALUE fmt)
#endif #endif
default: default:
rb_bug("unexpected intger size for pack"); rb_bug("unexpected intger size for pack: %d", integer_size);
} }
break; break;
@ -1554,51 +1556,53 @@ pack_unpack(VALUE str, VALUE fmt)
unpack_integer: unpack_integer:
switch (integer_size) { switch (integer_size) {
case SIZEOF_SHORT: #ifdef HAVE_INT16_T
case SIZEOF_INT16_T:
if (signed_p) { if (signed_p) {
PACK_LENGTH_ADJUST_SIZE(sizeof(short)); PACK_LENGTH_ADJUST_SIZE(sizeof(int16_t));
while (len-- > 0) { while (len-- > 0) {
short tmp; int16_t tmp;
memcpy(&tmp, s, sizeof(short)); memcpy(&tmp, s, sizeof(int16_t));
if (bigendian_p != BIGENDIAN_P()) tmp = swaps(tmp); if (bigendian_p != BIGENDIAN_P()) tmp = swap16(tmp);
s += sizeof(short); s += sizeof(int16_t);
UNPACK_PUSH(INT2FIX(tmp)); UNPACK_PUSH(INT2FIX(tmp));
} }
PACK_ITEM_ADJUST(); PACK_ITEM_ADJUST();
} }
else { else {
PACK_LENGTH_ADJUST_SIZE(sizeof(unsigned short)); PACK_LENGTH_ADJUST_SIZE(sizeof(uint16_t));
while (len-- > 0) { while (len-- > 0) {
unsigned short tmp; uint16_t tmp;
memcpy(&tmp, s, sizeof(unsigned short)); memcpy(&tmp, s, sizeof(uint16_t));
if (bigendian_p != BIGENDIAN_P()) tmp = swaps(tmp); if (bigendian_p != BIGENDIAN_P()) tmp = swap16(tmp);
s += sizeof(unsigned short); s += sizeof(uint16_t);
UNPACK_PUSH(INT2FIX(tmp)); UNPACK_PUSH(INT2FIX(tmp));
} }
PACK_ITEM_ADJUST(); PACK_ITEM_ADJUST();
} }
break; break;
#endif
#if SIZEOF_SHORT != SIZEOF_INT #ifdef HAVE_INT32_T
case SIZEOF_INT: case SIZEOF_INT32_T:
if (signed_p) { if (signed_p) {
PACK_LENGTH_ADJUST_SIZE(sizeof(int)); PACK_LENGTH_ADJUST_SIZE(sizeof(int32_t));
while (len-- > 0) { while (len-- > 0) {
int tmp; int32_t tmp;
memcpy(&tmp, s, sizeof(int)); memcpy(&tmp, s, sizeof(int32_t));
if (bigendian_p != BIGENDIAN_P()) tmp = swapi(tmp); if (bigendian_p != BIGENDIAN_P()) tmp = swap32(tmp);
s += sizeof(int); s += sizeof(int32_t);
UNPACK_PUSH(INT2NUM(tmp)); UNPACK_PUSH(INT2NUM(tmp));
} }
PACK_ITEM_ADJUST(); PACK_ITEM_ADJUST();
} }
else { else {
PACK_LENGTH_ADJUST_SIZE(sizeof(unsigned int)); PACK_LENGTH_ADJUST_SIZE(sizeof(uint32_t));
while (len-- > 0) { while (len-- > 0) {
unsigned int tmp; uint32_t tmp;
memcpy(&tmp, s, sizeof(unsigned int)); memcpy(&tmp, s, sizeof(uint32_t));
if (bigendian_p != BIGENDIAN_P()) tmp = swapi(tmp); if (bigendian_p != BIGENDIAN_P()) tmp = swap32(tmp);
s += sizeof(unsigned int); s += sizeof(uint32_t);
UNPACK_PUSH(UINT2NUM(tmp)); UNPACK_PUSH(UINT2NUM(tmp));
} }
PACK_ITEM_ADJUST(); PACK_ITEM_ADJUST();
@ -1606,68 +1610,35 @@ pack_unpack(VALUE str, VALUE fmt)
break; break;
#endif #endif
#if SIZEOF_INT != SIZEOF_LONG #if defined(HAVE_INT64_T) && !defined(FORCE_BIG_PACK)
#if !defined(FORCE_BIG_PACK) || SIZEOF_LONG != QUAD_SIZE case SIZEOF_INT64_T:
case SIZEOF_LONG:
if (signed_p) { if (signed_p) {
PACK_LENGTH_ADJUST_SIZE(sizeof(long)); PACK_LENGTH_ADJUST_SIZE(sizeof(int64_t));
while (len-- > 0) { while (len-- > 0) {
long tmp; int64_t tmp;
memcpy(&tmp, s, sizeof(long)); memcpy(&tmp, s, sizeof(int64_t));
if (bigendian_p != BIGENDIAN_P()) tmp = swapl(tmp); if (bigendian_p != BIGENDIAN_P()) tmp = swap64(tmp);
s += sizeof(long); s += sizeof(int64_t);
UNPACK_PUSH(LONG2NUM(tmp)); UNPACK_PUSH(INT64toNUM(tmp));
} }
PACK_ITEM_ADJUST(); PACK_ITEM_ADJUST();
} }
else { else {
PACK_LENGTH_ADJUST_SIZE(sizeof(unsigned long)); PACK_LENGTH_ADJUST_SIZE(sizeof(uint64_t));
while (len-- > 0) { while (len-- > 0) {
unsigned long tmp; uint64_t tmp;
memcpy(&tmp, s, sizeof(unsigned long)); memcpy(&tmp, s, sizeof(uint64_t));
if (bigendian_p != BIGENDIAN_P()) tmp = swapl(tmp); if (bigendian_p != BIGENDIAN_P()) tmp = swap64(tmp);
s += sizeof(unsigned long); s += sizeof(uint64_t);
UNPACK_PUSH(ULONG2NUM(tmp)); UNPACK_PUSH(UINT64toNUM(tmp));
} }
PACK_ITEM_ADJUST(); PACK_ITEM_ADJUST();
} }
break; break;
#endif #else
#endif # if QUAD_SIZE % SIZEOF_LONG != 0
#if defined(HAVE_LONG_LONG) && SIZEOF_LONG != SIZEOF_LONG_LONG
#if !defined(FORCE_BIG_PACK) || SIZEOF_LONG_LONG != QUAD_SIZE
case SIZEOF_LONG_LONG:
if (signed_p) {
PACK_LENGTH_ADJUST_SIZE(sizeof(LONG_LONG));
while (len-- > 0) {
LONG_LONG tmp;
memcpy(&tmp, s, sizeof(LONG_LONG));
if (bigendian_p != BIGENDIAN_P()) tmp = swapll(tmp);
s += sizeof(LONG_LONG);
UNPACK_PUSH(LL2NUM(tmp));
}
PACK_ITEM_ADJUST();
}
else {
PACK_LENGTH_ADJUST_SIZE(sizeof(unsigned LONG_LONG));
while (len-- > 0) {
unsigned LONG_LONG tmp;
memcpy(&tmp, s, sizeof(unsigned LONG_LONG));
if (bigendian_p != BIGENDIAN_P()) tmp = swapll(tmp);
s += sizeof(unsigned LONG_LONG);
UNPACK_PUSH(ULL2NUM(tmp));
}
PACK_ITEM_ADJUST();
}
break;
#endif
#endif
#if (SIZEOF_LONG != QUAD_SIZE && (!defined(HAVE_LONG_LONG) || SIZEOF_LONG_LONG != QUAD_SIZE)) || defined(FORCE_BIG_PACK)
#if QUAD_SIZE % SIZEOF_LONG != 0
# error unexpected QUAD_SIZE : SIZEOF_LONG ratio # error unexpected QUAD_SIZE : SIZEOF_LONG ratio
#endif # endif
case QUAD_SIZE: case QUAD_SIZE:
if (bigendian_p != BIGENDIAN_P()) if (bigendian_p != BIGENDIAN_P())
rb_bug("unexpected endian for unpack"); rb_bug("unexpected endian for unpack");