kaitai-io / kaitai_struct_java_runtime

Kaitai Struct: runtime for Java
MIT License
42 stars 18 forks source link

Add ability to report positions relative to root stream and add PositionInfo interface #28

Open Mingun opened 4 years ago

Mingun commented 4 years ago

Runtime changes for https://github.com/kaitai-io/kaitai_struct_compiler/pull/197

Mingun commented 3 years ago

@generalmimon, thanks for review! I post some answers with explanation of my minds when I made these changes and if you find they unconvincing, just say and I'll apply your suggestions.

Also, over the time, I'm noticed, that usually things like Region called Span (ex: C#, Rust), so I'm going to change names accordingly

generalmimon commented 3 years ago

There's also an important thing to consider: creating a stream on processed bytes, for example with process: zlib (kudos to @GreyCat and his https://github.com/kaitai-io/kaitai_struct/issues/331#issuecomment-361432895 that reminded me of it). The decompression gives you a brand new block of data and the bytes and stream positions don't map to anything in the original root file stream. In fact, you get another root stream.

Mingun commented 3 years ago

Good point! But need to notice, that the such problem exists in the web-ide and maybe in the https://github.com/kaitai-io/kaitai_struct_visualizer.

To solve that problem we can track which root Span belongs to, for example, by assign unique identifiers to the each root, created by the process key, or for the each Span to store its parent Span:

class Span {
  private final long offset;
  private final long long start;
  private long end = -1;

  private final int rootCategory;
  //-OR-
  private final Span parent;
  ...
}

Each root Span will have root = null

Mingun commented 3 years ago

Implementation of tags for different stream origins I'll push later. Anyway, it's needed only for correct interpretation of offsets, so maybe even another PR will be preferrable