Processing multiple chunks
Cool library! It would be nice to be able to do this:
uint32_t crc = CRC32::CRC32::calc(chunk1, size1);
//...
crc = CRC32::CRC32::calc(chunk2, size2, crc); // incorrect result - but this works for this specific case: calc(chunk2, size2, ~crc);
@pbsurf, Thank you for pointing this out, that is definitely a good feature to have! Especially since it's a potential foot gun that might be hard to detect.
I went ahead and added a compile-time error that will tell people when they're using an algorithm that needs this special treatment, and then gives them a choice of ignoring the error (using the exact CRC algorithm from literature via a new calc_exact(...) API). Or switching to a new calc_chunk(...) API, which will automatically handle those details for them, with the downside of the algorithm no longer exactly matching literature (totally worth it though in most cases for ease of use I'd think).
So, for your use-case where the CRC32 happens to be susceptible to this corner case, this code will no longer compile, it will tell you to use calc_exact or calc_chunk instead in order to be explicit about your intent:
uint32_t crc = CRC32::CRC32::calc(chunk1, size1);
crc = CRC32::CRC32::calc(chunk2, size2, crc);
Recommending you use either the automated option (calc_chunk):
uint32_t crc = CRC32::CRC32::calc_chunk(chunk1, size1);
crc = CRC32::CRC32::calc_chunk(chunk2, size2, crc);
Or the manual option (calc_exact) to choose from - where you'd have to manually take care of reversals and X-OR undoing:
uint32_t crc = CRC32::CRC32::calc_exact(chunk1, size1);
crc = CRC32::CRC32::calc_exact(chunk2, size2, crc_utils::reverse(crc ^ CRC32::CRC32::x_or_out));
Note that I didn't make calc_chunk the default, because I still want people to be able to perform a calculation that exactly matches what is found in literature, which both calc (when it compiles) and calc_exact will still do.
Here are the changes that added those features: https://github.com/DarrenLevine/cppcrc/commit/ea15099f0e47f6b915c0b124fb876a6618567205
After sleeping on this, I've decided to change direction and make the calc_chunk behavior the default behavior after all - and just update the documentation to clearly differentiate "init" from the "null CRC" value. I think the benefit of having just a single unified function outweighs the downside of confusing init for the null crc, since most people won't care about the difference anyway, and it's one less potential compilation error to deal with. Apologies for the flip flop in design.