crc-fast 1.10.0

World's fastest generic CRC16, CRC32, and CRC64 calculator using SIMD. Supplies a C-compatible shared library for use in other languages.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.

//! FFI bindings for the Rust library
//!
//! This module provides a C-compatible interface for the Rust library, allowing
//! C programs to use the library's functionality.

#![cfg(all(
    feature = "ffi",
    any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86")
))]

use crate::CrcAlgorithm;
use crate::CrcParams;
use crate::{get_calculator_target, Digest};
use std::collections::HashMap;
use std::collections::HashSet;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;
use std::sync::{Mutex, OnceLock};

static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();

/// Error codes for FFI operations
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CrcFastError {
    /// Operation completed successfully
    Success = 0,
    /// Lock was poisoned (thread panicked while holding lock)
    LockPoisoned = 1,
    /// Null pointer was passed where non-null required
    NullPointer = 2,
    /// Invalid key count for CRC parameters
    InvalidKeyCount = 3,
    /// Unsupported CRC width (must be 32 or 64)
    UnsupportedWidth = 4,
    /// Invalid UTF-8 string
    InvalidUtf8 = 5,
    /// File I/O error
    IoError = 6,
    /// Internal string conversion error
    StringConversionError = 7,
}

impl CrcFastError {
    /// Returns a static string describing the error
    fn message(&self) -> &'static str {
        match self {
            CrcFastError::Success => "Operation completed successfully",
            CrcFastError::LockPoisoned => "Lock was poisoned (thread panicked while holding lock)",
            CrcFastError::NullPointer => "Null pointer was passed where non-null required",
            CrcFastError::InvalidKeyCount => "Invalid key count for CRC parameters",
            CrcFastError::UnsupportedWidth => "Unsupported CRC width (must be 32 or 64)",
            CrcFastError::InvalidUtf8 => "Invalid UTF-8 string",
            CrcFastError::IoError => "File I/O error",
            CrcFastError::StringConversionError => "Internal string conversion error",
        }
    }
}

// Thread-local storage for the last error that occurred
thread_local! {
    static LAST_ERROR: std::cell::Cell<CrcFastError> = const { std::cell::Cell::new(CrcFastError::Success) };
}

/// Sets the thread-local last error
fn set_last_error(error: CrcFastError) {
    LAST_ERROR.with(|e| e.set(error));
}

/// Clears the thread-local last error (sets it to Success)
fn clear_last_error() {
    LAST_ERROR.with(|e| e.set(CrcFastError::Success));
}

// Global storage for stable key pointers to ensure they remain valid across FFI boundary
static STABLE_KEY_STORAGE: OnceLock<Mutex<HashMap<u64, Box<[u64]>>>> = OnceLock::new();

/// Creates a stable pointer to the keys for FFI usage.
/// The keys are stored in global memory to ensure the pointer remains valid.
/// Returns (pointer, count) on success, or (null, 0) on error.
/// Sets CrcFastError::LockPoisoned on lock failure.
fn create_stable_key_pointer(keys: &crate::CrcKeysStorage) -> (*const u64, u32) {
    let storage = STABLE_KEY_STORAGE.get_or_init(|| Mutex::new(HashMap::new()));

    // Create a unique hash for this key set to avoid duplicates
    let key_hash = match keys {
        crate::CrcKeysStorage::KeysFold256(keys) => {
            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            use std::hash::{Hash, Hasher};
            keys.hash(&mut hasher);
            hasher.finish()
        }
        crate::CrcKeysStorage::KeysFutureTest(keys) => {
            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            use std::hash::{Hash, Hasher};
            keys.hash(&mut hasher);
            hasher.finish()
        }
    };

    let mut storage_map = match storage.lock() {
        Ok(guard) => guard,
        Err(_) => {
            set_last_error(CrcFastError::LockPoisoned);
            return (std::ptr::null(), 0);
        }
    };

    // Check if we already have this key set stored
    if let Some(stored_keys) = storage_map.get(&key_hash) {
        return (stored_keys.as_ptr(), stored_keys.len() as u32);
    }

    // Store the keys in stable memory
    let key_vec: Vec<u64> = match keys {
        crate::CrcKeysStorage::KeysFold256(keys) => keys.to_vec(),
        crate::CrcKeysStorage::KeysFutureTest(keys) => keys.to_vec(),
    };

    let boxed_keys = key_vec.into_boxed_slice();
    let count = boxed_keys.len() as u32;

    storage_map.insert(key_hash, boxed_keys);

    let ptr = storage_map.get(&key_hash).expect("just inserted").as_ptr();

    (ptr, count)
}

