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 cleaned = (line or ''):gsub('%s+','')
88 output = output .. "'"..cleaned.."',\n"
91 output = output .. "]"
95 local function convert_binary_dump_to_JS(infile, blockLen)
96 local bindata = infile:read("*all")
97 len = string.len(bindata)
99 if len % blockLen ~= 0 then
100 return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen))
103 local _,hex = bin.unpack(("H%d"):format(len),bindata)
105 -- Now that we've converted binary data into hex, we doubled the size.
106 -- One byte, like 0xDE is now
107 -- the characters 'D' and 'E' : one byte each.
109 blockLen = blockLen * 2
112 for i = 1, string.len(hex),blockLen do
113 js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n"
119 local function convert_ascii_dump_to_BIN(infile)
120 local t = infile:read("*all")
123 for line in string.gmatch(t, "[^\n]+") do
124 if string.byte(line) ~= string.byte("+") then
125 cleaned = (line or ''):gsub('%s+','')
126 for c in cleaned:gmatch('..') do
127 output[#output+1] = string.char( tonumber(c,16) )
136 -- Converts a .eml-file into a HTML/Javascript file.
137 -- @param input the file to convert
138 -- @param output the file to write to
139 -- @return the name of the new file.
140 local function convert_eml_to_html(input, output)
141 input = input or 'dumpdata.eml'
142 output = output or input .. 'html'
144 local infile = io.open(input, "r")
145 if infile == nil then
146 return oops(string.format("Could not read file %s",tostring(input)))
150 local javascript = convert_ascii_dump_to_JS(infile)
152 return save_HTML(javascript, output )
155 --- Converts a binary dump into HTML/Javascript file
156 -- @param input the file containing the dump (defaults to dumpdata.bin)
157 -- @param output the file to write to
158 -- @param blockLen, the length of each block. Defaults to 16 bytes
159 local function convert_bin_to_html(input, output, blockLen)
160 input = input or 'dumpdata.bin'
161 blockLen = blockLen or 16
162 output = output or input .. 'html'
164 local infile = io.open(input, "rb")
165 if infile == nil then
166 return oops(string.format("Could not read file %s",tostring(input)))
169 local javascript = convert_binary_dump_to_JS(infile, blockLen)
172 return save_HTML(javascript, output )
175 --- Converts a eml dump into a binary file
176 -- @param input the file containing the eml-dump (defaults to dumpdata.eml)
177 -- @param output the file to write to ( defaults to dumpdata.bin)
178 local function convert_eml_to_bin(input, output)
179 input = input or 'dumpdata.eml'
180 output = output or 'dumpdata.bin'
182 local infile = io.open(input, "rb")
183 if infile == nil then
184 return oops(string.format("Could not read file %s",tostring(input)))
186 -- Read file, get BIN
187 local data = convert_ascii_dump_to_BIN(infile)
190 return save_BIN(data, output )
195 convert_bin_to_html = convert_bin_to_html,
196 convert_eml_to_html = convert_eml_to_html,
197 convert_eml_to_bin = convert_eml_to_bin,
198 SaveAsBinary = save_BIN,
199 SaveAsText = save_TEXT,
200 SaveAsBinary = save_BIN,
201 SaveAsText = save_TEXT,