Integration with schemas

Add a "script" field to the schema JSON. The script runs after the normal tag evaluation, so all aliases from the JSON tags are available.

Schema example

{
  "version": 1,
  "script": "png_parser.py",
  "tags": [
    {
      "name": "Signature",
      "start_offset": 0,
      "length": 8,
      "type": "magic",
      "value": "89504e470d0a1a0a",
      "color": "4444aa"
    },
    {
      "name": "First chunk length",
      "length": 4,
      "alias": "chunk_len"
    }
  ]
}

The "script" path is resolved relative to the schema file. If the schema is at schemas/png.json, then "script": "png_parser.py" looks for schemas/png_parser.py.

Script example (png_parser.py)

import hexedit

# Aliases from JSON tags are already available
chunk_len = hexedit.get_alias("chunk_len")

# Parse all PNG chunks starting after the signature
offset = 8
while offset + 8 <= hexedit.file_size():
    length = hexedit.read_uint32(offset, big_endian=True)
    chunk_type = hexedit.read_bytes(offset + 4, 4).decode('ascii', errors='replace')

    hexedit.add_tag(
        name=f"Chunk: {chunk_type}",
        offset=offset,
        length=12 + length,  # length + type + data + crc
        color="44aa44"
    )

    hexedit.add_tag(
        name=f"  Length",
        offset=offset,
        length=4,
        color="888844"
    )

    hexedit.add_tag(
        name=f"  Type: {chunk_type}",
        offset=offset + 4,
        length=4,
        type="string",
        color="4444aa"
    )

    if length > 0:
        hexedit.add_tag(
            name=f"  Data",
            offset=offset + 8,
            length=length,
            type="blob",
            color="448844"
        )

    hexedit.add_tag(
        name=f"  CRC",
        offset=offset + 8 + length,
        length=4,
        color="884444"
    )

    offset += 12 + length

    if chunk_type == "IEND":
        break