Skip to content

Evaluator: implement indirect offset resolution #37

@unclesp1d3r

Description

@unclesp1d3r

Summary

Implement evaluation of indirect offsets. The AST representation (OffsetSpec::Indirect) already exists, but the evaluator returns a "not yet implemented" error at src/evaluator/offset.rs:159.

Context

Indirect offsets are a core feature of magic files that allow pointer-chasing through binary structures. The syntax (offset.type) reads a value at the given offset, interprets it as a pointer of the specified type, and uses the result as the actual offset for matching.

Examples:

  • (0x3c.l) - Read a 32-bit long at offset 0x3c, use that value as the offset (PE executable header pointer)
  • (4.s) - Read a 16-bit short at offset 4, use as offset

Current state: AST variant defined, evaluation stubbed with TODO.

Acceptance Criteria

  • resolve_offset() handles OffsetSpec::Indirect by reading the pointer value from the buffer
  • Supports all numeric types as the indirect type (byte, short, long, quad)
  • Handles endianness correctly for indirect reads
  • Supports optional adjustment arithmetic: (offset.type)+adjustment
  • Bounds checking on both the pointer read and the resulting offset
  • Unit tests for indirect offset resolution
  • Integration tests with PE header detection (classic indirect offset use case)

Impact

HIGH - Indirect offsets are critical for complex binary format detection (PE executables, compound documents, archive formats with headers pointing to content).

Files to Modify

  • src/evaluator/offset.rs - Implement indirect resolution (line ~159)
  • src/evaluator/types.rs - May need to expose read functions for indirect use

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    compatibilitylibmagic compatibility and migrationenhancementNew feature or requestevaluatorRule evaluation engine and logicpriority:highImportant, significant compatibility or architecture impact

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions