binary-parser
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -5,2 +5,4 @@ //======================================================================================== | ||
var Context = require("./context").Context; | ||
var PRIMITIVE_TYPES = { | ||
@@ -409,3 +411,16 @@ 'UInt8' : 1, | ||
Parser.prototype.generateString = function(ctx) { | ||
if (this.options.zeroTerminated) { | ||
if(this.options.length) { | ||
var name = ctx.generateVariable(this.varName); | ||
ctx.pushCode('{0} = buffer.toString(\'{1}\', offset, offset + {2});', | ||
name, | ||
this.options.encoding, | ||
ctx.generateOption(this.options.length) | ||
); | ||
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(); | ||
@@ -421,10 +436,2 @@ | ||
} | ||
else { | ||
ctx.pushCode('{0} = buffer.toString(\'{1}\', offset, offset + {2});', | ||
ctx.generateVariable(this.varName), | ||
this.options.encoding, | ||
ctx.generateOption(this.options.length) | ||
); | ||
ctx.pushCode('offset += {0};', ctx.generateOption(this.options.length)); | ||
} | ||
}; | ||
@@ -459,4 +466,11 @@ | ||
var lhs = ctx.generateVariable(this.varName); | ||
var item = ctx.generateTmpVariable(); | ||
var key = this.options.key; | ||
var isHash = typeof key === 'string'; | ||
ctx.pushCode('{0} = [];', lhs); | ||
if (isHash) { | ||
ctx.pushCode('{0} = {};', lhs); | ||
} else { | ||
ctx.pushCode('{0} = [];', lhs); | ||
} | ||
if (this.options.readUntil === 'eof') { | ||
@@ -469,11 +483,18 @@ ctx.pushCode('for (var {0} = 0; offset < buffer.length; {0}++) {', counter); | ||
if (typeof type === 'string') { | ||
ctx.pushCode('{0}.push(buffer.read{1}(offset));', lhs, NAME_MAP[type]); | ||
ctx.pushCode('var {0} = buffer.read{1}(offset);', item, NAME_MAP[type]); | ||
ctx.pushCode('offset += {0};', PRIMITIVE_TYPES[NAME_MAP[type]]); | ||
} else if (type instanceof Parser) { | ||
ctx.pushCode('{0}.push({});', lhs); | ||
ctx.pushCode('var {0} = {};', item); | ||
ctx.scopes.push(this.varName + '[' + counter + ']'); | ||
ctx.pushScope(item); | ||
type.generate(ctx); | ||
ctx.scopes.pop(); | ||
ctx.popScope(); | ||
} | ||
if (isHash) { | ||
ctx.pushCode('{0}[{2}.{1}] = {2};', lhs, key, item); | ||
} else { | ||
ctx.pushCode('{0}.push({1});', lhs, item); | ||
} | ||
ctx.pushCode('}'); | ||
@@ -487,5 +508,5 @@ }; | ||
} else if (type instanceof Parser) { | ||
ctx.scopes.push(varName); | ||
ctx.pushPath(varName); | ||
type.generate(ctx); | ||
ctx.scopes.pop(); | ||
ctx.popPath(); | ||
} | ||
@@ -517,5 +538,5 @@ }; | ||
ctx.pushCode('{0} = {};', ctx.generateVariable(this.varName)); | ||
ctx.scopes.push(this.varName); | ||
ctx.pushPath(this.varName); | ||
this.options.type.generate(ctx); | ||
ctx.scopes.pop(); | ||
ctx.popPath(); | ||
}; | ||
@@ -528,83 +549,2 @@ | ||
//======================================================================================== | ||
// class Context | ||
//======================================================================================== | ||
//---------------------------------------------------------------------------------------- | ||
// constructor | ||
//---------------------------------------------------------------------------------------- | ||
var Context = function() { | ||
this.code = ''; | ||
this.scopes = []; | ||
this.isAsync = false; | ||
this.bitFields = []; | ||
this.tmpVariableCount = 0; | ||
}; | ||
//---------------------------------------------------------------------------------------- | ||
// public methods | ||
//---------------------------------------------------------------------------------------- | ||
Context.prototype.generateVariable = function(name) { | ||
var arr = ['vars']; | ||
Array.prototype.push.apply(arr, this.scopes); | ||
if (name) { | ||
arr.push(name); | ||
} | ||
return arr.join('.'); | ||
}; | ||
Context.prototype.generateOption = function(val) { | ||
switch(typeof val) { | ||
case 'number': | ||
return val.toString(); | ||
case 'string': | ||
return this.generateVariable(val); | ||
case 'function': | ||
return '(' + val + ').call(' + this.generateVariable() + ')'; | ||
} | ||
}; | ||
Context.prototype.generateError = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var err = Context.interpolate.apply(this, args); | ||
if (this.isAsync) { | ||
this.pushCode('return process.nextTick(function() { callback(new Error(' + err + '), vars); });'); | ||
} else { | ||
this.pushCode('throw new Error(' + err + ');'); | ||
} | ||
}; | ||
Context.prototype.generateTmpVariable = function() { | ||
return 'tmp' + (this.tmpVariableCount++); | ||
}; | ||
Context.prototype.pushCode = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
this.code += Context.interpolate.apply(this, args) + '\n'; | ||
}; | ||
//---------------------------------------------------------------------------------------- | ||
// private methods | ||
//---------------------------------------------------------------------------------------- | ||
Context.interpolate = function(s) { | ||
var re = /{\d+}/g; | ||
var matches = s.match(re); | ||
var params = Array.prototype.slice.call(arguments, 1); | ||
if (matches) { | ||
matches.forEach(function(match) { | ||
var index = parseInt(match.substr(1, match.length - 2), 10); | ||
s = s.replace(match, params[index].toString()); | ||
}); | ||
} | ||
return s; | ||
}; | ||
//======================================================================================== | ||
// Exports | ||
@@ -611,0 +551,0 @@ //======================================================================================== |
{ | ||
"name": "binary-parser", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Blazing-fast binary parser builder", | ||
@@ -5,0 +5,0 @@ "main": "lib/binary_parser.js", |
@@ -129,6 +129,7 @@ # Binary-parser | ||
are valid. See [`Buffer.toString`](http://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end) for more info. | ||
- `length ` - (Required) Length of the string. Can be a number, string or a function. | ||
- `length ` - (Optional) Length of the string. Can be a number, string or a function. | ||
Use number for statically sized arrays, string to reference another variable and | ||
function to do some calculation. | ||
- `zeroTerminated` - (Optional, defaults to `false`) If true, then this parser reads until it reaches zero. | ||
- `stripNull` - (Optional, must be used with `length`) If true, then strip null characters from end of the string | ||
@@ -290,3 +291,3 @@ ### buffer(name [,options]) | ||
Copyright (c) 2013 Keichi Takahashi <keichi.t@me.com> | ||
Copyright (c) 2013-2014 Keichi Takahashi <keichi.t@me.com> | ||
@@ -293,0 +294,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
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
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
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
109526
17
1374
309
1