Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

binary-parser

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binary-parser - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

2

example/bmp.js

@@ -15,3 +15,3 @@ var Parser = require('../lib/binary_parser.js').Parser;

length: 2,
assert: function(s) {return s === 'BM';}
assert: 'BM'
})

@@ -18,0 +18,0 @@ .uint32('size')

@@ -94,3 +94,3 @@ var Parser = require('../lib/binary_parser').Parser;

.endianess('big')
.uint32('magic', {assert: function(x) {return x === 0xcafebabe; }})
.uint32('magic', {assert: 0xcafebabe})
.uint16('minor_version')

@@ -97,0 +97,0 @@ .uint16('major_version')

@@ -173,8 +173,10 @@ //========================================================================================

Parser.prototype.endianess = function(endianess) {
endianess = endianess.toLowerCase();
if (endianess === 'little') {
switch (endianess.toLowerCase()) {
case 'little':
this.endian = 'le';
} else if (endianess === 'big') {
break;
case 'big':
this.endian = 'be';
} else {
break;
default:
throw new Error('Invalid endianess: ' + endianess);

@@ -309,3 +311,15 @@ }

ctx.pushCode('if (!({0})({1})) {', this.options.assert, varName);
switch (typeof this.options.assert) {
case 'function':
ctx.pushCode('if (!({0}).call(vars, {1})) {', this.options.assert, varName);
break;
case 'number':
ctx.pushCode('if ({0} !== {1}) {', this.options.assert, varName);
break;
case 'string':
ctx.pushCode('if ("{0}" !== {1}) {', this.options.assert, varName);
break;
default:
throw new Error('Assert option supports only strings, numbers and assert functions.');
}
ctx.generateError('"Assert error: {0} is " + {0}', varName);

@@ -489,2 +503,6 @@ ctx.pushCode('}');

Parser.prototype.isInteger = function() {
return !!this.type.match(/U?Int[8|16|32][BE|LE]?|Bit\d+/);
};
//========================================================================================

@@ -521,8 +539,9 @@ // class Context

Context.prototype.generateOption = function(val) {
if (typeof val === 'number') {
return val.toString();
} else if (typeof val === 'string') {
return this.generateVariable(val);
} else if (typeof val === 'function') {
return '(' + val + ').call(' + this.generateVariable() + ')';
switch(typeof val) {
case 'number':
return val.toString();
case 'string':
return this.generateVariable(val);
case 'function':
return '(' + val + ').call(' + this.generateVariable() + ')';
}

@@ -529,0 +548,0 @@ };

{
"name": "binary-parser",
"version": "1.0.0",
"version": "1.1.0",
"description": "Blazing-fast binary parser builder",

@@ -5,0 +5,0 @@ "main": "lib/binary_parser.js",

@@ -5,9 +5,18 @@ # Binary-parser

Binary-parser is a parser builder library for [node](http://nodejs.org),
which enables you to write efficient binary parsers in a simple & declarative way.
It supports all common data types required to parse a structured binary data,
such as integers, bit fields, floating point numbers, strings, arrays
(both fixed length and variable length), etc.
Binary-parser dynamically generates and compiles the parser code on-the-fly.
Binary-parser is a binary parser builder library for [node](http://nodejs.org),
which enables you to write efficient parsers in a simple & declarative way.
It supports all common data types required to analyze a structured binary data.
Binary-parser dynamically generates and compiles the parser code on-the-fly,
which runs as fast as a hand-written parser (which takes much more time and effort to write).
Supported data types are:
- Integers (supports 8, 16, 32 bit signed- and unsigned integers)
- Floating point numbers (supports 32 and 64 bit floating point values)
- Bit fields (supports bit fields with length from 1 to 32 bits)
- Strings (supports various encodings, fixed-length and variable-length, zero terminated string)
- Arrays (supports user-defined element type, fixed-length and variable-length)
- Choices
- User defined types
This library's features are inspired by [BinData](https://github.com/dmendel/bindata)

@@ -17,3 +26,3 @@ , its syntax by [binary](https://github.com/substack/node-binary).

## Installation
In your project's directory, execute:
Binary-parser can be installed with [npm](https://npmjs.org/):

@@ -25,22 +34,39 @@ ```shell

## Quick Start
First create an empty Parser object with `new Parser()`, then chain methods to build the desired parser.
Calling `Parser.parse` with an `Buffer` object returns the result object.
1. Create an empty Parser object with `new Parser()`.
2. Chain builder methods to build the desired parser. (See [API](https://github.com/Keichi/binary-parser#api) for detailed document
of each methods)
3. Call `Parser.prototype.parse` with an `Buffer` object passed as argument.
4. Parsed result will be returned as an object.
```javascript
// Module import
var Parser = require('binary-parser').Parser;
var keyValue = new Parser()
.int32le('key')
.int16le('length')
.string('message', {length: 'length'});
var parser = new Parser()
.uint16le('count')
.array('kvs', {
type: keyValueParser,
length: 'count'
// Build an IP packet header Parser
var ipHeader = new Parser()
.endianess('big')
.bit4('version')
.bit4('headerLength')
.uint8('tos')
.uint16('packetLength')
.uint16('id')
.bit3('offset')
.bit13('fragOffset')
.uint8('ttl')
.uint8('protocol')
.uint16('checksum')
.array('src', {
type: 'uint8',
length: 4
})
.array('dst', {
type: 'uint8',
length: 4
});
parser.parse(buffer);
// Prepare buffer to parse.
var buf = new Buffer('450002c5939900002c06ef98adc24f6c850186d1', 'hex');
// Parse buffer and show result
console.log(ipHeader.parse(buf));
```

@@ -190,10 +216,11 @@

Define what endianess to use in this parser. `endianess` can be either `'little'` or `'big'`.
After this method is called, you can omit endianess postfix from primitive parsers.
The default endianess of `Parser` is set to big-endian.
```javascript
var parser = new Parser()
// usually you have to specify endianess explicitly
.endianess('le')
// You can specify endianess explicitly
.uint16be('a')
.endianess('big')
// you can omit le/be after endianess is called
.uint32le('a')
// Or you can omit endianess (in this case, little-endian is used)
.uint16('b')

@@ -215,12 +242,24 @@ .int32('c')

- `assert` - A predicate function. You can do assertions during the parsing (useful for checking magic numbers and so on).
This assertion function should take one argument, which is the parsed result, and return
`true` if assertion successes or `false` when assertion fails.
An exception is thrown during the parsing when assertion fails.
- `assert` - Do assertion on the parsed result (useful for checking magic numbers and so on).
If `assert` is a `string` or `number`, the actual parsed result will be compared with it
with `===` (strict equality check), and an exception is thrown if they mismatch.
On the other hand, if `assert` is a function, that function is executed with one argument
(parsed result) and if it returns false, an exception is thrown.
```javascript
// simple maginc number validation
var ClassFile =
Parser.start()
.endianess('big')
.uint32('magic', {assert: function(x) {return x === 0xcafebabe; }})
.uint32('magic', {assert: 0xcafebabe})
// Doing more complex assertion with a predicate function
var parser = new Parser()
.int16le('a')
.int16le('b')
.int16le('c', {
assert: function(x) {
return this.a + this.b === x;
}
});
```

@@ -227,0 +266,0 @@

@@ -474,3 +474,3 @@ var assert = require('assert');

zeroTerminated: true,
assert: function(x) { return x === 'hello, world'; }
assert: 'hello, world'
});

@@ -486,2 +486,21 @@ var buffer = new Buffer('68656c6c6f2c20776f726c6400', 'hex');

});
parser = new Parser()
.int16le('a')
.int16le('b')
.int16le('c', {
assert: function(x) {
return this.a + this.b === x;
}
});
buffer = new Buffer('d2042e16001b', 'hex');
assert.doesNotThrow(function() {
parser.parse(buffer);
});
buffer = new Buffer('2e16001bd204', 'hex');
assert.throws(function() {
parser.parse(buffer);
});
});

@@ -488,0 +507,0 @@ it('should parse asynchronously', function() {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc