Skip to content

Parser: implement bitwise XOR (^), NOT (~), and any-value (x) operators #35

@unclesp1d3r

Description

@unclesp1d3r

Summary

Implement the remaining bitwise and special operators: ^ (XOR), ~ (NOT/complement), and x (match any value).

Context

GNU file's magic format supports these operators for bit-level format detection:

  • ^ (XOR) - Used for flag toggling and bitmask checks
  • ~ (NOT/complement) - Used for inverted bit patterns
  • x (any value) - Matches any value at an offset (used to unconditionally output text, often in child rules)

Current state: Only =, !=, &, and &mask are implemented.

Acceptance Criteria

  • Parser recognizes ^, ~, x operators
  • Operator enum extended with BitwiseXor, BitwiseNot, AnyValue variants
  • Evaluator implements XOR and NOT logic for numeric types
  • x operator always returns true (matches any value)
  • Unit tests for each operator
  • All existing tests continue to pass

Impact

MEDIUM - x (any value) is particularly common in magic files for unconditional child rule output. XOR and NOT are less common but needed for full compatibility.

Files to Modify

  • src/parser/ast.rs - Extend Operator enum
  • src/parser/grammar.rs - Parse new operators
  • src/evaluator/operators.rs - Implement logic

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    compatibilitylibmagic compatibility and migrationenhancementNew feature or requestevaluatorRule evaluation engine and logicparserMagic file parsing components and grammarpriority:normalStandard work item
    No fields configured for Feature.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions