crc-rs icon indicating copy to clipboard operation
crc-rs copied to clipboard

Docs on how to upgrade from v1 to v2 api?

Open ghost opened this issue 3 years ago • 7 comments

ghost avatar May 28 '21 09:05 ghost

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?

0-wiz-0 avatar May 28 '21 17:05 0-wiz-0

@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).

akhilles avatar May 28 '21 18:05 akhilles

Thanks, @akhilles , that worked!

0-wiz-0 avatar May 28 '21 18:05 0-wiz-0

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()?

jszwedko avatar Jul 08 '21 20:07 jszwedko

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.

akhilles avatar Jul 08 '21 21:07 akhilles

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

jszwedko avatar Jul 09 '21 00:07 jszwedko

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);

jimvdl avatar Nov 09 '21 13:11 jimvdl