String dump to original text

209 views
Skip to first unread message

Rana Ayaz

unread,
Mar 3, 2024, 1:38:24 AM3/3/24
to lu...@googlegroups.com
Need guidance from expert members.
i am using version lua 5.3
I am learning Lua language yet I am very beginner in it.
I am learning to use string dump.
I have created a code that converts print hello and print world via string dump.
But I am not able to convert it back to original text properly.
Please guide me what is wrong in the code.
Here below I am posting the complete code I made.

-- original text
text = [[
print('hello')
print('world')
]]
-- convert in string dump
dmp = string.dump(load(text))
-- print converted string dump text
print(dmp)

-- convert back to original text
textBack = loadstring(dmp)
-- print back to original text
print(textBack)

This code is converting the original text to a string dump perfectly.
But when I try to convert it back to original text it shows below result.
function: 0x7432287880
Please guide me what changes I need to make in the code so that I can get my original text back..

Kartik Agaram

unread,
Mar 3, 2024, 1:40:38 AM3/3/24
to lu...@googlegroups.com
loadstring returns a function that you need to call.

Be sure to also check the manual for possible error returns.

Rana Ayaz

unread,
Mar 3, 2024, 1:44:41 AM3/3/24
to lu...@googlegroups.com
Can you guide me completely in this regard. 
Please correct the given code and guide me where I am doing wrong. 

On Sun, Mar 3, 2024, 11:40 AM Kartik Agaram <a...@akkartik.com> wrote:
loadstring returns a function that you need to call.

Be sure to also check the manual for possible error returns.

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lua-l/dc0dd8bc-0d2f-4a85-b79e-d52514277bfd%40app.fastmail.com.

l...@cdelord.fr

unread,
Mar 3, 2024, 3:18:31 AM3/3/24
to lu...@googlegroups.com

load or loadstring is not the inverse of string.dump.

string.dump (function [, strip])

Returns a string containing a binary representation (a binary chunk) of the given function, so that a later load on this string returns a copy of the function (but with new upvalues).

string.dump returns the bytecode of the function.

 load (chunk [, chunkname [, mode [, env]]])

Loads a chunk.

...

If there are no syntactic errors, load returns the compiled chunk as a function; otherwise, it returns fail plus the error message.

load (loadstring in older Lua versions) returns a function (not the source code of the function). You can call this function but not get its source code.

Try `textBack()` instead of `print(textBack)` (the name "textBack" may be misleading in this case):

chunk = assert(loadstring(dmp))
chunk()


On the other side, if you want to get the original text back, dump/load may not be the right functions. Do you actually need to compile/decompile Lua sources? Or just encrypt them for whatever purpose?

Rana Ayaz

unread,
Mar 3, 2024, 5:35:14 AM3/3/24
to lu...@googlegroups.com
Actually i am using lua version 5.3
Please guide me for this function as per this version.
As you said.
load or loadstring is not the inverse of string.dump.
So what should I do according to this version of me?.
As you asked.
Do you actually need to compile/decompile Lua sources? Or just encrypt them for whatever purpose?
I would like to learn both jobs.
As I mentioned in the previous message I am very new to this language and now. very beginner in this programming language
So I request you to guide me in such a way that I can understand it properly.
It would be easier for me to understand if you could edit my code and send it back to me..

-- original text
text = [[
print('hello')
print('world')
]]
-- convert in string dump
dmp = string.dump(load(text))
-- print converted string dump text
print(dmp)

-- convert back to original text

textBack = loadstring(dmp)
-- print back to original text
print(textBack)

Sainan

unread,
Mar 3, 2024, 6:11:56 AM3/3/24
to lu...@googlegroups.com
As was already explained, what you're trying to do is impossible. You
cannot get back the original source code of a compiled Lua chunk.
>>> Returns a string containing a binary representation (a _binary
>>> chunk_) of the given function, so that a later load [1] on this
>> [2].
>> --
>> You received this message because you are subscribed to the Google
>> Groups "lua-l" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to lua-l+un...@googlegroups.com.
>> To view this discussion on the web visit
>>
> https://groups.google.com/d/msgid/lua-l/CA%2BymkRioG0Y9L7vHSsgGhgSj9eBbg%2BMnWSZxSuDS%2BK9h62hXOQ%40mail.gmail.com
>> [3].
>
> --
> You received this message because you are subscribed to the Google
> Groups "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lua-l+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lua-l/4c5aec79-37fb-4bae-ae9b-995428fda2f8%40cdelord.fr
> [4].
>
> --
> You received this message because you are subscribed to the Google
> Groups "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lua-l+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lua-l/CA%2BymkRg-p0cLN_Q6n4bpQSGX5NcCo5iLNCTsZSdeiwf2j0cckg%40mail.gmail.com
> [5].
>
>
> Links:
> ------
> [1] https://www.lua.org/manual/5.4/manual.html#pdf-load
> [2]
> https://groups.google.com/d/msgid/lua-l/dc0dd8bc-0d2f-4a85-b79e-d52514277bfd%40app.fastmail.com?utm_medium=email&amp;utm_source=footer
> [3]
> https://groups.google.com/d/msgid/lua-l/CA%2BymkRioG0Y9L7vHSsgGhgSj9eBbg%2BMnWSZxSuDS%2BK9h62hXOQ%40mail.gmail.com?utm_medium=email&amp;utm_source=footer
> [4]
> https://groups.google.com/d/msgid/lua-l/4c5aec79-37fb-4bae-ae9b-995428fda2f8%40cdelord.fr?utm_medium=email&amp;utm_source=footer
> [5]
> https://groups.google.com/d/msgid/lua-l/CA%2BymkRg-p0cLN_Q6n4bpQSGX5NcCo5iLNCTsZSdeiwf2j0cckg%40mail.gmail.com?utm_medium=email&utm_source=footer
Reply all
Reply to author
Forward
0 new messages