Merge pull request #1327 from Pondorasti/patch-1
[RRG-proxmark3.git] / client / lualibs / html_dumplib.lua
blobde462f19bf4c3488d38a5f65c38dc2a4a8ba7dbb
1 bin = require('bin')
4 -------------------------------
5 -- Some utilities
6 -------------------------------
8 ---
9 -- A debug printout-function
10 local function dbg(args)
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
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)))
40 end
41 -- Write the data into it
42 outfile:write(html)
43 io.close(outfile)
45 -- Done
46 return filename
48 end
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)))
55 end
57 outfile:write(data)
58 io.close(outfile)
59 return filename
60 end
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)))
68 end
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
77 io.close(outfile)
78 return filename
79 end
81 local function convert_ascii_dump_to_JS(infile)
82 local t = infile:read("*all")
83 local cleaned
84 local output = "[";
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"
89 end
90 end
91 output = output .. "]"
92 return output
93 end
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.
108 -- Thus:
109 blockLen = blockLen * 2
111 local js,i = "[";
112 for i = 1, string.len(hex),blockLen do
113 js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n"
115 js = js .. "]"
116 return js
119 local function convert_ascii_dump_to_BIN(infile)
120 local t = infile:read("*all")
121 local cleaned
122 local output = {};
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) )
131 return output
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)))
149 -- Read file, get JS
150 local javascript = convert_ascii_dump_to_JS(infile)
151 io.close(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)))
168 -- Read file, get JS
169 local javascript = convert_binary_dump_to_JS(infile, blockLen)
170 io.close(infile)
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)
188 io.close(infile)
190 return save_BIN(data, output )
194 return {
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,