]>
Commit | Line | Data |
---|---|---|
d78792f5 | 1 | -- The getopt-functionality is loaded from pm3/getopt.lua |
2 | -- Have a look there for further details | |
3 | getopt = require('getopt') | |
4 | bin = require('bin') | |
5 | ||
2fca3ad9 | 6 | example = "script run htmldump -o mifarecard_foo.html" |
d78792f5 | 7 | author = "Martin Holst Swende" |
2fca3ad9 | 8 | usage = "script run htmldump [-i <file>] [-o <file>]" |
d78792f5 | 9 | desc =[[ |
10 | This script takes a dumpfile and produces a html based dump, which is a | |
11 | bit more easily analyzed. | |
12 | ||
13 | Arguments: | |
14 | -h This help | |
15 | -i <file> Specifies the dump-file (input). If omitted, 'dumpdata.bin' is used | |
16 | -o <filename> Speciies the output file. If omitted, <curtime>.html is used. | |
17 | ||
18 | ]] | |
19 | ||
20 | ------------------------------- | |
21 | -- Some utilities | |
22 | ------------------------------- | |
23 | ||
24 | --- | |
25 | -- A debug printout-function | |
26 | function dbg(args) | |
27 | if DEBUG then | |
28 | print("###", args) | |
29 | end | |
30 | end | |
31 | --- | |
32 | -- This is only meant to be used when errors occur | |
33 | function oops(err) | |
34 | print("ERROR: ",err) | |
35 | end | |
36 | ||
37 | ||
38 | --- | |
39 | -- Usage help | |
40 | function help() | |
41 | print(desc) | |
42 | print("Example usage") | |
43 | print(example) | |
44 | end | |
45 | ||
46 | local function readdump(infile) | |
47 | t = infile:read("*all") | |
48 | --print(string.len(t)) | |
49 | len = string.len(t) | |
50 | local len,hex = bin.unpack(("H%d"):format(len),t) | |
51 | --print(len,hex) | |
52 | return hex | |
53 | end | |
54 | local function convert_to_js(hexdata) | |
55 | if string.len(hexdata) % 32 ~= 0 then | |
56 | return oops(("Bad data, length should be a multiple of 32 (was %d)"):format(string.len(hexdata))) | |
57 | end | |
58 | local js,i = "["; | |
59 | for i = 1, string.len(hexdata),32 do | |
60 | js = js .."'" ..string.sub(hexdata,i,i+31).."',\n" | |
61 | end | |
62 | js = js .. "]" | |
63 | return js | |
64 | end | |
65 | ||
66 | local function main(args) | |
67 | ||
d78792f5 | 68 | local input = "dumpdata.bin" |
69 | local output = os.date("%Y-%m-%d_%H%M%S.html"); | |
70 | for o, a in getopt.getopt(args, 'i:o:h') do | |
71 | if o == "h" then return help() end | |
72 | if o == "i" then input = a end | |
73 | if o == "o" then output = a end | |
74 | end | |
75 | -- Validate the parameters | |
76 | ||
77 | local infile = io.open(input, "r") | |
78 | if infile == nil then | |
acfdf952 | 79 | return oops("Could not read file ", input) |
d78792f5 | 80 | end |
81 | --lokal skel = require("skel") | |
82 | local dumpdata = readdump(infile) | |
83 | io.close(infile) | |
84 | ||
85 | local js_code = convert_to_js(dumpdata) | |
86 | --print(js_code) | |
87 | local skel = require("htmlskel") | |
88 | html = skel.getHTML(js_code); | |
89 | ||
90 | local outfile = io.open(output, "w") | |
91 | if outfile == nil then | |
92 | return oops("Could not write to file ", output) | |
93 | end | |
94 | outfile:write(html) | |
95 | io.close(outfile) | |
96 | print(("Wrote a HTML dump to the file %s"):format(output)) | |
97 | end | |
98 | ||
99 | ||
100 | --[[ | |
101 | In the future, we may implement so that scripts are invoked directly | |
102 | into a 'main' function, instead of being executed blindly. For future | |
103 | compatibility, I have done so, but I invoke my main from here. | |
104 | --]] | |
105 | main(args) |