Does mime.b64 have a limit

Is there a limit or something to the no of characters which can be input to the mime.b64 function?

The following (plug-n-play)code crashes the app in my iPhone…

[lua]local mime = require “mime”

local screenW = display.contentWidth
local screenH = display.contentHeight
local sW = screenW/2
local sH = screenH/2

local function convertDisplayObjectToBase64(image)

–save display object to a temp file
local fileName = “temp_custom.jpg”
local dir = system.TemporaryDirectory
display.save(image,fileName,dir)

–open and read file
local fileHandle = io.open( system.pathForFile( fileName ,dir), “rb” )
local imageData = fileHandle:read( “*a” )
fileHandle:close()

–base64 encode it
local encodedImageData = mime.b64(imageData)

return encodedImageData
end

local function imageListener(event)
local img = event.target
if not img then return end

img.width = screenW
img.height = screenH
img.x = sW
img.y = sH

timer.performWithDelay(2000,function()
local base64 = convertDisplayObjectToBase64(img)
print(base64)
end,1)
end
media.show(media.Camera, imageListener)[/lua]
What I am doing here is

  1. open camera and get an image
  2. scale and position the image
  3. after a delay of 2s call convertDisplayObjectToBase64 function

in the convertDisplayObjectToBase64 function

  1. save the image to a temporary file
  2. read the file to a variable
  3. base64 encode it using mime.b64

Now most of the times, the app crashes when the convertDisplayObjectToBase64 function is called… This happens only i the device. In the Mac Simulator, everything works fine…

Whats wrong here?
Does the crash occur because the file to be encoded is very large? [import]uid: 64174 topic_id: 33675 reply_id: 333675[/import]

I narrowed down the problem to the line
[lua]local encodedImageData = mime.b64(imageData)[/lua]
by giving timer delays…

So mime.b64 is the problem here… I even tried coroutines to cal mime.b64 after a few seconds… Still no luck…

Any ideas guys? [import]uid: 64174 topic_id: 33675 reply_id: 133870[/import]

there is a bug in mime.b64 that incorrectly encodes/decodes strings longer than 384 chars.

Use this function which I wrote instead.

Supposedly there is a Corona bug fix in the works since its a bug in LuaSocket but I dunno when/if it will be released.

local mime=require('mime')  
local socket=require('socket')  
local base64 = {}  
  
function base64.test()  
 local ts = string.rep('1234567890ABCDEF', 256)..'a'  
 local enc, \_ = base64.encode(ts)  
 local dec, \_ = base64.decode(enc)  
 if ts ~= dec then   
 print('BASE64 FAIL', ts:len(), dec:len())  
 end  
end  
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  
  
base64.test()  
  
return base64  
  

[import]uid: 122310 topic_id: 33675 reply_id: 133875[/import]

@aisaksen I know about the bug, and I know about your work around as well… I have even used your code in a couple of places…

But I thought the Luasocket bug only increases the size of the image after decoding… Is it the cause of the App Crash? Not sure abt that…

And mime.b64 is fast since it is written in Native code… Does your code take too long? My image binary is about 500000 characters in length before encoding…
Anyway, I will try base64 encoding with your code and post the result…
Thanks!

Also would appreciate some sort of feedback from the Corona staff… guys? [import]uid: 64174 topic_id: 33675 reply_id: 133885[/import]

ok! I used your code, and it works pretty well!
Thanks a million! [import]uid: 64174 topic_id: 33675 reply_id: 133970[/import]

I narrowed down the problem to the line
[lua]local encodedImageData = mime.b64(imageData)[/lua]
by giving timer delays…

So mime.b64 is the problem here… I even tried coroutines to cal mime.b64 after a few seconds… Still no luck…

Any ideas guys? [import]uid: 64174 topic_id: 33675 reply_id: 133870[/import]

there is a bug in mime.b64 that incorrectly encodes/decodes strings longer than 384 chars.

Use this function which I wrote instead.

Supposedly there is a Corona bug fix in the works since its a bug in LuaSocket but I dunno when/if it will be released.

local mime=require('mime')  
local socket=require('socket')  
local base64 = {}  
  
function base64.test()  
 local ts = string.rep('1234567890ABCDEF', 256)..'a'  
 local enc, \_ = base64.encode(ts)  
 local dec, \_ = base64.decode(enc)  
 if ts ~= dec then   
 print('BASE64 FAIL', ts:len(), dec:len())  
 end  
end  
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  
  
base64.test()  
  
return base64  
  

[import]uid: 122310 topic_id: 33675 reply_id: 133875[/import]

@aisaksen I know about the bug, and I know about your work around as well… I have even used your code in a couple of places…

But I thought the Luasocket bug only increases the size of the image after decoding… Is it the cause of the App Crash? Not sure abt that…

And mime.b64 is fast since it is written in Native code… Does your code take too long? My image binary is about 500000 characters in length before encoding…
Anyway, I will try base64 encoding with your code and post the result…
Thanks!

Also would appreciate some sort of feedback from the Corona staff… guys? [import]uid: 64174 topic_id: 33675 reply_id: 133885[/import]

ok! I used your code, and it works pretty well!
Thanks a million! [import]uid: 64174 topic_id: 33675 reply_id: 133970[/import]

Has this bug been fixed yet? 

Not that I’m aware of.  Most people who need to base64 encode something long will use their own encoder.

Has this bug been fixed yet? 

Not that I’m aware of.  Most people who need to base64 encode something long will use their own encoder.