Skip to content

Commit

Permalink
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 18 additions & 1 deletion spec/std/base64_spec.cr
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ require "base64"
require "crypto/md5"

describe "Base64" do
it "simple test" do
context "simple test" do
eqs = {"" => "", "a" => "YQ==\n", "ab" => "YWI=\n", "abc" => "YWJj\n",
"abcd" => "YWJjZA==\n", "abcde" => "YWJjZGU=\n", "abcdef" => "YWJjZGVm\n",
"abcdefg" => "YWJjZGVmZw==\n"}
@@ -18,6 +18,18 @@ describe "Base64" do
end
end

context "\n in multiple places" do
eqs = {"abcd" => "YWJj\nZA==\n", "abcde" => "YWJj\nZGU=\n", "abcdef" => "YWJj\nZGVm\n",
"abcdefg" => "YWJj\nZGVmZw==\n", "abcdefg" => "YWJj\nZGVm\nZw==\n",
}
eqs.each do |a, b|
it "decode from #{b.inspect} to #{a.inspect}" do
Base64.decode(b).should eq(a.to_slice)
Base64.decode_string(b).should eq(a)
end
end
end

it "encodes byte slice" do
slice = Slice(UInt8).new(5) { 1_u8 }
Base64.encode(slice).should eq("AQEBAQE=\n")
@@ -130,6 +142,11 @@ describe "Base64" do
Base64.decode_string("a")
end
end

it "decode small tail after last \n, was a bug" do
s = "Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g\nnA==\n"
Base64.decode(s).should eq Bytes[78, 111, 119, 32, 105, 115, 32, 116, 104, 101, 32, 116, 105, 109, 101, 32, 102, 111, 114, 32, 97, 108, 108, 32, 103, 111, 111, 100, 32, 99, 111, 100, 101, 114, 115, 10, 116, 111, 32, 108, 101, 97, 114, 110, 32, 156]
end
end

describe "scrict" do
7 changes: 6 additions & 1 deletion src/base64.cr
Original file line number Diff line number Diff line change
@@ -237,6 +237,7 @@ module Base64
size = data.size
dt = DECODE_TABLE.to_unsafe
cstr = data.pointer(size)
start_cstr = cstr
while (size > 0) && (sym = cstr[size - 1]) && (sym == NL || sym == NR || sym == PAD)
size -= 1
end
@@ -256,6 +257,10 @@ module Base64
yield (c << 6 | d).to_u8
end

while (cstr < endcstr + 4) && (cstr.value == NL || cstr.value == NR)
cstr += 1
end

mod = (endcstr - cstr) % 4
if mod == 2
a, b = next_decoded_value, next_decoded_value
@@ -275,7 +280,7 @@ module Base64
res = dt[sym]
cstr += 1
if res < 0
raise Error.new("Unexpected symbol '#{sym.chr}'")
raise Error.new("Unexpected byte 0x#{sym.to_s(16)} at #{cstr - start_cstr - 1}")
end
res
end

0 comments on commit 1fff36d

Please sign in to comment.