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