Skip to content
Browse files

Fixed a bug in Nibbler where the final parts of a byte would not be r…

…ead out when the buffer got realigned; added test for this case
  • Loading branch information...
1 parent 20435d6 commit a16504943625ad3cbd4f97054ce62c5478338971 @tec27 committed May 8, 2012
Showing with 18 additions and 2 deletions.
  1. +5 −2 lib/parsers/nibbler.js
  2. +13 −0 test/nibbler.mocha.js
View
7 lib/parsers/nibbler.js
@@ -46,11 +46,14 @@ Nibbler.prototype.readBits = function(cnt) {
info.cnt -= leftBits;
info.buffer = ((0xFF ^ rightLoMask) & this.data) << leftBits; // top (8-shifted) bits right-aligned at 2nd byte
-
if(info.cnt >= 8) {
this._readMiddleBytes(val, info, leftBits);
- if(info.cnt === 0)
+ if(info.cnt === 0) {
+ // buffer has re-aligned, get the rest of the bits and set shifted to 0
+ val.push((info.buffer >>> 8) & loBitMask[leftBits]);
+ this.shifted = 0;
return val;
+ }
}
// number of bits in the buffer at this point is leftBits
View
13 test/nibbler.mocha.js
@@ -115,6 +115,19 @@ describe('Nibbler', function() {
// ---- ---- AA-- ---- a
ret[0].should.equal(0x6);
});
+ it('should be able to read from a shifted state to a non-shifted state across a byte boundary', function() {
+ var buf = new Buffer([0xF0, 0xFA, 0x8E]);
+ var curs = new Cursorize(buf);
+ nib = new Nibbler(curs);
+ nib.readBits(2);
+ var ret = nib.readBits(14);
+ // bits look like:
+ // 1111 0000 1111 1010 1000 1110
+ // AAAA aa-- aaBB bbbb
+ ret.length.should.equal(2);
+ ret[0].should.equal(0xF3);
+ ret[1].should.equal(0x3A);
+ });
});
describe('readToBoundary', function() {

0 comments on commit a165049

Please sign in to comment.
Something went wrong with that request. Please try again.