Skip to content

Commit

Permalink
fixed Base64 bug with last bytes processing
Browse files Browse the repository at this point in the history
  • Loading branch information
philanc committed Nov 4, 2017
1 parent 373d36e commit 28835c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions plc/base64.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ local function encode(s, filename_safe)
if lln > maxlln then st[#st+1] = "\n"; lln = 1 end
end
-- process remaining bytes and padding
local llx = #st -- index of last st element with data
if st[llx] == "\n" then llx = llx - 1 end
if rn == 2 then
st[#st] = string.gsub(st[#st], ".$", "=")
st[llx] = string.gsub(st[llx], ".$", "=")
elseif rn == 1 then
st[#st] = string.gsub(st[#st], "..$", "==")
st[llx] = string.gsub(st[llx], "..$", "==")
end
local b = concat(st)
if filename_safe then
Expand Down
8 changes: 8 additions & 0 deletions test/test_base64.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ local function base64test()
assert(bd"YWF\nhY W\t\r\nFhYQ" == "aaaaaaa")
assert(bd(be"\x00\x01\x02\x03\x00" ) == "\x00\x01\x02\x03\x00")

-- 171103 - add a test for the "n*54-1" bug
-- (strings with n*54-1 or n*54-2 bytes)
for i = 0, 3 do
local ln = 3 * 54 - i
local s = ("a"):rep(ln)
assert(bd(be(s)) == s)
end

end


Expand Down

0 comments on commit 28835c2

Please sign in to comment.