mrhooray / crc-rs

Rust implementation of CRC(16, 32, 64) with support of various standards
Apache License 2.0
191 stars 49 forks source link

Docs on how to upgrade from v1 to v2 api? #62

Open ghost opened 3 years ago

0-wiz-0 commented 3 years ago

I tried this in my code:

-use crc::{crc32, Hasher32};
+use crc::{Crc, CRC_32_BZIP2};
...
-    let mut crc = crc32::Digest::new(crc32::IEEE);
+    let crc = Crc::<u32>::new(&CRC_32_BZIP2);
+    let mut digest = crc.digest();
...
-       crc.write(&buffer[..n]);
+       digest.update(&buffer[..n]);
...
-    Ok(format!("crc {:08x}", crc.sum32())
+    Ok(format!("crc {:08x}", digest.finalize())

but then I get different output for my test files, so this is wrong. What did I do wrong?

akhilles commented 3 years ago

@0-wiz-0 CRC_32_BZIP2 is a different algorithm from CRC_32_IEEE, try CRC_32_ISO_HDLC instead. Other than that your diff is correct, and should work. One performance nitpick: use const crc: Crc<u32> = Crc::<u32>::new(&CRC_32_BZIP2); instead to generate the CRC table at compile-time instead of runtime.

It's unfortunate that these CRC polynomials have multiple names (https://reveng.sourceforge.io/crc-catalogue/all.htm is a pretty good reference).

0-wiz-0 commented 3 years ago

Thanks, @akhilles , that worked!

jszwedko commented 3 years ago

I also have a question about this. We were using:

crc::crc64::checksum_ecma(&buf[..])

It seems like this should be replace by:

Crc::<u64>::new(&crc::CRC_64_ECMA_182).checksum(&buf[..])

But this is calculating a different checksum value. Is there something equivalent to the old crc::crc64::checksum_ecma()?

akhilles commented 3 years ago

I also have a question about this. We were using:

crc::crc64::checksum_ecma(&buf[..])

It seems like this should be replace by:

Crc::<u64>::new(&crc::CRC_64_ECMA_182).checksum()

But this is calculating a different checksum value. Is there something equivalent to the old crc::crc64::checksum_ecma()?

Could you try crc::CRC_64_XZ? According to https://reveng.sourceforge.io/crc-catalogue/all.htm, it's an algorithm commonly misidentified as ECMA.

jszwedko commented 3 years ago

Aha, yep, that appears to be it. Thanks @akhilles !

jimvdl commented 2 years ago

I upgraded from v1.8 to v2 and it was pretty straightforward and painless. I used the reference that was in the documentation (this link: https://reveng.sourceforge.io/crc-catalogue/all.htm) to figure out what constant I needed (for me this was from IEEE to CRC_32_ISO_HDLC). It took me about 3 minutes to upgrade. The tip about generating the CRC table at compile-time was also very handy: const crc: Crc<u32> = Crc::<u32>::new(&CRC_32_BZIP2);