• Stars
    star
    3,850
  • Rank 11,404 (Top 0.3 %)
  • Language
    Shell
  • Created almost 9 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby

Kaitai Struct

Join the chat at https://gitter.im/kaitai_struct/Lobby

Note: if you want to make changes to the project, do not fork this repository kaitai_struct. Instead, choose the component you want to modify in the file tree above and fork that individual component instead.

This is an umbrella repository, containing the components only as submodules to make it easier to check out the entire project. Unless you want to modify this README, it is not the repo where you can make edits.

What is Kaitai Struct?

Kaitai Struct is a declarative language used for describing 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 only once and then can be compiled with a ksc into source files in one of the supported programming languages. These modules will include a generated code for a parser that can read described data structure from a file / stream and give access to it in a nice, easy-to-comprehend API.

What it's used for?

Have you ever found yourself writing repetitive, error-prone and hard-to-debug code that reads binary data structures from file / network stream and somehow represents them in memory for easier access?

Kaitai Struct tries to make this job easier β€” you only have to describe the binary format once and then everybody can use it from their programming languages β€” cross-language, cross-platform.

Kaitai Struct includes a growing collection of format descriptions, available in formats submodule repository.

Can you give me a quick example?

Sure. Consider this simple .ksy format description file that describes the header of a GIF file (a popular web image format):

meta:
  id: gif
  file-extension: gif
  endian: le
seq:
  - id: header
    type: header
  - id: logical_screen
    type: logical_screen
types:
  header:
    seq:
      - id: magic
        contents: 'GIF'
      - id: version
        size: 3
  logical_screen:
    seq:
      - id: image_width
        type: u2
      - id: image_height
        type: u2
      - id: flags
        type: u1
      - id: bg_color_index
        type: u1
      - id: pixel_aspect_ratio
        type: u1

It declares that GIF files usually have a .gif extension and use little-endian integer encoding. The file itself starts with two blocks: first comes header and then comes logical_screen:

  • "Header" consists of "magic" string of 3 bytes ("GIF") that identifies that it's a GIF file starting and then there are 3 more bytes that identify format version (87a or 89a).
  • "Logical screen descriptor" is a block of integers:
    • image_width and image_height are 2-byte unsigned ints
    • flags, bg_color_index and pixel_aspect_ratio take 1-byte unsigned int each

This .ksy file can be compiled it into Gif.cs / Gif.java / Gif.js / Gif.php / gif.py / gif.rb and then instantly one can load .gif file and access, for example, it's width and height.

In C#

Gif g = Gif.FromFile("path/to/some.gif");
Console.WriteLine("width = " + g.LogicalScreen.ImageWidth);
Console.WriteLine("height = " + g.LogicalScreen.ImageHeight);

In Java

Gif g = Gif.fromFile("path/to/some.gif");
System.out.println("width = " + g.logicalScreen().imageWidth());
System.out.println("height = " + g.logicalScreen().imageHeight());

In JavaScript

See JavaScript notes in the documentation for a more complete quick start guide.

var g = new Gif(new KaitaiStream(someArrayBuffer));
console.log("width = " + g.logicalScreen.imageWidth);
console.log("height = " + g.logicalScreen.imageHeight);

In Lua

local g = Gif:from_file("path/to/some.gif")
print("width = " .. g.logical_screen.image_width)
print("height = " .. g.logical_screen.image_height)

In Nim

let g = Gif.fromFile("path/to/some.gif")
echo "width = " & $g.logicalScreen.imageWidth
echo "height = " & $g.logicalScreen.imageHeight

In PHP

$g = Gif::fromFile('path/to/some.gif');
printf("width = %d\n", $g->logicalScreen()->imageWidth());
printf("height = %d\n", $g->logicalScreen()->imageHeight());

In Python

g = Gif.from_file("path/to/some.gif")
print "width = %d" % (g.logical_screen.image_width)
print "height = %d" % (g.logical_screen.image_height)

In Ruby

g = Gif.from_file("path/to/some.gif")
puts "width = #{g.logical_screen.image_width}"
puts "height = #{g.logical_screen.image_height}"

Of course, this example shows only a very limited subset of what Kaitai Struct can do. Please refer to the tutorials and documentation for more insights.

Supported languages

Official Kaitai Struct compiler now supports compiling .ksy into source modules for the following languages:

  • C#
  • Java
  • JavaScript
  • Lua
  • Nim
  • PHP
  • Python
  • Ruby

Downloading and installing

The easiest way to check out the whole Kaitai Struct project is to download the main project repository that already imports all other parts as submodules. Use:

git clone --recursive https://github.com/kaitai-io/kaitai_struct.git

Note the --recursive option.

Alternatively, one can check out individual subprojects that consitute Kaitai Struct suite. They are:

Using KS in your project

