关于数据流的crc32功能说明 [英] crc32 function explanation with regards to data streams
问题描述
在哪里可以找到此crc32()函数的详细信息?我看到了这个链接,但我不知道CRC是如何正在计算.我问了一个有关基于数据流更新CRC而不是等待拥有所有数据的问题.我得到的答案如下(感谢@MarkAdler):
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
- 能否请您更详细地介绍
crc32()
函数? - 该函数是否有任何伪代码可以解释它?
- 这里的
for
循环是为了正确获取数据?
- 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?
谢谢
推荐答案
- 具体如何?
- 您想解释一下如何使用它,或如何在内部使用它吗?它在内部的工作方式在您链接的源代码中.
- 是的.您可以从任何地方获取数据块,然后通过
crc32()
馈送它们,以获取整个数据流的CRC-32/ISO-HDLC.
- 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.
至于您链接的源代码,并不是为了告诉任何人有关CRC的工作原理而编写的.它写得很快.这是C语言中一个简单但缓慢(每次一次)的CRC例程,可能会或可能不会帮助您找到所需的内容:
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;
}
(该代码由 crcany 生成.)
如果您想了解有关CRC的更多信息,请在此处开始.
If you would like to learn more about CRCs, start here.
这篇关于关于数据流的crc32功能说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!