@@ -507,42 +507,6 @@ pub fn checksum_combine_with_custom_params(
507
507
combine:: checksums ( checksum1, checksum2, checksum2_len, params)
508
508
}
509
509
510
- /// Returns the custom CRC parameters for a given set of Rocksoft CRC parameters.
511
- ///
512
- /// Does not support mis-matched refin/refout parameters, so both must be true or both false.
513
- ///
514
- /// Rocksoft parameters for lots of variants: https://reveng.sourceforge.io/crc-catalogue/all.htm
515
- pub fn get_custom_params (
516
- name : & ' static str ,
517
- width : u8 ,
518
- poly : u64 ,
519
- init : u64 ,
520
- reflected : bool ,
521
- xorout : u64 ,
522
- check : u64 ,
523
- ) -> CrcParams {
524
- let keys = generate:: keys ( width, poly, reflected) ;
525
-
526
- let algorithm = match width {
527
- 32 => CrcAlgorithm :: Crc32Custom ,
528
- 64 => CrcAlgorithm :: Crc64Custom ,
529
- _ => panic ! ( "Unsupported width: {}" , width) ,
530
- } ;
531
-
532
- CrcParams {
533
- algorithm,
534
- name,
535
- width,
536
- poly,
537
- init,
538
- refin : reflected,
539
- refout : reflected,
540
- xorout,
541
- check,
542
- keys,
543
- }
544
- }
545
-
546
510
/// Returns the target used to calculate the CRC checksum for the specified algorithm.
547
511
///
548
512
/// These strings are informational only, not stable, and shouldn't be relied on to match across
@@ -569,7 +533,7 @@ fn get_calculator_params(algorithm: CrcAlgorithm) -> (CalculatorFn, CrcParams) {
569
533
CrcAlgorithm :: Crc32CdRomEdc => ( Calculator :: calculate as CalculatorFn , CRC32_CD_ROM_EDC ) ,
570
534
CrcAlgorithm :: Crc32Cksum => ( Calculator :: calculate as CalculatorFn , CRC32_CKSUM ) ,
571
535
CrcAlgorithm :: Crc32Custom => {
572
- panic ! ( "Custom CRC-32 requires parameters via get_custom_params ()" )
536
+ panic ! ( "Custom CRC-32 requires parameters via CrcParams::new ()" )
573
537
}
574
538
CrcAlgorithm :: Crc32Iscsi => ( crc32_iscsi_calculator as CalculatorFn , CRC32_ISCSI ) ,
575
539
CrcAlgorithm :: Crc32IsoHdlc => ( crc32_iso_hdlc_calculator as CalculatorFn , CRC32_ISO_HDLC ) ,
@@ -578,7 +542,7 @@ fn get_calculator_params(algorithm: CrcAlgorithm) -> (CalculatorFn, CrcParams) {
578
542
CrcAlgorithm :: Crc32Mpeg2 => ( Calculator :: calculate as CalculatorFn , CRC32_MPEG_2 ) ,
579
543
CrcAlgorithm :: Crc32Xfer => ( Calculator :: calculate as CalculatorFn , CRC32_XFER ) ,
580
544
CrcAlgorithm :: Crc64Custom => {
581
- panic ! ( "Custom CRC-64 requires parameters via get_custom_params ()" )
545
+ panic ! ( "Custom CRC-64 requires parameters via CrcParams::new ()" )
582
546
}
583
547
CrcAlgorithm :: Crc64Ecma182 => ( Calculator :: calculate as CalculatorFn , CRC64_ECMA_182 ) ,
584
548
CrcAlgorithm :: Crc64GoIso => ( Calculator :: calculate as CalculatorFn , CRC64_GO_ISO ) ,
@@ -658,59 +622,49 @@ mod lib {
658
622
fn test_checksum_with_custom_params ( ) {
659
623
// CRC-32 reflected
660
624
assert_eq ! (
661
- checksum_with_params( CRC32_ISCSI , TEST_CHECK_STRING ) ,
625
+ checksum_with_params( get_custom_crc32_reflected ( ) , TEST_CHECK_STRING ) ,
662
626
CRC32_ISCSI . check,
663
627
) ;
664
628
665
629
// CRC-32 forward
666
630
assert_eq ! (
667
- checksum_with_params( CRC32_BZIP2 , TEST_CHECK_STRING ) ,
631
+ checksum_with_params( get_custom_crc32_forward ( ) , TEST_CHECK_STRING ) ,
668
632
CRC32_BZIP2 . check,
669
633
) ;
670
634
671
635
// CRC-64 reflected
672
636
assert_eq ! (
673
- checksum_with_params( CRC64_NVME , TEST_CHECK_STRING ) ,
637
+ checksum_with_params( get_custom_crc64_reflected ( ) , TEST_CHECK_STRING ) ,
674
638
CRC64_NVME . check,
675
639
) ;
676
640
677
641
// CRC-64 forward
678
642
assert_eq ! (
679
- checksum_with_params( CRC64_ECMA_182 , TEST_CHECK_STRING ) ,
643
+ checksum_with_params( get_custom_crc64_forward ( ) , TEST_CHECK_STRING ) ,
680
644
CRC64_ECMA_182 . check,
681
645
) ;
682
646
}
683
647
684
648
#[ test]
685
649
fn test_get_custom_params ( ) {
686
- let custom_crc32 = get_custom_params (
687
- "Custom CRC-32/ISCSI" ,
688
- 32 ,
689
- CRC32_ISCSI . poly ,
690
- CRC32_ISCSI . init ,
691
- CRC32_ISCSI . refin ,
692
- CRC32_ISCSI . xorout ,
650
+ assert_eq ! (
651
+ checksum_with_params( get_custom_crc32_reflected( ) , TEST_CHECK_STRING ) ,
693
652
CRC32_ISCSI . check,
694
653
) ;
695
654
696
655
assert_eq ! (
697
- checksum_with_params( custom_crc32 , TEST_CHECK_STRING ) ,
698
- CRC32_ISCSI . check,
656
+ checksum_with_params( get_custom_crc32_forward ( ) , TEST_CHECK_STRING ) ,
657
+ CRC32_BZIP2 . check,
699
658
) ;
700
659
701
- let custom_crc64 = get_custom_params (
702
- "Custom CRC-64/NVME" ,
703
- 64 ,
704
- CRC64_NVME . poly ,
705
- CRC64_NVME . init ,
706
- CRC64_NVME . refin ,
707
- CRC64_NVME . xorout ,
660
+ assert_eq ! (
661
+ checksum_with_params( get_custom_crc64_reflected( ) , TEST_CHECK_STRING ) ,
708
662
CRC64_NVME . check,
709
663
) ;
710
664
711
665
assert_eq ! (
712
- checksum_with_params( custom_crc64 , TEST_CHECK_STRING ) ,
713
- CRC64_NVME . check,
666
+ checksum_with_params( get_custom_crc64_forward ( ) , TEST_CHECK_STRING ) ,
667
+ CRC64_ECMA_182 . check,
714
668
) ;
715
669
}
716
670
@@ -724,17 +678,26 @@ mod lib {
724
678
#[ test]
725
679
fn test_digest_updates_check_with_custom_params ( ) {
726
680
// CRC-32 reflected
727
- check_digest ( Digest :: new_with_params ( CRC32_ISCSI ) , CRC32_ISCSI . check ) ;
681
+ check_digest (
682
+ Digest :: new_with_params ( get_custom_crc32_reflected ( ) ) ,
683
+ CRC32_ISCSI . check ,
684
+ ) ;
728
685
729
686
// CRC-32 forward
730
- check_digest ( Digest :: new_with_params ( CRC32_BZIP2 ) , CRC32_BZIP2 . check ) ;
687
+ check_digest (
688
+ Digest :: new_with_params ( get_custom_crc32_forward ( ) ) ,
689
+ CRC32_BZIP2 . check ,
690
+ ) ;
731
691
732
692
// CRC-64 reflected
733
- check_digest ( Digest :: new_with_params ( CRC64_NVME ) , CRC64_NVME . check ) ;
693
+ check_digest (
694
+ Digest :: new_with_params ( get_custom_crc64_reflected ( ) ) ,
695
+ CRC64_NVME . check ,
696
+ ) ;
734
697
735
698
// CRC-64 forward
736
699
check_digest (
737
- Digest :: new_with_params ( CRC64_ECMA_182 ) ,
700
+ Digest :: new_with_params ( get_custom_crc64_forward ( ) ) ,
738
701
CRC64_ECMA_182 . check ,
739
702
) ;
740
703
}
@@ -825,34 +788,38 @@ mod lib {
825
788
#[ test]
826
789
fn test_combine_with_custom_params ( ) {
827
790
// CRC-32 reflected
828
- let checksum1 = checksum_with_params ( CRC32_ISCSI , "1234" . as_ref ( ) ) ;
829
- let checksum2 = checksum_with_params ( CRC32_ISCSI , "56789" . as_ref ( ) ) ;
791
+ let crc32_params = get_custom_crc32_reflected ( ) ;
792
+ let checksum1 = checksum_with_params ( crc32_params, "1234" . as_ref ( ) ) ;
793
+ let checksum2 = checksum_with_params ( crc32_params, "56789" . as_ref ( ) ) ;
830
794
assert_eq ! (
831
- checksum_combine_with_custom_params( CRC32_ISCSI , checksum1, checksum2, 5 ) ,
795
+ checksum_combine_with_custom_params( crc32_params , checksum1, checksum2, 5 ) ,
832
796
CRC32_ISCSI . check,
833
797
) ;
834
798
835
799
// CRC-32 forward
836
- let checksum1 = checksum_with_params ( CRC32_BZIP2 , "1234" . as_ref ( ) ) ;
837
- let checksum2 = checksum_with_params ( CRC32_BZIP2 , "56789" . as_ref ( ) ) ;
800
+ let crc32_params = get_custom_crc32_forward ( ) ;
801
+ let checksum1 = checksum_with_params ( crc32_params, "1234" . as_ref ( ) ) ;
802
+ let checksum2 = checksum_with_params ( crc32_params, "56789" . as_ref ( ) ) ;
838
803
assert_eq ! (
839
- checksum_combine_with_custom_params( CRC32_BZIP2 , checksum1, checksum2, 5 ) ,
804
+ checksum_combine_with_custom_params( crc32_params , checksum1, checksum2, 5 ) ,
840
805
CRC32_BZIP2 . check,
841
806
) ;
842
807
843
808
// CRC-64 reflected
844
- let checksum1 = checksum_with_params ( CRC64_NVME , "1234" . as_ref ( ) ) ;
845
- let checksum2 = checksum_with_params ( CRC64_NVME , "56789" . as_ref ( ) ) ;
809
+ let crc64_params = get_custom_crc64_reflected ( ) ;
810
+ let checksum1 = checksum_with_params ( crc64_params, "1234" . as_ref ( ) ) ;
811
+ let checksum2 = checksum_with_params ( crc64_params, "56789" . as_ref ( ) ) ;
846
812
assert_eq ! (
847
- checksum_combine_with_custom_params( CRC64_NVME , checksum1, checksum2, 5 ) ,
813
+ checksum_combine_with_custom_params( crc64_params , checksum1, checksum2, 5 ) ,
848
814
CRC64_NVME . check,
849
815
) ;
850
816
851
817
// CRC-64 forward
852
- let checksum1 = checksum_with_params ( CRC64_ECMA_182 , "1234" . as_ref ( ) ) ;
853
- let checksum2 = checksum_with_params ( CRC64_ECMA_182 , "56789" . as_ref ( ) ) ;
818
+ let crc64_params = get_custom_crc64_forward ( ) ;
819
+ let checksum1 = checksum_with_params ( crc64_params, "1234" . as_ref ( ) ) ;
820
+ let checksum2 = checksum_with_params ( crc64_params, "56789" . as_ref ( ) ) ;
854
821
assert_eq ! (
855
- checksum_combine_with_custom_params( CRC64_ECMA_182 , checksum1, checksum2, 5 ) ,
822
+ checksum_combine_with_custom_params( crc64_params , checksum1, checksum2, 5 ) ,
856
823
CRC64_ECMA_182 . check,
857
824
) ;
858
825
}
@@ -886,16 +853,32 @@ mod lib {
886
853
}
887
854
888
855
// CRC-32 reflected
889
- check_file ( CRC32_ISCSI , test_file_path, CRC32_ISCSI . check ) ;
856
+ check_file (
857
+ get_custom_crc32_reflected ( ) ,
858
+ test_file_path,
859
+ CRC32_ISCSI . check ,
860
+ ) ;
890
861
891
862
// CRC-32 forward
892
- check_file ( CRC32_BZIP2 , test_file_path, CRC32_BZIP2 . check ) ;
863
+ check_file (
864
+ get_custom_crc32_forward ( ) ,
865
+ test_file_path,
866
+ CRC32_BZIP2 . check ,
867
+ ) ;
893
868
894
869
// CRC-64 reflected
895
- check_file ( CRC64_NVME , test_file_path, CRC64_NVME . check ) ;
870
+ check_file (
871
+ get_custom_crc64_reflected ( ) ,
872
+ test_file_path,
873
+ CRC64_NVME . check ,
874
+ ) ;
896
875
897
876
// CRC-64 forward
898
- check_file ( CRC64_ECMA_182 , test_file_path, CRC64_ECMA_182 . check ) ;
877
+ check_file (
878
+ get_custom_crc64_forward ( ) ,
879
+ test_file_path,
880
+ CRC64_ECMA_182 . check ,
881
+ ) ;
899
882
900
883
std:: fs:: remove_file ( test_file_path) . unwrap ( ) ;
901
884
}
@@ -1058,4 +1041,52 @@ mod lib {
1058
1041
1059
1042
Ok ( ( ) )
1060
1043
}
1044
+
1045
+ fn get_custom_crc32_reflected ( ) -> CrcParams {
1046
+ CrcParams :: new (
1047
+ "Custom CRC-32/ISCSI" ,
1048
+ 32 ,
1049
+ CRC32_ISCSI . poly ,
1050
+ CRC32_ISCSI . init ,
1051
+ CRC32_ISCSI . refin ,
1052
+ CRC32_ISCSI . xorout ,
1053
+ CRC32_ISCSI . check ,
1054
+ )
1055
+ }
1056
+
1057
+ fn get_custom_crc32_forward ( ) -> CrcParams {
1058
+ CrcParams :: new (
1059
+ "Custom CRC-32/BZIP2" ,
1060
+ 32 ,
1061
+ CRC32_BZIP2 . poly ,
1062
+ CRC32_BZIP2 . init ,
1063
+ CRC32_BZIP2 . refin ,
1064
+ CRC32_BZIP2 . xorout ,
1065
+ CRC32_BZIP2 . check ,
1066
+ )
1067
+ }
1068
+
1069
+ fn get_custom_crc64_reflected ( ) -> CrcParams {
1070
+ CrcParams :: new (
1071
+ "Custom CRC-64/NVME" ,
1072
+ 64 ,
1073
+ CRC64_NVME . poly ,
1074
+ CRC64_NVME . init ,
1075
+ CRC64_NVME . refin ,
1076
+ CRC64_NVME . xorout ,
1077
+ CRC64_NVME . check ,
1078
+ )
1079
+ }
1080
+
1081
+ fn get_custom_crc64_forward ( ) -> CrcParams {
1082
+ CrcParams :: new (
1083
+ "Custom CRC-64/ECMA-182" ,
1084
+ 64 ,
1085
+ CRC64_ECMA_182 . poly ,
1086
+ CRC64_ECMA_182 . init ,
1087
+ CRC64_ECMA_182 . refin ,
1088
+ CRC64_ECMA_182 . xorout ,
1089
+ CRC64_ECMA_182 . check ,
1090
+ )
1091
+ }
1061
1092
}
0 commit comments