Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions docs/API_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ use libmagic_rs::TypeKind;

| Variant | Description |
|---------|-------------|
| `Byte { signed }` | Single byte with explicit signedness |
| `Byte { signed }` | Single byte with explicit signedness (changed in v0.2.0) |
| `Short { endian, signed }` | 16-bit integer |
| `Long { endian, signed }` | 32-bit integer |
| `String { max_length }` | String data |

**Version Note:** In v0.2.0, the `Byte` variant changed from a unit variant to a struct variant with a `signed` field.

#### Operator

Comparison operators.
Expand All @@ -315,13 +317,15 @@ use libmagic_rs::Operator;
|---------|-------------|
| `Equal` | Equality comparison (`=` or `==`) |
| `NotEqual` | Inequality comparison (`!=` or `<>`) |
| `LessThan` | Less than comparison (`<`) |
| `GreaterThan` | Greater than comparison (`>`) |
| `LessEqual` | Less than or equal comparison (`<=`) |
| `GreaterEqual` | Greater than or equal comparison (`>=`) |
| `LessThan` | Less than comparison (`<`) (added in v0.2.0) |
| `GreaterThan` | Greater than comparison (`>`) (added in v0.2.0) |
| `LessEqual` | Less than or equal comparison (`<=`) (added in v0.2.0) |
| `GreaterEqual` | Greater than or equal comparison (`>=`) (added in v0.2.0) |
| `BitwiseAnd` | Bitwise AND (`&`) |
| `BitwiseAndMask(u64)` | Bitwise AND with mask value |

**Version Note:** The comparison operators `LessThan`, `GreaterThan`, `LessEqual`, and `GreaterEqual` were added in v0.2.0.

#### Value

Value types for matching.
Expand Down
6 changes: 4 additions & 2 deletions docs/GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ Add libmagic-rs to your `Cargo.toml`:

```toml
[dependencies]
libmagic-rs = "0.1"
libmagic-rs = "0.2"
```

**Note:** Version 0.2.0 introduces breaking changes. If upgrading from 0.1.x, note that `TypeKind::Byte` changed from a unit variant to a tuple variant, and the `Operator` enum gained new variants (`LessThan`, `GreaterThan`, `LessEqual`, `GreaterEqual`) for comparison operations. Exhaustive pattern matching on these enums requires updates.

### Build from Source

```bash
Expand Down Expand Up @@ -61,7 +63,7 @@ Edit `Cargo.toml`:

```toml
[dependencies]
libmagic-rs = "0.1"
libmagic-rs = "0.2"
```

#### Step 3: Write Code
Expand Down
16 changes: 11 additions & 5 deletions docs/src/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ use libmagic_rs::TypeKind;

| Variant | Description |
|---------|-------------|
| `Byte { signed }` | Single byte with explicit signedness |
| `Byte { signed }` | Single byte with explicit signedness (struct variant since v0.2.0; previously unit variant) |
| `Short { endian, signed }` | 16-bit integer |
| `Long { endian, signed }` | 32-bit integer |
| `String { max_length }` | String data |
Expand All @@ -240,10 +240,10 @@ use libmagic_rs::Operator;
|---------|-------------|
| `Equal` | Equality comparison (`=`) |
| `NotEqual` | Inequality comparison (`!=`) |
| `LessThan` | Less than comparison (`<`) |
| `GreaterThan` | Greater than comparison (`>`) |
| `LessEqual` | Less than or equal comparison (`<=`) |
| `GreaterEqual` | Greater than or equal comparison (`>=`) |
| `LessThan` | Less than comparison (`<`) (added in v0.2.0) |
| `GreaterThan` | Greater than comparison (`>`) (added in v0.2.0) |
| `LessEqual` | Less than or equal comparison (`<=`) (added in v0.2.0) |
| `GreaterEqual` | Greater than or equal comparison (`>=`) (added in v0.2.0) |
| `BitwiseAnd` | Bitwise AND |
| `BitwiseAndMask(u64)` | Bitwise AND with mask |

Expand Down Expand Up @@ -477,6 +477,12 @@ pub use error::{EvaluationError, LibmagicError, ParseError};
- **Minimum Rust Version**: 1.85
- **Edition**: 2024
- **License**: Apache-2.0
- **Current Version**: 0.2.0

### Breaking Changes in v0.2.0

- `TypeKind::Byte` changed from a unit variant to a struct variant `Byte { signed }` to support explicit signedness
- Added comparison operators: `LessThan`, `GreaterThan`, `LessEqual`, `GreaterEqual` to the `Operator` enum (breaking change due to exhaustive enum)

For complete API documentation with examples, run:

Expand Down
4 changes: 4 additions & 0 deletions docs/src/ast-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ let from_end = OffsetSpec::FromEnd(-16);

The `TypeKind` enum specifies how to interpret bytes at the given offset:

> **Breaking Change in v0.2.0:** The `Byte` variant changed from a unit variant (`Byte`) to a struct variant (`Byte { signed: bool }`). Code that pattern-matches exhaustively on `TypeKind` requires updates.

### Numeric Types

```rust
Expand Down Expand Up @@ -236,6 +238,8 @@ pub enum Operator {
}
```

> **Added in v0.2.0:** The comparison operators `LessThan`, `GreaterThan`, `LessEqual`, and `GreaterEqual` were added. This is a breaking change for exhaustive matches on `Operator`.

**Usage Examples:**

```rust
Expand Down
60 changes: 55 additions & 5 deletions docs/src/evaluator.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn resolve_offset(

Interprets bytes according to type specifications:

- **Byte**: Single byte values
- **Byte**: Single byte values (signed or unsigned)
- **Short**: 16-bit integers with endianness
- **Long**: 32-bit integers with endianness
- **String**: Byte sequences with length limits
Expand All @@ -104,16 +104,18 @@ pub fn read_typed_value(
) -> Result<Value, TypeReadError>
```

The `read_byte` function signature changed in v0.2.0 to accept three parameters (`buffer`, `offset`, and `signed`) instead of two, allowing explicit control over signed vs unsigned byte interpretation.

### Operator Application (`evaluator/operators.rs`)

Applies comparison operations:

- **Equal** (`=`, `==`): Exact value matching
- **NotEqual** (`!=`, `<>`): Non-matching values
- **LessThan** (`<`): Less-than comparison (numeric or lexicographic)
- **GreaterThan** (`>`): Greater-than comparison (numeric or lexicographic)
- **LessEqual** (`<=`): Less-than-or-equal comparison (numeric or lexicographic)
- **GreaterEqual** (`>=`): Greater-than-or-equal comparison (numeric or lexicographic)
- **LessThan** (`<`): Less-than comparison (numeric or lexicographic) *(added in v0.2.0)*
- **GreaterThan** (`>`): Greater-than comparison (numeric or lexicographic) *(added in v0.2.0)*
- **LessEqual** (`<=`): Less-than-or-equal comparison (numeric or lexicographic) *(added in v0.2.0)*
- **GreaterEqual** (`>=`): Greater-than-or-equal comparison (numeric or lexicographic) *(added in v0.2.0)*
- **BitwiseAnd** (`&`): Pattern matching for flags
- **BitwiseAndMask**: AND with mask then compare

Expand All @@ -127,6 +129,34 @@ pub fn apply_operator(
) -> bool
```

**Example with comparison operators:**

```rust
use libmagic_rs::parser::ast::{Operator, Value};
use libmagic_rs::evaluator::operators::apply_operator;

// Less-than comparison (v0.2.0+)
assert!(apply_operator(
&Operator::LessThan,
&Value::Uint(5),
&Value::Uint(10)
));

// Greater-than-or-equal comparison (v0.2.0+)
assert!(apply_operator(
&Operator::GreaterEqual,
&Value::Uint(10),
&Value::Uint(10)
));

// Cross-type integer comparison (v0.2.0+)
assert!(apply_operator(
&Operator::LessThan,
&Value::Int(-1),
&Value::Uint(0)
));
```

## Evaluation Algorithm

The evaluator uses a depth-first hierarchical algorithm:
Expand Down Expand Up @@ -292,6 +322,26 @@ for m in matches {
}
```

**Example with comparison operators (v0.2.0+):**

```rust
use libmagic_rs::{evaluate_rules, EvaluationConfig};
use libmagic_rs::parser::parse_text_magic_file;

// Parse magic rule with comparison operator
let magic_content = r#"
0 leshort <100 Small value detected
0 leshort >=1000 Large value detected
"#;
let rules = parse_text_magic_file(magic_content)?;

let buffer = vec![0x0A, 0x00]; // Little-endian 10
let matches = evaluate_rules(&rules, &buffer)?;

// Matches first rule (<100)
assert_eq!(matches[0].message, "Small value detected");
```

## Implementation Status

- [x] Basic evaluation engine structure
Expand Down
100 changes: 100 additions & 0 deletions docs/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,106 @@ libmagic-rs aims to maintain compatibility with:
- **GNU file output**: Text output format compatibility
- **Common use cases**: Drop-in replacement for most applications

## Migrating from v0.1.x to v0.2.0

Version 0.2.0 introduces breaking changes to support comparison operators and improve type handling. Update your code as follows:

### TypeKind::Byte Variant Change

The `Byte` variant changed from a unit variant to a struct variant with a `signed` field.

**Before (v0.1.x):**

```rust,ignore
use libmagic_rs::parser::ast::TypeKind;

match type_kind {
TypeKind::Byte => {
// Handle byte type
}
_ => {}
}

// Constructing
let byte_type = TypeKind::Byte;
```

**After (v0.2.0):**

```rust,ignore
use libmagic_rs::parser::ast::TypeKind;

match type_kind {
TypeKind::Byte { signed } => {
// Handle byte type, check signedness if needed
if signed {
// Handle signed byte
} else {
// Handle unsigned byte
}
}
_ => {}
}

// Constructing
let signed_byte = TypeKind::Byte { signed: true };
let unsigned_byte = TypeKind::Byte { signed: false };
```

### New Operator Variants

The `Operator` enum added four comparison operators. Exhaustive matches must handle these variants.

**Before (v0.1.x):**

```rust,ignore
use libmagic_rs::parser::ast::Operator;

match operator {
Operator::Equal => { /* ... */ }
Operator::NotEqual => { /* ... */ }
// Other existing variants
}
```

**After (v0.2.0):**

```rust,ignore
use libmagic_rs::parser::ast::Operator;

match operator {
Operator::Equal => { /* ... */ }
Operator::NotEqual => { /* ... */ }
Operator::LessThan => { /* ... */ }
Operator::GreaterThan => { /* ... */ }
Operator::LessEqual => { /* ... */ }
Operator::GreaterEqual => { /* ... */ }
// Other existing variants
}
```

### read_byte Function Signature

The `libmagic_rs::evaluator::types::read_byte` function signature changed from 2 to 3 parameters.

**Before (v0.1.x):**

```rust,ignore
use libmagic_rs::evaluator::types::read_byte;

let value = read_byte(buffer, offset)?;
```

**After (v0.2.0):**

```rust,ignore
use libmagic_rs::evaluator::types::read_byte;

// The third parameter indicates signedness
let signed_value = read_byte(buffer, offset, true)?;
let unsigned_value = read_byte(buffer, offset, false)?;
```

## Getting Help

If you encounter migration issues:
Expand Down
4 changes: 3 additions & 1 deletion docs/src/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ parse_operator("==") // Ok(("", Operator::Equal))
parse_operator("!=") // Ok(("", Operator::NotEqual))
parse_operator("<>") // Ok(("", Operator::NotEqual))

// Comparison operators
// Comparison operators (v0.2.0+)
parse_operator("<") // Ok(("", Operator::LessThan))
parse_operator(">") // Ok(("", Operator::GreaterThan))
parse_operator("<=") // Ok(("", Operator::LessEqual))
Expand All @@ -88,6 +88,8 @@ parse_operator("&") // Ok(("", Operator::BitwiseAnd))
- ✅ Invalid operator rejection with clear errors
- ✅ Eight comparison and bitwise operators supported

**Note:** Comparison operators (`<`, `>`, `<=`, `>=`) were implemented in v0.2.0 via [#104](https://github.com/EvilBit-Labs/libmagic-rs/pull/104).

### Value Parsing (`parse_value`)

Handles multiple value types with intelligent type detection:
Expand Down
25 changes: 11 additions & 14 deletions docs/src/release-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,21 @@ version = "0.2.0" # Update version
```markdown
# Changelog

## [0.2.0] - 2024-03-15
## [0.2.0] - 2026-03-01

### Added
- Magic file parser implementation
- Basic rule evaluation engine
- Memory-mapped file I/O support
### Features
- **parser**: Implement comparison operators

### Changed
- Improved AST structure for better performance
- Enhanced error messages with more context

### Fixed
- Buffer overflow protection in string reading
- Proper handling of indirect offsets
### Miscellaneous Tasks
- **Mergify**: Add outdated PR protection
- Add Mergify merge queue and simplify CI
- Mergify merge queue, dependabot integration, and CI simplification
- **release**: Add regex for version bumping based on commit types

### Breaking Changes
- `EvaluationConfig` structure modified
- `MagicRule::new()` signature changed
- `TypeKind::Byte` variant changed from unit variant to a different kind
- `Operator` enum: added `LessThan`, `GreaterThan`, `LessEqual`, `GreaterEqual` variants (exhaustive enum)
- `read_byte` function signature changed (parameter count modified)
```

### Release Creation
Expand Down
Loading