本文介绍了实时计算CRC(可能与否)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以即时(在流中)计算CRC?

Is it possible to calculate the CRCs on the fly (in the streams)?

例如,我有1 GB的数据,我想减少未检测到的错误的可能性。
我想在整个文件中实现某些内容(CRC或哈希),
(我已经为每个包含一些数据包的块实现了CRC),

For example, I have 1-gigabyte data and I want to reduce the possibility of undetected errors.I want to implement something (CRC or Hash) over the whole file,(I already have implemented CRCs for each chunk, which contains some packets),

当我们将CRC放在整个文件上时,是否有可能在收到第一个数据包后立即开始计算CRC,还是必须等待接收到整个文件然后开始计算CRC?

When we put a CRC over the whole file, is it possible to start calculating the CRC as soon as we have the first packet or do we have to wait for the whole file to be received and then start calculating the CRCs?

推荐答案

是。 CRC和我所知道的每个哈希都可以流式传输。它们具有一个很小的有限状态,该状态会随着数据通过它们的更新而更新。对于CRC,状态是CRC本身。

Yes. CRCs and every hash I know of are all streamable. They have a small, finite state that is updated as data is fed through them. For CRCs, the state is the CRC itself.

zlib中的CRC采用以下形式:

The CRC in zlib takes this form:

unsigned long crc32(unsigned long crc, const unsigned char *buf, unsigned len);

buf NULL ,返回初始CRC。因此它是这样使用的:

When buf is NULL, the initial CRC is returned. So it is used like this:

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

这篇关于实时计算CRC(可能与否)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:24