/// A handle to the Digest object
#[repr(C)]
pub struct CrcFastDigestHandle(*mut Digest);

/// The supported CRC algorithms
#[repr(C)]
#[derive(Clone, Copy)]
pub enum CrcFastAlgorithm {
    // CrcCustom works with any supported widths (16, 32, 64)
    CrcCustom,
    Crc16Arc,
    Crc16Cdma2000,
    Crc16Cms,
    Crc16Dds110,
    Crc16DectR,
    Crc16DectX,
    Crc16Dnp,
    Crc16En13757,
    Crc16Genibus,
    Crc16Gsm,
    Crc16Ibm3740,
    Crc16IbmSdlc,
    Crc16IsoIec144433A,
    Crc16Kermit,
    Crc16Lj1200,
    Crc16M17,
    Crc16MaximDow,
    Crc16Mcrf4xx,
    Crc16Modbus,
    Crc16Nrsc5,
    Crc16OpensafetyA,
    Crc16OpensafetyB,
    Crc16Profibus,
    Crc16Riello,
    Crc16SpiFujitsu,
    Crc16T10Dif,
    Crc16Teledisk,
    Crc16Tms37157,
    Crc16Umts,
    Crc16Usb,
    Crc16Xmodem,
    Crc32Aixm,
    Crc32Autosar,
    Crc32Base91D,
    Crc32Bzip2,
    Crc32CdRomEdc,
    Crc32Cksum,
    Crc32Custom,
    Crc32Iscsi,
    Crc32IsoHdlc,
    Crc32Jamcrc,
    Crc32Mef,
    Crc32Mpeg2,
    Crc32Xfer,
    Crc64Custom,
    Crc64Ecma182,
    Crc64GoIso,
    Crc64Ms,
    Crc64Nvme,
    Crc64Redis,
    Crc64We,
    Crc64Xz,
}

