feat(parser): implement bitwise xor not and any value x operators#145
Conversation
Updated the documentation to include the Bitwise NOT operator (`~`) in the comparison operators section of MAGIC_FORMAT.md. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Implemented the Bitwise XOR (`^`) and Bitwise NOT (`~`) operators in the evaluator, allowing for bitwise operations on integer values. - Introduced the AnyValue operator (`x`), which matches unconditionally, and updated the parsing logic to handle it. - Added corresponding tests for the new operators and updated documentation to reflect these changes. This enhances the operator functionality and improves the evaluation capabilities of the magic rules. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Introduced new tests to validate the parsing and evaluation of Bitwise XOR (`^`) and Bitwise NOT (`~`) operators. - Added assertions to check for correct matching behavior based on buffer values for both operators. - Enhanced documentation with examples for the Bitwise XOR and Bitwise NOT functions in the evaluator. This improves test coverage and ensures the correctness of the new bitwise operations. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Revised the formatting of the analysis steps and simplification principles for better readability. - Enhanced descriptions under complexity reduction, test proportionality, and idiomatic code sections to provide clearer guidance. This update aims to improve the usability of the simplicity review prompt for developers. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Updated command examples in `cli-usage.md` to remove unnecessary `$` prefix for better clarity and consistency. - Ensured that all examples follow a uniform format for easier understanding. This change enhances the documentation's usability for users referencing CLI commands. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Added blank lines in `conventional-commits.md` and `testing-conventions.md` to improve readability and structure. - This change aims to enhance the overall clarity of the documentation for better user experience. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Added blank lines for improved readability in `SKILL.md` files across API design and security review sections. - Included additional guidelines and examples for error handling, type safety, and verification steps to enhance clarity and usability. - This update aims to provide clearer guidance for developers on best practices in API design and security measures. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Added `.claude/**` to the ignores list in the markdownlint configuration to prevent linting of generated files. - This change helps streamline the linting process by excluding unnecessary directories. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Changed email and URLs in the CODE_OF_CONDUCT.md to use angle brackets for uniformity. - This update enhances the formatting and readability of the document. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Updated the `lint-actions` recipe in the justfile to remove the `release.yml` and `scorecard.yml` workflow files from the actionlint command. - This change streamlines the linting process by focusing on relevant workflows. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Moved the grammar parsing logic for magic files from `grammar.rs` to a new `mod.rs` file, improving modularity and maintainability. - Added comprehensive tests in `tests.rs` to ensure the correctness of the parsing functions, including decimal, hexadecimal, and offset specifications. - This restructuring enhances code organization and facilitates easier updates in the future. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Transferred existing tests from `mod.rs` to a new `tests.rs` file, improving code organization and maintainability. - This change enhances the clarity of the evaluator module by separating implementation from testing logic. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
…tion - Moved existing tests from `lib.rs` to a newly created `tests.rs` file, enhancing code organization and maintainability. - This change separates the implementation details from the testing logic, making the codebase cleaner and easier to navigate. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
|
Caution Review failedPull request was closed or merged during review Summary by CodeRabbit
WalkthroughAdded three operators (BitwiseXor, BitwiseNot, AnyValue); implemented their evaluator functions and dispatcher entries; moved/rewrote parser grammar into Changes
Sequence Diagram(s)sequenceDiagram
participant Parser as Parser
participant AST as AST (Operator)
participant Evaluator as Evaluator Dispatcher
participant OpImpl as Operator Impl
participant Strength as Strength Calculator
rect rgba(0,200,100,0.5)
Note over Parser,Strength: BitwiseXor Flow
Parser->>AST: parse "^" token + value
AST->>Evaluator: emit BitwiseXor node
Evaluator->>OpImpl: apply_bitwise_xor(left, right)
OpImpl-->>Evaluator: bool
Evaluator->>Strength: compute strength (contrib: 4)
end
rect rgba(100,150,255,0.5)
Note over Parser,Strength: BitwiseNot Flow
Parser->>AST: parse "~" token + value
AST->>Evaluator: emit BitwiseNot node
Evaluator->>OpImpl: apply_bitwise_not_with_width(left, right, bit_width)
OpImpl-->>Evaluator: bool
Evaluator->>Strength: compute strength (contrib: 4)
end
rect rgba(200,100,150,0.5)
Note over Parser,Strength: AnyValue Flow
Parser->>AST: parse "x" token
AST->>Evaluator: emit AnyValue node
Evaluator->>OpImpl: apply_any_value(left, right)
OpImpl-->>Evaluator: true
Evaluator->>Strength: compute strength (contrib: 1)
end
Estimated code review effort🎯 5 (Critical) | ⏱️ ~120 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 CI must passWonderful, this rule succeeded.All CI checks must pass. Release-plz PRs are exempt because they only bump versions and changelogs (code was already tested on main), and GITHUB_TOKEN-triggered force-pushes suppress CI.
🟢 Do not merge outdated PRsWonderful, this rule succeeded.Make sure PRs are within 10 commits of the base branch before merging
|
|
Related Documentation 7 document(s) may need updating based on files changed in this PR: libMagic-rs api-reference
|
- Updated section headers from bold to header format for improved readability in the magic format analysis document. - Standardized code block syntax to `text` for better clarity in error messages and examples throughout the document. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
…tation - Updated code block syntax from generic to specific `text` in multiple documentation files, including architecture, CLI reference, magic format, and release process. - Enhanced readability and presentation of examples, error messages, and structural elements throughout the documents. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Introduced new operators: BitwiseXor, BitwiseNot, and AnyValue to the arb_operator function in property_tests.rs. - This enhancement expands the test coverage for operator functionality. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Added new comparison operators: LessThan, GreaterThan, LessEqual, and GreaterEqual to the Operator enum. - Introduced BitwiseXor, BitwiseNot, and AnyValue operators, enhancing the operator capabilities. - Refactored parser and evaluator module structure for better organization, including moving grammar parsing logic to a dedicated directory and adding tests for the evaluator. - Updated documentation to reflect the new operators and their usage. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
…ators - Clarified the behavior of the bitwise NOT operator, emphasizing its comparison against a specific expected value. - Added detailed documentation for the any-value operator, including its unconditional match behavior and usage examples. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Updated the apply_strength_modifier function to log a warning when division by zero occurs, ensuring the base strength remains unchanged. - Clarified the comment to indicate that the invalid operation is due to a magic file containing an unsupported strength modifier. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
…rs in operator parsing - Added a check to ensure that the 'x' operator is not followed by alphanumeric characters or underscores, returning an error if this condition is met. - This change enhances the robustness of the operator parsing logic. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
…value-x-operators Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Revised the documentation to reflect the support for ten comparison and bitwise operators, along with the AnyValue (`x`) operator. - This change clarifies the current capabilities of the operator parsing feature. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
…value-x-operators
- Introduced the `x` operator in the documentation, which matches any value unconditionally. - Updated the section title from "Special Values" to "Any-Value Operator" for clarity. - Provided an example to illustrate the usage of the `x` operator in child rules. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Added detailed arguments and return value descriptions for the `apply_bitwise_and` and `apply_bitwise_xor` functions. - Updated the documentation for the `x` operator to include argument and return value details, ensuring clarity on its unconditional match behavior. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Implemented a new method `bit_width` for the `TypeKind` enum, which returns the bit width of integer types (Byte, Short, Long, Quad) or `None` for non-integer types (e.g., String). - Included examples in the documentation to illustrate the method's usage. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Introduced `apply_bitwise_not_with_width` function to apply bitwise NOT with optional bit-width masking, enhancing type awareness for bitwise operations. - Updated the `apply_bitwise_not` function to utilize the new width-aware implementation. - Added comprehensive tests for various bit-width scenarios (8, 16, 32, and 64 bits) to ensure correct behavior. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
…andling - Updated the `evaluate_single_rule` function to apply bitwise NOT with type-aware bit-width masking, ensuring correct behavior for different integer types. - Modified tests for bitwise NOT to reflect changes in expected outcomes based on byte width, improving clarity in assertions. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Enhanced the formatting of method and field tables in the API reference documentation for better readability and consistency. - Adjusted column widths and alignment across various sections, including constructor, evaluation, accessor methods, and fields in multiple data structures. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
- Enhanced the formatting of various tables in the CONTRIBUTING.md file for better readability and consistency. - Adjusted column widths and alignment in sections including Module Overview, Naming Conventions, and Roles. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
## 🤖 New release
* `libmagic-rs`: 0.3.2 -> 0.4.0 (⚠ API breaking changes)
### ⚠ `libmagic-rs` breaking changes
```text
--- failure enum_variant_added: enum variant added on exhaustive enum ---
Description:
A publicly-visible enum without #[non_exhaustive] has a new variant.
ref: https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new
impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/enum_variant_added.ron
Failed in:
variant Operator:BitwiseXor in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:252
variant Operator:BitwiseNot in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:263
variant Operator:AnyValue in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:274
variant Operator:BitwiseXor in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:252
variant Operator:BitwiseNot in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:263
variant Operator:AnyValue in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:274
variant Operator:BitwiseXor in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:252
variant Operator:BitwiseNot in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:263
variant Operator:AnyValue in /tmp/.tmp8YIKcr/libmagic-rs/src/parser/ast.rs:274
```
<details><summary><i><b>Changelog</b></i></summary><p>
<blockquote>
## [0.4.0] - 2026-03-06
### Features
- **parser**: Implement bitwise xor not and any value x operators
([#145](#145))
### Documentation
- Add comprehensive contributing guide
([#149](#149))
<!-- generated by git-cliff -->
</blockquote>
</p></details>
---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This pull request adds support for three new operators—bitwise XOR (
^), bitwise NOT (~), and any-value (x)—to the magic rule evaluation system. The changes include parsing, evaluation, documentation, and comprehensive tests for these operators, ensuring they are handled consistently throughout the codebase.Operator support and parsing:
Operator::BitwiseXor,Operator::BitwiseNot, andOperator::AnyValuevariants to theOperatorenum, with documentation and serialization support (src/parser/ast.rs,src/parser/codegen.rs). [1] [2]^,~, andxas valid operators, including documentation and examples (src/parser/grammar.rs). [1] [2] [3]Evaluation logic and integration:
apply_bitwise_xor,apply_bitwise_not, andapply_any_valuefunctions, and integrated them into the main operator dispatch logic (src/evaluator/operators/bitwise.rs,src/evaluator/operators/mod.rs). [1] [2] [3] [4] [5]src/evaluator/strength.rs).Testing and documentation:
src/evaluator/operators/mod.rs,src/evaluator/operators/bitwise.rs,src/parser/ast.rs). [1] [2] [3] [4] [5] [6] [7]MAGIC_FORMAT.md(docs/MAGIC_FORMAT.md).End-to-end validation:
x) operator, ensuring it matches unconditionally (src/evaluator/mod.rs).