forked from RfidResearchGroup/proxmark3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_dumplib.lua
202 lines (167 loc) · 5.26 KB
/
html_dumplib.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
bin = require('bin')
-------------------------------
-- Some utilities
-------------------------------
---
-- A debug printout-function
local function dbg(args)
if type(args) == "table" then
local i = 1
while args[i] do
print("###", args[i])
i = i+1
end
else
print("###", args)
end
end
---
-- This is only meant to be used when errors occur
local function oops(err)
print("ERROR: ",err)
return nil, err
end
local function save_HTML(javascript, filename)
-- Read the HTML-skel file
local skel = require("htmlskel")
html = skel.getHTML(javascript);
-- Open the output file
local outfile = io.open(filename, "w")
if outfile == nil then
return oops(string.format("Could not write to file %s",tostring(filename)))
end
-- Write the data into it
outfile:write(html)
io.close(outfile)
-- Done
return filename
end
local function save_TEXT(data,filename)
-- Open the output file
local outfile = io.open(filename, "w")
if outfile == nil then
return oops(string.format("Could not write to file %s",tostring(filename)))
end
outfile:write(data)
io.close(outfile)
return filename
end
local function save_BIN(data, filename)
-- Open the output file
local outfile = io.open(filename, "wb")
if outfile == nil then
return oops(string.format("Could not write to file %s",tostring(filename)))
end
-- Write the data into it
local i = 1
while data[i] do
outfile:write(data[i])
i = i+1
end
io.close(outfile)
return filename
end
local function convert_ascii_dump_to_JS(infile)
local t = infile:read("*all")
local cleaned
local output = "[";
for line in string.gmatch(t, "[^\n]+") do
if string.byte(line,1) ~= string.byte("+",1) then
cleaned = (line or ''):gsub('%s+','')
output = output .. "'"..cleaned.."',\n"
end
end
output = output .. "]"
return output
end
local function convert_binary_dump_to_JS(infile, blockLen)
local bindata = infile:read("*all")
len = string.len(bindata)
if len % blockLen ~= 0 then
return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen))
end
local _,hex = bin.unpack(("H%d"):format(len),bindata)
-- Now that we've converted binary data into hex, we doubled the size.
-- One byte, like 0xDE is now
-- the characters 'D' and 'E' : one byte each.
-- Thus:
blockLen = blockLen * 2
local js,i = "[";
for i = 1, string.len(hex),blockLen do
js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n"
end
js = js .. "]"
return js
end
local function convert_ascii_dump_to_BIN(infile)
local t = infile:read("*all")
local cleaned
local output = {};
for line in string.gmatch(t, "[^\n]+") do
if string.byte(line) ~= string.byte("+") then
cleaned = (line or ''):gsub('%s+','')
for c in cleaned:gmatch('..') do
output[#output+1] = string.char( tonumber(c,16) )
end
end
end
return output
end
---
-- Converts a .eml-file into a HTML/Javascript file.
-- @param input the file to convert
-- @param output the file to write to
-- @return the name of the new file.
local function convert_eml_to_html(input, output)
input = input or 'dumpdata.eml'
output = output or input .. 'html'
local infile = io.open(input, "r")
if infile == nil then
return oops(string.format("Could not read file %s",tostring(input)))
end
-- Read file, get JS
local javascript = convert_ascii_dump_to_JS(infile)
io.close(infile)
return save_HTML(javascript, output )
end
--- Converts a binary dump into HTML/Javascript file
-- @param input the file containing the dump (defaults to dumpdata.bin)
-- @param output the file to write to
-- @param blockLen, the length of each block. Defaults to 16 bytes
local function convert_bin_to_html(input, output, blockLen)
input = input or 'dumpdata.bin'
blockLen = blockLen or 16
output = output or input .. 'html'
local infile = io.open(input, "rb")
if infile == nil then
return oops(string.format("Could not read file %s",tostring(input)))
end
-- Read file, get JS
local javascript = convert_binary_dump_to_JS(infile, blockLen)
io.close(infile)
return save_HTML(javascript, output )
end
--- Converts a eml dump into a binary file
-- @param input the file containing the eml-dump (defaults to dumpdata.eml)
-- @param output the file to write to ( defaults to dumpdata.bin)
local function convert_eml_to_bin(input, output)
input = input or 'dumpdata.eml'
output = output or 'dumpdata.bin'
local infile = io.open(input, "rb")
if infile == nil then
return oops(string.format("Could not read file %s",tostring(input)))
end
-- Read file, get BIN
local data = convert_ascii_dump_to_BIN(infile)
io.close(infile)
return save_BIN(data, output )
end
return {
convert_bin_to_html = convert_bin_to_html,
convert_eml_to_html = convert_eml_to_html,
convert_eml_to_bin = convert_eml_to_bin,
SaveAsBinary = save_BIN,
SaveAsText = save_TEXT,
SaveAsBinary = save_BIN,
SaveAsText = save_TEXT,
}