// Convert from FFI enum to internal enum
#[allow(deprecated)]
impl From<CrcFastAlgorithm> for CrcAlgorithm {
    fn from(value: CrcFastAlgorithm) -> Self {
        match value {
            CrcFastAlgorithm::Crc16Arc => CrcAlgorithm::Crc16Arc,
            CrcFastAlgorithm::Crc16Cdma2000 => CrcAlgorithm::Crc16Cdma2000,
            CrcFastAlgorithm::Crc16Cms => CrcAlgorithm::Crc16Cms,
            CrcFastAlgorithm::Crc16Dds110 => CrcAlgorithm::Crc16Dds110,
            CrcFastAlgorithm::Crc16DectR => CrcAlgorithm::Crc16DectR,
            CrcFastAlgorithm::Crc16DectX => CrcAlgorithm::Crc16DectX,
            CrcFastAlgorithm::Crc16Dnp => CrcAlgorithm::Crc16Dnp,
            CrcFastAlgorithm::Crc16En13757 => CrcAlgorithm::Crc16En13757,
            CrcFastAlgorithm::Crc16Genibus => CrcAlgorithm::Crc16Genibus,
            CrcFastAlgorithm::Crc16Gsm => CrcAlgorithm::Crc16Gsm,
            CrcFastAlgorithm::Crc16Ibm3740 => CrcAlgorithm::Crc16Ibm3740,
            CrcFastAlgorithm::Crc16IbmSdlc => CrcAlgorithm::Crc16IbmSdlc,
            CrcFastAlgorithm::Crc16IsoIec144433A => CrcAlgorithm::Crc16IsoIec144433A,
            CrcFastAlgorithm::Crc16Kermit => CrcAlgorithm::Crc16Kermit,
            CrcFastAlgorithm::Crc16Lj1200 => CrcAlgorithm::Crc16Lj1200,
            CrcFastAlgorithm::Crc16M17 => CrcAlgorithm::Crc16M17,
            CrcFastAlgorithm::Crc16MaximDow => CrcAlgorithm::Crc16MaximDow,
            CrcFastAlgorithm::Crc16Mcrf4xx => CrcAlgorithm::Crc16Mcrf4xx,
            CrcFastAlgorithm::Crc16Modbus => CrcAlgorithm::Crc16Modbus,
            CrcFastAlgorithm::Crc16Nrsc5 => CrcAlgorithm::Crc16Nrsc5,
            CrcFastAlgorithm::Crc16OpensafetyA => CrcAlgorithm::Crc16OpensafetyA,
            CrcFastAlgorithm::Crc16OpensafetyB => CrcAlgorithm::Crc16OpensafetyB,
            CrcFastAlgorithm::Crc16Profibus => CrcAlgorithm::Crc16Profibus,
            CrcFastAlgorithm::Crc16Riello => CrcAlgorithm::Crc16Riello,
            CrcFastAlgorithm::Crc16SpiFujitsu => CrcAlgorithm::Crc16SpiFujitsu,
            CrcFastAlgorithm::Crc16T10Dif => CrcAlgorithm::Crc16T10Dif,
            CrcFastAlgorithm::Crc16Teledisk => CrcAlgorithm::Crc16Teledisk,
            CrcFastAlgorithm::Crc16Tms37157 => CrcAlgorithm::Crc16Tms37157,
            CrcFastAlgorithm::Crc16Umts => CrcAlgorithm::Crc16Umts,
            CrcFastAlgorithm::Crc16Usb => CrcAlgorithm::Crc16Usb,
            CrcFastAlgorithm::Crc16Xmodem => CrcAlgorithm::Crc16Xmodem,
            CrcFastAlgorithm::Crc32Aixm => CrcAlgorithm::Crc32Aixm,
            CrcFastAlgorithm::Crc32Autosar => CrcAlgorithm::Crc32Autosar,
            CrcFastAlgorithm::Crc32Base91D => CrcAlgorithm::Crc32Base91D,
            CrcFastAlgorithm::Crc32Bzip2 => CrcAlgorithm::Crc32Bzip2,
            CrcFastAlgorithm::Crc32CdRomEdc => CrcAlgorithm::Crc32CdRomEdc,
            CrcFastAlgorithm::Crc32Cksum => CrcAlgorithm::Crc32Cksum,
            CrcFastAlgorithm::Crc32Custom => CrcAlgorithm::Crc32Custom,
            CrcFastAlgorithm::Crc32Iscsi => CrcAlgorithm::Crc32Iscsi,
            CrcFastAlgorithm::Crc32IsoHdlc => CrcAlgorithm::Crc32IsoHdlc,
            CrcFastAlgorithm::Crc32Jamcrc => CrcAlgorithm::Crc32Jamcrc,
            CrcFastAlgorithm::Crc32Mef => CrcAlgorithm::Crc32Mef,
            CrcFastAlgorithm::Crc32Mpeg2 => CrcAlgorithm::Crc32Mpeg2,
            CrcFastAlgorithm::Crc32Xfer => CrcAlgorithm::Crc32Xfer,
            CrcFastAlgorithm::CrcCustom => CrcAlgorithm::CrcCustom,
            CrcFastAlgorithm::Crc64Custom => CrcAlgorithm::Crc64Custom,
            CrcFastAlgorithm::Crc64Ecma182 => CrcAlgorithm::Crc64Ecma182,
            CrcFastAlgorithm::Crc64GoIso => CrcAlgorithm::Crc64GoIso,
            CrcFastAlgorithm::Crc64Ms => CrcAlgorithm::Crc64Ms,
            CrcFastAlgorithm::Crc64Nvme => CrcAlgorithm::Crc64Nvme,
            CrcFastAlgorithm::Crc64Redis => CrcAlgorithm::Crc64Redis,
            CrcFastAlgorithm::Crc64We => CrcAlgorithm::Crc64We,
            CrcFastAlgorithm::Crc64Xz => CrcAlgorithm::Crc64Xz,
        }
    }
}

/// Gets the last error that occurred in the current thread
/// Returns CrcFastError::Success if no error has occurred
#[no_mangle]
pub extern "C" fn crc_fast_get_last_error() -> CrcFastError {
    LAST_ERROR.with(|e| e.get())
}

/// Clears the last error for the current thread
#[no_mangle]
pub extern "C" fn crc_fast_clear_error() {
    clear_last_error();
}

/// Gets a human-readable error message for the given error code
/// Returns a pointer to a static string (do not free)
#[no_mangle]
pub extern "C" fn crc_fast_error_message(error: CrcFastError) -> *const c_char {
    let message = error.message();
    // These are static strings, so we can safely return them as C strings
    // The strings are guaranteed to be valid UTF-8 and null-terminated
    match std::ffi::CString::new(message) {
        Ok(c_str) => {
            // Leak the string so it remains valid for the lifetime of the program
            // This is safe because error messages are static and small
            Box::leak(Box::new(c_str)).as_ptr()
        }
        Err(_) => std::ptr::null(),
    }
}

/// Custom CRC parameters
#[repr(C)]
pub struct CrcFastParams {
    pub algorithm: CrcFastAlgorithm,
    pub width: u8,
    pub poly: u64,
    pub init: u64,
    pub refin: bool,
    pub refout: bool,
    pub xorout: u64,
    pub check: u64,
    pub key_count: u32,
    pub keys: *const u64,
}

