Skip to content

Kaitai Struct stream API

Mikhail Yakshin edited this page Mar 10, 2016 · 6 revisions

All source files in supported languages generated by a Kaitai Struct compiler have a goal to be human-readable, thus they utilize an extra layer of stream API. This API is followed by Kaitai Struct runtime libraries:

Obviously, languages differ and thus API has slight differences, but in the nutshell, the general idea is the same. All generated code uses the same set of operations to parse primitive types one-by-one.

Integers

One can read integers using one of read_$S$L$E operations, where:

  • $S is either u if we want to read unsigned integer or s if we want signed one;
  • $L is length of integer type in bytes. 1, 2, 4 and 8 bytes are supported;
  • $E is endianness (order of bytes): l for little-endian or b for big-endian;

A few examples:

  • read_u8le - reads 8-byte (64-bit) unsigned integer, little-endian (AKA Intel, AKA VAX, etc)
  • read_s2be - reads 2-byte (16-bit) signed integer, big-endian (AKA "network byte order", AKA Power, AKA Motorola, etc)
  • read_u1 - reads 1-byte unsigned integer - no endianness is given as it's pointless to do so

Basically, it's the same designation as used in the type clause in .ksy format.

Byte arrays

Strings

Clone this wiki locally