Expressions
An Expression is a small formula that computes a number from constants and Fields read
earlier: a size, a count, a condition or a switch subject. It is written as a YAML
string.
name: demo
fields:
- { name: header, type: bytes, size: "4 * 6 + 2" }
- { name: count, type: uint8 }
- { name: records, type: bytes, size: "count * 8" }
This version reads Expressions in size:, in the count: of an array, and in
if:.
Writing an Expression in YAML
Quote every Expression with "...". Some unquoted ones would parse as YAML (4 * 6), but
others would not: one starting with * or ! means something else in YAML, and # starts a
comment. A plain YAML integer such as size: 16 is a constant, not an Expression.
Values
| Value | Example |
|---|---|
| decimal integer | 42, 1_000 |
hexadecimal integer, 0x or 0X | 0x1F, 0xFFFF_0000 |
binary integer, 0b or 0B | 0b1010_0001 |
| a Field read earlier, by its path | length, header.length |
| a built-in | _offset, _remaining |
| a parenthesized Expression | (length + 4) |
_separates digits anywhere after the first one. There are no character literals and no strings.- A literal starting with
0followed by more digits, such as010, isleading-zero-literal: it would be octal in C, and Expressions have no octal. - A literal must fit in 64 bits. Above
0x7FFF_FFFF_FFFF_FFFFit wraps to a negative value, so0xFFFF_FFFF_FFFF_FFFFis-1. - A name starting with
_is a built-in, which is why Field names cannot start with_._offsetis the number of bytes from the start of the message, and_remainingthe number of bytes left in it.
Operators
From the tightest binding to the loosest:
| Operators | Meaning |
|---|---|
! ~ - (unary) | not (1 for zero, else 0), bitwise complement, negation |
* / % | multiplication, division, remainder |
+ - | addition, subtraction |
<< >> | shifts |
& | bitwise and |
^ | bitwise exclusive or |
| | bitwise or |
== != < <= > >= | comparisons |
&& | logical and |
|| | logical or |
Operators of one row group from the left: a - b - c is (a - b) - c.
The order is Rust’s and Python’s, not C’s: bitwise operators bind tighter than comparisons,
so a mask test means what it looks like. flags & 0x80 == 0x80 is (flags & 0x80) == 0x80,
where C would read flags & (0x80 == 0x80).
Comparisons do not chain: a < b < c is
chained-comparison. Write a < b && b < c, or
(a < b) < c to compare the first result.
An Expression nests at most 64 levels deep. Each value, parenthesis and unary operator is a
level, and so is each binary operator over its operands: x is 1 deep, -x and (x) are 2
deep, and a + a + … + a with 64 operands is 64 deep. A deeper Expression is
expression-too-deep, because Lua could not load the
Dissector it makes. No Expression written by hand comes close.
Any other mistake, such as a missing value or parenthesis or a character Expressions do not
use, is expression-syntax, pointing at the token.
name: demo
fields: [ {name: data, type: bytes, size: "1 < 2 < 3"} ]
Arithmetic
Every value is a signed 64-bit integer, and the arithmetic is C’s:
/truncates toward zero and%takes the sign of the left side:-7 / 2is-3and-7 % 2is-1. Lua’s own operators would give-4and1.>>is arithmetic: it keeps the sign, so-8 >> 1is-4.- Overflow wraps around:
0x7FFF_FFFF_FFFF_FFFF + 1is the most negative value. - Comparisons,
!,&&and||give 1 or 0, and any value other than 0 counts as true. &&and||stop as soon as the result is known: in0 && 1 / 0, the division is never done.
name: demo
fields:
- { name: masked, type: bytes, size: "0x1234 & 0xF" }
- { name: rounded, type: bytes, size: "(13 + 3) / 4 * 4" }
- { name: either, type: bytes, size: "(2 > 1 || 0) + 1" }
A constant size: that is negative, divides by zero or shifts by a count outside 0 to 63 is
invalid-size, and such a count: is
invalid-count.
name: demo
fields: [ {name: data, type: bytes, size: "2 - 4"} ]
References
A name in an Expression reads the value of a Field read earlier in the packet.
- Nearest first. A name is looked up among the earlier Fields of the structure the
Expression is in, then among the earlier Fields of each enclosing structure, out to the
top-level
fields:. The nearest Field of that name wins. - Dotted paths go into a structure or a Field of a Type read earlier:
hdr.len,request.header.flags, and into the sub-Fields of a bitfield:flags.ext. - Only earlier Fields. A Field read later, or the Field the Expression belongs to, is
forward-reference. A name that matches nothing isunknown-field, and so is a path into the structure still being read: insidehdr, writelen, nothdr.len. - Not into arrays. A Field inside an element of an array can be read only from
inside that element: after
items,items.lenisunknown-field, since each element has its ownlen. - Not into conditionals. A Field under
if:, or inside one, can be read only from inside the Field carrying theif:; from anywhere else it isconditional-reference. - Only numbers. Integer and
boolFields and bitfield sub-Fields can be read; aboolor a boolean sub-Field reads as 1 or 0. Astring,bytes, float, address, timestamp or structure isnon-numeric-reference.
name: demo
fields:
- { name: length, type: uint8 }
- name: header
fields:
- { name: length, type: uint16 }
- { name: compressed, type: bool }
- { name: options, type: bytes, size: "length - 4" }
- { name: body, type: bytes, size: "header.length * (header.compressed + 1)" }
- { name: trailer, type: bytes, size: length }
Here options reads header.length, the nearest length, and trailer reads the top-level
one.
Inside a Type
A Type can read its own Fields, and also names it does not declare. Those are resolved at each place the Type is used, in the scope of that use, so one Type can read a different Field at each use. An error is reported at the use that cannot resolve the name, with a note pointing at the name in the Type.
name: demo
fields:
- { name: size, type: uint8 }
- { name: request, type: Block }
- name: reply
fields:
- { name: size, type: uint16 }
- { name: block, type: Block }
types:
Block:
fields:
- { name: kind, type: uint8 }
- { name: data, type: bytes, size: "size - 1" }
request.data reads the top-level size, and reply.block.data reads reply.size.
Built-ins
| Built-in | Value |
|---|---|
_offset | the number of bytes from the start of the message to the Field being read |
_remaining | the number of bytes left in the message from the Field being read |
size: "_remaining" reads everything left (see Sizes). Under
Framing, the message is the one Framing cut out of the TCP stream, so both count
from its start.
name: demo
fields:
- { name: kind, type: uint8 }
- { name: align, type: bytes, size: "(4 - _offset % 4) % 4" }
- { name: payload, type: bytes, size: "_remaining" }
In the packet
An Expression that reads Fields is computed when the packet is read, with the arithmetic above.
A Field value is a signed 64-bit integer, so a uint64 above 0x7FFF_FFFF_FFFF_FFFF reads as a
negative number.
When the value cannot be used, the Dissector shows what it read so far, adds a “Malformed”
expert info error on the Field, filtered as <protocol>._malformed.expression, and stops:
- a division or remainder by zero;
- a shift by a count outside 0 to 63;
- a negative size or count, which includes a
uint64above0x7FFF_FFFF_FFFF_FFFF.
A size or count larger than what is left of the packet is a truncation, as for any Field that does not
fit: “Malformed: truncated”, filtered as <protocol>._malformed.truncated.