]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/html_dumplib.lua
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / client / lualibs / html_dumplib.lua
1 bin = require('bin')
2
3
4 -------------------------------
5 -- Some utilities
6 -------------------------------
7
8 ---
9 -- A debug printout-function
10 local function dbg(args)
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
19 print("###", args)
20 end
21 end
22 ---
23 -- This is only meant to be used when errors occur
24 local function oops(err)
25 print("ERROR: ",err)
26 return nil, err
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
39 return oops(string.format("Could not write to file %s",tostring(filename)))
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
50 local function save_BIN(data, filename)
51 -- Open the output file
52
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)))
56 end
57
58 -- Write the data into it
59 local i = 1
60 while data[i] do
61 outfile:write(data[i])
62 i = i+1
63 end
64
65 io.close(outfile)
66 return filename
67 end
68
69 local function convert_ascii_dump_to_JS(infile)
70 local t = infile:read("*all")
71
72 local output = "[";
73 for line in string.gmatch(t, "[^\n]+") do
74 if string.byte(line,1) ~= string.byte("+",1) then
75 output = output .. "'"..line.."',\n"
76 end
77 end
78 output = output .. "]"
79 return output
80 end
81
82 local function convert_binary_dump_to_JS(infile, blockLen)
83 local bindata = infile:read("*all")
84 len = string.len(bindata)
85
86 if len % blockLen ~= 0 then
87 return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen))
88 end
89
90 local _,hex = bin.unpack(("H%d"):format(len),bindata)
91
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.
95 -- Thus:
96 blockLen = blockLen * 2
97
98 local js,i = "[";
99 for i = 1, string.len(hex),blockLen do
100 js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n"
101 end
102 js = js .. "]"
103 return js
104 end
105
106 local function convert_ascii_dump_to_BIN(infile)
107 local t = infile:read("*all")
108
109 local output = {};
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) )
114 end
115 end
116 end
117 return output
118 end
119
120
121 ---
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'
129
130 local infile = io.open(input, "r")
131 if infile == nil then
132 return oops(string.format("Could not read file %s",tostring(input)))
133 end
134
135 -- Read file, get JS
136 local javascript = convert_ascii_dump_to_JS(infile)
137 io.close(infile)
138 return save_HTML(javascript, output )
139 end
140
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'
149
150 local infile = io.open(input, "rb")
151 if infile == nil then
152 return oops(string.format("Could not read file %s",tostring(input)))
153 end
154 -- Read file, get JS
155 local javascript = convert_binary_dump_to_JS(infile, blockLen)
156 io.close(infile)
157
158 return save_HTML(javascript, output )
159 end
160
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'
167
168 local infile = io.open(input, "rb")
169 if infile == nil then
170 return oops(string.format("Could not read file %s",tostring(input)))
171 end
172 -- Read file, get BIN
173 local data = convert_ascii_dump_to_BIN(infile)
174 io.close(infile)
175
176 return save_BIN(data, output )
177 end
178
179
180 return {
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,
184 }
Impressum, Datenschutz