Skip to content

Commit

Permalink
Allow bytes to be sized by a packet key.
Browse files Browse the repository at this point in the history
* src/api/bytes.lua: allow strings in the legth parameter
to indicate that a key must be dereferenced to calculate the
length of the array. Add a refkey parameter for that case.
* src/spackets_w3gs.lua: Adjust W3GS_INCOMING_ACTION packet
description to use the previous feature.
  • Loading branch information
diegonc committed Aug 8, 2015
1 parent d407942 commit 4c7ae58
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
33 changes: 29 additions & 4 deletions src/api/bytes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,49 @@
--
-- Creates a field for an array of bytes.
--
-- Quick call:
-- Quick call: ( label, length, refkey )
-- Table call: { label=..., length=..., refkey=... }
-- @par label Name of the field. It will be used as a label for the
-- field's node at the dissection tree.
-- @par length Length of the array.
-- @par length Length of the array.
-- If it is numeric, it indicates an array of that fixed
-- legth.
-- If it is a string, it specifies one of the following
-- values:
-- * "key": use the key parameter to set the length of
the array.
-- @par refkey Key that holds the length of the array. It must have
-- been initialized by a former field.
-- Required if the length parameter is "key".
--
--]]
do
local valid_lengths = {
key = true
}
local template = {
protofield_type = "bytes",
size = function(self, state)
return self.length
if type(self.length) == "number" then
return self.length
end
if type(self.length) ~= "string"
or not valid_lengths[self.length] then
error("length has an invalid value: "
.. tostring(self.length))
end
local length = state.packet[self.refkey]
if length == nil then
error("key '" .. self.refkey .. "' has not been defined")
end
return length
end,
length = 1,
}

function bytes(...)
local args = make_args_table_with_positional_map(
{"label", "length"},
{"label", "length", "refkey"},
#if LUA_VERSION >= 510
...
#else
Expand Down
39 changes: 32 additions & 7 deletions src/packet-bnetp.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--[[ packet-bnetp.lua build on Thu Aug 6 20:00:04 2015
--[[ packet-bnetp.lua build on Sat Aug 8 01:56:29 2015
packet-bnetp is a Wireshark plugin written in Lua for dissecting the Battle.net® protocol.
Homepage: https://github.com/diegonc/packet-bnetp/
Expand Down Expand Up @@ -1377,24 +1377,49 @@ end
--
-- Creates a field for an array of bytes.
--
-- Quick call:
-- Quick call: ( label, length, refkey )
-- Table call: { label=..., length=..., refkey=... }
-- @par label Name of the field. It will be used as a label for the
-- field's node at the dissection tree.
-- @par length Length of the array.
-- @par length Length of the array.
-- If it is numeric, it indicates an array of that fixed
-- legth.
-- If it is a string, it specifies one of the following
-- values:
-- * "key": use the key parameter to set the length of
the array.
-- @par refkey Key that holds the length of the array. It must have
-- been initialized by a former field.
-- Required if the length parameter is "key".
--
--]]
do
local valid_lengths = {
key = true
}
local template = {
protofield_type = "bytes",
size = function(self, state)
return self.length
if type(self.length) == "number" then
return self.length
end
if type(self.length) ~= "string"
or not valid_lengths[self.length] then
error("length has an invalid value: "
.. tostring(self.length))
end
local length = state.packet[self.refkey]
if length == nil then
error("key '" .. self.refkey .. "' has not been defined")
end
return length
end,
length = 1,
}

function bytes(...)
local args = make_args_table_with_positional_map(
{"label", "length"},
{"label", "length", "refkey"},
...
)

Expand Down Expand Up @@ -3195,8 +3220,8 @@ end
uint16("Send interval"),
uint16("CRC-16 encryption"),
uint8("Player number"),
uint16("Length of action data"),
bytes("Action data"),
uint16 {"Length of action data", key="action_length"},
bytes("Action data", "key", "action_length"),
},
[0xF70F] = { -- 0x0F
uint8("Player count"),
Expand Down
4 changes: 2 additions & 2 deletions src/spackets_w3gs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@
uint16("Send interval"),
uint16("CRC-16 encryption"),
uint8("Player number"),
uint16("Length of action data"),
bytes("Action data"),
uint16 {"Length of action data", key="action_length"},
bytes("Action data", "key", "action_length"),
},
--[[doc
Message ID: 0x0F
Expand Down

0 comments on commit 4c7ae58

Please sign in to comment.