Skip to main content

Module strength

Module strength 

Source
Expand description

Strength calculation for magic rules

This module implements the strength calculation algorithm based on libmagic’s apprentice_magic_strength function. Strength is used to order rules during evaluation, giving priority to more specific rules.

§Algorithm Overview

The default strength of a rule is calculated based on several factors:

  • Type specificity: String types have higher strength than numeric types
  • Operator specificity: Equality operators are more specific than bitwise
  • Offset type: Absolute offsets are more reliable than indirect/relative
  • Value length: Longer strings are more specific matches

The calculated strength can be modified using !:strength directives in magic files, which apply arithmetic operations to the default strength.

Constants§

MAX_STRENGTH
Maximum strength value (clamped to prevent overflow)
MIN_STRENGTH
Minimum strength value (clamped to prevent negative strength)

Functions§

apply_strength_modifier
Apply a strength modifier to a base strength value.
calculate_default_strength
Calculate the default strength of a magic rule based on its specificity.
calculate_rule_strength
Calculate the final strength of a magic rule, including any modifiers.
into_sorted_by_strength
Sort magic rules by strength and return the sorted vec (consuming the input).
sort_rules_by_strength
Sort magic rules by their calculated strength in descending order.