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(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 static zend_object *clone_ion_ ## cname(zend_object *std) \
394 php_ion_ ## type *old_obj = php_ion_obj(type, std), \
395 *new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \
396 php_ion_ ## type ## _copy(new_obj, old_obj); \
398 return &new_obj->std; \
400 #define php_ion_register(type, cname, ...) do { \
401 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
402 ce_ ## cname ->create_object = create_ion_ ## cname; \
403 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
404 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
405 oh_ ## cname .free_obj = free_ion_ ## cname; \
406 oh_ ## cname .clone_obj = clone_ion_ ## cname; \
409 #define ION_CHECK_RETURN(r, err, ...) do { \
411 if (UNEXPECTED(__err)) { \
412 zend_throw_exception_ex(ce_Exception, __err, "%s: %s", ion_error_to_str(__err), #err); \
418 #define ION_CHECK(err, ...) \
419 ION_CHECK_RETURN(, err, __VA_ARGS__)
421 #define ION_CATCH_RETURN(ret, ...) do { \
422 if (UNEXPECTED(EG(exception))) { \
428 #define ION_CATCH(...) \
429 ION_CATCH_RETURN(, __VA_ARGS__)
431 #define PTR_CHECK_RETURN(ret, ptr, ...) do { \
432 if (UNEXPECTED(!(ptr))) { \
433 zend_throw_error(NULL, "Uninitialized object"); \
438 #define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
440 #define OBJ_CHECK_RETURN(ret, obj, ...) do { \
441 PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
442 PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
444 #define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
446 static ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
448 is
->length
= zs
? (SIZE
) zs
->len
: 0;
449 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
453 static zend_string
*zend_string_from_ion(const ION_STRING
*s
)
455 return zend_string_init((const char *) s
->value
, s
->length
, 0);
458 static void call_constructor(zend_object
*zo
, uint32_t argc
, zval
*argv
, zend_array
*args_named
)
460 zend_call_known_function(zo
->ce
->constructor
, zo
, zo
->ce
, NULL
, argc
, argv
, args_named
);
463 static zend_object
*object_construct(zend_class_entry
*ce
, uint32_t argc
, zval
*argv
, zend_array
*args_named
)
466 object_init_ex(&z_obj
, ce
);
467 call_constructor(Z_OBJ(z_obj
), argc
, argv
, args_named
);
471 static void update_property_obj_ex(zend_class_entry
*scope
, zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
475 zend_update_property(scope
, obj
, n
, l
, &zobj
);
477 static void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
479 update_property_obj_ex(obj
->ce
, obj
, n
, l
, p
);
482 #define RETURN_IONTYPE(typ) do { \
483 zend_object *__zo = php_ion_type_fetch(typ); \
484 if (UNEXPECTED(!__zo)) { \
487 RETURN_OBJ_COPY(__zo); \
490 static zend_object
*php_ion_type_fetch(ION_TYPE typ
)
492 zend_long index
= ION_TYPE_INT(typ
);
493 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
495 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
496 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
499 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
502 static ION_TYPE
ion_type_from_enum(zend_object
*zo
)
504 return (ION_TYPE
) Z_LVAL_P(zend_enum_fetch_case_value(zo
));
507 typedef struct php_ion_symbol_iloc
{
508 ION_SYMBOL_IMPORT_LOCATION loc
;
511 } php_ion_symbol_iloc
;
513 static void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
515 zend_update_property_long(ce_Symbol_ImportLocation
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
516 zend_update_property_str(ce_Symbol_ImportLocation
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
517 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
520 static void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
522 zend_string_release(obj
->name
);
525 static void php_ion_symbol_iloc_copy(php_ion_symbol_iloc
*new_obj
, php_ion_symbol_iloc
*old_obj
)
527 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
528 new_obj
->name
= zend_string_copy(old_obj
->name
);
529 ion_string_from_zend(&new_obj
->loc
.name
, new_obj
->name
);
530 new_obj
->loc
.location
= old_obj
->loc
.location
;
533 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
);
535 typedef struct php_ion_symbol
{
538 zend_object
*iloc
, std
;
541 static int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
542 zend_string
*zs1
= zval_get_string(zv1
);
543 zend_string
*zs2
= zval_get_string(zv2
);
550 if (zs1
->len
> zs2
->len
) {
552 } else if (zs2
->len
> zs1
->len
) {
555 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
557 zend_string_release(zs1
);
558 zend_string_release(zs2
);
562 static void php_ion_symbol_ctor(php_ion_symbol
*obj
)
564 zend_update_property_long(ce_Symbol
, &obj
->std
, ZEND_STRL("sid"),
567 zend_update_property_str(ce_Symbol
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
569 zend_update_property_null(ce_Symbol
, &obj
->std
, ZEND_STRL("value"));
571 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
573 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
574 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
576 zend_update_property_null(ce_Symbol
, &obj
->std
, ZEND_STRL("importLocation"));
580 static void php_ion_symbol_dtor(php_ion_symbol
*obj
)
583 zend_string_release(obj
->value
);
587 static void php_ion_symbol_copy(php_ion_symbol
*new_obj
, php_ion_symbol
*old_obj
)
589 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
590 new_obj
->sym
= old_obj
->sym
;
591 if (old_obj
->value
) {
592 new_obj
->value
= zend_string_copy(old_obj
->value
);
593 ion_string_from_zend(&new_obj
->sym
.value
, new_obj
->value
);
596 if ((new_obj
->iloc
= old_obj
->iloc
)) {
597 new_obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, new_obj
->iloc
)->loc
;
601 static void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
603 object_init_ex(return_value
, ce_Symbol
);
604 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
606 sym
->sym
.sid
= sym_ptr
->sid
;
607 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
608 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
610 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
611 sym
->iloc
= Z_OBJ(ziloc
);
613 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
614 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
615 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
617 php_ion_symbol_iloc_ctor(iloc
);
620 php_ion_symbol_ctor(sym
);
622 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
623 GC_DELREF(sym
->iloc
);
627 static zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
629 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
631 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
633 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
636 object_init_ex(&zsym
, ce_Symbol
);
637 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
638 sym
->sym
.sid
= Z_LVAL_P(zid
);
639 sym
->value
= zval_get_string(zss
);
640 php_ion_symbol_ctor(sym
);
641 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);
648 php_ion_decl(symbol
, Symbol
);
650 typedef struct php_ion_symbol_table
{
651 ION_SYMBOL_TABLE
*tab
;
652 int (*dtor
)(ION_SYMBOL_TABLE
*);
654 } php_ion_symbol_table
;
656 static void php_ion_symbol_table_ctor(php_ion_symbol_table
*obj
)
660 ION_SYMBOL_TABLE_TYPE typ
= ist_EMPTY
;
661 ion_symbol_table_get_type(obj
->tab
, &typ
);
662 if (typ
!= ist_LOCAL
) {
664 if (IERR_OK
== ion_symbol_table_get_name(obj
->tab
, &is
)) {
665 zend_update_property_stringl(ce_Symbol_Table_Shared
, &obj
->std
, ZEND_STRL("name"), (char *) is
.value
, is
.length
);
668 if (IERR_OK
== ion_symbol_table_get_version(obj
->tab
, &iv
)) {
669 zend_update_property_long(ce_Symbol_Table_Shared
, &obj
->std
, ZEND_STRL("version"), iv
);
674 static void php_ion_symbol_table_dtor(php_ion_symbol_table
*obj
)
684 static void php_ion_symbol_table_copy(php_ion_symbol_table
*new_obj
, php_ion_symbol_table
*old_obj
)
686 // do not clone cache members
687 // zend_objects_clone_members(...)
688 if ((new_obj
->dtor
= old_obj
->dtor
)) {
689 ION_CHECK(ion_symbol_table_clone_with_owner(old_obj
->tab
, &new_obj
->tab
, NULL
));
691 new_obj
->tab
= old_obj
->tab
;
693 // update non-cache members
694 php_ion_symbol_table_ctor(new_obj
);
697 static void php_ion_symbol_table_import(php_ion_symbol_table
*obj
, php_ion_symbol_table
*import
)
703 zval
*zimports
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), 0, &tmp
);
705 zend_ulong idx
= (uintptr_t) &import
->std
.gc
;
706 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports
), idx
)) {
707 ION_CHECK(ion_symbol_table_import_symbol_table(obj
->tab
, import
->tab
));
709 SEPARATE_ARRAY(zimports
);
710 GC_ADDREF(&import
->std
);
711 add_index_object(zimports
, idx
, &import
->std
);
712 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), zimports
);
717 static void php_ion_symbol_table_symbol_zval(php_ion_symbol_table
*obj
, ION_SYMBOL
*sym
, zval
*return_value
)
720 zval
*zsyms
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbols"), 0, &tmp
);
722 zval
*zsym
= zend_hash_index_find(Z_ARRVAL_P(zsyms
), sym
->sid
);
728 php_ion_symbol_zval(sym
, return_value
);
731 SEPARATE_ARRAY(zsyms
);
732 ZVAL_ADDREF(return_value
);
733 add_index_zval(zsyms
, sym
->sid
, return_value
);
737 php_ion_decl(symbol_table
, Symbol_Table
);
739 typedef struct php_ion_decimal_ctx
{
742 } php_ion_decimal_ctx
;
744 #define php_ion_decimal_ctx_init_max(c, rounding) \
745 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
746 static void php_ion_decimal_ctx_init(decContext
*ctx
,
747 int digits
, int emax
, int emin
, enum rounding round
, zend_bool clamp
)
749 memset(ctx
, 0, sizeof(*ctx
));
750 ctx
->digits
= digits
;
757 static void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
759 if (!obj
->ctx
.digits
) {
760 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
763 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
765 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
767 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
768 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
769 zend_update_property_long(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
770 zend_update_property_bool(ce_Decimal_Context
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
773 static void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx
*obj
)
777 static void php_ion_decimal_ctx_copy(php_ion_decimal_ctx
*new_obj
, php_ion_decimal_ctx
*old_obj
)
779 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
780 new_obj
->ctx
= old_obj
->ctx
;
783 php_ion_decl(decimal_ctx
, Decimal_Context
);
785 typedef struct php_ion_decimal
{
787 zend_object
*ctx
, std
;
790 static void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
792 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
793 ION_CHECK(ion_decimal_from_int32(dec
, num
));
794 } else if (num
> 0 && num
<= UINT32_MAX
) {
795 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
798 ION_CHECK(ion_int_alloc(NULL
, &iint
));
799 ION_CHECK(ion_int_from_long(iint
, num
),
801 /* WATCH OUT: BS API */
802 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
803 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
809 static zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
811 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
812 (void) ion_decimal_to_string(dec
, zstr
->val
);
813 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
816 static void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
819 ION_CHECK(ion_int_alloc(NULL
, &ii
));
820 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
822 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
827 static bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
831 if (!ion_decimal_is_integer(&obj
->dec
)) {
836 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_max
, &php_ion_globals
.decimal
.ctx
, &result
);
841 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_min
, &php_ion_globals
.decimal
.ctx
, &result
);
848 static void php_ion_decimal_ctor(php_ion_decimal
*obj
)
852 object_init_ex(&zdc
, ce_Decimal_Context
);
853 obj
->ctx
= Z_OBJ(zdc
);
854 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
857 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
859 if (php_ion_decimal_fits_zend_long(obj
)) {
861 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
862 zend_update_property_long(ce_Decimal
, &obj
->std
, ZEND_STRL("number"), l
);
864 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
865 zend_update_property_str(ce_Decimal
, &obj
->std
, ZEND_STRL("number"), zstr
);
866 zend_string_release(zstr
);
870 static void php_ion_decimal_dtor(php_ion_decimal
*obj
)
872 ion_decimal_free(&obj
->dec
);
875 static void php_ion_decimal_copy(php_ion_decimal
*new_obj
, php_ion_decimal
*old_obj
)
877 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
878 new_obj
->ctx
= old_obj
->ctx
;
879 ION_CHECK(ion_decimal_copy(&new_obj
->dec
, &old_obj
->dec
));
882 php_ion_decl(decimal
, Decimal
);
884 typedef php_date_obj php_ion_timestamp
;
886 static zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
889 ctx
= &php_ion_globals
.decimal
.ctx
;
891 decQuad microsecs
, result
;
892 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
893 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
896 static decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, int usec
, decContext
*ctx
)
899 ctx
= &php_ion_globals
.decimal
.ctx
;
901 decQuad microsecs
, us
;
902 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
905 static zend_string
*php_ion_timestamp_format_fetch(zend_string
*fmt_case
)
907 return Z_STR_P(zend_enum_fetch_case_value(zend_enum_get_case(ce_Timestamp_Format
, fmt_case
)));
910 static zend_string
*php_dt_format_from_precision(uint8_t precision
)
913 case ION_TS_FRAC
| 0x80:
914 return php_ion_timestamp_format_fetch(g_intern_str
.FracTZ
);
916 return php_ion_timestamp_format_fetch(g_intern_str
.Frac
);
917 case ION_TS_SEC
| 0x80:
918 return php_ion_timestamp_format_fetch(g_intern_str
.SecTZ
);
920 return php_ion_timestamp_format_fetch(g_intern_str
.Sec
);
921 case ION_TS_MIN
| 0x80:
922 return php_ion_timestamp_format_fetch(g_intern_str
.MinTZ
);
924 return php_ion_timestamp_format_fetch(g_intern_str
.Min
);
926 return php_ion_timestamp_format_fetch(g_intern_str
.Day
);
928 return php_ion_timestamp_format_fetch(g_intern_str
.Month
);
930 return php_ion_timestamp_format_fetch(g_intern_str
.Year
);
932 return ZSTR_CHAR('c');
936 static timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
938 timelib_time
*time
= ecalloc(1, sizeof(*time
));
945 switch (ts
->precision
& 0x7f) {
947 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
950 time
->s
= ts
->seconds
;
953 time
->i
= ts
->minutes
;
966 time
->z
= ts
->tz_offset
* 60;
967 if (time
->z
|| ts
->precision
& 0x80) {
968 time
->zone_type
= TIMELIB_ZONETYPE_OFFSET
;
970 time
->zone_type
= TIMELIB_ZONETYPE_ID
;
971 time
->tz_info
= get_timezone_info();
976 *fmt
= php_dt_format_from_precision(ts
->precision
);
981 static ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
983 memset(buf
, 0, sizeof(*buf
));
986 int precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
988 if (!precision
|| precision
> (ION_TS_FRAC
|0x80)) {
989 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
990 "Invalid precision (%d) of ion\\Timestamp", precision
);
991 } else switch ((buf
->precision
= precision
) & 0x7f) {
993 ion_ts_frac_from_usec(&buf
->fraction
, (int) ts
->time
->us
, ctx
);
996 buf
->seconds
= ts
->time
->s
;
999 buf
->minutes
= ts
->time
->i
;
1002 buf
->hours
= ts
->time
->h
;
1003 buf
->day
= ts
->time
->d
;
1006 buf
->month
= ts
->time
->m
;
1009 buf
->year
= ts
->time
->y
;
1012 buf
->tz_offset
= (short) (ts
->time
->z
/ 60);
1013 if (buf
->tz_offset
) {
1014 buf
->precision
|= 0x80;
1021 static void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
1024 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
1026 zend_update_property_long(ce_Timestamp
, &obj
->std
, ZEND_STRL("precision"), precision
);
1028 fmt
= php_dt_format_from_precision(precision
);
1029 zend_update_property_str(ce_Timestamp
, &obj
->std
, ZEND_STRL("format"), fmt
);
1030 zend_string_release(fmt
);
1033 typedef struct php_ion_catalog
{
1038 static void php_ion_catalog_ctor(php_ion_catalog
*obj
)
1040 ION_CHECK(ion_catalog_open(&obj
->cat
));
1043 static void php_ion_catalog_dtor(php_ion_catalog
*obj
)
1046 ion_catalog_close(obj
->cat
);
1050 static ION_COLLECTION
*php_ion_catalog_collection(php_ion_catalog
*cat
)
1052 /* do not look too close */
1055 ION_SYMBOL_TABLE
*sys
;
1056 ION_COLLECTION collection
;
1057 } *cat_ptr
= (void *) cat
->cat
;
1058 return &cat_ptr
->collection
;
1060 // see https://github.com/amzn/ion-c/issues/269
1061 #ifndef IPCN_pNODE_TO_pDATA
1062 # define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0]))
1065 static void php_ion_catalog_copy(php_ion_catalog
*new_obj
, php_ion_catalog
*old_obj
)
1067 // do not clone cache members
1068 php_ion_catalog_ctor(new_obj
);
1071 ION_COLLECTION
*col
= php_ion_catalog_collection(old_obj
);
1072 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1073 ION_COLLECTION_CURSOR cur
;
1074 ION_COLLECTION_OPEN(col
, cur
);
1076 ION_SYMBOL_TABLE
**ptr
;
1077 ION_COLLECTION_NEXT(cur
, ptr
);
1079 ION_CHECK(ion_catalog_add_symbol_table(new_obj
->cat
, *ptr
));
1085 static zend_string
*ion_symbol_table_to_key(ION_SYMBOL_TABLE
*tab
)
1089 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_name(tab
, &is
));
1090 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_version(tab
, &version
));
1093 smart_str_appendl(&s
, (char *) is
.value
, is
.length
);
1094 smart_str_appendc(&s
, ':');
1095 smart_str_append_long(&s
, version
);
1101 static void php_ion_catalog_add_symbol_table(php_ion_catalog
*obj
, php_ion_symbol_table
*tab
)
1107 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1109 zend_ulong idx
= (uintptr_t) &tab
->std
.gc
;
1110 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs
), idx
)) {
1111 zend_string
*key
= ion_symbol_table_to_key(tab
->tab
);
1113 ION_CHECK(ion_catalog_add_symbol_table(obj
->cat
, tab
->tab
),
1114 zend_string_release(key
));
1115 SEPARATE_ARRAY(ztabs
);
1116 GC_ADDREF(&tab
->std
);
1117 add_index_object(ztabs
, idx
, &tab
->std
);
1118 GC_ADDREF(&tab
->std
);
1119 add_assoc_object_ex(ztabs
, key
->val
, key
->len
, &tab
->std
);
1120 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), ztabs
);
1121 zend_string_release(key
);
1127 static void php_ion_catalog_symbol_table_zval(php_ion_catalog
*obj
, ION_SYMBOL_TABLE
*tab
, zval
*return_value
)
1129 zend_string
*key
= ion_symbol_table_to_key(tab
);
1133 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1135 zval
*ztab
= zend_hash_find(Z_ARRVAL_P(ztabs
), key
);
1137 zend_string_release(key
);
1142 object_init_ex(return_value
, ce_Symbol_Table_Shared
);
1143 php_ion_symbol_table
*o_tab
= php_ion_obj(symbol_table
, Z_OBJ_P(return_value
));
1145 php_ion_symbol_table_ctor(o_tab
);
1148 SEPARATE_ARRAY(ztabs
);
1149 ZVAL_ADDREF(return_value
);
1150 add_index_zval(ztabs
, (uintptr_t) &o_tab
->std
.gc
, return_value
);
1151 ZVAL_ADDREF(return_value
);
1152 add_assoc_zval_ex(ztabs
, key
->val
, key
->len
, return_value
);
1154 zend_string_release(key
);
1157 php_ion_decl(catalog
, Catalog
);
1159 typedef struct php_ion_reader_options_ccn_ctx
{
1161 zend_fcall_info fci
;
1162 zend_fcall_info_cache fcc
;
1163 } php_ion_reader_options_ccn_ctx
;
1165 typedef struct php_ion_reader_options
{
1166 ION_READER_OPTIONS opt
;
1167 php_ion_reader_options_ccn_ctx ccn
;
1168 zend_object
*cat
, *dec_ctx
, *cb
, std
;
1169 } php_ion_reader_options
;
1171 static void php_ion_reader_options_dtor(php_ion_reader_options
*obj
)
1174 zend_fcall_info_args_clear(&obj
->ccn
.fci
, true);
1178 static void php_ion_reader_options_copy(php_ion_reader_options
*new_obj
, php_ion_reader_options
*old_obj
)
1180 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1182 new_obj
->opt
= old_obj
->opt
;
1183 new_obj
->cat
= old_obj
->cat
;
1184 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1185 new_obj
->cb
= old_obj
->cb
;
1188 ZVAL_OBJ(&zcb
, new_obj
->cb
);
1189 zend_fcall_info_init(&zcb
, 0, &new_obj
->ccn
.fci
, &new_obj
->ccn
.fcc
, NULL
, NULL
);
1190 new_obj
->opt
.context_change_notifier
.context
= &new_obj
->ccn
;
1194 php_ion_decl(reader_options
, Reader_Options
);
1196 typedef struct php_ion_reader
{
1204 zend_string
*buffer
;
1210 zend_object
*opt
, std
;
1213 static iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
1215 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
1216 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
1218 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
1219 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
1220 user
->limit
-= remaining
;
1223 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
1226 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
1227 if (EXPECTED(read
> 0)) {
1228 user
->limit
+= read
;
1232 if (EXPECTED(read
== 0)) {
1236 return IERR_READ_ERROR
;
1239 static iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1244 php_ion_reader_options_ccn_ctx
*ctx
= context
;
1247 ZVAL_OBJ(&zobj
, ctx
->obj
);
1248 zend_fcall_info_argn(&ctx
->fci
, 1, &zobj
);
1249 if (SUCCESS
!= zend_fcall_info_call(&ctx
->fci
, &ctx
->fcc
, NULL
, NULL
)) {
1250 e
= IERR_INTERNAL_ERROR
;
1252 zend_fcall_info_args_clear(&ctx
->fci
, false);
1257 static void php_ion_reader_ctor(php_ion_reader
*obj
)
1260 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1263 obj
->opt
= object_construct(ce_Reader_Options
, 0, NULL
, NULL
);
1264 update_property_obj_ex(ce_Reader_Reader
, &obj
->std
, ZEND_STRL("options"), obj
->opt
);
1265 OBJ_RELEASE(obj
->opt
);
1266 } else if (opt
->opt
.context_change_notifier
.context
) {
1267 php_ion_reader_options_ccn_ctx
*ctx
= opt
->opt
.context_change_notifier
.context
;
1268 ctx
->obj
= &obj
->std
;
1269 opt
->opt
.context_change_notifier
.notify
= on_context_change
;
1271 if (obj
->type
== STREAM_READER
) {
1272 PTR_CHECK(obj
->stream
.ptr
);
1273 GC_ADDREF(obj
->stream
.ptr
->res
);
1275 obj
->stream
.buf
.length
= opt
&& opt
->opt
.chunk_threshold
? opt
->opt
.chunk_threshold
: 0x4000;
1276 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1277 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1280 err
= ion_reader_open_buffer(&obj
->reader
, (BYTE
*) obj
->buffer
->val
, (SIZE
) obj
->buffer
->len
, opt
? &opt
->opt
: NULL
);
1287 static void php_ion_reader_dtor(php_ion_reader
*obj
)
1290 ion_reader_close(obj
->reader
);
1292 if (obj
->type
== STREAM_READER
) {
1293 if (obj
->stream
.buf
.value
) {
1294 efree(obj
->stream
.buf
.value
);
1296 if (obj
->stream
.ptr
) {
1297 zend_list_delete(obj
->stream
.ptr
->res
);
1301 zend_string_release(obj
->buffer
);
1306 #define php_ion_reader_copy(n,o)
1307 php_ion_decl(reader
, Reader_Reader
);
1308 #define clone_ion_Reader_Reader NULL
1310 typedef struct php_ion_writer_options
{
1311 ION_WRITER_OPTIONS opt
;
1312 zend_object
*cat
, *dec_ctx
, std
;
1313 } php_ion_writer_options
;
1315 static void php_ion_writer_options_copy(php_ion_writer_options
*new_obj
, php_ion_writer_options
*old_obj
)
1317 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1319 new_obj
->opt
= old_obj
->opt
;
1320 new_obj
->cat
= old_obj
->cat
;
1321 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1324 static void php_ion_writer_options_dtor(php_ion_writer_options
*obj
)
1328 php_ion_decl(writer_options
, Writer_Options
);
1330 static zend_object
*php_ion_writer_options_new(void)
1332 zend_object
*obj
= create_ion_Writer_Options(NULL
);
1333 zend_call_known_instance_method_with_0_params(obj
->ce
->constructor
, obj
, NULL
);
1337 typedef struct php_ion_writer
{
1346 struct _ion_user_stream
*usr
;
1353 zend_object
*opt
, std
;
1357 static iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1359 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1361 if (EXPECTED(user
->limit
&& user
->curr
)) {
1362 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1363 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1364 return IERR_WRITE_ERROR
;
1367 user
->curr
= writer
->stream
.buf
.value
;
1368 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1372 static void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1374 PTR_CHECK(obj
->stream
.ptr
);
1375 GC_ADDREF(obj
->stream
.ptr
->res
);
1377 obj
->stream
.buf
.length
= opt
? opt
->opt
.temp_buffer_size
: 0x1000;
1378 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1381 static void php_ion_writer_buffer_offer(php_ion_writer
*obj
)
1383 if (obj
->buffer
.usr
) {
1384 obj
->buffer
.usr
->curr
= (BYTE
*) &obj
->buffer
.str
.s
->val
[obj
->buffer
.str
.s
->len
];
1385 obj
->buffer
.usr
->limit
= obj
->buffer
.usr
->curr
+ obj
->buffer
.str
.a
- obj
->buffer
.str
.s
->len
;
1389 static void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1391 smart_str_alloc(&obj
->buffer
.str
, 0, false);
1392 php_ion_writer_buffer_offer(obj
);
1395 static void php_ion_writer_buffer_reset(php_ion_writer
*obj
)
1397 smart_str_free(&obj
->buffer
.str
);
1398 memset(&obj
->buffer
.str
, 0, sizeof(obj
->buffer
.str
));
1399 php_ion_writer_buffer_init(obj
);
1402 static zend_string
*php_ion_writer_buffer_copy(php_ion_writer
*obj
)
1404 if (obj
->buffer
.usr
) {
1405 // ensure that brain-dead ion_stream interface calls us again
1406 obj
->buffer
.usr
->curr
= NULL
;
1407 obj
->buffer
.usr
->limit
= NULL
;
1409 smart_str_0(&obj
->buffer
.str
);
1410 return zend_string_copy(obj
->buffer
.str
.s
);
1413 static void php_ion_writer_buffer_separate(php_ion_writer
*obj
, bool grow
)
1415 // see zend_string_separate and smart_str_erealloc
1416 zend_string
*old_str
= obj
->buffer
.str
.s
;
1417 zend_string
*new_str
= zend_string_alloc(obj
->buffer
.str
.a
<< grow
, false);
1418 memcpy(new_str
->val
, old_str
->val
, new_str
->len
= old_str
->len
);
1419 zend_string_release(old_str
);
1420 obj
->buffer
.str
.s
= new_str
;
1423 static void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1425 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
) {
1426 obj
->buffer
.str
.s
->len
= obj
->buffer
.usr
->curr
- (BYTE
*) obj
->buffer
.str
.s
->val
;
1428 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
&& obj
->buffer
.usr
->curr
== obj
->buffer
.usr
->limit
) {
1429 if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1430 php_ion_writer_buffer_separate(obj
, true);
1432 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1434 } else if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1435 php_ion_writer_buffer_separate(obj
, false);
1437 php_ion_writer_buffer_offer(obj
);
1441 static iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1443 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1444 writer
->buffer
.usr
= user
;
1445 php_ion_writer_buffer_grow(writer
);
1449 static void php_ion_writer_options_init_shared_imports(php_ion_writer_options
*opt
)
1451 php_ion_catalog
*cat
= php_ion_obj(catalog
, opt
->cat
);
1454 ION_CHECK(ion_writer_options_initialize_shared_imports(&opt
->opt
));
1456 ION_COLLECTION
*col
= php_ion_catalog_collection(cat
);
1457 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1458 // holy, nah, forget it batman...
1459 ION_COLLECTION_CURSOR cur
;
1460 ION_COLLECTION_OPEN(col
, cur
);
1462 ION_SYMBOL_TABLE
**ptr
;
1463 ION_COLLECTION_NEXT(cur
, ptr
);
1465 ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt
->opt
, ptr
, 1));
1471 static void php_ion_writer_ctor(php_ion_writer
*obj
)
1473 php_ion_writer_options
*opt
= NULL
;
1476 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1477 opt
= php_ion_obj(writer_options
, obj
->opt
);
1479 php_ion_writer_options_init_shared_imports(opt
);
1482 obj
->opt
= object_construct(ce_Writer_Options
, 0, NULL
, NULL
);
1483 update_property_obj_ex(ce_Writer_Options
, &obj
->std
, ZEND_STRL("options"), obj
->opt
);
1484 OBJ_RELEASE(obj
->opt
);
1487 ION_STREAM_HANDLER h
;
1488 if (obj
->type
== STREAM_WRITER
) {
1489 h
= php_ion_writer_stream_handler
;
1490 php_ion_writer_stream_init(obj
, opt
);
1492 h
= php_ion_writer_buffer_handler
;
1493 php_ion_writer_buffer_init(obj
);
1496 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1500 static void php_ion_writer_dtor(php_ion_writer
*obj
)
1503 ion_writer_close(obj
->writer
);
1506 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1508 ion_writer_options_close_shared_imports(&opt
->opt
);
1511 if (obj
->type
== STREAM_WRITER
) {
1512 if (obj
->stream
.buf
.value
) {
1513 efree(obj
->stream
.buf
.value
);
1515 if (obj
->stream
.ptr
) {
1516 zend_list_delete(obj
->stream
.ptr
->res
);
1519 if (obj
->buffer
.str
.s
) {
1520 smart_str_0(&obj
->buffer
.str
);
1521 zend_string_release(obj
->buffer
.str
.s
);
1526 #define php_ion_writer_copy(o,n)
1527 php_ion_decl(writer
, Writer_Writer
);
1528 #define clone_ion_Writer_Writer NULL
1530 static bool can_serialize_fast(php_ion_serializer
*ser
)
1532 if (ser
->wri
->ce
!= ce_Writer_Buffer_Writer
&& ser
->wri
->ce
!= ce_Writer_Stream_Writer
) {
1536 if (ser
->std
.ce
!= ce_Serializer_Serializer
) {
1543 static void php_ion_serializer_ctor(php_ion_serializer
*ser_obj
)
1545 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1546 ser_obj
->ids
= global_ser
->ids
;
1547 ser_obj
->tmp
= global_ser
->tmp
;
1549 zend_update_property_bool(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
1550 ser_obj
->multi_seq
);
1551 zend_update_property_bool(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1552 ser_obj
->call_magic
);
1553 if (ser_obj
->call_custom
) {
1554 zend_update_property_str(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1555 ser_obj
->call_custom
);
1556 ser_obj
->call_custom
= zend_string_tolower(ser_obj
->call_custom
);
1558 zend_update_property_null(ce_Serializer_Serializer
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1562 static void php_ion_serializer_dtor(php_ion_serializer
*obj
)
1564 if (obj
->call_custom
) {
1565 zend_string_release(obj
->call_custom
);
1569 static void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1571 static void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool unmangle_props
, bool annotate_props
)
1573 if (can_serialize_fast(ser
)) {
1574 ION_CHECK(ion_writer_start_container(php_ion_obj(writer
, ser
->wri
)->writer
, tid_STRUCT
));
1577 ZVAL_OBJ(&z_type
, php_ion_type_fetch(tid_STRUCT
));
1578 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "startContainer", NULL
, &z_type
);
1584 zend_string
*k
= NULL
;
1585 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1586 char buf
[MAX_LENGTH_OF_LONG
+ 1];
1590 const char *class_name
, *prop_name
;
1591 if (unmangle_props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1592 if (annotate_props
) {
1593 if (can_serialize_fast(ser
)) {
1594 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_PROPERTY
));
1595 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
,
1596 ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1598 zval z_ann_prop
, z_ann_class
;
1599 ZVAL_CHAR(&z_ann_prop
, 'p');
1600 ZVAL_STRINGL(&z_ann_class
, class_name
, prop_name
- class_name
);
1601 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_prop
, &z_ann_class
);
1602 zval_ptr_dtor(&z_ann_class
);
1610 ion_string_assign_cstr(&is
, (char *) prop_name
, (SIZE
) prop_len
);
1612 char *end
= buf
+ sizeof(buf
) - 1;
1613 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1614 ion_string_assign_cstr(&is
, ptr
, (SIZE
) (end
- ptr
));
1617 if (can_serialize_fast(ser
)) {
1618 // WATCH OUT: field names need to be copied
1620 ION_CHECK(ion_string_copy_to_owner(php_ion_obj(writer
, ser
->wri
)->writer
, &fn
, &is
));
1621 ION_CHECK(ion_writer_write_field_name(php_ion_obj(writer
, ser
->wri
)->writer
, &fn
));
1624 ZVAL_STRINGL(&z_field_name
, (const char *) is
.value
, is
.length
);
1625 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeFieldName", NULL
, &z_field_name
);
1626 zval_ptr_dtor(&z_field_name
);
1630 php_ion_serialize_zval(ser
, v
);
1632 ZEND_HASH_FOREACH_END();
1634 if(can_serialize_fast(ser
)) {
1635 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer
, ser
->wri
)->writer
));
1637 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "finishContainer", NULL
);
1641 static void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1643 if (can_serialize_fast(ser
)) {
1644 ION_CHECK(ion_writer_start_container(php_ion_obj(writer
, ser
->wri
)->writer
, tid_LIST
));
1647 ZVAL_OBJ(&z_type
, php_ion_type_fetch(tid_LIST
));
1648 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "startContainer", NULL
, &z_type
);
1653 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1654 php_ion_serialize_zval(ser
, v
);
1656 ZEND_HASH_FOREACH_END();
1658 if (can_serialize_fast(ser
)) {
1659 ION_CHECK(ion_writer_finish_container(php_ion_obj(writer
, ser
->wri
)->writer
));
1661 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "finishContainer", NULL
);
1665 static void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1671 ZVAL_OBJ(&tmp
, zobject
);
1672 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1673 if (can_serialize_fast(ser
)) {
1675 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_SERIALIZEABLE
));
1676 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1677 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer
, ser
->wri
)->writer
, buf
, len
));
1680 zval z_ann_srlzbl
, z_ann_class
, z_clob
;
1681 ZVAL_CHAR(&z_ann_srlzbl
, 'S');
1682 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1683 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_srlzbl
, &z_ann_class
);
1684 zval_ptr_dtor(&z_ann_class
);
1686 ZVAL_STRINGL(&z_clob
, (const char *) buf
, len
);
1687 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeCLob", NULL
, &z_clob
);
1688 zval_ptr_dtor(&z_clob
);
1690 } else if (!EG(exception
)){
1691 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1692 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1696 static void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1701 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1704 if (IS_ARRAY
== Z_TYPE(rv
)) {
1705 if (can_serialize_fast(ser
)) {
1707 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
));
1708 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1710 zval z_ann_cust
, z_ann_class
;
1711 ZVAL_CHAR(&z_ann_cust
, 'C');
1712 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1713 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_cust
, &z_ann_class
);
1714 zval_ptr_dtor(&z_ann_class
);
1716 if (!EG(exception
)) {
1717 php_ion_serialize_zval(ser
, &rv
);
1721 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1722 "%s serializer %s::%s did not return an array",
1723 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1724 fn
? fn
->common
.function_name
->val
: "__serialize");
1728 static void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1730 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1732 if (can_serialize_fast(ser
)) {
1734 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_ENUM
));
1735 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1736 ION_CHECK(ion_writer_write_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1738 zval z_ann_enm
, z_ann_class
;
1739 ZVAL_CHAR(&z_ann_enm
, 'E');
1740 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1741 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_enm
, &z_ann_class
);
1742 zval_ptr_dtor(&z_ann_class
);
1744 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeSymbol", NULL
, z_cname
);
1748 static void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1750 if (can_serialize_fast(ser
)) {
1753 if (zobject
->ce
!= zend_standard_class_def
) {
1754 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_CLASS_OBJECT
));
1755 ION_CHECK(ion_writer_add_annotation(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1757 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_OBJECT
));
1760 if (zobject
->ce
!= zend_standard_class_def
) {
1761 zval z_ann_cobj
, z_ann_class
;
1762 ZVAL_CHAR(&z_ann_cobj
, 'c');
1763 ZVAL_STR_COPY(&z_ann_class
, zobject
->ce
->name
);
1764 zend_call_method_with_2_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_cobj
, &z_ann_class
);
1765 zval_ptr_dtor(&z_ann_class
);
1768 ZVAL_CHAR(&z_ann_obj
, 'o');
1769 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_obj
);
1775 ZVAL_OBJ(&zobj
, zobject
);
1776 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1778 php_ion_serialize_struct(ser
, props
, true, true);
1779 zend_release_properties(props
);
1781 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1782 "Could not get properties for serialization of class %s",
1783 zobject
->ce
->name
->val
);
1787 static void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1789 zval tmp_type
, *type
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_TYPE
), 0, &tmp_type
);
1790 zval tmp_value
, *value
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_VALUE
), 0, &tmp_value
);
1792 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1794 if (can_serialize_fast(ser
)) {
1795 ION_CHECK(ion_writer_write_blob(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
, "writeBLob", NULL
, value
);
1801 if (can_serialize_fast(ser
)) {
1802 ION_CHECK(ion_writer_write_clob(php_ion_obj(writer
, ser
->wri
)->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1804 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeCLob", NULL
, value
);
1808 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1809 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1814 static bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1816 return ce
->__serialize
&& ser
->call_magic
;
1819 static bool can_call_iface_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1822 return !!ce
->serialize
; // NOLINT
1825 static bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1827 if (ser
->call_custom
) {
1828 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
1833 static bool is_special_class(const zend_class_entry
*ce
, zend_class_entry
**target
) {
1834 #define IS_TARGET(_ce) \
1835 if (instanceof_function(ce, _ce)) { \
1839 IS_TARGET(ce_Symbol
);
1840 IS_TARGET(ce_Decimal
);
1841 IS_TARGET(ce_Timestamp
);
1847 static void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
) {
1849 zend_class_entry
*special_ce
, *ce
= zobject
->ce
;
1852 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1853 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1854 "Serializing %s is not allowed", ce
->name
->val
);
1858 if (can_call_magic_serialize(ser
, ce
)) {
1859 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1860 } else if (can_call_iface_serialize(ser
, ce
)) {
1861 php_ion_serialize_object_iface(ser
, zobject
);
1862 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1863 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1864 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1865 php_ion_serialize_object_enum(ser
, zobject
);
1866 } else if (!is_special_class(ce
, &special_ce
)) {
1867 php_ion_serialize_object_std(ser
, zobject
);
1869 if (can_serialize_fast(ser
) || special_ce
== ce_LOB
) {
1870 if (special_ce
== ce_Symbol
) {
1871 ION_CHECK(ion_writer_write_ion_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1872 } else if (special_ce
== ce_Decimal
) {
1873 ION_CHECK(ion_writer_write_ion_decimal(php_ion_obj(writer
, ser
->wri
)->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1874 } else if (special_ce
== ce_Timestamp
) {
1876 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1877 decContext
*ctx
= php_ion_obj(writer_options
, php_ion_obj(writer
, ser
->wri
)->opt
)->opt
.decimal_context
;
1878 ION_CHECK(ion_writer_write_timestamp(php_ion_obj(writer
, ser
->wri
)->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1880 assert(special_ce
== ce_LOB
);
1881 php_ion_serialize_object_lob(ser
, zobject
);
1885 const char *method
= NULL
;
1887 ZVAL_OBJ(&z_param
, zobject
);
1888 if (special_ce
== ce_Symbol
) {
1889 method
= "writeSymbol";
1890 } else if (special_ce
== ce_Decimal
) {
1891 method
= "writeDecimal";
1892 } else if (special_ce
== ce_Timestamp
) {
1893 method
= "writeTimestamp";
1897 zend_call_method(ser
->wri
, NULL
, NULL
, method
, strlen(method
), NULL
, 1, &z_param
, NULL
);
1902 static bool php_ion_serialize_system_value(php_ion_serializer
*ser
, zval
*zv
)
1904 if (1 == php_ion_globals
.serializer
.level
) {
1905 if (Z_TYPE_P(zv
) == IS_OBJECT
) {
1906 if (Z_OBJCE_P(zv
) == ce_Symbol_Table_Shared
) {
1907 php_ion_symbol_table
*obj
= php_ion_obj(symbol_table
, Z_OBJ_P(zv
));
1908 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj
->tab
, php_ion_obj(writer
, ser
->wri
)->writer
));
1916 static bool php_ion_serialize_backref(php_ion_serializer
*ser
, zval
*zv
)
1918 if (Z_TYPE_P(zv
) == IS_STRING
&& Z_STR_P(zv
) == zend_empty_string
) {
1921 if (Z_TYPE_P(zv
) == IS_ARRAY
&& Z_ARR_P(zv
) == &zend_empty_array
) {
1925 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1926 zval
*ref
= zend_hash_index_find(ser
->ids
, idx
);
1930 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1931 zend_hash_index_add(ser
->ids
, idx
, &num
);
1934 zend_hash_next_index_insert(ser
->tmp
, zv
);
1939 if (can_serialize_fast(ser
)) {
1940 ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_BACKREF
));
1941 ION_CHECK_RETURN(true, ion_writer_write_int64(php_ion_obj(writer
, ser
->wri
)->writer
, Z_LVAL_P(ref
)));
1944 ZVAL_CHAR(&z_ann_bref
, 'r');
1945 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_bref
);
1946 ION_CATCH_RETURN(true);
1947 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeInt", NULL
, ref
);
1952 static void php_ion_serialize_string(php_ion_serializer
*ser
, zend_string
*str
)
1954 if (can_serialize_fast(ser
)) {
1956 ION_CHECK(ion_writer_write_string(php_ion_obj(writer
, ser
->wri
)->writer
, ion_string_from_zend(&is
, str
)));
1962 static void php_ion_serialize_reference(php_ion_serializer
*ser
, zval
*noref
)
1964 if (can_serialize_fast(ser
)) {
1965 ION_CHECK(ion_writer_add_annotation_symbol(php_ion_obj(writer
, ser
->wri
)->writer
, &PHP_ION_SYMBOL_REFERENCE
));
1968 ZVAL_CHAR(&z_ann_ref
, 'R');
1969 zend_call_method_with_1_params(ser
->wri
, NULL
, NULL
, "writeAnnotation", NULL
, &z_ann_ref
);
1972 php_ion_serialize_zval(ser
, noref
);
1975 static void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1977 if (php_ion_serialize_system_value(ser
, zv
)) {
1980 if (php_ion_serialize_backref(ser
, zv
)) {
1984 switch (Z_TYPE_P(zv
)) {
1986 php_ion_serialize_string(ser
, Z_STR_P(zv
));
1990 if (zend_array_is_list(Z_ARRVAL_P(zv
))) {
1991 php_ion_serialize_list(ser
, Z_ARRVAL_P(zv
));
1993 php_ion_serialize_struct(ser
, Z_ARRVAL_P(zv
), false, false);
1998 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
2002 php_ion_serialize_reference(ser
, Z_REFVAL_P(zv
));
2007 static void php_ion_serialize_scalar(php_ion_serializer
*ser
, zval
*zv
)
2009 if (can_serialize_fast(ser
)) {
2010 switch (Z_TYPE_P(zv
)) {
2012 ION_CHECK(ion_writer_write_null(php_ion_obj(writer
, ser
->wri
)->writer
));
2015 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer
, ser
->wri
)->writer
, TRUE
));
2018 ION_CHECK(ion_writer_write_bool(php_ion_obj(writer
, ser
->wri
)->writer
, FALSE
));
2021 ION_CHECK(ion_writer_write_int64(php_ion_obj(writer
, ser
->wri
)->writer
, Z_LVAL_P(zv
)));
2024 ION_CHECK(ion_writer_write_double(php_ion_obj(writer
, ser
->wri
)->writer
, Z_DVAL_P(zv
)));
2028 if (Z_ISNULL_P(zv
)) {
2029 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "writeNull", NULL
);
2031 const char *method
= NULL
;
2033 if (Z_TYPE_P(zv
) < IS_LONG
) {
2034 method
= "writeBool";
2035 } else if (Z_TYPE_P(zv
) < IS_DOUBLE
) {
2036 method
= "writeInt";
2038 method
= "writeFloat";
2041 zend_call_method(ser
->wri
, NULL
, NULL
, method
, strlen(method
), NULL
, 1, zv
, NULL
);
2046 static void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
2050 switch (Z_TYPE_P(zv
)) {
2056 php_ion_serialize_scalar(ser
, zv
);
2062 php_ion_serialize_refcounted(ser
, zv
);
2065 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2066 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
2070 #define php_ion_serializer_copy(o,n)
2071 php_ion_decl(serializer
, Serializer_Serializer
);
2072 #define clone_ion_Serializer NULL
2074 static void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
2077 HashTable
*arr
= NULL
;
2079 if (ser
->multi_seq
) {
2080 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
2081 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2082 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
2086 arr
= Z_ARRVAL_P(zv
);
2088 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
2089 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
2093 php_ion_globals_serializer_step();
2094 php_ion_serialize_zval(ser
, zv
);
2095 php_ion_globals_serializer_exit();
2097 if (!ser
->multi_seq
) {
2100 zend_hash_move_forward_ex(arr
, &pos
);
2101 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
2105 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
2107 zend_object
*zo_ser
= NULL
, *zo_wri
= NULL
;
2110 zo_ser
= create_ion_Serializer_Serializer(NULL
);
2111 ser
= php_ion_obj(serializer
, zo_ser
);
2113 ser
->call_magic
= true;
2114 php_ion_serializer_ctor(ser
);
2118 if (!ser
->wri
|| !instanceof_function(ser
->wri
->ce
, ce_Writer
)) {
2119 zo_wri
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
2120 php_ion_writer
*wri
= php_ion_obj(writer
, zo_wri
);
2121 wri
->type
= BUFFER_WRITER
;
2122 if (ser
->wri
&& instanceof_function(ser
->wri
->ce
, ce_Writer_Options
)) {
2123 wri
->opt
= ser
->wri
;
2125 php_ion_writer_ctor(wri
);
2129 if (!EG(exception
)) {
2130 php_ion_serialize_ex(ser
, zv
);
2133 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
2134 if (can_serialize_fast(ser
)) {
2135 ion_writer_flush(php_ion_obj(writer
, ser
->wri
)->writer
, NULL
);
2137 zend_call_method_with_0_params(ser
->wri
, NULL
, NULL
, "flush", NULL
);
2139 RETVAL_STR_COPY(php_ion_obj(writer
, ser
->wri
)->buffer
.str
.s
);
2142 OBJ_RELEASE(zo_wri
);
2145 OBJ_RELEASE(zo_ser
);
2149 static bool can_unserialize_fast(php_ion_unserializer
*ser
)
2151 if (ser
->rdr
->ce
!= ce_Reader_Buffer_Reader
&& ser
->rdr
->ce
!= ce_Reader_Stream_Reader
) {
2155 if (ser
->std
.ce
!= ce_Unserializer_Unserializer
) {
2162 static void php_ion_unserializer_ctor(php_ion_unserializer
*ser_obj
)
2164 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
2165 ser_obj
->ids
= global_ser
->ids
;
2166 ser_obj
->tmp
= global_ser
->tmp
;
2167 ser_obj
->addref
= global_ser
->addref
;
2169 zend_update_property_bool(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
2170 ser_obj
->multi_seq
);
2171 zend_update_property_bool(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callMagicUnserialize"),
2172 ser_obj
->call_magic
);
2173 if (ser_obj
->call_custom
) {
2174 zend_update_property_str(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"),
2175 ser_obj
->call_custom
);
2176 ser_obj
->call_custom
= zend_string_tolower(ser_obj
->call_custom
);
2178 zend_update_property_null(ce_Unserializer_Unserializer
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"));
2182 static void php_ion_unserializer_dtor(php_ion_unserializer
*obj
)
2184 if (obj
->call_custom
) {
2185 zend_string_release(obj
->call_custom
);
2189 static void php_ion_unserializer_next(php_ion_unserializer
*ser
, ION_TYPE
*typ
)
2191 if (can_unserialize_fast(ser
)) {
2192 ION_CHECK(ion_reader_next(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2194 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "next", NULL
);
2197 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "key", &z_type
);
2199 *typ
= ion_type_from_enum(Z_OBJ(z_type
));
2203 static void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
2205 static bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
2207 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
2210 static bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
2212 return (ce
&& ce
->unserialize
);
2215 static bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2217 if (ser
->call_custom
) {
2218 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
2223 static zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
2225 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2228 object_init_ex(return_value
, ce
);
2229 return zend_hash_next_index_insert(ser
->ids
, return_value
);
2232 zend_throw_exception_ex(ce_Exception
, IERR_IMPORT_NOT_FOUND
,
2233 "Could not find class %s", ser
->annotations
.object_class
->val
);
2237 static void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
2239 zend_string
*zs_case
= zval_get_string(return_value
);
2240 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2241 ZVAL_NULL(return_value
);
2244 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2245 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
2246 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2247 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
2250 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
2251 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2252 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
2255 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
2256 zend_hash_next_index_insert(ser
->ids
, return_value
);
2257 zend_string_release(zs_case
);
2260 static void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
2262 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2263 if (can_call_iface_unserialize(ser
, ce
)) {
2264 zend_string
*s
= zval_get_string(return_value
);
2265 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2266 ZVAL_NULL(return_value
);
2267 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
2268 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
2269 RETVAL_ZVAL(backref
, 0, 0);
2271 zval_ptr_dtor(backref
);
2273 if (!EG(exception
)) {
2274 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2275 "Failed to unserialize class %s", ce
->name
->val
);
2278 zend_string_release(s
);
2280 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2281 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
2285 static void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
, bool is_prop
)
2287 if (can_unserialize_fast(ser
)) {
2288 // FIXME: symbol table?
2290 SID sid
= UNKNOWN_SID
;
2291 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
2293 ION_CHECK(ion_reader_get_field_name(php_ion_obj(reader
, ser
->rdr
)->reader
, &name
));
2296 ION_CHECK(ion_reader_get_field_name_symbol(php_ion_obj(reader
, ser
->rdr
)->reader
, &is_ptr
));
2297 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
2298 ION_STRING_ASSIGN(&name
, &is_ptr
->value
);
2304 switch (name
.length
) {
2306 ptr
= zend_print_long_to_buf(end
, sid
);
2308 *key
= zend_string_init(ptr
, end
- ptr
, 0);
2311 *key
= ZSTR_CHAR(*name
.value
);
2315 *key
= zend_string_init_interned((char *) name
.value
, name
.length
, 0);
2317 *key
= zend_string_from_ion(&name
);
2324 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getFieldName", &z_fn
);
2325 if (!Z_STRLEN(z_fn
) || EG(exception
)) {
2326 zend_clear_exception();
2327 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getFieldNameSymbol", &z_fn
);
2331 *key
= zval_get_string(&z_fn
);
2332 zval_ptr_dtor(&z_fn
);
2336 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
2338 zend_hash_next_index_insert(ser
->ids
, return_value
);
2340 if (can_unserialize_fast(ser
)) {
2341 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2343 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2349 php_ion_unserializer_next(ser
, &typ
);
2352 if (typ
== tid_EOF
) {
2357 php_ion_unserialize_field_name(ser
, &key
, true);
2361 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2362 ION_CATCH(zend_string_release(key
));
2364 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
2365 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
2366 ce
= zend_lookup_class(ser
->annotations
.property_class
);
2368 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
2369 zval_ptr_dtor(&zvalue
);
2370 zend_string_release(key
);
2373 if (can_unserialize_fast(ser
)) {
2374 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2379 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
2380 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
2381 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
2382 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
2383 * nondeterministic behavior.
2385 static void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
2387 zend_hash_next_index_insert(ser
->ids
, return_value
);
2389 if (can_unserialize_fast(ser
)) {
2390 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2392 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2398 php_ion_unserializer_next(ser
, &typ
);
2401 if (typ
== tid_EOF
) {
2406 php_ion_unserialize_field_name(ser
, &key
, false);
2410 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2411 ION_CATCH(zend_string_release(key
));
2413 // FIXME:: too naive; b0rked if the previous value is an array
2414 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
2415 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
2416 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
2418 Z_TRY_ADDREF_P(prev
);
2419 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
2420 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
2422 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
2424 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
2426 zend_string_release(key
);
2429 if (can_unserialize_fast(ser
)) {
2430 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2434 static void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2436 switch (ser
->annotations
.object_type
) {
2442 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
2443 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2444 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
2449 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
2450 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2451 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
2453 *fn
= zobject
->ce
->__unserialize
;
2457 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2458 "Invalid object type %c", ser
->annotations
.object_type
);
2462 static void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
2464 // backup possible backref to array returned by magic/custom __serialize()
2465 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
2466 ZVAL_NULL(return_value
);
2467 php_ion_unserialize_class(ser
, return_value
);
2470 zend_function
*fn
= NULL
;
2471 zend_object
*zobject
= Z_OBJ_P(return_value
);
2472 verify_unserializer(ser
, zobject
, &fn
);
2477 php_ion_unserialize_props(ser
, return_value
);
2482 if (Z_TYPE_P(input
) != IS_ARRAY
) {
2483 zval_ptr_dtor(input
);
2485 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
2486 php_ion_unserialize_hash(ser
, input
);
2491 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
2495 static void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
2497 if (ser
->annotations
.object_class
) {
2498 switch (ser
->annotations
.object_type
) {
2500 php_ion_unserialize_object_iface(ser
, return_value
);
2503 php_ion_unserialize_object_enum(ser
, return_value
);
2506 php_ion_unserialize_object(ser
, return_value
);
2508 } else if (!ser
->annotations
.object_type
) {
2509 bool is_shared_symtab
= ser
->annotations
.shared_symtab
;
2510 array_init(return_value
);
2511 php_ion_unserialize_hash(ser
, return_value
);
2512 if (is_shared_symtab
) {
2513 zend_object
*zo_ss
= object_construct(ce_Symbol_Table_Shared
, 0, NULL
, Z_ARRVAL_P(return_value
));
2514 zval_ptr_dtor(return_value
);
2517 } else if (ser
->annotations
.object_type
== 'o') {
2518 object_init(return_value
);
2519 php_ion_unserialize_hash(ser
, return_value
);
2521 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2522 "Invalid object annotation %c::", ser
->annotations
.object_type
);
2526 static void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
2528 if (can_unserialize_fast(ser
)) {
2529 ION_CHECK(ion_reader_step_in(php_ion_obj(reader
, ser
->rdr
)->reader
));
2531 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getChildren", NULL
);
2533 array_init(return_value
);
2534 zend_hash_next_index_insert(ser
->ids
, return_value
);
2538 php_ion_unserializer_next(ser
, &typ
);
2541 if (typ
== tid_EOF
) {
2546 php_ion_unserialize_zval(ser
, &next
, &typ
);
2549 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
2552 if (can_unserialize_fast(ser
)) {
2553 ION_CHECK(ion_reader_step_out(php_ion_obj(reader
, ser
->rdr
)->reader
));
2555 // userland reader object already stepped out
2559 static void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
2561 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
2564 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, (SIZE
) zstr
->len
, &read
);
2565 if (err
== IERR_BUFFER_TOO_SMALL
) {
2566 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
2569 ION_CHECK(err
, zend_string_release(zstr
));
2570 if (zstr
->len
> read
) {
2571 zstr
->val
[read
] = 0;
2572 zstr
= zend_string_truncate(zstr
, read
, 0);
2577 static void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
2580 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
2582 object_init_ex(return_value
, ce_Timestamp
);
2583 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
2585 zend_string
*fmt
= NULL
;
2586 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
2587 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
2588 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
2589 zend_string_release(fmt
);
2594 static void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
2596 ION_INT
*num
= NULL
;
2597 ION_CHECK(ion_int_alloc(reader
, &num
));
2598 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
2600 // TODO: SIZEOF_ZEND_LONG == 4
2602 iERR err
= ion_int_to_int64(num
, &i64
);
2608 case IERR_NUMERIC_OVERFLOW
: ;
2610 ION_CHECK(ion_int_char_length(num
, &max
));
2611 zend_string
*zs
= zend_string_alloc(max
, 0);
2613 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2614 zs
->val
[zs
->len
= len
] = 0;
2625 static void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2627 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2630 ZVAL_COPY_VALUE(return_value
, backref
);
2631 zend_hash_next_index_insert(ser
->addref
, return_value
);
2633 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2634 "Could not find back reference " ZEND_LONG_FMT
, Z_LVAL_P(return_value
));
2638 static zend_string
*php_ion_unserialize_annotation(php_ion_unserializer
*ser
, zend_long idx
)
2642 if (can_unserialize_fast(ser
)) {
2644 ION_CHECK_RETURN(NULL
, ion_reader_get_an_annotation(php_ion_obj(reader
, ser
->rdr
)->reader
, idx
, &ann_str
));
2646 if (ann_str
.length
== 1){
2647 // no need to remember interned string
2648 ZVAL_CHAR(&z_ann
, *ann_str
.value
);
2650 ZVAL_STRINGL(&z_ann
, (char *) ann_str
.value
, ann_str
.length
);
2651 zend_hash_next_index_insert(ser
->tmp
, &z_ann
);
2655 ZVAL_LONG(&z_idx
, idx
);
2656 zend_call_method_with_1_params(&ser
->std
, NULL
, NULL
, "getAnnotation", &z_ann
, &z_idx
);
2657 ION_CATCH_RETURN(NULL
);
2658 zend_hash_next_index_insert(ser
->tmp
, &z_ann
);
2661 return Z_STR(z_ann
);
2664 static void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2666 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2669 if (can_unserialize_fast(ser
)) {
2670 ION_CHECK(ion_reader_get_annotation_count(php_ion_obj(reader
, ser
->rdr
)->reader
, &ann_cnt
));
2673 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "countAnnotations", &z_ann_cnt
);
2675 ann_cnt
= Z_LVAL(z_ann_cnt
);
2678 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2679 zend_string
*zs_ann
= php_ion_unserialize_annotation(ser
, i
);
2681 if (zs_ann
->len
!= 1) {
2682 if (zend_string_equals_literal(zs_ann
, ION_SYS_SYMBOL_SHARED_SYMBOL_TABLE
)) {
2683 ser
->annotations
.shared_symtab
= true;
2689 switch (*zs_ann
->val
) {
2695 if (ser
->annotations
.makeref
) {
2696 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2697 "Invalid multiple reference annotations");
2700 ser
->annotations
.makeref
= true;
2704 if (ser
->annotations
.backref
) {
2705 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2706 "Invalid multiple back reference annotations");
2709 ser
->annotations
.backref
= true;
2713 if (ser
->annotations
.object_prop
) {
2714 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2715 "Invalid multiple object property annotations");
2718 ser
->annotations
.object_prop
= true;
2719 ser
->annotations
.property_class
= php_ion_unserialize_annotation(ser
, ++i
);
2729 if (ser
->annotations
.object_type
) {
2730 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2731 "Invalid multiple object type annotations: %c::%c",
2732 ser
->annotations
.object_type
, *zs_ann
->val
);
2736 ser
->annotations
.object_type
= *zs_ann
->val
;
2737 if (ser
->annotations
.object_type
!= 'o') {
2738 ser
->annotations
.object_class
= php_ion_unserialize_annotation(ser
, ++i
);
2745 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2746 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2747 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2750 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2751 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2752 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2758 static void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2761 memcpy(&ser
->type
, typ
, sizeof(ser
->type
)); // NOLINT
2764 if (can_unserialize_fast(ser
)) {
2765 ION_CHECK(ion_reader_next(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2767 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "next", NULL
);
2771 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "getType", &z_type
);
2773 *typ
= ion_type_from_enum(Z_OBJ_P(return_value
));
2777 php_ion_unserialize_annotations(ser
);
2780 if (ser
->annotations
.makeref
) {
2781 ZVAL_MAKE_REF(return_value
);
2782 zend_hash_next_index_insert(ser
->ids
, return_value
);
2783 ZVAL_DEREF(return_value
);
2786 if (ION_TYPE_INT(*typ
) > 0) {
2788 if (can_unserialize_fast(ser
)) {
2789 ION_CHECK(ion_reader_is_null(php_ion_obj(reader
, ser
->rdr
)->reader
, &is_null
));
2792 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "isNull", &z_is_null
);
2793 is_null
= i_zend_is_true(&z_is_null
);
2800 switch (ION_TYPE_INT(*typ
)) {
2802 if (can_unserialize_fast(ser
)) {
2803 ION_CHECK(ion_reader_read_null(php_ion_obj(reader
, ser
->rdr
)->reader
, typ
));
2805 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readNull", return_value
);
2807 *typ
= ion_type_from_enum(Z_OBJ_P(return_value
));
2812 if (can_unserialize_fast(ser
)) {
2814 ION_CHECK(ion_reader_read_bool(php_ion_obj(reader
, ser
->rdr
)->reader
, &bval
));
2817 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readBool", return_value
);
2822 if (can_unserialize_fast(ser
)) {
2823 php_ion_reader_read_int(php_ion_obj(reader
, ser
->rdr
)->reader
, return_value
);
2825 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readInt", return_value
);
2827 if (ser
->annotations
.backref
) {
2829 php_ion_unserialize_backref(ser
, return_value
);
2831 if (ser
->annotations
.object_type
) {
2832 if (!ser
->annotations
.backref
) {
2833 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2834 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2835 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2838 goto unserialize_struct
;
2843 if (can_unserialize_fast(ser
)) {
2845 ION_CHECK(ion_reader_read_double(php_ion_obj(reader
, ser
->rdr
)->reader
, &d
));
2848 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readFloat", return_value
);
2852 case tid_DECIMAL_INT
:
2853 if (can_unserialize_fast(ser
)) {
2854 object_init_ex(return_value
, ce_Decimal
);
2855 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2856 ION_CHECK(ion_reader_read_ion_decimal(php_ion_obj(reader
, ser
->rdr
)->reader
, &dec
->dec
));
2857 php_ion_decimal_ctor(dec
);
2859 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readDecimal", return_value
);
2862 zend_hash_next_index_insert(ser
->ids
, return_value
);
2865 case tid_TIMESTAMP_INT
:
2866 if (can_unserialize_fast(ser
)) {
2867 php_ion_reader
*reader
= php_ion_obj(reader
, ser
->rdr
);
2868 php_ion_reader_read_timestamp(reader
->reader
, &php_ion_obj(reader_options
, reader
->opt
)->opt
, return_value
);
2870 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readTimestamp", return_value
);
2873 zend_hash_next_index_insert(ser
->ids
, return_value
);
2876 case tid_SYMBOL_INT
:
2877 if (can_unserialize_fast(ser
)) {
2879 ION_CHECK(ion_reader_read_ion_symbol(php_ion_obj(reader
, ser
->rdr
)->reader
, &sym
));
2880 php_ion_symbol_zval(&sym
, return_value
);
2882 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readSymbol", return_value
);
2885 if (ser
->annotations
.object_type
) {
2886 goto unserialize_struct
;
2888 zend_hash_next_index_insert(ser
->ids
, return_value
);
2891 case tid_STRING_INT
:
2892 if (can_unserialize_fast(ser
)) {
2894 ION_CHECK(ion_reader_read_string(php_ion_obj(reader
, ser
->rdr
)->reader
, &str
));
2895 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2897 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, "readString", return_value
);
2900 if (ser
->annotations
.object_type
) {
2901 goto unserialize_struct
;
2903 zend_hash_next_index_insert(ser
->ids
, return_value
);
2908 if (can_unserialize_fast(ser
)) {
2909 php_ion_reader_read_lob(php_ion_obj(reader
, ser
->rdr
)->reader
, return_value
);
2911 zend_call_method_with_0_params(&ser
->std
, NULL
, NULL
, *typ
== tid_BLOB
? "readBLob" : "readCLob", return_value
);
2914 if (ser
->annotations
.object_type
) {
2915 goto unserialize_struct
;
2917 zend_hash_next_index_insert(ser
->ids
, return_value
);
2921 case tid_SEXP_INT
: // FIXME
2922 php_ion_unserialize_list(ser
, return_value
);
2924 if (!ser
->annotations
.object_type
) {
2929 case tid_STRUCT_INT
:
2930 unserialize_struct
: ;
2931 php_ion_unserialize_struct(ser
, return_value
);
2935 ZEND_ASSERT(!"none");
2938 case tid_DATAGRAM_INT
:
2939 ZEND_ASSERT(!"datagram");
2945 #define php_ion_unserializer_copy(o,n)
2946 php_ion_decl(unserializer
, Unserializer_Unserializer
);
2947 #define clone_ion_Unserializer NULL
2949 static void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2951 if (ser
->multi_seq
) {
2952 array_init(return_value
);
2958 php_ion_globals_unserializer_step();
2959 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2960 php_ion_globals_unserializer_exit();
2961 ION_CATCH(zval_ptr_dtor(&tmp
));
2963 if (!ser
->multi_seq
) {
2964 RETURN_COPY_VALUE(&tmp
);
2965 } else if (ser
->type
!= tid_EOF
) {
2966 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2968 } while (ser
->type
!= tid_EOF
);
2971 static php_ion_reader
*create_unserialize_reader(zval
*zdata
)
2973 zend_object
*zo_reader
;
2974 php_ion_reader
*reader
= NULL
;
2977 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2978 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2979 reader
= php_ion_obj(reader
, zo_reader
);
2980 reader
->type
= STREAM_READER
;
2981 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2982 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2983 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2984 reader
= php_ion_obj(reader
, zo_reader
);
2985 reader
->type
= BUFFER_READER
;
2986 reader
->buffer
= zval_get_string(zdata
);
2987 } else if (Z_TYPE_P(zdata
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(zdata
), ce_Reader
)) {
2989 reader
= php_ion_obj(reader
, Z_OBJ_P(zdata
));
2991 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2992 "Invalid source to unserialize: expected string or resource, got %s",
2993 zend_zval_type_name(zdata
));
2999 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
3001 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
3004 zo_ser
= create_ion_Unserializer_Unserializer(NULL
);
3005 ser
= php_ion_obj(unserializer
, zo_ser
);
3007 ser
->call_magic
= true;
3008 php_ion_unserializer_ctor(ser
);
3012 php_ion_reader
*reader
= create_unserialize_reader(zdata
);
3014 if (ser
->rdr
&& instanceof_function(ser
->rdr
->ce
, ce_Reader_Options
)) {
3015 zo_opt
= reader
->opt
= ser
->rdr
;
3017 php_ion_reader_ctor(reader
);
3018 ser
->rdr
= &reader
->std
;
3019 if (!EG(exception
)) {
3020 php_ion_unserialize_ex(ser
, return_value
);
3022 OBJ_RELEASE(&reader
->std
);
3026 OBJ_RELEASE(zo_opt
);
3029 OBJ_RELEASE(zo_ser
);