2011-09-19 174 views
1

说到这个:http://www.win-rar.com/index.php?id=24&kb_article_id=162如何计算WinRAR文件头的CRC?

我能够做计算归档头(MAIN_HEAD)的正确CRC:

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

第一行写着“CRC领域的HEAD_TYPE到RESERVED2“作为文件中的状态。正如我所指出的那样,它对档案头部来说工作正常。

当我尝试计算一个文件头的CRC时,它总是会因不明原因吐出错误的CRC。我按照文档所述 - “从HEAD_TYPE到FILEATTR的字段的CRC”,但它根本不起作用。如果文档不正确,我也尝试了不同的读长度变化,实际上它可能是从HEAD_TYPE到FILE_NAME。一切都没有成功。

任何人都可以给我一个提示吗?我也检查了unrar源代码,但它并没有让我变得更聪明,可能是因为我根本不知道C语言......

回答

1

我写了一些代码来做同样的事情。下面是它为更好地理解一些附加的片段:

$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); 

我测试了它的一对夫妇的SRR文件,它按预期工作。我开始阅读基本的7字节标题。标题的大小可以在那里找到。我用它来获取crc32函数的正确数据量。我注意到,当您将其转换为十六进制时,在比较'0f00'!='f00'时可能会出现误报。你需要用零填充它。这就是为什么我保留crc32()和unpack()的十进制表示以进行比较。另外,如果设置了一些标题标记,则文件块的字段数可能会有所不同:可能是您输入了错误的大小。

+0

谢谢!将尽快尝试。 – nginxguy

+0

如果其他人感兴趣,可以在这里找到一些读取RAR文件的PHP代码: http://www.newznab.com/download.html – Gfy