Skip to content

Commit 8c97d7e

Browse files
committed
Update custom CRC params support in FFI
- Adds the Crc32Custom and Crc64Custom CrcFastAlgorithms - Adds the crc_fast_checksum_combine_with_custom_params() function - Adds the crc_fast_get_custom_params() function
1 parent 17f4057 commit 8c97d7e

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

libcrc_fast.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ typedef enum CrcFastAlgorithm {
1919
Crc32Bzip2,
2020
Crc32CdRomEdc,
2121
Crc32Cksum,
22+
Crc32Custom,
2223
Crc32Iscsi,
2324
Crc32IsoHdlc,
2425
Crc32Jamcrc,
2526
Crc32Mef,
2627
Crc32Mpeg2,
2728
Crc32Xfer,
29+
Crc64Custom,
2830
Crc64Ecma182,
2931
Crc64GoIso,
3032
Crc64Ms,
@@ -149,6 +151,25 @@ uint64_t crc_fast_checksum_combine(enum CrcFastAlgorithm algorithm,
149151
uint64_t checksum2,
150152
uint64_t checksum2_len);
151153

154+
/**
155+
* Combine two CRC checksums using custom parameters
156+
*/
157+
uint64_t crc_fast_checksum_combine_with_custom_params(struct CrcFastParams params,
158+
uint64_t checksum1,
159+
uint64_t checksum2,
160+
uint64_t checksum2_len);
161+
162+
/**
163+
* Returns the custom CRC parameters for a given set of Rocksoft CRC parameters
164+
*/
165+
struct CrcFastParams crc_fast_get_custom_params(const char *name_ptr,
166+
uint8_t width,
167+
uint64_t poly,
168+
uint64_t init,
169+
bool reflected,
170+
uint64_t xorout,
171+
uint64_t check);
172+
152173
/**
153174
* Gets the target build properties (CPU architecture and fine-tuning parameters) for this algorithm
154175
*/

src/ffi.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ pub enum CrcFastAlgorithm {
2727
Crc32Bzip2,
2828
Crc32CdRomEdc,
2929
Crc32Cksum,
30+
Crc32Custom,
3031
Crc32Iscsi,
3132
Crc32IsoHdlc,
3233
Crc32Jamcrc,
3334
Crc32Mef,
3435
Crc32Mpeg2,
3536
Crc32Xfer,
37+
Crc64Custom,
3638
Crc64Ecma182,
3739
Crc64GoIso,
3840
Crc64Ms,
@@ -52,12 +54,14 @@ impl From<CrcFastAlgorithm> for CrcAlgorithm {
5254
CrcFastAlgorithm::Crc32Bzip2 => CrcAlgorithm::Crc32Bzip2,
5355
CrcFastAlgorithm::Crc32CdRomEdc => CrcAlgorithm::Crc32CdRomEdc,
5456
CrcFastAlgorithm::Crc32Cksum => CrcAlgorithm::Crc32Cksum,
57+
CrcFastAlgorithm::Crc32Custom => CrcAlgorithm::Crc32Custom,
5558
CrcFastAlgorithm::Crc32Iscsi => CrcAlgorithm::Crc32Iscsi,
5659
CrcFastAlgorithm::Crc32IsoHdlc => CrcAlgorithm::Crc32IsoHdlc,
5760
CrcFastAlgorithm::Crc32Jamcrc => CrcAlgorithm::Crc32Jamcrc,
5861
CrcFastAlgorithm::Crc32Mef => CrcAlgorithm::Crc32Mef,
5962
CrcFastAlgorithm::Crc32Mpeg2 => CrcAlgorithm::Crc32Mpeg2,
6063
CrcFastAlgorithm::Crc32Xfer => CrcAlgorithm::Crc32Xfer,
64+
CrcFastAlgorithm::Crc64Custom => CrcAlgorithm::Crc64Custom,
6165
CrcFastAlgorithm::Crc64Ecma182 => CrcAlgorithm::Crc64Ecma182,
6266
CrcFastAlgorithm::Crc64GoIso => CrcAlgorithm::Crc64GoIso,
6367
CrcFastAlgorithm::Crc64Ms => CrcAlgorithm::Crc64Ms,
@@ -310,6 +314,66 @@ pub extern "C" fn crc_fast_checksum_combine(
310314
crate::checksum_combine(algorithm.into(), checksum1, checksum2, checksum2_len)
311315
}
312316

317+
/// Combine two CRC checksums using custom parameters
318+
#[no_mangle]
319+
pub extern "C" fn crc_fast_checksum_combine_with_custom_params(
320+
params: CrcFastParams,
321+
checksum1: u64,
322+
checksum2: u64,
323+
checksum2_len: u64,
324+
) -> u64 {
325+
crate::checksum_combine_with_custom_params(params.into(), checksum1, checksum2, checksum2_len)
326+
}
327+
328+
/// Returns the custom CRC parameters for a given set of Rocksoft CRC parameters
329+
#[no_mangle]
330+
pub extern "C" fn crc_fast_get_custom_params(
331+
name_ptr: *const c_char,
332+
width: u8,
333+
poly: u64,
334+
init: u64,
335+
reflected: bool,
336+
xorout: u64,
337+
check: u64,
338+
) -> CrcFastParams {
339+
let name = if name_ptr.is_null() {
340+
"custom"
341+
} else {
342+
unsafe {
343+
CStr::from_ptr(name_ptr).to_str().unwrap_or("custom")
344+
}
345+
};
346+
347+
// Get the custom params from the library
348+
let params = crate::get_custom_params(
349+
// We need to use a static string for the name field
350+
Box::leak(name.to_string().into_boxed_str()),
351+
width,
352+
poly,
353+
init,
354+
reflected,
355+
xorout,
356+
check,
357+
);
358+
359+
// Convert to FFI struct
360+
CrcFastParams {
361+
algorithm: match width {
362+
32 => CrcFastAlgorithm::Crc32Custom,
363+
64 => CrcFastAlgorithm::Crc64Custom,
364+
_ => panic!("Unsupported width: {}", width),
365+
},
366+
width: params.width,
367+
poly: params.poly,
368+
init: params.init,
369+
refin: params.refin,
370+
refout: params.refout,
371+
xorout: params.xorout,
372+
check: params.check,
373+
keys: params.keys,
374+
}
375+
}
376+
313377
/// Gets the target build properties (CPU architecture and fine-tuning parameters) for this algorithm
314378
#[no_mangle]
315379
pub extern "C" fn crc_fast_get_calculator_target(algorithm: CrcFastAlgorithm) -> *const c_char {

0 commit comments

Comments
 (0)