Merge pull request #4577 from dearblue/pack-unpack

Fix pack/unpack for base64; ref #4556
This commit is contained in:
Yukihiro "Matz" Matsumoto
2019-07-14 18:59:41 +09:00
committed by GitHub
2 changed files with 22 additions and 6 deletions
+11 -6
View File
@@ -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;
@@ -1196,7 +1196,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;
}
@@ -1249,6 +1249,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;
}
@@ -1275,9 +1278,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);
@@ -1299,7 +1299,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;
}
+11
View File
@@ -35,6 +35,17 @@ assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do
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
assert_pack "H*", "01", ["3031"]