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_TEXT(data,filename)
51 -- Open the output file
52 local outfile = io.open(filename, "w")
53 if outfile == nil then
54 return oops(string.format("Could not write to file %s",tostring(filename)))
62 local function save_BIN(data, filename)
63 -- Open the output file
65 local outfile = io.open(filename, "wb")
66 if outfile == nil then
67 return oops(string.format("Could not write to file %s",tostring(filename)))
70 -- Write the data into it
73 outfile:write(data[i])
81 local function convert_ascii_dump_to_JS(infile)
82 local t = infile:read("*all")
85 for line in string.gmatch(t, "[^\n]+") do
86 if string.byte(line,1) ~= string.byte("+",1) then
87 output = output .. "'"..line.."',\n"
90 output = output .. "]"
94 local function convert_binary_dump_to_JS(infile, blockLen)
95 local bindata = infile:read("*all")
96 len = string.len(bindata)
98 if len % blockLen ~= 0 then
99 return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen))
102 local _,hex = bin.unpack(("H%d"):format(len),bindata)
104 -- Now that we've converted binary data into hex, we doubled the size.
105 -- One byte, like 0xDE is now
106 -- the characters 'D' and 'E' : one byte each.
108 blockLen = blockLen * 2
111 for i = 1, string.len(hex),blockLen do
112 js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n"
118 local function convert_ascii_dump_to_BIN(infile)
119 local t = infile:read("*all")
122 for line in string.gmatch(t, "[^\n]+") do
123 if string.byte(line) ~= string.byte("+") then
124 for c in (line or ''):gmatch('..') do
125 output[#output+1] = string.char( tonumber(c,16) )
134 -- Converts a .eml-file into a HTML/Javascript file.
135 -- @param input the file to convert
136 -- @param output the file to write to
137 -- @return the name of the new file.
138 local function convert_eml_to_html(input, output)
139 input = input or 'dumpdata.eml'
140 output = output or input .. 'html'
142 local infile = io.open(input, "r")
143 if infile == nil then
144 return oops(string.format("Could not read file %s",tostring(input)))
148 local javascript = convert_ascii_dump_to_JS(infile)
150 return save_HTML(javascript, output )
153 --- Converts a binary dump into HTML/Javascript file
154 -- @param input the file containing the dump (defaults to dumpdata.bin)
155 -- @param output the file to write to
156 -- @param blockLen, the length of each block. Defaults to 16 bytes
157 local function convert_bin_to_html(input, output, blockLen)
158 input = input or 'dumpdata.bin'
159 blockLen = blockLen or 16
160 output = output or input .. 'html'
162 local infile = io.open(input, "rb")
163 if infile == nil then
164 return oops(string.format("Could not read file %s",tostring(input)))
167 local javascript = convert_binary_dump_to_JS(infile, blockLen)
170 return save_HTML(javascript, output )
173 --- Converts a eml dump into a binary file
174 -- @param input the file containing the eml-dump (defaults to dumpdata.eml)
175 -- @param output the file to write to ( defaults to dumpdata.bin)
176 local function convert_eml_to_bin(input, output)
177 input = input or 'dumpdata.eml'
178 output = output or 'dumpdata.bin'
180 local infile = io.open(input, "rb")
181 if infile == nil then
182 return oops(string.format("Could not read file %s",tostring(input)))
184 -- Read file, get BIN
185 local data = convert_ascii_dump_to_BIN(infile)
188 return save_BIN(data, output )
193 convert_bin_to_html = convert_bin_to_html,
194 convert_eml_to_html = convert_eml_to_html,
195 convert_eml_to_bin = convert_eml_to_bin,
196 SaveAsBinary = save_BIN,
197 SaveAsText = save_TEXT,