kaitai-struct

Kaitai Struct Parameter Type


I am trying to pass a parameter to ksy file. The parameter is of type another ksy file. The reason is that i need to access all the fields from the ksy file passed as parameter. Is that possible? If yes, would you please provide me with syntax code snippet so I can mimic it. If no, what would be another solution?

Thank You.

enter image description here

enter image description here


Solution

  • Affiliate disclaimer: I'm a Kaitai Struct maintainer (see my GitHub profile).

    First, I recommend always using the development version of the Kaitai Struct Web IDE (https://ide.kaitai.io/devel/), not the stable one. The stable IDE deployed at https://ide.kaitai.io/ has KS compiler of version 0.8, which is indeed the latest stable version, but already 2 years old at the moment. But the project is under active development, new bug fixes and improvements are coming every week, so the stable Web IDE is pretty much outdated. And thanks to the recent infrastructure enhancement, the devel Web IDE now gets rebuilt every time the compiler is updated, so you can use even the most recent features.

    However, you won't be able to simulate the particular situation you describe in the Web IDE, because it can't currently handle top-level parameteric types (there is no hook where you can pass your own values as arguments). But it should work in a local environment. You can compile the commontype.ksy and pty.ksy specs in the Web IDE to the target language you want to use (the manual shows how to do it). The code putting it together could look like this (Java):

    Commontype ct = new Commontype(new ByteBufferKaitaiStream(new byte[] { 80, 75 }));
    Pty r = new Pty(
        new ByteBufferKaitaiStream(new byte[] { 80 }), // IO stream
        ct // commonword
    );
    

    Note that the actual parameter order of the Pty constructor may be different, e.g. in Python come the custom params (commonword) first and then the IO object. Check the generated code in your particular language.