binary-parser
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -111,4 +111,7 @@ //======================================================================================== | ||
if (!options.zeroTerminated && !options.length) { | ||
throw new Error('Length option of string is not defined.'); | ||
throw new Error('Neiter length nor zeroTerminated is defined for string.'); | ||
} | ||
if (options.stripNull && !options.length) { | ||
throw new Error('Length must be defined if stripNull is defined.'); | ||
} | ||
options.encoding = options.encoding || 'utf8'; | ||
@@ -365,2 +368,9 @@ | ||
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) { | ||
@@ -396,4 +406,18 @@ ctx.pushCode('var {0} = buffer.readUInt32BE(offset);', val); | ||
Parser.prototype.generateString = function(ctx) { | ||
if(this.options.length) { | ||
var name = ctx.generateVariable(this.varName); | ||
var name = ctx.generateVariable(this.varName); | ||
var start = ctx.generateTmpVariable(); | ||
if (this.options.length && this.options.zeroTerminated) { | ||
ctx.pushCode('var {0} = offset;', start); | ||
ctx.pushCode('while(buffer.readUInt8(offset++) !== 0 && offset - {0} < {1});', | ||
start, | ||
this.options.length | ||
); | ||
ctx.pushCode('{0} = buffer.toString(\'{1}\', {2}, offset - {2} < {3} ? offset - 1 : offset);', | ||
name, | ||
this.options.encoding, | ||
start, | ||
this.options.length | ||
); | ||
} else if(this.options.length) { | ||
ctx.pushCode('{0} = buffer.toString(\'{1}\', offset, offset + {2});', | ||
@@ -404,15 +428,8 @@ name, | ||
); | ||
if(this.options.stripNull) | ||
{ | ||
ctx.pushCode('{0} = {0}.replace(/\0/g, \'\')', name); | ||
} | ||
ctx.pushCode('offset += {0};', ctx.generateOption(this.options.length)); | ||
} | ||
else { | ||
var start = ctx.generateTmpVariable(); | ||
} else if (this.options.zeroTerminated) { | ||
ctx.pushCode('var {0} = offset;', start); | ||
ctx.pushCode('while(buffer.readUInt8(offset++) !== 0);'); | ||
ctx.pushCode('{0} = buffer.toString(\'{1}\', {2}, offset - 1);', | ||
ctx.generateVariable(this.varName), | ||
name, | ||
this.options.encoding, | ||
@@ -422,2 +439,5 @@ start | ||
} | ||
if(this.options.stripNull) { | ||
ctx.pushCode('{0} = {0}.replace(/\\x00+$/g, \'\')', name); | ||
} | ||
}; | ||
@@ -537,3 +557,3 @@ | ||
} | ||
} | ||
}; | ||
@@ -540,0 +560,0 @@ Parser.prototype.isInteger = function() { |
{ | ||
"name": "binary-parser", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "Blazing-fast binary parser builder", | ||
@@ -5,0 +5,0 @@ "main": "lib/binary_parser.js", |
@@ -390,2 +390,17 @@ var assert = require('assert'); | ||
}); | ||
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, | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |
@@ -235,2 +235,23 @@ var assert = require('assert'); | ||
}); | ||
it('should parser zero terminated fixed-length string', function() { | ||
var buffer = new Buffer('abc\u0000defghij\u0000'); | ||
var parser = Parser.start() | ||
.string('a', {length: 5, zeroTerminated: true}) | ||
.string('b', {length: 5, zeroTerminated: true}) | ||
.string('c', {length: 5, zeroTerminated: true}) | ||
assert.deepEqual(parser.parse(buffer), { | ||
a: 'abc', | ||
b: 'defgh', | ||
c: 'ij' | ||
}); | ||
}); | ||
it('should strip trailing null characters', function() { | ||
var buffer = new Buffer('746573740000', 'hex'); | ||
var parser1 = Parser.start().string('str', {length: 7, stripNull: false}); | ||
var parser2 = Parser.start().string('str', {length: 7, stripNull: true}); | ||
assert.equal(parser1.parse(buffer).str, 'test\u0000\u0000'); | ||
assert.equal(parser2.parse(buffer).str, 'test'); | ||
}); | ||
}); | ||
@@ -237,0 +258,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
1462
125566
18