/// Fallible conversion from FFI struct to internal struct
/// Returns None if the parameters are invalid (unsupported key count)
fn try_params_from_ffi(value: &CrcFastParams) -> Option<CrcParams> {
    // Validate key pointer
    if value.keys.is_null() {
        return None;
    }

    // Convert C array back to appropriate CrcKeysStorage
    let keys = unsafe { std::slice::from_raw_parts(value.keys, value.key_count as usize) };

    let storage = match value.key_count {
        23 => match keys.try_into() {
            Ok(arr) => crate::CrcKeysStorage::from_keys_fold_256(arr),
            Err(_) => return None,
        },
        25 => match keys.try_into() {
            Ok(arr) => crate::CrcKeysStorage::from_keys_fold_future_test(arr),
            Err(_) => return None,
        },
        _ => return None, // Unsupported key count
    };

    // For reflected CRC-16, bit-reverse the init value for the SIMD algorithm
    let init_algorithm = if value.width == 16 && value.refin {
        (value.init as u16).reverse_bits() as u64
    } else {
        value.init
    };

    Some(CrcParams {
        algorithm: value.algorithm.into(),
        name: "custom", // C interface doesn't need the name field
        width: value.width,
        poly: value.poly,
        init: value.init,
        init_algorithm,
        refin: value.refin,
        refout: value.refout,
        xorout: value.xorout,
        check: value.check,
        keys: storage,
    })
}

// Convert from FFI struct to internal struct (legacy, may panic)
// For backwards compatibility, but prefer try_params_from_ffi
impl From<CrcFastParams> for CrcParams {
    fn from(value: CrcFastParams) -> Self {
        try_params_from_ffi(&value)
            .expect("Invalid CRC parameters: unsupported key count or null pointer")
    }
}

// Convert from internal struct to FFI struct
#[allow(deprecated)]
impl From<CrcParams> for CrcFastParams {
    fn from(params: CrcParams) -> Self {
        // Create stable key pointer for FFI usage
        let (keys_ptr, key_count) = create_stable_key_pointer(&params.keys);

        CrcFastParams {
            algorithm: match params.algorithm {
                CrcAlgorithm::Crc16Arc => CrcFastAlgorithm::Crc16Arc,
                CrcAlgorithm::Crc16Cdma2000 => CrcFastAlgorithm::Crc16Cdma2000,
                CrcAlgorithm::Crc16Cms => CrcFastAlgorithm::Crc16Cms,
                CrcAlgorithm::Crc16Dds110 => CrcFastAlgorithm::Crc16Dds110,
                CrcAlgorithm::Crc16DectR => CrcFastAlgorithm::Crc16DectR,
                CrcAlgorithm::Crc16DectX => CrcFastAlgorithm::Crc16DectX,
                CrcAlgorithm::Crc16Dnp => CrcFastAlgorithm::Crc16Dnp,
                CrcAlgorithm::Crc16En13757 => CrcFastAlgorithm::Crc16En13757,
                CrcAlgorithm::Crc16Genibus => CrcFastAlgorithm::Crc16Genibus,
                CrcAlgorithm::Crc16Gsm => CrcFastAlgorithm::Crc16Gsm,
                CrcAlgorithm::Crc16Ibm3740 => CrcFastAlgorithm::Crc16Ibm3740,
                CrcAlgorithm::Crc16IbmSdlc => CrcFastAlgorithm::Crc16IbmSdlc,
                CrcAlgorithm::Crc16IsoIec144433A => CrcFastAlgorithm::Crc16IsoIec144433A,
                CrcAlgorithm::Crc16Kermit => CrcFastAlgorithm::Crc16Kermit,
                CrcAlgorithm::Crc16Lj1200 => CrcFastAlgorithm::Crc16Lj1200,
                CrcAlgorithm::Crc16M17 => CrcFastAlgorithm::Crc16M17,
                CrcAlgorithm::Crc16MaximDow => CrcFastAlgorithm::Crc16MaximDow,
                CrcAlgorithm::Crc16Mcrf4xx => CrcFastAlgorithm::Crc16Mcrf4xx,
                CrcAlgorithm::Crc16Modbus => CrcFastAlgorithm::Crc16Modbus,
                CrcAlgorithm::Crc16Nrsc5 => CrcFastAlgorithm::Crc16Nrsc5,
                CrcAlgorithm::Crc16OpensafetyA => CrcFastAlgorithm::Crc16OpensafetyA,
                CrcAlgorithm::Crc16OpensafetyB => CrcFastAlgorithm::Crc16OpensafetyB,
                CrcAlgorithm::Crc16Profibus => CrcFastAlgorithm::Crc16Profibus,
                CrcAlgorithm::Crc16Riello => CrcFastAlgorithm::Crc16Riello,
                CrcAlgorithm::Crc16SpiFujitsu => CrcFastAlgorithm::Crc16SpiFujitsu,
                CrcAlgorithm::Crc16T10Dif => CrcFastAlgorithm::Crc16T10Dif,
                CrcAlgorithm::Crc16Teledisk => CrcFastAlgorithm::Crc16Teledisk,
                CrcAlgorithm::Crc16Tms37157 => CrcFastAlgorithm::Crc16Tms37157,
                CrcAlgorithm::Crc16Umts => CrcFastAlgorithm::Crc16Umts,
                CrcAlgorithm::Crc16Usb => CrcFastAlgorithm::Crc16Usb,
                CrcAlgorithm::Crc16Xmodem => CrcFastAlgorithm::Crc16Xmodem,
                CrcAlgorithm::Crc32Aixm => CrcFastAlgorithm::Crc32Aixm,
                CrcAlgorithm::Crc32Autosar => CrcFastAlgorithm::Crc32Autosar,
                CrcAlgorithm::Crc32Base91D => CrcFastAlgorithm::Crc32Base91D,
                CrcAlgorithm::Crc32Bzip2 => CrcFastAlgorithm::Crc32Bzip2,
                CrcAlgorithm::Crc32CdRomEdc => CrcFastAlgorithm::Crc32CdRomEdc,
                CrcAlgorithm::Crc32Cksum => CrcFastAlgorithm::Crc32Cksum,
                CrcAlgorithm::Crc32Custom => CrcFastAlgorithm::Crc32Custom,
                CrcAlgorithm::Crc32Iscsi => CrcFastAlgorithm::Crc32Iscsi,
                CrcAlgorithm::Crc32IsoHdlc => CrcFastAlgorithm::Crc32IsoHdlc,
                CrcAlgorithm::Crc32Jamcrc => CrcFastAlgorithm::Crc32Jamcrc,
                CrcAlgorithm::Crc32Mef => CrcFastAlgorithm::Crc32Mef,
                CrcAlgorithm::Crc32Mpeg2 => CrcFastAlgorithm::Crc32Mpeg2,
                CrcAlgorithm::Crc32Xfer => CrcFastAlgorithm::Crc32Xfer,
                CrcAlgorithm::CrcCustom => CrcFastAlgorithm::CrcCustom,
                CrcAlgorithm::Crc64Custom => CrcFastAlgorithm::Crc64Custom,
                CrcAlgorithm::Crc64Ecma182 => CrcFastAlgorithm::Crc64Ecma182,
                CrcAlgorithm::Crc64GoIso => CrcFastAlgorithm::Crc64GoIso,
                CrcAlgorithm::Crc64Ms => CrcFastAlgorithm::Crc64Ms,
                CrcAlgorithm::Crc64Nvme => CrcFastAlgorithm::Crc64Nvme,
                CrcAlgorithm::Crc64Redis => CrcFastAlgorithm::Crc64Redis,
                CrcAlgorithm::Crc64We => CrcFastAlgorithm::Crc64We,
                CrcAlgorithm::Crc64Xz => CrcFastAlgorithm::Crc64Xz,
            },
            width: params.width,
            poly: params.poly,
            init: params.init,
            refin: params.refin,
            refout: params.refout,
            xorout: params.xorout,
            check: params.check,
            key_count,
            keys: keys_ptr,
        }
    }
}

/// Creates a new Digest to compute CRC checksums using algorithm
#[no_mangle]
pub extern "C" fn crc_fast_digest_new(algorithm: CrcFastAlgorithm) -> *mut CrcFastDigestHandle {
    clear_last_error();
    let digest = Box::new(Digest::new(algorithm.into()));
    let handle = Box::new(CrcFastDigestHandle(Box::into_raw(digest)));
    Box::into_raw(handle)
}

/// Creates a new Digest with a custom initial state
#[no_mangle]
pub extern "C" fn crc_fast_digest_new_with_init_state(
    algorithm: CrcFastAlgorithm,
    init_state: u64,
) -> *mut CrcFastDigestHandle {
    clear_last_error();
    let digest = Box::new(Digest::new_with_init_state(algorithm.into(), init_state));
    let handle = Box::new(CrcFastDigestHandle(Box::into_raw(digest)));
    Box::into_raw(handle)
}

