proxmox-backup/proxmox-protocol/src/c_chunker.rs

46 lines
1.3 KiB
Rust
Raw Normal View History

//! C API for the Chunker.
use std::os::raw::c_void;
use crate::Chunker;
/// Creates a new chunker instance.
#[no_mangle]
pub extern "C" fn proxmox_chunker_new(chunk_size_avg: u64) -> *mut Chunker {
Box::leak(Box::new(Chunker::new(chunk_size_avg as usize)))
}
/// Drops an instance of a chunker. The pointer must be valid or `NULL`.
#[no_mangle]
pub extern "C" fn proxmox_chunker_done(me: *mut Chunker) {
if !me.is_null() {
unsafe {
Box::from_raw(me);
}
}
}
/// Scan the specified data for a chunk border. Returns 0 if none was found, or a positive offset
/// to a border.
#[no_mangle]
pub extern "C" fn proxmox_chunker_scan(
me: *mut Chunker,
data: *const c_void,
size: u64,
) -> u64 {
let me = unsafe { &mut *me };
me.scan(unsafe {
std::slice::from_raw_parts(data as *const u8, size as usize)
}) as u64
}
/// Compute a chunk digest. This is mostly a convenience method to avoid having to lookup the right
/// digest method for your language of choice.
#[no_mangle]
pub extern "C" fn proxmox_chunk_digest(data: *const c_void, size: u64, out_digest: *mut [u8; 32]) {
let digest = crate::FixedChunk::from_data(unsafe {
std::slice::from_raw_parts(data as *const u8, size as usize)
});
unsafe { *out_digest = digest.0 };
}