Anyone a guru at Binary? base64 bug workaround

There is a known bug in Lua Socket 2.0.2 that causes base64 decoded files to come back a whole lot bigger than they should be.
See https://developer.coronalabs.com/forum/2012/08/10/fyi-base64-decode-and-encode-corona#comment-119690

I have implemented the suggested workaround which works for text data but not binary. Has anyone overcome this issue or anyone who has some understanding of binary willing to look and see if there is a simple edit to make it work (its been along time since my CS degree)?

Any suggestions or advice would be greatly appreciated
Here is my code to test this all out
(if you want to run this code, I am using two resource files KingLearEntirePlay.html and test.aif -either download them here, or change to your own
http://www.antovate.com/test.aif
http://www.antovate.com/KingLearEntirePlay.html
)
[lua]local mime = require “mime”

–load a text file
local testpath = system.pathForFile( “KingLearEntirePlay.html”, system.ResourceDirectory)
local txtContents = “”
local file = io.open( testpath, “r” )
if file then
txtContents = file:read( “*a” )
io.close( file )
end

– load a binary file
local testpath = system.pathForFile( “test.aif”, system.ResourceDirectory)
local contents = “”
local file = io.open( testpath, “rb” )
if file then
contents = file:read( “*a” )
io.close( file )
end

– the workaround
local base64 = {}

function base64.encode(data)
local len = data:len()
local t = {}
for i=1,len,384 do
local n = math.min(384, len+1-i)
if n > 0 then
local s = data:sub(i, i+n-1)
local enc, _ = mime.b64(s)
t[#t+1] = enc
end
end

return table.concat(t)
end

function base64.decode(data)
local len = data:len()
local t = {}
for i=1,len,384 do
local n = math.min(384, len+1-i)
if n > 0 then
local s = data:sub(i, i+n-1)
local dec, _ = mime.unb64(s)
t[#t+1] = dec
end
end
return table.concat(t)
end

— test TEXT------
print ("----------------TESTING TEXT FILE---------------")
– Test the normal mime base64 encode
print("\nMime.b64")
print(“Origional :”, string.len(txtContents))
local mimecontents = mime.b64(txtContents)
print(“encoded :”, string.len(mimecontents))
local newContents = mime.unb64(mimecontents)
print (“decoded :”, string.len(newContents))

if (newContents == txtContents) then
print( “SUCCESS: files compare”)
else
print( “FAILE: files differ”)
end

–Work around
print("\nbase64")
print(“WA Origional :”, string.len(txtContents))
local WAmimecontents = base64.encode(txtContents)
print(“WAencoded :”, string.len(WAmimecontents))
local WAnewContents = base64.decode(WAmimecontents)
print (“WA decoded :”, string.len(WAnewContents))

if (WAnewContents == txtContents) then
print( “SUCCESS: files compare”)
else
print( “FAIL: files differ”)
end

— test BINARY------
print ("----------------TESTING BINARY FILE---------------")
– Test the normal mime base64 encode
print("\nMime.b64")
print(“Origional :”, string.len(contents))
local mimecontents = mime.b64(contents)
print(“encoded :”, string.len(mimecontents))
local newContents = mime.unb64(mimecontents)
print (“decoded :”, string.len(newContents))

if (newContents == contents) then
print( “SUCCESS: files compare”)
else
print( “FAIL: files differ”)
end

–Work around
print("\nbase64")
print(“WA Origional :”, string.len(contents))
local WAmimecontents = base64.encode(contents)
print(“WAencoded :”, string.len(WAmimecontents))
local WAnewContents = base64.decode(WAmimecontents)
print(“WA decoded :”, string.len(WAnewContents))

if (WAnewContents == contents) then
print( “SUCCESS: files compare”)
else
print( “FAIL: files differ”)
end[/lua]
[import]uid: 169392 topic_id: 33641 reply_id: 333641[/import]

I wrote this Ascii85 encoding some time back…
Check that out if you want… Encoded string would be a bit smaller… [import]uid: 64174 topic_id: 33641 reply_id: 133781[/import]

Thanks Satheesh, I have actually seen that. It’s great and consistently gives smaller string sizes. Unfortunately because it is not native, it is not as fast as the mime library and when I am dealing with 150K+ file sizes, it just makes the user experience too slow

I might have to fall back to it and be creative about encoding in the background if I can’t solve this mime issue. [import]uid: 169392 topic_id: 33641 reply_id: 133782[/import]

I wrote this Ascii85 encoding some time back…
Check that out if you want… Encoded string would be a bit smaller… [import]uid: 64174 topic_id: 33641 reply_id: 133781[/import]

Thanks Satheesh, I have actually seen that. It’s great and consistently gives smaller string sizes. Unfortunately because it is not native, it is not as fast as the mime library and when I am dealing with 150K+ file sizes, it just makes the user experience too slow

I might have to fall back to it and be creative about encoding in the background if I can’t solve this mime issue. [import]uid: 169392 topic_id: 33641 reply_id: 133782[/import]

Bump! [import]uid: 169392 topic_id: 33641 reply_id: 134589[/import]

Bump! [import]uid: 169392 topic_id: 33641 reply_id: 134589[/import]