fragmede
fragmede

Reputation: 11

Kaitai Struct: error accessing elements in _parent

I'm trying to find the index variable of the parent object, but getting an error trying to call against _parent. The docs for _parent aren't much help, unfortunately.

The binary file format I'm trying to reverse has a table object, which contains pages. The table has a first page and last page, and the pages themselves have an index, and data. The last page is used to denote how many pages are in the table, and thus when to stop reading, but to find pages, I have to look at the first page and iterate.

For the cut-down test case reproducer, the ksy file is:

meta:
  id: hello_world
  endian: be
  file-extension:
    - hw
seq:
  - id: num_tables
    type: u2
  - id: tables
    type: table
    repeat: expr
    repeat-expr: num_tables
    -webide-representation: '{type}'

types:
  table:
    seq:
      - id: first_page
        type: page_ref
      - id: last_page
        type: page_ref
    -webide-representation: '{type}'

  page_ref:
    seq:
      - id: page_index
        type: u2
    instances:
      body:
        pos: 16 * (page_index)
        io: _root._io
        size: 16
        type: page
        if: page_index < _parent.last_page.page_index

  page:
    seq:
      - id: next_page
        type: page_ref
      - id: data
        size: 14

And the binary is:

00000000: 0001 0001 0003 0001 0000 0000 0000 0000  ................
00000010: 0002 0001 0001 0001 0001 0001 0001 0001  ................
00000020: 0003 0001 0001 0001 0001 0001 0001 0001  ................

The error I'm getting is:

hello_world: /types/page_ref/instances/body/if:
    error: don't know how to call method 'last_page' of object type 'CalcKaitaiStructType(false)'

From my if on line 34.

I'm expecting _parent of a page_ref to be a table, and to be able to access objects in that table, but that's not working.

A variation of my if would be if page_index < _root.tables[SOMETHING].last_page.index, but I couldn't figure out what SOMETHING should be in the case where there is more than one table.

I also tried repeat: eos instead of if but I couldn't get that to work.

Thanks for reading my question.

Upvotes: 1

Views: 96

Answers (1)

fragmede
fragmede

Reputation: 11

The syntax to get around the error is to cast the reference with.as<object>. Eg:

_parent.as<table>.last_page.page_index

Upvotes: 0

Related Questions