diff --git a/lib/parser.js b/lib/parser.js index b5d8af6..d5f7c38 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -10,14 +10,16 @@ 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 var packetLength = payload[i++]; + if (packetLength == 0) + break; // Grab the kind of packet var type = payload[i++]; // The length of the data is the whole thing minus the type byte @@ -35,7 +37,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 +55,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..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,6 +32,11 @@ describe("Parser", function() { var parsed = parser.parse(testPayload); expect(parsed.length).to.equal(3); }); + 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); + }); it("should properly parse a payload into the correct kinds of data", function() { var parsed = parser.parse(testPayload); parsed.forEach(function(packet) {