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

Reader::readUInt16()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CHMLib\Reader;
4
5
/**
6
 * Read data from a generic source.
7
 */
8
abstract class Reader
9
{
10
    /**
11
     * Set the current position.
12
     *
13
     * @param int $position
14
     *
15
     * @throws \Exception Throws an Exception in case of errors.
16
     */
17
    abstract public function setPosition($position);
18
19
    /**
20
     * Get the current position.
21
     *
22
     * @return int
23
     *
24
     * @throws \Exception Throws an Exception in case of errors.
25
     */
26
    abstract public function getPosition();
27
28
    /**
29
     * Get the total length of the data.
30
     *
31
     * @return int
32
     */
33
    abstract public function getLength();
34
35
    /**
36
     * Read a fixed number of bytes as a raw string.
37
     *
38
     * @param int $length The number of bytes to read.
39
     *
40
     * @return string
41
     *
42
     * @throws \Exception Throws an Exception in case of errors.
43
     */
44
    abstract public function readString($length);
45
46
    /**
47
     * Read a fixed number of bytes and return it as a byte array.
48
     *
49
     * @param int $length The number of bytes to read.
50
     *
51
     * @return int[]
52
     *
53
     * @throws \Exception Throws an Exception in case of errors.
54
     */
55 1
    public function readBytes($length)
56
    {
57 1
        $data = $this->readString($length);
58
59
        switch ($length) {
60 1
            case 0:
61
                $result = array();
62
                break;
63 1
            case 1:
64 1
                $result = array(ord($data[0]));
65 1
                break;
66
            default:
67
                $result = unpack('C*', $data);
68
                break;
69
        }
70
71 1
        return $result;
72
    }
73
74
    /**
75
     * Read a byte.
76
     *
77
     * @return int
78
     *
79
     * @throws \Exception Throws an Exception in case of errors.
80
     */
81 1
    public function readByte()
82
    {
83 1
        $bytes = $this->readBytes(1);
84
85 1
        return $bytes[0];
86
    }
87
88
    /**
89
     * Read an unsigned 16-bit integer (little endian).
90
     *
91
     * @return int
92
     *
93
     * @throws \Exception Throws an Exception in case of errors.
94
     */
95 23
    public function readUInt16()
96
    {
97 23
        $chunk = unpack('v', $this->readString(2));
98
99 23
        return array_pop($chunk);
100
    }
101
102
    /**
103
     * Read an unsigned 32-bit integer (little endian).
104
     *
105
     * @return int
106
     *
107
     * @throws \Exception Throws an Exception in case of errors.
108
     */
109 1
    public function readUInt32()
110
    {
111 1
        $bytes = $this->readString(4);
112 1
        $chunk = unpack('V', $bytes);
113 1
        $int = array_pop($chunk);
114 1 View Code Duplication
        if ($int < 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
            $bits = decbin($int);
116
            $int = bindec(substr($bits, 0, -1)) * 2;
117
            if (substr($bits, -1) === '1') {
118
                $int += 1;
119
            }
120
        }
121
122 1
        return $int;
123
    }
124
125
    /**
126
     * Read a signed 32-bit integer (little endian).
127
     *
128
     * @return int
129
     *
130
     * @throws \Exception Throws an Exception in case of errors.
131
     */
132 1
    public function readInt32()
133
    {
134 1
        static $osIsBigEndian;
135 1
        if (!isset($osIsBigEndian)) {
136 1
            $osIsBigEndian = (pack('L', 1) === pack('N', 1)) ? true : false;
137 1
        }
138 1
        $data = $this->readString(4);
139 1
        if ($osIsBigEndian) {
140
            $data = strrev($data);
141
        }
142 1
        $chunk = unpack('l', $data);
143
144 1
        return array_pop($chunk);
145
    }
146
147
    /**
148
     * Read an unsigned 64-bit integer (little endian).
149
     *
150
     * @return int|float
151
     *
152
     * @throws \Exception Throws an Exception in case of errors.
153
     */
154 1
    public function readUInt64()
155
    {
156 1
        static $nativeUnpack;
157
158 1
        if (!isset($nativeUnpack)) {
159 1
            $nativeUnpack = (PHP_INT_SIZE >= 8 && version_compare(PHP_VERSION, '5.6.3') >= 0) ? true : false;
160 1
        }
161 1
        if ($nativeUnpack) {
162
            $chunk = unpack('P', $this->readString(8));
163
            $result = array_pop($chunk);
164 View Code Duplication
            if ($result < 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
                $bits = decbin($result);
166
                $result = bindec(substr($bits, 0, -1)) * 2;
167
                if (substr($bits, -1) === '1') {
168
                    $result += 1;
169
                }
170
            }
171
        } else {
172 1
            $n1 = $this->readUInt32();
173 1
            $n2 = $this->readUInt32();
174
175 1
            $result = $n2 * 0x100000000 + $n1;
176 1
            if ($result <= PHP_INT_MAX) {
177 1
                $result = (int) $result;
178 1
            }
179
        }
180
181 1
        return $result;
182
    }
183
184
    /**
185
     * Read a GUID.
186
     *
187
     * @return string
188
     *
189
     * @throws \Exception Throws an Exception in case of errors.
190
     *
191
     * @example '{5D02926A-212E-11D0-9DF9-00A0C922E6EC}'
192
     */
193 1
    public function readGUID()
194
    {
195 1
        return sprintf(
196 1
            '{%1$08X-%2$04X-%3$04X-%4$02X%5$02X-%6$02X%7$02X-%8$02X%9$02X-%10$02X%11$02X}',
197 1
            $this->readUInt32(),
198 1
            $this->readUInt16(),
199 1
            $this->readUInt16(),
200 1
            $this->readByte(),
201 1
            $this->readByte(),
202 1
            $this->readByte(),
203 1
            $this->readByte(),
204 1
            $this->readByte(),
205 1
            $this->readByte(),
206 1
            $this->readByte(),
207 1
            $this->readByte()
208 1
        );
209
    }
210
211
    /**
212
     * Read a compressed unsigned 32-bit integer (little endian).
213
     *
214
     * @return number
215
     */
216 1
    public function readCompressedUInt32()
217
    {
218 1
        $result = 0;
219 1
        for (;;) {
220 1
            $result <<= 7;
221 1
            $byte = $this->readByte();
222 1
            if ($byte < 0x80) {
223 1
                $result += $byte;
224 1
                break;
225
            }
226 1
            $result += $byte & 0x7f;
227 1
        }
228
229 1
        return $result;
230
    }
231
}
232