-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgpack.php
401 lines (358 loc) · 12.3 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/// Micro MsgPack Library
/// Version: 1.0.2
/// Author: Codesmith32
/// License: MIT / https://mit-license.org
/// URL: https://github.com/CodeSmith32/msgpack-php-micro
if(!class_exists('MsgPack')):
class MsgPack {
// static-only class
private function __construct() {}
private static $extensions = array();
private static $extensionCodes = array();
private static $strsAsBufs = NULL;
private static $assocArrays = NULL;
private static $buffer = NULL;
private static $pos = 0;
private static $i16 = 'n';
private static $i32 = 'N';
private static $f32 = 'G';
private static $f64 = 'E';
private static function needsF64($n) {
$n = abs($n);
$lg = log($n,2);
if($lg < -120 || $lg > 120) return true;
$n = $n / pow(2,floor($lg) - 23);
return $n !== floor($n);
}
private static function isArray($arr) {
$l = count($arr);
for($i=0;$i<$l;$i++)
if(!isset($arr[$i])) return FALSE;
return TRUE;
}
private static function detectRecursion(&$arr) {
// based on https://stackoverflow.com/questions/9042142/detecting-infinite-array-recursion-in-php#9042169
$printed_count = preg_match_all('%\\*RECURSION\\*%',print_r($arr,TRUE));
if($printed_count === 0) return FALSE;
$serial_count = preg_match_all('%\\*RECURSION\\*%',serialize($arr));
return $printed_count > $serial_count;
}
private static function read($l) {
if(self::$pos + $l > strlen(self::$buffer))
throw new Exception('MsgPack Error: Unexpected end of data');
$str = substr(self::$buffer,self::$pos,$l);
self::$pos += $l;
return $str;
}
private static function ui16() {
$d = unpack(self::$i16.'n',self::read(2));
return $d['n'];
}
private static function ui32() {
$d = unpack(self::$i32.'n',self::read(4));
return $d['n'] < 0 ? $d['n'] + 0x100000000 : $d['n'];
}
private static function ui64() {
return self::ui32() * 0x100000000 + self::ui32();
}
private static function i8() {
$v = ord(self::read(1));
if($v & 0x80) $v |= -1^255;
return $v;
}
private static function i16() {
$v = self::ui16();
if($v & 0x8000) $v |= -1^255;
return $v;
}
private static function i32() {
return self::ui32() >> 0;
}
private static function i64() {
return self::i32() * 0x100000000 + self::ui32();
}
private static function f32() {
$d = unpack(self::$f32.'n',self::read(4));
return $d['n'];
}
private static function f64() {
$d = unpack(self::$f64.'n',self::read(8));
return $d['n'];
}
private static function dec_obj($l) {
if(self::$assocArrays) {
$o = array();
for($i=0;$i<$l;$i++)
$o[self::dec()] = self::dec();
return $o;
}
$o = new stdClass();
for($i=0;$i<$l;$i++) {
$prop = self::dec();
$o->$prop = self::dec();
}
return $o;
}
private static function dec_arr($l) {
$o = array();
for($i=0;$i<$l;$i++)
$o []= self::dec();
return $o;
}
private static function dec_ext($ty,$buf) {
if($ty & 0x80) $ty |= (-1^255);
if(!isset(self::$extensionCodes[$ty]))
return NULL;
return call_user_func(self::$extensionCodes[$ty]['dec'],$buf);
}
private static function enc($obj) {
$ty = gettype($obj);
if(isset(self::$extensions[$ty])) {
foreach(self::$extensions[$ty] as $ext) {
$buf = call_user_func($ext['enc'],$obj); // call extension encode
if($buf === FALSE) continue; // returning FALSE passes it on
if(!is_string($buf))
throw new Exception("MsgPack Error: Extension code {$ext['ty']} failed to return a string");
$ret = '';
$l = strlen($buf);
if($l === 1)
$ret .= "\xd4";
elseif($l === 2)
$ret .= "\xd5";
elseif($l === 4)
$ret .= "\xd6";
elseif($l === 8)
$ret .= "\xd7";
elseif($l === 16)
$ret .= "\xd8";
elseif($l < 256)
$ret .= "\xc7" . chr($l);
elseif($l < 65536)
$ret .= "\xc8" . pack(self::$i16, $l);
else
$ret .= "\xc9" . pack(self::$i32, $l);
$ret .= chr($ext['ty']) . $buf;
return $ret;
}
}
switch($ty) {
case 'NULL':
return "\xc0";
case 'boolean':
return $obj ? "\xc3" : "\xc2";
case 'double':
if(floor($obj) !== $obj) {
if(self::needsF64($obj))
return "\xcb" . pack(self::$f64, $obj);
return "\xca" . pack(self::$f32, $obj);
}
// fallthrough for fractionless doubles
case 'integer':
// signed
if($obj < 0) {
if($obj >= -32)
return chr($obj);
if($obj >= -128)
return "\xd0" . chr($obj);
if($obj >= -32768)
return "\xd1" . pack(self::$i16, $obj);
if($obj >= -0x100000000)
return "\xd2" . pack(self::$i32, $obj);
return "\xd3" . pack(self::$i32, floor($obj/0x100000000)) . pack(self::$i32, $obj >> 0);
}
// unsigned
if($obj < 128)
return chr($obj);
if($obj < 256)
return "\xcc" . chr($obj);
if($obj < 65536)
return "\xcd" . pack(self::$i16, $obj);
if($obj < 0x100000000)
return "\xce" . pack(self::$i32, $obj);
return "\xcf" . pack(self::$i32, $obj / 0x100000000) . pack(self::$i32, $obj >> 0);
case 'string': // treat as buffers
if(self::$strsAsBufs) {
$l = strlen($obj);
if($l < 256)
return chr(0xc4) . chr($l) . $obj;
if($l < 65536)
return chr(0xc5) . pack(self::$i16, $l) . $obj;
return chr(0xc6) . pack(self::$i32, $l) . $obj;
}
$l = strlen($obj);
if($l < 32)
return chr(0xa0 | $l) . $obj;
if($l < 256)
return chr(0xd9) . chr($l) . $obj;
if($l < 65536)
return chr(0xda) . pack(self::$i16, $l) . $obj;
return chr(0xdb) . pack(self::$i32, $l) . $obj;
case 'array':
if(self::isArray($obj)) {
$l = count($obj);
$buf = '';
if($l < 16)
$buf .= chr(0x90 | $l);
elseif($l < 65536)
$buf .= "\xdc" . pack(self::$i16, $l);
else
$buf .= "\xdd" . pack(self::$i32, $l);
for($i=0;$i<$l;$i++)
$buf .= self::enc($obj[$i]);
return $buf;
}
// associative arrays fall through
case 'object':
$l = 0;
$buf = '';
foreach($obj as $v) $l++;
if($l < 16)
$buf .= chr(0x80 | $l);
elseif($l < 65536)
$buf .= "\xde" . pack(self::$i16, $l);
else
$buf .= "\xdf" . pack(self::$i32, $l);
foreach($obj as $k => $v)
$buf .= self::enc($k) . self::enc($v);
return $buf;
}
throw new Exception("MsgPack Error: Could not encode value of type $ty");
}
private static function dec() {
$b = ord(self::read(1));
if(($b&0x80) === 0) return $b; // +fixint
if(($b&0xe0) === 0xe0) return $b|(-1^255); // -fixint
if(($b&0xf0) === 0x80) return self::dec_obj($b&15); // fixmap
if(($b&0xf0) === 0x90) return self::dec_arr($b&15); // fixarray
if(($b&0xe0) === 0xa0) return self::read($b&31); // fixstr
switch($b) {
case 0xc1: throw new Exception("MsgPack Error: Encountered reserved type 0xc1");
case 0xc0: return NULL; // nil
case 0xc2: return FALSE; // false
case 0xc3: return TRUE; // true
case 0xd9: case 0xc4: return self::read(ord(self::read(1))); // bin 8 / str 8
case 0xda: case 0xc5: return self::read(self::ui16()); // bin 16 / str 16
case 0xdb: case 0xc6: return self::read(self::ui32()); // bin 32 / str 32
case 0xc7: $l = ord(self::read(1)); return self::dec_ext(ord(self::read(1)),self::read($l)); // ext 8
case 0xc8: $l = self::ui16(); return self::dec_ext(ord(self::read(1)),self::read($l)); // ext 16
case 0xc9: $l = self::ui32(); return self::dec_ext(ord(self::read(1)),self::read($l)); // ext 32
case 0xca: return self::f32(); // float 32
case 0xcb: return self::f64(); // float 64
case 0xcc: return ord(self::read(1)); // uint 8
case 0xcd: return self::ui16(); // uint 16
case 0xce: return self::ui32(); // uint 32
case 0xcf: return self::ui64(); // uint 64
case 0xd0: return ord(self::read(1)); // int 8
case 0xd1: return self::i16(); // int 16
case 0xd2: return self::i32(); // int 32
case 0xd3: return self::i64(); // int 64
case 0xd4: return self::dec_ext(ord(self::read(1)),self::read(1)); // fixext 1
case 0xd5: return self::dec_ext(ord(self::read(1)),self::read(2)); // fixext 2
case 0xd6: return self::dec_ext(ord(self::read(1)),self::read(4)); // fixext 4
case 0xd7: return self::dec_ext(ord(self::read(1)),self::read(8)); // fixext 8
case 0xd8: return self::dec_ext(ord(self::read(1)),self::read(16)); // fixext 16
case 0xdc: return self::dec_arr(self::ui16()); // array 16
case 0xdd: return self::dec_arr(self::ui32()); // array 32
case 0xde: return self::dec_obj(self::ui16()); // map 16
case 0xdf: return self::dec_obj(self::ui32()); // map 32
}
throw new Exception("MsgPack Error: Somehow encountered unknown byte code: $b");
}
public static function encode($obj,$settings=array()) {
self::$strsAsBufs = isset($settings['stringbuffers']) ? $settings['stringbuffers'] : FALSE;
$testRecursion = isset($settings['testrecursion']) ? $settings['testrecursion'] : FALSE;
if($settings === TRUE) self::$strsAsBufs = TRUE;
if($testRecursion) {
if(self::detectRecursion($obj))
throw new Exception('MsgPack Error: Recursive structure detected');
}
$encoded = self::enc($obj);
return $encoded;
}
public static function decode($str,$settings=array()) {
self::$assocArrays = isset($settings['associative']) ? $settings['associative'] : FALSE;
if($settings === TRUE) self::$assocArrays = TRUE;
self::$buffer = $str;
self::$pos = 0;
$decoded = self::dec();
if(self::$pos < strlen(self::$buffer))
throw new Exception("MsgPack Error: Trying to decode more data than expected");
self::$buffer = NULL;
return $decoded;
}
public static function extend($ext,$core=FALSE) {
$type = intval($ext['type']);
$typeof = isset($ext['varType']) ? $ext['varType'] : 'object';
$encode = $ext['encode'];
$decode = $ext['decode'];
if(!$core && $type < 0)
throw new Exception("MsgPack Error: Non-core extension cannot have negative type $type");
if($type < -128 || $type > 127)
throw new Exception("MsgPack Error: Type code for extension is out of range: $type");
if(!is_callable($encode))
throw new Exception("MsgPack Error: Extension for code $type must have callable 'encode'");
if(!is_callable($decode))
throw new Exception("MsgPack Error: Extension for code $type must have callable 'decode'");
if(isset(self::$extensionCodes[$type]))
throw new Exception("MsgPack Error: Trying to register extension code $type more than once");
$ext = array(
'ty' => $type,
'enc' => $encode,
'dec' => $decode,
);
if(!isset(self::$extensions[$typeof]))
self::$extensions[$typeof] = array();
self::$extensions[$typeof] []= $ext;
self::$extensionCodes[$type] = $ext;
}
}
MsgPack::extend(array(
'type' => -1,
'encode' => function($obj) {
if(!($obj instanceof DateTime)) return FALSE;
$secs = floatval($obj->format('U'));
$nano = floatval($obj->format('u'))*1000;
if($secs < 0 || $secs > 0x3ffffffff)
return pack('NNN', $nano, floor($secs/0x100000000), $secs >> 0);
if($nano || $secs > 0xffffffff)
return pack('NN', $nano*4 + ($secs/0x100000000 & 3), $secs >> 0);
return pack('N', $secs >> 0);
},
'decode' => function($data) {
$secs = 0;
$nano = 0;
switch(strlen($data)) {
case 4:
$time = unpack('Ns', $data);
$secs = $time['s'];
if($secs < 0) $secs += 0x100000000;
break;
case 8:
$time = unpack('Nn/Ns', $data);
$nano = $time['n'];
$secs = $time['s'];
if($secs < 0) $secs += 0x100000000;
if($nano < 0) $nano += 0x100000000;
$secs += ($nano & 3) * 0x100000000;
$nano = floor($nano / 4);
break;
case 12:
$time = unpack('Nn/Na/Nb', $data);
$nano = $time['n'];
$a = $time['a'];
$b = $time['b'];
if($nano < 0) $nano += 0x100000000;
if($b < 0) $b += 0x100000000; // a is signed
$secs = $a * 0x100000000 + $b;
var_dump($secs);
break;
default:
throw new Exception("MsgPack Error: Failed to decode Timestamp: Unknown timestamp encoding version with length ".strlen($data));
}
var_dump(sprintf('%0.0f %06d UTC',$secs,$nano/1000));
return DateTime::createFromFormat('U u T',sprintf('%0.0f %06d UTC',$secs,$nano/1000));
},
),TRUE);
endif;
?>