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