local f = fs.open("/file","r")
if not f then
return
end
local fn = loadstring(f.readAll())
f.close()
fn() -- the error is here
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[bytecode][fs][error] how to load a dumped function from a file?
Started by tesla1889, 27 October 2012 - 05:27 PMPosted 27 October 2012 - 07:27 PM
I used string.dump on a function and wrote that dump into a file. When i use loadstring on the contents of the file, it gives me a function. The problem occurs when i try to use that function. I get the error "invalid object." How do you read bytecode from a file and turn it into a function?
Posted 27 October 2012 - 07:33 PM
If you're using string.dump, you really should be using binary mode file operations (wb and rb for write and read, respectively). It's possible that non-binary mode usage is screwing up your function string.
Posted 27 October 2012 - 09:01 PM
thanks, i'll try that and post the results
Posted 27 October 2012 - 10:14 PM
if a file is opened in binary read mode, it only reads one byte at a time and returns the integer, so how do i reconstruct the dump of the function?
Posted 27 October 2012 - 10:50 PM
You can read a byte and concatenate it to the string with string.char:
local file = fs.open("path/to/file", "rb")
if file then
local s = ""
local b = file.read()
while b do
s = s..string.char(:D/>/>
b = file.read()
end
file.close()
local f = loadstring(s)
if f then
f()
end
end
Posted 27 October 2012 - 11:18 PM
still didnt work. the function comes up nil.
Posted 27 October 2012 - 11:24 PM
Are you writing it to the file as binary?
Like this:
Like this:
local s = string.dump(someFunction)
local file = fs.open("path", "wb")
if file then
for i = 1, #s do
file.write(string.byte(i))
end
file.close()
end
Posted 27 October 2012 - 11:29 PM
Okay, why are you doing this? I mean, you already have the working function, right?
Posted 28 October 2012 - 05:22 AM
its closed source. i wanted a way to hide security features in my work, and the only way to do that is closed source
Posted 28 October 2012 - 05:48 AM
You should google 'Security by obscurity' :D/>/> if it's secure, then it shouldn't matter that people can see the code. also there are some decompilers for Lua 5.1 . So they can see it anyways.
(off-topic: in your signature: rs.setRedstone( ) is no valid CC function? )
(off-topic: in your signature: rs.setRedstone( ) is no valid CC function? )
Posted 28 October 2012 - 06:13 AM
(off-topic: in your signature: rs.setRedstone( ) is no valid CC function? )
I think he meant rs.setOutput()
Posted 28 October 2012 - 06:30 AM
Like others say, if you can use loadstring on it, then others can see the code in a form that they'll be able to figure out how it works and what the vulnerabilities are. loadstring is not the right tool for this job.