Completed
Push — master ( 5cbbf9...fe28cf )
by Michele
05:41 queued 01:59
created

Entry::getOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 1
    public function __construct(CHM $chm)
88
    {
89 1
        $reader = $chm->getReader();
90 1
        $this->chm = $chm;
91 1
        $stringLength = $reader->readCompressedUInt32();
92 1
        $this->path = $reader->readString($stringLength);
93 1
        $this->contentSectionIndex = $reader->readCompressedUInt32();
94 1
        $this->offset = $reader->readCompressedUInt32();
95 1
        $this->length = $reader->readCompressedUInt32();
96 1
        $pathLength = strlen($this->path);
97 1
        if (substr($this->path, -1) === '/') {
98 1
            $this->type = static::TYPE_DIRECTORY;
99 1
        } elseif ($this->path[0] === '/') {
100 1
            if ($pathLength > 1 && ($this->path[1] === '#' || $this->path[1] === '$')) {
101 1
                $this->type = static::TYPE_SPECIAL_FILE;
102 1
            } else {
103 1
                $this->type = static::TYPE_FILE;
104
            }
105 1
        } else {
106 1
            $this->type = static::TYPE_METADATA;
107
        }
108 1
    }
109
110
    /**
111
     * Get the path of this entry.
112
     *
113
     * @return string
114
     */
115 2
    public function getPath()
116
    {
117 2
        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 1
    public function getContentSectionIndex()
126
    {
127 1
        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 1
    public function getOffset()
136
    {
137 1
        return $this->offset;
138
    }
139
140
    /**
141
     * Get the length of the entry data after decompression (if appropriate).
142
     *
143
     * @return int
144
     */
145 1
    public function getLength()
146
    {
147 1
        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 1
    {
167
        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 1
    public function getType()
196
    {
197 1
        return $this->type;
198
    }
199
200
    /**
201
     * Get the contents of this entry.
202
     *
203
     * @return string
204
     *
205
     * @throws Exception Throws an Exception in case of errors.
206
     */
207 466
    public function getContents()
208
    {
209 466
        $section = $this->chm->getSectionByIndex($this->contentSectionIndex);
210 466
        if ($section === null) {
211
            throw new Exception("The CHM file does not contain a data section with index {$this->contentSectionIndex}");
212
        }
213
214 466
        return $section->getContents($this->offset, $this->length);
215
    }
216
}
217