1
0
mirror of https://github.com/Rogiel/php-mpq synced 2025-12-06 08:23:05 +00:00

Fixes a bug on block decompression handling

This commit is contained in:
2016-01-18 18:35:03 -02:00
parent 2669ee5738
commit 7f516c051a

View File

@@ -30,6 +30,8 @@ namespace Rogiel\MPQ\Stream\Block;
use Rogiel\MPQ\Compression\BZIPCompression; use Rogiel\MPQ\Compression\BZIPCompression;
use Rogiel\MPQ\Compression\DeflateCompression;
use Rogiel\MPQ\Exception\Compression\CompressionException;
use Rogiel\MPQ\Metadata\Block; use Rogiel\MPQ\Metadata\Block;
use Rogiel\MPQ\MPQFile; use Rogiel\MPQ\MPQFile;
use Rogiel\MPQ\Stream\CompressedStream; use Rogiel\MPQ\Stream\CompressedStream;
@@ -93,6 +95,7 @@ class BlockStream implements Stream {
$this->position = 0; $this->position = 0;
$this->buffer = NULL; $this->buffer = NULL;
$this->currentSector = $this->sectors[0]; $this->currentSector = $this->sectors[0];
$this->positionInSector = 0;
} }
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
@@ -113,8 +116,9 @@ class BlockStream implements Stream {
$bytes = $this->block->getSize() - $this->position; $bytes = $this->block->getSize() - $this->position;
} }
if($this->buffer == NULL) { if($this->buffer === NULL) {
$this->buffer = $this->readSector($this->currentSector); $this->buffer = $this->readSector($this->currentSector);
$this->positionInSector = 0;
} else if($this->positionInSector >= strlen($this->buffer)) { } else if($this->positionInSector >= strlen($this->buffer)) {
$this->currentSector = $this->sectors[$this->currentSector->getIndex() + 1]; $this->currentSector = $this->sectors[$this->currentSector->getIndex() + 1];
$this->buffer = $this->readSector($this->currentSector); $this->buffer = $this->readSector($this->currentSector);
@@ -163,27 +167,19 @@ class BlockStream implements Stream {
private function createCompressedStream() { private function createCompressedStream() {
$stream = $this->stream; $stream = $this->stream;
if($this->block->isCompressed()) {
if($this->block->isCompressed() && $this->block->getSize() > $this->block->getCompressedSize()) {
$parser = new BinaryStreamParser($this->stream); $parser = new BinaryStreamParser($this->stream);
$compressionType = $parser->readByte(); $compressionType = $parser->readByte();
switch ($compressionType) { switch ($compressionType) {
case 0x10: case 0x00: return $stream;
return new CompressedStream($stream, new BZIPCompression()); case 0x02: return new CompressedStream($stream, new DeflateCompression());
break; case 0x10: return new CompressedStream($stream, new BZIPCompression());
default:
throw new CompressionException(sprintf('Invalid compression format: %s', $compressionType));
} }
} }
return $stream; return $stream;
} }
private function computeSectors($start, $length) {
$sectors = array();
foreach($this->sectors as $sector) {
/** @var $sector Sector */
if($sector->contains($start, $length)) {
$sectors[] = $sector;
}
}
return $sectors;
}
} }