Kaitai Struct is a declarative language used to describe various binary data structures, laid out in files or in memory: i.e. binary file formats, network stream packet formats, etc.
The main idea is that a particular format is described in Kaitai Struct language (.ksy file) and then can be compiled with ksc into source files in one of the supported programming languages. These modules will include a generated code for a parser that can read the described data structure from a file or stream and give access to it in a nice, easy-to-comprehend API.
Let's install the Python module:
python3 -m pip install --upgrade kaitaistruct
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
C:\PythonProjects\kaitai_001>python -m pip install --upgrade kaitaistruct
Collecting kaitaistruct
Downloading kaitaistruct-0.10-py2.py3-none-any.whl.metadata (2.5 kB)
Downloading kaitaistruct-0.10-py2.py3-none-any.whl (7.0 kB)
Installing collected packages: kaitaistruct
Successfully installed kaitaistruct-0.10
The Kaitai compiler can be downloaded from the official website.
After installation, you can use the compiler ...
kaitai-struct-compiler.bat --version
kaitai-struct-compiler 0.10
...
kaitai-struct-compiler.bat --help
kaitai-struct-compiler 0.10
Usage: kaitai-struct-compiler [options] ...
... source files (.ksy)
-t, --target target languages (graphviz, csharp, rust, all, perl, java, go, cpp_stl, php, lua, python, nim, html, ruby, construct, javascript)
-d, --outdir
output directory (filenames will be auto-generated); on Unix-like shells, the short form `-d` requires arguments to be preceded by `--`
-I, --import-path ;;...
.ksy library search path(s) for imports (see also KSPATH env variable)
--cpp-namespace
C++ namespace (C++ only, default: none)
--cpp-standard
C++ standard to target (C++ only, supported: 98, 11, default: 98)
--go-package Go package (Go only, default: none)
--java-package
Java package (Java only, default: root package)
--java-from-file-class
Java class to be invoked in fromFile() helper (default: io.kaitai.struct.ByteBufferKaitaiStream)
--dotnet-namespace
.NET Namespace (.NET only, default: Kaitai)
--php-namespace
PHP Namespace (PHP only, default: root package)
--python-package
Python package (Python only, default: root package)
--nim-module Path of Nim runtime module (Nim only, default: kaitai_struct_nim_runtime)
--nim-opaque Directory of opaque Nim modules (Nim only, default: directory of generated module)
--opaque-types opaque types allowed, default: false
--ksc-exceptions ksc throws exceptions instead of human-readable error messages
--ksc-json-output output compilation results as JSON to stdout
--verbose verbose output
--no-auto-read disable auto-running `_read` in constructor
--read-pos `_read` remembers attribute positions in stream
--debug same as --no-auto-read --read-pos (useful for visualization tools)
--help display this help and exit
--version output version information and exit
Steps to use this tool with Python. You need to use a defined kaitai file format for your file type - for example, gif file format, compile this kaitai then you can use it in this manner:
from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
import mmap
print('kaitai version : ', ks_version)
f = open("python_giphy.gif", "rb")
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as buf:
stream = KaitaiStream(BytesIO(buf))
obj1 = print(stream)
obj2 = print(stream)
obj3 = print(stream)
stream.close()
I only test a little but is a great tool.
Kaitai Struct is free and open-source software, licensed under the following terms: Compiler and visualizer — GPLv3+ and these Runtime libraries:
- C++/STL — MIT
- C# — MIT
- Go — MIT
- Java — MIT
- JavaScript — Apache v2
- Lua — MIT
- Nim — MIT
- Perl — MIT
- PHP — MIT
- Python — MIT
- Ruby — MIT
- Rust — MIT
- Swift — MIT
Is easier to understand if you use the IDE on the web. On the left side you can see a cloud icon for upload, first, select the kaitai GIF type from formats/image/gif.ksy from web IDE, then select a GIF file from your computer and upload.
The default IDE looks like this:
You can use the kaitai online IDE.