forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 6
/
PythonPickle.php
1359 lines (1228 loc) · 37.4 KB
/
PythonPickle.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Serializer\Adapter;
use stdClass;
use Traversable;
use Zend\Math\BigInteger;
use Zend\Serializer\Exception;
use Zend\Stdlib\ArrayUtils;
/**
* @link http://www.python.org
* @see Phython3.1/Lib/pickle.py
* @see Phython3.1/Modules/_pickle.c
* @link http://pickle-js.googlecode.com
*/
class PythonPickle extends AbstractAdapter
{
/**
* Pickle opcodes. See pickletools.py for extensive docs.
* @link http://hg.python.org/cpython/file/2.7/Lib/pickletools.py
* The listing here is in kind-of alphabetical order of 1-character pickle code.
* pickletools groups them by purpose.
*/
const OP_MARK = '('; // push special markobject on stack
const OP_STOP = '.'; // every pickle ends with STOP
const OP_POP = '0'; // discard topmost stack item
const OP_POP_MARK = '1'; // discard stack top through topmost markobject
const OP_DUP = '2'; // duplicate top stack item
const OP_FLOAT = 'F'; // push float object; decimal string argument
const OP_INT = 'I'; // push integer or bool; decimal string argument
const OP_BININT = 'J'; // push four-byte signed int
const OP_BININT1 = 'K'; // push 1-byte unsigned int
const OP_LONG = 'L'; // push long; decimal string argument
const OP_BININT2 = 'M'; // push 2-byte unsigned int
const OP_NONE = 'N'; // push None
const OP_PERSID = 'P'; // push persistent object; id is taken from string arg
const OP_BINPERSID = 'Q'; // " " " ; " " " " stack
const OP_REDUCE = 'R'; // apply callable to argtuple, both on stack
const OP_STRING = 'S'; // push string; NL-terminated string argument
const OP_BINSTRING = 'T'; // push string; counted binary string argument
const OP_SHORT_BINSTRING = 'U'; // " " ; " " " " < 256 bytes
const OP_UNICODE = 'V'; // push Unicode string; raw-unicode-escaped'd argument
const OP_BINUNICODE = 'X'; // " " " ; counted UTF-8 string argument
const OP_APPEND = 'a'; // append stack top to list below it
const OP_BUILD = 'b'; // call __setstate__ or __dict__.update()
const OP_GLOBAL = 'c'; // push self.find_class(modname, name); 2 string args
const OP_DICT = 'd'; // build a dict from stack items
const OP_EMPTY_DICT = '}'; // push empty dict
const OP_APPENDS = 'e'; // extend list on stack by topmost stack slice
const OP_GET = 'g'; // push item from memo on stack; index is string arg
const OP_BINGET = 'h'; // " " " " " " ; " " 1-byte arg
const OP_INST = 'i'; // build & push class instance
const OP_LONG_BINGET = 'j'; // push item from memo on stack; index is 4-byte arg
const OP_LIST = 'l'; // build list from topmost stack items
const OP_EMPTY_LIST = ']'; // push empty list
const OP_OBJ = 'o'; // build & push class instance
const OP_PUT = 'p'; // store stack top in memo; index is string arg
const OP_BINPUT = 'q'; // " " " " " ; " " 1-byte arg
const OP_LONG_BINPUT = 'r'; // " " " " " ; " " 4-byte arg
const OP_SETITEM = 's'; // add key+value pair to dict
const OP_TUPLE = 't'; // build tuple from topmost stack items
const OP_EMPTY_TUPLE = ')'; // push empty tuple
const OP_SETITEMS = 'u'; // modify dict by adding topmost key+value pairs
const OP_BINFLOAT = 'G'; // push float; arg is 8-byte float encoding
/* Protocol 2 */
const OP_PROTO = "\x80"; // identify pickle protocol
const OP_NEWOBJ = "\x81"; // build object by applying cls.__new__ to argtuple
const OP_EXT1 = "\x82"; // push object from extension registry; 1-byte index
const OP_EXT2 = "\x83"; // ditto, but 2-byte index
const OP_EXT4 = "\x84"; // ditto, but 4-byte index
const OP_TUPLE1 = "\x85"; // build 1-tuple from stack top
const OP_TUPLE2 = "\x86"; // build 2-tuple from two topmost stack items
const OP_TUPLE3 = "\x87"; // build 3-tuple from three topmost stack items
const OP_NEWTRUE = "\x88"; // push True
const OP_NEWFALSE = "\x89"; // push False
const OP_LONG1 = "\x8a"; // push long from < 256 bytes
const OP_LONG4 = "\x8b"; // push really big long
/* Protocol 3 (Python 3.x) */
const OP_BINBYTES = 'B'; // push bytes; counted binary string argument
const OP_SHORT_BINBYTES = 'C'; // " " ; " " " " < 256 bytes
/**
* Whether or not the system is little-endian
*
* @var bool
*/
protected static $isLittleEndian = null;
/**
* @var array Strings representing quotes
*/
protected static $quoteString = array(
'\\' => '\\\\',
"\x00" => '\\x00', "\x01" => '\\x01', "\x02" => '\\x02', "\x03" => '\\x03',
"\x04" => '\\x04', "\x05" => '\\x05', "\x06" => '\\x06', "\x07" => '\\x07',
"\x08" => '\\x08', "\x09" => '\\t', "\x0a" => '\\n', "\x0b" => '\\x0b',
"\x0c" => '\\x0c', "\x0d" => '\\r', "\x0e" => '\\x0e', "\x0f" => '\\x0f',
"\x10" => '\\x10', "\x11" => '\\x11', "\x12" => '\\x12', "\x13" => '\\x13',
"\x14" => '\\x14', "\x15" => '\\x15', "\x16" => '\\x16', "\x17" => '\\x17',
"\x18" => '\\x18', "\x19" => '\\x19', "\x1a" => '\\x1a', "\x1b" => '\\x1b',
"\x1c" => '\\x1c', "\x1d" => '\\x1d', "\x1e" => '\\x1e', "\x1f" => '\\x1f',
"\xff" => '\\xff'
);
// process vars
protected $protocol = null;
protected $memo = array();
protected $pickle = '';
protected $pickleLen = 0;
protected $pos = 0;
protected $stack = array();
protected $marker = null;
/**
* @var BigInteger\Adapter\AdapterInterface
*/
protected $bigIntegerAdapter = null;
/**
* @var PythonPickleOptions
*/
protected $options = null;
/**
* Constructor.
*
* @param array|Traversable|PythonPickleOptions $options Optional
*/
public function __construct($options = null)
{
// init
if (static::$isLittleEndian === null) {
static::$isLittleEndian = (pack('l', 1) === "\x01\x00\x00\x00");
}
$this->marker = new stdClass();
parent::__construct($options);
}
/**
* Set options
*
* @param array|Traversable|PythonPickleOptions $options
* @return PythonPickle
*/
public function setOptions($options)
{
if (!$options instanceof PythonPickleOptions) {
$options = new PythonPickleOptions($options);
}
$this->options = $options;
return $this;
}
/**
* Get options
*
* @return PythonPickleOptions
*/
public function getOptions()
{
if ($this->options === null) {
$this->options = new PythonPickleOptions();
}
return $this->options;
}
/* serialize */
/**
* Serialize PHP to PythonPickle format
*
* @param mixed $value
* @return string
*/
public function serialize($value)
{
$this->clearProcessVars();
$this->protocol = $this->getOptions()->getProtocol();
// write
if ($this->protocol >= 2) {
$this->writeProto($this->protocol);
}
$this->write($value);
$this->writeStop();
$pickle = $this->pickle;
$this->clearProcessVars();
return $pickle;
}
/**
* Write a value
*
* @param mixed $value
* @throws Exception\RuntimeException on invalid or unrecognized value type
*/
protected function write($value)
{
if ($value === null) {
$this->writeNull();
} elseif (is_bool($value)) {
$this->writeBool($value);
} elseif (is_int($value)) {
$this->writeInt($value);
} elseif (is_float($value)) {
$this->writeFloat($value);
} elseif (is_string($value)) {
// TODO: write unicode / binary
$this->writeString($value);
} elseif (is_array($value)) {
if (ArrayUtils::isList($value)) {
$this->writeArrayList($value);
} else {
$this->writeArrayDict($value);
}
} elseif (is_object($value)) {
$this->writeObject($value);
} else {
throw new Exception\RuntimeException(sprintf(
'PHP-Type "%s" can not be serialized by %s',
gettype($value),
get_class($this)
));
}
}
/**
* Write pickle protocol
*
* @param int $protocol
*/
protected function writeProto($protocol)
{
$this->pickle .= self::OP_PROTO . $protocol;
}
/**
* Write a get
*
* @param int $id Id of memo
*/
protected function writeGet($id)
{
if ($this->protocol == 0) {
$this->pickle .= self::OP_GET . $id . "\r\n";
} elseif ($id <= 0xFF) {
// BINGET + chr(i)
$this->pickle .= self::OP_BINGET . chr($id);
} else {
// LONG_BINGET + pack("<i", i)
$bin = pack('l', $id);
if (static::$isLittleEndian === false) {
$bin = strrev($bin);
}
$this->pickle .= self::OP_LONG_BINGET . $bin;
}
}
/**
* Write a put
*
* @param int $id Id of memo
*/
protected function writePut($id)
{
if ($this->protocol == 0) {
$this->pickle .= self::OP_PUT . $id . "\r\n";
} elseif ($id <= 0xff) {
// BINPUT + chr(i)
$this->pickle .= self::OP_BINPUT . chr($id);
} else {
// LONG_BINPUT + pack("<i", i)
$bin = pack('l', $id);
if (static::$isLittleEndian === false) {
$bin = strrev($bin);
}
$this->pickle .= self::OP_LONG_BINPUT . $bin;
}
}
/**
* Write a null as None
*
*/
protected function writeNull()
{
$this->pickle .= self::OP_NONE;
}
/**
* Write boolean value
*
* @param bool $value
*/
protected function writeBool($value)
{
if ($this->protocol >= 2) {
$this->pickle .= ($value === true) ? self::OP_NEWTRUE : self::OP_NEWFALSE;
} else {
$this->pickle .= self::OP_INT . (($value === true) ? '01' : '00') . "\r\n";
}
}
/**
* Write an integer value
*
* @param int $value
*/
protected function writeInt($value)
{
if ($this->protocol == 0) {
$this->pickle .= self::OP_INT . $value . "\r\n";
return;
}
if ($value >= 0) {
if ($value <= 0xFF) {
// self.write(BININT1 + chr(obj))
$this->pickle .= self::OP_BININT1 . chr($value);
} elseif ($value <= 0xFFFF) {
// self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8))
$this->pickle .= self::OP_BININT2 . pack('v', $value);
}
return;
}
// Next check for 4-byte signed ints:
$highBits = $value >> 31; // note that Python shift sign-extends
if ($highBits == 0 || $highBits == -1) {
// All high bits are copies of bit 2**31, so the value
// fits in a 4-byte signed int.
// self.write(BININT + pack("<i", obj))
$bin = pack('l', $value);
if (static::$isLittleEndian === false) {
$bin = strrev($bin);
}
$this->pickle .= self::OP_BININT . $bin;
return;
}
}
/**
* Write a float value
*
* @param float $value
*/
protected function writeFloat($value)
{
if ($this->protocol == 0) {
$this->pickle .= self::OP_FLOAT . $value . "\r\n";
} else {
// self.write(BINFLOAT + pack('>d', obj))
$bin = pack('d', $value);
if (static::$isLittleEndian === true) {
$bin = strrev($bin);
}
$this->pickle .= self::OP_BINFLOAT . $bin;
}
}
/**
* Write a string value
*
* @param string $value
*/
protected function writeString($value)
{
if (($id = $this->searchMemo($value)) !== false) {
$this->writeGet($id);
return;
}
if ($this->protocol == 0) {
$this->pickle .= self::OP_STRING . $this->quoteString($value) . "\r\n";
} else {
$n = strlen($value);
if ($n <= 0xFF) {
// self.write(SHORT_BINSTRING + chr(n) + obj)
$this->pickle .= self::OP_SHORT_BINSTRING . chr($n) . $value;
} else {
// self.write(BINSTRING + pack("<i", n) + obj)
$binLen = pack('l', $n);
if (static::$isLittleEndian === false) {
$binLen = strrev($binLen);
}
$this->pickle .= self::OP_BINSTRING . $binLen . $value;
}
}
$this->memorize($value);
}
/**
* Write an associative array value as dictionary
*
* @param array|Traversable $value
*/
protected function writeArrayDict($value)
{
if (($id = $this->searchMemo($value)) !== false) {
$this->writeGet($id);
return;
}
$this->pickle .= self::OP_MARK . self::OP_DICT;
$this->memorize($value);
foreach ($value as $k => $v) {
$this->write($k);
$this->write($v);
$this->pickle .= self::OP_SETITEM;
}
}
/**
* Write a simple array value as list
*
* @param array $value
*/
protected function writeArrayList(array $value)
{
if (($id = $this->searchMemo($value)) !== false) {
$this->writeGet($id);
return;
}
$this->pickle .= self::OP_MARK . self::OP_LIST;
$this->memorize($value);
foreach ($value as $v) {
$this->write($v);
$this->pickle .= self::OP_APPEND;
}
}
/**
* Write an object as an dictionary
*
* @param object $value
*/
protected function writeObject($value)
{
// The main differences between a SplFixedArray and a normal PHP array is
// that the SplFixedArray is of fixed length and allows only integers
// within the range as indexes.
if ($value instanceof \SplFixedArray) {
$this->writeArrayList($value->toArray());
// Use the object method toArray if available
} elseif (method_exists($value, 'toArray')) {
$this->writeArrayDict($value->toArray());
// If the object is an iterator simply iterate it
// and convert it to an dictionary
} elseif ($value instanceof Traversable) {
$this->writeArrayDict($value);
// other objects are simply converted by using its properties
} else {
$this->writeArrayDict(get_object_vars($value));
}
}
/**
* Write stop
*/
protected function writeStop()
{
$this->pickle .= self::OP_STOP;
}
/* serialize helper */
/**
* Add a value to the memo and write the id
*
* @param mixed $value
*/
protected function memorize($value)
{
$id = count($this->memo);
$this->memo[$id] = $value;
$this->writePut($id);
}
/**
* Search a value in the memo and return the id
*
* @param mixed $value
* @return int|bool The id or false
*/
protected function searchMemo($value)
{
return array_search($value, $this->memo, true);
}
/**
* Quote/Escape a string
*
* @param string $str
* @return string quoted string
*/
protected function quoteString($str)
{
$quoteArr = static::$quoteString;
if (($cntSingleQuote = substr_count($str, "'"))
&& ($cntDoubleQuote = substr_count($str, '"'))
&& ($cntSingleQuote < $cntDoubleQuote)
) {
$quoteArr['"'] = '\\"';
$enclosure = '"';
} else {
$quoteArr["'"] = "\\'";
$enclosure = "'";
}
return $enclosure . strtr($str, $quoteArr) . $enclosure;
}
/* unserialize */
/**
* Unserialize from Python Pickle format to PHP
*
* @param string $pickle
* @return mixed
* @throws Exception\RuntimeException on invalid Pickle string
*/
public function unserialize($pickle)
{
// init process vars
$this->clearProcessVars();
$this->pickle = $pickle;
$this->pickleLen = strlen($this->pickle);
// read pickle string
while (($op = $this->read(1)) !== self::OP_STOP) {
$this->load($op);
}
if (!count($this->stack)) {
throw new Exception\RuntimeException('No data found');
}
$ret = array_pop($this->stack);
// clear process vars
$this->clearProcessVars();
return $ret;
}
/**
* Clear temp variables needed for processing
*/
protected function clearProcessVars()
{
$this->pos = 0;
$this->pickle = '';
$this->pickleLen = 0;
$this->memo = array();
$this->stack = array();
}
/**
* Load a pickle opcode
*
* @param string $op
* @throws Exception\RuntimeException on invalid opcode
*/
protected function load($op)
{
switch ($op) {
case self::OP_PUT:
$this->loadPut();
break;
case self::OP_BINPUT:
$this->loadBinPut();
break;
case self::OP_LONG_BINPUT:
$this->loadLongBinPut();
break;
case self::OP_GET:
$this->loadGet();
break;
case self::OP_BINGET:
$this->loadBinGet();
break;
case self::OP_LONG_BINGET:
$this->loadLongBinGet();
break;
case self::OP_NONE:
$this->loadNone();
break;
case self::OP_NEWTRUE:
$this->loadNewTrue();
break;
case self::OP_NEWFALSE:
$this->loadNewFalse();
break;
case self::OP_INT:
$this->loadInt();
break;
case self::OP_BININT:
$this->loadBinInt();
break;
case self::OP_BININT1:
$this->loadBinInt1();
break;
case self::OP_BININT2:
$this->loadBinInt2();
break;
case self::OP_LONG:
$this->loadLong();
break;
case self::OP_LONG1:
$this->loadLong1();
break;
case self::OP_LONG4:
$this->loadLong4();
break;
case self::OP_FLOAT:
$this->loadFloat();
break;
case self::OP_BINFLOAT:
$this->loadBinFloat();
break;
case self::OP_STRING:
$this->loadString();
break;
case self::OP_BINSTRING:
$this->loadBinString();
break;
case self::OP_SHORT_BINSTRING:
$this->loadShortBinString();
break;
case self::OP_BINBYTES:
$this->loadBinBytes();
break;
case self::OP_SHORT_BINBYTES:
$this->loadShortBinBytes();
break;
case self::OP_UNICODE:
$this->loadUnicode();
break;
case self::OP_BINUNICODE:
$this->loadBinUnicode();
break;
case self::OP_MARK:
$this->loadMark();
break;
case self::OP_LIST:
$this->loadList();
break;
case self::OP_EMPTY_LIST:
$this->loadEmptyList();
break;
case self::OP_APPEND:
$this->loadAppend();
break;
case self::OP_APPENDS:
$this->loadAppends();
break;
case self::OP_DICT:
$this->loadDict();
break;
case self::OP_EMPTY_DICT:
$this->_loadEmptyDict();
break;
case self::OP_SETITEM:
$this->loadSetItem();
break;
case self::OP_SETITEMS:
$this->loadSetItems();
break;
case self::OP_TUPLE:
$this->loadTuple();
break;
case self::OP_TUPLE1:
$this->loadTuple1();
break;
case self::OP_TUPLE2:
$this->loadTuple2();
break;
case self::OP_TUPLE3:
$this->loadTuple3();
break;
case self::OP_PROTO:
$this->loadProto();
break;
default:
throw new Exception\RuntimeException("Invalid or unknown opcode '{$op}'");
}
}
/**
* Load a PUT opcode
*
* @throws Exception\RuntimeException on missing stack
*/
protected function loadPut()
{
$id = (int) $this->readline();
$lastStack = count($this->stack) - 1;
if (!isset($this->stack[$lastStack])) {
throw new Exception\RuntimeException('No stack exist');
}
$this->memo[$id] =& $this->stack[$lastStack];
}
/**
* Load a binary PUT
*
* @throws Exception\RuntimeException on missing stack
*/
protected function loadBinPut()
{
$id = ord($this->read(1));
$lastStack = count($this->stack)-1;
if (!isset($this->stack[$lastStack])) {
throw new Exception\RuntimeException('No stack exist');
}
$this->memo[$id] =& $this->stack[$lastStack];
}
/**
* Load a long binary PUT
*
* @throws Exception\RuntimeException on missing stack
*/
protected function loadLongBinPut()
{
$bin = $this->read(4);
if (static::$isLittleEndian === false) {
$bin = strrev($bin);
}
list(, $id) = unpack('l', $bin);
$lastStack = count($this->stack)-1;
if (!isset($this->stack[$lastStack])) {
throw new Exception\RuntimeException('No stack exist');
}
$this->memo[$id] =& $this->stack[$lastStack];
}
/**
* Load a GET operation
*
* @throws Exception\RuntimeException on missing GET identifier
*/
protected function loadGet()
{
$id = (int) $this->readline();
if (!array_key_exists($id, $this->memo)) {
throw new Exception\RuntimeException('Get id "' . $id . '" not found in memo');
}
$this->stack[] =& $this->memo[$id];
}
/**
* Load a binary GET operation
*
* @throws Exception\RuntimeException on missing GET identifier
*/
protected function loadBinGet()
{
$id = ord($this->read(1));
if (!array_key_exists($id, $this->memo)) {
throw new Exception\RuntimeException('Get id "' . $id . '" not found in memo');
}
$this->stack[] =& $this->memo[$id];
}
/**
* Load a long binary GET operation
*
* @throws Exception\RuntimeException on missing GET identifier
*/
protected function loadLongBinGet()
{
$bin = $this->read(4);
if (static::$isLittleEndian === false) {
$bin = strrev($bin);
}
list(, $id) = unpack('l', $bin);
if (!array_key_exists($id, $this->memo)) {
throw new Exception\RuntimeException('Get id "' . $id . '" not found in memo');
}
$this->stack[] =& $this->memo[$id];
}
/**
* Load a NONE operator
*/
protected function loadNone()
{
$this->stack[] = null;
}
/**
* Load a boolean TRUE operator
*/
protected function loadNewTrue()
{
$this->stack[] = true;
}
/**
* Load a boolean FALSE operator
*/
protected function loadNewFalse()
{
$this->stack[] = false;
}
/**
* Load an integer operator
*/
protected function loadInt()
{
$line = $this->readline();
if ($line === '01') {
$this->stack[] = true;
} elseif ($line === '00') {
$this->stack[] = false;
} else {
$this->stack[] = (int) $line;
}
}
/**
* Load a binary integer operator
*/
protected function loadBinInt()
{
$bin = $this->read(4);
if (static::$isLittleEndian === false) {
$bin = strrev($bin);
}
list(, $int) = unpack('l', $bin);
$this->stack[] = $int;
}
/**
* Load the first byte of a binary integer
*/
protected function loadBinInt1()
{
$this->stack[] = ord($this->read(1));
}
/**
* Load the second byte of a binary integer
*/
protected function loadBinInt2()
{
$bin = $this->read(2);
list(, $int) = unpack('v', $bin);
$this->stack[] = $int;
}
/**
* Load a long (float) operator
*/
protected function loadLong()
{
$data = rtrim($this->readline(), 'L');
if ($data === '') {
$this->stack[] = 0;
} else {
$this->stack[] = $data;
}
}
/**
* Load a one byte long integer
*/
protected function loadLong1()
{
$n = ord($this->read(1));
$data = $this->read($n);
$this->stack[] = $this->decodeBinLong($data);
}
/**
* Load a 4 byte long integer
*
*/
protected function loadLong4()
{
$nBin = $this->read(4);
if (static::$isLittleEndian === false) {
$nBin = strrev($$nBin);
}
list(, $n) = unpack('l', $nBin);
$data = $this->read($n);
$this->stack[] = $this->decodeBinLong($data);
}
/**
* Load a float value
*
*/
protected function loadFloat()
{
$float = (float) $this->readline();
$this->stack[] = $float;
}
/**
* Load a binary float value
*
*/
protected function loadBinFloat()
{
$bin = $this->read(8);
if (static::$isLittleEndian === true) {
$bin = strrev($bin);
}
list(, $float) = unpack('d', $bin);
$this->stack[] = $float;
}
/**
* Load a string
*
*/
protected function loadString()
{
$this->stack[] = $this->unquoteString((string) $this->readline());
}
/**
* Load a binary string
*
*/
protected function loadBinString()
{
$bin = $this->read(4);
if (!static::$isLittleEndian) {
$bin = strrev($bin);
}
list(, $len) = unpack('l', $bin);
$this->stack[] = (string) $this->read($len);
}
/**
* Load a short binary string
*
*/
protected function loadShortBinString()
{
$len = ord($this->read(1));
$this->stack[] = (string) $this->read($len);
}
/**
* Load arbitrary binary bytes
*/
protected function loadBinBytes()
{
// read byte length
$nBin = $this->read(4);
if (static::$isLittleEndian === false) {
$nBin = strrev($$nBin);
}
list(, $n) = unpack('l', $nBin);
$this->stack[] = $this->read($n);
}
/**
* Load a single binary byte
*/
protected function loadShortBinBytes()
{
$n = ord($this->read(1));
$this->stack[] = $this->read($n);
}