rm unused code
[awesomized/ext-ion] / ion_private.h
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: ion |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2021, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "ionc/ion.h"
14
15 #include "php.h"
16 #include "ext/date/php_date.h"
17
18 typedef struct php_ion_serializer {
19 ION_WRITER *writer;
20 ION_WRITER_OPTIONS *options;
21 smart_str *buffer;
22
23 zend_string *call_custom;
24 zend_bool call_magic;
25
26 uint32_t level;
27 HashTable *ids;
28 HashTable *tmp;
29 } php_ion_serializer;
30
31 typedef struct php_ion_annotaions {
32 uint8_t backref:1;
33 uint8_t makeref:1;
34 uint8_t object_prop:1;
35 uint8_t object_type;
36 zend_string *object_class;
37 zend_string *property_class;
38 } php_ion_annotations;
39
40 typedef struct php_ion_unserializer {
41 ION_READER *reader;
42 ION_READER_OPTIONS *options;
43 ION_TYPE type;
44
45 zend_string *call_custom;
46 zend_bool call_magic;
47
48 uint32_t level;
49 HashTable *ids;
50 HashTable *tmp;
51 HashTable *addref;
52
53 php_ion_annotations annotations;
54 } php_ion_unserializer;
55
56 ZEND_BEGIN_MODULE_GLOBALS(ion)
57
58 php_ion_serializer serializer;
59 php_ion_unserializer unserializer;
60
61 struct {
62 HashTable serializer[2];
63 HashTable unserializer[3];
64 } _ht;
65
66 ZEND_END_MODULE_GLOBALS(ion);
67
68 #ifdef ZTS
69 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
70 #else
71 # define php_ion_globals ion_globals
72 #endif
73
74 ZEND_DECLARE_MODULE_GLOBALS(ion);
75
76 static inline void php_ion_globals_serializer_init(void)
77 {
78 php_ion_serializer *s = &php_ion_globals.serializer;
79 HashTable *h = php_ion_globals._ht.serializer;
80
81 zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
82 zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
83 }
84
85 static inline uint32_t php_ion_globals_serializer_step(void)
86 {
87 php_ion_serializer *s = &php_ion_globals.serializer;
88 uint32_t level;
89
90 if (!(level = s->level++)) {
91 zend_hash_clean(s->ids);
92 zend_hash_clean(s->tmp);
93 }
94 return level;
95 }
96
97 static inline uint32_t php_ion_globals_serializer_exit(void)
98 {
99 php_ion_serializer *s = &php_ion_globals.serializer;
100
101 ZEND_ASSERT(s->level);
102 if (!--s->level) {
103 zend_hash_clean(s->ids);
104 zend_hash_clean(s->tmp);
105 }
106 return s->level;
107 }
108
109 static inline void php_ion_globals_serializer_dtor(void)
110 {
111 php_ion_serializer *s = &php_ion_globals.serializer;
112
113 zend_hash_destroy(s->tmp);
114 zend_hash_destroy(s->ids);
115 }
116
117 void ZVAL_ADDREF(zval *zv)
118 {
119 if (Z_ISREF_P(zv)) {
120 Z_TRY_ADDREF_P(Z_REFVAL_P(zv));
121 } else {
122 Z_TRY_ADDREF_P(zv);
123 }
124 }
125 static inline void php_ion_globals_unserializer_init(void)
126 {
127 php_ion_unserializer *s = &php_ion_globals.unserializer;
128 HashTable *h = php_ion_globals._ht.unserializer;
129
130 zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
131 zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
132 zend_hash_init(s->addref = &h[2], 0, NULL, ZVAL_ADDREF, 0);
133 }
134
135 static inline void php_ion_globals_unserializer_step(void)
136 {
137 php_ion_unserializer *s = &php_ion_globals.unserializer;
138
139 if (!s->level++) {
140 zend_hash_clean(s->addref);
141 zend_hash_clean(s->ids);
142 zend_hash_clean(s->tmp);
143 }
144 }
145
146 static inline void php_ion_globals_unserializer_exit(void)
147 {
148 php_ion_unserializer *s = &php_ion_globals.unserializer;
149
150 ZEND_ASSERT(s->level);
151 if (!--s->level) {
152 zend_hash_clean(s->addref);
153 zend_hash_clean(s->ids);
154 zend_hash_clean(s->tmp);
155 }
156 }
157
158 static inline void php_ion_globals_unserializer_dtor(void)
159 {
160 php_ion_unserializer *s = &php_ion_globals.unserializer;
161
162 zend_hash_destroy(s->addref);
163 zend_hash_destroy(s->ids);
164 zend_hash_destroy(s->tmp);
165 }
166
167 static zend_class_entry
168 *ce_Annotation,
169 *ce_Catalog,
170 *ce_Collection,
171 *ce_Decimal,
172 *ce_Decimal_Context,
173 *ce_Reader,
174 *ce_Reader_Options,
175 *ce_Reader_Reader,
176 *ce_Reader_Buffer,
177 *ce_Reader_Stream,
178 *ce_Reader_Buffer_Reader,
179 *ce_Reader_Stream_Reader,
180 *ce_Serializer,
181 *ce_Serializer_PHP,
182 *ce_Symbol,
183 *ce_Symbol_ImportLocation,
184 *ce_Symbol_System,
185 *ce_Symbol_System_SID,
186 *ce_Symbol_Table,
187 *ce_Timestamp,
188 *ce_Timestamp_Precision,
189 *ce_Type,
190 *ce_Unserializer,
191 *ce_Unserializer_PHP,
192 *ce_Writer,
193 *ce_Writer_Options,
194 *ce_Writer_Buffer,
195 *ce_Writer_Buffer_Writer,
196 *ce_Writer_Stream,
197 *ce_Writer_Stream_Writer,
198 *ce_Writer_Writer
199 ;
200
201 #define php_ion_decl(type, cname, ...) \
202 static zend_object_handlers oh_ ## cname; \
203 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
204 { \
205 if (!ce) ce = ce_ ## cname; \
206 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
207 zend_object_std_init(&o->std, ce); \
208 object_properties_init(&o->std, ce); \
209 o->std.handlers = &oh_ ## cname; \
210 return &o->std; \
211 } \
212 static inline void free_ion_ ## cname(zend_object *std) \
213 { \
214 php_ion_ ## type *obj = php_ion_obj(type, std); \
215 __VA_ARGS__; \
216 zend_object_std_dtor(std); \
217 (void) obj; \
218 }
219 #define php_ion_register(type, cname, ...) do { \
220 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
221 ce_ ## cname ->create_object = create_ion_ ## cname; \
222 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
223 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
224 oh_ ## cname .free_obj = free_ion_ ## cname; \
225 } while (0)
226
227 #define php_ion_obj(type, obj) \
228 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
229
230 #define ION_CHECK(err, ...) do { \
231 iERR __err = err; \
232 if (__err) { \
233 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
234 __VA_ARGS__; \
235 return; \
236 } \
237 } while (0)
238
239 #define ION_CATCH(...) do { \
240 if (EG(exception)) { \
241 __VA_ARGS__; \
242 return; \
243 } \
244 } while (0)
245
246 #define PTR_CHECK(ptr, ...) do { \
247 if (!(ptr)) { \
248 zend_throw_error(NULL, "Uninitialized object"); \
249 __VA_ARGS__; \
250 return; \
251 } \
252 } while (0)
253
254 #define OBJ_CHECK(obj) do { \
255 PTR_CHECK(obj); \
256 PTR_CHECK(*((void **)obj)); \
257 } while (0)
258
259 static inline ION_STRING *ion_string_from_cstr(ION_STRING *is, const char *s, size_t l)
260 {
261 is->length = l;
262 is->value = (BYTE *) s;
263 return is;
264 }
265
266 static inline ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs)
267 {
268 is->length = zs ? zs->len : 0;
269 is->value = (BYTE *) (zs ? zs->val : NULL);
270 return is;
271 }
272
273 static inline zend_string *zend_string_from_ion(const ION_STRING *s)
274 {
275 return zend_string_init((const char *) s->value, s->length, 0);
276 }
277
278 static inline void update_property_obj(zend_object *obj, const char *n, size_t l, zend_object *p)
279 {
280 zval zobj;
281 ZVAL_OBJ(&zobj, p);
282 zend_update_property(obj->ce, obj, n, l, &zobj);
283 }
284
285 typedef struct php_ion_type {
286 ION_TYPE typ;
287 zend_object std;
288 } php_ion_type;
289
290 php_ion_decl(type, Type);
291
292 #define RETURN_IONTYPE(typ) do { \
293 zend_object *__zo = php_ion_type_fetch(typ); \
294 if (UNEXPECTED(!__zo)) { \
295 RETURN_THROWS(); \
296 } \
297 RETURN_OBJ_COPY(__zo); \
298 } while(0)
299
300 static inline zend_object *php_ion_type_fetch(ION_TYPE typ)
301 {
302 zend_long index = ION_TYPE_INT(typ);
303 zval *ztype = zend_hash_index_find(ce_Type->backed_enum_table, index);
304
305 if (UNEXPECTED(!ztype || Z_TYPE_P(ztype) != IS_STRING)) {
306 zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", index, ZSTR_VAL(ce_Type->name));
307 return NULL;
308 }
309 return zend_enum_get_case(ce_Type, Z_STR_P(ztype));
310 }
311
312 typedef struct php_ion_symbol_iloc {
313 ION_SYMBOL_IMPORT_LOCATION loc;
314 zend_string *name;
315 zend_object std;
316 } php_ion_symbol_iloc;
317
318 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc *obj)
319 {
320 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("location"), obj->loc.location);
321 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("name"), obj->name);
322 ion_string_from_zend(&obj->loc.name, obj->name);
323 }
324
325 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc *obj)
326 {
327 zend_string_release(obj->name);
328 }
329
330 php_ion_decl(symbol_iloc, Symbol_ImportLocation, php_ion_symbol_iloc_dtor(obj));
331
332 typedef struct php_ion_symbol {
333 ION_SYMBOL sym;
334 zend_string *value;
335 zend_object *iloc, std;
336 } php_ion_symbol;
337
338 static inline void php_ion_symbol_ctor(php_ion_symbol *obj)
339 {
340 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("sid"),
341 obj->sym.sid);
342 if (obj->value) {
343 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("value"), obj->value);
344 } else{
345 zend_update_property_null(obj->std.ce, &obj->std, ZEND_STRL("value"));
346 }
347 ion_string_from_zend(&obj->sym.value, obj->value);
348 if (obj->iloc) {
349 update_property_obj(&obj->std, ZEND_STRL("importLocation"), obj->iloc);
350 obj->sym.import_location = php_ion_obj(symbol_iloc, obj->iloc)->loc;
351 } else {
352 zend_update_property_null(obj->std.ce, &obj->std, ZEND_STRL("importLocation"));
353 }
354 }
355
356 static inline void php_ion_symbol_dtor(php_ion_symbol *obj)
357 {
358 if (obj->value) {
359 zend_string_release(obj->value);
360 }
361 }
362
363 static inline void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value)
364 {
365 object_init_ex(return_value, ce_Symbol);
366 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(return_value));
367
368 sym->sym.sid = sym_ptr->sid;
369 sym->value = zend_string_from_ion(&sym_ptr->value);
370 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
371 zval ziloc;
372 object_init_ex(&ziloc, ce_Symbol_ImportLocation);
373 sym->iloc = Z_OBJ(ziloc);
374
375 php_ion_symbol_iloc *iloc = php_ion_obj(symbol_iloc, sym->iloc);
376 iloc->loc.location = sym_ptr->import_location.location;
377 iloc->name = zend_string_from_ion(&sym_ptr->import_location.name);
378
379 php_ion_symbol_iloc_ctor(iloc);
380 }
381
382 php_ion_symbol_ctor(sym);
383 }
384
385 php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj));
386
387 typedef struct php_ion_symbol_table {
388 zend_object std;
389 } php_ion_symbol_table;
390
391 php_ion_decl(symbol_table, Symbol_Table);
392
393 typedef struct php_ion_decimal_ctx {
394 decContext ctx;
395 zend_object std;
396 } php_ion_decimal_ctx;
397
398 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj) {
399 zval tmp, *zbits = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("bits"), 1, &tmp);
400
401 int bits = 128;
402 if (zbits != &EG(uninitialized_zval)) {
403 bits = Z_LVAL_P(zbits);
404 } else {
405 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("bits"), bits);
406 }
407 switch (bits) {
408 case 32:
409 case 64:
410 case 128:
411 decContextDefault(&obj->ctx, bits);
412 break;
413 default:
414 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
415 "Decimal context only allows 32, 64 or 128 bits");
416 }
417 }
418
419 php_ion_decl(decimal_ctx, Decimal_Context);
420
421 typedef struct php_ion_decimal {
422 ION_DECIMAL dec;
423 zend_object *ctx, std;
424 } php_ion_decimal;
425
426 static inline zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec)
427 {
428 zend_string *zstr = zend_string_alloc(ION_DECIMAL_STRLEN(dec), 0);
429 (void) ion_decimal_to_string(dec, zstr->val);
430 return zend_string_truncate(zstr, strlen(zstr->val), 0);
431 }
432
433 static inline void php_ion_decimal_to_int(ION_DECIMAL *dec, decContext *ctx, zend_long *l)
434 {
435 ION_INT *ii = NULL;
436 ION_CHECK(ion_int_alloc(NULL, &ii));
437 ION_CHECK(ion_decimal_to_ion_int(dec, ctx, ii), ion_int_free(ii));
438 int64_t i64;
439 ION_CHECK(ion_int_to_int64(ii, &i64), ion_int_free(ii));
440 *l = i64;
441 ion_int_free(ii);
442 }
443
444 static inline void php_ion_decimal_ctor(php_ion_decimal *obj)
445 {
446 if (!obj->ctx) {
447 zval zdc;
448 object_init_ex(&zdc, ce_Decimal_Context);
449 obj->ctx = Z_OBJ(zdc);
450 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx));
451 GC_DELREF(obj->ctx);
452 }
453 update_property_obj(&obj->std, ZEND_STRL("context"), obj->ctx);
454
455 if (ion_decimal_is_integer(&obj->dec)) {
456 zend_long l;
457 php_ion_decimal_to_int(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
458 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("number"), l);
459 } else {
460 zend_string *zstr = php_ion_decimal_to_string(&obj->dec);
461 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("number"), zstr);
462 zend_string_release(zstr);
463 }
464 }
465
466 static inline void php_ion_decimal_dtor(php_ion_decimal *obj)
467 {
468 ion_decimal_free(&obj->dec);
469 }
470
471 php_ion_decl(decimal, Decimal, php_ion_decimal_dtor(obj));
472
473 typedef php_date_obj php_ion_timestamp;
474
475 static inline zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx)
476 {
477 decQuad microsecs, result;
478 decQuadMultiply(&result, decQuadFromInt32(&microsecs, 1000000), frac, ctx);
479 return (zend_long) decQuadToUInt32(&result, ctx, DEC_ROUND_HALF_EVEN);
480 }
481
482 static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, zend_long usec, decContext *ctx)
483 {
484 decQuad microsecs, us;
485 return decQuadDivide(frac, decQuadFromInt32(&us, usec), decQuadFromInt32(&microsecs, 1000000), ctx);
486 }
487
488 static inline zend_string *php_dt_format_from_precision(uint8_t precision)
489 {
490 switch (precision) {
491 case ION_TS_FRAC:
492 return zend_string_init(ZEND_STRL("c"), 0);
493 case ION_TS_SEC:
494 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
495 case ION_TS_MIN:
496 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
497 case ION_TS_DAY:
498 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
499 case ION_TS_MONTH:
500 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
501 case ION_TS_YEAR:
502 return zend_string_init(ZEND_STRL("Y\\T"), 0);
503 default:
504 return zend_string_init(ZEND_STRL("c"), 0);
505 }
506 }
507
508 static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContext *ctx, zend_string **fmt)
509 {
510 timelib_time *time = timelib_time_ctor();
511
512 switch (ts->precision) {
513 case ION_TS_FRAC:
514 time->us = php_usec_from_ion(&ts->fraction, ctx);
515 /* fallthrough */
516 case ION_TS_SEC:
517 time->s = ts->seconds;
518 /* fallthrough */
519 case ION_TS_MIN:
520 time->i = ts->minutes;
521 time->h = ts->hours;
522 /* fallthrough */
523 case ION_TS_DAY:
524 time->d = ts->day;
525 /* fallthrough */
526 case ION_TS_MONTH:
527 time->m = ts->month;
528 /* fallthrough */
529 case ION_TS_YEAR:
530 time->y = ts->year;
531 /* fallthrough */
532 default:
533 time->z = ts->tz_offset * 60;
534 }
535
536 if (fmt) {
537 *fmt = php_dt_format_from_precision(ts->precision);
538 }
539 return time;
540 }
541
542 static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_timestamp *ts, decContext *ctx)
543 {
544 memset(buf, 0, sizeof(*buf));
545
546 zval tmp;
547 uint8_t precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp));
548
549 if (!precision || precision > ION_TS_FRAC) {
550 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
551 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision);
552 } else switch ((buf->precision = precision)) {
553 case ION_TS_FRAC:
554 ion_ts_frac_from_usec(&buf->fraction, ts->time->us, ctx);
555 /* fallthrough */
556 case ION_TS_SEC:
557 buf->seconds = ts->time->s;
558 /* fallthrough */
559 case ION_TS_MIN:
560 buf->minutes = ts->time->i;
561 /* fallthrough */
562 case ION_TS_DAY:
563 buf->hours = ts->time->h;
564 buf->day = ts->time->d;
565 /* fallthrough */
566 case ION_TS_MONTH:
567 buf->month = ts->time->m;
568 /* fallthrough */
569 case ION_TS_YEAR:
570 buf->year = ts->time->y;
571 /* fallthrough */
572 default:
573 buf->tz_offset = ts->time->z / 60;
574 }
575
576 return buf;
577 }
578
579 static inline void php_ion_timestamp_ctor(php_ion_timestamp *obj, zend_long precision, zend_string *fmt, zend_string *dt, zval *tz)
580 {
581 if (!obj->time) {
582 php_date_initialize(obj, dt ? dt->val : "", dt ? dt->len : 0, fmt ? fmt->val : NULL, tz, PHP_DATE_INIT_CTOR);
583 }
584 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("precision"), precision);
585
586 fmt = php_dt_format_from_precision(precision);
587 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("format"), fmt);
588 zend_string_release(fmt);
589 }
590
591 static inline void php_ion_timestamp_dtor(php_ion_timestamp *obj)
592 {
593 if (obj->time) {
594 timelib_time_dtor(obj->time);
595 }
596 }
597
598 php_ion_decl(timestamp, Timestamp, php_ion_timestamp_dtor(obj));
599
600 typedef struct php_ion_catalog {
601 ION_CATALOG *cat;
602 zend_object std;
603 } php_ion_catalog;
604
605 php_ion_decl(catalog, Catalog);
606
607 typedef struct php_ion_reader_options {
608 ION_READER_OPTIONS opt;
609 zend_object *cat, *dec_ctx, *cb, std;
610 } php_ion_reader_options;
611
612 php_ion_decl(reader_options, Reader_Options);
613
614 typedef struct php_ion_reader {
615 ION_READER *reader;
616 ION_TYPE state;
617 enum {
618 BUFFER_READER,
619 STREAM_READER,
620 } type;
621 union {
622 zend_string *buffer;
623 struct {
624 php_stream *ptr;
625 ION_STRING buf;
626 } stream;
627 };
628 zend_object *opt, std;
629 } php_ion_reader;
630
631 static inline iERR php_ion_reader_stream_handler(struct _ion_user_stream *user)
632 {
633 php_ion_reader *reader = (php_ion_reader *) user->handler_state;
634 size_t remaining = 0, spare = reader->stream.buf.length;
635
636 if (user->curr && user->limit && (remaining = user->limit - user->curr)) {
637 memmove(reader->stream.buf.value, user->curr, remaining);
638 user->limit -= remaining;
639 spare -= remaining;
640 } else {
641 user->curr = user->limit = reader->stream.buf.value;
642 }
643
644 ssize_t read = php_stream_read(reader->stream.ptr, (char *) user->limit, spare);
645 if (EXPECTED(read > 0)) {
646 user->limit += read;
647 return IERR_OK;
648 }
649
650 if (EXPECTED(read == 0)) {
651 return IERR_EOF;
652 }
653
654 return IERR_READ_ERROR;
655 }
656
657 static inline iERR on_context_change(void *context, ION_COLLECTION *imports)
658 {
659 if (context) {
660 php_ion_reader *obj = php_ion_obj(reader, context);
661 (void) obj;
662 }
663 return IERR_OK;
664 }
665
666 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER = {
667 on_context_change,
668 NULL
669 };
670
671 static inline void php_ion_reader_ctor(php_ion_reader *obj)
672 {
673 iERR err;
674 php_ion_reader_options *opt = php_ion_obj(reader_options, obj->opt);
675
676 if (opt) {
677 opt->opt.context_change_notifier.context = obj;
678 }
679 if (obj->type == STREAM_READER) {
680 PTR_CHECK(obj->stream.ptr);
681 GC_ADDREF(obj->stream.ptr->res);
682
683 php_ion_reader_options *opt = php_ion_obj(reader_options, obj->opt);
684 obj->stream.buf.length = opt ? opt->opt.allocation_page_size : 0x1000;
685 obj->stream.buf.value = emalloc(obj->stream.buf.length);
686 err = ion_reader_open_stream(&obj->reader, obj, php_ion_reader_stream_handler, opt ? &opt->opt : NULL);
687
688 } else {
689 err = ion_reader_open_buffer(&obj->reader,
690 (BYTE *) obj->buffer->val, obj->buffer->len,
691 opt ? &opt->opt : NULL);
692 }
693 if (opt) {
694 opt->opt.context_change_notifier.context = NULL;
695 }
696
697 ION_CHECK(err);
698 OBJ_CHECK(obj);
699 }
700 static inline void php_ion_reader_dtor(php_ion_reader *obj)
701 {
702 if (obj->reader) {
703 ion_reader_close(obj->reader);
704 }
705 if (obj->type == STREAM_READER) {
706 if (obj->stream.buf.value) {
707 efree(obj->stream.buf.value);
708 }
709 if (obj->stream.ptr) {
710 zend_list_delete(obj->stream.ptr->res);
711 }
712 } else {
713 if (obj->buffer) {
714 zend_string_release(obj->buffer);
715 }
716 }
717 }
718
719 php_ion_decl(reader, Reader_Reader, php_ion_reader_dtor(obj));
720
721 typedef struct php_ion_writer_options {
722 ION_WRITER_OPTIONS opt;
723 zend_object *cat, *dec_ctx, *col, std;
724 } php_ion_writer_options;
725
726 php_ion_decl(writer_options, Writer_Options);
727
728 typedef struct php_ion_writer {
729 ION_WRITER *writer;
730 enum {
731 BUFFER_WRITER,
732 STREAM_WRITER,
733 } type;
734 union {
735 struct {
736 zval val;
737 smart_str str;
738 } buffer;
739 struct {
740 ION_STRING buf;
741 php_stream *ptr;
742 } stream;
743 };
744 zend_object *opt, std;
745
746 } php_ion_writer;
747
748 static inline iERR php_ion_writer_stream_handler(struct _ion_user_stream *user)
749 {
750 php_ion_writer *writer = (php_ion_writer *) user->handler_state;
751
752 if (EXPECTED(user->limit && user->curr)) {
753 ptrdiff_t len = user->curr - writer->stream.buf.value;
754 if (len != php_stream_write(writer->stream.ptr, (char *) writer->stream.buf.value, len)) {
755 return IERR_WRITE_ERROR;
756 }
757 }
758 user->curr = writer->stream.buf.value;
759 user->limit = writer->stream.buf.value + writer->stream.buf.length;
760 return IERR_OK;
761 }
762
763 #define REF_STR() do { \
764 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
765 GC_ADDREF(obj->buffer.str.s); \
766 } while (0)
767
768 #define NEW_REF_STR() do {\
769 if (Z_STR_P(ref) != obj->buffer.str.s) { \
770 zval_ptr_dtor(ref); \
771 REF_STR(); \
772 } \
773 } while(0)
774
775 static inline void php_ion_writer_stream_init(php_ion_writer *obj, php_ion_writer_options *opt)
776 {
777 PTR_CHECK(obj->stream.ptr);
778 GC_ADDREF(obj->stream.ptr->res);
779
780 obj->stream.buf.length = opt ? opt->opt.allocation_page_size : 0x1000;
781 obj->stream.buf.value = emalloc(obj->stream.buf.length);
782 }
783 static inline void php_ion_writer_buffer_init(php_ion_writer *obj)
784 {
785 zval *ref = &obj->buffer.val;
786 ZVAL_DEREF(ref);
787
788 smart_str_alloc(&obj->buffer.str, 0, 0);
789 smart_str_0(&obj->buffer.str);
790 REF_STR();
791 }
792
793 static inline void php_ion_writer_buffer_grow(php_ion_writer *obj)
794 {
795 zval *ref = &obj->buffer.val;
796 ZVAL_DEREF(ref);
797
798 switch (GC_REFCOUNT(obj->buffer.str.s)) {
799 case 2:
800 // nothing to do
801 break;
802 case 1:
803 // we've been separated
804 GC_ADDREF(obj->buffer.str.s);
805 break;
806 default:
807 // we have to separate
808 fprintf(stderr, "SEPARATE\n");
809 obj->buffer.str.s = zend_string_dup(obj->buffer.str.s, 0);
810 break;
811 }
812
813 zend_string *old = obj->buffer.str.s;
814 GC_DELREF(old);
815 smart_str_erealloc(&obj->buffer.str, obj->buffer.str.a << 1);
816 if (old == obj->buffer.str.s) {
817 GC_ADDREF(old);
818 } else if(old == Z_STR_P(ref)) {
819 ZVAL_NULL(ref);
820 }
821
822 NEW_REF_STR();
823 }
824
825 static inline iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user)
826 {
827 php_ion_writer *writer = (php_ion_writer *) user->handler_state;
828
829 if (user->curr) {
830 writer->buffer.str.s->len += user->curr - (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len];
831 smart_str_0(&writer->buffer.str);
832 if (user->limit == user->curr) {
833 php_ion_writer_buffer_grow(writer);
834 }
835 }
836 user->curr = (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len];
837 user->limit = user->curr + writer->buffer.str.a - writer->buffer.str.s->len;
838
839 return IERR_OK;
840 }
841
842 static inline void php_ion_writer_ctor(php_ion_writer *obj)
843 {
844 if (obj->opt) {
845 update_property_obj(&obj->std, ZEND_STRL("options"), obj->opt);
846 }
847
848 php_ion_writer_options *opt = php_ion_obj(writer_options, obj->opt);
849 ION_STREAM_HANDLER h;
850 if (obj->type == STREAM_WRITER) {
851 h = php_ion_writer_stream_handler;
852 php_ion_writer_stream_init(obj, opt);
853 } else {
854 h = php_ion_writer_buffer_handler;
855 php_ion_writer_buffer_init(obj);
856 }
857
858 ION_CHECK(ion_writer_open_stream(&obj->writer, h, obj, opt ? &opt->opt : NULL));
859 OBJ_CHECK(obj);
860 }
861
862 static inline void php_ion_writer_dtor(php_ion_writer *obj)
863 {
864 if (obj->writer) {
865 ion_writer_close(obj->writer);
866 }
867 if (obj->type == STREAM_WRITER) {
868 if (obj->stream.buf.value) {
869 efree(obj->stream.buf.value);
870 }
871 if (obj->stream.ptr) {
872 zend_list_delete(obj->stream.ptr->res);
873 }
874 } else {
875 if (obj->buffer.str.s) {
876 smart_str_0(&obj->buffer.str);
877 zend_string_release(obj->buffer.str.s);
878 }
879 zval_ptr_dtor(&obj->buffer.val);
880 }
881 }
882
883 php_ion_decl(writer, Writer_Writer, php_ion_writer_dtor(obj));
884
885 typedef struct php_ion_serializer_php {
886 php_ion_serializer serializer;
887 zend_object *opt, std;
888 } php_ion_serializer_php;
889
890 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj)
891 {
892 php_ion_serializer *global_ser = &php_ion_globals.serializer;
893 ser_obj->serializer.ids = global_ser->ids;
894 ser_obj->serializer.tmp = global_ser->tmp;
895
896 zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
897 ser_obj->serializer.call_magic);
898 if (ser_obj->serializer.call_custom) {
899 zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"),
900 ser_obj->serializer.call_custom);
901 ser_obj->serializer.call_custom = zend_string_tolower(ser_obj->serializer.call_custom);
902 } else {
903 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
904 }
905 if (ser_obj->opt) {
906 php_ion_writer_options *o_woptions = php_ion_obj(writer_options, ser_obj->opt);
907 ser_obj->serializer.options = &o_woptions->opt;
908 update_property_obj(&ser_obj->std, ZEND_STRL("writerOptions"), ser_obj->opt);
909 } else {
910 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("writerOptions"));
911 }
912 }
913
914 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php *obj)
915 {
916 if (obj->serializer.call_custom) {
917 zend_string_release(obj->serializer.call_custom);
918 }
919 }
920
921 static inline void php_ion_serialize_zval(php_ion_serializer *, zval *);
922
923 static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array *arr, bool props)
924 {
925 ION_CHECK(ion_writer_start_container(ser->writer, tid_STRUCT));
926
927 zval *v;
928 zend_ulong h;
929 zend_string *k = NULL;
930 if (arr) ZEND_HASH_FOREACH_KEY_VAL_IND(arr, h, k, v)
931 ION_STRING is;
932 if (k) {
933 size_t prop_len;
934 const char *class_name, *prop_name;
935 if (props && (SUCCESS == zend_unmangle_property_name_ex(k, &class_name, &prop_name, &prop_len)) && class_name) {
936 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("p"))));
937 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, class_name, prop_name - class_name - 1)));
938 } else {
939 prop_name = k->val;
940 prop_len = k->len;
941 }
942 ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_from_cstr(&is, prop_name, prop_len)));
943 } else {
944 char buf[MAX_LENGTH_OF_LONG + 1], *end = buf + sizeof(buf) - 1;
945 char *ptr = zend_print_long_to_buf(end, (zend_long) h);
946 ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_from_cstr(&is, ptr, end - ptr)));
947 }
948
949 php_ion_serialize_zval(ser, v);
950 ION_CATCH();
951 ZEND_HASH_FOREACH_END();
952
953 ION_CHECK(ion_writer_finish_container(ser->writer));
954 }
955
956 static inline void php_ion_serialize_list(php_ion_serializer *ser, zend_array *arr)
957 {
958 ION_CHECK(ion_writer_start_container(ser->writer, tid_LIST));
959
960 zval *v;
961 ZEND_HASH_FOREACH_VAL_IND(arr, v)
962 php_ion_serialize_zval(ser, v);
963 ION_CATCH();
964 ZEND_HASH_FOREACH_END();
965
966 ION_CHECK(ion_writer_finish_container(ser->writer));
967 }
968
969 static inline void php_ion_serialize_array(php_ion_serializer *ser, zend_array *arr)
970 {
971 if (zend_array_is_list(arr)) {
972 php_ion_serialize_list(ser, arr);
973 } else {
974 php_ion_serialize_struct(ser, arr, false);
975 }
976 }
977
978 static inline void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_object *zobject)
979 {
980 uint8_t *buf;
981 size_t len;
982 zval tmp;
983
984 ZVAL_OBJ(&tmp, zobject);
985 if (SUCCESS == zobject->ce->serialize(&tmp, &buf, &len, NULL)) {
986 ION_STRING is;
987 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("S"))));
988 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
989 ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_cstr(&is, (char *) buf, len)));
990 efree(buf);
991 } else if (!EG(exception)){
992 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
993 "Failed to serialize class %s", zobject->ce->name->val);
994 }
995 }
996
997 static inline void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_object *zobject, zend_function *fn)
998 {
999 zval rv;
1000
1001 ZVAL_NULL(&rv);
1002 zend_call_known_instance_method_with_0_params(fn ? fn : zobject->ce->__serialize, zobject, &rv);
1003 ION_CATCH();
1004
1005 if (IS_ARRAY == Z_TYPE(rv)) {
1006 ION_STRING is;
1007 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, fn ? "C" : "O", 1)));
1008 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1009 php_ion_serialize_zval(ser, &rv);
1010 zval_ptr_dtor(&rv);
1011 } else {
1012 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1013 "%s serializer %s::%s did not return an array",
1014 fn ? "Custom" : "Magic", zobject->ce->name->val,
1015 fn ? fn->common.function_name->val : "__serialize");
1016 }
1017 }
1018
1019 static inline void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_object *zobject)
1020 {
1021 ION_STRING is;
1022 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("E"))));
1023
1024 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1025 zval *z_cname = zend_enum_fetch_case_name(zobject);
1026 ION_CHECK(ion_writer_write_symbol(ser->writer, ion_string_from_zend(&is, Z_STR_P(z_cname))));
1027 }
1028
1029 static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_object *zobject)
1030 {
1031 ION_STRING is;
1032
1033 if (zobject->ce != zend_standard_class_def) {
1034 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("c"))));
1035 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1036 } else {
1037 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("o"))));
1038 }
1039
1040 zval zobj;
1041 ZVAL_OBJ(&zobj, zobject);
1042 HashTable *props = zend_get_properties_for(&zobj, ZEND_PROP_PURPOSE_SERIALIZE);
1043 if (props) {
1044 php_ion_serialize_struct(ser, props, true);
1045 zend_release_properties(props);
1046 } else {
1047 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1048 "Could not get properties for serialization of class %s",
1049 zobject->ce->name->val);
1050 }
1051 }
1052
1053 static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce)
1054 {
1055 return ce->__serialize && ser->call_magic;
1056 }
1057
1058 static inline bool can_call_iface_serialize(php_ion_serializer *, zend_class_entry *ce)
1059 {
1060 return !!ce->serialize;
1061 }
1062
1063 static inline bool can_call_custom_serialize(php_ion_serializer *ser, zend_object *zobject, zend_function **fn)
1064 {
1065 if (ser->call_custom) {
1066 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
1067 }
1068 return false;
1069 }
1070
1071 static inline void php_ion_serialize_object(php_ion_serializer *ser, zend_object *zobject)
1072 {
1073 zend_function *fn;
1074 zend_class_entry *ce = zobject->ce;
1075 ZEND_ASSERT(ce);
1076
1077 if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1078 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1079 "Serializing %s is not allowed", ce->name->val);
1080 return;
1081 }
1082
1083 if (can_call_magic_serialize(ser, ce)) {
1084 php_ion_serialize_object_magic(ser, zobject, NULL);
1085 } else if (can_call_iface_serialize(ser, ce)) {
1086 php_ion_serialize_object_iface(ser, zobject);
1087 } else if (can_call_custom_serialize(ser, zobject, &fn)) {
1088 php_ion_serialize_object_magic(ser, zobject, fn);
1089 } else if (zobject->ce->ce_flags & ZEND_ACC_ENUM) {
1090 php_ion_serialize_object_enum(ser, zobject);
1091 } else if (ce == ce_Symbol) {
1092 ION_CHECK(ion_writer_write_ion_symbol(ser->writer, &php_ion_obj(symbol, zobject)->sym));
1093 } else if (ce == ce_Decimal) {
1094 ION_CHECK(ion_writer_write_ion_decimal(ser->writer, &php_ion_obj(decimal, zobject)->dec));
1095 } else if (ce == ce_Timestamp) {
1096 ION_TIMESTAMP its;
1097 php_ion_timestamp *pts = php_ion_obj(timestamp, zobject);
1098 decContext *ctx = ser->options ? ser->options->decimal_context : NULL;
1099 ION_CHECK(ion_writer_write_timestamp(ser->writer, ion_timestamp_from_php(&its, pts, ctx)));
1100 } else {
1101 php_ion_serialize_object_std(ser, zobject);
1102 }
1103 }
1104
1105 static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv)
1106 {
1107 zend_ulong idx = (zend_ulong) (uintptr_t) Z_COUNTED_P(zv);
1108
1109 ION_STRING is;
1110 if (zend_hash_index_exists(ser->ids, idx)) {
1111 zval *num = zend_hash_index_find(ser->ids, idx);
1112
1113 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("r"))));
1114 ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(num)));
1115 } else {
1116 zval num;
1117
1118 ZVAL_LONG(&num, zend_hash_num_elements(ser->ids));
1119 zend_hash_index_add(ser->ids, idx, &num);
1120
1121 Z_TRY_ADDREF_P(zv);
1122 zend_hash_next_index_insert(ser->tmp, zv);
1123
1124 switch (Z_TYPE_P(zv)) {
1125 case IS_STRING:
1126 ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_zend(&is, Z_STR_P(zv))));
1127 break;
1128
1129 case IS_ARRAY:
1130 php_ion_serialize_array(ser, Z_ARRVAL_P(zv));
1131 break;
1132
1133 case IS_OBJECT:
1134 php_ion_serialize_object(ser, Z_OBJ_P(zv));
1135 break;
1136
1137 case IS_REFERENCE:
1138 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("R"))));
1139 php_ion_serialize_zval(ser, Z_REFVAL_P(zv));
1140 break;
1141 }
1142 }
1143 }
1144
1145 static inline void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv)
1146 {
1147 OBJ_CHECK(ser);
1148
1149 switch (Z_TYPE_P(zv)) {
1150 case IS_NULL:
1151 ION_CHECK(ion_writer_write_null(ser->writer));
1152 break;
1153 case IS_TRUE:
1154 ION_CHECK(ion_writer_write_bool(ser->writer, TRUE));
1155 break;
1156 case IS_FALSE:
1157 ION_CHECK(ion_writer_write_bool(ser->writer, FALSE));
1158 break;
1159 case IS_LONG:
1160 ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(zv)));
1161 break;
1162 case IS_DOUBLE:
1163 ION_CHECK(ion_writer_write_double(ser->writer, Z_DVAL_P(zv)));
1164 break;
1165 case IS_STRING:
1166 case IS_ARRAY:
1167 case IS_OBJECT:
1168 case IS_REFERENCE:
1169 php_ion_serialize_refcounted(ser, zv);
1170 break;
1171 default:
1172 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1173 "Failed to serialize value of type %s", zend_zval_type_name(zv));
1174 }
1175 }
1176
1177 php_ion_decl(serializer_php, Serializer_PHP, php_ion_serializer_php_dtor(obj));
1178
1179 void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value)
1180 {
1181 zend_object *zo_opt = NULL, *zo_ser = NULL;
1182
1183 if (!ser) {
1184 zo_ser = create_ion_Serializer_PHP(NULL);
1185 php_ion_serializer_php *o_ser = php_ion_obj(serializer_php, zo_ser);
1186 PTR_CHECK(o_ser);
1187 o_ser->serializer.call_magic = true;
1188 php_ion_serializer_php_ctor(o_ser);
1189 ION_CATCH();
1190 ser = &o_ser->serializer;
1191 }
1192
1193 zend_object *zo_writer = create_ion_Writer_Writer(ce_Writer_Buffer_Writer);
1194 php_ion_writer *writer = php_ion_obj(writer, zo_writer);
1195 writer->type = BUFFER_WRITER;
1196
1197 if (ser->options) {
1198 zo_opt = writer->opt = create_ion_Writer_Options(NULL);
1199 php_ion_obj(writer_options, writer->opt)->opt = *ser->options;
1200 }
1201
1202 php_ion_writer_ctor(writer);
1203 ser->writer = writer->writer;
1204 ser->buffer = &writer->buffer.str;
1205
1206 /* start off with a global PHP annotation instead of repeating it all over the place */
1207 if (0 == php_ion_globals_serializer_step()) {
1208 ION_STRING is;
1209 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("PHP"))),
1210 if (zo_ser) OBJ_RELEASE(zo_ser));
1211 }
1212 php_ion_serialize_zval(ser, zv);
1213 php_ion_globals_serializer_exit();
1214
1215 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1216 ion_writer_flush(ser->writer, NULL);
1217 RETVAL_STR_COPY(ser->buffer->s);
1218
1219 OBJ_RELEASE(zo_writer);
1220 if (zo_opt) {
1221 OBJ_RELEASE(zo_opt);
1222 }
1223 if (zo_ser) {
1224 OBJ_RELEASE(zo_ser);
1225 }
1226 }
1227
1228 typedef struct php_ion_unserializer_php {
1229 php_ion_unserializer unserializer;
1230 zend_object *opt, std;
1231 } php_ion_unserializer_php;
1232
1233 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_obj)
1234 {
1235 php_ion_unserializer *global_ser = &php_ion_globals.unserializer;
1236 ser_obj->unserializer.ids = global_ser->ids;
1237 ser_obj->unserializer.tmp = global_ser->tmp;
1238 ser_obj->unserializer.addref = global_ser->addref;
1239
1240 zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
1241 ser_obj->unserializer.call_magic);
1242 if (ser_obj->unserializer.call_custom) {
1243 zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"),
1244 ser_obj->unserializer.call_custom);
1245 ser_obj->unserializer.call_custom = zend_string_tolower(ser_obj->unserializer.call_custom);
1246 } else {
1247 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
1248 }
1249 if (ser_obj->opt) {
1250 php_ion_reader_options *o_roptions = php_ion_obj(reader_options, ser_obj->opt);
1251 ser_obj->unserializer.options = &o_roptions->opt;
1252 update_property_obj(&ser_obj->std, ZEND_STRL("readerOptions"), ser_obj->opt);
1253 } else {
1254 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("readerOptions"));
1255 }
1256 }
1257
1258 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php *obj)
1259 {
1260 if (obj->unserializer.call_custom) {
1261 zend_string_release(obj->unserializer.call_custom);
1262 }
1263 }
1264
1265 static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ);
1266
1267 static inline bool can_call_magic_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
1268 {
1269 return (ce && ce->__unserialize && ser->call_magic);
1270 }
1271
1272 static inline bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
1273 {
1274 return (ce && ce->unserialize);
1275 }
1276
1277 static inline bool can_call_custom_unserialize(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
1278 {
1279 if (ser->call_custom) {
1280 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
1281 }
1282 return false;
1283 }
1284
1285 static inline zval *php_ion_unserialize_class(php_ion_unserializer *ser, zval *return_value)
1286 {
1287 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1288
1289 if (ce) {
1290 object_init_ex(return_value, ce);
1291 return zend_hash_next_index_insert(ser->ids, return_value);
1292 }
1293
1294 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_IMPORT_NOT_FOUND,
1295 "Could not find class %s", ser->annotations.object_class->val);
1296 return NULL;
1297 }
1298
1299 static inline void php_ion_unserialize_object_enum(php_ion_unserializer *ser, zval *return_value)
1300 {
1301 zend_string *zs_case = zval_get_string(return_value);
1302 ION_CATCH();
1303
1304 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1305 if (!ce || !(ce->ce_flags & ZEND_ACC_ENUM)) {
1306 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1307 "Not a valid enum: %s", ser->annotations.object_class->val);
1308 return;
1309 }
1310 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce), zs_case)) {
1311 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1312 "Not a valid enum case: %s::%s", ser->annotations.object_class->val, zs_case->val);
1313 return;
1314 }
1315 RETVAL_OBJ_COPY(zend_enum_get_case(ce, zs_case));
1316 zend_hash_next_index_insert(ser->ids, return_value);
1317 zend_string_release(zs_case);
1318 }
1319
1320 static inline void php_ion_unserialize_object_iface(php_ion_unserializer *ser, zval *return_value)
1321 {
1322 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1323 if (can_call_iface_unserialize(ser, ce)) {
1324 zend_string *s = zval_get_string(return_value);
1325 ZVAL_NULL(return_value);
1326 zval *backref = zend_hash_next_index_insert(ser->ids, return_value);
1327 if (SUCCESS == ce->unserialize(backref, ce, (BYTE *) s->val, s->len, NULL)) {
1328 RETVAL_ZVAL(backref, 0, 0);
1329 } else if (!EG(exception)) {
1330 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1331 "Failed to unserialize class %s", ce->name->val);
1332 }
1333 zend_string_release(s);
1334 } else {
1335 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1336 "Class %s does not implement Serializable", ser->annotations.object_class->val);
1337 }
1338 }
1339
1340 static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_value)
1341 {
1342 zend_hash_next_index_insert(ser->ids, return_value);
1343
1344 ION_CHECK(ion_reader_step_in(ser->reader));
1345
1346 while (true) {
1347 ION_TYPE typ;
1348 ION_CHECK(ion_reader_next(ser->reader, &typ));
1349
1350 if (typ == tid_EOF) {
1351 break;
1352 }
1353
1354 ION_STRING is;
1355 ION_CHECK(ion_reader_get_field_name(ser->reader, &is));
1356 zend_string *key = zend_string_from_ion(&is);
1357
1358 zval zvalue;
1359 php_ion_unserialize_zval(ser, &zvalue, &typ);
1360 ION_CATCH(zend_string_release(key));
1361
1362 zend_class_entry *ce = Z_OBJCE_P(return_value);
1363 if (ser->annotations.object_prop && ser->annotations.property_class->val[0] != '*') {
1364 ce = zend_lookup_class(ser->annotations.property_class);
1365 }
1366 zend_update_property_ex(ce, Z_OBJ_P(return_value), key, &zvalue);
1367 zval_ptr_dtor(&zvalue);
1368 zend_string_release(key);
1369 }
1370
1371 ION_CHECK(ion_reader_step_out(ser->reader));
1372 }
1373
1374 static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *return_value)
1375 {
1376 zend_hash_next_index_insert(ser->ids, return_value);
1377
1378 ION_CHECK(ion_reader_step_in(ser->reader));
1379
1380 while (true) {
1381 ION_TYPE typ;
1382 ION_CHECK(ion_reader_next(ser->reader, &typ));
1383
1384 ION_STRING name;
1385 ION_CHECK(ion_reader_get_field_name(ser->reader, &name));
1386 zend_string *key = zend_string_from_ion(&name);
1387
1388 zval zvalue;
1389 php_ion_unserialize_zval(ser, &zvalue, &typ);
1390 ION_CATCH(zend_string_release(key));
1391
1392 if (typ == tid_EOF) {
1393 zend_string_release(key);
1394 break;
1395 }
1396
1397 zend_symtable_update(HASH_OF(return_value), key, &zvalue);
1398
1399 zend_string_release(key);
1400 }
1401
1402 ION_CHECK(ion_reader_step_out(ser->reader));
1403 }
1404
1405 static inline void verify_unserializer(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
1406 {
1407 switch (ser->annotations.object_type) {
1408 case 'c':
1409 *fn = NULL;
1410 break;
1411
1412 case 'C':
1413 if (!can_call_custom_unserialize(ser, zobject, fn)) {
1414 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1415 "Could not find custom serializer method of %s", ser->annotations.object_class->val);
1416 }
1417 break;
1418
1419 case 'O':
1420 if (!can_call_magic_unserialize(ser, zobject->ce)) {
1421 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1422 "Could not find method %s::__unserialize()", ser->annotations.object_class->val);
1423 }
1424 *fn = zobject->ce->__unserialize;
1425 break;
1426
1427 default:
1428 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1429 "Invalid object type %c", ser->annotations.object_type);
1430 }
1431 }
1432
1433 static inline void php_ion_unserialize_object(php_ion_unserializer *ser, zval *return_value)
1434 {
1435 // backup possible backref to array returned by magic/custom __serialize()
1436 zval *input = zend_hash_next_index_insert(ser->tmp, return_value);
1437
1438 php_ion_unserialize_class(ser, return_value);
1439 ION_CATCH();
1440
1441 zend_function *fn = NULL;
1442 zend_object *zobject = Z_OBJ_P(return_value);
1443 verify_unserializer(ser, zobject, &fn);
1444 ION_CATCH();
1445
1446 // plain object
1447 if (!fn) {
1448 php_ion_unserialize_props(ser, return_value);
1449 return;
1450 }
1451
1452 // magic object
1453 if (Z_TYPE_P(input) != IS_ARRAY) {
1454 zval_ptr_dtor(input);
1455 array_init(input);
1456 zend_hash_real_init_mixed(Z_ARRVAL_P(input));
1457 php_ion_unserialize_hash(ser, input);
1458 ION_CATCH();
1459 }
1460 zval rv;
1461 ZVAL_NULL(&rv);
1462 zend_call_method_with_1_params(zobject, zobject->ce, &fn, "", &rv, input);
1463 zval_ptr_dtor(&rv);
1464 }
1465
1466 static inline void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *return_value)
1467 {
1468 if (ser->annotations.object_class) {
1469 switch (ser->annotations.object_type) {
1470 case 'S':
1471 php_ion_unserialize_object_iface(ser, return_value);
1472 break;
1473 case 'E':
1474 php_ion_unserialize_object_enum(ser, return_value);
1475 break;
1476 default:
1477 php_ion_unserialize_object(ser, return_value);
1478 }
1479 } else if (!ser->annotations.object_type) {
1480 array_init(return_value);
1481 php_ion_unserialize_hash(ser, return_value);
1482 } else if (ser->annotations.object_type == 'o') {
1483 object_init(return_value);
1484 php_ion_unserialize_hash(ser, return_value);
1485 } else {
1486 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1487 "Invalid object annotation %c::", ser->annotations.object_type);
1488 }
1489 }
1490
1491 static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *return_value)
1492 {
1493 ION_CHECK(ion_reader_step_in(ser->reader));
1494 array_init(return_value);
1495 zend_hash_next_index_insert(ser->ids, return_value);
1496
1497 while (true) {
1498 ION_TYPE typ;
1499 ION_CHECK(ion_reader_next(ser->reader, &typ));
1500
1501 zval next;
1502 php_ion_unserialize_zval(ser, &next, &typ);
1503 ION_CATCH();
1504
1505 if (typ == tid_EOF) {
1506 break;
1507 }
1508
1509 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &next);
1510 }
1511
1512 ION_CHECK(ion_reader_step_out(ser->reader));
1513 }
1514
1515 static inline void php_ion_reader_read_lob(ION_READER *reader, zval *return_value)
1516 {
1517 zend_string *zstr = zend_string_alloc(0x1000, 0);
1518 again:
1519 SIZE read = 0;
1520 iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, zstr->len, &read);
1521 if (err == IERR_BUFFER_TOO_SMALL) {
1522 zstr = zend_string_extend(zstr, zstr->len << 2, 0);
1523 goto again;
1524 }
1525 ION_CHECK(err, zend_string_release(zstr));
1526 if (zstr->len > read) {
1527 zstr = zend_string_truncate(zstr, read, 0);
1528 }
1529 RETURN_STR(zstr);
1530 }
1531
1532 static inline void php_ion_reader_read_timestamp(ION_READER *reader, ION_READER_OPTIONS *opt, zval *return_value)
1533 {
1534 ION_TIMESTAMP ts;
1535 ION_CHECK(ion_reader_read_timestamp(reader, &ts));
1536
1537 object_init_ex(return_value, ce_Timestamp);
1538 php_ion_timestamp *ts_obj = php_ion_obj(timestamp, Z_OBJ_P(return_value));
1539
1540 zend_string *fmt = NULL;
1541 decContext *ctx = opt ? opt->decimal_context : NULL;
1542 ts_obj->time = php_time_from_ion(&ts, ctx, &fmt);
1543 php_ion_timestamp_ctor(ts_obj, ts.precision, fmt, NULL, NULL);
1544 zend_string_release(fmt);
1545
1546 OBJ_CHECK(ts_obj);
1547 }
1548
1549 static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_value)
1550 {
1551 ION_INT *num = NULL;
1552 ION_CHECK(ion_int_alloc(reader, &num));
1553 ION_CHECK(ion_reader_read_ion_int(reader, num));
1554
1555 // TODO: SIZEOF_ZEND_LONG == 4
1556 int64_t i64;
1557 iERR err = ion_int_to_int64(num, &i64);
1558 switch (err) {
1559 case IERR_OK:
1560 RETVAL_LONG(i64);
1561 goto done;
1562
1563 case IERR_NUMERIC_OVERFLOW:
1564 SIZE max, len;
1565 ION_CHECK(ion_int_char_length(num, &max));
1566 zend_string *zs = zend_string_alloc(max-1, 0);
1567
1568 err = ion_int_to_char(num, (BYTE *) zs->val, max, &len);
1569 ZEND_ASSERT(len == zs->len);
1570 RETVAL_STR(zs);
1571 /* fall through */
1572
1573 default:
1574 done:
1575 ion_int_free(num);
1576 ION_CHECK(err);
1577 }
1578 }
1579
1580 static inline void php_ion_unserialize_backref(php_ion_unserializer *ser, zval *return_value)
1581 {
1582 zval *backref = zend_hash_index_find(ser->ids, Z_LVAL_P(return_value));
1583
1584 if (backref) {
1585 ZVAL_COPY_VALUE(return_value, backref);
1586 zend_hash_next_index_insert(ser->addref, return_value);
1587 } else {
1588 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INTERNAL_ERROR,
1589 "Could not find back reference %ld", Z_LVAL_P(return_value));
1590 }
1591 }
1592
1593 static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser)
1594 {
1595 memset(&ser->annotations, 0, sizeof(ser->annotations));
1596
1597 int32_t ann_cnt;
1598 ION_CHECK(ion_reader_get_annotation_count(ser->reader, &ann_cnt));
1599 for (int32_t i = 0; i < ann_cnt; ++i) {
1600 ION_STRING ann_str;
1601 ION_CHECK(ion_reader_get_an_annotation(ser->reader, i, &ann_str));
1602
1603 switch (*ann_str.value) {
1604 case 'R':
1605 if (ser->annotations.makeref) {
1606 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1607 "Invalid multiple reference annotations");
1608 return;
1609 }
1610 ser->annotations.makeref = true;
1611 break;
1612
1613 case 'r':
1614 if (ser->annotations.backref) {
1615 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1616 "Invalid multiple back reference annotations");
1617 return;
1618 }
1619 ser->annotations.backref = true;
1620 break;
1621
1622 case 'p':
1623 if (ser->annotations.object_prop) {
1624 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1625 "Invalid multiple object property annotations");
1626 return;
1627 }
1628 ser->annotations.object_prop = true;
1629
1630 ION_STRING prop_class;
1631 ION_CHECK(ion_reader_get_an_annotation(ser->reader, ++i, &prop_class));
1632 ser->annotations.property_class = zend_string_from_ion(&prop_class);
1633
1634 zval zptmp;
1635 ZVAL_STR(&zptmp, ser->annotations.property_class);
1636 zend_hash_next_index_insert(ser->tmp, &zptmp);
1637 break;
1638
1639 case 'E':
1640 case 'S':
1641 case 'O':
1642 case 'C':
1643 case 'o':
1644 case 'c':
1645 if (ser->annotations.object_type) {
1646 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1647 "Invalid multiple object type annotations: %c::%c",
1648 ser->annotations.object_type, *ann_str.value);
1649 return;
1650 }
1651 if ('o' != (ser->annotations.object_type = *ann_str.value)) {
1652 ION_STRING class_name;
1653 ION_CHECK(ion_reader_get_an_annotation(ser->reader, ++i, &class_name));
1654 ser->annotations.object_class = zend_string_from_ion(&class_name);
1655
1656 zval zctmp;
1657 ZVAL_STR(&zctmp, ser->annotations.object_class);
1658 zend_hash_next_index_insert(ser->tmp, &zctmp);
1659 }
1660 break;
1661 }
1662
1663 // sanity checks
1664 if (ser->annotations.object_type && ser->annotations.object_type != 'o' && !ser->annotations.object_class) {
1665 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1666 "Invalid object annotation without class name: %c::", ser->annotations.object_type);
1667 return;
1668 }
1669 if (ser->annotations.object_type == 'o' && ser->annotations.object_class) {
1670 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1671 "Invalid object annotation with class name: o::%s", ser->annotations.object_class->val);
1672 return;
1673 }
1674 }
1675 }
1676
1677 static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ)
1678 {
1679 if (typ) {
1680 memcpy(&ser->type, typ, sizeof(ser->type));
1681 } else {
1682 typ = &ser->type;
1683 ION_CHECK(ion_reader_next(ser->reader, typ));
1684 }
1685
1686 php_ion_unserialize_annotations(ser);
1687 ION_CATCH();
1688
1689 if (ser->annotations.makeref) {
1690 ZVAL_MAKE_REF(return_value);
1691 zend_hash_next_index_insert(ser->ids, return_value);
1692 ZVAL_DEREF(return_value);
1693 }
1694
1695 BOOL bval;
1696 ION_CHECK(ion_reader_is_null(ser->reader, &bval));
1697 if (bval) {
1698 goto read_null;
1699 }
1700
1701 switch (ION_TYPE_INT(*typ)) {
1702 case tid_NULL_INT:
1703 read_null: ;
1704 ION_CHECK(ion_reader_read_null(ser->reader, typ));
1705 RETURN_NULL();
1706
1707 case tid_BOOL_INT:
1708 ION_CHECK(ion_reader_read_bool(ser->reader, &bval));
1709 RETURN_BOOL(bval);
1710
1711 case tid_INT_INT:
1712 php_ion_reader_read_int(ser->reader, return_value);
1713 if (ser->annotations.backref) {
1714 ION_CATCH();
1715 php_ion_unserialize_backref(ser, return_value);
1716 }
1717 if (ser->annotations.object_type) {
1718 if (!ser->annotations.backref) {
1719 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1720 "Invalid object type annotation: %c::" ZEND_LONG_FMT,
1721 ser->annotations.object_type, Z_LVAL_P(return_value));
1722 return;
1723 }
1724 goto unserialize_struct;
1725 }
1726 return;
1727
1728 case tid_FLOAT_INT:
1729 double d;
1730 ION_CHECK(ion_reader_read_double(ser->reader, &d));
1731 RETURN_DOUBLE(d);
1732
1733 case tid_DECIMAL_INT:
1734 object_init_ex(return_value, ce_Decimal);
1735 php_ion_decimal *dec = php_ion_obj(decimal, Z_OBJ_P(return_value));
1736 ION_CHECK(ion_reader_read_ion_decimal(ser->reader, &dec->dec));
1737 php_ion_decimal_ctor(dec);
1738 zend_hash_next_index_insert(ser->ids, return_value);
1739 return;
1740
1741 case tid_TIMESTAMP_INT:
1742 php_ion_reader_read_timestamp(ser->reader, ser->options, return_value);
1743 zend_hash_next_index_insert(ser->ids, return_value);
1744 return;
1745
1746 case tid_SYMBOL_INT:
1747 ION_SYMBOL sym;
1748 ION_CHECK(ion_reader_read_ion_symbol(ser->reader, &sym));
1749 php_ion_symbol_zval(&sym, return_value);
1750 if (ser->annotations.object_type) {
1751 zend_hash_next_index_insert(ser->tmp, return_value);
1752 goto unserialize_struct;
1753 }
1754 zend_hash_next_index_insert(ser->ids, return_value);
1755 return;
1756
1757 case tid_STRING_INT:
1758 ION_STRING str;
1759 ION_CHECK(ion_reader_read_string(ser->reader, &str));
1760 RETVAL_STRINGL((char *) str.value, str.length);
1761 if (ser->annotations.object_type) {
1762 zend_hash_next_index_insert(ser->tmp, return_value);
1763 goto unserialize_struct;
1764 }
1765 zend_hash_next_index_insert(ser->ids, return_value);
1766 return;
1767
1768 case tid_CLOB_INT:
1769 case tid_BLOB_INT:
1770 php_ion_reader_read_lob(ser->reader, return_value);
1771 if (ser->annotations.object_type) {
1772 zend_hash_next_index_insert(ser->tmp, return_value);
1773 goto unserialize_struct;
1774 }
1775 zend_hash_next_index_insert(ser->ids, return_value);
1776 return;
1777
1778 case tid_LIST_INT:
1779 case tid_SEXP_INT: // FIXME
1780 php_ion_unserialize_list(ser, return_value);
1781 if (!ser->annotations.object_type) {
1782 return;
1783 }
1784 /* fall through */
1785
1786 case tid_STRUCT_INT:
1787 unserialize_struct: ;
1788 php_ion_unserialize_struct(ser, return_value);
1789 return;
1790
1791 case tid_none_INT:
1792 ZEND_ASSERT(0);
1793 break;
1794
1795 case tid_DATAGRAM_INT:
1796 ZEND_ASSERT(!"datagram");
1797 case tid_EOF_INT:
1798 return;
1799 }
1800 }
1801
1802 php_ion_decl(unserializer_php, Unserializer_PHP, php_ion_unserializer_php_dtor(obj));
1803
1804 void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_value)
1805 {
1806 zend_object *zo_opt = NULL, *zo_ser = NULL;
1807
1808 if (!ser) {
1809 zo_ser = create_ion_Unserializer_PHP(NULL);
1810 php_ion_unserializer_php *o_ser = php_ion_obj(unserializer_php, zo_ser);
1811 PTR_CHECK(o_ser);
1812 o_ser->unserializer.call_magic = true;
1813 php_ion_unserializer_php_ctor(o_ser);
1814 ION_CATCH();
1815 ser = &o_ser->unserializer;
1816 }
1817
1818 zend_object *zo_reader;
1819 php_ion_reader *reader;
1820 ZVAL_DEREF(zdata);
1821 switch (Z_TYPE_P(zdata)) {
1822 case IS_STRING:
1823 zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader);
1824 reader = php_ion_obj(reader, zo_reader);
1825 reader->type = BUFFER_READER;
1826 reader->buffer = zend_string_copy(Z_STR_P(zdata));
1827 break;
1828
1829 case IS_RESOURCE:
1830 zo_reader = create_ion_Reader_Reader(ce_Reader_Stream_Reader);
1831 reader = php_ion_obj(reader, zo_reader);
1832 reader->type = STREAM_READER;
1833 php_stream_from_zval_no_verify(reader->stream.ptr, zdata);
1834 break;
1835
1836 default:
1837 ZEND_ASSERT(!IS_STRING && !IS_RESOURCE);
1838 }
1839
1840 if (ser->options) {
1841 zo_opt = reader->opt = create_ion_Reader_Options(NULL);
1842 php_ion_obj(reader_options, reader->opt)->opt = *ser->options;
1843 }
1844
1845 php_ion_reader_ctor(reader);
1846 ser->reader = reader->reader;
1847
1848 php_ion_globals_unserializer_step();
1849 php_ion_unserialize_zval(ser, return_value, NULL);
1850 php_ion_globals_unserializer_exit();
1851
1852 OBJ_RELEASE(zo_reader);
1853 if (zo_opt) {
1854 OBJ_RELEASE(zo_opt);
1855 }
1856 if (zo_ser) {
1857 OBJ_RELEASE(zo_ser);
1858 }
1859 }