]> git.zerfleddert.de Git - proxmark3-svn/blame - client/lualibs/html_dumplib.lua
CHG: Minor code clean up.
[proxmark3-svn] / client / lualibs / html_dumplib.lua
CommitLineData
6742c089 1bin = require('bin')
2
3
4-------------------------------
5-- Some utilities
6-------------------------------
7
8---
9-- A debug printout-function
10local 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
21end
6742c089 22---
23-- This is only meant to be used when errors occur
24local function oops(err)
25 print("ERROR: ",err)
c44241fd 26 return nil, err
6742c089 27end
28
29local 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
48end
49
fdefed66
MHS
50local 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
67end
6742c089 68
69local 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
fdefed66
MHS
74 if string.byte(line,1) ~= string.byte("+",1) then
75 output = output .. "'"..line.."',\n"
76 end
6742c089 77 end
78 output = output .. "]"
79 return output
80end
81
6742c089 82local 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
104end
105
fdefed66
MHS
106local 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
118end
119
120
6742c089 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.
126local 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
c44241fd 132 return oops(string.format("Could not read file %s",tostring(input)))
6742c089 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 )
139end
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
145local 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
4c367827 150 local infile = io.open(input, "rb")
6742c089 151 if infile == nil then
c44241fd 152 return oops(string.format("Could not read file %s",tostring(input)))
6742c089 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 )
159end
160
fdefed66
MHS
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)
164local 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 )
177end
178
179
6742c089 180return {
181 convert_bin_to_html = convert_bin_to_html,
fdefed66
MHS
182 convert_eml_to_html = convert_eml_to_html,
183 convert_eml_to_bin = convert_eml_to_bin,
6742c089 184}
Impressum, Datenschutz