/construct

A Java version of "Construct" a python library for parsing and building of data structures (binary or textual)

Primary LanguageJavaMIT LicenseMIT

Python Construct

Construct is a powerful declarative parser for binary data.

It is based on the concept of defining data structures in a declarative
manner, rather than procedural code: Simple constructs can be combined
hierarchically to form increasingly complex data structures. It’s the first
library that makes parsing fun, instead of the usual headache it is today.

Construct features bit and byte granularity, symmetrical operation (parsing
and building), component-oriented declarative design, easy debugging and
testing, an easy-to-extend subclass system, and lots of primitive
constructs to make your work easier:

  • Fields
  • Structs
  • Unions
  • Repeaters
  • Meta constructs
  • Switches
  • On-demand parsing
  • Pointers
  • And more!

Java Construct

This Java version employs some syntactic sugar (i.e. static methods) to make the syntax as close as possible to the original Construct library in Python.

See for example


    Construct struct = BitStruct("foo",
                         BitField("a", 3),
                         Flag("b"),
                         Padding(3),
                         Nibble("c"),
                         Struct("bar",
                           Nibble("d"),
                           Bit("e")));

A Java Construct can parse byte arrays and produces Objects like Containers. Viceversa, it can take Objects to produce byte arrays.

public Object parse(byte[] data);
public byte[] build( Object obj);

Parsing example:


    Container c1 = Container( "a", 7, "b", false, "bar", Container( "d", 15 , "e", 1), "c",8);
    Container c2 = struct.parse( ByteArray( 0xe1, 0x1f ));
    assertEquals( c1, c2 );

Features

Currently Java Construct supports enough Macros, Adapters and repeaters to parse and build these protocols: