fix #6: compatibility with 8.2
[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/standard/php_var.h"
15 #include "ext/date/php_date.h"
16
17 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
18 #include "ionc/ion.h"
19
20 #define PHP_ION_SYMBOL_TABLE_VERSION 1
21 #define PHP_ION_SYMBOL(c, s) { \
22 { \
23 0, \
24 { sizeof(s)-1, (BYTE *) s }, \
25 { { 0, NULL }, 0 }, \
26 0 \
27 }, \
28 { sizeof(c)-1, (BYTE *) c } \
29 },
30
31 typedef struct php_ion_global_symbol {
32 ION_SYMBOL s;
33 ION_STRING e;
34 } php_ion_global_symbol;
35
36 static php_ion_global_symbol g_sym_tab_php_sym[] = {
37 #define PHP_ION_SYMBOL_PHP (g_sym_tab_php_sym[0]).s
38 PHP_ION_SYMBOL("PHP", "PHP")
39 #define PHP_ION_SYMBOL_REFERENCE (g_sym_tab_php_sym[1]).s
40 PHP_ION_SYMBOL("Reference", "R")
41 #define PHP_ION_SYMBOL_BACKREF (g_sym_tab_php_sym[2]).s
42 PHP_ION_SYMBOL("Backref", "r")
43 #define PHP_ION_SYMBOL_PROPERTY (g_sym_tab_php_sym[3]).s
44 PHP_ION_SYMBOL("Property", "p")
45 #define PHP_ION_SYMBOL_CLASS_OBJECT (g_sym_tab_php_sym[4]).s
46 PHP_ION_SYMBOL("ClassObject", "c")
47 #define PHP_ION_SYMBOL_CUSTOM_OBJECT (g_sym_tab_php_sym[5]).s
48 PHP_ION_SYMBOL("CustomObject", "C")
49 #define PHP_ION_SYMBOL_OBJECT (g_sym_tab_php_sym[6]).s
50 PHP_ION_SYMBOL("Object", "o")
51 #define PHP_ION_SYMBOL_MAGIC_OBJECT (g_sym_tab_php_sym[7]).s
52 PHP_ION_SYMBOL("MagicObject", "O")
53 #define PHP_ION_SYMBOL_SERIALIZEABLE (g_sym_tab_php_sym[8]).s
54 PHP_ION_SYMBOL("Serializable", "S")
55 #define PHP_ION_SYMBOL_ENUM (g_sym_tab_php_sym[9]).s
56 PHP_ION_SYMBOL("Enum", "E")
57 {{0}, {0}}
58 };
59 #undef PHP_ION_SYMBOL
60
61 static ION_SYMBOL_TABLE *g_sym_tab_php;
62
63 /* [SID => STRING, STRING => SID] */
64 static HashTable g_sym_hash;
65 /* [enum_case_name => SID] */
66 static HashTable g_sym_map;
67
68 #define ION_SYS_SYMBOL_SYMBOL_TABLE \
69 ION_SYS_SYMBOL_ION_SYMBOL_TABLE
70 #define g_sym_add(enum_name, c_name) do { \
71 g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \
72 g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \
73 } while (0)
74
75 static void g_sym_hash_add(int sid, const char *str, size_t len)
76 {
77 zval zl, zs;
78 ZVAL_LONG(&zl, sid);
79 ZVAL_STR(&zs, zend_string_init_interned(str, len ,1));
80 zend_hash_add(&g_sym_hash, Z_STR(zs), &zl);
81 zend_hash_index_add(&g_sym_hash, sid, &zs);
82 }
83
84 static void g_sym_map_add(int sid, const char *str, size_t len)
85 {
86 zval zv;
87 ZVAL_LONG(&zv, sid);
88 zend_hash_str_add(&g_sym_map, str, len, &zv);
89 }
90
91 static void g_sym_dtor(void)
92 {
93 ion_symbol_table_close(g_sym_tab_php);
94 zend_hash_destroy(&g_sym_map);
95 zend_hash_destroy(&g_sym_hash);
96 }
97
98 static int g_sym_init(void)
99 {
100 zend_hash_init(&g_sym_hash, 0, NULL, NULL, 1);
101 zend_hash_init(&g_sym_map, 0, NULL, NULL, 1);
102
103 g_sym_hash_add(0, ZEND_STRL(""));
104 g_sym_map_add(0, ZEND_STRL(""));
105 g_sym_add("Ion", ION);
106 g_sym_add("Ivm_1_0", IVM);
107 g_sym_add("IonSymbolTable", SYMBOL_TABLE);
108 g_sym_add("Name", NAME);
109 g_sym_add("Version", VERSION);
110 g_sym_add("Imports", IMPORTS);
111 g_sym_add("Symbols", SYMBOLS);
112 g_sym_add("MaxId", MAX_ID);
113 g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE);
114
115 int sys_max_id = ION_SYS_SID_SHARED_SYMBOL_TABLE;
116
117 if (IERR_OK != ion_symbol_table_open_with_type(&g_sym_tab_php, NULL, ist_SHARED)) {
118 return FAILURE;
119 }
120 php_ion_global_symbol *ptr = g_sym_tab_php_sym;
121 ion_symbol_table_set_version(g_sym_tab_php, PHP_ION_SYMBOL_TABLE_VERSION);
122 ion_symbol_table_set_name(g_sym_tab_php, &ptr->s.value);
123 while (ptr->e.value) {
124 ion_symbol_table_add_symbol(g_sym_tab_php, &ptr->s.value, &ptr->s.sid);
125 g_sym_hash_add(sys_max_id + ptr->s.sid, (const char *) ptr->s.value.value, ptr->s.value.length);
126 g_sym_map_add(sys_max_id + ptr->s.sid, (const char *) ptr->e.value, ptr->e.length);
127 ++ptr;
128 }
129 ion_symbol_table_lock(g_sym_tab_php);
130 return SUCCESS;
131 }
132
133 static struct {
134 zend_string *Year, *Month, *Day, *Min, *Sec, *Frac, *MinTZ, *SecTZ, *FracTZ;
135 } g_intern_str;
136
137 static void g_intern_str_init()
138 {
139 #define NEW_INTERN_STR(s) \
140 g_intern_str.s = zend_string_init_interned(#s, sizeof(#s)-1, 1)
141 NEW_INTERN_STR(Year);
142 NEW_INTERN_STR(Month);
143 NEW_INTERN_STR(Day);
144 NEW_INTERN_STR(Min);
145 NEW_INTERN_STR(Sec);
146 NEW_INTERN_STR(Frac);
147 NEW_INTERN_STR(MinTZ);
148 NEW_INTERN_STR(SecTZ);
149 NEW_INTERN_STR(FracTZ);
150 #undef NEW_INTERN_STR
151 }
152
153 typedef struct php_ion_serializer {
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
162 zend_object *wri, std;
163
164 } php_ion_serializer;
165
166 typedef int (*php_ion_serialize_zval_cb)(void *ctx, zval *zv);
167
168 typedef struct php_ion_annotations {
169 uint8_t shared_symtab:1;
170 uint8_t backref:1;
171 uint8_t makeref:1;
172 uint8_t object_prop:1;
173 uint8_t object_type;
174 zend_string *object_class;
175 zend_string *property_class;
176 } php_ion_annotations;
177
178 typedef struct php_ion_unserializer {
179 zend_string *call_custom;
180 zend_bool call_magic;
181 zend_bool multi_seq;
182
183 uint32_t level;
184 HashTable *ids;
185 HashTable *tmp;
186
187 HashTable *addref;
188
189 ION_TYPE type; // FIXME: there's already `php_ion_obj(reader, rdr)->state`
190 php_ion_annotations annotations;
191
192 zend_object *rdr, std;
193
194 } php_ion_unserializer;
195
196 ZEND_BEGIN_MODULE_GLOBALS(ion)
197
198 struct {
199 decContext ctx;
200 ION_DECIMAL zend_max;
201 ION_DECIMAL zend_min;
202 } decimal;
203
204 struct {
205 HashTable cache;
206 } symbol;
207
208 php_ion_serializer serializer;
209 php_ion_unserializer unserializer;
210
211 struct {
212 HashTable serializer[2];
213 HashTable unserializer[3];
214 } _ht;
215
216 ZEND_END_MODULE_GLOBALS(ion);
217
218 #ifdef ZTS
219 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
220 #else
221 # define php_ion_globals ion_globals
222 #endif
223
224 ZEND_DECLARE_MODULE_GLOBALS(ion);
225
226 static zend_class_entry
227 *ce_Catalog,
228 *ce_Decimal,
229 *ce_Decimal_Context,
230 *ce_Decimal_Context_Rounding,
231 *ce_Exception,
232 *ce_LOB,
233 *ce_Reader,
234 *ce_Reader_Reader,
235 *ce_Reader_Buffer,
236 *ce_Reader_Stream,
237 *ce_Reader_Buffer_Reader,
238 *ce_Reader_Stream_Reader,
239 *ce_Serializer,
240 *ce_Serializer_Serializer,
241 *ce_Symbol,
242 *ce_Symbol_ImportLocation,
243 *ce_Symbol_Enum,
244 *ce_Symbol_Table,
245 *ce_Symbol_Table_Local,
246 *ce_Symbol_Table_PHP,
247 *ce_Symbol_Table_Shared,
248 *ce_Symbol_Table_System,
249 *ce_Timestamp,
250 *ce_Timestamp_Format,
251 *ce_Timestamp_Precision,
252 *ce_Type,
253 *ce_Unserializer,
254 *ce_Unserializer_Unserializer,
255 *ce_Writer,
256 *ce_Writer_Buffer,
257 *ce_Writer_Buffer_Writer,
258 *ce_Writer_Stream,
259 *ce_Writer_Stream_Writer,
260 *ce_Writer_Writer
261 ;
262
263 static void php_ion_globals_symbols_init(void)
264 {
265 zend_hash_init(&php_ion_globals.symbol.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
266 }
267
268 static void php_ion_globals_symbols_dtor(void)
269 {
270 zend_hash_destroy(&php_ion_globals.symbol.cache);
271 }
272
273 static void php_ion_globals_serializer_init(void)
274 {
275 php_ion_serializer *s = &php_ion_globals.serializer;
276 HashTable *h = php_ion_globals._ht.serializer;
277
278 zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
279 zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
280 }
281
282 static uint32_t php_ion_globals_serializer_step(void)
283 {
284 php_ion_serializer *s = &php_ion_globals.serializer;
285 uint32_t level;
286
287 if (!(level = s->level++)) {
288 zend_hash_clean(s->ids);
289 zend_hash_clean(s->tmp);
290 }
291 return level;
292 }
293
294 static uint32_t php_ion_globals_serializer_exit(void)
295 {
296 php_ion_serializer *s = &php_ion_globals.serializer;
297
298 ZEND_ASSERT(s->level);
299 if (!--s->level) {
300 zend_hash_clean(s->ids);
301 zend_hash_clean(s->tmp);
302 }
303 return s->level;
304 }
305
306 static void php_ion_globals_serializer_dtor(void)
307 {
308 php_ion_serializer *s = &php_ion_globals.serializer;
309
310 zend_hash_destroy(s->tmp);
311 zend_hash_destroy(s->ids);
312 }
313
314 void ZVAL_ADDREF(zval *zv)
315 {
316 if (Z_ISREF_P(zv)) {
317 Z_TRY_ADDREF_P(Z_REFVAL_P(zv));
318 } else {
319 Z_TRY_ADDREF_P(zv);
320 }
321 }
322 static void php_ion_globals_unserializer_init(void)
323 {
324 php_ion_unserializer *s = &php_ion_globals.unserializer;
325 HashTable *h = php_ion_globals._ht.unserializer;
326
327 zend_hash_init(s->tmp = &h[0], 0, NULL, ZVAL_PTR_DTOR, 0);
328 zend_hash_init(s->ids = &h[1], 0, NULL, NULL, 0);
329 zend_hash_init(s->addref = &h[2], 0, NULL, ZVAL_ADDREF, 0);
330 }
331
332 static void php_ion_globals_unserializer_step(void)
333 {
334 php_ion_unserializer *s = &php_ion_globals.unserializer;
335
336 if (!s->level++) {
337 zend_hash_clean(s->addref);
338 zend_hash_clean(s->ids);
339 zend_hash_clean(s->tmp);
340 }
341 }
342
343 static void php_ion_globals_unserializer_exit(void)
344 {
345 php_ion_unserializer *s = &php_ion_globals.unserializer;
346
347 ZEND_ASSERT(s->level);
348 if (!--s->level) {
349 zend_hash_clean(s->addref);
350 zend_hash_clean(s->ids);
351 zend_hash_clean(s->tmp);
352 }
353 }
354
355 static void php_ion_globals_unserializer_dtor(void)
356 {
357 php_ion_unserializer *s = &php_ion_globals.unserializer;
358
359 zend_hash_destroy(s->addref);
360 zend_hash_destroy(s->ids);
361 zend_hash_destroy(s->tmp);
362 }
363
364 #define php_ion_obj(type, zo) \
365 ((php_ion_ ##type *) php_ion_obj_ex(zo, XtOffsetOf(php_ion_ ## type, std)))
366 static void *php_ion_obj_ex(void *obj, ptrdiff_t offset) {
367 if (obj) {
368 return ((char *) obj) - offset;
369 }
370 return NULL;
371 }
372
373 #define php_ion_decl_noclone(type, cname) \
374 static zend_object_handlers oh_ ## cname; \
375 static zend_object *create_ion_ ## cname(zend_class_entry *ce) \
376 { \
377 if (!ce) ce = ce_ ## cname; \
378 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
379 zend_object_std_init(&o->std, ce); \
380 object_properties_init(&o->std, ce); \
381 o->std.handlers = &oh_ ## cname; \
382 return &o->std; \
383 } \
384 static void free_ion_ ## cname(zend_object *std) \
385 { \
386 php_ion_ ## type *obj = php_ion_obj(type, std); \
387 php_ion_ ## type ## _dtor(obj); \
388 zend_object_std_dtor(std); \
389 }
390 #define php_ion_decl(type, cname) \
391 php_ion_decl_noclone(type, cname) \
392 static zend_object *clone_ion_ ## cname(zend_object *std) \
393 { \
394 php_ion_ ## type *old_obj = php_ion_obj(type, std), \
395 *new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \
396 php_ion_ ## type ## _copy(new_obj, old_obj); \
397 (void) old_obj; \
398 return &new_obj->std; \
399 }
400 #define php_ion_register(type, cname, ...) do { \
401 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
402 ce_ ## cname ->create_object = create_ion_ ## cname; \
403 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
404 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
405 oh_ ## cname .free_obj = free_ion_ ## cname; \
406 oh_ ## cname .clone_obj = clone_ion_ ## cname; \
407 } while (0)
408
409 #define ION_CHECK_RETURN(r, err, ...) do { \
410 iERR __err = err; \
411 if (UNEXPECTED(__err)) { \
412 zend_throw_exception_ex(ce_Exception, __err, "%s: %s", ion_error_to_str(__err), #err); \
413 __VA_ARGS__; \
414 return r; \
415 } \
416 } while (0)
417
418 #define ION_CHECK(err, ...) \
419 ION_CHECK_RETURN(, err, __VA_ARGS__)
420
421 #define ION_CATCH_RETURN(ret, ...) do { \
422 if (UNEXPECTED(EG(exception))) { \
423 __VA_ARGS__; \
424 return ret; \
425 } \
426 } while (0)
427
428 #define ION_CATCH(...) \
429 ION_CATCH_RETURN(, __VA_ARGS__)
430
431 #define PTR_CHECK_RETURN(ret, ptr, ...) do { \
432 if (UNEXPECTED(!(ptr))) { \
433 zend_throw_error(NULL, "Uninitialized object"); \
434 __VA_ARGS__; \
435 return ret; \
436 } \
437 } while (0)
438 #define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
439
440 #define OBJ_CHECK_RETURN(ret, obj, ...) do { \
441 PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
442 PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
443 } while (0)
444 #define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
445
446 static ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs)
447 {
448 is->length = zs ? (SIZE) zs->len : 0;
449 is->value = (BYTE *) (zs ? zs->val : NULL);
450 return is;
451 }
452
453 static zend_string *zend_string_from_ion(const ION_STRING *s)
454 {
455 return zend_string_init((const char *) s->value, s->length, 0);
456 }
457
458 static void call_constructor(zend_object *zo, uint32_t argc, zval *argv, zend_array *args_named)
459 {
460 zend_call_known_function(zo->ce->constructor, zo, zo->ce, NULL, argc, argv, args_named);
461 }
462
463 static zend_object *object_construct(zend_class_entry *ce, uint32_t argc, zval *argv, zend_array *args_named)
464 {
465 zval z_obj;
466 object_init_ex(&z_obj, ce);
467 call_constructor(Z_OBJ(z_obj), argc, argv, args_named);
468 return Z_OBJ(z_obj);
469 }
470
471 static void update_property_obj_ex(zend_class_entry *scope, zend_object *obj, const char *n, size_t l, zend_object *p)
472 {
473 zval zobj;
474 ZVAL_OBJ(&zobj, p);
475 zend_update_property(scope, obj, n, l, &zobj);
476 }
477 static void update_property_obj(zend_object *obj, const char *n, size_t l, zend_object *p)
478 {
479 update_property_obj_ex(obj->ce, obj, n, l, p);
480 }
481
482 #define RETURN_IONTYPE(typ) do { \
483 zend_object *__zo = php_ion_type_fetch(typ); \
484 if (UNEXPECTED(!__zo)) { \
485 RETURN_THROWS(); \
486 } \
487 RETURN_OBJ_COPY(__zo); \
488 } while(0)
489
490 static zend_object *php_ion_type_fetch(ION_TYPE typ)
491 {
492 zend_long index = ION_TYPE_INT(typ);
493 zval *ztype = zend_hash_index_find(ce_Type->backed_enum_table, index);
494
495 if (UNEXPECTED(!ztype || Z_TYPE_P(ztype) != IS_STRING)) {
496 zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", index, ZSTR_VAL(ce_Type->name));
497 return NULL;
498 }
499 return zend_enum_get_case(ce_Type, Z_STR_P(ztype));
500 }
501
502 static ION_TYPE ion_type_from_enum(zend_object *zo)
503 {
504 return (ION_TYPE) Z_LVAL_P(zend_enum_fetch_case_value(zo));
505 }
506
507 typedef struct php_ion_symbol_iloc {
508 ION_SYMBOL_IMPORT_LOCATION loc;
509 zend_string *name;
510 zend_object std;
511 } php_ion_symbol_iloc;
512
513 static void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc *obj)
514 {
515 zend_update_property_long(ce_Symbol_ImportLocation, &obj->std, ZEND_STRL("location"), obj->loc.location);
516 zend_update_property_str(ce_Symbol_ImportLocation, &obj->std, ZEND_STRL("name"), obj->name);
517 ion_string_from_zend(&obj->loc.name, obj->name);
518 }
519
520 static void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc *obj)
521 {
522 zend_string_release(obj->name);
523 }
524
525 static void php_ion_symbol_iloc_copy(php_ion_symbol_iloc *new_obj, php_ion_symbol_iloc *old_obj)
526 {
527 zend_objects_clone_members(&new_obj->std, &old_obj->std);
528 new_obj->name = zend_string_copy(old_obj->name);
529 ion_string_from_zend(&new_obj->loc.name, new_obj->name);
530 new_obj->loc.location = old_obj->loc.location;
531 }
532
533 php_ion_decl(symbol_iloc, Symbol_ImportLocation);
534
535 typedef struct php_ion_symbol {
536 ION_SYMBOL sym;
537 zend_string *value;
538 zend_object *iloc, std;
539 } php_ion_symbol;
540
541 static int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) {
542 zend_string *zs1 = zval_get_string(zv1);
543 zend_string *zs2 = zval_get_string(zv2);
544
545 if (EG(exception)) {
546 return 0;
547 }
548
549 int result;
550 if (zs1->len > zs2->len) {
551 result = 1;
552 } else if (zs2->len > zs1->len) {
553 result = -1;
554 } else {
555 result = memcmp(zs1->val, zs2->val, zs1->len);
556 }
557 zend_string_release(zs1);
558 zend_string_release(zs2);
559 return result;
560 }
561
562 static void php_ion_symbol_ctor(php_ion_symbol *obj)
563 {
564 zend_update_property_long(ce_Symbol, &obj->std, ZEND_STRL("sid"),
565 obj->sym.sid);
566 if (obj->value) {
567 zend_update_property_str(ce_Symbol, &obj->std, ZEND_STRL("value"), obj->value);
568 } else{
569 zend_update_property_null(ce_Symbol, &obj->std, ZEND_STRL("value"));
570 }
571 ion_string_from_zend(&obj->sym.value, obj->value);
572 if (obj->iloc) {
573 update_property_obj(&obj->std, ZEND_STRL("importLocation"), obj->iloc);
574 obj->sym.import_location = php_ion_obj(symbol_iloc, obj->iloc)->loc;
575 } else {
576 zend_update_property_null(ce_Symbol, &obj->std, ZEND_STRL("importLocation"));
577 }
578 }
579
580 static void php_ion_symbol_dtor(php_ion_symbol *obj)
581 {
582 if (obj->value) {
583 zend_string_release(obj->value);
584 }
585 }
586
587 static void php_ion_symbol_copy(php_ion_symbol *new_obj, php_ion_symbol *old_obj)
588 {
589 zend_objects_clone_members(&new_obj->std, &old_obj->std);
590 new_obj->sym = old_obj->sym;
591 if (old_obj->value) {
592 new_obj->value = zend_string_copy(old_obj->value);
593 ion_string_from_zend(&new_obj->sym.value, new_obj->value);
594 }
595
596 if ((new_obj->iloc = old_obj->iloc)) {
597 new_obj->sym.import_location = php_ion_obj(symbol_iloc, new_obj->iloc)->loc;
598 }
599 }
600
601 static void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value)
602 {
603 object_init_ex(return_value, ce_Symbol);
604 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ_P(return_value));
605
606 sym->sym.sid = sym_ptr->sid;
607 sym->value = zend_string_from_ion(&sym_ptr->value);
608 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
609 zval ziloc;
610 object_init_ex(&ziloc, ce_Symbol_ImportLocation);
611 sym->iloc = Z_OBJ(ziloc);
612
613 php_ion_symbol_iloc *iloc = php_ion_obj(symbol_iloc, sym->iloc);
614 iloc->loc.location = sym_ptr->import_location.location;
615 iloc->name = zend_string_from_ion(&sym_ptr->import_location.name);
616
617 php_ion_symbol_iloc_ctor(iloc);
618 }
619
620 php_ion_symbol_ctor(sym);
621
622 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
623 GC_DELREF(sym->iloc);
624 }
625 }
626
627 static zval *php_ion_global_symbol_fetch_by_enum(zend_string *name)
628 {
629 zval *zgs = zend_hash_find(&php_ion_globals.symbol.cache, name);
630 if (!zgs) {
631 zval *zid = zend_hash_find(&g_sym_map, name);
632 if (zid) {
633 zval *zss = zend_hash_index_find(&g_sym_hash, Z_LVAL_P(zid));
634 if (zss) {
635 zval zsym;
636 object_init_ex(&zsym, ce_Symbol);
637 php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ(zsym));
638 sym->sym.sid = Z_LVAL_P(zid);
639 sym->value = zval_get_string(zss);
640 php_ion_symbol_ctor(sym);
641 zgs = zend_hash_add(&php_ion_globals.symbol.cache, name, &zsym);
642 }
643 }
644 }
645 return zgs;
646 }
647
648 php_ion_decl(symbol, Symbol);
649
650 typedef struct php_ion_symbol_table {
651 ION_SYMBOL_TABLE *tab;
652 int (*dtor)(ION_SYMBOL_TABLE *);
653 zend_object std;
654 } php_ion_symbol_table;
655
656 static void php_ion_symbol_table_ctor(php_ion_symbol_table *obj)
657 {
658 OBJ_CHECK(obj);
659
660 ION_SYMBOL_TABLE_TYPE typ = ist_EMPTY;
661 ion_symbol_table_get_type(obj->tab, &typ);
662 if (typ != ist_LOCAL) {
663 ION_STRING is;
664 if (IERR_OK == ion_symbol_table_get_name(obj->tab, &is)) {
665 zend_update_property_stringl(ce_Symbol_Table_Shared, &obj->std, ZEND_STRL("name"), (char *) is.value, is.length);
666 }
667 int32_t iv;
668 if (IERR_OK == ion_symbol_table_get_version(obj->tab, &iv)) {
669 zend_update_property_long(ce_Symbol_Table_Shared, &obj->std, ZEND_STRL("version"), iv);
670 }
671 }
672 }
673
674 static void php_ion_symbol_table_dtor(php_ion_symbol_table *obj)
675 {
676 if (obj->tab) {
677 if (obj->dtor) {
678 obj->dtor(obj->tab);
679 }
680 obj->tab = NULL;
681 }
682 }
683
684 static void php_ion_symbol_table_copy(php_ion_symbol_table *new_obj, php_ion_symbol_table *old_obj)
685 {
686 // do not clone cache members
687 // zend_objects_clone_members(...)
688 if ((new_obj->dtor = old_obj->dtor)) {
689 ION_CHECK(ion_symbol_table_clone_with_owner(old_obj->tab, &new_obj->tab, NULL));
690 } else {
691 new_obj->tab = old_obj->tab;
692 }
693 // update non-cache members
694 php_ion_symbol_table_ctor(new_obj);
695 }
696
697 static void php_ion_symbol_table_import(php_ion_symbol_table *obj, php_ion_symbol_table *import)
698 {
699 OBJ_CHECK(obj);
700 OBJ_CHECK(import);
701
702 zval tmp;
703 zval *zimports = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), 0, &tmp);
704 if (zimports) {
705 zend_ulong idx = (uintptr_t) &import->std.gc;
706 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports), idx)) {
707 ION_CHECK(ion_symbol_table_import_symbol_table(obj->tab, import->tab));
708
709 SEPARATE_ARRAY(zimports);
710 GC_ADDREF(&import->std);
711 add_index_object(zimports, idx, &import->std);
712 zend_update_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), zimports);
713 }
714 }
715 }
716
717 static void php_ion_symbol_table_symbol_zval(php_ion_symbol_table *obj, ION_SYMBOL *sym, zval *return_value)
718 {
719 zval tmp;
720 zval *zsyms = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbols"), 0, &tmp);
721 if (zsyms) {
722 zval *zsym = zend_hash_index_find(Z_ARRVAL_P(zsyms), sym->sid);
723 if (zsym) {
724 RETURN_COPY(zsym);
725 }
726 }
727
728 php_ion_symbol_zval(sym, return_value);
729
730 if (zsyms) {
731 SEPARATE_ARRAY(zsyms);
732 ZVAL_ADDREF(return_value);
733 add_index_zval(zsyms, sym->sid, return_value);
734 }
735 }
736
737 php_ion_decl(symbol_table, Symbol_Table);
738
739 typedef struct php_ion_decimal_ctx {
740 decContext ctx;
741 zend_object std;
742 } php_ion_decimal_ctx;
743
744 #define php_ion_decimal_ctx_init_max(c, rounding) \
745 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
746 static void php_ion_decimal_ctx_init(decContext *ctx,
747 int digits, int emax, int emin, enum rounding round, zend_bool clamp)
748 {
749 memset(ctx, 0, sizeof(*ctx));
750 ctx->digits = digits;
751 ctx->emax = emax;
752 ctx->emin = emin;
753 ctx->round = round;
754 ctx->clamp = clamp;
755 }
756
757 static void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj, zend_object *o_round)
758 {
759 if (!obj->ctx.digits) {
760 php_ion_decimal_ctx_init_max(&obj->ctx, DEC_ROUND_HALF_EVEN);
761 }
762 if (o_round) {
763 update_property_obj(&obj->std, ZEND_STRL("round"), o_round);
764 } else {
765 zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("round"), obj->ctx.round);
766 }
767 zend_update_property_long(ce_Decimal_Context, &obj->std, ZEND_STRL("digits"), obj->ctx.digits);
768 zend_update_property_long(ce_Decimal_Context, &obj->std, ZEND_STRL("eMax"), obj->ctx.emax);
769 zend_update_property_long(ce_Decimal_Context, &obj->std, ZEND_STRL("eMin"), obj->ctx.emin);
770 zend_update_property_bool(ce_Decimal_Context, &obj->std, ZEND_STRL("clamp"), obj->ctx.clamp);
771 }
772
773 static void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx *obj)
774 {
775 }
776
777 static void php_ion_decimal_ctx_copy(php_ion_decimal_ctx *new_obj, php_ion_decimal_ctx *old_obj)
778 {
779 zend_objects_clone_members(&new_obj->std, &old_obj->std);
780 new_obj->ctx = old_obj->ctx;
781 }
782
783 php_ion_decl(decimal_ctx, Decimal_Context);
784
785 typedef struct php_ion_decimal {
786 ION_DECIMAL dec;
787 zend_object *ctx, std;
788 } php_ion_decimal;
789
790 static void php_ion_decimal_from_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long num)
791 {
792 if (num <= INT32_MAX && num >= INT32_MIN) {
793 ION_CHECK(ion_decimal_from_int32(dec, num));
794 } else if (num > 0 && num <= UINT32_MAX) {
795 ION_CHECK(ion_decimal_from_uint32(dec, num));
796 } else {
797 ION_INT *iint;
798 ION_CHECK(ion_int_alloc(NULL, &iint));
799 ION_CHECK(ion_int_from_long(iint, num),
800 ion_int_free(iint));
801 /* WATCH OUT: BS API */
802 dec->type = ION_DECIMAL_TYPE_QUAD;
803 ION_CHECK(ion_decimal_from_ion_int(dec, ctx, iint),
804 ion_int_free(iint));
805 ion_int_free(iint);
806 }
807 }
808
809 static zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec)
810 {
811 zend_string *zstr = zend_string_alloc(ION_DECIMAL_STRLEN(dec), 0);
812 (void) ion_decimal_to_string(dec, zstr->val);
813 return zend_string_truncate(zstr, strlen(zstr->val), 0);
814 }
815
816 static void php_ion_decimal_to_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long *l)
817 {
818 ION_INT *ii = NULL;
819 ION_CHECK(ion_int_alloc(NULL, &ii));
820 ION_CHECK(ion_decimal_to_ion_int(dec, ctx, ii), ion_int_free(ii));
821 int64_t i64;
822 ION_CHECK(ion_int_to_int64(ii, &i64), ion_int_free(ii));
823 *l = i64;
824 ion_int_free(ii);
825 }
826
827 static bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj)
828 {
829 int32_t result;
830
831 if (!ion_decimal_is_integer(&obj->dec)) {
832 return false;
833 }
834
835 result = 1;
836 ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_max, &php_ion_globals.decimal.ctx, &result);
837 if (result == 1) {
838 return false;
839 }
840 result = -1;
841 ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_min, &php_ion_globals.decimal.ctx, &result);
842 if (result == -1) {
843 return false;
844 }
845 return true;
846 }
847
848 static void php_ion_decimal_ctor(php_ion_decimal *obj)
849 {
850 if (!obj->ctx) {
851 zval zdc;
852 object_init_ex(&zdc, ce_Decimal_Context);
853 obj->ctx = Z_OBJ(zdc);
854 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL);
855 GC_DELREF(obj->ctx);
856 }
857 update_property_obj(&obj->std, ZEND_STRL("context"), obj->ctx);
858
859 if (php_ion_decimal_fits_zend_long(obj)) {
860 zend_long l;
861 php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l);
862 zend_update_property_long(ce_Decimal, &obj->std, ZEND_STRL("number"), l);
863 } else {
864 zend_string *zstr = php_ion_decimal_to_string(&obj->dec);
865 zend_update_property_str(ce_Decimal, &obj->std, ZEND_STRL("number"), zstr);
866 zend_string_release(zstr);
867 }
868 }
869
870 static void php_ion_decimal_dtor(php_ion_decimal *obj)
871 {
872 ion_decimal_free(&obj->dec);
873 }
874
875 static void php_ion_decimal_copy(php_ion_decimal *new_obj, php_ion_decimal *old_obj)
876 {
877 zend_objects_clone_members(&new_obj->std, &old_obj->std);
878 new_obj->ctx = old_obj->ctx;
879 ION_CHECK(ion_decimal_copy(&new_obj->dec, &old_obj->dec));
880 }
881
882 php_ion_decl(decimal, Decimal);
883
884 typedef php_date_obj php_ion_timestamp;
885
886 static zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx)
887 {
888 if (!ctx) {
889 ctx = &php_ion_globals.decimal.ctx;
890 }
891 decQuad microsecs, result;
892 decQuadMultiply(&result, decQuadFromInt32(&microsecs, 1000000), frac, ctx);
893 return (zend_long) decQuadToUInt32(&result, ctx, DEC_ROUND_HALF_EVEN);
894 }
895
896 static decQuad *ion_ts_frac_from_usec(decQuad *frac, int usec, decContext *ctx)
897 {
898 if (!ctx) {
899 ctx = &php_ion_globals.decimal.ctx;
900 }
901 decQuad microsecs, us;
902 return decQuadDivide(frac, decQuadFromInt32(&us, usec), decQuadFromInt32(&microsecs, 1000000), ctx);
903 }
904
905 static zend_string *php_ion_timestamp_format_fetch(zend_string *fmt_case)
906 {
907 return Z_STR_P(zend_enum_fetch_case_value(zend_enum_get_case(ce_Timestamp_Format, fmt_case)));
908 }
909
910 static zend_string *php_dt_format_from_precision(uint8_t precision)
911 {
912 switch (precision) {
913 case ION_TS_FRAC | 0x80:
914 return php_ion_timestamp_format_fetch(g_intern_str.FracTZ);
915 case ION_TS_FRAC:
916 return php_ion_timestamp_format_fetch(g_intern_str.Frac);
917 case ION_TS_SEC | 0x80:
918 return php_ion_timestamp_format_fetch(g_intern_str.SecTZ);
919 case ION_TS_SEC:
920 return php_ion_timestamp_format_fetch(g_intern_str.Sec);
921 case ION_TS_MIN | 0x80:
922 return php_ion_timestamp_format_fetch(g_intern_str.MinTZ);
923 case ION_TS_MIN:
924 return php_ion_timestamp_format_fetch(g_intern_str.Min);
925 case ION_TS_DAY:
926 return php_ion_timestamp_format_fetch(g_intern_str.Day);
927 case ION_TS_MONTH:
928 return php_ion_timestamp_format_fetch(g_intern_str.Month);
929 case ION_TS_YEAR:
930 return php_ion_timestamp_format_fetch(g_intern_str.Year);
931 default:
932 return ZSTR_CHAR('c');
933 }
934 }
935
936 static timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContext *ctx, zend_string **fmt)
937 {
938 timelib_time *time = ecalloc(1, sizeof(*time));
939
940 /* defaults */
941 time->y = 1970;
942 time->m = 1;
943 time->d = 1;
944
945 switch (ts->precision & 0x7f) {
946 case ION_TS_FRAC:
947 time->us = php_usec_from_ion(&ts->fraction, ctx);
948 /* fallthrough */
949 case ION_TS_SEC:
950 time->s = ts->seconds;
951 /* fallthrough */
952 case ION_TS_MIN:
953 time->i = ts->minutes;
954 time->h = ts->hours;
955 /* fallthrough */
956 case ION_TS_DAY:
957 time->d = ts->day;
958 /* fallthrough */
959 case ION_TS_MONTH:
960 time->m = ts->month;
961 /* fallthrough */
962 case ION_TS_YEAR:
963 time->y = ts->year;
964 /* fallthrough */
965 default:
966 time->z = ts->tz_offset * 60;
967 if (time->z || ts->precision & 0x80) {
968 time->zone_type = TIMELIB_ZONETYPE_OFFSET;
969 } else {
970 time->zone_type = TIMELIB_ZONETYPE_ID;
971 time->tz_info = get_timezone_info();
972 }
973 }
974
975 if (fmt) {
976 *fmt = php_dt_format_from_precision(ts->precision);
977 }
978 return time;
979 }
980
981 static ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_timestamp *ts, decContext *ctx)
982 {
983 memset(buf, 0, sizeof(*buf));
984
985 zval tmp;
986 int precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp));
987
988 if (!precision || precision > (ION_TS_FRAC|0x80)) {
989 zend_throw_exception_ex(ce_Exception, IERR_INVALID_ARG,
990 "Invalid precision (%d) of ion\\Timestamp", precision);
991 } else switch ((buf->precision = precision) & 0x7f) {
992 case ION_TS_FRAC:
993 ion_ts_frac_from_usec(&buf->fraction, (int) ts->time->us, ctx);
994 /* fallthrough */
995 case ION_TS_SEC:
996 buf->seconds = ts->time->s;
997 /* fallthrough */
998 case ION_TS_MIN:
999 buf->minutes = ts->time->i;
1000 /* fallthrough */
1001 case ION_TS_DAY:
1002 buf->hours = ts->time->h;
1003 buf->day = ts->time->d;
1004 /* fallthrough */
1005 case ION_TS_MONTH:
1006 buf->month = ts->time->m;
1007 /* fallthrough */
1008 case ION_TS_YEAR:
1009 buf->year = ts->time->y;
1010 /* fallthrough */
1011 default:
1012 buf->tz_offset = (short) (ts->time->z / 60);
1013 if (buf->tz_offset) {
1014 buf->precision |= 0x80;
1015 }
1016 }
1017
1018 return buf;
1019 }
1020
1021 static void php_ion_timestamp_ctor(php_ion_timestamp *obj, zend_long precision, zend_string *fmt, zend_string *dt, zval *tz)
1022 {
1023 if (!obj->time) {
1024 php_date_initialize(obj, dt ? dt->val : "", dt ? dt->len : 0, fmt ? fmt->val : NULL, tz, PHP_DATE_INIT_CTOR);
1025 }
1026 zend_update_property_long(ce_Timestamp, &obj->std, ZEND_STRL("precision"), precision);
1027
1028 fmt = php_dt_format_from_precision(precision);
1029 zend_update_property_str(ce_Timestamp, &obj->std, ZEND_STRL("format"), fmt);
1030 zend_string_release(fmt);
1031 }
1032
1033 typedef struct php_ion_catalog {
1034 ION_CATALOG *cat;
1035 zend_object std;
1036 } php_ion_catalog;
1037
1038 static void php_ion_catalog_ctor(php_ion_catalog *obj)
1039 {
1040 ION_CHECK(ion_catalog_open(&obj->cat));
1041 }
1042
1043 static void php_ion_catalog_dtor(php_ion_catalog *obj)
1044 {
1045 if (obj->cat) {
1046 ion_catalog_close(obj->cat);
1047 }
1048 }
1049
1050 static ION_COLLECTION *php_ion_catalog_collection(php_ion_catalog *cat)
1051 {
1052 /* do not look too close */
1053 struct {
1054 void *owner;
1055 ION_SYMBOL_TABLE *sys;
1056 ION_COLLECTION collection;
1057 } *cat_ptr = (void *) cat->cat;
1058 return &cat_ptr->collection;
1059 }
1060 // see https://github.com/amzn/ion-c/issues/269
1061 #ifndef IPCN_pNODE_TO_pDATA
1062 # define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0]))
1063 #endif
1064
1065 static void php_ion_catalog_copy(php_ion_catalog *new_obj, php_ion_catalog *old_obj)
1066 {
1067 // do not clone cache members
1068 php_ion_catalog_ctor(new_obj);
1069 OBJ_CHECK(new_obj);
1070
1071 ION_COLLECTION *col = php_ion_catalog_collection(old_obj);
1072 if (!ION_COLLECTION_IS_EMPTY(col)) {
1073 ION_COLLECTION_CURSOR cur;
1074 ION_COLLECTION_OPEN(col, cur);
1075 while (cur) {
1076 ION_SYMBOL_TABLE **ptr;
1077 ION_COLLECTION_NEXT(cur, ptr);
1078 if (*ptr) {
1079 ION_CHECK(ion_catalog_add_symbol_table(new_obj->cat, *ptr));
1080 }
1081 }
1082 }
1083 }
1084
1085 static zend_string *ion_symbol_table_to_key(ION_SYMBOL_TABLE *tab)
1086 {
1087 int32_t version;
1088 ION_STRING is;
1089 ION_CHECK_RETURN(NULL, ion_symbol_table_get_name(tab, &is));
1090 ION_CHECK_RETURN(NULL, ion_symbol_table_get_version(tab, &version));
1091
1092 smart_str s = {0};
1093 smart_str_appendl(&s, (char *) is.value, is.length);
1094 smart_str_appendc(&s, ':');
1095 smart_str_append_long(&s, version);
1096 smart_str_0(&s);
1097
1098 return s.s;
1099 }
1100
1101 static void php_ion_catalog_add_symbol_table(php_ion_catalog *obj, php_ion_symbol_table *tab)
1102 {
1103 OBJ_CHECK(obj);
1104 OBJ_CHECK(tab);
1105
1106 zval tmp;
1107 zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp);
1108 if (ztabs) {
1109 zend_ulong idx = (uintptr_t) &tab->std.gc;
1110 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs), idx)) {
1111 zend_string *key = ion_symbol_table_to_key(tab->tab);
1112 if (key) {
1113 ION_CHECK(ion_catalog_add_symbol_table(obj->cat, tab->tab),
1114 zend_string_release(key));
1115 SEPARATE_ARRAY(ztabs);
1116 GC_ADDREF(&tab->std);
1117 add_index_object(ztabs, idx, &tab->std);
1118 GC_ADDREF(&tab->std);
1119 add_assoc_object_ex(ztabs, key->val, key->len, &tab->std);
1120 zend_update_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), ztabs);
1121 zend_string_release(key);
1122 }
1123 }
1124 }
1125 }
1126
1127 static void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_SYMBOL_TABLE *tab, zval *return_value)
1128 {
1129 zend_string *key = ion_symbol_table_to_key(tab);
1130 PTR_CHECK(key);
1131
1132 zval tmp;
1133 zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp);
1134 if (ztabs) {
1135 zval *ztab = zend_hash_find(Z_ARRVAL_P(ztabs), key);
1136 if (ztab) {
1137 zend_string_release(key);
1138 RETURN_COPY(ztab);
1139 }
1140 }
1141
1142 object_init_ex(return_value, ce_Symbol_Table_Shared);
1143 php_ion_symbol_table *o_tab = php_ion_obj(symbol_table, Z_OBJ_P(return_value));
1144 o_tab->tab = tab;
1145 php_ion_symbol_table_ctor(o_tab);
1146
1147 if (ztabs) {
1148 SEPARATE_ARRAY(ztabs);
1149 ZVAL_ADDREF(return_value);
1150 add_index_zval(ztabs, (uintptr_t) &o_tab->std.gc, return_value);
1151 ZVAL_ADDREF(return_value);
1152 add_assoc_zval_ex(ztabs, key->val, key->len, return_value);
1153 }
1154 zend_string_release(key);
1155 }
1156
1157 php_ion_decl(catalog, Catalog);
1158
1159 typedef struct php_ion_reader_ccn_ctx {
1160 zend_object *obj;
1161 zend_fcall_info fci;
1162 zend_fcall_info_cache fcc;
1163 } php_ion_reader_ccn_ctx;
1164
1165 /*
1166 static void php_ion_reader_options_copy(php_ion_reader_options *new_obj, php_ion_reader_options *old_obj)
1167 {
1168 zend_objects_clone_members(&new_obj->std, &old_obj->std);
1169
1170 new_obj->opt = old_obj->opt;
1171 new_obj->cat = old_obj->cat;
1172 new_obj->dec_ctx = old_obj->dec_ctx;
1173 new_obj->cb = old_obj->cb;
1174 if (new_obj->cb) {
1175 zval zcb;
1176 ZVAL_OBJ(&zcb, new_obj->cb);
1177 zend_fcall_info_init(&zcb, 0, &new_obj->ccn.fci, &new_obj->ccn.fcc, NULL, NULL);
1178 new_obj->opt.context_change_notifier.context = &new_obj->ccn;
1179 }
1180 }
1181 */
1182
1183 typedef struct php_ion_reader {
1184 ION_READER *reader;
1185 ION_TYPE state;
1186 enum {
1187 BUFFER_READER,
1188 STREAM_READER,
1189 } type;
1190 union {
1191 zend_string *buffer;
1192 struct {
1193 php_stream *ptr;
1194 ION_STRING buf;
1195 } stream;
1196 };
1197
1198 ION_READER_OPTIONS options;
1199 php_ion_reader_ccn_ctx ccn;
1200 zend_object *cat, *dec_ctx, *cb, std;
1201 } php_ion_reader;
1202
1203 static iERR php_ion_reader_stream_handler(struct _ion_user_stream *user)
1204 {
1205 php_ion_reader *reader = (php_ion_reader *) user->handler_state;
1206 size_t remaining = 0, spare = reader->stream.buf.length;
1207
1208 if (user->curr && user->limit && (remaining = user->limit - user->curr)) {
1209 memmove(reader->stream.buf.value, user->curr, remaining);
1210 user->limit -= remaining;
1211 spare -= remaining;
1212 } else {
1213 user->curr = user->limit = reader->stream.buf.value;
1214 }
1215
1216 ssize_t read = php_stream_read(reader->stream.ptr, (char *) user->limit, spare);
1217 if (EXPECTED(read > 0)) {
1218 user->limit += read;
1219 return IERR_OK;
1220 }
1221
1222 if (EXPECTED(read == 0)) {
1223 return IERR_EOF;
1224 }
1225
1226 return IERR_READ_ERROR;
1227 }
1228
1229 static iERR on_context_change(void *context, ION_COLLECTION *imports)
1230 {
1231 iERR e = IERR_OK;
1232
1233 if (context) {
1234 php_ion_reader_ccn_ctx *ctx = context;
1235
1236 zval zobj;
1237 ZVAL_OBJ(&zobj, ctx->obj);
1238 zend_fcall_info_argn(&ctx->fci, 1, &zobj);
1239 if (SUCCESS != zend_fcall_info_call(&ctx->fci, &ctx->fcc, NULL, NULL)) {
1240 e = IERR_INTERNAL_ERROR;
1241 }
1242 zend_fcall_info_args_clear(&ctx->fci, false);
1243 }
1244 return e;
1245 }
1246
1247 static void php_ion_reader_ctor(php_ion_reader *obj)
1248 {
1249 iERR err;
1250
1251 if (obj->cb) {
1252 zval zcb;
1253 ZVAL_OBJ(&zcb, obj->cb);
1254 zend_fcall_info_init(&zcb, 0, &obj->ccn.fci, &obj->ccn.fcc, NULL, NULL);
1255 obj->ccn.obj = &obj->std;
1256 obj->options.context_change_notifier.context = &obj->ccn;
1257 obj->options.context_change_notifier.notify = on_context_change;
1258 update_property_obj_ex(ce_Reader_Reader,&obj->std, ZEND_STRL("onContextChange"), obj->cb);
1259 } else {
1260 zend_update_property_null(ce_Reader_Reader, &obj->std, ZEND_STRL("onContextChange"));
1261 }
1262 if (obj->cat) {
1263 update_property_obj_ex(ce_Reader_Reader,&obj->std, ZEND_STRL("catalog"), obj->cat);
1264 obj->options.pcatalog = php_ion_obj(catalog, obj->cat)->cat;
1265 } else {
1266 zend_update_property_null(ce_Reader_Reader, &obj->std, ZEND_STRL("catalog"));
1267 }
1268 if (obj->dec_ctx) {
1269 update_property_obj_ex(ce_Reader_Reader,&obj->std, ZEND_STRL("decimalContext"), obj->dec_ctx);
1270 obj->options.decimal_context = &php_ion_obj(decimal_ctx, obj->dec_ctx)->ctx;
1271 } else {
1272 zend_update_property_null(ce_Reader_Reader, &obj->std, ZEND_STRL("decimalContext"));
1273 }
1274
1275 zend_update_property_bool(ce_Reader_Reader, &obj->std, ZEND_STRL("returnSystemValues"),
1276 obj->options.return_system_values);
1277 zend_update_property_long(ce_Reader_Reader, &obj->std, ZEND_STRL("maxContainerDepth"),
1278 obj->options.max_container_depth);
1279 zend_update_property_long(ce_Reader_Reader, &obj->std, ZEND_STRL("maxAnnotations"),
1280 obj->options.max_annotation_count);
1281 zend_update_property_long(ce_Reader_Reader, &obj->std, ZEND_STRL("annotationBufferSize"),
1282 obj->options.max_annotation_buffered);
1283 zend_update_property_long(ce_Reader_Reader, &obj->std, ZEND_STRL("tempBufferSize"),
1284 obj->options.chunk_threshold);
1285 zend_update_property_bool(ce_Reader_Reader, &obj->std, ZEND_STRL("skipCharacterValidation"),
1286 obj->options.skip_character_validation);
1287
1288 if (obj->type == STREAM_READER) {
1289 obj->stream.buf.length = obj->options.chunk_threshold ? obj->options.chunk_threshold : 0x4000;
1290 obj->stream.buf.value = emalloc(obj->stream.buf.length);
1291 err = ion_reader_open_stream(&obj->reader, obj, php_ion_reader_stream_handler, &obj->options);
1292 } else {
1293 err = ion_reader_open_buffer(&obj->reader, (BYTE *) obj->buffer->val, (SIZE) obj->buffer->len, &obj->options);
1294 }
1295
1296 ION_CHECK(err);
1297 OBJ_CHECK(obj);
1298 }
1299
1300 static void php_ion_reader_dtor(php_ion_reader *obj)
1301 {
1302 if (obj->reader) {
1303 ion_reader_close(obj->reader);
1304 }
1305 if (obj->cb) {
1306 zend_fcall_info_args_clear(&obj->ccn.fci, true);
1307 }
1308 if (obj->type == STREAM_READER) {
1309 if (obj->stream.buf.value) {
1310 efree(obj->stream.buf.value);
1311 }
1312 if (obj->stream.ptr) {
1313 zend_list_delete(obj->stream.ptr->res);
1314 }
1315 } else {
1316 if (obj->buffer) {
1317 zend_string_release(obj->buffer);
1318 }
1319 }
1320 }
1321
1322 #define php_ion_reader_copy(n,o)
1323 php_ion_decl_noclone(reader, Reader_Reader);
1324 #define clone_ion_Reader_Reader NULL
1325
1326 typedef struct php_ion_writer {
1327 ION_WRITER *writer;
1328 enum {
1329 BUFFER_WRITER,
1330 STREAM_WRITER,
1331 } type;
1332 union {
1333 struct {
1334 smart_str str;
1335 struct _ion_user_stream *usr;
1336 } buffer;
1337 struct {
1338 ION_STRING buf;
1339 php_stream *ptr;
1340 } stream;
1341 };
1342
1343 ION_WRITER_OPTIONS options;
1344 zend_object *cat, *dec_ctx, std;
1345
1346 } php_ion_writer;
1347
1348 static iERR php_ion_writer_stream_handler(struct _ion_user_stream *user)
1349 {
1350 php_ion_writer *writer = (php_ion_writer *) user->handler_state;
1351
1352 if (EXPECTED(user->limit && user->curr)) {
1353 ptrdiff_t len = user->curr - writer->stream.buf.value;
1354 if (len != php_stream_write(writer->stream.ptr, (char *) writer->stream.buf.value, len)) {
1355 return IERR_WRITE_ERROR;
1356 }
1357 }
1358 user->curr = writer->stream.buf.value;
1359 user->limit = writer->stream.buf.value + writer->stream.buf.length;
1360 return IERR_OK;
1361 }
1362
1363 static void php_ion_writer_stream_init(php_ion_writer *obj)
1364 {
1365 obj->stream.buf.length = obj->options.temp_buffer_size ? obj->options.temp_buffer_size : 0x1000;
1366 obj->stream.buf.value = emalloc(obj->stream.buf.length);
1367 }
1368
1369 static void php_ion_writer_buffer_offer(php_ion_writer *obj)
1370 {
1371 if (obj->buffer.usr) {
1372 obj->buffer.usr->curr = (BYTE *) &obj->buffer.str.s->val[obj->buffer.str.s->len];
1373 obj->buffer.usr->limit = obj->buffer.usr->curr + obj->buffer.str.a - obj->buffer.str.s->len;
1374 }
1375 }
1376
1377 static void php_ion_writer_buffer_init(php_ion_writer *obj)
1378 {
1379 smart_str_alloc(&obj->buffer.str, 0, false);
1380 php_ion_writer_buffer_offer(obj);
1381 }
1382
1383 static void php_ion_writer_buffer_reset(php_ion_writer *obj)
1384 {
1385 smart_str_free(&obj->buffer.str);
1386 memset(&obj->buffer.str, 0, sizeof(obj->buffer.str));
1387 php_ion_writer_buffer_init(obj);
1388 }
1389
1390 static zend_string *php_ion_writer_buffer_copy(php_ion_writer *obj)
1391 {
1392 if (obj->buffer.usr) {
1393 // ensure that brain-dead ion_stream interface calls us again
1394 obj->buffer.usr->curr = NULL;
1395 obj->buffer.usr->limit = NULL;
1396 }
1397 smart_str_0(&obj->buffer.str);
1398 return zend_string_copy(obj->buffer.str.s);
1399 }
1400
1401 static void php_ion_writer_buffer_separate(php_ion_writer *obj, bool grow)
1402 {
1403 // see zend_string_separate and smart_str_erealloc
1404 zend_string *old_str = obj->buffer.str.s;
1405 zend_string *new_str = zend_string_alloc(obj->buffer.str.a << grow, false);
1406 memcpy(new_str->val, old_str->val, new_str->len = old_str->len);
1407 zend_string_release(old_str);
1408 obj->buffer.str.s = new_str;
1409 }
1410
1411 static void php_ion_writer_buffer_grow(php_ion_writer *obj)
1412 {
1413 if (obj->buffer.usr && obj->buffer.usr->curr) {
1414 obj->buffer.str.s->len = obj->buffer.usr->curr - (BYTE *) obj->buffer.str.s->val;
1415 }
1416 if (obj->buffer.usr && obj->buffer.usr->curr && obj->buffer.usr->curr == obj->buffer.usr->limit) {
1417 if (UNEXPECTED(GC_REFCOUNT(obj->buffer.str.s) > 1)) {
1418 php_ion_writer_buffer_separate(obj, true);
1419 } else {
1420 smart_str_erealloc(&obj->buffer.str, obj->buffer.str.a << 1);
1421 }
1422 } else if (UNEXPECTED(GC_REFCOUNT(obj->buffer.str.s) > 1)) {
1423 php_ion_writer_buffer_separate(obj, false);
1424 }
1425 php_ion_writer_buffer_offer(obj);
1426 }
1427
1428
1429 static iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user)
1430 {
1431 php_ion_writer *writer = (php_ion_writer *) user->handler_state;
1432 writer->buffer.usr = user;
1433 php_ion_writer_buffer_grow(writer);
1434 return IERR_OK;
1435 }
1436
1437 static void php_ion_writer_init_shared_imports(php_ion_writer *obj)
1438 {
1439 php_ion_catalog *cat = php_ion_obj(catalog, obj->cat);
1440 OBJ_CHECK(cat);
1441
1442 ION_CHECK(ion_writer_options_initialize_shared_imports(&obj->options));
1443
1444 ION_COLLECTION *col = php_ion_catalog_collection(cat);
1445 if (!ION_COLLECTION_IS_EMPTY(col)) {
1446 // holy, nah, forget it batman...
1447 ION_COLLECTION_CURSOR cur;
1448 ION_COLLECTION_OPEN(col, cur);
1449 while (cur) {
1450 ION_SYMBOL_TABLE **ptr;
1451 ION_COLLECTION_NEXT(cur, ptr);
1452 if (*ptr) {
1453 ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&obj->options, ptr, 1));
1454 }
1455 }
1456 }
1457 }
1458
1459 static void php_ion_writer_ctor(php_ion_writer *obj)
1460 {
1461 if (obj->dec_ctx) {
1462 update_property_obj_ex(ce_Writer_Writer, &obj->std, ZEND_STRL("decimalContext"), obj->dec_ctx);
1463 obj->options.decimal_context = &php_ion_obj(decimal_ctx, obj->dec_ctx)->ctx;
1464 } else {
1465 zend_update_property_null(ce_Writer_Writer, &obj->std, ZEND_STRL("decimalContext"));
1466 }
1467 if (obj->cat) {
1468 update_property_obj_ex(ce_Writer_Writer, &obj->std, ZEND_STRL("catalog"), obj->cat);
1469 obj->options.pcatalog = php_ion_obj(catalog, obj->cat)->cat;
1470 php_ion_writer_init_shared_imports(obj);
1471 } else {
1472 zend_update_property_null(ce_Writer_Writer, &obj->std, ZEND_STRL("catalog"));
1473 }
1474
1475 zend_update_property_bool(ce_Writer_Writer, &obj->std, ZEND_STRL("outputBinary"),
1476 obj->options.output_as_binary);
1477 zend_update_property_bool(ce_Writer_Writer, &obj->std, ZEND_STRL("compactFloats"),
1478 obj->options.compact_floats);
1479 zend_update_property_bool(ce_Writer_Writer, &obj->std, ZEND_STRL("escapeNonAscii"),
1480 obj->options.escape_all_non_ascii);
1481 zend_update_property_bool(ce_Writer_Writer, &obj->std, ZEND_STRL("prettyPrint"),
1482 obj->options.pretty_print);
1483 zend_update_property_bool(ce_Writer_Writer, &obj->std, ZEND_STRL("indentTabs"),
1484 obj->options.indent_with_tabs);
1485 zend_update_property_long(ce_Writer_Writer, &obj->std, ZEND_STRL("indentSize"),
1486 obj->options.indent_size);
1487 zend_update_property_bool(ce_Writer_Writer, &obj->std, ZEND_STRL("flushEveryValue"),
1488 obj->options.flush_every_value);
1489 zend_update_property_long(ce_Writer_Writer, &obj->std, ZEND_STRL("maxContainerDepth"),
1490 obj->options.max_container_depth);
1491 zend_update_property_long(ce_Writer_Writer, &obj->std, ZEND_STRL("maxAnnotations"),
1492 obj->options.max_annotation_count);
1493 zend_update_property_long(ce_Writer_Writer, &obj->std, ZEND_STRL("tempBufferSize"),
1494 obj->options.temp_buffer_size);
1495
1496 ION_STREAM_HANDLER h;
1497 if (obj->type == STREAM_WRITER) {
1498 h = php_ion_writer_stream_handler;
1499 php_ion_writer_stream_init(obj);
1500 } else {
1501 h = php_ion_writer_buffer_handler;
1502 php_ion_writer_buffer_init(obj);
1503 }
1504
1505 ION_CHECK(ion_writer_open_stream(&obj->writer, h, obj, &obj->options));
1506 OBJ_CHECK(obj);
1507 }
1508
1509 static void php_ion_writer_dtor(php_ion_writer *obj)
1510 {
1511 if (obj->writer) {
1512 ion_writer_close(obj->writer);
1513 }
1514 if (obj->cat) {
1515 ion_writer_options_close_shared_imports(&obj->options);
1516 }
1517 if (obj->type == STREAM_WRITER) {
1518 if (obj->stream.buf.value) {
1519 efree(obj->stream.buf.value);
1520 }
1521 if (obj->stream.ptr) {
1522 zend_list_delete(obj->stream.ptr->res);
1523 }
1524 } else {
1525 if (obj->buffer.str.s) {
1526 smart_str_0(&obj->buffer.str);
1527 zend_string_release(obj->buffer.str.s);
1528 }
1529 }
1530 }
1531
1532 #define php_ion_writer_copy(o,n)
1533 php_ion_decl_noclone(writer, Writer_Writer);
1534 #define clone_ion_Writer_Writer NULL
1535
1536 static bool can_serialize_fast(php_ion_serializer *ser)
1537 {
1538 if (ser->wri->ce != ce_Writer_Buffer_Writer && ser->wri->ce != ce_Writer_Stream_Writer) {
1539 return false;
1540 }
1541
1542 if (ser->std.ce != ce_Serializer_Serializer) {
1543 return false;
1544 }
1545
1546 return true;
1547 }
1548
1549 static void php_ion_serializer_ctor(php_ion_serializer *ser_obj)
1550 {
1551 php_ion_serializer *global_ser = &php_ion_globals.serializer;
1552 ser_obj->ids = global_ser->ids;
1553 ser_obj->tmp = global_ser->tmp;
1554
1555 zend_update_property_bool(ce_Serializer_Serializer, &ser_obj->std, ZEND_STRL("multiSequence"),
1556 ser_obj->multi_seq);
1557 zend_update_property_bool(ce_Serializer_Serializer, &ser_obj->std, ZEND_STRL("callMagicSerialize"),
1558 ser_obj->call_magic);
1559 if (ser_obj->call_custom) {
1560 zend_update_property_str(ce_Serializer_Serializer, &ser_obj->std, ZEND_STRL("callCustomSerialize"),
1561 ser_obj->call_custom);
1562 ser_obj->call_custom = zend_string_tolower(ser_obj->call_custom);
1563 } else {
1564 zend_update_property_null(ce_Serializer_Serializer, &ser_obj->std, ZEND_STRL("callCustomSerialize"));
1565 }
1566 }
1567
1568 static void php_ion_serializer_dtor(php_ion_serializer *obj)
1569 {
1570 if (obj->call_custom) {
1571 zend_string_release(obj->call_custom);
1572 }
1573 }
1574
1575 static void php_ion_serialize_zval(php_ion_serializer *, zval *);
1576
1577 static void php_ion_serialize_struct(php_ion_serializer *ser, zend_array *arr, bool unmangle_props, bool annotate_props)
1578 {
1579 if (can_serialize_fast(ser)) {
1580 ION_CHECK(ion_writer_start_container(php_ion_obj(writer, ser->wri)->writer, tid_STRUCT));
1581 } else {
1582 zval z_type;
1583 ZVAL_OBJ(&z_type, php_ion_type_fetch(tid_STRUCT));
1584 zend_call_method_with_1_params(ser->wri, NULL, NULL, "startContainer", NULL, &z_type);
1585 ION_CATCH();
1586 }
1587
1588 zval *v;
1589 zend_ulong h;
1590 zend_string *k = NULL;
1591 if (arr) ZEND_HASH_FOREACH_KEY_VAL_IND(arr, h, k, v)
1592 char buf[MAX_LENGTH_OF_LONG + 1];
1593 ION_STRING is;
1594 if (k) {
1595 size_t prop_len;
1596 const char *class_name, *prop_name;
1597 if (unmangle_props && (SUCCESS == zend_unmangle_property_name_ex(k, &class_name, &prop_name, &prop_len)) && class_name) {
1598 if (annotate_props) {
1599 if (can_serialize_fast(ser)) {
1600 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_PROPERTY));
1601 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer, ser->wri)->writer,
1602 ion_string_assign_cstr(&is, (char *) class_name, prop_name - class_name - 1)));
1603 } else {
1604 zval z_ann_prop, z_ann_class;
1605 ZVAL_CHAR(&z_ann_prop, 'p');
1606 ZVAL_STRINGL(&z_ann_class, class_name, prop_name - class_name);
1607 zend_call_method_with_2_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_prop, &z_ann_class);
1608 zval_ptr_dtor(&z_ann_class);
1609 ION_CATCH();
1610 }
1611 }
1612 } else {
1613 prop_name = k->val;
1614 prop_len = k->len;
1615 }
1616 ion_string_assign_cstr(&is, (char *) prop_name, (SIZE) prop_len);
1617 } else {
1618 char *end = buf + sizeof(buf) - 1;
1619 char *ptr = zend_print_long_to_buf(end, (zend_long) h);
1620 ion_string_assign_cstr(&is, ptr, (SIZE) (end - ptr));
1621 }
1622
1623 if (can_serialize_fast(ser)) {
1624 // WATCH OUT: field names need to be copied
1625 ION_STRING fn;
1626 ION_CHECK(ion_string_copy_to_owner(php_ion_obj(writer, ser->wri)->writer, &fn, &is));
1627 ION_CHECK(ion_writer_write_field_name(php_ion_obj(writer, ser->wri)->writer, &fn));
1628 } else {
1629 zval z_field_name;
1630 ZVAL_STRINGL(&z_field_name, (const char *) is.value, is.length);
1631 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeFieldName", NULL, &z_field_name);
1632 zval_ptr_dtor(&z_field_name);
1633 ION_CATCH();
1634 }
1635
1636 php_ion_serialize_zval(ser, v);
1637 ION_CATCH();
1638 ZEND_HASH_FOREACH_END();
1639
1640 if(can_serialize_fast(ser)) {
1641 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer, ser->wri)->writer));
1642 } else {
1643 zend_call_method_with_0_params(ser->wri, NULL, NULL, "finishContainer", NULL);
1644 }
1645 }
1646
1647 static void php_ion_serialize_list(php_ion_serializer *ser, zend_array *arr)
1648 {
1649 if (can_serialize_fast(ser)) {
1650 ION_CHECK(ion_writer_start_container(php_ion_obj(writer, ser->wri)->writer, tid_LIST));
1651 } else {
1652 zval z_type;
1653 ZVAL_OBJ(&z_type, php_ion_type_fetch(tid_LIST));
1654 zend_call_method_with_1_params(ser->wri, NULL, NULL, "startContainer", NULL, &z_type);
1655 ION_CATCH();
1656 }
1657
1658 zval *v;
1659 ZEND_HASH_FOREACH_VAL_IND(arr, v)
1660 php_ion_serialize_zval(ser, v);
1661 ION_CATCH();
1662 ZEND_HASH_FOREACH_END();
1663
1664 if (can_serialize_fast(ser)) {
1665 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer, ser->wri)->writer));
1666 } else {
1667 zend_call_method_with_0_params(ser->wri, NULL, NULL, "finishContainer", NULL);
1668 }
1669 }
1670
1671 static void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_object *zobject)
1672 {
1673 uint8_t *buf;
1674 size_t len;
1675 zval tmp;
1676
1677 ZVAL_OBJ(&tmp, zobject);
1678 if (SUCCESS == zobject->ce->serialize(&tmp, &buf, &len, NULL)) {
1679 if (can_serialize_fast(ser)) {
1680 ION_STRING is;
1681 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_SERIALIZEABLE));
1682 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, zobject->ce->name)));
1683 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer, ser->wri)->writer, buf, len));
1684 efree(buf);
1685 } else {
1686 zval z_ann_srlzbl, z_ann_class, z_clob;
1687 ZVAL_CHAR(&z_ann_srlzbl, 'S');
1688 ZVAL_STR_COPY(&z_ann_class, zobject->ce->name);
1689 zend_call_method_with_2_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_srlzbl, &z_ann_class);
1690 zval_ptr_dtor(&z_ann_class);
1691 ION_CATCH();
1692 ZVAL_STRINGL(&z_clob, (const char *) buf, len);
1693 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeCLob", NULL, &z_clob);
1694 zval_ptr_dtor(&z_clob);
1695 }
1696 } else if (!EG(exception)){
1697 zend_throw_exception_ex(ce_Exception, IERR_INTERNAL_ERROR,
1698 "Failed to serialize class %s", zobject->ce->name->val);
1699 }
1700 }
1701
1702 static void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_object *zobject, zend_function *fn)
1703 {
1704 zval rv;
1705
1706 ZVAL_NULL(&rv);
1707 zend_call_known_instance_method_with_0_params(fn ? fn : zobject->ce->__serialize, zobject, &rv);
1708 ION_CATCH();
1709
1710 if (IS_ARRAY == Z_TYPE(rv)) {
1711 if (can_serialize_fast(ser)) {
1712 ION_STRING is;
1713 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, fn ? &PHP_ION_SYMBOL_CUSTOM_OBJECT : &PHP_ION_SYMBOL_MAGIC_OBJECT));
1714 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, zobject->ce->name)));
1715 } else {
1716 zval z_ann_cust, z_ann_class;
1717 ZVAL_CHAR(&z_ann_cust, 'C');
1718 ZVAL_STR_COPY(&z_ann_class, zobject->ce->name);
1719 zend_call_method_with_2_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_cust, &z_ann_class);
1720 zval_ptr_dtor(&z_ann_class);
1721 }
1722 if (!EG(exception)) {
1723 php_ion_serialize_zval(ser, &rv);
1724 }
1725 zval_ptr_dtor(&rv);
1726 } else {
1727 zend_throw_exception_ex(ce_Exception, IERR_INTERNAL_ERROR,
1728 "%s serializer %s::%s did not return an array",
1729 fn ? "Custom" : "Magic", zobject->ce->name->val,
1730 fn ? fn->common.function_name->val : "__serialize");
1731 }
1732 }
1733
1734 static void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_object *zobject)
1735 {
1736 zval *z_cname = zend_enum_fetch_case_name(zobject);
1737
1738 if (can_serialize_fast(ser)) {
1739 ION_STRING is;
1740 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_ENUM));
1741 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, zobject->ce->name)));
1742 ION_CHECK(ion_writer_write_symbol(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, Z_STR_P(z_cname))));
1743 } else {
1744 zval z_ann_enm, z_ann_class;
1745 ZVAL_CHAR(&z_ann_enm, 'E');
1746 ZVAL_STR_COPY(&z_ann_class, zobject->ce->name);
1747 zend_call_method_with_2_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_enm, &z_ann_class);
1748 zval_ptr_dtor(&z_ann_class);
1749 ION_CATCH();
1750 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeSymbol", NULL, z_cname);
1751 }
1752 }
1753
1754 static void php_ion_serialize_object_std(php_ion_serializer *ser, zend_object *zobject)
1755 {
1756 if (can_serialize_fast(ser)) {
1757 ION_STRING is;
1758
1759 if (zobject->ce != zend_standard_class_def) {
1760 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_CLASS_OBJECT));
1761 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, zobject->ce->name)));
1762 } else {
1763 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_OBJECT));
1764 }
1765 } else {
1766 if (zobject->ce != zend_standard_class_def) {
1767 zval z_ann_cobj, z_ann_class;
1768 ZVAL_CHAR(&z_ann_cobj, 'c');
1769 ZVAL_STR_COPY(&z_ann_class, zobject->ce->name);
1770 zend_call_method_with_2_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_cobj, &z_ann_class);
1771 zval_ptr_dtor(&z_ann_class);
1772 } else {
1773 zval z_ann_obj;
1774 ZVAL_CHAR(&z_ann_obj, 'o');
1775 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_obj);
1776 }
1777 ION_CATCH();
1778 }
1779
1780 zval zobj;
1781 ZVAL_OBJ(&zobj, zobject);
1782 HashTable *props = zend_get_properties_for(&zobj, ZEND_PROP_PURPOSE_SERIALIZE);
1783 if (props) {
1784 php_ion_serialize_struct(ser, props, true, true);
1785 zend_release_properties(props);
1786 } else {
1787 zend_throw_exception_ex(ce_Exception, IERR_INTERNAL_ERROR,
1788 "Could not get properties for serialization of class %s",
1789 zobject->ce->name->val);
1790 }
1791 }
1792
1793 static void php_ion_serialize_object_lob(php_ion_serializer *ser, zend_object *zobject)
1794 {
1795 zval tmp_type, *type = zend_read_property_ex(NULL, zobject, ZSTR_KNOWN(ZEND_STR_TYPE), 0, &tmp_type);
1796 zval tmp_value, *value = zend_read_property_ex(NULL, zobject, ZSTR_KNOWN(ZEND_STR_VALUE), 0, &tmp_value);
1797
1798 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type)))) {
1799 case tid_BLOB_INT:
1800 if (can_serialize_fast(ser)) {
1801 ION_CHECK(ion_writer_write_blob(php_ion_obj(writer, ser->wri)->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
1802 } else {
1803 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeBLob", NULL, value);
1804 }
1805 break;
1806 case tid_CLOB_INT:
1807 if (can_serialize_fast(ser)) {
1808 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer, ser->wri)->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
1809 } else {
1810 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeCLob", NULL, value);
1811 }
1812 break;
1813 default:
1814 zend_throw_exception_ex(ce_Exception, IERR_INVALID_ARG,
1815 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type))));
1816 break;
1817 }
1818 }
1819
1820 static bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce)
1821 {
1822 return ce->__serialize && ser->call_magic;
1823 }
1824
1825 static bool can_call_iface_serialize(php_ion_serializer *ser, zend_class_entry *ce)
1826 {
1827 (void) ser;
1828 return !!ce->serialize; // NOLINT
1829 }
1830
1831 static bool can_call_custom_serialize(php_ion_serializer *ser, zend_object *zobject, zend_function **fn)
1832 {
1833 if (ser->call_custom) {
1834 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); // NOLINT
1835 }
1836 return false;
1837 }
1838
1839 static bool is_special_class(const zend_class_entry *ce, zend_class_entry **target) {
1840 #define IS_TARGET(_ce) \
1841 if (instanceof_function(ce, _ce)) { \
1842 *target = _ce; \
1843 return true; \
1844 }
1845 IS_TARGET(ce_Symbol);
1846 IS_TARGET(ce_Decimal);
1847 IS_TARGET(ce_Timestamp);
1848 IS_TARGET(ce_LOB);
1849 return false;
1850 #undef IS_TARGET
1851 }
1852
1853 static void php_ion_serialize_object(php_ion_serializer *ser, zend_object *zobject) {
1854 zend_function *fn;
1855 zend_class_entry *special_ce, *ce = zobject->ce;
1856 ZEND_ASSERT(ce);
1857
1858 if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1859 zend_throw_exception_ex(ce_Exception, IERR_INVALID_ARG,
1860 "Serializing %s is not allowed", ce->name->val);
1861 return;
1862 }
1863
1864 if (is_special_class(ce, &special_ce)) {
1865 if (can_serialize_fast(ser) || special_ce == ce_LOB) {
1866 if (special_ce == ce_Symbol) {
1867 ION_CHECK(ion_writer_write_ion_symbol(php_ion_obj(writer, ser->wri)->writer, &php_ion_obj(symbol, zobject)->sym));
1868 } else if (special_ce == ce_Decimal) {
1869 ION_CHECK(ion_writer_write_ion_decimal(php_ion_obj(writer, ser->wri)->writer, &php_ion_obj(decimal, zobject)->dec));
1870 } else if (special_ce == ce_Timestamp) {
1871 ION_TIMESTAMP its;
1872 php_ion_timestamp *pts = php_ion_obj(timestamp, zobject);
1873 decContext *ctx = php_ion_obj(writer, ser->wri)->options.decimal_context;
1874 ION_CHECK(ion_writer_write_timestamp(php_ion_obj(writer, ser->wri)->writer, ion_timestamp_from_php(&its, pts, ctx)));
1875 } else {
1876 assert(special_ce == ce_LOB);
1877 php_ion_serialize_object_lob(ser, zobject);
1878 }
1879 } else {
1880 zval z_param;
1881 const char *method = NULL;
1882
1883 ZVAL_OBJ(&z_param, zobject);
1884 if (special_ce == ce_Symbol) {
1885 method = "writeSymbol";
1886 } else if (special_ce == ce_Decimal) {
1887 method = "writeDecimal";
1888 } else if (special_ce == ce_Timestamp) {
1889 method = "writeTimestamp";
1890 } else {
1891 assert(!!method);
1892 }
1893 zend_call_method(ser->wri, NULL, NULL, method, strlen(method), NULL, 1, &z_param, NULL);
1894 }
1895 } else if (can_call_magic_serialize(ser, ce)) {
1896 php_ion_serialize_object_magic(ser, zobject, NULL);
1897 } else if (can_call_iface_serialize(ser, ce)) {
1898 php_ion_serialize_object_iface(ser, zobject);
1899 } else if (can_call_custom_serialize(ser, zobject, &fn)) {
1900 php_ion_serialize_object_magic(ser, zobject, fn);
1901 } else if (zobject->ce->ce_flags & ZEND_ACC_ENUM) {
1902 php_ion_serialize_object_enum(ser, zobject);
1903 } else {
1904 php_ion_serialize_object_std(ser, zobject);
1905 }
1906 }
1907
1908 static bool php_ion_serialize_system_value(php_ion_serializer *ser, zval *zv)
1909 {
1910 if (1 == php_ion_globals.serializer.level) {
1911 if (Z_TYPE_P(zv) == IS_OBJECT) {
1912 if (Z_OBJCE_P(zv) == ce_Symbol_Table_Shared) {
1913 php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(zv));
1914 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj->tab, php_ion_obj(writer, ser->wri)->writer));
1915 return true;
1916 }
1917 }
1918 }
1919 return false;
1920 }
1921
1922 static bool php_ion_serialize_backref(php_ion_serializer *ser, zval *zv)
1923 {
1924 if (Z_TYPE_P(zv) == IS_STRING && Z_STR_P(zv) == zend_empty_string) {
1925 return false;
1926 }
1927 if (Z_TYPE_P(zv) == IS_ARRAY && Z_ARR_P(zv) == &zend_empty_array) {
1928 return false;
1929 }
1930
1931 zend_ulong idx = (zend_ulong) (uintptr_t) Z_COUNTED_P(zv);
1932 zval *ref = zend_hash_index_find(ser->ids, idx);
1933 if (!ref) {
1934 zval num;
1935
1936 ZVAL_LONG(&num, zend_hash_num_elements(ser->ids));
1937 zend_hash_index_add(ser->ids, idx, &num);
1938
1939 Z_TRY_ADDREF_P(zv);
1940 zend_hash_next_index_insert(ser->tmp, zv);
1941
1942 return false;
1943 }
1944
1945 if (can_serialize_fast(ser)) {
1946 ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_BACKREF));
1947 ION_CHECK_RETURN(true, ion_writer_write_int64(php_ion_obj(writer, ser->wri)->writer, Z_LVAL_P(ref)));
1948 } else {
1949 zval z_ann_bref;
1950 ZVAL_CHAR(&z_ann_bref, 'r');
1951 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_bref);
1952 ION_CATCH_RETURN(true);
1953 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeInt", NULL, ref);
1954 }
1955 return true;
1956 }
1957
1958 static void php_ion_serialize_string(php_ion_serializer *ser, zend_string *str)
1959 {
1960 if (can_serialize_fast(ser)) {
1961 ION_STRING is;
1962 ION_CHECK(ion_writer_write_string(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, str)));
1963 } else {
1964 zval zs;
1965 ZVAL_STR(&zs, str);
1966 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeString", NULL, &zs);
1967 }
1968 }
1969
1970 static void php_ion_serialize_reference(php_ion_serializer *ser, zval *noref)
1971 {
1972 if (can_serialize_fast(ser)) {
1973 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer, ser->wri)->writer, &PHP_ION_SYMBOL_REFERENCE));
1974 } else {
1975 zval z_ann_ref;
1976 ZVAL_CHAR(&z_ann_ref, 'R');
1977 zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeAnnotation", NULL, &z_ann_ref);
1978 ION_CATCH();
1979 }
1980 php_ion_serialize_zval(ser, noref);
1981
1982 }
1983 static void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv)
1984 {
1985 if (php_ion_serialize_system_value(ser, zv)) {
1986 return;
1987 }
1988 if (php_ion_serialize_backref(ser, zv)) {
1989 return;
1990 }
1991
1992 switch (Z_TYPE_P(zv)) {
1993 case IS_STRING:
1994 php_ion_serialize_string(ser, Z_STR_P(zv));
1995 break;
1996
1997 case IS_ARRAY:
1998 if (zend_array_is_list(Z_ARRVAL_P(zv))) {
1999 php_ion_serialize_list(ser, Z_ARRVAL_P(zv));
2000 } else {
2001 php_ion_serialize_struct(ser, Z_ARRVAL_P(zv), false, false);
2002 }
2003 break;
2004
2005 case IS_OBJECT:
2006 php_ion_serialize_object(ser, Z_OBJ_P(zv));
2007 break;
2008
2009 case IS_REFERENCE:
2010 php_ion_serialize_reference(ser, Z_REFVAL_P(zv));
2011 break;
2012 }
2013 }
2014
2015 static void php_ion_serialize_scalar(php_ion_serializer *ser, zval *zv)
2016 {
2017 if (can_serialize_fast(ser)) {
2018 switch (Z_TYPE_P(zv)) {
2019 case IS_NULL:
2020 ION_CHECK(ion_writer_write_null(php_ion_obj(writer, ser->wri)->writer));
2021 break;
2022 case IS_TRUE:
2023 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer, ser->wri)->writer, TRUE));
2024 break;
2025 case IS_FALSE:
2026 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer, ser->wri)->writer, FALSE));
2027 break;
2028 case IS_LONG:
2029 ION_CHECK(ion_writer_write_int64(php_ion_obj(writer, ser->wri)->writer, Z_LVAL_P(zv)));
2030 break;
2031 case IS_DOUBLE:
2032 ION_CHECK(ion_writer_write_double(php_ion_obj(writer, ser->wri)->writer, Z_DVAL_P(zv)));
2033 break;
2034 }
2035 } else {
2036 if (Z_ISNULL_P(zv)) {
2037 zend_call_method_with_0_params(ser->wri, NULL, NULL, "writeNull", NULL);
2038 } else {
2039 const char *method = NULL;
2040
2041 if (Z_TYPE_P(zv) < IS_LONG) {
2042 method = "writeBool";
2043 } else if (Z_TYPE_P(zv) < IS_DOUBLE) {
2044 method = "writeInt";
2045 } else {
2046 method = "writeFloat";
2047 }
2048
2049 zend_call_method(ser->wri, NULL, NULL, method, strlen(method), NULL, 1, zv, NULL);
2050 }
2051 }
2052 }
2053
2054 static void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv)
2055 {
2056 PTR_CHECK(ser);
2057
2058 switch (Z_TYPE_P(zv)) {
2059 case IS_NULL:
2060 case IS_TRUE:
2061 case IS_FALSE:
2062 case IS_LONG:
2063 case IS_DOUBLE:
2064 php_ion_serialize_scalar(ser, zv);
2065 break;
2066 case IS_STRING:
2067 case IS_ARRAY:
2068 case IS_OBJECT:
2069 case IS_REFERENCE:
2070 php_ion_serialize_refcounted(ser, zv);
2071 break;
2072 default:
2073 zend_throw_exception_ex(ce_Exception, IERR_INVALID_ARG,
2074 "Failed to serialize value of type %s", zend_zval_type_name(zv));
2075 }
2076 }
2077
2078 #define php_ion_serializer_copy(o,n)
2079 php_ion_decl(serializer, Serializer_Serializer);
2080 #define clone_ion_Serializer NULL
2081
2082 static void php_ion_serialize_ex(php_ion_serializer *ser, zval *zv)
2083 {
2084 HashPosition pos;
2085 HashTable *arr = NULL;
2086
2087 if (ser->multi_seq) {
2088 if (Z_TYPE_P(zv) != IS_ARRAY || !zend_array_is_list(Z_ARRVAL_P(zv))) {
2089 zend_throw_exception_ex(ce_Exception, IERR_INVALID_ARG,
2090 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
2091 return;
2092 }
2093
2094 arr = Z_ARRVAL_P(zv);
2095
2096 zend_hash_internal_pointer_reset_ex(arr, &pos);
2097 zv = zend_hash_get_current_data_ex(arr, &pos);
2098 }
2099
2100 while (zv) {
2101 php_ion_globals_serializer_step();
2102 php_ion_serialize_zval(ser, zv);
2103 php_ion_globals_serializer_exit();
2104
2105 if (!ser->multi_seq) {
2106 break;
2107 }
2108 zend_hash_move_forward_ex(arr, &pos);
2109 zv = zend_hash_get_current_data_ex(arr, &pos);
2110 }
2111 }
2112
2113 void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value)
2114 {
2115 zend_object *zo_ser = NULL, *zo_wri = NULL;
2116
2117 if (!ser) {
2118 zo_ser = create_ion_Serializer_Serializer(NULL);
2119 ser = php_ion_obj(serializer, zo_ser);
2120 PTR_CHECK(ser);
2121 ser->call_magic = true;
2122 php_ion_serializer_ctor(ser);
2123 ION_CATCH();
2124 }
2125
2126 if (!ser->wri) {
2127 zo_wri = create_ion_Writer_Writer(ce_Writer_Buffer_Writer);
2128 php_ion_writer_ctor(php_ion_obj(writer, zo_wri));
2129 ser->wri = zo_wri;
2130 }
2131
2132 php_ion_serialize_ex(ser, zv);
2133
2134 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
2135 if (can_serialize_fast(ser)) {
2136 ion_writer_flush(php_ion_obj(writer, ser->wri)->writer, NULL);
2137 } else {
2138 zend_call_method_with_0_params(ser->wri, NULL, NULL, "flush", NULL);
2139 }
2140 RETVAL_STR_COPY(php_ion_obj(writer, ser->wri)->buffer.str.s);
2141
2142 if (zo_wri) {
2143 OBJ_RELEASE(zo_wri);
2144 ser->wri = NULL;
2145 }
2146 if (zo_ser) {
2147 OBJ_RELEASE(zo_ser);
2148 }
2149 }
2150
2151 static bool can_unserialize_fast(php_ion_unserializer *ser)
2152 {
2153 if (ser->rdr->ce != ce_Reader_Buffer_Reader && ser->rdr->ce != ce_Reader_Stream_Reader) {
2154 return false;
2155 }
2156
2157 if (ser->std.ce != ce_Unserializer_Unserializer) {
2158 return false;
2159 }
2160
2161 return true;
2162 }
2163
2164 static void php_ion_unserializer_ctor(php_ion_unserializer *ser_obj)
2165 {
2166 php_ion_unserializer *global_ser = &php_ion_globals.unserializer;
2167 ser_obj->ids = global_ser->ids;
2168 ser_obj->tmp = global_ser->tmp;
2169 ser_obj->addref = global_ser->addref;
2170
2171 zend_update_property_bool(ce_Unserializer_Unserializer, &ser_obj->std, ZEND_STRL("multiSequence"),
2172 ser_obj->multi_seq);
2173 zend_update_property_bool(ce_Unserializer_Unserializer, &ser_obj->std, ZEND_STRL("callMagicUnserialize"),
2174 ser_obj->call_magic);
2175 if (ser_obj->call_custom) {
2176 zend_update_property_str(ce_Unserializer_Unserializer, &ser_obj->std, ZEND_STRL("callCustomUnserialize"),
2177 ser_obj->call_custom);
2178 ser_obj->call_custom = zend_string_tolower(ser_obj->call_custom);
2179 } else {
2180 zend_update_property_null(ce_Unserializer_Unserializer, &ser_obj->std, ZEND_STRL("callCustomUnserialize"));
2181 }
2182 }
2183
2184 static void php_ion_unserializer_dtor(php_ion_unserializer *obj)
2185 {
2186 if (obj->call_custom) {
2187 zend_string_release(obj->call_custom);
2188 }
2189 }
2190
2191 static void php_ion_unserializer_next(php_ion_unserializer *ser, ION_TYPE *typ)
2192 {
2193 if (can_unserialize_fast(ser)) {
2194 ION_CHECK(ion_reader_next(php_ion_obj(reader, ser->rdr)->reader, typ));
2195 } else {
2196 zend_call_method_with_0_params(&ser->std, NULL, NULL, "next", NULL);
2197 ION_CATCH();
2198 zval z_type;
2199 zend_call_method_with_0_params(&ser->std, NULL, NULL, "key", &z_type);
2200 ION_CATCH();
2201 *typ = ion_type_from_enum(Z_OBJ(z_type));
2202 }
2203 }
2204
2205 static void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ);
2206
2207 static bool can_call_magic_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
2208 {
2209 return (ce && ce->__unserialize && ser->call_magic);
2210 }
2211
2212 static bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_class_entry *ce)
2213 {
2214 return (ce && ce->unserialize);
2215 }
2216
2217 static bool can_call_custom_unserialize(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
2218 {
2219 if (ser->call_custom) {
2220 return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); // NOLINT
2221 }
2222 return false;
2223 }
2224
2225 static zval *php_ion_unserialize_class(php_ion_unserializer *ser, zval *return_value)
2226 {
2227 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
2228
2229 if (ce) {
2230 object_init_ex(return_value, ce);
2231 return zend_hash_next_index_insert(ser->ids, return_value);
2232 }
2233
2234 zend_throw_exception_ex(ce_Exception, IERR_IMPORT_NOT_FOUND,
2235 "Could not find class %s", ser->annotations.object_class->val);
2236 return NULL;
2237 }
2238
2239 static void php_ion_unserialize_object_enum(php_ion_unserializer *ser, zval *return_value)
2240 {
2241 zend_string *zs_case = zval_get_string(return_value);
2242 zend_hash_next_index_insert(ser->tmp, return_value);
2243 ZVAL_NULL(return_value);
2244 ION_CATCH();
2245
2246 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
2247 if (!ce || !(ce->ce_flags & ZEND_ACC_ENUM)) {
2248 zend_throw_exception_ex(ce_Exception, IERR_INVALID_TOKEN,
2249 "Not a valid enum: %s", ser->annotations.object_class->val);
2250 return;
2251 }
2252 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce), zs_case)) {
2253 zend_throw_exception_ex(ce_Exception, IERR_INVALID_TOKEN,
2254 "Not a valid enum case: %s::%s", ser->annotations.object_class->val, zs_case->val);
2255 return;
2256 }
2257 RETVAL_OBJ_COPY(zend_enum_get_case(ce, zs_case));
2258 zend_hash_next_index_insert(ser->ids, return_value);
2259 zend_string_release(zs_case);
2260 }
2261
2262 static void php_ion_unserialize_object_iface(php_ion_unserializer *ser, zval *return_value)
2263 {
2264 zend_class_entry *ce = zend_lookup_class(ser->annotations.object_class);
2265 if (can_call_iface_unserialize(ser, ce)) {
2266 zend_string *s = zval_get_string(return_value);
2267 zend_hash_next_index_insert(ser->tmp, return_value);
2268 ZVAL_NULL(return_value);
2269 zval *backref = zend_hash_next_index_insert(ser->ids, return_value);
2270 if (SUCCESS == ce->unserialize(backref, ce, (BYTE *) s->val, s->len, NULL)) {
2271 RETVAL_ZVAL(backref, 0, 0);
2272 } else {
2273 zval_ptr_dtor(backref);
2274 ZVAL_NULL(backref);
2275 if (!EG(exception)) {
2276 zend_throw_exception_ex(ce_Exception, IERR_INTERNAL_ERROR,
2277 "Failed to unserialize class %s", ce->name->val);
2278 }
2279 }
2280 zend_string_release(s);
2281 } else {
2282 zend_throw_exception_ex(ce_Exception, IERR_INVALID_TOKEN,
2283 "Class %s does not implement Serializable", ser->annotations.object_class->val);
2284 }
2285 }
2286
2287 static void php_ion_unserialize_field_name(php_ion_unserializer *ser, zend_string **key, bool is_prop)
2288 {
2289 if (can_unserialize_fast(ser)) {
2290 // FIXME: symbol table?
2291 ION_STRING name;
2292 SID sid = UNKNOWN_SID;
2293 char buf[MAX_LENGTH_OF_LONG + 1 + 1] = {0}, *end = buf + sizeof(buf) - 1, *ptr;
2294
2295 ION_CHECK(ion_reader_get_field_name(php_ion_obj(reader, ser->rdr)->reader, &name));
2296 if (!name.length) {
2297 ION_SYMBOL *is_ptr;
2298 ION_CHECK(ion_reader_get_field_name_symbol(php_ion_obj(reader, ser->rdr)->reader, &is_ptr));
2299 if (!ION_SYMBOL_IS_NULL(is_ptr) && is_ptr->value.length) {
2300 ION_STRING_ASSIGN(&name, &is_ptr->value);
2301 } else {
2302 sid = is_ptr->sid;
2303 }
2304 }
2305
2306 switch (name.length) {
2307 case 0:
2308 ptr = zend_print_long_to_buf(end, sid);
2309 *--ptr = '$';
2310 *key = zend_string_init(ptr, end - ptr, 0);
2311 break;
2312 case 1:
2313 *key = ZSTR_CHAR(*name.value);
2314 break;
2315 default:
2316 if (is_prop) {
2317 *key = zend_string_init_interned((char *) name.value, name.length, 0);
2318 } else {
2319 *key = zend_string_from_ion(&name);
2320 }
2321 break;
2322 }
2323 } else {
2324 zval z_fn;
2325 ZVAL_UNDEF(&z_fn);
2326 zend_call_method_with_0_params(&ser->std, NULL, NULL, "getFieldName", &z_fn);
2327 if (!Z_STRLEN(z_fn) || EG(exception)) {
2328 zend_clear_exception();
2329 zend_call_method_with_0_params(&ser->std, NULL, NULL, "getFieldNameSymbol", &z_fn);
2330 ION_CATCH();
2331
2332 }
2333 *key = zval_get_string(&z_fn);
2334 zval_ptr_dtor(&z_fn);
2335 }
2336 }
2337
2338 static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_value)
2339 {
2340 zend_hash_next_index_insert(ser->ids, return_value);
2341
2342 if (can_unserialize_fast(ser)) {
2343 ION_CHECK(ion_reader_step_in(php_ion_obj(reader, ser->rdr)->reader));
2344 } else {
2345 zend_call_method_with_0_params(&ser->std, NULL, NULL, "getChildren", NULL);
2346 ION_CATCH();
2347 }
2348
2349 while (true) {
2350 ION_TYPE typ;
2351 php_ion_unserializer_next(ser, &typ);
2352 ION_CATCH();
2353
2354 if (typ == tid_EOF) {
2355 break;
2356 }
2357
2358 zend_string *key;
2359 php_ion_unserialize_field_name(ser, &key, true);
2360 ION_CATCH();
2361
2362 zval zvalue;
2363 ZVAL_UNDEF(&zvalue);
2364 php_ion_unserialize_zval(ser, &zvalue, &typ);
2365 ION_CATCH(zend_string_release(key));
2366
2367 zend_class_entry *ce = Z_OBJCE_P(return_value);
2368 if (ser->annotations.object_prop && ser->annotations.property_class->val[0] != '*') {
2369 ce = zend_lookup_class(ser->annotations.property_class);
2370 }
2371 zend_update_property_ex(ce, Z_OBJ_P(return_value), key, &zvalue);
2372 zval_ptr_dtor(&zvalue);
2373 zend_string_release(key);
2374 }
2375
2376 if (can_unserialize_fast(ser)) {
2377 ION_CHECK(ion_reader_step_out(php_ion_obj(reader, ser->rdr)->reader));
2378 }
2379 }
2380
2381 /**
2382 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
2383 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
2384 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
2385 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
2386 * nondeterministic behavior.
2387 */
2388 static void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *return_value)
2389 {
2390 zend_hash_next_index_insert(ser->ids, return_value);
2391
2392 if (can_unserialize_fast(ser)) {
2393 ION_CHECK(ion_reader_step_in(php_ion_obj(reader, ser->rdr)->reader));
2394 } else {
2395 zend_call_method_with_0_params(&ser->std, NULL, NULL, "getChildren", NULL);
2396 ION_CATCH();
2397 }
2398
2399 while (true) {
2400 ION_TYPE typ;
2401 php_ion_unserializer_next(ser, &typ);
2402 ION_CATCH();
2403
2404 if (typ == tid_EOF) {
2405 break;
2406 }
2407
2408 zend_string *key;
2409 php_ion_unserialize_field_name(ser, &key, false);
2410 ION_CATCH();
2411
2412 zval zvalue;
2413 ZVAL_UNDEF(&zvalue);
2414 php_ion_unserialize_zval(ser, &zvalue, &typ);
2415 ION_CATCH(zend_string_release(key));
2416
2417 // FIXME:: too naive; b0rked if the previous value is an array
2418 if (zend_symtable_exists(HASH_OF(return_value), key)) {
2419 zval tmp, *prev = zend_hash_find(HASH_OF(return_value), key);
2420 if (Z_TYPE_P(prev) != IS_ARRAY) {
2421 array_init(&tmp);
2422 Z_TRY_ADDREF_P(prev);
2423 zend_hash_next_index_insert(Z_ARRVAL(tmp), prev);
2424 prev = zend_hash_update(HASH_OF(return_value), key, &tmp);
2425 }
2426 zend_hash_next_index_insert(Z_ARRVAL_P(prev), &zvalue);
2427 } else {
2428 zend_symtable_update(HASH_OF(return_value), key, &zvalue);
2429 }
2430 zend_string_release(key);
2431 }
2432
2433 if (can_unserialize_fast(ser)) {
2434 ION_CHECK(ion_reader_step_out(php_ion_obj(reader, ser->rdr)->reader));
2435 }
2436 }
2437
2438 static void verify_unserializer(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn)
2439 {
2440 switch (ser->annotations.object_type) {
2441 case 'c':
2442 *fn = NULL;
2443 break;
2444
2445 case 'C':
2446 if (!can_call_custom_unserialize(ser, zobject, fn)) {
2447 zend_throw_exception_ex(ce_Exception, IERR_INVALID_TOKEN,
2448 "Could not find custom serializer method of %s", ser->annotations.object_class->val);
2449 }
2450 break;
2451
2452 case 'O':
2453 if (!can_call_magic_unserialize(ser, zobject->ce)) {
2454 zend_throw_exception_ex(ce_Exception, IERR_INVALID_TOKEN,
2455 "Could not find method %s::__unserialize()", ser->annotations.object_class->val);
2456 }
2457 *fn = zobject->ce->__unserialize;
2458 break;
2459
2460 default:
2461 zend_throw_exception_ex(ce_Exception, IERR_INVALID_TOKEN,
2462 "Invalid object type %c", ser->annotations.object_type);
2463 }
2464 }
2465
2466 static void php_ion_unserialize_object(php_ion_unserializer *ser, zval *return_value)
2467 {
2468 // backup possible backref to array returned by magic/custom __serialize()
2469 zval *input = zend_hash_next_index_insert(ser->tmp, return_value);
2470 ZVAL_NULL(return_value);
2471 php_ion_unserialize_class(ser, return_value);
2472 ION_CATCH();
2473
2474 zend_function *fn = NULL;
2475 zend_object *zobject = Z_OBJ_P(return_value);
2476 verify_unserializer(ser, zobject, &fn);
2477 ION_CATCH();
2478
2479 // plain object
2480 if (!fn) {
2481 php_ion_unserialize_props(ser, return_value);
2482 return;
2483 }
2484
2485 // magic object
2486 if (Z_TYPE_P(input) != IS_ARRAY) {
2487 zval_ptr_dtor(input);
2488 array_init(input);
2489 zend_hash_real_init_mixed(Z_ARRVAL_P(input));
2490 php_ion_unserialize_hash(ser, input);
2491 ION_CATCH();
2492 }
2493 zval rv;
2494 ZVAL_NULL(&rv);
2495 zend_call_method_with_1_params(zobject, zobject->ce, &fn, "", &rv, input);
2496 zval_ptr_dtor(&rv);
2497 }
2498
2499 static void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *return_value)
2500 {
2501 if (ser->annotations.object_class) {
2502 switch (ser->annotations.object_type) {
2503 case 'S':
2504 php_ion_unserialize_object_iface(ser, return_value);
2505 break;
2506 case 'E':
2507 php_ion_unserialize_object_enum(ser, return_value);
2508 break;
2509 default:
2510 php_ion_unserialize_object(ser, return_value);
2511 }
2512 } else if (!ser->annotations.object_type) {
2513 bool is_shared_symtab = ser->annotations.shared_symtab;
2514 array_init(return_value);
2515 php_ion_unserialize_hash(ser, return_value);
2516 if (is_shared_symtab) {
2517 zend_object *zo_ss = object_construct(ce_Symbol_Table_Shared, 0, NULL, Z_ARRVAL_P(return_value));
2518 zval_ptr_dtor(return_value);
2519 RETURN_OBJ(zo_ss);
2520 }
2521 } else if (ser->annotations.object_type == 'o') {
2522 object_init(return_value);
2523 php_ion_unserialize_hash(ser, return_value);
2524 } else {
2525 zend_throw_exception_ex(ce_Exception, IERR_INVALID_TOKEN,
2526 "Invalid object annotation %c::", ser->annotations.object_type);
2527 }
2528 }
2529
2530 static void php_ion_unserialize_list(php_ion_unserializer *ser, zval *return_value)
2531 {
2532 if (can_unserialize_fast(ser)) {
2533 ION_CHECK(ion_reader_step_in(php_ion_obj(reader, ser->rdr)->reader));
2534 } else {
2535 zend_call_method_with_0_params(&ser->std, NULL, NULL, "getChildren", NULL);
2536 }
2537 array_init(return_value);
2538 zend_hash_next_index_insert(ser->ids, return_value);
2539
2540 while (true) {
2541 ION_TYPE typ;
2542 php_ion_unserializer_next(ser, &typ);
2543 ION_CATCH();
2544
2545 if (typ == tid_EOF) {
2546 break;
2547 }
2548
2549 zval next;
2550 php_ion_unserialize_zval(ser, &next, &typ);
2551 ION_CATCH();
2552
2553 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &next);
2554 }
2555
2556 if (can_unserialize_fast(ser)) {
2557 ION_CHECK(ion_reader_step_out(php_ion_obj(reader, ser->rdr)->reader));
2558 } else {
2559 // userland reader object already stepped out
2560 }
2561 }
2562
2563 static void php_ion_reader_read_lob(ION_READER *reader, zval *return_value)
2564 {
2565 zend_string *zstr = zend_string_alloc(0x1000, 0);
2566 again: ;
2567 SIZE read = 0;
2568 iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, (SIZE) zstr->len, &read);
2569 if (err == IERR_BUFFER_TOO_SMALL) {
2570 zstr = zend_string_extend(zstr, zstr->len << 2, 0);
2571 goto again;
2572 }
2573 ION_CHECK(err, zend_string_release(zstr));
2574 if (zstr->len > read) {
2575 zstr->val[read] = 0;
2576 zstr = zend_string_truncate(zstr, read, 0);
2577 }
2578 RETURN_STR(zstr);
2579 }
2580
2581 static void php_ion_reader_read_timestamp(ION_READER *reader, ION_READER_OPTIONS *opt, zval *return_value)
2582 {
2583 ION_TIMESTAMP ts;
2584 ION_CHECK(ion_reader_read_timestamp(reader, &ts));
2585
2586 object_init_ex(return_value, ce_Timestamp);
2587 php_ion_timestamp *ts_obj = php_ion_obj(timestamp, Z_OBJ_P(return_value));
2588
2589 zend_string *fmt = NULL;
2590 decContext *ctx = opt ? opt->decimal_context : NULL;
2591 ts_obj->time = php_time_from_ion(&ts, ctx, &fmt);
2592 php_ion_timestamp_ctor(ts_obj, ts.precision, fmt, NULL, NULL);
2593 zend_string_release(fmt);
2594
2595 OBJ_CHECK(ts_obj);
2596 }
2597
2598 static void php_ion_reader_read_int(ION_READER *reader, zval *return_value)
2599 {
2600 ION_INT *num = NULL;
2601 ION_CHECK(ion_int_alloc(reader, &num));
2602 ION_CHECK(ion_reader_read_ion_int(reader, num));
2603
2604 // TODO: SIZEOF_ZEND_LONG == 4
2605 int64_t i64;
2606 iERR err = ion_int_to_int64(num, &i64);
2607 switch (err) {
2608 case IERR_OK:
2609 RETVAL_LONG(i64);
2610 goto done;
2611
2612 case IERR_NUMERIC_OVERFLOW: ;
2613 SIZE max, len;
2614 ION_CHECK(ion_int_char_length(num, &max));
2615 zend_string *zs = zend_string_alloc(max, 0);
2616
2617 err = ion_int_to_char(num, (BYTE *) zs->val, max, &len);
2618 zs->val[zs->len = len] = 0;
2619 RETVAL_STR(zs);
2620 /* fall through */
2621
2622 default:
2623 done:
2624 ion_int_free(num);
2625 ION_CHECK(err);
2626 }
2627 }
2628
2629 static void php_ion_unserialize_backref(php_ion_unserializer *ser, zval *return_value)
2630 {
2631 zval *backref = zend_hash_index_find(ser->ids, Z_LVAL_P(return_value));
2632
2633 if (backref) {
2634 ZVAL_COPY_VALUE(return_value, backref);
2635 zend_hash_next_index_insert(ser->addref, return_value);
2636 } else {
2637 zend_throw_exception_ex(ce_Exception, IERR_INTERNAL_ERROR,
2638 "Could not find back reference " ZEND_LONG_FMT, Z_LVAL_P(return_value));
2639 }
2640 }
2641
2642 static zend_string *php_ion_unserialize_annotation(php_ion_unserializer *ser, zend_long idx)
2643 {
2644 zval z_ann;
2645
2646 if (can_unserialize_fast(ser)) {
2647 ION_STRING ann_str;
2648 ION_CHECK_RETURN(NULL, ion_reader_get_an_annotation(php_ion_obj(reader, ser->rdr)->reader, idx, &ann_str));
2649
2650 if (ann_str.length == 1){
2651 // no need to remember interned string
2652 ZVAL_CHAR(&z_ann, *ann_str.value);
2653 } else {
2654 ZVAL_STRINGL(&z_ann, (char *) ann_str.value, ann_str.length);
2655 zend_hash_next_index_insert(ser->tmp, &z_ann);
2656 }
2657 } else {
2658 zval z_idx;
2659 ZVAL_LONG(&z_idx, idx);
2660 zend_call_method_with_1_params(&ser->std, NULL, NULL, "getAnnotation", &z_ann, &z_idx);
2661 ION_CATCH_RETURN(NULL);
2662 zend_hash_next_index_insert(ser->tmp, &z_ann);
2663 }
2664
2665 return Z_STR(z_ann);
2666 }
2667
2668 static void php_ion_unserialize_annotations(php_ion_unserializer *ser)
2669 {
2670 memset(&ser->annotations, 0, sizeof(ser->annotations));
2671
2672 int32_t ann_cnt;
2673 if (can_unserialize_fast(ser)) {
2674 ION_CHECK(ion_reader_get_annotation_count(php_ion_obj(reader, ser->rdr)->reader, &ann_cnt));
2675 } else {
2676 zval z_ann_cnt;
2677 zend_call_method_with_0_params(&ser->std, NULL, NULL, "countAnnotations", &z_ann_cnt);
2678 ION_CATCH();
2679 ann_cnt = Z_LVAL(z_ann_cnt);
2680 }
2681
2682 for (int32_t i = 0; i < ann_cnt; ++i) {
2683 zend_string *zs_ann = php_ion_unserialize_annotation(ser, i);
2684
2685 if (zs_ann->len != 1) {
2686 if (zend_string_equals_literal(zs_ann, ION_SYS_SYMBOL_SHARED_SYMBOL_TABLE)) {
2687 ser->annotations.shared_symtab = true;
2688 }
2689
2690 continue;
2691 }
2692
2693 switch (*zs_ann->val) {
2694 default:
2695 // ignore
2696 break;
2697
2698 case 'R':
2699 if (ser->annotations.makeref) {
2700 zend_throw_exception_ex(ce_Exception, IERR_INVALID_SYNTAX,
2701 "Invalid multiple reference annotations");
2702 return;
2703 }
2704 ser->annotations.makeref = true;
2705 break;
2706
2707 case 'r':
2708 if (ser->annotations.backref) {
2709 zend_throw_exception_ex(ce_Exception, IERR_INVALID_SYNTAX,
2710 "Invalid multiple back reference annotations");
2711 return;
2712 }
2713 ser->annotations.backref = true;
2714 break;
2715
2716 case 'p':
2717 if (ser->annotations.object_prop) {
2718 zend_throw_exception_ex(ce_Exception, IERR_INVALID_SYNTAX,
2719 "Invalid multiple object property annotations");
2720 return;
2721 }
2722 ser->annotations.object_prop = true;
2723 ser->annotations.property_class = php_ion_unserialize_annotation(ser, ++i);
2724 ION_CATCH();
2725 break;
2726
2727 case 'E':
2728 case 'S':
2729 case 'O':
2730 case 'C':
2731 case 'o':
2732 case 'c':
2733 if (ser->annotations.object_type) {
2734 zend_throw_exception_ex(ce_Exception, IERR_INVALID_SYNTAX,
2735 "Invalid multiple object type annotations: %c::%c",
2736 ser->annotations.object_type, *zs_ann->val);
2737 return;
2738 }
2739
2740 ser->annotations.object_type = *zs_ann->val;
2741 if (ser->annotations.object_type != 'o') {
2742 ser->annotations.object_class = php_ion_unserialize_annotation(ser, ++i);
2743 ION_CATCH();
2744 }
2745 break;
2746 }
2747
2748 // sanity checks
2749 if (ser->annotations.object_type && ser->annotations.object_type != 'o' && !ser->annotations.object_class) {
2750 zend_throw_exception_ex(ce_Exception, IERR_INVALID_SYNTAX,
2751 "Invalid object annotation without class name: %c::", ser->annotations.object_type);
2752 return;
2753 }
2754 if (ser->annotations.object_type == 'o' && ser->annotations.object_class) {
2755 zend_throw_exception_ex(ce_Exception, IERR_INVALID_SYNTAX,
2756 "Invalid object annotation with class name: o::%s", ser->annotations.object_class->val);
2757 return;
2758 }
2759 }
2760 }
2761
2762 static void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ)
2763 {
2764 if (typ) {
2765 memcpy(&ser->type, typ, sizeof(ser->type)); // NOLINT
2766 } else {
2767 typ = &ser->type;
2768 if (can_unserialize_fast(ser)) {
2769 ION_CHECK(ion_reader_next(php_ion_obj(reader, ser->rdr)->reader, typ));
2770 } else {
2771 zend_call_method_with_0_params(&ser->std, NULL, NULL, "next", NULL);
2772 ION_CATCH();
2773
2774 zval z_type;
2775 zend_call_method_with_0_params(&ser->std, NULL, NULL, "getType", &z_type);
2776 ION_CATCH();
2777 *typ = ion_type_from_enum(Z_OBJ_P(return_value));
2778 }
2779 }
2780
2781 php_ion_unserialize_annotations(ser);
2782 ION_CATCH();
2783
2784 if (ser->annotations.makeref) {
2785 ZVAL_MAKE_REF(return_value);
2786 zend_hash_next_index_insert(ser->ids, return_value);
2787 ZVAL_DEREF(return_value);
2788 }
2789
2790 if (ION_TYPE_INT(*typ) > 0) {
2791 BOOL is_null;
2792 if (can_unserialize_fast(ser)) {
2793 ION_CHECK(ion_reader_is_null(php_ion_obj(reader, ser->rdr)->reader, &is_null));
2794 } else {
2795 zval z_is_null;
2796 zend_call_method_with_0_params(&ser->std, NULL, NULL, "isNull", &z_is_null);
2797 is_null = i_zend_is_true(&z_is_null);
2798 }
2799 if (is_null) {
2800 RETURN_NULL();
2801 }
2802 }
2803
2804 switch (ION_TYPE_INT(*typ)) {
2805 case tid_NULL_INT:
2806 if (can_unserialize_fast(ser)) {
2807 ION_CHECK(ion_reader_read_null(php_ion_obj(reader, ser->rdr)->reader, typ));
2808 } else {
2809 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readNull", return_value);
2810 ION_CATCH();
2811 *typ = ion_type_from_enum(Z_OBJ_P(return_value));
2812 }
2813 RETURN_NULL();
2814
2815 case tid_BOOL_INT:
2816 if (can_unserialize_fast(ser)) {
2817 BOOL bval;
2818 ION_CHECK(ion_reader_read_bool(php_ion_obj(reader, ser->rdr)->reader, &bval));
2819 RETURN_BOOL(bval);
2820 } else {
2821 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readBool", return_value);
2822 return;
2823 }
2824
2825 case tid_INT_INT:
2826 if (can_unserialize_fast(ser)) {
2827 php_ion_reader_read_int(php_ion_obj(reader, ser->rdr)->reader, return_value);
2828 } else {
2829 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readInt", return_value);
2830 }
2831 if (ser->annotations.backref) {
2832 ION_CATCH();
2833 php_ion_unserialize_backref(ser, return_value);
2834 }
2835 if (ser->annotations.object_type) {
2836 if (!ser->annotations.backref) {
2837 zend_throw_exception_ex(ce_Exception, IERR_INVALID_SYNTAX,
2838 "Invalid object type annotation: %c::" ZEND_LONG_FMT,
2839 ser->annotations.object_type, Z_LVAL_P(return_value));
2840 return;
2841 }
2842 goto unserialize_struct;
2843 }
2844 return;
2845
2846 case tid_FLOAT_INT:
2847 if (can_unserialize_fast(ser)) {
2848 double d;
2849 ION_CHECK(ion_reader_read_double(php_ion_obj(reader, ser->rdr)->reader, &d));
2850 RETURN_DOUBLE(d);
2851 } else {
2852 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readFloat", return_value);
2853 return;
2854 }
2855
2856 case tid_DECIMAL_INT:
2857 if (can_unserialize_fast(ser)) {
2858 object_init_ex(return_value, ce_Decimal);
2859 php_ion_decimal *dec = php_ion_obj(decimal, Z_OBJ_P(return_value));
2860 ION_CHECK(ion_reader_read_ion_decimal(php_ion_obj(reader, ser->rdr)->reader, &dec->dec));
2861 php_ion_decimal_ctor(dec);
2862 } else {
2863 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readDecimal", return_value);
2864 ION_CATCH();
2865 }
2866 zend_hash_next_index_insert(ser->ids, return_value);
2867 return;
2868
2869 case tid_TIMESTAMP_INT:
2870 if (can_unserialize_fast(ser)) {
2871 php_ion_reader *reader = php_ion_obj(reader, ser->rdr);
2872 php_ion_reader_read_timestamp(reader->reader, &reader->options, return_value);
2873 } else {
2874 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readTimestamp", return_value);
2875 }
2876 ION_CATCH();
2877 zend_hash_next_index_insert(ser->ids, return_value);
2878 return;
2879
2880 case tid_SYMBOL_INT:
2881 if (can_unserialize_fast(ser)) {
2882 ION_SYMBOL sym;
2883 ION_CHECK(ion_reader_read_ion_symbol(php_ion_obj(reader, ser->rdr)->reader, &sym));
2884 php_ion_symbol_zval(&sym, return_value);
2885 } else {
2886 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readSymbol", return_value);
2887 }
2888 ION_CATCH();
2889 if (ser->annotations.object_type) {
2890 goto unserialize_struct;
2891 }
2892 zend_hash_next_index_insert(ser->ids, return_value);
2893 return;
2894
2895 case tid_STRING_INT:
2896 if (can_unserialize_fast(ser)) {
2897 ION_STRING str;
2898 ION_CHECK(ion_reader_read_string(php_ion_obj(reader, ser->rdr)->reader, &str));
2899 RETVAL_STRINGL((char *) str.value, str.length);
2900 } else {
2901 zend_call_method_with_0_params(&ser->std, NULL, NULL, "readString", return_value);
2902 ION_CATCH();
2903 }
2904 if (ser->annotations.object_type) {
2905 goto unserialize_struct;
2906 }
2907 zend_hash_next_index_insert(ser->ids, return_value);
2908 return;
2909
2910 case tid_CLOB_INT:
2911 case tid_BLOB_INT:
2912 if (can_unserialize_fast(ser)) {
2913 php_ion_reader_read_lob(php_ion_obj(reader, ser->rdr)->reader, return_value);
2914 } else {
2915 zend_call_method_with_0_params(&ser->std, NULL, NULL, *typ == tid_BLOB ? "readBLob" : "readCLob", return_value);
2916 }
2917 ION_CATCH();
2918 if (ser->annotations.object_type) {
2919 goto unserialize_struct;
2920 }
2921 zend_hash_next_index_insert(ser->ids, return_value);
2922 return;
2923
2924 case tid_LIST_INT:
2925 case tid_SEXP_INT: // FIXME
2926 php_ion_unserialize_list(ser, return_value);
2927 ION_CATCH();
2928 if (!ser->annotations.object_type) {
2929 return;
2930 }
2931 /* fall through */
2932
2933 case tid_STRUCT_INT:
2934 unserialize_struct: ;
2935 php_ion_unserialize_struct(ser, return_value);
2936 return;
2937
2938 case tid_none_INT:
2939 ZEND_ASSERT(!"none");
2940 break;
2941
2942 case tid_DATAGRAM_INT:
2943 ZEND_ASSERT(!"datagram");
2944 case tid_EOF_INT:
2945 return;
2946 }
2947 }
2948
2949 #define php_ion_unserializer_copy(o,n)
2950 php_ion_decl(unserializer, Unserializer_Unserializer);
2951 #define clone_ion_Unserializer NULL
2952
2953 static void php_ion_unserialize_ex(php_ion_unserializer *ser, zval *return_value)
2954 {
2955 if (ser->multi_seq) {
2956 array_init(return_value);
2957 }
2958
2959 do {
2960 zval tmp;
2961 ZVAL_NULL(&tmp);
2962 php_ion_globals_unserializer_step();
2963 php_ion_unserialize_zval(ser, &tmp, NULL);
2964 php_ion_globals_unserializer_exit();
2965 ION_CATCH(zval_ptr_dtor(&tmp));
2966
2967 if (!ser->multi_seq) {
2968 RETURN_COPY_VALUE(&tmp);
2969 } else if (ser->type != tid_EOF) {
2970 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
2971 }
2972 } while (ser->type != tid_EOF);
2973 }
2974
2975 static php_ion_reader *create_unserialize_reader(zval *zdata)
2976 {
2977 ZVAL_DEREF(zdata);
2978
2979 if (Z_TYPE_P(zdata) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zdata), ce_Reader)) {
2980 Z_ADDREF_P(zdata);
2981 return php_ion_obj(reader, Z_OBJ_P(zdata));
2982 } else {
2983 zend_object *zo_reader = NULL;
2984 php_ion_reader *reader = NULL;
2985
2986 if (Z_TYPE_P(zdata) == IS_RESOURCE) {
2987 zo_reader = create_ion_Reader_Reader(ce_Reader_Stream_Reader);
2988 reader = php_ion_obj(reader, zo_reader);
2989 reader->type = STREAM_READER;
2990 php_stream_from_zval_no_verify(reader->stream.ptr, zdata);
2991 PTR_CHECK_RETURN(NULL, reader->stream.ptr, OBJ_RELEASE(zo_reader));
2992 GC_ADDREF(reader->stream.ptr->res);
2993 php_ion_reader_ctor(reader);
2994 } else if (Z_TYPE_P(zdata) <= IS_STRING) {
2995 zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader);
2996 reader = php_ion_obj(reader, zo_reader);
2997 reader->type = BUFFER_READER;
2998 reader->buffer = zval_get_string(zdata);
2999 ION_CATCH_RETURN(NULL, OBJ_RELEASE(zo_reader));
3000 php_ion_reader_ctor(reader);
3001 } else {
3002 zend_throw_exception_ex(ce_Exception, IERR_INVALID_ARG,
3003 "Invalid source to unserialize: expected string or resource, got %s",
3004 zend_zval_type_name(zdata));
3005 }
3006
3007 ION_CATCH_RETURN(NULL, if (zo_reader) {
3008 OBJ_RELEASE(zo_reader);
3009 });
3010 return reader;
3011 }
3012 }
3013
3014 void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_value)
3015 {
3016 zend_object *zo_ser = NULL;
3017
3018 if (!ser) {
3019 zo_ser = create_ion_Unserializer_Unserializer(NULL);
3020 ser = php_ion_obj(unserializer, zo_ser);
3021 PTR_CHECK(ser);
3022 ser->call_magic = true;
3023 php_ion_unserializer_ctor(ser);
3024 ION_CATCH();
3025 }
3026
3027 php_ion_reader *reader = create_unserialize_reader(zdata);
3028 if (reader) {
3029 ser->rdr = &reader->std;
3030 if (!EG(exception)) {
3031 php_ion_unserialize_ex(ser, return_value);
3032 }
3033 OBJ_RELEASE(&reader->std);
3034 ser->rdr = NULL;
3035 }
3036
3037 if (zo_ser) {
3038 OBJ_RELEASE(zo_ser);
3039 }
3040 }