binary-parser
Advanced tools
Comparing version 1.3.1 to 1.3.2
//======================================================================================== | ||
// Globals | ||
//======================================================================================== | ||
var vm = require("vm"); | ||
@@ -31,4 +32,3 @@ var Context = require("./context").Context; | ||
Nest: null, | ||
Bit: null, | ||
Pointer: null | ||
Bit: null | ||
}; | ||
@@ -170,2 +170,7 @@ | ||
Parser.prototype.choice = function(varName, options) { | ||
if (arguments.length == 1 && typeof varName === "object") { | ||
options = varName; | ||
varName = null; | ||
} | ||
if (!options.tag) { | ||
@@ -177,2 +182,3 @@ throw new Error("Tag option of array is not defined."); | ||
} | ||
Object.keys(options.choices).forEach(function(key) { | ||
@@ -205,37 +211,20 @@ if (isNaN(parseInt(key, 10))) { | ||
Parser.prototype.nest = function(varName, options) { | ||
if (arguments.length == 1 && typeof varName === "object") { | ||
options = varName; | ||
varName = null; | ||
} | ||
if (!options.type) { | ||
throw new Error("Type option of nest is not defined."); | ||
} | ||
if (!(options.type instanceof Parser) && !aliasRegistry[options.type]) { | ||
throw new Error("Type option of nest must be a Parser object."); | ||
} | ||
return this.setNextParser("nest", varName, options); | ||
}; | ||
Parser.prototype.pointer = function(varName, options) { | ||
if (!options.offset) { | ||
throw new Error("Offset option of pointer is not defined."); | ||
} | ||
if (!options.type) { | ||
throw new Error("Type option of pointer is not defined."); | ||
} else if (typeof options.type === "string") { | ||
if ( | ||
!(NAME_MAP[options.type] in PRIMITIVE_TYPES) && | ||
!aliasRegistry[options.type] | ||
) { | ||
throw new Error( | ||
'Specified type "' + options.type + '" is not supported.' | ||
); | ||
} | ||
} else if (options.type instanceof Parser) { | ||
} else { | ||
if (!(options.type instanceof Parser) && !varName) { | ||
throw new Error( | ||
"Type option of pointer must be a string or a Parser object." | ||
"options.type must be a object if variable name is omitted." | ||
); | ||
} | ||
return this.setNextParser("pointer", varName, options); | ||
return this.setNextParser("nest", varName, options); | ||
}; | ||
@@ -336,8 +325,4 @@ | ||
Parser.prototype.compile = function() { | ||
this.compiled = new Function( | ||
"buffer", | ||
"callback", | ||
"constructorFn", | ||
this.getCode() | ||
); | ||
var src = "(function(buffer, constructorFn) { " + this.getCode() + " })"; | ||
this.compiled = vm.runInThisContext(src); | ||
}; | ||
@@ -394,3 +379,3 @@ | ||
// Follow the parser chain till the root and start parsing from there | ||
Parser.prototype.parse = function(buffer, callback) { | ||
Parser.prototype.parse = function(buffer) { | ||
if (!this.compiled) { | ||
@@ -400,3 +385,3 @@ this.compile(); | ||
return this.compiled(buffer, callback, this.constructorFn); | ||
return this.compiled(buffer, this.constructorFn); | ||
}; | ||
@@ -619,11 +604,3 @@ | ||
if (this.options.clone) { | ||
var buf = ctx.generateTmpVariable(); | ||
ctx.pushCode( | ||
"var {0} = new Buffer({1}.length);", | ||
buf, | ||
ctx.generateVariable(this.varName) | ||
); | ||
ctx.pushCode("{0}.copy({1});", ctx.generateVariable(this.varName), buf); | ||
ctx.pushCode("{0} = {1}", ctx.generateVariable(this.varName), buf); | ||
ctx.pushCode("{0} = Buffer.from({0});", ctx.generateVariable(this.varName)); | ||
} | ||
@@ -746,4 +723,7 @@ }; | ||
var nestVar = ctx.generateVariable(this.varName); | ||
if (this.options.type instanceof Parser) { | ||
ctx.pushCode("{0} = {};", nestVar); | ||
if (this.varName) { | ||
ctx.pushCode("{0} = {};", nestVar); | ||
} | ||
ctx.pushPath(this.varName); | ||
@@ -770,40 +750,2 @@ this.options.type.generate(ctx); | ||
Parser.prototype.generatePointer = function(ctx) { | ||
var type = this.options.type; | ||
var offset = ctx.generateOption(this.options.offset); | ||
var tempVar = ctx.generateTmpVariable(); | ||
var nestVar = ctx.generateVariable(this.varName); | ||
// Save current offset | ||
ctx.pushCode("var {0} = offset;", tempVar); | ||
// Move offset | ||
ctx.pushCode("offset = {0};", offset); | ||
if (this.options.type instanceof Parser) { | ||
ctx.pushCode("{0} = {};", nestVar); | ||
ctx.pushPath(this.varName); | ||
this.options.type.generate(ctx); | ||
ctx.popPath(this.varName); | ||
} else if (aliasRegistry[this.options.type]) { | ||
var tempVar = ctx.generateTmpVariable(); | ||
ctx.pushCode( | ||
"var {0} = {1}(offset);", | ||
tempVar, | ||
FUNCTION_PREFIX + this.options.type | ||
); | ||
ctx.pushCode("{0} = {1}.result; offset = {1}.offset;", nestVar, tempVar); | ||
if (this.options.type !== this.alias) ctx.addReference(this.options.type); | ||
ctx.pushScope(item); | ||
type.generate(ctx); | ||
ctx.popScope(); | ||
} else if (this.options.type in NAME_MAP) { | ||
ctx.pushCode("{0} = buffer.read{1}(offset);", nestVar, NAME_MAP[type]); | ||
ctx.pushCode("offset += {0};", PRIMITIVE_TYPES[NAME_MAP[type]]); | ||
} | ||
// Restore offset | ||
ctx.pushCode("offset = {0};", tempVar); | ||
}; | ||
Parser.prototype.isInteger = function() { | ||
@@ -810,0 +752,0 @@ return !!this.type.match(/U?Int[8|16|32][BE|LE]?|Bit\d+/); |
{ | ||
"name": "binary-parser", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "Blazing-fast binary parser builder", | ||
@@ -37,3 +37,6 @@ "main": "lib/binary_parser.js", | ||
"bugs": "http://github.com/keichi/binary-parser/issues", | ||
"dependencies": {} | ||
"dependencies": {}, | ||
"engines": { | ||
"node": ">=5.10.0" | ||
} | ||
} |
@@ -67,3 +67,3 @@ # Binary-parser | ||
// Prepare buffer to parse. | ||
var buf = new Buffer("450002c5939900002c06ef98adc24f6c850186d1", "hex"); | ||
var buf = Buffer.from("450002c5939900002c06ef98adc24f6c850186d1", "hex"); | ||
@@ -80,3 +80,3 @@ // Parse buffer and show result | ||
### parse(buffer[, callback]) | ||
### parse(buffer) | ||
Parse a `Buffer` object `buffer` with this parser and return the resulting | ||
@@ -90,3 +90,3 @@ object. When `parse(buffer)` is called for the first time, parser code is | ||
### [u]int{8, 16, 32}{le, be}(name [,options]) | ||
### [u]int{8, 16, 32}{le, be}(name[, options]) | ||
Parse bytes as an integer and store it in a variable named `name`. `name` | ||
@@ -108,3 +108,3 @@ should consist only of alphanumeric characters and start with an alphabet. | ||
### bit\[1-32\](name [,options]) | ||
### bit\[1-32\](name[, options]) | ||
Parse bytes as a bit field and store it in variable `name`. There are 32 | ||
@@ -114,3 +114,3 @@ methods from `bit1` to `bit32` each corresponding to 1-bit-length to | ||
### {float, double}{le, be}(name [,options]) | ||
### {float, double}{le, be}(name[, options]) | ||
Parse bytes as an floating-point value and store it in a variable named | ||
@@ -128,6 +128,6 @@ `name`. `name` should consist only of alphanumeric characters and start with | ||
### string(name [,options]) | ||
### string(name[, options]) | ||
Parse bytes as a string. `name` should consist only of alpha numeric | ||
characters and start with an alphabet. `options` is an object; following | ||
options are available: | ||
characters and start with an alphabet. `options` is an object which can have | ||
the following keys: | ||
@@ -148,6 +148,6 @@ - `encoding` - (Optional, defaults to `utf8`) Specify which encoding to use. | ||
### buffer(name [,options]) | ||
### buffer(name[, options]) | ||
Parse bytes as a buffer. `name` should consist only of alpha numeric | ||
characters and start with an alphabet. `options` is an object; following | ||
options are available: | ||
characters and start with an alphabet. `options` is an object which can have | ||
the following keys: | ||
@@ -166,5 +166,5 @@ - `clone` - (Optional, defaults to `false`) By default, | ||
### array(name [,options]) | ||
Parse bytes as an array. `options` is an object; following options are | ||
available: | ||
### array(name, options) | ||
Parse bytes as an array. `options` is an object which can have the following | ||
keys: | ||
@@ -243,7 +243,7 @@ - `type` - (Required) Type of the array element. Can be a string or an user | ||
### choice(name [,options]) | ||
Choose one parser from several choices according to a field value. Combining | ||
`choice` with `array` is useful for parsing a typical | ||
[Type-Length-Value](http://en.wikipedia.org/wiki/Type-length-value) styled | ||
format. | ||
### choice([name,] options) | ||
Choose one parser from multiple parsers according to a field value and store | ||
its parsed result to key `name`. If `name` is null or omitted, the result of | ||
the chosen parser is directly embedded into the current object. `options` is | ||
an object which can have the following keys: | ||
@@ -255,3 +255,3 @@ - `tag` - (Required) The value used to determine which parser to use from the | ||
- `defaultChoice` - (Optional) In case of the tag value doesn't match any of | ||
`choices` use this parser. | ||
`choices`, this parser is used. | ||
@@ -273,6 +273,10 @@ ```javascript | ||
### nest(name [,options]) | ||
Nest a parser in this position. Parse result of the nested parser is stored in the variable | ||
`name`. | ||
Combining `choice` with `array` is an idiom to parse | ||
[TLV](http://en.wikipedia.org/wiki/Type-length-value)-based formats. | ||
### nest([name,] options) | ||
Execute an inner parser and store its result to key `name`. If `name` is null | ||
or omitted, the result of the inner parser is directly embedded into the | ||
current object. `options` is an object which can have the following keys: | ||
- `type` - (Required) A `Parser` object. | ||
@@ -336,3 +340,3 @@ | ||
var buffer = new Buffer([ | ||
var buffer = Buffer.from([ | ||
2, | ||
@@ -387,3 +391,3 @@ /* left -> */ 3, | ||
var buffer = new Buffer([2, /* left */ 1, 1, 0, /* right */ 0]); | ||
var buffer = Buffer.from([2, /* left */ 1, 1, 0, /* right */ 0]); | ||
@@ -390,0 +394,0 @@ parser.parse(buffer); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
188334
441
1
1
15
1132