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 void g_sym_dtor(void)
93 ion_symbol_table_close(g_sym_tab_php
);
94 zend_hash_destroy(&g_sym_map
);
95 zend_hash_destroy(&g_sym_hash
);
98 static int g_sym_init(void)
100 zend_hash_init(&g_sym_hash
, 0, NULL
, NULL
, 1);
101 zend_hash_init(&g_sym_map
, 0, NULL
, NULL
, 1);
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
);
115 int sys_max_id
= ION_SYS_SID_SHARED_SYMBOL_TABLE
;
117 if (IERR_OK
!= ion_symbol_table_open_with_type(&g_sym_tab_php
, NULL
, ist_SHARED
)) {
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
);
129 ion_symbol_table_lock(g_sym_tab_php
);
134 zend_string
*Year
, *Month
, *Day
, *Min
, *Sec
, *Frac
, *MinTZ
, *SecTZ
, *FracTZ
;
137 static void g_intern_str_init()
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
);
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
153 typedef struct php_ion_serializer
{
154 zend_string
*call_custom
;
155 zend_bool call_magic
;
162 zend_object
*wri
, std
;
164 } php_ion_serializer
;
166 typedef int (*php_ion_serialize_zval_cb
)(void *ctx
, zval
*zv
);
168 typedef struct php_ion_annotations
{
169 uint8_t shared_symtab
:1;
172 uint8_t object_prop
:1;
174 zend_string
*object_class
;
175 zend_string
*property_class
;
176 } php_ion_annotations
;
178 typedef struct php_ion_unserializer
{
179 zend_string
*call_custom
;
180 zend_bool call_magic
;
189 ION_TYPE type
; // FIXME: there's already `php_ion_obj(reader, rdr)->state`
190 php_ion_annotations annotations
;
192 zend_object
*rdr
, std
;
194 } php_ion_unserializer
;
196 ZEND_BEGIN_MODULE_GLOBALS(ion
)
200 ION_DECIMAL zend_max
;
201 ION_DECIMAL zend_min
;
208 php_ion_serializer serializer
;
209 php_ion_unserializer unserializer
;
212 HashTable serializer
[2];
213 HashTable unserializer
[3];
216 ZEND_END_MODULE_GLOBALS(ion
);
219 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
221 # define php_ion_globals ion_globals
224 ZEND_DECLARE_MODULE_GLOBALS(ion
);
226 static zend_class_entry
230 *ce_Decimal_Context_Rounding
,
238 *ce_Reader_Buffer_Reader
,
239 *ce_Reader_Stream_Reader
,
241 *ce_Serializer_Serializer
,
243 *ce_Symbol_ImportLocation
,
246 *ce_Symbol_Table_Local
,
247 *ce_Symbol_Table_PHP
,
248 *ce_Symbol_Table_Shared
,
249 *ce_Symbol_Table_System
,
251 *ce_Timestamp_Format
,
252 *ce_Timestamp_Precision
,
255 *ce_Unserializer_Unserializer
,
259 *ce_Writer_Buffer_Writer
,
261 *ce_Writer_Stream_Writer
,
265 static void php_ion_globals_symbols_init(void)
267 zend_hash_init(&php_ion_globals
.symbol
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
270 static void php_ion_globals_symbols_dtor(void)
272 zend_hash_destroy(&php_ion_globals
.symbol
.cache
);
275 static void php_ion_globals_serializer_init(void)
277 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
278 HashTable
*h
= php_ion_globals
._ht
.serializer
;
280 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
281 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
284 static uint32_t php_ion_globals_serializer_step(void)
286 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
289 if (!(level
= s
->level
++)) {
290 zend_hash_clean(s
->ids
);
291 zend_hash_clean(s
->tmp
);
296 static uint32_t php_ion_globals_serializer_exit(void)
298 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
300 ZEND_ASSERT(s
->level
);
302 zend_hash_clean(s
->ids
);
303 zend_hash_clean(s
->tmp
);
308 static void php_ion_globals_serializer_dtor(void)
310 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
312 zend_hash_destroy(s
->tmp
);
313 zend_hash_destroy(s
->ids
);
316 void ZVAL_ADDREF(zval
*zv
)
319 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
324 static void php_ion_globals_unserializer_init(void)
326 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
327 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
329 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
330 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
331 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
334 static void php_ion_globals_unserializer_step(void)
336 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
339 zend_hash_clean(s
->addref
);
340 zend_hash_clean(s
->ids
);
341 zend_hash_clean(s
->tmp
);
345 static void php_ion_globals_unserializer_exit(void)
347 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
349 ZEND_ASSERT(s
->level
);
351 zend_hash_clean(s
->addref
);
352 zend_hash_clean(s
->ids
);
353 zend_hash_clean(s
->tmp
);
357 static void php_ion_globals_unserializer_dtor(void)
359 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
361 zend_hash_destroy(s
->addref
);
362 zend_hash_destroy(s
->ids
);
363 zend_hash_destroy(s
->tmp
);
366 #define php_ion_obj(type, zo) \
367 ((php_ion_ ##type *) php_ion_obj_ex(zo, XtOffsetOf(php_ion_ ## type, std)))
368 static void *php_ion_obj_ex(void *obj
, ptrdiff_t offset
) {
370 return ((char *) obj
) - offset
;
375 #define php_ion_decl_noclone(type, cname) \
376 static zend_object_handlers oh_ ## cname; \
377 static zend_object *create_ion_ ## cname(zend_class_entry *ce) \
379 if (!ce) ce = ce_ ## cname; \
380 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
381 zend_object_std_init(&o->std, ce); \
382 object_properties_init(&o->std, ce); \
383 o->std.handlers = &oh_ ## cname; \
386 static void free_ion_ ## cname(zend_object *std) \
388 php_ion_ ## type *obj = php_ion_obj(type, std); \
389 php_ion_ ## type ## _dtor(obj); \
390 zend_object_std_dtor(std); \
392 #define php_ion_decl(type, cname) \
393 php_ion_decl_noclone(type, cname) \
394 static zend_object *clone_ion_ ## cname(zend_object *std) \
396 php_ion_ ## type *old_obj = php_ion_obj(type, std), \
397 *new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \
398 php_ion_ ## type ## _copy(new_obj, old_obj); \
400 return &new_obj->std; \
402 #define php_ion_register(type, cname, ...) do { \
403 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
404 ce_ ## cname ->create_object = create_ion_ ## cname; \
405 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
406 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
407 oh_ ## cname .free_obj = free_ion_ ## cname; \
408 oh_ ## cname .clone_obj = clone_ion_ ## cname; \
411 #define ION_CHECK_RETURN(r, err, ...) do { \
413 if (UNEXPECTED(__err)) { \
414 zend_throw_exception_ex(ce_Exception, __err, "%s: %s", ion_error_to_str(__err), #err); \
420 #define ION_CHECK(err, ...) \
421 ION_CHECK_RETURN(, err, __VA_ARGS__)
423 #define ION_CATCH_RETURN(ret, ...) do { \
424 if (UNEXPECTED(EG(exception))) { \
430 #define ION_CATCH(...) \
431 ION_CATCH_RETURN(, __VA_ARGS__)
433 #define PTR_CHECK_RETURN(ret, ptr, ...) do { \
434 if (UNEXPECTED(!(ptr))) { \
435 zend_throw_error(NULL, "Uninitialized object"); \
440 #define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
442 #define OBJ_CHECK_RETURN(ret, obj, ...) do { \
443 PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
444 PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
446 #define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
448 static ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
450 is
->length
= zs
? (SIZE
) zs
->len
: 0;
451 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
455 static zend_string
*zend_string_from_ion(const ION_STRING
*s
)
457 return zend_string_init((const char *) s
->value
, s
->length
, 0);
460 static void call_constructor(zend_object
*zo
, uint32_t argc
, zval
*argv
, zend_array
*args_named
)
462 zend_call_known_function(zo
->ce
->constructor
, zo
, zo
->ce
, NULL
, argc
, argv
, args_named
);
465 static zend_object
*object_construct(zend_class_entry
*ce
, uint32_t argc
, zval
*argv
, zend_array
*args_named
)
468 object_init_ex(&z_obj
, ce
);
469 call_constructor(Z_OBJ(z_obj
), argc
, argv
, args_named
);
473 static void update_property_obj_ex(zend_class_entry
*scope
, zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
477 zend_update_property(scope
, obj
, n
, l
, &zobj
);
479 static void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
481 update_property_obj_ex(obj
->ce
, obj
, n
, l
, p
);
484 #define RETURN_IONTYPE(typ) do { \
485 zend_object *__zo = php_ion_type_fetch(typ); \
486 if (UNEXPECTED(!__zo)) { \
489 RETURN_OBJ_COPY(__zo); \
492 static zend_object
*php_ion_type_fetch(ION_TYPE typ
)
494 zend_long index
= ION_TYPE_INT(typ
);
495 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
497 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
498 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
501 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
504 static ION_TYPE
ion_type_from_enum(zend_object
*zo
)
506 return (ION_TYPE
) Z_LVAL_P(zend_enum_fetch_case_value(zo
));
509 typedef struct php_ion_symbol_iloc
{
510 ION_SYMBOL_IMPORT_LOCATION loc
;
513 } php_ion_symbol_iloc
;
515 static void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
517 zend_update_property_long(ce_Symbol_ImportLocation
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
518 zend_update_property_str(ce_Symbol_ImportLocation
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
519 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
522 static void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
524 zend_string_release(obj
->name
);
527 static void php_ion_symbol_iloc_copy(php_ion_symbol_iloc
*new_obj
, php_ion_symbol_iloc
*old_obj
)
529 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
530 new_obj
->name
= zend_string_copy(old_obj
->name
);
531 ion_string_from_zend(&new_obj
->loc
.name
, new_obj
->name
);
532 new_obj
->loc
.location
= old_obj
->loc
.location
;
535 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
);
537 typedef struct php_ion_symbol
{
540 zend_object
*iloc
, std
;
543 static int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
544 zend_string
*zs1
= zval_get_string(zv1
);
545 zend_string
*zs2
= zval_get_string(zv2
);
552 if (zs1
->len
> zs2
->len
) {
554 } else if (zs2
->len
> zs1
->len
) {
557 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
559 zend_string_release(zs1
);
560 zend_string_release(zs2
);
564 static void php_ion_symbol_ctor(php_ion_symbol
*obj
)
566 zend_update_property_long(ce_Symbol
, &obj
->std
, ZEND_STRL("sid"),
569 zend_update_property_str(ce_Symbol
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
571 zend_update_property_null(ce_Symbol
, &obj
->std
, ZEND_STRL("value"));
573 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
575 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
576 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
578 zend_update_property_null(ce_Symbol
, &obj
->std
, ZEND_STRL("importLocation"));
582 static void php_ion_symbol_dtor(php_ion_symbol
*obj
)
585 zend_string_release(obj
->value
);
589 static void php_ion_symbol_copy(php_ion_symbol
*new_obj
, php_ion_symbol
*old_obj
)
591 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
592 new_obj
->sym
= old_obj
->sym
;
593 if (old_obj
->value
) {
594 new_obj
->value
= zend_string_copy(old_obj
->value
);
595 ion_string_from_zend(&new_obj
->sym
.value
, new_obj
->value
);
598 if ((new_obj
->iloc
= old_obj
->iloc
)) {
599 new_obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, new_obj
->iloc
)->loc
;
603 static void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
605 object_init_ex(return_value
, ce_Symbol
);
606 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
608 sym
->sym
.sid
= sym_ptr
->sid
;
609 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
610 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
612 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
613 sym
->iloc
= Z_OBJ(ziloc
);
615 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
616 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
617 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
619 php_ion_symbol_iloc_ctor(iloc
);
622 php_ion_symbol_ctor(sym
);
624 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
625 GC_DELREF(sym
->iloc
);
629 static zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
631 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
633 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
635 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
638 object_init_ex(&zsym
, ce_Symbol
);
639 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
640 sym
->sym
.sid
= Z_LVAL_P(zid
);
641 sym
->value
= zval_get_string(zss
);
642 php_ion_symbol_ctor(sym
);
643 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);
650 php_ion_decl(symbol
, Symbol
);
652 typedef struct php_ion_symbol_table
{
653 ION_SYMBOL_TABLE
*tab
;
654 int (*dtor
)(ION_SYMBOL_TABLE
*);
656 } php_ion_symbol_table
;
658 static void php_ion_symbol_table_ctor(php_ion_symbol_table
*obj
)
662 ION_SYMBOL_TABLE_TYPE typ
= ist_EMPTY
;
663 ion_symbol_table_get_type(obj
->tab
, &typ
);
664 if (typ
!= ist_LOCAL
) {
666 if (IERR_OK
== ion_symbol_table_get_name(obj
->tab
, &is
)) {
667 zend_update_property_stringl(ce_Symbol_Table_Shared
, &obj
->std
, ZEND_STRL("name"), (char *) is
.value
, is
.length
);
670 if (IERR_OK
== ion_symbol_table_get_version(obj
->tab
, &iv
)) {
671 zend_update_property_long(ce_Symbol_Table_Shared
, &obj
->std
, ZEND_STRL("version"), iv
);
676 static void php_ion_symbol_table_dtor(php_ion_symbol_table
*obj
)
686 static void php_ion_symbol_table_copy(php_ion_symbol_table
*new_obj
, php_ion_symbol_table
*old_obj
)
688 // do not clone cache members
689 // zend_objects_clone_members(...)
690 if ((new_obj
->dtor
= old_obj
->dtor
)) {
691 ION_CHECK(ion_symbol_table_clone_with_owner(old_obj
->tab
, &new_obj
->tab
, NULL
));
693 new_obj
->tab
= old_obj
->tab
;
695 // update non-cache members
696 php_ion_symbol_table_ctor(new_obj
);
699 static void php_ion_symbol_table_import(php_ion_symbol_table
*obj
, php_ion_symbol_table
*import
)
705 zval
*zimports
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), 0, &tmp
);
707 zend_ulong idx
= (uintptr_t) &import
->std
.gc
;
708 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports
), idx
)) {
709 ION_CHECK(ion_symbol_table_import_symbol_table(obj
->tab
, import
->tab
));
711 SEPARATE_ARRAY(zimports
);
712 GC_ADDREF(&import
->std
);
713 add_index_object(zimports
, idx
, &import
->std
);
714 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), zimports
);
719 static void php_ion_symbol_table_symbol_zval(php_ion_symbol_table
*obj
, ION_SYMBOL
*sym
, zval
*return_value
)
722 zval
*zsyms
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbols"), 0, &tmp
);
724 zval
*zsym
= zend_hash_index_find(Z_ARRVAL_P(zsyms
), sym
->sid
);
730 php_ion_symbol_zval(sym
, return_value
);
733 SEPARATE_ARRAY(zsyms
);
734 ZVAL_ADDREF(return_value
);
735 add_index_zval(zsyms
, sym
->sid
, return_value
);
739 php_ion_decl(symbol_table
, Symbol_Table
);
741 typedef struct php_ion_decimal_ctx
{
744 } php_ion_decimal_ctx
;
746 #define php_ion_decimal_ctx_init_max(c, rounding) \
747 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
748 static void php_ion_decimal_ctx_init(decContext
*ctx
,
749 int digits
, int emax
, int emin
, enum rounding round
, zend_bool clamp
)
751 memset(ctx
, 0, sizeof(*ctx
));
752 ctx
->digits
= digits
;
759 static void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
761 if (!obj
->ctx
.digits
) {
762 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
765 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
767 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
769 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
770 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
771 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
772 zend_update_property_bool(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
775 static void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx
*obj
)
779 static void php_ion_decimal_ctx_copy(php_ion_decimal_ctx
*new_obj
, php_ion_decimal_ctx
*old_obj
)
781 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
782 new_obj
->ctx
= old_obj
->ctx
;
785 php_ion_decl(decimal_ctx
, Decimal_Context
);
787 typedef struct php_ion_decimal
{
789 zend_object
*ctx
, std
;
792 static void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
794 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
795 ION_CHECK(ion_decimal_from_int32(dec
, num
));
796 } else if (num
> 0 && num
<= UINT32_MAX
) {
797 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
800 ION_CHECK(ion_int_alloc(NULL
, &iint
));
801 ION_CHECK(ion_int_from_long(iint
, num
),
803 /* WATCH OUT: BS API */
804 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
805 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
811 static zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
813 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
814 (void) ion_decimal_to_string(dec
, zstr
->val
);
815 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
818 static void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
821 ION_CHECK(ion_int_alloc(NULL
, &ii
));
822 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
824 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
829 static bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
833 if (!ion_decimal_is_integer(&obj
->dec
)) {
838 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_max
, &php_ion_globals
.decimal
.ctx
, &result
);
843 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_min
, &php_ion_globals
.decimal
.ctx
, &result
);
850 static void php_ion_decimal_ctor(php_ion_decimal
*obj
)
854 object_init_ex(&zdc
, ce_Decimal_Context
);
855 obj
->ctx
= Z_OBJ(zdc
);
856 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
859 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
861 if (php_ion_decimal_fits_zend_long(obj
)) {
863 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
864 zend_update_property_long(ce_Decimal
, &obj
->std
, ZEND_STRL("number"), l
);
866 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
867 zend_update_property_str(ce_Decimal
, &obj
->std
, ZEND_STRL("number"), zstr
);
868 zend_string_release(zstr
);
872 static void php_ion_decimal_dtor(php_ion_decimal
*obj
)
874 ion_decimal_free(&obj
->dec
);
877 static void php_ion_decimal_copy(php_ion_decimal
*new_obj
, php_ion_decimal
*old_obj
)
879 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
880 new_obj
->ctx
= old_obj
->ctx
;
881 ION_CHECK(ion_decimal_copy(&new_obj
->dec
, &old_obj
->dec
));
884 php_ion_decl(decimal
, Decimal
);
886 typedef php_date_obj php_ion_timestamp
;
888 static zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
891 ctx
= &php_ion_globals
.decimal
.ctx
;
893 decQuad microsecs
, result
;
894 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
895 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
898 static decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, int usec
, decContext
*ctx
)
901 ctx
= &php_ion_globals
.decimal
.ctx
;
903 decQuad microsecs
, us
;
904 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
907 static zend_string
*php_ion_timestamp_format_fetch(zend_string
*fmt_case
)
909 return Z_STR_P(zend_enum_fetch_case_value(zend_enum_get_case(ce_Timestamp_Format
, fmt_case
)));
912 static zend_string
*php_dt_format_from_precision(uint8_t precision
)
915 case ION_TS_FRAC
| 0x80:
916 return php_ion_timestamp_format_fetch(g_intern_str
.FracTZ
);
918 return php_ion_timestamp_format_fetch(g_intern_str
.Frac
);
919 case ION_TS_SEC
| 0x80:
920 return php_ion_timestamp_format_fetch(g_intern_str
.SecTZ
);
922 return php_ion_timestamp_format_fetch(g_intern_str
.Sec
);
923 case ION_TS_MIN
| 0x80:
924 return php_ion_timestamp_format_fetch(g_intern_str
.MinTZ
);
926 return php_ion_timestamp_format_fetch(g_intern_str
.Min
);
928 return php_ion_timestamp_format_fetch(g_intern_str
.Day
);
930 return php_ion_timestamp_format_fetch(g_intern_str
.Month
);
932 return php_ion_timestamp_format_fetch(g_intern_str
.Year
);
934 return ZSTR_CHAR('c');
938 static timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
940 timelib_time
*time
= ecalloc(1, sizeof(*time
));
947 switch (ts
->precision
& 0x7f) {
949 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
952 time
->s
= ts
->seconds
;
955 time
->i
= ts
->minutes
;
968 time
->z
= ts
->tz_offset
* 60;
969 if (time
->z
|| ts
->precision
& 0x80) {
970 time
->zone_type
= TIMELIB_ZONETYPE_OFFSET
;
972 time
->zone_type
= TIMELIB_ZONETYPE_ID
;
973 time
->tz_info
= get_timezone_info();
978 *fmt
= php_dt_format_from_precision(ts
->precision
);
983 static ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
985 memset(buf
, 0, sizeof(*buf
));
988 int precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
990 if (!precision
|| precision
> (ION_TS_FRAC
|0x80)) {
991 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
992 "Invalid precision (%d) of ion\\Timestamp", precision
);
993 } else switch ((buf
->precision
= precision
) & 0x7f) {
995 ion_ts_frac_from_usec(&buf
->fraction
, (int) ts
->time
->us
, ctx
);
998 buf
->seconds
= ts
->time
->s
;
1001 buf
->minutes
= ts
->time
->i
;
1004 buf
->hours
= ts
->time
->h
;
1005 buf
->day
= ts
->time
->d
;
1008 buf
->month
= ts
->time
->m
;
1011 buf
->year
= ts
->time
->y
;
1014 buf
->tz_offset
= (short) (ts
->time
->z
/ 60);
1015 if (buf
->tz_offset
) {
1016 buf
->precision
|= 0x80;
1023 static void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
1026 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
1028 zend_update_property_long(ce_Timestamp
, &obj
->std
, ZEND_STRL("precision"), precision
);
1030 fmt
= php_dt_format_from_precision(precision
);
1031 zend_update_property_str(ce_Timestamp
, &obj
->std
, ZEND_STRL("format"), fmt
);
1032 zend_string_release(fmt
);
1035 typedef struct php_ion_catalog
{
1040 static void php_ion_catalog_ctor(php_ion_catalog
*obj
)
1042 ION_CHECK(ion_catalog_open(&obj
->cat
));
1045 static void php_ion_catalog_dtor(php_ion_catalog
*obj
)
1048 ion_catalog_close(obj
->cat
);
1052 static ION_COLLECTION
*php_ion_catalog_collection(php_ion_catalog
*cat
)
1054 /* do not look too close */
1057 ION_SYMBOL_TABLE
*sys
;
1058 ION_COLLECTION collection
;
1059 } *cat_ptr
= (void *) cat
->cat
;
1060 return &cat_ptr
->collection
;
1062 // see https://github.com/amzn/ion-c/issues/269
1063 #ifndef IPCN_pNODE_TO_pDATA
1064 # define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0]))
1067 static void php_ion_catalog_copy(php_ion_catalog
*new_obj
, php_ion_catalog
*old_obj
)
1069 // do not clone cache members
1070 php_ion_catalog_ctor(new_obj
);
1073 ION_COLLECTION
*col
= php_ion_catalog_collection(old_obj
);
1074 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1075 ION_COLLECTION_CURSOR cur
;
1076 ION_COLLECTION_OPEN(col
, cur
);
1078 ION_SYMBOL_TABLE
**ptr
;
1079 ION_COLLECTION_NEXT(cur
, ptr
);
1081 ION_CHECK(ion_catalog_add_symbol_table(new_obj
->cat
, *ptr
));
1087 static zend_string
*ion_symbol_table_to_key(ION_SYMBOL_TABLE
*tab
)
1091 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_name(tab
, &is
));
1092 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_version(tab
, &version
));
1095 smart_str_appendl(&s
, (char *) is
.value
, is
.length
);
1096 smart_str_appendc(&s
, ':');
1097 smart_str_append_long(&s
, version
);
1103 static void php_ion_catalog_add_symbol_table(php_ion_catalog
*obj
, php_ion_symbol_table
*tab
)
1109 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1111 zend_ulong idx
= (uintptr_t) &tab
->std
.gc
;
1112 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs
), idx
)) {
1113 zend_string
*key
= ion_symbol_table_to_key(tab
->tab
);
1115 ION_CHECK(ion_catalog_add_symbol_table(obj
->cat
, tab
->tab
),
1116 zend_string_release(key
));
1117 SEPARATE_ARRAY(ztabs
);
1118 GC_ADDREF(&tab
->std
);
1119 add_index_object(ztabs
, idx
, &tab
->std
);
1120 GC_ADDREF(&tab
->std
);
1121 add_assoc_object_ex(ztabs
, key
->val
, key
->len
, &tab
->std
);
1122 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), ztabs
);
1123 zend_string_release(key
);
1129 static void php_ion_catalog_symbol_table_zval(php_ion_catalog
*obj
, ION_SYMBOL_TABLE
*tab
, zval
*return_value
)
1131 zend_string
*key
= ion_symbol_table_to_key(tab
);
1135 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1137 zval
*ztab
= zend_hash_find(Z_ARRVAL_P(ztabs
), key
);
1139 zend_string_release(key
);
1144 object_init_ex(return_value
, ce_Symbol_Table_Shared
);
1145 php_ion_symbol_table
*o_tab
= php_ion_obj(symbol_table
, Z_OBJ_P(return_value
));
1147 php_ion_symbol_table_ctor(o_tab
);
1150 SEPARATE_ARRAY(ztabs
);
1151 ZVAL_ADDREF(return_value
);
1152 add_index_zval(ztabs
, (uintptr_t) &o_tab
->std
.gc
, return_value
);
1153 ZVAL_ADDREF(return_value
);
1154 add_assoc_zval_ex(ztabs
, key
->val
, key
->len
, return_value
);
1156 zend_string_release(key
);
1159 php_ion_decl(catalog
, Catalog
);
1161 typedef struct php_ion_reader_options_ccn_ctx
{
1163 zend_fcall_info fci
;
1164 zend_fcall_info_cache fcc
;
1165 } php_ion_reader_options_ccn_ctx
;
1167 typedef struct php_ion_reader_options
{
1168 ION_READER_OPTIONS opt
;
1169 php_ion_reader_options_ccn_ctx ccn
;
1170 zend_object
*cat
, *dec_ctx
, *cb
, std
;
1171 } php_ion_reader_options
;
1173 static void php_ion_reader_options_dtor(php_ion_reader_options
*obj
)
1176 zend_fcall_info_args_clear(&obj
->ccn
.fci
, true);
1180 static void php_ion_reader_options_copy(php_ion_reader_options
*new_obj
, php_ion_reader_options
*old_obj
)
1182 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1184 new_obj
->opt
= old_obj
->opt
;
1185 new_obj
->cat
= old_obj
->cat
;
1186 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1187 new_obj
->cb
= old_obj
->cb
;
1190 ZVAL_OBJ(&zcb
, new_obj
->cb
);
1191 zend_fcall_info_init(&zcb
, 0, &new_obj
->ccn
.fci
, &new_obj
->ccn
.fcc
, NULL
, NULL
);
1192 new_obj
->opt
.context_change_notifier
.context
= &new_obj
->ccn
;
1196 php_ion_decl(reader_options
, Reader_Options
);
1198 typedef struct php_ion_reader
{
1206 zend_string
*buffer
;
1212 zend_object
*opt
, std
;
1215 static iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
1217 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
1218 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
1220 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
1221 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
1222 user
->limit
-= remaining
;
1225 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
1228 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
1229 if (EXPECTED(read
> 0)) {
1230 user
->limit
+= read
;
1234 if (EXPECTED(read
== 0)) {
1238 return IERR_READ_ERROR
;
1241 static iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1246 php_ion_reader_options_ccn_ctx
*ctx
= context
;
1249 ZVAL_OBJ(&zobj
, ctx
->obj
);
1250 zend_fcall_info_argn(&ctx
->fci
, 1, &zobj
);
1251 if (SUCCESS
!= zend_fcall_info_call(&ctx
->fci
, &ctx
->fcc
, NULL
, NULL
)) {
1252 e
= IERR_INTERNAL_ERROR
;
1254 zend_fcall_info_args_clear(&ctx
->fci
, false);
1259 static void php_ion_reader_ctor(php_ion_reader
*obj
)
1262 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1265 obj
->opt
= object_construct(ce_Reader_Options
, 0, NULL
, NULL
);
1266 update_property_obj_ex(ce_Reader_Reader
, &obj
->std
, ZEND_STRL("options"), obj
->opt
);
1267 OBJ_RELEASE(obj
->opt
);
1268 } else if (opt
->opt
.context_change_notifier
.context
) {
1269 php_ion_reader_options_ccn_ctx
*ctx
= opt
->opt
.context_change_notifier
.context
;
1270 ctx
->obj
= &obj
->std
;
1271 opt
->opt
.context_change_notifier
.notify
= on_context_change
;
1273 if (obj
->type
== STREAM_READER
) {
1274 PTR_CHECK(obj
->stream
.ptr
);
1275 GC_ADDREF(obj
->stream
.ptr
->res
);
1277 obj
->stream
.buf
.length
= opt
&& opt
->opt
.chunk_threshold
? opt
->opt
.chunk_threshold
: 0x4000;
1278 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1279 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1282 err
= ion_reader_open_buffer(&obj
->reader
, (BYTE
*) obj
->buffer
->val
, (SIZE
) obj
->buffer
->len
, opt
? &opt
->opt
: NULL
);
1289 static void php_ion_reader_dtor(php_ion_reader
*obj
)
1292 ion_reader_close(obj
->reader
);
1294 if (obj
->type
== STREAM_READER
) {
1295 if (obj
->stream
.buf
.value
) {
1296 efree(obj
->stream
.buf
.value
);
1298 if (obj
->stream
.ptr
) {
1299 zend_list_delete(obj
->stream
.ptr
->res
);
1303 zend_string_release(obj
->buffer
);
1308 #define php_ion_reader_copy(n,o)
1309 php_ion_decl_noclone(reader
, Reader_Reader
);
1310 #define clone_ion_Reader_Reader NULL
1312 typedef struct php_ion_writer_options
{
1313 ION_WRITER_OPTIONS opt
;
1314 zend_object
*cat
, *dec_ctx
, std
;
1315 } php_ion_writer_options
;
1317 static void php_ion_writer_options_copy(php_ion_writer_options
*new_obj
, php_ion_writer_options
*old_obj
)
1319 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1321 new_obj
->opt
= old_obj
->opt
;
1322 new_obj
->cat
= old_obj
->cat
;
1323 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1326 static void php_ion_writer_options_dtor(php_ion_writer_options
*obj
)
1330 php_ion_decl(writer_options
, Writer_Options
);
1332 typedef struct php_ion_writer
{
1341 struct _ion_user_stream
*usr
;
1348 zend_object
*opt
, std
;
1352 static iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1354 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1356 if (EXPECTED(user
->limit
&& user
->curr
)) {
1357 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1358 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1359 return IERR_WRITE_ERROR
;
1362 user
->curr
= writer
->stream
.buf
.value
;
1363 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1367 static void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1369 PTR_CHECK(obj
->stream
.ptr
);
1370 GC_ADDREF(obj
->stream
.ptr
->res
);
1372 obj
->stream
.buf
.length
= opt
? opt
->opt
.temp_buffer_size
: 0x1000;
1373 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1376 static void php_ion_writer_buffer_offer(php_ion_writer
*obj
)
1378 if (obj
->buffer
.usr
) {
1379 obj
->buffer
.usr
->curr
= (BYTE
*) &obj
->buffer
.str
.s
->val
[obj
->buffer
.str
.s
->len
];
1380 obj
->buffer
.usr
->limit
= obj
->buffer
.usr
->curr
+ obj
->buffer
.str
.a
- obj
->buffer
.str
.s
->len
;
1384 static void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1386 smart_str_alloc(&obj
->buffer
.str
, 0, false);
1387 php_ion_writer_buffer_offer(obj
);
1390 static void php_ion_writer_buffer_reset(php_ion_writer
*obj
)
1392 smart_str_free(&obj
->buffer
.str
);
1393 memset(&obj
->buffer
.str
, 0, sizeof(obj
->buffer
.str
));
1394 php_ion_writer_buffer_init(obj
);
1397 static zend_string
*php_ion_writer_buffer_copy(php_ion_writer
*obj
)
1399 if (obj
->buffer
.usr
) {
1400 // ensure that brain-dead ion_stream interface calls us again
1401 obj
->buffer
.usr
->curr
= NULL
;
1402 obj
->buffer
.usr
->limit
= NULL
;
1404 smart_str_0(&obj
->buffer
.str
);
1405 return zend_string_copy(obj
->buffer
.str
.s
);
1408 static void php_ion_writer_buffer_separate(php_ion_writer
*obj
, bool grow
)
1410 // see zend_string_separate and smart_str_erealloc
1411 zend_string
*old_str
= obj
->buffer
.str
.s
;
1412 zend_string
*new_str
= zend_string_alloc(obj
->buffer
.str
.a
<< grow
, false);
1413 memcpy(new_str
->val
, old_str
->val
, new_str
->len
= old_str
->len
);
1414 zend_string_release(old_str
);
1415 obj
->buffer
.str
.s
= new_str
;
1418 static void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1420 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
) {
1421 obj
->buffer
.str
.s
->len
= obj
->buffer
.usr
->curr
- (BYTE
*) obj
->buffer
.str
.s
->val
;
1423 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
&& obj
->buffer
.usr
->curr
== obj
->buffer
.usr
->limit
) {
1424 if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1425 php_ion_writer_buffer_separate(obj
, true);
1427 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1429 } else if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1430 php_ion_writer_buffer_separate(obj
, false);
1432 php_ion_writer_buffer_offer(obj
);
1436 static iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1438 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1439 writer
->buffer
.usr
= user
;
1440 php_ion_writer_buffer_grow(writer
);
1444 static void php_ion_writer_options_init_shared_imports(php_ion_writer_options
*opt
)
1446 php_ion_catalog
*cat
= php_ion_obj(catalog
, opt
->cat
);
1449 ION_CHECK(ion_writer_options_initialize_shared_imports(&opt
->opt
));
1451 ION_COLLECTION
*col
= php_ion_catalog_collection(cat
);
1452 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1453 // holy, nah, forget it batman...
1454 ION_COLLECTION_CURSOR cur
;
1455 ION_COLLECTION_OPEN(col
, cur
);
1457 ION_SYMBOL_TABLE
**ptr
;
1458 ION_COLLECTION_NEXT(cur
, ptr
);
1460 ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt
->opt
, ptr
, 1));
1466 static void php_ion_writer_ctor(php_ion_writer
*obj
)
1468 php_ion_writer_options
*opt
= NULL
;
1471 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1472 opt
= php_ion_obj(writer_options
, obj
->opt
);
1474 php_ion_writer_options_init_shared_imports(opt
);
1477 obj
->opt
= object_construct(ce_Writer_Options
, 0, NULL
, NULL
);
1478 update_property_obj_ex(ce_Writer_Options
, &obj
->std
, ZEND_STRL("options"), obj
->opt
);
1479 OBJ_RELEASE(obj
->opt
);
1482 ION_STREAM_HANDLER h
;
1483 if (obj
->type
== STREAM_WRITER
) {
1484 h
= php_ion_writer_stream_handler
;
1485 php_ion_writer_stream_init(obj
, opt
);
1487 h
= php_ion_writer_buffer_handler
;
1488 php_ion_writer_buffer_init(obj
);
1491 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1495 static void php_ion_writer_dtor(php_ion_writer
*obj
)
1498 ion_writer_close(obj
->writer
);
1501 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1503 ion_writer_options_close_shared_imports(&opt
->opt
);
1506 if (obj
->type
== STREAM_WRITER
) {
1507 if (obj
->stream
.buf
.value
) {
1508 efree(obj
->stream
.buf
.value
);
1510 if (obj
->stream
.ptr
) {
1511 zend_list_delete(obj
->stream
.ptr
->res
);
1514 if (obj
->buffer
.str
.s
) {
1515 smart_str_0(&obj
->buffer
.str
);
1516 zend_string_release(obj
->buffer
.str
.s
);
1521 #define php_ion_writer_copy(o,n)
1522 php_ion_decl_noclone(writer
, Writer_Writer
);
1523 #define clone_ion_Writer_Writer NULL
1525 static bool can_serialize_fast(php_ion_serializer
*ser
)
1527 if (ser
->wri
->ce
!= ce_Writer_Buffer_Writer
&& ser
->wri
->ce
!= ce_Writer_Stream_Writer
) {
1531 if (ser
->std
.ce
!= ce_Serializer_Serializer
) {
1538 static void php_ion_serializer_ctor(php_ion_serializer
*ser_obj
)
1540 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1541 ser_obj
->ids
= global_ser
->ids
;
1542 ser_obj
->tmp
= global_ser
->tmp
;
1544 zend_update_property_bool(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
1545 ser_obj
->multi_seq
);
1546 zend_update_property_bool(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1547 ser_obj
->call_magic
);
1548 if (ser_obj
->call_custom
) {
1549 zend_update_property_str(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1550 ser_obj
->call_custom
);
1551 ser_obj
->call_custom
= zend_string_tolower(ser_obj
->call_custom
);
1553 zend_update_property_null(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1557 static void php_ion_serializer_dtor(php_ion_serializer
*obj
)
1559 if (obj
->call_custom
) {
1560 zend_string_release(obj
->call_custom
);
1564 static void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1566 static void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool unmangle_props
, bool annotate_props
)
1568 if (can_serialize_fast(ser
)) {
1569 ION_CHECK(ion_writer_start_container(php_ion_obj(writer
, ser
->wri
)->writer
, tid_STRUCT
));
1572 ZVAL_OBJ(&z_type
, php_ion_type_fetch(tid_STRUCT
));
1573 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "startContainer", NULL
, &z_type
);
1579 zend_string
*k
= NULL
;
1580 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1581 char buf
[MAX_LENGTH_OF_LONG
+ 1];
1585 const char *class_name
, *prop_name
;
1586 if (unmangle_props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1587 if (annotate_props
) {
1588 if (can_serialize_fast(ser
)) {
1589 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_PROPERTY
));
1590 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
,
1591 ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1593 zval z_ann_prop
, z_ann_class
;
1594 ZVAL_CHAR(&z_ann_prop
, 'p');
1595 ZVAL_STRINGL(&z_ann_class
, class_name
, prop_name
- class_name
);
1596 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_prop
, &z_ann_class
);
1597 zval_ptr_dtor(&z_ann_class
);
1605 ion_string_assign_cstr(&is
, (char *) prop_name
, (SIZE
) prop_len
);
1607 char *end
= buf
+ sizeof(buf
) - 1;
1608 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1609 ion_string_assign_cstr(&is
, ptr
, (SIZE
) (end
- ptr
));
1612 if (can_serialize_fast(ser
)) {
1613 // WATCH OUT: field names need to be copied
1615 ION_CHECK(ion_string_copy_to_owner(php_ion_obj(writer
, ser
->wri
)->writer
, &fn
, &is
));
1616 ION_CHECK(ion_writer_write_field_name(php_ion_obj(writer
, ser
->wri
)->writer
, &fn
));
1619 ZVAL_STRINGL(&z_field_name
, (const char *) is
.value
, is
.length
);
1620 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeFieldName", NULL
, &z_field_name
);
1621 zval_ptr_dtor(&z_field_name
);
1625 php_ion_serialize_zval(ser
, v
);
1627 ZEND_HASH_FOREACH_END();
1629 if(can_serialize_fast(ser
)) {
1630 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer
, ser
->wri
)->writer
));
1632 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "finishContainer", NULL
);
1636 static void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1638 if (can_serialize_fast(ser
)) {
1639 ION_CHECK(ion_writer_start_container(php_ion_obj(writer
, ser
->wri
)->writer
, tid_LIST
));
1642 ZVAL_OBJ(&z_type
, php_ion_type_fetch(tid_LIST
));
1643 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "startContainer", NULL
, &z_type
);
1648 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1649 php_ion_serialize_zval(ser
, v
);
1651 ZEND_HASH_FOREACH_END();
1653 if (can_serialize_fast(ser
)) {
1654 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer
, ser
->wri
)->writer
));
1656 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "finishContainer", NULL
);
1660 static void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1666 ZVAL_OBJ(&tmp
, zobject
);
1667 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1668 if (can_serialize_fast(ser
)) {
1670 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_SERIALIZEABLE
));
1671 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1672 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer
, ser
->wri
)->writer
, buf
, len
));
1675 zval z_ann_srlzbl
, z_ann_class
, z_clob
;
1676 ZVAL_CHAR(&z_ann_srlzbl
, 'S');
1677 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1678 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_srlzbl
, &z_ann_class
);
1679 zval_ptr_dtor(&z_ann_class
);
1681 ZVAL_STRINGL(&z_clob
, (const char *) buf
, len
);
1682 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeCLob", NULL
, &z_clob
);
1683 zval_ptr_dtor(&z_clob
);
1685 } else if (!EG(exception
)){
1686 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1687 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1691 static void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1696 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1699 if (IS_ARRAY
== Z_TYPE(rv
)) {
1700 if (can_serialize_fast(ser
)) {
1702 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
));
1703 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1705 zval z_ann_cust
, z_ann_class
;
1706 ZVAL_CHAR(&z_ann_cust
, 'C');
1707 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1708 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_cust
, &z_ann_class
);
1709 zval_ptr_dtor(&z_ann_class
);
1711 if (!EG(exception
)) {
1712 php_ion_serialize_zval(ser
, &rv
);
1716 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1717 "%s serializer %s::%s did not return an array",
1718 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1719 fn
? fn
->common
.function_name
->val
: "__serialize");
1723 static void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1725 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1727 if (can_serialize_fast(ser
)) {
1729 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_ENUM
));
1730 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1731 ION_CHECK(ion_writer_write_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1733 zval z_ann_enm
, z_ann_class
;
1734 ZVAL_CHAR(&z_ann_enm
, 'E');
1735 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1736 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_enm
, &z_ann_class
);
1737 zval_ptr_dtor(&z_ann_class
);
1739 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeSymbol", NULL
, z_cname
);
1743 static void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1745 if (can_serialize_fast(ser
)) {
1748 if (zobject
->ce
!= zend_standard_class_def
) {
1749 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_CLASS_OBJECT
));
1750 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1752 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_OBJECT
));
1755 if (zobject
->ce
!= zend_standard_class_def
) {
1756 zval z_ann_cobj
, z_ann_class
;
1757 ZVAL_CHAR(&z_ann_cobj
, 'c');
1758 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1759 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_cobj
, &z_ann_class
);
1760 zval_ptr_dtor(&z_ann_class
);
1763 ZVAL_CHAR(&z_ann_obj
, 'o');
1764 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_obj
);
1770 ZVAL_OBJ(&zobj
, zobject
);
1771 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1773 php_ion_serialize_struct(ser
, props
, true, true);
1774 zend_release_properties(props
);
1776 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1777 "Could not get properties for serialization of class %s",
1778 zobject
->ce
->name
->val
);
1782 static void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1784 zval tmp_type
, *type
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_TYPE
), 0, &tmp_type
);
1785 zval tmp_value
, *value
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_VALUE
), 0, &tmp_value
);
1787 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1789 if (can_serialize_fast(ser
)) {
1790 ION_CHECK(ion_writer_write_blob(php_ion_obj(writer
, ser
->wri
)->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1792 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeBLob", NULL
, value
);
1796 if (can_serialize_fast(ser
)) {
1797 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer
, ser
->wri
)->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1799 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeCLob", NULL
, value
);
1803 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1804 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1809 static bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1811 return ce
->__serialize
&& ser
->call_magic
;
1814 static bool can_call_iface_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1817 return !!ce
->serialize
; // NOLINT
1820 static bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1822 if (ser
->call_custom
) {
1823 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
1828 static bool is_special_class(const zend_class_entry
*ce
, zend_class_entry
**target
) {
1829 #define IS_TARGET(_ce) \
1830 if (instanceof_function(ce, _ce)) { \
1834 IS_TARGET(ce_Symbol
);
1835 IS_TARGET(ce_Decimal
);
1836 IS_TARGET(ce_Timestamp
);
1842 static void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
) {
1844 zend_class_entry
*special_ce
, *ce
= zobject
->ce
;
1847 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1848 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1849 "Serializing %s is not allowed", ce
->name
->val
);
1853 if (can_call_magic_serialize(ser
, ce
)) {
1854 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1855 } else if (can_call_iface_serialize(ser
, ce
)) {
1856 php_ion_serialize_object_iface(ser
, zobject
);
1857 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1858 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1859 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1860 php_ion_serialize_object_enum(ser
, zobject
);
1861 } else if (!is_special_class(ce
, &special_ce
)) {
1862 php_ion_serialize_object_std(ser
, zobject
);
1864 if (can_serialize_fast(ser
) || special_ce
== ce_LOB
) {
1865 if (special_ce
== ce_Symbol
) {
1866 ION_CHECK(ion_writer_write_ion_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1867 } else if (special_ce
== ce_Decimal
) {
1868 ION_CHECK(ion_writer_write_ion_decimal(php_ion_obj(writer
, ser
->wri
)->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1869 } else if (special_ce
== ce_Timestamp
) {
1871 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1872 decContext
*ctx
= php_ion_obj(writer_options
, php_ion_obj(writer
, ser
->wri
)->opt
)->opt
.decimal_context
;
1873 ION_CHECK(ion_writer_write_timestamp(php_ion_obj(writer
, ser
->wri
)->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1875 assert(special_ce
== ce_LOB
);
1876 php_ion_serialize_object_lob(ser
, zobject
);
1880 const char *method
= NULL
;
1882 ZVAL_OBJ(&z_param
, zobject
);
1883 if (special_ce
== ce_Symbol
) {
1884 method
= "writeSymbol";
1885 } else if (special_ce
== ce_Decimal
) {
1886 method
= "writeDecimal";
1887 } else if (special_ce
== ce_Timestamp
) {
1888 method
= "writeTimestamp";
1892 zend_call_method(ser
->wri
, NULL
, NULL
, method
, strlen(method
), NULL
, 1, &z_param
, NULL
);
1897 static bool php_ion_serialize_system_value(php_ion_serializer
*ser
, zval
*zv
)
1899 if (1 == php_ion_globals
.serializer
.level
) {
1900 if (Z_TYPE_P(zv
) == IS_OBJECT
) {
1901 if (Z_OBJCE_P(zv
) == ce_Symbol_Table_Shared
) {
1902 php_ion_symbol_table
*obj
= php_ion_obj(symbol_table
, Z_OBJ_P(zv
));
1903 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj
->tab
, php_ion_obj(writer
, ser
->wri
)->writer
));
1911 static bool php_ion_serialize_backref(php_ion_serializer
*ser
, zval
*zv
)
1913 if (Z_TYPE_P(zv
) == IS_STRING
&& Z_STR_P(zv
) == zend_empty_string
) {
1916 if (Z_TYPE_P(zv
) == IS_ARRAY
&& Z_ARR_P(zv
) == &zend_empty_array
) {
1920 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1921 zval
*ref
= zend_hash_index_find(ser
->ids
, idx
);
1925 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1926 zend_hash_index_add(ser
->ids
, idx
, &num
);
1929 zend_hash_next_index_insert(ser
->tmp
, zv
);
1934 if (can_serialize_fast(ser
)) {
1935 ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_BACKREF
));
1936 ION_CHECK_RETURN(true, ion_writer_write_int64(php_ion_obj(writer
, ser
->wri
)->writer
, Z_LVAL_P(ref
)));
1939 ZVAL_CHAR(&z_ann_bref
, 'r');
1940 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_bref
);
1941 ION_CATCH_RETURN(true);
1942 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeInt", NULL
, ref
);
1947 static void php_ion_serialize_string(php_ion_serializer
*ser
, zend_string
*str
)
1949 if (can_serialize_fast(ser
)) {
1951 ION_CHECK(ion_writer_write_string(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, str
)));
1955 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeString", NULL
, &zs
);
1959 static void php_ion_serialize_reference(php_ion_serializer
*ser
, zval
*noref
)
1961 if (can_serialize_fast(ser
)) {
1962 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_REFERENCE
));
1965 ZVAL_CHAR(&z_ann_ref
, 'R');
1966 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_ref
);
1969 php_ion_serialize_zval(ser
, noref
);
1972 static void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1974 if (php_ion_serialize_system_value(ser
, zv
)) {
1977 if (php_ion_serialize_backref(ser
, zv
)) {
1981 switch (Z_TYPE_P(zv
)) {
1983 php_ion_serialize_string(ser
, Z_STR_P(zv
));
1987 if (zend_array_is_list(Z_ARRVAL_P(zv
))) {
1988 php_ion_serialize_list(ser
, Z_ARRVAL_P(zv
));
1990 php_ion_serialize_struct(ser
, Z_ARRVAL_P(zv
), false, false);
1995 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1999 php_ion_serialize_reference(ser
, Z_REFVAL_P(zv
));
2004 static void php_ion_serialize_scalar(php_ion_serializer
*ser
, zval
*zv
)
2006 if (can_serialize_fast(ser
)) {
2007 switch (Z_TYPE_P(zv
)) {
2009 ION_CHECK(ion_writer_write_null(php_ion_obj(writer
, ser
->wri
)->writer
));
2012 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer
, ser
->wri
)->writer
, TRUE
));
2015 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer
, ser
->wri
)->writer
, FALSE
));
2018 ION_CHECK(ion_writer_write_int64(php_ion_obj(writer
, ser
->wri
)->writer
, Z_LVAL_P(zv
)));
2021 ION_CHECK(ion_writer_write_double(php_ion_obj(writer
, ser
->wri
)->writer
, Z_DVAL_P(zv
)));
2025 if (Z_ISNULL_P(zv
)) {
2026 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "writeNull", NULL
);
2028 const char *method
= NULL
;
2030 if (Z_TYPE_P(zv
) < IS_LONG
) {
2031 method
= "writeBool";
2032 } else if (Z_TYPE_P(zv
) < IS_DOUBLE
) {
2033 method
= "writeInt";
2035 method
= "writeFloat";
2038 zend_call_method(ser
->wri
, NULL
, NULL
, method
, strlen(method
), NULL
, 1, zv
, NULL
);
2043 static void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
2047 switch (Z_TYPE_P(zv
)) {
2053 php_ion_serialize_scalar(ser
, zv
);
2059 php_ion_serialize_refcounted(ser
, zv
);
2062 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2063 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
2067 #define php_ion_serializer_copy(o,n)
2068 php_ion_decl(serializer
, Serializer_Serializer
);
2069 #define clone_ion_Serializer NULL
2071 static void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
2074 HashTable
*arr
= NULL
;
2076 if (ser
->multi_seq
) {
2077 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
2078 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2079 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
2083 arr
= Z_ARRVAL_P(zv
);
2085 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
2086 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
2090 php_ion_globals_serializer_step();
2091 php_ion_serialize_zval(ser
, zv
);
2092 php_ion_globals_serializer_exit();
2094 if (!ser
->multi_seq
) {
2097 zend_hash_move_forward_ex(arr
, &pos
);
2098 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
2102 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
2104 zend_object
*zo_ser
= NULL
, *zo_wri
= NULL
;
2107 zo_ser
= create_ion_Serializer_Serializer(NULL
);
2108 ser
= php_ion_obj(serializer
, zo_ser
);
2110 ser
->call_magic
= true;
2111 php_ion_serializer_ctor(ser
);
2115 if (!ser
->wri
|| !instanceof_function(ser
->wri
->ce
, ce_Writer
)) {
2116 zo_wri
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
2117 php_ion_writer
*wri
= php_ion_obj(writer
, zo_wri
);
2118 wri
->type
= BUFFER_WRITER
;
2119 if (ser
->wri
&& instanceof_function(ser
->wri
->ce
, ce_Writer_Options
)) {
2120 wri
->opt
= ser
->wri
;
2122 php_ion_writer_ctor(wri
);
2126 if (!EG(exception
)) {
2127 php_ion_serialize_ex(ser
, zv
);
2130 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
2131 if (can_serialize_fast(ser
)) {
2132 ion_writer_flush(php_ion_obj(writer
, ser
->wri
)->writer
, NULL
);
2134 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "flush", NULL
);
2136 RETVAL_STR_COPY(php_ion_obj(writer
, ser
->wri
)->buffer
.str
.s
);
2139 OBJ_RELEASE(zo_wri
);
2142 OBJ_RELEASE(zo_ser
);
2146 static bool can_unserialize_fast(php_ion_unserializer
*ser
)
2148 if (ser
->rdr
->ce
!= ce_Reader_Buffer_Reader
&& ser
->rdr
->ce
!= ce_Reader_Stream_Reader
) {
2152 if (ser
->std
.ce
!= ce_Unserializer_Unserializer
) {
2159 static void php_ion_unserializer_ctor(php_ion_unserializer
*ser_obj
)
2161 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
2162 ser_obj
->ids
= global_ser
->ids
;
2163 ser_obj
->tmp
= global_ser
->tmp
;
2164 ser_obj
->addref
= global_ser
->addref
;
2166 zend_update_property_bool(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
2167 ser_obj
->multi_seq
);
2168 zend_update_property_bool(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callMagicUnserialize"),
2169 ser_obj
->call_magic
);
2170 if (ser_obj
->call_custom
) {
2171 zend_update_property_str(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"),
2172 ser_obj
->call_custom
);
2173 ser_obj
->call_custom
= zend_string_tolower(ser_obj
->call_custom
);
2175 zend_update_property_null(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"));
2179 static void php_ion_unserializer_dtor(php_ion_unserializer
*obj
)
2181 if (obj
->call_custom
) {
2182 zend_string_release(obj
->call_custom
);
2186 static void php_ion_unserializer_next(php_ion_unserializer
*ser
, ION_TYPE
*typ
)
2188 if (can_unserialize_fast(ser
)) {
2189 ION_CHECK(ion_reader_next(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2191 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "next", NULL
);
2194 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "key", &z_type
);
2196 *typ
= ion_type_from_enum(Z_OBJ(z_type
));
2200 static void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
2202 static bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
2204 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
2207 static bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
2209 return (ce
&& ce
->unserialize
);
2212 static bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2214 if (ser
->call_custom
) {
2215 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
2220 static zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
2222 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2225 object_init_ex(return_value
, ce
);
2226 return zend_hash_next_index_insert(ser
->ids
, return_value
);
2229 zend_throw_exception_ex(ce_Exception
, IERR_IMPORT_NOT_FOUND
,
2230 "Could not find class %s", ser
->annotations
.object_class
->val
);
2234 static void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
2236 zend_string
*zs_case
= zval_get_string(return_value
);
2237 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2238 ZVAL_NULL(return_value
);
2241 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2242 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
2243 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2244 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
2247 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
2248 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2249 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
2252 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
2253 zend_hash_next_index_insert(ser
->ids
, return_value
);
2254 zend_string_release(zs_case
);
2257 static void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
2259 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2260 if (can_call_iface_unserialize(ser
, ce
)) {
2261 zend_string
*s
= zval_get_string(return_value
);
2262 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2263 ZVAL_NULL(return_value
);
2264 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
2265 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
2266 RETVAL_ZVAL(backref
, 0, 0);
2268 zval_ptr_dtor(backref
);
2270 if (!EG(exception
)) {
2271 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2272 "Failed to unserialize class %s", ce
->name
->val
);
2275 zend_string_release(s
);
2277 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2278 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
2282 static void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
, bool is_prop
)
2284 if (can_unserialize_fast(ser
)) {
2285 // FIXME: symbol table?
2287 SID sid
= UNKNOWN_SID
;
2288 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
2290 ION_CHECK(ion_reader_get_field_name(php_ion_obj(reader
, ser
->rdr
)->reader
, &name
));
2293 ION_CHECK(ion_reader_get_field_name_symbol(php_ion_obj(reader
, ser
->rdr
)->reader
, &is_ptr
));
2294 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
2295 ION_STRING_ASSIGN(&name
, &is_ptr
->value
);
2301 switch (name
.length
) {
2303 ptr
= zend_print_long_to_buf(end
, sid
);
2305 *key
= zend_string_init(ptr
, end
- ptr
, 0);
2308 *key
= ZSTR_CHAR(*name
.value
);
2312 *key
= zend_string_init_interned((char *) name
.value
, name
.length
, 0);
2314 *key
= zend_string_from_ion(&name
);
2321 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getFieldName", &z_fn
);
2322 if (!Z_STRLEN(z_fn
) || EG(exception
)) {
2323 zend_clear_exception();
2324 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getFieldNameSymbol", &z_fn
);
2328 *key
= zval_get_string(&z_fn
);
2329 zval_ptr_dtor(&z_fn
);
2333 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
2335 zend_hash_next_index_insert(ser
->ids
, return_value
);
2337 if (can_unserialize_fast(ser
)) {
2338 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2340 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2346 php_ion_unserializer_next(ser
, &typ
);
2349 if (typ
== tid_EOF
) {
2354 php_ion_unserialize_field_name(ser
, &key
, true);
2358 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2359 ION_CATCH(zend_string_release(key
));
2361 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
2362 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
2363 ce
= zend_lookup_class(ser
->annotations
.property_class
);
2365 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
2366 zval_ptr_dtor(&zvalue
);
2367 zend_string_release(key
);
2370 if (can_unserialize_fast(ser
)) {
2371 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2376 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
2377 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
2378 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
2379 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
2380 * nondeterministic behavior.
2382 static void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
2384 zend_hash_next_index_insert(ser
->ids
, return_value
);
2386 if (can_unserialize_fast(ser
)) {
2387 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2389 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2395 php_ion_unserializer_next(ser
, &typ
);
2398 if (typ
== tid_EOF
) {
2403 php_ion_unserialize_field_name(ser
, &key
, false);
2407 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2408 ION_CATCH(zend_string_release(key
));
2410 // FIXME:: too naive; b0rked if the previous value is an array
2411 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
2412 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
2413 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
2415 Z_TRY_ADDREF_P(prev
);
2416 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
2417 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
2419 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
2421 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
2423 zend_string_release(key
);
2426 if (can_unserialize_fast(ser
)) {
2427 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2431 static void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2433 switch (ser
->annotations
.object_type
) {
2439 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
2440 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2441 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
2446 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
2447 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2448 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
2450 *fn
= zobject
->ce
->__unserialize
;
2454 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2455 "Invalid object type %c", ser
->annotations
.object_type
);
2459 static void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
2461 // backup possible backref to array returned by magic/custom __serialize()
2462 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
2463 ZVAL_NULL(return_value
);
2464 php_ion_unserialize_class(ser
, return_value
);
2467 zend_function
*fn
= NULL
;
2468 zend_object
*zobject
= Z_OBJ_P(return_value
);
2469 verify_unserializer(ser
, zobject
, &fn
);
2474 php_ion_unserialize_props(ser
, return_value
);
2479 if (Z_TYPE_P(input
) != IS_ARRAY
) {
2480 zval_ptr_dtor(input
);
2482 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
2483 php_ion_unserialize_hash(ser
, input
);
2488 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
2492 static void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
2494 if (ser
->annotations
.object_class
) {
2495 switch (ser
->annotations
.object_type
) {
2497 php_ion_unserialize_object_iface(ser
, return_value
);
2500 php_ion_unserialize_object_enum(ser
, return_value
);
2503 php_ion_unserialize_object(ser
, return_value
);
2505 } else if (!ser
->annotations
.object_type
) {
2506 bool is_shared_symtab
= ser
->annotations
.shared_symtab
;
2507 array_init(return_value
);
2508 php_ion_unserialize_hash(ser
, return_value
);
2509 if (is_shared_symtab
) {
2510 zend_object
*zo_ss
= object_construct(ce_Symbol_Table_Shared
, 0, NULL
, Z_ARRVAL_P(return_value
));
2511 zval_ptr_dtor(return_value
);
2514 } else if (ser
->annotations
.object_type
== 'o') {
2515 object_init(return_value
);
2516 php_ion_unserialize_hash(ser
, return_value
);
2518 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2519 "Invalid object annotation %c::", ser
->annotations
.object_type
);
2523 static void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
2525 if (can_unserialize_fast(ser
)) {
2526 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2528 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2530 array_init(return_value
);
2531 zend_hash_next_index_insert(ser
->ids
, return_value
);
2535 php_ion_unserializer_next(ser
, &typ
);
2538 if (typ
== tid_EOF
) {
2543 php_ion_unserialize_zval(ser
, &next
, &typ
);
2546 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
2549 if (can_unserialize_fast(ser
)) {
2550 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2552 // userland reader object already stepped out
2556 static void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
2558 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
2561 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, (SIZE
) zstr
->len
, &read
);
2562 if (err
== IERR_BUFFER_TOO_SMALL
) {
2563 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
2566 ION_CHECK(err
, zend_string_release(zstr
));
2567 if (zstr
->len
> read
) {
2568 zstr
->val
[read
] = 0;
2569 zstr
= zend_string_truncate(zstr
, read
, 0);
2574 static void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
2577 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
2579 object_init_ex(return_value
, ce_Timestamp
);
2580 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
2582 zend_string
*fmt
= NULL
;
2583 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
2584 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
2585 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
2586 zend_string_release(fmt
);
2591 static void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
2593 ION_INT
*num
= NULL
;
2594 ION_CHECK(ion_int_alloc(reader
, &num
));
2595 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
2597 // TODO: SIZEOF_ZEND_LONG == 4
2599 iERR err
= ion_int_to_int64(num
, &i64
);
2605 case IERR_NUMERIC_OVERFLOW
: ;
2607 ION_CHECK(ion_int_char_length(num
, &max
));
2608 zend_string
*zs
= zend_string_alloc(max
, 0);
2610 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2611 zs
->val
[zs
->len
= len
] = 0;
2622 static void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2624 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2627 ZVAL_COPY_VALUE(return_value
, backref
);
2628 zend_hash_next_index_insert(ser
->addref
, return_value
);
2630 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2631 "Could not find back reference " ZEND_LONG_FMT
, Z_LVAL_P(return_value
));
2635 static zend_string
*php_ion_unserialize_annotation(php_ion_unserializer
*ser
, zend_long idx
)
2639 if (can_unserialize_fast(ser
)) {
2641 ION_CHECK_RETURN(NULL
, ion_reader_get_an_annotation(php_ion_obj(reader
, ser
->rdr
)->reader
, idx
, &ann_str
));
2643 if (ann_str
.length
== 1){
2644 // no need to remember interned string
2645 ZVAL_CHAR(&z_ann
, *ann_str
.value
);
2647 ZVAL_STRINGL(&z_ann
, (char *) ann_str
.value
, ann_str
.length
);
2648 zend_hash_next_index_insert(ser
->tmp
, &z_ann
);
2652 ZVAL_LONG(&z_idx
, idx
);
2653 zend_call_method_with_1_params(&ser
->std
, NULL
, NULL
, "getAnnotation", &z_ann
, &z_idx
);
2654 ION_CATCH_RETURN(NULL
);
2655 zend_hash_next_index_insert(ser
->tmp
, &z_ann
);
2658 return Z_STR(z_ann
);
2661 static void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2663 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2666 if (can_unserialize_fast(ser
)) {
2667 ION_CHECK(ion_reader_get_annotation_count(php_ion_obj(reader
, ser
->rdr
)->reader
, &ann_cnt
));
2670 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "countAnnotations", &z_ann_cnt
);
2672 ann_cnt
= Z_LVAL(z_ann_cnt
);
2675 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2676 zend_string
*zs_ann
= php_ion_unserialize_annotation(ser
, i
);
2678 if (zs_ann
->len
!= 1) {
2679 if (zend_string_equals_literal(zs_ann
, ION_SYS_SYMBOL_SHARED_SYMBOL_TABLE
)) {
2680 ser
->annotations
.shared_symtab
= true;
2686 switch (*zs_ann
->val
) {
2692 if (ser
->annotations
.makeref
) {
2693 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2694 "Invalid multiple reference annotations");
2697 ser
->annotations
.makeref
= true;
2701 if (ser
->annotations
.backref
) {
2702 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2703 "Invalid multiple back reference annotations");
2706 ser
->annotations
.backref
= true;
2710 if (ser
->annotations
.object_prop
) {
2711 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2712 "Invalid multiple object property annotations");
2715 ser
->annotations
.object_prop
= true;
2716 ser
->annotations
.property_class
= php_ion_unserialize_annotation(ser
, ++i
);
2726 if (ser
->annotations
.object_type
) {
2727 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2728 "Invalid multiple object type annotations: %c::%c",
2729 ser
->annotations
.object_type
, *zs_ann
->val
);
2733 ser
->annotations
.object_type
= *zs_ann
->val
;
2734 if (ser
->annotations
.object_type
!= 'o') {
2735 ser
->annotations
.object_class
= php_ion_unserialize_annotation(ser
, ++i
);
2742 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2743 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2744 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2747 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2748 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2749 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2755 static void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2758 memcpy(&ser
->type
, typ
, sizeof(ser
->type
)); // NOLINT
2761 if (can_unserialize_fast(ser
)) {
2762 ION_CHECK(ion_reader_next(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2764 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "next", NULL
);
2768 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getType", &z_type
);
2770 *typ
= ion_type_from_enum(Z_OBJ_P(return_value
));
2774 php_ion_unserialize_annotations(ser
);
2777 if (ser
->annotations
.makeref
) {
2778 ZVAL_MAKE_REF(return_value
);
2779 zend_hash_next_index_insert(ser
->ids
, return_value
);
2780 ZVAL_DEREF(return_value
);
2783 if (ION_TYPE_INT(*typ
) > 0) {
2785 if (can_unserialize_fast(ser
)) {
2786 ION_CHECK(ion_reader_is_null(php_ion_obj(reader
, ser
->rdr
)->reader
, &is_null
));
2789 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "isNull", &z_is_null
);
2790 is_null
= i_zend_is_true(&z_is_null
);
2797 switch (ION_TYPE_INT(*typ
)) {
2799 if (can_unserialize_fast(ser
)) {
2800 ION_CHECK(ion_reader_read_null(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2802 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readNull", return_value
);
2804 *typ
= ion_type_from_enum(Z_OBJ_P(return_value
));
2809 if (can_unserialize_fast(ser
)) {
2811 ION_CHECK(ion_reader_read_bool(php_ion_obj(reader
, ser
->rdr
)->reader
, &bval
));
2814 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readBool", return_value
);
2819 if (can_unserialize_fast(ser
)) {
2820 php_ion_reader_read_int(php_ion_obj(reader
, ser
->rdr
)->reader
, return_value
);
2822 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readInt", return_value
);
2824 if (ser
->annotations
.backref
) {
2826 php_ion_unserialize_backref(ser
, return_value
);
2828 if (ser
->annotations
.object_type
) {
2829 if (!ser
->annotations
.backref
) {
2830 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2831 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2832 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2835 goto unserialize_struct
;
2840 if (can_unserialize_fast(ser
)) {
2842 ION_CHECK(ion_reader_read_double(php_ion_obj(reader
, ser
->rdr
)->reader
, &d
));
2845 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readFloat", return_value
);
2849 case tid_DECIMAL_INT
:
2850 if (can_unserialize_fast(ser
)) {
2851 object_init_ex(return_value
, ce_Decimal
);
2852 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2853 ION_CHECK(ion_reader_read_ion_decimal(php_ion_obj(reader
, ser
->rdr
)->reader
, &dec
->dec
));
2854 php_ion_decimal_ctor(dec
);
2856 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readDecimal", return_value
);
2859 zend_hash_next_index_insert(ser
->ids
, return_value
);
2862 case tid_TIMESTAMP_INT
:
2863 if (can_unserialize_fast(ser
)) {
2864 php_ion_reader
*reader
= php_ion_obj(reader
, ser
->rdr
);
2865 php_ion_reader_read_timestamp(reader
->reader
, &php_ion_obj(reader_options
, reader
->opt
)->opt
, return_value
);
2867 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readTimestamp", return_value
);
2870 zend_hash_next_index_insert(ser
->ids
, return_value
);
2873 case tid_SYMBOL_INT
:
2874 if (can_unserialize_fast(ser
)) {
2876 ION_CHECK(ion_reader_read_ion_symbol(php_ion_obj(reader
, ser
->rdr
)->reader
, &sym
));
2877 php_ion_symbol_zval(&sym
, return_value
);
2879 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readSymbol", return_value
);
2882 if (ser
->annotations
.object_type
) {
2883 goto unserialize_struct
;
2885 zend_hash_next_index_insert(ser
->ids
, return_value
);
2888 case tid_STRING_INT
:
2889 if (can_unserialize_fast(ser
)) {
2891 ION_CHECK(ion_reader_read_string(php_ion_obj(reader
, ser
->rdr
)->reader
, &str
));
2892 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2894 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readString", return_value
);
2897 if (ser
->annotations
.object_type
) {
2898 goto unserialize_struct
;
2900 zend_hash_next_index_insert(ser
->ids
, return_value
);
2905 if (can_unserialize_fast(ser
)) {
2906 php_ion_reader_read_lob(php_ion_obj(reader
, ser
->rdr
)->reader
, return_value
);
2908 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, *typ
== tid_BLOB
? "readBLob" : "readCLob", return_value
);
2911 if (ser
->annotations
.object_type
) {
2912 goto unserialize_struct
;
2914 zend_hash_next_index_insert(ser
->ids
, return_value
);
2918 case tid_SEXP_INT
: // FIXME
2919 php_ion_unserialize_list(ser
, return_value
);
2921 if (!ser
->annotations
.object_type
) {
2926 case tid_STRUCT_INT
:
2927 unserialize_struct
: ;
2928 php_ion_unserialize_struct(ser
, return_value
);
2932 ZEND_ASSERT(!"none");
2935 case tid_DATAGRAM_INT
:
2936 ZEND_ASSERT(!"datagram");
2942 #define php_ion_unserializer_copy(o,n)
2943 php_ion_decl(unserializer
, Unserializer_Unserializer
);
2944 #define clone_ion_Unserializer NULL
2946 static void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2948 if (ser
->multi_seq
) {
2949 array_init(return_value
);
2955 php_ion_globals_unserializer_step();
2956 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2957 php_ion_globals_unserializer_exit();
2958 ION_CATCH(zval_ptr_dtor(&tmp
));
2960 if (!ser
->multi_seq
) {
2961 RETURN_COPY_VALUE(&tmp
);
2962 } else if (ser
->type
!= tid_EOF
) {
2963 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2965 } while (ser
->type
!= tid_EOF
);
2968 static php_ion_reader
*create_unserialize_reader(zval
*zdata
)
2970 zend_object
*zo_reader
;
2971 php_ion_reader
*reader
= NULL
;
2974 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2975 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2976 reader
= php_ion_obj(reader
, zo_reader
);
2977 reader
->type
= STREAM_READER
;
2978 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2979 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2980 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2981 reader
= php_ion_obj(reader
, zo_reader
);
2982 reader
->type
= BUFFER_READER
;
2983 reader
->buffer
= zval_get_string(zdata
);
2984 } else if (Z_TYPE_P(zdata
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(zdata
), ce_Reader
)) {
2986 reader
= php_ion_obj(reader
, Z_OBJ_P(zdata
));
2988 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2989 "Invalid source to unserialize: expected string or resource, got %s",
2990 zend_zval_type_name(zdata
));
2996 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
2998 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
3001 zo_ser
= create_ion_Unserializer_Unserializer(NULL
);
3002 ser
= php_ion_obj(unserializer
, zo_ser
);
3004 ser
->call_magic
= true;
3005 php_ion_unserializer_ctor(ser
);
3009 php_ion_reader
*reader
= create_unserialize_reader(zdata
);
3011 if (ser
->rdr
&& instanceof_function(ser
->rdr
->ce
, ce_Reader_Options
)) {
3012 zo_opt
= reader
->opt
= ser
->rdr
;
3014 php_ion_reader_ctor(reader
);
3015 ser
->rdr
= &reader
->std
;
3016 if (!EG(exception
)) {
3017 php_ion_unserialize_ex(ser
, return_value
);
3019 OBJ_RELEASE(&reader
->std
);
3023 OBJ_RELEASE(zo_opt
);
3026 OBJ_RELEASE(zo_ser
);