binary-parser
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -5,2 +5,3 @@ "use strict"; | ||
var vm_1 = require("vm"); | ||
require("console"); | ||
var context_1 = require("./context"); | ||
@@ -54,4 +55,5 @@ var aliasRegistry = {}; | ||
nest: 'Nest', | ||
skip: 'Skip', | ||
seek: 'Seek', | ||
pointer: 'Pointer', | ||
saveOffset: 'SaveOffset', | ||
'': '', | ||
@@ -278,6 +280,9 @@ }; | ||
Parser.prototype.skip = function (length, options) { | ||
return this.seek(length, options); | ||
}; | ||
Parser.prototype.seek = function (relOffset, options) { | ||
if (options && options.assert) { | ||
throw new Error('assert option on skip is not allowed.'); | ||
throw new Error('assert option on seek is not allowed.'); | ||
} | ||
return this.setNextParser('skip', '', { length: length }); | ||
return this.setNextParser('seek', '', { length: relOffset }); | ||
}; | ||
@@ -328,13 +333,15 @@ Parser.prototype.string = function (varName, options) { | ||
} | ||
Object.keys(options.choices).forEach(function (key) { | ||
if (isNaN(parseInt(key, 10))) { | ||
Object.keys(options.choices).forEach(function (keyString) { | ||
var key = parseInt(keyString, 10); | ||
var value = options.choices[key]; | ||
if (isNaN(key)) { | ||
throw new Error('Key of choices must be a number.'); | ||
} | ||
if (!options.choices[key]) { | ||
throw new Error("Choice Case " + key + " of " + varName + " is not valid."); | ||
if (!value) { | ||
throw new Error("Choice Case " + keyString + " of " + varName + " is not valid."); | ||
} | ||
if (typeof options.choices[key] === 'string' && | ||
!aliasRegistry[options.choices[key]] && | ||
Object.keys(PRIMITIVE_SIZES).indexOf(options.choices[key]) < 0) { | ||
throw new Error("Specified primitive type \"" + options.choices[key] + "\" is not supported."); | ||
if (typeof value === 'string' && | ||
!aliasRegistry[value] && | ||
Object.keys(PRIMITIVE_SIZES).indexOf(value) < 0) { | ||
throw new Error("Specified primitive type \"" + value + "\" is not supported."); | ||
} | ||
@@ -380,2 +387,5 @@ }); | ||
}; | ||
Parser.prototype.saveOffset = function (varName, options) { | ||
return this.setNextParser('saveOffset', varName, options); | ||
}; | ||
Parser.prototype.endianess = function (endianess) { | ||
@@ -403,5 +413,2 @@ switch (endianess.toLowerCase()) { | ||
var ctx = new context_1.Context(); | ||
ctx.pushCode('if (!Buffer.isBuffer(buffer)) {'); | ||
ctx.generateError('"argument buffer is not a Buffer object"'); | ||
ctx.pushCode('}'); | ||
if (!this.alias) { | ||
@@ -431,3 +438,2 @@ this.addRawCode(ctx); | ||
this.resolveReferences(ctx); | ||
ctx.pushCode('return vars;'); | ||
}; | ||
@@ -458,4 +464,4 @@ Parser.prototype.addAliasedCode = function (ctx) { | ||
Parser.prototype.compile = function () { | ||
var src = '(function(buffer, constructorFn) { ' + this.getCode() + ' })'; | ||
this.compiled = vm_1.runInNewContext(src, { Buffer: buffer_1.Buffer }); | ||
var src = "(function(buffer, constructorFn) { " + this.getCode() + " })"; | ||
this.compiled = vm_1.runInNewContext(src, { Buffer: buffer_1.Buffer, console: console }); | ||
}; | ||
@@ -490,3 +496,3 @@ Parser.prototype.sizeOf = function () { | ||
} | ||
else if (this.type === 'skip') { | ||
else if (this.type === 'seek') { | ||
size = this.options.length; | ||
@@ -511,2 +517,5 @@ // if this is a nested parser | ||
} | ||
if (!buffer_1.Buffer.isBuffer(buffer)) { | ||
throw new Error('argument buffer is not a Buffer object'); | ||
} | ||
return this.compiled(buffer, this.constructorFn); | ||
@@ -562,4 +571,4 @@ }; | ||
break; | ||
case 'skip': | ||
this.generateSkip(ctx); | ||
case 'seek': | ||
this.generateSeek(ctx); | ||
break; | ||
@@ -578,2 +587,5 @@ case 'nest': | ||
break; | ||
case 'saveOffset': | ||
this.generateSaveOffset(ctx); | ||
break; | ||
} | ||
@@ -653,8 +665,7 @@ this.generateAssert(ctx); | ||
ctx.bitFields.forEach(function (parser) { | ||
var offset = isBigEndian_1 | ||
? sum_1 - bitOffset_1 - parser.options.length | ||
: bitOffset_1; | ||
var mask = (1 << parser.options.length) - 1; | ||
var length = parser.options.length; | ||
var offset = isBigEndian_1 ? sum_1 - bitOffset_1 - length : bitOffset_1; | ||
var mask = (1 << length) - 1; | ||
ctx.pushCode(parser.varName + " = " + val_1 + " >> " + offset + " & " + mask + ";"); | ||
bitOffset_1 += parser.options.length; | ||
bitOffset_1 += length; | ||
}); | ||
@@ -664,3 +675,3 @@ ctx.bitFields = []; | ||
}; | ||
Parser.prototype.generateSkip = function (ctx) { | ||
Parser.prototype.generateSeek = function (ctx) { | ||
var length = ctx.generateOption(this.options.length); | ||
@@ -814,3 +825,3 @@ ctx.pushCode("offset += " + length + ";"); | ||
Object.keys(this.options.choices).forEach(function (tag) { | ||
var type = _this.options.choices[tag]; | ||
var type = _this.options.choices[parseInt(tag, 10)]; | ||
ctx.pushCode("case " + tag + ":"); | ||
@@ -882,2 +893,6 @@ _this.generateChoiceCase(ctx, _this.varName, type); | ||
}; | ||
Parser.prototype.generateSaveOffset = function (ctx) { | ||
var varName = ctx.generateVariable(this.varName); | ||
ctx.pushCode(varName + " = offset"); | ||
}; | ||
return Parser; | ||
@@ -884,0 +899,0 @@ }()); |
{ | ||
"name": "binary-parser", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Blazing-fast binary parser builder", | ||
@@ -14,7 +14,8 @@ "main": "dist/binary_parser.js", | ||
"scripts": { | ||
"build": "npx tsc", | ||
"fmt": "npx prettier --write \"{lib,example,test}/**/*.{ts,js}\"", | ||
"check-fmt": "npx prettier --list-different \"{lib,example,test}/**/*.{ts,js}\"", | ||
"test": "npx mocha", | ||
"cover": "npx nyc --reporter html mocha", | ||
"build": "tsc", | ||
"fmt": "prettier --write \"{lib,example,test}/**/*.{ts,js}\"", | ||
"check-fmt": "prettier --list-different \"{lib,example,test}/**/*.{ts,js}\"", | ||
"test": "mocha", | ||
"test-browser": "parcel test/browser.html --open", | ||
"cover": "nyc --reporter html mocha", | ||
"prepare": "npm run build" | ||
@@ -21,0 +22,0 @@ }, |
@@ -289,5 +289,32 @@ # Binary-parser | ||
### skip(length) | ||
Skip `length` bytes. | ||
### saveOffset(name [,options]) | ||
Save the current buffer offset as key `name`. This function is only useful | ||
when called after another function which would advance the internal buffer | ||
offset. | ||
```javascript | ||
var parser = new Parser() | ||
// this call advances the buffer offset by | ||
// a variable (i.e. unknown to us) number of bytes | ||
.string('name', { | ||
zeroTerminated: true | ||
}) | ||
// this variable points to an absolute position | ||
// in the buffer | ||
.uint32('seekOffset') | ||
// now, save the "current" offset in the stream | ||
// as the variable "currentOffset" | ||
.saveOffset('currentOffset') | ||
// finally, use the saved offset to figure out | ||
// how many bytes we need to skip | ||
.seek(function() { | ||
return this.seekOffset - this.currentOffset; | ||
}) | ||
... // the parser would continue here | ||
``` | ||
### seek(relOffset) | ||
Move the buffer offset for `relOffset` bytes from the current position. Use a | ||
negative `relOffset` value to rewind the offset. Previously named `skip(length)`. | ||
### endianess(endianess) | ||
@@ -294,0 +321,0 @@ Define what endianess to use in this parser. `endianess` can be either |
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
56193
959
484
0