-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.lua
50 lines (43 loc) · 1.3 KB
/
tools.lua
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
-- Tools
function unpack_int(buf, pos)
pos = pos or 1
buf = {buf:byte(pos, pos+4)}
local i = 1
local Sign = bit.band(bit.rshift(buf[i], 6), 1)
local res = bit.band(buf[i], 0x3F)
local shift = 6
for _=1,4 do
if bit.band(buf[i], 0x80) == 0 then
break
end
i = i+1
res = bit.bor(res, bit.lshift(bit.band(buf[i], 0x7F), shift))
shift = shift + 7
end
res = bit.bxor(res, -Sign)
return res, i
end
function unpack_int_from_tvb(tvb, pos)
return unpack_int(tvb:raw(pos,math.min(5,tvb:len()-pos)))
end
function tree_to_treeitem(tree, stub, tvb, offset, default)
local stub = stub:add(tvb(offset + tree.start, tree.size), tree.name)
local last_pos = tree.start
for _, field in ipairs(tree) do
if type(field.value) == 'nil' then
tree_to_treeitem(field, stub, tvb, offset, default)
else
stub:add(tvb(offset + field.start, field.size), field.name .. ': ' .. field.value)
end
last_pos = field.start + field.size
end
if default and last_pos < tree.start + tree.size then
stub:add(tvb(offset + last_pos, tree.start + tree.size - last_pos), ("Data [%d bytes]"):format(tree.start + tree.size - last_pos))
end
end
function string.tohex(str)
return (str:gsub('.', function (c)
return string.format('%02x', string.byte(c))
end))
end
-- Tools end