syara-x 0.3.1

Super YARA — extends YARA-compatible rules with semantic, classifier, and LLM-based matching
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Perceptual hash matching for binary files (images, audio, video).
//!
//! All matchers produce a 64-bit dHash-style fingerprint. Similarity is
//! `1.0 - hamming_distance / 64.0`. Three built-in implementations:
//!
//! - [`ImageHashMatcher`] — dHash on images via the `image` crate
//! - [`AudioHashMatcher`] — dHash on PCM WAV files (pure Rust WAV reader)
//! - [`VideoHashMatcher`] — byte-sampling fingerprint (no external deps)

use std::path::Path;

use crate::error::SyaraError;
use crate::models::{MatchDetail, PHashRule};

// ── Trait ─────────────────────────────────────────────────────────────────────

/// Perceptual hash matcher.
///
/// Implementations compute a 64-bit hash of a file's content. The default
/// [`match_rule`] compares the scanned file against the reference path stored
/// in the rule.
pub trait PHashMatcher: Send + Sync {
    /// Compute a 64-bit perceptual hash for `file_path`.
    fn compute_hash(&self, file_path: &Path) -> Result<u64, SyaraError>;

    /// Hamming distance between two hashes (number of differing bits).
    fn hamming_distance(&self, hash1: u64, hash2: u64) -> u32 {
        (hash1 ^ hash2).count_ones()
    }

    /// Compare `file_path` (scanned file) against `rule.file_path` (reference).
    ///
    /// Returns a [`MatchDetail`] when `similarity >= rule.threshold`.
    fn match_rule(
        &self,
        rule: &PHashRule,
        file_path: &Path,
    ) -> Result<Vec<MatchDetail>, SyaraError> {
        let ref_path = Path::new(&rule.file_path);
        if !ref_path.exists() {
            return Err(SyaraError::FileNotFound(rule.file_path.clone()));
        }
        let ref_hash = self.compute_hash(ref_path)?;
        let input_hash = self.compute_hash(file_path)?;
        let distance = self.hamming_distance(ref_hash, input_hash);
        let similarity = 1.0 - (distance as f64 / 64.0);
        if similarity >= rule.threshold {
            let mut detail = MatchDetail::new(
                rule.identifier.clone(),
                file_path.to_string_lossy().into_owned(),
            );
            detail.score = similarity;
            detail.explanation = format!("PHash similarity: {similarity:.3}");
            Ok(vec![detail])
        } else {
            Ok(vec![])
        }
    }
}

// ── Image matcher ─────────────────────────────────────────────────────────────

/// dHash matcher for image files.
///
/// Converts to grayscale, resizes to `(hash_size + 1) × hash_size`, and
/// encodes the horizontal gradient as a 64-bit hash. Default `hash_size = 8`
/// produces a 64-bit hash.
pub struct ImageHashMatcher {
    hash_size: u32,
}

impl ImageHashMatcher {
    pub fn new(hash_size: u32) -> Self {
        Self { hash_size }
    }

    /// Maximum hash_size that fits in a 64-bit hash (8×8 = 64 bits).
    const MAX_HASH_SIZE: u32 = 8;
}

impl Default for ImageHashMatcher {
    fn default() -> Self {
        Self::new(8)
    }
}

impl PHashMatcher for ImageHashMatcher {
    fn compute_hash(&self, file_path: &Path) -> Result<u64, SyaraError> {
        // BUG-003: hash_size > 8 causes bit shift overflow (8×8 = 64 bits max)
        if self.hash_size > Self::MAX_HASH_SIZE {
            return Err(SyaraError::PhashError(format!(
                "hash_size {} exceeds maximum of {} (64-bit hash limit)",
                self.hash_size,
                Self::MAX_HASH_SIZE,
            )));
        }

        let img = image::open(file_path)
            .map_err(|e| SyaraError::PhashError(e.to_string()))?
            .into_luma8();

        let width = self.hash_size + 1;
        let height = self.hash_size;

        let img =
            image::imageops::resize(&img, width, height, image::imageops::FilterType::Lanczos3);

        let mut hash: u64 = 0;
        for row in 0..height {
            for col in 0..self.hash_size {
                let left = img.get_pixel(col, row).0[0];
                let right = img.get_pixel(col + 1, row).0[0];
                if left > right {
                    hash |= 1u64 << (row * self.hash_size + col);
                }
            }
        }
        Ok(hash)
    }
}

// ── Audio (WAV) matcher ───────────────────────────────────────────────────────

/// dHash-style matcher for PCM WAV audio files.
///
/// Samples 65 evenly-spaced frames from the `data` chunk, then builds a
/// 64-bit hash by comparing consecutive amplitude values. Uses a minimal
/// pure-Rust WAV reader — no external audio crate required.
pub struct AudioHashMatcher;

impl Default for AudioHashMatcher {
    fn default() -> Self {
        Self
    }
}

impl PHashMatcher for AudioHashMatcher {
    fn compute_hash(&self, file_path: &Path) -> Result<u64, SyaraError> {
        use std::fs::File;
        use std::io::{Read, Seek, SeekFrom};

        let mut f = File::open(file_path).map_err(|e| SyaraError::PhashError(e.to_string()))?;

        // Validate RIFF/WAVE header
        let mut header = [0u8; 12];
        f.read_exact(&mut header)
            .map_err(|e| SyaraError::PhashError(e.to_string()))?;
        if &header[0..4] != b"RIFF" || &header[8..12] != b"WAVE" {
            return Err(SyaraError::PhashError("not a valid WAV file".into()));
        }

        // BUG-015: maximum allocation for any single WAV chunk
        const MAX_CHUNK_ALLOC: u64 = 256 * 1024 * 1024; // 256 MB

        // Scan chunks for fmt + data
        let mut sample_width: u64 = 2; // bytes per sample (default 16-bit)
        let mut n_channels: u64 = 1; // BUG-014: track channel count
        let mut data_offset: u64 = 0;
        let mut n_frames: u64 = 0;

        loop {
            let mut id = [0u8; 4];
            let mut sz = [0u8; 4];
            if f.read_exact(&mut id).is_err() || f.read_exact(&mut sz).is_err() {
                break;
            }
            let chunk_len = u32::from_le_bytes(sz) as u64;

            if &id == b"fmt " {
                // BUG-015: reject oversized fmt chunks
                if chunk_len > MAX_CHUNK_ALLOC {
                    return Err(SyaraError::PhashError(format!(
                        "WAV fmt chunk too large: {chunk_len} bytes"
                    )));
                }
                let mut fmt = vec![0u8; chunk_len as usize];
                f.read_exact(&mut fmt)
                    .map_err(|e| SyaraError::PhashError(e.to_string()))?;
                if fmt.len() >= 16 {
                    // BUG-014: num_channels at offset 2 in fmt chunk body
                    n_channels = u16::from_le_bytes([fmt[2], fmt[3]]).max(1) as u64;
                    // bits_per_sample at offset 14
                    let bits = u16::from_le_bytes([fmt[14], fmt[15]]);
                    sample_width = (bits / 8).max(1) as u64;
                }
                // BUG-029: RIFF chunks are padded to even byte boundaries
                if !chunk_len.is_multiple_of(2) {
                    f.seek(SeekFrom::Current(1))
                        .map_err(|e| SyaraError::PhashError(e.to_string()))?;
                }
            } else if &id == b"data" {
                // BUG-015: reject oversized data chunks
                if chunk_len > MAX_CHUNK_ALLOC {
                    return Err(SyaraError::PhashError(format!(
                        "WAV data chunk too large: {chunk_len} bytes"
                    )));
                }
                data_offset = f
                    .stream_position()
                    .map_err(|e| SyaraError::PhashError(e.to_string()))?;
                // BUG-014: divide by frame size (channels × sample_width)
                let frame_size = n_channels * sample_width.max(1);
                n_frames = chunk_len / frame_size;
                break;
            } else {
                // BUG-029: seek past chunk + pad byte for odd-length chunks
                let seek_len = chunk_len + (chunk_len % 2);
                f.seek(SeekFrom::Current(seek_len as i64))
                    .map_err(|e| SyaraError::PhashError(e.to_string()))?;
            }
        }

        if n_frames == 0 {
            return Ok(0);
        }

        // Sample 65 evenly-spaced frames
        let n_samples: u64 = 65;
        let step = (n_frames / n_samples).max(1);
        let frame_size = n_channels * sample_width;
        let mut samples: Vec<i32> = Vec::with_capacity(n_samples as usize);

        for i in 0..n_samples {
            let pos = (i * step).min(n_frames - 1);
            // BUG-014: seek by frame_size to account for multi-channel audio
            f.seek(SeekFrom::Start(data_offset + pos * frame_size))
                .map_err(|e| SyaraError::PhashError(e.to_string()))?;
            let mut raw = vec![0u8; sample_width as usize];
            let val = if f.read_exact(&mut raw).is_ok() {
                match sample_width {
                    1 => (raw[0] as i32) - 128,
                    2 => i16::from_le_bytes([raw[0], raw[1]]) as i32,
                    4 => i32::from_le_bytes([raw[0], raw[1], raw[2], raw[3]]),
                    _ => raw[0] as i32,
                }
            } else {
                0
            };
            samples.push(val);
        }

        // Build 64-bit hash: bit i set when sample[i] > sample[i+1]
        let mut hash: u64 = 0;
        for i in 0..64usize {
            if i + 1 < samples.len() && samples[i] > samples[i + 1] {
                hash |= 1u64 << i;
            }
        }
        Ok(hash)
    }
}

