Using sequence items

There is a way to filter out the item with a condition. has_crc32 is a ubyte which then referred in the condition as has_crc32 != 0

In this example parser can handle variable length payloads: 1 or 5 bytes.

meta:
  id: if_simple
seq:
  - id: has_crc32
    type: u1
  - id: crc32
    type: u4
    if: has_crc32 != 0

Enums

Readability can be also improved in some cases by using enums. The following example also shows how to filter out sequence items based on previously read values. In this case, dog_tag value will be only parsed if the animal is dog.

seq:
  - id: my_animal
    type: u1
    enum: animal
  - id: dog_tag
    type: u4
    # Comparing to enum literal
    if: my_animal == animal::dog
enums:
  animal:
    1: cat
    2: dog

Instances

This example can be used to produce instance only if needed.

Instances

meta:
  id: if_simple
seq:
  - id: has_crc32
    type: u1
instances:
  result:
   if: has_crc32 != 0
   value: has_crc32 != 0