Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

File encoding and decoding
Started by Scott Harrison Aug 16 2016 11:14 AM

4 replies to this topic
file encode file decode
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,820 posts
  • Enterprise

I am using these function a plugin coming soon, thought they could be helpful for share files to another device with services like gamkit, autolan( it does technical have a file send service build in), or one signal. You also can store files inside your lua code. Of course these functions generate long strings so beware of size limits on some services.

local function encodeFile( filename,dir )
	local base64 = require("base64")
	local myDir = system.ResourceDirectory
	if (dir) then
		myDir = dir
	end
	local path = system.pathForFile( filename, myDir )
	local fileHandle = io.open( path, "rb" )
	local tempString=base64.encode(fileHandle:read( "*a" ))
	local tempTable = filename:split("%.")
	local tempTable2 = tempTable[1]:split("/")
	local tempString3 = ""
	if (tempTable2 and tempTable2[1]) then
		tempString3= tempTable2[#tempTable2]
	else
		tempString3= tempTable[1]
	end
	local tempString2 = tempString.."_"..tempString3.."_"..tempTable[2]
	return tempString2
end
local function decodeFile( txt, dir )
	local base64 = require("base64")
	local myDir = system.DocumentsDirectory
	if (dir) then
		myDir = dir
	end
	local tempTable= txt:split("_")
	local tempString2 = ""
	for i=1,#tempTable-2 do
		tempString2 = tempString2..tempTable[i]
	end
	local path
	local file, errorString
	if (path) then
		path = system.pathForFile( path2.."/"..tempTable[#tempTable-1].."."..tempTable[#tempTable], myDir )
		file, errorString= io.open( path, "w" )
	else
		path = system.pathForFile( tempTable[#tempTable-1].."."..tempTable[#tempTable], myDir)
		file, errorString= io.open( path, "w" )
	end
	if not file then
	else
	    -- Write data to file
	    file:write( base64.decode(tempString2) )
	    -- Close the file handle
	    io.close( file )
	end
	return tempTable[#tempTable-1].."."..tempTable[#tempTable], dir
end

You also need to include this lua file

local mime=require('mime')
local socket=require('socket')
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

return base64


[TOPIC: post.html]
#2

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 920 posts
  • Corona SDK

I'm trying to obfuscate some images in my app using a similar method, and wondered if you or anyone else know how to get around one thing that's stopping us. 

 

I'm encrypting some images, saving the string to a txt file and then I'm putting those files into my app prior to building instead of the jpg files. Then at runtime, when I need to use an image (I only ever need one at any one time) I read the file, decrypt it, save it to a jpg in the DocumentsDirectory and then open that file using newImageRect.

 

The problem is that saving it to a jpg defeats the point of encrypting the image in the first place. The perfect workaround would be if the decrypted string could be passed into newImageRect. Is there anyway to pass that kind of data into an image or texture or anything like that?

 

I could do what I'm currently doing and delete the jpg file once the image loads, but I can see that causing problems where the image goes out of memory if the app sleeps or something like that so would prefer a better solution if there is one.



[TOPIC: post.html]
#3

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,820 posts
  • Enterprise

You might try using bitmap

[TOPIC: post.html]
#4

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 920 posts
  • Corona SDK

Do you mean using BitmapPaint or am I overlooking something? As far as I can tell that takes a filename as well.



[TOPIC: post.html]
#5

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,820 posts
  • Enterprise

You could try converting your images to a bitmaps and place them inside the lua file


[topic_controls]
[/topic_controls]