kaitai_struct icon indicating copy to clipboard operation
kaitai_struct copied to clipboard

Inconsistent behavior of list-of-ints vs list-of-floats

Open larsks opened this issue 4 years ago • 3 comments

I just ran into the following odd behavior. If I specify a literal value as a list of integers, like this:

types:
  tables:
    instances:
      example_lookup:
        value: >-
          [10, 20, 30, 40, 50]

Then the generated Python code is a byte string:

self._m_example_lookup_2 = b"\x0A\x14\x1E\x28\x32"

And if I try the following...

types:
  tables:
    instances:
      example_lookup:
        value: >-
          [10, 20, 30, 40, 50]
  example:
    seq:
      - id: value_raw
        type: u1
    instances:
      value_decoded:
        value: _root.tables.example_lookup[value_raw]

...it fails with:

(main): /types/example/instances/value_decoded_2/value: unable to apply operation [] to CalcBytesType

But if I put a single float into that example_lookup table, like this:

types:
  tables:
    instances:
      example_lookup:
        value: >-
          [10.0, 20, 30, 40, 50]
  example:
    seq:
      - id: value_raw
        type: u1
    instances:
      value_decoded:
        value: _root.tables.example_lookup[value_raw]

...it works as expected, and the generated Python code looks like this:

self._m_example_lookup_1 = [10.0, 20, 30, 40, 50]

What's up with that?

larsks avatar Oct 11 '19 21:10 larsks

The main issue here is KS expression language does not provide special syntax for true integer arrays vs byte arrays. If all members of array can be expressed as bytes — it automatically generates a byte array, i.e.:

  • [10, 20, 30, 40, 50] — byte array
  • [10, 20, 30, 40, 50, 10000] — true integer array

You can force array to be treated as true integers array using cast syntax, i.e.:

[10, 20, 30, 40, 50].as<u4[]>

On top of that, this is actually wrong:

(main): /types/example/instances/value_decoded_2/value: unable to apply operation [] to CalcBytesType

There should be no distinction of applying [] operator to byte array vs true arrays.

GreyCat avatar Oct 12 '19 11:10 GreyCat

You can force array to be treated as true integers array using cast syntax, i.e.:

[10, 20, 30, 40, 50].as<u4[]>

This seems to be undocumented - I can't find any place on https://doc.kaitai.io/ that describes type[] as a valid type name that stands for "array of type". (Admittedly, this syntax is not needed much in practice. Arrays are usually created through repeated fields, where you specify the type of the element, not the type of the array.)

dgelessus avatar Oct 12 '19 15:10 dgelessus

Makes sense, we should at very least document that...

GreyCat avatar Oct 13 '19 00:10 GreyCat