Skip to main content

resolve_offset

Function resolve_offset 

Source
pub fn resolve_offset(
    spec: &OffsetSpec,
    buffer: &[u8],
) -> Result<usize, LibmagicError>
Expand description

Resolve any offset specification to an absolute position.

Convenience wrapper for callers that do not have a relative-offset anchor (e.g., tests, top-level evaluation with no prior match). Internally delegates with last_match_end = 0. For OffsetSpec::Relative, that means non-negative deltas behave like absolute offsets from the start of the buffer (Relative(N) for N >= 0 resolves to absolute N), but negative deltas underflow the anchor and return EvaluationError::InvalidOffset – they are not interpreted like OffsetSpec::Absolute(-N) from the end of the buffer. Callers that need relative offsets to anchor against actual prior matches should use evaluate_rules and let the engine thread the anchor.

Behavior change: before the relative-offset feature landed in v0.5, this function returned EvaluationError::UnsupportedType for OffsetSpec::Relative. It now resolves against anchor 0, which can succeed (non-negative delta) or fail with InvalidOffset (negative delta) depending on the value. Callers with existing error-handling code that pattern-matched UnsupportedType for relative offsets must remove that arm.

§Arguments

  • spec - The offset specification to resolve
  • buffer - The file buffer to resolve against

§Returns

Returns the resolved absolute offset as a usize, or a LibmagicError if resolution fails.

§Examples

use libmagic_rs::evaluator::offset::resolve_offset;
use libmagic_rs::parser::ast::OffsetSpec;

let buffer = b"Test data";
let spec = OffsetSpec::Absolute(4);

let offset = resolve_offset(&spec, buffer).unwrap();
assert_eq!(offset, 4);

§Errors

  • LibmagicError::EvaluationError - If offset resolution fails