summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/supportlib.lua90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/supportlib.lua b/lib/supportlib.lua
new file mode 100644
index 0000000..961c2c3
--- /dev/null
+++ b/lib/supportlib.lua
@@ -0,0 +1,90 @@
+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
+
+function pchar(n)
+ if (not ((n >= 32) and (n <= 127))) then
+ n = 46 -- aka '.' or 0x2e
+ end
+ return hex(n, "%c")
+end
+
+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