mirror of
https://github.com/Rogiel/php-mpq
synced 2025-12-09 09:52:46 +00:00
Initial commit
This commit is contained in:
185
src/Stream/Block/BlockStream.php
Normal file
185
src/Stream/Block/BlockStream.php
Normal file
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream\Block;
|
||||
|
||||
|
||||
use Rogiel\MPQ\Compression\BZIPCompression;
|
||||
use Rogiel\MPQ\Metadata\Block;
|
||||
use Rogiel\MPQ\MPQFile;
|
||||
use Rogiel\MPQ\Stream\CompressedStream;
|
||||
use Rogiel\MPQ\Stream\Parser\BinaryStreamParser;
|
||||
use Rogiel\MPQ\Stream\Stream;
|
||||
|
||||
class BlockStream implements Stream {
|
||||
|
||||
/**
|
||||
* @var MPQFile
|
||||
*/
|
||||
private $file;
|
||||
|
||||
/**
|
||||
* @var Stream
|
||||
*/
|
||||
private $stream;
|
||||
|
||||
/**
|
||||
* @var Block
|
||||
*/
|
||||
private $block;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $sectors;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
private $position;
|
||||
private $buffer;
|
||||
|
||||
private $positionInSector;
|
||||
|
||||
/**
|
||||
* @var Sector
|
||||
*/
|
||||
private $currentSector;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* BlockStream constructor.
|
||||
* @param MPQFile $file
|
||||
* @param Stream $stream
|
||||
* @param Block $block
|
||||
* @param array $sectors
|
||||
*/
|
||||
public function __construct(MPQFile $file, Stream $stream, Block $block, array $sectors) {
|
||||
$this->file = $file;
|
||||
$this->stream = $stream;
|
||||
$this->block = $block;
|
||||
|
||||
$this->sectors = array();
|
||||
$c = count($sectors) - 1;
|
||||
for ($i = 0; $i < $c; $i++) {
|
||||
$this->sectors[] = new Sector($i, $sectors[$i], $sectors[$i + 1]);
|
||||
}
|
||||
|
||||
$this->position = 0;
|
||||
$this->buffer = NULL;
|
||||
$this->currentSector = $this->sectors[0];
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public function close() {
|
||||
$this->stream->close();
|
||||
}
|
||||
|
||||
public function readByte() {
|
||||
return $this->readBytes(1);
|
||||
}
|
||||
|
||||
public function readBytes($bytes) {
|
||||
if($this->position >= $this->block->getSize()) {
|
||||
return false;
|
||||
}
|
||||
if(($this->position + $bytes) > $this->block->getSize()) {
|
||||
$bytes = $this->block->getSize() - $this->position;
|
||||
}
|
||||
|
||||
if($this->buffer == NULL) {
|
||||
$this->buffer = $this->readSector($this->currentSector);
|
||||
} else if($this->positionInSector >= strlen($this->buffer)) {
|
||||
$this->currentSector = $this->sectors[$this->currentSector->getIndex() + 1];
|
||||
$this->buffer = $this->readSector($this->currentSector);
|
||||
$this->positionInSector = 0;
|
||||
}
|
||||
|
||||
$data = substr(
|
||||
$this->buffer,
|
||||
$this->positionInSector,
|
||||
$bytes
|
||||
);
|
||||
$this->position += strlen($data);
|
||||
$this->positionInSector += strlen($data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function seek($position) {
|
||||
if($this->block->isCompressed()) {
|
||||
throw new \RuntimeException("Seek is not supported on compressed streams");
|
||||
}
|
||||
$this->position = $position;
|
||||
}
|
||||
|
||||
public function skip($bytes) {
|
||||
if($this->block->isCompressed()) {
|
||||
throw new \RuntimeException("Seek is not supported on compressed streams");
|
||||
}
|
||||
$this->position += $bytes;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
private function readSector(Sector $sector) {
|
||||
$this->stream->seek($this->file->getUserData()->getHeaderOffset()
|
||||
+ $this->block->getFilePos()
|
||||
+ $sector->getStart());
|
||||
|
||||
$compressedStream = $this->createCompressedStream();
|
||||
return $compressedStream->readBytes($sector->getLength());
|
||||
}
|
||||
|
||||
private function createCompressedStream() {
|
||||
$stream = $this->stream;
|
||||
if($this->block->isCompressed()) {
|
||||
$parser = new BinaryStreamParser($this->stream);
|
||||
$compressionType = $parser->readByte();
|
||||
switch ($compressionType) {
|
||||
case 0x10:
|
||||
return new CompressedStream($stream, new BZIPCompression());
|
||||
break;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
129
src/Stream/Block/Sector.php
Normal file
129
src/Stream/Block/Sector.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream\Block;
|
||||
|
||||
class Sector {
|
||||
|
||||
/**
|
||||
* The sector index
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $index;
|
||||
|
||||
/**
|
||||
* The sector starting position (relative to the parent block)
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $start;
|
||||
|
||||
/**
|
||||
* The sector ending position (relative to the parent block)
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $end;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sector constructor.
|
||||
* @param $index integer the sector index
|
||||
* @param $start integer the sector starting position (relative to the parent block)
|
||||
* @param $end integer the sector ending position (relative to the parent block)
|
||||
*/
|
||||
public function __construct($index, $start, $end) {
|
||||
$this->index = $index;
|
||||
$this->start = $start;
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getIndex() {
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStart() {
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getEnd() {
|
||||
return $this->end;
|
||||
}
|
||||
|
||||
public function getLength() {
|
||||
return $this->end - $this->start;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public function intersectionBegin($start, $length) {
|
||||
if($start < $this->start) {
|
||||
return $this->start;
|
||||
}
|
||||
return $start - $this->start;
|
||||
}
|
||||
|
||||
public function intersectionEnd($start, $length) {
|
||||
if(($start + $length) > $this->end) {
|
||||
return $this->getLength();
|
||||
}
|
||||
return $length;
|
||||
}
|
||||
|
||||
public function contains($start, $length) {
|
||||
if($start >= $this->start) {
|
||||
if($start <= ($this->end)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function fullyContains($start, $length) {
|
||||
if($start >= $this->start) {
|
||||
if(($start + $length) <= ($this->end)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
89
src/Stream/CompressedStream.php
Normal file
89
src/Stream/CompressedStream.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream;
|
||||
|
||||
use Rogiel\MPQ\Compression\Compression;
|
||||
|
||||
class CompressedStream implements Stream{
|
||||
|
||||
/**
|
||||
* @var Stream
|
||||
*/
|
||||
private $stream;
|
||||
|
||||
/**
|
||||
* @var Compression
|
||||
*/
|
||||
private $compression;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public function __construct(Stream $stream, Compression $compression) {
|
||||
$this->stream = $stream;
|
||||
$this->compression = $compression;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function close() {
|
||||
$this->stream->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readByte() {
|
||||
return $this->readBytes(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readBytes($bytes) {
|
||||
return $this->compression->decompress($this->stream->readBytes($bytes), $bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function seek($position) {
|
||||
$this->stream->seek($position);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function skip($position) {
|
||||
$this->stream->skip($position);
|
||||
}
|
||||
|
||||
}
|
||||
110
src/Stream/EncryptedStream.php
Normal file
110
src/Stream/EncryptedStream.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream;
|
||||
|
||||
use Rogiel\MPQ\Encryption\Encryption;
|
||||
|
||||
class EncryptedStream implements Stream{
|
||||
|
||||
/**
|
||||
* @var Stream
|
||||
*/
|
||||
private $stream;
|
||||
|
||||
/**
|
||||
* @var Encryption
|
||||
*/
|
||||
private $encryption;
|
||||
|
||||
private $buffer;
|
||||
private $bufferPointer;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public function __construct(Stream $stream, Encryption $encryption) {
|
||||
$this->stream = $stream;
|
||||
$this->encryption = $encryption;
|
||||
|
||||
$this->bufferPointer = 0xFF;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function close() {
|
||||
$this->stream->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readByte() {
|
||||
return $this->readBytes(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readBytes($bytes) {
|
||||
$data = '';
|
||||
$remaining = $bytes;
|
||||
|
||||
for($block = 0; $block < ceil($bytes / $this->encryption->getBlockSize()); $block++) {
|
||||
if($this->bufferPointer >= $this->encryption->getBlockSize()) {
|
||||
$this->bufferPointer = 0;
|
||||
$buffer = $this->stream->readBytes($this->encryption->getBlockSize());
|
||||
$this->buffer = $this->encryption->decrypt($buffer, $this->encryption->getBlockSize());
|
||||
}
|
||||
$data .= substr($this->buffer, $this->bufferPointer, $remaining);
|
||||
$this->bufferPointer += ($remaining > $this->encryption->getBlockSize() ? $this->encryption->getBlockSize() : $remaining);
|
||||
$remaining -= $this->encryption->getBlockSize();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function seek($position) {
|
||||
$this->stream->seek($position);
|
||||
$this->bufferPointer = 0xFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function skip($position) {
|
||||
$this->stream->skip($position);
|
||||
$this->bufferPointer = 0xFF;
|
||||
}
|
||||
|
||||
}
|
||||
86
src/Stream/FileStream.php
Normal file
86
src/Stream/FileStream.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream;
|
||||
|
||||
class FileStream implements Stream {
|
||||
|
||||
private $file;
|
||||
private $handle;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public function __construct($file) {
|
||||
$this->file = $file;
|
||||
$this->handle = fopen($file, 'r');
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function close() {
|
||||
fclose($this->handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readByte() {
|
||||
return fread($this->handle, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readBytes($bytes) {
|
||||
return fread($this->handle, $bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function seek($position) {
|
||||
fseek($this->handle, $position);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function skip($position) {
|
||||
fseek($this->handle, $position, SEEK_CUR);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public function __clone() {
|
||||
return new FileStream($this->file);
|
||||
}
|
||||
|
||||
}
|
||||
80
src/Stream/MemoryStream.php
Normal file
80
src/Stream/MemoryStream.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream;
|
||||
|
||||
class MemoryStream implements Stream {
|
||||
|
||||
private $data;
|
||||
private $pointer;
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
public function __construct($data) {
|
||||
$this->data = $data;
|
||||
$this->pointer = 0;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function close() {}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readByte() {
|
||||
return $this->readBytes(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readBytes($bytes) {
|
||||
$data = substr($this->data, $this->pointer, $bytes);
|
||||
$this->pointer += strlen($data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function seek($position) {
|
||||
$this->pointer = $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function skip($position) {
|
||||
$this->pointer += $position;
|
||||
}
|
||||
|
||||
}
|
||||
72
src/Stream/Parser/BinaryStreamParser.php
Normal file
72
src/Stream/Parser/BinaryStreamParser.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream\Parser;
|
||||
|
||||
|
||||
use Rogiel\MPQ\Stream\Stream;
|
||||
|
||||
class BinaryStreamParser {
|
||||
private $stream;
|
||||
|
||||
/**
|
||||
* BinaryStreamParser constructor.
|
||||
* @param Stream $stream
|
||||
*/
|
||||
public function __construct(Stream $stream) {
|
||||
$this->stream = $stream;
|
||||
}
|
||||
|
||||
public function seek($position) {
|
||||
$this->stream->seek($position);
|
||||
}
|
||||
|
||||
public function skip($bytes) {
|
||||
$this->stream->skip($bytes);
|
||||
}
|
||||
|
||||
// read little endian 32-bit integer
|
||||
public function readUInt32() {
|
||||
$t = unpack("V", $this->stream->readBytes(4));
|
||||
return $t[1];
|
||||
}
|
||||
|
||||
public function readUInt16() {
|
||||
$t = unpack("v", $this->stream->readBytes(2));
|
||||
return $t[1];
|
||||
}
|
||||
public function readByte() {
|
||||
$t = unpack("C", $this->stream->readBytes(1));
|
||||
return $t[1];
|
||||
}
|
||||
|
||||
public function readBytes($size) {
|
||||
return $this->stream->readBytes($size);
|
||||
}
|
||||
|
||||
}
|
||||
67
src/Stream/Stream.php
Normal file
67
src/Stream/Stream.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2016, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
namespace Rogiel\MPQ\Stream;
|
||||
|
||||
interface Stream {
|
||||
|
||||
/**
|
||||
* Closes the stream
|
||||
*/
|
||||
public function close();
|
||||
|
||||
/**
|
||||
* Reads a single byte from the stream
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function readByte();
|
||||
|
||||
/**
|
||||
* Read up to "$bytes" bytes from the stream
|
||||
*
|
||||
* @param $bytes integer the maximum number of bytes to read
|
||||
* @return string a string with up to $bytes characters
|
||||
*/
|
||||
public function readBytes($bytes);
|
||||
|
||||
/**
|
||||
* Seeks the stream into the given position
|
||||
*
|
||||
* @param $position integer the position to seek to
|
||||
*/
|
||||
public function seek($position);
|
||||
|
||||
/**
|
||||
* Skips $bytes bytes in the stream
|
||||
*
|
||||
* @param $bytes integer the number of bytes to skip
|
||||
*/
|
||||
public function skip($bytes);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user