1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
--
-- Dumps a file to the screen.
--
function display(inp, n)
local i
if (type(inp) == "string") then
inp = Input(inp)
elseif (type(inp) ~= "table") then
error("Display needs a string or an Input object")
end
i = 0
while(not inp:isclosed()) do
i = i + 1
print(inp:read())
if ((n ~= nil) and (i >= n)) then
return
end
end
end
--
-- Squash characters into printable chars.
--
function pchar(n)
if (not ((n >= 32) and (n <= 127))) then
n = 46 -- aka '.' or 0x2e
end
return hex(n, "%c")
end
--
-- Does a hexadecimal dump on the screen of the file.
--
function hexdump(inp, from, to, width)
local size, nlines, remaining, data_array, line, byte, outstring
if (type(inp) == "string") then
inp = Input(inp)
elseif (type(inp) ~= "table") then
error("Hexdump needs a string or an Input object")
end
size = inp:getsize()
if (from == nil) then
from = 0
end
if (to == nil) then
to = size
end
if (to > size) then
to = size
end
size = to - from
if (width == nil) then
width = 16
end
nlines = math.floor(size / width)
remaining = math.mod(size, width)
inp:seek(from)
data_array = inp:read(size)
for line = 0, nlines - 1, 1 do
outstring = hex(line * width + from, "%08x ")
for byte = 0, width - 1, 1 do
outstring = outstring .. hex(data_array[line * 16 + byte]) .. " "
end
outstring = outstring .. " "
for byte = 0, width - 1, 1 do
outstring = outstring .. pchar(data_array[line * 16 + byte])
end
print(outstring)
end
if (remaining == 0) then
return
end
outstring = hex(nlines * width + from, "%08x ");
for byte = 0, remaining - 1, 1 do
outstring = outstring .. hex(data_array[nlines * 16 + byte]) .. " "
end
for byte = remaining + 1, width - 1, 1 do
outstring = outstring .. " "
end
outstring = outstring .. " "
for byte = 0, remaining - 1, 1 do
outstring = outstring .. pchar(data_array[nlines * 16 + byte])
end
print(outstring)
end
--
-- Splits a string given a separator character into an array.
--
function split(str, at)
local splut = {}
if (type(str) ~= "string") then return nil end
if (not str) then str = "" end
if (not at) then
table.insert(splut, str)
else
for n, c in string.gfind(str, '([^%'..at..']*)(%'..at..'?)') do
table.insert(splut, n)
if (c == '') then break end
end
end
return splut
end
|