]>
Commit | Line | Data |
---|---|---|
1 | bin = require('bin') | |
2 | ||
3 | ||
4 | ------------------------------- | |
5 | -- Some utilities | |
6 | ------------------------------- | |
7 | ||
8 | --- | |
9 | -- A debug printout-function | |
10 | local function dbg(args) | |
11 | if DEBUG then | |
12 | print("###", args) | |
13 | end | |
14 | end | |
15 | --- | |
16 | -- This is only meant to be used when errors occur | |
17 | local function oops(err) | |
18 | print("ERROR: ",err) | |
19 | return nil, err | |
20 | end | |
21 | ||
22 | local function save_HTML(javascript, filename) | |
23 | ||
24 | -- Read the HTML-skel file | |
25 | local skel = require("htmlskel") | |
26 | html = skel.getHTML(javascript); | |
27 | ||
28 | -- Open the output file | |
29 | ||
30 | local outfile = io.open(filename, "w") | |
31 | if outfile == nil then | |
32 | return oops(string.format("Could not write to file %s",tostring(filename))) | |
33 | end | |
34 | -- Write the data into it | |
35 | outfile:write(html) | |
36 | io.close(outfile) | |
37 | ||
38 | -- Done | |
39 | return filename | |
40 | ||
41 | end | |
42 | ||
43 | ||
44 | ||
45 | local function convert_ascii_dump_to_JS(infile) | |
46 | local t = infile:read("*all") | |
47 | ||
48 | local output = "["; | |
49 | for line in string.gmatch(t, "[^\n]+") do | |
50 | output = output .. "'"..line.."',\n" | |
51 | end | |
52 | output = output .. "]" | |
53 | return output | |
54 | end | |
55 | ||
56 | ||
57 | local function convert_binary_dump_to_JS(infile, blockLen) | |
58 | local bindata = infile:read("*all") | |
59 | len = string.len(bindata) | |
60 | ||
61 | if len % blockLen ~= 0 then | |
62 | return oops(("Bad data, length (%d) should be a multiple of blocklen (%d)"):format(len, blockLen)) | |
63 | end | |
64 | ||
65 | local _,hex = bin.unpack(("H%d"):format(len),bindata) | |
66 | ||
67 | -- Now that we've converted binary data into hex, we doubled the size. | |
68 | -- One byte, like 0xDE is now | |
69 | -- the characters 'D' and 'E' : one byte each. | |
70 | -- Thus: | |
71 | blockLen = blockLen * 2 | |
72 | ||
73 | local js,i = "["; | |
74 | for i = 1, string.len(hex),blockLen do | |
75 | js = js .."'" ..string.sub(hex,i,i+blockLen -1).."',\n" | |
76 | end | |
77 | js = js .. "]" | |
78 | return js | |
79 | end | |
80 | ||
81 | --- | |
82 | -- Converts a .eml-file into a HTML/Javascript file. | |
83 | -- @param input the file to convert | |
84 | -- @param output the file to write to | |
85 | -- @return the name of the new file. | |
86 | local function convert_eml_to_html(input, output) | |
87 | input = input or 'dumpdata.eml' | |
88 | output = output or input .. 'html' | |
89 | ||
90 | local infile = io.open(input, "r") | |
91 | if infile == nil then | |
92 | return oops(string.format("Could not read file %s",tostring(input))) | |
93 | end | |
94 | ||
95 | -- Read file, get JS | |
96 | local javascript = convert_ascii_dump_to_JS(infile) | |
97 | io.close(infile) | |
98 | return save_HTML(javascript, output ) | |
99 | end | |
100 | ||
101 | --- Converts a binary dump into HTML/Javascript file | |
102 | -- @param input the file containing the dump (defaults to dumpdata.bin) | |
103 | -- @param output the file to write to | |
104 | -- @param blockLen, the length of each block. Defaults to 16 bytes | |
105 | local function convert_bin_to_html(input, output, blockLen) | |
106 | input = input or 'dumpdata.bin' | |
107 | blockLen = blockLen or 16 | |
108 | output = output or input .. 'html' | |
109 | ||
110 | local infile = io.open(input, "r") | |
111 | if infile == nil then | |
112 | return oops(string.format("Could not read file %s",tostring(input))) | |
113 | end | |
114 | -- Read file, get JS | |
115 | local javascript = convert_binary_dump_to_JS(infile, blockLen) | |
116 | io.close(infile) | |
117 | ||
118 | return save_HTML(javascript, output ) | |
119 | end | |
120 | ||
121 | return { | |
122 | convert_bin_to_html = convert_bin_to_html, | |
123 | convert_eml_to_html = convert_eml_to_html, | |
124 | } |