How to calculate CRC of a WinRAR file?

2019-06-25 09:12发布

I know CRC calculation algorithm from Wikipedia. About structure of RAR file I read here. For example, there was written:

The file has the magic number of:

   0x 52 61 72 21 1A 07 00

Which is a break down of the following to describe an Archive Header:

   0x6152 - HEAD_CRC
   0x72   - HEAD_TYPE
   0x1A21 - HEAD_FLAGS
   0x0007 - HEAD_SIZE

If I understand correctly, the HEAD_CRC (0x6152) is CRC value of Marker Block (MARK_HEAD). Somewhere I read, that CRC of a WinRAR file is calculated with standard polynomial 0xEDB88320, but when size of CRC is less than 4 bytes, it's necessary to use less significant bytes. In this case (of course if I undestand correctly) CRC value is 0x6152, so it has 2 bytes. Now I don't know, which bytes I have to take as less significant. From the standard polynomial (0xEDB88320)? Then 0x8320 probably are less significant bytes of this polynomial. Next, how to calculate CRC of the Marker Block (i. e. from the following bytes: 0x 52 61 72 21 1A 07 00), if we have already right polynomial?

标签: crc rar winrar
2条回答
迷人小祖宗
2楼-- · 2019-06-25 09:27

There was likely a 16-bit check for an older format that is not derived from a 32-bit CRC. The standard 32-bit CRC, used by zip and rar, applied to the last five bytes of the header has no portion equal to the first two bytes. The Polish page appears to be incorrect in claiming that the two-byte check is the low two-bytes of a 32-bit CRC.

It does appear from the documentation that that header is constructed in a standard way as other blocks in the older format, so that the author, for fun, arranged for his format to give the check value "Ra" so that it could spell out "Rar!" followed by a text-terminating control-Z.

I found another 16-bit check in the unrar source code, but that check does not result in those values either.

Oh, and no, you can't take part of a CRC polynomial and expect that to be a good CRC polynomial for a smaller check. What the page in Polish is saying is that you would compute the full 32-bit CRC, and then take the low two bytes of the result. However that doesn't work for the magic number header.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-06-25 09:32

Per WinRAR TechNote.txt file included with the install:

The marker block is actually considered as a fixed byte sequence: 0x52 0x61 0x72 0x21 0x1a 0x07 0x00

And as you already indicated, at the very end you can read:

The CRC is calculated using the standard polynomial 0xEDB88320. In case the size of the CRC is less than 4 bytes, only the low order bytes are used.

In Python, the calculation and grabbing of the 2 low order bytes goes like this:

zlib.crc32(correct_byte_range) & 0xffff

rerar has some code that does this, just like the rarfile library that it uses. ReScene .NET source code has an algorithm in C# for calculating the CRC32 hash. See also How do I calculate CRC32 mathematically?

查看更多
登录 后发表回答