Repository navigation
Expand file tree
/
Copy pathphpickle_unpickle.php
More file actions
77 lines (68 loc) · 1.59 KB
/
Copy pathphpickle_unpickle.php
File metadata and controls
77 lines (68 loc) · 1.59 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
<?php
require_once "phpickle_stack.php";
require_once "phpickle_memo.php";
require_once "phpickle_stream.php";
require_once "phpickle_gen_read_ops.php";
require_once "phpickle_op_mapper.php";
class phpickle_unpickle
{
private $debug = false;
public function set_debug($onoff)
{
$this->debug = $onoff;
}
/** unpickle from string **/
public function loads($str)
{
$stream = new phpickle_stream("data://text/plain;base64,".base64_encode($str));
return $this->unpickle_stream($stream);
}
/** unpickle from string "encrypted" as django session **/
public function loads_django_session($str)
{
$str = base64_decode($str);
//echo "str = $str \r\n";
list($hash, $pickle) = explode(":", $str, 2);
//echo "hash: $hash, pickle: $pickle \r\n";
return $this->loads($pickle);
}
public function unpickle_stream(phpickle_stream $stream)
{
$reader = new phpickle_read_ops();
$stack = new phpickle_stack();
$memo = new phpickle_memo();
$ops = new phpickle_op_mapper();
if ($this->debug)
{
echo "start unpickle \r\n";
}
while (!$stream->eof())
{
$byte = $stream->get_byte();
if ($this->debug)
{
echo "read command as $byte (".ord($byte).") \r\n";
}
$op_str = $ops->bin2str($byte);
if ($this->debug)
{
echo "op_str = $op_str \r\n";
}
if ($op_str == "STOP")
{
break;
}
$fn = "op_".$op_str;
if ($this->debug)
{
echo "calling $fn \r\n";
}
$reader->$fn($stream, $stack, $memo, $this->debug);
}
return $stack->pop();
}
private function _string_op($bin_op)
{
return array_search($bin_op, $this->pickle_op_codes);
}
}