forked from krowinski/php-mysql-replication
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBinaryDataReader.php
320 lines (267 loc) · 8.51 KB
/
BinaryDataReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
declare(strict_types=1);
namespace MySQLReplication\BinaryDataReader;
class BinaryDataReader
{
public const NULL_COLUMN = 251;
public const UNSIGNED_CHAR_COLUMN = 251;
public const UNSIGNED_SHORT_COLUMN = 252;
public const UNSIGNED_INT24_COLUMN = 253;
public const UNSIGNED_INT64_COLUMN = 254;
public const UNSIGNED_CHAR_LENGTH = 1;
public const UNSIGNED_SHORT_LENGTH = 2;
public const UNSIGNED_INT24_LENGTH = 3;
public const UNSIGNED_INT32_LENGTH = 4;
public const UNSIGNED_FLOAT_LENGTH = 4;
public const UNSIGNED_DOUBLE_LENGTH = 8;
public const UNSIGNED_INT40_LENGTH = 5;
public const UNSIGNED_INT48_LENGTH = 6;
public const UNSIGNED_INT56_LENGTH = 7;
public const UNSIGNED_INT64_LENGTH = 8;
private $data;
/**
* @var int
*/
private $readBytes = 0;
public function __construct(string $data)
{
$this->data = $data;
}
public static function pack64bit(int $value): string
{
return pack(
'C8',
($value >> 0) & 0xFF,
($value >> 8) & 0xFF,
($value >> 16) & 0xFF,
($value >> 24) & 0xFF,
($value >> 32) & 0xFF,
($value >> 40) & 0xFF,
($value >> 48) & 0xFF,
($value >> 56) & 0xFF
);
}
public function advance(int $length): void
{
$this->read($length);
}
public function readInt16(): int
{
return unpack('s', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];
}
public function read(int $length): string
{
$return = substr($this->data, 0, $length);
$this->readBytes += $length;
$this->data = substr($this->data, $length);
return $return;
}
public function unread(string $data): void
{
$this->readBytes -= strlen($data);
$this->data = $data . $this->data;
}
/**
* @throws BinaryDataReaderException
*/
public function readCodedBinary(): ?int
{
$c = ord($this->read(self::UNSIGNED_CHAR_LENGTH));
if ($c === self::NULL_COLUMN) {
return null;
}
if ($c < self::UNSIGNED_CHAR_COLUMN) {
return $c;
}
if ($c === self::UNSIGNED_SHORT_COLUMN) {
return $this->readUInt16();
}
if ($c === self::UNSIGNED_INT24_COLUMN) {
return $this->readUInt24();
}
throw new BinaryDataReaderException('Column num ' . $c . ' not handled');
}
public function readUInt16(): int
{
return unpack('v', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];
}
public function readUInt24(): int
{
$data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH));
return $data[1] + ($data[2] << 8) + ($data[3] << 16);
}
public function readUInt64(): string
{
return $this->unpackUInt64($this->read(self::UNSIGNED_INT64_LENGTH));
}
public function unpackUInt64(string $binary): string
{
$data = unpack('V*', $binary);
return bcadd((string)$data[1], bcmul((string)$data[2], bcpow('2', '32')));
}
public function readInt24(): int
{
$data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH));
$res = $data[1] | ($data[2] << 8) | ($data[3] << 16);
if ($res >= 0x800000) {
$res -= 0x1000000;
}
return $res;
}
public function readInt64(): string
{
$data = unpack('V*', $this->read(self::UNSIGNED_INT64_LENGTH));
return bcadd((string)$data[1], (string)($data[2] << 32));
}
/**
* @throws BinaryDataReaderException
*/
public function readLengthString(int $size): string
{
return $this->read($this->readUIntBySize($size));
}
/**
* @throws BinaryDataReaderException
*/
public function readUIntBySize(int $size): int
{
if ($size === self::UNSIGNED_CHAR_LENGTH) {
return $this->readUInt8();
}
if ($size === self::UNSIGNED_SHORT_LENGTH) {
return $this->readUInt16();
}
if ($size === self::UNSIGNED_INT24_LENGTH) {
return $this->readUInt24();
}
if ($size === self::UNSIGNED_INT32_LENGTH) {
return $this->readUInt32();
}
if ($size === self::UNSIGNED_INT40_LENGTH) {
return $this->readUInt40();
}
if ($size === self::UNSIGNED_INT48_LENGTH) {
return $this->readUInt48();
}
if ($size === self::UNSIGNED_INT56_LENGTH) {
return $this->readUInt56();
}
if ($size === self::UNSIGNED_INT64_LENGTH) {
return intval($this->readUInt64());
}
throw new BinaryDataReaderException('$size ' . $size . ' not handled');
}
public function readUInt8(): int
{
return unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1];
}
public function readUInt32(): int
{
return unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
}
public function readUInt40(): int
{
$data1 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1];
$data2 = unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
return $data1 + ($data2 << 8);
}
public function readUInt48(): int
{
$data = unpack('v3', $this->read(self::UNSIGNED_INT48_LENGTH));
return $data[1] + ($data[2] << 16) + ($data[3] << 32);
}
public function readUInt56(): int
{
$data1 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1];
$data2 = unpack('S', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];
$data3 = unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
return $data1 + ($data2 << 8) + ($data3 << 24);
}
/**
* @throws BinaryDataReaderException
*/
public function readIntBeBySize(int $size): int
{
if ($size === self::UNSIGNED_CHAR_LENGTH) {
return $this->readInt8();
}
if ($size === self::UNSIGNED_SHORT_LENGTH) {
return $this->readInt16Be();
}
if ($size === self::UNSIGNED_INT24_LENGTH) {
return $this->readInt24Be();
}
if ($size === self::UNSIGNED_INT32_LENGTH) {
return $this->readInt32Be();
}
if ($size === self::UNSIGNED_INT40_LENGTH) {
return $this->readInt40Be();
}
throw new BinaryDataReaderException('$size ' . $size . ' not handled');
}
public function readInt8(): int
{
$re = unpack('c', $this->read(self::UNSIGNED_CHAR_LENGTH))[1];
return $re >= 0x80 ? $re - 0x100 : $re;
}
public function readInt16Be(): int
{
$re = unpack('n', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];
return $re >= 0x8000 ? $re - 0x10000 : $re;
}
public function readInt24Be(): int
{
$data = unpack('C3', $this->read(self::UNSIGNED_INT24_LENGTH));
$re = ($data[1] << 16) | ($data[2] << 8) | $data[3];
return $re >= 0x800000 ? $re - 0x1000000 : $re;
}
public function readInt32Be(): int
{
$re = unpack('N', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
return $re >= 0x80000000 ? $re - 0x100000000 : $re;
}
public function readInt40Be(): int
{
$data1 = unpack('N', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
$data2 = unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1];
return $data2 + ($data1 << 8);
}
public function readInt32(): int
{
return unpack('i', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
}
public function readFloat(): float
{
return unpack('f', $this->read(self::UNSIGNED_FLOAT_LENGTH))[1];
}
public function readDouble(): float
{
return unpack('d', $this->read(self::UNSIGNED_DOUBLE_LENGTH))[1];
}
public function readTableId(): string
{
return $this->unpackUInt64($this->read(self::UNSIGNED_INT48_LENGTH) . chr(0) . chr(0));
}
public function isComplete(int $size): bool
{
return ! ($this->readBytes - 20 < $size);
}
public function getBinaryDataLength(): int
{
return strlen($this->data);
}
public function getData(): string
{
return $this->data;
}
public function getBinarySlice(int $binary, int $start, int $size, int $binaryLength): int
{
$binary >>= $binaryLength - ($start + $size);
$mask = ((1 << $size) - 1);
return $binary & $mask;
}
public function getReadBytes(): int
{
return $this->readBytes;
}
}