Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kaitai Struct produced a nim code that can't be compiled? #1141

Open
trufanov-nok opened this issue Nov 2, 2024 · 2 comments
Open

Kaitai Struct produced a nim code that can't be compiled? #1141

trufanov-nok opened this issue Nov 2, 2024 · 2 comments
Labels

Comments

@trufanov-nok
Copy link

Hi, I'm new to KS and nim. In fact I tried to clone serializable branch of KS according to the serialization guide. Then I got scala-sbt from https://repo.scala-sbt.org/scalasbt/debian according to their doc and successfully build ksc.

After that I tried to generate a code for this file: https://gitlab.com/librespacefoundation/satnogs/satnogs-decoders/-/blob/master/ksy/siriussat.ksy

with ../jvm/target/universal/stage/bin/kaitai-struct-compiler --no-auto-read -t all ../siriussat.ksy

and got nim/siriussat.nim file.

So I installed nim from repository with sudo apt install nim and tried to compile it to C code with: nim c siriussat.nim
And got error: /tmp/kaitai_struct_compiler/cpp/nim/siriussat.nim(1, 8) Error: cannot open file: kaitai_struct_nim_runtime

Thus I downloaded this file into the same folder: https://github.com/kaitai-io/kaitai_struct_nim_runtime/blob/master/kaitai_struct_nim_runtime.nim

and tried again. Now I got error:
/tmp/kaitai_struct_compiler/cpp/nim/siriussat.nim(967, 16) Error: undeclared identifier: 'rptInstance'

And I guess that's a generated nim code problem.

I tried to switch to the master branch of ksc and tried it without --read-write argument and got the same result.

May be I misunderstood something and this could be workarounded?

My aim is to get C or C++ serializer code from ksy wile via nim code as direct cpp serializer code generation isn't supported

@generalmimon
Copy link
Member

@trufanov-nok:

My aim is to get C or C++ serializer code from ksy wile via nim code as direct cpp serializer code generation isn't supported

First of all, just to align your expectations, the most important thing for you might be that serialization support has only been implemented for Java and Python, see the serialization guide:

At the time of writing, it's only available for Java and Python

So nothing like "get C or C++ serializer code from ksy wile via nim code" is possible with Kaitai Struct right now. Notice in the Compiling a .ksy specification in read-write mode section that you only have two options: --read-write -t java and --read-write -t python. Other targets will simply ignore --read-write.

Now I got error: /tmp/kaitai_struct_compiler/cpp/nim/siriussat.nim(967, 16) Error: undeclared identifier: 'rptInstance'

And I guess that's a generated nim code problem.

Yeah, Kaitai Struct (like all software) is not bug-free, so it sometimes happens that the KS compiler generates invalid code. Nim in particular is least supported language in Kaitai Struct. As you can see at our own testing matrix at https://ci.kaitai.io/, it has the lowest percentage of passed tests. So if you don't need to use Nim, better choose a more supported language if you want to have fewer problems. If the only reason you tried Nim generation was to get serialization in C/C++, that won't work with the current state of Kaitai Struct - it is really only implemented for Java and Python.

If you want a solution now, you might want to use something else than Kaitai Struct. I can't give you a specific recommendation because I don't really have experience with anything else. https://github.com/dloss/binary-parsing generally gives a good overview of other tools similar to Kaitai Struct. Most of them can only parse, but some can both parse and serialize. There might be other C++-specific libraries not listed there that could also help you with binary serialization in C++ - I have no idea.

@generalmimon
Copy link
Member

generalmimon commented Nov 2, 2024

@trufanov-nok Of course, if you want to use Kaitai Struct serialization, you could also write a small Python/Java application that accepts the structured data to serialize (for example in JSON via standard input) and outputs the serialized binary output. You would then invoke it from your C++ application as an external program. That certainly sounds technically feasible, but of course whether it's a good idea or not depends on the application. If it has to be entirely in C/C++, then you need to look for something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants