crc32 function explanation with regards to data streams

房东的猫 提交于 2021-01-07 01:29:52

问题


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
  1. Can you please be more specific about the crc32() function?
  2. Is there any pseudo code for that function which explains it?
  3. And for loop here is for getting data right?

Thank you


回答1:


  1. More specific how?
  2. 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.
  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!