Coder Social home page Coder Social logo

sdnv's Introduction

SDNV Build Status

Self-Delimiting Numeric Value Codec

This package allows you to use Self-Delimiting Numeric Values (SDNVs) in Node.js programs. It is basically a wrapper class that encloses basic Node Buffers encoded as SDNVs. Additionaly, it also provides you utility functions to encode and decode a Buffer on-demand.

What are SDNVs?

SDNVs were created by the DTNRG and they aim to overcome common problems and limitations of fixed size fields in networking protocols (e.g. TPC advertised received window field or even the entire IPv4 header).

As stated in the official spec released as RFC 6256:

An SDNV is simply a way of representing non-negative integers (both positive integers of 
arbitrary magnitude and 0) without expending much unnecessary space.

SDNVs closely resemble certain constructs with ASN.1 but they are focused exclusively on numeric strings or bistrings unlike the latter which was developed for encoding more complex data structures.

How do they work?

An SDNV is a numeric value encoded in N octets, the last of which has its most significant bit (MSB) set to 0; the MSB of every other octet in the SDNV must be set to 1. The value encoded in an SDNV is the unsigned binary number obtained by concatenating into a single bit string the 7 least significant bits of each octet of the SDNV.

The following examples illustrate the encoding scheme for various hexadecimal values:

0xABC  : 1010 1011 1100
         is encoded as
         {1 00 10101} {0 0111100}
         = 10010101 00111100
         
0x1234 : 0001 0010 0011 0100
       =    1 0010 0011 0100
         is encoded as
         {1 0 100100} {0 0110100}
         = 10100100 00110100
         
0x4234 : 0100 0010 0011 0100
       =  100 0010 0011 0100
         is encoded as
         {1 000000 1} {1 0000100} {0 0110100}
         = 10000001 10000100 00110100
         
0x7F   : 0111 1111
       =  111 1111
         is encoded as
         {0 1111111}
         = 01111111

Using the package

var SDNV = require('sdnv');

// Using the SDNV constructor automatically wraps the encoded buffer value
var sdnv = new SDNV(new Buffer([0x12, 0x34]);

// Decode the wrapped value
sdnv.decode();  // <Buffer 12 34>

// Utility functions are also available
SDNV.encode(new Buffer[0x12, 0x34]));  // <Buffer a4 34>
SDNV.decode(new Buffer[0xa4, 0x34]));  // <Buffer 12 34>

You should provide a Node.js buffer object on input, unless you want the following to happen:

Error: the argument should be a Buffer

Also, you can create both encoding and decoding Transform Streams and pipe those to other ones:

var SDNV = require('sdnv');

// create some source stream (don't forget to emit data and "end" it later)
var Stream = require('stream');
var source = new Stream();

// use the API to create encoding or decoding streams
var encoder = SDNV.createEncodeStream();
var decoder = SDNV.createDecodeStream();

// pipe them (in this case, the output should match the input)
source.pipe(encoder).pipe(decoder);

Contributing

You know the drill.

sdnv's People

Contributors

ruiquelhas avatar

Watchers

 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.