use std::fs::File;
use std::io::Read;
use std::path::Path;
pub fn is_zip(path: &Path) -> bool {
check_magic_bytes(path, &[0x50, 0x4B, 0x03, 0x04])
}
pub fn is_squashfs(path: &Path) -> bool {
check_magic_bytes(path, &[0x68, 0x73, 0x71, 0x73])
|| check_magic_bytes(path, &[0x73, 0x71, 0x73, 0x68])
}
pub fn is_nsis_installer(path: &Path) -> bool {
const SEARCH_SIZE: usize = 8192; const NSIS_SIGNATURE: &[u8] = b"Nullsoft.NSIS.exehead";
let mut file = match File::open(path) {
Ok(f) => f,
Err(_) => return false,
};
let mut buffer = vec![0u8; SEARCH_SIZE];
let bytes_read = match file.read(&mut buffer) {
Ok(n) => n,
Err(_) => return false,
};
buffer.truncate(bytes_read);
buffer
.windows(NSIS_SIGNATURE.len())
.any(|window| window == NSIS_SIGNATURE)
}
fn check_magic_bytes(path: &Path, magic: &[u8]) -> bool {
let mut file = match File::open(path) {
Ok(f) => f,
Err(_) => return false,
};
let mut buffer = vec![0u8; magic.len()];
match file.read_exact(&mut buffer) {
Ok(()) => buffer == magic,
Err(_) => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_is_zip() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(&[0x50, 0x4B, 0x03, 0x04, 0x00, 0x00])
.unwrap();
assert!(is_zip(file.path()));
let mut file2 = NamedTempFile::new().unwrap();
file2.write_all(&[0x1F, 0x8B, 0x08, 0x00]).unwrap();
assert!(!is_zip(file2.path()));
assert!(!is_zip(Path::new("/nonexistent/file.zip")));
}
#[test]
fn test_is_squashfs_little_endian() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(&[0x68, 0x73, 0x71, 0x73, 0x00, 0x00])
.unwrap();
assert!(is_squashfs(file.path()));
}
#[test]
fn test_is_squashfs_big_endian() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(&[0x73, 0x71, 0x73, 0x68, 0x00, 0x00])
.unwrap();
assert!(is_squashfs(file.path()));
}
#[test]
fn test_is_squashfs_negative() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(&[0x50, 0x4B, 0x03, 0x04]).unwrap();
assert!(!is_squashfs(file.path()));
assert!(!is_squashfs(Path::new("/nonexistent/file.squashfs")));
}
#[test]
fn test_is_nsis_installer() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"MZ\x90\x00").unwrap(); file.write_all(b"Nullsoft.NSIS.exehead").unwrap();
file.write_all(&[0u8; 100]).unwrap();
assert!(is_nsis_installer(file.path()));
let mut file2 = NamedTempFile::new().unwrap();
file2.write_all(&vec![0u8; 1000]).unwrap();
file2.write_all(b"Nullsoft.NSIS.exehead").unwrap();
assert!(is_nsis_installer(file2.path()));
let mut file3 = NamedTempFile::new().unwrap();
file3.write_all(b"This is not an NSIS installer").unwrap();
assert!(!is_nsis_installer(file3.path()));
assert!(!is_nsis_installer(Path::new("/nonexistent/setup.exe")));
}
#[test]
fn test_is_nsis_installer_beyond_8kb() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(&vec![0u8; 8500]).unwrap();
file.write_all(b"Nullsoft.NSIS.exehead").unwrap();
assert!(!is_nsis_installer(file.path()));
}
#[test]
fn test_check_magic_bytes_short_file() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(&[0x50, 0x4B]).unwrap(); assert!(!check_magic_bytes(file.path(), &[0x50, 0x4B, 0x03, 0x04]));
}
#[test]
fn test_check_magic_bytes_empty_file() {
let file = NamedTempFile::new().unwrap();
assert!(!check_magic_bytes(file.path(), &[0x50, 0x4B]));
}
}