Skip to content

Commit 786b4df

Browse files
committed
Enable custom parameters via CrcParams constructor
Replaces get_custom_params() with CrcParams::new(). awesomized#11 (comment)
1 parent 9272a48 commit 786b4df

File tree

4 files changed

+154
-83
lines changed

4 files changed

+154
-83
lines changed

src/bin/get-custom-params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn main() -> ExitCode {
167167

168168
let static_name: &'static str = Box::leak(config.name.unwrap().into_boxed_str());
169169

170-
let params = crc_fast::get_custom_params(
170+
let params = crc_fast::CrcParams::new(
171171
static_name,
172172
config.width.unwrap() as u8,
173173
config.polynomial.unwrap(),

src/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ pub extern "C" fn crc_fast_get_custom_params(
343343
};
344344

345345
// Get the custom params from the library
346-
let params = crate::get_custom_params(
346+
let params = CrcParams::new(
347347
// We need to use a static string for the name field
348348
Box::leak(name.to_string().into_boxed_str()),
349349
width,

src/lib.rs

Lines changed: 111 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -507,42 +507,6 @@ pub fn checksum_combine_with_custom_params(
507507
combine::checksums(checksum1, checksum2, checksum2_len, params)
508508
}
509509

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-
546510
/// Returns the target used to calculate the CRC checksum for the specified algorithm.
547511
///
548512
/// 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) {
569533
CrcAlgorithm::Crc32CdRomEdc => (Calculator::calculate as CalculatorFn, CRC32_CD_ROM_EDC),
570534
CrcAlgorithm::Crc32Cksum => (Calculator::calculate as CalculatorFn, CRC32_CKSUM),
571535
CrcAlgorithm::Crc32Custom => {
572-
panic!("Custom CRC-32 requires parameters via get_custom_params()")
536+
panic!("Custom CRC-32 requires parameters via CrcParams::new()")
573537
}
574538
CrcAlgorithm::Crc32Iscsi => (crc32_iscsi_calculator as CalculatorFn, CRC32_ISCSI),
575539
CrcAlgorithm::Crc32IsoHdlc => (crc32_iso_hdlc_calculator as CalculatorFn, CRC32_ISO_HDLC),
@@ -578,7 +542,7 @@ fn get_calculator_params(algorithm: CrcAlgorithm) -> (CalculatorFn, CrcParams) {
578542
CrcAlgorithm::Crc32Mpeg2 => (Calculator::calculate as CalculatorFn, CRC32_MPEG_2),
579543
CrcAlgorithm::Crc32Xfer => (Calculator::calculate as CalculatorFn, CRC32_XFER),
580544
CrcAlgorithm::Crc64Custom => {
581-
panic!("Custom CRC-64 requires parameters via get_custom_params()")
545+
panic!("Custom CRC-64 requires parameters via CrcParams::new()")
582546
}
583547
CrcAlgorithm::Crc64Ecma182 => (Calculator::calculate as CalculatorFn, CRC64_ECMA_182),
584548
CrcAlgorithm::Crc64GoIso => (Calculator::calculate as CalculatorFn, CRC64_GO_ISO),
@@ -658,59 +622,49 @@ mod lib {
658622
fn test_checksum_with_custom_params() {
659623
// CRC-32 reflected
660624
assert_eq!(
661-
checksum_with_params(CRC32_ISCSI, TEST_CHECK_STRING),
625+
checksum_with_params(get_custom_crc32_reflected(), TEST_CHECK_STRING),
662626
CRC32_ISCSI.check,
663627
);
664628

665629
// CRC-32 forward
666630
assert_eq!(
667-
checksum_with_params(CRC32_BZIP2, TEST_CHECK_STRING),
631+
checksum_with_params(get_custom_crc32_forward(), TEST_CHECK_STRING),
668632
CRC32_BZIP2.check,
669633
);
670634

671635
// CRC-64 reflected
672636
assert_eq!(
673-
checksum_with_params(CRC64_NVME, TEST_CHECK_STRING),
637+
checksum_with_params(get_custom_crc64_reflected(), TEST_CHECK_STRING),
674638
CRC64_NVME.check,
675639
);
676640

677641
// CRC-64 forward
678642
assert_eq!(
679-
checksum_with_params(CRC64_ECMA_182, TEST_CHECK_STRING),
643+
checksum_with_params(get_custom_crc64_forward(), TEST_CHECK_STRING),
680644
CRC64_ECMA_182.check,
681645
);
682646
}
683647

684648
#[test]
685649
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),
693652
CRC32_ISCSI.check,
694653
);
695654

696655
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,
699658
);
700659

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),
708662
CRC64_NVME.check,
709663
);
710664

711665
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,
714668
);
715669
}
716670

