improve shared symbol tables
[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 "php.h"
14 #include "ext/date/php_date.h"
15
16 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
17 #include "ionc/ion.h"
18
19 #define PHP_ION_SYMBOL_TABLE_VERSION 1
20 #define PHP_ION_SYMBOL(c, s) { \
21 { \
22 0, \
23 { sizeof(s)-1, (BYTE *) s }, \
24 { { 0, NULL }, 0 }, \
25 0 \
26 }, \
27 { sizeof(c)-1, (BYTE *) c } \
28 },
29
30 typedef struct php_ion_global_symbol {
31 ION_SYMBOL s;
32 ION_STRING e;
33 } php_ion_global_symbol;
34
35 static php_ion_global_symbol g_sym_tab_php_sym[] = {
36 #define PHP_ION_SYMBOL_PHP (g_sym_tab_php_sym[0]).s
37 PHP_ION_SYMBOL("PHP", "PHP")
38 #define PHP_ION_SYMBOL_REFERENCE (g_sym_tab_php_sym[1]).s
39 PHP_ION_SYMBOL("Reference", "R")
40 #define PHP_ION_SYMBOL_BACKREF (g_sym_tab_php_sym[2]).s
41 PHP_ION_SYMBOL("Backref", "r")
42 #define PHP_ION_SYMBOL_PROPERTY (g_sym_tab_php_sym[3]).s
43 PHP_ION_SYMBOL("Property", "p")
44 #define PHP_ION_SYMBOL_CLASS_OBJECT (g_sym_tab_php_sym[4]).s
45 PHP_ION_SYMBOL("ClassObject", "c")
46 #define PHP_ION_SYMBOL_CUSTOM_OBJECT (g_sym_tab_php_sym[5]).s
47 PHP_ION_SYMBOL("CustomObject", "C")
48 #define PHP_ION_SYMBOL_OBJECT (g_sym_tab_php_sym[6]).s
49 PHP_ION_SYMBOL("Object", "o")
50 #define PHP_ION_SYMBOL_MAGIC_OBJECT (g_sym_tab_php_sym[7]).s
51 PHP_ION_SYMBOL("MagicObject", "O")
52 #define PHP_ION_SYMBOL_SERIALIZEABLE (g_sym_tab_php_sym[8]).s
53 PHP_ION_SYMBOL("Serializable", "S")
54 #define PHP_ION_SYMBOL_ENUM (g_sym_tab_php_sym[9]).s
55 PHP_ION_SYMBOL("Enum", "E")
56 {{0}, {0}}
57 };
58 #undef PHP_ION_SYMBOL
59
60 static ION_SYMBOL_TABLE *g_sym_tab_php;
61
62 /* [SID => STRING, STRING => SID] */
63 static HashTable g_sym_hash;
64 /* [enum_case_name => SID] */
65 static HashTable g_sym_map;
66
67 #define ION_SYS_SYMBOL_SYMBOL_TABLE \
68 ION_SYS_SYMBOL_ION_SYMBOL_TABLE
69 #define g_sym_add(enum_name, c_name) do { \
70 g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \
71 g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \
72 } while (0)
73
74 static inline void g_sym_hash_add(int sid, const char *str, size_t len)
75 {
76 zval zl, zs;
77 ZVAL_LONG(&zl, sid);
78 ZVAL_STR(&zs, zend_string_init_interned(str, len ,1));
79 zend_hash_add(&g_sym_hash, Z_STR(zs), &zl);
80 zend_hash_index_add(&g_sym_hash, sid, &zs);
81 }
82
83 static inline void g_sym_map_add(int sid, const char *str, size_t len)
84 {
85 zval zv;
86 ZVAL_LONG(&zv, sid);
87 zend_hash_str_add(&g_sym_map, str, len, &zv);
88 }
89
90 static inline int g_sym_init(void)
91 {
92 zend_hash_init(&g_sym_hash, 0, NULL, NULL, 1);
93 zend_hash_init(&g_sym_map, 0, NULL, NULL, 1);
94
95 g_sym_hash_add(0, ZEND_STRL(""));
96 g_sym_map_add(0, ZEND_STRL(""));
97 g_sym_add("Ion", ION);
98 g_sym_add("Ivm_1_0", IVM);
99 g_sym_add("IonSymbolTable", SYMBOL_TABLE);
100 g_sym_add("Name", NAME);
101 g_sym_add("Version", VERSION);
102 g_sym_add("Imports", IMPORTS);
103 g_sym_add("Symbols", SYMBOLS);
104 g_sym_add("MaxId", MAX_ID);
105 g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE);
106
107 int sys_max_id = ION_SYS_SID_SHARED_SYMBOL_TABLE;
108
109 if (IERR_OK != ion_symbol_table_open_with_type(&g_sym_tab_php, NULL, ist_SHARED)) {
110 return FAILURE;
111 }
112 php_ion_global_symbol *ptr = g_sym_tab_php_sym;
113 ion_symbol_table_set_version(g_sym_tab_php, PHP_ION_SYMBOL_TABLE_VERSION);
114 ion_symbol_table_set_name(g_sym_tab_php, &ptr->s.value);
115 while (ptr->e.value) {
116 ion_symbol_table_add_symbol(g_sym_tab_php, &ptr->s.value, &ptr->s.sid);
117 g_sym_hash_add(sys_max_id + ptr->s.sid, (const char *) ptr->s.value.value, ptr->s.value.length);
118 g_sym_map_add(sys_max_id + ptr->s.sid, (const char *) ptr->e.value, ptr->e.length);
119 ++ptr;
120 }
121 ion_symbol_table_lock(g_sym_tab_php);
122 return SUCCESS;
123 }
124
125 typedef struct php_ion_serializer {
126 ION_WRITER *writer;
127 ION_WRITER_OPTIONS *options;
128 smart_str *buffer;
129
130 zend_string *call_custom;
131 zend_bool call_magic;
132 zend_bool multi_seq;
133
134 uint32_t level;
135 HashTable *ids;
136 HashTable *tmp;
137 } php_ion_serializer;
138
139 typedef struct php_ion_annotaions {
140 uint8_t shared_symtab:1;
141 uint8_t backref:1;
142 uint8_t makeref:1;
143 uint8_t object_prop:1;
144 uint8_t object_type;
145 zend_string *object_class;
146 zend_string *property_class;
147 } php_ion_annotations;
148
149 typedef struct php_ion_unserializer {
150 ION_READER *reader;
151 ION_READER_OPTIONS *options;
152 ION_TYPE type;
153
154 zend_string *call_custom;
155 zend_bool call_magic;
156 zend_bool multi_seq;
157
158 uint32_t level;
159 HashTable *ids;
160 HashTable *tmp;
161 HashTable *addref;
162
163 php_ion_annotations annotations;
164 } php_ion_unserializer;
165
166 ZEND_BEGIN_MODULE_GLOBALS(ion)
167
168 struct {
169 decContext ctx;
170 ION_DECIMAL zend_max;
171 ION_DECIMAL zend_min;
172 } decimal;
173
174 struct {
175 HashTable cache;
176 } symbol;
177
178 php_ion_serializer serializer;
179 php_ion_unserializer unserializer;
180
181 struct {
182 HashTable serializer[2];
183 HashTable unserializer[3];
184 } _ht;
185
186 ZEND_END_MODULE_GLOBALS(ion);
187
188 #ifdef ZTS
189 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
190 #else
191 # define php_ion_globals ion_globals
192 #endif
193
194 ZEND_DECLARE_MODULE_GLOBALS(ion);
195
196 static zend_class_entry
197 *ce_Catalog,
198 *ce_Collection,
199 *ce_Decimal,
200 *ce_Decimal_Context,
201 *ce_Decimal_Context_Rounding,
202 *ce_LOB,
203 *ce_Reader,
204 *ce_Reader_Options,
205 *ce_Reader_Reader,
206 *ce_Reader_Buffer,
207 *ce_Reader_Stream,
208 *ce_Reader_Buffer_Reader,
209 *ce_Reader_Stream_Reader,
210 *ce_Serializer,
211 *ce_Serializer_PHP,
212 *ce_Symbol,
213 *ce_Symbol_ImportLocation,
214 *ce_Symbol_Enum,
215 *ce_Symbol_Table,
216 *ce_Symbol_Table_Local,
217 *ce_Symbol_Table_PHP,
218 *ce_Symbol_Table_Shared,
219 *ce_Symbol_Table_System,
220 *ce_Timestamp,
221 *ce_Timestamp_Precision,
222 *ce_Type,
223 *ce_Unserializer,
224 *ce_Unserializer_PHP,
225 *ce_Writer,
226 *ce_Writer_Options,
227 *ce_Writer_Buffer,
228 *ce_Writer_Buffer_Writer,
229 *ce_Writer_Stream,
230 *ce_Writer_Stream_Writer,
231 *ce_Writer_Writer
232 ;
233
234 static inline void php_ion_globals_symbols_init(void)
235 {
236 zend_hash_init(&php_ion_globals.symbol.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
237 }
238
239 static inline void php_ion_globals_symbols_dtor(void)
240 {
241 zend_hash_destroy(&php_ion_globals.symbol.cache);
242 }
243
244 static inline void php_ion_globals_serializer_init(void)
245 {
246 php_ion_serializer *s = &php_ion_globals.serializer;
247 HashTable *h = php_ion_globals._ht.serializer;
248
249 zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
250 zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
251 }
252
253 static inline uint32_t php_ion_globals_serializer_step(void)
254 {
255 php_ion_serializer *s = &php_ion_globals.serializer;
256 uint32_t level;
257
258 if (!(level = s->level++)) {
259 zend_hash_clean(s->ids);
260 zend_hash_clean(s->tmp);
261 }
262 return level;
263 }
264
265 static inline uint32_t php_ion_globals_serializer_exit(void)
266 {
267 php_ion_serializer *s = &php_ion_globals.serializer;
268
269 ZEND_ASSERT(s->level);
270 if (!--s->level) {
271 zend_hash_clean(s->ids);
272 zend_hash_clean(s->tmp);
273 }
274 return s->level;
275 }
276
277 static inline void php_ion_globals_serializer_dtor(void)
278 {
279 php_ion_serializer *s = &php_ion_globals.serializer;
280
281 zend_hash_destroy(s->tmp);
282 zend_hash_destroy(s->ids);
283 }
284
285 void ZVAL_ADDREF(zval *zv)
286 {
287 if (Z_ISREF_P(zv)) {
288 Z_TRY_ADDREF_P(Z_REFVAL_P(zv));
289 } else {
290 Z_TRY_ADDREF_P(zv);
291 }
292 }
293 static inline void php_ion_globals_unserializer_init(void)
294 {
295 php_ion_unserializer *s = &php_ion_globals.unserializer;
296 HashTable *h = php_ion_globals._ht.unserializer;
297
298 zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
299 zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
300 zend_hash_init(s->addref = &h[2], 0, NULL, ZVAL_ADDREF, 0);
301 }
302
303 static inline void php_ion_globals_unserializer_step(void)
304 {
305 php_ion_unserializer *s = &php_ion_globals.unserializer;
306
307 if (!s->level++) {
308 zend_hash_clean(s->addref);
309 zend_hash_clean(s->ids);
310 zend_hash_clean(s->tmp);
311 }
312 }
313
314 static inline void php_ion_globals_unserializer_exit(void)
315 {
316 php_ion_unserializer *s = &php_ion_globals.unserializer;
317
318 ZEND_ASSERT(s->level);
319 if (!--s->level) {
320 zend_hash_clean(s->addref);
321 zend_hash_clean(s->ids);
322 zend_hash_clean(s->tmp);
323 }
324 }
325
326 static inline void php_ion_globals_unserializer_dtor(void)
327 {
328 php_ion_unserializer *s = &php_ion_globals.unserializer;
329
330 zend_hash_destroy(s->addref);
331 zend_hash_destroy(s->ids);
332 zend_hash_destroy(s->tmp);
333 }
334
335 #define php_ion_decl(type, cname, ...) \
336 static zend_object_handlers oh_ ## cname; \
337 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
338 { \
339 if (!ce) ce = ce_ ## cname; \
340 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
341 zend_object_std_init(&o->std, ce); \
342 object_properties_init(&o->std, ce); \
343 o->std.handlers = &oh_ ## cname; \
344 return &o->std; \
345 } \
346 static inline void free_ion_ ## cname(zend_object *std) \
347 { \
348 php_ion_ ## type *obj = php_ion_obj(type, std); \
349 __VA_ARGS__; \
350 zend_object_std_dtor(std); \
351 (void) obj; \
352 }
353 #define php_ion_register(type, cname, ...) do { \
354 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
355 ce_ ## cname ->create_object = create_ion_ ## cname; \
356 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
357 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
358 oh_ ## cname .free_obj = free_ion_ ## cname; \
359 } while (0)
360
361 #define php_ion_obj(type, obj) \
362 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
363
364 #define ION_CHECK_RETURN(r, err, ...) do { \
365 iERR __err = err; \
366 if (__err) { \
367 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
368 __VA_ARGS__; \
369 return r; \
370 } \
371 } while (0)
372
373 #define ION_CHECK(err, ...) \
374 ION_CHECK_RETURN(, err, __VA_ARGS__)
375
376 #define ION_CATCH(...) do { \
377 if (EG(exception)) { \
378 __VA_ARGS__; \
379 return; \
380 } \
381 } while (0)
382
383 #define PTR_CHECK(ptr, ...) do { \
384 if (!(ptr)) { \
385 zend_throw_error(NULL, "Uninitialized object"); \
386 __VA_ARGS__; \
387 return; \
388 } \
389 } while (0)
390
391 #define OBJ_CHECK(obj, ...) do { \
392 PTR_CHECK(obj, __VA_ARGS__); \
393 PTR_CHECK(*((void **)obj), __VA_ARGS__); \
394 } while (0)
395
396 static inline ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs)
397 {
398 is->length = zs ? zs->len : 0;
399 is->value = (BYTE *) (zs ? zs->val : NULL);
400 return is;
401 }
402
403 static inline zend_string *zend_string_from_ion(const ION_STRING *s)
404 {
405 return zend_string_init((const char *) s->value, s->length, 0);
406 }
407
408 static inline void update_property_obj(zend_object *obj, const char *n, size_t l, zend_object *p)
409 {
410 zval zobj;
411 ZVAL_OBJ(&zobj, p);
412 zend_update_property(obj->ce, obj, n, l, &zobj);
413 }
414
415 typedef struct php_ion_type {
416 ION_TYPE typ;
417 zend_object std;
418 } php_ion_type;
419
420 php_ion_decl(type, Type);
421
422 #define RETURN_IONTYPE(typ) do { \
423 zend_object *__zo = php_ion_type_fetch(typ); \
424 if (UNEXPECTED(!__zo)) { \
425 RETURN_THROWS(); \
426 } \
427 RETURN_OBJ_COPY(__zo); \
428 } while(0)
429
430 static inline zend_object *php_ion_type_fetch(ION_TYPE typ)
431 {
432 zend_long index = ION_TYPE_INT(typ);
433 zval *ztype = zend_hash_index_find(ce_Type->backed_enum_table, index);
434
435 if (UNEXPECTED(!ztype || Z_TYPE_P(ztype) != IS_STRING)) {
436 zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", index, ZSTR_VAL(ce_Type->name));
437 return NULL;
438 }
439 return zend_enum_get_case(ce_Type, Z_STR_P(ztype));
440 }
441
442 typedef struct php_ion_symbol_iloc {
443 ION_SYMBOL_IMPORT_LOCATION loc;
444 zend_string *name;
445 zend_object std;
446 } php_ion_symbol_iloc;
447
448 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc *obj)
449 {
450 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("location"), obj->loc.location);
451 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("name"), obj->name);
452 ion_string_from_zend(&obj->loc.name, obj->name);
453 }
454
455 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc *obj)
456 {
457 zend_string_release(obj->name);
458 }
459
460 php_ion_decl(symbol_iloc, Symbol_ImportLocation, php_ion_symbol_iloc_dtor(obj));
461
462 typedef struct php_ion_symbol {
463 ION_SYMBOL sym;
464 zend_string *value;
465 zend_object *iloc, std;
466 } php_ion_symbol;
467
468 static inline int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) {
469 zend_string *zs1 = zval_get_string(zv1);
470 zend_string *zs2 = zval_get_string(zv2);
471
472 if (EG(exception)) {
473 return 0;
474 }
475
476 int result;
477 if (zs1->len > zs2->len) {
478 result = 1;
479 } else if (zs2->len > zs1->len) {
480 result = -1;
481 } else {
482 result = memcmp(zs1->val, zs2->val, zs1->len);
483 }
484 zend_string_release(zs1);
485 zend_string_release(zs2);
486 return result;
487 }
488
489 static inline void php_ion_symbol_ctor(php_ion_symbol *obj)
490 {
491 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("sid"),
492 obj->sym.sid);
493 if (obj->value) {
494 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("value"), obj->value);
495 } else{
496 zend_update_property_null(obj->std.ce, &obj->std, ZEND_STRL("value"));
497 }
498 ion_string_from_zend(&obj->sym.value, obj->value);
499 if (obj->iloc) {
500 update_property_obj(&obj->std, ZEND_STRL("importLocation"), obj->iloc);
501 obj->sym.import_location = php_ion_obj(symbol_iloc, obj->iloc)->loc;
502 } else {
503 zend_update_property_null(obj->std.ce, &obj->std, ZEND_STRL("importLocation"));
504 }
505 }
506
507 static inline void php_ion_symbol_dtor(php_ion_symbol *obj)
508 {
509 if (obj->value) {
510 zend_string_release(obj->value);
511 }
512 }
513
514 static inline void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value)
515 {
516 object_init_ex(return_value, ce_Symbol);
517 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(return_value));
518
519 sym->sym.sid = sym_ptr->sid;
520 sym->value = zend_string_from_ion(&sym_ptr->value);
521 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
522 zval ziloc;
523 object_init_ex(&ziloc, ce_Symbol_ImportLocation);
524 sym->iloc = Z_OBJ(ziloc);
525
526 php_ion_symbol_iloc *iloc = php_ion_obj(symbol_iloc, sym->iloc);
527 iloc->loc.location = sym_ptr->import_location.location;
528 iloc->name = zend_string_from_ion(&sym_ptr->import_location.name);
529
530 php_ion_symbol_iloc_ctor(iloc);
531 }
532
533 php_ion_symbol_ctor(sym);
534
535 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
536 GC_DELREF(sym->iloc);
537 }
538 }
539
540 static inline zval *php_ion_global_symbol_fetch_by_enum(zend_string *name)
541 {
542 zval *zgs = zend_hash_find(&php_ion_globals.symbol.cache, name);
543 if (!zgs) {
544 zval *zid = zend_hash_find(&g_sym_map, name);
545 if (zid) {
546 zval *zss = zend_hash_index_find(&g_sym_hash, Z_LVAL_P(zid));
547 if (zss) {
548 zval zsym;
549 object_init_ex(&zsym, ce_Symbol);
550 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ(zsym));
551 sym->sym.sid = Z_LVAL_P(zid);
552 sym->value = zval_get_string(zss);
553 php_ion_symbol_ctor(sym);
554 zgs = zend_hash_add(&php_ion_globals.symbol.cache, name, &zsym);
555 }
556 }
557 }
558 return zgs;
559 }
560
561 php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj));
562
563 typedef struct php_ion_symbol_table {
564 ION_SYMBOL_TABLE *tab;
565 int (*dtor)(ION_SYMBOL_TABLE *);
566 zend_object std;
567 } php_ion_symbol_table;
568
569 static inline void php_ion_symbol_table_ctor(php_ion_symbol_table *obj)
570 {
571 OBJ_CHECK(obj);
572
573 ION_STRING is;
574 if (IERR_OK == ion_symbol_table_get_name(obj->tab, &is)) {
575 zend_update_property_stringl(obj->std.ce, &obj->std, ZEND_STRL("name"), (char *) is.value, is.length);
576 }
577 int32_t iv;
578 if (IERR_OK == ion_symbol_table_get_version(obj->tab, &iv)) {
579 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("version"), iv);
580 }
581 }
582
583 static inline void php_ion_symbol_table_dtor(php_ion_symbol_table *obj)
584 {
585 if (obj->tab) {
586 if (obj->dtor) {
587 obj->dtor(obj->tab);
588 }
589 obj->tab = NULL;
590 }
591 }
592
593 static inline void php_ion_symbol_table_import(php_ion_symbol_table *obj, php_ion_symbol_table *import)
594 {
595 OBJ_CHECK(obj);
596 OBJ_CHECK(import);
597
598 zval tmp;
599 zval *zimports = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), 0, &tmp);
600 if (zimports) {
601 zend_ulong idx = (uintptr_t) &import->std.gc;
602 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports), idx)) {
603 ION_CHECK(ion_symbol_table_import_symbol_table(obj->tab, import->tab));
604
605 SEPARATE_ARRAY(zimports);
606 GC_ADDREF(&import->std);
607 add_index_object(zimports, idx, &import->std);
608 zend_update_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), zimports);
609 }
610 }
611 }
612
613 static inline void php_ion_symbol_table_symbol_zval(php_ion_symbol_table *obj, ION_SYMBOL *sym, zval *return_value)
614 {
615 zval tmp;
616 zval *zsyms = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbols"), 0, &tmp);
617 if (zsyms) {
618 zval *zsym = zend_hash_index_find(Z_ARRVAL_P(zsyms), sym->sid);
619 if (zsym) {
620 RETURN_COPY(zsym);
621 }
622 }
623
624 php_ion_symbol_zval(sym, return_value);
625
626 if (zsyms) {
627 SEPARATE_ARRAY(zsyms);
628 ZVAL_ADDREF(return_value);
629 add_index_zval(zsyms, sym->sid, return_value);
630 }
631 }
632
633 php_ion_decl(symbol_table, Symbol_Table, php_ion_symbol_table_dtor(obj));
634
635 typedef struct php_ion_decimal_ctx {
636 decContext ctx;
637 zend_object std;
638 } php_ion_decimal_ctx;
639
640 #define php_ion_decimal_ctx_init_max(c, rounding) \
641 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
642 static inline void php_ion_decimal_ctx_init(decContext *ctx,
643 zend_long digits, zend_long emax, zend_long emin, zend_long round, zend_bool clamp)
644 {
645 memset(ctx, 0, sizeof(*ctx));
646 ctx->digits = digits;
647 ctx->emax = emax;
648 ctx->emin = emin;
649 ctx->round = round;
650 ctx->clamp = clamp;
651 }
652
653 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj, zend_object *o_round)
654 {
655 if (!obj->ctx.digits) {
656 php_ion_decimal_ctx_init_max(&obj->ctx, DEC_ROUND_HALF_EVEN);
657 }
658 if (o_round) {
659 update_property_obj(&obj->std, ZEND_STRL("round"), o_round);
660 } else {
661 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("round"), obj->ctx.round);
662 }
663 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("digits"), obj->ctx.digits);
664 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("eMax"), obj->ctx.emax);
665 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("eMin"), obj->ctx.emin);
666 zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("clamp"), obj->ctx.clamp);
667 }
668
669 php_ion_decl(decimal_ctx, Decimal_Context);
670
671 typedef struct php_ion_decimal {
672 ION_DECIMAL dec;
673 zend_object *ctx, std;
674 } php_ion_decimal;
675
676 static inline void php_ion_decimal_from_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long num)
677 {
678 if (num <= INT32_MAX && num >= INT32_MIN) {
679 ION_CHECK(ion_decimal_from_int32(dec, num));
680 } else if (num > 0 && num <= UINT32_MAX) {
681 ION_CHECK(ion_decimal_from_uint32(dec, num));
682 } else {
683 ION_INT *iint;
684 ION_CHECK(ion_int_alloc(NULL, &iint));
685 ION_CHECK(ion_int_from_long(iint, num),
686 ion_int_free(iint));
687 /* WATCH OUT: BS API */
688 dec->type = ION_DECIMAL_TYPE_QUAD;
689 ION_CHECK(ion_decimal_from_ion_int(dec, ctx, iint),
690 ion_int_free(iint));
691 ion_int_free(iint);
692 }
693 }
694
695 static inline zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec)
696 {
697 zend_string *zstr = zend_string_alloc(ION_DECIMAL_STRLEN(dec), 0);
698 (void) ion_decimal_to_string(dec, zstr->val);
699 return zend_string_truncate(zstr, strlen(zstr->val), 0);
700 }
701
702 static inline void php_ion_decimal_to_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long *l)
703 {
704 ION_INT *ii = NULL;
705 ION_CHECK(ion_int_alloc(NULL, &ii));
706 ION_CHECK(ion_decimal_to_ion_int(dec, ctx, ii), ion_int_free(ii));
707 int64_t i64;
708 ION_CHECK(ion_int_to_int64(ii, &i64), ion_int_free(ii));
709 *l = i64;
710 ion_int_free(ii);
711 }
712
713 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj)
714 {
715 int32_t result;
716
717 if (!ion_decimal_is_integer(&obj->dec)) {
718 return false;
719 }
720
721 result = 1;
722 ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_max, &php_ion_globals.decimal.ctx, &result);
723 if (result == 1) {
724 return false;
725 }
726 result = -1;
727 ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_min, &php_ion_globals.decimal.ctx, &result);
728 if (result == -1) {
729 return false;
730 }
731 return true;
732 }
733
734 static inline void php_ion_decimal_ctor(php_ion_decimal *obj)
735 {
736 if (!obj->ctx) {
737 zval zdc;
738 object_init_ex(&zdc, ce_Decimal_Context);
739 obj->ctx = Z_OBJ(zdc);
740 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL);
741 GC_DELREF(obj->ctx);
742 }
743 update_property_obj(&obj->std, ZEND_STRL("context"), obj->ctx);
744
745 if (php_ion_decimal_fits_zend_long(obj)) {
746 zend_long l;
747 php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
748 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("number"), l);
749 } else {
750 zend_string *zstr = php_ion_decimal_to_string(&obj->dec);
751 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("number"), zstr);
752 zend_string_release(zstr);
753 }
754 }
755
756 static inline void php_ion_decimal_dtor(php_ion_decimal *obj)
757 {
758 ion_decimal_free(&obj->dec);
759 }
760
761 php_ion_decl(decimal, Decimal, php_ion_decimal_dtor(obj));
762
763 typedef php_date_obj php_ion_timestamp;
764
765 static inline zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx)
766 {
767 if (!ctx) {
768 ctx = &php_ion_globals.decimal.ctx;
769 }
770 decQuad microsecs, result;
771 decQuadMultiply(&result, decQuadFromInt32(&microsecs, 1000000), frac, ctx);
772 return (zend_long) decQuadToUInt32(&result, ctx, DEC_ROUND_HALF_EVEN);
773 }
774
775 static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, zend_long usec, decContext *ctx)
776 {
777 if (!ctx) {
778 ctx = &php_ion_globals.decimal.ctx;
779 }
780 decQuad microsecs, us;
781 return decQuadDivide(frac, decQuadFromInt32(&us, usec), decQuadFromInt32(&microsecs, 1000000), ctx);
782 }
783
784 static inline zend_string *php_dt_format_from_precision(int precision)
785 {
786 switch (precision & 0x7f) {
787 case ION_TS_FRAC:
788 return zend_string_init(ZEND_STRL("c"), 0);
789 case ION_TS_SEC:
790 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
791 case ION_TS_MIN:
792 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
793 case ION_TS_DAY:
794 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
795 case ION_TS_MONTH:
796 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
797 case ION_TS_YEAR:
798 return zend_string_init(ZEND_STRL("Y\\T"), 0);
799 default:
800 return zend_string_init(ZEND_STRL("c"), 0);
801 }
802 }
803
804 static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContext *ctx, zend_string **fmt)
805 {
806 timelib_time *time = timelib_time_ctor();
807
808 int precision = ION_TS_FRAC;
809 ion_timestamp_get_precision(ts, &precision);
810 switch (precision) {
811 case ION_TS_FRAC:
812 time->us = php_usec_from_ion(&ts->fraction, ctx);
813 /* fallthrough */
814 case ION_TS_SEC:
815 time->s = ts->seconds;
816 /* fallthrough */
817 case ION_TS_MIN:
818 time->i = ts->minutes;
819 time->h = ts->hours;
820 /* fallthrough */
821 case ION_TS_DAY:
822 time->d = ts->day;
823 /* fallthrough */
824 case ION_TS_MONTH:
825 time->m = ts->month;
826 /* fallthrough */
827 case ION_TS_YEAR:
828 time->y = ts->year;
829 /* fallthrough */
830 default:
831 time->z = ts->tz_offset * 60;
832 if (time->z) {
833 time->zone_type = TIMELIB_ZONETYPE_OFFSET;
834 } else {
835 time->zone_type = TIMELIB_ZONETYPE_ID;
836 time->tz_info = get_timezone_info();
837 }
838 }
839
840 if (fmt) {
841 *fmt = php_dt_format_from_precision(precision);
842 }
843 return time;
844 }
845
846 static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_timestamp *ts, decContext *ctx)
847 {
848 memset(buf, 0, sizeof(*buf));
849
850 zval tmp;
851 int precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp));
852
853 if (!precision || precision > ION_TS_FRAC) {
854 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
855 "Invalid precision (%d) of ion\\Timestamp", precision);
856 } else switch ((buf->precision = precision)) {
857 case ION_TS_FRAC:
858 ion_ts_frac_from_usec(&buf->fraction, ts->time->us, ctx);
859 /* fallthrough */
860 case ION_TS_SEC:
861 buf->seconds = ts->time->s;
862 /* fallthrough */
863 case ION_TS_MIN:
864 buf->minutes = ts->time->i;
865 /* fallthrough */
866 case ION_TS_DAY:
867 buf->hours = ts->time->h;
868 buf->day = ts->time->d;
869 /* fallthrough */
870 case ION_TS_MONTH:
871 buf->month = ts->time->m;
872 /* fallthrough */
873 case ION_TS_YEAR:
874 buf->year = ts->time->y;
875 /* fallthrough */
876 default:
877 buf->tz_offset = ts->time->z / 60;
878 if (buf->tz_offset) {
879 buf->precision |= 0x80;
880 }
881 }
882
883 return buf;
884 }
885
886 static inline void php_ion_timestamp_ctor(php_ion_timestamp *obj, zend_long precision, zend_string *fmt, zend_string *dt, zval *tz)
887 {
888 if (!obj->time) {
889 php_date_initialize(obj, dt ? dt->val : "", dt ? dt->len : 0, fmt ? fmt->val : NULL, tz, PHP_DATE_INIT_CTOR);
890 }
891 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("precision"), precision);
892
893 fmt = php_dt_format_from_precision(precision);
894 zend_update_property_str(obj->std.ce, &obj->std, ZEND_STRL("format"), fmt);
895 zend_string_release(fmt);
896 }
897
898 typedef struct php_ion_catalog {
899 ION_CATALOG *cat;
900 zend_object std;
901 } php_ion_catalog;
902
903 static inline void php_ion_catalog_ctor(php_ion_catalog *obj)
904 {
905 ION_CHECK(ion_catalog_open(&obj->cat));
906 }
907
908 static inline void php_ion_catalog_dtor(php_ion_catalog *obj)
909 {
910 if (obj->cat) {
911 ion_catalog_close(obj->cat);
912 }
913 }
914
915 static inline zend_string *ion_symbol_table_to_key(ION_SYMBOL_TABLE *tab)
916 {
917 int32_t version;
918 ION_STRING is;
919 ION_CHECK_RETURN(NULL, ion_symbol_table_get_name(tab, &is));
920 ION_CHECK_RETURN(NULL, ion_symbol_table_get_version(tab, &version));
921
922 smart_str s = {0};
923 smart_str_appendl(&s, (char *) is.value, is.length);
924 smart_str_appendc(&s, ':');
925 smart_str_append_long(&s, version);
926 smart_str_0(&s);
927
928 return s.s;
929 }
930
931 static inline void php_ion_catalog_add_symbol_table(php_ion_catalog *obj, php_ion_symbol_table *tab)
932 {
933 OBJ_CHECK(obj);
934 OBJ_CHECK(tab);
935
936 zval tmp;
937 zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp);
938 if (ztabs) {
939 zend_ulong idx = (uintptr_t) &tab->std.gc;
940 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs), idx)) {
941 zend_string *key = ion_symbol_table_to_key(tab->tab);
942 if (key) {
943 ION_CHECK(ion_catalog_add_symbol_table(obj->cat, tab->tab),
944 zend_string_release(key));
945 SEPARATE_ARRAY(ztabs);
946 GC_ADDREF(&tab->std);
947 add_index_object(ztabs, idx, &tab->std);
948 GC_ADDREF(&tab->std);
949 add_assoc_object_ex(ztabs, key->val, key->len, &tab->std);
950 zend_update_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), ztabs);
951 zend_string_release(key);
952 }
953 }
954 }
955 }
956
957 static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_SYMBOL_TABLE *tab, zval *return_value)
958 {
959 zend_string *key = ion_symbol_table_to_key(tab);
960 PTR_CHECK(key);
961
962 zval tmp;
963 zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp);
964 if (ztabs) {
965 zval *ztab = zend_hash_find(Z_ARRVAL_P(ztabs), key);
966 if (ztab) {
967 zend_string_release(key);
968 RETURN_COPY(ztab);
969 }
970 }
971
972 php_error_docref(NULL, E_NOTICE, "Previously unknown ion\\Symbol\\Table encountered: %s", key->val);
973
974 object_init_ex(return_value, ce_Symbol_Table_Shared);
975 php_ion_symbol_table *o_tab = php_ion_obj(symbol_table, Z_OBJ_P(return_value));
976 OBJ_CHECK(o_tab, zend_string_release(key));
977 php_ion_symbol_table_ctor(o_tab);
978
979 if (ztabs) {
980 SEPARATE_ARRAY(ztabs);
981 ZVAL_ADDREF(return_value);
982 add_index_zval(ztabs, (uintptr_t) &o_tab->std.gc, return_value);
983 ZVAL_ADDREF(return_value);
984 add_assoc_zval_ex(ztabs, key->val, key->len, return_value);
985 }
986 zend_string_release(key);
987 }
988
989 php_ion_decl(catalog, Catalog, php_ion_catalog_dtor(obj));
990
991 typedef struct php_ion_reader_options {
992 ION_READER_OPTIONS opt;
993 zend_object *cat, *dec_ctx, *cb, std;
994 } php_ion_reader_options;
995
996 php_ion_decl(reader_options, Reader_Options);
997
998 typedef struct php_ion_reader {
999 ION_READER *reader;
1000 ION_TYPE state;
1001 enum {
1002 BUFFER_READER,
1003 STREAM_READER,
1004 } type;
1005 union {
1006 zend_string *buffer;
1007 struct {
1008 php_stream *ptr;
1009 ION_STRING buf;
1010 } stream;
1011 };
1012 zend_object *opt, std;
1013 } php_ion_reader;
1014
1015 static inline iERR php_ion_reader_stream_handler(struct _ion_user_stream *user)
1016 {
1017 php_ion_reader *reader = (php_ion_reader *) user->handler_state;
1018 size_t remaining = 0, spare = reader->stream.buf.length;
1019
1020 if (user->curr && user->limit && (remaining = user->limit - user->curr)) {
1021 memmove(reader->stream.buf.value, user->curr, remaining);
1022 user->limit -= remaining;
1023 spare -= remaining;
1024 } else {
1025 user->curr = user->limit = reader->stream.buf.value;
1026 }
1027
1028 ssize_t read = php_stream_read(reader->stream.ptr, (char *) user->limit, spare);
1029 if (EXPECTED(read > 0)) {
1030 user->limit += read;
1031 return IERR_OK;
1032 }
1033
1034 if (EXPECTED(read == 0)) {
1035 return IERR_EOF;
1036 }
1037
1038 return IERR_READ_ERROR;
1039 }
1040
1041 static inline iERR on_context_change(void *context, ION_COLLECTION *imports)
1042 {
1043 if (context) {
1044 php_ion_reader *obj = php_ion_obj(reader, context);
1045 (void) obj;
1046 }
1047 fprintf(stderr, "%s\n", __FUNCTION__);
1048 return IERR_OK;
1049 }
1050
1051 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER = {
1052 on_context_change,
1053 NULL
1054 };
1055
1056 static inline void php_ion_reader_ctor(php_ion_reader *obj)
1057 {
1058 iERR err;
1059 php_ion_reader_options *opt = php_ion_obj(reader_options, obj->opt);
1060
1061 if (opt) {
1062 opt->opt.context_change_notifier.context = obj;
1063 }
1064 if (obj->type == STREAM_READER) {
1065 PTR_CHECK(obj->stream.ptr);
1066 GC_ADDREF(obj->stream.ptr->res);
1067
1068 obj->stream.buf.length = opt && opt->opt.allocation_page_size ? opt->opt.allocation_page_size : 0x10000;
1069 obj->stream.buf.value = emalloc(obj->stream.buf.length);
1070 err = ion_reader_open_stream(&obj->reader, obj, php_ion_reader_stream_handler, opt ? &opt->opt : NULL);
1071
1072 } else {
1073 err = ion_reader_open_buffer(&obj->reader,
1074 (BYTE *) obj->buffer->val, obj->buffer->len,
1075 opt ? &opt->opt : NULL);
1076 }
1077 if (opt) {
1078 opt->opt.context_change_notifier.context = NULL;
1079 }
1080
1081 ION_CHECK(err);
1082 OBJ_CHECK(obj);
1083 }
1084 static inline void php_ion_reader_dtor(php_ion_reader *obj)
1085 {
1086 if (obj->reader) {
1087 ion_reader_close(obj->reader);
1088 }
1089 if (obj->type == STREAM_READER) {
1090 if (obj->stream.buf.value) {
1091 efree(obj->stream.buf.value);
1092 }
1093 if (obj->stream.ptr) {
1094 zend_list_delete(obj->stream.ptr->res);
1095 }
1096 } else {
1097 if (obj->buffer) {
1098 zend_string_release(obj->buffer);
1099 }
1100 }
1101 }
1102
1103 php_ion_decl(reader, Reader_Reader, php_ion_reader_dtor(obj));
1104
1105 typedef struct php_ion_writer_options {
1106 ION_WRITER_OPTIONS opt;
1107 zend_object *cat, *dec_ctx, *col, std;
1108 } php_ion_writer_options;
1109
1110 php_ion_decl(writer_options, Writer_Options);
1111
1112 typedef struct php_ion_writer {
1113 ION_WRITER *writer;
1114 enum {
1115 BUFFER_WRITER,
1116 STREAM_WRITER,
1117 } type;
1118 union {
1119 struct {
1120 zval val;
1121 smart_str str;
1122 } buffer;
1123 struct {
1124 ION_STRING buf;
1125 php_stream *ptr;
1126 } stream;
1127 };
1128 zend_object *opt, std;
1129
1130 } php_ion_writer;
1131
1132 static inline iERR php_ion_writer_stream_handler(struct _ion_user_stream *user)
1133 {
1134 php_ion_writer *writer = (php_ion_writer *) user->handler_state;
1135
1136 if (EXPECTED(user->limit && user->curr)) {
1137 ptrdiff_t len = user->curr - writer->stream.buf.value;
1138 if (len != php_stream_write(writer->stream.ptr, (char *) writer->stream.buf.value, len)) {
1139 return IERR_WRITE_ERROR;
1140 }
1141 }
1142 user->curr = writer->stream.buf.value;
1143 user->limit = writer->stream.buf.value + writer->stream.buf.length;
1144 return IERR_OK;
1145 }
1146
1147 #define REF_STR() do { \
1148 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
1149 GC_ADDREF(obj->buffer.str.s); \
1150 } while (0)
1151
1152 #define NEW_REF_STR() do {\
1153 if (Z_STR_P(ref) != obj->buffer.str.s) { \
1154 zval_ptr_dtor(ref); \
1155 REF_STR(); \
1156 } \
1157 } while(0)
1158
1159 static inline void php_ion_writer_stream_init(php_ion_writer *obj, php_ion_writer_options *opt)
1160 {
1161 PTR_CHECK(obj->stream.ptr);
1162 GC_ADDREF(obj->stream.ptr->res);
1163
1164 obj->stream.buf.length = opt ? opt->opt.allocation_page_size : 0x1000;
1165 obj->stream.buf.value = emalloc(obj->stream.buf.length);
1166 }
1167 static inline void php_ion_writer_buffer_init(php_ion_writer *obj)
1168 {
1169 zval *ref = &obj->buffer.val;
1170 ZVAL_DEREF(ref);
1171
1172 smart_str_alloc(&obj->buffer.str, 0, 0);
1173 smart_str_0(&obj->buffer.str);
1174 REF_STR();
1175 }
1176
1177 static inline void php_ion_writer_buffer_grow(php_ion_writer *obj)
1178 {
1179 zval *ref = &obj->buffer.val;
1180 ZVAL_DEREF(ref);
1181
1182 switch (GC_REFCOUNT(obj->buffer.str.s)) {
1183 case 2:
1184 // nothing to do
1185 break;
1186 case 1:
1187 // we've been separated
1188 GC_ADDREF(obj->buffer.str.s);
1189 break;
1190 default:
1191 // we have to separate
1192 fprintf(stderr, "SEPARATE\n");
1193 obj->buffer.str.s = zend_string_dup(obj->buffer.str.s, 0);
1194 break;
1195 }
1196
1197 zend_string *old = obj->buffer.str.s;
1198 GC_DELREF(old);
1199 smart_str_erealloc(&obj->buffer.str, obj->buffer.str.a << 1);
1200 if (old == obj->buffer.str.s) {
1201 GC_ADDREF(old);
1202 } else if(old == Z_STR_P(ref)) {
1203 ZVAL_NULL(ref);
1204 }
1205
1206 NEW_REF_STR();
1207 }
1208
1209 static inline iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user)
1210 {
1211 php_ion_writer *writer = (php_ion_writer *) user->handler_state;
1212
1213 if (user->curr) {
1214 writer->buffer.str.s->len += user->curr - (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len];
1215 smart_str_0(&writer->buffer.str);
1216 if (user->limit == user->curr) {
1217 php_ion_writer_buffer_grow(writer);
1218 }
1219 }
1220 user->curr = (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len];
1221 user->limit = user->curr + writer->buffer.str.a - writer->buffer.str.s->len;
1222
1223 return IERR_OK;
1224 }
1225
1226 static inline void php_ion_writer_ctor(php_ion_writer *obj)
1227 {
1228 if (obj->opt) {
1229 update_property_obj(&obj->std, ZEND_STRL("options"), obj->opt);
1230 }
1231
1232 php_ion_writer_options *opt = php_ion_obj(writer_options, obj->opt);
1233 ION_STREAM_HANDLER h;
1234 if (obj->type == STREAM_WRITER) {
1235 h = php_ion_writer_stream_handler;
1236 php_ion_writer_stream_init(obj, opt);
1237 } else {
1238 h = php_ion_writer_buffer_handler;
1239 php_ion_writer_buffer_init(obj);
1240 }
1241
1242 ION_CHECK(ion_writer_open_stream(&obj->writer, h, obj, opt ? &opt->opt : NULL));
1243 OBJ_CHECK(obj);
1244 }
1245
1246 static inline void php_ion_writer_dtor(php_ion_writer *obj)
1247 {
1248 if (obj->writer) {
1249 ion_writer_close(obj->writer);
1250 }
1251 if (obj->type == STREAM_WRITER) {
1252 if (obj->stream.buf.value) {
1253 efree(obj->stream.buf.value);
1254 }
1255 if (obj->stream.ptr) {
1256 zend_list_delete(obj->stream.ptr->res);
1257 }
1258 } else {
1259 if (obj->buffer.str.s) {
1260 smart_str_0(&obj->buffer.str);
1261 zend_string_release(obj->buffer.str.s);
1262 }
1263 zval_ptr_dtor(&obj->buffer.val);
1264 }
1265 }
1266
1267 php_ion_decl(writer, Writer_Writer, php_ion_writer_dtor(obj));
1268
1269 typedef struct php_ion_serializer_php {
1270 php_ion_serializer serializer;
1271 zend_object *opt, std;
1272 } php_ion_serializer_php;
1273
1274 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj)
1275 {
1276 php_ion_serializer *global_ser = &php_ion_globals.serializer;
1277 ser_obj->serializer.ids = global_ser->ids;
1278 ser_obj->serializer.tmp = global_ser->tmp;
1279
1280 zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
1281 ser_obj->serializer.call_magic);
1282 if (ser_obj->serializer.call_custom) {
1283 zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"),
1284 ser_obj->serializer.call_custom);
1285 ser_obj->serializer.call_custom = zend_string_tolower(ser_obj->serializer.call_custom);
1286 } else {
1287 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
1288 }
1289 if (!ser_obj->opt) {
1290 ser_obj->opt = create_ion_Writer_Options(NULL);
1291 } else {
1292 GC_ADDREF(ser_obj->opt);
1293 }
1294 ser_obj->serializer.options = &php_ion_obj(writer_options, ser_obj->opt)->opt;
1295 update_property_obj(&ser_obj->std, ZEND_STRL("writerOptions"), ser_obj->opt);
1296 GC_DELREF(ser_obj->opt);
1297 }
1298
1299 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php *obj)
1300 {
1301 if (obj->serializer.call_custom) {
1302 zend_string_release(obj->serializer.call_custom);
1303 }
1304 }
1305
1306 static inline void php_ion_serialize_zval(php_ion_serializer *, zval *);
1307
1308 static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array *arr, bool unmangle_props, bool annotate_props)
1309 {
1310 ION_CHECK(ion_writer_start_container(ser->writer, tid_STRUCT));
1311
1312 zval *v;
1313 zend_ulong h;
1314 zend_string *k = NULL;
1315 if (arr) ZEND_HASH_FOREACH_KEY_VAL_IND(arr, h, k, v)
1316 char buf[MAX_LENGTH_OF_LONG + 1];
1317 ION_STRING is;
1318 if (k) {
1319 size_t prop_len;
1320 const char *class_name, *prop_name;
1321 if (unmangle_props && (SUCCESS == zend_unmangle_property_name_ex(k, &class_name, &prop_name, &prop_len)) && class_name) {
1322 if (annotate_props) {
1323 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_PROPERTY));
1324 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, (char *) class_name, prop_name - class_name - 1)));
1325 }
1326 } else {
1327 prop_name = k->val;
1328 prop_len = k->len;
1329 }
1330 ion_string_assign_cstr(&is, (char *) prop_name, prop_len);
1331 } else {
1332 char *end = buf + sizeof(buf) - 1;
1333 char *ptr = zend_print_long_to_buf(end, (zend_long) h);
1334 ion_string_assign_cstr(&is, ptr, end - ptr);
1335 }
1336
1337 // WATCH OUT: field names need to be copied
1338 ION_STRING fn;
1339 ION_CHECK(ion_string_copy_to_owner(ser->writer, &fn, &is));
1340 ION_CHECK(ion_writer_write_field_name(ser->writer, &fn));
1341
1342 php_ion_serialize_zval(ser, v);
1343 ION_CATCH();
1344 ZEND_HASH_FOREACH_END();
1345
1346 ION_CHECK(ion_writer_finish_container(ser->writer));
1347 }
1348
1349 static inline void php_ion_serialize_list(php_ion_serializer *ser, zend_array *arr)
1350 {
1351 ION_CHECK(ion_writer_start_container(ser->writer, tid_LIST));
1352
1353 zval *v;
1354 ZEND_HASH_FOREACH_VAL_IND(arr, v)
1355 php_ion_serialize_zval(ser, v);
1356 ION_CATCH();
1357 ZEND_HASH_FOREACH_END();
1358
1359 ION_CHECK(ion_writer_finish_container(ser->writer));
1360 }
1361
1362 static inline void php_ion_serialize_array(php_ion_serializer *ser, zend_array *arr)
1363 {
1364 if (zend_array_is_list(arr)) {
1365 php_ion_serialize_list(ser, arr);
1366 } else {
1367 php_ion_serialize_struct(ser, arr, false, false);
1368 }
1369 }
1370
1371 static inline void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_object *zobject)
1372 {
1373 uint8_t *buf;
1374 size_t len;
1375 zval tmp;
1376
1377 ZVAL_OBJ(&tmp, zobject);
1378 if (SUCCESS == zobject->ce->serialize(&tmp, &buf, &len, NULL)) {
1379 ION_STRING is;
1380 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_SERIALIZEABLE));
1381 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1382 ION_CHECK(ion_writer_write_string(ser->writer, ion_string_assign_cstr(&is, (char *) buf, len)));
1383 efree(buf);
1384 } else if (!EG(exception)){
1385 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1386 "Failed to serialize class %s", zobject->ce->name->val);
1387 }
1388 }
1389
1390 static inline void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_object *zobject, zend_function *fn)
1391 {
1392 zval rv;
1393
1394 ZVAL_NULL(&rv);
1395 zend_call_known_instance_method_with_0_params(fn ? fn : zobject->ce->__serialize, zobject, &rv);
1396 ION_CATCH();
1397
1398 if (IS_ARRAY == Z_TYPE(rv)) {
1399 ION_STRING is;
1400 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, fn ? &PHP_ION_SYMBOL_CUSTOM_OBJECT : &PHP_ION_SYMBOL_MAGIC_OBJECT));
1401 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1402 php_ion_serialize_zval(ser, &rv);
1403 zval_ptr_dtor(&rv);
1404 } else {
1405 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1406 "%s serializer %s::%s did not return an array",
1407 fn ? "Custom" : "Magic", zobject->ce->name->val,
1408 fn ? fn->common.function_name->val : "__serialize");
1409 }
1410 }
1411
1412 static inline void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_object *zobject)
1413 {
1414 ION_STRING is;
1415 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_ENUM));
1416
1417 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1418 zval *z_cname = zend_enum_fetch_case_name(zobject);
1419 ION_CHECK(ion_writer_write_symbol(ser->writer, ion_string_from_zend(&is, Z_STR_P(z_cname))));
1420 }
1421
1422 static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_object *zobject)
1423 {
1424 ION_STRING is;
1425
1426 if (zobject->ce != zend_standard_class_def) {
1427 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_CLASS_OBJECT));
1428 ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name)));
1429 } else {
1430 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_OBJECT));
1431 }
1432
1433 zval zobj;
1434 ZVAL_OBJ(&zobj, zobject);
1435 HashTable *props = zend_get_properties_for(&zobj, ZEND_PROP_PURPOSE_SERIALIZE);
1436 if (props) {
1437 php_ion_serialize_struct(ser, props, true, true);
1438 zend_release_properties(props);
1439 } else {
1440 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1441 "Could not get properties for serialization of class %s",
1442 zobject->ce->name->val);
1443 }
1444 }
1445
1446 static inline void php_ion_serialize_object_lob(php_ion_serializer *ser, zend_object *zobject)
1447 {
1448 zval tmp_type, *type = zend_read_property(NULL, zobject, ZEND_STRL("type"), 0, &tmp_type);
1449 zval tmp_value, *value = zend_read_property(NULL, zobject, ZEND_STRL("value"), 0, &tmp_value);
1450 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type)))) {
1451 case tid_BLOB_INT:
1452 ION_CHECK(ion_writer_write_blob(ser->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
1453 break;
1454 case tid_CLOB_INT:
1455 ION_CHECK(ion_writer_write_clob(ser->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
1456 break;
1457 default:
1458 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1459 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type))));
1460 break;
1461 }
1462 }
1463
1464 static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce)
1465 {
1466 return ce->__serialize && ser->call_magic;
1467 }
1468
1469 static inline bool can_call_iface_serialize(php_ion_serializer *, zend_class_entry *ce)
1470 {
1471 return !!ce->serialize;
1472 }
1473
1474 static inline bool can_call_custom_serialize(php_ion_serializer *ser, zend_object *zobject, zend_function **fn)
1475 {
1476 if (ser->call_custom) {
1477 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
1478 }
1479 return false;
1480 }
1481
1482 static inline void php_ion_serialize_object(php_ion_serializer *ser, zend_object *zobject)
1483 {
1484 zend_function *fn;
1485 zend_class_entry *ce = zobject->ce;
1486 ZEND_ASSERT(ce);
1487
1488 if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1489 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1490 "Serializing %s is not allowed", ce->name->val);
1491 return;
1492 }
1493
1494 if (can_call_magic_serialize(ser, ce)) {
1495 php_ion_serialize_object_magic(ser, zobject, NULL);
1496 } else if (can_call_iface_serialize(ser, ce)) {
1497 php_ion_serialize_object_iface(ser, zobject);
1498 } else if (can_call_custom_serialize(ser, zobject, &fn)) {
1499 php_ion_serialize_object_magic(ser, zobject, fn);
1500 } else if (zobject->ce->ce_flags & ZEND_ACC_ENUM) {
1501 php_ion_serialize_object_enum(ser, zobject);
1502 } else if (ce == ce_Symbol) {
1503 ION_CHECK(ion_writer_write_ion_symbol(ser->writer, &php_ion_obj(symbol, zobject)->sym));
1504 } else if (ce == ce_Decimal) {
1505 ION_CHECK(ion_writer_write_ion_decimal(ser->writer, &php_ion_obj(decimal, zobject)->dec));
1506 } else if (ce == ce_Timestamp) {
1507 ION_TIMESTAMP its;
1508 php_ion_timestamp *pts = php_ion_obj(timestamp, zobject);
1509 decContext *ctx = ser->options ? ser->options->decimal_context : NULL;
1510 ION_CHECK(ion_writer_write_timestamp(ser->writer, ion_timestamp_from_php(&its, pts, ctx)));
1511 } else if (ce == ce_LOB) {
1512 php_ion_serialize_object_lob(ser, zobject);
1513 } else {
1514 php_ion_serialize_object_std(ser, zobject);
1515 }
1516 }
1517
1518 static inline bool php_ion_serialize_system_value(php_ion_serializer *ser, zval *zv)
1519 {
1520 if (1 == php_ion_globals.serializer.level) {
1521 if (Z_TYPE_P(zv) == IS_OBJECT) {
1522 if (Z_OBJCE_P(zv) == ce_Symbol_Table_Shared) {
1523 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(zv));
1524 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj->tab, ser->writer));
1525 return true;
1526 }
1527 }
1528 }
1529 return false;
1530 }
1531
1532 static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv)
1533 {
1534 if (php_ion_serialize_system_value(ser, zv)) {
1535 return;
1536 }
1537
1538 zend_ulong idx = (zend_ulong) (uintptr_t) Z_COUNTED_P(zv);
1539
1540 ION_STRING is;
1541 if (zend_hash_index_exists(ser->ids, idx)) {
1542 zval *num = zend_hash_index_find(ser->ids, idx);
1543
1544 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_BACKREF));
1545 ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(num)));
1546 } else {
1547 zval num;
1548
1549 ZVAL_LONG(&num, zend_hash_num_elements(ser->ids));
1550 zend_hash_index_add(ser->ids, idx, &num);
1551
1552 Z_TRY_ADDREF_P(zv);
1553 zend_hash_next_index_insert(ser->tmp, zv);
1554
1555 switch (Z_TYPE_P(zv)) {
1556 case IS_STRING:
1557 ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_zend(&is, Z_STR_P(zv))));
1558 break;
1559
1560 case IS_ARRAY:
1561 php_ion_serialize_array(ser, Z_ARRVAL_P(zv));
1562 break;
1563
1564 case IS_OBJECT:
1565 php_ion_serialize_object(ser, Z_OBJ_P(zv));
1566 break;
1567
1568 case IS_REFERENCE:
1569 ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_REFERENCE));
1570 php_ion_serialize_zval(ser, Z_REFVAL_P(zv));
1571 break;
1572 }
1573 }
1574 }
1575
1576 static inline void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv)
1577 {
1578 OBJ_CHECK(ser);
1579
1580 switch (Z_TYPE_P(zv)) {
1581 case IS_NULL:
1582 ION_CHECK(ion_writer_write_null(ser->writer));
1583 break;
1584 case IS_TRUE:
1585 ION_CHECK(ion_writer_write_bool(ser->writer, TRUE));
1586 break;
1587 case IS_FALSE:
1588 ION_CHECK(ion_writer_write_bool(ser->writer, FALSE));
1589 break;
1590 case IS_LONG:
1591 ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(zv)));
1592 break;
1593 case IS_DOUBLE:
1594 ION_CHECK(ion_writer_write_double(ser->writer, Z_DVAL_P(zv)));
1595 break;
1596 case IS_STRING:
1597 case IS_ARRAY:
1598 case IS_OBJECT:
1599 case IS_REFERENCE:
1600 php_ion_serialize_refcounted(ser, zv);
1601 break;
1602 default:
1603 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1604 "Failed to serialize value of type %s", zend_zval_type_name(zv));
1605 }
1606 }
1607
1608 php_ion_decl(serializer_php, Serializer_PHP, php_ion_serializer_php_dtor(obj));
1609
1610 static inline void php_ion_serialize_ex(php_ion_serializer *ser, zval *zv)
1611 {
1612 HashPosition pos;
1613 HashTable *arr = NULL;
1614
1615 if (ser->multi_seq) {
1616 if (Z_TYPE_P(zv) != IS_ARRAY || !zend_array_is_list(Z_ARRVAL_P(zv))) {
1617 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
1618 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1619 return;
1620 }
1621
1622 arr = Z_ARRVAL_P(zv);
1623
1624 zend_hash_internal_pointer_reset_ex(arr, &pos);
1625 zv = zend_hash_get_current_data_ex(arr, &pos);
1626 }
1627
1628 while (zv) {
1629 php_ion_globals_serializer_step();
1630 php_ion_serialize_zval(ser, zv);
1631 php_ion_globals_serializer_exit();
1632
1633 if (!ser->multi_seq) {
1634 break;
1635 }
1636 zend_hash_move_forward_ex(arr, &pos);
1637 zv = zend_hash_get_current_data_ex(arr, &pos);
1638 }
1639 }
1640
1641 void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value)
1642 {
1643 zend_object *zo_opt = NULL, *zo_ser = NULL;
1644
1645 if (!ser) {
1646 zo_ser = create_ion_Serializer_PHP(NULL);
1647 php_ion_serializer_php *o_ser = php_ion_obj(serializer_php, zo_ser);
1648 PTR_CHECK(o_ser);
1649 o_ser->serializer.call_magic = true;
1650 php_ion_serializer_php_ctor(o_ser);
1651 ION_CATCH();
1652 ser = &o_ser->serializer;
1653 }
1654
1655 zend_object *zo_writer = create_ion_Writer_Writer(ce_Writer_Buffer_Writer);
1656 php_ion_writer *writer = php_ion_obj(writer, zo_writer);
1657 writer->type = BUFFER_WRITER;
1658
1659 if (ser->options) {
1660 zo_opt = writer->opt = create_ion_Writer_Options(NULL);
1661 php_ion_obj(writer_options, writer->opt)->opt = *ser->options;
1662 }
1663
1664 php_ion_writer_ctor(writer);
1665 ser->writer = writer->writer;
1666 ser->buffer = &writer->buffer.str;
1667
1668 if (!EG(exception)) {
1669 php_ion_serialize_ex(ser, zv);
1670 }
1671
1672 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1673 ion_writer_flush(ser->writer, NULL);
1674 RETVAL_STR_COPY(ser->buffer->s);
1675
1676 OBJ_RELEASE(zo_writer);
1677 if (zo_opt) {
1678 OBJ_RELEASE(zo_opt);
1679 }
1680 if (zo_ser) {
1681 OBJ_RELEASE(zo_ser);
1682 }
1683 }
1684
1685 typedef struct php_ion_unserializer_php {
1686 php_ion_unserializer unserializer;
1687 zend_object *opt, std;
1688 } php_ion_unserializer_php;
1689
1690 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_obj)
1691 {
1692 php_ion_unserializer *global_ser = &php_ion_globals.unserializer;
1693 ser_obj->unserializer.ids = global_ser->ids;
1694 ser_obj->unserializer.tmp = global_ser->tmp;
1695 ser_obj->unserializer.addref = global_ser->addref;
1696
1697 zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
1698 ser_obj->unserializer.call_magic);
1699 if (ser_obj->unserializer.call_custom) {
1700 zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"),
1701 ser_obj->unserializer.call_custom);
1702 ser_obj->unserializer.call_custom = zend_string_tolower(ser_obj->unserializer.call_custom);
1703 } else {
1704 zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
1705 }
1706 if (!ser_obj->opt) {
1707 ser_obj->opt = create_ion_Reader_Options(NULL);
1708 } else {
1709 GC_ADDREF(ser_obj->opt);
1710 }
1711 ser_obj->unserializer.options = &php_ion_obj(reader_options, ser_obj->opt)->opt;
1712 update_property_obj(&ser_obj->std, ZEND_STRL("readerOptions"), ser_obj->opt);
1713 GC_DELREF(ser_obj->opt);
1714 }
1715
1716 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php *obj)
1717 {
1718 if (obj->unserializer.call_custom) {
1719 zend_string_release(obj->unserializer.call_custom);
1720 }
1721 }
1722
1723 static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ);
1724
1725 static inline bool can_call_magic_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
1726 {
1727 return (ce && ce->__unserialize && ser->call_magic);
1728 }
1729
1730 static inline bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
1731 {
1732 return (ce && ce->unserialize);
1733 }
1734
1735 static inline bool can_call_custom_unserialize(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
1736 {
1737 if (ser->call_custom) {
1738 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom));
1739 }
1740 return false;
1741 }
1742
1743 static inline zval *php_ion_unserialize_class(php_ion_unserializer *ser, zval *return_value)
1744 {
1745 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1746
1747 if (ce) {
1748 object_init_ex(return_value, ce);
1749 return zend_hash_next_index_insert(ser->ids, return_value);
1750 }
1751
1752 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_IMPORT_NOT_FOUND,
1753 "Could not find class %s", ser->annotations.object_class->val);
1754 return NULL;
1755 }
1756
1757 static inline void php_ion_unserialize_object_enum(php_ion_unserializer *ser, zval *return_value)
1758 {
1759 zend_string *zs_case = zval_get_string(return_value);
1760 ION_CATCH();
1761
1762 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1763 if (!ce || !(ce->ce_flags & ZEND_ACC_ENUM)) {
1764 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1765 "Not a valid enum: %s", ser->annotations.object_class->val);
1766 return;
1767 }
1768 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce), zs_case)) {
1769 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1770 "Not a valid enum case: %s::%s", ser->annotations.object_class->val, zs_case->val);
1771 return;
1772 }
1773 RETVAL_OBJ_COPY(zend_enum_get_case(ce, zs_case));
1774 zend_hash_next_index_insert(ser->ids, return_value);
1775 zend_string_release(zs_case);
1776 }
1777
1778 static inline void php_ion_unserialize_object_iface(php_ion_unserializer *ser, zval *return_value)
1779 {
1780 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
1781 if (can_call_iface_unserialize(ser, ce)) {
1782 zend_string *s = zval_get_string(return_value);
1783 ZVAL_NULL(return_value);
1784 zval *backref = zend_hash_next_index_insert(ser->ids, return_value);
1785 if (SUCCESS == ce->unserialize(backref, ce, (BYTE *) s->val, s->len, NULL)) {
1786 RETVAL_ZVAL(backref, 0, 0);
1787 } else if (!EG(exception)) {
1788 zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
1789 "Failed to unserialize class %s", ce->name->val);
1790 }
1791 zend_string_release(s);
1792 } else {
1793 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1794 "Class %s does not implement Serializable", ser->annotations.object_class->val);
1795 }
1796 }
1797
1798 static inline void php_ion_unserialize_field_name(php_ion_unserializer *ser, zend_string **key)
1799 {
1800 // FIXME: symbol table
1801 ION_STRING name;
1802 ION_CHECK(ion_reader_get_field_name(ser->reader, &name));
1803 if (!name.length) {
1804 ION_SYMBOL *is_ptr;
1805 ION_CHECK(ion_reader_get_field_name_symbol(ser->reader, &is_ptr));
1806 if (!ION_SYMBOL_IS_NULL(is_ptr) && is_ptr->value.length) {
1807 name = is_ptr->value;
1808 } else if (is_ptr) {
1809 char buf[MAX_LENGTH_OF_LONG + 1 + 1] = {0}, *end = buf + sizeof(buf) - 1, *ptr;
1810 ptr = zend_print_long_to_buf(end, is_ptr->sid);
1811 *--ptr = '$';
1812 *key = zend_string_init(ptr, end - ptr, 0);
1813 return;
1814 }
1815 }
1816 *key = zend_string_from_ion(&name);
1817 }
1818
1819 static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_value)
1820 {
1821 zend_hash_next_index_insert(ser->ids, return_value);
1822
1823 ION_CHECK(ion_reader_step_in(ser->reader));
1824
1825 while (true) {
1826 ION_TYPE typ;
1827 ION_CHECK(ion_reader_next(ser->reader, &typ));
1828
1829 if (typ == tid_EOF) {
1830 break;
1831 }
1832
1833 zend_string *key;
1834 php_ion_unserialize_field_name(ser, &key);
1835 ION_CATCH();
1836
1837 zval zvalue;
1838 php_ion_unserialize_zval(ser, &zvalue, &typ);
1839 ION_CATCH(zend_string_release(key));
1840
1841 zend_class_entry *ce = Z_OBJCE_P(return_value);
1842 if (ser->annotations.object_prop && ser->annotations.property_class->val[0] != '*') {
1843 ce = zend_lookup_class(ser->annotations.property_class);
1844 }
1845 zend_update_property_ex(ce, Z_OBJ_P(return_value), key, &zvalue);
1846 zval_ptr_dtor(&zvalue);
1847 zend_string_release(key);
1848 }
1849
1850 ION_CHECK(ion_reader_step_out(ser->reader));
1851 }
1852
1853 /**
1854 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
1855 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
1856 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
1857 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
1858 * nondeterministic behavior.
1859 */
1860 static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *return_value)
1861 {
1862 zend_hash_next_index_insert(ser->ids, return_value);
1863
1864 ION_CHECK(ion_reader_step_in(ser->reader));
1865
1866 while (true) {
1867 ION_TYPE typ;
1868 ION_CHECK(ion_reader_next(ser->reader, &typ));
1869
1870 if (typ == tid_EOF) {
1871 break;
1872 }
1873
1874 zend_string *key;
1875 php_ion_unserialize_field_name(ser, &key);
1876 ION_CATCH();
1877
1878 zval zvalue;
1879 php_ion_unserialize_zval(ser, &zvalue, &typ);
1880 ION_CATCH(zend_string_release(key));
1881
1882 // FIXME:: too naive; b0rked if the previous value is an array
1883 if (zend_symtable_exists(HASH_OF(return_value), key)) {
1884 zval tmp, *prev = zend_hash_find(HASH_OF(return_value), key);
1885 if (Z_TYPE_P(prev) != IS_ARRAY) {
1886 array_init(&tmp);
1887 Z_TRY_ADDREF_P(prev);
1888 zend_hash_next_index_insert(Z_ARRVAL(tmp), prev);
1889 prev = zend_hash_update(HASH_OF(return_value), key, &tmp);
1890 }
1891 zend_hash_next_index_insert(Z_ARRVAL_P(prev), &zvalue);
1892 } else {
1893 zend_symtable_update(HASH_OF(return_value), key, &zvalue);
1894 }
1895 zend_string_release(key);
1896 }
1897
1898 ION_CHECK(ion_reader_step_out(ser->reader));
1899 }
1900
1901 static inline void verify_unserializer(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
1902 {
1903 switch (ser->annotations.object_type) {
1904 case 'c':
1905 *fn = NULL;
1906 break;
1907
1908 case 'C':
1909 if (!can_call_custom_unserialize(ser, zobject, fn)) {
1910 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1911 "Could not find custom serializer method of %s", ser->annotations.object_class->val);
1912 }
1913 break;
1914
1915 case 'O':
1916 if (!can_call_magic_unserialize(ser, zobject->ce)) {
1917 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1918 "Could not find method %s::__unserialize()", ser->annotations.object_class->val);
1919 }
1920 *fn = zobject->ce->__unserialize;
1921 break;
1922
1923 default:
1924 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1925 "Invalid object type %c", ser->annotations.object_type);
1926 }
1927 }
1928
1929 static inline void php_ion_unserialize_object(php_ion_unserializer *ser, zval *return_value)
1930 {
1931 // backup possible backref to array returned by magic/custom __serialize()
1932 zval *input = zend_hash_next_index_insert(ser->tmp, return_value);
1933
1934 php_ion_unserialize_class(ser, return_value);
1935 ION_CATCH();
1936
1937 zend_function *fn = NULL;
1938 zend_object *zobject = Z_OBJ_P(return_value);
1939 verify_unserializer(ser, zobject, &fn);
1940 ION_CATCH();
1941
1942 // plain object
1943 if (!fn) {
1944 php_ion_unserialize_props(ser, return_value);
1945 return;
1946 }
1947
1948 // magic object
1949 if (Z_TYPE_P(input) != IS_ARRAY) {
1950 zval_ptr_dtor(input);
1951 array_init(input);
1952 zend_hash_real_init_mixed(Z_ARRVAL_P(input));
1953 php_ion_unserialize_hash(ser, input);
1954 ION_CATCH();
1955 }
1956 zval rv;
1957 ZVAL_NULL(&rv);
1958 zend_call_method_with_1_params(zobject, zobject->ce, &fn, "", &rv, input);
1959 zval_ptr_dtor(&rv);
1960 }
1961
1962 static inline void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *return_value)
1963 {
1964 if (ser->annotations.object_class) {
1965 switch (ser->annotations.object_type) {
1966 case 'S':
1967 php_ion_unserialize_object_iface(ser, return_value);
1968 break;
1969 case 'E':
1970 php_ion_unserialize_object_enum(ser, return_value);
1971 break;
1972 default:
1973 php_ion_unserialize_object(ser, return_value);
1974 }
1975 } else if (!ser->annotations.object_type) {
1976 bool is_shared_symtab = ser->annotations.shared_symtab;
1977 array_init(return_value);
1978 php_ion_unserialize_hash(ser, return_value);
1979 if (is_shared_symtab) {
1980 zval zobj;
1981 object_init_ex(&zobj, ce_Symbol_Table_Shared);
1982 zend_call_known_function(Z_OBJCE(zobj)->constructor, Z_OBJ(zobj), Z_OBJCE(zobj), NULL, 0, NULL, Z_ARRVAL_P(return_value));
1983 zval_ptr_dtor(return_value);
1984 RETURN_COPY_VALUE(&zobj);
1985 }
1986 } else if (ser->annotations.object_type == 'o') {
1987 object_init(return_value);
1988 php_ion_unserialize_hash(ser, return_value);
1989 } else {
1990 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_TOKEN,
1991 "Invalid object annotation %c::", ser->annotations.object_type);
1992 }
1993 }
1994
1995 static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *return_value)
1996 {
1997 ION_CHECK(ion_reader_step_in(ser->reader));
1998 array_init(return_value);
1999 zend_hash_next_index_insert(ser->ids, return_value);
2000
2001 while (true) {
2002 ION_TYPE typ;
2003 ION_CHECK(ion_reader_next(ser->reader, &typ));
2004
2005 if (typ == tid_EOF) {
2006 break;
2007 }
2008
2009 zval next;
2010 php_ion_unserialize_zval(ser, &next, &typ);
2011 ION_CATCH();
2012
2013 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &next);
2014 }
2015
2016 ION_CHECK(ion_reader_step_out(ser->reader));
2017 }
2018
2019 static inline void php_ion_reader_read_lob(ION_READER *reader, zval *return_value)
2020 {
2021 zend_string *zstr = zend_string_alloc(0x1000, 0);
2022 again:
2023 SIZE read = 0;
2024 iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, zstr->len, &read);
2025 if (err == IERR_BUFFER_TOO_SMALL) {
2026 zstr = zend_string_extend(zstr, zstr->len << 2, 0);
2027 goto again;
2028 }
2029 ION_CHECK(err, zend_string_release(zstr));
2030 if (zstr->len > read) {
2031 zstr->val[read] = 0;
2032 zstr = zend_string_truncate(zstr, read, 0);
2033 }
2034 RETURN_STR(zstr);
2035 }
2036
2037 static inline void php_ion_reader_read_timestamp(ION_READER *reader, ION_READER_OPTIONS *opt, zval *return_value)
2038 {
2039 ION_TIMESTAMP ts;
2040 ION_CHECK(ion_reader_read_timestamp(reader, &ts));
2041
2042 object_init_ex(return_value, ce_Timestamp);
2043 php_ion_timestamp *ts_obj = php_ion_obj(timestamp, Z_OBJ_P(return_value));
2044
2045 zend_string *fmt = NULL;
2046 decContext *ctx = opt ? opt->decimal_context : NULL;
2047 ts_obj->time = php_time_from_ion(&ts, ctx, &fmt);
2048 php_ion_timestamp_ctor(ts_obj, ts.precision, fmt, NULL, NULL);
2049 zend_string_release(fmt);
2050
2051 OBJ_CHECK(ts_obj);
2052 }
2053
2054 static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_value)
2055 {
2056 ION_INT *num = NULL;
2057 ION_CHECK(ion_int_alloc(reader, &num));
2058 ION_CHECK(ion_reader_read_ion_int(reader, num));
2059
2060 // TODO: SIZEOF_ZEND_LONG == 4
2061 int64_t i64;
2062 iERR err = ion_int_to_int64(num, &i64);
2063 switch (err) {
2064 case IERR_OK:
2065 RETVAL_LONG(i64);
2066 goto done;
2067
2068 case IERR_NUMERIC_OVERFLOW:
2069 SIZE max, len;
2070 ION_CHECK(ion_int_char_length(num, &max));
2071 zend_string *zs = zend_string_alloc(max, 0);
2072
2073 err = ion_int_to_char(num, (BYTE *) zs->val, max, &len);
2074 zs->val[zs->len = len] = 0;
2075 RETVAL_STR(zs);
2076 /* fall through */
2077
2078 default:
2079 done:
2080 ion_int_free(num);
2081 ION_CHECK(err);
2082 }
2083 }
2084
2085 static inline void php_ion_unserialize_backref(php_ion_unserializer *ser, zval *return_value)
2086 {
2087 zval *backref = zend_hash_index_find(ser->ids, Z_LVAL_P(return_value));
2088
2089 if (backref) {
2090 ZVAL_COPY_VALUE(return_value, backref);
2091 zend_hash_next_index_insert(ser->addref, return_value);
2092 } else {
2093 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INTERNAL_ERROR,
2094 "Could not find back reference %ld", Z_LVAL_P(return_value));
2095 }
2096 }
2097
2098 static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser)
2099 {
2100 memset(&ser->annotations, 0, sizeof(ser->annotations));
2101
2102 int32_t ann_cnt;
2103 ION_CHECK(ion_reader_get_annotation_count(ser->reader, &ann_cnt));
2104 for (int32_t i = 0; i < ann_cnt; ++i) {
2105 ION_STRING ann_str;
2106 ION_CHECK(ion_reader_get_an_annotation(ser->reader, i, &ann_str));
2107
2108 if (ann_str.length != 1) {
2109 if (ann_str.length == ION_SYS_STRLEN_SHARED_SYMBOL_TABLE) {
2110 if (!memcmp(ann_str.value, ION_SYMBOL_SHARED_SYMBOL_TABLE_BYTES, ION_SYS_STRLEN_SHARED_SYMBOL_TABLE)) {
2111 ser->annotations.shared_symtab = true;
2112 }
2113 }
2114 continue;
2115 }
2116
2117 switch (*ann_str.value) {
2118 case 'R':
2119 if (ser->annotations.makeref) {
2120 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
2121 "Invalid multiple reference annotations");
2122 return;
2123 }
2124 ser->annotations.makeref = true;
2125 break;
2126
2127 case 'r':
2128 if (ser->annotations.backref) {
2129 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
2130 "Invalid multiple back reference annotations");
2131 return;
2132 }
2133 ser->annotations.backref = true;
2134 break;
2135
2136 case 'p':
2137 if (ser->annotations.object_prop) {
2138 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
2139 "Invalid multiple object property annotations");
2140 return;
2141 }
2142 ser->annotations.object_prop = true;
2143
2144 ION_STRING prop_class;
2145 ION_CHECK(ion_reader_get_an_annotation(ser->reader, ++i, &prop_class));
2146 ser->annotations.property_class = zend_string_from_ion(&prop_class);
2147
2148 zval zptmp;
2149 ZVAL_STR(&zptmp, ser->annotations.property_class);
2150 zend_hash_next_index_insert(ser->tmp, &zptmp);
2151 break;
2152
2153 case 'E':
2154 case 'S':
2155 case 'O':
2156 case 'C':
2157 case 'o':
2158 case 'c':
2159 if (ser->annotations.object_type) {
2160 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
2161 "Invalid multiple object type annotations: %c::%c",
2162 ser->annotations.object_type, *ann_str.value);
2163 return;
2164 }
2165 if ('o' != (ser->annotations.object_type = *ann_str.value)) {
2166 ION_STRING class_name;
2167 ION_CHECK(ion_reader_get_an_annotation(ser->reader, ++i, &class_name));
2168 ser->annotations.object_class = zend_string_from_ion(&class_name);
2169
2170 zval zctmp;
2171 ZVAL_STR(&zctmp, ser->annotations.object_class);
2172 zend_hash_next_index_insert(ser->tmp, &zctmp);
2173 }
2174 break;
2175 }
2176
2177 // sanity checks
2178 if (ser->annotations.object_type && ser->annotations.object_type != 'o' && !ser->annotations.object_class) {
2179 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
2180 "Invalid object annotation without class name: %c::", ser->annotations.object_type);
2181 return;
2182 }
2183 if (ser->annotations.object_type == 'o' && ser->annotations.object_class) {
2184 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
2185 "Invalid object annotation with class name: o::%s", ser->annotations.object_class->val);
2186 return;
2187 }
2188 }
2189 }
2190
2191 static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ)
2192 {
2193 if (typ) {
2194 memcpy(&ser->type, typ, sizeof(ser->type));
2195 } else {
2196 typ = &ser->type;
2197 ION_CHECK(ion_reader_next(ser->reader, typ));
2198 }
2199
2200 php_ion_unserialize_annotations(ser);
2201 ION_CATCH();
2202
2203 if (ser->annotations.makeref) {
2204 ZVAL_MAKE_REF(return_value);
2205 zend_hash_next_index_insert(ser->ids, return_value);
2206 ZVAL_DEREF(return_value);
2207 }
2208
2209 if (ION_TYPE_INT(*typ) > 0) {
2210 BOOL is_null;
2211 ION_CHECK(ion_reader_is_null(ser->reader, &is_null));
2212 if (is_null) {
2213 RETURN_NULL();
2214 }
2215 }
2216
2217 switch (ION_TYPE_INT(*typ)) {
2218 case tid_NULL_INT:
2219 ION_CHECK(ion_reader_read_null(ser->reader, typ));
2220 RETURN_NULL();
2221
2222 case tid_BOOL_INT:
2223 BOOL bval;
2224 ION_CHECK(ion_reader_read_bool(ser->reader, &bval));
2225 RETURN_BOOL(bval);
2226
2227 case tid_INT_INT:
2228 php_ion_reader_read_int(ser->reader, return_value);
2229 if (ser->annotations.backref) {
2230 ION_CATCH();
2231 php_ion_unserialize_backref(ser, return_value);
2232 }
2233 if (ser->annotations.object_type) {
2234 if (!ser->annotations.backref) {
2235 zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INVALID_SYNTAX,
2236 "Invalid object type annotation: %c::" ZEND_LONG_FMT,
2237 ser->annotations.object_type, Z_LVAL_P(return_value));
2238 return;
2239 }
2240 goto unserialize_struct;
2241 }
2242 return;
2243
2244 case tid_FLOAT_INT:
2245 double d;
2246 ION_CHECK(ion_reader_read_double(ser->reader, &d));
2247 RETURN_DOUBLE(d);
2248
2249 case tid_DECIMAL_INT:
2250 object_init_ex(return_value, ce_Decimal);
2251 php_ion_decimal *dec = php_ion_obj(decimal, Z_OBJ_P(return_value));
2252 ION_CHECK(ion_reader_read_ion_decimal(ser->reader, &dec->dec));
2253 php_ion_decimal_ctor(dec);
2254 zend_hash_next_index_insert(ser->ids, return_value);
2255 return;
2256
2257 case tid_TIMESTAMP_INT:
2258 php_ion_reader_read_timestamp(ser->reader, ser->options, return_value);
2259 zend_hash_next_index_insert(ser->ids, return_value);
2260 return;
2261
2262 case tid_SYMBOL_INT:
2263 ION_SYMBOL sym;
2264 ION_CHECK(ion_reader_read_ion_symbol(ser->reader, &sym));
2265 php_ion_symbol_zval(&sym, return_value);
2266 if (ser->annotations.object_type) {
2267 zend_hash_next_index_insert(ser->tmp, return_value);
2268 goto unserialize_struct;
2269 }
2270 zend_hash_next_index_insert(ser->ids, return_value);
2271 return;
2272
2273 case tid_STRING_INT:
2274 ION_STRING str;
2275 ION_CHECK(ion_reader_read_string(ser->reader, &str));
2276 RETVAL_STRINGL((char *) str.value, str.length);
2277 if (ser->annotations.object_type) {
2278 zend_hash_next_index_insert(ser->tmp, return_value);
2279 goto unserialize_struct;
2280 }
2281 zend_hash_next_index_insert(ser->ids, return_value);
2282 return;
2283
2284 case tid_CLOB_INT:
2285 case tid_BLOB_INT:
2286 php_ion_reader_read_lob(ser->reader, return_value);
2287 if (ser->annotations.object_type) {
2288 zend_hash_next_index_insert(ser->tmp, return_value);
2289 goto unserialize_struct;
2290 }
2291 zend_hash_next_index_insert(ser->ids, return_value);
2292 return;
2293
2294 case tid_LIST_INT:
2295 case tid_SEXP_INT: // FIXME
2296 php_ion_unserialize_list(ser, return_value);
2297 if (!ser->annotations.object_type) {
2298 return;
2299 }
2300 /* fall through */
2301
2302 case tid_STRUCT_INT:
2303 unserialize_struct: ;
2304 php_ion_unserialize_struct(ser, return_value);
2305 return;
2306
2307 case tid_none_INT:
2308 ZEND_ASSERT(0);
2309 break;
2310
2311 case tid_DATAGRAM_INT:
2312 ZEND_ASSERT(!"datagram");
2313 case tid_EOF_INT:
2314 return;
2315 }
2316 }
2317
2318 php_ion_decl(unserializer_php, Unserializer_PHP, php_ion_unserializer_php_dtor(obj));
2319
2320 static inline void php_ion_unserialize_ex(php_ion_unserializer *ser, zval *return_value)
2321 {
2322 if (ser->multi_seq) {
2323 array_init(return_value);
2324 }
2325
2326 do {
2327 zval tmp;
2328 ZVAL_NULL(&tmp);
2329 php_ion_globals_unserializer_step();
2330 php_ion_unserialize_zval(ser, &tmp, NULL);
2331 php_ion_globals_unserializer_exit();
2332 ION_CATCH(zval_ptr_dtor(&tmp));
2333
2334 if (!ser->multi_seq) {
2335 RETURN_COPY_VALUE(&tmp);
2336 } else if (ser->type != tid_EOF) {
2337 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
2338 }
2339 } while (ser->type != tid_EOF);
2340 }
2341
2342 void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_value)
2343 {
2344 zend_object *zo_opt = NULL, *zo_ser = NULL;
2345
2346 if (!ser) {
2347 zo_ser = create_ion_Unserializer_PHP(NULL);
2348 php_ion_unserializer_php *o_ser = php_ion_obj(unserializer_php, zo_ser);
2349 PTR_CHECK(o_ser);
2350 o_ser->unserializer.call_magic = true;
2351 php_ion_unserializer_php_ctor(o_ser);
2352 ION_CATCH();
2353 ser = &o_ser->unserializer;
2354 }
2355
2356 zend_object *zo_reader;
2357 php_ion_reader *reader;
2358 ZVAL_DEREF(zdata);
2359
2360 if (Z_TYPE_P(zdata) == IS_RESOURCE) {
2361 zo_reader = create_ion_Reader_Reader(ce_Reader_Stream_Reader);
2362 reader = php_ion_obj(reader, zo_reader);
2363 reader->type = STREAM_READER;
2364 php_stream_from_zval_no_verify(reader->stream.ptr, zdata);
2365 } else if (Z_TYPE_P(zdata) <= IS_STRING) {
2366 zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader);
2367 reader = php_ion_obj(reader, zo_reader);
2368 reader->type = BUFFER_READER;
2369 reader->buffer = zval_get_string(zdata);
2370 } else {
2371 zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
2372 "Invalid source to unserialize; expected string or resource");
2373 if (zo_ser) {
2374 OBJ_RELEASE(zo_ser);
2375 }
2376 return;
2377 }
2378
2379 if (ser->options) {
2380 zo_opt = reader->opt = create_ion_Reader_Options(NULL);
2381 php_ion_obj(reader_options, reader->opt)->opt = *ser->options;
2382 }
2383
2384 php_ion_reader_ctor(reader);
2385 ser->reader = reader->reader;
2386
2387 if (!EG(exception)) {
2388 php_ion_unserialize_ex(ser, return_value);
2389 }
2390
2391 OBJ_RELEASE(zo_reader);
2392 if (zo_opt) {
2393 OBJ_RELEASE(zo_opt);
2394 }
2395 if (zo_ser) {
2396 OBJ_RELEASE(zo_ser);
2397 }
2398 }