1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plasma Driver MySQL component |
4
|
|
|
* Copyright 2018 PlasmaPHP, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://github.com/PlasmaPHP |
7
|
|
|
* License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Plasma\Drivers\MySQL\Messages; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Utilities for messages. |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class MessageUtility { |
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* Parses a 1 byte / 8 bit integer (0 to 255). |
19
|
|
|
* @return int |
20
|
|
|
*/ |
21
|
1 |
|
static function readInt1(string &$buffer): int { |
22
|
1 |
|
return \ord(static::readBuffer($buffer, 1)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* Parses a 2 byte / 16 bit integer (0 to 64 K / 0xFFFF). |
27
|
|
|
* @return int |
28
|
|
|
*/ |
29
|
|
|
static function readInt2(string &$buffer): int { |
30
|
|
|
return \unpack('v', static::readBuffer($buffer, 2))[1]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* Parses a 3 byte / 24 bit integer (0 to 16 M / 0xFFFFFF). |
35
|
|
|
* @return int |
36
|
|
|
*/ |
37
|
1 |
|
static function readInt3(string &$buffer): int { |
38
|
1 |
|
return \unpack('V', static::readBuffer($buffer, 3)."\0")[1]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* Parses a 4 byte / 32 bit integer (0 to 4 G / 0xFFFFFFFF). |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
static function readInt4(string &$buffer): int { |
46
|
|
|
return \unpack('V', static::readBuffer($buffer, 4))[1]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* Parses a 8 byte / 64 bit integer (0 to 2^64-1). |
51
|
|
|
* @return int|string |
52
|
|
|
*/ |
53
|
|
|
static function readInt8(string &$buffer) { |
54
|
|
|
$strInt = static::readBuffer($buffer, 8); |
55
|
|
|
|
56
|
|
|
if(\PHP_INT_SIZE > 4) { |
57
|
|
|
return \unpack('P', $strInt)[1]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$result = \bcadd('0', \unpack('n', \substr($strInt, 0, 2))); |
|
|
|
|
61
|
|
|
$result = \bcmul($result, '65536'); |
62
|
|
|
$result = \bcadd($result, \unpack('n', \substr($strInt, 2, 2))); |
63
|
|
|
$result = \bcmul($result, '65536'); |
64
|
|
|
$result = \bcadd($result, \unpack('n', \substr($strInt, 4, 2))); |
65
|
|
|
$result = \bcmul($result, '65536'); |
66
|
|
|
$result = \bcadd($result, \unpack('n', \substr($strInt, 6, 2))); |
67
|
|
|
|
68
|
|
|
// 9223372036854775808 is equal to (1 << 63) |
69
|
|
|
if(\bccomp($result, '9223372036854775808') !== -1) { |
70
|
|
|
$result = \bcsub($result, '18446744073709551616'); // $result -= (1 << 64) |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* Parses length-encoded binary integer. |
78
|
|
|
* Returns the decoded integer 0 to 2^64 or `null` for special null int. |
79
|
|
|
* @return int|null |
80
|
|
|
*/ |
81
|
|
|
static function readIntLength(string &$buffer): ?int { |
|
|
|
|
82
|
|
|
$f = static::readInt1(); |
|
|
|
|
83
|
|
|
if($f <= 250) { |
84
|
|
|
return $f; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if($f === 251) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if($f === 252) { |
92
|
|
|
return static::readInt2(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if($f === 253) { |
96
|
|
|
return static::readInt3(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return static::readInt8(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* Parses a length-encoded binary string. If length is null, `null` will be returned. |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
|
|
static function readStringLength(string &$buffer, ?int $length = null): ?string { |
107
|
|
|
$length = ($length !== null ? $length : static::readIntLength()); |
|
|
|
|
108
|
|
|
if($length === null) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return static::readBuffer($buffer, $length); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
|
|
|
|
116
|
|
|
* Reads NULL-terminated C string. |
117
|
|
|
* @return string |
118
|
|
|
* @throws \InvalidArgumentException |
119
|
|
|
*/ |
120
|
|
|
static function readStringNull(string &$buffer): string { |
121
|
|
|
$pos = \strpos($buffer, "\0"); |
122
|
|
|
if($pos === false) { |
123
|
|
|
throw new \InvalidArgumentException('Missing NULL character'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return static::readBuffer($buffer, $pos); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param int $int |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
static function writeInt1(int $int): string { |
134
|
|
|
return \chr($int); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param int $int |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
static function writeInt2(int $int): string { |
142
|
|
|
return \pack('v', $int); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int $int |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
static function writeInt3(int $int): string { |
150
|
|
|
return \substr(\pack('V', $int), 0, 3); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int $int |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
static function writeInt4(int $int): string { |
158
|
|
|
return \pack('V', $int); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string|int $int |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
static function writeInt8($int): string { |
166
|
|
|
if(\PHP_INT_SIZE > 4) { |
167
|
|
|
return \pack('P', ((int) $int)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if(\bccomp($int, '0') === -1) { |
171
|
|
|
// 18446744073709551616 is equal to (1 << 64) |
172
|
|
|
$int = \bcadd($int, '18446744073709551616'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return \pack('v', \bcmod(\bcdiv($int, '281474976710656'), '65536')). |
176
|
|
|
\pack('v', \bcmod(\bcdiv($int, '4294967296'), '65536')). |
177
|
|
|
\pack('v', \bcdiv($int, '65536'), '65536'). |
178
|
|
|
\pack('v', \bcmod($int, '65536')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param float $float |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
static function writeFloat(float $float): string { |
186
|
|
|
return \pack('e', $float); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Builds length-encoded binary string. |
191
|
|
|
* @param string|null $s |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
static function writeStringLength(?string $s): string { |
195
|
|
|
if($s === NULL) { |
|
|
|
|
196
|
|
|
// \xFB (251) |
197
|
|
|
return "\xFB"; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$l = \strlen($s); |
201
|
|
|
if($l <= 250) { |
202
|
|
|
return static::writeInt1($l).$s; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if($l <= 0xFFFF) { // max 2^16: \xFC (252) |
206
|
|
|
return "\xFC".static::writeInt2($l).$s; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if($l <= 0xFFFFFF) { // max 2^24: \xFD (253) |
210
|
|
|
return "\xFD".static::writeInt3($l).$s; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return "\xFE".static::writeInt8($l).$s; // max 2^64: \xFE (254) |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
|
|
|
|
217
|
|
|
* Reads a specified length from the buffer and discards the read part. |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
1 |
|
static function readBuffer(string &$buffer, int $length): string { |
221
|
1 |
|
$str = \substr($buffer, 0, $length); |
222
|
1 |
|
$buffer = \substr($buffer, ($length + 1)); |
223
|
|
|
|
224
|
1 |
|
return $str; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|