PNG 文件格式的 CRC

发布于 2024-08-27 05:08:22 字数 325 浏览 6 评论 0原文

我需要读取 PNG 文件并解释其中存储的所有信息并以人类可读的格式打印它。在处理 PNG 时,我了解到它使用 CRC-32 为每个块生成校验和。但我无法理解 PNG 文件规范网站上提到的以下信息: PNG 使用的多项式是: x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1

这是参考链接: http://www.w3.org/TR/PNG/

谁能帮助我理解这一点?

I need to read a PNG file and interpret all the information stored in it and print it in human readable format. While working on PNG, I understood that it uses CRC-32 for generating checksum for each chunk. But I could not understand the following information mentioned on the PNG file specification site:
The polynomial used by PNG is:
x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1

Here is the link for reference:
http://www.w3.org/TR/PNG/

Can anyone please help me in understanding this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迎风吟唱 2024-09-03 05:08:28

每个 x 项均指 0xedb88320 的二进制表示形式中的 1,其中是 11101101 10111000 10000011 00100000。左侧(最低有效)端的数字 1 是常数 (x^0) 项(的系数)。左边的下一个数字 1 是 x 项的系数。下一个数字 1 是 x^2 项的系数。下一个数字 0 是 x^3 项的系数(该项不存在,因为 0*x^3 = 0)。等等。右端(最重要)的右侧有一个隐含的 1,它是 x^32 项的系数。

Every x term refers to a 1 in the binary representation of 0xedb88320, which is 11101101 10111000 10000011 00100000. The number on the left (least-significant) end, 1, is the (coefficient of the) constant (x^0) term. The next number from the left, 1, is the coefficient of the x term. The next number, 1, is the coefficient of the x^2 term. The next number, 0, is the coefficient of the x^3 term (which is absent because 0*x^3 = 0). And so on. There is an implied 1 to the right of the right (most-significant) end that is the coefficient of the x^32 term.

秋日私语 2024-09-03 05:08:27

http://en.wikipedia.org/wiki/Computation_of_CRC

根据 wiki 中的 CRC 列表,该多项式(又名 AUTODIN II 多项式)是最常用的多项式之一。
CRC-32-IEEE 802.3 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1

用于(以太网、V.42、MPEG-2、PNG、 POSIX cksum、Arj、Lha32、Rar、Zip 等..)

^ 标记的电源重写:

 x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1.

因此您可以阅读 cksum 的源代码,例如这里

http://www.opensource.apple.com/source/file_cmds/file_cmds-188/cksum/ crc32.c

32 位 AutoDIN-II CRC 已构建
在以下移位寄存器上
参考模型。

多项式:g(x) = 1 + x + x^4 + x^5 + x^7 + x^8 + x^10 + x^11 +
x^12 + x^1 + x^22 + x^23 + x^26 + x^32

先输入数据位0

前导零检查通过以下过程执行:

 1. crc寄存器初始化为0xffffffff,而不是零。

 2. 当附加crc 时,crc 的32 位被反转。

 3. 当检查带有附加 crc 的好消息时,寄存器
    将返回固定值 0xdebb20e3,而不是零。

http://en.wikipedia.org/wiki/Computation_of_CRC ?

According to list of CRCs in wiki, this polynomial (aka AUTODIN II polynomial ) is one of the most used.
CRC-32-IEEE 802.3 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1

Used in (Ethernet, V.42, MPEG-2, PNG, POSIX cksum, Arj, Lha32, Rar, Zip, and more..)

Rewritted with power marked by ^:

 x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1.

So you can read source of cksum, e.g. here

http://www.opensource.apple.com/source/file_cmds/file_cmds-188/cksum/crc32.c

The 32-bit AutoDIN-II CRC is built
upon the following shift-register
reference model.

Polynomial: g(x) = 1 + x + x^4 + x^5 + x^7 + x^8 + x^10 + x^11 +
x^12 + x^1 + x^22 + x^23 + x^26 + x^32

Input data bit 0 first

Leading-zero checking is performed by the following procedure:

 1. The crc register is initialized to 0xffffffff, not zero.

 2. When a crc is appended, the 32 bits of the crc are inverted.

 3. When checking a good message with an appended crc, the register
    will return to the fixed value of 0xdebb20e3, rather than zero.
如日中天 2024-09-03 05:08:27

这就是 zlib 中实现的 CRC-32 算法。当您可以使用该库时,请不要实现您自己的库。


[编辑]:如何使用 zlib 中的 CRC 计算器(从 zlib 文档中提取的 C 示例)。

#include <zlib.h>

uLong crc = crc32(0L, Z_NULL, 0);

while (read_buffer(buffer, length) != EOF) {
   crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();

如果您有想要获取 CRC 的数据块,则不需要 while 循环;您只需获取初始值(上面对 crc 的第一次赋值),然后根据您拥有的数据计算该值(对 crc 的第二次赋值)。

That's the CRC-32 algorithm implemented in zlib. Please don't implement your own when you can use that library instead.


[EDIT]: How to use the CRC calculator from zlib (an example in C extracted from the zlib docs).

#include <zlib.h>

uLong crc = crc32(0L, Z_NULL, 0);

while (read_buffer(buffer, length) != EOF) {
   crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();

If you have the block of data that you want to get the CRC for, you don't need that while loop; you just get the initial value (first assignment to crc above) and then compute the value over the data that you have (second assignment to crc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文