]> git.proxmox.com Git - proxmox-backup.git/blob - proxmox-protocol/src/c_chunker.rs
c24730eb856b54ca3a0b3e8bfc173380f37c111a
[proxmox-backup.git] / proxmox-protocol / src / c_chunker.rs
1 //! C API for the Chunker.
2
3 use std::os::raw::c_void;
4
5 use libc::size_t;
6
7 use crate::Chunker;
8
9 /// Creates a new chunker instance.
10 #[no_mangle]
11 pub extern "C" fn proxmox_chunker_new(chunk_size_avg: size_t) -> *mut Chunker {
12 Box::leak(Box::new(Chunker::new(chunk_size_avg as usize)))
13 }
14
15 /// Drops an instance of a chunker. The pointer must be valid or `NULL`.
16 #[no_mangle]
17 pub extern "C" fn proxmox_chunker_done(me: *mut Chunker) {
18 if !me.is_null() {
19 unsafe {
20 Box::from_raw(me);
21 }
22 }
23 }
24
25 /// Scan the specified data for a chunk border. Returns 0 if none was found, or a positive offset
26 /// to a border.
27 #[no_mangle]
28 pub extern "C" fn proxmox_chunker_scan(
29 me: *mut Chunker,
30 data: *const c_void,
31 size: size_t,
32 ) -> size_t {
33 let me = unsafe { &mut *me };
34 me.scan(unsafe { std::slice::from_raw_parts(data as *const u8, size as usize) }) as size_t
35 }
36
37 /// Compute a chunk digest. This is mostly a convenience method to avoid having to lookup the right
38 /// digest method for your language of choice.
39 #[no_mangle]
40 pub extern "C" fn proxmox_chunk_digest(
41 data: *const c_void,
42 size: size_t,
43 out_digest: *mut [u8; 32],
44 ) {
45 let digest = crate::FixedChunk::from_data(unsafe {
46 std::slice::from_raw_parts(data as *const u8, size as usize)
47 });
48 unsafe { *out_digest = digest.0 };
49 }