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