@@ -12,6 +12,7 @@ use crate::{get_calculator_target, Digest};
12
12
use std:: ffi:: CStr ;
13
13
use std:: os:: raw:: c_char;
14
14
use std:: slice;
15
+ use crate :: structs:: CrcParams ;
15
16
16
17
/// A handle to the Digest object
17
18
#[ repr( C ) ]
@@ -68,6 +69,38 @@ impl From<CrcFastAlgorithm> for CrcAlgorithm {
68
69
}
69
70
}
70
71
72
+ /// Custom CRC parameters
73
+ #[ repr( C ) ]
74
+ pub struct CrcFastParams {
75
+ pub algorithm : CrcFastAlgorithm ,
76
+ pub width : u8 ,
77
+ pub poly : u64 ,
78
+ pub init : u64 ,
79
+ pub refin : bool ,
80
+ pub refout : bool ,
81
+ pub xorout : u64 ,
82
+ pub check : u64 ,
83
+ pub keys : [ u64 ; 23 ] ,
84
+ }
85
+
86
+ // Convert from FFI struct to internal struct
87
+ impl From < CrcFastParams > for CrcParams {
88
+ fn from ( value : CrcFastParams ) -> Self {
89
+ CrcParams {
90
+ algorithm : value. algorithm . into ( ) ,
91
+ name : "custom" , // C interface doesn't need the name field
92
+ width : value. width ,
93
+ poly : value. poly ,
94
+ init : value. init ,
95
+ refin : value. refin ,
96
+ refout : value. refout ,
97
+ xorout : value. xorout ,
98
+ check : value. check ,
99
+ keys : value. keys ,
100
+ }
101
+ }
102
+ }
103
+
71
104
/// Creates a new Digest to compute CRC checksums using algorithm
72
105
#[ no_mangle]
73
106
pub extern "C" fn crc_fast_digest_new ( algorithm : CrcFastAlgorithm ) -> * mut CrcFastDigestHandle {
@@ -76,6 +109,14 @@ pub extern "C" fn crc_fast_digest_new(algorithm: CrcFastAlgorithm) -> *mut CrcFa
76
109
Box :: into_raw ( handle)
77
110
}
78
111
112
+ /// Creates a new Digest to compute CRC checksums using custom parameters
113
+ #[ no_mangle]
114
+ pub extern "C" fn crc_fast_digest_new_with_params ( params : CrcFastParams ) -> * mut CrcFastDigestHandle {
115
+ let digest = Box :: new ( Digest :: new_with_params ( params. into ( ) ) ) ;
116
+ let handle = Box :: new ( CrcFastDigestHandle ( Box :: into_raw ( digest) ) ) ;
117
+ Box :: into_raw ( handle)
118
+ }
119
+
79
120
/// Updates the Digest with data
80
121
#[ no_mangle]
81
122
pub extern "C" fn crc_fast_digest_update (
@@ -197,6 +238,23 @@ pub extern "C" fn crc_fast_checksum(
197
238
}
198
239
}
199
240
241
+ /// Helper method to calculate a CRC checksum directly for data using custom parameters
242
+ #[ no_mangle]
243
+ pub extern "C" fn crc_fast_checksum_with_params (
244
+ params : CrcFastParams ,
245
+ data : * const c_char ,
246
+ len : usize ,
247
+ ) -> u64 {
248
+ if data. is_null ( ) {
249
+ return 0 ;
250
+ }
251
+ unsafe {
252
+ #[ allow( clippy:: unnecessary_cast) ]
253
+ let bytes = slice:: from_raw_parts ( data as * const u8 , len) ;
254
+ crate :: checksum_with_params ( params. into ( ) , bytes)
255
+ }
256
+ }
257
+
200
258
/// Helper method to just calculate a CRC checksum directly for a file using algorithm
201
259
#[ no_mangle]
202
260
pub extern "C" fn crc_fast_checksum_file (
@@ -218,6 +276,27 @@ pub extern "C" fn crc_fast_checksum_file(
218
276
}
219
277
}
220
278
279
+ /// Helper method to calculate a CRC checksum directly for a file using custom parameters
280
+ #[ no_mangle]
281
+ pub extern "C" fn crc_fast_checksum_file_with_params (
282
+ params : CrcFastParams ,
283
+ path_ptr : * const u8 ,
284
+ path_len : usize ,
285
+ ) -> u64 {
286
+ if path_ptr. is_null ( ) {
287
+ return 0 ;
288
+ }
289
+
290
+ unsafe {
291
+ crate :: checksum_file_with_params (
292
+ params. into ( ) ,
293
+ & convert_to_string ( path_ptr, path_len) ,
294
+ None ,
295
+ )
296
+ . unwrap_or ( 0 ) // Return 0 on error instead of panicking
297
+ }
298
+ }
299
+
221
300
/// Combine two CRC checksums using algorithm
222
301
#[ no_mangle]
223
302
pub extern "C" fn crc_fast_checksum_combine (
0 commit comments