lua-users home
lua-l archive

Re: binary string modified

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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" )

AltStyle によって変換されたページ (->オリジナル) /