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(c, s) { \
23 { sizeof(s)-1, (BYTE *) s }, \
27 { sizeof(c)-1, (BYTE *) c } \
30 typedef struct php_ion_global_symbol
{
33 } php_ion_global_symbol
;
35 static php_ion_global_symbol g_sym_tab_php_sym
[] = {
36 #define PHP_ION_SYMBOL_PHP (g_sym_tab_php_sym[0]).s
37 PHP_ION_SYMBOL("PHP", "PHP")
38 #define PHP_ION_SYMBOL_REFERENCE (g_sym_tab_php_sym[1]).s
39 PHP_ION_SYMBOL("Reference", "R")
40 #define PHP_ION_SYMBOL_BACKREF (g_sym_tab_php_sym[2]).s
41 PHP_ION_SYMBOL("Backref", "r")
42 #define PHP_ION_SYMBOL_PROPERTY (g_sym_tab_php_sym[3]).s
43 PHP_ION_SYMBOL("Property", "p")
44 #define PHP_ION_SYMBOL_CLASS_OBJECT (g_sym_tab_php_sym[4]).s
45 PHP_ION_SYMBOL("ClassObject", "c")
46 #define PHP_ION_SYMBOL_CUSTOM_OBJECT (g_sym_tab_php_sym[5]).s
47 PHP_ION_SYMBOL("CustomObject", "C")
48 #define PHP_ION_SYMBOL_OBJECT (g_sym_tab_php_sym[6]).s
49 PHP_ION_SYMBOL("Object", "o")
50 #define PHP_ION_SYMBOL_MAGIC_OBJECT (g_sym_tab_php_sym[7]).s
51 PHP_ION_SYMBOL("MagicObject", "O")
52 #define PHP_ION_SYMBOL_SERIALIZEABLE (g_sym_tab_php_sym[8]).s
53 PHP_ION_SYMBOL("Serializable", "S")
54 #define PHP_ION_SYMBOL_ENUM (g_sym_tab_php_sym[9]).s
55 PHP_ION_SYMBOL("Enum", "E")
60 static ION_SYMBOL_TABLE
*g_sym_tab_php
;
62 /* [SID => STRING, STRING => SID] */
63 static HashTable g_sym_hash
;
64 /* [enum_case_name => SID] */
65 static HashTable g_sym_map
;
67 #define ION_SYS_SYMBOL_SYMBOL_TABLE \
68 ION_SYS_SYMBOL_ION_SYMBOL_TABLE
69 #define g_sym_add(enum_name, c_name) do { \
70 g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \
71 g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \
74 static inline void g_sym_hash_add(int sid
, const char *str
, size_t len
)
78 ZVAL_STR(&zs
, zend_string_init_interned(str
, len
,1));
79 zend_hash_add(&g_sym_hash
, Z_STR(zs
), &zl
);
80 zend_hash_index_add(&g_sym_hash
, sid
, &zs
);
83 static inline void g_sym_map_add(int sid
, const char *str
, size_t len
)
87 zend_hash_str_add(&g_sym_map
, str
, len
, &zv
);
90 static inline int g_sym_init(void)
92 zend_hash_init(&g_sym_hash
, 0, NULL
, NULL
, 1);
93 zend_hash_init(&g_sym_map
, 0, NULL
, NULL
, 1);
95 g_sym_hash_add(0, ZEND_STRL(""));
96 g_sym_map_add(0, ZEND_STRL(""));
97 g_sym_add("Ion", ION
);
98 g_sym_add("Ivm_1_0", IVM
);
99 g_sym_add("IonSymbolTable", SYMBOL_TABLE
);
100 g_sym_add("Name", NAME
);
101 g_sym_add("Version", VERSION
);
102 g_sym_add("Imports", IMPORTS
);
103 g_sym_add("Symbols", SYMBOLS
);
104 g_sym_add("MaxId", MAX_ID
);
105 g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE
);
107 int sys_max_id
= ION_SYS_SID_SHARED_SYMBOL_TABLE
;
109 if (IERR_OK
!= ion_symbol_table_open_with_type(&g_sym_tab_php
, NULL
, ist_SHARED
)) {
112 php_ion_global_symbol
*ptr
= g_sym_tab_php_sym
;
113 ion_symbol_table_set_version(g_sym_tab_php
, PHP_ION_SYMBOL_TABLE_VERSION
);
114 ion_symbol_table_set_name(g_sym_tab_php
, &ptr
->s
.value
);
115 while (ptr
->e
.value
) {
116 ion_symbol_table_add_symbol(g_sym_tab_php
, &ptr
->s
.value
, &ptr
->s
.sid
);
117 g_sym_hash_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->s
.value
.value
, ptr
->s
.value
.length
);
118 g_sym_map_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->e
.value
, ptr
->e
.length
);
121 ion_symbol_table_lock(g_sym_tab_php
);
125 typedef struct php_ion_serializer
{
127 ION_WRITER_OPTIONS
*options
;
130 zend_string
*call_custom
;
131 zend_bool call_magic
;
137 } php_ion_serializer
;
139 typedef struct php_ion_annotaions
{
140 uint8_t shared_symtab
:1;
143 uint8_t object_prop
:1;
145 zend_string
*object_class
;
146 zend_string
*property_class
;
147 } php_ion_annotations
;
149 typedef struct php_ion_unserializer
{
151 ION_READER_OPTIONS
*options
;
154 zend_string
*call_custom
;
155 zend_bool call_magic
;
163 php_ion_annotations annotations
;
164 } php_ion_unserializer
;
166 ZEND_BEGIN_MODULE_GLOBALS(ion
)
170 ION_DECIMAL zend_max
;
171 ION_DECIMAL zend_min
;
178 php_ion_serializer serializer
;
179 php_ion_unserializer unserializer
;
182 HashTable serializer
[2];
183 HashTable unserializer
[3];
186 ZEND_END_MODULE_GLOBALS(ion
);
189 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
191 # define php_ion_globals ion_globals
194 ZEND_DECLARE_MODULE_GLOBALS(ion
);
196 static zend_class_entry
200 *ce_Decimal_Context_Rounding
,
207 *ce_Reader_Buffer_Reader
,
208 *ce_Reader_Stream_Reader
,
212 *ce_Symbol_ImportLocation
,
215 *ce_Symbol_Table_Local
,
216 *ce_Symbol_Table_PHP
,
217 *ce_Symbol_Table_Shared
,
218 *ce_Symbol_Table_System
,
220 *ce_Timestamp_Precision
,
223 *ce_Unserializer_PHP
,
227 *ce_Writer_Buffer_Writer
,
229 *ce_Writer_Stream_Writer
,
233 static inline void php_ion_globals_symbols_init(void)
235 zend_hash_init(&php_ion_globals
.symbol
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
238 static inline void php_ion_globals_symbols_dtor(void)
240 zend_hash_destroy(&php_ion_globals
.symbol
.cache
);
243 static inline void php_ion_globals_serializer_init(void)
245 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
246 HashTable
*h
= php_ion_globals
._ht
.serializer
;
248 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
249 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
252 static inline uint32_t php_ion_globals_serializer_step(void)
254 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
257 if (!(level
= s
->level
++)) {
258 zend_hash_clean(s
->ids
);
259 zend_hash_clean(s
->tmp
);
264 static inline uint32_t php_ion_globals_serializer_exit(void)
266 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
268 ZEND_ASSERT(s
->level
);
270 zend_hash_clean(s
->ids
);
271 zend_hash_clean(s
->tmp
);
276 static inline void php_ion_globals_serializer_dtor(void)
278 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
280 zend_hash_destroy(s
->tmp
);
281 zend_hash_destroy(s
->ids
);
284 void ZVAL_ADDREF(zval
*zv
)
287 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
292 static inline void php_ion_globals_unserializer_init(void)
294 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
295 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
297 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
298 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
299 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
302 static inline void php_ion_globals_unserializer_step(void)
304 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
307 zend_hash_clean(s
->addref
);
308 zend_hash_clean(s
->ids
);
309 zend_hash_clean(s
->tmp
);
313 static inline void php_ion_globals_unserializer_exit(void)
315 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
317 ZEND_ASSERT(s
->level
);
319 zend_hash_clean(s
->addref
);
320 zend_hash_clean(s
->ids
);
321 zend_hash_clean(s
->tmp
);
325 static inline void php_ion_globals_unserializer_dtor(void)
327 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
329 zend_hash_destroy(s
->addref
);
330 zend_hash_destroy(s
->ids
);
331 zend_hash_destroy(s
->tmp
);
334 #define php_ion_decl(type, cname, ...) \
335 static zend_object_handlers oh_ ## cname; \
336 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
338 if (!ce) ce = ce_ ## cname; \
339 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
340 zend_object_std_init(&o->std, ce); \
341 object_properties_init(&o->std, ce); \
342 o->std.handlers = &oh_ ## cname; \
345 static inline void free_ion_ ## cname(zend_object *std) \
347 php_ion_ ## type *obj = php_ion_obj(type, std); \
349 zend_object_std_dtor(std); \
352 #define php_ion_register(type, cname, ...) do { \
353 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
354 ce_ ## cname ->create_object = create_ion_ ## cname; \
355 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
356 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
357 oh_ ## cname .free_obj = free_ion_ ## cname; \
360 #define php_ion_obj(type, obj) \
361 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
363 #define ION_CHECK_RETURN(r, err, ...) do { \
366 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
372 #define ION_CHECK(err, ...) \
373 ION_CHECK_RETURN(, err, __VA_ARGS__)
375 #define ION_CATCH(...) do { \
376 if (EG(exception)) { \
382 #define PTR_CHECK(ptr, ...) do { \
384 zend_throw_error(NULL, "Uninitialized object"); \
390 #define OBJ_CHECK(obj, ...) do { \
391 PTR_CHECK(obj, __VA_ARGS__); \
392 PTR_CHECK(*((void **)obj), __VA_ARGS__); \
395 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
397 is
->length
= zs
? zs
->len
: 0;
398 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
402 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
404 return zend_string_init((const char *) s
->value
, s
->length
, 0);
407 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
411 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
414 typedef struct php_ion_type
{
419 php_ion_decl(type
, Type
);
421 #define RETURN_IONTYPE(typ) do { \
422 zend_object *__zo = php_ion_type_fetch(typ); \
423 if (UNEXPECTED(!__zo)) { \
426 RETURN_OBJ_COPY(__zo); \
429 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
431 zend_long index
= ION_TYPE_INT(typ
);
432 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
434 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
435 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
438 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
441 typedef struct php_ion_symbol_iloc
{
442 ION_SYMBOL_IMPORT_LOCATION loc
;
445 } php_ion_symbol_iloc
;
447 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
449 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
450 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
451 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
454 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
456 zend_string_release(obj
->name
);
459 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
461 typedef struct php_ion_symbol
{
464 zend_object
*iloc
, std
;
467 static inline int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
468 zend_string
*zs1
= zval_get_string(zv1
);
469 zend_string
*zs2
= zval_get_string(zv2
);
476 if (zs1
->len
> zs2
->len
) {
478 } else if (zs2
->len
> zs1
->len
) {
481 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
483 zend_string_release(zs1
);
484 zend_string_release(zs2
);
488 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
490 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
493 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
495 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
497 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
499 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
500 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
502 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
506 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
509 zend_string_release(obj
->value
);
513 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
515 object_init_ex(return_value
, ce_Symbol
);
516 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
518 sym
->sym
.sid
= sym_ptr
->sid
;
519 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
520 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
522 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
523 sym
->iloc
= Z_OBJ(ziloc
);
525 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
526 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
527 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
529 php_ion_symbol_iloc_ctor(iloc
);
532 php_ion_symbol_ctor(sym
);
534 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
535 GC_DELREF(sym
->iloc
);
539 static inline zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
541 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
543 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
545 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
548 object_init_ex(&zsym
, ce_Symbol
);
549 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
550 sym
->sym
.sid
= Z_LVAL_P(zid
);
551 sym
->value
= zval_get_string(zss
);
552 php_ion_symbol_ctor(sym
);
553 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);
560 php_ion_decl(symbol
, Symbol
, php_ion_symbol_dtor(obj
));
562 typedef struct php_ion_symbol_table
{
563 ION_SYMBOL_TABLE
*tab
;
564 int (*dtor
)(ION_SYMBOL_TABLE
*);
566 } php_ion_symbol_table
;
568 static inline void php_ion_symbol_table_ctor(php_ion_symbol_table
*obj
)
573 if (IERR_OK
== ion_symbol_table_get_name(obj
->tab
, &is
)) {
574 zend_update_property_stringl(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), (char *) is
.value
, is
.length
);
577 if (IERR_OK
== ion_symbol_table_get_version(obj
->tab
, &iv
)) {
578 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("version"), iv
);
582 static inline void php_ion_symbol_table_dtor(php_ion_symbol_table
*obj
)
592 static inline void php_ion_symbol_table_import(php_ion_symbol_table
*obj
, php_ion_symbol_table
*import
)
598 zval
*zimports
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), 0, &tmp
);
600 zend_ulong idx
= (uintptr_t) &import
->std
.gc
;
601 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports
), idx
)) {
602 ION_CHECK(ion_symbol_table_import_symbol_table(obj
->tab
, import
->tab
));
604 SEPARATE_ARRAY(zimports
);
605 GC_ADDREF(&import
->std
);
606 add_index_object(zimports
, idx
, &import
->std
);
607 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), zimports
);
612 static inline void php_ion_symbol_table_symbol_zval(php_ion_symbol_table
*obj
, ION_SYMBOL
*sym
, zval
*return_value
)
615 zval
*zsyms
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbols"), 0, &tmp
);
617 zval
*zsym
= zend_hash_index_find(Z_ARRVAL_P(zsyms
), sym
->sid
);
623 php_ion_symbol_zval(sym
, return_value
);
626 SEPARATE_ARRAY(zsyms
);
627 ZVAL_ADDREF(return_value
);
628 add_index_zval(zsyms
, sym
->sid
, return_value
);
632 php_ion_decl(symbol_table
, Symbol_Table
, php_ion_symbol_table_dtor(obj
));
634 typedef struct php_ion_decimal_ctx
{
637 } php_ion_decimal_ctx
;
639 #define php_ion_decimal_ctx_init_max(c, rounding) \
640 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
641 static inline void php_ion_decimal_ctx_init(decContext
*ctx
,
642 zend_long digits
, zend_long emax
, zend_long emin
, zend_long round
, zend_bool clamp
)
644 memset(ctx
, 0, sizeof(*ctx
));
645 ctx
->digits
= digits
;
652 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
654 if (!obj
->ctx
.digits
) {
655 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
658 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
660 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
662 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
663 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
664 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
665 zend_update_property_bool(obj
->std
.ce
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
668 php_ion_decl(decimal_ctx
, Decimal_Context
);
670 typedef struct php_ion_decimal
{
672 zend_object
*ctx
, std
;
675 static inline void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
677 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
678 ION_CHECK(ion_decimal_from_int32(dec
, num
));
679 } else if (num
> 0 && num
<= UINT32_MAX
) {
680 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
683 ION_CHECK(ion_int_alloc(NULL
, &iint
));
684 ION_CHECK(ion_int_from_long(iint
, num
),
686 /* WATCH OUT: BS API */
687 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
688 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
694 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
696 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
697 (void) ion_decimal_to_string(dec
, zstr
->val
);
698 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
701 static inline void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
704 ION_CHECK(ion_int_alloc(NULL
, &ii
));
705 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
707 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
712 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
716 if (!ion_decimal_is_integer(&obj
->dec
)) {
721 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_max
, &php_ion_globals
.decimal
.ctx
, &result
);
726 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_min
, &php_ion_globals
.decimal
.ctx
, &result
);
733 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
737 object_init_ex(&zdc
, ce_Decimal_Context
);
738 obj
->ctx
= Z_OBJ(zdc
);
739 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
742 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
744 if (php_ion_decimal_fits_zend_long(obj
)) {
746 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
747 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
749 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
750 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
751 zend_string_release(zstr
);
755 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
757 ion_decimal_free(&obj
->dec
);
760 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
762 typedef php_date_obj php_ion_timestamp
;
764 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
767 ctx
= &php_ion_globals
.decimal
.ctx
;
769 decQuad microsecs
, result
;
770 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
771 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
774 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
777 ctx
= &php_ion_globals
.decimal
.ctx
;
779 decQuad microsecs
, us
;
780 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
783 static inline zend_string
*php_dt_format_from_precision(int precision
)
785 switch (precision
& 0x7f) {
787 return zend_string_init(ZEND_STRL("c"), 0);
789 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
791 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
793 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
795 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
797 return zend_string_init(ZEND_STRL("Y\\T"), 0);
799 return zend_string_init(ZEND_STRL("c"), 0);
803 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
805 timelib_time
*time
= timelib_time_ctor();
807 int precision
= ION_TS_FRAC
;
808 ion_timestamp_get_precision(ts
, &precision
);
811 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
814 time
->s
= ts
->seconds
;
817 time
->i
= ts
->minutes
;
830 time
->z
= ts
->tz_offset
* 60;
832 time
->zone_type
= TIMELIB_ZONETYPE_OFFSET
;
834 time
->zone_type
= TIMELIB_ZONETYPE_ID
;
835 time
->tz_info
= get_timezone_info();
840 *fmt
= php_dt_format_from_precision(precision
);
845 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
847 memset(buf
, 0, sizeof(*buf
));
850 int precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
852 if (!precision
|| precision
> ION_TS_FRAC
) {
853 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
854 "Invalid precision (%d) of ion\\Timestamp", precision
);
855 } else switch ((buf
->precision
= precision
)) {
857 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
860 buf
->seconds
= ts
->time
->s
;
863 buf
->minutes
= ts
->time
->i
;
866 buf
->hours
= ts
->time
->h
;
867 buf
->day
= ts
->time
->d
;
870 buf
->month
= ts
->time
->m
;
873 buf
->year
= ts
->time
->y
;
876 buf
->tz_offset
= ts
->time
->z
/ 60;
877 if (buf
->tz_offset
) {
878 buf
->precision
|= 0x80;
885 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
888 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
890 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
892 fmt
= php_dt_format_from_precision(precision
);
893 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
894 zend_string_release(fmt
);
897 typedef struct php_ion_catalog
{
902 static inline void php_ion_catalog_ctor(php_ion_catalog
*obj
)
904 ION_CHECK(ion_catalog_open(&obj
->cat
));
907 static inline void php_ion_catalog_dtor(php_ion_catalog
*obj
)
910 ion_catalog_close(obj
->cat
);
914 static inline zend_string
*ion_symbol_table_to_key(ION_SYMBOL_TABLE
*tab
)
918 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_name(tab
, &is
));
919 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_version(tab
, &version
));
922 smart_str_appendl(&s
, (char *) is
.value
, is
.length
);
923 smart_str_appendc(&s
, ':');
924 smart_str_append_long(&s
, version
);
930 static inline void php_ion_catalog_add_symbol_table(php_ion_catalog
*obj
, php_ion_symbol_table
*tab
)
936 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
938 zend_ulong idx
= (uintptr_t) &tab
->std
.gc
;
939 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs
), idx
)) {
940 zend_string
*key
= ion_symbol_table_to_key(tab
->tab
);
942 ION_CHECK(ion_catalog_add_symbol_table(obj
->cat
, tab
->tab
),
943 zend_string_release(key
));
944 SEPARATE_ARRAY(ztabs
);
945 GC_ADDREF(&tab
->std
);
946 add_index_object(ztabs
, idx
, &tab
->std
);
947 GC_ADDREF(&tab
->std
);
948 add_assoc_object_ex(ztabs
, key
->val
, key
->len
, &tab
->std
);
949 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), ztabs
);
950 zend_string_release(key
);
956 static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog
*obj
, ION_SYMBOL_TABLE
*tab
, zval
*return_value
)
958 zend_string
*key
= ion_symbol_table_to_key(tab
);
962 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
964 zval
*ztab
= zend_hash_find(Z_ARRVAL_P(ztabs
), key
);
966 zend_string_release(key
);
971 php_error_docref(NULL
, E_NOTICE
, "Previously unknown ion\\Symbol\\Table encountered: %s", key
->val
);
973 object_init_ex(return_value
, ce_Symbol_Table_Shared
);
974 php_ion_symbol_table
*o_tab
= php_ion_obj(symbol_table
, Z_OBJ_P(return_value
));
975 OBJ_CHECK(o_tab
, zend_string_release(key
));
976 php_ion_symbol_table_ctor(o_tab
);
979 SEPARATE_ARRAY(ztabs
);
980 ZVAL_ADDREF(return_value
);
981 add_index_zval(ztabs
, (uintptr_t) &o_tab
->std
.gc
, return_value
);
982 ZVAL_ADDREF(return_value
);
983 add_assoc_zval_ex(ztabs
, key
->val
, key
->len
, return_value
);
985 zend_string_release(key
);
988 php_ion_decl(catalog
, Catalog
, php_ion_catalog_dtor(obj
));
990 typedef struct php_ion_reader_options
{
991 ION_READER_OPTIONS opt
;
992 zend_object
*cat
, *dec_ctx
, *cb
, std
;
993 } php_ion_reader_options
;
995 php_ion_decl(reader_options
, Reader_Options
);
997 typedef struct php_ion_reader
{
1005 zend_string
*buffer
;
1011 zend_object
*opt
, std
;
1014 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
1016 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
1017 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
1019 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
1020 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
1021 user
->limit
-= remaining
;
1024 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
1027 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
1028 if (EXPECTED(read
> 0)) {
1029 user
->limit
+= read
;
1033 if (EXPECTED(read
== 0)) {
1037 return IERR_READ_ERROR
;
1040 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1043 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
1046 fprintf(stderr
, "%s\n", __FUNCTION__
);
1050 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
1055 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
1058 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1061 opt
->opt
.context_change_notifier
.context
= obj
;
1063 if (obj
->type
== STREAM_READER
) {
1064 PTR_CHECK(obj
->stream
.ptr
);
1065 GC_ADDREF(obj
->stream
.ptr
->res
);
1067 obj
->stream
.buf
.length
= opt
&& opt
->opt
.allocation_page_size
? opt
->opt
.allocation_page_size
: 0x10000;
1068 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1069 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1072 err
= ion_reader_open_buffer(&obj
->reader
,
1073 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
1074 opt
? &opt
->opt
: NULL
);
1077 opt
->opt
.context_change_notifier
.context
= NULL
;
1083 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
1086 ion_reader_close(obj
->reader
);
1088 if (obj
->type
== STREAM_READER
) {
1089 if (obj
->stream
.buf
.value
) {
1090 efree(obj
->stream
.buf
.value
);
1092 if (obj
->stream
.ptr
) {
1093 zend_list_delete(obj
->stream
.ptr
->res
);
1097 zend_string_release(obj
->buffer
);
1102 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
1104 typedef struct php_ion_writer_options
{
1105 ION_WRITER_OPTIONS opt
;
1106 zend_object
*cat
, *dec_ctx
, std
;
1107 } php_ion_writer_options
;
1109 php_ion_decl(writer_options
, Writer_Options
);
1111 typedef struct php_ion_writer
{
1127 zend_object
*opt
, std
;
1131 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1133 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1135 if (EXPECTED(user
->limit
&& user
->curr
)) {
1136 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1137 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1138 return IERR_WRITE_ERROR
;
1141 user
->curr
= writer
->stream
.buf
.value
;
1142 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1146 #define REF_STR() do { \
1147 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
1148 GC_ADDREF(obj->buffer.str.s); \
1151 #define NEW_REF_STR() do {\
1152 if (Z_STR_P(ref) != obj->buffer.str.s) { \
1153 zval_ptr_dtor(ref); \
1158 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1160 PTR_CHECK(obj
->stream
.ptr
);
1161 GC_ADDREF(obj
->stream
.ptr
->res
);
1163 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
1164 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1166 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1168 zval
*ref
= &obj
->buffer
.val
;
1171 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
1172 smart_str_0(&obj
->buffer
.str
);
1176 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1178 zval
*ref
= &obj
->buffer
.val
;
1181 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
1186 // we've been separated
1187 GC_ADDREF(obj
->buffer
.str
.s
);
1190 // we have to separate
1191 fprintf(stderr
, "SEPARATE\n");
1192 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
1196 zend_string
*old
= obj
->buffer
.str
.s
;
1198 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1199 if (old
== obj
->buffer
.str
.s
) {
1201 } else if(old
== Z_STR_P(ref
)) {
1208 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1210 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1213 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1214 smart_str_0(&writer
->buffer
.str
);
1215 if (user
->limit
== user
->curr
) {
1216 php_ion_writer_buffer_grow(writer
);
1219 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1220 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
1225 static inline ION_COLLECTION
*php_ion_catalog_collection(php_ion_catalog
*cat
)
1227 /* do not look too close */
1230 ION_SYMBOL_TABLE
*sys
;
1231 ION_COLLECTION collection
;
1232 } *cat_ptr
= (void *) cat
->cat
;
1233 return &cat_ptr
->collection
;
1236 static inline void php_ion_writer_options_init_shared_imports(php_ion_writer_options
*opt
)
1238 php_ion_catalog
*cat
= php_ion_obj(catalog
, opt
->cat
);
1241 ION_CHECK(ion_writer_options_initialize_shared_imports(&opt
->opt
));
1243 ION_COLLECTION
*col
= php_ion_catalog_collection(cat
);
1244 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1245 // holy, nah, forget it batman...
1246 #ifndef IPCN_pNODE_TO_pDATA
1247 # define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0]))
1249 ION_COLLECTION_CURSOR cur
;
1250 ION_COLLECTION_OPEN(col
, cur
);
1252 ION_SYMBOL_TABLE
**ptr
;
1253 ION_COLLECTION_NEXT(cur
, ptr
);
1255 ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt
->opt
, ptr
, 1));
1263 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
1265 php_ion_writer_options
*opt
= NULL
;
1268 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1269 opt
= php_ion_obj(writer_options
, obj
->opt
);
1271 php_ion_writer_options_init_shared_imports(opt
);
1275 ION_STREAM_HANDLER h
;
1276 if (obj
->type
== STREAM_WRITER
) {
1277 h
= php_ion_writer_stream_handler
;
1278 php_ion_writer_stream_init(obj
, opt
);
1280 h
= php_ion_writer_buffer_handler
;
1281 php_ion_writer_buffer_init(obj
);
1285 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1289 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
1292 ion_writer_close(obj
->writer
);
1295 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1297 ion_writer_options_close_shared_imports(&opt
->opt
);
1300 if (obj
->type
== STREAM_WRITER
) {
1301 if (obj
->stream
.buf
.value
) {
1302 efree(obj
->stream
.buf
.value
);
1304 if (obj
->stream
.ptr
) {
1305 zend_list_delete(obj
->stream
.ptr
->res
);
1308 if (obj
->buffer
.str
.s
) {
1309 smart_str_0(&obj
->buffer
.str
);
1310 zend_string_release(obj
->buffer
.str
.s
);
1312 zval_ptr_dtor(&obj
->buffer
.val
);
1316 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
1318 typedef struct php_ion_serializer_php
{
1319 php_ion_serializer serializer
;
1320 zend_object
*opt
, std
;
1321 } php_ion_serializer_php
;
1323 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
1325 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1326 ser_obj
->serializer
.ids
= global_ser
->ids
;
1327 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
1329 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1330 ser_obj
->serializer
.call_magic
);
1331 if (ser_obj
->serializer
.call_custom
) {
1332 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1333 ser_obj
->serializer
.call_custom
);
1334 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
1336 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1338 if (!ser_obj
->opt
) {
1339 ser_obj
->opt
= create_ion_Writer_Options(NULL
);
1341 GC_ADDREF(ser_obj
->opt
);
1343 ser_obj
->serializer
.options
= &php_ion_obj(writer_options
, ser_obj
->opt
)->opt
;
1344 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
1345 GC_DELREF(ser_obj
->opt
);
1348 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
1350 if (obj
->serializer
.call_custom
) {
1351 zend_string_release(obj
->serializer
.call_custom
);
1355 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1357 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool unmangle_props
, bool annotate_props
)
1359 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
1363 zend_string
*k
= NULL
;
1364 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1365 char buf
[MAX_LENGTH_OF_LONG
+ 1];
1369 const char *class_name
, *prop_name
;
1370 if (unmangle_props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1371 if (annotate_props
) {
1372 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_PROPERTY
));
1373 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1379 ion_string_assign_cstr(&is
, (char *) prop_name
, prop_len
);
1381 char *end
= buf
+ sizeof(buf
) - 1;
1382 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1383 ion_string_assign_cstr(&is
, ptr
, end
- ptr
);
1386 // WATCH OUT: field names need to be copied
1388 ION_CHECK(ion_string_copy_to_owner(ser
->writer
, &fn
, &is
));
1389 ION_CHECK(ion_writer_write_field_name(ser
->writer
, &fn
));
1391 php_ion_serialize_zval(ser
, v
);
1393 ZEND_HASH_FOREACH_END();
1395 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1398 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1400 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1403 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1404 php_ion_serialize_zval(ser
, v
);
1406 ZEND_HASH_FOREACH_END();
1408 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1411 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1413 if (zend_array_is_list(arr
)) {
1414 php_ion_serialize_list(ser
, arr
);
1416 php_ion_serialize_struct(ser
, arr
, false, false);
1420 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1426 ZVAL_OBJ(&tmp
, zobject
);
1427 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1429 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_SERIALIZEABLE
));
1430 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1431 ION_CHECK(ion_writer_write_clob(ser
->writer
, buf
, len
));
1433 } else if (!EG(exception
)){
1434 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1435 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1439 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1444 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1447 if (IS_ARRAY
== Z_TYPE(rv
)) {
1449 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, fn
? &PHP_ION_SYMBOL_CUSTOM_OBJECT
: &PHP_ION_SYMBOL_MAGIC_OBJECT
));
1450 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1451 php_ion_serialize_zval(ser
, &rv
);
1454 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1455 "%s serializer %s::%s did not return an array",
1456 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1457 fn
? fn
->common
.function_name
->val
: "__serialize");
1461 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1464 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_ENUM
));
1466 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1467 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1468 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1471 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1475 if (zobject
->ce
!= zend_standard_class_def
) {
1476 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_CLASS_OBJECT
));
1477 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1479 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_OBJECT
));
1483 ZVAL_OBJ(&zobj
, zobject
);
1484 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1486 php_ion_serialize_struct(ser
, props
, true, true);
1487 zend_release_properties(props
);
1489 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1490 "Could not get properties for serialization of class %s",
1491 zobject
->ce
->name
->val
);
1495 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1497 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1498 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1499 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1501 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1504 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1507 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1508 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1513 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1515 return ce
->__serialize
&& ser
->call_magic
;
1518 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1520 return !!ce
->serialize
;
1523 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1525 if (ser
->call_custom
) {
1526 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1531 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1534 zend_class_entry
*ce
= zobject
->ce
;
1537 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1538 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1539 "Serializing %s is not allowed", ce
->name
->val
);
1543 if (can_call_magic_serialize(ser
, ce
)) {
1544 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1545 } else if (can_call_iface_serialize(ser
, ce
)) {
1546 php_ion_serialize_object_iface(ser
, zobject
);
1547 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1548 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1549 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1550 php_ion_serialize_object_enum(ser
, zobject
);
1551 } else if (ce
== ce_Symbol
) {
1552 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1553 } else if (ce
== ce_Decimal
) {
1554 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1555 } else if (ce
== ce_Timestamp
) {
1557 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1558 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1559 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1560 } else if (ce
== ce_LOB
) {
1561 php_ion_serialize_object_lob(ser
, zobject
);
1563 php_ion_serialize_object_std(ser
, zobject
);
1567 static inline bool php_ion_serialize_system_value(php_ion_serializer
*ser
, zval
*zv
)
1569 if (1 == php_ion_globals
.serializer
.level
) {
1570 if (Z_TYPE_P(zv
) == IS_OBJECT
) {
1571 if (Z_OBJCE_P(zv
) == ce_Symbol_Table_Shared
) {
1572 php_ion_symbol_table
*obj
= php_ion_obj(symbol_table
, Z_OBJ_P(zv
));
1573 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj
->tab
, ser
->writer
));
1581 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1583 if (php_ion_serialize_system_value(ser
, zv
)) {
1587 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1590 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1591 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1593 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_BACKREF
));
1594 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1598 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1599 zend_hash_index_add(ser
->ids
, idx
, &num
);
1602 zend_hash_next_index_insert(ser
->tmp
, zv
);
1604 switch (Z_TYPE_P(zv
)) {
1606 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1610 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1614 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1618 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_REFERENCE
));
1619 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1625 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1629 switch (Z_TYPE_P(zv
)) {
1631 ION_CHECK(ion_writer_write_null(ser
->writer
));
1634 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1637 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1640 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1643 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1649 php_ion_serialize_refcounted(ser
, zv
);
1652 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1653 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1657 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1659 static inline void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1662 HashTable
*arr
= NULL
;
1664 if (ser
->multi_seq
) {
1665 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1666 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1667 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1671 arr
= Z_ARRVAL_P(zv
);
1673 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1674 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1678 php_ion_globals_serializer_step();
1679 php_ion_serialize_zval(ser
, zv
);
1680 php_ion_globals_serializer_exit();
1682 if (!ser
->multi_seq
) {
1685 zend_hash_move_forward_ex(arr
, &pos
);
1686 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1690 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1692 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1695 zo_ser
= create_ion_Serializer_PHP(NULL
);
1696 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1698 o_ser
->serializer
.call_magic
= true;
1699 php_ion_serializer_php_ctor(o_ser
);
1701 ser
= &o_ser
->serializer
;
1704 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1705 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1706 writer
->type
= BUFFER_WRITER
;
1709 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1710 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1713 php_ion_writer_ctor(writer
);
1714 ser
->writer
= writer
->writer
;
1715 ser
->buffer
= &writer
->buffer
.str
;
1717 if (!EG(exception
)) {
1718 php_ion_serialize_ex(ser
, zv
);
1721 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1722 ion_writer_flush(ser
->writer
, NULL
);
1723 RETVAL_STR_COPY(ser
->buffer
->s
);
1725 OBJ_RELEASE(zo_writer
);
1727 OBJ_RELEASE(zo_opt
);
1730 OBJ_RELEASE(zo_ser
);
1734 typedef struct php_ion_unserializer_php
{
1735 php_ion_unserializer unserializer
;
1736 zend_object
*opt
, std
;
1737 } php_ion_unserializer_php
;
1739 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1741 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1742 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1743 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1744 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1746 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1747 ser_obj
->unserializer
.call_magic
);
1748 if (ser_obj
->unserializer
.call_custom
) {
1749 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1750 ser_obj
->unserializer
.call_custom
);
1751 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1753 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1755 if (!ser_obj
->opt
) {
1756 ser_obj
->opt
= create_ion_Reader_Options(NULL
);
1758 GC_ADDREF(ser_obj
->opt
);
1760 ser_obj
->unserializer
.options
= &php_ion_obj(reader_options
, ser_obj
->opt
)->opt
;
1761 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1762 GC_DELREF(ser_obj
->opt
);
1765 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1767 if (obj
->unserializer
.call_custom
) {
1768 zend_string_release(obj
->unserializer
.call_custom
);
1772 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1774 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1776 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1779 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1781 return (ce
&& ce
->unserialize
);
1784 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1786 if (ser
->call_custom
) {
1787 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1792 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1794 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1797 object_init_ex(return_value
, ce
);
1798 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1801 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1802 "Could not find class %s", ser
->annotations
.object_class
->val
);
1806 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1808 zend_string
*zs_case
= zval_get_string(return_value
);
1811 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1812 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1813 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1814 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1817 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1818 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1819 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1822 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1823 zend_hash_next_index_insert(ser
->ids
, return_value
);
1824 zend_string_release(zs_case
);
1827 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1829 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1830 if (can_call_iface_unserialize(ser
, ce
)) {
1831 zend_string
*s
= zval_get_string(return_value
);
1832 ZVAL_NULL(return_value
);
1833 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1834 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1835 RETVAL_ZVAL(backref
, 0, 0);
1836 } else if (!EG(exception
)) {
1837 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1838 "Failed to unserialize class %s", ce
->name
->val
);
1840 zend_string_release(s
);
1842 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1843 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1847 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1849 // FIXME: symbol table
1851 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1854 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1855 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1856 name
= is_ptr
->value
;
1857 } else if (is_ptr
) {
1858 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1859 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1861 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1865 *key
= zend_string_from_ion(&name
);
1868 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1870 zend_hash_next_index_insert(ser
->ids
, return_value
);
1872 ION_CHECK(ion_reader_step_in(ser
->reader
));
1876 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1878 if (typ
== tid_EOF
) {
1883 php_ion_unserialize_field_name(ser
, &key
);
1887 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1888 ION_CATCH(zend_string_release(key
));
1890 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1891 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1892 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1894 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1895 zval_ptr_dtor(&zvalue
);
1896 zend_string_release(key
);
1899 ION_CHECK(ion_reader_step_out(ser
->reader
));
1903 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
1904 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
1905 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
1906 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
1907 * nondeterministic behavior.
1909 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1911 zend_hash_next_index_insert(ser
->ids
, return_value
);
1913 ION_CHECK(ion_reader_step_in(ser
->reader
));
1917 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1919 if (typ
== tid_EOF
) {
1924 php_ion_unserialize_field_name(ser
, &key
);
1928 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1929 ION_CATCH(zend_string_release(key
));
1931 // FIXME:: too naive; b0rked if the previous value is an array
1932 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
1933 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
1934 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
1936 Z_TRY_ADDREF_P(prev
);
1937 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
1938 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
1940 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
1942 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1944 zend_string_release(key
);
1947 ION_CHECK(ion_reader_step_out(ser
->reader
));
1950 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1952 switch (ser
->annotations
.object_type
) {
1958 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1959 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1960 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1965 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1966 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1967 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1969 *fn
= zobject
->ce
->__unserialize
;
1973 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1974 "Invalid object type %c", ser
->annotations
.object_type
);
1978 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1980 // backup possible backref to array returned by magic/custom __serialize()
1981 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1983 php_ion_unserialize_class(ser
, return_value
);
1986 zend_function
*fn
= NULL
;
1987 zend_object
*zobject
= Z_OBJ_P(return_value
);
1988 verify_unserializer(ser
, zobject
, &fn
);
1993 php_ion_unserialize_props(ser
, return_value
);
1998 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1999 zval_ptr_dtor(input
);
2001 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
2002 php_ion_unserialize_hash(ser
, input
);
2007 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
2011 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
2013 if (ser
->annotations
.object_class
) {
2014 switch (ser
->annotations
.object_type
) {
2016 php_ion_unserialize_object_iface(ser
, return_value
);
2019 php_ion_unserialize_object_enum(ser
, return_value
);
2022 php_ion_unserialize_object(ser
, return_value
);
2024 } else if (!ser
->annotations
.object_type
) {
2025 bool is_shared_symtab
= ser
->annotations
.shared_symtab
;
2026 array_init(return_value
);
2027 php_ion_unserialize_hash(ser
, return_value
);
2028 if (is_shared_symtab
) {
2030 object_init_ex(&zobj
, ce_Symbol_Table_Shared
);
2031 zend_call_known_function(Z_OBJCE(zobj
)->constructor
, Z_OBJ(zobj
), Z_OBJCE(zobj
), NULL
, 0, NULL
, Z_ARRVAL_P(return_value
));
2032 zval_ptr_dtor(return_value
);
2033 RETURN_COPY_VALUE(&zobj
);
2035 } else if (ser
->annotations
.object_type
== 'o') {
2036 object_init(return_value
);
2037 php_ion_unserialize_hash(ser
, return_value
);
2039 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2040 "Invalid object annotation %c::", ser
->annotations
.object_type
);
2044 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
2046 ION_CHECK(ion_reader_step_in(ser
->reader
));
2047 array_init(return_value
);
2048 zend_hash_next_index_insert(ser
->ids
, return_value
);
2052 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
2054 if (typ
== tid_EOF
) {
2059 php_ion_unserialize_zval(ser
, &next
, &typ
);
2062 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
2065 ION_CHECK(ion_reader_step_out(ser
->reader
));
2068 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
2070 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
2073 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
2074 if (err
== IERR_BUFFER_TOO_SMALL
) {
2075 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
2078 ION_CHECK(err
, zend_string_release(zstr
));
2079 if (zstr
->len
> read
) {
2080 zstr
->val
[read
] = 0;
2081 zstr
= zend_string_truncate(zstr
, read
, 0);
2086 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
2089 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
2091 object_init_ex(return_value
, ce_Timestamp
);
2092 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
2094 zend_string
*fmt
= NULL
;
2095 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
2096 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
2097 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
2098 zend_string_release(fmt
);
2103 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
2105 ION_INT
*num
= NULL
;
2106 ION_CHECK(ion_int_alloc(reader
, &num
));
2107 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
2109 // TODO: SIZEOF_ZEND_LONG == 4
2111 iERR err
= ion_int_to_int64(num
, &i64
);
2117 case IERR_NUMERIC_OVERFLOW
:
2119 ION_CHECK(ion_int_char_length(num
, &max
));
2120 zend_string
*zs
= zend_string_alloc(max
, 0);
2122 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2123 zs
->val
[zs
->len
= len
] = 0;
2134 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2136 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2139 ZVAL_COPY_VALUE(return_value
, backref
);
2140 zend_hash_next_index_insert(ser
->addref
, return_value
);
2142 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
2143 "Could not find back reference %ld", Z_LVAL_P(return_value
));
2147 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2149 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2152 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
2153 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2155 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
2157 if (ann_str
.length
!= 1) {
2158 if (ION_STRING_EQUALS(&ann_str
, &ION_SYMBOL_SHARED_SYMBOL_TABLE_STRING
)) {
2159 ser
->annotations
.shared_symtab
= true;
2164 switch (*ann_str
.value
) {
2166 if (ser
->annotations
.makeref
) {
2167 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2168 "Invalid multiple reference annotations");
2171 ser
->annotations
.makeref
= true;
2175 if (ser
->annotations
.backref
) {
2176 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2177 "Invalid multiple back reference annotations");
2180 ser
->annotations
.backref
= true;
2184 if (ser
->annotations
.object_prop
) {
2185 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2186 "Invalid multiple object property annotations");
2189 ser
->annotations
.object_prop
= true;
2191 ION_STRING prop_class
;
2192 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
2193 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
2196 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
2197 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
2206 if (ser
->annotations
.object_type
) {
2207 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2208 "Invalid multiple object type annotations: %c::%c",
2209 ser
->annotations
.object_type
, *ann_str
.value
);
2212 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
2213 ION_STRING class_name
;
2214 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
2215 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
2218 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
2219 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
2225 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2226 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2227 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2230 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2231 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2232 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2238 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2241 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
2244 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
2247 php_ion_unserialize_annotations(ser
);
2250 if (ser
->annotations
.makeref
) {
2251 ZVAL_MAKE_REF(return_value
);
2252 zend_hash_next_index_insert(ser
->ids
, return_value
);
2253 ZVAL_DEREF(return_value
);
2256 if (ION_TYPE_INT(*typ
) > 0) {
2258 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
2264 switch (ION_TYPE_INT(*typ
)) {
2266 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
2271 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
2275 php_ion_reader_read_int(ser
->reader
, return_value
);
2276 if (ser
->annotations
.backref
) {
2278 php_ion_unserialize_backref(ser
, return_value
);
2280 if (ser
->annotations
.object_type
) {
2281 if (!ser
->annotations
.backref
) {
2282 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2283 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2284 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2287 goto unserialize_struct
;
2293 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
2296 case tid_DECIMAL_INT
:
2297 object_init_ex(return_value
, ce_Decimal
);
2298 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2299 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
2300 php_ion_decimal_ctor(dec
);
2301 zend_hash_next_index_insert(ser
->ids
, return_value
);
2304 case tid_TIMESTAMP_INT
:
2305 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
2306 zend_hash_next_index_insert(ser
->ids
, return_value
);
2309 case tid_SYMBOL_INT
:
2311 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
2312 php_ion_symbol_zval(&sym
, return_value
);
2313 if (ser
->annotations
.object_type
) {
2314 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2315 goto unserialize_struct
;
2317 zend_hash_next_index_insert(ser
->ids
, return_value
);
2320 case tid_STRING_INT
:
2322 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
2323 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2324 if (ser
->annotations
.object_type
) {
2325 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2326 goto unserialize_struct
;
2328 zend_hash_next_index_insert(ser
->ids
, return_value
);
2333 php_ion_reader_read_lob(ser
->reader
, return_value
);
2334 if (ser
->annotations
.object_type
) {
2335 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2336 goto unserialize_struct
;
2338 zend_hash_next_index_insert(ser
->ids
, return_value
);
2342 case tid_SEXP_INT
: // FIXME
2343 php_ion_unserialize_list(ser
, return_value
);
2344 if (!ser
->annotations
.object_type
) {
2349 case tid_STRUCT_INT
:
2350 unserialize_struct
: ;
2351 php_ion_unserialize_struct(ser
, return_value
);
2358 case tid_DATAGRAM_INT
:
2359 ZEND_ASSERT(!"datagram");
2365 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
2367 static inline void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2369 if (ser
->multi_seq
) {
2370 array_init(return_value
);
2376 php_ion_globals_unserializer_step();
2377 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2378 php_ion_globals_unserializer_exit();
2379 ION_CATCH(zval_ptr_dtor(&tmp
));
2381 if (!ser
->multi_seq
) {
2382 RETURN_COPY_VALUE(&tmp
);
2383 } else if (ser
->type
!= tid_EOF
) {
2384 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2386 } while (ser
->type
!= tid_EOF
);
2389 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
2391 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
2394 zo_ser
= create_ion_Unserializer_PHP(NULL
);
2395 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
2397 o_ser
->unserializer
.call_magic
= true;
2398 php_ion_unserializer_php_ctor(o_ser
);
2400 ser
= &o_ser
->unserializer
;
2403 zend_object
*zo_reader
;
2404 php_ion_reader
*reader
;
2407 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2408 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2409 reader
= php_ion_obj(reader
, zo_reader
);
2410 reader
->type
= STREAM_READER
;
2411 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2412 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2413 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2414 reader
= php_ion_obj(reader
, zo_reader
);
2415 reader
->type
= BUFFER_READER
;
2416 reader
->buffer
= zval_get_string(zdata
);
2418 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
2419 "Invalid source to unserialize; expected string or resource");
2421 OBJ_RELEASE(zo_ser
);
2427 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
2428 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
2431 php_ion_reader_ctor(reader
);
2432 ser
->reader
= reader
->reader
;
2434 if (!EG(exception
)) {
2435 php_ion_unserialize_ex(ser
, return_value
);
2438 OBJ_RELEASE(zo_reader
);
2440 OBJ_RELEASE(zo_opt
);
2443 OBJ_RELEASE(zo_ser
);