|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CHMLib; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Represent an entry (file/directory) contained in a CHM file. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Entry |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Entry type: directory. |
|
14
|
|
|
* |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
const TYPE_DIRECTORY = 0x1; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Entry type: normal file. |
|
21
|
|
|
* |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
const TYPE_FILE = 0x2; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Entry type: special file. |
|
28
|
|
|
* |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
const TYPE_SPECIAL_FILE = 0x4; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Entry type: meta data. |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
const TYPE_METADATA = 0x8; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The parent CHM file. |
|
42
|
|
|
* |
|
43
|
|
|
* @var CHM |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $chm; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The path of this entry. |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $path; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The index of the content section that contains the data of this entry. |
|
56
|
|
|
* |
|
57
|
|
|
* @var int |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $contentSectionIndex; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The offset of the entry data from the beginning of the content section this entry is in, after the section has been decompressed (if appropriate). |
|
63
|
|
|
* |
|
64
|
|
|
* @var int |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $offset; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The length of the entry data after decompression (if appropriate). |
|
70
|
|
|
* |
|
71
|
|
|
* @var int |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $length; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The type of this entry (one of the static::TYPE_... constants). |
|
77
|
|
|
* |
|
78
|
|
|
* @var int |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $type; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Initializes the instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @param CHM $chm The parent CHM file. |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function __construct(CHM $chm) |
|
88
|
|
|
{ |
|
89
|
4 |
|
$reader = $chm->getReader(); |
|
90
|
4 |
|
$this->chm = $chm; |
|
91
|
4 |
|
$stringLength = $reader->readCompressedUInt32(); |
|
92
|
4 |
|
$this->path = $reader->readString($stringLength); |
|
93
|
4 |
|
$this->contentSectionIndex = $reader->readCompressedUInt32(); |
|
94
|
4 |
|
$this->offset = $reader->readCompressedUInt32(); |
|
95
|
4 |
|
$this->length = $reader->readCompressedUInt32(); |
|
96
|
4 |
|
$pathLength = strlen($this->path); |
|
97
|
4 |
|
if (substr($this->path, -1) === '/') { |
|
98
|
4 |
|
$this->type = static::TYPE_DIRECTORY; |
|
99
|
4 |
|
} elseif ($this->path[0] === '/') { |
|
100
|
4 |
|
if ($pathLength > 1 && ($this->path[1] === '#' || $this->path[1] === '$')) { |
|
101
|
4 |
|
$this->type = static::TYPE_SPECIAL_FILE; |
|
102
|
|
|
} else { |
|
103
|
4 |
|
$this->type = static::TYPE_FILE; |
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
4 |
|
$this->type = static::TYPE_METADATA; |
|
107
|
|
|
} |
|
108
|
4 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the path of this entry. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
7 |
|
public function getPath() |
|
116
|
|
|
{ |
|
117
|
7 |
|
return $this->path; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the index of the content section that contains the data of this entry. |
|
122
|
|
|
* |
|
123
|
|
|
* @return int |
|
124
|
|
|
*/ |
|
125
|
4 |
|
public function getContentSectionIndex() |
|
126
|
|
|
{ |
|
127
|
4 |
|
return $this->contentSectionIndex; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the offset from the beginning of the content section this entry is in, after the section has been decompressed (if appropriate). |
|
132
|
|
|
* |
|
133
|
|
|
* @return int |
|
134
|
|
|
*/ |
|
135
|
4 |
|
public function getOffset() |
|
136
|
|
|
{ |
|
137
|
4 |
|
return $this->offset; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the length of the entry data after decompression (if appropriate). |
|
142
|
|
|
* |
|
143
|
|
|
* @return int |
|
144
|
|
|
*/ |
|
145
|
4 |
|
public function getLength() |
|
146
|
|
|
{ |
|
147
|
4 |
|
return $this->length; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Is this a directory entry? |
|
152
|
|
|
* |
|
153
|
|
|
* @return bool |
|
154
|
|
|
*/ |
|
155
|
|
|
public function isDirectory() |
|
156
|
|
|
{ |
|
157
|
|
|
return (bool) ($this->type === static::TYPE_DIRECTORY); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Is this a normal file entry? |
|
162
|
|
|
* |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function isFile() |
|
166
|
|
|
{ |
|
167
|
1 |
|
return (bool) ($this->type === static::TYPE_FILE); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Is this a special file entry? |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
public function isSpecialFile() |
|
176
|
|
|
{ |
|
177
|
|
|
return (bool) ($this->type === static::TYPE_SPECIAL_FILE); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Is this a meta-data entry? |
|
182
|
|
|
* |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
public function isMetaData() |
|
186
|
|
|
{ |
|
187
|
|
|
return (bool) ($this->type === static::TYPE_METADATA); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Get the type of this entry (one of the static::TYPE_... constants). |
|
192
|
|
|
* |
|
193
|
|
|
* @return int |
|
194
|
|
|
*/ |
|
195
|
3 |
|
public function getType() |
|
196
|
|
|
{ |
|
197
|
3 |
|
return $this->type; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Get the contents of this entry. |
|
202
|
|
|
* |
|
203
|
|
|
* @throws Exception Throws an Exception in case of errors. |
|
204
|
|
|
* |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
491 |
|
public function getContents() |
|
208
|
|
|
{ |
|
209
|
491 |
|
$section = $this->chm->getSectionByIndex($this->contentSectionIndex); |
|
210
|
491 |
|
if ($section === null) { |
|
211
|
|
|
throw new Exception("The CHM file does not contain a data section with index {$this->contentSectionIndex}"); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
491 |
|
return $section->getContents($this->offset, $this->length); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|