2011-04-29 12:15:02 +00:00
|
|
|
--============================================================================
|
2018-03-16 08:36:32 +00:00
|
|
|
-- bin2carray.lua
|
2011-04-29 12:15:02 +00:00
|
|
|
--
|
|
|
|
-- License: GPLv2 (see COPYING) or any later version
|
|
|
|
--
|
|
|
|
-- Authors: Axel Kittenberger <axkibe@gmail.com>
|
|
|
|
--
|
2018-03-16 08:36:32 +00:00
|
|
|
-- Transforms a binary file (the compiled lsyncd luacode) in a c array
|
2011-04-29 12:15:02 +00:00
|
|
|
-- so it can be included into the executable in a portable way.
|
2018-03-16 08:36:32 +00:00
|
|
|
--
|
2011-04-29 12:15:02 +00:00
|
|
|
--============================================================================
|
|
|
|
|
2018-03-16 08:36:32 +00:00
|
|
|
if #arg < 3
|
|
|
|
then
|
|
|
|
error( 'Usage: '..arg[ 0 ]..' [infile] [varname] [outfile]' )
|
2011-04-29 12:15:02 +00:00
|
|
|
end
|
|
|
|
|
2018-03-16 08:36:32 +00:00
|
|
|
fin, err = io.open( arg[ 1 ], 'rb' )
|
|
|
|
if fin == nil
|
|
|
|
then
|
|
|
|
error( 'Cannot open "'..arg[ 1 ]..'" for reading: '..err )
|
2011-04-29 12:15:02 +00:00
|
|
|
end
|
|
|
|
|
2018-03-16 08:36:32 +00:00
|
|
|
fout, err = io.open( arg[ 3 ], 'w' )
|
|
|
|
if fout == nil
|
|
|
|
then
|
|
|
|
error( 'Cannot open "'..arg[ 3 ]..'"for writing: '..err )
|
2011-04-29 12:15:02 +00:00
|
|
|
end
|
|
|
|
|
2018-03-16 08:36:32 +00:00
|
|
|
fout:write( '/* created by '..arg[ 0 ]..' from file '..arg[ 1 ]..' */\n')
|
|
|
|
fout:write( '#include <stddef.h>\n' )
|
|
|
|
fout:write( 'const char '..arg[ 2 ]..'_out[] = {\n' )
|
|
|
|
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
local block = fin:read( 16 )
|
|
|
|
|
|
|
|
if block == nil then break end
|
|
|
|
|
|
|
|
for i = 1, #block
|
|
|
|
do
|
|
|
|
local val = string.format( '%x', block:byte( i ) )
|
|
|
|
|
|
|
|
if #val < 2 then val = "0" ..val end
|
|
|
|
|
|
|
|
fout:write( "0x", val, "," )
|
2011-04-29 12:15:02 +00:00
|
|
|
end
|
2018-03-16 08:36:32 +00:00
|
|
|
|
|
|
|
fout:write( '\n' )
|
2011-04-29 12:15:02 +00:00
|
|
|
end
|
|
|
|
|
2018-03-16 08:36:32 +00:00
|
|
|
fout:write( '};\n\nsize_t '..arg[ 2 ]..'_size = sizeof('..arg[ 2 ]..'_out);\n' );
|
|
|
|
|
|
|
|
fin:close( );
|
|
|
|
fout:close( );
|
|
|
|
|