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"
20 # define LOCAL static inline
23 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
26 #define PHP_ION_SYMBOL_TABLE_VERSION 1
27 #define PHP_ION_SYMBOL(c, s) { \
30 { sizeof(s)-1, (BYTE *) s }, \
34 { sizeof(c)-1, (BYTE *) c } \
37 typedef struct php_ion_global_symbol
{
40 } php_ion_global_symbol
;
42 static php_ion_global_symbol g_sym_tab_php_sym
[] = {
43 #define PHP_ION_SYMBOL_PHP (g_sym_tab_php_sym[0]).s
44 PHP_ION_SYMBOL("PHP", "PHP")
45 #define PHP_ION_SYMBOL_REFERENCE (g_sym_tab_php_sym[1]).s
46 PHP_ION_SYMBOL("Reference", "R")
47 #define PHP_ION_SYMBOL_BACKREF (g_sym_tab_php_sym[2]).s
48 PHP_ION_SYMBOL("Backref", "r")
49 #define PHP_ION_SYMBOL_PROPERTY (g_sym_tab_php_sym[3]).s
50 PHP_ION_SYMBOL("Property", "p")
51 #define PHP_ION_SYMBOL_CLASS_OBJECT (g_sym_tab_php_sym[4]).s
52 PHP_ION_SYMBOL("ClassObject", "c")
53 #define PHP_ION_SYMBOL_CUSTOM_OBJECT (g_sym_tab_php_sym[5]).s
54 PHP_ION_SYMBOL("CustomObject", "C")
55 #define PHP_ION_SYMBOL_OBJECT (g_sym_tab_php_sym[6]).s
56 PHP_ION_SYMBOL("Object", "o")
57 #define PHP_ION_SYMBOL_MAGIC_OBJECT (g_sym_tab_php_sym[7]).s
58 PHP_ION_SYMBOL("MagicObject", "O")
59 #define PHP_ION_SYMBOL_SERIALIZEABLE (g_sym_tab_php_sym[8]).s
60 PHP_ION_SYMBOL("Serializable", "S")
61 #define PHP_ION_SYMBOL_ENUM (g_sym_tab_php_sym[9]).s
62 PHP_ION_SYMBOL("Enum", "E")
67 static ION_SYMBOL_TABLE
*g_sym_tab_php
;
69 /* [SID => STRING, STRING => SID] */
70 static HashTable g_sym_hash
;
71 /* [enum_case_name => SID] */
72 static HashTable g_sym_map
;
74 #define ION_SYS_SYMBOL_SYMBOL_TABLE \
75 ION_SYS_SYMBOL_ION_SYMBOL_TABLE
76 #define g_sym_add(enum_name, c_name) do { \
77 g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \
78 g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \
81 LOCAL
void g_sym_hash_add(int sid
, const char *str
, size_t len
)
85 ZVAL_STR(&zs
, zend_string_init_interned(str
, len
,1));
86 zend_hash_add(&g_sym_hash
, Z_STR(zs
), &zl
);
87 zend_hash_index_add(&g_sym_hash
, sid
, &zs
);
90 LOCAL
void g_sym_map_add(int sid
, const char *str
, size_t len
)
94 zend_hash_str_add(&g_sym_map
, str
, len
, &zv
);
97 LOCAL
int g_sym_init(void)
99 zend_hash_init(&g_sym_hash
, 0, NULL
, NULL
, 1);
100 zend_hash_init(&g_sym_map
, 0, NULL
, NULL
, 1);
102 g_sym_hash_add(0, ZEND_STRL(""));
103 g_sym_map_add(0, ZEND_STRL(""));
104 g_sym_add("Ion", ION
);
105 g_sym_add("Ivm_1_0", IVM
);
106 g_sym_add("IonSymbolTable", SYMBOL_TABLE
);
107 g_sym_add("Name", NAME
);
108 g_sym_add("Version", VERSION
);
109 g_sym_add("Imports", IMPORTS
);
110 g_sym_add("Symbols", SYMBOLS
);
111 g_sym_add("MaxId", MAX_ID
);
112 g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE
);
114 int sys_max_id
= ION_SYS_SID_SHARED_SYMBOL_TABLE
;
116 if (IERR_OK
!= ion_symbol_table_open_with_type(&g_sym_tab_php
, NULL
, ist_SHARED
)) {
119 php_ion_global_symbol
*ptr
= g_sym_tab_php_sym
;
120 ion_symbol_table_set_version(g_sym_tab_php
, PHP_ION_SYMBOL_TABLE_VERSION
);
121 ion_symbol_table_set_name(g_sym_tab_php
, &ptr
->s
.value
);
122 while (ptr
->e
.value
) {
123 ion_symbol_table_add_symbol(g_sym_tab_php
, &ptr
->s
.value
, &ptr
->s
.sid
);
124 g_sym_hash_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->s
.value
.value
, ptr
->s
.value
.length
);
125 g_sym_map_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->e
.value
, ptr
->e
.length
);
128 ion_symbol_table_lock(g_sym_tab_php
);
133 zend_string
*Year
, *Month
, *Day
, *Min
, *Sec
, *Frac
, *MinTZ
, *SecTZ
, *FracTZ
;
136 static void g_intern_str_init()
138 #define NEW_INTERN_STR(s) \
139 g_intern_str.s = zend_string_init_interned(#s, sizeof(#s)-1, 1)
140 NEW_INTERN_STR(Year
);
141 NEW_INTERN_STR(Month
);
145 NEW_INTERN_STR(Frac
);
146 NEW_INTERN_STR(MinTZ
);
147 NEW_INTERN_STR(SecTZ
);
148 NEW_INTERN_STR(FracTZ
);
149 #undef NEW_INTERN_STR
152 typedef struct php_ion_serializer
{
154 ION_WRITER_OPTIONS
*options
;
157 zend_string
*call_custom
;
158 zend_bool call_magic
;
164 } php_ion_serializer
;
166 typedef struct php_ion_annotations
{
167 uint8_t shared_symtab
:1;
170 uint8_t object_prop
:1;
172 zend_string
*object_class
;
173 zend_string
*property_class
;
174 } php_ion_annotations
;
176 typedef struct php_ion_unserializer
{
178 ION_READER_OPTIONS
*options
;
181 zend_string
*call_custom
;
182 zend_bool call_magic
;
190 php_ion_annotations annotations
;
191 } php_ion_unserializer
;
193 ZEND_BEGIN_MODULE_GLOBALS(ion
)
197 ION_DECIMAL zend_max
;
198 ION_DECIMAL zend_min
;
205 php_ion_serializer serializer
;
206 php_ion_unserializer unserializer
;
209 HashTable serializer
[2];
210 HashTable unserializer
[3];
213 ZEND_END_MODULE_GLOBALS(ion
);
216 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
218 # define php_ion_globals ion_globals
221 ZEND_DECLARE_MODULE_GLOBALS(ion
);
223 static zend_class_entry
227 *ce_Decimal_Context_Rounding
,
235 *ce_Reader_Buffer_Reader
,
236 *ce_Reader_Stream_Reader
,
240 *ce_Symbol_ImportLocation
,
243 *ce_Symbol_Table_Local
,
244 *ce_Symbol_Table_PHP
,
245 *ce_Symbol_Table_Shared
,
246 *ce_Symbol_Table_System
,
248 *ce_Timestamp_Format
,
249 *ce_Timestamp_Precision
,
252 *ce_Unserializer_PHP
,
256 *ce_Writer_Buffer_Writer
,
258 *ce_Writer_Stream_Writer
,
262 LOCAL
void php_ion_globals_symbols_init(void)
264 zend_hash_init(&php_ion_globals
.symbol
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
267 LOCAL
void php_ion_globals_symbols_dtor(void)
269 zend_hash_destroy(&php_ion_globals
.symbol
.cache
);
272 LOCAL
void php_ion_globals_serializer_init(void)
274 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
275 HashTable
*h
= php_ion_globals
._ht
.serializer
;
277 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
278 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
281 LOCAL
uint32_t php_ion_globals_serializer_step(void)
283 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
286 if (!(level
= s
->level
++)) {
287 zend_hash_clean(s
->ids
);
288 zend_hash_clean(s
->tmp
);
293 LOCAL
uint32_t php_ion_globals_serializer_exit(void)
295 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
297 ZEND_ASSERT(s
->level
);
299 zend_hash_clean(s
->ids
);
300 zend_hash_clean(s
->tmp
);
305 LOCAL
void php_ion_globals_serializer_dtor(void)
307 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
309 zend_hash_destroy(s
->tmp
);
310 zend_hash_destroy(s
->ids
);
313 void ZVAL_ADDREF(zval
*zv
)
316 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
321 LOCAL
void php_ion_globals_unserializer_init(void)
323 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
324 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
326 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
327 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
328 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
331 LOCAL
void php_ion_globals_unserializer_step(void)
333 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
336 zend_hash_clean(s
->addref
);
337 zend_hash_clean(s
->ids
);
338 zend_hash_clean(s
->tmp
);
342 LOCAL
void php_ion_globals_unserializer_exit(void)
344 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
346 ZEND_ASSERT(s
->level
);
348 zend_hash_clean(s
->addref
);
349 zend_hash_clean(s
->ids
);
350 zend_hash_clean(s
->tmp
);
354 LOCAL
void php_ion_globals_unserializer_dtor(void)
356 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
358 zend_hash_destroy(s
->addref
);
359 zend_hash_destroy(s
->ids
);
360 zend_hash_destroy(s
->tmp
);
363 #define php_ion_obj(type, zo) \
364 ((php_ion_ ##type *) php_ion_obj_ex(zo, XtOffsetOf(php_ion_ ## type, std)))
365 LOCAL
void *php_ion_obj_ex(void *obj
, ptrdiff_t offset
) {
367 return ((char *) obj
) - offset
;
372 #define php_ion_decl(type, cname) \
373 static zend_object_handlers oh_ ## cname; \
374 LOCAL zend_object *create_ion_ ## cname(zend_class_entry *ce) \
376 if (!ce) ce = ce_ ## cname; \
377 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
378 zend_object_std_init(&o->std, ce); \
379 object_properties_init(&o->std, ce); \
380 o->std.handlers = &oh_ ## cname; \
383 LOCAL void free_ion_ ## cname(zend_object *std) \
385 php_ion_ ## type *obj = php_ion_obj(type, std); \
386 php_ion_ ## type ## _dtor(obj); \
387 zend_object_std_dtor(std); \
389 LOCAL zend_object *clone_ion_ ## cname(zend_object *std) \
391 php_ion_ ## type *old_obj = php_ion_obj(type, std), \
392 *new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \
393 php_ion_ ## type ## _copy(new_obj, old_obj); \
395 return &new_obj->std; \
397 #define php_ion_register(type, cname, ...) do { \
398 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
399 ce_ ## cname ->create_object = create_ion_ ## cname; \
400 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
401 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
402 oh_ ## cname .free_obj = free_ion_ ## cname; \
403 oh_ ## cname .clone_obj = clone_ion_ ## cname; \
406 #define ION_CHECK_RETURN(r, err, ...) do { \
408 if (UNEXPECTED(__err)) { \
409 zend_throw_exception_ex(ce_Exception, __err, "%s: %s", ion_error_to_str(__err), #err); \
415 #define ION_CHECK(err, ...) \
416 ION_CHECK_RETURN(, err, __VA_ARGS__)
418 #define ION_CATCH(...) do { \
419 if (UNEXPECTED(EG(exception))) { \
425 #define PTR_CHECK_RETURN(ret, ptr, ...) do { \
426 if (UNEXPECTED(!(ptr))) { \
427 zend_throw_error(NULL, "Uninitialized object"); \
432 #define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
434 #define OBJ_CHECK_RETURN(ret, obj, ...) do { \
435 PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
436 PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
438 #define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
440 LOCAL ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
442 is
->length
= zs
? (SIZE
) zs
->len
: 0;
443 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
447 LOCAL zend_string
*zend_string_from_ion(const ION_STRING
*s
)
449 return zend_string_init((const char *) s
->value
, s
->length
, 0);
452 LOCAL
void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
456 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
459 #define RETURN_IONTYPE(typ) do { \
460 zend_object *__zo = php_ion_type_fetch(typ); \
461 if (UNEXPECTED(!__zo)) { \
464 RETURN_OBJ_COPY(__zo); \
467 LOCAL zend_object
*php_ion_type_fetch(ION_TYPE typ
)
469 zend_long index
= ION_TYPE_INT(typ
);
470 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
472 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
473 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
476 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
479 LOCAL ION_TYPE
ion_type_from_enum(zend_object
*zo
)
481 return (ION_TYPE
) Z_LVAL_P(zend_enum_fetch_case_value(zo
));
484 typedef struct php_ion_symbol_iloc
{
485 ION_SYMBOL_IMPORT_LOCATION loc
;
488 } php_ion_symbol_iloc
;
490 LOCAL
void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
492 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
493 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
494 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
497 LOCAL
void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
499 zend_string_release(obj
->name
);
502 LOCAL
void php_ion_symbol_iloc_copy(php_ion_symbol_iloc
*new_obj
, php_ion_symbol_iloc
*old_obj
)
504 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
505 new_obj
->name
= zend_string_copy(old_obj
->name
);
506 ion_string_from_zend(&new_obj
->loc
.name
, new_obj
->name
);
507 new_obj
->loc
.location
= old_obj
->loc
.location
;
510 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
);
512 typedef struct php_ion_symbol
{
515 zend_object
*iloc
, std
;
518 LOCAL
int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
519 zend_string
*zs1
= zval_get_string(zv1
);
520 zend_string
*zs2
= zval_get_string(zv2
);
527 if (zs1
->len
> zs2
->len
) {
529 } else if (zs2
->len
> zs1
->len
) {
532 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
534 zend_string_release(zs1
);
535 zend_string_release(zs2
);
539 LOCAL
void php_ion_symbol_ctor(php_ion_symbol
*obj
)
541 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
544 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
546 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
548 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
550 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
551 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
553 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
557 LOCAL
void php_ion_symbol_dtor(php_ion_symbol
*obj
)
560 zend_string_release(obj
->value
);
564 LOCAL
void php_ion_symbol_copy(php_ion_symbol
*new_obj
, php_ion_symbol
*old_obj
)
566 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
567 new_obj
->sym
= old_obj
->sym
;
568 if (old_obj
->value
) {
569 new_obj
->value
= zend_string_copy(old_obj
->value
);
570 ion_string_from_zend(&new_obj
->sym
.value
, new_obj
->value
);
573 if ((new_obj
->iloc
= old_obj
->iloc
)) {
574 new_obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, new_obj
->iloc
)->loc
;
578 LOCAL
void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
580 object_init_ex(return_value
, ce_Symbol
);
581 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
583 sym
->sym
.sid
= sym_ptr
->sid
;
584 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
585 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
587 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
588 sym
->iloc
= Z_OBJ(ziloc
);
590 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
591 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
592 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
594 php_ion_symbol_iloc_ctor(iloc
);
597 php_ion_symbol_ctor(sym
);
599 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
600 GC_DELREF(sym
->iloc
);
604 LOCAL zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
606 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
608 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
610 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
613 object_init_ex(&zsym
, ce_Symbol
);
614 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
615 sym
->sym
.sid
= Z_LVAL_P(zid
);
616 sym
->value
= zval_get_string(zss
);
617 php_ion_symbol_ctor(sym
);
618 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);
625 php_ion_decl(symbol
, Symbol
);
627 typedef struct php_ion_symbol_table
{
628 ION_SYMBOL_TABLE
*tab
;
629 int (*dtor
)(ION_SYMBOL_TABLE
*);
631 } php_ion_symbol_table
;
633 LOCAL
void php_ion_symbol_table_ctor(php_ion_symbol_table
*obj
)
637 ION_SYMBOL_TABLE_TYPE typ
= ist_EMPTY
;
638 ion_symbol_table_get_type(obj
->tab
, &typ
);
639 if (typ
!= ist_LOCAL
) {
641 if (IERR_OK
== ion_symbol_table_get_name(obj
->tab
, &is
)) {
642 zend_update_property_stringl(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), (char *) is
.value
, is
.length
);
645 if (IERR_OK
== ion_symbol_table_get_version(obj
->tab
, &iv
)) {
646 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("version"), iv
);
651 LOCAL
void php_ion_symbol_table_dtor(php_ion_symbol_table
*obj
)
661 LOCAL
void php_ion_symbol_table_copy(php_ion_symbol_table
*new_obj
, php_ion_symbol_table
*old_obj
)
663 // do not clone cache members
664 // zend_objects_clone_members(...)
665 if ((new_obj
->dtor
= old_obj
->dtor
)) {
666 ION_CHECK(ion_symbol_table_clone_with_owner(old_obj
->tab
, &new_obj
->tab
, NULL
));
668 new_obj
->tab
= old_obj
->tab
;
670 // update non-cache members
671 php_ion_symbol_table_ctor(new_obj
);
674 LOCAL
void php_ion_symbol_table_import(php_ion_symbol_table
*obj
, php_ion_symbol_table
*import
)
680 zval
*zimports
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), 0, &tmp
);
682 zend_ulong idx
= (uintptr_t) &import
->std
.gc
;
683 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports
), idx
)) {
684 ION_CHECK(ion_symbol_table_import_symbol_table(obj
->tab
, import
->tab
));
686 SEPARATE_ARRAY(zimports
);
687 GC_ADDREF(&import
->std
);
688 add_index_object(zimports
, idx
, &import
->std
);
689 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), zimports
);
694 LOCAL
void php_ion_symbol_table_symbol_zval(php_ion_symbol_table
*obj
, ION_SYMBOL
*sym
, zval
*return_value
)
697 zval
*zsyms
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbols"), 0, &tmp
);
699 zval
*zsym
= zend_hash_index_find(Z_ARRVAL_P(zsyms
), sym
->sid
);
705 php_ion_symbol_zval(sym
, return_value
);
708 SEPARATE_ARRAY(zsyms
);
709 ZVAL_ADDREF(return_value
);
710 add_index_zval(zsyms
, sym
->sid
, return_value
);
714 php_ion_decl(symbol_table
, Symbol_Table
);
716 typedef struct php_ion_decimal_ctx
{
719 } php_ion_decimal_ctx
;
721 #define php_ion_decimal_ctx_init_max(c, rounding) \
722 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
723 LOCAL
void php_ion_decimal_ctx_init(decContext
*ctx
,
724 int digits
, int emax
, int emin
, enum rounding round
, zend_bool clamp
)
726 memset(ctx
, 0, sizeof(*ctx
));
727 ctx
->digits
= digits
;
734 LOCAL
void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
736 if (!obj
->ctx
.digits
) {
737 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
740 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
742 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
744 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
745 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
746 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
747 zend_update_property_bool(obj
->std
.ce
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
750 LOCAL
void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx
*obj
)
754 LOCAL
void php_ion_decimal_ctx_copy(php_ion_decimal_ctx
*new_obj
, php_ion_decimal_ctx
*old_obj
)
756 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
757 new_obj
->ctx
= old_obj
->ctx
;
760 php_ion_decl(decimal_ctx
, Decimal_Context
);
762 typedef struct php_ion_decimal
{
764 zend_object
*ctx
, std
;
767 LOCAL
void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
769 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
770 ION_CHECK(ion_decimal_from_int32(dec
, num
));
771 } else if (num
> 0 && num
<= UINT32_MAX
) {
772 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
775 ION_CHECK(ion_int_alloc(NULL
, &iint
));
776 ION_CHECK(ion_int_from_long(iint
, num
),
778 /* WATCH OUT: BS API */
779 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
780 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
786 LOCAL zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
788 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
789 (void) ion_decimal_to_string(dec
, zstr
->val
);
790 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
793 LOCAL
void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
796 ION_CHECK(ion_int_alloc(NULL
, &ii
));
797 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
799 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
804 LOCAL
bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
808 if (!ion_decimal_is_integer(&obj
->dec
)) {
813 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_max
, &php_ion_globals
.decimal
.ctx
, &result
);
818 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_min
, &php_ion_globals
.decimal
.ctx
, &result
);
825 LOCAL
void php_ion_decimal_ctor(php_ion_decimal
*obj
)
829 object_init_ex(&zdc
, ce_Decimal_Context
);
830 obj
->ctx
= Z_OBJ(zdc
);
831 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
834 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
836 if (php_ion_decimal_fits_zend_long(obj
)) {
838 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
839 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
841 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
842 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
843 zend_string_release(zstr
);
847 LOCAL
void php_ion_decimal_dtor(php_ion_decimal
*obj
)
849 ion_decimal_free(&obj
->dec
);
852 LOCAL
void php_ion_decimal_copy(php_ion_decimal
*new_obj
, php_ion_decimal
*old_obj
)
854 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
855 new_obj
->ctx
= old_obj
->ctx
;
856 ION_CHECK(ion_decimal_copy(&new_obj
->dec
, &old_obj
->dec
));
859 php_ion_decl(decimal
, Decimal
);
861 typedef php_date_obj php_ion_timestamp
;
863 LOCAL zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
866 ctx
= &php_ion_globals
.decimal
.ctx
;
868 decQuad microsecs
, result
;
869 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
870 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
873 LOCAL decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, int usec
, decContext
*ctx
)
876 ctx
= &php_ion_globals
.decimal
.ctx
;
878 decQuad microsecs
, us
;
879 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
882 LOCAL zend_string
*php_ion_timestamp_format_fetch(zend_string
*fmt_case
)
884 return Z_STR_P(zend_enum_fetch_case_value(zend_enum_get_case(ce_Timestamp_Format
, fmt_case
)));
887 LOCAL zend_string
*php_dt_format_from_precision(uint8_t precision
)
890 case ION_TS_FRAC
| 0x80:
891 return php_ion_timestamp_format_fetch(g_intern_str
.FracTZ
);
893 return php_ion_timestamp_format_fetch(g_intern_str
.Frac
);
894 case ION_TS_SEC
| 0x80:
895 return php_ion_timestamp_format_fetch(g_intern_str
.SecTZ
);
897 return php_ion_timestamp_format_fetch(g_intern_str
.Sec
);
898 case ION_TS_MIN
| 0x80:
899 return php_ion_timestamp_format_fetch(g_intern_str
.MinTZ
);
901 return php_ion_timestamp_format_fetch(g_intern_str
.Min
);
903 return php_ion_timestamp_format_fetch(g_intern_str
.Day
);
905 return php_ion_timestamp_format_fetch(g_intern_str
.Month
);
907 return php_ion_timestamp_format_fetch(g_intern_str
.Year
);
909 return ZSTR_CHAR('c');
913 LOCAL timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
915 timelib_time
*time
= ecalloc(1, sizeof(*time
));
922 switch (ts
->precision
& 0x7f) {
924 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
927 time
->s
= ts
->seconds
;
930 time
->i
= ts
->minutes
;
943 time
->z
= ts
->tz_offset
* 60;
944 if (time
->z
|| ts
->precision
& 0x80) {
945 time
->zone_type
= TIMELIB_ZONETYPE_OFFSET
;
947 time
->zone_type
= TIMELIB_ZONETYPE_ID
;
948 time
->tz_info
= get_timezone_info();
953 *fmt
= php_dt_format_from_precision(ts
->precision
);
958 LOCAL ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
960 memset(buf
, 0, sizeof(*buf
));
963 int precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
965 if (!precision
|| precision
> (ION_TS_FRAC
|0x80)) {
966 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
967 "Invalid precision (%d) of ion\\Timestamp", precision
);
968 } else switch ((buf
->precision
= precision
) & 0x7f) {
970 ion_ts_frac_from_usec(&buf
->fraction
, (int) ts
->time
->us
, ctx
);
973 buf
->seconds
= ts
->time
->s
;
976 buf
->minutes
= ts
->time
->i
;
979 buf
->hours
= ts
->time
->h
;
980 buf
->day
= ts
->time
->d
;
983 buf
->month
= ts
->time
->m
;
986 buf
->year
= ts
->time
->y
;
989 buf
->tz_offset
= (short) (ts
->time
->z
/ 60);
990 if (buf
->tz_offset
) {
991 buf
->precision
|= 0x80;
998 LOCAL
void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
1001 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
1003 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
1005 fmt
= php_dt_format_from_precision(precision
);
1006 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
1007 zend_string_release(fmt
);
1010 typedef struct php_ion_catalog
{
1015 LOCAL
void php_ion_catalog_ctor(php_ion_catalog
*obj
)
1017 ION_CHECK(ion_catalog_open(&obj
->cat
));
1020 LOCAL
void php_ion_catalog_dtor(php_ion_catalog
*obj
)
1023 ion_catalog_close(obj
->cat
);
1027 LOCAL ION_COLLECTION
*php_ion_catalog_collection(php_ion_catalog
*cat
)
1029 /* do not look too close */
1032 ION_SYMBOL_TABLE
*sys
;
1033 ION_COLLECTION collection
;
1034 } *cat_ptr
= (void *) cat
->cat
;
1035 return &cat_ptr
->collection
;
1037 // see https://github.com/amzn/ion-c/issues/269
1038 #ifndef IPCN_pNODE_TO_pDATA
1039 # define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0]))
1042 LOCAL
void php_ion_catalog_copy(php_ion_catalog
*new_obj
, php_ion_catalog
*old_obj
)
1044 // do not clone cache members
1045 php_ion_catalog_ctor(new_obj
);
1048 ION_COLLECTION
*col
= php_ion_catalog_collection(old_obj
);
1049 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1050 ION_COLLECTION_CURSOR cur
;
1051 ION_COLLECTION_OPEN(col
, cur
);
1053 ION_SYMBOL_TABLE
**ptr
;
1054 ION_COLLECTION_NEXT(cur
, ptr
);
1056 ION_CHECK(ion_catalog_add_symbol_table(new_obj
->cat
, *ptr
));
1062 LOCAL zend_string
*ion_symbol_table_to_key(ION_SYMBOL_TABLE
*tab
)
1066 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_name(tab
, &is
));
1067 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_version(tab
, &version
));
1070 smart_str_appendl(&s
, (char *) is
.value
, is
.length
);
1071 smart_str_appendc(&s
, ':');
1072 smart_str_append_long(&s
, version
);
1078 LOCAL
void php_ion_catalog_add_symbol_table(php_ion_catalog
*obj
, php_ion_symbol_table
*tab
)
1084 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1086 zend_ulong idx
= (uintptr_t) &tab
->std
.gc
;
1087 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs
), idx
)) {
1088 zend_string
*key
= ion_symbol_table_to_key(tab
->tab
);
1090 ION_CHECK(ion_catalog_add_symbol_table(obj
->cat
, tab
->tab
),
1091 zend_string_release(key
));
1092 SEPARATE_ARRAY(ztabs
);
1093 GC_ADDREF(&tab
->std
);
1094 add_index_object(ztabs
, idx
, &tab
->std
);
1095 GC_ADDREF(&tab
->std
);
1096 add_assoc_object_ex(ztabs
, key
->val
, key
->len
, &tab
->std
);
1097 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), ztabs
);
1098 zend_string_release(key
);
1104 LOCAL
void php_ion_catalog_symbol_table_zval(php_ion_catalog
*obj
, ION_SYMBOL_TABLE
*tab
, zval
*return_value
)
1106 zend_string
*key
= ion_symbol_table_to_key(tab
);
1110 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1112 zval
*ztab
= zend_hash_find(Z_ARRVAL_P(ztabs
), key
);
1114 zend_string_release(key
);
1119 object_init_ex(return_value
, ce_Symbol_Table_Shared
);
1120 php_ion_symbol_table
*o_tab
= php_ion_obj(symbol_table
, Z_OBJ_P(return_value
));
1122 php_ion_symbol_table_ctor(o_tab
);
1125 SEPARATE_ARRAY(ztabs
);
1126 ZVAL_ADDREF(return_value
);
1127 add_index_zval(ztabs
, (uintptr_t) &o_tab
->std
.gc
, return_value
);
1128 ZVAL_ADDREF(return_value
);
1129 add_assoc_zval_ex(ztabs
, key
->val
, key
->len
, return_value
);
1131 zend_string_release(key
);
1134 php_ion_decl(catalog
, Catalog
);
1136 typedef struct php_ion_reader_options_ccn_ctx
{
1138 zend_fcall_info fci
;
1139 zend_fcall_info_cache fcc
;
1140 } php_ion_reader_options_ccn_ctx
;
1142 typedef struct php_ion_reader_options
{
1143 ION_READER_OPTIONS opt
;
1144 php_ion_reader_options_ccn_ctx ccn
;
1145 zend_object
*cat
, *dec_ctx
, *cb
, std
;
1146 } php_ion_reader_options
;
1148 LOCAL
void php_ion_reader_options_dtor(php_ion_reader_options
*obj
)
1151 zend_fcall_info_args_clear(&obj
->ccn
.fci
, true);
1155 LOCAL
void php_ion_reader_options_copy(php_ion_reader_options
*new_obj
, php_ion_reader_options
*old_obj
)
1157 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1159 new_obj
->opt
= old_obj
->opt
;
1160 new_obj
->cat
= old_obj
->cat
;
1161 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1162 new_obj
->cb
= old_obj
->cb
;
1165 ZVAL_OBJ(&zcb
, new_obj
->cb
);
1166 zend_fcall_info_init(&zcb
, 0, &new_obj
->ccn
.fci
, &new_obj
->ccn
.fcc
, NULL
, NULL
);
1167 new_obj
->opt
.context_change_notifier
.context
= &new_obj
->ccn
;
1171 php_ion_decl(reader_options
, Reader_Options
);
1173 LOCAL zend_object
*php_ion_reader_options_new(void)
1175 zend_object
*obj
= create_ion_Reader_Options(NULL
);
1176 zend_call_known_instance_method_with_0_params(obj
->ce
->constructor
, obj
, NULL
);
1180 typedef struct php_ion_reader
{
1188 zend_string
*buffer
;
1194 zend_object
*opt
, std
;
1197 LOCAL iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
1199 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
1200 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
1202 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
1203 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
1204 user
->limit
-= remaining
;
1207 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
1210 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
1211 if (EXPECTED(read
> 0)) {
1212 user
->limit
+= read
;
1216 if (EXPECTED(read
== 0)) {
1220 return IERR_READ_ERROR
;
1223 LOCAL iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1228 php_ion_reader_options_ccn_ctx
*ctx
= context
;
1231 ZVAL_OBJ(&zobj
, ctx
->obj
);
1232 zend_fcall_info_argn(&ctx
->fci
, 1, &zobj
);
1233 if (SUCCESS
!= zend_fcall_info_call(&ctx
->fci
, &ctx
->fcc
, NULL
, NULL
)) {
1234 e
= IERR_INTERNAL_ERROR
;
1236 zend_fcall_info_args_clear(&ctx
->fci
, false);
1241 LOCAL
void php_ion_reader_ctor(php_ion_reader
*obj
)
1244 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1246 if (opt
&& opt
->opt
.context_change_notifier
.context
) {
1247 php_ion_reader_options_ccn_ctx
*ctx
= opt
->opt
.context_change_notifier
.context
;
1248 ctx
->obj
= &obj
->std
;
1249 opt
->opt
.context_change_notifier
.notify
= on_context_change
;
1251 if (obj
->type
== STREAM_READER
) {
1252 PTR_CHECK(obj
->stream
.ptr
);
1253 GC_ADDREF(obj
->stream
.ptr
->res
);
1255 obj
->stream
.buf
.length
= opt
&& opt
->opt
.chunk_threshold
? opt
->opt
.chunk_threshold
: 0x4000;
1256 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1257 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1260 err
= ion_reader_open_buffer(&obj
->reader
, (BYTE
*) obj
->buffer
->val
, (SIZE
) obj
->buffer
->len
, opt
? &opt
->opt
: NULL
);
1267 LOCAL
void php_ion_reader_dtor(php_ion_reader
*obj
)
1270 ion_reader_close(obj
->reader
);
1272 if (obj
->type
== STREAM_READER
) {
1273 if (obj
->stream
.buf
.value
) {
1274 efree(obj
->stream
.buf
.value
);
1276 if (obj
->stream
.ptr
) {
1277 zend_list_delete(obj
->stream
.ptr
->res
);
1281 zend_string_release(obj
->buffer
);
1286 #define php_ion_reader_copy(n,o)
1287 php_ion_decl(reader
, Reader_Reader
);
1288 #define clone_ion_Reader_Reader NULL
1290 typedef struct php_ion_writer_options
{
1291 ION_WRITER_OPTIONS opt
;
1292 zend_object
*cat
, *dec_ctx
, std
;
1293 } php_ion_writer_options
;
1295 LOCAL
void php_ion_writer_options_copy(php_ion_writer_options
*new_obj
, php_ion_writer_options
*old_obj
)
1297 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1299 new_obj
->opt
= old_obj
->opt
;
1300 new_obj
->cat
= old_obj
->cat
;
1301 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1304 LOCAL
void php_ion_writer_options_dtor(php_ion_writer_options
*obj
)
1308 php_ion_decl(writer_options
, Writer_Options
);
1310 LOCAL zend_object
*php_ion_writer_options_new(void)
1312 zend_object
*obj
= create_ion_Writer_Options(NULL
);
1313 zend_call_known_instance_method_with_0_params(obj
->ce
->constructor
, obj
, NULL
);
1317 typedef struct php_ion_writer
{
1326 struct _ion_user_stream
*usr
;
1333 zend_object
*opt
, std
;
1337 LOCAL iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1339 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1341 if (EXPECTED(user
->limit
&& user
->curr
)) {
1342 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1343 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1344 return IERR_WRITE_ERROR
;
1347 user
->curr
= writer
->stream
.buf
.value
;
1348 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1352 LOCAL
void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1354 PTR_CHECK(obj
->stream
.ptr
);
1355 GC_ADDREF(obj
->stream
.ptr
->res
);
1357 obj
->stream
.buf
.length
= opt
? opt
->opt
.temp_buffer_size
: 0x1000;
1358 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1361 LOCAL
void php_ion_writer_buffer_offer(php_ion_writer
*obj
)
1363 if (obj
->buffer
.usr
) {
1364 obj
->buffer
.usr
->curr
= (BYTE
*) &obj
->buffer
.str
.s
->val
[obj
->buffer
.str
.s
->len
];
1365 obj
->buffer
.usr
->limit
= obj
->buffer
.usr
->curr
+ obj
->buffer
.str
.a
- obj
->buffer
.str
.s
->len
;
1369 LOCAL
void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1371 smart_str_alloc(&obj
->buffer
.str
, 0, false);
1372 php_ion_writer_buffer_offer(obj
);
1375 LOCAL
void php_ion_writer_buffer_reset(php_ion_writer
*obj
)
1377 smart_str_free(&obj
->buffer
.str
);
1378 memset(&obj
->buffer
.str
, 0, sizeof(obj
->buffer
.str
));
1379 php_ion_writer_buffer_init(obj
);
1382 LOCAL zend_string
*php_ion_writer_buffer_copy(php_ion_writer
*obj
)
1384 if (obj
->buffer
.usr
) {
1385 // ensure that brain-dead ion_stream interface calls us again
1386 obj
->buffer
.usr
->curr
= NULL
;
1387 obj
->buffer
.usr
->limit
= NULL
;
1389 smart_str_0(&obj
->buffer
.str
);
1390 return zend_string_copy(obj
->buffer
.str
.s
);
1393 LOCAL
void php_ion_writer_buffer_separate(php_ion_writer
*obj
, bool grow
)
1395 // see zend_string_separate and smart_str_erealloc
1396 zend_string
*old_str
= obj
->buffer
.str
.s
;
1397 zend_string
*new_str
= zend_string_alloc(obj
->buffer
.str
.a
<< grow
, false);
1398 memcpy(new_str
->val
, old_str
->val
, new_str
->len
= old_str
->len
);
1399 zend_string_release(old_str
);
1400 obj
->buffer
.str
.s
= new_str
;
1403 LOCAL
void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1405 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
) {
1406 obj
->buffer
.str
.s
->len
= obj
->buffer
.usr
->curr
- (BYTE
*) obj
->buffer
.str
.s
->val
;
1408 if (obj
->buffer
.usr
&& obj
->buffer
.usr
->curr
&& obj
->buffer
.usr
->curr
== obj
->buffer
.usr
->limit
) {
1409 if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1410 php_ion_writer_buffer_separate(obj
, true);
1412 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1414 } else if (UNEXPECTED(GC_REFCOUNT(obj
->buffer
.str
.s
) > 1)) {
1415 php_ion_writer_buffer_separate(obj
, false);
1417 php_ion_writer_buffer_offer(obj
);
1421 LOCAL iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1423 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1424 writer
->buffer
.usr
= user
;
1425 php_ion_writer_buffer_grow(writer
);
1429 LOCAL
void php_ion_writer_options_init_shared_imports(php_ion_writer_options
*opt
)
1431 php_ion_catalog
*cat
= php_ion_obj(catalog
, opt
->cat
);
1434 ION_CHECK(ion_writer_options_initialize_shared_imports(&opt
->opt
));
1436 ION_COLLECTION
*col
= php_ion_catalog_collection(cat
);
1437 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1438 // holy, nah, forget it batman...
1439 ION_COLLECTION_CURSOR cur
;
1440 ION_COLLECTION_OPEN(col
, cur
);
1442 ION_SYMBOL_TABLE
**ptr
;
1443 ION_COLLECTION_NEXT(cur
, ptr
);
1445 ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt
->opt
, ptr
, 1));
1451 LOCAL
void php_ion_writer_ctor(php_ion_writer
*obj
)
1453 php_ion_writer_options
*opt
= NULL
;
1456 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1457 opt
= php_ion_obj(writer_options
, obj
->opt
);
1459 php_ion_writer_options_init_shared_imports(opt
);
1463 ION_STREAM_HANDLER h
;
1464 if (obj
->type
== STREAM_WRITER
) {
1465 h
= php_ion_writer_stream_handler
;
1466 php_ion_writer_stream_init(obj
, opt
);
1468 h
= php_ion_writer_buffer_handler
;
1469 php_ion_writer_buffer_init(obj
);
1473 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1477 LOCAL
void php_ion_writer_dtor(php_ion_writer
*obj
)
1480 ion_writer_close(obj
->writer
);
1483 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1485 ion_writer_options_close_shared_imports(&opt
->opt
);
1488 if (obj
->type
== STREAM_WRITER
) {
1489 if (obj
->stream
.buf
.value
) {
1490 efree(obj
->stream
.buf
.value
);
1492 if (obj
->stream
.ptr
) {
1493 zend_list_delete(obj
->stream
.ptr
->res
);
1496 if (obj
->buffer
.str
.s
) {
1497 smart_str_0(&obj
->buffer
.str
);
1498 zend_string_release(obj
->buffer
.str
.s
);
1503 #define php_ion_writer_copy(o,n)
1504 php_ion_decl(writer
, Writer_Writer
);
1505 #define clone_ion_Writer_Writer NULL
1507 typedef struct php_ion_serializer_php
{
1508 php_ion_serializer serializer
;
1509 zend_object
*opt
, std
;
1510 } php_ion_serializer_php
;
1512 LOCAL
void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
1514 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1515 ser_obj
->serializer
.ids
= global_ser
->ids
;
1516 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
1518 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
1519 ser_obj
->serializer
.multi_seq
);
1520 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1521 ser_obj
->serializer
.call_magic
);
1522 if (ser_obj
->serializer
.call_custom
) {
1523 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1524 ser_obj
->serializer
.call_custom
);
1525 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
1527 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1529 if (!ser_obj
->opt
) {
1530 ser_obj
->opt
= php_ion_writer_options_new();
1532 GC_ADDREF(ser_obj
->opt
);
1534 ser_obj
->serializer
.options
= &php_ion_obj(writer_options
, ser_obj
->opt
)->opt
;
1535 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
1536 GC_DELREF(ser_obj
->opt
);
1539 LOCAL
void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
1541 if (obj
->serializer
.call_custom
) {
1542 zend_string_release(obj
->serializer
.call_custom
);
1546 LOCAL
void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1548 LOCAL
void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool unmangle_props
, bool annotate_props
)
1550 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
1554 zend_string
*k
= NULL
;
1555 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1556 char buf
[MAX_LENGTH_OF_LONG
+ 1];
1560 const char *class_name
, *prop_name
;
1561 if (unmangle_props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1562 if (annotate_props
) {
1563 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_PROPERTY
));
1564 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1570 ion_string_assign_cstr(&is
, (char *) prop_name
, (SIZE
) prop_len
);
1572 char *end
= buf
+ sizeof(buf
) - 1;
1573 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1574 ion_string_assign_cstr(&is
, ptr
, (SIZE
) (end
- ptr
));
1577 // WATCH OUT: field names need to be copied
1579 ION_CHECK(ion_string_copy_to_owner(ser
->writer
, &fn
, &is
));
1580 ION_CHECK(ion_writer_write_field_name(ser
->writer
, &fn
));
1582 php_ion_serialize_zval(ser
, v
);
1584 ZEND_HASH_FOREACH_END();
1586 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1589 LOCAL
void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1591 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1594 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1595 php_ion_serialize_zval(ser
, v
);
1597 ZEND_HASH_FOREACH_END();
1599 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1602 LOCAL
void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1604 if (zend_array_is_list(arr
)) {
1605 php_ion_serialize_list(ser
, arr
);
1607 php_ion_serialize_struct(ser
, arr
, false, false);
1611 LOCAL
void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1617 ZVAL_OBJ(&tmp
, zobject
);
1618 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1620 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_SERIALIZEABLE
));
1621 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1622 ION_CHECK(ion_writer_write_clob(ser
->writer
, buf
, len
));
1624 } else if (!EG(exception
)){
1625 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1626 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1630 LOCAL
void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1635 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1638 if (IS_ARRAY
== Z_TYPE(rv
)) {
1640 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, fn
? &PHP_ION_SYMBOL_CUSTOM_OBJECT
: &PHP_ION_SYMBOL_MAGIC_OBJECT
));
1641 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1642 php_ion_serialize_zval(ser
, &rv
);
1645 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1646 "%s serializer %s::%s did not return an array",
1647 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1648 fn
? fn
->common
.function_name
->val
: "__serialize");
1652 LOCAL
void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1655 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_ENUM
));
1657 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1658 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1659 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1662 LOCAL
void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1666 if (zobject
->ce
!= zend_standard_class_def
) {
1667 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_CLASS_OBJECT
));
1668 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1670 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_OBJECT
));
1674 ZVAL_OBJ(&zobj
, zobject
);
1675 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1677 php_ion_serialize_struct(ser
, props
, true, true);
1678 zend_release_properties(props
);
1680 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
1681 "Could not get properties for serialization of class %s",
1682 zobject
->ce
->name
->val
);
1686 LOCAL
void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1688 zval tmp_type
, *type
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_TYPE
), 0, &tmp_type
);
1689 zval tmp_value
, *value
= zend_read_property_ex(NULL
, zobject
, ZSTR_KNOWN(ZEND_STR_VALUE
), 0, &tmp_value
);
1690 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1692 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1695 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1698 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1699 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1704 LOCAL
bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1706 return ce
->__serialize
&& ser
->call_magic
;
1709 LOCAL
bool can_call_iface_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1712 return !!ce
->serialize
; // NOLINT
1715 LOCAL
bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1717 if (ser
->call_custom
) {
1718 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
1723 LOCAL
void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1726 zend_class_entry
*ce
= zobject
->ce
;
1729 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1730 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1731 "Serializing %s is not allowed", ce
->name
->val
);
1735 if (can_call_magic_serialize(ser
, ce
)) {
1736 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1737 } else if (can_call_iface_serialize(ser
, ce
)) {
1738 php_ion_serialize_object_iface(ser
, zobject
);
1739 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1740 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1741 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1742 php_ion_serialize_object_enum(ser
, zobject
);
1743 } else if (ce
== ce_Symbol
) {
1744 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1745 } else if (ce
== ce_Decimal
) {
1746 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1747 } else if (ce
== ce_Timestamp
) {
1749 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1750 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1751 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1752 } else if (ce
== ce_LOB
) {
1753 php_ion_serialize_object_lob(ser
, zobject
);
1755 php_ion_serialize_object_std(ser
, zobject
);
1759 LOCAL
bool php_ion_serialize_system_value(php_ion_serializer
*ser
, zval
*zv
)
1761 if (1 == php_ion_globals
.serializer
.level
) {
1762 if (Z_TYPE_P(zv
) == IS_OBJECT
) {
1763 if (Z_OBJCE_P(zv
) == ce_Symbol_Table_Shared
) {
1764 php_ion_symbol_table
*obj
= php_ion_obj(symbol_table
, Z_OBJ_P(zv
));
1765 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj
->tab
, ser
->writer
));
1773 LOCAL
bool php_ion_serialize_backref(php_ion_serializer
*ser
, zval
*zv
)
1775 if (Z_TYPE_P(zv
) == IS_STRING
&& Z_STR_P(zv
) == zend_empty_string
) {
1778 if (Z_TYPE_P(zv
) == IS_ARRAY
&& Z_ARR_P(zv
) == &zend_empty_array
) {
1782 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1783 zval
*ref
= zend_hash_index_find(ser
->ids
, idx
);
1787 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1788 zend_hash_index_add(ser
->ids
, idx
, &num
);
1791 zend_hash_next_index_insert(ser
->tmp
, zv
);
1796 ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_BACKREF
));
1797 ION_CHECK_RETURN(true, ion_writer_write_int64(ser
->writer
, Z_LVAL_P(ref
)));
1801 LOCAL
void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1803 if (php_ion_serialize_system_value(ser
, zv
)) {
1806 if (php_ion_serialize_backref(ser
, zv
)) {
1811 switch (Z_TYPE_P(zv
)) {
1813 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1817 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1821 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1825 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_REFERENCE
));
1826 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1831 LOCAL
void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1835 switch (Z_TYPE_P(zv
)) {
1837 ION_CHECK(ion_writer_write_null(ser
->writer
));
1840 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1843 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1846 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1849 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1855 php_ion_serialize_refcounted(ser
, zv
);
1858 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1859 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1863 #define php_ion_serializer_php_copy(o,n)
1864 php_ion_decl(serializer_php
, Serializer_PHP
);
1865 #define clone_ion_Serializer_PHP NULL
1867 LOCAL
void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1870 HashTable
*arr
= NULL
;
1872 if (ser
->multi_seq
) {
1873 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1874 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
1875 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1879 arr
= Z_ARRVAL_P(zv
);
1881 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1882 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1886 php_ion_globals_serializer_step();
1887 php_ion_serialize_zval(ser
, zv
);
1888 php_ion_globals_serializer_exit();
1890 if (!ser
->multi_seq
) {
1893 zend_hash_move_forward_ex(arr
, &pos
);
1894 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1898 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1900 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1903 zo_ser
= create_ion_Serializer_PHP(NULL
);
1904 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1906 o_ser
->serializer
.call_magic
= true;
1907 php_ion_serializer_php_ctor(o_ser
);
1909 ser
= &o_ser
->serializer
;
1912 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1913 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1914 writer
->type
= BUFFER_WRITER
;
1917 zo_opt
= writer
->opt
= php_ion_writer_options_new();
1918 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1921 php_ion_writer_ctor(writer
);
1922 ser
->writer
= writer
->writer
;
1923 ser
->buffer
= &writer
->buffer
.str
;
1925 if (!EG(exception
)) {
1926 php_ion_serialize_ex(ser
, zv
);
1929 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1930 ion_writer_flush(ser
->writer
, NULL
);
1931 RETVAL_STR_COPY(ser
->buffer
->s
);
1933 OBJ_RELEASE(zo_writer
);
1935 OBJ_RELEASE(zo_opt
);
1938 OBJ_RELEASE(zo_ser
);
1942 typedef struct php_ion_unserializer_php
{
1943 php_ion_unserializer unserializer
;
1944 zend_object
*opt
, std
;
1945 } php_ion_unserializer_php
;
1947 LOCAL
void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1949 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1950 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1951 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1952 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1954 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
1955 ser_obj
->unserializer
.multi_seq
);
1956 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicUnserialize"),
1957 ser_obj
->unserializer
.call_magic
);
1958 if (ser_obj
->unserializer
.call_custom
) {
1959 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"),
1960 ser_obj
->unserializer
.call_custom
);
1961 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1963 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"));
1965 if (!ser_obj
->opt
) {
1966 ser_obj
->opt
= php_ion_reader_options_new();
1968 GC_ADDREF(ser_obj
->opt
);
1970 ser_obj
->unserializer
.options
= &php_ion_obj(reader_options
, ser_obj
->opt
)->opt
;
1971 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1972 GC_DELREF(ser_obj
->opt
);
1975 LOCAL
void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1977 if (obj
->unserializer
.call_custom
) {
1978 zend_string_release(obj
->unserializer
.call_custom
);
1982 LOCAL
void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1984 LOCAL
bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1986 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1989 LOCAL
bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1991 return (ce
&& ce
->unserialize
);
1994 LOCAL
bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1996 if (ser
->call_custom
) {
1997 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
2002 LOCAL zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
2004 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2007 object_init_ex(return_value
, ce
);
2008 return zend_hash_next_index_insert(ser
->ids
, return_value
);
2011 zend_throw_exception_ex(ce_Exception
, IERR_IMPORT_NOT_FOUND
,
2012 "Could not find class %s", ser
->annotations
.object_class
->val
);
2016 LOCAL
void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
2018 zend_string
*zs_case
= zval_get_string(return_value
);
2019 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2020 ZVAL_NULL(return_value
);
2023 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2024 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
2025 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2026 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
2029 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
2030 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2031 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
2034 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
2035 zend_hash_next_index_insert(ser
->ids
, return_value
);
2036 zend_string_release(zs_case
);
2039 LOCAL
void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
2041 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2042 if (can_call_iface_unserialize(ser
, ce
)) {
2043 zend_string
*s
= zval_get_string(return_value
);
2044 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2045 ZVAL_NULL(return_value
);
2046 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
2047 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
2048 RETVAL_ZVAL(backref
, 0, 0);
2050 zval_ptr_dtor(backref
);
2052 if (!EG(exception
)) {
2053 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2054 "Failed to unserialize class %s", ce
->name
->val
);
2057 zend_string_release(s
);
2059 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2060 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
2064 LOCAL
void php_ion_unserialize_field_name_ex(php_ion_unserializer
*ser
, ION_STRING
*name
, SID
*sid
)
2066 ION_CHECK(ion_reader_get_field_name(ser
->reader
, name
));
2067 if (!name
->length
) {
2069 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
2070 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
2071 ION_STRING_ASSIGN(name
, &is_ptr
->value
);
2078 LOCAL
void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
, bool is_prop
)
2080 // FIXME: symbol table
2082 SID sid
= UNKNOWN_SID
;
2083 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
2085 php_ion_unserialize_field_name_ex(ser
, &name
, &sid
);
2088 switch (name
.length
) {
2090 ptr
= zend_print_long_to_buf(end
, sid
);
2092 *key
= zend_string_init(ptr
, end
- ptr
, 0);
2095 *key
= ZSTR_CHAR(*name
.value
);
2099 *key
= zend_string_init_interned((char *) name
.value
, name
.length
, 0);
2101 *key
= zend_string_from_ion(&name
);
2107 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
2109 zend_hash_next_index_insert(ser
->ids
, return_value
);
2111 ION_CHECK(ion_reader_step_in(ser
->reader
));
2115 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
2117 if (typ
== tid_EOF
) {
2122 php_ion_unserialize_field_name(ser
, &key
, true);
2126 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2127 ION_CATCH(zend_string_release(key
));
2129 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
2130 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
2131 ce
= zend_lookup_class(ser
->annotations
.property_class
);
2133 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
2134 zval_ptr_dtor(&zvalue
);
2135 zend_string_release(key
);
2138 ION_CHECK(ion_reader_step_out(ser
->reader
));
2142 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
2143 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
2144 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
2145 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
2146 * nondeterministic behavior.
2148 LOCAL
void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
2150 zend_hash_next_index_insert(ser
->ids
, return_value
);
2152 ION_CHECK(ion_reader_step_in(ser
->reader
));
2156 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
2158 if (typ
== tid_EOF
) {
2163 php_ion_unserialize_field_name(ser
, &key
, false);
2167 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2168 ION_CATCH(zend_string_release(key
));
2170 // FIXME:: too naive; b0rked if the previous value is an array
2171 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
2172 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
2173 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
2175 Z_TRY_ADDREF_P(prev
);
2176 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
2177 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
2179 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
2181 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
2183 zend_string_release(key
);
2186 ION_CHECK(ion_reader_step_out(ser
->reader
));
2189 LOCAL
void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2191 switch (ser
->annotations
.object_type
) {
2197 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
2198 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2199 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
2204 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
2205 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2206 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
2208 *fn
= zobject
->ce
->__unserialize
;
2212 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2213 "Invalid object type %c", ser
->annotations
.object_type
);
2217 LOCAL
void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
2219 // backup possible backref to array returned by magic/custom __serialize()
2220 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
2221 ZVAL_NULL(return_value
);
2222 php_ion_unserialize_class(ser
, return_value
);
2225 zend_function
*fn
= NULL
;
2226 zend_object
*zobject
= Z_OBJ_P(return_value
);
2227 verify_unserializer(ser
, zobject
, &fn
);
2232 php_ion_unserialize_props(ser
, return_value
);
2237 if (Z_TYPE_P(input
) != IS_ARRAY
) {
2238 zval_ptr_dtor(input
);
2240 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
2241 php_ion_unserialize_hash(ser
, input
);
2246 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
2250 LOCAL
void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
2252 if (ser
->annotations
.object_class
) {
2253 switch (ser
->annotations
.object_type
) {
2255 php_ion_unserialize_object_iface(ser
, return_value
);
2258 php_ion_unserialize_object_enum(ser
, return_value
);
2261 php_ion_unserialize_object(ser
, return_value
);
2263 } else if (!ser
->annotations
.object_type
) {
2264 bool is_shared_symtab
= ser
->annotations
.shared_symtab
;
2265 array_init(return_value
);
2266 php_ion_unserialize_hash(ser
, return_value
);
2267 if (is_shared_symtab
) {
2269 object_init_ex(&zobj
, ce_Symbol_Table_Shared
);
2270 zend_call_known_function(Z_OBJCE(zobj
)->constructor
, Z_OBJ(zobj
), Z_OBJCE(zobj
), NULL
, 0, NULL
, Z_ARRVAL_P(return_value
));
2271 zval_ptr_dtor(return_value
);
2272 RETURN_COPY_VALUE(&zobj
);
2274 } else if (ser
->annotations
.object_type
== 'o') {
2275 object_init(return_value
);
2276 php_ion_unserialize_hash(ser
, return_value
);
2278 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_TOKEN
,
2279 "Invalid object annotation %c::", ser
->annotations
.object_type
);
2283 LOCAL
void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
2285 ION_CHECK(ion_reader_step_in(ser
->reader
));
2286 array_init(return_value
);
2287 zend_hash_next_index_insert(ser
->ids
, return_value
);
2291 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
2293 if (typ
== tid_EOF
) {
2298 php_ion_unserialize_zval(ser
, &next
, &typ
);
2301 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
2304 ION_CHECK(ion_reader_step_out(ser
->reader
));
2307 LOCAL
void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
2309 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
2312 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, (SIZE
) zstr
->len
, &read
);
2313 if (err
== IERR_BUFFER_TOO_SMALL
) {
2314 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
2317 ION_CHECK(err
, zend_string_release(zstr
));
2318 if (zstr
->len
> read
) {
2319 zstr
->val
[read
] = 0;
2320 zstr
= zend_string_truncate(zstr
, read
, 0);
2325 LOCAL
void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
2328 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
2330 object_init_ex(return_value
, ce_Timestamp
);
2331 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
2333 zend_string
*fmt
= NULL
;
2334 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
2335 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
2336 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
2337 zend_string_release(fmt
);
2342 LOCAL
void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
2344 ION_INT
*num
= NULL
;
2345 ION_CHECK(ion_int_alloc(reader
, &num
));
2346 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
2348 // TODO: SIZEOF_ZEND_LONG == 4
2350 iERR err
= ion_int_to_int64(num
, &i64
);
2356 case IERR_NUMERIC_OVERFLOW
: ;
2358 ION_CHECK(ion_int_char_length(num
, &max
));
2359 zend_string
*zs
= zend_string_alloc(max
, 0);
2361 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2362 zs
->val
[zs
->len
= len
] = 0;
2373 LOCAL
void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2375 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2378 ZVAL_COPY_VALUE(return_value
, backref
);
2379 zend_hash_next_index_insert(ser
->addref
, return_value
);
2381 zend_throw_exception_ex(ce_Exception
, IERR_INTERNAL_ERROR
,
2382 "Could not find back reference " ZEND_LONG_FMT
, Z_LVAL_P(return_value
));
2386 LOCAL
void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2388 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2391 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
2392 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2394 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
2396 if (ann_str
.length
!= 1) {
2397 if (ION_STRING_EQUALS(&ann_str
, &ION_SYMBOL_SHARED_SYMBOL_TABLE_STRING
)) {
2398 ser
->annotations
.shared_symtab
= true;
2403 switch (*ann_str
.value
) {
2405 if (ser
->annotations
.makeref
) {
2406 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2407 "Invalid multiple reference annotations");
2410 ser
->annotations
.makeref
= true;
2414 if (ser
->annotations
.backref
) {
2415 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2416 "Invalid multiple back reference annotations");
2419 ser
->annotations
.backref
= true;
2423 if (ser
->annotations
.object_prop
) {
2424 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2425 "Invalid multiple object property annotations");
2428 ser
->annotations
.object_prop
= true;
2430 ION_STRING prop_class
;
2431 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
2432 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
2435 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
2436 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
2445 if (ser
->annotations
.object_type
) {
2446 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2447 "Invalid multiple object type annotations: %c::%c",
2448 ser
->annotations
.object_type
, *ann_str
.value
);
2451 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
2452 ION_STRING class_name
;
2453 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
2454 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
2457 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
2458 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
2464 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2465 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2466 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2469 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2470 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2471 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2477 LOCAL
void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2480 memcpy(&ser
->type
, typ
, sizeof(ser
->type
)); // NOLINT
2483 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
2486 php_ion_unserialize_annotations(ser
);
2489 if (ser
->annotations
.makeref
) {
2490 ZVAL_MAKE_REF(return_value
);
2491 zend_hash_next_index_insert(ser
->ids
, return_value
);
2492 ZVAL_DEREF(return_value
);
2495 if (ION_TYPE_INT(*typ
) > 0) {
2497 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
2503 switch (ION_TYPE_INT(*typ
)) {
2505 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
2508 case tid_BOOL_INT
: ;
2510 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
2514 php_ion_reader_read_int(ser
->reader
, return_value
);
2515 if (ser
->annotations
.backref
) {
2517 php_ion_unserialize_backref(ser
, return_value
);
2519 if (ser
->annotations
.object_type
) {
2520 if (!ser
->annotations
.backref
) {
2521 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_SYNTAX
,
2522 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2523 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2526 goto unserialize_struct
;
2530 case tid_FLOAT_INT
: ;
2532 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
2535 case tid_DECIMAL_INT
:
2536 object_init_ex(return_value
, ce_Decimal
);
2537 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2538 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
2539 php_ion_decimal_ctor(dec
);
2540 zend_hash_next_index_insert(ser
->ids
, return_value
);
2543 case tid_TIMESTAMP_INT
:
2544 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
2545 zend_hash_next_index_insert(ser
->ids
, return_value
);
2548 case tid_SYMBOL_INT
: ;
2550 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
2551 php_ion_symbol_zval(&sym
, return_value
);
2552 if (ser
->annotations
.object_type
) {
2553 goto unserialize_struct
;
2555 zend_hash_next_index_insert(ser
->ids
, return_value
);
2558 case tid_STRING_INT
: ;
2560 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
2561 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2562 if (ser
->annotations
.object_type
) {
2563 goto unserialize_struct
;
2565 zend_hash_next_index_insert(ser
->ids
, return_value
);
2570 php_ion_reader_read_lob(ser
->reader
, return_value
);
2571 if (ser
->annotations
.object_type
) {
2572 goto unserialize_struct
;
2574 zend_hash_next_index_insert(ser
->ids
, return_value
);
2578 case tid_SEXP_INT
: // FIXME
2579 php_ion_unserialize_list(ser
, return_value
);
2580 if (!ser
->annotations
.object_type
) {
2585 case tid_STRUCT_INT
:
2586 unserialize_struct
: ;
2587 php_ion_unserialize_struct(ser
, return_value
);
2594 case tid_DATAGRAM_INT
:
2595 ZEND_ASSERT(!"datagram");
2601 #define php_ion_unserializer_php_copy(o,n)
2602 php_ion_decl(unserializer_php
, Unserializer_PHP
);
2603 #define clone_ion_Unserializer_PHP NULL
2605 LOCAL
void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2607 if (ser
->multi_seq
) {
2608 array_init(return_value
);
2614 php_ion_globals_unserializer_step();
2615 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2616 php_ion_globals_unserializer_exit();
2617 ION_CATCH(zval_ptr_dtor(&tmp
));
2619 if (!ser
->multi_seq
) {
2620 RETURN_COPY_VALUE(&tmp
);
2621 } else if (ser
->type
!= tid_EOF
) {
2622 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2624 } while (ser
->type
!= tid_EOF
);
2627 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
2629 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
2632 zo_ser
= create_ion_Unserializer_PHP(NULL
);
2633 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
2635 o_ser
->unserializer
.call_magic
= true;
2636 php_ion_unserializer_php_ctor(o_ser
);
2638 ser
= &o_ser
->unserializer
;
2641 zend_object
*zo_reader
;
2642 php_ion_reader
*reader
;
2645 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2646 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2647 reader
= php_ion_obj(reader
, zo_reader
);
2648 reader
->type
= STREAM_READER
;
2649 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2650 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2651 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2652 reader
= php_ion_obj(reader
, zo_reader
);
2653 reader
->type
= BUFFER_READER
;
2654 reader
->buffer
= zval_get_string(zdata
);
2656 zend_throw_exception_ex(ce_Exception
, IERR_INVALID_ARG
,
2657 "Invalid source to unserialize: expected string or resource, got %s",
2658 zend_zval_type_name(zdata
));
2660 OBJ_RELEASE(zo_ser
);
2666 zo_opt
= reader
->opt
= php_ion_reader_options_new();
2667 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
2670 php_ion_reader_ctor(reader
);
2671 ser
->reader
= reader
->reader
;
2673 if (!EG(exception
)) {
2674 php_ion_unserialize_ex(ser
, return_value
);
2677 OBJ_RELEASE(zo_reader
);
2679 OBJ_RELEASE(zo_opt
);
2682 OBJ_RELEASE(zo_ser
);