Repository navigation
Expand file tree
/
Copy pathphpickle_pickle.php
More file actions
315 lines (295 loc) · 6.39 KB
/
Copy pathphpickle_pickle.php
File metadata and controls
315 lines (295 loc) · 6.39 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
<?php
require_once "phpickle_stack.php";
require_once "phpickle_memo.php";
require_once "phpickle_stream.php";
require_once "phpickle_gen_write_ops.php";
require_once "phpickle_op_mapper.php";
class phpickle_pickle
{
private $_stream;
private $_ops;
private $_debug = false;
public function set_debug($state)
{
$this->_debug = $state;
}
/** pickles the data given in $data **/
public function dumps($data, $proto = -1)
{
$this->_stream = new phpickle_stream("data://text/plain;base64,".base64_encode(""));
$this->_ops = new phpickle_write_ops();
$this->_stack = new phpickle_stack();
$this->_memo = new phpickle_memo();
$this->_mapper = new phpickle_op_mapper();
$this->_proto = ($proto == -1 ? 2 : $proto);
if ($this->_proto >= 2)
{
$this->_call_op("PROTO", 2, $this->_stream);
}
$this->_pickle($data, $this->_stream);
return $this->_stream->get_contents().".";
}
private function _pickle($data, &$stream)
{
$type = gettype($data);
if ($type == "unknown type")
{
return;
}
$op = "_pickle_".$type;
$this->$op($data, $stream);
}
private function _pickle_boolean($data, &$stream)
{
if ($data == true)
{
$this->_call_op($this->_proto >= 2 ? "NEWTRUE" : "TRUE", null, $stream);
}
else
{
$this->_call_op($this->_proto >= 2 ? "NEWFALSE" : "FALSE", null, $stream);
}
}
private function _pickle_integer($data, &$stream)
{
if ($this->_proto >= 1 && $data >= 0 && $data <= 0xffff)
{
if ($data <= 0xff)
{
$this->_call_op("BININT1", $data, $stream);
}
else
{
$this->_call_op("BININT2", $data, $stream);
}
}
else
if ($this->_proto >= 2)
{
$this->_call_op("LONG1", $data, $stream);
}
else
if ($this->_proto >= 1)
{
$this->_call_op("BININT", $data, $stream);
}
else
{
$this->_call_op("INT", $data, $stream);
}
}
private function _pickle_double($data, &$stream)
{
if ($this->_proto >= 1)
{
$this->_call_op("BINFLOAT", $data, $stream);
}
else
{
$this->_call_op("FLOAT", $data, $stream);
}
}
private function _pickle_string($data, &$stream)
{
if ($this->_str_has_bin($data))
{
// if string has binary data, save to SHORT_BINBYTES or BINBYTES
if (strlen($data) < 0xff)
{
$this->_call_op("SHORT_BINBYTES", $data, $stream);
}
else
{
$this->_call_op("BINBYTES", $data, $stream);
}
}
else
{
if ($this->_proto > 0)
{
if (($idx = $this->_memo->has($data)) !== false)
{
$this->_call_op("BINGET", $idx, $stream);
}
else
{
if (strlen($data) < 0xff)
{
$this->_call_op("SHORT_BINSTRING", $data, $stream);
}
else
{
$this->_call_op("BINSTRING", $data, $stream);
}
$this->_call_op("BINPUT", $this->_memo->put($data), $stream);
}
}
else
{
if (($idx = $this->_memo->has($data)) !== false)
{
$this->_call_op("GET", $idx, $stream);
}
else
{
$this->_call_op("STRING", $data, $stream);
$this->_call_op("PUT", $this->_memo->put($data), $stream);
}
}
}
}
private function _pickle_array($data, &$stream)
{
if ($this->_array_has_keys($data))
{
$this->_pickle_dict($data, $stream);
}
else
{
$this->_pickle_list($data, $stream);
}
}
private function _pickle_list($data, $stream)
{
// LIST
if ($this->_proto == 0)
{
$this->_call_op("MARK", null, $stream);
$this->_call_op("LIST", null, $stream);
$this->_call_op("PUT", $this->_memo->put(array()), $stream);
}
else
{
$this->_call_op("EMPTY_LIST", null, $stream);
$this->_call_op("BINPUT", $this->_memo->put(array()), $stream);
$this->_call_op("MARK", null, $stream);
}
foreach($data as $v)
{
$this->_pickle($v, $stream);
if ($this->_proto == 0)
{
$this->_call_op("APPEND", null, $stream);
}
}
if ($this->_proto > 0)
{
$this->_call_op("APPENDS", null, $stream);
}
}
private function _pickle_dict($data, &$stream)
{
// DICT
if ($this->_proto == 0)
{
$this->_call_op("MARK", null, $stream);
$this->_call_op("DICT", null, $stream);
$this->_call_op("PUT", $this->_memo->put(array()), $stream);
}
else
{
$this->_call_op("EMPTY_DICT", null, $stream);
$this->_call_op("BINPUT", $this->_memo->put(array()), $stream);
if (count($data) > 1)
{
$this->_call_op("MARK", null, $stream);
}
}
foreach($data as $k => $v)
{
$this->_pickle($k, $stream);
$this->_pickle($v, $stream);
if ($this->_proto == 0)
{
$this->_call_op("SETITEM", null, $stream);
}
}
if ($this->_proto > 0 && count($data) > 0)
{
if (count($data) == 1)
{
$this->_call_op("SETITEM", null, $stream);
}
else
{
$this->_call_op("SETITEMS", null, $stream);
}
}
}
private function _pickle_object($data, &$stream)
{
// if unpickled from python object, then try to reconstruct the original stream
// else just forget it
if (isset($data->__python_class__))
{
if (isset($data->__python_pickle_op__) && $data->__python_pickle_op__ == "GLOBAL")
{
$this->_call_op("GLOBAL", $data, $stream);
}
else
{
// INST
$this->_call_op("MARK", null, $stream);
if (is_array($data->__python_construct_args__))
{
foreach($data->__python_construct_args__ as $v)
{
$this->_pickle($v, $stream);
}
}
$this->_call_op("INST", $data, $stream);
}
// now, if the object has other properties, then add them as dict and then BUILD
$d = get_object_vars($data);
unset($d["__python_class__"]);
unset($d["__python_pickle_op__"]);
unset($d["__python_construct_args__"]);
if (count($d) > 0)
{
$this->_pickle_dict($d, $stream);
$this->_call_op("BUILD", $data, $stream);
}
}
else
{
// regular php object, serialize to dict
$d = array();
foreach($data as $k => $v)
{
$d[$k] = $v;
}
return $this->_pickle_dict($d, $stream);
}
}
private function _pickle_resource($data, &$stream)
{
// this just isn't happening. really.
}
private function _pickle_NULL($data, &$stream)
{
$this->_call_op("NONE", null, $stream);
}
private function _call_op($op, $arg, &$stream)
{
$op = "op_".$op;
$this->_ops->$op($arg, $stream, $this->_stack, $this->_memo, $this->_debug, $this->_mapper);
}
private function _array_has_keys($array)
{
$num = 0;
foreach($array as $k => $v)
{
if ($k !== $num)
{
return true;
}
$num++;
}
return false;
}
private function _str_has_bin($str)
{
$r = ctype_print($str);
return !$r;
}
}