Giter VIP home page Giter VIP logo

binaryformat's Introduction

BinaryFormat Build Publish

.NET helpers for parsing various binary formats.

BinaryFormat NuGet Badge

Commonly used types:

  • BinaryFormatReader

BinaryFormat.Network NuGet Badge

Commonly used types:

  • MacAddress

BinaryFormat.EthernetFrame NuGet Badge

Commonly used types:

  • L2EthertnetFrame
  • VLANTag

BinaryFormat.IPv4 NuGet Badge

Commonly used types:

  • IPv4Packet

BinaryFormat.Udp NuGet Badge

Commonly used types:

  • UdpPacket

Usage

var buffer = new byte[] { 0x81, 0x00, 0x00, 0x7b };

// create reader 
var reader = new BinaryFormatReader(buffer);

// read vlan tag
var tag = reader.ReadVLANTag();

// output parsed information
Console.WriteLine($"TPID={tag.TPID}");
Console.WriteLine($"PCP={tag.PCP}");
Console.WriteLine($"DEI={tag.DEI}");
Console.WriteLine($"VID={tag.VID}");

Adding support for new formats

  1. declare structure to hold parsed information
public ref struct VLANTagShape
{
    public ushort TPID;
    public byte PCP;
    public bool DEI;
    public ushort VID;
}
  1. write extension to read data from BinaryFormatReader
public static class VLANTagShapeExtensions
{
    // separating actual reading logic to private method makes it easier not to 
    // read from wrong reader, see `TryReadVLANTag`
    private static bool TryReadVLANTagInternal(ref BinaryFormatReader reader, ref VLANTagShape vlanTag)
    {
        vlanTag.TPID = reader.ReadUInt16();
 
        var tci = reader.ReadUInt16();
        vlanTag.PCP = (byte)((tci & 0b11100000_00000000) >> 13);
        vlanTag.DEI = (tci & 0b00010000_00000000) != 0;
        vlanTag.VID = (ushort)(tci & 0b00001111_11111111);

        return true;
    }

    // main read method 
    public static bool TryReadVLANTag(this ref BinaryFormatReader reader, ref VLANTagShape vlanTag)
    {
        if (reader.Remainder.Length < 4)
        {
            return false;
        }

        // create new reader starting at current positon
        var scope = new BinaryFormatReader(reader.Remainder);

        // try read shape using the new reader
        if (!TryReadVLANTagInternal(ref scope, ref vlanTag))
        {
            return false;
        }

        // when successful, advance original reader by number of bytes read
        reader.Skip(scope.Index);
        return true;
    }

    // helper method for top level reads
    public static VLANTagShape ReadVLANTag(this ref BinaryFormatReader reader)
    {
        var vlanTag = new VLANTagShape();

        if (!TryReadVLANTag(ref reader, ref vlanTag))
        {
            throw new InvalidOperationException($"Cannot read {nameof(VLANTagShape)}");
        }

        return vlanTag;
    }
}

binaryformat's People

Contributors

kukkimonsuta 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.