lz_decompress/
lib.rs

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
10/// Decompresses a `.lz` file to the specified output path.
11///
12/// # Arguments
13/// - `input_path`: path to the input `.lz` file.
14/// - `output_path`: path to write the decompressed output.
15///
16/// # Returns
17/// - `Ok(())` on success.
18/// - `Err(String)` on failure.
19pub 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}