Skip to content

Instantly share code, notes, and snippets.

@jadonk
Forked from ravikp7/output
Last active June 2, 2017 14:06
Show Gist options
  • Save jadonk/e3796e7373a1ce1ac5d441a642b4ea33 to your computer and use it in GitHub Desktop.
Save jadonk/e3796e7373a1ce1ac5d441a642b4ea33 to your computer and use it in GitHub Desktop.
parsing test
<Buffer ff ff ff ff ff ff a0 f6 fd 8a e9 1a 08 00 03>
{ h_dest: [ 255, 255, 255, 255, 255, 255 ],
h_source: [ 160, 246, 253, 138, 233, 26 ],
h_proto: 2048 }
{
"name": "parsing-test",
"version": "1.0.0",
"description": "Dummy test of binary-parser and schemapack",
"main": "serializer.js",
"dependencies": {
"binary-parser": "^1.1.5",
"schemapack": "^1.4.2"
},
"devDependencies": {},
"scripts": {
"test": "node serializer.js > output"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/e3796e7373a1ce1ac5d441a642b4ea33.git"
},
"author": "Ravi Kumar Prasad",
"license": "MIT",
"bugs": {
"url": "https://gist.github.com/e3796e7373a1ce1ac5d441a642b4ea33"
},
"homepage": "https://gist.github.com/e3796e7373a1ce1ac5d441a642b4ea33"
}
var bp = require('binary-parser');
var Parser = bp.Parser;
// schemapack
var sp = require('schemapack');
var ethhdr_e = sp.build([
{ h_dest:{0:'uint8',1:'uint8',2:'uint8',3:'uint8',4:'uint8',5:'uint8'} },
{ h_source:{0:'uint8',1:'uint8',2:'uint8',3:'uint8',4:'uint8',5:'uint8'} },
{ h_proto : 'uint16' },
{ pad: 'string' }
]);
var eth = [
{ h_dest : [ 255, 255, 255, 255, 255, 255 ] },
{ h_source : [ 160, 246, 253, 138, 233, 26 ] },
{ h_proto : 2048 }
];
// encode
var schbuf = ethhdr_e.encode(eth);
// Parsing
var ethhdr = new Parser()
.array('h_dest',{
type: 'uint8',
length: 6
})
.array('h_source',{
type: 'uint8',
length: 6
})
.int16be('h_proto');
console.log(schbuf);
console.log(ethhdr.parse(schbuf));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment