Skip to content

Parser: implement regex and search types #39

@unclesp1d3r

Description

@unclesp1d3r

Summary

Implement regex and search type specifiers for pattern-based matching in magic rules.

Context

The regex type matches binary-safe regular expressions against file content. The search type searches for a byte sequence within a range. Both are essential for text file detection (JSON, XML, scripts, etc.) and many binary formats.

Current state: Neither type exists in the TypeKind enum or parser.

Acceptance Criteria

  • TypeKind enum extended with Regex and Search variants
  • Parser recognizes regex and search type specifiers including flags (e.g., regex/c for case-insensitive)
  • Evaluator uses regex::bytes::Regex for binary-safe regex matching
  • Search type supports range-limited byte sequence searching
  • Common regex flags supported: case-insensitive, line-start/end
  • Unit tests for regex and search matching
  • Integration tests with text file detection rules

Impact

HIGH - Required for text file detection (JSON, XML, HTML, scripts, config files). The test corpus includes JSON tests, shell script tests, and regex-eol.magic that all require this.

Test Corpus Files Blocked

  • json1-json8, jsonlines1 (JSON detection)
  • cmd1-cmd4 (shell script detection)
  • regex-eol (custom magic using regex)
  • gedcom (text format detection)

Files to Modify

  • src/parser/ast.rs - Extend TypeKind enum
  • src/parser/grammar.rs - Parse regex/search type specifiers
  • src/evaluator/types.rs - Implement regex/search evaluation
  • Cargo.toml - Ensure regex crate bytes feature is enabled

References

Metadata

Metadata

Assignees

Labels

compatibilitylibmagic compatibility and migrationenhancementNew feature or requestevaluatorRule evaluation engine and logicparserMagic file parsing components and grammarpriority:highImportant, significant compatibility or architecture impact
No fields configured for Feature.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions