libmagic-rs 0.6.0

A pure-Rust implementation of libmagic for file type identification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Copyright (c) 2025-2026 the libmagic-rs contributors
// SPDX-License-Identifier: Apache-2.0

//! Integration tests for relative offset (`OffsetSpec::Relative`) evaluation.
//!
//! Relative offsets resolve against the end of the most recent successful
//! match (the GNU `file` "previous match" anchor). The evaluation engine
//! threads this anchor through `EvaluationContext::last_match_end()`, and
//! advances it after each successful match by the number of bytes the read
//! consumed.
//!
//! Magic-file syntax for `&+N`/`&-N` is not yet wired into the parser, so
//! these tests construct rules programmatically and exercise them through
//! `evaluate_rules` directly.

use libmagic_rs::evaluator::{EvaluationContext, evaluate_rules};
use libmagic_rs::parser::ast::PStringLengthWidth;
use libmagic_rs::{Endianness, EvaluationConfig, MagicRule, OffsetSpec, Operator, TypeKind, Value};

fn cfg() -> EvaluationConfig {
    EvaluationConfig::default().with_stop_at_first_match(false)
}

fn child_rule(offset: OffsetSpec, typ: TypeKind, value: Value, message: &str) -> MagicRule {
    MagicRule {
        offset,
        typ,
        op: Operator::Equal,
        value,
        message: message.to_string(),
        children: vec![],
        level: 1,
        strength_modifier: None,
        value_transform: None,
    }
}

#[test]
fn relative_child_after_long_parent() {
    // Buffer: 4-byte LE long (0x12345678) followed by another 4-byte LE long
    // (0xCAFEBABE). Parent matches the first long, child uses Relative(0)
    // and reads at offset 4 (= parent end).
    let buffer = [0x78, 0x56, 0x34, 0x12, 0xBE, 0xBA, 0xFE, 0xCA];

    let parent = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        op: Operator::Equal,
        value: Value::Uint(0x1234_5678),
        message: "parent-long".to_string(),
        children: vec![child_rule(
            OffsetSpec::Relative(0),
            TypeKind::Long {
                endian: Endianness::Little,
                signed: false,
            },
            Value::Uint(0xCAFE_BABE),
            "child-long",
        )],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[parent], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 2, "expected parent + child match");
    assert_eq!(matches[0].message, "parent-long");
    assert_eq!(matches[0].offset, 0);
    assert_eq!(matches[1].message, "child-long");
    assert_eq!(matches[1].offset, 4);
}

#[test]
fn relative_child_with_positive_delta() {
    // Parent matches one byte at offset 0; child uses Relative(2) and reads
    // at offset 1 (parent_end) + 2 = 3.
    let buffer = [0x7F, 0xAA, 0xBB, 0x42, 0xCC];

    let parent = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x7F),
        message: "p".to_string(),
        children: vec![child_rule(
            OffsetSpec::Relative(2),
            TypeKind::Byte { signed: false },
            Value::Uint(0x42),
            "c",
        )],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[parent], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 2);
    assert_eq!(matches[1].offset, 3);
}

#[test]
fn relative_child_with_negative_delta() {
    // Parent matches a 4-byte long at offset 4; child Relative(-7) reads at
    // (4+4) - 7 = 1.
    let buffer = [0x00, 0xAA, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12, 0x00];

    let parent = MagicRule {
        offset: OffsetSpec::Absolute(4),
        typ: TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        op: Operator::Equal,
        value: Value::Uint(0x1234_5678),
        message: "p".to_string(),
        children: vec![child_rule(
            OffsetSpec::Relative(-7),
            TypeKind::Byte { signed: false },
            Value::Uint(0xAA),
            "c",
        )],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[parent], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 2);
    assert_eq!(matches[1].offset, 1);
    assert_eq!(matches[1].value, Value::Uint(0xAA));
}

