diff options
| author | Hiroshi Mimaki <[email protected]> | 2019-10-18 14:46:03 +0900 |
|---|---|---|
| committer | Hiroshi Mimaki <[email protected]> | 2019-10-18 14:46:03 +0900 |
| commit | b6546835457d1935a9c77965686b2a1256874d96 (patch) | |
| tree | 724cfd71a7c956b0648e8c58f3717d797fff5f29 /mrbgems/mruby-pack | |
| parent | 8ee516436b8d174a50764939bee23a442aa00b3f (diff) | |
| parent | 20d01f118ddb7e7f2f36926a7a3db35573611857 (diff) | |
| download | mruby-b6546835457d1935a9c77965686b2a1256874d96.tar.gz mruby-b6546835457d1935a9c77965686b2a1256874d96.zip | |
Merge master.
Diffstat (limited to 'mrbgems/mruby-pack')
| -rw-r--r-- | mrbgems/mruby-pack/.gitignore | 5 | ||||
| -rw-r--r-- | mrbgems/mruby-pack/.travis.yml | 2 | ||||
| -rw-r--r-- | mrbgems/mruby-pack/packtest.rb | 157 | ||||
| -rw-r--r-- | mrbgems/mruby-pack/run_test.rb | 26 | ||||
| -rw-r--r-- | mrbgems/mruby-pack/src/pack.c | 51 | ||||
| -rw-r--r-- | mrbgems/mruby-pack/test/pack.rb | 92 |
6 files changed, 71 insertions, 262 deletions
diff --git a/mrbgems/mruby-pack/.gitignore b/mrbgems/mruby-pack/.gitignore deleted file mode 100644 index 55ef3162f..000000000 --- a/mrbgems/mruby-pack/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -gem_* -gem-* -mrb-*.a -src/*.o -/tmp diff --git a/mrbgems/mruby-pack/.travis.yml b/mrbgems/mruby-pack/.travis.yml deleted file mode 100644 index ffe227284..000000000 --- a/mrbgems/mruby-pack/.travis.yml +++ /dev/null @@ -1,2 +0,0 @@ -script: - - "ruby run_test.rb all test" diff --git a/mrbgems/mruby-pack/packtest.rb b/mrbgems/mruby-pack/packtest.rb deleted file mode 100644 index 459447af7..000000000 --- a/mrbgems/mruby-pack/packtest.rb +++ /dev/null @@ -1,157 +0,0 @@ -# encoding: ascii - -# a = Array, s = String, t = Template - -def packtest(a, s, t) - begin - r = a.pack(t) - return if r == s - puts "#{a.inspect}.pack(#{t.inspect}) -> #{r.inspect} should be #{s.inspect}" - rescue => r - unless r.is_a? s - puts "#{a.inspect}.pack(#{t.inspect}) -> #{r.inspect} should be #{s.inspect}" - end - end -end - -def unpacktest(a, s, t) - r = s.unpack(t) - return if r == a - puts "#{s.inspect}.unpack(#{t.inspect}) -> #{r.inspect} should be #{a.inspect}" -end - -def pptest(a, s, t) - packtest(a, s, t) - unpacktest(a, s, t) -end - -pptest [1], "\x01", "C" - -packtest [1.1], "\x01", "C" -packtest [-1], "\xff", "C" -packtest [1,2], "\x01\x02", "C2" -#packtest [1], "X", ArgumentError - -unpacktest [48, nil], "0", "CC" -unpacktest [160, -96], "\xa0\xa0", "Cc" -unpacktest [49, 50, 51], "123", "C*" - -pptest [12849], "12", "S" -unpacktest [nil], "0", "S" -unpacktest [12849, nil], "123", "SS" -unpacktest [12849], "123", "S*" - -pptest [10000], "\x27\x10", "s>" -pptest [-10000], "\xd8\xf0", "s>" -pptest [50000], "\xc3\x50", "S>" - -pptest [10000], "\x10\x27", "s<" -pptest [-10000], "\xf0\xd8", "s<" -pptest [50000], "\x50\xc3", "S<" - -pptest [1000000000], "\x3b\x9a\xca\x00", "l>" -pptest [-1000000000], "\xc4\x65\x36\x00", "l>" - -pptest [1], "\x01\x00\x00\x00", "L<" -pptest [258], "\x02\x01\x00\x00", "L<" -pptest [66051], "\x03\x02\x01\x00", "L<" -pptest [16909060], "\x04\x03\x02\x01", "L<" -pptest [16909060], "\x01\x02\x03\x04", "L>" - -packtest [-1], "\xff\xff\xff\xff", "L<" - -pptest [1000000000], "\x00\x00\x00\x00\x3b\x9a\xca\x00", "q>" -pptest [-1000000000], "\xff\xff\xff\xff\xc4\x65\x36\x00", "q>" - -if (2**33).is_a? Fixnum - pptest [81985529216486895], "\x01\x23\x45\x67\x89\xab\xcd\xef", "q>" - pptest [-1167088121787636991], "\x01\x23\x45\x67\x89\xab\xcd\xef", "q<" -end - -pptest [16909060], "\x01\x02\x03\x04", "N" -pptest [258], "\x01\x02", "n" -pptest [32769], "\x80\x01", "n" - -pptest [16909060], "\x04\x03\x02\x01", "V" -pptest [258], "\x02\x01", "v" - -packtest [""], "", "m" -packtest ["a"], "YQ==\n", "m" -packtest ["ab"], "YWI=\n", "m" -packtest ["abc"], "YWJj\n", "m" -packtest ["abcd"], "YWJjZA==\n", "m" - -unpacktest [""], "", "m" -unpacktest ["a"], "YQ==\n", "m" -unpacktest ["ab"], "YWI=\n", "m" -unpacktest ["abc"], "YWJj\n", "m" -unpacktest ["abcd"], "YWJjZA==\n", "m" - -packtest [""], "\0", "H" -packtest ["3"], "0", "H" -packtest ["34"], "", "H0" -packtest ["34"], "0", "H" -packtest ["34"], "4", "H2" -packtest ["34"], "4\0", "H3" -packtest ["3456"], "4P", "H3" -packtest ["34563"], "4V0", "H*" -packtest ["5a"], "Z", "H*" -packtest ["5A"], "Z", "H*" - -unpacktest [""], "", "H" -unpacktest [""], "0", "H0" -unpacktest ["3"], "0", "H" -unpacktest ["30"], "0", "H2" -unpacktest ["30"], "0", "H3" -unpacktest ["303"], "01", "H3" -unpacktest ["303132"], "012", "H*" -unpacktest ["3031", 50], "012", "H4C" -unpacktest ["5a"], "Z", "H*" - -packtest [""], "\0", "h" -packtest ["3"], "\03", "h" -packtest ["34"], "", "h0" -packtest ["34"], "\03", "h" -packtest ["34"], "C", "h2" -packtest ["34"], "C\0", "h3" -packtest ["3456"], "C\05", "h3" -packtest ["34563"], "Ce\03", "h*" - -packtest [""], " ", "A" -unpacktest [""], "", "A" -pptest ["1"], "1", "A" -pptest ["1"], "1 ", "A2" -unpacktest ["1"], "1", "A2" -unpacktest ["1"], "1 ", "A2" -unpacktest ["1"], "1\0", "A2" -packtest ["12"], "1", "A" -unpacktest ["1"], "12", "A" -pptest ["123"], "123", "A*" -packtest ["1","2"], "2", "A0A" -unpacktest ["","2"], "2", "A0A" - -packtest [""], "\0", "a" -unpacktest [""], "", "a" -pptest ["1"], "1", "a" -pptest ["1 "], "1 ", "a2" -pptest ["1\0"], "1\0", "a2" -packtest ["1"], "1\0", "a2" -pptest ["123"], "123", "a*" - -packtest [""], "\0", "Z" -unpacktest [""], "", "Z" -pptest ["1"], "1", "Z" -pptest ["1"], "1\0", "Z2" -pptest ["1 "], "1 ", "Z2" -pptest ["123"], "123\0", "Z*" -pptest ["1","2"], "12", "ZZ" -pptest ["1","2"], "1\0002", "Z*Z" -unpacktest ["1","3"], "1\00023", "Z3Z" - -packtest [1, 2], "\x01\x02", "CyC" - -packtest [65], "A", 'U' -packtest [59411], "\xEE\xA0\x93", 'U' - -pptest [1], "\x00\x01", "xC" -unpacktest [2], "\xcc\x02", "xC" diff --git a/mrbgems/mruby-pack/run_test.rb b/mrbgems/mruby-pack/run_test.rb deleted file mode 100644 index d9566a2a6..000000000 --- a/mrbgems/mruby-pack/run_test.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env ruby -# -# mrbgems test runner -# - -gemname = File.basename(File.dirname(File.expand_path __FILE__)) - -if __FILE__ == $0 - repository, dir = 'https://github.com/mruby/mruby.git', 'tmp/mruby' - build_args = ARGV - build_args = ['all', 'test'] if build_args.nil? or build_args.empty? - - Dir.mkdir 'tmp' unless File.exist?('tmp') - unless File.exist?(dir) - system "git clone #{repository} #{dir}" - end - - exit system(%Q[cd #{dir}; MRUBY_CONFIG=#{File.expand_path __FILE__} ruby minirake #{build_args.join(' ')}]) -end - -MRuby::Build.new do |conf| - toolchain :gcc - conf.gembox 'default' - - conf.gem File.expand_path(File.dirname(__FILE__)) -end diff --git a/mrbgems/mruby-pack/src/pack.c b/mrbgems/mruby-pack/src/pack.c index ac29fdbf3..73b6ce635 100644 --- a/mrbgems/mruby-pack/src/pack.c +++ b/mrbgems/mruby-pack/src/pack.c @@ -3,7 +3,7 @@ */ #include "mruby.h" -#include "error.h" +#include "mruby/error.h" #include "mruby/array.h" #include "mruby/class.h" #include "mruby/numeric.h" @@ -118,9 +118,9 @@ make_base64_dec_tab(void) base64_dec_tab['a' + i] = i + 26; for (i = 0; i < 10; i++) base64_dec_tab['0' + i] = i + 52; - base64_dec_tab['+'] = 62; - base64_dec_tab['/'] = 63; - base64_dec_tab['='] = PACK_BASE64_PADDING; + base64_dec_tab['+'+0] = 62; + base64_dec_tab['/'+0] = 63; + base64_dec_tab['='+0] = PACK_BASE64_PADDING; } static mrb_value @@ -237,7 +237,7 @@ unpack_l(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary, un int32_t sl = ul; #ifndef MRB_INT64 if (!FIXABLE(sl)) { - snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %ld", (long)sl); + snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %" PRId32, sl); mrb_raise(mrb, E_RANGE_ERROR, msg); } #endif @@ -245,7 +245,7 @@ unpack_l(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary, un } else { #ifndef MRB_INT64 if (!POSFIXABLE(ul)) { - snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %lu", (unsigned long)ul); + snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %" PRIu32, ul); mrb_raise(mrb, E_RANGE_ERROR, msg); } #endif @@ -307,13 +307,13 @@ unpack_q(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary, un if (flags & PACK_FLAG_SIGNED) { int64_t sll = ull; if (!FIXABLE(sll)) { - snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %lld", (long long)sll); + snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %" PRId64, sll); mrb_raise(mrb, E_RANGE_ERROR, msg); } n = sll; } else { if (!POSFIXABLE(ull)) { - snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %llu", (unsigned long long)ull); + snprintf(msg, sizeof(msg), "cannot unpack to Fixnum: %" PRIu64, ull); mrb_raise(mrb, E_RANGE_ERROR, msg); } n = ull; @@ -529,8 +529,8 @@ utf8_to_uv(mrb_state *mrb, const char *p, long *lenp) mrb_raise(mrb, E_ARGUMENT_ERROR, "malformed UTF-8 character"); } if (n > *lenp) { - mrb_raisef(mrb, E_ARGUMENT_ERROR, "malformed UTF-8 character (expected %S bytes, given %S bytes)", - mrb_fixnum_value(n), mrb_fixnum_value(*lenp)); + mrb_raisef(mrb, E_ARGUMENT_ERROR, "malformed UTF-8 character (expected %d bytes, given %d bytes)", + n, *lenp); } *lenp = n--; if (n != 0) { @@ -976,7 +976,7 @@ 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))); + mrb_raisef(mrb, E_RUNTIME_ERROR, "mruby-pack does not support sizeof(int) == %d", (int)sizeof(int)); } break; case 'i': @@ -985,7 +985,7 @@ 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))); + mrb_raisef(mrb, E_RUNTIME_ERROR, "mruby-pack does not support sizeof(int) == %d", (int)sizeof(int)); } break; case 'L': @@ -1002,7 +1002,7 @@ alias: case 'm': dir = PACK_DIR_BASE64; type = PACK_TYPE_STRING; - flags |= PACK_FLAG_WIDTH; + flags |= PACK_FLAG_WIDTH | PACK_FLAG_COUNT2; break; case 'N': /* = "L>" */ dir = PACK_DIR_LONG; @@ -1075,18 +1075,18 @@ alias: if (ISDIGIT(ch)) { count = ch - '0'; while (tmpl->idx < tlen && ISDIGIT(tptr[tmpl->idx])) { - count = count * 10 + (tptr[tmpl->idx++] - '0'); - if (count < 0) { + int ch = tptr[tmpl->idx++] - '0'; + if (count+ch > INT_MAX/10) { mrb_raise(mrb, E_RUNTIME_ERROR, "too big template length"); } + count = count * 10 + ch; } continue; /* special case */ } else if (ch == '*') { count = -1; } else if (ch == '_' || ch == '!' || ch == '<' || ch == '>') { if (strchr("sSiIlLqQ", (int)t) == NULL) { - char ch_str = (char)ch; - mrb_raisef(mrb, E_ARGUMENT_ERROR, "'%S' allowed only after types sSiIlLqQ", mrb_str_new(mrb, &ch_str, 1)); + mrb_raisef(mrb, E_ARGUMENT_ERROR, "'%c' allowed only after types sSiIlLqQ", ch); } if (ch == '_' || ch == '!') { flags |= PACK_FLAG_s; @@ -1155,7 +1155,7 @@ mrb_pack_pack(mrb_state *mrb, mrb_value ary) #endif else if (type == PACK_TYPE_STRING) { if (!mrb_string_p(o)) { - mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into String", mrb_class_path(mrb, mrb_obj_class(mrb, o))); + mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %T into String", o); } } @@ -1195,7 +1195,7 @@ mrb_pack_pack(mrb_state *mrb, mrb_value ary) default: break; } - if (dir == PACK_DIR_STR) { /* always consumes 1 entry */ + if (dir == PACK_DIR_STR || dir == PACK_DIR_BASE64) { /* always consumes 1 entry */ aidx++; break; } @@ -1248,6 +1248,9 @@ pack_unpack(mrb_state *mrb, mrb_value str, int single) case PACK_DIR_STR: srcidx += unpack_a(mrb, sptr, srclen - srcidx, result, count, flags); break; + case PACK_DIR_BASE64: + srcidx += unpack_m(mrb, sptr, srclen - srcidx, result, flags); + break; } continue; } @@ -1274,9 +1277,6 @@ pack_unpack(mrb_state *mrb, mrb_value str, int single) case PACK_DIR_QUAD: srcidx += unpack_q(mrb, sptr, srclen - srcidx, result, flags); break; - case PACK_DIR_BASE64: - srcidx += unpack_m(mrb, sptr, srclen - srcidx, result, flags); - break; #ifndef MRB_WITHOUT_FLOAT case PACK_DIR_FLOAT: srcidx += unpack_float(mrb, sptr, srclen - srcidx, result, flags); @@ -1298,7 +1298,12 @@ pack_unpack(mrb_state *mrb, mrb_value str, int single) if (single) break; } - if (single) return RARRAY_PTR(result)[0]; + if (single) { + if (RARRAY_LEN(result) > 0) { + return RARRAY_PTR(result)[0]; + } + return mrb_nil_value(); + } return result; } diff --git a/mrbgems/mruby-pack/test/pack.rb b/mrbgems/mruby-pack/test/pack.rb index 1788c2b72..6832adcc7 100644 --- a/mrbgems/mruby-pack/test/pack.rb +++ b/mrbgems/mruby-pack/test/pack.rb @@ -1,101 +1,95 @@ +PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01 + +def assert_pack tmpl, packed, unpacked + t = tmpl.inspect + assert "assert_pack" do + assert_equal packed, unpacked.pack(tmpl), "#{unpacked.inspect}.pack(#{t})" + assert_equal unpacked, packed.unpack(tmpl), "#{packed.inspect}.unpack(#{t})" + end +end + # pack & unpack 'm' (base64) assert('[""].pack("m")') do - ary = "" - str = "" - [ary].pack("m") == str and - str.unpack("m") == [ary] + assert_pack "m", "", [""] end assert('["\0"].pack("m")') do - ary = "\0" - str = "AA==\n" - [ary].pack("m") == str and - str.unpack("m") == [ary] + assert_pack "m", "AA==\n", ["\0"] end assert('["\0\0"].pack("m")') do - ary = "\0\0" - str = "AAA=\n" - [ary].pack("m") == str and - str.unpack("m") == [ary] + assert_pack "m", "AAA=\n", ["\0\0"] end assert('["\0\0\0"].pack("m")') do - ary = "\0\0\0" - str = "AAAA\n" - [ary].pack("m") == str and - str.unpack("m") == [ary] + assert_pack "m", "AAAA\n", ["\0\0\0"] end assert('["abc..xyzABC..XYZ"].pack("m")') do - ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") == "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n" + assert_pack "m", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] end assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do - str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m") == [str] and - "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m") == [str] + ary = ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] + assert_equal ary, "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m") + assert_equal ary, "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m") +end + +assert('["A", "B"].pack') do + assert_equal "QQ==\n", ["A", "B"].pack("m50") + assert_equal ["A"], "QQ==\n".unpack("m50") + assert_equal "QQ==Qg==", ["A", "B"].pack("m0 m0") + assert_equal ["A", "B"], "QQ==Qg==".unpack("m10 m10") +end + +assert('["abc..xyzABC..XYZ"].pack("m0")') do + assert_pack "m0", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] end # pack & unpack 'H' assert('["3031"].pack("H*")') do - ary = "3031" - str = "01" - [ary].pack("H*") == str and - str.unpack("H*") == [ary] + assert_pack "H*", "01", ["3031"] end assert('["10"].pack("H*")') do - ary = "10" - str = "\020" - [ary].pack("H*") == str and - str.unpack("H*") == [ary] + assert_pack "H*", "\020", ["10"] end assert('[0,1,127,128,255].pack("C*")') do - ary = [ 0, 1, 127, 128, 255 ] - str = "\x00\x01\x7F\x80\xFF" - ary.pack("C*") == str and str.unpack("C*") == ary + assert_pack "C*", "\x00\x01\x7F\x80\xFF", [0, 1, 127, 128, 255] end # pack "a" assert('["abc"].pack("a")') do - ["abc"].pack("a") == "a" and - ["abc"].pack("a*") == "abc" and - ["abc"].pack("a4") == "abc\0" + assert_equal "a", ["abc"].pack("a") + assert_equal "abc", ["abc"].pack("a*") + assert_equal "abc\0", ["abc"].pack("a4") end # upack "a" assert('["abc"].pack("a")') do - "abc\0".unpack("a4") == ["abc\0"] and - "abc ".unpack("a4") == ["abc "] + assert_equal ["abc\0"], "abc\0".unpack("a4") + assert_equal ["abc "], "abc ".unpack("a4") end # pack "A" assert('["abc"].pack("A")') do - ["abc"].pack("A") == "a" and - ["abc"].pack("A*") == "abc" and - ["abc"].pack("A4") == "abc " + assert_equal "a", ["abc"].pack("A") + assert_equal "abc", ["abc"].pack("A*") + assert_equal "abc ", ["abc"].pack("A4") end # upack "A" assert('["abc"].pack("A")') do - "abc\0".unpack("A4") == ["abc"] and - "abc ".unpack("A4") == ["abc"] + assert_equal ["abc"], "abc\0".unpack("A4") + assert_equal ["abc"], "abc ".unpack("A4") end # regression tests assert('issue #1') do - [1, 2].pack("nn") == "\000\001\000\002" + assert_equal "\000\001\000\002", [1, 2].pack("nn") end -def assert_pack tmpl, packed, unpacked - assert_equal packed, unpacked.pack(tmpl) - assert_equal unpacked, packed.unpack(tmpl) -end - -PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01 - assert 'pack float' do assert_pack 'e', "\x00\x00@@", [3.0] assert_pack 'g', "@@\x00\x00", [3.0] |
