|Version 8 (modified by 12 years ago) ( diff ),
Structured Binary Data
This page will document my thoughts and design ideas for the structured binary data project. The project aims to address #317; a description of my overall approach can be found on the GSoC project page.
- Work in HelenOS—this means the code must be in C and/or an easily ported language like Lua.
- View on different layers; for instance, switch between viewing the formatted date and time for a FAT directory entry, the integers, and the original bytes.
- Check whether data is valid; handle broken data reasonably well.
- Parse pieces of the data lazily; don’t try to read everything at once.
- Work in both directions (parsing and building) without requiring too much extra effort.
- Support full modifications. Ideally, allow creation of a whole filesystem from scratch.
These formats will be interesting and/or difficult to handle. I will keep them in mind when designing the library.
- Filesystem allocation tables, which should be kept consistent with the actual usage of the disk.
- Filesystem logs, which should be applied to the rest of the disk before interpreting it.
- Formats where the whole file can have either endianness depending on a field in the header.
- The Blender file format is especially dynamic. When Blender saves a file, it just copies the structures from memory and translates the pointers. Since each Blender version and architecture will have different structures, the output file includes a header describing the fields and binary layout of each structure. When the file is loaded, the header is read first and the structures will be translated as necessary.
- If the language is powerful enough, it might be possible to have a native description of Zlib and other compression formats.
- It could be interesting to parse ARM or x86 machine code.
I am researching existing tools related to my project, so they can be used for inspiration.
Construct is a Python library for creating
declarative structure definitions. Each instance of the
Construct class has a
name, and knows how to read from a stream, write to a stream, and determine its
length. Some predefined
Construct subclasses use an arbitrary Python function
evaluated at runtime, or behave differently depending on whether
Constructs throw exceptions.
Const uses a sub‐
Construct and makes
sure the value is correct. Also has lazy
Unfortunately, if you change the size of a structure, you still have to change everything else manually.
BinData makes good use of Ruby syntax; it mostly has the same features as Construct.
DSLs in this category are used in an obvious, deterministic manner, and complex
edits (changing the length of a structure) are difficult or impossible. They
are simple imperative languages in which fields, structures, bitstructures, and
arrays can be defined. The length, decoded value, and presence of fields can be
determined by expressions using any previously decoded field, and structures
break and similar statements. Structures can
inherit from other structures, meaning that the parent’s fields are present at
the beginning of the child. Statements can move to a different offset in the
input data. There may be a real programming language that can be used along
with the DSL.
- Uses a simple stack‐based VM to calculate variable locations.
- “Grammar‐based specification and parsing of binary file formats”
- Actually uses an attribute grammar, but it isn’t terribly different from an imperative language.
- Lets you create or modify files instead of just reading them. Fields can refer to blocks of data elsewhere in the file. Uses an XML format.
- A popular tool for extracting files from video game archives. Its main strength is the broad number of compression formats supported. It can put modified files back in the archive in trivial situations.
- Synalize It!
- Not completely imperative; if you declare optional structs where part of the data is constant, the correct struct will be displayed. Has a Graphviz export of file structure. Uses an XML format.
- Other free
- BinPAC, Data::ParseBinary, DataScript, DataWorkshop, Wireshark Generic Dissector, Metafuzz BinStruct, and PADS.
- Other proprietary
- 010 Editor, Andys Binary Folding Editor, Hackman Suite, Hex Editor Neo, iBored, and WinHext.
Less interesting tools
- Simple formats in hex editors
- These support static fields and dynamic lengths only: FlexHex, HexEdit, Hexplorer, Hex Workshop, and Okteta.
- Simple formats elsewhere
- CTF, ffe, Node Packet, Scapy, and stabs can only handle trivial structures. lpack, Perl’s pack, Python’s struct, and VStruct use concise string formats to describe simple structures. Hachoir uses Python for most things.
- Protocol definition formats
- ASN.1, MIDL, Piqi, and other IPC implementations go in the other direction: they generate a binary format from a text description of a structure. ASN.1 in particular has many features.
- Wireshark and tcpdump
- As the Construct wiki notes, you would expect these developers to have some sort of DSL, but they just use C for everything. Wireshark does use ASN.1, Diameter, and MIDL for protocols developed with them.
A tool could generate C code to read and write data given a specification. A separate file could be used to specify which types should be used and which things should be read lazily or strictly.
A diff tool could show differences in the interpreted data.
Space‐filling curves look cool, but this project is about avoiding looking at raw binary data.