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
179 *ce_Reader_Buffer_Reader
,
180 *ce_Reader_Stream_Reader
,
184 *ce_Symbol_ImportLocation
,
186 *ce_Symbol_System_SID
,
189 *ce_Timestamp_Precision
,
192 *ce_Unserializer_PHP
,
196 *ce_Writer_Buffer_Writer
,
198 *ce_Writer_Stream_Writer
,
202 #define php_ion_decl(type, cname, ...) \
203 static zend_object_handlers oh_ ## cname; \
204 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
206 if (!ce) ce = ce_ ## cname; \
207 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
208 zend_object_std_init(&o->std, ce); \
209 object_properties_init(&o->std, ce); \
210 o->std.handlers = &oh_ ## cname; \
213 static inline void free_ion_ ## cname(zend_object *std) \
215 php_ion_ ## type *obj = php_ion_obj(type, std); \
217 zend_object_std_dtor(std); \
220 #define php_ion_register(type, cname, ...) do { \
221 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
222 ce_ ## cname ->create_object = create_ion_ ## cname; \
223 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
224 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
225 oh_ ## cname .free_obj = free_ion_ ## cname; \
228 #define php_ion_obj(type, obj) \
229 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
231 #define ION_CHECK(err, ...) do { \
234 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
240 #define ION_CATCH(...) do { \
241 if (EG(exception)) { \
247 #define PTR_CHECK(ptr, ...) do { \
249 zend_throw_error(NULL, "Uninitialized object"); \
255 #define OBJ_CHECK(obj) do { \
257 PTR_CHECK(*((void **)obj)); \
260 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
262 is
->length
= zs
? zs
->len
: 0;
263 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
267 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
269 return zend_string_init((const char *) s
->value
, s
->length
, 0);
272 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
276 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
279 typedef struct php_ion_type
{
284 php_ion_decl(type
, Type
);
286 #define RETURN_IONTYPE(typ) do { \
287 zend_object *__zo = php_ion_type_fetch(typ); \
288 if (UNEXPECTED(!__zo)) { \
291 RETURN_OBJ_COPY(__zo); \
294 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
296 zend_long index
= ION_TYPE_INT(typ
);
297 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
299 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
300 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
303 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
306 typedef struct php_ion_symbol_iloc
{
307 ION_SYMBOL_IMPORT_LOCATION loc
;
310 } php_ion_symbol_iloc
;
312 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
314 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
315 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
316 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
319 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
321 zend_string_release(obj
->name
);
324 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
326 typedef struct php_ion_symbol
{
329 zend_object
*iloc
, std
;
332 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
334 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
337 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
339 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
341 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
343 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
344 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
346 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
350 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
353 zend_string_release(obj
->value
);
357 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
359 object_init_ex(return_value
, ce_Symbol
);
360 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
362 sym
->sym
.sid
= sym_ptr
->sid
;
363 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
364 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
366 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
367 sym
->iloc
= Z_OBJ(ziloc
);
369 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
370 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
371 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
373 php_ion_symbol_iloc_ctor(iloc
);
376 php_ion_symbol_ctor(sym
);
378 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
379 GC_DELREF(sym
->iloc
);
383 php_ion_decl(symbol
, Symbol
, php_ion_symbol_dtor(obj
));
385 typedef struct php_ion_symbol_table
{
387 } php_ion_symbol_table
;
389 php_ion_decl(symbol_table
, Symbol_Table
);
391 typedef struct php_ion_decimal_ctx
{
394 } php_ion_decimal_ctx
;
396 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
) {
397 zval tmp
, *zbits
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), 1, &tmp
);
400 if (zbits
!= &EG(uninitialized_zval
)) {
401 bits
= Z_LVAL_P(zbits
);
403 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), bits
);
409 decContextDefault(&obj
->ctx
, bits
);
412 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
413 "Decimal context only allows 32, 64 or 128 bits");
417 php_ion_decl(decimal_ctx
, Decimal_Context
);
419 typedef struct php_ion_decimal
{
421 zend_object
*ctx
, std
;
424 static inline void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
426 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
427 ION_CHECK(ion_decimal_from_int32(dec
, num
));
428 } else if (num
> 0 && num
<= UINT32_MAX
) {
429 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
432 ION_CHECK(ion_int_alloc(NULL
, &iint
));
433 ION_CHECK(ion_int_from_long(iint
, num
),
435 /* WATCH OUT: BS API */
436 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
437 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
443 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
445 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
446 (void) ion_decimal_to_string(dec
, zstr
->val
);
447 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
450 static inline void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
453 ION_CHECK(ion_int_alloc(NULL
, &ii
));
454 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
456 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
461 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
465 if (!ion_decimal_is_integer(&obj
->dec
)) {
470 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_max
, &g_dec_ctx
, &result
);
475 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_min
, &g_dec_ctx
, &result
);
482 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
486 object_init_ex(&zdc
, ce_Decimal_Context
);
487 obj
->ctx
= Z_OBJ(zdc
);
488 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
));
491 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
493 if (php_ion_decimal_fits_zend_long(obj
)) {
495 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
496 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
498 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
499 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
500 zend_string_release(zstr
);
504 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
506 ion_decimal_free(&obj
->dec
);
509 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
511 typedef php_date_obj php_ion_timestamp
;
513 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
515 decQuad microsecs
, result
;
516 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
517 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
520 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
522 decQuad microsecs
, us
;
523 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
526 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
530 return zend_string_init(ZEND_STRL("c"), 0);
532 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
534 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
536 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
538 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
540 return zend_string_init(ZEND_STRL("Y\\T"), 0);
542 return zend_string_init(ZEND_STRL("c"), 0);
546 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
548 timelib_time
*time
= timelib_time_ctor();
550 switch (ts
->precision
) {
552 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
555 time
->s
= ts
->seconds
;
558 time
->i
= ts
->minutes
;
571 time
->z
= ts
->tz_offset
* 60;
575 *fmt
= php_dt_format_from_precision(ts
->precision
);
580 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
582 memset(buf
, 0, sizeof(*buf
));
585 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
587 if (!precision
|| precision
> ION_TS_FRAC
) {
588 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
589 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
590 } else switch ((buf
->precision
= precision
)) {
592 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
595 buf
->seconds
= ts
->time
->s
;
598 buf
->minutes
= ts
->time
->i
;
601 buf
->hours
= ts
->time
->h
;
602 buf
->day
= ts
->time
->d
;
605 buf
->month
= ts
->time
->m
;
608 buf
->year
= ts
->time
->y
;
611 buf
->tz_offset
= ts
->time
->z
/ 60;
617 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
620 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
622 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
624 fmt
= php_dt_format_from_precision(precision
);
625 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
626 zend_string_release(fmt
);
629 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
632 timelib_time_dtor(obj
->time
);
636 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
638 typedef struct php_ion_catalog
{
643 php_ion_decl(catalog
, Catalog
);
645 typedef struct php_ion_reader_options
{
646 ION_READER_OPTIONS opt
;
647 zend_object
*cat
, *dec_ctx
, *cb
, std
;
648 } php_ion_reader_options
;
650 php_ion_decl(reader_options
, Reader_Options
);
652 typedef struct php_ion_reader
{
666 zend_object
*opt
, std
;
669 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
671 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
672 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
674 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
675 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
676 user
->limit
-= remaining
;
679 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
682 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
683 if (EXPECTED(read
> 0)) {
688 if (EXPECTED(read
== 0)) {
692 return IERR_READ_ERROR
;
695 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
698 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
704 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
709 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
712 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
715 opt
->opt
.context_change_notifier
.context
= obj
;
717 if (obj
->type
== STREAM_READER
) {
718 PTR_CHECK(obj
->stream
.ptr
);
719 GC_ADDREF(obj
->stream
.ptr
->res
);
721 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
722 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
723 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
724 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
727 err
= ion_reader_open_buffer(&obj
->reader
,
728 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
729 opt
? &opt
->opt
: NULL
);
732 opt
->opt
.context_change_notifier
.context
= NULL
;
738 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
741 ion_reader_close(obj
->reader
);
743 if (obj
->type
== STREAM_READER
) {
744 if (obj
->stream
.buf
.value
) {
745 efree(obj
->stream
.buf
.value
);
747 if (obj
->stream
.ptr
) {
748 zend_list_delete(obj
->stream
.ptr
->res
);
752 zend_string_release(obj
->buffer
);
757 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
759 typedef struct php_ion_writer_options
{
760 ION_WRITER_OPTIONS opt
;
761 zend_object
*cat
, *dec_ctx
, *col
, std
;
762 } php_ion_writer_options
;
764 php_ion_decl(writer_options
, Writer_Options
);
766 typedef struct php_ion_writer
{
782 zend_object
*opt
, std
;
786 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
788 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
790 if (EXPECTED(user
->limit
&& user
->curr
)) {
791 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
792 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
793 return IERR_WRITE_ERROR
;
796 user
->curr
= writer
->stream
.buf
.value
;
797 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
801 #define REF_STR() do { \
802 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
803 GC_ADDREF(obj->buffer.str.s); \
806 #define NEW_REF_STR() do {\
807 if (Z_STR_P(ref) != obj->buffer.str.s) { \
808 zval_ptr_dtor(ref); \
813 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
815 PTR_CHECK(obj
->stream
.ptr
);
816 GC_ADDREF(obj
->stream
.ptr
->res
);
818 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
819 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
821 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
823 zval
*ref
= &obj
->buffer
.val
;
826 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
827 smart_str_0(&obj
->buffer
.str
);
831 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
833 zval
*ref
= &obj
->buffer
.val
;
836 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
841 // we've been separated
842 GC_ADDREF(obj
->buffer
.str
.s
);
845 // we have to separate
846 fprintf(stderr
, "SEPARATE\n");
847 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
851 zend_string
*old
= obj
->buffer
.str
.s
;
853 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
854 if (old
== obj
->buffer
.str
.s
) {
856 } else if(old
== Z_STR_P(ref
)) {
863 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
865 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
868 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
869 smart_str_0(&writer
->buffer
.str
);
870 if (user
->limit
== user
->curr
) {
871 php_ion_writer_buffer_grow(writer
);
874 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
875 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
880 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
883 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
886 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
887 ION_STREAM_HANDLER h
;
888 if (obj
->type
== STREAM_WRITER
) {
889 h
= php_ion_writer_stream_handler
;
890 php_ion_writer_stream_init(obj
, opt
);
892 h
= php_ion_writer_buffer_handler
;
893 php_ion_writer_buffer_init(obj
);
896 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
900 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
903 ion_writer_close(obj
->writer
);
905 if (obj
->type
== STREAM_WRITER
) {
906 if (obj
->stream
.buf
.value
) {
907 efree(obj
->stream
.buf
.value
);
909 if (obj
->stream
.ptr
) {
910 zend_list_delete(obj
->stream
.ptr
->res
);
913 if (obj
->buffer
.str
.s
) {
914 smart_str_0(&obj
->buffer
.str
);
915 zend_string_release(obj
->buffer
.str
.s
);
917 zval_ptr_dtor(&obj
->buffer
.val
);
921 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
923 typedef struct php_ion_serializer_php
{
924 php_ion_serializer serializer
;
925 zend_object
*opt
, std
;
926 } php_ion_serializer_php
;
928 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
930 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
931 ser_obj
->serializer
.ids
= global_ser
->ids
;
932 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
934 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
935 ser_obj
->serializer
.call_magic
);
936 if (ser_obj
->serializer
.call_custom
) {
937 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
938 ser_obj
->serializer
.call_custom
);
939 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
941 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
944 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
945 ser_obj
->serializer
.options
= &o_woptions
->opt
;
946 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
948 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
952 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
954 if (obj
->serializer
.call_custom
) {
955 zend_string_release(obj
->serializer
.call_custom
);
959 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
961 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
963 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
967 zend_string
*k
= NULL
;
968 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
972 const char *class_name
, *prop_name
;
973 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
974 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("p"))));
975 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
980 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, (char *) prop_name
, prop_len
)));
982 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
983 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
984 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, ptr
, end
- ptr
)));
987 php_ion_serialize_zval(ser
, v
);
989 ZEND_HASH_FOREACH_END();
991 ION_CHECK(ion_writer_finish_container(ser
->writer
));
994 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
996 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
999 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1000 php_ion_serialize_zval(ser
, v
);
1002 ZEND_HASH_FOREACH_END();
1004 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1007 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1009 if (zend_array_is_list(arr
)) {
1010 php_ion_serialize_list(ser
, arr
);
1012 php_ion_serialize_struct(ser
, arr
, false);
1016 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1022 ZVAL_OBJ(&tmp
, zobject
);
1023 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1025 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("S"))));
1026 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1027 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_assign_cstr(&is
, (char *) buf
, len
)));
1029 } else if (!EG(exception
)){
1030 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1031 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1035 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1040 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1043 if (IS_ARRAY
== Z_TYPE(rv
)) {
1045 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, fn
? "C" : "O", 1)));
1046 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1047 php_ion_serialize_zval(ser
, &rv
);
1050 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1051 "%s serializer %s::%s did not return an array",
1052 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1053 fn
? fn
->common
.function_name
->val
: "__serialize");
1057 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1060 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("E"))));
1062 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1063 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1064 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1067 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1071 if (zobject
->ce
!= zend_standard_class_def
) {
1072 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("c"))));
1073 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1075 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("o"))));
1079 ZVAL_OBJ(&zobj
, zobject
);
1080 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1082 php_ion_serialize_struct(ser
, props
, true);
1083 zend_release_properties(props
);
1085 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1086 "Could not get properties for serialization of class %s",
1087 zobject
->ce
->name
->val
);
1091 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1093 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1094 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1095 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1097 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1100 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1103 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1104 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1109 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1111 return ce
->__serialize
&& ser
->call_magic
;
1114 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1116 return !!ce
->serialize
;
1119 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1121 if (ser
->call_custom
) {
1122 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1127 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1130 zend_class_entry
*ce
= zobject
->ce
;
1133 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1134 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1135 "Serializing %s is not allowed", ce
->name
->val
);
1139 if (can_call_magic_serialize(ser
, ce
)) {
1140 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1141 } else if (can_call_iface_serialize(ser
, ce
)) {
1142 php_ion_serialize_object_iface(ser
, zobject
);
1143 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1144 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1145 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1146 php_ion_serialize_object_enum(ser
, zobject
);
1147 } else if (ce
== ce_Symbol
) {
1148 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1149 } else if (ce
== ce_Decimal
) {
1150 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1151 } else if (ce
== ce_Timestamp
) {
1153 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1154 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1155 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1156 } else if (ce
== ce_LOB
) {
1157 php_ion_serialize_object_lob(ser
, zobject
);
1159 php_ion_serialize_object_std(ser
, zobject
);
1163 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1165 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1168 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1169 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1171 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("r"))));
1172 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1176 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1177 zend_hash_index_add(ser
->ids
, idx
, &num
);
1180 zend_hash_next_index_insert(ser
->tmp
, zv
);
1182 switch (Z_TYPE_P(zv
)) {
1184 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1188 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1192 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1196 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("R"))));
1197 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1203 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1207 switch (Z_TYPE_P(zv
)) {
1209 ION_CHECK(ion_writer_write_null(ser
->writer
));
1212 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1215 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1218 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1221 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1227 php_ion_serialize_refcounted(ser
, zv
);
1230 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1231 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1235 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1237 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1239 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1242 zo_ser
= create_ion_Serializer_PHP(NULL
);
1243 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1245 o_ser
->serializer
.call_magic
= true;
1246 php_ion_serializer_php_ctor(o_ser
);
1248 ser
= &o_ser
->serializer
;
1251 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1252 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1253 writer
->type
= BUFFER_WRITER
;
1256 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1257 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1260 php_ion_writer_ctor(writer
);
1261 ser
->writer
= writer
->writer
;
1262 ser
->buffer
= &writer
->buffer
.str
;
1264 /* start off with a global PHP annotation instead of repeating it all over the place */
1265 if (0 == php_ion_globals_serializer_step()) {
1267 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("PHP"))),
1268 if (zo_ser
) OBJ_RELEASE(zo_ser
));
1270 php_ion_serialize_zval(ser
, zv
);
1271 php_ion_globals_serializer_exit();
1273 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1274 ion_writer_flush(ser
->writer
, NULL
);
1275 RETVAL_STR_COPY(ser
->buffer
->s
);
1277 OBJ_RELEASE(zo_writer
);
1279 OBJ_RELEASE(zo_opt
);
1282 OBJ_RELEASE(zo_ser
);
1286 typedef struct php_ion_unserializer_php
{
1287 php_ion_unserializer unserializer
;
1288 zend_object
*opt
, std
;
1289 } php_ion_unserializer_php
;
1291 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1293 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1294 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1295 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1296 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1298 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1299 ser_obj
->unserializer
.call_magic
);
1300 if (ser_obj
->unserializer
.call_custom
) {
1301 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1302 ser_obj
->unserializer
.call_custom
);
1303 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1305 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1308 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1309 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1310 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1312 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1316 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1318 if (obj
->unserializer
.call_custom
) {
1319 zend_string_release(obj
->unserializer
.call_custom
);
1323 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1325 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1327 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1330 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1332 return (ce
&& ce
->unserialize
);
1335 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1337 if (ser
->call_custom
) {
1338 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1343 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1345 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1348 object_init_ex(return_value
, ce
);
1349 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1352 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1353 "Could not find class %s", ser
->annotations
.object_class
->val
);
1357 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1359 zend_string
*zs_case
= zval_get_string(return_value
);
1362 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1363 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1364 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1365 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1368 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1369 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1370 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1373 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1374 zend_hash_next_index_insert(ser
->ids
, return_value
);
1375 zend_string_release(zs_case
);
1378 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1380 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1381 if (can_call_iface_unserialize(ser
, ce
)) {
1382 zend_string
*s
= zval_get_string(return_value
);
1383 ZVAL_NULL(return_value
);
1384 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1385 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1386 RETVAL_ZVAL(backref
, 0, 0);
1387 } else if (!EG(exception
)) {
1388 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1389 "Failed to unserialize class %s", ce
->name
->val
);
1391 zend_string_release(s
);
1393 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1394 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1398 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1400 // FIXME: symbol table
1402 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1405 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1406 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1407 name
= is_ptr
->value
;
1408 } else if (is_ptr
) {
1409 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1410 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1412 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1416 *key
= zend_string_from_ion(&name
);
1419 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1421 zend_hash_next_index_insert(ser
->ids
, return_value
);
1423 ION_CHECK(ion_reader_step_in(ser
->reader
));
1427 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1429 if (typ
== tid_EOF
) {
1434 php_ion_unserialize_field_name(ser
, &key
);
1438 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1439 ION_CATCH(zend_string_release(key
));
1441 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1442 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1443 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1445 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1446 zval_ptr_dtor(&zvalue
);
1447 zend_string_release(key
);
1450 ION_CHECK(ion_reader_step_out(ser
->reader
));
1453 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1455 zend_hash_next_index_insert(ser
->ids
, return_value
);
1457 ION_CHECK(ion_reader_step_in(ser
->reader
));
1461 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1463 if (typ
== tid_EOF
) {
1468 php_ion_unserialize_field_name(ser
, &key
);
1472 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1473 ION_CATCH(zend_string_release(key
));
1475 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1477 zend_string_release(key
);
1480 ION_CHECK(ion_reader_step_out(ser
->reader
));
1483 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1485 switch (ser
->annotations
.object_type
) {
1491 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1492 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1493 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1498 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1499 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1500 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1502 *fn
= zobject
->ce
->__unserialize
;
1506 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1507 "Invalid object type %c", ser
->annotations
.object_type
);
1511 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1513 // backup possible backref to array returned by magic/custom __serialize()
1514 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1516 php_ion_unserialize_class(ser
, return_value
);
1519 zend_function
*fn
= NULL
;
1520 zend_object
*zobject
= Z_OBJ_P(return_value
);
1521 verify_unserializer(ser
, zobject
, &fn
);
1526 php_ion_unserialize_props(ser
, return_value
);
1531 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1532 zval_ptr_dtor(input
);
1534 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1535 php_ion_unserialize_hash(ser
, input
);
1540 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1544 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1546 if (ser
->annotations
.object_class
) {
1547 switch (ser
->annotations
.object_type
) {
1549 php_ion_unserialize_object_iface(ser
, return_value
);
1552 php_ion_unserialize_object_enum(ser
, return_value
);
1555 php_ion_unserialize_object(ser
, return_value
);
1557 } else if (!ser
->annotations
.object_type
) {
1558 array_init(return_value
);
1559 php_ion_unserialize_hash(ser
, return_value
);
1560 } else if (ser
->annotations
.object_type
== 'o') {
1561 object_init(return_value
);
1562 php_ion_unserialize_hash(ser
, return_value
);
1564 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1565 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1569 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1571 ION_CHECK(ion_reader_step_in(ser
->reader
));
1572 array_init(return_value
);
1573 zend_hash_next_index_insert(ser
->ids
, return_value
);
1577 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1579 if (typ
== tid_EOF
) {
1584 php_ion_unserialize_zval(ser
, &next
, &typ
);
1587 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1590 ION_CHECK(ion_reader_step_out(ser
->reader
));
1593 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1595 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1598 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1599 if (err
== IERR_BUFFER_TOO_SMALL
) {
1600 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1603 ION_CHECK(err
, zend_string_release(zstr
));
1604 if (zstr
->len
> read
) {
1605 zstr
->val
[read
] = 0;
1606 zstr
= zend_string_truncate(zstr
, read
, 0);
1611 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1614 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1616 object_init_ex(return_value
, ce_Timestamp
);
1617 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1619 zend_string
*fmt
= NULL
;
1620 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1621 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1622 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1623 zend_string_release(fmt
);
1628 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1630 ION_INT
*num
= NULL
;
1631 ION_CHECK(ion_int_alloc(reader
, &num
));
1632 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1634 // TODO: SIZEOF_ZEND_LONG == 4
1636 iERR err
= ion_int_to_int64(num
, &i64
);
1642 case IERR_NUMERIC_OVERFLOW
:
1644 ION_CHECK(ion_int_char_length(num
, &max
));
1645 zend_string
*zs
= zend_string_alloc(max
, 0);
1647 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1648 zs
->val
[zs
->len
= len
] = 0;
1659 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1661 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1664 ZVAL_COPY_VALUE(return_value
, backref
);
1665 zend_hash_next_index_insert(ser
->addref
, return_value
);
1667 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1668 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1672 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1674 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1677 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1678 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1680 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1682 if (ann_str
.length
!= 1) {
1686 switch (*ann_str
.value
) {
1688 if (ser
->annotations
.makeref
) {
1689 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1690 "Invalid multiple reference annotations");
1693 ser
->annotations
.makeref
= true;
1697 if (ser
->annotations
.backref
) {
1698 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1699 "Invalid multiple back reference annotations");
1702 ser
->annotations
.backref
= true;
1706 if (ser
->annotations
.object_prop
) {
1707 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1708 "Invalid multiple object property annotations");
1711 ser
->annotations
.object_prop
= true;
1713 ION_STRING prop_class
;
1714 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1715 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1718 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1719 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1728 if (ser
->annotations
.object_type
) {
1729 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1730 "Invalid multiple object type annotations: %c::%c",
1731 ser
->annotations
.object_type
, *ann_str
.value
);
1734 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1735 ION_STRING class_name
;
1736 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1737 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1740 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1741 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1747 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1748 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1749 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1752 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1753 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1754 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1760 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1763 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1766 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1769 php_ion_unserialize_annotations(ser
);
1772 if (ser
->annotations
.makeref
) {
1773 ZVAL_MAKE_REF(return_value
);
1774 zend_hash_next_index_insert(ser
->ids
, return_value
);
1775 ZVAL_DEREF(return_value
);
1778 if (ION_TYPE_INT(*typ
) > 0) {
1780 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
1786 switch (ION_TYPE_INT(*typ
)) {
1788 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1793 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1797 php_ion_reader_read_int(ser
->reader
, return_value
);
1798 if (ser
->annotations
.backref
) {
1800 php_ion_unserialize_backref(ser
, return_value
);
1802 if (ser
->annotations
.object_type
) {
1803 if (!ser
->annotations
.backref
) {
1804 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1805 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1806 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1809 goto unserialize_struct
;
1815 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1818 case tid_DECIMAL_INT
:
1819 object_init_ex(return_value
, ce_Decimal
);
1820 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1821 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1822 php_ion_decimal_ctor(dec
);
1823 zend_hash_next_index_insert(ser
->ids
, return_value
);
1826 case tid_TIMESTAMP_INT
:
1827 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1828 zend_hash_next_index_insert(ser
->ids
, return_value
);
1831 case tid_SYMBOL_INT
:
1833 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1834 php_ion_symbol_zval(&sym
, return_value
);
1835 if (ser
->annotations
.object_type
) {
1836 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1837 goto unserialize_struct
;
1839 zend_hash_next_index_insert(ser
->ids
, return_value
);
1842 case tid_STRING_INT
:
1844 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1845 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1846 if (ser
->annotations
.object_type
) {
1847 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1848 goto unserialize_struct
;
1850 zend_hash_next_index_insert(ser
->ids
, return_value
);
1855 php_ion_reader_read_lob(ser
->reader
, return_value
);
1856 if (ser
->annotations
.object_type
) {
1857 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1858 goto unserialize_struct
;
1860 zend_hash_next_index_insert(ser
->ids
, return_value
);
1864 case tid_SEXP_INT
: // FIXME
1865 php_ion_unserialize_list(ser
, return_value
);
1866 if (!ser
->annotations
.object_type
) {
1871 case tid_STRUCT_INT
:
1872 unserialize_struct
: ;
1873 php_ion_unserialize_struct(ser
, return_value
);
1880 case tid_DATAGRAM_INT
:
1881 ZEND_ASSERT(!"datagram");
1887 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1889 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1891 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1894 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1895 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1897 o_ser
->unserializer
.call_magic
= true;
1898 php_ion_unserializer_php_ctor(o_ser
);
1900 ser
= &o_ser
->unserializer
;
1903 zend_object
*zo_reader
;
1904 php_ion_reader
*reader
;
1907 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
1908 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
1909 reader
= php_ion_obj(reader
, zo_reader
);
1910 reader
->type
= STREAM_READER
;
1911 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
1912 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
1913 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
1914 reader
= php_ion_obj(reader
, zo_reader
);
1915 reader
->type
= BUFFER_READER
;
1916 reader
->buffer
= zval_get_string(zdata
);
1918 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1919 "Invalid source to unserialize; expected string or resource");
1921 OBJ_RELEASE(zo_ser
);
1927 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
1928 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
1931 php_ion_reader_ctor(reader
);
1932 ser
->reader
= reader
->reader
;
1935 php_ion_globals_unserializer_step();
1936 php_ion_unserialize_zval(ser
, return_value
, NULL
);
1937 php_ion_globals_unserializer_exit();
1939 OBJ_RELEASE(zo_reader
);
1941 OBJ_RELEASE(zo_opt
);
1944 OBJ_RELEASE(zo_ser
);