Search code examples
javaparsinghexdumpkaitai-struct

Kaitai code writing


I recently started kaitai-struct for dealing with arbitrary binary formats. I have created the .ksy file for my data and parsed it to targeted language that is java. Now can anyone point me how to pass the input file that has the data and how to get the data that is parsed as output so that I can write code to manipulate that data to my requirements? Is there any tutorial on how to write code depending on the data we get.

Thanks in advance.


Solution

  • First you have to generate Java classes from the .ksy file using the Kaitai Struct Compiler or the WebIDE. You can find more information how to use the compiler in the Kaitai user guide.

    If you use the WebIDE then just simply right-click on your .ksy file and select the Generate parser > Java menu item.

    After you have the generated Java code, you can parse a structure directly from a local file like this:

    AnExampleClass output = AnExampleClass.fromFile("an_example.data");
    // ... manipulate output ...
    

    Or you can parse a structure from a byte array (byte[]):

    AnExampleClass output = new AnExampleClass(new KaitaiStream(byteArray));
    // ... manipulate output ...
    

    Note that parsing from non-seekable streams (i.e. FileInputStream, BufferedInputStream, etc) is not supported and probably won’t be supported, as a lot of parsing functionality in KS relies on seek support.

    You can read the generic documentation how to use the API here and you can find the Java-specific documentation here.