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_cstr(ION_STRING
*is
, const char *s
, size_t l
)
263 is
->value
= (BYTE
*) s
;
267 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
269 is
->length
= zs
? zs
->len
: 0;
270 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
274 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
276 return zend_string_init((const char *) s
->value
, s
->length
, 0);
279 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
283 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
286 typedef struct php_ion_type
{
291 php_ion_decl(type
, Type
);
293 #define RETURN_IONTYPE(typ) do { \
294 zend_object *__zo = php_ion_type_fetch(typ); \
295 if (UNEXPECTED(!__zo)) { \
298 RETURN_OBJ_COPY(__zo); \
301 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
303 zend_long index
= ION_TYPE_INT(typ
);
304 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
306 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
307 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
310 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
313 typedef struct php_ion_symbol_iloc
{
314 ION_SYMBOL_IMPORT_LOCATION loc
;
317 } php_ion_symbol_iloc
;
319 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
321 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
322 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
323 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
326 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
328 zend_string_release(obj
->name
);
331 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
333 typedef struct php_ion_symbol
{
336 zend_object
*iloc
, std
;
339 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
341 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
344 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
346 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
348 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
350 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
351 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
353 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
357 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
360 zend_string_release(obj
->value
);
364 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
366 object_init_ex(return_value
, ce_Symbol
);
367 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
369 sym
->sym
.sid
= sym_ptr
->sid
;
370 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
371 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
373 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
374 sym
->iloc
= Z_OBJ(ziloc
);
376 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
377 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
378 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
380 php_ion_symbol_iloc_ctor(iloc
);
383 php_ion_symbol_ctor(sym
);
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 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
) {
400 zval tmp
, *zbits
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), 1, &tmp
);
403 if (zbits
!= &EG(uninitialized_zval
)) {
404 bits
= Z_LVAL_P(zbits
);
406 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), bits
);
412 decContextDefault(&obj
->ctx
, bits
);
415 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
416 "Decimal context only allows 32, 64 or 128 bits");
420 php_ion_decl(decimal_ctx
, Decimal_Context
);
422 typedef struct php_ion_decimal
{
424 zend_object
*ctx
, std
;
427 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
429 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
430 (void) ion_decimal_to_string(dec
, zstr
->val
);
431 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
434 static inline void php_ion_decimal_to_int(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
437 ION_CHECK(ion_int_alloc(NULL
, &ii
));
438 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
440 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
445 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
449 object_init_ex(&zdc
, ce_Decimal_Context
);
450 obj
->ctx
= Z_OBJ(zdc
);
451 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
));
454 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
456 if (ion_decimal_is_integer(&obj
->dec
)) {
458 php_ion_decimal_to_int(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
459 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
461 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
462 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
463 zend_string_release(zstr
);
467 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
469 ion_decimal_free(&obj
->dec
);
472 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
474 typedef php_date_obj php_ion_timestamp
;
476 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
478 decQuad microsecs
, result
;
479 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
480 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
483 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
485 decQuad microsecs
, us
;
486 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
489 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
493 return zend_string_init(ZEND_STRL("c"), 0);
495 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
497 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
499 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
501 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
503 return zend_string_init(ZEND_STRL("Y\\T"), 0);
505 return zend_string_init(ZEND_STRL("c"), 0);
509 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
511 timelib_time
*time
= timelib_time_ctor();
513 switch (ts
->precision
) {
515 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
518 time
->s
= ts
->seconds
;
521 time
->i
= ts
->minutes
;
534 time
->z
= ts
->tz_offset
* 60;
538 *fmt
= php_dt_format_from_precision(ts
->precision
);
543 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
545 memset(buf
, 0, sizeof(*buf
));
548 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
550 if (!precision
|| precision
> ION_TS_FRAC
) {
551 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
552 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
553 } else switch ((buf
->precision
= precision
)) {
555 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
558 buf
->seconds
= ts
->time
->s
;
561 buf
->minutes
= ts
->time
->i
;
564 buf
->hours
= ts
->time
->h
;
565 buf
->day
= ts
->time
->d
;
568 buf
->month
= ts
->time
->m
;
571 buf
->year
= ts
->time
->y
;
574 buf
->tz_offset
= ts
->time
->z
/ 60;
580 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
583 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
585 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
587 fmt
= php_dt_format_from_precision(precision
);
588 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
589 zend_string_release(fmt
);
592 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
595 timelib_time_dtor(obj
->time
);
599 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
601 typedef struct php_ion_catalog
{
606 php_ion_decl(catalog
, Catalog
);
608 typedef struct php_ion_reader_options
{
609 ION_READER_OPTIONS opt
;
610 zend_object
*cat
, *dec_ctx
, *cb
, std
;
611 } php_ion_reader_options
;
613 php_ion_decl(reader_options
, Reader_Options
);
615 typedef struct php_ion_reader
{
629 zend_object
*opt
, std
;
632 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
634 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
635 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
637 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
638 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
639 user
->limit
-= remaining
;
642 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
645 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
646 if (EXPECTED(read
> 0)) {
651 if (EXPECTED(read
== 0)) {
655 return IERR_READ_ERROR
;
658 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
661 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
667 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
672 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
675 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
678 opt
->opt
.context_change_notifier
.context
= obj
;
680 if (obj
->type
== STREAM_READER
) {
681 PTR_CHECK(obj
->stream
.ptr
);
682 GC_ADDREF(obj
->stream
.ptr
->res
);
684 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
685 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
686 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
687 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
690 err
= ion_reader_open_buffer(&obj
->reader
,
691 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
692 opt
? &opt
->opt
: NULL
);
695 opt
->opt
.context_change_notifier
.context
= NULL
;
701 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
704 ion_reader_close(obj
->reader
);
706 if (obj
->type
== STREAM_READER
) {
707 if (obj
->stream
.buf
.value
) {
708 efree(obj
->stream
.buf
.value
);
710 if (obj
->stream
.ptr
) {
711 zend_list_delete(obj
->stream
.ptr
->res
);
715 zend_string_release(obj
->buffer
);
720 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
722 typedef struct php_ion_writer_options
{
723 ION_WRITER_OPTIONS opt
;
724 zend_object
*cat
, *dec_ctx
, *col
, std
;
725 } php_ion_writer_options
;
727 php_ion_decl(writer_options
, Writer_Options
);
729 typedef struct php_ion_writer
{
745 zend_object
*opt
, std
;
749 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
751 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
753 if (EXPECTED(user
->limit
&& user
->curr
)) {
754 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
755 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
756 return IERR_WRITE_ERROR
;
759 user
->curr
= writer
->stream
.buf
.value
;
760 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
764 #define REF_STR() do { \
765 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
766 GC_ADDREF(obj->buffer.str.s); \
769 #define NEW_REF_STR() do {\
770 if (Z_STR_P(ref) != obj->buffer.str.s) { \
771 zval_ptr_dtor(ref); \
776 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
778 PTR_CHECK(obj
->stream
.ptr
);
779 GC_ADDREF(obj
->stream
.ptr
->res
);
781 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
782 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
784 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
786 zval
*ref
= &obj
->buffer
.val
;
789 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
790 smart_str_0(&obj
->buffer
.str
);
794 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
796 zval
*ref
= &obj
->buffer
.val
;
799 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
804 // we've been separated
805 GC_ADDREF(obj
->buffer
.str
.s
);
808 // we have to separate
809 fprintf(stderr
, "SEPARATE\n");
810 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
814 zend_string
*old
= obj
->buffer
.str
.s
;
816 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
817 if (old
== obj
->buffer
.str
.s
) {
819 } else if(old
== Z_STR_P(ref
)) {
826 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
828 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
831 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
832 smart_str_0(&writer
->buffer
.str
);
833 if (user
->limit
== user
->curr
) {
834 php_ion_writer_buffer_grow(writer
);
837 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
838 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
843 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
846 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
849 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
850 ION_STREAM_HANDLER h
;
851 if (obj
->type
== STREAM_WRITER
) {
852 h
= php_ion_writer_stream_handler
;
853 php_ion_writer_stream_init(obj
, opt
);
855 h
= php_ion_writer_buffer_handler
;
856 php_ion_writer_buffer_init(obj
);
859 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
863 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
866 ion_writer_close(obj
->writer
);
868 if (obj
->type
== STREAM_WRITER
) {
869 if (obj
->stream
.buf
.value
) {
870 efree(obj
->stream
.buf
.value
);
872 if (obj
->stream
.ptr
) {
873 zend_list_delete(obj
->stream
.ptr
->res
);
876 if (obj
->buffer
.str
.s
) {
877 smart_str_0(&obj
->buffer
.str
);
878 zend_string_release(obj
->buffer
.str
.s
);
880 zval_ptr_dtor(&obj
->buffer
.val
);
884 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
886 typedef struct php_ion_serializer_php
{
887 php_ion_serializer serializer
;
888 zend_object
*opt
, std
;
889 } php_ion_serializer_php
;
891 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
893 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
894 ser_obj
->serializer
.ids
= global_ser
->ids
;
895 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
897 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
898 ser_obj
->serializer
.call_magic
);
899 if (ser_obj
->serializer
.call_custom
) {
900 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
901 ser_obj
->serializer
.call_custom
);
902 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
904 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
907 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
908 ser_obj
->serializer
.options
= &o_woptions
->opt
;
909 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
911 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
915 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
917 if (obj
->serializer
.call_custom
) {
918 zend_string_release(obj
->serializer
.call_custom
);
922 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
924 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
926 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
930 zend_string
*k
= NULL
;
931 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
935 const char *class_name
, *prop_name
;
936 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
937 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("p"))));
938 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, class_name
, prop_name
- class_name
- 1)));
943 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, prop_name
, prop_len
)));
945 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
946 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
947 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, ptr
, end
- ptr
)));
950 php_ion_serialize_zval(ser
, v
);
952 ZEND_HASH_FOREACH_END();
954 ION_CHECK(ion_writer_finish_container(ser
->writer
));
957 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
959 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
962 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
963 php_ion_serialize_zval(ser
, v
);
965 ZEND_HASH_FOREACH_END();
967 ION_CHECK(ion_writer_finish_container(ser
->writer
));
970 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
972 if (zend_array_is_list(arr
)) {
973 php_ion_serialize_list(ser
, arr
);
975 php_ion_serialize_struct(ser
, arr
, false);
979 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
985 ZVAL_OBJ(&tmp
, zobject
);
986 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
988 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("S"))));
989 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
990 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_cstr(&is
, (char *) buf
, len
)));
992 } else if (!EG(exception
)){
993 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
994 "Failed to serialize class %s", zobject
->ce
->name
->val
);
998 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1003 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1006 if (IS_ARRAY
== Z_TYPE(rv
)) {
1008 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, fn
? "C" : "O", 1)));
1009 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1010 php_ion_serialize_zval(ser
, &rv
);
1013 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1014 "%s serializer %s::%s did not return an array",
1015 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1016 fn
? fn
->common
.function_name
->val
: "__serialize");
1020 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1023 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("E"))));
1025 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1026 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1027 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1030 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1034 if (zobject
->ce
!= zend_standard_class_def
) {
1035 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("c"))));
1036 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1038 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("o"))));
1042 ZVAL_OBJ(&zobj
, zobject
);
1043 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1045 php_ion_serialize_struct(ser
, props
, true);
1046 zend_release_properties(props
);
1048 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1049 "Could not get properties for serialization of class %s",
1050 zobject
->ce
->name
->val
);
1054 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1056 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1057 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1058 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1060 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1063 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1066 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1067 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1072 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1074 return ce
->__serialize
&& ser
->call_magic
;
1077 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1079 return !!ce
->serialize
;
1082 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1084 if (ser
->call_custom
) {
1085 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1090 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1093 zend_class_entry
*ce
= zobject
->ce
;
1096 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1097 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1098 "Serializing %s is not allowed", ce
->name
->val
);
1102 if (can_call_magic_serialize(ser
, ce
)) {
1103 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1104 } else if (can_call_iface_serialize(ser
, ce
)) {
1105 php_ion_serialize_object_iface(ser
, zobject
);
1106 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1107 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1108 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1109 php_ion_serialize_object_enum(ser
, zobject
);
1110 } else if (ce
== ce_Symbol
) {
1111 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1112 } else if (ce
== ce_Decimal
) {
1113 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1114 } else if (ce
== ce_Timestamp
) {
1116 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1117 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1118 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1119 } else if (ce
== ce_LOB
) {
1120 php_ion_serialize_object_lob(ser
, zobject
);
1122 php_ion_serialize_object_std(ser
, zobject
);
1126 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1128 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1131 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1132 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1134 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("r"))));
1135 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1139 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1140 zend_hash_index_add(ser
->ids
, idx
, &num
);
1143 zend_hash_next_index_insert(ser
->tmp
, zv
);
1145 switch (Z_TYPE_P(zv
)) {
1147 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1151 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1155 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1159 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("R"))));
1160 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1166 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1170 switch (Z_TYPE_P(zv
)) {
1172 ION_CHECK(ion_writer_write_null(ser
->writer
));
1175 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1178 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1181 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1184 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1190 php_ion_serialize_refcounted(ser
, zv
);
1193 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1194 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1198 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1200 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1202 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1205 zo_ser
= create_ion_Serializer_PHP(NULL
);
1206 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1208 o_ser
->serializer
.call_magic
= true;
1209 php_ion_serializer_php_ctor(o_ser
);
1211 ser
= &o_ser
->serializer
;
1214 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1215 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1216 writer
->type
= BUFFER_WRITER
;
1219 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1220 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1223 php_ion_writer_ctor(writer
);
1224 ser
->writer
= writer
->writer
;
1225 ser
->buffer
= &writer
->buffer
.str
;
1227 /* start off with a global PHP annotation instead of repeating it all over the place */
1228 if (0 == php_ion_globals_serializer_step()) {
1230 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("PHP"))),
1231 if (zo_ser
) OBJ_RELEASE(zo_ser
));
1233 php_ion_serialize_zval(ser
, zv
);
1234 php_ion_globals_serializer_exit();
1236 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1237 ion_writer_flush(ser
->writer
, NULL
);
1238 RETVAL_STR_COPY(ser
->buffer
->s
);
1240 OBJ_RELEASE(zo_writer
);
1242 OBJ_RELEASE(zo_opt
);
1245 OBJ_RELEASE(zo_ser
);
1249 typedef struct php_ion_unserializer_php
{
1250 php_ion_unserializer unserializer
;
1251 zend_object
*opt
, std
;
1252 } php_ion_unserializer_php
;
1254 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1256 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1257 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1258 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1259 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1261 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1262 ser_obj
->unserializer
.call_magic
);
1263 if (ser_obj
->unserializer
.call_custom
) {
1264 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1265 ser_obj
->unserializer
.call_custom
);
1266 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1268 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1271 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1272 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1273 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1275 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1279 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1281 if (obj
->unserializer
.call_custom
) {
1282 zend_string_release(obj
->unserializer
.call_custom
);
1286 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1288 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1290 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1293 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1295 return (ce
&& ce
->unserialize
);
1298 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1300 if (ser
->call_custom
) {
1301 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1306 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1308 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1311 object_init_ex(return_value
, ce
);
1312 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1315 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1316 "Could not find class %s", ser
->annotations
.object_class
->val
);
1320 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1322 zend_string
*zs_case
= zval_get_string(return_value
);
1325 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1326 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1327 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1328 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1331 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1332 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1333 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1336 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1337 zend_hash_next_index_insert(ser
->ids
, return_value
);
1338 zend_string_release(zs_case
);
1341 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1343 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1344 if (can_call_iface_unserialize(ser
, ce
)) {
1345 zend_string
*s
= zval_get_string(return_value
);
1346 ZVAL_NULL(return_value
);
1347 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1348 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1349 RETVAL_ZVAL(backref
, 0, 0);
1350 } else if (!EG(exception
)) {
1351 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1352 "Failed to unserialize class %s", ce
->name
->val
);
1354 zend_string_release(s
);
1356 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1357 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1361 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1363 zend_hash_next_index_insert(ser
->ids
, return_value
);
1365 ION_CHECK(ion_reader_step_in(ser
->reader
));
1369 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1371 if (typ
== tid_EOF
) {
1376 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &is
));
1377 zend_string
*key
= zend_string_from_ion(&is
);
1380 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1381 ION_CATCH(zend_string_release(key
));
1383 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1384 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1385 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1387 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1388 zval_ptr_dtor(&zvalue
);
1389 zend_string_release(key
);
1392 ION_CHECK(ion_reader_step_out(ser
->reader
));
1395 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1397 zend_hash_next_index_insert(ser
->ids
, return_value
);
1399 ION_CHECK(ion_reader_step_in(ser
->reader
));
1403 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1406 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1407 zend_string
*key
= zend_string_from_ion(&name
);
1410 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1411 ION_CATCH(zend_string_release(key
));
1413 if (typ
== tid_EOF
) {
1414 zend_string_release(key
);
1418 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1420 zend_string_release(key
);
1423 ION_CHECK(ion_reader_step_out(ser
->reader
));
1426 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1428 switch (ser
->annotations
.object_type
) {
1434 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1435 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1436 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1441 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1442 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1443 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1445 *fn
= zobject
->ce
->__unserialize
;
1449 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1450 "Invalid object type %c", ser
->annotations
.object_type
);
1454 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1456 // backup possible backref to array returned by magic/custom __serialize()
1457 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1459 php_ion_unserialize_class(ser
, return_value
);
1462 zend_function
*fn
= NULL
;
1463 zend_object
*zobject
= Z_OBJ_P(return_value
);
1464 verify_unserializer(ser
, zobject
, &fn
);
1469 php_ion_unserialize_props(ser
, return_value
);
1474 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1475 zval_ptr_dtor(input
);
1477 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1478 php_ion_unserialize_hash(ser
, input
);
1483 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1487 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1489 if (ser
->annotations
.object_class
) {
1490 switch (ser
->annotations
.object_type
) {
1492 php_ion_unserialize_object_iface(ser
, return_value
);
1495 php_ion_unserialize_object_enum(ser
, return_value
);
1498 php_ion_unserialize_object(ser
, return_value
);
1500 } else if (!ser
->annotations
.object_type
) {
1501 array_init(return_value
);
1502 php_ion_unserialize_hash(ser
, return_value
);
1503 } else if (ser
->annotations
.object_type
== 'o') {
1504 object_init(return_value
);
1505 php_ion_unserialize_hash(ser
, return_value
);
1507 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1508 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1512 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1514 ION_CHECK(ion_reader_step_in(ser
->reader
));
1515 array_init(return_value
);
1516 zend_hash_next_index_insert(ser
->ids
, return_value
);
1520 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1523 php_ion_unserialize_zval(ser
, &next
, &typ
);
1526 if (typ
== tid_EOF
) {
1530 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1533 ION_CHECK(ion_reader_step_out(ser
->reader
));
1536 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1538 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1541 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1542 if (err
== IERR_BUFFER_TOO_SMALL
) {
1543 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1546 ION_CHECK(err
, zend_string_release(zstr
));
1547 if (zstr
->len
> read
) {
1548 zstr
->val
[read
] = 0;
1549 zstr
= zend_string_truncate(zstr
, read
, 0);
1554 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1557 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1559 object_init_ex(return_value
, ce_Timestamp
);
1560 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1562 zend_string
*fmt
= NULL
;
1563 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1564 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1565 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1566 zend_string_release(fmt
);
1571 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1573 ION_INT
*num
= NULL
;
1574 ION_CHECK(ion_int_alloc(reader
, &num
));
1575 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1577 // TODO: SIZEOF_ZEND_LONG == 4
1579 iERR err
= ion_int_to_int64(num
, &i64
);
1585 case IERR_NUMERIC_OVERFLOW
:
1587 ION_CHECK(ion_int_char_length(num
, &max
));
1588 zend_string
*zs
= zend_string_alloc(max
-1, 0);
1590 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1591 ZEND_ASSERT(len
== zs
->len
);
1602 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1604 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1607 ZVAL_COPY_VALUE(return_value
, backref
);
1608 zend_hash_next_index_insert(ser
->addref
, return_value
);
1610 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1611 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1615 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1617 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1620 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1621 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1623 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1625 switch (*ann_str
.value
) {
1627 if (ser
->annotations
.makeref
) {
1628 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1629 "Invalid multiple reference annotations");
1632 ser
->annotations
.makeref
= true;
1636 if (ser
->annotations
.backref
) {
1637 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1638 "Invalid multiple back reference annotations");
1641 ser
->annotations
.backref
= true;
1645 if (ser
->annotations
.object_prop
) {
1646 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1647 "Invalid multiple object property annotations");
1650 ser
->annotations
.object_prop
= true;
1652 ION_STRING prop_class
;
1653 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1654 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1657 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1658 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1667 if (ser
->annotations
.object_type
) {
1668 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1669 "Invalid multiple object type annotations: %c::%c",
1670 ser
->annotations
.object_type
, *ann_str
.value
);
1673 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1674 ION_STRING class_name
;
1675 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1676 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1679 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1680 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1686 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1687 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1688 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1691 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1692 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1693 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1699 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1702 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1705 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1708 php_ion_unserialize_annotations(ser
);
1711 if (ser
->annotations
.makeref
) {
1712 ZVAL_MAKE_REF(return_value
);
1713 zend_hash_next_index_insert(ser
->ids
, return_value
);
1714 ZVAL_DEREF(return_value
);
1718 ION_CHECK(ion_reader_is_null(ser
->reader
, &bval
));
1723 switch (ION_TYPE_INT(*typ
)) {
1726 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1730 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1734 php_ion_reader_read_int(ser
->reader
, return_value
);
1735 if (ser
->annotations
.backref
) {
1737 php_ion_unserialize_backref(ser
, return_value
);
1739 if (ser
->annotations
.object_type
) {
1740 if (!ser
->annotations
.backref
) {
1741 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1742 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1743 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1746 goto unserialize_struct
;
1752 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1755 case tid_DECIMAL_INT
:
1756 object_init_ex(return_value
, ce_Decimal
);
1757 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1758 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1759 php_ion_decimal_ctor(dec
);
1760 zend_hash_next_index_insert(ser
->ids
, return_value
);
1763 case tid_TIMESTAMP_INT
:
1764 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1765 zend_hash_next_index_insert(ser
->ids
, return_value
);
1768 case tid_SYMBOL_INT
:
1770 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1771 php_ion_symbol_zval(&sym
, return_value
);
1772 if (ser
->annotations
.object_type
) {
1773 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1774 goto unserialize_struct
;
1776 zend_hash_next_index_insert(ser
->ids
, return_value
);
1779 case tid_STRING_INT
:
1781 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1782 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1783 if (ser
->annotations
.object_type
) {
1784 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1785 goto unserialize_struct
;
1787 zend_hash_next_index_insert(ser
->ids
, return_value
);
1792 php_ion_reader_read_lob(ser
->reader
, return_value
);
1793 if (ser
->annotations
.object_type
) {
1794 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1795 goto unserialize_struct
;
1797 zend_hash_next_index_insert(ser
->ids
, return_value
);
1801 case tid_SEXP_INT
: // FIXME
1802 php_ion_unserialize_list(ser
, return_value
);
1803 if (!ser
->annotations
.object_type
) {
1808 case tid_STRUCT_INT
:
1809 unserialize_struct
: ;
1810 php_ion_unserialize_struct(ser
, return_value
);
1817 case tid_DATAGRAM_INT
:
1818 ZEND_ASSERT(!"datagram");
1824 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1826 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1828 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1831 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1832 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1834 o_ser
->unserializer
.call_magic
= true;
1835 php_ion_unserializer_php_ctor(o_ser
);
1837 ser
= &o_ser
->unserializer
;
1840 zend_object
*zo_reader
;
1841 php_ion_reader
*reader
;
1843 switch (Z_TYPE_P(zdata
)) {
1845 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
1846 reader
= php_ion_obj(reader
, zo_reader
);
1847 reader
->type
= BUFFER_READER
;
1848 reader
->buffer
= zend_string_copy(Z_STR_P(zdata
));
1852 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
1853 reader
= php_ion_obj(reader
, zo_reader
);
1854 reader
->type
= STREAM_READER
;
1855 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
1859 ZEND_ASSERT(!IS_STRING
&& !IS_RESOURCE
);
1863 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
1864 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
1867 php_ion_reader_ctor(reader
);
1868 ser
->reader
= reader
->reader
;
1870 php_ion_globals_unserializer_step();
1871 php_ion_unserialize_zval(ser
, return_value
, NULL
);
1872 php_ion_globals_unserializer_exit();
1874 OBJ_RELEASE(zo_reader
);
1876 OBJ_RELEASE(zo_opt
);
1879 OBJ_RELEASE(zo_ser
);