From 36d9cfe5474b526edb4131169d779a6666108a5b Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Fri, 29 Apr 2011 12:15:02 +0000 Subject: [PATCH] added binary to carray conversion script --- bin2carray.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 bin2carray.lua diff --git a/bin2carray.lua b/bin2carray.lua new file mode 100755 index 0000000..eb9127c --- /dev/null +++ b/bin2carray.lua @@ -0,0 +1,46 @@ +#!/usr/bin/lua +--============================================================================ +-- bin2carray.lua +-- +-- License: GPLv2 (see COPYING) or any later version +-- +-- Authors: Axel Kittenberger +-- +-- Transforms a binary file (the compiled lsyncd runner script) in a c array +-- so it can be included into the executable in a portable way. +--============================================================================ + +if #arg < 3 then + error("Usage: "..arg[0].." [infile] [varname] [outfile]") +end + +fin, err = io.open(arg[1], "rb") +if fin == nil then + error("Cannot open '"..arg[1].."' for reading: "..err) +end + +fout, err = io.open(arg[3], "w") +if fout == nil then + error("Cannot open '"..arg[3].."'for writing: "..err) +end + +fout:write("/* created by "..arg[0].." from file "..arg[1].." */\n") +fout:write("const unsigned char "..arg[2].."[] = {\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,",") + end + fout:write("\n") +end +fout:write("};"); + +fin:close(); +fout:close();