1

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

Luis Peq
  • 13
  • 3

1 Answers1

1

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.

GreyCat
  • 16,622
  • 18
  • 74
  • 112
  • Thank you. I'll try your suggestion. I'm targeting python3 code, and actually, if I pass this step, what I'would need is something like [ [0,2],[1,3] ], the first elements being of an enum type at least, or a char ('F' for example) at best. – Luis Peq Dec 24 '19 at 00:02
  • I tried your suggested solution, but I got: /seq/0: parsing expression '[0, 1, 2].as' failed on 1:16, expected "," | CharsWhile(Set( , n)) | "\\\n" | End. I also tried using u1[] definitions in other places of code, trying to force the array but none of them worked. I'm using v0.8, on Mac, installed with Homebrew on python3 virtualenv. Rgds. – Luis Peq Dec 25 '19 at 22:09
  • Alas, I might be incorrect about 0.8 :( If so, then the only option left is to upgrade. – GreyCat Dec 28 '19 at 10:57