2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
14 #include "ext/standard/php_var.h"
15 #include "ext/date/php_date.h"
17 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
20 #define PHP_ION_SYMBOL_TABLE_VERSION 1
21 #define PHP_ION_SYMBOL(c, s) { \
24 { sizeof(s)-1, (BYTE *) s }, \
28 { sizeof(c)-1, (BYTE *) c } \
31 typedef struct php_ion_global_symbol
{
34 } php_ion_global_symbol
;
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")
61 static ION_SYMBOL_TABLE
*g_sym_tab_php
;
63 /* [SID => STRING, STRING => SID] */
64 static HashTable g_sym_hash
;
65 /* [enum_case_name => SID] */
66 static HashTable g_sym_map
;
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); \
75 static void g_sym_hash_add(int sid
, const char *str
, size_t len
)
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
);
84 static void g_sym_map_add(int sid
, const char *str
, size_t len
)
88 zend_hash_str_add(&g_sym_map
, str
, len
, &zv
);
91 static int g_sym_init(void)
93 zend_hash_init(&g_sym_hash
, 0, NULL
, NULL
, 1);
94 zend_hash_init(&g_sym_map
, 0, NULL
, NULL
, 1);
96 g_sym_hash_add(0, ZEND_STRL(""));
97 g_sym_map_add(0, ZEND_STRL(""));
98 g_sym_add("Ion", ION
);
99 g_sym_add("Ivm_1_0", IVM
);
100 g_sym_add("IonSymbolTable", SYMBOL_TABLE
);
101 g_sym_add("Name", NAME
);
102 g_sym_add("Version", VERSION
);
103 g_sym_add("Imports", IMPORTS
);
104 g_sym_add("Symbols", SYMBOLS
);
105 g_sym_add("MaxId", MAX_ID
);
106 g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE
);
108 int sys_max_id
= ION_SYS_SID_SHARED_SYMBOL_TABLE
;
110 if (IERR_OK
!= ion_symbol_table_open_with_type(&g_sym_tab_php
, NULL
, ist_SHARED
)) {
113 php_ion_global_symbol
*ptr
= g_sym_tab_php_sym
;
114 ion_symbol_table_set_version(g_sym_tab_php
, PHP_ION_SYMBOL_TABLE_VERSION
);
115 ion_symbol_table_set_name(g_sym_tab_php
, &ptr
->s
.value
);
116 while (ptr
->e
.value
) {
117 ion_symbol_table_add_symbol(g_sym_tab_php
, &ptr
->s
.value
, &ptr
->s
.sid
);
118 g_sym_hash_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->s
.value
.value
, ptr
->s
.value
.length
);
119 g_sym_map_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->e
.value
, ptr
->e
.length
);
122 ion_symbol_table_lock(g_sym_tab_php
);
127 zend_string
*Year
, *Month
, *Day
, *Min
, *Sec
, *Frac
, *MinTZ
, *SecTZ
, *FracTZ
;
130 static void g_intern_str_init()
132 #define NEW_INTERN_STR(s) \
133 g_intern_str.s = zend_string_init_interned(#s, sizeof(#s)-1, 1)
134 NEW_INTERN_STR(Year
);
135 NEW_INTERN_STR(Month
);
139 NEW_INTERN_STR(Frac
);
140 NEW_INTERN_STR(MinTZ
);
141 NEW_INTERN_STR(SecTZ
);
142 NEW_INTERN_STR(FracTZ
);
143 #undef NEW_INTERN_STR
146 typedef struct php_ion_serializer
{
147 zend_string
*call_custom
;
148 zend_bool call_magic
;
155 zend_object
*wri
, std
;
157 } php_ion_serializer
;
159 typedef int (*php_ion_serialize_zval_cb
)(void *ctx
, zval
*zv
);
161 typedef struct php_ion_annotations
{
162 uint8_t shared_symtab
:1;
165 uint8_t object_prop
:1;
167 zend_string
*object_class
;
168 zend_string
*property_class
;
169 } php_ion_annotations
;
171 typedef struct php_ion_unserializer
{
172 zend_string
*call_custom
;
173 zend_bool call_magic
;
182 ION_TYPE type
; // FIXME: there's already `php_ion_obj(reader, rdr)->state`
183 php_ion_annotations annotations
;
185 zend_object
*rdr
, std
;
187 } php_ion_unserializer
;
189 ZEND_BEGIN_MODULE_GLOBALS(ion
)
193 ION_DECIMAL zend_max
;
194 ION_DECIMAL zend_min
;
201 php_ion_serializer serializer
;
202 php_ion_unserializer unserializer
;
205 HashTable serializer
[2];
206 HashTable unserializer
[3];
209 ZEND_END_MODULE_GLOBALS(ion
);
212 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
214 # define php_ion_globals ion_globals
217 ZEND_DECLARE_MODULE_GLOBALS(ion
);
219 static zend_class_entry
223 *ce_Decimal_Context_Rounding
,
231 *ce_Reader_Buffer_Reader
,
232 *ce_Reader_Stream_Reader
,
234 *ce_Serializer_Serializer
,
236 *ce_Symbol_ImportLocation
,
239 *ce_Symbol_Table_Local
,
240 *ce_Symbol_Table_PHP
,
241 *ce_Symbol_Table_Shared
,
242 *ce_Symbol_Table_System
,
244 *ce_Timestamp_Format
,
245 *ce_Timestamp_Precision
,
248 *ce_Unserializer_Unserializer
,
252 *ce_Writer_Buffer_Writer
,
254 *ce_Writer_Stream_Writer
,
258 static void php_ion_globals_symbols_init(void)
260 zend_hash_init(&php_ion_globals
.symbol
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
263 static void php_ion_globals_symbols_dtor(void)
265 zend_hash_destroy(&php_ion_globals
.symbol
.cache
);
268 static void php_ion_globals_serializer_init(void)
270 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
271 HashTable
*h
= php_ion_globals
._ht
.serializer
;
273 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
274 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
277 static uint32_t php_ion_globals_serializer_step(void)
279 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
282 if (!(level
= s
->level
++)) {
283 zend_hash_clean(s
->ids
);
284 zend_hash_clean(s
->tmp
);
289 static uint32_t php_ion_globals_serializer_exit(void)
291 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
293 ZEND_ASSERT(s
->level
);
295 zend_hash_clean(s
->ids
);
296 zend_hash_clean(s
->tmp
);
301 static void php_ion_globals_serializer_dtor(void)
303 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
305 zend_hash_destroy(s
->tmp
);
306 zend_hash_destroy(s
->ids
);
309 void ZVAL_ADDREF(zval
*zv
)
312 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
317 static void php_ion_globals_unserializer_init(void)
319 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
320 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
322 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
323 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
324 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
327 static void php_ion_globals_unserializer_step(void)
329 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
332 zend_hash_clean(s
->addref
);
333 zend_hash_clean(s
->ids
);
334 zend_hash_clean(s
->tmp
);
338 static void php_ion_globals_unserializer_exit(void)
340 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
342 ZEND_ASSERT(s
->level
);
344 zend_hash_clean(s
->addref
);
345 zend_hash_clean(s
->ids
);
346 zend_hash_clean(s
->tmp
);
350 static void php_ion_globals_unserializer_dtor(void)
352 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
354 zend_hash_destroy(s
->addref
);
355 zend_hash_destroy(s
->ids
);
356 zend_hash_destroy(s
->tmp
);
359 #define php_ion_obj(type, zo) \
360 ((php_ion_ ##type *) php_ion_obj_ex(zo, XtOffsetOf(php_ion_ ## type, std)))
361 static void *php_ion_obj_ex(void *obj
, ptrdiff_t offset
) {
363 return ((char *) obj
) - offset
;
368 #define php_ion_decl(type, cname) \
369 static zend_object_handlers oh_ ## cname; \
370 static zend_object *create_ion_ ## cname(zend_class_entry *ce) \
372 if (!ce) ce = ce_ ## cname; \
373 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
374 zend_object_std_init(&o->std, ce); \
375 object_properties_init(&o->std, ce); \
376 o->std.handlers = &oh_ ## cname; \
379 static void free_ion_ ## cname(zend_object *std) \
381 php_ion_ ## type *obj = php_ion_obj(type, std); \
382 php_ion_ ## type ## _dtor(obj); \
383 zend_object_std_dtor(std); \
385 static zend_object *clone_ion_ ## cname(zend_object *std) \
387 php_ion_ ## type *old_obj = php_ion_obj(type, std), \
388 *new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \
389 php_ion_ ## type ## _copy(new_obj, old_obj); \
391 return &new_obj->std; \
393 #define php_ion_register(type, cname, ...) do { \
394 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
395 ce_ ## cname ->create_object = create_ion_ ## cname; \
396 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
397 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
398 oh_ ## cname .free_obj = free_ion_ ## cname; \
399 oh_ ## cname .clone_obj = clone_ion_ ## cname; \
402 #define ION_CHECK_RETURN(r, err, ...) do { \
404 if (UNEXPECTED(__err)) { \
405 zend_throw_exception_ex(ce_Exception, __err, "%s: %s", ion_error_to_str(__err), #err); \
411 #define ION_CHECK(err, ...) \
412 ION_CHECK_RETURN(, err, __VA_ARGS__)
414 #define ION_CATCH_RETURN(ret, ...) do { \
415 if (UNEXPECTED(EG(exception))) { \
421 #define ION_CATCH(...) \
422 ION_CATCH_RETURN(, __VA_ARGS__)
424 #define PTR_CHECK_RETURN(ret, ptr, ...) do { \
425 if (UNEXPECTED(!(ptr))) { \
426 zend_throw_error(NULL, "Uninitialized object"); \
431 #define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
433 #define OBJ_CHECK_RETURN(ret, obj, ...) do { \
434 PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
435 PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
437 #define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
439 static ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
441 is
->length
= zs
? (SIZE
) zs
->len
: 0;
442 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
446 static zend_string
*zend_string_from_ion(const ION_STRING
*s
)
448 return zend_string_init((const char *) s
->value
, s
->length
, 0);
451 static void call_constructor(zend_object
*zo
, uint32_t argc
, zval
*argv
, zend_array
*args_named
)
453 zend_call_known_function(zo
->ce
->constructor
, zo
, zo
->ce
, NULL
, argc
, argv
, args_named
);
456 static zend_object
*object_construct(zend_class_entry
*ce
, uint32_t argc
, zval
*argv
, zend_array
*args_named
)
459 object_init_ex(&z_obj
, ce
);
460 call_constructor(Z_OBJ(z_obj
), argc
, argv
, args_named
);
464 static void update_property_obj_ex(zend_class_entry
*scope
, zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
468 zend_update_property(scope
, obj
, n
, l
, &zobj
);
470 static void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
472 update_property_obj_ex(obj
->ce
, obj
, n
, l
, p
);
475 #define RETURN_IONTYPE(typ) do { \
476 zend_object *__zo = php_ion_type_fetch(typ); \
477 if (UNEXPECTED(!__zo)) { \
480 RETURN_OBJ_COPY(__zo); \
483 static zend_object
*php_ion_type_fetch(ION_TYPE typ
)
485 zend_long index
= ION_TYPE_INT(typ
);
486 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
488 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
489 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
492 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
495 static ION_TYPE
ion_type_from_enum(zend_object
*zo
)
497 return (ION_TYPE
) Z_LVAL_P(zend_enum_fetch_case_value(zo
));
500 typedef struct php_ion_symbol_iloc
{
501 ION_SYMBOL_IMPORT_LOCATION loc
;
504 } php_ion_symbol_iloc
;
506 static void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
508 zend_update_property_long(ce_Symbol_ImportLocation
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
509 zend_update_property_str(ce_Symbol_ImportLocation
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
510 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
513 static void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
515 zend_string_release(obj
->name
);
518 static void php_ion_symbol_iloc_copy(php_ion_symbol_iloc
*new_obj
, php_ion_symbol_iloc
*old_obj
)
520 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
521 new_obj
->name
= zend_string_copy(old_obj
->name
);
522 ion_string_from_zend(&new_obj
->loc
.name
, new_obj
->name
);
523 new_obj
->loc
.location
= old_obj
->loc
.location
;
526 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
);
528 typedef struct php_ion_symbol
{
531 zend_object
*iloc
, std
;
534 static int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
535 zend_string
*zs1
= zval_get_string(zv1
);
536 zend_string
*zs2
= zval_get_string(zv2
);
543 if (zs1
->len
> zs2
->len
) {
545 } else if (zs2
->len
> zs1
->len
) {
548 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
550 zend_string_release(zs1
);
551 zend_string_release(zs2
);
555 static void php_ion_symbol_ctor(php_ion_symbol
*obj
)
557 zend_update_property_long(ce_Symbol
, &obj
->std
, ZEND_STRL("sid"),
560 zend_update_property_str(ce_Symbol
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
562 zend_update_property_null(ce_Symbol
, &obj
->std
, ZEND_STRL("value"));
564 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
566 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
567 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
569 zend_update_property_null(ce_Symbol
, &obj
->std
, ZEND_STRL("importLocation"));
573 static void php_ion_symbol_dtor(php_ion_symbol
*obj
)
576 zend_string_release(obj
->value
);
580 static void php_ion_symbol_copy(php_ion_symbol
*new_obj
, php_ion_symbol
*old_obj
)
582 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
583 new_obj
->sym
= old_obj
->sym
;
584 if (old_obj
->value
) {
585 new_obj
->value
= zend_string_copy(old_obj
->value
);
586 ion_string_from_zend(&new_obj
->sym
.value
, new_obj
->value
);
589 if ((new_obj
->iloc
= old_obj
->iloc
)) {
590 new_obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, new_obj
->iloc
)->loc
;
594 static void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
596 object_init_ex(return_value
, ce_Symbol
);
597 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
599 sym
->sym
.sid
= sym_ptr
->sid
;
600 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
601 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
603 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
604 sym
->iloc
= Z_OBJ(ziloc
);
606 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
607 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
608 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
610 php_ion_symbol_iloc_ctor(iloc
);
613 php_ion_symbol_ctor(sym
);
615 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
616 GC_DELREF(sym
->iloc
);
620 static zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
622 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
624 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
626 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
629 object_init_ex(&zsym
, ce_Symbol
);
630 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
631 sym
->sym
.sid
= Z_LVAL_P(zid
);
632 sym
->value
= zval_get_string(zss
);
633 php_ion_symbol_ctor(sym
);
634 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);
641 php_ion_decl(symbol
, Symbol
);
643 typedef struct php_ion_symbol_table
{
644 ION_SYMBOL_TABLE
*tab
;
645 int (*dtor
)(ION_SYMBOL_TABLE
*);
647 } php_ion_symbol_table
;
649 static void php_ion_symbol_table_ctor(php_ion_symbol_table
*obj
)
653 ION_SYMBOL_TABLE_TYPE typ
= ist_EMPTY
;
654 ion_symbol_table_get_type(obj
->tab
, &typ
);
655 if (typ
!= ist_LOCAL
) {
657 if (IERR_OK
== ion_symbol_table_get_name(obj
->tab
, &is
)) {
658 zend_update_property_stringl(ce_Symbol_Table_Shared
, &obj
->std
, ZEND_STRL("name"), (char *) is
.value
, is
.length
);
661 if (IERR_OK
== ion_symbol_table_get_version(obj
->tab
, &iv
)) {
662 zend_update_property_long(ce_Symbol_Table_Shared
, &obj
->std
, ZEND_STRL("version"), iv
);
667 static void php_ion_symbol_table_dtor(php_ion_symbol_table
*obj
)
677 static void php_ion_symbol_table_copy(php_ion_symbol_table
*new_obj
, php_ion_symbol_table
*old_obj
)
679 // do not clone cache members
680 // zend_objects_clone_members(...)
681 if ((new_obj
->dtor
= old_obj
->dtor
)) {
682 ION_CHECK(ion_symbol_table_clone_with_owner(old_obj
->tab
, &new_obj
->tab
, NULL
));
684 new_obj
->tab
= old_obj
->tab
;
686 // update non-cache members
687 php_ion_symbol_table_ctor(new_obj
);
690 static void php_ion_symbol_table_import(php_ion_symbol_table
*obj
, php_ion_symbol_table
*import
)
696 zval
*zimports
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), 0, &tmp
);
698 zend_ulong idx
= (uintptr_t) &import
->std
.gc
;
699 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports
), idx
)) {
700 ION_CHECK(ion_symbol_table_import_symbol_table(obj
->tab
, import
->tab
));
702 SEPARATE_ARRAY(zimports
);
703 GC_ADDREF(&import
->std
);
704 add_index_object(zimports
, idx
, &import
->std
);
705 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), zimports
);
710 static void php_ion_symbol_table_symbol_zval(php_ion_symbol_table
*obj
, ION_SYMBOL
*sym
, zval
*return_value
)
713 zval
*zsyms
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbols"), 0, &tmp
);
715 zval
*zsym
= zend_hash_index_find(Z_ARRVAL_P(zsyms
), sym
->sid
);
721 php_ion_symbol_zval(sym
, return_value
);
724 SEPARATE_ARRAY(zsyms
);
725 ZVAL_ADDREF(return_value
);
726 add_index_zval(zsyms
, sym
->sid
, return_value
);
730 php_ion_decl(symbol_table
, Symbol_Table
);
732 typedef struct php_ion_decimal_ctx
{
735 } php_ion_decimal_ctx
;
737 #define php_ion_decimal_ctx_init_max(c, rounding) \
738 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
739 static void php_ion_decimal_ctx_init(decContext
*ctx
,
740 int digits
, int emax
, int emin
, enum rounding round
, zend_bool clamp
)
742 memset(ctx
, 0, sizeof(*ctx
));
743 ctx
->digits
= digits
;
750 static void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
752 if (!obj
->ctx
.digits
) {
753 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
756 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
758 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
760 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
761 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
762 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
763 zend_update_property_bool(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
766 static void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx
*obj
)
770 static void php_ion_decimal_ctx_copy(php_ion_decimal_ctx
*new_obj
, php_ion_decimal_ctx
*old_obj
)
772 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
773 new_obj
->ctx
= old_obj
->ctx
;
776 php_ion_decl(decimal_ctx
, Decimal_Context
);
778 typedef struct php_ion_decimal
{
780 zend_object
*ctx
, std
;
783 static void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
785 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
786 ION_CHECK(ion_decimal_from_int32(dec
, num
));
787 } else if (num
> 0 && num
<= UINT32_MAX
) {
788 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
791 ION_CHECK(ion_int_alloc(NULL
, &iint
));
792 ION_CHECK(ion_int_from_long(iint
, num
),
794 /* WATCH OUT: BS API */
795 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
796 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
802 static zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
804 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
805 (void) ion_decimal_to_string(dec
, zstr
->val
);
806 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
809 static void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
812 ION_CHECK(ion_int_alloc(NULL
, &ii
));
813 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
815 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
820 static bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
824 if (!ion_decimal_is_integer(&obj
->dec
)) {
829 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_max
, &php_ion_globals
.decimal
.ctx
, &result
);
834 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_min
, &php_ion_globals
.decimal
.ctx
, &result
);
841 static void php_ion_decimal_ctor(php_ion_decimal
*obj
)
845 object_init_ex(&zdc
, ce_Decimal_Context
);
846 obj
->ctx
= Z_OBJ(zdc
);
847 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
850 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
852 if (php_ion_decimal_fits_zend_long(obj
)) {
854 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
855 zend_update_property_long(ce_Decimal
, &obj
->std
, ZEND_STRL("number"), l
);
857 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
858 zend_update_property_str(ce_Decimal
, &obj
->std
, ZEND_STRL("number"), zstr
);
859 zend_string_release(zstr
);
863 static void php_ion_decimal_dtor(php_ion_decimal
*obj
)
865 ion_decimal_free(&obj
->dec
);
868 static void php_ion_decimal_copy(php_ion_decimal
*new_obj
, php_ion_decimal
*old_obj
)
870 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
871 new_obj
->ctx
= old_obj
->ctx
;
872 ION_CHECK(ion_decimal_copy(&new_obj
->dec
, &old_obj
->dec
));
875 php_ion_decl(decimal
, Decimal
);
877 typedef php_date_obj php_ion_timestamp
;
879 static zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
882 ctx
= &php_ion_globals
.decimal
.ctx
;
884 decQuad microsecs
, result
;
885 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
886 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
889 static decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, int usec
, decContext
*ctx
)
892 ctx
= &php_ion_globals
.decimal
.ctx
;
894 decQuad microsecs
, us
;
895 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
898 static zend_string
*php_ion_timestamp_format_fetch(zend_string
*fmt_case
)
900 return Z_STR_P(zend_enum_fetch_case_value(zend_enum_get_case(ce_Timestamp_Format
, fmt_case
)));
903 static zend_string
*php_dt_format_from_precision(uint8_t precision
)
906 case ION_TS_FRAC
| 0x80:
907 return php_ion_timestamp_format_fetch(g_intern_str
.FracTZ
);
909 return php_ion_timestamp_format_fetch(g_intern_str
.Frac
);
910 case ION_TS_SEC
| 0x80:
911 return php_ion_timestamp_format_fetch(g_intern_str
.SecTZ
);
913 return php_ion_timestamp_format_fetch(g_intern_str
.Sec
);
914 case ION_TS_MIN
| 0x80:
915 return php_ion_timestamp_format_fetch(g_intern_str
.MinTZ
);
917 return php_ion_timestamp_format_fetch(g_intern_str
.Min
);
919 return php_ion_timestamp_format_fetch(g_intern_str
.Day
);
921 return php_ion_timestamp_format_fetch(g_intern_str
.Month
);
923 return php_ion_timestamp_format_fetch(g_intern_str
.Year
);
925 return ZSTR_CHAR('c');
929 static timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
931 timelib_time
*time
= ecalloc(1, sizeof(*time
));
938 switch (ts
->precision
& 0x7f) {
940 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
943 time
->s
= ts
->seconds
;
946 time
->i
= ts
->minutes
;
959 time
->z
= ts
->tz_offset
* 60;
960 if (time
->z
|| ts
->precision
& 0x80) {
961 time
->zone_type
= TIMELIB_ZONETYPE_OFFSET
;
963 time
->zone_type
= TIMELIB_ZONETYPE_ID
;
964 time
->tz_info
= get_timezone_info();
969 *fmt
= php_dt_format_from_precision(ts
->precision
);
974 static ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
976 memset(buf
, 0, sizeof(*buf
));
979 int precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
981 if (!precision
|| precision
> (ION_TS_FRAC
|0x80)) {
982 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
983 "Invalid precision (%d) of ion\\Timestamp", precision
);
984 } else switch ((buf
->precision
= precision
) & 0x7f) {
986 ion_ts_frac_from_usec(&buf
->fraction
, (int) ts
->time
->us
, ctx
);
989 buf
->seconds
= ts
->time
->s
;
992 buf
->minutes
= ts
->time
->i
;
995 buf
->hours
= ts
->time
->h
;
996 buf
->day
= ts
->time
->d
;
999 buf
->month
= ts
->time
->m
;
1002 buf
->year
= ts
->time
->y
;
1005 buf
->tz_offset
= (short) (ts
->time
->z
/ 60);
1006 if (buf
->tz_offset
) {
1007 buf
->precision
|= 0x80;
1014 static void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
1017 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
1019 zend_update_property_long(ce_Timestamp
, &obj
->std
, ZEND_STRL("precision"), precision
);
1021 fmt
= php_dt_format_from_precision(precision
);
1022 zend_update_property_str(ce_Timestamp
, &obj
->std
, ZEND_STRL("format"), fmt
);
1023 zend_string_release(fmt
);
1026 typedef struct php_ion_catalog
{
1031 static void php_ion_catalog_ctor(php_ion_catalog
*obj
)
1033 ION_CHECK(ion_catalog_open(&obj
->cat
));
1036 static void php_ion_catalog_dtor(php_ion_catalog
*obj
)
1039 ion_catalog_close(obj
->cat
);
1043 static ION_COLLECTION
*php_ion_catalog_collection(php_ion_catalog
*cat
)
1045 /* do not look too close */
1048 ION_SYMBOL_TABLE
*sys
;
1049 ION_COLLECTION collection
;
1050 } *cat_ptr
= (void *) cat
->cat
;
1051 return &cat_ptr
->collection
;
1053 // see https://github.com/amzn/ion-c/issues/269
1054 #ifndef IPCN_pNODE_TO_pDATA
1055 # define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0]))
1058 static void php_ion_catalog_copy(php_ion_catalog
*new_obj
, php_ion_catalog
*old_obj
)
1060 // do not clone cache members
1061 php_ion_catalog_ctor(new_obj
);
1064 ION_COLLECTION
*col
= php_ion_catalog_collection(old_obj
);
1065 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1066 ION_COLLECTION_CURSOR cur
;
1067 ION_COLLECTION_OPEN(col
, cur
);
1069 ION_SYMBOL_TABLE
**ptr
;
1070 ION_COLLECTION_NEXT(cur
, ptr
);
1072 ION_CHECK(ion_catalog_add_symbol_table(new_obj
->cat
, *ptr
));
1078 static zend_string
*ion_symbol_table_to_key(ION_SYMBOL_TABLE
*tab
)
1082 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_name(tab
, &is
));
1083 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_version(tab
, &version
));
1086 smart_str_appendl(&s
, (char *) is
.value
, is
.length
);
1087 smart_str_appendc(&s
, ':');
1088 smart_str_append_long(&s
, version
);
1094 static void php_ion_catalog_add_symbol_table(php_ion_catalog
*obj
, php_ion_symbol_table
*tab
)
1100 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1102 zend_ulong idx
= (uintptr_t) &tab
->std
.gc
;
1103 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs
), idx
)) {
1104 zend_string
*key
= ion_symbol_table_to_key(tab
->tab
);
1106 ION_CHECK(ion_catalog_add_symbol_table(obj
->cat
, tab
->tab
),
1107 zend_string_release(key
));
1108 SEPARATE_ARRAY(ztabs
);
1109 GC_ADDREF(&tab
->std
);
1110 add_index_object(ztabs
, idx
, &tab
->std
);
1111 GC_ADDREF(&tab
->std
);
1112 add_assoc_object_ex(ztabs
, key
->val
, key
->len
, &tab
->std
);
1113 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), ztabs
);
1114 zend_string_release(key
);
1120 static void php_ion_catalog_symbol_table_zval(php_ion_catalog
*obj
, ION_SYMBOL_TABLE
*tab
, zval
*return_value
)
1122 zend_string
*key
= ion_symbol_table_to_key(tab
);
1126 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1128 zval
*ztab
= zend_hash_find(Z_ARRVAL_P(ztabs
), key
);
1130 zend_string_release(key
);
1135 object_init_ex(return_value
, ce_Symbol_Table_Shared
);
1136 php_ion_symbol_table
*o_tab
= php_ion_obj(symbol_table
, Z_OBJ_P(return_value
));
1138 php_ion_symbol_table_ctor(o_tab
);
1141 SEPARATE_ARRAY(ztabs
);
1142 ZVAL_ADDREF(return_value
);
1143 add_index_zval(ztabs
, (uintptr_t) &o_tab
->std
.gc
, return_value
);
1144 ZVAL_ADDREF(return_value
);
1145 add_assoc_zval_ex(ztabs
, key
->val
, key
->len
, return_value
);
1147 zend_string_release(key
);
1150 php_ion_decl(catalog
, Catalog
);
1152 typedef struct php_ion_reader_options_ccn_ctx
{
1154 zend_fcall_info fci
;
1155 zend_fcall_info_cache fcc
;
1156 } php_ion_reader_options_ccn_ctx
;
1158 typedef struct php_ion_reader_options
{
1159 ION_READER_OPTIONS opt
;
1160 php_ion_reader_options_ccn_ctx ccn
;
1161 zend_object
*cat
, *dec_ctx
, *cb
, std
;
1162 } php_ion_reader_options
;
1164 static void php_ion_reader_options_dtor(php_ion_reader_options
*obj
)
1167 zend_fcall_info_args_clear(&obj
->ccn
.fci
, true);
1171 static void php_ion_reader_options_copy(php_ion_reader_options
*new_obj
, php_ion_reader_options
*old_obj
)
1173 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1175 new_obj
->opt
= old_obj
->opt
;
1176 new_obj
->cat
= old_obj
->cat
;
1177 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1178 new_obj
->cb
= old_obj
->cb
;
1181 ZVAL_OBJ(&zcb
, new_obj
->cb
);
1182 zend_fcall_info_init(&zcb
, 0, &new_obj
->ccn
.fci
, &new_obj
->ccn
.fcc
, NULL
, NULL
);
1183 new_obj
->opt
.context_change_notifier
.context
= &new_obj
->ccn
;
1187 php_ion_decl(reader_options
, Reader_Options
);
1189 typedef struct php_ion_reader
{
1197 zend_string
*buffer
;
1203 zend_object
*opt
, std
;
1206 static iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
1208 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
1209 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
1211 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
1212 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
1213 user
->limit
-= remaining
;
1216 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
1219 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
1220 if (EXPECTED(read
> 0)) {
1221 user
->limit
+= read
;
1225 if (EXPECTED(read
== 0)) {
1229 return IERR_READ_ERROR
;
1232 static iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1237 php_ion_reader_options_ccn_ctx
*ctx
= context
;
1240 ZVAL_OBJ(&zobj
, ctx
->obj
);
1241 zend_fcall_info_argn(&ctx
->fci
, 1, &zobj
);
1242 if (SUCCESS
!= zend_fcall_info_call(&ctx
->fci
, &ctx
->fcc
, NULL
, NULL
)) {
1243 e
= IERR_INTERNAL_ERROR
;
1245 zend_fcall_info_args_clear(&ctx
->fci
, false);
1250 static void php_ion_reader_ctor(php_ion_reader
*obj
)
1253 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1256 obj
->opt
= object_construct(ce_Reader_Options
, 0, NULL
, NULL
);
1257 update_property_obj_ex(ce_Reader_Reader
, &obj
->std
, ZEND_STRL("options"), obj
->opt
);
1258 OBJ_RELEASE(obj
->opt
);
1259 } else if (opt
->opt
.context_change_notifier
.context
) {
1260 php_ion_reader_options_ccn_ctx
*ctx
= opt
->opt
.context_change_notifier
.context
;
1261 ctx
->obj
= &obj
->std
;
1262 opt
->opt
.context_change_notifier
.notify
= on_context_change
;
1264 if (obj
->type
== STREAM_READER
) {
1265 PTR_CHECK(obj
->stream
.ptr
);
1266 GC_ADDREF(obj
->stream
.ptr
->res
);
1268 obj
->stream
.buf
.length
= opt
&& opt
->opt
.chunk_threshold
? opt
->opt
.chunk_threshold
: 0x4000;
1269 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1270 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1273 err
= ion_reader_open_buffer(&obj
->reader
, (BYTE
*) obj
->buffer
->val
, (SIZE
) obj
->buffer
->len
, opt
? &opt
->opt
: NULL
);
1280 static void php_ion_reader_dtor(php_ion_reader
*obj
)
1283 ion_reader_close(obj
->reader
);
1285 if (obj
->type
== STREAM_READER
) {
1286 if (obj
->stream
.buf
.value
) {
1287 efree(obj
->stream
.buf
.value
);
1289 if (obj
->stream
.ptr
) {
1290 zend_list_delete(obj
->stream
.ptr
->res
);
1294 zend_string_release(obj
->buffer
);
1299 #define php_ion_reader_copy(n,o)
1300 php_ion_decl(reader
, Reader_Reader
);
1301 #define clone_ion_Reader_Reader NULL
1303 typedef struct php_ion_writer_options
{
1304 ION_WRITER_OPTIONS opt
;
1305 zend_object
*cat
, *dec_ctx
, std
;
1306 } php_ion_writer_options
;
1308 static void php_ion_writer_options_copy(php_ion_writer_options
*new_obj
, php_ion_writer_options
*old_obj
)
1310 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1312 new_obj
->opt
= old_obj
->opt
;
1313 new_obj
->cat
= old_obj
->cat
;
1314 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1317 static void php_ion_writer_options_dtor(php_ion_writer_options
*obj
)
1321 php_ion_decl(writer_options
, Writer_Options
);
1323 static zend_object
*php_ion_writer_options_new(void)
1325 zend_object
*obj
= create_ion_Writer_Options(NULL
);
1326 zend_call_known_instance_method_with_0_params(obj
->ce
->constructor
, obj
, NULL
);
1330 typedef struct php_ion_writer
{
1339 struct _ion_user_stream
*usr
;
1346 zend_object
*opt
, std
;
1350 static iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1352 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1354 if (EXPECTED(user
->limit
&& user
->curr
)) {
1355 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1356 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1357 return IERR_WRITE_ERROR
;
1360 user
->curr
= writer
->stream
.buf
.value
;
1361 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1365 static void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1367 PTR_CHECK(obj
->stream
.ptr
);
1368 GC_ADDREF(obj
->stream
.ptr
->res
);
1370 obj
->stream
.buf
.length
= opt
? opt
->opt
.temp_buffer_size
: 0x1000;
1371 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1374 static void php_ion_writer_buffer_offer(php_ion_writer
*obj
)
1376 if (obj
->buffer
.usr
) {
1377 obj
->buffer
.usr
->curr
= (BYTE
*) &obj
->buffer
.str
.s
->val
[obj
->buffer
.str
.s
->len
];
1378 obj
->buffer
.usr
->limit
= obj
->buffer
.usr
->curr
+ obj
->buffer
.str
.a
- obj
->buffer
.str
.s
->len
;
1382 static void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1384 smart_str_alloc(&obj
->buffer
.str
, 0, false);
1385 php_ion_writer_buffer_offer(obj
);
1388 static void php_ion_writer_buffer_reset(php_ion_writer
*obj
)
1390 smart_str_free(&obj
->buffer
.str
);
1391 memset(&obj
->buffer
.str
, 0, sizeof(obj
->buffer
.str
));
1392 php_ion_writer_buffer_init(obj
);
1395 static zend_string
*php_ion_writer_buffer_copy(php_ion_writer
*obj
)
1397 if (obj
->buffer
.usr
) {
1398 // ensure that brain-dead ion_stream interface calls us again
1399 obj
->buffer
.usr
->curr
= NULL
;
1400 obj
->buffer
.usr
->limit
= NULL
;
1402 smart_str_0(&obj
->buffer
.str
);
1403 return zend_string_copy(obj
->buffer
.str
.s
);
1406 static void php_ion_writer_buffer_separate(php_ion_writer
*obj
, bool grow
)
1408 // see zend_string_separate and smart_str_erealloc
1409 zend_string
*old_str
= obj
->buffer
.str
.s
;
1410 zend_string
*new_str
= zend_string_alloc(obj
->buffer
.str
.a
<< grow
, false);
1411 memcpy(new_str
->val
, old_str
->val
, new_str
->len
= old_str
->len
);
1412 zend_string_release(old_str
);
1413 obj
->buffer
.str
.s
= new_str
;
1416 static void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1418 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
) {
1419 obj
->buffer
.str
.s
->len
= obj
->buffer
.usr
->curr
- (BYTE
*) obj
->buffer
.str
.s
->val
;
1421 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
&& obj
->buffer
.usr
->curr
== obj
->buffer
.usr
->limit
) {
1422 if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1423 php_ion_writer_buffer_separate(obj
, true);
1425 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1427 } else if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1428 php_ion_writer_buffer_separate(obj
, false);
1430 php_ion_writer_buffer_offer(obj
);
1434 static iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1436 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1437 writer
->buffer
.usr
= user
;
1438 php_ion_writer_buffer_grow(writer
);
1442 static void php_ion_writer_options_init_shared_imports(php_ion_writer_options
*opt
)
1444 php_ion_catalog
*cat
= php_ion_obj(catalog
, opt
->cat
);
1447 ION_CHECK(ion_writer_options_initialize_shared_imports(&opt
->opt
));
1449 ION_COLLECTION
*col
= php_ion_catalog_collection(cat
);
1450 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1451 // holy, nah, forget it batman...
1452 ION_COLLECTION_CURSOR cur
;
1453 ION_COLLECTION_OPEN(col
, cur
);
1455 ION_SYMBOL_TABLE
**ptr
;
1456 ION_COLLECTION_NEXT(cur
, ptr
);
1458 ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt
->opt
, ptr
, 1));
1464 static void php_ion_writer_ctor(php_ion_writer
*obj
)
1466 php_ion_writer_options
*opt
= NULL
;
1469 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1470 opt
= php_ion_obj(writer_options
, obj
->opt
);
1472 php_ion_writer_options_init_shared_imports(opt
);
1475 obj
->opt
= object_construct(ce_Writer_Options
, 0, NULL
, NULL
);
1476 update_property_obj_ex(ce_Writer_Options
, &obj
->std
, ZEND_STRL("options"), obj
->opt
);
1477 OBJ_RELEASE(obj
->opt
);
1480 ION_STREAM_HANDLER h
;
1481 if (obj
->type
== STREAM_WRITER
) {
1482 h
= php_ion_writer_stream_handler
;
1483 php_ion_writer_stream_init(obj
, opt
);
1485 h
= php_ion_writer_buffer_handler
;
1486 php_ion_writer_buffer_init(obj
);
1489 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1493 static void php_ion_writer_dtor(php_ion_writer
*obj
)
1496 ion_writer_close(obj
->writer
);
1499 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1501 ion_writer_options_close_shared_imports(&opt
->opt
);
1504 if (obj
->type
== STREAM_WRITER
) {
1505 if (obj
->stream
.buf
.value
) {
1506 efree(obj
->stream
.buf
.value
);
1508 if (obj
->stream
.ptr
) {
1509 zend_list_delete(obj
->stream
.ptr
->res
);
1512 if (obj
->buffer
.str
.s
) {
1513 smart_str_0(&obj
->buffer
.str
);
1514 zend_string_release(obj
->buffer
.str
.s
);
1519 #define php_ion_writer_copy(o,n)
1520 php_ion_decl(writer
, Writer_Writer
);
1521 #define clone_ion_Writer_Writer NULL
1523 static bool can_serialize_fast(php_ion_serializer
*ser
)
1525 if (ser
->wri
->ce
!= ce_Writer_Buffer_Writer
&& ser
->wri
->ce
!= ce_Writer_Stream_Writer
) {
1529 if (ser
->std
.ce
!= ce_Serializer_Serializer
) {
1536 static void php_ion_serializer_ctor(php_ion_serializer
*ser_obj
)
1538 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1539 ser_obj
->ids
= global_ser
->ids
;
1540 ser_obj
->tmp
= global_ser
->tmp
;
1542 zend_update_property_bool(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
1543 ser_obj
->multi_seq
);
1544 zend_update_property_bool(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1545 ser_obj
->call_magic
);
1546 if (ser_obj
->call_custom
) {
1547 zend_update_property_str(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1548 ser_obj
->call_custom
);
1549 ser_obj
->call_custom
= zend_string_tolower(ser_obj
->call_custom
);
1551 zend_update_property_null(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1555 static void php_ion_serializer_dtor(php_ion_serializer
*obj
)
1557 if (obj
->call_custom
) {
1558 zend_string_release(obj
->call_custom
);
1562 static void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1564 static void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool unmangle_props
, bool annotate_props
)
1566 if (can_serialize_fast(ser
)) {
1567 ION_CHECK(ion_writer_start_container(php_ion_obj(writer
, ser
->wri
)->writer
, tid_STRUCT
));
1570 ZVAL_OBJ(&z_type
, php_ion_type_fetch(tid_STRUCT
));
1571 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "startContainer", NULL
, &z_type
);
1577 zend_string
*k
= NULL
;
1578 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1579 char buf
[MAX_LENGTH_OF_LONG
+ 1];
1583 const char *class_name
, *prop_name
;
1584 if (unmangle_props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1585 if (annotate_props
) {
1586 if (can_serialize_fast(ser
)) {
1587 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_PROPERTY
));
1588 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
,
1589 ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1591 zval z_ann_prop
, z_ann_class
;
1592 ZVAL_CHAR(&z_ann_prop
, 'p');
1593 ZVAL_STRINGL(&z_ann_class
, class_name
, prop_name
- class_name
);
1594 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_prop
, &z_ann_class
);
1595 zval_ptr_dtor(&z_ann_class
);
1603 ion_string_assign_cstr(&is
, (char *) prop_name
, (SIZE
) prop_len
);
1605 char *end
= buf
+ sizeof(buf
) - 1;
1606 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1607 ion_string_assign_cstr(&is
, ptr
, (SIZE
) (end
- ptr
));
1610 if (can_serialize_fast(ser
)) {
1611 // WATCH OUT: field names need to be copied
1613 ION_CHECK(ion_string_copy_to_owner(php_ion_obj(writer
, ser
->wri
)->writer
, &fn
, &is
));
1614 ION_CHECK(ion_writer_write_field_name(php_ion_obj(writer
, ser
->wri
)->writer
, &fn
));
1617 ZVAL_STRINGL(&z_field_name
, (const char *) is
.value
, is
.length
);
1618 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeFieldName", NULL
, &z_field_name
);
1619 zval_ptr_dtor(&z_field_name
);
1623 php_ion_serialize_zval(ser
, v
);
1625 ZEND_HASH_FOREACH_END();
1627 if(can_serialize_fast(ser
)) {
1628 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer
, ser
->wri
)->writer
));
1630 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "finishContainer", NULL
);
1634 static void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1636 if (can_serialize_fast(ser
)) {
1637 ION_CHECK(ion_writer_start_container(php_ion_obj(writer
, ser
->wri
)->writer
, tid_LIST
));
1640 ZVAL_OBJ(&z_type
, php_ion_type_fetch(tid_LIST
));
1641 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "startContainer", NULL
, &z_type
);
1646 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1647 php_ion_serialize_zval(ser
, v
);
1649 ZEND_HASH_FOREACH_END();
1651 if (can_serialize_fast(ser
)) {
1652 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer
, ser
->wri
)->writer
));
1654 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "finishContainer", NULL
);
1658 static void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1664 ZVAL_OBJ(&tmp
, zobject
);
1665 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1666 if (can_serialize_fast(ser
)) {
1668 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_SERIALIZEABLE
));
1669 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1670 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer
, ser
->wri
)->writer
, buf
, len
));
1673 zval z_ann_srlzbl
, z_ann_class
, z_clob
;
1674 ZVAL_CHAR(&z_ann_srlzbl
, 'S');
1675 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1676 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_srlzbl
, &z_ann_class
);
1677 zval_ptr_dtor(&z_ann_class
);
1679 ZVAL_STRINGL(&z_clob
, (const char *) buf
, len
);
1680 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeCLob", NULL
, &z_clob
);
1681 zval_ptr_dtor(&z_clob
);
1683 } else if (!EG(exception
)){
1684 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1685 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1689 static void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1694 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1697 if (IS_ARRAY
== Z_TYPE(rv
)) {
1698 if (can_serialize_fast(ser
)) {
1700 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
));
1701 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1703 zval z_ann_cust
, z_ann_class
;
1704 ZVAL_CHAR(&z_ann_cust
, 'C');
1705 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1706 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_cust
, &z_ann_class
);
1707 zval_ptr_dtor(&z_ann_class
);
1709 if (!EG(exception
)) {
1710 php_ion_serialize_zval(ser
, &rv
);
1714 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1715 "%s serializer %s::%s did not return an array",
1716 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1717 fn
? fn
->common
.function_name
->val
: "__serialize");
1721 static void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1723 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1725 if (can_serialize_fast(ser
)) {
1727 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_ENUM
));
1728 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1729 ION_CHECK(ion_writer_write_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1731 zval z_ann_enm
, z_ann_class
;
1732 ZVAL_CHAR(&z_ann_enm
, 'E');
1733 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1734 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_enm
, &z_ann_class
);
1735 zval_ptr_dtor(&z_ann_class
);
1737 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeSymbol", NULL
, z_cname
);
1741 static void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1743 if (can_serialize_fast(ser
)) {
1746 if (zobject
->ce
!= zend_standard_class_def
) {
1747 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_CLASS_OBJECT
));
1748 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1750 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_OBJECT
));
1753 if (zobject
->ce
!= zend_standard_class_def
) {
1754 zval z_ann_cobj
, z_ann_class
;
1755 ZVAL_CHAR(&z_ann_cobj
, 'c');
1756 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1757 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_cobj
, &z_ann_class
);
1758 zval_ptr_dtor(&z_ann_class
);
1761 ZVAL_CHAR(&z_ann_obj
, 'o');
1762 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_obj
);
1768 ZVAL_OBJ(&zobj
, zobject
);
1769 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1771 php_ion_serialize_struct(ser
, props
, true, true);
1772 zend_release_properties(props
);
1774 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1775 "Could not get properties for serialization of class %s",
1776 zobject
->ce
->name
->val
);
1780 static void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1782 zval tmp_type
, *type
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_TYPE
), 0, &tmp_type
);
1783 zval tmp_value
, *value
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_VALUE
), 0, &tmp_value
);
1785 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1787 if (can_serialize_fast(ser
)) {
1788 ION_CHECK(ion_writer_write_blob(php_ion_obj(writer
, ser
->wri
)->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1790 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeBLob", NULL
, value
);
1794 if (can_serialize_fast(ser
)) {
1795 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer
, ser
->wri
)->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1797 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeCLob", NULL
, value
);
1801 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1802 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1807 static bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1809 return ce
->__serialize
&& ser
->call_magic
;
1812 static bool can_call_iface_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1815 return !!ce
->serialize
; // NOLINT
1818 static bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1820 if (ser
->call_custom
) {
1821 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
1826 static bool is_special_class(const zend_class_entry
*ce
, zend_class_entry
**target
) {
1827 #define IS_TARGET(_ce) \
1828 if (instanceof_function(ce, _ce)) { \
1832 IS_TARGET(ce_Symbol
);
1833 IS_TARGET(ce_Decimal
);
1834 IS_TARGET(ce_Timestamp
);
1840 static void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
) {
1842 zend_class_entry
*special_ce
, *ce
= zobject
->ce
;
1845 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1846 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1847 "Serializing %s is not allowed", ce
->name
->val
);
1851 if (can_call_magic_serialize(ser
, ce
)) {
1852 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1853 } else if (can_call_iface_serialize(ser
, ce
)) {
1854 php_ion_serialize_object_iface(ser
, zobject
);
1855 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1856 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1857 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1858 php_ion_serialize_object_enum(ser
, zobject
);
1859 } else if (!is_special_class(ce
, &special_ce
)) {
1860 php_ion_serialize_object_std(ser
, zobject
);
1862 if (can_serialize_fast(ser
) || special_ce
== ce_LOB
) {
1863 if (special_ce
== ce_Symbol
) {
1864 ION_CHECK(ion_writer_write_ion_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1865 } else if (special_ce
== ce_Decimal
) {
1866 ION_CHECK(ion_writer_write_ion_decimal(php_ion_obj(writer
, ser
->wri
)->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1867 } else if (special_ce
== ce_Timestamp
) {
1869 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1870 decContext
*ctx
= php_ion_obj(writer_options
, php_ion_obj(writer
, ser
->wri
)->opt
)->opt
.decimal_context
;
1871 ION_CHECK(ion_writer_write_timestamp(php_ion_obj(writer
, ser
->wri
)->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1873 assert(special_ce
== ce_LOB
);
1874 php_ion_serialize_object_lob(ser
, zobject
);
1878 const char *method
= NULL
;
1880 ZVAL_OBJ(&z_param
, zobject
);
1881 if (special_ce
== ce_Symbol
) {
1882 method
= "writeSymbol";
1883 } else if (special_ce
== ce_Decimal
) {
1884 method
= "writeDecimal";
1885 } else if (special_ce
== ce_Timestamp
) {
1886 method
= "writeTimestamp";
1890 zend_call_method(ser
->wri
, NULL
, NULL
, method
, strlen(method
), NULL
, 1, &z_param
, NULL
);
1895 static bool php_ion_serialize_system_value(php_ion_serializer
*ser
, zval
*zv
)
1897 if (1 == php_ion_globals
.serializer
.level
) {
1898 if (Z_TYPE_P(zv
) == IS_OBJECT
) {
1899 if (Z_OBJCE_P(zv
) == ce_Symbol_Table_Shared
) {
1900 php_ion_symbol_table
*obj
= php_ion_obj(symbol_table
, Z_OBJ_P(zv
));
1901 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj
->tab
, php_ion_obj(writer
, ser
->wri
)->writer
));
1909 static bool php_ion_serialize_backref(php_ion_serializer
*ser
, zval
*zv
)
1911 if (Z_TYPE_P(zv
) == IS_STRING
&& Z_STR_P(zv
) == zend_empty_string
) {
1914 if (Z_TYPE_P(zv
) == IS_ARRAY
&& Z_ARR_P(zv
) == &zend_empty_array
) {
1918 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1919 zval
*ref
= zend_hash_index_find(ser
->ids
, idx
);
1923 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1924 zend_hash_index_add(ser
->ids
, idx
, &num
);
1927 zend_hash_next_index_insert(ser
->tmp
, zv
);
1932 if (can_serialize_fast(ser
)) {
1933 ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_BACKREF
));
1934 ION_CHECK_RETURN(true, ion_writer_write_int64(php_ion_obj(writer
, ser
->wri
)->writer
, Z_LVAL_P(ref
)));
1937 ZVAL_CHAR(&z_ann_bref
, 'r');
1938 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_bref
);
1939 ION_CATCH_RETURN(true);
1940 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeInt", NULL
, ref
);
1945 static void php_ion_serialize_string(php_ion_serializer
*ser
, zend_string
*str
)
1947 if (can_serialize_fast(ser
)) {
1949 ION_CHECK(ion_writer_write_string(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, str
)));
1955 static void php_ion_serialize_reference(php_ion_serializer
*ser
, zval
*noref
)
1957 if (can_serialize_fast(ser
)) {
1958 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_REFERENCE
));
1961 ZVAL_CHAR(&z_ann_ref
, 'R');
1962 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_ref
);
1965 php_ion_serialize_zval(ser
, noref
);
1968 static void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1970 if (php_ion_serialize_system_value(ser
, zv
)) {
1973 if (php_ion_serialize_backref(ser
, zv
)) {
1977 switch (Z_TYPE_P(zv
)) {
1979 php_ion_serialize_string(ser
, Z_STR_P(zv
));
1983 if (zend_array_is_list(Z_ARRVAL_P(zv
))) {
1984 php_ion_serialize_list(ser
, Z_ARRVAL_P(zv
));
1986 php_ion_serialize_struct(ser
, Z_ARRVAL_P(zv
), false, false);
1991 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1995 php_ion_serialize_reference(ser
, Z_REFVAL_P(zv
));
2000 static void php_ion_serialize_scalar(php_ion_serializer
*ser
, zval
*zv
)
2002 if (can_serialize_fast(ser
)) {
2003 switch (Z_TYPE_P(zv
)) {
2005 ION_CHECK(ion_writer_write_null(php_ion_obj(writer
, ser
->wri
)->writer
));
2008 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer
, ser
->wri
)->writer
, TRUE
));
2011 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer
, ser
->wri
)->writer
, FALSE
));
2014 ION_CHECK(ion_writer_write_int64(php_ion_obj(writer
, ser
->wri
)->writer
, Z_LVAL_P(zv
)));
2017 ION_CHECK(ion_writer_write_double(php_ion_obj(writer
, ser
->wri
)->writer
, Z_DVAL_P(zv
)));
2021 if (Z_ISNULL_P(zv
)) {
2022 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "writeNull", NULL
);
2024 const char *method
= NULL
;
2026 if (Z_TYPE_P(zv
) < IS_LONG
) {
2027 method
= "writeBool";
2028 } else if (Z_TYPE_P(zv
) < IS_DOUBLE
) {
2029 method
= "writeInt";
2031 method
= "writeFloat";
2034 zend_call_method(ser
->wri
, NULL
, NULL
, method
, strlen(method
), NULL
, 1, zv
, NULL
);
2039 static void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
2043 switch (Z_TYPE_P(zv
)) {
2049 php_ion_serialize_scalar(ser
, zv
);
2055 php_ion_serialize_refcounted(ser
, zv
);
2058 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2059 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
2063 #define php_ion_serializer_copy(o,n)
2064 php_ion_decl(serializer
, Serializer_Serializer
);
2065 #define clone_ion_Serializer NULL
2067 static void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
2070 HashTable
*arr
= NULL
;
2072 if (ser
->multi_seq
) {
2073 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
2074 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2075 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
2079 arr
= Z_ARRVAL_P(zv
);
2081 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
2082 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
2086 php_ion_globals_serializer_step();
2087 php_ion_serialize_zval(ser
, zv
);
2088 php_ion_globals_serializer_exit();
2090 if (!ser
->multi_seq
) {
2093 zend_hash_move_forward_ex(arr
, &pos
);
2094 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
2098 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
2100 zend_object
*zo_ser
= NULL
, *zo_wri
= NULL
;
2103 zo_ser
= create_ion_Serializer_Serializer(NULL
);
2104 ser
= php_ion_obj(serializer
, zo_ser
);
2106 ser
->call_magic
= true;
2107 php_ion_serializer_ctor(ser
);
2111 if (!ser
->wri
|| !instanceof_function(ser
->wri
->ce
, ce_Writer
)) {
2112 zo_wri
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
2113 php_ion_writer
*wri
= php_ion_obj(writer
, zo_wri
);
2114 wri
->type
= BUFFER_WRITER
;
2115 if (ser
->wri
&& instanceof_function(ser
->wri
->ce
, ce_Writer_Options
)) {
2116 wri
->opt
= ser
->wri
;
2118 php_ion_writer_ctor(wri
);
2122 if (!EG(exception
)) {
2123 php_ion_serialize_ex(ser
, zv
);
2126 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
2127 if (can_serialize_fast(ser
)) {
2128 ion_writer_flush(php_ion_obj(writer
, ser
->wri
)->writer
, NULL
);
2130 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "flush", NULL
);
2132 RETVAL_STR_COPY(php_ion_obj(writer
, ser
->wri
)->buffer
.str
.s
);
2135 OBJ_RELEASE(zo_wri
);
2138 OBJ_RELEASE(zo_ser
);
2142 static bool can_unserialize_fast(php_ion_unserializer
*ser
)
2144 if (ser
->rdr
->ce
!= ce_Reader_Buffer_Reader
&& ser
->rdr
->ce
!= ce_Reader_Stream_Reader
) {
2148 if (ser
->std
.ce
!= ce_Unserializer_Unserializer
) {
2155 static void php_ion_unserializer_ctor(php_ion_unserializer
*ser_obj
)
2157 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
2158 ser_obj
->ids
= global_ser
->ids
;
2159 ser_obj
->tmp
= global_ser
->tmp
;
2160 ser_obj
->addref
= global_ser
->addref
;
2162 zend_update_property_bool(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
2163 ser_obj
->multi_seq
);
2164 zend_update_property_bool(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callMagicUnserialize"),
2165 ser_obj
->call_magic
);
2166 if (ser_obj
->call_custom
) {
2167 zend_update_property_str(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"),
2168 ser_obj
->call_custom
);
2169 ser_obj
->call_custom
= zend_string_tolower(ser_obj
->call_custom
);
2171 zend_update_property_null(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"));
2175 static void php_ion_unserializer_dtor(php_ion_unserializer
*obj
)
2177 if (obj
->call_custom
) {
2178 zend_string_release(obj
->call_custom
);
2182 static void php_ion_unserializer_next(php_ion_unserializer
*ser
, ION_TYPE
*typ
)
2184 if (can_unserialize_fast(ser
)) {
2185 ION_CHECK(ion_reader_next(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2187 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "next", NULL
);
2190 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "key", &z_type
);
2192 *typ
= ion_type_from_enum(Z_OBJ(z_type
));
2196 static void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
2198 static bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
2200 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
2203 static bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
2205 return (ce
&& ce
->unserialize
);
2208 static bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2210 if (ser
->call_custom
) {
2211 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
2216 static zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
2218 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2221 object_init_ex(return_value
, ce
);
2222 return zend_hash_next_index_insert(ser
->ids
, return_value
);
2225 zend_throw_exception_ex(ce_Exception
, IERR_IMPORT_NOT_FOUND
,
2226 "Could not find class %s", ser
->annotations
.object_class
->val
);
2230 static void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
2232 zend_string
*zs_case
= zval_get_string(return_value
);
2233 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2234 ZVAL_NULL(return_value
);
2237 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2238 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
2239 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2240 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
2243 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
2244 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2245 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
2248 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
2249 zend_hash_next_index_insert(ser
->ids
, return_value
);
2250 zend_string_release(zs_case
);
2253 static void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
2255 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2256 if (can_call_iface_unserialize(ser
, ce
)) {
2257 zend_string
*s
= zval_get_string(return_value
);
2258 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2259 ZVAL_NULL(return_value
);
2260 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
2261 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
2262 RETVAL_ZVAL(backref
, 0, 0);
2264 zval_ptr_dtor(backref
);
2266 if (!EG(exception
)) {
2267 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2268 "Failed to unserialize class %s", ce
->name
->val
);
2271 zend_string_release(s
);
2273 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2274 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
2278 static void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
, bool is_prop
)
2280 if (can_unserialize_fast(ser
)) {
2281 // FIXME: symbol table?
2283 SID sid
= UNKNOWN_SID
;
2284 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
2286 ION_CHECK(ion_reader_get_field_name(php_ion_obj(reader
, ser
->rdr
)->reader
, &name
));
2289 ION_CHECK(ion_reader_get_field_name_symbol(php_ion_obj(reader
, ser
->rdr
)->reader
, &is_ptr
));
2290 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
2291 ION_STRING_ASSIGN(&name
, &is_ptr
->value
);
2297 switch (name
.length
) {
2299 ptr
= zend_print_long_to_buf(end
, sid
);
2301 *key
= zend_string_init(ptr
, end
- ptr
, 0);
2304 *key
= ZSTR_CHAR(*name
.value
);
2308 *key
= zend_string_init_interned((char *) name
.value
, name
.length
, 0);
2310 *key
= zend_string_from_ion(&name
);
2317 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getFieldName", &z_fn
);
2318 if (!Z_STRLEN(z_fn
) || EG(exception
)) {
2319 zend_clear_exception();
2320 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getFieldNameSymbol", &z_fn
);
2324 *key
= zval_get_string(&z_fn
);
2325 zval_ptr_dtor(&z_fn
);
2329 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
2331 zend_hash_next_index_insert(ser
->ids
, return_value
);
2333 if (can_unserialize_fast(ser
)) {
2334 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2336 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2342 php_ion_unserializer_next(ser
, &typ
);
2345 if (typ
== tid_EOF
) {
2350 php_ion_unserialize_field_name(ser
, &key
, true);
2354 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2355 ION_CATCH(zend_string_release(key
));
2357 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
2358 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
2359 ce
= zend_lookup_class(ser
->annotations
.property_class
);
2361 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
2362 zval_ptr_dtor(&zvalue
);
2363 zend_string_release(key
);
2366 if (can_unserialize_fast(ser
)) {
2367 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2372 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
2373 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
2374 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
2375 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
2376 * nondeterministic behavior.
2378 static void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
2380 zend_hash_next_index_insert(ser
->ids
, return_value
);
2382 if (can_unserialize_fast(ser
)) {
2383 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2385 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2391 php_ion_unserializer_next(ser
, &typ
);
2394 if (typ
== tid_EOF
) {
2399 php_ion_unserialize_field_name(ser
, &key
, false);
2403 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2404 ION_CATCH(zend_string_release(key
));
2406 // FIXME:: too naive; b0rked if the previous value is an array
2407 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
2408 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
2409 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
2411 Z_TRY_ADDREF_P(prev
);
2412 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
2413 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
2415 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
2417 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
2419 zend_string_release(key
);
2422 if (can_unserialize_fast(ser
)) {
2423 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2427 static void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2429 switch (ser
->annotations
.object_type
) {
2435 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
2436 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2437 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
2442 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
2443 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2444 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
2446 *fn
= zobject
->ce
->__unserialize
;
2450 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2451 "Invalid object type %c", ser
->annotations
.object_type
);
2455 static void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
2457 // backup possible backref to array returned by magic/custom __serialize()
2458 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
2459 ZVAL_NULL(return_value
);
2460 php_ion_unserialize_class(ser
, return_value
);
2463 zend_function
*fn
= NULL
;
2464 zend_object
*zobject
= Z_OBJ_P(return_value
);
2465 verify_unserializer(ser
, zobject
, &fn
);
2470 php_ion_unserialize_props(ser
, return_value
);
2475 if (Z_TYPE_P(input
) != IS_ARRAY
) {
2476 zval_ptr_dtor(input
);
2478 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
2479 php_ion_unserialize_hash(ser
, input
);
2484 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
2488 static void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
2490 if (ser
->annotations
.object_class
) {
2491 switch (ser
->annotations
.object_type
) {
2493 php_ion_unserialize_object_iface(ser
, return_value
);
2496 php_ion_unserialize_object_enum(ser
, return_value
);
2499 php_ion_unserialize_object(ser
, return_value
);
2501 } else if (!ser
->annotations
.object_type
) {
2502 bool is_shared_symtab
= ser
->annotations
.shared_symtab
;
2503 array_init(return_value
);
2504 php_ion_unserialize_hash(ser
, return_value
);
2505 if (is_shared_symtab
) {
2506 zend_object
*zo_ss
= object_construct(ce_Symbol_Table_Shared
, 0, NULL
, Z_ARRVAL_P(return_value
));
2507 zval_ptr_dtor(return_value
);
2510 } else if (ser
->annotations
.object_type
== 'o') {
2511 object_init(return_value
);
2512 php_ion_unserialize_hash(ser
, return_value
);
2514 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2515 "Invalid object annotation %c::", ser
->annotations
.object_type
);
2519 static void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
2521 if (can_unserialize_fast(ser
)) {
2522 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2524 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2526 array_init(return_value
);
2527 zend_hash_next_index_insert(ser
->ids
, return_value
);
2531 php_ion_unserializer_next(ser
, &typ
);
2534 if (typ
== tid_EOF
) {
2539 php_ion_unserialize_zval(ser
, &next
, &typ
);
2542 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
2545 if (can_unserialize_fast(ser
)) {
2546 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2548 // userland reader object already stepped out
2552 static void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
2554 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
2557 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, (SIZE
) zstr
->len
, &read
);
2558 if (err
== IERR_BUFFER_TOO_SMALL
) {
2559 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
2562 ION_CHECK(err
, zend_string_release(zstr
));
2563 if (zstr
->len
> read
) {
2564 zstr
->val
[read
] = 0;
2565 zstr
= zend_string_truncate(zstr
, read
, 0);
2570 static void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
2573 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
2575 object_init_ex(return_value
, ce_Timestamp
);
2576 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
2578 zend_string
*fmt
= NULL
;
2579 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
2580 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
2581 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
2582 zend_string_release(fmt
);
2587 static void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
2589 ION_INT
*num
= NULL
;
2590 ION_CHECK(ion_int_alloc(reader
, &num
));
2591 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
2593 // TODO: SIZEOF_ZEND_LONG == 4
2595 iERR err
= ion_int_to_int64(num
, &i64
);
2601 case IERR_NUMERIC_OVERFLOW
: ;
2603 ION_CHECK(ion_int_char_length(num
, &max
));
2604 zend_string
*zs
= zend_string_alloc(max
, 0);
2606 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2607 zs
->val
[zs
->len
= len
] = 0;
2618 static void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2620 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2623 ZVAL_COPY_VALUE(return_value
, backref
);
2624 zend_hash_next_index_insert(ser
->addref
, return_value
);
2626 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2627 "Could not find back reference " ZEND_LONG_FMT
, Z_LVAL_P(return_value
));
2631 static zend_string
*php_ion_unserialize_annotation(php_ion_unserializer
*ser
, zend_long idx
)
2635 if (can_unserialize_fast(ser
)) {
2637 ION_CHECK_RETURN(NULL
, ion_reader_get_an_annotation(php_ion_obj(reader
, ser
->rdr
)->reader
, idx
, &ann_str
));
2639 if (ann_str
.length
== 1){
2640 // no need to remember interned string
2641 ZVAL_CHAR(&z_ann
, *ann_str
.value
);
2643 ZVAL_STRINGL(&z_ann
, (char *) ann_str
.value
, ann_str
.length
);
2644 zend_hash_next_index_insert(ser
->tmp
, &z_ann
);
2648 ZVAL_LONG(&z_idx
, idx
);
2649 zend_call_method_with_1_params(&ser
->std
, NULL
, NULL
, "getAnnotation", &z_ann
, &z_idx
);
2650 ION_CATCH_RETURN(NULL
);
2651 zend_hash_next_index_insert(ser
->tmp
, &z_ann
);
2654 return Z_STR(z_ann
);
2657 static void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2659 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2662 if (can_unserialize_fast(ser
)) {
2663 ION_CHECK(ion_reader_get_annotation_count(php_ion_obj(reader
, ser
->rdr
)->reader
, &ann_cnt
));
2666 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "countAnnotations", &z_ann_cnt
);
2668 ann_cnt
= Z_LVAL(z_ann_cnt
);
2671 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2672 zend_string
*zs_ann
= php_ion_unserialize_annotation(ser
, i
);
2674 if (zs_ann
->len
!= 1) {
2675 if (zend_string_equals_literal(zs_ann
, ION_SYS_SYMBOL_SHARED_SYMBOL_TABLE
)) {
2676 ser
->annotations
.shared_symtab
= true;
2682 switch (*zs_ann
->val
) {
2688 if (ser
->annotations
.makeref
) {
2689 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2690 "Invalid multiple reference annotations");
2693 ser
->annotations
.makeref
= true;
2697 if (ser
->annotations
.backref
) {
2698 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2699 "Invalid multiple back reference annotations");
2702 ser
->annotations
.backref
= true;
2706 if (ser
->annotations
.object_prop
) {
2707 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2708 "Invalid multiple object property annotations");
2711 ser
->annotations
.object_prop
= true;
2712 ser
->annotations
.property_class
= php_ion_unserialize_annotation(ser
, ++i
);
2722 if (ser
->annotations
.object_type
) {
2723 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2724 "Invalid multiple object type annotations: %c::%c",
2725 ser
->annotations
.object_type
, *zs_ann
->val
);
2729 ser
->annotations
.object_type
= *zs_ann
->val
;
2730 if (ser
->annotations
.object_type
!= 'o') {
2731 ser
->annotations
.object_class
= php_ion_unserialize_annotation(ser
, ++i
);
2738 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2739 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2740 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2743 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2744 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2745 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2751 static void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2754 memcpy(&ser
->type
, typ
, sizeof(ser
->type
)); // NOLINT
2757 if (can_unserialize_fast(ser
)) {
2758 ION_CHECK(ion_reader_next(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2760 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "next", NULL
);
2764 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getType", &z_type
);
2766 *typ
= ion_type_from_enum(Z_OBJ_P(return_value
));
2770 php_ion_unserialize_annotations(ser
);
2773 if (ser
->annotations
.makeref
) {
2774 ZVAL_MAKE_REF(return_value
);
2775 zend_hash_next_index_insert(ser
->ids
, return_value
);
2776 ZVAL_DEREF(return_value
);
2779 if (ION_TYPE_INT(*typ
) > 0) {
2781 if (can_unserialize_fast(ser
)) {
2782 ION_CHECK(ion_reader_is_null(php_ion_obj(reader
, ser
->rdr
)->reader
, &is_null
));
2785 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "isNull", &z_is_null
);
2786 is_null
= i_zend_is_true(&z_is_null
);
2793 switch (ION_TYPE_INT(*typ
)) {
2795 if (can_unserialize_fast(ser
)) {
2796 ION_CHECK(ion_reader_read_null(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2798 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readNull", return_value
);
2800 *typ
= ion_type_from_enum(Z_OBJ_P(return_value
));
2805 if (can_unserialize_fast(ser
)) {
2807 ION_CHECK(ion_reader_read_bool(php_ion_obj(reader
, ser
->rdr
)->reader
, &bval
));
2810 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readBool", return_value
);
2815 if (can_unserialize_fast(ser
)) {
2816 php_ion_reader_read_int(php_ion_obj(reader
, ser
->rdr
)->reader
, return_value
);
2818 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readInt", return_value
);
2820 if (ser
->annotations
.backref
) {
2822 php_ion_unserialize_backref(ser
, return_value
);
2824 if (ser
->annotations
.object_type
) {
2825 if (!ser
->annotations
.backref
) {
2826 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2827 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2828 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2831 goto unserialize_struct
;
2836 if (can_unserialize_fast(ser
)) {
2838 ION_CHECK(ion_reader_read_double(php_ion_obj(reader
, ser
->rdr
)->reader
, &d
));
2841 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readFloat", return_value
);
2845 case tid_DECIMAL_INT
:
2846 if (can_unserialize_fast(ser
)) {
2847 object_init_ex(return_value
, ce_Decimal
);
2848 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2849 ION_CHECK(ion_reader_read_ion_decimal(php_ion_obj(reader
, ser
->rdr
)->reader
, &dec
->dec
));
2850 php_ion_decimal_ctor(dec
);
2852 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readDecimal", return_value
);
2855 zend_hash_next_index_insert(ser
->ids
, return_value
);
2858 case tid_TIMESTAMP_INT
:
2859 if (can_unserialize_fast(ser
)) {
2860 php_ion_reader
*reader
= php_ion_obj(reader
, ser
->rdr
);
2861 php_ion_reader_read_timestamp(reader
->reader
, &php_ion_obj(reader_options
, reader
->opt
)->opt
, return_value
);
2863 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readTimestamp", return_value
);
2866 zend_hash_next_index_insert(ser
->ids
, return_value
);
2869 case tid_SYMBOL_INT
:
2870 if (can_unserialize_fast(ser
)) {
2872 ION_CHECK(ion_reader_read_ion_symbol(php_ion_obj(reader
, ser
->rdr
)->reader
, &sym
));
2873 php_ion_symbol_zval(&sym
, return_value
);
2875 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readSymbol", return_value
);
2878 if (ser
->annotations
.object_type
) {
2879 goto unserialize_struct
;
2881 zend_hash_next_index_insert(ser
->ids
, return_value
);
2884 case tid_STRING_INT
:
2885 if (can_unserialize_fast(ser
)) {
2887 ION_CHECK(ion_reader_read_string(php_ion_obj(reader
, ser
->rdr
)->reader
, &str
));
2888 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2890 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readString", return_value
);
2893 if (ser
->annotations
.object_type
) {
2894 goto unserialize_struct
;
2896 zend_hash_next_index_insert(ser
->ids
, return_value
);
2901 if (can_unserialize_fast(ser
)) {
2902 php_ion_reader_read_lob(php_ion_obj(reader
, ser
->rdr
)->reader
, return_value
);
2904 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, *typ
== tid_BLOB
? "readBLob" : "readCLob", return_value
);
2907 if (ser
->annotations
.object_type
) {
2908 goto unserialize_struct
;
2910 zend_hash_next_index_insert(ser
->ids
, return_value
);
2914 case tid_SEXP_INT
: // FIXME
2915 php_ion_unserialize_list(ser
, return_value
);
2917 if (!ser
->annotations
.object_type
) {
2922 case tid_STRUCT_INT
:
2923 unserialize_struct
: ;
2924 php_ion_unserialize_struct(ser
, return_value
);
2928 ZEND_ASSERT(!"none");
2931 case tid_DATAGRAM_INT
:
2932 ZEND_ASSERT(!"datagram");
2938 #define php_ion_unserializer_copy(o,n)
2939 php_ion_decl(unserializer
, Unserializer_Unserializer
);
2940 #define clone_ion_Unserializer NULL
2942 static void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2944 if (ser
->multi_seq
) {
2945 array_init(return_value
);
2951 php_ion_globals_unserializer_step();
2952 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2953 php_ion_globals_unserializer_exit();
2954 ION_CATCH(zval_ptr_dtor(&tmp
));
2956 if (!ser
->multi_seq
) {
2957 RETURN_COPY_VALUE(&tmp
);
2958 } else if (ser
->type
!= tid_EOF
) {
2959 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2961 } while (ser
->type
!= tid_EOF
);
2964 static php_ion_reader
*create_unserialize_reader(zval
*zdata
)
2966 zend_object
*zo_reader
;
2967 php_ion_reader
*reader
= NULL
;
2970 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2971 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2972 reader
= php_ion_obj(reader
, zo_reader
);
2973 reader
->type
= STREAM_READER
;
2974 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2975 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2976 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2977 reader
= php_ion_obj(reader
, zo_reader
);
2978 reader
->type
= BUFFER_READER
;
2979 reader
->buffer
= zval_get_string(zdata
);
2980 } else if (Z_TYPE_P(zdata
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(zdata
), ce_Reader
)) {
2982 reader
= php_ion_obj(reader
, Z_OBJ_P(zdata
));
2984 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2985 "Invalid source to unserialize: expected string or resource, got %s",
2986 zend_zval_type_name(zdata
));
2992 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
2994 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
2997 zo_ser
= create_ion_Unserializer_Unserializer(NULL
);
2998 ser
= php_ion_obj(unserializer
, zo_ser
);
3000 ser
->call_magic
= true;
3001 php_ion_unserializer_ctor(ser
);
3005 php_ion_reader
*reader
= create_unserialize_reader(zdata
);
3007 if (ser
->rdr
&& instanceof_function(ser
->rdr
->ce
, ce_Reader_Options
)) {
3008 zo_opt
= reader
->opt
= ser
->rdr
;
3010 php_ion_reader_ctor(reader
);
3011 ser
->rdr
= &reader
->std
;
3012 if (!EG(exception
)) {
3013 php_ion_unserialize_ex(ser
, return_value
);
3015 OBJ_RELEASE(&reader
->std
);
3019 OBJ_RELEASE(zo_opt
);
3022 OBJ_RELEASE(zo_ser
);