1use libc::{fclose, fopen};
2use std::ffi::CString;
3use std::os::raw::c_int;
4use std::path::Path;
5
6extern "C" {
7 fn ffdecompress(infile: *mut libc::c_void, outfile: *mut libc::c_void) -> c_int;
8}
9
10pub fn decompress_file<P: AsRef<Path>>(input_path: P, output_path: P) -> Result<(), String> {
20 unsafe {
21 let input = CString::new(input_path.as_ref().to_string_lossy().as_bytes())
22 .map_err(|e| format!("Invalid input path: {e}"))?;
23 let output = CString::new(output_path.as_ref().to_string_lossy().as_bytes())
24 .map_err(|e| format!("Invalid output path: {e}"))?;
25 let read_binary = CString::new("rb").map_err(|e| format!("Invalid mode: {e}"))?;
26 let write_binary = CString::new("wb").map_err(|e| format!("Invalid mode: {e}"))?;
27
28 let in_file = fopen(input.as_ptr(), read_binary.as_ptr());
29 let out_file = fopen(output.as_ptr(), write_binary.as_ptr());
30
31 if in_file.is_null() || out_file.is_null() {
32 return Err("Failed to open input or output file".into());
33 }
34
35 let result = ffdecompress(in_file.cast(), out_file.cast());
36
37 fclose(in_file);
38 fclose(out_file);
39
40 if result == 0 {
41 Ok(())
42 } else {
43 Err(format!("ffdecompress failed with code {result}"))
44 }
45 }
46}