调用外部代码以确定重复表达的次数

时间:2019-02-01 00:54:16

标签: kaitai-struct

我有一个序列,该序列的重复表达式为'id',我需要重复一次未知的次数,并且不确定当前是否支持该序列。

verticalLayoutWidget_2

我想做的是调用一个外部方法,该方法计算正确的重复次数,并将该次数返回给kaitai。喜欢:

data_channels:
  seq:
    - id: fast_data
      type: u2
      repeat: expr
      repeat-expr: ???

但是,即使在元数据中使用“ ks-opaque-types:true”,我也会收到一个错误,指出“ CalculateRepetitions”无法访问。

重复次数取决于kaitai无法访问的许多事物,因此外部方法是一个完美的解决方案。

预先感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

如果你知道该类型的调用之前的重复次数,可以声明,作为一个类型参数和从应用程序传递给它:

data_channels:
  params:
    - id: num_items
      type: u4
  seq:
    - id: fast_data
      type: u2
      repeat: expr
      repeat-expr: num_items

如果您不知道在解析之前,仍然可以使用此技巧来桥接到不透明的类型并返回,即:

meta:
  id: your_main_type
  ksc-opaque-types: true
seq:
  - id: something
    type: u1
  # at this point we don't know number of repetitions yet
  - id: data_channels
    type: opaque_data_channels_wrapper

然后,在您的应用中实现OpaqueDataChannelsWrapper类。如果有,它可能是类似的东西:

class OpaqueDataChannelsWrapper {
  public OpaqueDataChannelsWrapper(KaitaiStream io) {
    // do something here to determine number of repetitions

    // dive back into KaitaiStruct parsing with that information
    DataChannels realDataChannels = new DataChannels(io, numRepetitions);
  }
}