4 -------------------------------
6 -------------------------------
9 -- A debug printout-function
10 local function dbg(args)
12 if type(args) == "table" then
23 -- This is only meant to be used when errors occur
24 local function oops(err)
29 local function save_HTML(javascript, filename)
31 -- Read the HTML-skel file
32 local skel = require("htmlskel")
33 html = skel.getHTML(javascript);
35 -- Open the output file
37 local outfile = io.open(filename, "w")
38 if outfile == nil then
39 return oops(string.format("Could not write to file %s",tostring(filename)))
41 -- Write the data into it
50 local function save_BIN(data, filename)
51 -- Open the output file
53 local outfile = io.open(filename, "wb")
54 if outfile == nil then
55 return oops(string.format("Could not write to file %s",tostring(filename)))
58 -- Write the data into it
61 outfile:write(data[i])
69 local function convert_ascii_dump_to_JS(infile)
70 local t = infile:read("*all")
73 for line in string.gmatch(t, "[^\n]+") do
74 if string.byte(line,1) ~= string.byte("+",1) then
75 output = output .. "'"..line.."',\n"
78 output = output .. "]"
82 local function convert_binary_dump_to_JS(infile, blockLen)
83 local bindata = infile:read("*all")
84 len = string.len(bindata)
86 if len % blockLen ~= 0 then
87 return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen))
90 local _,hex = bin.unpack(("H%d"):format(len),bindata)
92 -- Now that we've converted binary data into hex, we doubled the size.
93 -- One byte, like 0xDE is now
94 -- the characters 'D' and 'E' : one byte each.
96 blockLen = blockLen * 2
99 for i = 1, string.len(hex),blockLen do
100 js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n"
106 local function convert_ascii_dump_to_BIN(infile)
107 local t = infile:read("*all")
110 for line in string.gmatch(t, "[^\n]+") do
111 if string.byte(line) ~= string.byte("+") then
112 for c in (line or ''):gmatch('..') do
113 output[#output+1] = string.char( tonumber(c,16) )
122 -- Converts a .eml-file into a HTML/Javascript file.
123 -- @param input the file to convert
124 -- @param output the file to write to
125 -- @return the name of the new file.
126 local function convert_eml_to_html(input, output)
127 input = input or 'dumpdata.eml'
128 output = output or input .. 'html'
130 local infile = io.open(input, "r")
131 if infile == nil then
132 return oops(string.format("Could not read file %s",tostring(input)))
136 local javascript = convert_ascii_dump_to_JS(infile)
138 return save_HTML(javascript, output )
141 --- Converts a binary dump into HTML/Javascript file
142 -- @param input the file containing the dump (defaults to dumpdata.bin)
143 -- @param output the file to write to
144 -- @param blockLen, the length of each block. Defaults to 16 bytes
145 local function convert_bin_to_html(input, output, blockLen)
146 input = input or 'dumpdata.bin'
147 blockLen = blockLen or 16
148 output = output or input .. 'html'
150 local infile = io.open(input, "rb")
151 if infile == nil then
152 return oops(string.format("Could not read file %s",tostring(input)))
155 local javascript = convert_binary_dump_to_JS(infile, blockLen)
158 return save_HTML(javascript, output )
161 --- Converts a eml dump into a binary file
162 -- @param input the file containing the eml-dump (defaults to dumpdata.eml)
163 -- @param output the file to write to ( defaults to dumpdata.bin)
164 local function convert_eml_to_bin(input, output)
165 input = input or 'dumpdata.eml'
166 output = output or 'dumpdata.bin'
168 local infile = io.open(input, "rb")
169 if infile == nil then
170 return oops(string.format("Could not read file %s",tostring(input)))
172 -- Read file, get BIN
173 local data = convert_ascii_dump_to_BIN(infile)
176 return save_BIN(data, output )
181 convert_bin_to_html = convert_bin_to_html,
182 convert_eml_to_html = convert_eml_to_html,
183 convert_eml_to_bin = convert_eml_to_bin,