2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2021, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
16 #include "ext/date/php_date.h"
18 typedef struct php_ion_serializer
{
20 ION_WRITER_OPTIONS
*options
;
23 zend_string
*call_custom
;
32 typedef struct php_ion_annotaions
{
35 uint8_t object_prop
:1;
37 zend_string
*object_class
;
38 zend_string
*property_class
;
39 } php_ion_annotations
;
41 typedef struct php_ion_unserializer
{
43 ION_READER_OPTIONS
*options
;
46 zend_string
*call_custom
;
55 php_ion_annotations annotations
;
56 } php_ion_unserializer
;
58 ZEND_BEGIN_MODULE_GLOBALS(ion
)
60 php_ion_serializer serializer
;
61 php_ion_unserializer unserializer
;
64 HashTable serializer
[2];
65 HashTable unserializer
[3];
68 ZEND_END_MODULE_GLOBALS(ion
);
71 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
73 # define php_ion_globals ion_globals
76 ZEND_DECLARE_MODULE_GLOBALS(ion
);
78 static inline void php_ion_globals_serializer_init(void)
80 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
81 HashTable
*h
= php_ion_globals
._ht
.serializer
;
83 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
84 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
87 static inline uint32_t php_ion_globals_serializer_step(void)
89 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
92 if (!(level
= s
->level
++)) {
93 zend_hash_clean(s
->ids
);
94 zend_hash_clean(s
->tmp
);
99 static inline uint32_t php_ion_globals_serializer_exit(void)
101 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
103 ZEND_ASSERT(s
->level
);
105 zend_hash_clean(s
->ids
);
106 zend_hash_clean(s
->tmp
);
111 static inline void php_ion_globals_serializer_dtor(void)
113 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
115 zend_hash_destroy(s
->tmp
);
116 zend_hash_destroy(s
->ids
);
119 void ZVAL_ADDREF(zval
*zv
)
122 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
127 static inline void php_ion_globals_unserializer_init(void)
129 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
130 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
132 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
133 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
134 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
137 static inline void php_ion_globals_unserializer_step(void)
139 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
142 zend_hash_clean(s
->addref
);
143 zend_hash_clean(s
->ids
);
144 zend_hash_clean(s
->tmp
);
148 static inline void php_ion_globals_unserializer_exit(void)
150 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
152 ZEND_ASSERT(s
->level
);
154 zend_hash_clean(s
->addref
);
155 zend_hash_clean(s
->ids
);
156 zend_hash_clean(s
->tmp
);
160 static inline void php_ion_globals_unserializer_dtor(void)
162 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
164 zend_hash_destroy(s
->addref
);
165 zend_hash_destroy(s
->ids
);
166 zend_hash_destroy(s
->tmp
);
169 static zend_class_entry
175 *ce_Decimal_Context_Rounding
,
182 *ce_Reader_Buffer_Reader
,
183 *ce_Reader_Stream_Reader
,
187 *ce_Symbol_ImportLocation
,
189 *ce_Symbol_System_SID
,
192 *ce_Timestamp_Precision
,
195 *ce_Unserializer_PHP
,
199 *ce_Writer_Buffer_Writer
,
201 *ce_Writer_Stream_Writer
,
205 #define php_ion_decl(type, cname, ...) \
206 static zend_object_handlers oh_ ## cname; \
207 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
209 if (!ce) ce = ce_ ## cname; \
210 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
211 zend_object_std_init(&o->std, ce); \
212 object_properties_init(&o->std, ce); \
213 o->std.handlers = &oh_ ## cname; \
216 static inline void free_ion_ ## cname(zend_object *std) \
218 php_ion_ ## type *obj = php_ion_obj(type, std); \
220 zend_object_std_dtor(std); \
223 #define php_ion_register(type, cname, ...) do { \
224 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
225 ce_ ## cname ->create_object = create_ion_ ## cname; \
226 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
227 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
228 oh_ ## cname .free_obj = free_ion_ ## cname; \
231 #define php_ion_obj(type, obj) \
232 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
234 #define ION_CHECK(err, ...) do { \
237 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
243 #define ION_CATCH(...) do { \
244 if (EG(exception)) { \
250 #define PTR_CHECK(ptr, ...) do { \
252 zend_throw_error(NULL, "Uninitialized object"); \
258 #define OBJ_CHECK(obj) do { \
260 PTR_CHECK(*((void **)obj)); \
263 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
265 is
->length
= zs
? zs
->len
: 0;
266 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
270 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
272 return zend_string_init((const char *) s
->value
, s
->length
, 0);
275 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
279 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
282 typedef struct php_ion_type
{
287 php_ion_decl(type
, Type
);
289 #define RETURN_IONTYPE(typ) do { \
290 zend_object *__zo = php_ion_type_fetch(typ); \
291 if (UNEXPECTED(!__zo)) { \
294 RETURN_OBJ_COPY(__zo); \
297 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
299 zend_long index
= ION_TYPE_INT(typ
);
300 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
302 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
303 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
306 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
309 typedef struct php_ion_symbol_iloc
{
310 ION_SYMBOL_IMPORT_LOCATION loc
;
313 } php_ion_symbol_iloc
;
315 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
317 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
318 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
319 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
322 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
324 zend_string_release(obj
->name
);
327 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
329 typedef struct php_ion_symbol
{
332 zend_object
*iloc
, std
;
335 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
337 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
340 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
342 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
344 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
346 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
347 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
349 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
353 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
356 zend_string_release(obj
->value
);
360 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
362 object_init_ex(return_value
, ce_Symbol
);
363 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
365 sym
->sym
.sid
= sym_ptr
->sid
;
366 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
367 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
369 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
370 sym
->iloc
= Z_OBJ(ziloc
);
372 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
373 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
374 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
376 php_ion_symbol_iloc_ctor(iloc
);
379 php_ion_symbol_ctor(sym
);
381 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
382 GC_DELREF(sym
->iloc
);
386 php_ion_decl(symbol
, Symbol
, php_ion_symbol_dtor(obj
));
388 typedef struct php_ion_symbol_table
{
390 } php_ion_symbol_table
;
392 php_ion_decl(symbol_table
, Symbol_Table
);
394 typedef struct php_ion_decimal_ctx
{
397 } php_ion_decimal_ctx
;
399 #define php_ion_decimal_ctx_init_max(c, rounding) \
400 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
401 static inline void php_ion_decimal_ctx_init(decContext
*ctx
,
402 zend_long digits
, zend_long emax
, zend_long emin
, zend_long round
, zend_bool clamp
)
404 memset(ctx
, 0, sizeof(*ctx
));
405 ctx
->digits
= digits
;
412 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
414 if (!obj
->ctx
.digits
) {
415 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
418 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
420 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
422 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
423 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
424 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
425 zend_update_property_bool(obj
->std
.ce
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
428 php_ion_decl(decimal_ctx
, Decimal_Context
);
430 typedef struct php_ion_decimal
{
432 zend_object
*ctx
, std
;
435 static inline void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
437 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
438 ION_CHECK(ion_decimal_from_int32(dec
, num
));
439 } else if (num
> 0 && num
<= UINT32_MAX
) {
440 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
443 ION_CHECK(ion_int_alloc(NULL
, &iint
));
444 ION_CHECK(ion_int_from_long(iint
, num
),
446 /* WATCH OUT: BS API */
447 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
448 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
454 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
456 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
457 (void) ion_decimal_to_string(dec
, zstr
->val
);
458 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
461 static inline void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
464 ION_CHECK(ion_int_alloc(NULL
, &ii
));
465 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
467 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
472 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
476 if (!ion_decimal_is_integer(&obj
->dec
)) {
481 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_max
, &g_dec_ctx
, &result
);
486 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_min
, &g_dec_ctx
, &result
);
493 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
497 object_init_ex(&zdc
, ce_Decimal_Context
);
498 obj
->ctx
= Z_OBJ(zdc
);
499 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
502 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
504 if (php_ion_decimal_fits_zend_long(obj
)) {
506 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
507 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
509 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
510 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
511 zend_string_release(zstr
);
515 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
517 ion_decimal_free(&obj
->dec
);
520 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
522 typedef php_date_obj php_ion_timestamp
;
524 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
526 decQuad microsecs
, result
;
527 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
528 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
531 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
533 decQuad microsecs
, us
;
534 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
537 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
541 return zend_string_init(ZEND_STRL("c"), 0);
543 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
545 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
547 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
549 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
551 return zend_string_init(ZEND_STRL("Y\\T"), 0);
553 return zend_string_init(ZEND_STRL("c"), 0);
557 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
559 timelib_time
*time
= timelib_time_ctor();
561 switch (ts
->precision
) {
563 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
566 time
->s
= ts
->seconds
;
569 time
->i
= ts
->minutes
;
582 time
->z
= ts
->tz_offset
* 60;
586 *fmt
= php_dt_format_from_precision(ts
->precision
);
591 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
593 memset(buf
, 0, sizeof(*buf
));
596 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
598 if (!precision
|| precision
> ION_TS_FRAC
) {
599 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
600 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
601 } else switch ((buf
->precision
= precision
)) {
603 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
606 buf
->seconds
= ts
->time
->s
;
609 buf
->minutes
= ts
->time
->i
;
612 buf
->hours
= ts
->time
->h
;
613 buf
->day
= ts
->time
->d
;
616 buf
->month
= ts
->time
->m
;
619 buf
->year
= ts
->time
->y
;
622 buf
->tz_offset
= ts
->time
->z
/ 60;
628 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
631 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
633 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
635 fmt
= php_dt_format_from_precision(precision
);
636 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
637 zend_string_release(fmt
);
640 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
643 timelib_time_dtor(obj
->time
);
647 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
649 typedef struct php_ion_catalog
{
654 php_ion_decl(catalog
, Catalog
);
656 typedef struct php_ion_reader_options
{
657 ION_READER_OPTIONS opt
;
658 zend_object
*cat
, *dec_ctx
, *cb
, std
;
659 } php_ion_reader_options
;
661 php_ion_decl(reader_options
, Reader_Options
);
663 typedef struct php_ion_reader
{
677 zend_object
*opt
, std
;
680 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
682 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
683 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
685 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
686 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
687 user
->limit
-= remaining
;
690 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
693 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
694 if (EXPECTED(read
> 0)) {
699 if (EXPECTED(read
== 0)) {
703 return IERR_READ_ERROR
;
706 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
709 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
715 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
720 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
723 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
726 opt
->opt
.context_change_notifier
.context
= obj
;
728 if (obj
->type
== STREAM_READER
) {
729 PTR_CHECK(obj
->stream
.ptr
);
730 GC_ADDREF(obj
->stream
.ptr
->res
);
732 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
733 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
734 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
735 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
738 err
= ion_reader_open_buffer(&obj
->reader
,
739 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
740 opt
? &opt
->opt
: NULL
);
743 opt
->opt
.context_change_notifier
.context
= NULL
;
749 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
752 ion_reader_close(obj
->reader
);
754 if (obj
->type
== STREAM_READER
) {
755 if (obj
->stream
.buf
.value
) {
756 efree(obj
->stream
.buf
.value
);
758 if (obj
->stream
.ptr
) {
759 zend_list_delete(obj
->stream
.ptr
->res
);
763 zend_string_release(obj
->buffer
);
768 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
770 typedef struct php_ion_writer_options
{
771 ION_WRITER_OPTIONS opt
;
772 zend_object
*cat
, *dec_ctx
, *col
, std
;
773 } php_ion_writer_options
;
775 php_ion_decl(writer_options
, Writer_Options
);
777 typedef struct php_ion_writer
{
793 zend_object
*opt
, std
;
797 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
799 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
801 if (EXPECTED(user
->limit
&& user
->curr
)) {
802 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
803 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
804 return IERR_WRITE_ERROR
;
807 user
->curr
= writer
->stream
.buf
.value
;
808 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
812 #define REF_STR() do { \
813 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
814 GC_ADDREF(obj->buffer.str.s); \
817 #define NEW_REF_STR() do {\
818 if (Z_STR_P(ref) != obj->buffer.str.s) { \
819 zval_ptr_dtor(ref); \
824 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
826 PTR_CHECK(obj
->stream
.ptr
);
827 GC_ADDREF(obj
->stream
.ptr
->res
);
829 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
830 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
832 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
834 zval
*ref
= &obj
->buffer
.val
;
837 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
838 smart_str_0(&obj
->buffer
.str
);
842 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
844 zval
*ref
= &obj
->buffer
.val
;
847 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
852 // we've been separated
853 GC_ADDREF(obj
->buffer
.str
.s
);
856 // we have to separate
857 fprintf(stderr
, "SEPARATE\n");
858 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
862 zend_string
*old
= obj
->buffer
.str
.s
;
864 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
865 if (old
== obj
->buffer
.str
.s
) {
867 } else if(old
== Z_STR_P(ref
)) {
874 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
876 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
879 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
880 smart_str_0(&writer
->buffer
.str
);
881 if (user
->limit
== user
->curr
) {
882 php_ion_writer_buffer_grow(writer
);
885 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
886 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
891 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
894 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
897 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
898 ION_STREAM_HANDLER h
;
899 if (obj
->type
== STREAM_WRITER
) {
900 h
= php_ion_writer_stream_handler
;
901 php_ion_writer_stream_init(obj
, opt
);
903 h
= php_ion_writer_buffer_handler
;
904 php_ion_writer_buffer_init(obj
);
907 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
911 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
914 ion_writer_close(obj
->writer
);
916 if (obj
->type
== STREAM_WRITER
) {
917 if (obj
->stream
.buf
.value
) {
918 efree(obj
->stream
.buf
.value
);
920 if (obj
->stream
.ptr
) {
921 zend_list_delete(obj
->stream
.ptr
->res
);
924 if (obj
->buffer
.str
.s
) {
925 smart_str_0(&obj
->buffer
.str
);
926 zend_string_release(obj
->buffer
.str
.s
);
928 zval_ptr_dtor(&obj
->buffer
.val
);
932 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
934 typedef struct php_ion_serializer_php
{
935 php_ion_serializer serializer
;
936 zend_object
*opt
, std
;
937 } php_ion_serializer_php
;
939 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
941 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
942 ser_obj
->serializer
.ids
= global_ser
->ids
;
943 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
945 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
946 ser_obj
->serializer
.call_magic
);
947 if (ser_obj
->serializer
.call_custom
) {
948 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
949 ser_obj
->serializer
.call_custom
);
950 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
952 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
955 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
956 ser_obj
->serializer
.options
= &o_woptions
->opt
;
957 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
959 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
963 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
965 if (obj
->serializer
.call_custom
) {
966 zend_string_release(obj
->serializer
.call_custom
);
970 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
972 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
974 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
978 zend_string
*k
= NULL
;
979 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
983 const char *class_name
, *prop_name
;
984 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
985 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("p"))));
986 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
991 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, (char *) prop_name
, prop_len
)));
993 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
994 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
995 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, ptr
, end
- ptr
)));
998 php_ion_serialize_zval(ser
, v
);
1000 ZEND_HASH_FOREACH_END();
1002 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1005 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1007 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1010 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1011 php_ion_serialize_zval(ser
, v
);
1013 ZEND_HASH_FOREACH_END();
1015 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1018 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1020 if (zend_array_is_list(arr
)) {
1021 php_ion_serialize_list(ser
, arr
);
1023 php_ion_serialize_struct(ser
, arr
, false);
1027 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1033 ZVAL_OBJ(&tmp
, zobject
);
1034 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1036 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("S"))));
1037 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1038 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_assign_cstr(&is
, (char *) buf
, len
)));
1040 } else if (!EG(exception
)){
1041 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1042 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1046 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1051 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1054 if (IS_ARRAY
== Z_TYPE(rv
)) {
1056 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, fn
? "C" : "O", 1)));
1057 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1058 php_ion_serialize_zval(ser
, &rv
);
1061 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1062 "%s serializer %s::%s did not return an array",
1063 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1064 fn
? fn
->common
.function_name
->val
: "__serialize");
1068 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1071 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("E"))));
1073 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1074 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1075 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1078 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1082 if (zobject
->ce
!= zend_standard_class_def
) {
1083 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("c"))));
1084 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1086 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("o"))));
1090 ZVAL_OBJ(&zobj
, zobject
);
1091 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1093 php_ion_serialize_struct(ser
, props
, true);
1094 zend_release_properties(props
);
1096 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1097 "Could not get properties for serialization of class %s",
1098 zobject
->ce
->name
->val
);
1102 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1104 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1105 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1106 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1108 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1111 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1114 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1115 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1120 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1122 return ce
->__serialize
&& ser
->call_magic
;
1125 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1127 return !!ce
->serialize
;
1130 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1132 if (ser
->call_custom
) {
1133 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1138 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1141 zend_class_entry
*ce
= zobject
->ce
;
1144 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1145 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1146 "Serializing %s is not allowed", ce
->name
->val
);
1150 if (can_call_magic_serialize(ser
, ce
)) {
1151 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1152 } else if (can_call_iface_serialize(ser
, ce
)) {
1153 php_ion_serialize_object_iface(ser
, zobject
);
1154 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1155 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1156 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1157 php_ion_serialize_object_enum(ser
, zobject
);
1158 } else if (ce
== ce_Symbol
) {
1159 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1160 } else if (ce
== ce_Decimal
) {
1161 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1162 } else if (ce
== ce_Timestamp
) {
1164 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1165 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1166 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1167 } else if (ce
== ce_LOB
) {
1168 php_ion_serialize_object_lob(ser
, zobject
);
1170 php_ion_serialize_object_std(ser
, zobject
);
1174 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1176 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1179 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1180 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1182 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("r"))));
1183 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1187 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1188 zend_hash_index_add(ser
->ids
, idx
, &num
);
1191 zend_hash_next_index_insert(ser
->tmp
, zv
);
1193 switch (Z_TYPE_P(zv
)) {
1195 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1199 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1203 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1207 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("R"))));
1208 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1214 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1218 switch (Z_TYPE_P(zv
)) {
1220 ION_CHECK(ion_writer_write_null(ser
->writer
));
1223 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1226 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1229 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1232 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1238 php_ion_serialize_refcounted(ser
, zv
);
1241 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1242 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1246 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1248 static inline void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1251 HashTable
*arr
= NULL
;
1253 if (ser
->multi_seq
) {
1254 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1255 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1256 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1260 arr
= Z_ARRVAL_P(zv
);
1262 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1263 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1267 /* start off with a global PHP annotation instead of repeating it all over the place */
1268 if (0 == php_ion_globals_serializer_step()) {
1270 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("PHP"))));
1272 php_ion_serialize_zval(ser
, zv
);
1273 php_ion_globals_serializer_exit();
1275 if (!ser
->multi_seq
) {
1278 zend_hash_move_forward_ex(arr
, &pos
);
1279 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1283 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1285 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1288 zo_ser
= create_ion_Serializer_PHP(NULL
);
1289 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1291 o_ser
->serializer
.call_magic
= true;
1292 php_ion_serializer_php_ctor(o_ser
);
1294 ser
= &o_ser
->serializer
;
1297 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1298 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1299 writer
->type
= BUFFER_WRITER
;
1302 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1303 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1306 php_ion_writer_ctor(writer
);
1307 ser
->writer
= writer
->writer
;
1308 ser
->buffer
= &writer
->buffer
.str
;
1310 if (!EG(exception
)) {
1311 php_ion_serialize_ex(ser
, zv
);
1314 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1315 ion_writer_flush(ser
->writer
, NULL
);
1316 RETVAL_STR_COPY(ser
->buffer
->s
);
1318 OBJ_RELEASE(zo_writer
);
1320 OBJ_RELEASE(zo_opt
);
1323 OBJ_RELEASE(zo_ser
);
1327 typedef struct php_ion_unserializer_php
{
1328 php_ion_unserializer unserializer
;
1329 zend_object
*opt
, std
;
1330 } php_ion_unserializer_php
;
1332 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1334 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1335 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1336 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1337 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1339 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1340 ser_obj
->unserializer
.call_magic
);
1341 if (ser_obj
->unserializer
.call_custom
) {
1342 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1343 ser_obj
->unserializer
.call_custom
);
1344 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1346 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1349 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1350 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1351 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1353 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1357 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1359 if (obj
->unserializer
.call_custom
) {
1360 zend_string_release(obj
->unserializer
.call_custom
);
1364 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1366 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1368 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1371 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1373 return (ce
&& ce
->unserialize
);
1376 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1378 if (ser
->call_custom
) {
1379 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1384 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1386 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1389 object_init_ex(return_value
, ce
);
1390 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1393 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1394 "Could not find class %s", ser
->annotations
.object_class
->val
);
1398 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1400 zend_string
*zs_case
= zval_get_string(return_value
);
1403 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1404 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1405 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1406 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1409 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1410 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1411 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1414 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1415 zend_hash_next_index_insert(ser
->ids
, return_value
);
1416 zend_string_release(zs_case
);
1419 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1421 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1422 if (can_call_iface_unserialize(ser
, ce
)) {
1423 zend_string
*s
= zval_get_string(return_value
);
1424 ZVAL_NULL(return_value
);
1425 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1426 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1427 RETVAL_ZVAL(backref
, 0, 0);
1428 } else if (!EG(exception
)) {
1429 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1430 "Failed to unserialize class %s", ce
->name
->val
);
1432 zend_string_release(s
);
1434 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1435 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1439 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1441 // FIXME: symbol table
1443 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1446 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1447 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1448 name
= is_ptr
->value
;
1449 } else if (is_ptr
) {
1450 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1451 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1453 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1457 *key
= zend_string_from_ion(&name
);
1460 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1462 zend_hash_next_index_insert(ser
->ids
, return_value
);
1464 ION_CHECK(ion_reader_step_in(ser
->reader
));
1468 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1470 if (typ
== tid_EOF
) {
1475 php_ion_unserialize_field_name(ser
, &key
);
1479 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1480 ION_CATCH(zend_string_release(key
));
1482 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1483 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1484 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1486 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1487 zval_ptr_dtor(&zvalue
);
1488 zend_string_release(key
);
1491 ION_CHECK(ion_reader_step_out(ser
->reader
));
1494 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1496 zend_hash_next_index_insert(ser
->ids
, return_value
);
1498 ION_CHECK(ion_reader_step_in(ser
->reader
));
1502 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1504 if (typ
== tid_EOF
) {
1509 php_ion_unserialize_field_name(ser
, &key
);
1513 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1514 ION_CATCH(zend_string_release(key
));
1516 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1518 zend_string_release(key
);
1521 ION_CHECK(ion_reader_step_out(ser
->reader
));
1524 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1526 switch (ser
->annotations
.object_type
) {
1532 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1533 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1534 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1539 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1540 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1541 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1543 *fn
= zobject
->ce
->__unserialize
;
1547 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1548 "Invalid object type %c", ser
->annotations
.object_type
);
1552 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1554 // backup possible backref to array returned by magic/custom __serialize()
1555 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1557 php_ion_unserialize_class(ser
, return_value
);
1560 zend_function
*fn
= NULL
;
1561 zend_object
*zobject
= Z_OBJ_P(return_value
);
1562 verify_unserializer(ser
, zobject
, &fn
);
1567 php_ion_unserialize_props(ser
, return_value
);
1572 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1573 zval_ptr_dtor(input
);
1575 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1576 php_ion_unserialize_hash(ser
, input
);
1581 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1585 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1587 if (ser
->annotations
.object_class
) {
1588 switch (ser
->annotations
.object_type
) {
1590 php_ion_unserialize_object_iface(ser
, return_value
);
1593 php_ion_unserialize_object_enum(ser
, return_value
);
1596 php_ion_unserialize_object(ser
, return_value
);
1598 } else if (!ser
->annotations
.object_type
) {
1599 array_init(return_value
);
1600 php_ion_unserialize_hash(ser
, return_value
);
1601 } else if (ser
->annotations
.object_type
== 'o') {
1602 object_init(return_value
);
1603 php_ion_unserialize_hash(ser
, return_value
);
1605 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1606 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1610 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1612 ION_CHECK(ion_reader_step_in(ser
->reader
));
1613 array_init(return_value
);
1614 zend_hash_next_index_insert(ser
->ids
, return_value
);
1618 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1620 if (typ
== tid_EOF
) {
1625 php_ion_unserialize_zval(ser
, &next
, &typ
);
1628 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1631 ION_CHECK(ion_reader_step_out(ser
->reader
));
1634 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1636 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1639 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1640 if (err
== IERR_BUFFER_TOO_SMALL
) {
1641 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1644 ION_CHECK(err
, zend_string_release(zstr
));
1645 if (zstr
->len
> read
) {
1646 zstr
->val
[read
] = 0;
1647 zstr
= zend_string_truncate(zstr
, read
, 0);
1652 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1655 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1657 object_init_ex(return_value
, ce_Timestamp
);
1658 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1660 zend_string
*fmt
= NULL
;
1661 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1662 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1663 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1664 zend_string_release(fmt
);
1669 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1671 ION_INT
*num
= NULL
;
1672 ION_CHECK(ion_int_alloc(reader
, &num
));
1673 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1675 // TODO: SIZEOF_ZEND_LONG == 4
1677 iERR err
= ion_int_to_int64(num
, &i64
);
1683 case IERR_NUMERIC_OVERFLOW
:
1685 ION_CHECK(ion_int_char_length(num
, &max
));
1686 zend_string
*zs
= zend_string_alloc(max
, 0);
1688 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1689 zs
->val
[zs
->len
= len
] = 0;
1700 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1702 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1705 ZVAL_COPY_VALUE(return_value
, backref
);
1706 zend_hash_next_index_insert(ser
->addref
, return_value
);
1708 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1709 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1713 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1715 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1718 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1719 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1721 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1723 if (ann_str
.length
!= 1) {
1727 switch (*ann_str
.value
) {
1729 if (ser
->annotations
.makeref
) {
1730 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1731 "Invalid multiple reference annotations");
1734 ser
->annotations
.makeref
= true;
1738 if (ser
->annotations
.backref
) {
1739 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1740 "Invalid multiple back reference annotations");
1743 ser
->annotations
.backref
= true;
1747 if (ser
->annotations
.object_prop
) {
1748 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1749 "Invalid multiple object property annotations");
1752 ser
->annotations
.object_prop
= true;
1754 ION_STRING prop_class
;
1755 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1756 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1759 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1760 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1769 if (ser
->annotations
.object_type
) {
1770 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1771 "Invalid multiple object type annotations: %c::%c",
1772 ser
->annotations
.object_type
, *ann_str
.value
);
1775 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1776 ION_STRING class_name
;
1777 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1778 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1781 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1782 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1788 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1789 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1790 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1793 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1794 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1795 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1801 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1804 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1807 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1810 php_ion_unserialize_annotations(ser
);
1813 if (ser
->annotations
.makeref
) {
1814 ZVAL_MAKE_REF(return_value
);
1815 zend_hash_next_index_insert(ser
->ids
, return_value
);
1816 ZVAL_DEREF(return_value
);
1819 if (ION_TYPE_INT(*typ
) > 0) {
1821 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
1827 switch (ION_TYPE_INT(*typ
)) {
1829 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1834 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1838 php_ion_reader_read_int(ser
->reader
, return_value
);
1839 if (ser
->annotations
.backref
) {
1841 php_ion_unserialize_backref(ser
, return_value
);
1843 if (ser
->annotations
.object_type
) {
1844 if (!ser
->annotations
.backref
) {
1845 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1846 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1847 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1850 goto unserialize_struct
;
1856 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1859 case tid_DECIMAL_INT
:
1860 object_init_ex(return_value
, ce_Decimal
);
1861 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1862 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1863 php_ion_decimal_ctor(dec
);
1864 zend_hash_next_index_insert(ser
->ids
, return_value
);
1867 case tid_TIMESTAMP_INT
:
1868 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1869 zend_hash_next_index_insert(ser
->ids
, return_value
);
1872 case tid_SYMBOL_INT
:
1874 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1875 php_ion_symbol_zval(&sym
, return_value
);
1876 if (ser
->annotations
.object_type
) {
1877 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1878 goto unserialize_struct
;
1880 zend_hash_next_index_insert(ser
->ids
, return_value
);
1883 case tid_STRING_INT
:
1885 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1886 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1887 if (ser
->annotations
.object_type
) {
1888 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1889 goto unserialize_struct
;
1891 zend_hash_next_index_insert(ser
->ids
, return_value
);
1896 php_ion_reader_read_lob(ser
->reader
, return_value
);
1897 if (ser
->annotations
.object_type
) {
1898 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1899 goto unserialize_struct
;
1901 zend_hash_next_index_insert(ser
->ids
, return_value
);
1905 case tid_SEXP_INT
: // FIXME
1906 php_ion_unserialize_list(ser
, return_value
);
1907 if (!ser
->annotations
.object_type
) {
1912 case tid_STRUCT_INT
:
1913 unserialize_struct
: ;
1914 php_ion_unserialize_struct(ser
, return_value
);
1921 case tid_DATAGRAM_INT
:
1922 ZEND_ASSERT(!"datagram");
1928 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1930 static inline void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
1932 if (ser
->multi_seq
) {
1933 array_init(return_value
);
1939 php_ion_globals_unserializer_step();
1940 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
1941 php_ion_globals_unserializer_exit();
1942 ION_CATCH(zval_ptr_dtor(&tmp
));
1944 if (!ser
->multi_seq
) {
1945 RETURN_COPY_VALUE(&tmp
);
1946 } else if (ser
->type
!= tid_EOF
) {
1947 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
1949 } while (ser
->type
!= tid_EOF
);
1952 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1954 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1957 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1958 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1960 o_ser
->unserializer
.call_magic
= true;
1961 php_ion_unserializer_php_ctor(o_ser
);
1963 ser
= &o_ser
->unserializer
;
1966 zend_object
*zo_reader
;
1967 php_ion_reader
*reader
;
1970 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
1971 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
1972 reader
= php_ion_obj(reader
, zo_reader
);
1973 reader
->type
= STREAM_READER
;
1974 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
1975 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
1976 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
1977 reader
= php_ion_obj(reader
, zo_reader
);
1978 reader
->type
= BUFFER_READER
;
1979 reader
->buffer
= zval_get_string(zdata
);
1981 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1982 "Invalid source to unserialize; expected string or resource");
1984 OBJ_RELEASE(zo_ser
);
1990 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
1991 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
1994 php_ion_reader_ctor(reader
);
1995 ser
->reader
= reader
->reader
;
1997 if (!EG(exception
)) {
1998 php_ion_unserialize_ex(ser
, return_value
);
2001 OBJ_RELEASE(zo_reader
);
2003 OBJ_RELEASE(zo_opt
);
2006 OBJ_RELEASE(zo_ser
);