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