summaryrefslogtreecommitdiff
path: root/pack.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-10-16 09:13:20 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-10-16 09:13:20 +0000
commit8353f303e3c1109e04ef47707df3b7d84b12c5fe (patch)
tree33bc2638db8c9dac5e94b0d6fa9af090c45774b8 /pack.c
parent525836d683d8c0cc2ff2479875eb9e71bd63d151 (diff)
matz
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1009 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'pack.c')
-rw-r--r--pack.c38
1 files changed, 29 insertions, 9 deletions
diff --git a/pack.c b/pack.c
index 7c60dde6c7..ee63ffd35c 100644
--- a/pack.c
+++ b/pack.c
@@ -22,16 +22,24 @@
#endif
#ifdef NATINT_PACK
+# define OFF16B(p) ((char*)(p) + (natint?0:(sizeof(short) - SIZE16)))
+# define OFF32B(p) ((char*)(p) + (natint?0:(sizeof(long) - SIZE32)))
+# define NATINT_I32(x) (natint?sizeof(NUM2LONG(x)):(NUM2I32(x)))
+# define NATINT_U32(x) (natint?sizeof(NUM2ULONG(x)):(NUM2U32(x)))
# define NATINT_LEN(type,len) (natint?sizeof(type):(len))
# ifdef WORDS_BIGENDIAN
-# define OFF16(p) ((char*)(p) + (natint?0:(sizeof(short) - 2)))
-# define OFF32(p) ((char*)(p) + (natint?0:(sizeof(long) - 4)))
+# define OFF16(p) OFF16B(p)
+# define OFF32(p) OFF32B(p)
# endif
#else
+# define NATINT_I32(x) NUM2I32(x)
+# define NATINT_U32(x) NUM2U32(x)
# define NATINT_LEN(type,len) sizeof(type)
#endif
#ifndef OFF16
+# define OFF16B(p) (char*)(p)
+# define OFF32B(p) (char*)(p)
# define OFF16(p) (char*)(p)
# define OFF32(p) (char*)(p)
#endif
@@ -293,6 +301,18 @@ endian()
#define VTOHD(x,y) vtohd(x)
#endif
+#if SIZEOF_LONG == SIZE32
+typedef long I32;
+typedef unsigned long U32;
+#define NUM2I32(x) NUM2LONG(x)
+#define NUM2U32(x) NUM2LONG(x)
+#elif SIZEOF_INT == SIZE32
+typedef int I32;
+typedef unsigned int U32;
+#define NUM2I32(x) NUM2INT(x)
+#define NUM2U32(x) NUM2UINT(x)
+#endif
+
static char *toofew = "too few arguments";
static void encodes _((VALUE,char*,int,int));
@@ -569,7 +589,7 @@ pack_pack(ary, fmt)
from = NEXTFROM;
if (NIL_P(from)) l = 0;
else {
- l = NUM2ULONG(from);
+ l = NATINT_U32(from);
}
rb_str_cat(res, OFF32(&l), NATINT_LEN(long,4));
}
@@ -585,7 +605,7 @@ pack_pack(ary, fmt)
s = NUM2INT(from);
}
s = htons(s);
- rb_str_cat(res, OFF16(&s), NATINT_LEN(short,2));
+ rb_str_cat(res, OFF16B(&s), NATINT_LEN(short,2));
}
break;
@@ -596,10 +616,10 @@ pack_pack(ary, fmt)
from = NEXTFROM;
if (NIL_P(from)) l = 0;
else {
- l = NUM2ULONG(from);
+ l = NATINT_U32(from);
}
l = htonl(l);
- rb_str_cat(res, OFF32(&l), NATINT_LEN(long,4));
+ rb_str_cat(res, OFF32B(&l), NATINT_LEN(long,4));
}
break;
@@ -624,7 +644,7 @@ pack_pack(ary, fmt)
from = NEXTFROM;
if (NIL_P(from)) l = 0;
else {
- l = NUM2ULONG(from);
+ l = NATINT_U32(from);
}
l = htovl(l);
rb_str_cat(res, OFF32(&l), NATINT_LEN(long,4));
@@ -1315,7 +1335,7 @@ pack_unpack(str, fmt)
PACK_LENGTH_ADJUST(unsigned short,2);
while (len-- > 0) {
unsigned short tmp = 0;
- memcpy(OFF16(&tmp), s, NATINT_LEN(unsigned short,2));
+ memcpy(OFF16B(&tmp), s, NATINT_LEN(unsigned short,2));
s += NATINT_LEN(unsigned short,2);
rb_ary_push(ary, rb_uint2inum(ntohs(tmp)));
}
@@ -1326,7 +1346,7 @@ pack_unpack(str, fmt)
PACK_LENGTH_ADJUST(unsigned long,4);
while (len-- > 0) {
unsigned long tmp = 0;
- memcpy(OFF32(&tmp), s, NATINT_LEN(unsigned long,4));
+ memcpy(OFF32B(&tmp), s, NATINT_LEN(unsigned long,4));
s += NATINT_LEN(unsigned long,4);
rb_ary_push(ary, rb_uint2inum(ntohl(tmp)));
}