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
));
452 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
454 if (ion_decimal_is_integer(&obj
->dec
)) {
456 php_ion_decimal_to_int(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
457 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
459 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
460 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
461 zend_string_release(zstr
);
465 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
467 ion_decimal_free(&obj
->dec
);
470 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
472 typedef php_date_obj php_ion_timestamp
;
474 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
476 decQuad microsecs
, result
;
477 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
478 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
481 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
483 decQuad microsecs
, us
;
484 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
487 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
491 return zend_string_init(ZEND_STRL("c"), 0);
493 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
495 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
497 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
499 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
501 return zend_string_init(ZEND_STRL("Y\\T"), 0);
503 return zend_string_init(ZEND_STRL("c"), 0);
507 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
509 timelib_time
*time
= timelib_time_ctor();
511 switch (ts
->precision
) {
513 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
516 time
->s
= ts
->seconds
;
519 time
->i
= ts
->minutes
;
532 time
->z
= ts
->tz_offset
* 60;
536 *fmt
= php_dt_format_from_precision(ts
->precision
);
541 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
543 memset(buf
, 0, sizeof(*buf
));
546 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
548 if (!precision
|| precision
> ION_TS_FRAC
) {
549 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
550 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
551 } else switch ((buf
->precision
= precision
)) {
553 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
556 buf
->seconds
= ts
->time
->s
;
559 buf
->minutes
= ts
->time
->i
;
562 buf
->hours
= ts
->time
->h
;
563 buf
->day
= ts
->time
->d
;
566 buf
->month
= ts
->time
->m
;
569 buf
->year
= ts
->time
->y
;
572 buf
->tz_offset
= ts
->time
->z
/ 60;
578 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
581 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
583 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
585 fmt
= php_dt_format_from_precision(precision
);
586 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
587 zend_string_release(fmt
);
590 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
593 timelib_time_dtor(obj
->time
);
597 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
599 typedef struct php_ion_catalog
{
604 php_ion_decl(catalog
, Catalog
);
606 typedef struct php_ion_reader_options
{
607 ION_READER_OPTIONS opt
;
608 zend_object
*cat
, *dec_ctx
, *cb
, std
;
609 } php_ion_reader_options
;
611 php_ion_decl(reader_options
, Reader_Options
);
613 typedef struct php_ion_reader
{
627 zend_object
*opt
, std
;
630 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
632 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
633 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
635 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
636 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
637 user
->limit
-= remaining
;
640 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
643 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
644 if (EXPECTED(read
> 0)) {
649 if (EXPECTED(read
== 0)) {
653 return IERR_READ_ERROR
;
656 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
659 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
665 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
670 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
673 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
676 opt
->opt
.context_change_notifier
.context
= obj
;
678 if (obj
->type
== STREAM_READER
) {
679 PTR_CHECK(obj
->stream
.ptr
);
680 GC_ADDREF(obj
->stream
.ptr
->res
);
682 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
683 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
684 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
685 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
688 err
= ion_reader_open_buffer(&obj
->reader
,
689 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
690 opt
? &opt
->opt
: NULL
);
693 opt
->opt
.context_change_notifier
.context
= NULL
;
699 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
702 ion_reader_close(obj
->reader
);
704 if (obj
->type
== STREAM_READER
) {
705 if (obj
->stream
.buf
.value
) {
706 efree(obj
->stream
.buf
.value
);
708 if (obj
->stream
.ptr
) {
709 zend_list_delete(obj
->stream
.ptr
->res
);
713 zend_string_release(obj
->buffer
);
718 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
720 typedef struct php_ion_writer_options
{
721 ION_WRITER_OPTIONS opt
;
722 zend_object
*cat
, *dec_ctx
, *col
, std
;
723 } php_ion_writer_options
;
725 php_ion_decl(writer_options
, Writer_Options
);
727 typedef struct php_ion_writer
{
743 zend_object
*opt
, std
;
747 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
749 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
751 if (EXPECTED(user
->limit
&& user
->curr
)) {
752 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
753 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
754 return IERR_WRITE_ERROR
;
757 user
->curr
= writer
->stream
.buf
.value
;
758 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
762 #define REF_STR() do { \
763 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
764 GC_ADDREF(obj->buffer.str.s); \
767 #define NEW_REF_STR() do {\
768 if (Z_STR_P(ref) != obj->buffer.str.s) { \
769 zval_ptr_dtor(ref); \
774 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
776 PTR_CHECK(obj
->stream
.ptr
);
777 GC_ADDREF(obj
->stream
.ptr
->res
);
779 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
780 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
782 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
784 zval
*ref
= &obj
->buffer
.val
;
787 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
788 smart_str_0(&obj
->buffer
.str
);
792 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
794 zval
*ref
= &obj
->buffer
.val
;
797 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
802 // we've been separated
803 GC_ADDREF(obj
->buffer
.str
.s
);
806 // we have to separate
807 fprintf(stderr
, "SEPARATE\n");
808 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
812 zend_string
*old
= obj
->buffer
.str
.s
;
814 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
815 if (old
== obj
->buffer
.str
.s
) {
817 } else if(old
== Z_STR_P(ref
)) {
824 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
826 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
829 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
830 smart_str_0(&writer
->buffer
.str
);
831 if (user
->limit
== user
->curr
) {
832 php_ion_writer_buffer_grow(writer
);
835 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
836 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
841 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
844 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
847 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
848 ION_STREAM_HANDLER h
;
849 if (obj
->type
== STREAM_WRITER
) {
850 h
= php_ion_writer_stream_handler
;
851 php_ion_writer_stream_init(obj
, opt
);
853 h
= php_ion_writer_buffer_handler
;
854 php_ion_writer_buffer_init(obj
);
857 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
861 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
864 ion_writer_close(obj
->writer
);
866 if (obj
->type
== STREAM_WRITER
) {
867 if (obj
->stream
.buf
.value
) {
868 efree(obj
->stream
.buf
.value
);
870 if (obj
->stream
.ptr
) {
871 zend_list_delete(obj
->stream
.ptr
->res
);
874 if (obj
->buffer
.str
.s
) {
875 smart_str_0(&obj
->buffer
.str
);
876 zend_string_release(obj
->buffer
.str
.s
);
878 zval_ptr_dtor(&obj
->buffer
.val
);
882 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
884 typedef struct php_ion_serializer_php
{
885 php_ion_serializer serializer
;
886 zend_object
*opt
, std
;
887 } php_ion_serializer_php
;
889 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
891 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
892 ser_obj
->serializer
.ids
= global_ser
->ids
;
893 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
895 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
896 ser_obj
->serializer
.call_magic
);
897 if (ser_obj
->serializer
.call_custom
) {
898 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
899 ser_obj
->serializer
.call_custom
);
900 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
902 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
905 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
906 ser_obj
->serializer
.options
= &o_woptions
->opt
;
907 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
909 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
913 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
915 if (obj
->serializer
.call_custom
) {
916 zend_string_release(obj
->serializer
.call_custom
);
920 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
922 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
924 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
928 zend_string
*k
= NULL
;
929 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
933 const char *class_name
, *prop_name
;
934 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
935 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("p"))));
936 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, class_name
, prop_name
- class_name
- 1)));
941 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, prop_name
, prop_len
)));
943 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
944 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
945 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, ptr
, end
- ptr
)));
948 php_ion_serialize_zval(ser
, v
);
950 ZEND_HASH_FOREACH_END();
952 ION_CHECK(ion_writer_finish_container(ser
->writer
));
955 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
957 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
960 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
961 php_ion_serialize_zval(ser
, v
);
963 ZEND_HASH_FOREACH_END();
965 ION_CHECK(ion_writer_finish_container(ser
->writer
));
968 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
970 if (zend_array_is_list(arr
)) {
971 php_ion_serialize_list(ser
, arr
);
973 php_ion_serialize_struct(ser
, arr
, false);
977 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
983 ZVAL_OBJ(&tmp
, zobject
);
984 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
986 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("S"))));
987 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
988 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_cstr(&is
, (char *) buf
, len
)));
990 } else if (!EG(exception
)){
991 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
992 "Failed to serialize class %s", zobject
->ce
->name
->val
);
996 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1001 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1004 if (IS_ARRAY
== Z_TYPE(rv
)) {
1006 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, fn
? "C" : "O", 1)));
1007 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1008 php_ion_serialize_zval(ser
, &rv
);
1011 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1012 "%s serializer %s::%s did not return an array",
1013 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1014 fn
? fn
->common
.function_name
->val
: "__serialize");
1018 static inline zend_string
*fq_enum_case(zend_object
*zobject
)
1020 zval
*cn
= zend_enum_fetch_case_name(zobject
);
1021 zend_string
*en
= zend_string_alloc(zobject
->ce
->name
->len
+ Z_STRLEN_P(cn
) + strlen("::"), 0);
1022 memcpy(en
->val
, zobject
->ce
->name
->val
, zobject
->ce
->name
->len
);
1023 en
->val
[zobject
->ce
->name
->len
] = ':';
1024 en
->val
[zobject
->ce
->name
->len
+ 1] = ':';
1025 memcpy(&en
->val
[zobject
->ce
->name
->len
+ 2], Z_STRVAL_P(cn
), Z_STRLEN_P(cn
));
1026 en
->val
[en
->len
] = 0;
1030 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1033 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("E"))));
1035 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1036 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1037 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1040 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1044 if (zobject
->ce
!= zend_standard_class_def
) {
1045 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("c"))));
1046 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1048 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("o"))));
1052 ZVAL_OBJ(&zobj
, zobject
);
1053 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1055 php_ion_serialize_struct(ser
, props
, true);
1056 zend_release_properties(props
);
1058 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1059 "Could not get properties for serialization of class %s",
1060 zobject
->ce
->name
->val
);
1064 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1066 return ce
->__serialize
&& ser
->call_magic
;
1069 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1071 return !!ce
->serialize
;
1074 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1076 if (ser
->call_custom
) {
1077 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1082 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1085 zend_class_entry
*ce
= zobject
->ce
;
1088 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1089 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1090 "Serializing %s is not allowed", ce
->name
->val
);
1094 if (can_call_magic_serialize(ser
, ce
)) {
1095 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1096 } else if (can_call_iface_serialize(ser
, ce
)) {
1097 php_ion_serialize_object_iface(ser
, zobject
);
1098 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1099 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1100 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1101 php_ion_serialize_object_enum(ser
, zobject
);
1102 } else if (ce
== ce_Symbol
) {
1103 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1104 } else if (ce
== ce_Decimal
) {
1105 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1106 } else if (ce
== ce_Timestamp
) {
1108 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1109 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1110 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1112 php_ion_serialize_object_std(ser
, zobject
);
1116 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1118 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1121 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1122 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1124 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("r"))));
1125 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1129 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1130 zend_hash_index_add(ser
->ids
, idx
, &num
);
1133 zend_hash_next_index_insert(ser
->tmp
, zv
);
1135 switch (Z_TYPE_P(zv
)) {
1137 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1141 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1145 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1149 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("R"))));
1150 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1156 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1160 switch (Z_TYPE_P(zv
)) {
1162 ION_CHECK(ion_writer_write_null(ser
->writer
));
1165 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1168 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1171 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1174 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1180 php_ion_serialize_refcounted(ser
, zv
);
1183 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1184 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1188 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1190 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1192 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1195 zo_ser
= create_ion_Serializer_PHP(NULL
);
1196 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1198 o_ser
->serializer
.call_magic
= true;
1199 php_ion_serializer_php_ctor(o_ser
);
1201 ser
= &o_ser
->serializer
;
1204 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1205 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1206 writer
->type
= BUFFER_WRITER
;
1209 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1210 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1213 php_ion_writer_ctor(writer
);
1214 ser
->writer
= writer
->writer
;
1215 ser
->buffer
= &writer
->buffer
.str
;
1217 /* start off with a global PHP annotation instead of repeating it all over the place */
1218 if (0 == php_ion_globals_serializer_step()) {
1220 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("PHP"))),
1221 if (zo_ser
) OBJ_RELEASE(zo_ser
));
1223 php_ion_serialize_zval(ser
, zv
);
1224 php_ion_globals_serializer_exit();
1226 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1227 ion_writer_flush(ser
->writer
, NULL
);
1228 RETVAL_STR_COPY(ser
->buffer
->s
);
1230 OBJ_RELEASE(zo_writer
);
1232 OBJ_RELEASE(zo_opt
);
1235 OBJ_RELEASE(zo_ser
);
1239 typedef struct php_ion_unserializer_php
{
1240 php_ion_unserializer unserializer
;
1241 zend_object
*opt
, std
;
1242 } php_ion_unserializer_php
;
1244 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1246 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1247 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1248 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1249 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1251 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1252 ser_obj
->unserializer
.call_magic
);
1253 if (ser_obj
->unserializer
.call_custom
) {
1254 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1255 ser_obj
->unserializer
.call_custom
);
1256 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1258 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1261 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1262 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1263 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1265 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1269 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1271 if (obj
->unserializer
.call_custom
) {
1272 zend_string_release(obj
->unserializer
.call_custom
);
1276 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1278 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1280 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1283 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1285 return (ce
&& ce
->unserialize
);
1288 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1290 if (ser
->call_custom
) {
1291 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1296 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1298 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1301 object_init_ex(return_value
, ce
);
1302 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1305 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1306 "Could not find class %s", ser
->annotations
.object_class
->val
);
1310 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1312 zend_string
*zs_case
= zval_get_string(return_value
);
1315 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1316 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1317 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1318 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1321 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1322 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1323 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1326 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1327 zend_hash_next_index_insert(ser
->ids
, return_value
);
1328 zend_string_release(zs_case
);
1331 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1333 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1334 if (can_call_iface_unserialize(ser
, ce
)) {
1335 zend_string
*s
= zval_get_string(return_value
);
1336 ZVAL_NULL(return_value
);
1337 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1338 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1339 RETVAL_ZVAL(backref
, 0, 0);
1340 } else if (!EG(exception
)) {
1341 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1342 "Failed to unserialize class %s", ce
->name
->val
);
1344 zend_string_release(s
);
1346 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1347 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1351 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1353 zend_hash_next_index_insert(ser
->ids
, return_value
);
1355 ION_CHECK(ion_reader_step_in(ser
->reader
));
1359 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1361 if (typ
== tid_EOF
) {
1366 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &is
));
1367 zend_string
*key
= zend_string_from_ion(&is
);
1370 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1371 ION_CATCH(zend_string_release(key
));
1373 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1374 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1375 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1377 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1378 zval_ptr_dtor(&zvalue
);
1379 zend_string_release(key
);
1382 ION_CHECK(ion_reader_step_out(ser
->reader
));
1385 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1387 zend_hash_next_index_insert(ser
->ids
, return_value
);
1389 ION_CHECK(ion_reader_step_in(ser
->reader
));
1393 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1396 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1397 zend_string
*key
= zend_string_from_ion(&name
);
1400 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1401 ION_CATCH(zend_string_release(key
));
1403 if (typ
== tid_EOF
) {
1404 zend_string_release(key
);
1408 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1410 zend_string_release(key
);
1413 ION_CHECK(ion_reader_step_out(ser
->reader
));
1416 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1418 switch (ser
->annotations
.object_type
) {
1424 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1425 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1426 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1431 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1432 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1433 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1435 *fn
= zobject
->ce
->__unserialize
;
1439 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1440 "Invalid object type %c", ser
->annotations
.object_type
);
1444 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1446 // backup possible backref to array returned by magic/custom __serialize()
1447 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1449 php_ion_unserialize_class(ser
, return_value
);
1452 zend_function
*fn
= NULL
;
1453 zend_object
*zobject
= Z_OBJ_P(return_value
);
1454 verify_unserializer(ser
, zobject
, &fn
);
1459 php_ion_unserialize_props(ser
, return_value
);
1464 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1465 zval_ptr_dtor(input
);
1467 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1468 php_ion_unserialize_hash(ser
, input
);
1473 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1477 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1479 if (ser
->annotations
.object_class
) {
1480 switch (ser
->annotations
.object_type
) {
1482 php_ion_unserialize_object_iface(ser
, return_value
);
1485 php_ion_unserialize_object_enum(ser
, return_value
);
1488 php_ion_unserialize_object(ser
, return_value
);
1490 } else if (!ser
->annotations
.object_type
) {
1491 array_init(return_value
);
1492 php_ion_unserialize_hash(ser
, return_value
);
1493 } else if (ser
->annotations
.object_type
== 'o') {
1494 object_init(return_value
);
1495 php_ion_unserialize_hash(ser
, return_value
);
1497 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1498 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1502 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1504 ION_CHECK(ion_reader_step_in(ser
->reader
));
1505 array_init(return_value
);
1506 zend_hash_next_index_insert(ser
->ids
, return_value
);
1510 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1513 php_ion_unserialize_zval(ser
, &next
, &typ
);
1516 if (typ
== tid_EOF
) {
1520 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1523 ION_CHECK(ion_reader_step_out(ser
->reader
));
1526 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1528 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1531 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1532 if (err
== IERR_BUFFER_TOO_SMALL
) {
1533 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1536 ION_CHECK(err
, zend_string_release(zstr
));
1537 if (zstr
->len
> read
) {
1538 zstr
= zend_string_truncate(zstr
, read
, 0);
1543 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1546 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1548 object_init_ex(return_value
, ce_Timestamp
);
1549 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1551 zend_string
*fmt
= NULL
;
1552 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1553 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1554 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1555 zend_string_release(fmt
);
1560 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1562 ION_INT
*num
= NULL
;
1563 ION_CHECK(ion_int_alloc(reader
, &num
));
1564 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1566 // TODO: SIZEOF_ZEND_LONG == 4
1568 iERR err
= ion_int_to_int64(num
, &i64
);
1574 case IERR_NUMERIC_OVERFLOW
:
1576 ION_CHECK(ion_int_char_length(num
, &max
));
1577 zend_string
*zs
= zend_string_alloc(max
-1, 0);
1579 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1580 ZEND_ASSERT(len
== zs
->len
);
1591 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1593 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1596 ZVAL_COPY_VALUE(return_value
, backref
);
1597 zend_hash_next_index_insert(ser
->addref
, return_value
);
1599 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1600 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1604 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1606 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1609 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1610 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1612 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1614 switch (*ann_str
.value
) {
1616 if (ser
->annotations
.makeref
) {
1617 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1618 "Invalid multiple reference annotations");
1621 ser
->annotations
.makeref
= true;
1625 if (ser
->annotations
.backref
) {
1626 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1627 "Invalid multiple back reference annotations");
1630 ser
->annotations
.backref
= true;
1634 if (ser
->annotations
.object_prop
) {
1635 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1636 "Invalid multiple object property annotations");
1639 ser
->annotations
.object_prop
= true;
1641 ION_STRING prop_class
;
1642 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1643 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1646 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1647 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1656 if (ser
->annotations
.object_type
) {
1657 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1658 "Invalid multiple object type annotations: %c::%c",
1659 ser
->annotations
.object_type
, *ann_str
.value
);
1662 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1663 ION_STRING class_name
;
1664 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1665 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1668 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1669 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1675 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1676 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1677 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1680 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1681 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1682 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1688 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1691 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1694 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1697 php_ion_unserialize_annotations(ser
);
1700 if (ser
->annotations
.makeref
) {
1701 ZVAL_MAKE_REF(return_value
);
1702 zend_hash_next_index_insert(ser
->ids
, return_value
);
1703 ZVAL_DEREF(return_value
);
1707 ION_CHECK(ion_reader_is_null(ser
->reader
, &bval
));
1712 switch (ION_TYPE_INT(*typ
)) {
1715 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1719 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1723 php_ion_reader_read_int(ser
->reader
, return_value
);
1724 if (ser
->annotations
.backref
) {
1726 php_ion_unserialize_backref(ser
, return_value
);
1728 if (ser
->annotations
.object_type
) {
1729 if (!ser
->annotations
.backref
) {
1730 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1731 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1732 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1735 goto unserialize_struct
;
1741 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1744 case tid_DECIMAL_INT
:
1745 object_init_ex(return_value
, ce_Decimal
);
1746 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1747 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1748 php_ion_decimal_ctor(dec
);
1749 zend_hash_next_index_insert(ser
->ids
, return_value
);
1752 case tid_TIMESTAMP_INT
:
1753 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1754 zend_hash_next_index_insert(ser
->ids
, return_value
);
1757 case tid_SYMBOL_INT
:
1759 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1760 php_ion_symbol_zval(&sym
, return_value
);
1761 if (ser
->annotations
.object_type
) {
1762 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1763 goto unserialize_struct
;
1765 zend_hash_next_index_insert(ser
->ids
, return_value
);
1768 case tid_STRING_INT
:
1770 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1771 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1772 if (ser
->annotations
.object_type
) {
1773 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1774 goto unserialize_struct
;
1776 zend_hash_next_index_insert(ser
->ids
, return_value
);
1781 php_ion_reader_read_lob(ser
->reader
, return_value
);
1782 if (ser
->annotations
.object_type
) {
1783 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1784 goto unserialize_struct
;
1786 zend_hash_next_index_insert(ser
->ids
, return_value
);
1790 case tid_SEXP_INT
: // FIXME
1791 php_ion_unserialize_list(ser
, return_value
);
1792 if (!ser
->annotations
.object_type
) {
1797 case tid_STRUCT_INT
:
1798 unserialize_struct
: ;
1799 php_ion_unserialize_struct(ser
, return_value
);
1806 case tid_DATAGRAM_INT
:
1807 ZEND_ASSERT(!"datagram");
1813 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1815 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1817 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1820 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1821 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1823 o_ser
->unserializer
.call_magic
= true;
1824 php_ion_unserializer_php_ctor(o_ser
);
1826 ser
= &o_ser
->unserializer
;
1829 zend_object
*zo_reader
;
1830 php_ion_reader
*reader
;
1832 switch (Z_TYPE_P(zdata
)) {
1834 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
1835 reader
= php_ion_obj(reader
, zo_reader
);
1836 reader
->type
= BUFFER_READER
;
1837 reader
->buffer
= zend_string_copy(Z_STR_P(zdata
));
1841 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
1842 reader
= php_ion_obj(reader
, zo_reader
);
1843 reader
->type
= STREAM_READER
;
1844 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
1848 ZEND_ASSERT(!IS_STRING
&& !IS_RESOURCE
);
1852 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
1853 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
1856 php_ion_reader_ctor(reader
);
1857 ser
->reader
= reader
->reader
;
1859 php_ion_globals_unserializer_step();
1860 php_ion_unserialize_zval(ser
, return_value
, NULL
);
1861 php_ion_globals_unserializer_exit();
1863 OBJ_RELEASE(zo_reader
);
1865 OBJ_RELEASE(zo_opt
);
1868 OBJ_RELEASE(zo_ser
);