๐Ÿ“ฆ qarmin / Automated-Fuzzer

๐Ÿ“„ vidduplicate.rs ยท 32 lines
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#![no_main]

use std::env::{args, temp_dir};
use std::path::{Path, PathBuf};
use std::{fs, io};

use libfuzzer_sys::{Corpus, fuzz_target};
use tempfile::tempdir;
use vid_dup_finder_lib::ffmpeg_builder;

fuzz_target!(|data: &[u8]| -> Corpus {
    if calculate_hash(data.to_vec()).is_ok() {
        Corpus::Keep
    } else {
        Corpus::Reject
    }
});

pub(crate) fn calculate_hash(content: Vec<u8>) -> Result<(), String> {
    let temp_dir = tempdir().map_err(|e| e.to_string())?;
    let temp_file = temp_dir.path().join(format!("fuzz_file_{}", rand::random::<u32>()));

    fs::write(&temp_file, content).map_err(|e| e.to_string())?;

    let res = ffmpeg_builder::VideoHashBuilder::default().hash(PathBuf::from(&temp_file));

    let _ = fs::remove_file(temp_file);

    res.map_err(|e| e.to_string())?;
    Ok(())
}