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
);
951 php_ion_decl(catalog
, Catalog
, php_ion_catalog_dtor(obj
));
953 typedef struct php_ion_reader_options
{
954 ION_READER_OPTIONS opt
;
955 zend_object
*cat
, *dec_ctx
, *cb
, std
;
956 } php_ion_reader_options
;
958 php_ion_decl(reader_options
, Reader_Options
);
960 typedef struct php_ion_reader
{
974 zend_object
*opt
, std
;
977 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
979 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
980 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
982 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
983 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
984 user
->limit
-= remaining
;
987 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
990 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
991 if (EXPECTED(read
> 0)) {
996 if (EXPECTED(read
== 0)) {
1000 return IERR_READ_ERROR
;
1003 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1006 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
1012 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
1017 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
1020 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1023 opt
->opt
.context_change_notifier
.context
= obj
;
1025 if (obj
->type
== STREAM_READER
) {
1026 PTR_CHECK(obj
->stream
.ptr
);
1027 GC_ADDREF(obj
->stream
.ptr
->res
);
1029 obj
->stream
.buf
.length
= opt
&& opt
->opt
.allocation_page_size
? opt
->opt
.allocation_page_size
: 0x10000;
1030 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1031 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1034 err
= ion_reader_open_buffer(&obj
->reader
,
1035 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
1036 opt
? &opt
->opt
: NULL
);
1039 opt
->opt
.context_change_notifier
.context
= NULL
;
1045 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
1048 ion_reader_close(obj
->reader
);
1050 if (obj
->type
== STREAM_READER
) {
1051 if (obj
->stream
.buf
.value
) {
1052 efree(obj
->stream
.buf
.value
);
1054 if (obj
->stream
.ptr
) {
1055 zend_list_delete(obj
->stream
.ptr
->res
);
1059 zend_string_release(obj
->buffer
);
1064 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
1066 typedef struct php_ion_writer_options
{
1067 ION_WRITER_OPTIONS opt
;
1068 zend_object
*cat
, *dec_ctx
, *col
, std
;
1069 } php_ion_writer_options
;
1071 php_ion_decl(writer_options
, Writer_Options
);
1073 typedef struct php_ion_writer
{
1089 zend_object
*opt
, std
;
1093 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1095 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1097 if (EXPECTED(user
->limit
&& user
->curr
)) {
1098 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1099 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1100 return IERR_WRITE_ERROR
;
1103 user
->curr
= writer
->stream
.buf
.value
;
1104 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1108 #define REF_STR() do { \
1109 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
1110 GC_ADDREF(obj->buffer.str.s); \
1113 #define NEW_REF_STR() do {\
1114 if (Z_STR_P(ref) != obj->buffer.str.s) { \
1115 zval_ptr_dtor(ref); \
1120 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1122 PTR_CHECK(obj
->stream
.ptr
);
1123 GC_ADDREF(obj
->stream
.ptr
->res
);
1125 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
1126 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1128 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1130 zval
*ref
= &obj
->buffer
.val
;
1133 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
1134 smart_str_0(&obj
->buffer
.str
);
1138 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1140 zval
*ref
= &obj
->buffer
.val
;
1143 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
1148 // we've been separated
1149 GC_ADDREF(obj
->buffer
.str
.s
);
1152 // we have to separate
1153 fprintf(stderr
, "SEPARATE\n");
1154 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
1158 zend_string
*old
= obj
->buffer
.str
.s
;
1160 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1161 if (old
== obj
->buffer
.str
.s
) {
1163 } else if(old
== Z_STR_P(ref
)) {
1170 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1172 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1175 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1176 smart_str_0(&writer
->buffer
.str
);
1177 if (user
->limit
== user
->curr
) {
1178 php_ion_writer_buffer_grow(writer
);
1181 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1182 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
1187 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
1190 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1193 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1194 ION_STREAM_HANDLER h
;
1195 if (obj
->type
== STREAM_WRITER
) {
1196 h
= php_ion_writer_stream_handler
;
1197 php_ion_writer_stream_init(obj
, opt
);
1199 h
= php_ion_writer_buffer_handler
;
1200 php_ion_writer_buffer_init(obj
);
1203 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1207 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
1210 ion_writer_close(obj
->writer
);
1212 if (obj
->type
== STREAM_WRITER
) {
1213 if (obj
->stream
.buf
.value
) {
1214 efree(obj
->stream
.buf
.value
);
1216 if (obj
->stream
.ptr
) {
1217 zend_list_delete(obj
->stream
.ptr
->res
);
1220 if (obj
->buffer
.str
.s
) {
1221 smart_str_0(&obj
->buffer
.str
);
1222 zend_string_release(obj
->buffer
.str
.s
);
1224 zval_ptr_dtor(&obj
->buffer
.val
);
1228 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
1230 typedef struct php_ion_serializer_php
{
1231 php_ion_serializer serializer
;
1232 zend_object
*opt
, std
;
1233 } php_ion_serializer_php
;
1235 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
1237 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1238 ser_obj
->serializer
.ids
= global_ser
->ids
;
1239 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
1241 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1242 ser_obj
->serializer
.call_magic
);
1243 if (ser_obj
->serializer
.call_custom
) {
1244 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1245 ser_obj
->serializer
.call_custom
);
1246 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
1248 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1250 if (!ser_obj
->opt
) {
1251 ser_obj
->opt
= create_ion_Writer_Options(NULL
);
1253 GC_ADDREF(ser_obj
->opt
);
1255 ser_obj
->serializer
.options
= &php_ion_obj(writer_options
, ser_obj
->opt
)->opt
;
1256 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
1257 GC_DELREF(ser_obj
->opt
);
1260 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
1262 if (obj
->serializer
.call_custom
) {
1263 zend_string_release(obj
->serializer
.call_custom
);
1267 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1269 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
1271 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
1275 zend_string
*k
= NULL
;
1276 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1280 const char *class_name
, *prop_name
;
1281 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1282 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("p"))));
1283 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1288 ion_string_assign_cstr(&is
, (char *) prop_name
, prop_len
);
1290 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
1291 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1292 ion_string_assign_cstr(&is
, ptr
, end
- ptr
);
1295 // WATCH OUT: field names need to be copied
1297 ION_CHECK(ion_string_copy_to_owner(ser
->writer
, &fn
, &is
));
1298 ION_CHECK(ion_writer_write_field_name(ser
->writer
, &fn
));
1300 php_ion_serialize_zval(ser
, v
);
1302 ZEND_HASH_FOREACH_END();
1304 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1307 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1309 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1312 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1313 php_ion_serialize_zval(ser
, v
);
1315 ZEND_HASH_FOREACH_END();
1317 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1320 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1322 if (zend_array_is_list(arr
)) {
1323 php_ion_serialize_list(ser
, arr
);
1325 php_ion_serialize_struct(ser
, arr
, false);
1329 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1335 ZVAL_OBJ(&tmp
, zobject
);
1336 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1338 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("S"))));
1339 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1340 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_assign_cstr(&is
, (char *) buf
, len
)));
1342 } else if (!EG(exception
)){
1343 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1344 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1348 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1353 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1356 if (IS_ARRAY
== Z_TYPE(rv
)) {
1358 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, fn
? "C" : "O", 1)));
1359 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1360 php_ion_serialize_zval(ser
, &rv
);
1363 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1364 "%s serializer %s::%s did not return an array",
1365 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1366 fn
? fn
->common
.function_name
->val
: "__serialize");
1370 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1373 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("E"))));
1375 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1376 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1377 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1380 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1384 if (zobject
->ce
!= zend_standard_class_def
) {
1385 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("c"))));
1386 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1388 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("o"))));
1392 ZVAL_OBJ(&zobj
, zobject
);
1393 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1395 php_ion_serialize_struct(ser
, props
, true);
1396 zend_release_properties(props
);
1398 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1399 "Could not get properties for serialization of class %s",
1400 zobject
->ce
->name
->val
);
1404 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1406 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1407 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1408 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1410 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1413 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1416 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1417 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1422 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1424 return ce
->__serialize
&& ser
->call_magic
;
1427 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1429 return !!ce
->serialize
;
1432 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1434 if (ser
->call_custom
) {
1435 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1440 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1443 zend_class_entry
*ce
= zobject
->ce
;
1446 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1447 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1448 "Serializing %s is not allowed", ce
->name
->val
);
1452 if (can_call_magic_serialize(ser
, ce
)) {
1453 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1454 } else if (can_call_iface_serialize(ser
, ce
)) {
1455 php_ion_serialize_object_iface(ser
, zobject
);
1456 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1457 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1458 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1459 php_ion_serialize_object_enum(ser
, zobject
);
1460 } else if (ce
== ce_Symbol
) {
1461 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1462 } else if (ce
== ce_Decimal
) {
1463 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1464 } else if (ce
== ce_Timestamp
) {
1466 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1467 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1468 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1469 } else if (ce
== ce_LOB
) {
1470 php_ion_serialize_object_lob(ser
, zobject
);
1472 php_ion_serialize_object_std(ser
, zobject
);
1476 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1478 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1481 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1482 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1484 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("r"))));
1485 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1489 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1490 zend_hash_index_add(ser
->ids
, idx
, &num
);
1493 zend_hash_next_index_insert(ser
->tmp
, zv
);
1495 switch (Z_TYPE_P(zv
)) {
1497 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1501 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1505 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1509 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("R"))));
1510 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1516 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1520 switch (Z_TYPE_P(zv
)) {
1522 ION_CHECK(ion_writer_write_null(ser
->writer
));
1525 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1528 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1531 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1534 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1540 php_ion_serialize_refcounted(ser
, zv
);
1543 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1544 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1548 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1550 static inline void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1553 HashTable
*arr
= NULL
;
1555 if (ser
->multi_seq
) {
1556 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1557 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1558 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1562 arr
= Z_ARRVAL_P(zv
);
1564 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1565 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1569 /* start off with a global PHP annotation instead of repeating it all over the place */
1570 if (0 == php_ion_globals_serializer_step()) {
1572 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("PHP"))));
1574 php_ion_serialize_zval(ser
, zv
);
1575 php_ion_globals_serializer_exit();
1577 if (!ser
->multi_seq
) {
1580 zend_hash_move_forward_ex(arr
, &pos
);
1581 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1585 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1587 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1590 zo_ser
= create_ion_Serializer_PHP(NULL
);
1591 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1593 o_ser
->serializer
.call_magic
= true;
1594 php_ion_serializer_php_ctor(o_ser
);
1596 ser
= &o_ser
->serializer
;
1599 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1600 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1601 writer
->type
= BUFFER_WRITER
;
1604 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1605 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1608 php_ion_writer_ctor(writer
);
1609 ser
->writer
= writer
->writer
;
1610 ser
->buffer
= &writer
->buffer
.str
;
1612 if (!EG(exception
)) {
1613 php_ion_serialize_ex(ser
, zv
);
1616 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1617 ion_writer_flush(ser
->writer
, NULL
);
1618 RETVAL_STR_COPY(ser
->buffer
->s
);
1620 OBJ_RELEASE(zo_writer
);
1622 OBJ_RELEASE(zo_opt
);
1625 OBJ_RELEASE(zo_ser
);
1629 typedef struct php_ion_unserializer_php
{
1630 php_ion_unserializer unserializer
;
1631 zend_object
*opt
, std
;
1632 } php_ion_unserializer_php
;
1634 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1636 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1637 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1638 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1639 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1641 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1642 ser_obj
->unserializer
.call_magic
);
1643 if (ser_obj
->unserializer
.call_custom
) {
1644 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1645 ser_obj
->unserializer
.call_custom
);
1646 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1648 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1650 if (!ser_obj
->opt
) {
1651 ser_obj
->opt
= create_ion_Reader_Options(NULL
);
1653 GC_ADDREF(ser_obj
->opt
);
1655 ser_obj
->unserializer
.options
= &php_ion_obj(reader_options
, ser_obj
->opt
)->opt
;
1656 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1657 GC_DELREF(ser_obj
->opt
);
1660 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1662 if (obj
->unserializer
.call_custom
) {
1663 zend_string_release(obj
->unserializer
.call_custom
);
1667 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1669 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1671 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1674 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1676 return (ce
&& ce
->unserialize
);
1679 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1681 if (ser
->call_custom
) {
1682 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1687 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1689 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1692 object_init_ex(return_value
, ce
);
1693 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1696 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1697 "Could not find class %s", ser
->annotations
.object_class
->val
);
1701 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1703 zend_string
*zs_case
= zval_get_string(return_value
);
1706 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1707 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1708 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1709 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1712 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1713 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1714 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1717 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1718 zend_hash_next_index_insert(ser
->ids
, return_value
);
1719 zend_string_release(zs_case
);
1722 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1724 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1725 if (can_call_iface_unserialize(ser
, ce
)) {
1726 zend_string
*s
= zval_get_string(return_value
);
1727 ZVAL_NULL(return_value
);
1728 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1729 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1730 RETVAL_ZVAL(backref
, 0, 0);
1731 } else if (!EG(exception
)) {
1732 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1733 "Failed to unserialize class %s", ce
->name
->val
);
1735 zend_string_release(s
);
1737 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1738 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1742 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1744 // FIXME: symbol table
1746 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1749 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1750 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1751 name
= is_ptr
->value
;
1752 } else if (is_ptr
) {
1753 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1754 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1756 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1760 *key
= zend_string_from_ion(&name
);
1763 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1765 zend_hash_next_index_insert(ser
->ids
, return_value
);
1767 ION_CHECK(ion_reader_step_in(ser
->reader
));
1771 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1773 if (typ
== tid_EOF
) {
1778 php_ion_unserialize_field_name(ser
, &key
);
1782 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1783 ION_CATCH(zend_string_release(key
));
1785 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1786 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1787 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1789 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1790 zval_ptr_dtor(&zvalue
);
1791 zend_string_release(key
);
1794 ION_CHECK(ion_reader_step_out(ser
->reader
));
1798 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
1799 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
1800 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
1801 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
1802 * nondeterministic behavior.
1804 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1806 zend_hash_next_index_insert(ser
->ids
, return_value
);
1808 ION_CHECK(ion_reader_step_in(ser
->reader
));
1812 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1814 if (typ
== tid_EOF
) {
1819 php_ion_unserialize_field_name(ser
, &key
);
1823 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1824 ION_CATCH(zend_string_release(key
));
1826 // FIXME:: too naive; b0rked if the previous value is an array
1827 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
1828 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
1829 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
1831 Z_TRY_ADDREF_P(prev
);
1832 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
1833 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
1835 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
1837 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1839 zend_string_release(key
);
1842 ION_CHECK(ion_reader_step_out(ser
->reader
));
1845 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1847 switch (ser
->annotations
.object_type
) {
1853 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1854 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1855 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1860 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1861 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1862 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1864 *fn
= zobject
->ce
->__unserialize
;
1868 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1869 "Invalid object type %c", ser
->annotations
.object_type
);
1873 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1875 // backup possible backref to array returned by magic/custom __serialize()
1876 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1878 php_ion_unserialize_class(ser
, return_value
);
1881 zend_function
*fn
= NULL
;
1882 zend_object
*zobject
= Z_OBJ_P(return_value
);
1883 verify_unserializer(ser
, zobject
, &fn
);
1888 php_ion_unserialize_props(ser
, return_value
);
1893 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1894 zval_ptr_dtor(input
);
1896 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1897 php_ion_unserialize_hash(ser
, input
);
1902 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1906 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1908 if (ser
->annotations
.object_class
) {
1909 switch (ser
->annotations
.object_type
) {
1911 php_ion_unserialize_object_iface(ser
, return_value
);
1914 php_ion_unserialize_object_enum(ser
, return_value
);
1917 php_ion_unserialize_object(ser
, return_value
);
1919 } else if (!ser
->annotations
.object_type
) {
1920 array_init(return_value
);
1921 php_ion_unserialize_hash(ser
, return_value
);
1922 } else if (ser
->annotations
.object_type
== 'o') {
1923 object_init(return_value
);
1924 php_ion_unserialize_hash(ser
, return_value
);
1926 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1927 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1931 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1933 ION_CHECK(ion_reader_step_in(ser
->reader
));
1934 array_init(return_value
);
1935 zend_hash_next_index_insert(ser
->ids
, return_value
);
1939 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1941 if (typ
== tid_EOF
) {
1946 php_ion_unserialize_zval(ser
, &next
, &typ
);
1949 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1952 ION_CHECK(ion_reader_step_out(ser
->reader
));
1955 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1957 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1960 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1961 if (err
== IERR_BUFFER_TOO_SMALL
) {
1962 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1965 ION_CHECK(err
, zend_string_release(zstr
));
1966 if (zstr
->len
> read
) {
1967 zstr
->val
[read
] = 0;
1968 zstr
= zend_string_truncate(zstr
, read
, 0);
1973 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1976 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1978 object_init_ex(return_value
, ce_Timestamp
);
1979 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1981 zend_string
*fmt
= NULL
;
1982 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1983 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1984 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1985 zend_string_release(fmt
);
1990 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1992 ION_INT
*num
= NULL
;
1993 ION_CHECK(ion_int_alloc(reader
, &num
));
1994 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1996 // TODO: SIZEOF_ZEND_LONG == 4
1998 iERR err
= ion_int_to_int64(num
, &i64
);
2004 case IERR_NUMERIC_OVERFLOW
:
2006 ION_CHECK(ion_int_char_length(num
, &max
));
2007 zend_string
*zs
= zend_string_alloc(max
, 0);
2009 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2010 zs
->val
[zs
->len
= len
] = 0;
2021 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2023 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2026 ZVAL_COPY_VALUE(return_value
, backref
);
2027 zend_hash_next_index_insert(ser
->addref
, return_value
);
2029 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
2030 "Could not find back reference %ld", Z_LVAL_P(return_value
));
2034 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2036 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2039 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
2040 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2042 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
2044 if (ann_str
.length
!= 1) {
2048 switch (*ann_str
.value
) {
2050 if (ser
->annotations
.makeref
) {
2051 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2052 "Invalid multiple reference annotations");
2055 ser
->annotations
.makeref
= true;
2059 if (ser
->annotations
.backref
) {
2060 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2061 "Invalid multiple back reference annotations");
2064 ser
->annotations
.backref
= true;
2068 if (ser
->annotations
.object_prop
) {
2069 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2070 "Invalid multiple object property annotations");
2073 ser
->annotations
.object_prop
= true;
2075 ION_STRING prop_class
;
2076 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
2077 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
2080 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
2081 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
2090 if (ser
->annotations
.object_type
) {
2091 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2092 "Invalid multiple object type annotations: %c::%c",
2093 ser
->annotations
.object_type
, *ann_str
.value
);
2096 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
2097 ION_STRING class_name
;
2098 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
2099 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
2102 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
2103 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
2109 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2110 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2111 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2114 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2115 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2116 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2122 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2125 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
2128 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
2131 php_ion_unserialize_annotations(ser
);
2134 if (ser
->annotations
.makeref
) {
2135 ZVAL_MAKE_REF(return_value
);
2136 zend_hash_next_index_insert(ser
->ids
, return_value
);
2137 ZVAL_DEREF(return_value
);
2140 if (ION_TYPE_INT(*typ
) > 0) {
2142 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
2148 switch (ION_TYPE_INT(*typ
)) {
2150 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
2155 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
2159 php_ion_reader_read_int(ser
->reader
, return_value
);
2160 if (ser
->annotations
.backref
) {
2162 php_ion_unserialize_backref(ser
, return_value
);
2164 if (ser
->annotations
.object_type
) {
2165 if (!ser
->annotations
.backref
) {
2166 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2167 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2168 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2171 goto unserialize_struct
;
2177 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
2180 case tid_DECIMAL_INT
:
2181 object_init_ex(return_value
, ce_Decimal
);
2182 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2183 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
2184 php_ion_decimal_ctor(dec
);
2185 zend_hash_next_index_insert(ser
->ids
, return_value
);
2188 case tid_TIMESTAMP_INT
:
2189 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
2190 zend_hash_next_index_insert(ser
->ids
, return_value
);
2193 case tid_SYMBOL_INT
:
2195 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
2196 php_ion_symbol_zval(&sym
, return_value
);
2197 if (ser
->annotations
.object_type
) {
2198 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2199 goto unserialize_struct
;
2201 zend_hash_next_index_insert(ser
->ids
, return_value
);
2204 case tid_STRING_INT
:
2206 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
2207 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2208 if (ser
->annotations
.object_type
) {
2209 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2210 goto unserialize_struct
;
2212 zend_hash_next_index_insert(ser
->ids
, return_value
);
2217 php_ion_reader_read_lob(ser
->reader
, return_value
);
2218 if (ser
->annotations
.object_type
) {
2219 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2220 goto unserialize_struct
;
2222 zend_hash_next_index_insert(ser
->ids
, return_value
);
2226 case tid_SEXP_INT
: // FIXME
2227 php_ion_unserialize_list(ser
, return_value
);
2228 if (!ser
->annotations
.object_type
) {
2233 case tid_STRUCT_INT
:
2234 unserialize_struct
: ;
2235 php_ion_unserialize_struct(ser
, return_value
);
2242 case tid_DATAGRAM_INT
:
2243 ZEND_ASSERT(!"datagram");
2249 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
2251 static inline void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2253 if (ser
->multi_seq
) {
2254 array_init(return_value
);
2260 php_ion_globals_unserializer_step();
2261 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2262 php_ion_globals_unserializer_exit();
2263 ION_CATCH(zval_ptr_dtor(&tmp
));
2265 if (!ser
->multi_seq
) {
2266 RETURN_COPY_VALUE(&tmp
);
2267 } else if (ser
->type
!= tid_EOF
) {
2268 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2270 } while (ser
->type
!= tid_EOF
);
2273 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
2275 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
2278 zo_ser
= create_ion_Unserializer_PHP(NULL
);
2279 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
2281 o_ser
->unserializer
.call_magic
= true;
2282 php_ion_unserializer_php_ctor(o_ser
);
2284 ser
= &o_ser
->unserializer
;
2287 zend_object
*zo_reader
;
2288 php_ion_reader
*reader
;
2291 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2292 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2293 reader
= php_ion_obj(reader
, zo_reader
);
2294 reader
->type
= STREAM_READER
;
2295 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2296 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2297 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2298 reader
= php_ion_obj(reader
, zo_reader
);
2299 reader
->type
= BUFFER_READER
;
2300 reader
->buffer
= zval_get_string(zdata
);
2302 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
2303 "Invalid source to unserialize; expected string or resource");
2305 OBJ_RELEASE(zo_ser
);
2311 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
2312 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
2315 php_ion_reader_ctor(reader
);
2316 ser
->reader
= reader
->reader
;
2318 if (!EG(exception
)) {
2319 php_ion_unserialize_ex(ser
, return_value
);
2322 OBJ_RELEASE(zo_reader
);
2324 OBJ_RELEASE(zo_opt
);
2327 OBJ_RELEASE(zo_ser
);
2331 static inline zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
2333 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
2335 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
2337 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
2340 object_init_ex(&zsym
, ce_Symbol
);
2341 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
2342 sym
->sym
.sid
= Z_LVAL_P(zid
);
2343 sym
->value
= zval_get_string(zss
);
2344 php_ion_symbol_ctor(sym
);
2345 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);