-
Notifications
You must be signed in to change notification settings - Fork 9
/
msgpack.php
302 lines (272 loc) · 7.37 KB
/
msgpack.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
<?php
/**
* Pack some input into msgpack format.
* Format specs: http://wiki.msgpack.org/display/MSGPACK/Format+specification
*
* @param mixed $input
* @return string
* @throws \InvalidArgumentException
*/
function msgpack_pack($input)
{
static $bigendian;
if (!isset($bigendian)) $bigendian = (pack('S',1)==pack('n',1));
// null
if (is_null($input)) {
return pack('C',0xC0);
}
// booleans
if (is_bool($input)) {
return pack('C',$input ? 0xC3 : 0xC2);
}
// Integers
if (is_int($input)) {
// positive fixnum
if (($input|0x7F) == 0x7F) return pack('C',$input&0x7F);
// negative fixnum
if ($input < 0 && $input>=-32) return pack('c',$input);
// uint8
if ($input > 0 && $input <= 0xFF) return pack('CC',0xCC,$input);
// uint16
if ($input > 0 && $input <= 0xFFFF) return pack('Cn',0xCD,$input);
// uint32
if ($input > 0 && $input <= 0xFFFFFFFF) return pack('CN',0xCE,$input);
// uint64
if ($input > 0 && $input <= 0xFFFFFFFFFFFFFFFF) {
// pack() does not support 64-bit ints, so pack into two 32-bits
$h = ($input&0xFFFFFFFF00000000)>>32;
$l = $input&0xFFFFFFFF;
return $bigendian ? pack('CNN',0xCF,$l,$h) : pack('CNN',0xCF,$h,$l);
}
// int8
if ($input < 0 && $input >= -0x80) return pack('Cc',0xD0,$input);
// int16
if ($input < 0 && $input >= -0x8000) {
$p = pack('s',$input);
return pack('Ca2',0xD1,$bigendian ? $p : strrev($p));
}
// int32
if ($input < 0 && $input >= -0x80000000) {
$p = pack('l',$input);
return pack('Ca4',0xD2,$bigendian ? $p : strrev($p));
}
// int64
if ($input < 0 && $input >= -0x8000000000000000) {
// pack() does not support 64-bit ints either so pack into two 32-bits
$p1 = pack('l',$input&0xFFFFFFFF);
$p2 = pack('l',($input>>32)&0xFFFFFFFF);
return $bigendian ? pack('Ca4a4',0xD3,$p1,$p2) : pack('Ca4a4',0xD3,strrev($p2),strrev($p1));
}
throw new \InvalidArgumentException('Invalid integer: '.$input);
}
// Floats
if (is_float($input)) {
// Just pack into a double, don't take any chances with single precision
return pack('C',0xCB).($bigendian ? pack('d',$input) : strrev(pack('d',$input)));
}
// Strings/Raw
if (is_string($input)) {
$len = strlen($input);
if ($len<32) {
return pack('Ca*',0xA0|$len,$input);
} else if ($len<=0xFFFF) {
return pack('Cna*',0xDA,$len,$input);
} else if ($len<=0xFFFFFFFF) {
return pack('CNa*',0xDB,$len,$input);
} else {
throw new \InvalidArgumentException('Input overflows (2^32)-1 byte max');
}
}
// Arrays & Maps
if (is_array($input)) {
$keys = array_keys($input);
$len = count($input);
// Is this an associative array?
$isMap = false;
foreach ($keys as $key) {
if (!is_int($key)) {
$isMap = true;
break;
}
}
$buf = '';
if ($len<16) {
$buf .= pack('C',($isMap?0x80:0x90)|$len);
} else if ($len<=0xFFFF) {
$buf .= pack('Cn',($isMap?0xDE:0xDC),$len);
} else if ($len<=0xFFFFFFFF) {
$buf .= pack('CN',($isMap?0xDF:0xDD),$len);
} else {
throw new \InvalidArgumentException('Input overflows (2^32)-1 max elements');
}
foreach ($input as $key => $elm) {
if ($isMap) $buf .= msgpack_pack($key);
$buf .= msgpack_pack($elm);
}
return $buf;
}
throw new \InvalidArgumentException('Not able to pack/serialize input type: '.gettype($input));
}
/**
* Unpack data from a msgpack'ed string
*
* @param string $input
* @return mixed
*/
function msgpack_unpack($input)
{
static $bigendian;
if (!isset($bigendian)) $bigendian = (pack('S',1)==pack('n',1));
// Store input into a memory buffer so we can operate on it with filepointers
static $buffer;
static $pos;
if (!isset($buffer)) {
$buffer = $input;
$pos = 0;
}
if ($pos==strlen($buffer)) {
$buffer = $input;
$pos = 0;
}
// Read a single byte
$byte = substr($buffer,$pos++,1);
// null
if ($byte == "\xC0") return null;
// booleans
if ($byte == "\xC2") return false;
if ($byte == "\xC3") return true;
// positive fixnum
if (($byte & "\x80") == "\x00") {
return current(unpack('C',$byte&"\x7F"));
}
// negative fixnum
if (($byte & "\xE0") == "\xE0") {
return current(unpack('c',$byte&"\xFF"));
}
// fixed raw
if ((($byte ^ "\xA0") & "\xE0") == "\x00") {
$len = current(unpack('c',($byte ^ "\xA0")));
if ($len == 0) return "";
$d = substr($buffer,$pos,$len);
$pos+=$len;
return current(unpack('a'.$len,$d));
}
// Arrays
if ((($byte ^ "\x90") & "\xF0") == "\x00") {
// fixed array
$len = current(unpack('c',($byte ^ "\x90")));
$data = array();
for($i=0;$i<$len;$i++) {
$data[] = msgpack_unpack($input);
}
return $data;
} else if ($byte == "\xDC" || $byte == "\xDD") {
if ($byte == "\xDC") {
$d = substr($buffer,$pos,2);
$pos+=2;
$len = current(unpack('n',$d));
}
if ($byte == "\xDD") {
$d = substr($buffer,$pos,4);
$pos+=4;
$len = current(unpack('N',$d));
}
$data = array();
for($i=0;$i<$len;$i++) {
$data[] = msgpack_unpack($input);
}
return $data;
}
// Maps
if ((($byte ^ "\x80") & "\xF0") == "\x00") {
// fixed map
$len = current(unpack('c',($byte ^ "\x80")));
$data = array();
for($i=0;$i<$len;$i++) {
$key = msgpack_unpack($input);
$value = msgpack_unpack($input);
$data[$key] = $value;
}
return $data;
} else if ($byte == "\xDE" || $byte == "\xDF") {
if ($byte == "\xDE") {
$d = substr($buffer,$pos,2);
$pos+=2;
$len = current(unpack('n',$d));
}
if ($byte == "\xDF") {
$d = substr($buffer,$pos,4);
$pos+=4;
$len = current(unpack('N',$d));
}
$data = array();
for($i=0;$i<$len;$i++) {
$key = msgpack_unpack($input);
$value = msgpack_unpack($input);
$data[$key] = $value;
}
return $data;
}
switch ($byte) {
// Unsigned integers
case "\xCC": // uint 8
return current(unpack('C',substr($buffer,$pos++,1)));
case "\xCD": // uint 16
$d = substr($buffer,$pos,2);
$pos+=2;
return current(unpack('n',$d));
case "\xCE": // uint 32
$d = substr($buffer,$pos,4);
$pos+=4;
return current(unpack('N',$d));
case "\xCF": // uint 64
$d = substr($buffer,$pos,8);
$pos+=8;
// Unpack into two uint32 and re-assemble
$dat = unpack('Np1/Np2',$d);
$dat['p1'] = $dat['p1'] << 32;
return $dat['p1']|$dat['p2'];
// Signed integers
case "\xD0": // int 8
return current(unpack('c',substr($buffer,$pos++,1)));
case "\xD1": // int 16
$d = substr($buffer,$pos,2);
$pos+=2;
return (current(unpack('n',~$d))+1)*-1;
case "\xD2": // int 32
$d = substr($buffer,$pos,4);
$pos+=4;
return (current(unpack('N',~$d))+1)*-1;
case "\xD3": // int 64
$d = substr($buffer,$pos,8);
$pos+=8;
$dat = unpack('Np1/Np2',~$d);
$dat['p1'] = $dat['p1'] << 32;
return (($dat['p1']|$dat['p2'])+1)*-1;
// String / Raw
case "\xDA": // raw 16
$d = substr($buffer,$pos,2);
$pos+=2;
$len = current(unpack('n',$d));
$d = substr($buffer,$pos,$len);
$pos+=$len;
return current(unpack('a'.$len,$d));
case "\xDB": // raw 32
$d = substr($buffer,$pos,4);
$pos+=4;
$len = current(unpack('N',$d));
$d = substr($buffer,$pos,$len);
$pos+=$len;
return current(unpack('a'.$len,$d));
// Floats
case "\xCA": // single-precision
$d = substr($buffer,$pos,4);
$pos+=4;
return current(unpack('f',$bigendian ? $d : strrev($d)));
case "\xCB": // double-precision
$d = substr($buffer,$pos,8);
$pos+=8;
return current(unpack('d',$bigendian ? $d : strrev($d)));
}
throw new \InvalidArgumentException('Can\'t unpack data with byte-header: '.$byte);
}