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