Typically, using formats described in KS in your project, involves the following steps:

  • Describe the format β€” i.e. create a .ksy file
  • Compile .ksy file into target language source file and include that file in your project
  • Add KS runtime library for your particular language into your project (don't worry, it's small and it's there mostly to ensure readability of generated code)
  • Use generated class(es) to parse your binary file / stream and access its components

Check out the tutorial and documentation for more information.

Licensing

  • Compiler β€” GPLv3+
  • Runtime libraries β€” MIT or Apache v2 (=> you can include generated code even into proprietary applications) β€” see individual libraries for details

More Repositories

1

kaitai_struct_formats

Kaitai Struct: library of binary file formats (.ksy)
Kaitai Struct
702
star
2

kaitai_struct_compiler

Kaitai Struct: compiler to translate .ksy => .cpp / .cs / .dot / .go / .java / .js / .lua / .nim / .php / .pm / .py / .rb
Scala
492
star
3

kaitai_struct_visualizer

Kaitai Struct: visualizer and hex viewer tool
Ruby
271
star
4

kaitai_struct_webide

Online editor / visualizer for Kaitai Struct .ksy files
TypeScript
264
star
5

awesome-kaitai

A curated list of Kaitai Struct tools and resources
186
star
6

kaitai_struct_python_runtime

Kaitai Struct: runtime for Python
Python
92
star
7

kaitai_struct_go_runtime

Kaitai Struct: runtime for Go
Go
68
star
8

kaitai_struct_cpp_stl_runtime

Kaitai Struct: runtime for C++ using STL
C++
66
star
9

kaitai_struct_csharp_runtime

Kaitai Struct: runtime for C#/.NET
C#
58
star
10

kaitai_struct_rust_runtime

Kaitai Struct: runtime for Rust
Rust
55
star
11

kaitai_fs

KaitaiFS: mount any filesystem specified with a .ksy as a real file system
Python
54
star
12

kaitai_struct_java_runtime

Kaitai Struct: runtime for Java
Java
41
star
13

kaitai_struct_javascript_runtime

Kaitai Struct: runtime for JavaScript
JavaScript
32
star
14

kaitai_struct_gui

Kaitai Struct: visualizer and hex viewer tool GUI in Java
Java
23
star
15

kaitai_struct_lua_runtime

Kaitai Struct: runtime for Lua
Lua
20
star
16

kaitai_struct_ruby_runtime

Kaitai Struct: runtime for Ruby
Ruby
18
star
17

kaitai_struct_doc

Kaitai Struct: documentation
CSS
16
star
18

ide-kaitai-io.github.io

Stable deployment of Kaitai Web IDE using GitHub pages - https://ide.kaitai.io/
JavaScript
14
star
19

kaitai_struct_tests

Kaitai Struct: tests in all supported programming languages
Ruby
13
star
20

ksy_schema

Kaitai Struct YAML (KSY) schema specification
12
star
21

ksylint

A linter for ksy files.
Python
11
star
22

kaitai_struct_php_runtime

Kaitai Struct: runtime for PHP
PHP
11
star
23

kaitai_struct_swift_runtime

Kaitai Struct: runtime for Swift
Swift
10
star
24

kaitai_struct_nim_runtime

Kaitai Struct: runtime for Nim
Nim
9
star
25

kaitai_compress

Kaitai Struct: data compression algorithms processing routines
Shell
9
star
26

kaitai_struct_loader

Webpack loader for kaitai-struct .ksy definitions
JavaScript
7
star
27

formats-kaitai-io.github.io

Rendered HTML pages repository powering http://formats.kaitai.io/ β€” generated from https://github.com/kaitai-io/kaitai_struct_formats/
HTML
6
star
28

java_bytecode.ksy

Java bytecode spec for Kaitai Struct
Ruby
6
star
29

kaitai-io.github.io

Kaitai Project homepage
HTML
6
star
30

edid.ksy

EDID (VESA Enhanced Extended Display Identification Data) structure for Kaitai Struct
Kaitai Struct
5
star
31

kaitai_struct_benchmarks

Kaitai Struct: benchmarking suite
Kaitai Struct
5
star
32

kaitai_struct_samples

Kaitai Struct: library of sample files for testing
Python
4
star
33

kaitai_ci_ui

Kaitai Struct CI UI
Vue
3
star
34

kaitai_struct_perl_runtime

Kaitai Struct: runtime for Perl
Perl
3
star
35

doc-kaitai-io.github.io

Rendered HTML pages repository powering https://doc.kaitai.io/ β€” generated from https://github.com/kaitai-io/kaitai_struct_doc/
HTML
3
star
36

dicom.ksy

DICOM (Digital Imaging and Communications in Medicine) file format spec for Kaitai Struct
Ruby
3
star
37

kaitai_struct_c_runtime

Kaitai Struct: runtime for C
3
star
38

ci_targets

Kaitai Struct CI: test .ksy files compiled into target languages
C++
3
star
39

kaitai_struct_typescript_runtime

Kaitai Struct: Runtime for Typescript
2
star
40

kaitai_struct_examples

HTML
2
star
41

coreldraw_cdr.ksy

CDR (CorelDRAW drawing) format specification for Kaitai Struct
Kaitai Struct
2
star
42

windows_resource_file.ksy

Windows resource file spec for Kaitai Struct
2
star
43

webide-usercontent.kaitai.io

HTML
1
star
44

kaitai_struct_docker_images

Docker containers that are sufficient to run relevant portion of Kaitai Struct tests for a particular target language
Dockerfile
1
star
45

ci-kaitai-io.github.io

ci.kaitai.io website
HTML
1
star
46

kaitai_struct_webide_vue

The kaitai struct webide completely rewritten from the ground up with vuejs and web components
Vue
1
star
47

ci_artifacts

Kaitai Struct CI: test run results
1
star