duanbei8904 2011-09-19 08:55
浏览 328
已采纳

如何计算WinRAR文件头的CRC?

Talking about this: http://www.win-rar.com/index.php?id=24&kb_article_id=162

I'm able to calculate the correct CRC of an archive header (MAIN_HEAD) by doing:

$crc = crc32(mb_substr($data, $blockOffset + 2, 11, '8bit'));
$crc = dechex($crc);
$crc = substr($crc, -4, 2) . substr($crc, -2, 2);
$crc = hexdec($crc);

The first line will read "CRC of fields HEAD_TYPE to RESERVED2" as states in the documentation. As I noted, it works fine for the archive header.

When I try to calculate the CRC of a file header it always spits out the wrong CRC for unknown reason. I did as the documentation says - "CRC of fields from HEAD_TYPE to FILEATTR" but it simply doesn't work. I've also tried different read-length variations in case the documentation is wrong and it might actually be from HEAD_TYPE to FILE_NAME. Everything without success.

Anyone can give me a hint? I've also checked the unrar source code but it doesn't make me smarter, probably because I don't know C language at all...

  • 写回答

1条回答 默认 最新

  • douxi7219 2011-09-21 18:23
    关注

    I wrote some code that does the same thing. Here is it with some additional snippets for a better understanding:

    $this->fh = $fileHandle;
    $this->startOffset = ftell($fileHandle); // current location in the file
    
    // reading basic 7 byte header block
    $array = unpack('vheaderCrc/CblockType/vflags/vheaderSize', fread($this->fh, 7));
    $this->headerCrc = $array['headerCrc'];
    $this->blockType = $array['blockType'];
    $this->flags = $array['flags'];
    $this->hsize = $array['headerSize'];
    $this->addSize = 0; // size of data after the header
    
    
    // -- check CRC of block header --
    $offset = ftell($this->fh);
    fseek($this->fh, $this->startOffset + 2, SEEK_SET);
    $crcData = fread($this->fh, $this->hsize - 2);
    // only the 4 lower order bytes are used
    $crc = crc32($crcData) & 0xffff;
    // igonore blocks with no CRC set (same as twice the blockType)
    if ($crc !== $this->headerCrc && $this->headerCrc !== 0x6969 // SRR Header
                && $this->headerCrc !== 0x6a6a // SRR Stored File
                && $this->headerCrc !== 0x7171 // SRR RAR block
                && $this->blockType !== 0x72 // RAR marker block (fixed: magic number)
    ) {
        array_push($warnings, 'Invalid block header CRC found: header is corrupt.');
    }
    // set offset back to where we started from
    fseek($this->fh, $offset, SEEK_SET);
    

    I tested it on a couple of SRR files and it works as expected. I started with reading the basic 7 byte header. The size of the header can be found there. I used this to grab the correct amount of data for the crc32 function. I noticed that when you convert it to hexadecimal, you can get false positives when comparing: '0f00' != 'f00'. You would need to pad it with zeros. This is why I kept the decimal representations of crc32() and unpack() for the comparison. Also, the number of fields of a file block can vary if some header flags are set: it is possible you took a wrong size.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页