#[test]
fn relative_chain_marches_forward() {
    // Three consecutive 4-byte LE longs; root + two relative children.
    let buffer = [
        0x78, 0x56, 0x34, 0x12, // 0x12345678
        0xBE, 0xBA, 0xFE, 0xCA, // 0xCAFEBABE
        0xEF, 0xBE, 0xAD, 0xDE, // 0xDEADBEEF
    ];

    let leaf = child_rule(
        OffsetSpec::Relative(0),
        TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        Value::Uint(0xDEAD_BEEF),
        "leaf",
    );
    let mut middle = child_rule(
        OffsetSpec::Relative(0),
        TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        Value::Uint(0xCAFE_BABE),
        "middle",
    );
    middle.children = vec![leaf];

    let root = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        op: Operator::Equal,
        value: Value::Uint(0x1234_5678),
        message: "root".to_string(),
        children: vec![middle],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[root], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 3);
    let offsets: Vec<usize> = matches.iter().map(|m| m.offset).collect();
    assert_eq!(offsets, vec![0, 4, 8]);
}

#[test]
fn relative_after_string_parent_includes_nul_terminator() {
    // String "MZ" at offset 0 followed by NUL (3 bytes consumed), then a
    // byte the child reads via Relative(0).
    let buffer = b"MZ\x00\x42rest";

    let parent = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::String { max_length: None },
        op: Operator::Equal,
        value: Value::String("MZ".to_string()),
        message: "mz".to_string(),
        children: vec![child_rule(
            OffsetSpec::Relative(0),
            TypeKind::Byte { signed: false },
            Value::Uint(0x42),
            "byte-after-mz",
        )],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[parent], buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 2, "child should match after MZ + NUL");
    assert_eq!(matches[1].offset, 3);
}

#[test]
fn relative_after_pstring_parent_consumes_prefix_and_payload() {
    // pstring(/B) at offset 0 with prefix 0x05, payload "Hello" (6 bytes
    // total), then a byte at offset 6.
    let buffer = b"\x05Hello\x42tail";

    let parent = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::PString {
            max_length: None,
            length_width: PStringLengthWidth::OneByte,
            length_includes_itself: false,
        },
        op: Operator::Equal,
        value: Value::String("Hello".to_string()),
        message: "pstr".to_string(),
        children: vec![child_rule(
            OffsetSpec::Relative(0),
            TypeKind::Byte { signed: false },
            Value::Uint(0x42),
            "byte-after-pstr",
        )],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[parent], buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 2);
    assert_eq!(matches[1].offset, 6);
}

#[test]
fn relative_top_level_resolves_from_zero_anchor() {
    // No prior match: top-level Relative(2) -> absolute 2.
    let buffer = [0xAA, 0xBB, 0x42, 0xCC];

    let rule = MagicRule {
        offset: OffsetSpec::Relative(2),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x42),
        message: "top".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[rule], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 1);
    assert_eq!(matches[0].offset, 2);
}

#[test]
fn relative_sibling_propagation_at_top_level() {
    // GNU `file` semantics: anchor advances monotonically; the second
    // top-level rule sees the anchor that the first rule left behind.
    // First rule matches a 4-byte long at offset 0 -> anchor becomes 4.
    // Second rule uses Relative(0) -> reads at offset 4.
    let buffer = [0x78, 0x56, 0x34, 0x12, 0x42, 0x00, 0x00, 0x00];

    let first = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        op: Operator::Equal,
        value: Value::Uint(0x1234_5678),
        message: "first".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };
    let second = MagicRule {
        offset: OffsetSpec::Relative(0),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x42),
        message: "second".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[first, second], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 2);
    assert_eq!(matches[0].offset, 0);
    assert_eq!(matches[1].offset, 4);
}

#[test]
fn relative_out_of_bounds_skips_child_gracefully() {
    // Parent matches; child uses Relative(50) which lands past the buffer.
    // Engine should skip the child and continue without panicking.
    let buffer = [0x7F, 0xAA, 0xBB];

    let parent = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x7F),
        message: "p".to_string(),
        children: vec![child_rule(
            OffsetSpec::Relative(50),
            TypeKind::Byte { signed: false },
            Value::Uint(0x00),
            "c",
        )],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[parent], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 1, "only the parent should match");
    assert_eq!(matches[0].message, "p");
}

