bitsyntax
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
module.exports.parse = require('./lib/parse').parse; | ||
@@ -2,0 +4,0 @@ module.exports.match = require('./lib/interp').match; |
@@ -1,3 +0,5 @@ | ||
// Compile patterns to recognisers | ||
// Compile patterns to recognisers and constructors | ||
'use strict'; | ||
require('buffer-more-ints'); | ||
@@ -107,9 +109,16 @@ var $ = require('util').format; | ||
} | ||
$line("if (result === false) { return false; }"); | ||
$line("if (result === false) return false;"); | ||
if (segment.name) { | ||
$line("else { %s = result; }", var_name(segment.name)); | ||
// variable is given a value in the environment | ||
$line("else if (%s !== undefined) {", var_name(segment.name)); | ||
// .. and it is not the same as that matched | ||
$line("if (%s != result) return false;", | ||
var_name(segment.name)); | ||
$line("}"); | ||
// variable is free | ||
$line('else %s = result;', var_name(segment.name)); | ||
} | ||
else { | ||
var repr = JSON.stringify(segment.value); | ||
$line("else if (result != %s) { return false; }", repr); | ||
$line("else if (result != %s) return false;", repr); | ||
} | ||
@@ -126,3 +135,3 @@ } | ||
for (var i = 0; i < segments.length; i++) { | ||
name = segments[i].name; | ||
var name = segments[i].name; | ||
if (name && name !== '_') { | ||
@@ -141,11 +150,11 @@ names[name] = true; | ||
$start(); | ||
$line("return function(binary, vars) {"); | ||
$line("var bin = binary, scope = vars || {};"); | ||
$line("return function(binary, env) {"); | ||
$line("'use strict';"); | ||
$line("var bin = binary, env = env || {};"); | ||
$line("var offset = 0, binsize = bin.length * 8;"); | ||
$line("var bits, result, byteoffset;"); | ||
var varnames = variables(segments); | ||
var bindings = ""; | ||
for (var v = 0; v < varnames.length; v++) { | ||
var name = varnames[v]; | ||
$line("var %s = scope['%s'];", var_name(name), name); | ||
$line("var %s = env['%s'];", var_name(name), name); | ||
} | ||
@@ -161,3 +170,3 @@ | ||
$line("if (offset == binsize) {"); | ||
$line("var bindings = {"); | ||
$line("return {"); | ||
for (var v = 0; v < varnames.length; v++) { | ||
@@ -167,5 +176,5 @@ var name = varnames[v]; | ||
} | ||
$line('}'); | ||
$line("return bindings; }"); | ||
$line("else { return false; }"); | ||
$line('};'); | ||
$line('}'); // if offset == binsize | ||
$line("else return false;"); | ||
$line("}"); // end function | ||
@@ -220,3 +229,2 @@ | ||
else { | ||
// could do this statically of course | ||
$line('size = %d;', (segment.size * segment.unit) / 8); | ||
@@ -263,6 +271,3 @@ } | ||
function compile_write(segments) { | ||
$start(); | ||
$line('return function(buf, offset, bindings) {'); | ||
function emit_write(segments) { | ||
$line('var val, size;'); | ||
@@ -276,20 +281,17 @@ | ||
} | ||
$line('return offset;'); | ||
$line('}'); // end function | ||
var fn = new Function('write_int', 'write_float', $result()); | ||
return fn(write_int, write_float); | ||
} | ||
function compile_ctor(segments) { | ||
var writer = compile_write(segments); | ||
$start(); | ||
$line('return function(bindings) {'); | ||
$line("'use strict';"); | ||
size_of(segments); | ||
$line('var buf = new Buffer(buffersize);'); | ||
$line('write(buf, 0, bindings);'); | ||
$line('var offset = 0;'); | ||
emit_write(segments); | ||
$line('return buf;'); | ||
$line('}'); // end function | ||
return new Function('write', $result())(writer); | ||
return new Function('write_int', 'write_float', | ||
$result())(write_int, write_float); | ||
} | ||
@@ -303,3 +305,2 @@ | ||
}; | ||
module.exports.compile_write = compile_write; | ||
module.exports.compile_builder = function() { | ||
@@ -306,0 +307,0 @@ var str = [].join.call(arguments, ','); |
@@ -5,2 +5,4 @@ // -*- js-indent-level: 2 -*- | ||
'use strict'; | ||
var ints = require('buffer-more-ints'); | ||
@@ -7,0 +9,0 @@ |
@@ -34,2 +34,4 @@ // -*- js-indent: 2 -*- | ||
'use strict'; | ||
var ints = require('buffer-more-ints'); | ||
@@ -36,0 +38,0 @@ |
// Parse patterns in string form into the form we use for interpreting | ||
// (and later, for compiling). | ||
'use strict'; | ||
var ast = require('./pattern'); | ||
@@ -5,0 +7,0 @@ var parser = require('./parser'); |
// -*- js-indent-level: 2 -*- | ||
// Constructing patterns | ||
'use strict'; | ||
function set(values) { | ||
@@ -5,0 +7,0 @@ var s = {}; |
@@ -8,3 +8,3 @@ { | ||
"description": "Pattern-matching on byte buffers", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"repository": { | ||
@@ -27,4 +27,4 @@ "type": "git", | ||
"pegjs": "0.7.x", | ||
"mocha": "0.9.x" | ||
"mocha": "1.x" | ||
} | ||
} |
@@ -59,4 +59,4 @@ # Byte-wise matching for Node.JS | ||
return either a map of bindings, or `false`, given a buffer and | ||
optionally an environment. The environment contains values for the | ||
bound variables in the pattern (if there are any). | ||
optionally an environment. The environment contains values for bound | ||
variables in the pattern (if there are any). | ||
@@ -75,4 +75,4 @@ ```js | ||
```js | ||
var p = bitsyntax.matcher('"foo:", str/binary'); | ||
p(new Buffer("bar:humbug")); | ||
var p = bitsyntax.matcher('"foo=", str/binary'); | ||
p(new Buffer("bar=humbug")); | ||
// => false | ||
@@ -166,3 +166,5 @@ ``` | ||
will be bound to that variable name for the rest of the pattern. If a | ||
literal value is given, the matched value must equal that value. | ||
literal value is given, the matched value must equal that value. If a | ||
variable's value is given in the environment, the matched value must | ||
equal the provided value. | ||
@@ -261,4 +263,4 @@ When used in a builder, the literal value will be copied into the | ||
When used in a builder, a quoted string is copied verbatim into the | ||
result. | ||
When used in a builder, a quoted string is copied into the result as | ||
the bytes of its UTF8 encoding. | ||
@@ -304,1 +306,6 @@ ## Examples | ||
remaining (possibly zero-length) binary as `rest`. | ||
s:8, key:s/binary, value/binary | ||
When given the environment `{s:6, key: "foobar"}`, will match a binary | ||
starting with [6, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, ...]. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents 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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
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
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents 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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
75367
2059
307
2
2