Skip to content

Commit

Permalink
Fix parse fields after bit[17-24] call.
Browse files Browse the repository at this point in the history
For bits with length in range of [17, 24], should only read 3 bytes for processing.
  • Loading branch information
HUANG Wei committed Apr 17, 2015
1 parent e2cc40b commit 7b8c53a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ Parser.prototype.generateBit = function(ctx) {
} else if (sum <= 16) {
ctx.pushCode('var {0} = buffer.readUInt16BE(offset);', val);
sum = 16;
} else if (sum <= 24) {
var val1 = ctx.generateTmpVariable();
var val2 = ctx.generateTmpVariable();
ctx.pushCode('var {0} = buffer.readUInt16BE(offset);', val1);
ctx.pushCode('var {0} = buffer.readUInt8(offset + 2);', val2);
ctx.pushCode('var {2} = ({0} << 8) | {1};', val1, val2, val);
sum = 24;
} else if (sum <= 32) {
ctx.pushCode('var {0} = buffer.readUInt32BE(offset);', val);
sum = 32;
Expand Down
15 changes: 15 additions & 0 deletions test/composite_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,19 @@ describe('Composite parser', function(){

});
});

describe('Parse other fields after bit', function() {
it('Parse uint8', function() {
var buffer = new Buffer([0, 1, 0, 4]);
for (var i = 17; i <= 24; i++) {
var parser =
Parser.start()['bit' + i]('a').uint8('b');

assert.deepEqual(parser.parse(buffer), {
a: 1 << (i - 16),
b: 4,
});
}
});
});
});

0 comments on commit 7b8c53a

Please sign in to comment.