#byte-array #json-parser #generic #define #output #parsed #send

kampu

Generic byte array parser function where you can define the structure/tree of the bytes in JSON, send it a byte array and get the parsed output in JSON

10 releases

0.1.11 Aug 13, 2024
0.1.10 Aug 7, 2024

#994 in Data structures

Apache-2.0 and AGPL-3.0-only

30KB
634 lines

Kampu

Generic byte array parser function where you can define the structure/tree of the bytes in JSON, send it a byte array and get the parsed output in JSON.

'Kampu' is the name a large tree common in Thailand, it has very large branches that could be allegorical to the complex bit/byte/loops/matches that this parser aims to handle.

Usage

See the tests folder for examples, a simple example below:

fn test_simple_packet() {
    const tree_id:u64 = 1;
    let data = hex_to_bin("01 00 E8 03");
    plant_tree(
        tree_id,
        json!({
                "branches": [
                    { "name": "fix_status", "type": "u8" },
                    { "name": "rcr", "type": "u8" },
                    { "name": "millisecond", "type": "u16_le" }
                ]
            })
    );
    let parsed_json = parse_tree(tree_id, &data);
    assert_eq!(parsed_json, json!({
            "fix_status": 1,
            "rcr": 0,
            "millisecond": 1000
        }));
}

History

Inspired by the 'data' folder of libqmi and its python scripts to gen C code to match. Some GNSS device manufactureres sent new GNSS devices where they had their own packet structures to be added for the bluetooth_gnss app. Manually coding of these parsers and more complex protocols seem to call for a generic parser tool that could offer a simple way to define structures in JSON and have the safety, portability and performance of rust in which GPT helped draft most of the initial code in this repo and would hopefully be benefical to other common protocol dissector works too.

Dependencies

~1.8–3MB
~55K SLoC