binary-parser
Advanced tools
Comparing version 0.0.5 to 1.0.0
@@ -64,2 +64,3 @@ //======================================================================================== | ||
this.isAsync = false; | ||
this.endian = 'be'; | ||
}; | ||
@@ -80,2 +81,9 @@ | ||
}; | ||
var typeWithoutEndian = type.replace(/BE|LE/, '').toLowerCase(); | ||
if (!(typeWithoutEndian in Parser.prototype)) { | ||
Parser.prototype[typeWithoutEndian] = function(varName, options) { | ||
return this[typeWithoutEndian + this.endian](varName, options); | ||
}; | ||
} | ||
}); | ||
@@ -167,15 +175,11 @@ | ||
Parser.prototype.endianess = function(endianess) { | ||
if (endianess !== 'little' && endianess !== 'big') { | ||
endianess = endianess.toLowerCase(); | ||
if (endianess === 'little') { | ||
this.endian = 'le'; | ||
} else if (endianess === 'big') { | ||
this.endian = 'be'; | ||
} else { | ||
throw new Error('Invalid endianess: ' + endianess); | ||
} | ||
var re = endianess === 'little' ? /(.+)LE/ : /(.+)BE/; | ||
Object.keys(PRIMITIVE_TYPES).forEach(function(type) { | ||
var matched = type.match(re); | ||
if (matched) { | ||
var funcName = matched[1].toLowerCase(); | ||
Parser.prototype[funcName] = Parser.prototype[type.toLowerCase()]; | ||
} | ||
}); | ||
return this; | ||
@@ -263,2 +267,3 @@ }; | ||
parser.options = options || parser.options; | ||
parser.endian = this.endian; | ||
@@ -329,5 +334,8 @@ if (this.head) { | ||
Parser.prototype.generateBit = function(ctx) { | ||
ctx.bitFields.push(this); | ||
// 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 && !this.next.type.match('Bit'))) { | ||
if (!this.next || (this.next && ['Bit', 'Nest'].indexOf(this.next.type) < 0)) { | ||
var sum = 0; | ||
@@ -355,7 +363,8 @@ ctx.bitFields.forEach(function(parser) { | ||
var bitOffset = 0; | ||
var isBigEndian = this.endian === 'be'; | ||
ctx.bitFields.forEach(function(parser) { | ||
ctx.pushCode('{0} = {1} >> {2} & {3};', | ||
ctx.generateVariable(parser.varName), | ||
parser.varName, | ||
val, | ||
sum - bitOffset - parser.options.length, | ||
isBigEndian ? sum - bitOffset - parser.options.length : bitOffset, | ||
(1 << parser.options.length) - 1 | ||
@@ -362,0 +371,0 @@ ); |
{ | ||
"name": "binary-parser", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"description": "Blazing-fast binary parser builder", | ||
@@ -6,0 +5,0 @@ "main": "lib/binary_parser.js", |
@@ -227,3 +227,3 @@ # Binary-parser | ||
## Examples | ||
See `test/` for more complex examples. | ||
See `example` for more complex examples. | ||
@@ -230,0 +230,0 @@ ## Support |
@@ -46,2 +46,19 @@ var assert = require('assert'); | ||
describe('Bit field parsers', function() { | ||
var binaryLiteral = function(s) { | ||
var i; | ||
var bytes = []; | ||
s = s.replace(/\s/g, ''); | ||
for (i = 0; i < s.length; i += 8) { | ||
bytes.push(parseInt(s.slice(i, i + 8), 2)); | ||
} | ||
return new Buffer(bytes); | ||
}; | ||
it('binary literal helper should work', function() { | ||
assert.deepEqual(binaryLiteral('11110000'), new Buffer([0xf0])); | ||
assert.deepEqual(binaryLiteral('11110000 10100101'), new Buffer([0xf0, 0xa5])); | ||
}); | ||
it('should parse 1-byte-length bit field sequence', function() { | ||
@@ -54,3 +71,3 @@ var parser = new Parser() | ||
var buf = new Buffer([parseInt('11010100', 2)]); | ||
var buf = binaryLiteral('1 10 1010 0'); | ||
assert.deepEqual(parser.parse(buf), { | ||
@@ -62,2 +79,16 @@ a: 1, | ||
}); | ||
parser = new Parser() | ||
.endianess('little') | ||
.bit1('a') | ||
.bit2('b') | ||
.bit4('c') | ||
.bit1('d'); | ||
assert.deepEqual(parser.parse(buf), { | ||
a: 0, | ||
b: 2, | ||
c: 10, | ||
d: 1 | ||
}); | ||
}); | ||
@@ -70,8 +101,19 @@ it('should parse 2-byte-length bit field sequence', function() { | ||
var buf = new Buffer([parseInt('10111100', 2), parseInt('01110101', 2)]); | ||
var buf = binaryLiteral('101 111000111 0111'); | ||
assert.deepEqual(parser.parse(buf), { | ||
a: 5, | ||
b: 455, | ||
c: 5 | ||
c: 7 | ||
}); | ||
parser = new Parser() | ||
.endianess('little') | ||
.bit3('a') | ||
.bit9('b') | ||
.bit4('c'); | ||
assert.deepEqual(parser.parse(buf), { | ||
a: 7, | ||
b: 398, | ||
c: 11 | ||
}); | ||
}); | ||
@@ -85,3 +127,3 @@ it('should parse 4-byte-length bit field sequence', function() { | ||
.bit1('e'); | ||
var buf = new Buffer([parseInt('11010101', 2), parseInt('01010101', 2), parseInt('01010101', 2), parseInt('01111011', 2)]); | ||
var buf = binaryLiteral('1 101010101010101010101010 1111 01 1'); | ||
assert.deepEqual(parser.parse(buf), { | ||
@@ -94,3 +136,39 @@ a: 1, | ||
}); | ||
parser = new Parser() | ||
.endianess('little') | ||
.bit1('a') | ||
.bit24('b') | ||
.bit4('c') | ||
.bit2('d') | ||
.bit1('e'); | ||
assert.deepEqual(parser.parse(buf), { | ||
a: 1, | ||
b: 11184829, | ||
c: 10, | ||
d: 2, | ||
e: 1 | ||
}); | ||
}); | ||
it('should parse nested bit fields', function() { | ||
var parser = new Parser() | ||
.bit1('a') | ||
.nest('x', { | ||
type: new Parser() | ||
.bit2('b') | ||
.bit4('c') | ||
.bit1('d') | ||
}); | ||
var buf = binaryLiteral('11010100'); | ||
assert.deepEqual(parser.parse(buf), { | ||
a: 1, | ||
x: { | ||
b: 2, | ||
c: 10, | ||
d: 0 | ||
} | ||
}); | ||
}); | ||
}); | ||
@@ -97,0 +175,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15
1
102508
1226