[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: binary string modified
- From: nobody <nobody+lua-list@...>
- Date: Sun, 21 Aug 2016 02:41:29 +0200
On 2016-08-20 23:09, Satoru Kawahara wrote:
> Hello,
>
> I tried to copy luac.out into a script file as string and run the native
> code string with `load()`.
Did you use a literal/long string ([[...]])? It seems to normalize all
line endings (0d0a / \r\n --> 0a / \n), thereby breaking it.
That is,
~$ { echo "io.stdout:write [[" ; cat luac.out ; echo "]]" ; } > a.lua
~$ lua a.lua > b.lua
~$ diff luac.out b.lua
Binary files luac.out and c.lua differ
But you can escape special characters and use a normal string:
f = io.open( "luac.out", "rb" )
code = f:read"a":gsub( "[%G'\\]", function( c )
return ("\\x%02x"):format( c:byte() )
end )
io.stdout:write( "print( pcall( assert( load '", code, "' ) ) )\n" )
Or, escaping just the minimum (and then you hope your editor doesn't
change anything and breaks it):
subst = {
["\r"] = "\\r", ["\n"] = "\\n", ["\\"] = "\\\\", ["'"] = "\\'",
}
f = io.open( "luac.out", "rb" )
code = f:read"a":gsub( ".", subst )
io.stdout:write( "print( pcall( assert( load '", code, "' ) ) )\n" )