summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/pack.c19
-rw-r--r--test/pack.rb24
2 files changed, 43 insertions, 0 deletions
diff --git a/src/pack.c b/src/pack.c
index 69cf740ff..fa8b5ef66 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -650,6 +650,7 @@ read_tmpl(mrb_state *mrb, struct tmpl *tmpl, int *dirp, int *typep, int *sizep,
tlen = RSTRING_LEN(tmpl->str);
t = tptr[tmpl->idx++];
+alias:
switch (t) {
case 'A':
dir = PACK_DIR_STR;
@@ -718,6 +719,24 @@ read_tmpl(mrb_state *mrb, struct tmpl *tmpl, int *dirp, int *typep, int *sizep,
type = PACK_TYPE_STRING;
flags |= PACK_FLAG_COUNT2 | PACK_FLAG_LSB;
break;
+ case 'I':
+ switch (sizeof(int)) {
+ case 2: t = 'S'; goto alias;
+ case 4: t = 'L'; goto alias;
+ case 8: t = 'Q'; goto alias;
+ default:
+ mrb_raisef(mrb, E_RUNTIME_ERROR, "mruby-pack does not support sizeof(int) == %S", mrb_fixnum_value(sizeof(int)));
+ }
+ break;
+ case 'i':
+ switch (sizeof(int)) {
+ case 2: t = 's'; goto alias;
+ case 4: t = 'l'; goto alias;
+ case 8: t = 'q'; goto alias;
+ default:
+ mrb_raisef(mrb, E_RUNTIME_ERROR, "mruby-pack does not support sizeof(int) == %S", mrb_fixnum_value(sizeof(int)));
+ }
+ break;
case 'L':
dir = PACK_DIR_LONG;
type = PACK_TYPE_INTEGER;
diff --git a/test/pack.rb b/test/pack.rb
index c7899e2d7..5e9932f4f 100644
--- a/test/pack.rb
+++ b/test/pack.rb
@@ -121,3 +121,27 @@ assert 'pack double' do
assert_pack 'D', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
end
end
+
+assert 'pack/unpack "i"' do
+ int_size = [0].pack('i').size
+ raise "pack('i').size is too small (#{int_size})" if int_size < 2
+
+ if PACK_IS_LITTLE_ENDIAN
+ str = "\xC7\xCF" + "\xFF" * (int_size-2)
+ else
+ str = "\xFF" * (int_size-2) + "\xC7\xCF"
+ end
+ assert_pack 'i', str, [-12345]
+end
+
+assert 'pack/unpack "I"' do
+ uint_size = [0].pack('I').size
+ raise "pack('I').size is too small (#{uint_size})" if uint_size < 2
+
+ if PACK_IS_LITTLE_ENDIAN
+ str = "\x39\x30" + "\0" * (uint_size-2)
+ else
+ str = "\0" * (uint_size-2) + "\x39\x30"
+ end
+ assert_pack 'I', str, [12345]
+end