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.

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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示