问题
Where I can find this crc32() function in detail? I saw this Link but I couldn't figure out how CRC is being calculated. I asked a question about updating the CRC based on the data stream instead of waiting to have all the data. I got the answer as follows (Thanks to @MarkAdler):
unsigned long crc = crc32(0, NULL, 0); // initial CRC
for (...) { // some sort of loop
... // generating a chunk of data
crc = crc32(crc, buf, len); // update the CRC with the data
... // this all gets repeated many times
}
... // loop is done, crc has the CRC
- Can you please be more specific about the
crc32()
function? - Is there any pseudo code for that function which explains it?
- And
for
loop here is for getting data right?
Thank you
回答1:
- More specific how?
- Do you want an explanation of how to use it, or how it works internally? How it works internally is in the source code you linked.
- Yes. You would get chunks of data from wherever, and feed them through
crc32()
to get the CRC-32/ISO-HDLC of the entire stream of data.
As for the source code you linked, that was not written to teach anyone about how CRC's work. It was written to be fast. Here is a simple, but slow (one bit at a time), CRC routine in C, that may, or may not, help you with what you're looking for:
uint32_t crc32iso_hdlc_bit(uint32_t crc, void const *mem, size_t len) {
unsigned char const *data = mem;
if (data == NULL)
return 0;
crc = ~crc;
for (size_t i = 0; i < len; i++) {
crc ^= data[i];
for (unsigned k = 0; k < 8; k++) {
crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
}
}
crc = ~crc;
return crc;
}
(That code was generated by crcany.)
If you would like to learn more about CRCs, start here.
来源:https://stackoverflow.com/questions/65515573/crc32-function-explanation-with-regards-to-data-streams