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

Add some features and change all types unit to byte #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ function size_of(segments, bindings) {

function writeInteger(segment, buf, offset, bindings) {
var value = (segment.name) ? bindings[segment.name] : segment.value;
var size = segment.size * segment.unit;
var size = segment.size * segment.unit, mod = segment.mod;
if(mod){
value = value * Math.pow(10, mod);
}
switch (size) {
case 8:
buf.writeUInt8(value, offset);
Expand Down
5 changes: 4 additions & 1 deletion lib/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ specifierTail
specifier
= 'little' / 'big' / 'signed' / 'unsigned'
/ 'integer' / 'binary' / 'float'
/ unit
/ unit / mod

unit
= 'unit:' num:number { return 'unit:' + num; }

mod
= 'mod:' num:number { return 'mod:' + num; }

ws = [ \t\n]*
9 changes: 7 additions & 2 deletions lib/interp.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,21 @@ function match(pattern, binary, boundvars) {
function get_integer(segment) {
debug("get_integer"); debug(segment);
// let's do only multiples of eight bits for now
var unit = segment.unit, size = size_of(segment, vars);
var unit = segment.unit, size = size_of(segment, vars), mod = segment.mod;
var bitsize = size * unit;
var byteoffset = offset / 8; // NB assumes aligned
offset += bitsize;
if (bitsize % 8 > 0 || (offset > binsize)) {
return false;
}
else {
return parse_int(binary, byteoffset, bitsize,
num = parse_int(binary, byteoffset, bitsize,
segment.bigendian, segment.signed);
if(mod){
return (num * Math.pow(0.1, mod)).toFixed(mod);
} else {
return num;
}
}
}

Expand Down
18 changes: 14 additions & 4 deletions lib/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function specs(segment, type, specifiers) {
switch (type) {
case 'integer':
segment.signed = signed_in(specifiers);
segment.mod = mod_in(specifiers);
// fall through
case 'float':
segment.bigendian = endian_in(specifiers);
Expand Down Expand Up @@ -89,23 +90,32 @@ function unit_in(specifiers, type) {
// OK defaults then
switch (type) {
case 'binary':
return 8;
case 'integer':
case 'float':
return 1;
return 8;
}
}

function mod_in(specifiers) {
for (var s in specifiers) {
if (s.substr(0, 4) == 'mod:') {
var mod = parseInt(s.substr(4));
return mod;
}
}
return 0;
}

function size_of(segment, type, size, unit) {
if (size !== undefined && size !== '') {
return size;
}
else {
switch (type) {
case 'integer':
return 8;
return 1;
case 'float':
return 64;
return 8;
case 'binary':
return true;
}
Expand Down