Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cradu committed Jan 15, 2025
1 parent e09608c commit db7ad1f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 13 deletions.
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
PHP ISO file Library. A library for reading ISO files using PHP.
# PHP ISO Library

PHP Library used to read metadata from ISO files based on [php-iso-file](https://github.com/php-classes/php-iso-file)

This library follows the [ECMA-119](https://www.ecma-international.org/wp-content/uploads/ECMA-119_4th_edition_june_2019.pdf) standard.

Basic concepts
-----
- `IsoFile` - main ISO file object, contains one more descriptors
- `Descriptor` - descriptor object which can have one of the following types defined in `Type` class:
- `BOOT_RECORD_DESC` : `Boot` object
- `PRIMARY_VOLUME_DESC` : `PrimaryVolume` object
- `SUPPLEMENTARY_VOLUME_DESC` : `SupplementaryVolume` object
- `PARTITION_VOLUME_DESC` : `Partition` object
- `TERMINATOR_DESC` : `Terminator` object
- upon initialization of the `IsoFile` object, the descriptors will be populated automatically
- Volume descriptors contain path table inside which can be loaded using `loadTable`
- `PathTableRecord` - object which contains the record information for a file/directory
- Each class contains various properties which can be used to interact with them, most of them `public`

Installation
------------

This class can easily be installed via [Composer](https://getcomposer.org):
`composer require indy2kro/php-iso`


Usage
-----
```php
<?php

use PhpIso\IsoFile;
use PhpIso\Descriptor\Type;
use PhpIso\Descriptor\PrimaryVolume;
use PhpIso\FileDirectory;
use PhpIso\PathTableRecord;

$isoFilePath = 'test.iso';

$isoFile = new IsoFile($isoFilePath);

// you can process each descriptor using $isoFile->descriptors directly

/** @var PrimaryVolume $primaryVolumeDescriptor */
$primaryVolumeDescriptor = $isoFile->descriptors[Type::PRIMARY_VOLUME_DESC];

// get the path table
$pathTable = $primaryVolumeDescriptor->loadTable($isoFile);

$paths = [];

/** @var PathTableRecord $pathRecord */
foreach ($pathTable as $pathRecord) {
$currentPath = $pathRecord->getFullPath($pathTable);

$paths[$currentPath] = [];

// check extents
$extents = $pathRecord->loadExtents($isoFile, $primaryVolumeDescriptor->blockSize);

if ($extents !== false) {
/** @var FileDirectory $extentRecord */
foreach ($extents as $extentRecord) {
$path = $extentRecord->fileId;
if ($extentRecord->isDirectory()) {
$path .= '/';
}
$paths[$currentPath][] = $path;
}
}
}

print_r($paths);
```
6 changes: 2 additions & 4 deletions src/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace PhpIso;

use PhpIso\Descriptor\Type;

abstract class Descriptor
{
public string $name = '';
protected int $type = Type::NOT_SET_DESC;
public string $name;
protected int $type;

/**
* @param array<int, mixed>|null $bytes
Expand Down
1 change: 1 addition & 0 deletions src/Descriptor/Boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Boot extends Descriptor
* An identification of the boot system specified in the Boot System Use field of the Boot Record.
*/
public string $bootId = '';

public string $name = 'Boot volume descriptor';

protected int $type = Type::BOOT_RECORD_DESC;
Expand Down
2 changes: 0 additions & 2 deletions src/Descriptor/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public function __construct(protected IsoFile $isoFile)

public function read(): ?Descriptor
{
$offset = 0;

$string = $this->isoFile->read(2048);

if ($string === false) {
Expand Down
1 change: 0 additions & 1 deletion src/Descriptor/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class Type
{
public const NOT_SET_DESC = -1;
public const BOOT_RECORD_DESC = 0;
public const PRIMARY_VOLUME_DESC = 1;
public const SUPPLEMENTARY_VOLUME_DESC = 2;
Expand Down
8 changes: 4 additions & 4 deletions src/FileDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ public static function loadExtentsSt(IsoFile $isoFile, int $blockSize, int $loca

$offset = 1;
$fdDesc = new self();
$Extents = [];
$extents = [];

while ($fdDesc->Init($bytes, $offset, $supplementary) !== false) {
$Extents[] = $fdDesc;
while ($fdDesc->init($bytes, $offset, $supplementary) !== false) {
$extents[] = $fdDesc;
$fdDesc = new self();
}

return $Extents;
return $extents;
}
}
15 changes: 14 additions & 1 deletion src/IsoFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class IsoFile
public array $descriptors = [];

/**
* @var resource
* @var ?resource
*/
protected mixed $fileHandle;

Expand All @@ -33,6 +33,10 @@ public function __destruct()

public function seek(int $offset, int $whence = SEEK_SET): int
{
if ($this->fileHandle === null) {
return -1;
}

return fseek($this->fileHandle, $offset, $whence);
}

Expand All @@ -42,6 +46,10 @@ public function read(int $length): string|false
return false;
}

if ($this->fileHandle === null) {
return false;
}

return fread($this->fileHandle, $length);
}

Expand Down Expand Up @@ -81,6 +89,11 @@ protected function openFile(): void

protected function closeFile(): void
{
if ($this->fileHandle === null) {
return;
}

fclose($this->fileHandle);
$this->fileHandle = null;
}
}

0 comments on commit db7ad1f

Please sign in to comment.