/// Creates a new Digest to compute CRC checksums using custom parameters
/// Returns NULL if parameters are invalid (invalid key count or null pointer)
/// Call crc_fast_get_last_error() to get the specific error code
#[no_mangle]
pub extern "C" fn crc_fast_digest_new_with_params(
    params: CrcFastParams,
) -> *mut CrcFastDigestHandle {
    clear_last_error();
    match try_params_from_ffi(&params) {
        Some(crc_params) => {
            let digest = Box::new(Digest::new_with_params(crc_params));
            let handle = Box::new(CrcFastDigestHandle(Box::into_raw(digest)));
            Box::into_raw(handle)
        }
        None => {
            // Set appropriate error based on the failure
            if params.keys.is_null() {
                set_last_error(CrcFastError::NullPointer);
            } else {
                set_last_error(CrcFastError::InvalidKeyCount);
            }
            std::ptr::null_mut()
        }
    }
}

/// Updates the Digest with data
#[no_mangle]
pub extern "C" fn crc_fast_digest_update(
    handle: *mut CrcFastDigestHandle,
    data: *const c_char,
    len: usize,
) {
    if handle.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return;
    }
    if data.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return;
    }

    clear_last_error();
    unsafe {
        let digest = &mut *(*handle).0;

        #[allow(clippy::unnecessary_cast)]
        let bytes = slice::from_raw_parts(data as *const u8, len);
        digest.update(bytes);
    }
}

/// Calculates the CRC checksum for data that's been written to the Digest
/// Returns 0 on error (e.g. null handle)
#[no_mangle]
pub extern "C" fn crc_fast_digest_finalize(handle: *mut CrcFastDigestHandle) -> u64 {
    if handle.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }

    clear_last_error();
    unsafe {
        let digest = &*(*handle).0;
        digest.finalize()
    }
}

/// Free the Digest resources without finalizing
#[no_mangle]
pub extern "C" fn crc_fast_digest_free(handle: *mut CrcFastDigestHandle) {
    if handle.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return;
    }

    clear_last_error();
    unsafe {
        let handle = Box::from_raw(handle);
        let _ = Box::from_raw(handle.0); // This drops the digest
    }
}

/// Reset the Digest state
#[no_mangle]
pub extern "C" fn crc_fast_digest_reset(handle: *mut CrcFastDigestHandle) {
    if handle.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return;
    }

    clear_last_error();
    unsafe {
        let digest = &mut *(*handle).0;

        digest.reset();
    }
}

/// Finalize and reset the Digest in one operation
/// Returns 0 on error (e.g. null handle)
#[no_mangle]
pub extern "C" fn crc_fast_digest_finalize_reset(handle: *mut CrcFastDigestHandle) -> u64 {
    if handle.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }

    clear_last_error();
    unsafe {
        let digest = &mut *(*handle).0;

        digest.finalize_reset()
    }
}

/// Combine two Digest checksums
#[no_mangle]
pub extern "C" fn crc_fast_digest_combine(
    handle1: *mut CrcFastDigestHandle,
    handle2: *mut CrcFastDigestHandle,
) {
    if handle1.is_null() || handle2.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return;
    }

    clear_last_error();
    unsafe {
        let digest1 = &mut *(*handle1).0;
        let digest2 = &*(*handle2).0;
        digest1.combine(digest2);
    }
}

/// Gets the amount of data processed by the Digest so far
/// Returns 0 on error (e.g. null handle)
#[no_mangle]
pub extern "C" fn crc_fast_digest_get_amount(handle: *mut CrcFastDigestHandle) -> u64 {
    if handle.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }

    clear_last_error();
    unsafe {
        let digest = &*(*handle).0;
        digest.get_amount()
    }
}

/// Gets the current state of the Digest
/// Returns 0 on error (e.g. null handle)
#[no_mangle]
pub extern "C" fn crc_fast_digest_get_state(handle: *mut CrcFastDigestHandle) -> u64 {
    if handle.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }
    clear_last_error();
    unsafe {
        let digest = &*(*handle).0;
        digest.get_state()
    }
}

/// Helper method to calculate a CRC checksum directly for a string using algorithm
/// Returns 0 on error (e.g. null data pointer)
#[no_mangle]
pub extern "C" fn crc_fast_checksum(
    algorithm: CrcFastAlgorithm,
    data: *const c_char,
    len: usize,
) -> u64 {
    if data.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }
    clear_last_error();
    unsafe {
        #[allow(clippy::unnecessary_cast)]
        let bytes = slice::from_raw_parts(data as *const u8, len);
        crate::checksum(algorithm.into(), bytes)
    }
}

