GithubHelp home page GithubHelp logo

binarnia's Introduction

Binarnia License NPM version Build Status Coverage

Parse binary buffer to json according to schema.

Install

npm i binarnia

How to use?

const binarnia = require('binarnia');
const schema = [{
    offset: '0x01',
    name: 'arch',
    size: 1,
    type: 'value',
}];

const buffer = Buffer.from([0x22]);

// endianness = 'LE'
binarnia({
    schema,
    buffer,
});

// returns
({
    arch: '0x22',
});

// directly pass endianness
binarnia({
    schema,
    endian: 'BE',
    buffer,
});

// returns
({
    arch: '0x22',
});

const array = [0x22];

// works with array as well
binarnia({
    schema,
    endian: 'BE',
    buffer: array,
});

// returns
({
    arch: 'x22',
});

Value

const schema = [{
    offset: '0x00',
    name: 'format',
    size: 1,
    type: 'value',
}];

const buffer = [0x03];

binarnia({
    schema,
    buffer,
});

// returns
({
    format: '0x03',
});

Decimal

const schema = [{
    offset: '0x00',
    name: 'size',
    size: 1,
    type: 'decimal',
}];

const buffer = [0x03];

binarnia({
    schema,
    buffer,
});

// returns
({
    size: '51',
});

String

const schema = [{
    offset: '0x00',
    name: 'message',
    size: 5,
    type: 'string',
}];

const buffer = [
    0x68,
    0x65,
    0x6c,
    0x6c,
    0x6f,
    0x27,
    0x00,
];

binarnia({
    schema,
    endian: 'BE',
    buffer,
});

// returns
({
    message: 'hello',
});

Bit

const schema = [{
    name: 'format',
    size: 1,
    type: 'bit',
    bit: {
        '0x1': 'MZ',
        '0x2': 'PE',
        '0x4': 'ELF',
    },
}];

const buffer = [0x03];

const result = binarnia({
    offset: '0x00',
    schema,
    buffer,
});

// returns
({
    format: ['MZ', 'PE'],
});

Enum

const binarnia = require('binarnia');
const schema = [{
    offset: '0x00',
    name: 'format',
    size: 1,
    type: 'enum',
    enum: {
        '0x22': 'MZ',
        '0x33': 'PE',
    },
}];

const buffer = Buffer.from([0x22, 0x01]);

binarnia({
    schema,
    buffer,
});

// returns
({
    format: 'MZ',
});

Links

const schema = [{
    name: 'msg_size',
    size: 1,
    type: 'value',
}, {
    name: 'msg',
    size: '<msg_size>',
    type: 'string',
}];

const buffer = [0x1, 0x31];

binarnia({
    schema,
    buffer,
});

// returns
({
    msg_size: '0x01',
    msg: '1',
});

Array

const binarnia = require('binarnia');
const schema = [{
    offset: '0x01',
    name: 'arch',
    size: 1,
    type: 'array',
    array: [
        'x32',
        'x64',
    ],
}];

const buffer = Buffer.from([0x22, 0x01]);

binarnia({
    schema,
    buffer,
});

// returns
({
    arch: 'x32',
});

Ignore

const schema = [{
    name: 'format',
    size: 2,
    type: 'value',
}, {
    name: 'reserved',
    size: 2,
    type: 'ignore',
}];

const buffer = [
    0x01,
    0x02,
    0x03,
    0x04,
];

binarnia({
    schema,
    buffer,
});

// returns
({
    format: '0x201',
});

sizeof(schema)

const schema = [{
    name: 'a',
    size: 4,
    type: 'value',
}, {
    name: 'b',
    size: 4,
    type: 'value',
}];

binarnia.sizeof(schema);
// returns
8;

License

MIT

binarnia's People

Contributors

coderaiser avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.