// ── Video (raw bytes) matcher ─────────────────────────────────────────────────

/// Byte-sampling fingerprint for video (or any binary) files.
///
/// Reads 65 bytes at evenly-spaced positions across the file and encodes each
/// consecutive pair comparison as a bit. Requires no external dependencies.
pub struct VideoHashMatcher;

impl Default for VideoHashMatcher {
    fn default() -> Self {
        Self
    }
}

impl PHashMatcher for VideoHashMatcher {
    fn compute_hash(&self, file_path: &Path) -> Result<u64, SyaraError> {
        use std::fs::File;
        use std::io::{Read, Seek, SeekFrom};

        let file_size = file_path
            .metadata()
            .map_err(|e| SyaraError::PhashError(e.to_string()))?
            .len();

        if file_size == 0 {
            return Ok(0);
        }

        let mut f = File::open(file_path).map_err(|e| SyaraError::PhashError(e.to_string()))?;
        let n_samples: u64 = 65;
        let mut samples = Vec::with_capacity(n_samples as usize);

        for i in 0..n_samples {
            let pos = if file_size > 1 {
                i * (file_size - 1) / (n_samples - 1)
            } else {
                0
            };
            f.seek(SeekFrom::Start(pos))
                .map_err(|e| SyaraError::PhashError(e.to_string()))?;
            let mut byte = [0u8; 1];
            samples.push(if f.read_exact(&mut byte).is_ok() { byte[0] } else { 0 });
        }

        let mut hash: u64 = 0;
        for i in 0..64usize {
            if samples[i] > samples[i + 1] {
                hash |= 1u64 << i;
            }
        }
        Ok(hash)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    // ── Trait unit tests ──────────────────────────────────────────────────────

    struct FixedHashMatcher(u64);

    impl PHashMatcher for FixedHashMatcher {
        fn compute_hash(&self, _: &Path) -> Result<u64, SyaraError> {
            Ok(self.0)
        }
    }

    #[test]
    fn hamming_distance_identical() {
        let m = FixedHashMatcher(0);
        assert_eq!(m.hamming_distance(0xDEADBEEF, 0xDEADBEEF), 0);
    }

    #[test]
    fn hamming_distance_known() {
        let m = FixedHashMatcher(0);
        // 0b0000 vs 0b1111 → 4 differing bits
        assert_eq!(m.hamming_distance(0b0000u64, 0b1111u64), 4);
    }

    #[test]
    fn match_rule_above_threshold() {
        // Two identical hashes → similarity = 1.0 ≥ 0.9
        let m = FixedHashMatcher(0xABCD);

        // Write two identical temp files (content irrelevant; matcher ignores it)
        let ref_file = temp_file(b"ref");
        let input_file = temp_file(b"input");

        let rule = PHashRule {
            identifier: "$ph1".into(),
            file_path: ref_file.path().to_string_lossy().into_owned(),
            threshold: 0.9,
            phash_name: "imagehash".into(),
        };

        let results = m.match_rule(&rule, input_file.path()).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].identifier, "$ph1");
        assert!((results[0].score - 1.0).abs() < 1e-9);
        assert!(results[0].explanation.contains("PHash similarity:"));
    }

    #[test]
    fn match_rule_below_threshold() {
        // We need a matcher that returns 0 for ref and MAX for input —
        struct PairMatcher { call: std::sync::atomic::AtomicU64 }
        impl PHashMatcher for PairMatcher {
            fn compute_hash(&self, _: &Path) -> Result<u64, SyaraError> {
                let n = self.call.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Ok(if n == 0 { 0u64 } else { u64::MAX })
            }
        }

        let ref_file = temp_file(b"ref");
        let input_file = temp_file(b"input");

        let rule = PHashRule {
            identifier: "$ph2".into(),
            file_path: ref_file.path().to_string_lossy().into_owned(),
            threshold: 0.9,
            phash_name: "imagehash".into(),
        };

        let m = PairMatcher { call: std::sync::atomic::AtomicU64::new(0) };
        let results = m.match_rule(&rule, input_file.path()).unwrap();
        assert!(results.is_empty());
    }

    // ── VideoHashMatcher tests ────────────────────────────────────────────────

    #[test]
    fn video_hash_empty_file_returns_zero() {
        let f = temp_file(b"");
        let hash = VideoHashMatcher.compute_hash(f.path()).unwrap();
        assert_eq!(hash, 0);
    }

    #[test]
    fn video_hash_deterministic() {
        let data = b"the quick brown fox jumps over the lazy dog 0123456789abcdef";
        let f = temp_file(data);
        let h1 = VideoHashMatcher.compute_hash(f.path()).unwrap();
        let h2 = VideoHashMatcher.compute_hash(f.path()).unwrap();
        assert_eq!(h1, h2);
    }

    #[test]
    fn video_hash_differs_for_different_content() {
        // Ascending bytes → each sample[i] < sample[i+1] → hash ≈ 0
        // Descending bytes → each sample[i] > sample[i+1] → hash ≈ MAX
        let asc: Vec<u8> = (0u8..=127).collect();
        let desc: Vec<u8> = (0u8..=127).rev().collect();
        let f1 = temp_file(&asc);
        let f2 = temp_file(&desc);
        let h1 = VideoHashMatcher.compute_hash(f1.path()).unwrap();
        let h2 = VideoHashMatcher.compute_hash(f2.path()).unwrap();
        assert_ne!(h1, h2);
    }

    // ── AudioHashMatcher tests ────────────────────────────────────────────────

    #[test]
    fn audio_hash_invalid_file_returns_error() {
        let f = temp_file(b"not a wav file at all");
        let result = AudioHashMatcher.compute_hash(f.path());
        assert!(result.is_err());
    }

    #[test]
    fn audio_hash_deterministic() {
        let wav = minimal_wav(44100, &sawtooth_samples(65));
        let f = temp_file(&wav);
        let h1 = AudioHashMatcher.compute_hash(f.path()).unwrap();
        let h2 = AudioHashMatcher.compute_hash(f.path()).unwrap();
        assert_eq!(h1, h2);
    }

    #[test]
    fn audio_hash_empty_data_returns_zero() {
        let wav = minimal_wav(44100, &[]);
        let f = temp_file(&wav);
        let hash = AudioHashMatcher.compute_hash(f.path()).unwrap();
        assert_eq!(hash, 0);
    }

    // ── BUG-003: hash_size > 8 returns error ──────────────────────────────

    #[test]
    fn image_hash_size_9_returns_error() {
        let m = ImageHashMatcher::new(9);
        let f = temp_file(b"fake image");
        let result = m.compute_hash(f.path());
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("hash_size"), "error should mention hash_size: {msg}");
    }

    #[test]
    fn image_hash_size_8_is_valid() {
        // hash_size = 8 is the default and must be accepted (no error from validation)
        let m = ImageHashMatcher::new(8);
        assert_eq!(m.hash_size, ImageHashMatcher::MAX_HASH_SIZE);
    }

    // ── BUG-014: stereo WAV frame count ─────────────────────────────────────

    #[test]
    fn audio_hash_stereo_correct_frame_count() {
        // Stereo WAV: 65 frames × 2 channels = 130 samples
        let mono_samples = sawtooth_samples(65);
        let stereo_samples: Vec<i16> = mono_samples
            .iter()
            .flat_map(|&s| [s, s]) // duplicate for L+R
            .collect();

        let mono_wav = minimal_wav_channels(44100, 1, &mono_samples);
        let stereo_wav = minimal_wav_channels(44100, 2, &stereo_samples);

        let f_mono = temp_file(&mono_wav);
        let f_stereo = temp_file(&stereo_wav);

        let h_mono = AudioHashMatcher.compute_hash(f_mono.path()).unwrap();
        let h_stereo = AudioHashMatcher.compute_hash(f_stereo.path()).unwrap();

        // With identical left-channel content, hashes should be equal
        assert_eq!(h_mono, h_stereo);
    }

    // ── BUG-015: oversized WAV chunk rejected ───────────────────────────────

    #[test]
    fn audio_hash_oversized_data_chunk_returns_error() {
        // Craft a WAV with a data chunk_len claiming 512MB
        let mut wav = Vec::new();
        wav.extend_from_slice(b"RIFF");
        wav.extend_from_slice(&0u32.to_le_bytes()); // file size (don't care)
        wav.extend_from_slice(b"WAVE");
        wav.extend_from_slice(b"fmt ");
        wav.extend_from_slice(&16u32.to_le_bytes());
        wav.extend_from_slice(&1u16.to_le_bytes()); // PCM
        wav.extend_from_slice(&1u16.to_le_bytes()); // mono
        wav.extend_from_slice(&44100u32.to_le_bytes());
        wav.extend_from_slice(&(44100u32 * 2).to_le_bytes());
        wav.extend_from_slice(&2u16.to_le_bytes());
        wav.extend_from_slice(&16u16.to_le_bytes());
        wav.extend_from_slice(b"data");
        wav.extend_from_slice(&(512 * 1024 * 1024u32).to_le_bytes()); // 512 MB

        let f = temp_file(&wav);
        let result = AudioHashMatcher.compute_hash(f.path());
        assert!(result.is_err(), "oversized data chunk must be rejected");
    }

    // ── BUG-029: odd-sized chunk padding ────────────────────────────────────

    #[test]
    fn audio_hash_odd_chunk_padding() {
        // Build WAV with an odd-length unknown chunk before the data chunk.
        // The parser must skip the padding byte to find the data chunk.
        let samples = sawtooth_samples(65);
        let data_len = (samples.len() * 2) as u32;

        let mut wav = Vec::new();
        wav.extend_from_slice(b"RIFF");
        wav.extend_from_slice(&0u32.to_le_bytes()); // placeholder
        wav.extend_from_slice(b"WAVE");
        // fmt chunk
        wav.extend_from_slice(b"fmt ");
        wav.extend_from_slice(&16u32.to_le_bytes());
        wav.extend_from_slice(&1u16.to_le_bytes()); // PCM
        wav.extend_from_slice(&1u16.to_le_bytes()); // mono
        wav.extend_from_slice(&44100u32.to_le_bytes());
        wav.extend_from_slice(&(44100u32 * 2).to_le_bytes());
        wav.extend_from_slice(&2u16.to_le_bytes());
        wav.extend_from_slice(&16u16.to_le_bytes());
        // Unknown chunk with odd length (3 bytes + 1 padding)
        wav.extend_from_slice(b"LIST");
        wav.extend_from_slice(&3u32.to_le_bytes()); // 3 bytes = odd
        wav.extend_from_slice(&[0xAA, 0xBB, 0xCC]); // body
        wav.push(0x00); // RIFF padding byte
        // data chunk
        wav.extend_from_slice(b"data");
        wav.extend_from_slice(&data_len.to_le_bytes());
        for &s in &samples {
            wav.extend_from_slice(&s.to_le_bytes());
        }
        // Fix RIFF file size
        let file_len = (wav.len() - 8) as u32;
        wav[4..8].copy_from_slice(&file_len.to_le_bytes());

        let f = temp_file(&wav);
        let result = AudioHashMatcher.compute_hash(f.path());
        assert!(result.is_ok(), "must parse WAV with odd-sized chunks: {result:?}");
        // Verify it produces a non-zero hash (65 sawtooth samples)
        assert_ne!(result.unwrap(), 0);
    }

    // ── Helpers ───────────────────────────────────────────────────────────────

    /// Write `data` to a named temporary file; returns the guard.
    fn temp_file(data: &[u8]) -> tempfile::NamedTempFile {
        let mut f = tempfile::NamedTempFile::new().unwrap();
        f.write_all(data).unwrap();
        f
    }

    /// Build a minimal valid PCM WAV byte vector with 16-bit mono samples.
    fn minimal_wav(sample_rate: u32, samples: &[i16]) -> Vec<u8> {
        minimal_wav_channels(sample_rate, 1, samples)
    }

    /// Build a valid PCM WAV byte vector with configurable channel count.
    fn minimal_wav_channels(sample_rate: u32, channels: u16, samples: &[i16]) -> Vec<u8> {
        let data_len = (samples.len() * 2) as u32;
        let file_len = 36 + data_len;
        let block_align = channels * 2; // 16-bit per channel
        let byte_rate = sample_rate * block_align as u32;

        let mut v: Vec<u8> = Vec::with_capacity(file_len as usize + 8);
        // RIFF header
        v.extend_from_slice(b"RIFF");
        v.extend_from_slice(&file_len.to_le_bytes());
        v.extend_from_slice(b"WAVE");
        // fmt chunk (16 bytes body)
        v.extend_from_slice(b"fmt ");
        v.extend_from_slice(&16u32.to_le_bytes());
        v.extend_from_slice(&1u16.to_le_bytes()); // PCM
        v.extend_from_slice(&channels.to_le_bytes());
        v.extend_from_slice(&sample_rate.to_le_bytes());
        v.extend_from_slice(&byte_rate.to_le_bytes());
        v.extend_from_slice(&block_align.to_le_bytes());
        v.extend_from_slice(&16u16.to_le_bytes()); // bits per sample
        // data chunk
        v.extend_from_slice(b"data");
        v.extend_from_slice(&data_len.to_le_bytes());
        for &s in samples {
            v.extend_from_slice(&s.to_le_bytes());
        }
        v
    }

    /// Generate a simple sawtooth wave of `n` 16-bit samples.
    fn sawtooth_samples(n: usize) -> Vec<i16> {
        (0..n).map(|i| ((i as i32 * 1000) % i16::MAX as i32) as i16).collect()
    }
}