/// Helper method to calculate a CRC checksum directly for data using custom parameters
/// Returns 0 if parameters are invalid or data is null
/// Call crc_fast_get_last_error() to get the specific error code
#[no_mangle]
pub extern "C" fn crc_fast_checksum_with_params(
    params: CrcFastParams,
    data: *const c_char,
    len: usize,
) -> u64 {
    if data.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }
    match try_params_from_ffi(&params) {
        Some(crc_params) => {
            clear_last_error();
            unsafe {
                #[allow(clippy::unnecessary_cast)]
                let bytes = slice::from_raw_parts(data as *const u8, len);
                crate::checksum_with_params(crc_params, bytes)
            }
        }
        None => {
            if params.keys.is_null() {
                set_last_error(CrcFastError::NullPointer);
            } else {
                set_last_error(CrcFastError::InvalidKeyCount);
            }
            0
        }
    }
}

/// Helper method to just calculate a CRC checksum directly for a file using algorithm
/// Returns 0 if path is null or file I/O fails
/// Call crc_fast_get_last_error() to get the specific error code
#[no_mangle]
pub extern "C" fn crc_fast_checksum_file(
    algorithm: CrcFastAlgorithm,
    path_ptr: *const u8,
    path_len: usize,
) -> u64 {
    if path_ptr.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }

    unsafe {
        match crate::checksum_file(
            algorithm.into(),
            &convert_to_string(path_ptr, path_len),
            None,
        ) {
            Ok(result) => {
                clear_last_error();
                result
            }
            Err(_) => {
                set_last_error(CrcFastError::IoError);
                0
            }
        }
    }
}

/// Helper method to calculate a CRC checksum directly for a file using custom parameters
/// Returns 0 if parameters are invalid, path is null, or file I/O fails
/// Call crc_fast_get_last_error() to get the specific error code
#[no_mangle]
pub extern "C" fn crc_fast_checksum_file_with_params(
    params: CrcFastParams,
    path_ptr: *const u8,
    path_len: usize,
) -> u64 {
    if path_ptr.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }

    match try_params_from_ffi(&params) {
        Some(crc_params) => unsafe {
            match crate::checksum_file_with_params(
                crc_params,
                &convert_to_string(path_ptr, path_len),
                None,
            ) {
                Ok(result) => {
                    clear_last_error();
                    result
                }
                Err(_) => {
                    set_last_error(CrcFastError::IoError);
                    0
                }
            }
        },
        None => {
            if params.keys.is_null() {
                set_last_error(CrcFastError::NullPointer);
            } else {
                set_last_error(CrcFastError::InvalidKeyCount);
            }
            0
        }
    }
}

/// Combine two CRC checksums using algorithm
#[no_mangle]
pub extern "C" fn crc_fast_checksum_combine(
    algorithm: CrcFastAlgorithm,
    checksum1: u64,
    checksum2: u64,
    checksum2_len: u64,
) -> u64 {
    clear_last_error();
    crate::checksum_combine(algorithm.into(), checksum1, checksum2, checksum2_len)
}

/// Combine two CRC checksums using custom parameters
/// Returns 0 if parameters are invalid
/// Call crc_fast_get_last_error() to get the specific error code
#[no_mangle]
pub extern "C" fn crc_fast_checksum_combine_with_params(
    params: CrcFastParams,
    checksum1: u64,
    checksum2: u64,
    checksum2_len: u64,
) -> u64 {
    match try_params_from_ffi(&params) {
        Some(crc_params) => {
            clear_last_error();
            crate::checksum_combine_with_params(crc_params, checksum1, checksum2, checksum2_len)
        }
        None => {
            if params.keys.is_null() {
                set_last_error(CrcFastError::NullPointer);
            } else {
                set_last_error(CrcFastError::InvalidKeyCount);
            }
            0
        }
    }
}

