kaitai_struct
kaitai_struct copied to clipboard
Inconsistent behavior of list-of-ints vs list-of-floats
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?
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.
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 repeat
ed fields, where you specify the type of the element, not the type of the array.)
Makes sense, we should at very least document that...