]>
Commit | Line | Data |
---|---|---|
6742c089 | 1 | bin = require('bin') |
2 | ||
3 | ||
4 | ------------------------------- | |
5 | -- Some utilities | |
6 | ------------------------------- | |
7 | ||
8 | --- | |
9 | -- A debug printout-function | |
10 | local function dbg(args) | |
fdefed66 MHS |
11 | |
12 | if type(args) == "table" then | |
13 | local i = 1 | |
14 | while args[i] do | |
15 | print("###", args[i]) | |
16 | i = i+1 | |
17 | end | |
18 | else | |
6742c089 | 19 | print("###", args) |
fdefed66 MHS |
20 | end |
21 | end | |
6742c089 | 22 | --- |
23 | -- This is only meant to be used when errors occur | |
24 | local function oops(err) | |
25 | print("ERROR: ",err) | |
c44241fd | 26 | return nil, err |
6742c089 | 27 | end |
28 | ||
29 | local function save_HTML(javascript, filename) | |
30 | ||
31 | -- Read the HTML-skel file | |
32 | local skel = require("htmlskel") | |
33 | html = skel.getHTML(javascript); | |
34 | ||
35 | -- Open the output file | |
36 | ||
37 | local outfile = io.open(filename, "w") | |
38 | if outfile == nil then | |
c44241fd | 39 | return oops(string.format("Could not write to file %s",tostring(filename))) |
6742c089 | 40 | end |
41 | -- Write the data into it | |
42 | outfile:write(html) | |
43 | io.close(outfile) | |
44 | ||
45 | -- Done | |
46 | return filename | |
47 | ||
48 | end | |
49 | ||
3af373f3 | 50 | local function save_TEXT(data,filename) |
51 | -- Open the output file | |
52 | local outfile = io.open(filename, "wb") | |
53 | if outfile == nil then | |
54 | return oops(string.format("Could not write to file %s",tostring(filename))) | |
55 | end | |
56 | ||
57 | outfile:write(data) | |
58 | io.close(outfile) | |
59 | return filename | |
60 | end | |
61 | ||
fdefed66 MHS |
62 | local function save_BIN(data, filename) |
63 | -- Open the output file | |
64 | ||
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))) | |
68 | end | |
69 | ||
70 | -- Write the data into it | |
71 | local i = 1 | |
72 | while data[i] do | |
73 | outfile:write(data[i]) | |
74 | i = i+1 | |
75 | end | |
76 | ||
77 | io.close(outfile) | |
78 | return filename | |
79 | end | |
6742c089 | 80 | |
81 | local function convert_ascii_dump_to_JS(infile) | |
82 | local t = infile:read("*all") | |
83 | ||
84 | local output = "["; | |
85 | for line in string.gmatch(t, "[^\n]+") do | |
fdefed66 MHS |
86 | if string.byte(line,1) ~= string.byte("+",1) then |
87 | output = output .. "'"..line.."',\n" | |
88 | end | |
6742c089 | 89 | end |
90 | output = output .. "]" | |
91 | return output | |
92 | end | |
93 | ||
6742c089 | 94 | local function convert_binary_dump_to_JS(infile, blockLen) |
95 | local bindata = infile:read("*all") | |
96 | len = string.len(bindata) | |
97 | ||
98 | if len % blockLen ~= 0 then | |
99 | return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen)) | |
100 | end | |
101 | ||
102 | local _,hex = bin.unpack(("H%d"):format(len),bindata) | |
103 | ||
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. | |
107 | -- Thus: | |
108 | blockLen = blockLen * 2 | |
109 | ||
110 | local js,i = "["; | |
111 | for i = 1, string.len(hex),blockLen do | |
112 | js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n" | |
113 | end | |
114 | js = js .. "]" | |
115 | return js | |
116 | end | |
117 | ||
fdefed66 MHS |
118 | local function convert_ascii_dump_to_BIN(infile) |
119 | local t = infile:read("*all") | |
120 | ||
121 | local output = {}; | |
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) ) | |
126 | end | |
127 | end | |
128 | end | |
129 | return output | |
130 | end | |
131 | ||
132 | ||
6742c089 | 133 | --- |
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' | |
141 | ||
142 | local infile = io.open(input, "r") | |
143 | if infile == nil then | |
c44241fd | 144 | return oops(string.format("Could not read file %s",tostring(input))) |
6742c089 | 145 | end |
146 | ||
147 | -- Read file, get JS | |
148 | local javascript = convert_ascii_dump_to_JS(infile) | |
149 | io.close(infile) | |
150 | return save_HTML(javascript, output ) | |
151 | end | |
152 | ||
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' | |
161 | ||
4c367827 | 162 | local infile = io.open(input, "rb") |
6742c089 | 163 | if infile == nil then |
c44241fd | 164 | return oops(string.format("Could not read file %s",tostring(input))) |
6742c089 | 165 | end |
166 | -- Read file, get JS | |
167 | local javascript = convert_binary_dump_to_JS(infile, blockLen) | |
168 | io.close(infile) | |
169 | ||
170 | return save_HTML(javascript, output ) | |
171 | end | |
172 | ||
fdefed66 MHS |
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' | |
179 | ||
180 | local infile = io.open(input, "rb") | |
181 | if infile == nil then | |
182 | return oops(string.format("Could not read file %s",tostring(input))) | |
183 | end | |
184 | -- Read file, get BIN | |
185 | local data = convert_ascii_dump_to_BIN(infile) | |
186 | io.close(infile) | |
187 | ||
188 | return save_BIN(data, output ) | |
189 | end | |
190 | ||
191 | ||
6742c089 | 192 | return { |
193 | convert_bin_to_html = convert_bin_to_html, | |
fdefed66 MHS |
194 | convert_eml_to_html = convert_eml_to_html, |
195 | convert_eml_to_bin = convert_eml_to_bin, | |
3af373f3 | 196 | SaveAsBinary = save_BIN, |
197 | SaveAsText = save_TEXT, | |
6742c089 | 198 | } |