Source code for algorithms.base

"""Base classes for analysis algorithms."""

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Sequence

from memdiver.core.models import CryptoSecret, deprecated_kwarg


[docs] @dataclass class AnalysisContext: """Describe the current capture and supply inputs to an algorithm. *secrets* can hold known values from ground truth. *extra* carries algorithm-specific options. """ library: str protocol_version: str phase: str secrets: List[CryptoSecret] = field(default_factory=list) extra: Dict[str, Any] = field(default_factory=dict) @property def tls_version(self) -> str: """Backward-compatible alias for protocol_version.""" return self.protocol_version @tls_version.setter def tls_version(self, value: str) -> None: self.protocol_version = value
deprecated_kwarg(AnalysisContext, "tls_version", "protocol_version")
[docs] @dataclass class Match: """Report one match with a byte offset, length, and confidence. The algorithm can supply a label, matched bytes, and metadata when it has them. Interpret the offset in the input coordinates. """ offset: int length: int confidence: float label: str = "" data: bytes = b"" metadata: Dict[str, Any] = field(default_factory=dict)
[docs] @dataclass class AlgorithmResult: """Report the algorithm name, overall confidence, and matches. The metadata can contain algorithm-specific diagnostics. """ algorithm_name: str confidence: float matches: List[Match] = field(default_factory=list) metadata: Dict[str, Any] = field(default_factory=dict)
[docs] class BaseAlgorithm(ABC): """Base class for all analysis algorithms.""" name: str = "" description: str = "" mode: str = "" # Use KNOWN_KEY or UNKNOWN_KEY from core.constants
[docs] @abstractmethod def run(self, dump_data: bytes, context: AnalysisContext) -> AlgorithmResult: """Run the algorithm on dump data.""" ...
[docs] def estimate_working_set( self, size: int, context: Optional[AnalysisContext] = None, sample: Optional[Sequence[bytes]] = None, ) -> int: """Estimate the peak additional memory that :meth:`run` needs, in bytes. Exclude the input buffer because the caller budgets it separately. Use *context* for analysis options and *sample* for examples of the input bytes when the memory cost depends on content. Give a conservative estimate. The default is zero for algorithms with no size-dependent working set. """ return 0