kaitai-struct

Passing array parameter to Kaitai Struct user defined type


I'm trying Kaitai Struct to decode some data. I need to pass an array parameter, but ksc gives an error. Follow is an example code:

meta:
  id: cat_34

seq:
  - id: test1
    type: fixed([0,1,2])

types:
  fixed:
    params:
      - id: f_size
        #no type is just an array
    seq:
      - id: val
        type: u1
        repeat: expr
        repeat-expr: f_size[1] #trying to use second value of array.

It gives the following error: cat_34: /types/fixed/seq/0/repeat-expr: unable to apply operation [] to CalcBytesType


Solution

  • No type (or using type: bytes) on param declaration yields a byte array type (AKA "CalcBytesType"), which not the same thing as true array of something.

    In this case, you're encountering an error that operation [] was not implemented in Kaitai Struct v0.8 and lower, and that was only fixed recently and not for all targets in 0.9 unstable. Thus, one of your options would be upgrading to v0.9+.

    As a workaround, you might want to insist on using true arrays - i.e. instead of passing [0, 1, 2], use [0, 1, 2].as<u1[]>. That will work for all versions, but that will yield (1) potentially less efficient true arrays, (2) potentially unsupported array instantiation.