From eeb9bc07c275233867a4471b90980cee69067aae Mon Sep 17 00:00:00 2001 From: Kaoru Kobo <475350+kaorukobo@users.noreply.github.com> Date: Tue, 12 Oct 2021 12:59:44 +0900 Subject: [PATCH 1/3] Add noPayloadLength option to parse() --- lib/parser.js | 8 ++++---- test/parser-spec.js | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index b5d8af6..ee24253 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -10,10 +10,10 @@ var Packet = require('./packet').Packet; // Expects payload to be a buffer -function split(payload) { +function split(payload, {noPayloadLength} = {}) { var splits = []; var i = 0; - var length = payload[i++]; + var length = noPayloadLength ? payload.length : payload[i++]; while (i < length) { // Grab the length of the entire packet @@ -35,7 +35,7 @@ function split(payload) { return splits; } -function parse(buffer, byteOrder, callback) { +function parse(buffer, byteOrder, callback, {noPayloadLength} = {}) { // If a byte order wasn't passed it, make it big endian by default byteOrder = byteOrder ? byteOrder : "BE"; @@ -53,7 +53,7 @@ function parse(buffer, byteOrder, callback) { } // Split up the payload into packet chunks - var splits = split(buffer); + var splits = split(buffer, {noPayloadLength}); var packets = []; diff --git a/test/parser-spec.js b/test/parser-spec.js index caaedeb..2c05f7d 100644 --- a/test/parser-spec.js +++ b/test/parser-spec.js @@ -25,6 +25,11 @@ describe("Parser", function() { var parsed = parser.parse(testPayload); expect(parsed.length).to.equal(3); }); + it ("should fetch the first length octet when noPayloadLength is true", function() { + var newPayload = new Buffer([ 2, 1, 6, 3, 2, 160, 255 ]); + var parsed = parser.parse(newPayload, null, null, {noPayloadLength: true}); + expect(parsed.length).to.equal(2); + }); it("should properly parse a payload into the correct kinds of data", function() { var parsed = parser.parse(testPayload); parsed.forEach(function(packet) { From 5cdfa3cedef0a856fe936a134eca6b61d93fa768 Mon Sep 17 00:00:00 2001 From: Kaoru Kobo <475350+kaorukobo@users.noreply.github.com> Date: Tue, 12 Oct 2021 13:01:43 +0900 Subject: [PATCH 2/3] Handle zero packet length in split() --- lib/parser.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/parser.js b/lib/parser.js index ee24253..e8442ae 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -18,6 +18,8 @@ function split(payload, {noPayloadLength} = {}) { while (i < length) { // Grab the length of the entire packet var packetLength = payload[i++]; + if (packetLength == 0) + continue; // Grab the kind of packet var type = payload[i++]; // The length of the data is the whole thing minus the type byte From 7221b897fd804f09001bbd0a64e94ca63669b130 Mon Sep 17 00:00:00 2001 From: Kaoru Kobo <475350+kaorukobo@users.noreply.github.com> Date: Sun, 7 Nov 2021 13:33:31 +0900 Subject: [PATCH 3/3] Fixup of previous two commits --- lib/parser.js | 2 +- test/parser-spec.js | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index e8442ae..d5f7c38 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -19,7 +19,7 @@ function split(payload, {noPayloadLength} = {}) { // Grab the length of the entire packet var packetLength = payload[i++]; if (packetLength == 0) - continue; + break; // Grab the kind of packet var type = payload[i++]; // The length of the data is the whole thing minus the type byte diff --git a/test/parser-spec.js b/test/parser-spec.js index 2c05f7d..f451a40 100644 --- a/test/parser-spec.js +++ b/test/parser-spec.js @@ -18,6 +18,13 @@ describe("Parser", function() { it("should split up an advertisement packet into distinct data formats", function() { expect(parser.split(testPayload).length).to.equal(3); }); + + it("should discard the following data assuming them zero-padding when the leading-length octet of AD structure was 0x00.", function() { + var newPayload = Buffer.from(testPayload); + // Set the length of second AD structure to zero. + newPayload[4] = 0x00; + expect(parser.split(newPayload).length).to.equal(1); + }); }); describe("#parse()", function() { @@ -25,7 +32,7 @@ describe("Parser", function() { var parsed = parser.parse(testPayload); expect(parsed.length).to.equal(3); }); - it ("should fetch the first length octet when noPayloadLength is true", function() { + it ("should parse the entire buffer when noPayloadLength is true, assuming the payload not to have the leading length octet.", function() { var newPayload = new Buffer([ 2, 1, 6, 3, 2, 160, 255 ]); var parsed = parser.parse(newPayload, null, null, {noPayloadLength: true}); expect(parsed.length).to.equal(2);