@@ -724,17 +678,26 @@ mod lib {
724678
#[test]
725679
fn test_digest_updates_check_with_custom_params() {
726680
// 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+
);
728685

729686
// 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+
);
731691

732692
// 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+
);
734697

735698
// CRC-64 forward
736699
check_digest(
737-
Digest::new_with_params(CRC64_ECMA_182),
700+
Digest::new_with_params(get_custom_crc64_forward()),
738701
CRC64_ECMA_182.check,
739702
);
740703
}
@@ -825,34 +788,38 @@ mod lib {
825788
#[test]
826789
fn test_combine_with_custom_params() {
827790
// 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());
830794
assert_eq!(
831-
checksum_combine_with_custom_params(CRC32_ISCSI, checksum1, checksum2, 5),
795+
checksum_combine_with_custom_params(crc32_params, checksum1, checksum2, 5),
832796
CRC32_ISCSI.check,
833797
);
834798

835799
// 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());
838803
assert_eq!(
839-
checksum_combine_with_custom_params(CRC32_BZIP2, checksum1, checksum2, 5),
804+
checksum_combine_with_custom_params(crc32_params, checksum1, checksum2, 5),
840805
CRC32_BZIP2.check,
841806
);
842807

843808
// 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());
846812
assert_eq!(
847-
checksum_combine_with_custom_params(CRC64_NVME, checksum1, checksum2, 5),
813+
checksum_combine_with_custom_params(crc64_params, checksum1, checksum2, 5),
848814
CRC64_NVME.check,
849815
);
850816

851817
// 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());
854821
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),
856823
CRC64_ECMA_182.check,
857824
);
858825
}
@@ -886,16 +853,32 @@ mod lib {
886853
}
887854

888855
// 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+
);
890861

891862
// 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+
);
893868

894869
// 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+
);
896875

897876
// 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+
);
899882

900883
std::fs::remove_file(test_file_path).unwrap();
901884
}
@@ -1058,4 +1041,52 @@ mod lib {
10581041

10591042
Ok(())
10601043
}
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+
}
10611092
}

src/structs.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#![allow(dead_code)]
44

55
use crate::traits::{CrcCalculator, CrcWidth};
6-
use crate::{arch, CrcParams};
6+
use crate::{arch, generate, CrcAlgorithm, CrcParams};
7+
78
/// CRC-32 width implementation
89
#[derive(Clone, Copy)]
910
pub struct Width32;
11+
1012
impl CrcWidth for Width32 {
1113
const WIDTH: u32 = 32;
1214
type Value = u32;
@@ -36,3 +38,41 @@ impl CrcCalculator for Calculator {
3638
unsafe { arch::update(state, data, params) }
3739
}
3840
}
41+
42+
impl CrcParams {
43+
/// Creates custom CRC parameters for a given set of Rocksoft CRC parameters.
44+
///
45+
/// Does not support mis-matched refin/refout parameters, so both must be true or both false.
46+
///
47+
/// Rocksoft parameters for lots of variants: https://reveng.sourceforge.io/crc-catalogue/all.htm
48+
pub fn new(
49+
name: &'static str,
50+
width: u8,
51+
poly: u64,
52+
init: u64,
53+
reflected: bool,
54+
xorout: u64,
55+
check: u64,
56+
) -> Self {
57+
let keys = generate::keys(width, poly, reflected);
58+
59+
let algorithm = match width {
60+
32 => CrcAlgorithm::Crc32Custom,
61+
64 => CrcAlgorithm::Crc64Custom,
62+
_ => panic!("Unsupported width: {}", width),
63+
};
64+
65+
Self {
66+
algorithm,
67+
name,
68+
width,
69+
poly,
70+
init,
71+
refin: reflected,
72+
refout: reflected,
73+
xorout,
74+
check,
75+
keys,
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)