- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgpack.php
More file actions
Latest commit
401 lines (358 loc) · 12.3 KB
/
Copy pathmsgpack.php
File metadata and controls
401 lines (358 loc) · 12.3 KB
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
privatefunction__construct() {}
privatestatic$extensions = array();
privatestatic$extensionCodes = array();
privatestatic$strsAsBufs = NULL;
privatestatic$assocArrays = NULL;
privatestatic$buffer = NULL;
privatestatic$pos = 0;
privatestatic$i16 = 'n';
privatestatic$i32 = 'N';
privatestatic$f32 = 'G';
privatestatic$f64 = 'E';
privatestaticfunctionneedsF64($n) {
$n = abs($n);
$lg = log($n,2);
if($lg < -120 || $lg > 120) returntrue;
$n = $n / pow(2,floor($lg) - 23);
return$n !== floor($n);
}
privatestaticfunctionisArray($arr) {
$l = count($arr);
for($i=0;$i<$l;$i++)
if(!isset($arr[$i])) returnFALSE;
returnTRUE;
}
privatestaticfunctiondetectRecursion(&$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) returnFALSE;
$serial_count = preg_match_all('%\\*RECURSION\\*%',serialize($arr));
return$printed_count > $serial_count;
}
privatestaticfunctionread($l) {
if(self::$pos + $l > strlen(self::$buffer))
thrownewException('MsgPack Error: Unexpected end of data');
$str = substr(self::$buffer,self::$pos,$l);
self::$pos += $l;
return$str;
}
privatestaticfunctionui16() {
$d = unpack(self::$i16.'n',self::read(2));
return$d['n'];
}
privatestaticfunctionui32() {
$d = unpack(self::$i32.'n',self::read(4));
return$d['n'] < 0 ? $d['n'] + 0x100000000 : $d['n'];
}
privatestaticfunctionui64() {
returnself::ui32() * 0x100000000 + self::ui32();
}
privatestaticfunctioni8() {
$v = ord(self::read(1));
if($v & 0x80) $v |= -1^255;
return$v;
}
privatestaticfunctioni16() {
$v = self::ui16();
if($v & 0x8000) $v |= -1^255;
return$v;
}
privatestaticfunctioni32() {
returnself::ui32() >> 0;
}
privatestaticfunctioni64() {
returnself::i32() * 0x100000000 + self::ui32();
}
privatestaticfunctionf32() {
$d = unpack(self::$f32.'n',self::read(4));
return$d['n'];
}
privatestaticfunctionf64() {
$d = unpack(self::$f64.'n',self::read(8));
return$d['n'];
}
privatestaticfunctiondec_obj($l) {
if(self::$assocArrays) {
$o = array();
for($i=0;$i<$l;$i++)
$o[self::dec()] = self::dec();
return$o;
}
$o = newstdClass();
for($i=0;$i<$l;$i++) {
$prop = self::dec();
$o->$prop = self::dec();
}
return$o;
}
privatestaticfunctiondec_arr($l) {
$o = array();
for($i=0;$i<$l;$i++)
$o []= self::dec();
return$o;
}
privatestaticfunctiondec_ext($ty,$buf) {
if($ty & 0x80) $ty |= (-1^255);
if(!isset(self::$extensionCodes[$ty]))
returnNULL;
returncall_user_func(self::$extensionCodes[$ty]['dec'],$buf);
}
privatestaticfunctionenc($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))
thrownewException("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)
returnchr($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)
returnchr($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)
returnchr(0xc4) . chr($l) . $obj;
if($l < 65536)
returnchr(0xc5) . pack(self::$i16, $l) . $obj;
returnchr(0xc6) . pack(self::$i32, $l) . $obj;
}
$l = strlen($obj);
if($l < 32)
returnchr(0xa0 | $l) . $obj;
if($l < 256)
returnchr(0xd9) . chr($l) . $obj;
if($l < 65536)
returnchr(0xda) . pack(self::$i16, $l) . $obj;
returnchr(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($objas$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($objas$k => $v)
$buf .= self::enc($k) . self::enc($v);
return$buf;
}
thrownewException("MsgPack Error: Could not encode value of type $ty");
}
privatestaticfunctiondec() {
$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) returnself::dec_obj($b&15); // fixmap
if(($b&0xf0) === 0x90) returnself::dec_arr($b&15); // fixarray
if(($b&0xe0) === 0xa0) returnself::read($b&31); // fixstr
switch($b) {
case0xc1: thrownewException("MsgPack Error: Encountered reserved type 0xc1");
case0xc0: returnNULL; // nil
case0xc2: returnFALSE; // false
case0xc3: returnTRUE; // true
case0xd9: case0xc4: returnself::read(ord(self::read(1))); // bin 8 / str 8
case0xda: case0xc5: returnself::read(self::ui16()); // bin 16 / str 16
case0xdb: case0xc6: returnself::read(self::ui32()); // bin 32 / str 32
case0xc7: $l = ord(self::read(1)); returnself::dec_ext(ord(self::read(1)),self::read($l)); // ext 8
case0xc8: $l = self::ui16(); returnself::dec_ext(ord(self::read(1)),self::read($l)); // ext 16
case0xc9: $l = self::ui32(); returnself::dec_ext(ord(self::read(1)),self::read($l)); // ext 32
case0xca: returnself::f32(); // float 32
case0xcb: returnself::f64(); // float 64
case0xcc: returnord(self::read(1)); // uint 8
case0xcd: returnself::ui16(); // uint 16
case0xce: returnself::ui32(); // uint 32
case0xcf: returnself::ui64(); // uint 64
case0xd0: returnord(self::read(1)); // int 8
case0xd1: returnself::i16(); // int 16
case0xd2: returnself::i32(); // int 32
case0xd3: returnself::i64(); // int 64
case0xd4: returnself::dec_ext(ord(self::read(1)),self::read(1)); // fixext 1
case0xd5: returnself::dec_ext(ord(self::read(1)),self::read(2)); // fixext 2
case0xd6: returnself::dec_ext(ord(self::read(1)),self::read(4)); // fixext 4
case0xd7: returnself::dec_ext(ord(self::read(1)),self::read(8)); // fixext 8
case0xd8: returnself::dec_ext(ord(self::read(1)),self::read(16)); // fixext 16
case0xdc: returnself::dec_arr(self::ui16()); // array 16
case0xdd: returnself::dec_arr(self::ui32()); // array 32
case0xde: returnself::dec_obj(self::ui16()); // map 16
case0xdf: returnself::dec_obj(self::ui32()); // map 32
}
thrownewException("MsgPack Error: Somehow encountered unknown byte code: $b");
}
publicstaticfunctionencode($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))
thrownewException('MsgPack Error: Recursive structure detected');
}
$encoded = self::enc($obj);
return$encoded;
}
publicstaticfunctiondecode($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))
thrownewException("MsgPack Error: Trying to decode more data than expected");
self::$buffer = NULL;
return$decoded;
}
publicstaticfunctionextend($ext,$core=FALSE) {
$type = intval($ext['type']);
$typeof = isset($ext['varType']) ? $ext['varType'] : 'object';
$encode = $ext['encode'];
$decode = $ext['decode'];
if(!$core && $type < 0)
thrownewException("MsgPack Error: Non-core extension cannot have negative type $type");
if($type < -128 || $type > 127)
thrownewException("MsgPack Error: Type code for extension is out of range: $type");
if(!is_callable($encode))
thrownewException("MsgPack Error: Extension for code $type must have callable 'encode'");
if(!is_callable($decode))
thrownewException("MsgPack Error: Extension for code $type must have callable 'decode'");
if(isset(self::$extensionCodes[$type]))
thrownewException("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(!($objinstanceof DateTime)) returnFALSE;
$secs = floatval($obj->format('U'));
$nano = floatval($obj->format('u'))*1000;
if($secs < 0 || $secs > 0x3ffffffff)
returnpack('NNN', $nano, floor($secs/0x100000000), $secs >> 0);
if($nano || $secs > 0xffffffff)
returnpack('NN', $nano*4 + ($secs/0x100000000 & 3), $secs >> 0);
returnpack('N', $secs >> 0);
},
'decode' => function($data) {
$secs = 0;
$nano = 0;
switch(strlen($data)) {
case4:
$time = unpack('Ns', $data);
$secs = $time['s'];
if($secs < 0) $secs += 0x100000000;
break;
case8:
$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;
case12:
$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:
thrownewException("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;
?>