fix decimal context leak
[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 zend_string *fq_enum_case(zend_object *zobject)
1020 {
1021 zval *cn = zend_enum_fetch_case_name(zobject);
1022 zend_string *en = zend_string_alloc(zobject->ce->name->len + Z_STRLEN_P(cn) + strlen("::"), 0);
1023 memcpy(en->val, zobject->ce->name->val, zobject->ce->name->len);
1024 en->val[zobject->ce->name->len] = ':';
1025 en->val[zobject->ce->name->len + 1] = ':';
1026 memcpy(&en->val[zobject->ce->name->len + 2], Z_STRVAL_P(cn), Z_STRLEN_P(cn));
1027 en->val[en->len] = 0;
1028 return en;
1029 }
1030
1031 static inline void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_object *zobject)
1032 {
1033 ION_STRING is;
1034 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("E"))));
1035
1036 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1037 zval *z_cname = zend_enum_fetch_case_name(zobject);
1038 ION_CHECK(ion_writer_write_symbol(ser->writer, ion_string_from_zend(&is, Z_STR_P(z_cname))));
1039 }
1040
1041 static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_object *zobject)
1042 {
1043 ION_STRING is;
1044
1045 if (zobject->ce != zend_standard_class_def) {
1046 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("c"))));
1047 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1048 } else {
1049 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("o"))));
1050 }
1051
1052 zval zobj;
1053 ZVAL_OBJ(&zobj, zobject);
1054 HashTable *props = zend_get_properties_for(&zobj, ZEND_PROP_PURPOSE_SERIALIZE);
1055 if (props) {
1056 php_ion_serialize_struct(ser, props, true);
1057 zend_release_properties(props);
1058 } else {
1059 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1060 "Could not get properties for serialization of class %s",
1061 zobject->ce->name->val);
1062 }
1063 }
1064
1065 static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce)
1066 {
1067 return ce->__serialize && ser->call_magic;
1068 }
1069
1070 static inline bool can_call_iface_serialize(php_ion_serializer *, zend_class_entry *ce)
1071 {
1072 return !!ce->serialize;
1073 }
1074
1075 static inline bool can_call_custom_serialize(php_ion_serializer *ser, zend_object *zobject, zend_function **fn)
1076 {
1077 if (ser->call_custom) {
1078 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
1079 }
1080 return false;
1081 }
1082
1083 static inline void php_ion_serialize_object(php_ion_serializer *ser, zend_object *zobject)
1084 {
1085 zend_function *fn;
1086 zend_class_entry *ce = zobject->ce;
1087 ZEND_ASSERT(ce);
1088
1089 if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1090 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1091 "Serializing %s is not allowed", ce->name->val);
1092 return;
1093 }
1094
1095 if (can_call_magic_serialize(ser, ce)) {
1096 php_ion_serialize_object_magic(ser, zobject, NULL);
1097 } else if (can_call_iface_serialize(ser, ce)) {
1098 php_ion_serialize_object_iface(ser, zobject);
1099 } else if (can_call_custom_serialize(ser, zobject, &fn)) {
1100 php_ion_serialize_object_magic(ser, zobject, fn);
1101 } else if (zobject->ce->ce_flags & ZEND_ACC_ENUM) {
1102 php_ion_serialize_object_enum(ser, zobject);
1103 } else if (ce == ce_Symbol) {
1104 ION_CHECK(ion_writer_write_ion_symbol(ser->writer, &php_ion_obj(symbol, zobject)->sym));
1105 } else if (ce == ce_Decimal) {
1106 ION_CHECK(ion_writer_write_ion_decimal(ser->writer, &php_ion_obj(decimal, zobject)->dec));
1107 } else if (ce == ce_Timestamp) {
1108 ION_TIMESTAMP its;
1109 php_ion_timestamp *pts = php_ion_obj(timestamp, zobject);
1110 decContext *ctx = ser->options ? ser->options->decimal_context : NULL;
1111 ION_CHECK(ion_writer_write_timestamp(ser->writer, ion_timestamp_from_php(&its, pts, ctx)));
1112 } else {
1113 php_ion_serialize_object_std(ser, zobject);
1114 }
1115 }
1116
1117 static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv)
1118 {
1119 zend_ulong idx = (zend_ulong) (uintptr_t) Z_COUNTED_P(zv);
1120
1121 ION_STRING is;
1122 if (zend_hash_index_exists(ser->ids, idx)) {
1123 zval *num = zend_hash_index_find(ser->ids, idx);
1124
1125 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("r"))));
1126 ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(num)));
1127 } else {
1128 zval num;
1129
1130 ZVAL_LONG(&num, zend_hash_num_elements(ser->ids));
1131 zend_hash_index_add(ser->ids, idx, &num);
1132
1133 Z_TRY_ADDREF_P(zv);
1134 zend_hash_next_index_insert(ser->tmp, zv);
1135
1136 switch (Z_TYPE_P(zv)) {
1137 case IS_STRING:
1138 ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_zend(&is, Z_STR_P(zv))));
1139 break;
1140
1141 case IS_ARRAY:
1142 php_ion_serialize_array(ser, Z_ARRVAL_P(zv));
1143 break;
1144
1145 case IS_OBJECT:
1146 php_ion_serialize_object(ser, Z_OBJ_P(zv));
1147 break;
1148
1149 case IS_REFERENCE:
1150 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("R"))));
1151 php_ion_serialize_zval(ser, Z_REFVAL_P(zv));
1152 break;
1153 }
1154 }
1155 }
1156
1157 static inline void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv)
1158 {
1159 OBJ_CHECK(ser);
1160
1161 switch (Z_TYPE_P(zv)) {
1162 case IS_NULL:
1163 ION_CHECK(ion_writer_write_null(ser->writer));
1164 break;
1165 case IS_TRUE:
1166 ION_CHECK(ion_writer_write_bool(ser->writer, TRUE));
1167 break;
1168 case IS_FALSE:
1169 ION_CHECK(ion_writer_write_bool(ser->writer, FALSE));
1170 break;
1171 case IS_LONG:
1172 ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(zv)));
1173 break;
1174 case IS_DOUBLE:
1175 ION_CHECK(ion_writer_write_double(ser->writer, Z_DVAL_P(zv)));
1176 break;
1177 case IS_STRING:
1178 case IS_ARRAY:
1179 case IS_OBJECT:
1180 case IS_REFERENCE:
1181 php_ion_serialize_refcounted(ser, zv);
1182 break;
1183 default:
1184 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1185 "Failed to serialize value of type %s", zend_zval_type_name(zv));
1186 }
1187 }
1188
1189 php_ion_decl(serializer_php, Serializer_PHP, php_ion_serializer_php_dtor(obj));
1190
1191 void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value)
1192 {
1193 zend_object *zo_opt = NULL, *zo_ser = NULL;
1194
1195 if (!ser) {
1196 zo_ser = create_ion_Serializer_PHP(NULL);
1197 php_ion_serializer_php *o_ser = php_ion_obj(serializer_php, zo_ser);
1198 PTR_CHECK(o_ser);
1199 o_ser->serializer.call_magic = true;
1200 php_ion_serializer_php_ctor(o_ser);
1201 ION_CATCH();
1202 ser = &o_ser->serializer;
1203 }
1204
1205 zend_object *zo_writer = create_ion_Writer_Writer(ce_Writer_Buffer_Writer);
1206 php_ion_writer *writer = php_ion_obj(writer, zo_writer);
1207 writer->type = BUFFER_WRITER;
1208
1209 if (ser->options) {
1210 zo_opt = writer->opt = create_ion_Writer_Options(NULL);
1211 php_ion_obj(writer_options, writer->opt)->opt = *ser->options;
1212 }
1213
1214 php_ion_writer_ctor(writer);
1215 ser->writer = writer->writer;
1216 ser->buffer = &writer->buffer.str;
1217
1218 /* start off with a global PHP annotation instead of repeating it all over the place */
1219 if (0 == php_ion_globals_serializer_step()) {
1220 ION_STRING is;
1221 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("PHP"))),
1222 if (zo_ser) OBJ_RELEASE(zo_ser));
1223 }
1224 php_ion_serialize_zval(ser, zv);
1225 php_ion_globals_serializer_exit();
1226
1227 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1228 ion_writer_flush(ser->writer, NULL);
1229 RETVAL_STR_COPY(ser->buffer->s);
1230
1231 OBJ_RELEASE(zo_writer);
1232 if (zo_opt) {
1233 OBJ_RELEASE(zo_opt);
1234 }
1235 if (zo_ser) {
1236 OBJ_RELEASE(zo_ser);
1237 }
1238 }
1239
1240 typedef struct php_ion_unserializer_php {
1241 php_ion_unserializer unserializer;
1242 zend_object *opt, std;
1243 } php_ion_unserializer_php;
1244
1245 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_obj)
1246 {
1247 php_ion_unserializer *global_ser = &php_ion_globals.unserializer;
1248 ser_obj->unserializer.ids = global_ser->ids;
1249 ser_obj->unserializer.tmp = global_ser->tmp;
1250 ser_obj->unserializer.addref = global_ser->addref;
1251
1252 zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
1253 ser_obj->unserializer.call_magic);
1254 if (ser_obj->unserializer.call_custom) {
1255 zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"),
1256 ser_obj->unserializer.call_custom);
1257 ser_obj->unserializer.call_custom = zend_string_tolower(ser_obj->unserializer.call_custom);
1258 } else {
1259 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
1260 }
1261 if (ser_obj->opt) {
1262 php_ion_reader_options *o_roptions = php_ion_obj(reader_options, ser_obj->opt);
1263 ser_obj->unserializer.options = &o_roptions->opt;
1264 update_property_obj(&ser_obj->std, ZEND_STRL("readerOptions"), ser_obj->opt);
1265 } else {
1266 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("readerOptions"));
1267 }
1268 }
1269
1270 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php *obj)
1271 {
1272 if (obj->unserializer.call_custom) {
1273 zend_string_release(obj->unserializer.call_custom);
1274 }
1275 }
1276
1277 static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ);
1278
1279 static inline bool can_call_magic_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
1280 {
1281 return (ce && ce->__unserialize && ser->call_magic);
1282 }
1283
1284 static inline bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
1285 {
1286 return (ce && ce->unserialize);
1287 }
1288
1289 static inline bool can_call_custom_unserialize(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
1290 {
1291 if (ser->call_custom) {
1292 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
1293 }
1294 return false;
1295 }
1296
1297 static inline zval *php_ion_unserialize_class(php_ion_unserializer *ser, zval *return_value)
1298 {
1299 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1300
1301 if (ce) {
1302 object_init_ex(return_value, ce);
1303 return zend_hash_next_index_insert(ser->ids, return_value);
1304 }
1305
1306 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_IMPORT_NOT_FOUND,
1307 "Could not find class %s", ser->annotations.object_class->val);
1308 return NULL;
1309 }
1310
1311 static inline void php_ion_unserialize_object_enum(php_ion_unserializer *ser, zval *return_value)
1312 {
1313 zend_string *zs_case = zval_get_string(return_value);
1314 ION_CATCH();
1315
1316 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1317 if (!ce || !(ce->ce_flags & ZEND_ACC_ENUM)) {
1318 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1319 "Not a valid enum: %s", ser->annotations.object_class->val);
1320 return;
1321 }
1322 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce), zs_case)) {
1323 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1324 "Not a valid enum case: %s::%s", ser->annotations.object_class->val, zs_case->val);
1325 return;
1326 }
1327 RETVAL_OBJ_COPY(zend_enum_get_case(ce, zs_case));
1328 zend_hash_next_index_insert(ser->ids, return_value);
1329 zend_string_release(zs_case);
1330 }
1331
1332 static inline void php_ion_unserialize_object_iface(php_ion_unserializer *ser, zval *return_value)
1333 {
1334 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1335 if (can_call_iface_unserialize(ser, ce)) {
1336 zend_string *s = zval_get_string(return_value);
1337 ZVAL_NULL(return_value);
1338 zval *backref = zend_hash_next_index_insert(ser->ids, return_value);
1339 if (SUCCESS == ce->unserialize(backref, ce, (BYTE *) s->val, s->len, NULL)) {
1340 RETVAL_ZVAL(backref, 0, 0);
1341 } else if (!EG(exception)) {
1342 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1343 "Failed to unserialize class %s", ce->name->val);
1344 }
1345 zend_string_release(s);
1346 } else {
1347 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1348 "Class %s does not implement Serializable", ser->annotations.object_class->val);
1349 }
1350 }
1351
1352 static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_value)
1353 {
1354 zend_hash_next_index_insert(ser->ids, return_value);
1355
1356 ION_CHECK(ion_reader_step_in(ser->reader));
1357
1358 while (true) {
1359 ION_TYPE typ;
1360 ION_CHECK(ion_reader_next(ser->reader, &typ));
1361
1362 if (typ == tid_EOF) {
1363 break;
1364 }
1365
1366 ION_STRING is;
1367 ION_CHECK(ion_reader_get_field_name(ser->reader, &is));
1368 zend_string *key = zend_string_from_ion(&is);
1369
1370 zval zvalue;
1371 php_ion_unserialize_zval(ser, &zvalue, &typ);
1372 ION_CATCH(zend_string_release(key));
1373
1374 zend_class_entry *ce = Z_OBJCE_P(return_value);
1375 if (ser->annotations.object_prop && ser->annotations.property_class->val[0] != '*') {
1376 ce = zend_lookup_class(ser->annotations.property_class);
1377 }
1378 zend_update_property_ex(ce, Z_OBJ_P(return_value), key, &zvalue);
1379 zval_ptr_dtor(&zvalue);
1380 zend_string_release(key);
1381 }
1382
1383 ION_CHECK(ion_reader_step_out(ser->reader));
1384 }
1385
1386 static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *return_value)
1387 {
1388 zend_hash_next_index_insert(ser->ids, return_value);
1389
1390 ION_CHECK(ion_reader_step_in(ser->reader));
1391
1392 while (true) {
1393 ION_TYPE typ;
1394 ION_CHECK(ion_reader_next(ser->reader, &typ));
1395
1396 ION_STRING name;
1397 ION_CHECK(ion_reader_get_field_name(ser->reader, &name));
1398 zend_string *key = zend_string_from_ion(&name);
1399
1400 zval zvalue;
1401 php_ion_unserialize_zval(ser, &zvalue, &typ);
1402 ION_CATCH(zend_string_release(key));
1403
1404 if (typ == tid_EOF) {
1405 zend_string_release(key);
1406 break;
1407 }
1408
1409 zend_symtable_update(HASH_OF(return_value), key, &zvalue);
1410
1411 zend_string_release(key);
1412 }
1413
1414 ION_CHECK(ion_reader_step_out(ser->reader));
1415 }
1416
1417 static inline void verify_unserializer(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
1418 {
1419 switch (ser->annotations.object_type) {
1420 case 'c':
1421 *fn = NULL;
1422 break;
1423
1424 case 'C':
1425 if (!can_call_custom_unserialize(ser, zobject, fn)) {
1426 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1427 "Could not find custom serializer method of %s", ser->annotations.object_class->val);
1428 }
1429 break;
1430
1431 case 'O':
1432 if (!can_call_magic_unserialize(ser, zobject->ce)) {
1433 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1434 "Could not find method %s::__unserialize()", ser->annotations.object_class->val);
1435 }
1436 *fn = zobject->ce->__unserialize;
1437 break;
1438
1439 default:
1440 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1441 "Invalid object type %c", ser->annotations.object_type);
1442 }
1443 }
1444
1445 static inline void php_ion_unserialize_object(php_ion_unserializer *ser, zval *return_value)
1446 {
1447 // backup possible backref to array returned by magic/custom __serialize()
1448 zval *input = zend_hash_next_index_insert(ser->tmp, return_value);
1449
1450 php_ion_unserialize_class(ser, return_value);
1451 ION_CATCH();
1452
1453 zend_function *fn = NULL;
1454 zend_object *zobject = Z_OBJ_P(return_value);
1455 verify_unserializer(ser, zobject, &fn);
1456 ION_CATCH();
1457
1458 // plain object
1459 if (!fn) {
1460 php_ion_unserialize_props(ser, return_value);
1461 return;
1462 }
1463
1464 // magic object
1465 if (Z_TYPE_P(input) != IS_ARRAY) {
1466 zval_ptr_dtor(input);
1467 array_init(input);
1468 zend_hash_real_init_mixed(Z_ARRVAL_P(input));
1469 php_ion_unserialize_hash(ser, input);
1470 ION_CATCH();
1471 }
1472 zval rv;
1473 ZVAL_NULL(&rv);
1474 zend_call_method_with_1_params(zobject, zobject->ce, &fn, "", &rv, input);
1475 zval_ptr_dtor(&rv);
1476 }
1477
1478 static inline void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *return_value)
1479 {
1480 if (ser->annotations.object_class) {
1481 switch (ser->annotations.object_type) {
1482 case 'S':
1483 php_ion_unserialize_object_iface(ser, return_value);
1484 break;
1485 case 'E':
1486 php_ion_unserialize_object_enum(ser, return_value);
1487 break;
1488 default:
1489 php_ion_unserialize_object(ser, return_value);
1490 }
1491 } else if (!ser->annotations.object_type) {
1492 array_init(return_value);
1493 php_ion_unserialize_hash(ser, return_value);
1494 } else if (ser->annotations.object_type == 'o') {
1495 object_init(return_value);
1496 php_ion_unserialize_hash(ser, return_value);
1497 } else {
1498 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1499 "Invalid object annotation %c::", ser->annotations.object_type);
1500 }
1501 }
1502
1503 static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *return_value)
1504 {
1505 ION_CHECK(ion_reader_step_in(ser->reader));
1506 array_init(return_value);
1507 zend_hash_next_index_insert(ser->ids, return_value);
1508
1509 while (true) {
1510 ION_TYPE typ;
1511 ION_CHECK(ion_reader_next(ser->reader, &typ));
1512
1513 zval next;
1514 php_ion_unserialize_zval(ser, &next, &typ);
1515 ION_CATCH();
1516
1517 if (typ == tid_EOF) {
1518 break;
1519 }
1520
1521 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &next);
1522 }
1523
1524 ION_CHECK(ion_reader_step_out(ser->reader));
1525 }
1526
1527 static inline void php_ion_reader_read_lob(ION_READER *reader, zval *return_value)
1528 {
1529 zend_string *zstr = zend_string_alloc(0x1000, 0);
1530 again:
1531 SIZE read = 0;
1532 iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, zstr->len, &read);
1533 if (err == IERR_BUFFER_TOO_SMALL) {
1534 zstr = zend_string_extend(zstr, zstr->len << 2, 0);
1535 goto again;
1536 }
1537 ION_CHECK(err, zend_string_release(zstr));
1538 if (zstr->len > read) {
1539 zstr = zend_string_truncate(zstr, read, 0);
1540 }
1541 RETURN_STR(zstr);
1542 }
1543
1544 static inline void php_ion_reader_read_timestamp(ION_READER *reader, ION_READER_OPTIONS *opt, zval *return_value)
1545 {
1546 ION_TIMESTAMP ts;
1547 ION_CHECK(ion_reader_read_timestamp(reader, &ts));
1548
1549 object_init_ex(return_value, ce_Timestamp);
1550 php_ion_timestamp *ts_obj = php_ion_obj(timestamp, Z_OBJ_P(return_value));
1551
1552 zend_string *fmt = NULL;
1553 decContext *ctx = opt ? opt->decimal_context : NULL;
1554 ts_obj->time = php_time_from_ion(&ts, ctx, &fmt);
1555 php_ion_timestamp_ctor(ts_obj, ts.precision, fmt, NULL, NULL);
1556 zend_string_release(fmt);
1557
1558 OBJ_CHECK(ts_obj);
1559 }
1560
1561 static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_value)
1562 {
1563 ION_INT *num = NULL;
1564 ION_CHECK(ion_int_alloc(reader, &num));
1565 ION_CHECK(ion_reader_read_ion_int(reader, num));
1566
1567 // TODO: SIZEOF_ZEND_LONG == 4
1568 int64_t i64;
1569 iERR err = ion_int_to_int64(num, &i64);
1570 switch (err) {
1571 case IERR_OK:
1572 RETVAL_LONG(i64);
1573 goto done;
1574
1575 case IERR_NUMERIC_OVERFLOW:
1576 SIZE max, len;
1577 ION_CHECK(ion_int_char_length(num, &max));
1578 zend_string *zs = zend_string_alloc(max-1, 0);
1579
1580 err = ion_int_to_char(num, (BYTE *) zs->val, max, &len);
1581 ZEND_ASSERT(len == zs->len);
1582 RETVAL_STR(zs);
1583 /* fall through */
1584
1585 default:
1586 done:
1587 ion_int_free(num);
1588 ION_CHECK(err);
1589 }
1590 }
1591
1592 static inline void php_ion_unserialize_backref(php_ion_unserializer *ser, zval *return_value)
1593 {
1594 zval *backref = zend_hash_index_find(ser->ids, Z_LVAL_P(return_value));
1595
1596 if (backref) {
1597 ZVAL_COPY_VALUE(return_value, backref);
1598 zend_hash_next_index_insert(ser->addref, return_value);
1599 } else {
1600 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INTERNAL_ERROR,
1601 "Could not find back reference %ld", Z_LVAL_P(return_value));
1602 }
1603 }
1604
1605 static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser)
1606 {
1607 memset(&ser->annotations, 0, sizeof(ser->annotations));
1608
1609 int32_t ann_cnt;
1610 ION_CHECK(ion_reader_get_annotation_count(ser->reader, &ann_cnt));
1611 for (int32_t i = 0; i < ann_cnt; ++i) {
1612 ION_STRING ann_str;
1613 ION_CHECK(ion_reader_get_an_annotation(ser->reader, i, &ann_str));
1614
1615 switch (*ann_str.value) {
1616 case 'R':
1617 if (ser->annotations.makeref) {
1618 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1619 "Invalid multiple reference annotations");
1620 return;
1621 }
1622 ser->annotations.makeref = true;
1623 break;
1624
1625 case 'r':
1626 if (ser->annotations.backref) {
1627 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1628 "Invalid multiple back reference annotations");
1629 return;
1630 }
1631 ser->annotations.backref = true;
1632 break;
1633
1634 case 'p':
1635 if (ser->annotations.object_prop) {
1636 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1637 "Invalid multiple object property annotations");
1638 return;
1639 }
1640 ser->annotations.object_prop = true;
1641
1642 ION_STRING prop_class;
1643 ION_CHECK(ion_reader_get_an_annotation(ser->reader, ++i, &prop_class));
1644 ser->annotations.property_class = zend_string_from_ion(&prop_class);
1645
1646 zval zptmp;
1647 ZVAL_STR(&zptmp, ser->annotations.property_class);
1648 zend_hash_next_index_insert(ser->tmp, &zptmp);
1649 break;
1650
1651 case 'E':
1652 case 'S':
1653 case 'O':
1654 case 'C':
1655 case 'o':
1656 case 'c':
1657 if (ser->annotations.object_type) {
1658 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1659 "Invalid multiple object type annotations: %c::%c",
1660 ser->annotations.object_type, *ann_str.value);
1661 return;
1662 }
1663 if ('o' != (ser->annotations.object_type = *ann_str.value)) {
1664 ION_STRING class_name;
1665 ION_CHECK(ion_reader_get_an_annotation(ser->reader, ++i, &class_name));
1666 ser->annotations.object_class = zend_string_from_ion(&class_name);
1667
1668 zval zctmp;
1669 ZVAL_STR(&zctmp, ser->annotations.object_class);
1670 zend_hash_next_index_insert(ser->tmp, &zctmp);
1671 }
1672 break;
1673 }
1674
1675 // sanity checks
1676 if (ser->annotations.object_type && ser->annotations.object_type != 'o' && !ser->annotations.object_class) {
1677 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1678 "Invalid object annotation without class name: %c::", ser->annotations.object_type);
1679 return;
1680 }
1681 if (ser->annotations.object_type == 'o' && ser->annotations.object_class) {
1682 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1683 "Invalid object annotation with class name: o::%s", ser->annotations.object_class->val);
1684 return;
1685 }
1686 }
1687 }
1688
1689 static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ)
1690 {
1691 if (typ) {
1692 memcpy(&ser->type, typ, sizeof(ser->type));
1693 } else {
1694 typ = &ser->type;
1695 ION_CHECK(ion_reader_next(ser->reader, typ));
1696 }
1697
1698 php_ion_unserialize_annotations(ser);
1699 ION_CATCH();
1700
1701 if (ser->annotations.makeref) {
1702 ZVAL_MAKE_REF(return_value);
1703 zend_hash_next_index_insert(ser->ids, return_value);
1704 ZVAL_DEREF(return_value);
1705 }
1706
1707 BOOL bval;
1708 ION_CHECK(ion_reader_is_null(ser->reader, &bval));
1709 if (bval) {
1710 goto read_null;
1711 }
1712
1713 switch (ION_TYPE_INT(*typ)) {
1714 case tid_NULL_INT:
1715 read_null: ;
1716 ION_CHECK(ion_reader_read_null(ser->reader, typ));
1717 RETURN_NULL();
1718
1719 case tid_BOOL_INT:
1720 ION_CHECK(ion_reader_read_bool(ser->reader, &bval));
1721 RETURN_BOOL(bval);
1722
1723 case tid_INT_INT:
1724 php_ion_reader_read_int(ser->reader, return_value);
1725 if (ser->annotations.backref) {
1726 ION_CATCH();
1727 php_ion_unserialize_backref(ser, return_value);
1728 }
1729 if (ser->annotations.object_type) {
1730 if (!ser->annotations.backref) {
1731 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
1732 "Invalid object type annotation: %c::" ZEND_LONG_FMT,
1733 ser->annotations.object_type, Z_LVAL_P(return_value));
1734 return;
1735 }
1736 goto unserialize_struct;
1737 }
1738 return;
1739
1740 case tid_FLOAT_INT:
1741 double d;
1742 ION_CHECK(ion_reader_read_double(ser->reader, &d));
1743 RETURN_DOUBLE(d);
1744
1745 case tid_DECIMAL_INT:
1746 object_init_ex(return_value, ce_Decimal);
1747 php_ion_decimal *dec = php_ion_obj(decimal, Z_OBJ_P(return_value));
1748 ION_CHECK(ion_reader_read_ion_decimal(ser->reader, &dec->dec));
1749 php_ion_decimal_ctor(dec);
1750 zend_hash_next_index_insert(ser->ids, return_value);
1751 return;
1752
1753 case tid_TIMESTAMP_INT:
1754 php_ion_reader_read_timestamp(ser->reader, ser->options, return_value);
1755 zend_hash_next_index_insert(ser->ids, return_value);
1756 return;
1757
1758 case tid_SYMBOL_INT:
1759 ION_SYMBOL sym;
1760 ION_CHECK(ion_reader_read_ion_symbol(ser->reader, &sym));
1761 php_ion_symbol_zval(&sym, return_value);
1762 if (ser->annotations.object_type) {
1763 zend_hash_next_index_insert(ser->tmp, return_value);
1764 goto unserialize_struct;
1765 }
1766 zend_hash_next_index_insert(ser->ids, return_value);
1767 return;
1768
1769 case tid_STRING_INT:
1770 ION_STRING str;
1771 ION_CHECK(ion_reader_read_string(ser->reader, &str));
1772 RETVAL_STRINGL((char *) str.value, str.length);
1773 if (ser->annotations.object_type) {
1774 zend_hash_next_index_insert(ser->tmp, return_value);
1775 goto unserialize_struct;
1776 }
1777 zend_hash_next_index_insert(ser->ids, return_value);
1778 return;
1779
1780 case tid_CLOB_INT:
1781 case tid_BLOB_INT:
1782 php_ion_reader_read_lob(ser->reader, return_value);
1783 if (ser->annotations.object_type) {
1784 zend_hash_next_index_insert(ser->tmp, return_value);
1785 goto unserialize_struct;
1786 }
1787 zend_hash_next_index_insert(ser->ids, return_value);
1788 return;
1789
1790 case tid_LIST_INT:
1791 case tid_SEXP_INT: // FIXME
1792 php_ion_unserialize_list(ser, return_value);
1793 if (!ser->annotations.object_type) {
1794 return;
1795 }
1796 /* fall through */
1797
1798 case tid_STRUCT_INT:
1799 unserialize_struct: ;
1800 php_ion_unserialize_struct(ser, return_value);
1801 return;
1802
1803 case tid_none_INT:
1804 ZEND_ASSERT(0);
1805 break;
1806
1807 case tid_DATAGRAM_INT:
1808 ZEND_ASSERT(!"datagram");
1809 case tid_EOF_INT:
1810 return;
1811 }
1812 }
1813
1814 php_ion_decl(unserializer_php, Unserializer_PHP, php_ion_unserializer_php_dtor(obj));
1815
1816 void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_value)
1817 {
1818 zend_object *zo_opt = NULL, *zo_ser = NULL;
1819
1820 if (!ser) {
1821 zo_ser = create_ion_Unserializer_PHP(NULL);
1822 php_ion_unserializer_php *o_ser = php_ion_obj(unserializer_php, zo_ser);
1823 PTR_CHECK(o_ser);
1824 o_ser->unserializer.call_magic = true;
1825 php_ion_unserializer_php_ctor(o_ser);
1826 ION_CATCH();
1827 ser = &o_ser->unserializer;
1828 }
1829
1830 zend_object *zo_reader;
1831 php_ion_reader *reader;
1832 ZVAL_DEREF(zdata);
1833 switch (Z_TYPE_P(zdata)) {
1834 case IS_STRING:
1835 zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader);
1836 reader = php_ion_obj(reader, zo_reader);
1837 reader->type = BUFFER_READER;
1838 reader->buffer = zend_string_copy(Z_STR_P(zdata));
1839 break;
1840
1841 case IS_RESOURCE:
1842 zo_reader = create_ion_Reader_Reader(ce_Reader_Stream_Reader);
1843 reader = php_ion_obj(reader, zo_reader);
1844 reader->type = STREAM_READER;
1845 php_stream_from_zval_no_verify(reader->stream.ptr, zdata);
1846 break;
1847
1848 default:
1849 ZEND_ASSERT(!IS_STRING && !IS_RESOURCE);
1850 }
1851
1852 if (ser->options) {
1853 zo_opt = reader->opt = create_ion_Reader_Options(NULL);
1854 php_ion_obj(reader_options, reader->opt)->opt = *ser->options;
1855 }
1856
1857 php_ion_reader_ctor(reader);
1858 ser->reader = reader->reader;
1859
1860 php_ion_globals_unserializer_step();
1861 php_ion_unserialize_zval(ser, return_value, NULL);
1862 php_ion_globals_unserializer_exit();
1863
1864 OBJ_RELEASE(zo_reader);
1865 if (zo_opt) {
1866 OBJ_RELEASE(zo_opt);
1867 }
1868 if (zo_ser) {
1869 OBJ_RELEASE(zo_ser);
1870 }
1871 }