#[test]
fn relative_anchor_can_decrease_when_later_sibling_matches_at_lower_position() {
    // GNU `file` semantics: the anchor reflects the END of the most recent
    // match -- not a high-watermark. If a later sibling matches at a lower
    // absolute position, the anchor moves backwards. This test pins the
    // documented "may increase or decrease" behavior so a future
    // optimization that adds a max() guard fails loudly.
    //
    // Layout: 16 bytes. Rule A matches a 4-byte LE long at offset 8.
    // After A, anchor = 12. Rule B matches a single byte at offset 2
    // (Absolute(2)). After B, anchor = 3. Rule C uses Relative(0) and
    // must read at offset 3, NOT offset 12.
    let buffer = [
        0x00, 0x00, 0xAA, 0x99, 0x00, 0x00, 0x00, 0x00, // bytes 0-7
        0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, // bytes 8-15
    ];

    let rule_a = MagicRule {
        offset: OffsetSpec::Absolute(8),
        typ: TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        op: Operator::Equal,
        value: Value::Uint(0x1234_5678),
        message: "rule-a-high".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };
    let rule_b = MagicRule {
        offset: OffsetSpec::Absolute(2),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0xAA),
        message: "rule-b-low".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };
    let rule_c = MagicRule {
        offset: OffsetSpec::Relative(0),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x99),
        message: "rule-c-relative".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[rule_a, rule_b, rule_c], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 3, "all three rules should match");
    assert_eq!(matches[0].message, "rule-a-high");
    assert_eq!(matches[0].offset, 8);
    assert_eq!(matches[1].message, "rule-b-low");
    assert_eq!(matches[1].offset, 2);
    assert_eq!(
        matches[2].offset, 3,
        "rule C must read at offset 3 (rule B's end), proving the anchor moved backwards from 12 -> 3"
    );
}

#[test]
fn relative_anchor_persists_across_non_matching_intermediate_sibling() {
    // First top-level rule matches a 4-byte LE long -> anchor advances to 4.
    // Second top-level rule does NOT match (wrong expected value) -> anchor
    // stays at 4.
    // Third top-level rule uses Relative(0) -> reads at offset 4.
    let buffer = [0x78, 0x56, 0x34, 0x12, 0x42, 0x00, 0x00, 0x00];

    let first = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        op: Operator::Equal,
        value: Value::Uint(0x1234_5678),
        message: "first".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };
    let middle_no_match = MagicRule {
        offset: OffsetSpec::Absolute(4),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0xDE), // does not match (real byte is 0x42)
        message: "middle-skip".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };
    let third = MagicRule {
        offset: OffsetSpec::Relative(0),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x42),
        message: "third".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[first, middle_no_match, third], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 2, "first + third match, middle skipped");
    assert_eq!(matches[0].message, "first");
    assert_eq!(matches[1].message, "third");
    assert_eq!(matches[1].offset, 4);
}

#[test]
fn relative_anchor_resets_between_evaluations_via_reset() {
    // Evaluate against a first buffer, advancing the anchor. Reset the
    // context. Evaluate against a second buffer with a Relative(0) rule;
    // the anchor must start at 0, not the leaked value from the first run.
    let buffer_a = [0x78, 0x56, 0x34, 0x12];
    let buffer_b = [0x42, 0xAA, 0xBB, 0xCC];

    let pass_one = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        op: Operator::Equal,
        value: Value::Uint(0x1234_5678),
        message: "pass-one".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };
    let pass_two = MagicRule {
        offset: OffsetSpec::Relative(0),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x42),
        message: "pass-two".to_string(),
        children: vec![],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let _ = evaluate_rules(&[pass_one], &buffer_a, &mut ctx).unwrap();
    ctx.reset();
    let matches = evaluate_rules(&[pass_two], &buffer_b, &mut ctx).unwrap();
    assert_eq!(matches.len(), 1);
    assert_eq!(
        matches[0].offset, 0,
        "Relative(0) should resolve to 0 after reset"
    );
}

#[test]
fn relative_underflow_skips_child_gracefully() {
    // Anchor=1 (after parent byte), child Relative(-100) underflows.
    let buffer = [0x7F, 0xAA];

    let parent = MagicRule {
        offset: OffsetSpec::Absolute(0),
        typ: TypeKind::Byte { signed: false },
        op: Operator::Equal,
        value: Value::Uint(0x7F),
        message: "p".to_string(),
        children: vec![child_rule(
            OffsetSpec::Relative(-100),
            TypeKind::Byte { signed: false },
            Value::Uint(0x00),
            "c",
        )],
        level: 0,
        strength_modifier: None,
        value_transform: None,
    };

    let mut ctx = EvaluationContext::new(cfg());
    let matches = evaluate_rules(&[parent], &buffer, &mut ctx).unwrap();
    assert_eq!(matches.len(), 1);
}