1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpIso\Util; |
6
|
|
|
|
7
|
|
|
use PhpIso\Exception; |
8
|
|
|
|
9
|
|
|
class Buffer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Align a number |
13
|
|
|
*/ |
14
|
4 |
|
public static function align(int $num, int $align): int |
15
|
|
|
{ |
16
|
4 |
|
$tmp = (int) ($num / $align); |
17
|
4 |
|
if ((int) ($num % $align) > 0) { |
18
|
4 |
|
$tmp++; |
19
|
|
|
} |
20
|
|
|
|
21
|
4 |
|
return $tmp * $align; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Read a string from the buffer |
26
|
|
|
* |
27
|
|
|
* @param array<int, int> $buffer |
28
|
|
|
*/ |
29
|
25 |
|
public static function getString(array &$buffer, int $length, int &$offset = 0, bool $supplementary = false): string |
30
|
|
|
{ |
31
|
25 |
|
$string = ''; |
32
|
25 |
|
for ($i = $offset; $i < $offset + $length; $i++) { |
33
|
25 |
|
if (! isset($buffer[$i])) { |
34
|
1 |
|
throw new Exception('Failed to read buffer entry ' . $i); |
35
|
|
|
} |
36
|
25 |
|
if ($supplementary || $buffer[$i] !== 0) { |
37
|
25 |
|
$string .= chr($buffer[$i]); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
24 |
|
if ($supplementary) { |
42
|
10 |
|
$string = mb_convert_encoding($string, 'UTF-8', 'UTF-16'); |
43
|
|
|
} |
44
|
|
|
|
45
|
24 |
|
$offset += $length; |
46
|
24 |
|
return $string; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Read an a-string from the buffer |
51
|
|
|
* |
52
|
|
|
* @param array<int, int> $buffer |
53
|
|
|
*/ |
54
|
18 |
|
public static function readAString(array &$buffer, int $length, int &$offset = 0, bool $supplementary = false): string |
55
|
|
|
{ |
56
|
18 |
|
return self::getString($buffer, $length, $offset); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Read a d-string from the buffer |
61
|
|
|
* |
62
|
|
|
* @param array<int, int> $buffer |
63
|
|
|
*/ |
64
|
18 |
|
public static function readDString(array &$buffer, int $length, int &$offset = 0, bool $supplementary = false): string |
65
|
|
|
{ |
66
|
18 |
|
return self::getString($buffer, $length, $offset, $supplementary); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Read datas from the buffer |
71
|
|
|
* |
72
|
|
|
* @param array<int, int> $buffer |
73
|
|
|
*/ |
74
|
19 |
|
public static function getBytes(array &$buffer, int $length, int &$offset = 0): string |
75
|
|
|
{ |
76
|
19 |
|
$datas = ''; |
77
|
19 |
|
for ($i = $offset; $i < $offset + $length; $i++) { |
78
|
19 |
|
if (! isset($buffer[$i])) { |
79
|
1 |
|
throw new Exception('Failed to read buffer entry ' . $i); |
80
|
|
|
} |
81
|
19 |
|
$datas .= $buffer[$i]; |
82
|
|
|
} |
83
|
|
|
|
84
|
18 |
|
$offset += $length; |
85
|
18 |
|
return $datas; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Read a number written in BBO (Bost Byte Order) (ex: a 4 BYTES number require 8 BYTES, 4 for LSM mode and 4 for MSB) |
90
|
|
|
* |
91
|
|
|
* @param array<int, int> $buffer |
92
|
|
|
* |
93
|
|
|
* @return int The BBO number OR -1 on error |
94
|
|
|
*/ |
95
|
18 |
|
public static function readBBO(array &$buffer, int $length, int &$offset = 0): int |
96
|
|
|
{ |
97
|
18 |
|
$n1 = 0; |
98
|
18 |
|
$n2 = 0; |
99
|
18 |
|
$len = $length / 2; |
100
|
|
|
|
101
|
18 |
|
for ($i = 0; $i < $len; $i++) { |
102
|
18 |
|
if (! isset($buffer[$offset + ($len - 1 - $i)])) { |
103
|
1 |
|
throw new Exception('Failed to read buffer entry ' . ($offset + ($len - 1 - $i))); |
104
|
|
|
} |
105
|
17 |
|
if (! isset($buffer[$offset + $len + $i])) { |
106
|
|
|
throw new Exception('Failed to read buffer entry ' . ($offset + $len + $i)); |
107
|
|
|
} |
108
|
17 |
|
$n1 += $buffer[$offset + ($len - 1 - $i)]; |
109
|
17 |
|
$n2 += $buffer[$offset + $len + $i]; |
110
|
|
|
|
111
|
17 |
|
if ($i + 1 < $len) { |
112
|
17 |
|
$n1 <<= 8; |
113
|
17 |
|
$n2 <<= 8; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
17 |
|
if ($n1 !== $n2) { |
118
|
2 |
|
return -1; |
119
|
|
|
} |
120
|
|
|
|
121
|
17 |
|
$offset += $length; |
122
|
17 |
|
return $n1; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Read a number written in LSB mode ("Less Signifient Bit" first) |
127
|
|
|
* |
128
|
|
|
* @param array<int, int> $buffer |
129
|
|
|
*/ |
130
|
20 |
|
public static function readLSB(array &$buffer, int $length, int &$offset = 0): int |
131
|
|
|
{ |
132
|
20 |
|
$lsb = 0; |
133
|
20 |
|
for ($i = 0; $i < $length; $i++) { |
134
|
20 |
|
if (! isset($buffer[$offset + ($length - 1 - $i)])) { |
135
|
2 |
|
throw new Exception('Failed to read buffer entry ' . ($offset + ($length - 1 - $i))); |
136
|
|
|
} |
137
|
|
|
|
138
|
18 |
|
$lsb += $buffer[$offset + $length - 1 - $i]; |
139
|
|
|
|
140
|
18 |
|
if ($i + 1 < $length) { |
141
|
18 |
|
$lsb <<= 8; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
18 |
|
$offset += $length; |
146
|
18 |
|
return $lsb; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Read a number written in MSB mode ("Most Signifient Bit" first) |
151
|
|
|
* |
152
|
|
|
* @param array<int, int> $buffer |
153
|
|
|
*/ |
154
|
20 |
|
public static function readMSB(array &$buffer, int $length, int &$offset = 0): int |
155
|
|
|
{ |
156
|
20 |
|
$msb = 0; |
157
|
20 |
|
for ($i = 0; $i < $length; $i++) { |
158
|
20 |
|
if (! isset($buffer[$offset + $i])) { |
159
|
2 |
|
throw new Exception('Failed to read buffer entry ' . ($offset + $i)); |
160
|
|
|
} |
161
|
19 |
|
$msb += $buffer[$offset + $i]; |
162
|
|
|
|
163
|
19 |
|
if ($i + 1 < $length) { |
164
|
19 |
|
$msb <<= 8; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
18 |
|
$offset += $length; |
169
|
18 |
|
return $msb; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Read a word (16 bits number) |
174
|
|
|
* |
175
|
|
|
* @param array<int, int> $buffer |
176
|
|
|
*/ |
177
|
6 |
|
public static function readInt16(array &$buffer, int &$offset = 0): int |
178
|
|
|
{ |
179
|
6 |
|
$output = 0; |
180
|
|
|
|
181
|
6 |
|
if (! isset($buffer[$offset + 0])) { |
182
|
1 |
|
throw new Exception('Failed to read buffer entry ' . ($offset + 0)); |
183
|
|
|
} |
184
|
|
|
|
185
|
5 |
|
if (! isset($buffer[$offset + 1])) { |
186
|
1 |
|
throw new Exception('Failed to read buffer entry ' . ($offset + 1)); |
187
|
|
|
} |
188
|
|
|
|
189
|
4 |
|
$output += $buffer[$offset + 0] << 8; |
190
|
4 |
|
$output += $buffer[$offset + 1]; |
191
|
|
|
|
192
|
4 |
|
$offset += 2; |
193
|
4 |
|
return $output; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Read a DWORD (32 bits number) |
198
|
|
|
* |
199
|
|
|
* @param array<int, int> $buffer |
200
|
|
|
*/ |
201
|
5 |
|
public static function readInt32(array &$buffer, int &$offset = 0): int |
202
|
|
|
{ |
203
|
5 |
|
$output = 0; |
204
|
|
|
|
205
|
5 |
|
if (! isset($buffer[$offset + 0])) { |
206
|
|
|
throw new Exception('Failed to read buffer entry ' . ($offset + 0)); |
207
|
|
|
} |
208
|
|
|
|
209
|
5 |
|
if (! isset($buffer[$offset + 1])) { |
210
|
|
|
throw new Exception('Failed to read buffer entry ' . ($offset + 1)); |
211
|
|
|
} |
212
|
|
|
|
213
|
5 |
|
if (! isset($buffer[$offset + 2])) { |
214
|
|
|
throw new Exception('Failed to read buffer entry ' . ($offset + 2)); |
215
|
|
|
} |
216
|
|
|
|
217
|
5 |
|
if (! isset($buffer[$offset + 3])) { |
218
|
1 |
|
throw new Exception('Failed to read buffer entry ' . ($offset + 3)); |
219
|
|
|
} |
220
|
|
|
|
221
|
4 |
|
$output += $buffer[$offset + 0] << 24; |
222
|
4 |
|
$output += $buffer[$offset + 1] << 16; |
223
|
4 |
|
$output += $buffer[$offset + 2] << 8; |
224
|
4 |
|
$output += $buffer[$offset + 3]; |
225
|
|
|
|
226
|
4 |
|
$offset += 4; |
227
|
4 |
|
return $output; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|