Next: Bindat Functions, Up: Byte Packing
To control unpacking and packing, you write a data layout specification, a special nested list describing named and typed fields. This specification controls length of each field to be processed, and how to pack or unpack it. We normally keep bindat specs in variables whose names end in “-bindat-spec”; that kind of name is automatically recognized as “risky.”
A field's type describes the size (in bytes) of the object
that the field represents and, in the case of multibyte fields, how
the bytes are ordered within the field. The two possible orderings
are “big endian” (also known as “network byte ordering”) and
“little endian.” For instance, the number #x23cd (decimal
9165) in big endian would be the two bytes #x23 #xcd;
and in little endian, #xcd #x23. Here are the possible
type values:
u8byteu16wordshortu24u32dwordlongu16ru24ru32rstr lenstrz lenvec lenip[127 0 0 1] for localhost.
bits len8 *
len − 1 and ending with zero. For example: bits
2 unpacks #x28 #x1c to (2 3 4 11 13) and
#x1c #x28 to (3 5 10 11 12).
(eval form)A field specification generally has the form ([name]
handler). The square braces indicate that name is
optional. (Don't use names that are symbols meaningful as type
specifications (above) or handler specifications (below), since that
would be ambiguous.) name can be a symbol or the expression
(eval form), in which case form should evaluate to
a symbol.
handler describes how to unpack or pack the field and can be one of the following:
eval formbindat-rawbindat-idxstructlastfill lenalign lenstruct spec-nameunion form (tag spec)...(eval expr), evaluate
expr with the variable tag dynamically bound to the value
of form. A non-nil result indicates a match.
equal to the value of form.
t.
repeat count field-specs...