Skip to content

Commit db7ad1f

Browse files
author
cradu
committed
Added more tests
1 parent e09608c commit db7ad1f

File tree

7 files changed

+96
-13
lines changed

7 files changed

+96
-13
lines changed

README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,75 @@
1-
PHP ISO file Library. A library for reading ISO files using PHP.
1+
# PHP ISO Library
2+
3+
PHP Library used to read metadata from ISO files based on [php-iso-file](https://github.com/php-classes/php-iso-file)
4+
5+
This library follows the [ECMA-119](https://www.ecma-international.org/wp-content/uploads/ECMA-119_4th_edition_june_2019.pdf) standard.
6+
7+
Basic concepts
8+
-----
9+
- `IsoFile` - main ISO file object, contains one more descriptors
10+
- `Descriptor` - descriptor object which can have one of the following types defined in `Type` class:
11+
- `BOOT_RECORD_DESC` : `Boot` object
12+
- `PRIMARY_VOLUME_DESC` : `PrimaryVolume` object
13+
- `SUPPLEMENTARY_VOLUME_DESC` : `SupplementaryVolume` object
14+
- `PARTITION_VOLUME_DESC` : `Partition` object
15+
- `TERMINATOR_DESC` : `Terminator` object
16+
- upon initialization of the `IsoFile` object, the descriptors will be populated automatically
17+
- Volume descriptors contain path table inside which can be loaded using `loadTable`
18+
- `PathTableRecord` - object which contains the record information for a file/directory
19+
- Each class contains various properties which can be used to interact with them, most of them `public`
20+
21+
Installation
22+
------------
23+
24+
This class can easily be installed via [Composer](https://getcomposer.org):
25+
`composer require indy2kro/php-iso`
26+
27+
28+
Usage
29+
-----
30+
```php
31+
<?php
32+
33+
use PhpIso\IsoFile;
34+
use PhpIso\Descriptor\Type;
35+
use PhpIso\Descriptor\PrimaryVolume;
36+
use PhpIso\FileDirectory;
37+
use PhpIso\PathTableRecord;
38+
39+
$isoFilePath = 'test.iso';
40+
41+
$isoFile = new IsoFile($isoFilePath);
42+
43+
// you can process each descriptor using $isoFile->descriptors directly
44+
45+
/** @var PrimaryVolume $primaryVolumeDescriptor */
46+
$primaryVolumeDescriptor = $isoFile->descriptors[Type::PRIMARY_VOLUME_DESC];
47+
48+
// get the path table
49+
$pathTable = $primaryVolumeDescriptor->loadTable($isoFile);
50+
51+
$paths = [];
52+
53+
/** @var PathTableRecord $pathRecord */
54+
foreach ($pathTable as $pathRecord) {
55+
$currentPath = $pathRecord->getFullPath($pathTable);
56+
57+
$paths[$currentPath] = [];
58+
59+
// check extents
60+
$extents = $pathRecord->loadExtents($isoFile, $primaryVolumeDescriptor->blockSize);
61+
62+
if ($extents !== false) {
63+
/** @var FileDirectory $extentRecord */
64+
foreach ($extents as $extentRecord) {
65+
$path = $extentRecord->fileId;
66+
if ($extentRecord->isDirectory()) {
67+
$path .= '/';
68+
}
69+
$paths[$currentPath][] = $path;
70+
}
71+
}
72+
}
73+
74+
print_r($paths);
75+
```

src/Descriptor.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
namespace PhpIso;
66

7-
use PhpIso\Descriptor\Type;
8-
97
abstract class Descriptor
108
{
11-
public string $name = '';
12-
protected int $type = Type::NOT_SET_DESC;
9+
public string $name;
10+
protected int $type;
1311

1412
/**
1513
* @param array<int, mixed>|null $bytes

src/Descriptor/Boot.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Boot extends Descriptor
2121
* An identification of the boot system specified in the Boot System Use field of the Boot Record.
2222
*/
2323
public string $bootId = '';
24+
2425
public string $name = 'Boot volume descriptor';
2526

2627
protected int $type = Type::BOOT_RECORD_DESC;

src/Descriptor/Reader.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public function __construct(protected IsoFile $isoFile)
1616

1717
public function read(): ?Descriptor
1818
{
19-
$offset = 0;
20-
2119
$string = $this->isoFile->read(2048);
2220

2321
if ($string === false) {

src/Descriptor/Type.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
class Type
88
{
9-
public const NOT_SET_DESC = -1;
109
public const BOOT_RECORD_DESC = 0;
1110
public const PRIMARY_VOLUME_DESC = 1;
1211
public const SUPPLEMENTARY_VOLUME_DESC = 2;

src/FileDirectory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,13 @@ public static function loadExtentsSt(IsoFile $isoFile, int $blockSize, int $loca
267267

268268
$offset = 1;
269269
$fdDesc = new self();
270-
$Extents = [];
270+
$extents = [];
271271

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

277-
return $Extents;
277+
return $extents;
278278
}
279279
}

src/IsoFile.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class IsoFile
1515
public array $descriptors = [];
1616

1717
/**
18-
* @var resource
18+
* @var ?resource
1919
*/
2020
protected mixed $fileHandle;
2121

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

3434
public function seek(int $offset, int $whence = SEEK_SET): int
3535
{
36+
if ($this->fileHandle === null) {
37+
return -1;
38+
}
39+
3640
return fseek($this->fileHandle, $offset, $whence);
3741
}
3842

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

49+
if ($this->fileHandle === null) {
50+
return false;
51+
}
52+
4553
return fread($this->fileHandle, $length);
4654
}
4755

@@ -81,6 +89,11 @@ protected function openFile(): void
8189

8290
protected function closeFile(): void
8391
{
92+
if ($this->fileHandle === null) {
93+
return;
94+
}
95+
8496
fclose($this->fileHandle);
97+
$this->fileHandle = null;
8598
}
8699
}

0 commit comments

Comments
 (0)