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 +--------------------------------------------------------------------+
16 #include "ext/date/php_date.h"
18 typedef struct php_ion_serializer
{
20 ION_WRITER_OPTIONS
*options
;
23 zend_string
*call_custom
;
32 typedef struct php_ion_annotaions
{
35 uint8_t object_prop
:1;
37 zend_string
*object_class
;
38 zend_string
*property_class
;
39 } php_ion_annotations
;
41 typedef struct php_ion_unserializer
{
43 ION_READER_OPTIONS
*options
;
46 zend_string
*call_custom
;
55 php_ion_annotations annotations
;
56 } php_ion_unserializer
;
58 ZEND_BEGIN_MODULE_GLOBALS(ion
)
60 php_ion_serializer serializer
;
61 php_ion_unserializer unserializer
;
64 HashTable serializer
[2];
65 HashTable unserializer
[3];
68 ZEND_END_MODULE_GLOBALS(ion
);
71 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
73 # define php_ion_globals ion_globals
76 ZEND_DECLARE_MODULE_GLOBALS(ion
);
78 static inline void php_ion_globals_serializer_init(void)
80 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
81 HashTable
*h
= php_ion_globals
._ht
.serializer
;
83 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
84 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
87 static inline uint32_t php_ion_globals_serializer_step(void)
89 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
92 if (!(level
= s
->level
++)) {
93 zend_hash_clean(s
->ids
);
94 zend_hash_clean(s
->tmp
);
99 static inline uint32_t php_ion_globals_serializer_exit(void)
101 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
103 ZEND_ASSERT(s
->level
);
105 zend_hash_clean(s
->ids
);
106 zend_hash_clean(s
->tmp
);
111 static inline void php_ion_globals_serializer_dtor(void)
113 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
115 zend_hash_destroy(s
->tmp
);
116 zend_hash_destroy(s
->ids
);
119 void ZVAL_ADDREF(zval
*zv
)
122 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
127 static inline void php_ion_globals_unserializer_init(void)
129 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
130 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
132 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
133 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
134 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
137 static inline void php_ion_globals_unserializer_step(void)
139 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
142 zend_hash_clean(s
->addref
);
143 zend_hash_clean(s
->ids
);
144 zend_hash_clean(s
->tmp
);
148 static inline void php_ion_globals_unserializer_exit(void)
150 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
152 ZEND_ASSERT(s
->level
);
154 zend_hash_clean(s
->addref
);
155 zend_hash_clean(s
->ids
);
156 zend_hash_clean(s
->tmp
);
160 static inline void php_ion_globals_unserializer_dtor(void)
162 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
164 zend_hash_destroy(s
->addref
);
165 zend_hash_destroy(s
->ids
);
166 zend_hash_destroy(s
->tmp
);
169 static zend_class_entry
181 *ce_Reader_Buffer_Reader
,
182 *ce_Reader_Stream_Reader
,
186 *ce_Symbol_ImportLocation
,
188 *ce_Symbol_System_SID
,
191 *ce_Timestamp_Precision
,
194 *ce_Unserializer_PHP
,
198 *ce_Writer_Buffer_Writer
,
200 *ce_Writer_Stream_Writer
,
204 #define php_ion_decl(type, cname, ...) \
205 static zend_object_handlers oh_ ## cname; \
206 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
208 if (!ce) ce = ce_ ## cname; \
209 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
210 zend_object_std_init(&o->std, ce); \
211 object_properties_init(&o->std, ce); \
212 o->std.handlers = &oh_ ## cname; \
215 static inline void free_ion_ ## cname(zend_object *std) \
217 php_ion_ ## type *obj = php_ion_obj(type, std); \
219 zend_object_std_dtor(std); \
222 #define php_ion_register(type, cname, ...) do { \
223 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
224 ce_ ## cname ->create_object = create_ion_ ## cname; \
225 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
226 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
227 oh_ ## cname .free_obj = free_ion_ ## cname; \
230 #define php_ion_obj(type, obj) \
231 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
233 #define ION_CHECK(err, ...) do { \
236 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
242 #define ION_CATCH(...) do { \
243 if (EG(exception)) { \
249 #define PTR_CHECK(ptr, ...) do { \
251 zend_throw_error(NULL, "Uninitialized object"); \
257 #define OBJ_CHECK(obj) do { \
259 PTR_CHECK(*((void **)obj)); \
262 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
264 is
->length
= zs
? zs
->len
: 0;
265 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
269 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
271 return zend_string_init((const char *) s
->value
, s
->length
, 0);
274 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
278 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
281 typedef struct php_ion_type
{
286 php_ion_decl(type
, Type
);
288 #define RETURN_IONTYPE(typ) do { \
289 zend_object *__zo = php_ion_type_fetch(typ); \
290 if (UNEXPECTED(!__zo)) { \
293 RETURN_OBJ_COPY(__zo); \
296 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
298 zend_long index
= ION_TYPE_INT(typ
);
299 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
301 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
302 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
305 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
308 typedef struct php_ion_symbol_iloc
{
309 ION_SYMBOL_IMPORT_LOCATION loc
;
312 } php_ion_symbol_iloc
;
314 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
316 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
317 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
318 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
321 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
323 zend_string_release(obj
->name
);
326 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
328 typedef struct php_ion_symbol
{
331 zend_object
*iloc
, std
;
334 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
336 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
339 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
341 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
343 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
345 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
346 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
348 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
352 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
355 zend_string_release(obj
->value
);
359 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
361 object_init_ex(return_value
, ce_Symbol
);
362 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
364 sym
->sym
.sid
= sym_ptr
->sid
;
365 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
366 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
368 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
369 sym
->iloc
= Z_OBJ(ziloc
);
371 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
372 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
373 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
375 php_ion_symbol_iloc_ctor(iloc
);
378 php_ion_symbol_ctor(sym
);
380 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
381 GC_DELREF(sym
->iloc
);
385 php_ion_decl(symbol
, Symbol
, php_ion_symbol_dtor(obj
));
387 typedef struct php_ion_symbol_table
{
389 } php_ion_symbol_table
;
391 php_ion_decl(symbol_table
, Symbol_Table
);
393 typedef struct php_ion_decimal_ctx
{
396 } php_ion_decimal_ctx
;
398 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
) {
399 zval tmp
, *zbits
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), 1, &tmp
);
402 if (zbits
!= &EG(uninitialized_zval
)) {
403 bits
= Z_LVAL_P(zbits
);
405 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), bits
);
411 decContextDefault(&obj
->ctx
, bits
);
414 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
415 "Decimal context only allows 32, 64 or 128 bits");
419 php_ion_decl(decimal_ctx
, Decimal_Context
);
421 typedef struct php_ion_decimal
{
423 zend_object
*ctx
, std
;
426 static inline void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
428 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
429 ION_CHECK(ion_decimal_from_int32(dec
, num
));
430 } else if (num
> 0 && num
<= UINT32_MAX
) {
431 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
434 ION_CHECK(ion_int_alloc(NULL
, &iint
));
435 ION_CHECK(ion_int_from_long(iint
, num
),
437 /* WATCH OUT: BS API */
438 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
439 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
445 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
447 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
448 (void) ion_decimal_to_string(dec
, zstr
->val
);
449 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
452 static inline void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
455 ION_CHECK(ion_int_alloc(NULL
, &ii
));
456 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
458 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
463 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
467 if (!ion_decimal_is_integer(&obj
->dec
)) {
472 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_max
, &g_dec_ctx
, &result
);
477 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_min
, &g_dec_ctx
, &result
);
484 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
488 object_init_ex(&zdc
, ce_Decimal_Context
);
489 obj
->ctx
= Z_OBJ(zdc
);
490 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
));
493 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
495 if (php_ion_decimal_fits_zend_long(obj
)) {
497 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
498 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
500 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
501 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
502 zend_string_release(zstr
);
506 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
508 ion_decimal_free(&obj
->dec
);
511 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
513 typedef php_date_obj php_ion_timestamp
;
515 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
517 decQuad microsecs
, result
;
518 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
519 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
522 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
524 decQuad microsecs
, us
;
525 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
528 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
532 return zend_string_init(ZEND_STRL("c"), 0);
534 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
536 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
538 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
540 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
542 return zend_string_init(ZEND_STRL("Y\\T"), 0);
544 return zend_string_init(ZEND_STRL("c"), 0);
548 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
550 timelib_time
*time
= timelib_time_ctor();
552 switch (ts
->precision
) {
554 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
557 time
->s
= ts
->seconds
;
560 time
->i
= ts
->minutes
;
573 time
->z
= ts
->tz_offset
* 60;
577 *fmt
= php_dt_format_from_precision(ts
->precision
);
582 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
584 memset(buf
, 0, sizeof(*buf
));
587 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
589 if (!precision
|| precision
> ION_TS_FRAC
) {
590 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
591 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
592 } else switch ((buf
->precision
= precision
)) {
594 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
597 buf
->seconds
= ts
->time
->s
;
600 buf
->minutes
= ts
->time
->i
;
603 buf
->hours
= ts
->time
->h
;
604 buf
->day
= ts
->time
->d
;
607 buf
->month
= ts
->time
->m
;
610 buf
->year
= ts
->time
->y
;
613 buf
->tz_offset
= ts
->time
->z
/ 60;
619 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
622 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
624 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
626 fmt
= php_dt_format_from_precision(precision
);
627 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
628 zend_string_release(fmt
);
631 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
634 timelib_time_dtor(obj
->time
);
638 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
640 typedef struct php_ion_catalog
{
645 php_ion_decl(catalog
, Catalog
);
647 typedef struct php_ion_reader_options
{
648 ION_READER_OPTIONS opt
;
649 zend_object
*cat
, *dec_ctx
, *cb
, std
;
650 } php_ion_reader_options
;
652 php_ion_decl(reader_options
, Reader_Options
);
654 typedef struct php_ion_reader
{
668 zend_object
*opt
, std
;
671 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
673 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
674 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
676 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
677 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
678 user
->limit
-= remaining
;
681 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
684 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
685 if (EXPECTED(read
> 0)) {
690 if (EXPECTED(read
== 0)) {
694 return IERR_READ_ERROR
;
697 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
700 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
706 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
711 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
714 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
717 opt
->opt
.context_change_notifier
.context
= obj
;
719 if (obj
->type
== STREAM_READER
) {
720 PTR_CHECK(obj
->stream
.ptr
);
721 GC_ADDREF(obj
->stream
.ptr
->res
);
723 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
724 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
725 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
726 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
729 err
= ion_reader_open_buffer(&obj
->reader
,
730 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
731 opt
? &opt
->opt
: NULL
);
734 opt
->opt
.context_change_notifier
.context
= NULL
;
740 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
743 ion_reader_close(obj
->reader
);
745 if (obj
->type
== STREAM_READER
) {
746 if (obj
->stream
.buf
.value
) {
747 efree(obj
->stream
.buf
.value
);
749 if (obj
->stream
.ptr
) {
750 zend_list_delete(obj
->stream
.ptr
->res
);
754 zend_string_release(obj
->buffer
);
759 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
761 typedef struct php_ion_writer_options
{
762 ION_WRITER_OPTIONS opt
;
763 zend_object
*cat
, *dec_ctx
, *col
, std
;
764 } php_ion_writer_options
;
766 php_ion_decl(writer_options
, Writer_Options
);
768 typedef struct php_ion_writer
{
784 zend_object
*opt
, std
;
788 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
790 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
792 if (EXPECTED(user
->limit
&& user
->curr
)) {
793 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
794 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
795 return IERR_WRITE_ERROR
;
798 user
->curr
= writer
->stream
.buf
.value
;
799 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
803 #define REF_STR() do { \
804 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
805 GC_ADDREF(obj->buffer.str.s); \
808 #define NEW_REF_STR() do {\
809 if (Z_STR_P(ref) != obj->buffer.str.s) { \
810 zval_ptr_dtor(ref); \
815 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
817 PTR_CHECK(obj
->stream
.ptr
);
818 GC_ADDREF(obj
->stream
.ptr
->res
);
820 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
821 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
823 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
825 zval
*ref
= &obj
->buffer
.val
;
828 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
829 smart_str_0(&obj
->buffer
.str
);
833 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
835 zval
*ref
= &obj
->buffer
.val
;
838 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
843 // we've been separated
844 GC_ADDREF(obj
->buffer
.str
.s
);
847 // we have to separate
848 fprintf(stderr
, "SEPARATE\n");
849 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
853 zend_string
*old
= obj
->buffer
.str
.s
;
855 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
856 if (old
== obj
->buffer
.str
.s
) {
858 } else if(old
== Z_STR_P(ref
)) {
865 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
867 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
870 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
871 smart_str_0(&writer
->buffer
.str
);
872 if (user
->limit
== user
->curr
) {
873 php_ion_writer_buffer_grow(writer
);
876 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
877 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
882 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
885 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
888 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
889 ION_STREAM_HANDLER h
;
890 if (obj
->type
== STREAM_WRITER
) {
891 h
= php_ion_writer_stream_handler
;
892 php_ion_writer_stream_init(obj
, opt
);
894 h
= php_ion_writer_buffer_handler
;
895 php_ion_writer_buffer_init(obj
);
898 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
902 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
905 ion_writer_close(obj
->writer
);
907 if (obj
->type
== STREAM_WRITER
) {
908 if (obj
->stream
.buf
.value
) {
909 efree(obj
->stream
.buf
.value
);
911 if (obj
->stream
.ptr
) {
912 zend_list_delete(obj
->stream
.ptr
->res
);
915 if (obj
->buffer
.str
.s
) {
916 smart_str_0(&obj
->buffer
.str
);
917 zend_string_release(obj
->buffer
.str
.s
);
919 zval_ptr_dtor(&obj
->buffer
.val
);
923 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
925 typedef struct php_ion_serializer_php
{
926 php_ion_serializer serializer
;
927 zend_object
*opt
, std
;
928 } php_ion_serializer_php
;
930 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
932 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
933 ser_obj
->serializer
.ids
= global_ser
->ids
;
934 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
936 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
937 ser_obj
->serializer
.call_magic
);
938 if (ser_obj
->serializer
.call_custom
) {
939 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
940 ser_obj
->serializer
.call_custom
);
941 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
943 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
946 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
947 ser_obj
->serializer
.options
= &o_woptions
->opt
;
948 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
950 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
954 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
956 if (obj
->serializer
.call_custom
) {
957 zend_string_release(obj
->serializer
.call_custom
);
961 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
963 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
965 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
969 zend_string
*k
= NULL
;
970 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
974 const char *class_name
, *prop_name
;
975 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
976 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("p"))));
977 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
982 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, (char *) prop_name
, prop_len
)));
984 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
985 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
986 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, ptr
, end
- ptr
)));
989 php_ion_serialize_zval(ser
, v
);
991 ZEND_HASH_FOREACH_END();
993 ION_CHECK(ion_writer_finish_container(ser
->writer
));
996 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
998 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1001 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1002 php_ion_serialize_zval(ser
, v
);
1004 ZEND_HASH_FOREACH_END();
1006 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1009 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1011 if (zend_array_is_list(arr
)) {
1012 php_ion_serialize_list(ser
, arr
);
1014 php_ion_serialize_struct(ser
, arr
, false);
1018 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1024 ZVAL_OBJ(&tmp
, zobject
);
1025 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1027 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("S"))));
1028 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1029 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_assign_cstr(&is
, (char *) buf
, len
)));
1031 } else if (!EG(exception
)){
1032 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1033 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1037 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1042 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1045 if (IS_ARRAY
== Z_TYPE(rv
)) {
1047 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, fn
? "C" : "O", 1)));
1048 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1049 php_ion_serialize_zval(ser
, &rv
);
1052 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1053 "%s serializer %s::%s did not return an array",
1054 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1055 fn
? fn
->common
.function_name
->val
: "__serialize");
1059 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1062 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("E"))));
1064 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1065 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1066 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1069 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1073 if (zobject
->ce
!= zend_standard_class_def
) {
1074 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("c"))));
1075 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1077 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("o"))));
1081 ZVAL_OBJ(&zobj
, zobject
);
1082 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1084 php_ion_serialize_struct(ser
, props
, true);
1085 zend_release_properties(props
);
1087 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1088 "Could not get properties for serialization of class %s",
1089 zobject
->ce
->name
->val
);
1093 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1095 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1096 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1097 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1099 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1102 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1105 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1106 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1111 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1113 return ce
->__serialize
&& ser
->call_magic
;
1116 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1118 return !!ce
->serialize
;
1121 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1123 if (ser
->call_custom
) {
1124 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1129 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1132 zend_class_entry
*ce
= zobject
->ce
;
1135 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1136 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1137 "Serializing %s is not allowed", ce
->name
->val
);
1141 if (can_call_magic_serialize(ser
, ce
)) {
1142 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1143 } else if (can_call_iface_serialize(ser
, ce
)) {
1144 php_ion_serialize_object_iface(ser
, zobject
);
1145 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1146 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1147 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1148 php_ion_serialize_object_enum(ser
, zobject
);
1149 } else if (ce
== ce_Symbol
) {
1150 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1151 } else if (ce
== ce_Decimal
) {
1152 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1153 } else if (ce
== ce_Timestamp
) {
1155 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1156 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1157 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1158 } else if (ce
== ce_LOB
) {
1159 php_ion_serialize_object_lob(ser
, zobject
);
1161 php_ion_serialize_object_std(ser
, zobject
);
1165 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1167 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1170 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1171 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1173 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("r"))));
1174 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1178 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1179 zend_hash_index_add(ser
->ids
, idx
, &num
);
1182 zend_hash_next_index_insert(ser
->tmp
, zv
);
1184 switch (Z_TYPE_P(zv
)) {
1186 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1190 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1194 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1198 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("R"))));
1199 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1205 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1209 switch (Z_TYPE_P(zv
)) {
1211 ION_CHECK(ion_writer_write_null(ser
->writer
));
1214 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1217 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1220 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1223 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1229 php_ion_serialize_refcounted(ser
, zv
);
1232 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1233 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1237 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1239 static inline void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1242 HashTable
*arr
= NULL
;
1244 if (ser
->multi_seq
) {
1245 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1246 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1247 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1251 arr
= Z_ARRVAL_P(zv
);
1253 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1254 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1258 /* start off with a global PHP annotation instead of repeating it all over the place */
1259 if (0 == php_ion_globals_serializer_step()) {
1261 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("PHP"))));
1263 php_ion_serialize_zval(ser
, zv
);
1264 php_ion_globals_serializer_exit();
1266 if (!ser
->multi_seq
) {
1269 zend_hash_move_forward_ex(arr
, &pos
);
1270 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1274 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1276 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1279 zo_ser
= create_ion_Serializer_PHP(NULL
);
1280 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1282 o_ser
->serializer
.call_magic
= true;
1283 php_ion_serializer_php_ctor(o_ser
);
1285 ser
= &o_ser
->serializer
;
1288 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1289 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1290 writer
->type
= BUFFER_WRITER
;
1293 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1294 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1297 php_ion_writer_ctor(writer
);
1298 ser
->writer
= writer
->writer
;
1299 ser
->buffer
= &writer
->buffer
.str
;
1301 if (!EG(exception
)) {
1302 php_ion_serialize_ex(ser
, zv
);
1305 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1306 ion_writer_flush(ser
->writer
, NULL
);
1307 RETVAL_STR_COPY(ser
->buffer
->s
);
1309 OBJ_RELEASE(zo_writer
);
1311 OBJ_RELEASE(zo_opt
);
1314 OBJ_RELEASE(zo_ser
);
1318 typedef struct php_ion_unserializer_php
{
1319 php_ion_unserializer unserializer
;
1320 zend_object
*opt
, std
;
1321 } php_ion_unserializer_php
;
1323 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1325 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1326 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1327 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1328 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1330 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1331 ser_obj
->unserializer
.call_magic
);
1332 if (ser_obj
->unserializer
.call_custom
) {
1333 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1334 ser_obj
->unserializer
.call_custom
);
1335 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1337 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1340 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1341 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1342 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1344 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1348 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1350 if (obj
->unserializer
.call_custom
) {
1351 zend_string_release(obj
->unserializer
.call_custom
);
1355 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1357 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1359 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1362 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1364 return (ce
&& ce
->unserialize
);
1367 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1369 if (ser
->call_custom
) {
1370 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1375 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1377 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1380 object_init_ex(return_value
, ce
);
1381 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1384 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1385 "Could not find class %s", ser
->annotations
.object_class
->val
);
1389 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1391 zend_string
*zs_case
= zval_get_string(return_value
);
1394 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1395 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1396 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1397 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1400 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1401 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1402 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1405 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1406 zend_hash_next_index_insert(ser
->ids
, return_value
);
1407 zend_string_release(zs_case
);
1410 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1412 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1413 if (can_call_iface_unserialize(ser
, ce
)) {
1414 zend_string
*s
= zval_get_string(return_value
);
1415 ZVAL_NULL(return_value
);
1416 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1417 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1418 RETVAL_ZVAL(backref
, 0, 0);
1419 } else if (!EG(exception
)) {
1420 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1421 "Failed to unserialize class %s", ce
->name
->val
);
1423 zend_string_release(s
);
1425 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1426 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1430 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1432 // FIXME: symbol table
1434 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1437 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1438 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1439 name
= is_ptr
->value
;
1440 } else if (is_ptr
) {
1441 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1442 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1444 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1448 *key
= zend_string_from_ion(&name
);
1451 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1453 zend_hash_next_index_insert(ser
->ids
, return_value
);
1455 ION_CHECK(ion_reader_step_in(ser
->reader
));
1459 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1461 if (typ
== tid_EOF
) {
1466 php_ion_unserialize_field_name(ser
, &key
);
1470 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1471 ION_CATCH(zend_string_release(key
));
1473 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1474 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1475 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1477 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1478 zval_ptr_dtor(&zvalue
);
1479 zend_string_release(key
);
1482 ION_CHECK(ion_reader_step_out(ser
->reader
));
1485 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1487 zend_hash_next_index_insert(ser
->ids
, return_value
);
1489 ION_CHECK(ion_reader_step_in(ser
->reader
));
1493 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1495 if (typ
== tid_EOF
) {
1500 php_ion_unserialize_field_name(ser
, &key
);
1504 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1505 ION_CATCH(zend_string_release(key
));
1507 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1509 zend_string_release(key
);
1512 ION_CHECK(ion_reader_step_out(ser
->reader
));
1515 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1517 switch (ser
->annotations
.object_type
) {
1523 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1524 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1525 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1530 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1531 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1532 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1534 *fn
= zobject
->ce
->__unserialize
;
1538 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1539 "Invalid object type %c", ser
->annotations
.object_type
);
1543 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1545 // backup possible backref to array returned by magic/custom __serialize()
1546 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1548 php_ion_unserialize_class(ser
, return_value
);
1551 zend_function
*fn
= NULL
;
1552 zend_object
*zobject
= Z_OBJ_P(return_value
);
1553 verify_unserializer(ser
, zobject
, &fn
);
1558 php_ion_unserialize_props(ser
, return_value
);
1563 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1564 zval_ptr_dtor(input
);
1566 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1567 php_ion_unserialize_hash(ser
, input
);
1572 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1576 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1578 if (ser
->annotations
.object_class
) {
1579 switch (ser
->annotations
.object_type
) {
1581 php_ion_unserialize_object_iface(ser
, return_value
);
1584 php_ion_unserialize_object_enum(ser
, return_value
);
1587 php_ion_unserialize_object(ser
, return_value
);
1589 } else if (!ser
->annotations
.object_type
) {
1590 array_init(return_value
);
1591 php_ion_unserialize_hash(ser
, return_value
);
1592 } else if (ser
->annotations
.object_type
== 'o') {
1593 object_init(return_value
);
1594 php_ion_unserialize_hash(ser
, return_value
);
1596 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1597 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1601 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1603 ION_CHECK(ion_reader_step_in(ser
->reader
));
1604 array_init(return_value
);
1605 zend_hash_next_index_insert(ser
->ids
, return_value
);
1609 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1611 if (typ
== tid_EOF
) {
1616 php_ion_unserialize_zval(ser
, &next
, &typ
);
1619 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1622 ION_CHECK(ion_reader_step_out(ser
->reader
));
1625 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1627 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1630 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1631 if (err
== IERR_BUFFER_TOO_SMALL
) {
1632 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1635 ION_CHECK(err
, zend_string_release(zstr
));
1636 if (zstr
->len
> read
) {
1637 zstr
->val
[read
] = 0;
1638 zstr
= zend_string_truncate(zstr
, read
, 0);
1643 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1646 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1648 object_init_ex(return_value
, ce_Timestamp
);
1649 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1651 zend_string
*fmt
= NULL
;
1652 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1653 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1654 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1655 zend_string_release(fmt
);
1660 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1662 ION_INT
*num
= NULL
;
1663 ION_CHECK(ion_int_alloc(reader
, &num
));
1664 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1666 // TODO: SIZEOF_ZEND_LONG == 4
1668 iERR err
= ion_int_to_int64(num
, &i64
);
1674 case IERR_NUMERIC_OVERFLOW
:
1676 ION_CHECK(ion_int_char_length(num
, &max
));
1677 zend_string
*zs
= zend_string_alloc(max
, 0);
1679 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1680 zs
->val
[zs
->len
= len
] = 0;
1691 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1693 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1696 ZVAL_COPY_VALUE(return_value
, backref
);
1697 zend_hash_next_index_insert(ser
->addref
, return_value
);
1699 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1700 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1704 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1706 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1709 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1710 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1712 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1714 if (ann_str
.length
!= 1) {
1718 switch (*ann_str
.value
) {
1720 if (ser
->annotations
.makeref
) {
1721 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1722 "Invalid multiple reference annotations");
1725 ser
->annotations
.makeref
= true;
1729 if (ser
->annotations
.backref
) {
1730 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1731 "Invalid multiple back reference annotations");
1734 ser
->annotations
.backref
= true;
1738 if (ser
->annotations
.object_prop
) {
1739 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1740 "Invalid multiple object property annotations");
1743 ser
->annotations
.object_prop
= true;
1745 ION_STRING prop_class
;
1746 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1747 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1750 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1751 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1760 if (ser
->annotations
.object_type
) {
1761 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1762 "Invalid multiple object type annotations: %c::%c",
1763 ser
->annotations
.object_type
, *ann_str
.value
);
1766 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1767 ION_STRING class_name
;
1768 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1769 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1772 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1773 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1779 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1780 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1781 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1784 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1785 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1786 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1792 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1795 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1798 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1801 php_ion_unserialize_annotations(ser
);
1804 if (ser
->annotations
.makeref
) {
1805 ZVAL_MAKE_REF(return_value
);
1806 zend_hash_next_index_insert(ser
->ids
, return_value
);
1807 ZVAL_DEREF(return_value
);
1810 if (ION_TYPE_INT(*typ
) > 0) {
1812 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
1818 switch (ION_TYPE_INT(*typ
)) {
1820 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1825 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1829 php_ion_reader_read_int(ser
->reader
, return_value
);
1830 if (ser
->annotations
.backref
) {
1832 php_ion_unserialize_backref(ser
, return_value
);
1834 if (ser
->annotations
.object_type
) {
1835 if (!ser
->annotations
.backref
) {
1836 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1837 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1838 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1841 goto unserialize_struct
;
1847 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1850 case tid_DECIMAL_INT
:
1851 object_init_ex(return_value
, ce_Decimal
);
1852 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1853 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1854 php_ion_decimal_ctor(dec
);
1855 zend_hash_next_index_insert(ser
->ids
, return_value
);
1858 case tid_TIMESTAMP_INT
:
1859 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1860 zend_hash_next_index_insert(ser
->ids
, return_value
);
1863 case tid_SYMBOL_INT
:
1865 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1866 php_ion_symbol_zval(&sym
, return_value
);
1867 if (ser
->annotations
.object_type
) {
1868 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1869 goto unserialize_struct
;
1871 zend_hash_next_index_insert(ser
->ids
, return_value
);
1874 case tid_STRING_INT
:
1876 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1877 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1878 if (ser
->annotations
.object_type
) {
1879 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1880 goto unserialize_struct
;
1882 zend_hash_next_index_insert(ser
->ids
, return_value
);
1887 php_ion_reader_read_lob(ser
->reader
, return_value
);
1888 if (ser
->annotations
.object_type
) {
1889 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1890 goto unserialize_struct
;
1892 zend_hash_next_index_insert(ser
->ids
, return_value
);
1896 case tid_SEXP_INT
: // FIXME
1897 php_ion_unserialize_list(ser
, return_value
);
1898 if (!ser
->annotations
.object_type
) {
1903 case tid_STRUCT_INT
:
1904 unserialize_struct
: ;
1905 php_ion_unserialize_struct(ser
, return_value
);
1912 case tid_DATAGRAM_INT
:
1913 ZEND_ASSERT(!"datagram");
1919 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1921 static inline void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
1923 if (ser
->multi_seq
) {
1924 array_init(return_value
);
1930 php_ion_globals_unserializer_step();
1931 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
1932 php_ion_globals_unserializer_exit();
1933 ION_CATCH(zval_ptr_dtor(&tmp
));
1935 if (!ser
->multi_seq
) {
1936 RETURN_COPY_VALUE(&tmp
);
1937 } else if (ser
->type
!= tid_EOF
) {
1938 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
1940 } while (ser
->type
!= tid_EOF
);
1943 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1945 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1948 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1949 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1951 o_ser
->unserializer
.call_magic
= true;
1952 php_ion_unserializer_php_ctor(o_ser
);
1954 ser
= &o_ser
->unserializer
;
1957 zend_object
*zo_reader
;
1958 php_ion_reader
*reader
;
1961 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
1962 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
1963 reader
= php_ion_obj(reader
, zo_reader
);
1964 reader
->type
= STREAM_READER
;
1965 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
1966 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
1967 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
1968 reader
= php_ion_obj(reader
, zo_reader
);
1969 reader
->type
= BUFFER_READER
;
1970 reader
->buffer
= zval_get_string(zdata
);
1972 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1973 "Invalid source to unserialize; expected string or resource");
1975 OBJ_RELEASE(zo_ser
);
1981 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
1982 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
1985 php_ion_reader_ctor(reader
);
1986 ser
->reader
= reader
->reader
;
1988 if (!EG(exception
)) {
1989 php_ion_unserialize_ex(ser
, return_value
);
1992 OBJ_RELEASE(zo_reader
);
1994 OBJ_RELEASE(zo_opt
);
1997 OBJ_RELEASE(zo_ser
);