-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigbinary_serializer.cpp
708 lines (645 loc) · 24.6 KB
/
igbinary_serializer.cpp
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
/*
+----------------------------------------------------------------------+
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author of hhvm fork: Tyson Andre <[email protected]> |
| Author of original igbinary: Oleg Grenrus <[email protected]>|
| See CREDITS for contributors |
+----------------------------------------------------------------------+
*/
#include <stddef.h>
#include <stdint.h>
#include "hash_ptr.hpp"
// For HHVM_VERSION_*
#include "hphp/runtime/version.h"
// TODO: Pin down exact version?
#if HHVM_VERSION_MAJOR < 3 || (HHVM_VERSION_MAJOR == 3 && HHVM_VERSION_MINOR < 22)
#error Unsupported HHVM version
#endif
#include "hphp/runtime/base/string-buffer.h"
#include "hphp/runtime/base/type-string.h"
#include "hphp/runtime/base/type-object.h"
// Includes type-object.h through type-variant.h, so these headers are placed below that block.
#include "ext_igbinary.hpp"
#include "hphp/runtime/base/array-iterator.h"
#include "hphp/runtime/base/builtin-functions.h"
#include "hphp/system/systemlib.h"
using namespace HPHP;
namespace{
const StaticString
s_zero("\0", 1),
s_protected_prefix("\0*\0", 3),
s_serialize("serialize");
inline static void igbinary_serialize_variant(struct igbinary_serialize_data *igsd, const Variant& self);
inline static int igbinary_serialize_array_ref_by_key(struct igbinary_serialize_data *igsd, const uintptr_t key, bool object);
typedef hphp_hash_map<const StringData*, uint32_t, string_data_hash, string_data_same> StringIdMap;
/** Serializer data.
* @author Oleg Grenrus <[email protected]>
*/
struct igbinary_serialize_data {
StringBuffer buffer;
bool scalar; /**< Serializing scalar. */
bool compact_strings; /**< Check for duplicate strings. */
StringIdMap strings; /**< Hash of already serialized strings. */
struct hash_si_ptr references; /**< Hash of already serialized potential references. (non-NULL uintptr_t => int32_t) */
int references_id; /**< Number of things that the unserializer might think are references. >= length of references */
};
inline static int igbinary_serialize_array_ref(struct igbinary_serialize_data *igsd, const Variant& self, bool object);
/* {{{ igbinary_serialize_data_init */
/** Inits igbinary_serialize_data. */
inline static int igbinary_serialize_data_init(struct igbinary_serialize_data *igsd, bool scalar) {
int r = 0;
igsd->buffer.setOutputLimit(StringData::MaxSize);
igsd->scalar = scalar; // TODO use?
if (!igsd->scalar) {
igsd->strings.reserve(16);
hash_si_ptr_init(&igsd->references, 16);
igsd->references_id = 0;
}
igsd->compact_strings = igbinary_should_compact_strings(); /* FIXME allow ini options parsing */
return r;
}
/* {{{ igbinary_serialize8 */
/** Serialize 8bit value. */
inline static void igbinary_serialize8(struct igbinary_serialize_data *igsd, uint8_t i) {
igsd->buffer.append((char)i);
}
/* }}} */
/* {{{ igbinary_serialize16 */
/** Serialize 16bit value. */
inline static void igbinary_serialize16(struct igbinary_serialize_data *igsd, uint16_t i) {
StringBuffer& buf = igsd->buffer;
char* const bytes = buf.appendCursor(2);
bytes[0] = (char) ((i >> 8) & 0xff);
bytes[1] = (char) (i & 0xff);
buf.resize(buf.size() + 2);
}
/* }}} */
/* {{{ igbinary_serialize32 */
/** Serialize 32bit value. */
inline static void igbinary_serialize32(struct igbinary_serialize_data *igsd, uint32_t i) {
StringBuffer& buf = igsd->buffer;
char* const bytes = buf.appendCursor(4);
bytes[0] = (char) ((i >> 24) & 0xff);
bytes[1] = (char) ((i >> 16) & 0xff);
bytes[2] = (char) ((i >> 8) & 0xff);
bytes[3] = (char) (i & 0xff);
buf.resize(buf.size() + 4);
}
/* }}} */
/* {{{ igbinary_serialize64 */
/** Serialize 64bit value. */
inline static int igbinary_serialize64(struct igbinary_serialize_data *igsd, uint64_t i) {
StringBuffer& buf = igsd->buffer;
char* const bytes = buf.appendCursor(8);
bytes[0] = (char) ((i >> 56) & 0xff);
bytes[1] = (char) ((i >> 48) & 0xff);
bytes[2] = (char) ((i >> 40) & 0xff);
bytes[3] = (char) ((i >> 32) & 0xff);
bytes[4] = (char) ((i >> 24) & 0xff);
bytes[5] = (char) ((i >> 16) & 0xff);
bytes[6] = (char) ((i >> 8) & 0xff);
bytes[7] = (char) (i & 0xff);
buf.resize(buf.size() + 8);
return 0;
}
/* }}} */
/* {{{ igbinary_serialize_header */
/** Serializes header. */
inline static void igbinary_serialize_header(struct igbinary_serialize_data *igsd) {
igbinary_serialize32(igsd, IGBINARY_FORMAT_VERSION); /* version */
}
/* }}} */
/* {{{ igbinary_serialize_null */
/** Serializes null. */
inline static void igbinary_serialize_null(struct igbinary_serialize_data *igsd) {
igbinary_serialize8(igsd, igbinary_type_null);
}
/* }}} */
/** Serializes a boolean. */
inline static void igbinary_serialize_bool(struct igbinary_serialize_data *igsd, int b) {
igbinary_serialize8(igsd, b ? igbinary_type_bool_true : igbinary_type_bool_false);
}
/* }}} */
/* {{{ igbinary_serialize_int64 */
/** Serializes 64-bit integer. */
inline static void igbinary_serialize_int64(struct igbinary_serialize_data *igsd, int64_t l) {
uint64_t k = l >= 0 ? l : -l;
bool p = l >= 0;
/* -ZEND_LONG_MIN is 0 otherwise. */
if (l == INT64_MIN) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_long64n);
igbinary_serialize64(igsd, (uint64_t) 0x8000000000000000);
return;
}
if (k <= 0xff) {
igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long8p : igbinary_type_long8n));
igbinary_serialize8(igsd, (uint8_t) k);
} else if (k <= 0xffff) {
igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long16p : igbinary_type_long16n));
igbinary_serialize16(igsd, (uint16_t) k);
} else if (k <= 0xffffffff) {
igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long32p : igbinary_type_long32n));
igbinary_serialize32(igsd, (uint32_t) k);
} else {
igbinary_serialize8(igsd, (uint8_t) (p ? igbinary_type_long64p : igbinary_type_long64n));
igbinary_serialize64(igsd, (uint64_t) k);
}
}
/* }}} */
/* {{{ igbinary_serialize_double */
/** Serializes double. */
inline static void igbinary_serialize_double(struct igbinary_serialize_data *igsd, double d) {
union {
double d;
uint64_t u;
} u;
igbinary_serialize8(igsd, igbinary_type_double);
u.d = d;
igbinary_serialize64(igsd, u.u);
}
/* }}} */
/* {{{ igbinary_serialize_append_bytes */
inline static void igbinary_serialize_append_bytes(struct igbinary_serialize_data *igsd, const char* data, const size_t len) {
StringBuffer& buf = igsd->buffer;
char* const bytes = buf.appendCursor(len);
memcpy(bytes, data, len);
buf.resize(buf.size() + len);
}
/* {{{ igbinary_serialize_chararray */
/** Serializes string data. */
inline static int igbinary_serialize_chararray(struct igbinary_serialize_data *igsd, const StringData* string) {
const size_t len = string->size();
if (len <= 0xff) {
igbinary_serialize8(igsd, igbinary_type_string8);
igbinary_serialize8(igsd, len);
} else if (len <= 0xffff) {
igbinary_serialize8(igsd, igbinary_type_string16);
igbinary_serialize16(igsd, len);
} else if (LIKELY(len <= 0xffffffff)) {
igbinary_serialize8(igsd, igbinary_type_string32);
igbinary_serialize32(igsd, len);
} else {
throw IgbinaryWarning("igbinary_serialize_chararray: Too long for other igbinary v2 implementations to parse");
}
igbinary_serialize_append_bytes(igsd, string->data(), len);
return 0;
}
/* }}} */
/* {{{ igbinary_serialize_string */
/** Serializes string.
* Serializes each string once, after first time uses pointers.
*/
inline static void igbinary_serialize_string(struct igbinary_serialize_data *igsd, const StringData* string) {
if (string->size() == 0) {
igbinary_serialize8(igsd, igbinary_type_string_empty);
return;
}
if (igsd->scalar || !igsd->compact_strings) {
igbinary_serialize_chararray(igsd, string);
return;
}
auto result = igsd->strings.insert(std::pair<const StringData*, uint32_t>(string, (uint32_t)igsd->strings.size()));
if (result.second) {
igbinary_serialize_chararray(igsd, string);
return;
}
uint32_t t = result.first->second; // old value.
if (t <= 0xff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_string_id8);
igbinary_serialize8(igsd, (uint8_t) t);
} else if (t <= 0xffff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_string_id16);
igbinary_serialize16(igsd, (uint16_t) t);
} else {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_string_id32);
igbinary_serialize32(igsd, (uint32_t) t);
}
}
/* }}} */
/* {{{ igbinary_serialize_array_key */
/** Serializes an array/object key */
inline static void igbinary_serialize_array_key(struct igbinary_serialize_data *igsd, const Variant& self) {
/* Same as igbinary7 igbinary_serialize_zval */
/* TODO: Figure out how to handle references and garbage collection */
auto tv = self.asTypedValue();
switch (tv->m_type) {
case KindOfInt64:
igbinary_serialize_int64(igsd, tv->m_data.num);
return;
case KindOfString:
#if HHVM_VERSION_MAJOR > 3 || (HHVM_VERSION_MAJOR >= 3 && HHVM_VERSION_MINOR >= 12)
case KindOfPersistentString:
#endif
igbinary_serialize_string(igsd, tv->m_data.pstr);
return;
default:
throw IgbinaryWarning("igbinary_serialize_array_key: Did not expect to get DataType 0x%x", (int) tv->m_type);
}
}
/* }}} */
/* {{{ igbinay_serialize_array */
/** Serializes array or objects inner properties */
inline static void igbinary_serialize_array(struct igbinary_serialize_data *igsd, const Variant& self, bool object) {
auto tv = self.asTypedValue();
const ArrayData* arr;
switch (tv->m_type) {
#if HHVM_VERSION_MAJOR > 3 || (HHVM_VERSION_MAJOR >= 3 && HHVM_VERSION_MINOR >= 11)
case KindOfPersistentArray:
#endif
case KindOfArray:
arr = tv->m_data.parr;
break;
case KindOfRef:
{
Variant& self_deref = *tv->m_data.pref->var();
auto tv_deref = self_deref.asTypedValue();
switch (tv_deref->m_type) {
#if HHVM_VERSION_MAJOR > 3 || (HHVM_VERSION_MAJOR >= 3 && HHVM_VERSION_MINOR >= 11)
case KindOfPersistentArray:
#endif
case KindOfArray:
arr = tv_deref->m_data.parr;
break;
default:
throw IgbinaryWarning("igbinary_serialize_array: Did not expect to get ref to DataType 0x%x", (int) tv->m_type);
}
}
break;
default:
throw IgbinaryWarning("igbinary_serialize_array: Did not expect to get DataType 0x%x", (int) tv->m_type);
}
size_t n = arr->size();
if (!object && igbinary_serialize_array_ref(igsd, self, false) == 0) {
return;
}
// TODO: Support refs.
if (n <= 0xff) {
igbinary_serialize8(igsd, igbinary_type_array8);
igbinary_serialize8(igsd, n);
} else if (n <= 0xffff) {
igbinary_serialize8(igsd, igbinary_type_array16);
igbinary_serialize16(igsd, n);
} else {
igbinary_serialize8(igsd, igbinary_type_array32);
igbinary_serialize32(igsd, n);
}
if (n == 0) {
return;
}
if (false) {
#if HHVM_VERSION_MAJOR > 3 || (HHVM_VERSION_MAJOR >= 3 && HHVM_VERSION_MINOR >= 14)
} else if (arr->isVecArray()) {
// it wouldn't be unserializable by php5 and php7.
// Maybe igbinary_keyset, igbinary_vecarray for those, or SPL....
throw new IgbinaryWarning("igbinary_serialize_array: Unable to handle case of isVecArray");
#endif
#if HHVM_VERSION_MAJOR > 3 || (HHVM_VERSION_MAJOR >= 3 && HHVM_VERSION_MINOR >= 15)
} else if (arr->isKeyset()) {
// TODO find exact patch version(s)?
// it wouldn't be unserializable by php5 and php7.
// Maybe igbinary_keyset, igbinary_vecarray for those, or SPL....
throw new IgbinaryWarning("igbinary_serialize_array: Unable to handle case of isKeyset");
#endif
} else {
for (ArrayIter iter(arr); iter; ++iter) {
// FIXME check if int or string?
igbinary_serialize_array_key(igsd, iter.first());
igbinary_serialize_variant(igsd, iter.secondRef());
}
}
}
/* }}} */
/* {{{ igbinary_serialize_object_name */
/** Serialize object name. */
inline static void igbinary_serialize_object_name(struct igbinary_serialize_data *igsd, const StringData* class_name) {
// TODO: optimize
const auto result = igsd->strings.insert(std::pair<const StringData*, uint32_t>(class_name, (uint32_t)igsd->strings.size()));
if (result.second) { // First time the class name was used as a string.
auto name_len = class_name->size();
if (name_len <= 0xff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object8);
igbinary_serialize8(igsd, (uint8_t) name_len TSRMLS_CC);
} else if (name_len <= 0xffff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object16 TSRMLS_CC);
igbinary_serialize16(igsd, (uint16_t) name_len TSRMLS_CC);
} else {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object32 TSRMLS_CC);
igbinary_serialize32(igsd, (uint32_t) name_len TSRMLS_CC);
}
igbinary_serialize_append_bytes(igsd, class_name->data(), class_name->size());
return;
}
const uint32_t t = result.first->second;
/* already serialized string */
if (t <= 0xff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object_id8);
igbinary_serialize8(igsd, (uint8_t) t);
} else if (t <= 0xffff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object_id16);
igbinary_serialize16(igsd, (uint16_t) t);
} else {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object_id32 TSRMLS_CC);
igbinary_serialize32(igsd, (uint32_t) t);
}
}
/* }}} */
/* {{{ igbinary_serialize_object_serialize_data */
inline static void igbinary_serialize_object_serialize_data(struct igbinary_serialize_data* igsd, const StrNR& classname, const String& serializedData) {
igbinary_serialize_object_name(igsd, classname.get());
const size_t serialized_len = serializedData.length();
if (serialized_len <= 0xff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object_ser8);
igbinary_serialize8(igsd, (uint8_t) serialized_len TSRMLS_CC);
} else if (serialized_len <= 0xffff) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object_ser16);
igbinary_serialize16(igsd, (uint16_t) serialized_len TSRMLS_CC);
} else if (LIKELY(serialized_len <= 0xffffffffL)) {
igbinary_serialize8(igsd, (uint8_t) igbinary_type_object_ser32 TSRMLS_CC);
igbinary_serialize32(igsd, (uint32_t) serialized_len TSRMLS_CC);
} else {
throw IgbinaryWarning("igbinary_serialize_object_serialize_data: Data is too long?");
}
igbinary_serialize_append_bytes(igsd, serializedData.data(), serialized_len);
}
/* }}} */
/* {{{ igbinary_serialize_object */
/** Serialize object.
* @see ext/standard/var.c
* */
inline static void igbinary_serialize_object(struct igbinary_serialize_data *igsd, const ObjectData* obj) {
if (!obj) {
igbinary_serialize_null(igsd);
return;
}
const uintptr_t key = reinterpret_cast<uintptr_t>(obj);
if (igbinary_serialize_array_ref_by_key(igsd, key, true) == 0) {
return;
}
if (obj->isCollection()) {
throw IgbinaryWarning("igbinary_serialize_object: Unsupported type isCollection");
}
if (obj->instanceof(SystemLib::s_SerializableClass)) {
assert(!obj->isCollection());
Variant ret =
const_cast<ObjectData*>(obj)->o_invoke_few_args(s_serialize, 0);
if (ret.isString()) {
igbinary_serialize_object_serialize_data(igsd, obj->getClassName(), ret.toString());
} else if (ret.isNull()) {
igbinary_serialize_null(igsd);
} else {
raise_error("%s::serialize() must return a string or NULL",
obj->getClassName().data());
}
return;
}
bool handleSleep = false;
auto cls = obj->getVMClass();
// From serializeObject:
// Only serialize CPP extension type instances which can actually
// be deserialized. Otherwise, raise a warning and serialize
// null.
// Similarly, do not try to serialize WaitHandles
// as they contain internal state via non-NativeData means.
//
// FIXME - don't unserialize other internal objects unless a configure option is provided to allow it.
if ((cls->instanceCtor() && !cls->isCppSerializable()) ||
obj->getAttribute(ObjectData::IsWaitHandle)) {
// Compatibility with php5: Don't unserialize the rest of the properties. Just stop unserializing.
throw_igbinary_exception("Attempted to serialize unserializable builtin class %s", obj->getVMClass()->preClass()->name()->data());
}
if (obj->getAttribute(ObjectData::HasNativeData)) {
throw IgbinaryWarning("Can't serialize object of class %s with native data?", obj->getClassName().data());
}
Variant ret;
if (obj->getAttribute(ObjectData::HasSleep)) {
handleSleep = true;
ret = const_cast<ObjectData*>(obj)->invokeSleep();
}
if (obj->getAttribute(ObjectData::HasNativeData)) {
throw IgbinaryWarning("TODO: Serializing native data not supported, not compatible with php5 igbinary?");
}
if (handleSleep) {
assert(!obj->isCollection());
if (!ret.isArray()) {
raise_notice("serialize(): __sleep should return an array only "
"containing the names of instance-variables to "
"serialize");
igbinary_serialize_null(igsd);
return;
}
const Array &props = ret.asCArrRef();
auto const obj_cls = obj->getVMClass();
igbinary_serialize_object_name(igsd, obj->getClassName().get());
const size_t n = props.size();
if (n <= 0xff) {
igbinary_serialize8(igsd, igbinary_type_array8);
igbinary_serialize8(igsd, n);
} else if (n <= 0xffff) {
igbinary_serialize8(igsd, igbinary_type_array16);
igbinary_serialize16(igsd, n);
} else if (n <= 0xffffffffL) {
igbinary_serialize8(igsd, igbinary_type_array32);
igbinary_serialize32(igsd, n);
}
for (ArrayIter iter(props); iter; ++iter) {
Class* ctx = obj_cls;
const Variant& memberKey = iter.second();
if (UNLIKELY(!memberKey.isString())) {
// __sleep doesn't support integers in php5 implementation. Print a notice and skip it.
raise_warning("__sleep should return an array only "
"containing the names of instance-variables to "
"serialize, got %d", (int)memberKey.getType());
igbinary_serialize_null(igsd);
return;
}
String memberName = memberKey.toString();
String propName = memberName;
// const String propName = memberName;
if (memberName.data()[0] == '\0') {
// TODO (Probably easy, but should probably also be done in php?)
throw IgbinaryWarning("igbinary_serialize_data: __sleep returned built in member \"%s\" - this is unsupported", memberName.data());
}
auto const lookup = obj_cls->getDeclPropIndex(ctx, memberName.get());
auto const propIdx = lookup.prop;
if (propIdx != kInvalidSlot) {
if (lookup.accessible) {
auto const prop = &obj->propVec()[propIdx];
if (prop->m_type != KindOfUninit) {
auto const attrs = obj_cls->declProperties()[propIdx].attrs;
if (attrs & AttrPrivate) {
memberName = concat4(s_zero, ctx->nameStr(),
s_zero, memberName);
} else if (attrs & AttrProtected) {
memberName = concat(s_protected_prefix, memberName);
}
igbinary_serialize_string(igsd, memberName.get());
igbinary_serialize_variant(igsd, tvAsCVarRef(prop));
continue;
}
}
}
if (UNLIKELY(obj->getAttribute(ObjectData::HasDynPropArr))) {
// TODO: look in depth at e513c6d6d4a847fd7d09e27f23e4554b6955c0f0
HPHP::member_rval prop = obj->dynPropArray()->rval(memberName.get());
if (prop) {
igbinary_serialize_string(igsd, memberName.get()); // TODO: Integer keys? Can probably ignore.
igbinary_serialize_variant(igsd, tvAsCVarRef(prop.tv_ptr()));
continue;
}
}
raise_notice("igbinary_serialize(): \"%s\" returned as member variable from "
"__sleep() but does not exist", propName.data());
// Note: Serialize null as both
igbinary_serialize_string(igsd, memberName.get()); // TODO: Integer keys?
igbinary_serialize_null(igsd);
}
return;
}
Array properties = obj->toArray(); // FIXME do names differ by visibility?
igbinary_serialize_object_name(igsd, obj->getClassName().get());
igbinary_serialize_array(igsd, properties, true);
}
/* }}} */
/* {{{ igbinary_serialize_array_ref_by_key */
inline static int igbinary_serialize_array_ref_by_key(struct igbinary_serialize_data *igsd, const uintptr_t key, bool object) {
// Serialize it by a key.
uint32_t t = 0;
uint32_t *i = &t;
if (hash_si_ptr_find(&igsd->references, key, i) == 1) {
t = igsd->references_id++;
/* FIXME hack? If the top-level element was an array, we assume that it can't be a reference when we serialize it, */
/* because that's the way it was serialized in php5. */
/* Does this work with different forms of recursive arrays? */
if (t > 0 || object) {
hash_si_ptr_insert(&igsd->references, key, t); /* TODO: Add a specialization for fixed-length numeric keys? */
}
return 1;
} else {
enum igbinary_type type;
if (*i <= 0xff) {
type = object ? igbinary_type_objref8 : igbinary_type_ref8;
igbinary_serialize8(igsd, (uint8_t) type);
igbinary_serialize8(igsd, (uint8_t) *i);
} else if (*i <= 0xffff) {
type = object ? igbinary_type_objref16 : igbinary_type_ref16;
igbinary_serialize8(igsd, (uint8_t) type);
igbinary_serialize16(igsd, (uint16_t) *i);
} else {
type = object ? igbinary_type_objref32 : igbinary_type_ref32;
igbinary_serialize8(igsd, (uint8_t) type);
igbinary_serialize32(igsd, (uint32_t) *i);
}
return 0;
}
return 1;
}
/* }}} */
/* {{{ igbinary_serialize_array_ref */
/** Serializes array reference (or reference in an object). Returns 0 on success. */
inline static int igbinary_serialize_array_ref(struct igbinary_serialize_data *igsd, const Variant& self, bool object) {
auto tv = self.asTypedValue();
uintptr_t key = 0; /* The address of the pointer to the zend_refcounted struct or other struct */
// Aside: it's a union, so these are all equivalent.
switch (tv->m_type) {
#if HHVM_VERSION_MAJOR > 3 || (HHVM_VERSION_MAJOR >= 3 && HHVM_VERSION_MINOR >= 11)
case KindOfPersistentArray:
#endif
case KindOfArray:
key = reinterpret_cast<uintptr_t>(tv->m_data.parr);
break;
case KindOfObject:
// TODO: Does this need to use o_id? Probably not.
key = reinterpret_cast<uintptr_t>(tv->m_data.pobj);
break;
case KindOfRef:
{
RefData* pref = tv->m_data.pref;
Variant& self_deref = *pref->var();
auto tv_deref = self_deref.asTypedValue();
if (tv_deref->m_type == KindOfObject) {
// For objects, give the references and non-references the same id. Dereference it.
key = reinterpret_cast<uintptr_t>(tv_deref->m_data.pobj);
} else {
key = reinterpret_cast<uintptr_t>(tv->m_data.pref);
}
}
// TODO ref to objects, check bool object
break;
default:
throw IgbinaryWarning("igbinary_serialize_array_ref expected object, array, or reference, got none of those : DataType=0x%02x", (int) tv->m_type);
}
return igbinary_serialize_array_ref_by_key(igsd, key, object);
}
/* }}} */
/* {{{ igbinary_serialize_variant */
/** Serializes a variant. Guaranteed not to be an array/object key. */
inline static void igbinary_serialize_variant(struct igbinary_serialize_data *igsd, const Variant& self) {
/* Same as igbinary7 igbinary_serialize_zval */
/* TODO: Figure out how to handle references and garbage collection */
auto tv = self.asTypedValue();
switch (tv->m_type) {
case KindOfUninit:
case KindOfNull:
igbinary_serialize_null(igsd);
return;
case KindOfResource:
igbinary_serialize_null(igsd); // Can't serialize resources.
return;
case KindOfBoolean:
igbinary_serialize_bool(igsd, tv->m_data.num != 0);
return;
case KindOfInt64:
igbinary_serialize_int64(igsd, tv->m_data.num);
return;
case KindOfDouble:
igbinary_serialize_double(igsd, tv->m_data.dbl);
return;
case KindOfString:
case KindOfPersistentString:
igbinary_serialize_string(igsd, tv->m_data.pstr);
return;
case KindOfObject:
igbinary_serialize_object(igsd, tv->m_data.pobj);
return;
case KindOfPersistentArray:
case KindOfArray:
igbinary_serialize_array(igsd, self, false);
return;
case KindOfRef:
{
Variant& self_deref = *tv->m_data.pref->var();
igbinary_serialize8(igsd, (uint8_t) igbinary_type_ref);
if (self_deref.isArray()) {
igbinary_serialize_array(igsd, self, false);
return;
} else if (self_deref.isObject()) {
// Nothing else to do, already recorded that it was an object and a reference.
} else {
if (igbinary_serialize_array_ref(igsd, self, false) == 0) {
return; // it was already serialized, and we printed the reference.
}
}
igbinary_serialize_variant(igsd, self_deref);
}
return;
default:
throw IgbinaryWarning("igbinary_serialize_variant: Not implemented yet for DataType 0x%x", (int) tv->m_type);
}
}
/* }}} */
} // namespace
namespace HPHP {
Variant igbinary_serialize(const Variant& variant) {
struct igbinary_serialize_data igsd;
igbinary_serialize_data_init(&igsd, !variant.isObject() && !variant.isArray());
igbinary_serialize_header(&igsd);
try {
igbinary_serialize_variant(&igsd, variant); // Succeed or throw
} catch (IgbinaryWarning& e) {
raise_warning(e.getMessage());
return false;
}
return igsd.buffer.detach();
}
} // HPHP