1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpIso\Descriptor; |
6
|
|
|
|
7
|
|
|
use PhpIso\Descriptor; |
8
|
|
|
use PhpIso\IsoFile; |
9
|
|
|
use PhpIso\Util\Buffer; |
10
|
|
|
|
11
|
|
|
class Reader |
12
|
|
|
{ |
13
|
20 |
|
public function __construct(protected IsoFile $isoFile) |
14
|
|
|
{ |
15
|
20 |
|
} |
16
|
|
|
|
17
|
20 |
|
public function read(): ?Descriptor |
18
|
|
|
{ |
19
|
20 |
|
$string = $this->isoFile->read(2048); |
20
|
|
|
|
21
|
20 |
|
if ($string === false) { |
22
|
|
|
return null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** @var array<int, int>|false $bytes */ |
26
|
20 |
|
$bytes = unpack('C*', $string); |
27
|
|
|
|
28
|
20 |
|
if ($bytes === false) { |
29
|
|
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
20 |
|
$offset = 1; |
33
|
|
|
|
34
|
20 |
|
if (! isset($bytes[$offset])) { |
35
|
1 |
|
throw new Exception('Failed to read buffer entry ' . $offset); |
36
|
|
|
} |
37
|
|
|
|
38
|
19 |
|
$type = $bytes[$offset]; |
39
|
19 |
|
$offset++; |
40
|
19 |
|
$stdId = Buffer::getString($bytes, 5, $offset); |
41
|
|
|
|
42
|
19 |
|
if (! isset($bytes[$offset])) { |
43
|
|
|
throw new Exception('Failed to read buffer entry ' . $offset); |
44
|
|
|
} |
45
|
|
|
|
46
|
19 |
|
$version = $bytes[$offset]; |
47
|
19 |
|
$offset++; |
48
|
|
|
|
49
|
|
|
// Check for UDF-specific descriptors |
50
|
19 |
|
if ($type === Type::BOOT_RECORD_DESC) { |
51
|
12 |
|
$type = match ($stdId) { |
52
|
18 |
|
'CD001' => Type::BOOT_RECORD_DESC, |
53
|
16 |
|
UdfType::BEA01 => Type::UDF_BEA_VOLUME_DESC, |
54
|
16 |
|
UdfType::NSR02 => Type::UDF_NSR2_VOLUME_DESC, |
55
|
16 |
|
UdfType::NSR03 => Type::UDF_NSR3_VOLUME_DESC, |
56
|
16 |
|
UdfType::TEA01 => Type::UDF_TEA_VOLUME_DESC, |
57
|
6 |
|
default => throw new Exception('Failed to detect UDF'), |
58
|
12 |
|
}; |
59
|
|
|
} |
60
|
|
|
|
61
|
19 |
|
$descriptor = Factory::create($type, $stdId, $version, $bytes); |
62
|
19 |
|
$descriptor->init($this->isoFile, $offset); |
63
|
|
|
|
64
|
19 |
|
return $descriptor; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|