Skip to content

Commit

Permalink
Added more validations
Browse files Browse the repository at this point in the history
  • Loading branch information
indy2kro committed Jan 12, 2025
1 parent 547606d commit 231e8e9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
colors="true"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
failOnDeprecation="true"
failOnEmptyTestSuite="true"
failOnIncomplete="true"
failOnNotice="true"
failOnRisky="true"
failOnSkipped="false"
failOnWarning="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.3/phpunit.xsd">
<source>
<include>
Expand Down
10 changes: 10 additions & 0 deletions src/Descriptor/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ public function read(): ?Descriptor
}

$offset = 1;

if (!isset($bytes[$offset])) {
throw new Exception('Failed to read buffer entry ' . $offset);
}

$type = $bytes[$offset];
$offset++;
$stdId = Buffer::getString($bytes, 5, $offset);

if (!isset($bytes[$offset])) {
throw new Exception('Failed to read buffer entry ' . $offset);
}

$version = $bytes[$offset];
$offset++;

Expand Down
45 changes: 45 additions & 0 deletions src/Util/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpIso\Util;

use PhpIso\Exception;

class Buffer
{
/**
Expand All @@ -28,6 +30,9 @@ public static function getString(array &$buffer, int $length, int &$offset = 0,
{
$string = '';
for ($i = $offset; $i < $offset + $length; $i++) {
if (!isset($buffer[$i])) {
throw new Exception('Failed to read buffer entry ' . $i);
}
$string .= chr($buffer[$i]);
}

Expand Down Expand Up @@ -68,6 +73,9 @@ public static function getBytes(array &$buffer, int $length, int &$offset = 0):
{
$datas = '';
for ($i = $offset; $i < $offset + $length; $i++) {
if (!isset($buffer[$i])) {
throw new Exception('Failed to read buffer entry ' . $i);
}
$datas .= $buffer[$i];
}

Expand All @@ -89,6 +97,12 @@ public static function readBBO(array &$buffer, int $length, int &$offset = 0): i
$len = $length / 2;

for ($i = 0; $i < $len; $i++) {
if (!isset($buffer[$offset + ($len - 1 - $i)])) {
throw new Exception('Failed to read buffer entry ' . ($offset + ($len - 1 - $i)));
}
if (!isset($buffer[$offset + $len + $i])) {
throw new Exception('Failed to read buffer entry ' . ($offset + $len + $i));
}
$n1 += $buffer[$offset + ($len - 1 - $i)];
$n2 += $buffer[$offset + $len + $i];

Expand All @@ -115,6 +129,10 @@ public static function readLSB(array &$buffer, int $length, int &$offset = 0): i
{
$lsb = 0;
for ($i = 0; $i < $length; $i++) {
if (!isset($buffer[$offset + ($length - 1 - $i)])) {
throw new Exception('Failed to read buffer entry ' . ($offset + ($length - 1 - $i)));
}

$lsb += $buffer[$offset + $length - 1 - $i];

if ($i + 1 < $length) {
Expand All @@ -135,6 +153,9 @@ public static function readMSB(array &$buffer, int $length, int &$offset = 0): i
{
$msb = 0;
for ($i = 0; $i < $length; $i++) {
if (!isset($buffer[$offset + $i])) {
throw new Exception('Failed to read buffer entry ' . ($offset + $i));
}
$msb += $buffer[$offset + $i];

if ($i + 1 < $length) {
Expand All @@ -155,6 +176,14 @@ public static function readInt16(array &$buffer, int &$offset = 0): int
{
$output = 0;

if (!isset($buffer[$offset + 0])) {
throw new Exception('Failed to read buffer entry ' . ($offset + 0));
}

if (!isset($buffer[$offset + 1])) {
throw new Exception('Failed to read buffer entry ' . ($offset + 1));
}

$output += $buffer[$offset + 0] << 8;
$output += $buffer[$offset + 1];

Expand All @@ -171,6 +200,22 @@ public static function readInt32(array &$buffer, int &$offset = 0): int
{
$output = 0;

if (!isset($buffer[$offset + 0])) {
throw new Exception('Failed to read buffer entry ' . ($offset + 0));
}

if (!isset($buffer[$offset + 1])) {
throw new Exception('Failed to read buffer entry ' . ($offset + 1));
}

if (!isset($buffer[$offset + 2])) {
throw new Exception('Failed to read buffer entry ' . ($offset + 2));
}

if (!isset($buffer[$offset + 3])) {
throw new Exception('Failed to read buffer entry ' . ($offset + 3));
}

$output += $buffer[$offset + 0] << 24;
$output += $buffer[$offset + 1] << 16;
$output += $buffer[$offset + 2] << 8;
Expand Down
14 changes: 7 additions & 7 deletions tests/IsoFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public function testConstructorFileDoesNotExist(): void
new IsoFile($testFile);
}

// public function testConstructorInvalidFile(): void
// {
// $testFile = dirname(__FILE__, 2) . '/fixtures/invalid.iso';
//
// $this->expectException(Exception::class);
// new IsoFile($testFile);
// }
public function testConstructorInvalidFile(): void
{
$testFile = dirname(__FILE__, 2) . '/fixtures/invalid.iso';

$this->expectException(Exception::class);
new IsoFile($testFile);
}

public function testConstructorExistingFile(): void
{
Expand Down

0 comments on commit 231e8e9

Please sign in to comment.