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
;
31 typedef struct php_ion_annotaions
{
34 uint8_t object_prop
:1;
36 zend_string
*object_class
;
37 zend_string
*property_class
;
38 } php_ion_annotations
;
40 typedef struct php_ion_unserializer
{
42 ION_READER_OPTIONS
*options
;
45 zend_string
*call_custom
;
53 php_ion_annotations annotations
;
54 } php_ion_unserializer
;
56 ZEND_BEGIN_MODULE_GLOBALS(ion
)
58 php_ion_serializer serializer
;
59 php_ion_unserializer unserializer
;
62 HashTable serializer
[2];
63 HashTable unserializer
[3];
66 ZEND_END_MODULE_GLOBALS(ion
);
69 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
71 # define php_ion_globals ion_globals
74 ZEND_DECLARE_MODULE_GLOBALS(ion
);
76 static inline void php_ion_globals_serializer_init(void)
78 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
79 HashTable
*h
= php_ion_globals
._ht
.serializer
;
81 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
82 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
85 static inline uint32_t php_ion_globals_serializer_step(void)
87 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
90 if (!(level
= s
->level
++)) {
91 zend_hash_clean(s
->ids
);
92 zend_hash_clean(s
->tmp
);
97 static inline uint32_t php_ion_globals_serializer_exit(void)
99 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
101 ZEND_ASSERT(s
->level
);
103 zend_hash_clean(s
->ids
);
104 zend_hash_clean(s
->tmp
);
109 static inline void php_ion_globals_serializer_dtor(void)
111 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
113 zend_hash_destroy(s
->tmp
);
114 zend_hash_destroy(s
->ids
);
117 void ZVAL_ADDREF(zval
*zv
)
120 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
125 static inline void php_ion_globals_unserializer_init(void)
127 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
128 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
130 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
131 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
132 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
135 static inline void php_ion_globals_unserializer_step(void)
137 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
140 zend_hash_clean(s
->addref
);
141 zend_hash_clean(s
->ids
);
142 zend_hash_clean(s
->tmp
);
146 static inline void php_ion_globals_unserializer_exit(void)
148 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
150 ZEND_ASSERT(s
->level
);
152 zend_hash_clean(s
->addref
);
153 zend_hash_clean(s
->ids
);
154 zend_hash_clean(s
->tmp
);
158 static inline void php_ion_globals_unserializer_dtor(void)
160 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
162 zend_hash_destroy(s
->addref
);
163 zend_hash_destroy(s
->ids
);
164 zend_hash_destroy(s
->tmp
);
167 static zend_class_entry
178 *ce_Reader_Buffer_Reader
,
179 *ce_Reader_Stream_Reader
,
183 *ce_Symbol_ImportLocation
,
185 *ce_Symbol_System_SID
,
188 *ce_Timestamp_Precision
,
191 *ce_Unserializer_PHP
,
195 *ce_Writer_Buffer_Writer
,
197 *ce_Writer_Stream_Writer
,
201 #define php_ion_decl(type, cname, ...) \
202 static zend_object_handlers oh_ ## cname; \
203 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
205 if (!ce) ce = ce_ ## cname; \
206 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
207 zend_object_std_init(&o->std, ce); \
208 object_properties_init(&o->std, ce); \
209 o->std.handlers = &oh_ ## cname; \
212 static inline void free_ion_ ## cname(zend_object *std) \
214 php_ion_ ## type *obj = php_ion_obj(type, std); \
216 zend_object_std_dtor(std); \
219 #define php_ion_register(type, cname, ...) do { \
220 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
221 ce_ ## cname ->create_object = create_ion_ ## cname; \
222 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
223 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
224 oh_ ## cname .free_obj = free_ion_ ## cname; \
227 #define php_ion_obj(type, obj) \
228 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
230 #define ION_CHECK(err, ...) do { \
233 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
239 #define ION_CATCH(...) do { \
240 if (EG(exception)) { \
246 #define PTR_CHECK(ptr, ...) do { \
248 zend_throw_error(NULL, "Uninitialized object"); \
254 #define OBJ_CHECK(obj) do { \
256 PTR_CHECK(*((void **)obj)); \
259 static inline ION_STRING
*ion_string_from_cstr(ION_STRING
*is
, const char *s
, size_t l
)
262 is
->value
= (BYTE
*) s
;
266 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
268 is
->length
= zs
? zs
->len
: 0;
269 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
273 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
275 return zend_string_init((const char *) s
->value
, s
->length
, 0);
278 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
282 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
285 typedef struct php_ion_type
{
290 php_ion_decl(type
, Type
);
292 #define RETURN_IONTYPE(typ) do { \
293 zend_object *__zo = php_ion_type_fetch(typ); \
294 if (UNEXPECTED(!__zo)) { \
297 RETURN_OBJ_COPY(__zo); \
300 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
302 zend_long index
= ION_TYPE_INT(typ
);
303 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
305 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
306 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
309 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
312 typedef struct php_ion_symbol_iloc
{
313 ION_SYMBOL_IMPORT_LOCATION loc
;
316 } php_ion_symbol_iloc
;
318 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
320 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
321 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
322 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
325 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
327 zend_string_release(obj
->name
);
330 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
332 typedef struct php_ion_symbol
{
335 zend_object
*iloc
, std
;
338 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
340 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
343 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
345 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
347 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
349 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
350 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
352 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
356 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
359 zend_string_release(obj
->value
);
363 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
365 object_init_ex(return_value
, ce_Symbol
);
366 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
368 sym
->sym
.sid
= sym_ptr
->sid
;
369 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
370 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
372 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
373 sym
->iloc
= Z_OBJ(ziloc
);
375 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
376 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
377 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
379 php_ion_symbol_iloc_ctor(iloc
);
382 php_ion_symbol_ctor(sym
);
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 zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
428 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
429 (void) ion_decimal_to_string(dec
, zstr
->val
);
430 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
433 static inline void php_ion_decimal_to_int(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
436 ION_CHECK(ion_int_alloc(NULL
, &ii
));
437 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
439 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
444 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
448 object_init_ex(&zdc
, ce_Decimal_Context
);
449 obj
->ctx
= Z_OBJ(zdc
);
450 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
));
453 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
455 if (ion_decimal_is_integer(&obj
->dec
)) {
457 php_ion_decimal_to_int(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
458 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
460 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
461 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
462 zend_string_release(zstr
);
466 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
468 ion_decimal_free(&obj
->dec
);
471 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
473 typedef php_date_obj php_ion_timestamp
;
475 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
477 decQuad microsecs
, result
;
478 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
479 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
482 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
484 decQuad microsecs
, us
;
485 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
488 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
492 return zend_string_init(ZEND_STRL("c"), 0);
494 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
496 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
498 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
500 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
502 return zend_string_init(ZEND_STRL("Y\\T"), 0);
504 return zend_string_init(ZEND_STRL("c"), 0);
508 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
510 timelib_time
*time
= timelib_time_ctor();
512 switch (ts
->precision
) {
514 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
517 time
->s
= ts
->seconds
;
520 time
->i
= ts
->minutes
;
533 time
->z
= ts
->tz_offset
* 60;
537 *fmt
= php_dt_format_from_precision(ts
->precision
);
542 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
544 memset(buf
, 0, sizeof(*buf
));
547 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
549 if (!precision
|| precision
> ION_TS_FRAC
) {
550 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
551 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
552 } else switch ((buf
->precision
= precision
)) {
554 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
557 buf
->seconds
= ts
->time
->s
;
560 buf
->minutes
= ts
->time
->i
;
563 buf
->hours
= ts
->time
->h
;
564 buf
->day
= ts
->time
->d
;
567 buf
->month
= ts
->time
->m
;
570 buf
->year
= ts
->time
->y
;
573 buf
->tz_offset
= ts
->time
->z
/ 60;
579 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
582 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
584 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
586 fmt
= php_dt_format_from_precision(precision
);
587 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
588 zend_string_release(fmt
);
591 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
594 timelib_time_dtor(obj
->time
);
598 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
600 typedef struct php_ion_catalog
{
605 php_ion_decl(catalog
, Catalog
);
607 typedef struct php_ion_reader_options
{
608 ION_READER_OPTIONS opt
;
609 zend_object
*cat
, *dec_ctx
, *cb
, std
;
610 } php_ion_reader_options
;
612 php_ion_decl(reader_options
, Reader_Options
);
614 typedef struct php_ion_reader
{
628 zend_object
*opt
, std
;
631 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
633 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
634 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
636 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
637 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
638 user
->limit
-= remaining
;
641 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
644 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
645 if (EXPECTED(read
> 0)) {
650 if (EXPECTED(read
== 0)) {
654 return IERR_READ_ERROR
;
657 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
660 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
666 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
671 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
674 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
677 opt
->opt
.context_change_notifier
.context
= obj
;
679 if (obj
->type
== STREAM_READER
) {
680 PTR_CHECK(obj
->stream
.ptr
);
681 GC_ADDREF(obj
->stream
.ptr
->res
);
683 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
684 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
685 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
686 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
689 err
= ion_reader_open_buffer(&obj
->reader
,
690 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
691 opt
? &opt
->opt
: NULL
);
694 opt
->opt
.context_change_notifier
.context
= NULL
;
700 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
703 ion_reader_close(obj
->reader
);
705 if (obj
->type
== STREAM_READER
) {
706 if (obj
->stream
.buf
.value
) {
707 efree(obj
->stream
.buf
.value
);
709 if (obj
->stream
.ptr
) {
710 zend_list_delete(obj
->stream
.ptr
->res
);
714 zend_string_release(obj
->buffer
);
719 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
721 typedef struct php_ion_writer_options
{
722 ION_WRITER_OPTIONS opt
;
723 zend_object
*cat
, *dec_ctx
, *col
, std
;
724 } php_ion_writer_options
;
726 php_ion_decl(writer_options
, Writer_Options
);
728 typedef struct php_ion_writer
{
744 zend_object
*opt
, std
;
748 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
750 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
752 if (EXPECTED(user
->limit
&& user
->curr
)) {
753 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
754 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
755 return IERR_WRITE_ERROR
;
758 user
->curr
= writer
->stream
.buf
.value
;
759 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
763 #define REF_STR() do { \
764 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
765 GC_ADDREF(obj->buffer.str.s); \
768 #define NEW_REF_STR() do {\
769 if (Z_STR_P(ref) != obj->buffer.str.s) { \
770 zval_ptr_dtor(ref); \
775 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
777 PTR_CHECK(obj
->stream
.ptr
);
778 GC_ADDREF(obj
->stream
.ptr
->res
);
780 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
781 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
783 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
785 zval
*ref
= &obj
->buffer
.val
;
788 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
789 smart_str_0(&obj
->buffer
.str
);
793 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
795 zval
*ref
= &obj
->buffer
.val
;
798 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
803 // we've been separated
804 GC_ADDREF(obj
->buffer
.str
.s
);
807 // we have to separate
808 fprintf(stderr
, "SEPARATE\n");
809 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
813 zend_string
*old
= obj
->buffer
.str
.s
;
815 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
816 if (old
== obj
->buffer
.str
.s
) {
818 } else if(old
== Z_STR_P(ref
)) {
825 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
827 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
830 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
831 smart_str_0(&writer
->buffer
.str
);
832 if (user
->limit
== user
->curr
) {
833 php_ion_writer_buffer_grow(writer
);
836 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
837 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
842 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
845 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
848 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
849 ION_STREAM_HANDLER h
;
850 if (obj
->type
== STREAM_WRITER
) {
851 h
= php_ion_writer_stream_handler
;
852 php_ion_writer_stream_init(obj
, opt
);
854 h
= php_ion_writer_buffer_handler
;
855 php_ion_writer_buffer_init(obj
);
858 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
862 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
865 ion_writer_close(obj
->writer
);
867 if (obj
->type
== STREAM_WRITER
) {
868 if (obj
->stream
.buf
.value
) {
869 efree(obj
->stream
.buf
.value
);
871 if (obj
->stream
.ptr
) {
872 zend_list_delete(obj
->stream
.ptr
->res
);
875 if (obj
->buffer
.str
.s
) {
876 smart_str_0(&obj
->buffer
.str
);
877 zend_string_release(obj
->buffer
.str
.s
);
879 zval_ptr_dtor(&obj
->buffer
.val
);
883 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
885 typedef struct php_ion_serializer_php
{
886 php_ion_serializer serializer
;
887 zend_object
*opt
, std
;
888 } php_ion_serializer_php
;
890 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
892 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
893 ser_obj
->serializer
.ids
= global_ser
->ids
;
894 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
896 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
897 ser_obj
->serializer
.call_magic
);
898 if (ser_obj
->serializer
.call_custom
) {
899 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
900 ser_obj
->serializer
.call_custom
);
901 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
903 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
906 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
907 ser_obj
->serializer
.options
= &o_woptions
->opt
;
908 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
910 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
914 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
916 if (obj
->serializer
.call_custom
) {
917 zend_string_release(obj
->serializer
.call_custom
);
921 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
923 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
925 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
929 zend_string
*k
= NULL
;
930 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
934 const char *class_name
, *prop_name
;
935 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
936 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("p"))));
937 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, class_name
, prop_name
- class_name
- 1)));
942 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, prop_name
, prop_len
)));
944 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
945 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
946 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, ptr
, end
- ptr
)));
949 php_ion_serialize_zval(ser
, v
);
951 ZEND_HASH_FOREACH_END();
953 ION_CHECK(ion_writer_finish_container(ser
->writer
));
956 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
958 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
961 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
962 php_ion_serialize_zval(ser
, v
);
964 ZEND_HASH_FOREACH_END();
966 ION_CHECK(ion_writer_finish_container(ser
->writer
));
969 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
971 if (zend_array_is_list(arr
)) {
972 php_ion_serialize_list(ser
, arr
);
974 php_ion_serialize_struct(ser
, arr
, false);
978 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
984 ZVAL_OBJ(&tmp
, zobject
);
985 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
987 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("S"))));
988 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
989 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_cstr(&is
, (char *) buf
, len
)));
991 } else if (!EG(exception
)){
992 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
993 "Failed to serialize class %s", zobject
->ce
->name
->val
);
997 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1002 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1005 if (IS_ARRAY
== Z_TYPE(rv
)) {
1007 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, fn
? "C" : "O", 1)));
1008 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1009 php_ion_serialize_zval(ser
, &rv
);
1012 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1013 "%s serializer %s::%s did not return an array",
1014 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1015 fn
? fn
->common
.function_name
->val
: "__serialize");
1019 static inline zend_string
*fq_enum_case(zend_object
*zobject
)
1021 zval
*cn
= zend_enum_fetch_case_name(zobject
);
1022 zend_string
*en
= zend_string_alloc(zobject
->ce
->name
->len
+ Z_STRLEN_P(cn
) + strlen("::"), 0);
1023 memcpy(en
->val
, zobject
->ce
->name
->val
, zobject
->ce
->name
->len
);
1024 en
->val
[zobject
->ce
->name
->len
] = ':';
1025 en
->val
[zobject
->ce
->name
->len
+ 1] = ':';
1026 memcpy(&en
->val
[zobject
->ce
->name
->len
+ 2], Z_STRVAL_P(cn
), Z_STRLEN_P(cn
));
1027 en
->val
[en
->len
] = 0;
1031 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1034 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("E"))));
1036 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1037 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1038 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1041 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1045 if (zobject
->ce
!= zend_standard_class_def
) {
1046 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("c"))));
1047 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1049 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("o"))));
1053 ZVAL_OBJ(&zobj
, zobject
);
1054 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1056 php_ion_serialize_struct(ser
, props
, true);
1057 zend_release_properties(props
);
1059 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1060 "Could not get properties for serialization of class %s",
1061 zobject
->ce
->name
->val
);
1065 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1067 return ce
->__serialize
&& ser
->call_magic
;
1070 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1072 return !!ce
->serialize
;
1075 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1077 if (ser
->call_custom
) {
1078 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1083 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1086 zend_class_entry
*ce
= zobject
->ce
;
1089 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1090 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1091 "Serializing %s is not allowed", ce
->name
->val
);
1095 if (can_call_magic_serialize(ser
, ce
)) {
1096 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1097 } else if (can_call_iface_serialize(ser
, ce
)) {
1098 php_ion_serialize_object_iface(ser
, zobject
);
1099 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1100 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1101 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1102 php_ion_serialize_object_enum(ser
, zobject
);
1103 } else if (ce
== ce_Symbol
) {
1104 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1105 } else if (ce
== ce_Decimal
) {
1106 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1107 } else if (ce
== ce_Timestamp
) {
1109 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1110 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1111 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1113 php_ion_serialize_object_std(ser
, zobject
);
1117 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1119 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1122 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1123 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1125 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("r"))));
1126 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1130 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1131 zend_hash_index_add(ser
->ids
, idx
, &num
);
1134 zend_hash_next_index_insert(ser
->tmp
, zv
);
1136 switch (Z_TYPE_P(zv
)) {
1138 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1142 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1146 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1150 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("R"))));
1151 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1157 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1161 switch (Z_TYPE_P(zv
)) {
1163 ION_CHECK(ion_writer_write_null(ser
->writer
));
1166 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1169 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1172 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1175 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1181 php_ion_serialize_refcounted(ser
, zv
);
1184 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1185 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1189 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1191 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1193 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1196 zo_ser
= create_ion_Serializer_PHP(NULL
);
1197 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1199 o_ser
->serializer
.call_magic
= true;
1200 php_ion_serializer_php_ctor(o_ser
);
1202 ser
= &o_ser
->serializer
;
1205 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1206 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1207 writer
->type
= BUFFER_WRITER
;
1210 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1211 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1214 php_ion_writer_ctor(writer
);
1215 ser
->writer
= writer
->writer
;
1216 ser
->buffer
= &writer
->buffer
.str
;
1218 /* start off with a global PHP annotation instead of repeating it all over the place */
1219 if (0 == php_ion_globals_serializer_step()) {
1221 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("PHP"))),
1222 if (zo_ser
) OBJ_RELEASE(zo_ser
));
1224 php_ion_serialize_zval(ser
, zv
);
1225 php_ion_globals_serializer_exit();
1227 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1228 ion_writer_flush(ser
->writer
, NULL
);
1229 RETVAL_STR_COPY(ser
->buffer
->s
);
1231 OBJ_RELEASE(zo_writer
);
1233 OBJ_RELEASE(zo_opt
);
1236 OBJ_RELEASE(zo_ser
);
1240 typedef struct php_ion_unserializer_php
{
1241 php_ion_unserializer unserializer
;
1242 zend_object
*opt
, std
;
1243 } php_ion_unserializer_php
;
1245 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1247 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1248 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1249 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1250 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1252 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1253 ser_obj
->unserializer
.call_magic
);
1254 if (ser_obj
->unserializer
.call_custom
) {
1255 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1256 ser_obj
->unserializer
.call_custom
);
1257 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1259 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1262 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1263 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1264 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1266 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1270 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1272 if (obj
->unserializer
.call_custom
) {
1273 zend_string_release(obj
->unserializer
.call_custom
);
1277 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1279 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1281 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1284 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1286 return (ce
&& ce
->unserialize
);
1289 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1291 if (ser
->call_custom
) {
1292 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1297 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1299 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1302 object_init_ex(return_value
, ce
);
1303 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1306 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1307 "Could not find class %s", ser
->annotations
.object_class
->val
);
1311 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1313 zend_string
*zs_case
= zval_get_string(return_value
);
1316 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1317 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1318 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1319 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1322 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1323 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1324 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1327 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1328 zend_hash_next_index_insert(ser
->ids
, return_value
);
1329 zend_string_release(zs_case
);
1332 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1334 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1335 if (can_call_iface_unserialize(ser
, ce
)) {
1336 zend_string
*s
= zval_get_string(return_value
);
1337 ZVAL_NULL(return_value
);
1338 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1339 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1340 RETVAL_ZVAL(backref
, 0, 0);
1341 } else if (!EG(exception
)) {
1342 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1343 "Failed to unserialize class %s", ce
->name
->val
);
1345 zend_string_release(s
);
1347 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1348 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1352 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1354 zend_hash_next_index_insert(ser
->ids
, return_value
);
1356 ION_CHECK(ion_reader_step_in(ser
->reader
));
1360 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1362 if (typ
== tid_EOF
) {
1367 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &is
));
1368 zend_string
*key
= zend_string_from_ion(&is
);
1371 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1372 ION_CATCH(zend_string_release(key
));
1374 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1375 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1376 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1378 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1379 zval_ptr_dtor(&zvalue
);
1380 zend_string_release(key
);
1383 ION_CHECK(ion_reader_step_out(ser
->reader
));
1386 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1388 zend_hash_next_index_insert(ser
->ids
, return_value
);
1390 ION_CHECK(ion_reader_step_in(ser
->reader
));
1394 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1397 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1398 zend_string
*key
= zend_string_from_ion(&name
);
1401 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1402 ION_CATCH(zend_string_release(key
));
1404 if (typ
== tid_EOF
) {
1405 zend_string_release(key
);
1409 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1411 zend_string_release(key
);
1414 ION_CHECK(ion_reader_step_out(ser
->reader
));
1417 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1419 switch (ser
->annotations
.object_type
) {
1425 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1426 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1427 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1432 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1433 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1434 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1436 *fn
= zobject
->ce
->__unserialize
;
1440 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1441 "Invalid object type %c", ser
->annotations
.object_type
);
1445 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1447 // backup possible backref to array returned by magic/custom __serialize()
1448 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1450 php_ion_unserialize_class(ser
, return_value
);
1453 zend_function
*fn
= NULL
;
1454 zend_object
*zobject
= Z_OBJ_P(return_value
);
1455 verify_unserializer(ser
, zobject
, &fn
);
1460 php_ion_unserialize_props(ser
, return_value
);
1465 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1466 zval_ptr_dtor(input
);
1468 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1469 php_ion_unserialize_hash(ser
, input
);
1474 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1478 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1480 if (ser
->annotations
.object_class
) {
1481 switch (ser
->annotations
.object_type
) {
1483 php_ion_unserialize_object_iface(ser
, return_value
);
1486 php_ion_unserialize_object_enum(ser
, return_value
);
1489 php_ion_unserialize_object(ser
, return_value
);
1491 } else if (!ser
->annotations
.object_type
) {
1492 array_init(return_value
);
1493 php_ion_unserialize_hash(ser
, return_value
);
1494 } else if (ser
->annotations
.object_type
== 'o') {
1495 object_init(return_value
);
1496 php_ion_unserialize_hash(ser
, return_value
);
1498 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1499 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1503 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1505 ION_CHECK(ion_reader_step_in(ser
->reader
));
1506 array_init(return_value
);
1507 zend_hash_next_index_insert(ser
->ids
, return_value
);
1511 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1514 php_ion_unserialize_zval(ser
, &next
, &typ
);
1517 if (typ
== tid_EOF
) {
1521 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1524 ION_CHECK(ion_reader_step_out(ser
->reader
));
1527 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1529 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1532 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1533 if (err
== IERR_BUFFER_TOO_SMALL
) {
1534 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1537 ION_CHECK(err
, zend_string_release(zstr
));
1538 if (zstr
->len
> read
) {
1539 zstr
= zend_string_truncate(zstr
, read
, 0);
1544 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1547 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1549 object_init_ex(return_value
, ce_Timestamp
);
1550 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1552 zend_string
*fmt
= NULL
;
1553 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1554 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1555 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1556 zend_string_release(fmt
);
1561 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1563 ION_INT
*num
= NULL
;
1564 ION_CHECK(ion_int_alloc(reader
, &num
));
1565 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1567 // TODO: SIZEOF_ZEND_LONG == 4
1569 iERR err
= ion_int_to_int64(num
, &i64
);
1575 case IERR_NUMERIC_OVERFLOW
:
1577 ION_CHECK(ion_int_char_length(num
, &max
));
1578 zend_string
*zs
= zend_string_alloc(max
-1, 0);
1580 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1581 ZEND_ASSERT(len
== zs
->len
);
1592 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1594 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1597 ZVAL_COPY_VALUE(return_value
, backref
);
1598 zend_hash_next_index_insert(ser
->addref
, return_value
);
1600 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1601 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1605 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1607 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1610 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1611 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1613 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1615 switch (*ann_str
.value
) {
1617 if (ser
->annotations
.makeref
) {
1618 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1619 "Invalid multiple reference annotations");
1622 ser
->annotations
.makeref
= true;
1626 if (ser
->annotations
.backref
) {
1627 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1628 "Invalid multiple back reference annotations");
1631 ser
->annotations
.backref
= true;
1635 if (ser
->annotations
.object_prop
) {
1636 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1637 "Invalid multiple object property annotations");
1640 ser
->annotations
.object_prop
= true;
1642 ION_STRING prop_class
;
1643 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1644 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1647 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1648 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1657 if (ser
->annotations
.object_type
) {
1658 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1659 "Invalid multiple object type annotations: %c::%c",
1660 ser
->annotations
.object_type
, *ann_str
.value
);
1663 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1664 ION_STRING class_name
;
1665 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1666 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1669 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1670 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1676 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1677 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1678 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1681 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1682 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1683 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1689 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1692 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1695 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1698 php_ion_unserialize_annotations(ser
);
1701 if (ser
->annotations
.makeref
) {
1702 ZVAL_MAKE_REF(return_value
);
1703 zend_hash_next_index_insert(ser
->ids
, return_value
);
1704 ZVAL_DEREF(return_value
);
1708 ION_CHECK(ion_reader_is_null(ser
->reader
, &bval
));
1713 switch (ION_TYPE_INT(*typ
)) {
1716 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1720 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1724 php_ion_reader_read_int(ser
->reader
, return_value
);
1725 if (ser
->annotations
.backref
) {
1727 php_ion_unserialize_backref(ser
, return_value
);
1729 if (ser
->annotations
.object_type
) {
1730 if (!ser
->annotations
.backref
) {
1731 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1732 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1733 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1736 goto unserialize_struct
;
1742 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1745 case tid_DECIMAL_INT
:
1746 object_init_ex(return_value
, ce_Decimal
);
1747 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1748 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1749 php_ion_decimal_ctor(dec
);
1750 zend_hash_next_index_insert(ser
->ids
, return_value
);
1753 case tid_TIMESTAMP_INT
:
1754 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1755 zend_hash_next_index_insert(ser
->ids
, return_value
);
1758 case tid_SYMBOL_INT
:
1760 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1761 php_ion_symbol_zval(&sym
, return_value
);
1762 if (ser
->annotations
.object_type
) {
1763 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1764 goto unserialize_struct
;
1766 zend_hash_next_index_insert(ser
->ids
, return_value
);
1769 case tid_STRING_INT
:
1771 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1772 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1773 if (ser
->annotations
.object_type
) {
1774 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1775 goto unserialize_struct
;
1777 zend_hash_next_index_insert(ser
->ids
, return_value
);
1782 php_ion_reader_read_lob(ser
->reader
, return_value
);
1783 if (ser
->annotations
.object_type
) {
1784 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1785 goto unserialize_struct
;
1787 zend_hash_next_index_insert(ser
->ids
, return_value
);
1791 case tid_SEXP_INT
: // FIXME
1792 php_ion_unserialize_list(ser
, return_value
);
1793 if (!ser
->annotations
.object_type
) {
1798 case tid_STRUCT_INT
:
1799 unserialize_struct
: ;
1800 php_ion_unserialize_struct(ser
, return_value
);
1807 case tid_DATAGRAM_INT
:
1808 ZEND_ASSERT(!"datagram");
1814 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1816 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1818 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1821 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1822 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1824 o_ser
->unserializer
.call_magic
= true;
1825 php_ion_unserializer_php_ctor(o_ser
);
1827 ser
= &o_ser
->unserializer
;
1830 zend_object
*zo_reader
;
1831 php_ion_reader
*reader
;
1833 switch (Z_TYPE_P(zdata
)) {
1835 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
1836 reader
= php_ion_obj(reader
, zo_reader
);
1837 reader
->type
= BUFFER_READER
;
1838 reader
->buffer
= zend_string_copy(Z_STR_P(zdata
));
1842 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
1843 reader
= php_ion_obj(reader
, zo_reader
);
1844 reader
->type
= STREAM_READER
;
1845 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
1849 ZEND_ASSERT(!IS_STRING
&& !IS_RESOURCE
);
1853 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
1854 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
1857 php_ion_reader_ctor(reader
);
1858 ser
->reader
= reader
->reader
;
1860 php_ion_globals_unserializer_step();
1861 php_ion_unserialize_zval(ser
, return_value
, NULL
);
1862 php_ion_globals_unserializer_exit();
1864 OBJ_RELEASE(zo_reader
);
1866 OBJ_RELEASE(zo_opt
);
1869 OBJ_RELEASE(zo_ser
);