/// Returns the custom CRC parameters for a given set of Rocksoft CRC parameters
/// If width is not 32 or 64, sets error to UnsupportedWidth
#[no_mangle]
pub extern "C" fn crc_fast_get_custom_params(
    name_ptr: *const c_char,
    width: u8,
    poly: u64,
    init: u64,
    reflected: bool,
    xorout: u64,
    check: u64,
) -> CrcFastParams {
    // Validate width
    if width != 32 && width != 64 {
        set_last_error(CrcFastError::UnsupportedWidth);
    } else {
        clear_last_error();
    }

    let name = if name_ptr.is_null() {
        "custom"
    } else {
        unsafe {
            match CStr::from_ptr(name_ptr).to_str() {
                Ok(s) => s,
                Err(_) => {
                    set_last_error(CrcFastError::InvalidUtf8);
                    "custom"
                }
            }
        }
    };

    // Get the custom params from the library
    let params = CrcParams::new(
        get_or_leak_string(name), // ✅ Use cached leak
        width,
        poly,
        init,
        reflected,
        xorout,
        check,
    );

    // Create stable key pointer for FFI usage
    let (keys_ptr, key_count) = create_stable_key_pointer(&params.keys);

    // Convert to FFI struct - use CrcCustom for all widths since CrcParams::new now uses it
    CrcFastParams {
        algorithm: CrcFastAlgorithm::CrcCustom,
        width: params.width,
        poly: params.poly,
        init: params.init,
        refin: params.refin,
        refout: params.refout,
        xorout: params.xorout,
        check: params.check,
        key_count,
        keys: keys_ptr,
    }
}

/// Gets the target build properties (CPU architecture and fine-tuning parameters) for this algorithm
/// Returns NULL if string conversion fails
/// Call crc_fast_get_last_error() to get the specific error code
#[no_mangle]
pub extern "C" fn crc_fast_get_calculator_target(algorithm: CrcFastAlgorithm) -> *const c_char {
    let target = get_calculator_target(algorithm.into());

    match std::ffi::CString::new(target) {
        Ok(s) => {
            clear_last_error();
            s.into_raw()
        }
        Err(_) => {
            set_last_error(CrcFastError::StringConversionError);
            std::ptr::null_mut()
        }
    }
}

/// Gets the version of this library
/// Returns a pointer to "unknown" if version string is invalid
#[no_mangle]
pub extern "C" fn crc_fast_get_version() -> *const c_char {
    const VERSION: &CStr =
        match CStr::from_bytes_with_nul(concat!(env!("CARGO_PKG_VERSION"), "\0").as_bytes()) {
            Ok(version) => version,
            // Fallback to "unknown" if version string is malformed
            Err(_) => c"unknown",
        };

    VERSION.as_ptr()
}

/// Calculates the CRC-32/ISCSI checksum (commonly called "crc32c" in many, but not all,
/// implementations).
///
/// https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-32-iscsi
///
/// Returns 0 on error (e.g. null data pointer)
#[no_mangle]
pub extern "C" fn crc_fast_crc32_iscsi(data: *const c_char, len: usize) -> u32 {
    if data.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }
    clear_last_error();
    unsafe {
        #[allow(clippy::unnecessary_cast)]
        let bytes = slice::from_raw_parts(data as *const u8, len);
        crate::crc32_iscsi(bytes)
    }
}

/// Calculates the CRC-32/ISO-HDLC checksum (commonly called "crc32" in many, but not all,
/// implementations).
///
/// https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-32-iso-hdlc
///
/// Returns 0 on error (e.g. null data pointer)
#[no_mangle]
pub extern "C" fn crc_fast_crc32_iso_hdlc(data: *const c_char, len: usize) -> u32 {
    if data.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }
    clear_last_error();
    unsafe {
        #[allow(clippy::unnecessary_cast)]
        let bytes = slice::from_raw_parts(data as *const u8, len);
        crate::crc32_iso_hdlc(bytes)
    }
}

/// Calculates the CRC-64/NVME checksum.
///
/// https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme
///
/// Returns 0 on error (e.g. null data pointer)
#[no_mangle]
pub extern "C" fn crc_fast_crc64_nvme(data: *const c_char, len: usize) -> u64 {
    if data.is_null() {
        set_last_error(CrcFastError::NullPointer);
        return 0;
    }
    clear_last_error();
    unsafe {
        #[allow(clippy::unnecessary_cast)]
        let bytes = slice::from_raw_parts(data as *const u8, len);
        crate::crc64_nvme(bytes)
    }
}

unsafe fn convert_to_string(data: *const u8, len: usize) -> String {
    if data.is_null() {
        return String::new();
    }

    // Safely construct string slice from raw parts
    match std::str::from_utf8(slice::from_raw_parts(data, len)) {
        Ok(s) => s.to_string(),
        Err(_) => String::new(), // Return empty string for invalid UTF-8
    }
}

fn get_or_leak_string(s: &str) -> &'static str {
    let cache = STRING_CACHE.get_or_init(|| Mutex::new(HashSet::new()));
    let mut cache = cache.lock().unwrap();

    // Check if we already have this string
    if let Some(&cached) = cache.get(s) {
        return cached;
    }

    // Leak it and cache the result
    let leaked: &'static str = Box::leak(s.to_string().into_boxed_str());
    cache.insert(leaked);
    leaked
}