Skip to main content

resolve_absolute_offset

Function resolve_absolute_offset 

Source
pub fn resolve_absolute_offset(
    offset: i64,
    buffer: &[u8],
) -> Result<usize, OffsetError>
Expand description

Resolve an absolute offset with bounds checking

This function takes an absolute offset (which can be negative for offsets from the end) and resolves it to a valid position within the buffer bounds.

§Arguments

  • offset - The absolute offset (positive from start, negative from end)
  • buffer - The file buffer to check bounds against

§Returns

Returns the resolved absolute offset as a usize, or an OffsetError if the offset is out of bounds or invalid.

§Examples

use libmagic_rs::evaluator::offset::resolve_absolute_offset;

let buffer = b"Hello, World!";

// Positive offset from start
let offset = resolve_absolute_offset(0, buffer).unwrap();
assert_eq!(offset, 0);

let offset = resolve_absolute_offset(7, buffer).unwrap();
assert_eq!(offset, 7);

// Negative offset from end
let offset = resolve_absolute_offset(-1, buffer).unwrap();
assert_eq!(offset, 12); // Last character

let offset = resolve_absolute_offset(-6, buffer).unwrap();
assert_eq!(offset, 7); // "World!"

§Errors

  • OffsetError::BufferOverrun - If the resolved offset is beyond buffer bounds
  • OffsetError::ArithmeticOverflow - If offset calculation overflows