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/date/php_date.h"
16 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
19 #define PHP_ION_SYMBOL_TABLE_VERSION 1
20 #define PHP_ION_SYMBOL(e, s) {0, {sizeof(e)-1, (BYTE *) e}, {sizeof(s)-1, (BYTE *) s}},
22 typedef struct php_ion_global_symbol
{
26 } php_ion_global_symbol
;
28 static php_ion_global_symbol g_sym_tab_php_sym
[] = {
29 PHP_ION_SYMBOL("PHP", "PHP")
30 PHP_ION_SYMBOL("Reference", "R")
31 PHP_ION_SYMBOL("Backref", "r")
32 PHP_ION_SYMBOL("Property", "p")
33 PHP_ION_SYMBOL("ClassObject", "c")
34 PHP_ION_SYMBOL("CustomObject", "C")
35 PHP_ION_SYMBOL("Object", "o")
36 PHP_ION_SYMBOL("MagicObject", "O")
37 PHP_ION_SYMBOL("Serializable", "S")
38 PHP_ION_SYMBOL("Enum", "E")
43 static ION_SYMBOL_TABLE
*g_sym_tab_php
;
45 /* [SID => STRING, STRING => SID] */
46 static HashTable g_sym_hash
;
47 /* [enum_case_name => SID] */
48 static HashTable g_sym_map
;
50 #define ION_SYS_SYMBOL_SYMBOL_TABLE \
51 ION_SYS_SYMBOL_ION_SYMBOL_TABLE
52 #define g_sym_add(enum_name, c_name) do { \
53 g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \
54 g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \
57 static inline void g_sym_hash_add(int sid
, const char *str
, size_t len
)
61 ZVAL_STR(&zs
, zend_string_init_interned(str
, len
,1));
62 zend_hash_add(&g_sym_hash
, Z_STR(zs
), &zl
);
63 zend_hash_index_add(&g_sym_hash
, sid
, &zs
);
66 static inline void g_sym_map_add(int sid
, const char *str
, size_t len
)
70 zend_hash_str_add(&g_sym_map
, str
, len
, &zv
);
73 static inline int g_sym_init(void)
75 zend_hash_init(&g_sym_hash
, 0, NULL
, NULL
, 1);
76 zend_hash_init(&g_sym_map
, 0, NULL
, NULL
, 1);
78 g_sym_hash_add(0, ZEND_STRL(""));
79 g_sym_map_add(0, ZEND_STRL(""));
80 g_sym_add("Ion", ION
);
81 g_sym_add("Ivm_1_0", IVM
);
82 g_sym_add("IonSymbolTable", SYMBOL_TABLE
);
83 g_sym_add("Name", NAME
);
84 g_sym_add("Version", VERSION
);
85 g_sym_add("Imports", IMPORTS
);
86 g_sym_add("Symbols", SYMBOLS
);
87 g_sym_add("MaxId", MAX_ID
);
88 g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE
);
90 int sys_max_id
= ION_SYS_SID_SHARED_SYMBOL_TABLE
;
92 if (IERR_OK
!= ion_symbol_table_open_with_type(&g_sym_tab_php
, NULL
, ist_SHARED
)) {
95 php_ion_global_symbol
*ptr
= g_sym_tab_php_sym
;
96 ion_symbol_table_set_version(g_sym_tab_php
, PHP_ION_SYMBOL_TABLE_VERSION
);
97 ion_symbol_table_set_name(g_sym_tab_php
, &ptr
->s
);
98 while (ptr
->e
.value
) {
99 ion_symbol_table_add_symbol(g_sym_tab_php
, &ptr
->s
, &ptr
->id
);
100 g_sym_hash_add(sys_max_id
+ ptr
->id
, (const char *) ptr
->s
.value
, ptr
->s
.length
);
101 g_sym_map_add(sys_max_id
+ ptr
->id
, (const char *) ptr
->e
.value
, ptr
->e
.length
);
104 ion_symbol_table_lock(g_sym_tab_php
);
108 typedef struct php_ion_serializer
{
110 ION_WRITER_OPTIONS
*options
;
113 zend_string
*call_custom
;
114 zend_bool call_magic
;
120 } php_ion_serializer
;
122 typedef struct php_ion_annotaions
{
125 uint8_t object_prop
:1;
127 zend_string
*object_class
;
128 zend_string
*property_class
;
129 } php_ion_annotations
;
131 typedef struct php_ion_unserializer
{
133 ION_READER_OPTIONS
*options
;
136 zend_string
*call_custom
;
137 zend_bool call_magic
;
145 php_ion_annotations annotations
;
146 } php_ion_unserializer
;
148 ZEND_BEGIN_MODULE_GLOBALS(ion
)
152 ION_DECIMAL zend_max
;
153 ION_DECIMAL zend_min
;
160 php_ion_serializer serializer
;
161 php_ion_unserializer unserializer
;
164 HashTable serializer
[2];
165 HashTable unserializer
[3];
168 ZEND_END_MODULE_GLOBALS(ion
);
171 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
173 # define php_ion_globals ion_globals
176 ZEND_DECLARE_MODULE_GLOBALS(ion
);
178 static zend_class_entry
183 *ce_Decimal_Context_Rounding
,
190 *ce_Reader_Buffer_Reader
,
191 *ce_Reader_Stream_Reader
,
195 *ce_Symbol_ImportLocation
,
198 *ce_Symbol_Table_Local
,
199 *ce_Symbol_Table_PHP
,
200 *ce_Symbol_Table_Shared
,
201 *ce_Symbol_Table_System
,
203 *ce_Timestamp_Precision
,
206 *ce_Unserializer_PHP
,
210 *ce_Writer_Buffer_Writer
,
212 *ce_Writer_Stream_Writer
,
216 static inline void php_ion_globals_symbols_init(void)
218 zend_hash_init(&php_ion_globals
.symbol
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
221 static inline void php_ion_globals_symbols_dtor(void)
223 zend_hash_destroy(&php_ion_globals
.symbol
.cache
);
226 static inline void php_ion_globals_serializer_init(void)
228 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
229 HashTable
*h
= php_ion_globals
._ht
.serializer
;
231 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
232 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
235 static inline uint32_t php_ion_globals_serializer_step(void)
237 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
240 if (!(level
= s
->level
++)) {
241 zend_hash_clean(s
->ids
);
242 zend_hash_clean(s
->tmp
);
247 static inline uint32_t php_ion_globals_serializer_exit(void)
249 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
251 ZEND_ASSERT(s
->level
);
253 zend_hash_clean(s
->ids
);
254 zend_hash_clean(s
->tmp
);
259 static inline void php_ion_globals_serializer_dtor(void)
261 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
263 zend_hash_destroy(s
->tmp
);
264 zend_hash_destroy(s
->ids
);
267 void ZVAL_ADDREF(zval
*zv
)
270 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
275 static inline void php_ion_globals_unserializer_init(void)
277 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
278 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
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);
282 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
285 static inline void php_ion_globals_unserializer_step(void)
287 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
290 zend_hash_clean(s
->addref
);
291 zend_hash_clean(s
->ids
);
292 zend_hash_clean(s
->tmp
);
296 static inline void php_ion_globals_unserializer_exit(void)
298 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
300 ZEND_ASSERT(s
->level
);
302 zend_hash_clean(s
->addref
);
303 zend_hash_clean(s
->ids
);
304 zend_hash_clean(s
->tmp
);
308 static inline void php_ion_globals_unserializer_dtor(void)
310 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
312 zend_hash_destroy(s
->addref
);
313 zend_hash_destroy(s
->ids
);
314 zend_hash_destroy(s
->tmp
);
317 #define php_ion_decl(type, cname, ...) \
318 static zend_object_handlers oh_ ## cname; \
319 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
321 if (!ce) ce = ce_ ## cname; \
322 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
323 zend_object_std_init(&o->std, ce); \
324 object_properties_init(&o->std, ce); \
325 o->std.handlers = &oh_ ## cname; \
328 static inline void free_ion_ ## cname(zend_object *std) \
330 php_ion_ ## type *obj = php_ion_obj(type, std); \
332 zend_object_std_dtor(std); \
335 #define php_ion_register(type, cname, ...) do { \
336 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
337 ce_ ## cname ->create_object = create_ion_ ## cname; \
338 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
339 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
340 oh_ ## cname .free_obj = free_ion_ ## cname; \
343 #define php_ion_obj(type, obj) \
344 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
346 #define ION_CHECK_RETURN(r, err, ...) do { \
349 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
355 #define ION_CHECK(err, ...) \
356 ION_CHECK_RETURN(, err, __VA_ARGS__)
358 #define ION_CATCH(...) do { \
359 if (EG(exception)) { \
365 #define PTR_CHECK(ptr, ...) do { \
367 zend_throw_error(NULL, "Uninitialized object"); \
373 #define OBJ_CHECK(obj, ...) do { \
374 PTR_CHECK(obj, __VA_ARGS__); \
375 PTR_CHECK(*((void **)obj), __VA_ARGS__); \
378 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
380 is
->length
= zs
? zs
->len
: 0;
381 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
385 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
387 return zend_string_init((const char *) s
->value
, s
->length
, 0);
390 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
394 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
397 typedef struct php_ion_type
{
402 php_ion_decl(type
, Type
);
404 #define RETURN_IONTYPE(typ) do { \
405 zend_object *__zo = php_ion_type_fetch(typ); \
406 if (UNEXPECTED(!__zo)) { \
409 RETURN_OBJ_COPY(__zo); \
412 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
414 zend_long index
= ION_TYPE_INT(typ
);
415 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
417 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
418 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
421 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
424 typedef struct php_ion_symbol_iloc
{
425 ION_SYMBOL_IMPORT_LOCATION loc
;
428 } php_ion_symbol_iloc
;
430 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
432 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
433 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
434 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
437 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
439 zend_string_release(obj
->name
);
442 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
444 typedef struct php_ion_symbol
{
447 zend_object
*iloc
, std
;
450 static inline int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
451 zend_string
*zs1
= zval_get_string(zv1
);
452 zend_string
*zs2
= zval_get_string(zv2
);
459 if (zs1
->len
> zs2
->len
) {
461 } else if (zs2
->len
> zs1
->len
) {
464 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
466 zend_string_release(zs1
);
467 zend_string_release(zs2
);
471 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
473 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
476 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
478 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
480 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
482 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
483 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
485 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
489 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
492 zend_string_release(obj
->value
);
496 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
498 object_init_ex(return_value
, ce_Symbol
);
499 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
501 sym
->sym
.sid
= sym_ptr
->sid
;
502 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
503 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
505 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
506 sym
->iloc
= Z_OBJ(ziloc
);
508 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
509 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
510 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
512 php_ion_symbol_iloc_ctor(iloc
);
515 php_ion_symbol_ctor(sym
);
517 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
518 GC_DELREF(sym
->iloc
);
522 php_ion_decl(symbol
, Symbol
, php_ion_symbol_dtor(obj
));
524 typedef struct php_ion_symbol_table
{
525 ION_SYMBOL_TABLE
*tab
;
526 int (*dtor
)(ION_SYMBOL_TABLE
*);
528 } php_ion_symbol_table
;
530 static inline void php_ion_symbol_table_ctor(php_ion_symbol_table
*obj
)
535 if (IERR_OK
== ion_symbol_table_get_name(obj
->tab
, &is
)) {
536 zend_update_property_stringl(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), (char *) is
.value
, is
.length
);
539 if (IERR_OK
== ion_symbol_table_get_version(obj
->tab
, &iv
)) {
540 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("version"), iv
);
544 static inline void php_ion_symbol_table_dtor(php_ion_symbol_table
*obj
)
554 static inline void php_ion_symbol_table_import(php_ion_symbol_table
*obj
, php_ion_symbol_table
*import
)
560 zval
*zimports
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), 0, &tmp
);
562 zend_ulong idx
= (uintptr_t) &import
->std
.gc
;
563 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports
), idx
)) {
564 ION_CHECK(ion_symbol_table_import_symbol_table(obj
->tab
, import
->tab
));
566 SEPARATE_ARRAY(zimports
);
567 GC_ADDREF(&import
->std
);
568 add_index_object(zimports
, idx
, &import
->std
);
569 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), zimports
);
574 static inline void php_ion_symbol_table_symbol_zval(php_ion_symbol_table
*obj
, ION_SYMBOL
*sym
, zval
*return_value
)
577 zval
*zsyms
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbols"), 0, &tmp
);
579 zval
*zsym
= zend_hash_index_find(Z_ARRVAL_P(zsyms
), sym
->sid
);
585 php_ion_symbol_zval(sym
, return_value
);
588 SEPARATE_ARRAY(zsyms
);
589 ZVAL_ADDREF(return_value
);
590 add_index_zval(zsyms
, sym
->sid
, return_value
);
594 php_ion_decl(symbol_table
, Symbol_Table
, php_ion_symbol_table_dtor(obj
));
596 typedef struct php_ion_decimal_ctx
{
599 } php_ion_decimal_ctx
;
601 #define php_ion_decimal_ctx_init_max(c, rounding) \
602 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
603 static inline void php_ion_decimal_ctx_init(decContext
*ctx
,
604 zend_long digits
, zend_long emax
, zend_long emin
, zend_long round
, zend_bool clamp
)
606 memset(ctx
, 0, sizeof(*ctx
));
607 ctx
->digits
= digits
;
614 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
616 if (!obj
->ctx
.digits
) {
617 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
620 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
622 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
624 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
625 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
626 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
627 zend_update_property_bool(obj
->std
.ce
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
630 php_ion_decl(decimal_ctx
, Decimal_Context
);
632 typedef struct php_ion_decimal
{
634 zend_object
*ctx
, std
;
637 static inline void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
639 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
640 ION_CHECK(ion_decimal_from_int32(dec
, num
));
641 } else if (num
> 0 && num
<= UINT32_MAX
) {
642 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
645 ION_CHECK(ion_int_alloc(NULL
, &iint
));
646 ION_CHECK(ion_int_from_long(iint
, num
),
648 /* WATCH OUT: BS API */
649 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
650 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
656 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
658 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
659 (void) ion_decimal_to_string(dec
, zstr
->val
);
660 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
663 static inline void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
666 ION_CHECK(ion_int_alloc(NULL
, &ii
));
667 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
669 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
674 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
678 if (!ion_decimal_is_integer(&obj
->dec
)) {
683 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_max
, &php_ion_globals
.decimal
.ctx
, &result
);
688 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_min
, &php_ion_globals
.decimal
.ctx
, &result
);
695 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
699 object_init_ex(&zdc
, ce_Decimal_Context
);
700 obj
->ctx
= Z_OBJ(zdc
);
701 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
704 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
706 if (php_ion_decimal_fits_zend_long(obj
)) {
708 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
709 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
711 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
712 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
713 zend_string_release(zstr
);
717 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
719 ion_decimal_free(&obj
->dec
);
722 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
724 typedef php_date_obj php_ion_timestamp
;
726 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
729 ctx
= &php_ion_globals
.decimal
.ctx
;
731 decQuad microsecs
, result
;
732 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
733 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
736 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
739 ctx
= &php_ion_globals
.decimal
.ctx
;
741 decQuad microsecs
, us
;
742 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
745 static inline zend_string
*php_dt_format_from_precision(int precision
)
747 switch (precision
& 0x7f) {
749 return zend_string_init(ZEND_STRL("c"), 0);
751 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
753 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
755 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
757 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
759 return zend_string_init(ZEND_STRL("Y\\T"), 0);
761 return zend_string_init(ZEND_STRL("c"), 0);
765 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
767 timelib_time
*time
= timelib_time_ctor();
769 int precision
= ION_TS_FRAC
;
770 ion_timestamp_get_precision(ts
, &precision
);
773 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
776 time
->s
= ts
->seconds
;
779 time
->i
= ts
->minutes
;
792 time
->z
= ts
->tz_offset
* 60;
794 time
->zone_type
= TIMELIB_ZONETYPE_OFFSET
;
796 time
->zone_type
= TIMELIB_ZONETYPE_ID
;
797 time
->tz_info
= get_timezone_info();
802 *fmt
= php_dt_format_from_precision(precision
);
807 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
809 memset(buf
, 0, sizeof(*buf
));
812 int precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
814 if (!precision
|| precision
> ION_TS_FRAC
) {
815 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
816 "Invalid precision (%d) of ion\\Timestamp", precision
);
817 } else switch ((buf
->precision
= precision
)) {
819 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
822 buf
->seconds
= ts
->time
->s
;
825 buf
->minutes
= ts
->time
->i
;
828 buf
->hours
= ts
->time
->h
;
829 buf
->day
= ts
->time
->d
;
832 buf
->month
= ts
->time
->m
;
835 buf
->year
= ts
->time
->y
;
838 buf
->tz_offset
= ts
->time
->z
/ 60;
839 if (buf
->tz_offset
) {
840 buf
->precision
|= 0x80;
847 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
850 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
852 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
854 fmt
= php_dt_format_from_precision(precision
);
855 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
856 zend_string_release(fmt
);
859 typedef struct php_ion_catalog
{
864 static inline void php_ion_catalog_ctor(php_ion_catalog
*obj
)
866 ION_CHECK(ion_catalog_open(&obj
->cat
));
869 static inline void php_ion_catalog_dtor(php_ion_catalog
*obj
)
872 ion_catalog_close(obj
->cat
);
876 static inline zend_string
*ion_symbol_table_to_key(ION_SYMBOL_TABLE
*tab
)
880 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_name(tab
, &is
));
881 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_version(tab
, &version
));
884 smart_str_appendl(&s
, (char *) is
.value
, is
.length
);
885 smart_str_appendc(&s
, ':');
886 smart_str_append_long(&s
, version
);
892 static inline void php_ion_catalog_add_symbol_table(php_ion_catalog
*obj
, php_ion_symbol_table
*tab
)
898 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
900 zend_ulong idx
= (uintptr_t) &tab
->std
.gc
;
901 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs
), idx
)) {
902 zend_string
*key
= ion_symbol_table_to_key(tab
->tab
);
904 ION_CHECK(ion_catalog_add_symbol_table(obj
->cat
, tab
->tab
),
905 zend_string_release(key
));
906 SEPARATE_ARRAY(ztabs
);
907 GC_ADDREF(&tab
->std
);
908 add_index_object(ztabs
, idx
, &tab
->std
);
909 GC_ADDREF(&tab
->std
);
910 add_assoc_object_ex(ztabs
, key
->val
, key
->len
, &tab
->std
);
911 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), ztabs
);
912 zend_string_release(key
);
918 static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog
*obj
, ION_SYMBOL_TABLE
*tab
, zval
*return_value
)
920 zend_string
*key
= ion_symbol_table_to_key(tab
);
924 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
926 zval
*ztab
= zend_hash_find(Z_ARRVAL_P(ztabs
), key
);
928 zend_string_release(key
);
931 fprintf(stderr
, "FAIL\n");
935 php_error_docref(NULL
, E_NOTICE
, "Previously unknown ion\\Symbol\\Table encountered: %s", key
->val
);
937 object_init_ex(return_value
, ce_Symbol_Table_Shared
);
938 php_ion_symbol_table
*o_tab
= php_ion_obj(symbol_table
, Z_OBJ_P(return_value
));
939 OBJ_CHECK(o_tab
, zend_string_release(key
));
940 php_ion_symbol_table_ctor(o_tab
);
943 SEPARATE_ARRAY(ztabs
);
944 ZVAL_ADDREF(return_value
);
945 add_index_zval(ztabs
, (uintptr_t) &o_tab
->std
.gc
, return_value
);
946 ZVAL_ADDREF(return_value
);
947 add_assoc_zval_ex(ztabs
, key
->val
, key
->len
, return_value
);
949 zend_string_release(key
);
952 php_ion_decl(catalog
, Catalog
, php_ion_catalog_dtor(obj
));
954 typedef struct php_ion_reader_options
{
955 ION_READER_OPTIONS opt
;
956 zend_object
*cat
, *dec_ctx
, *cb
, std
;
957 } php_ion_reader_options
;
959 php_ion_decl(reader_options
, Reader_Options
);
961 typedef struct php_ion_reader
{
975 zend_object
*opt
, std
;
978 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
980 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
981 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
983 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
984 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
985 user
->limit
-= remaining
;
988 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
991 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
992 if (EXPECTED(read
> 0)) {
997 if (EXPECTED(read
== 0)) {
1001 return IERR_READ_ERROR
;
1004 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1007 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
1013 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
1018 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
1021 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1024 opt
->opt
.context_change_notifier
.context
= obj
;
1026 if (obj
->type
== STREAM_READER
) {
1027 PTR_CHECK(obj
->stream
.ptr
);
1028 GC_ADDREF(obj
->stream
.ptr
->res
);
1030 obj
->stream
.buf
.length
= opt
&& opt
->opt
.allocation_page_size
? opt
->opt
.allocation_page_size
: 0x10000;
1031 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1032 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1035 err
= ion_reader_open_buffer(&obj
->reader
,
1036 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
1037 opt
? &opt
->opt
: NULL
);
1040 opt
->opt
.context_change_notifier
.context
= NULL
;
1046 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
1049 ion_reader_close(obj
->reader
);
1051 if (obj
->type
== STREAM_READER
) {
1052 if (obj
->stream
.buf
.value
) {
1053 efree(obj
->stream
.buf
.value
);
1055 if (obj
->stream
.ptr
) {
1056 zend_list_delete(obj
->stream
.ptr
->res
);
1060 zend_string_release(obj
->buffer
);
1065 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
1067 typedef struct php_ion_writer_options
{
1068 ION_WRITER_OPTIONS opt
;
1069 zend_object
*cat
, *dec_ctx
, *col
, std
;
1070 } php_ion_writer_options
;
1072 php_ion_decl(writer_options
, Writer_Options
);
1074 typedef struct php_ion_writer
{
1090 zend_object
*opt
, std
;
1094 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1096 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1098 if (EXPECTED(user
->limit
&& user
->curr
)) {
1099 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1100 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1101 return IERR_WRITE_ERROR
;
1104 user
->curr
= writer
->stream
.buf
.value
;
1105 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1109 #define REF_STR() do { \
1110 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
1111 GC_ADDREF(obj->buffer.str.s); \
1114 #define NEW_REF_STR() do {\
1115 if (Z_STR_P(ref) != obj->buffer.str.s) { \
1116 zval_ptr_dtor(ref); \
1121 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1123 PTR_CHECK(obj
->stream
.ptr
);
1124 GC_ADDREF(obj
->stream
.ptr
->res
);
1126 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
1127 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1129 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1131 zval
*ref
= &obj
->buffer
.val
;
1134 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
1135 smart_str_0(&obj
->buffer
.str
);
1139 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1141 zval
*ref
= &obj
->buffer
.val
;
1144 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
1149 // we've been separated
1150 GC_ADDREF(obj
->buffer
.str
.s
);
1153 // we have to separate
1154 fprintf(stderr
, "SEPARATE\n");
1155 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
1159 zend_string
*old
= obj
->buffer
.str
.s
;
1161 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1162 if (old
== obj
->buffer
.str
.s
) {
1164 } else if(old
== Z_STR_P(ref
)) {
1171 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1173 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1176 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1177 smart_str_0(&writer
->buffer
.str
);
1178 if (user
->limit
== user
->curr
) {
1179 php_ion_writer_buffer_grow(writer
);
1182 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1183 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
1188 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
1191 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1194 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1195 ION_STREAM_HANDLER h
;
1196 if (obj
->type
== STREAM_WRITER
) {
1197 h
= php_ion_writer_stream_handler
;
1198 php_ion_writer_stream_init(obj
, opt
);
1200 h
= php_ion_writer_buffer_handler
;
1201 php_ion_writer_buffer_init(obj
);
1204 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1208 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
1211 ion_writer_close(obj
->writer
);
1213 if (obj
->type
== STREAM_WRITER
) {
1214 if (obj
->stream
.buf
.value
) {
1215 efree(obj
->stream
.buf
.value
);
1217 if (obj
->stream
.ptr
) {
1218 zend_list_delete(obj
->stream
.ptr
->res
);
1221 if (obj
->buffer
.str
.s
) {
1222 smart_str_0(&obj
->buffer
.str
);
1223 zend_string_release(obj
->buffer
.str
.s
);
1225 zval_ptr_dtor(&obj
->buffer
.val
);
1229 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
1231 typedef struct php_ion_serializer_php
{
1232 php_ion_serializer serializer
;
1233 zend_object
*opt
, std
;
1234 } php_ion_serializer_php
;
1236 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
1238 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1239 ser_obj
->serializer
.ids
= global_ser
->ids
;
1240 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
1242 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1243 ser_obj
->serializer
.call_magic
);
1244 if (ser_obj
->serializer
.call_custom
) {
1245 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1246 ser_obj
->serializer
.call_custom
);
1247 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
1249 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1251 if (!ser_obj
->opt
) {
1252 ser_obj
->opt
= create_ion_Writer_Options(NULL
);
1254 GC_ADDREF(ser_obj
->opt
);
1256 ser_obj
->serializer
.options
= &php_ion_obj(writer_options
, ser_obj
->opt
)->opt
;
1257 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
1258 GC_DELREF(ser_obj
->opt
);
1261 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
1263 if (obj
->serializer
.call_custom
) {
1264 zend_string_release(obj
->serializer
.call_custom
);
1268 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1270 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
1272 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
1276 zend_string
*k
= NULL
;
1277 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1281 const char *class_name
, *prop_name
;
1282 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1283 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("p"))));
1284 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1289 ion_string_assign_cstr(&is
, (char *) prop_name
, prop_len
);
1291 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
1292 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1293 ion_string_assign_cstr(&is
, ptr
, end
- ptr
);
1296 // WATCH OUT: field names need to be copied
1298 ION_CHECK(ion_string_copy_to_owner(ser
->writer
, &fn
, &is
));
1299 ION_CHECK(ion_writer_write_field_name(ser
->writer
, &fn
));
1301 php_ion_serialize_zval(ser
, v
);
1303 ZEND_HASH_FOREACH_END();
1305 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1308 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1310 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1313 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1314 php_ion_serialize_zval(ser
, v
);
1316 ZEND_HASH_FOREACH_END();
1318 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1321 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1323 if (zend_array_is_list(arr
)) {
1324 php_ion_serialize_list(ser
, arr
);
1326 php_ion_serialize_struct(ser
, arr
, false);
1330 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1336 ZVAL_OBJ(&tmp
, zobject
);
1337 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1339 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("S"))));
1340 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1341 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_assign_cstr(&is
, (char *) buf
, len
)));
1343 } else if (!EG(exception
)){
1344 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1345 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1349 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1354 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1357 if (IS_ARRAY
== Z_TYPE(rv
)) {
1359 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, fn
? "C" : "O", 1)));
1360 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1361 php_ion_serialize_zval(ser
, &rv
);
1364 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1365 "%s serializer %s::%s did not return an array",
1366 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1367 fn
? fn
->common
.function_name
->val
: "__serialize");
1371 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1374 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("E"))));
1376 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1377 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1378 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1381 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1385 if (zobject
->ce
!= zend_standard_class_def
) {
1386 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("c"))));
1387 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1389 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("o"))));
1393 ZVAL_OBJ(&zobj
, zobject
);
1394 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1396 php_ion_serialize_struct(ser
, props
, true);
1397 zend_release_properties(props
);
1399 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1400 "Could not get properties for serialization of class %s",
1401 zobject
->ce
->name
->val
);
1405 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1407 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1408 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1409 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1411 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1414 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1417 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1418 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1423 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1425 return ce
->__serialize
&& ser
->call_magic
;
1428 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1430 return !!ce
->serialize
;
1433 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1435 if (ser
->call_custom
) {
1436 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1441 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1444 zend_class_entry
*ce
= zobject
->ce
;
1447 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1448 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1449 "Serializing %s is not allowed", ce
->name
->val
);
1453 if (can_call_magic_serialize(ser
, ce
)) {
1454 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1455 } else if (can_call_iface_serialize(ser
, ce
)) {
1456 php_ion_serialize_object_iface(ser
, zobject
);
1457 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1458 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1459 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1460 php_ion_serialize_object_enum(ser
, zobject
);
1461 } else if (ce
== ce_Symbol
) {
1462 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1463 } else if (ce
== ce_Decimal
) {
1464 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1465 } else if (ce
== ce_Timestamp
) {
1467 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1468 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1469 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1470 } else if (ce
== ce_LOB
) {
1471 php_ion_serialize_object_lob(ser
, zobject
);
1473 php_ion_serialize_object_std(ser
, zobject
);
1477 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1479 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1482 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1483 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1485 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("r"))));
1486 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1490 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1491 zend_hash_index_add(ser
->ids
, idx
, &num
);
1494 zend_hash_next_index_insert(ser
->tmp
, zv
);
1496 switch (Z_TYPE_P(zv
)) {
1498 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1502 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1506 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1510 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("R"))));
1511 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1517 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1521 switch (Z_TYPE_P(zv
)) {
1523 ION_CHECK(ion_writer_write_null(ser
->writer
));
1526 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1529 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1532 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1535 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1541 php_ion_serialize_refcounted(ser
, zv
);
1544 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1545 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1549 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1551 static inline void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1554 HashTable
*arr
= NULL
;
1556 if (ser
->multi_seq
) {
1557 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1558 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1559 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1563 arr
= Z_ARRVAL_P(zv
);
1565 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1566 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1570 php_ion_globals_serializer_step();
1571 php_ion_serialize_zval(ser
, zv
);
1572 php_ion_globals_serializer_exit();
1574 if (!ser
->multi_seq
) {
1577 zend_hash_move_forward_ex(arr
, &pos
);
1578 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1582 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1584 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1587 zo_ser
= create_ion_Serializer_PHP(NULL
);
1588 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1590 o_ser
->serializer
.call_magic
= true;
1591 php_ion_serializer_php_ctor(o_ser
);
1593 ser
= &o_ser
->serializer
;
1596 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1597 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1598 writer
->type
= BUFFER_WRITER
;
1601 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1602 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1605 php_ion_writer_ctor(writer
);
1606 ser
->writer
= writer
->writer
;
1607 ser
->buffer
= &writer
->buffer
.str
;
1609 if (!EG(exception
)) {
1610 php_ion_serialize_ex(ser
, zv
);
1613 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1614 ion_writer_flush(ser
->writer
, NULL
);
1615 RETVAL_STR_COPY(ser
->buffer
->s
);
1617 OBJ_RELEASE(zo_writer
);
1619 OBJ_RELEASE(zo_opt
);
1622 OBJ_RELEASE(zo_ser
);
1626 typedef struct php_ion_unserializer_php
{
1627 php_ion_unserializer unserializer
;
1628 zend_object
*opt
, std
;
1629 } php_ion_unserializer_php
;
1631 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1633 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1634 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1635 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1636 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1638 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1639 ser_obj
->unserializer
.call_magic
);
1640 if (ser_obj
->unserializer
.call_custom
) {
1641 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1642 ser_obj
->unserializer
.call_custom
);
1643 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1645 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1647 if (!ser_obj
->opt
) {
1648 ser_obj
->opt
= create_ion_Reader_Options(NULL
);
1650 GC_ADDREF(ser_obj
->opt
);
1652 ser_obj
->unserializer
.options
= &php_ion_obj(reader_options
, ser_obj
->opt
)->opt
;
1653 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1654 GC_DELREF(ser_obj
->opt
);
1657 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1659 if (obj
->unserializer
.call_custom
) {
1660 zend_string_release(obj
->unserializer
.call_custom
);
1664 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1666 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1668 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1671 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1673 return (ce
&& ce
->unserialize
);
1676 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1678 if (ser
->call_custom
) {
1679 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1684 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1686 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1689 object_init_ex(return_value
, ce
);
1690 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1693 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1694 "Could not find class %s", ser
->annotations
.object_class
->val
);
1698 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1700 zend_string
*zs_case
= zval_get_string(return_value
);
1703 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1704 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1705 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1706 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1709 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1710 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1711 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1714 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1715 zend_hash_next_index_insert(ser
->ids
, return_value
);
1716 zend_string_release(zs_case
);
1719 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1721 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1722 if (can_call_iface_unserialize(ser
, ce
)) {
1723 zend_string
*s
= zval_get_string(return_value
);
1724 ZVAL_NULL(return_value
);
1725 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1726 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1727 RETVAL_ZVAL(backref
, 0, 0);
1728 } else if (!EG(exception
)) {
1729 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1730 "Failed to unserialize class %s", ce
->name
->val
);
1732 zend_string_release(s
);
1734 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1735 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1739 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1741 // FIXME: symbol table
1743 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1746 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1747 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1748 name
= is_ptr
->value
;
1749 } else if (is_ptr
) {
1750 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1751 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1753 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1757 *key
= zend_string_from_ion(&name
);
1760 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1762 zend_hash_next_index_insert(ser
->ids
, return_value
);
1764 ION_CHECK(ion_reader_step_in(ser
->reader
));
1768 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1770 if (typ
== tid_EOF
) {
1775 php_ion_unserialize_field_name(ser
, &key
);
1779 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1780 ION_CATCH(zend_string_release(key
));
1782 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1783 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1784 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1786 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1787 zval_ptr_dtor(&zvalue
);
1788 zend_string_release(key
);
1791 ION_CHECK(ion_reader_step_out(ser
->reader
));
1795 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
1796 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
1797 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
1798 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
1799 * nondeterministic behavior.
1801 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1803 zend_hash_next_index_insert(ser
->ids
, return_value
);
1805 ION_CHECK(ion_reader_step_in(ser
->reader
));
1809 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1811 if (typ
== tid_EOF
) {
1816 php_ion_unserialize_field_name(ser
, &key
);
1820 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1821 ION_CATCH(zend_string_release(key
));
1823 // FIXME:: too naive; b0rked if the previous value is an array
1824 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
1825 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
1826 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
1828 Z_TRY_ADDREF_P(prev
);
1829 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
1830 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
1832 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
1834 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1836 zend_string_release(key
);
1839 ION_CHECK(ion_reader_step_out(ser
->reader
));
1842 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1844 switch (ser
->annotations
.object_type
) {
1850 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1851 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1852 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1857 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1858 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1859 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1861 *fn
= zobject
->ce
->__unserialize
;
1865 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1866 "Invalid object type %c", ser
->annotations
.object_type
);
1870 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1872 // backup possible backref to array returned by magic/custom __serialize()
1873 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1875 php_ion_unserialize_class(ser
, return_value
);
1878 zend_function
*fn
= NULL
;
1879 zend_object
*zobject
= Z_OBJ_P(return_value
);
1880 verify_unserializer(ser
, zobject
, &fn
);
1885 php_ion_unserialize_props(ser
, return_value
);
1890 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1891 zval_ptr_dtor(input
);
1893 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1894 php_ion_unserialize_hash(ser
, input
);
1899 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1903 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1905 if (ser
->annotations
.object_class
) {
1906 switch (ser
->annotations
.object_type
) {
1908 php_ion_unserialize_object_iface(ser
, return_value
);
1911 php_ion_unserialize_object_enum(ser
, return_value
);
1914 php_ion_unserialize_object(ser
, return_value
);
1916 } else if (!ser
->annotations
.object_type
) {
1917 array_init(return_value
);
1918 php_ion_unserialize_hash(ser
, return_value
);
1919 } else if (ser
->annotations
.object_type
== 'o') {
1920 object_init(return_value
);
1921 php_ion_unserialize_hash(ser
, return_value
);
1923 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1924 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1928 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1930 ION_CHECK(ion_reader_step_in(ser
->reader
));
1931 array_init(return_value
);
1932 zend_hash_next_index_insert(ser
->ids
, return_value
);
1936 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1938 if (typ
== tid_EOF
) {
1943 php_ion_unserialize_zval(ser
, &next
, &typ
);
1946 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1949 ION_CHECK(ion_reader_step_out(ser
->reader
));
1952 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1954 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1957 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1958 if (err
== IERR_BUFFER_TOO_SMALL
) {
1959 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1962 ION_CHECK(err
, zend_string_release(zstr
));
1963 if (zstr
->len
> read
) {
1964 zstr
->val
[read
] = 0;
1965 zstr
= zend_string_truncate(zstr
, read
, 0);
1970 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1973 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1975 object_init_ex(return_value
, ce_Timestamp
);
1976 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1978 zend_string
*fmt
= NULL
;
1979 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1980 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1981 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1982 zend_string_release(fmt
);
1987 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1989 ION_INT
*num
= NULL
;
1990 ION_CHECK(ion_int_alloc(reader
, &num
));
1991 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1993 // TODO: SIZEOF_ZEND_LONG == 4
1995 iERR err
= ion_int_to_int64(num
, &i64
);
2001 case IERR_NUMERIC_OVERFLOW
:
2003 ION_CHECK(ion_int_char_length(num
, &max
));
2004 zend_string
*zs
= zend_string_alloc(max
, 0);
2006 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2007 zs
->val
[zs
->len
= len
] = 0;
2018 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2020 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2023 ZVAL_COPY_VALUE(return_value
, backref
);
2024 zend_hash_next_index_insert(ser
->addref
, return_value
);
2026 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
2027 "Could not find back reference %ld", Z_LVAL_P(return_value
));
2031 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2033 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2036 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
2037 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2039 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
2041 if (ann_str
.length
!= 1) {
2045 switch (*ann_str
.value
) {
2047 if (ser
->annotations
.makeref
) {
2048 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2049 "Invalid multiple reference annotations");
2052 ser
->annotations
.makeref
= true;
2056 if (ser
->annotations
.backref
) {
2057 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2058 "Invalid multiple back reference annotations");
2061 ser
->annotations
.backref
= true;
2065 if (ser
->annotations
.object_prop
) {
2066 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2067 "Invalid multiple object property annotations");
2070 ser
->annotations
.object_prop
= true;
2072 ION_STRING prop_class
;
2073 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
2074 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
2077 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
2078 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
2087 if (ser
->annotations
.object_type
) {
2088 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2089 "Invalid multiple object type annotations: %c::%c",
2090 ser
->annotations
.object_type
, *ann_str
.value
);
2093 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
2094 ION_STRING class_name
;
2095 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
2096 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
2099 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
2100 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
2106 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2107 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2108 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2111 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2112 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2113 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2119 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2122 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
2125 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
2128 php_ion_unserialize_annotations(ser
);
2131 if (ser
->annotations
.makeref
) {
2132 ZVAL_MAKE_REF(return_value
);
2133 zend_hash_next_index_insert(ser
->ids
, return_value
);
2134 ZVAL_DEREF(return_value
);
2137 if (ION_TYPE_INT(*typ
) > 0) {
2139 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
2145 switch (ION_TYPE_INT(*typ
)) {
2147 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
2152 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
2156 php_ion_reader_read_int(ser
->reader
, return_value
);
2157 if (ser
->annotations
.backref
) {
2159 php_ion_unserialize_backref(ser
, return_value
);
2161 if (ser
->annotations
.object_type
) {
2162 if (!ser
->annotations
.backref
) {
2163 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2164 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2165 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2168 goto unserialize_struct
;
2174 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
2177 case tid_DECIMAL_INT
:
2178 object_init_ex(return_value
, ce_Decimal
);
2179 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2180 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
2181 php_ion_decimal_ctor(dec
);
2182 zend_hash_next_index_insert(ser
->ids
, return_value
);
2185 case tid_TIMESTAMP_INT
:
2186 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
2187 zend_hash_next_index_insert(ser
->ids
, return_value
);
2190 case tid_SYMBOL_INT
:
2192 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
2193 php_ion_symbol_zval(&sym
, return_value
);
2194 if (ser
->annotations
.object_type
) {
2195 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2196 goto unserialize_struct
;
2198 zend_hash_next_index_insert(ser
->ids
, return_value
);
2201 case tid_STRING_INT
:
2203 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
2204 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2205 if (ser
->annotations
.object_type
) {
2206 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2207 goto unserialize_struct
;
2209 zend_hash_next_index_insert(ser
->ids
, return_value
);
2214 php_ion_reader_read_lob(ser
->reader
, return_value
);
2215 if (ser
->annotations
.object_type
) {
2216 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2217 goto unserialize_struct
;
2219 zend_hash_next_index_insert(ser
->ids
, return_value
);
2223 case tid_SEXP_INT
: // FIXME
2224 php_ion_unserialize_list(ser
, return_value
);
2225 if (!ser
->annotations
.object_type
) {
2230 case tid_STRUCT_INT
:
2231 unserialize_struct
: ;
2232 php_ion_unserialize_struct(ser
, return_value
);
2239 case tid_DATAGRAM_INT
:
2240 ZEND_ASSERT(!"datagram");
2246 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
2248 static inline void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2250 if (ser
->multi_seq
) {
2251 array_init(return_value
);
2257 php_ion_globals_unserializer_step();
2258 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2259 php_ion_globals_unserializer_exit();
2260 ION_CATCH(zval_ptr_dtor(&tmp
));
2262 if (!ser
->multi_seq
) {
2263 RETURN_COPY_VALUE(&tmp
);
2264 } else if (ser
->type
!= tid_EOF
) {
2265 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2267 } while (ser
->type
!= tid_EOF
);
2270 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
2272 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
2275 zo_ser
= create_ion_Unserializer_PHP(NULL
);
2276 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
2278 o_ser
->unserializer
.call_magic
= true;
2279 php_ion_unserializer_php_ctor(o_ser
);
2281 ser
= &o_ser
->unserializer
;
2284 zend_object
*zo_reader
;
2285 php_ion_reader
*reader
;
2288 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2289 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2290 reader
= php_ion_obj(reader
, zo_reader
);
2291 reader
->type
= STREAM_READER
;
2292 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2293 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2294 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2295 reader
= php_ion_obj(reader
, zo_reader
);
2296 reader
->type
= BUFFER_READER
;
2297 reader
->buffer
= zval_get_string(zdata
);
2299 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
2300 "Invalid source to unserialize; expected string or resource");
2302 OBJ_RELEASE(zo_ser
);
2308 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
2309 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
2312 php_ion_reader_ctor(reader
);
2313 ser
->reader
= reader
->reader
;
2315 if (!EG(exception
)) {
2316 php_ion_unserialize_ex(ser
, return_value
);
2319 OBJ_RELEASE(zo_reader
);
2321 OBJ_RELEASE(zo_opt
);
2324 OBJ_RELEASE(zo_ser
);
2328 static inline zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
2330 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
2332 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
2334 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
2337 object_init_ex(&zsym
, ce_Symbol
);
2338 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
2339 sym
->sym
.sid
= Z_LVAL_P(zid
);
2340 sym
->value
= zval_get_string(zss
);
2341 php_ion_symbol_ctor(sym
);
2342 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);