Skip to content

Commit

Permalink
Remove more bin.lua packings.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonsaiviking committed Sep 22, 2018
1 parent e9addd7 commit ff6b5b6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 184 deletions.
106 changes: 31 additions & 75 deletions nselib/coap.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local bin = require "bin"
local comm = require "comm"
local json = require "json"
local lpeg = require "lpeg"
Expand Down Expand Up @@ -329,22 +328,23 @@ COAP.header.build = function(options)
code = COAP.header.codes.build(code)

-- Build the fixed portion of the header.
local pkt = ""

ver = ver << 6
mtype = mtype << 4

pkt = pkt .. bin.pack("C", ver | mtype | tkl)
pkt = pkt .. code
pkt = pkt .. bin.pack(">S", id)
pkt = pkt .. token
local pkt = {
string.pack("B", ver | mtype | tkl),
code,
string.pack(">I2", id),
token,
}

-- Include optional portions of the header.
if options["options"] then
pkt = pkt .. COAP.header.options.build(options.options)
pkt[#pkt+1] = COAP.header.options.build(options.options)
end

return pkt
return table.concat(pkt)
end

--- Parses a CoAP message header.
Expand All @@ -361,24 +361,18 @@ end
-- string containing the error message on failure.
COAP.header.parse = function(buf, pos)
assert(type(buf) == "string")
if #buf < 4 then
return false, "Cannot parse a string of less than four bytes."
end

if not pos or pos == 0 then
pos = 1
end
assert(type(pos) == "number")
assert(pos <= #buf)

if pos + 4 - 1 > #buf then
if #buf - pos + 1 < 4 then
return false, "Fixed header extends past end of buffer."
end

local pos, ver_type_tkl, code, id = bin.unpack("CA>S", buf, pos)
if not pos then
return false, "Failed to parse fixed header."
end
local ver_type_tkl, code, id, pos = string.unpack(">Bc1I2", buf, pos)

-- Parse the fixed header.
local hdr = {}
Expand Down Expand Up @@ -486,7 +480,7 @@ COAP.header.codes.build = function(name)

class = class << 5

return bin.pack("C", class | detail)
return string.pack("B", class | detail)
end

--- Parses a CoAP request or response code.
Expand All @@ -511,7 +505,7 @@ COAP.header.codes.parse = function(buf, pos)
assert(type(pos) == "number")
assert(pos <= #buf)

local pos, id = bin.unpack("C", buf, pos)
local id, pos = string.unpack("B", buf, pos)
if not pos then
return false, id
end
Expand Down Expand Up @@ -1462,20 +1456,8 @@ COAP.header.options.value.uint.build = function(val)
if val == 0 then
return ""
end

if val <= 255 then
return bin.pack("C", val)
end

if val <= 65535 then
return bin.pack(">S", val)
end

if val <= 16777215 then
return bin.pack(">I", val):sub(2, 5)
end

return bin.pack(">I", val)
-- strip leading null bytes to use smallest space
return string.pack(">I16", val):gsub("^\0*","")
end

--- Parses a CoAP message Uint header option value.
Expand All @@ -1490,25 +1472,15 @@ end
COAP.header.options.value.uint.parse = function(buf)
assert(type(buf) == "string")
assert(#buf >= 0)
assert(#buf <= 4)
assert(#buf <= 16)

if #buf == 0 then
return 0
end

local val, pos
if #buf == 1 then
pos, val = bin.unpack("C", buf)
elseif #buf == 2 then
pos, val = bin.unpack(">S", buf)
elseif #buf == 3 then
pos, val = bin.unpack(">I", string.char(0x00) .. buf)
else
pos, val = bin.unpack(">I", buf)
end
local val = string.unpack(">I" .. #buf, buf)

-- There should be no way for this to fail.
assert(pos)
assert(val)
assert(type(val) == "number")

Expand Down Expand Up @@ -1567,19 +1539,18 @@ COAP.header.options.delta_length.build = function(delta, length)
end

if num <= 268 then
return 13, bin.pack("C", num - 13)
return 13, string.pack("B", num - 13)
end

return 14, bin.pack(">S", num - 269)
return 14, string.pack(">I2", num - 269)
end

local d1, d2 = build(delta)
local l1, l2 = build(length)

d1 = d1 << 4
bin.pack("C", d1 | l1)

return bin.pack("C", d1 | l1) .. d2 .. l2
return string.pack("B", d1 | l1) .. d2 .. l2
end

--- Parse the variable-length option delta and length field.
Expand Down Expand Up @@ -1613,7 +1584,7 @@ COAP.header.options.delta_length.parse = function(buf, pos)
assert(type(pos) == "number")
assert(pos <= #buf)

local pos, delta_and_length = bin.unpack("C", buf, pos)
local delta_and_length, pos = string.unpack("B", buf, pos)
if not pos then
return false, nil, nil, delta_and_length
end
Expand All @@ -1636,20 +1607,20 @@ COAP.header.options.delta_length.parse = function(buf, pos)

if delta == 13 then
required_bytes = required_bytes + 1
dspec = "C"
dspec = "B"
elseif delta == 14 then
required_bytes = required_bytes + 2
delta = 269
dspec = ">S"
dspec = ">I2"
end

if length == 13 then
required_bytes = required_bytes + 1
lspec = "C"
lspec = "B"
elseif length == 14 then
required_bytes = required_bytes + 2
length = 269
lspec = ">S"
lspec = ">I2"
end

if pos + required_bytes - 1 > #buf then
Expand All @@ -1659,7 +1630,7 @@ COAP.header.options.delta_length.parse = function(buf, pos)
-- Extract the remaining bytes of each field.
if dspec then
local num
pos, num = bin.unpack(dspec, buf, pos)
num, pos = string.unpack(dspec, buf, pos)
if not pos then
return false, nil, nil, num
end
Expand All @@ -1668,7 +1639,7 @@ COAP.header.options.delta_length.parse = function(buf, pos)

if lspec then
local num
pos, num = bin.unpack(lspec, buf, pos)
num, pos = string.unpack(lspec, buf, pos)
if not pos then
return false, nil, nil, num
end
Expand Down Expand Up @@ -2426,7 +2397,7 @@ local tests = {
{
{["name"] = "etag", ["value"] = "ETAGETAG"},
},
bin.pack("CA", 0x48, "ETAGETAG")
"\x48ETAGETAG"
},
{
-- Before
Expand All @@ -2450,7 +2421,7 @@ local tests = {
{["name"] = "uri_path", ["value"] = "foo"},
{["name"] = "max_age", ["value"] = 0},
},
bin.pack("CAC", 0xB3, "foo", 0x30)
"\xB3foo\x30"
},
{
-- Before
Expand All @@ -2463,7 +2434,7 @@ local tests = {
{["name"] = "uri_path", ["value"] = ".well-known"},
{["name"] = "uri_path", ["value"] = "core"},
},
bin.pack("CACA", 0xBB, ".well-known", 0x04, "core")
"\xBB.well-known\x04core"
},
{
-- Before
Expand All @@ -2482,14 +2453,7 @@ local tests = {
{["name"] = "uri_path", ["value"] = "core"},
{["name"] = "max_age", ["value"] = 0},
},
bin.pack(
"CACCACAC",
0x48, "ETAGETAG", -- ID: 4, Delta: 4
0x10, -- ID: 5, Delta: 1
0x6B, ".well-known", -- ID: 11, Delta: 6
0x04, "core", -- ID: 11, Delta: 0
0x30 -- ID: 14, Delta: 3
)
"\x48ETAGETAG\x10\x6B.well-known\x04core\x30"
},
}

Expand Down Expand Up @@ -2537,15 +2501,7 @@ local tests = {
{["name"] = "uri_path", ["value"] = "core"},
},
},
bin.pack(
"CC>SACACA",
0x48,
0x01,
0x1234,
"nmapcoap",
0xBB, ".well-known",
0x04, "core"
)
"\x48\x01\x12\x34nmapcoap\xBB.well-known\x04core"
},
}

Expand Down
Loading

0 comments on commit ff6b5b6

Please sign in to comment.