Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility of reading little endian bits #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions lib/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var SPECIAL_TYPES = {
Skip: null,
Choice: null,
Nest: null,
Bit: null
Bit: null,
Bitle: null,
};

var aliasRegistry = {};
Expand Down Expand Up @@ -100,6 +101,14 @@ BIT_RANGE.forEach(function(i) {
options.length = i;
return this.setNextParser("bit", varName, options);
};

Parser.prototype["bitle" + i.toString()] = function(varName, options) {
if (!options) {
options = {};
}
options.length = i;
return this.setNextParser("bitle", varName, options);
};
});

Parser.prototype.namely = function(alias) {
Expand Down Expand Up @@ -487,17 +496,17 @@ Parser.prototype.generateBit = function(ctx) {
ctx.pushCode("var {0} = buffer.readUInt8(offset);", val);
sum = 8;
} else if (sum <= 16) {
ctx.pushCode("var {0} = buffer.readUInt16BE(offset);", val);
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.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);
ctx.pushCode(`var {0} = buffer.readUInt32BE(offset);`, val);
sum = 32;
} else {
throw new Error(
Expand All @@ -523,6 +532,62 @@ Parser.prototype.generateBit = function(ctx) {
}
};

Parser.prototype.generateBitle = function(ctx) {
// TODO find better method to handle nested bit fields
var parser = JSON.parse(JSON.stringify(this));
parser.varName = ctx.generateVariable(parser.varName);
ctx.bitFields.push(parser);

if (
!this.next ||
(this.next && ["Bitle", "Nest"].indexOf(this.next.type) < 0)
) {
var sum = 0;
ctx.bitFields.forEach(function(parser) {
sum += parser.options.length;
});

var val = ctx.generateTmpVariable();

if (sum <= 8) {
ctx.pushCode("var {0} = buffer.readUInt8(offset);", val);
sum = 8;
} else if (sum <= 16) {
ctx.pushCode(`var {0} = buffer.readUInt16LE(offset);`, val);
sum = 16;
} else if (sum <= 24) {
var val1 = ctx.generateTmpVariable();
var val2 = ctx.generateTmpVariable();
ctx.pushCode(`var {0} = buffer.readUInt16LE(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.readUInt32LE(offset);`, val);
sum = 32;
} else {
throw new Error(
"Currently, bit field sequence longer than 4-bytes is not supported."
);
}
ctx.pushCode("offset += {0};", sum / 8);

var bitOffset = 0;
ctx.bitFields.forEach(function(parser) {
ctx.pushCode(
"{0} = {1} >> {2} & {3};",
parser.varName,
val,
bitOffset,
(1 << parser.options.length) - 1
);
bitOffset += parser.options.length;
});

ctx.bitFields = [];
}
};

Parser.prototype.generateSkip = function(ctx) {
var length = ctx.generateOption(this.options.length);
ctx.pushCode("offset += {0};", length);
Expand Down Expand Up @@ -739,7 +804,7 @@ Parser.prototype.generateFormatter = function(ctx, varName, formatter) {
};

Parser.prototype.isInteger = function() {
return !!this.type.match(/U?Int[8|16|32][BE|LE]?|Bit\d+/);
return !!this.type.match(/U?Int[8|16|32][BE|LE]?|Bit\d+|Bitle\d+/);
};

//========================================================================================
Expand Down