2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2021, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
16 #include "ext/date/php_date.h"
18 typedef struct php_ion_serializer
{
20 ION_WRITER_OPTIONS
*options
;
23 zend_string
*call_custom
;
32 typedef struct php_ion_annotaions
{
35 uint8_t object_prop
:1;
37 zend_string
*object_class
;
38 zend_string
*property_class
;
39 } php_ion_annotations
;
41 typedef struct php_ion_unserializer
{
43 ION_READER_OPTIONS
*options
;
46 zend_string
*call_custom
;
55 php_ion_annotations annotations
;
56 } php_ion_unserializer
;
58 ZEND_BEGIN_MODULE_GLOBALS(ion
)
60 php_ion_serializer serializer
;
61 php_ion_unserializer unserializer
;
64 HashTable serializer
[2];
65 HashTable unserializer
[3];
68 ZEND_END_MODULE_GLOBALS(ion
);
71 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
73 # define php_ion_globals ion_globals
76 ZEND_DECLARE_MODULE_GLOBALS(ion
);
78 static inline void php_ion_globals_serializer_init(void)
80 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
81 HashTable
*h
= php_ion_globals
._ht
.serializer
;
83 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
84 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
87 static inline uint32_t php_ion_globals_serializer_step(void)
89 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
92 if (!(level
= s
->level
++)) {
93 zend_hash_clean(s
->ids
);
94 zend_hash_clean(s
->tmp
);
99 static inline uint32_t php_ion_globals_serializer_exit(void)
101 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
103 ZEND_ASSERT(s
->level
);
105 zend_hash_clean(s
->ids
);
106 zend_hash_clean(s
->tmp
);
111 static inline void php_ion_globals_serializer_dtor(void)
113 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
115 zend_hash_destroy(s
->tmp
);
116 zend_hash_destroy(s
->ids
);
119 void ZVAL_ADDREF(zval
*zv
)
122 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
127 static inline void php_ion_globals_unserializer_init(void)
129 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
130 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
132 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
133 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
134 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
137 static inline void php_ion_globals_unserializer_step(void)
139 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
142 zend_hash_clean(s
->addref
);
143 zend_hash_clean(s
->ids
);
144 zend_hash_clean(s
->tmp
);
148 static inline void php_ion_globals_unserializer_exit(void)
150 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
152 ZEND_ASSERT(s
->level
);
154 zend_hash_clean(s
->addref
);
155 zend_hash_clean(s
->ids
);
156 zend_hash_clean(s
->tmp
);
160 static inline void php_ion_globals_unserializer_dtor(void)
162 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
164 zend_hash_destroy(s
->addref
);
165 zend_hash_destroy(s
->ids
);
166 zend_hash_destroy(s
->tmp
);
169 static zend_class_entry
175 *ce_Decimal_Context_Rounding
,
182 *ce_Reader_Buffer_Reader
,
183 *ce_Reader_Stream_Reader
,
187 *ce_Symbol_ImportLocation
,
189 *ce_Symbol_System_SID
,
192 *ce_Timestamp_Precision
,
195 *ce_Unserializer_PHP
,
199 *ce_Writer_Buffer_Writer
,
201 *ce_Writer_Stream_Writer
,
205 #define php_ion_decl(type, cname, ...) \
206 static zend_object_handlers oh_ ## cname; \
207 static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \
209 if (!ce) ce = ce_ ## cname; \
210 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
211 zend_object_std_init(&o->std, ce); \
212 object_properties_init(&o->std, ce); \
213 o->std.handlers = &oh_ ## cname; \
216 static inline void free_ion_ ## cname(zend_object *std) \
218 php_ion_ ## type *obj = php_ion_obj(type, std); \
220 zend_object_std_dtor(std); \
223 #define php_ion_register(type, cname, ...) do { \
224 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
225 ce_ ## cname ->create_object = create_ion_ ## cname; \
226 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
227 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
228 oh_ ## cname .free_obj = free_ion_ ## cname; \
231 #define php_ion_obj(type, obj) \
232 ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL))
234 #define ION_CHECK(err, ...) do { \
237 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
243 #define ION_CATCH(...) do { \
244 if (EG(exception)) { \
250 #define PTR_CHECK(ptr, ...) do { \
252 zend_throw_error(NULL, "Uninitialized object"); \
258 #define OBJ_CHECK(obj) do { \
260 PTR_CHECK(*((void **)obj)); \
263 static inline ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
265 is
->length
= zs
? zs
->len
: 0;
266 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
270 static inline zend_string
*zend_string_from_ion(const ION_STRING
*s
)
272 return zend_string_init((const char *) s
->value
, s
->length
, 0);
275 static inline void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
279 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
282 typedef struct php_ion_type
{
287 php_ion_decl(type
, Type
);
289 #define RETURN_IONTYPE(typ) do { \
290 zend_object *__zo = php_ion_type_fetch(typ); \
291 if (UNEXPECTED(!__zo)) { \
294 RETURN_OBJ_COPY(__zo); \
297 static inline zend_object
*php_ion_type_fetch(ION_TYPE typ
)
299 zend_long index
= ION_TYPE_INT(typ
);
300 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
302 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
303 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
306 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
309 typedef struct php_ion_symbol_iloc
{
310 ION_SYMBOL_IMPORT_LOCATION loc
;
313 } php_ion_symbol_iloc
;
315 static inline void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
317 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
318 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
319 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
322 static inline void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
324 zend_string_release(obj
->name
);
327 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
, php_ion_symbol_iloc_dtor(obj
));
329 typedef struct php_ion_symbol
{
332 zend_object
*iloc
, std
;
335 static inline int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
336 zend_string
*zs1
= zval_get_string(zv1
);
337 zend_string
*zs2
= zval_get_string(zv2
);
344 if (zs1
->len
> zs2
->len
) {
346 } else if (zs2
->len
> zs1
->len
) {
349 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
351 zend_string_release(zs1
);
352 zend_string_release(zs2
);
356 static inline void php_ion_symbol_ctor(php_ion_symbol
*obj
)
358 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
361 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
363 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
365 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
367 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
368 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
370 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
374 static inline void php_ion_symbol_dtor(php_ion_symbol
*obj
)
377 zend_string_release(obj
->value
);
381 static inline void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
383 object_init_ex(return_value
, ce_Symbol
);
384 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
386 sym
->sym
.sid
= sym_ptr
->sid
;
387 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
388 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
390 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
391 sym
->iloc
= Z_OBJ(ziloc
);
393 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
394 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
395 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
397 php_ion_symbol_iloc_ctor(iloc
);
400 php_ion_symbol_ctor(sym
);
402 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
403 GC_DELREF(sym
->iloc
);
407 php_ion_decl(symbol
, Symbol
, php_ion_symbol_dtor(obj
));
409 typedef struct php_ion_symbol_table
{
411 } php_ion_symbol_table
;
413 php_ion_decl(symbol_table
, Symbol_Table
);
415 typedef struct php_ion_decimal_ctx
{
418 } php_ion_decimal_ctx
;
420 #define php_ion_decimal_ctx_init_max(c, rounding) \
421 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
422 static inline void php_ion_decimal_ctx_init(decContext
*ctx
,
423 zend_long digits
, zend_long emax
, zend_long emin
, zend_long round
, zend_bool clamp
)
425 memset(ctx
, 0, sizeof(*ctx
));
426 ctx
->digits
= digits
;
433 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
435 if (!obj
->ctx
.digits
) {
436 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
439 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
441 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
443 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
444 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
445 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
446 zend_update_property_bool(obj
->std
.ce
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
449 php_ion_decl(decimal_ctx
, Decimal_Context
);
451 typedef struct php_ion_decimal
{
453 zend_object
*ctx
, std
;
456 static inline void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
458 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
459 ION_CHECK(ion_decimal_from_int32(dec
, num
));
460 } else if (num
> 0 && num
<= UINT32_MAX
) {
461 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
464 ION_CHECK(ion_int_alloc(NULL
, &iint
));
465 ION_CHECK(ion_int_from_long(iint
, num
),
467 /* WATCH OUT: BS API */
468 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
469 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
475 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
477 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
478 (void) ion_decimal_to_string(dec
, zstr
->val
);
479 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
482 static inline void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
485 ION_CHECK(ion_int_alloc(NULL
, &ii
));
486 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
488 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
493 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
497 if (!ion_decimal_is_integer(&obj
->dec
)) {
502 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_max
, &g_dec_ctx
, &result
);
507 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_min
, &g_dec_ctx
, &result
);
514 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
518 object_init_ex(&zdc
, ce_Decimal_Context
);
519 obj
->ctx
= Z_OBJ(zdc
);
520 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
523 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
525 if (php_ion_decimal_fits_zend_long(obj
)) {
527 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
528 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
530 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
531 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
532 zend_string_release(zstr
);
536 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
538 ion_decimal_free(&obj
->dec
);
541 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
543 typedef php_date_obj php_ion_timestamp
;
545 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
547 decQuad microsecs
, result
;
548 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
549 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
552 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
554 decQuad microsecs
, us
;
555 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
558 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
562 return zend_string_init(ZEND_STRL("c"), 0);
564 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
566 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
568 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
570 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
572 return zend_string_init(ZEND_STRL("Y\\T"), 0);
574 return zend_string_init(ZEND_STRL("c"), 0);
578 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
580 timelib_time
*time
= timelib_time_ctor();
582 switch (ts
->precision
) {
584 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
587 time
->s
= ts
->seconds
;
590 time
->i
= ts
->minutes
;
603 time
->z
= ts
->tz_offset
* 60;
607 *fmt
= php_dt_format_from_precision(ts
->precision
);
612 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
614 memset(buf
, 0, sizeof(*buf
));
617 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
619 if (!precision
|| precision
> ION_TS_FRAC
) {
620 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
621 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
622 } else switch ((buf
->precision
= precision
)) {
624 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
627 buf
->seconds
= ts
->time
->s
;
630 buf
->minutes
= ts
->time
->i
;
633 buf
->hours
= ts
->time
->h
;
634 buf
->day
= ts
->time
->d
;
637 buf
->month
= ts
->time
->m
;
640 buf
->year
= ts
->time
->y
;
643 buf
->tz_offset
= ts
->time
->z
/ 60;
649 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
652 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
654 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
656 fmt
= php_dt_format_from_precision(precision
);
657 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
658 zend_string_release(fmt
);
661 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
664 timelib_time_dtor(obj
->time
);
668 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
670 typedef struct php_ion_catalog
{
675 php_ion_decl(catalog
, Catalog
);
677 typedef struct php_ion_reader_options
{
678 ION_READER_OPTIONS opt
;
679 zend_object
*cat
, *dec_ctx
, *cb
, std
;
680 } php_ion_reader_options
;
682 php_ion_decl(reader_options
, Reader_Options
);
684 typedef struct php_ion_reader
{
698 zend_object
*opt
, std
;
701 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
703 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
704 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
706 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
707 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
708 user
->limit
-= remaining
;
711 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
714 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
715 if (EXPECTED(read
> 0)) {
720 if (EXPECTED(read
== 0)) {
724 return IERR_READ_ERROR
;
727 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
730 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
736 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
741 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
744 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
747 opt
->opt
.context_change_notifier
.context
= obj
;
749 if (obj
->type
== STREAM_READER
) {
750 PTR_CHECK(obj
->stream
.ptr
);
751 GC_ADDREF(obj
->stream
.ptr
->res
);
753 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
754 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
755 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
756 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
759 err
= ion_reader_open_buffer(&obj
->reader
,
760 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
761 opt
? &opt
->opt
: NULL
);
764 opt
->opt
.context_change_notifier
.context
= NULL
;
770 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
773 ion_reader_close(obj
->reader
);
775 if (obj
->type
== STREAM_READER
) {
776 if (obj
->stream
.buf
.value
) {
777 efree(obj
->stream
.buf
.value
);
779 if (obj
->stream
.ptr
) {
780 zend_list_delete(obj
->stream
.ptr
->res
);
784 zend_string_release(obj
->buffer
);
789 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
791 typedef struct php_ion_writer_options
{
792 ION_WRITER_OPTIONS opt
;
793 zend_object
*cat
, *dec_ctx
, *col
, std
;
794 } php_ion_writer_options
;
796 php_ion_decl(writer_options
, Writer_Options
);
798 typedef struct php_ion_writer
{
814 zend_object
*opt
, std
;
818 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
820 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
822 if (EXPECTED(user
->limit
&& user
->curr
)) {
823 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
824 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
825 return IERR_WRITE_ERROR
;
828 user
->curr
= writer
->stream
.buf
.value
;
829 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
833 #define REF_STR() do { \
834 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
835 GC_ADDREF(obj->buffer.str.s); \
838 #define NEW_REF_STR() do {\
839 if (Z_STR_P(ref) != obj->buffer.str.s) { \
840 zval_ptr_dtor(ref); \
845 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
847 PTR_CHECK(obj
->stream
.ptr
);
848 GC_ADDREF(obj
->stream
.ptr
->res
);
850 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
851 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
853 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
855 zval
*ref
= &obj
->buffer
.val
;
858 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
859 smart_str_0(&obj
->buffer
.str
);
863 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
865 zval
*ref
= &obj
->buffer
.val
;
868 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
873 // we've been separated
874 GC_ADDREF(obj
->buffer
.str
.s
);
877 // we have to separate
878 fprintf(stderr
, "SEPARATE\n");
879 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
883 zend_string
*old
= obj
->buffer
.str
.s
;
885 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
886 if (old
== obj
->buffer
.str
.s
) {
888 } else if(old
== Z_STR_P(ref
)) {
895 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
897 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
900 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
901 smart_str_0(&writer
->buffer
.str
);
902 if (user
->limit
== user
->curr
) {
903 php_ion_writer_buffer_grow(writer
);
906 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
907 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
912 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
915 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
918 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
919 ION_STREAM_HANDLER h
;
920 if (obj
->type
== STREAM_WRITER
) {
921 h
= php_ion_writer_stream_handler
;
922 php_ion_writer_stream_init(obj
, opt
);
924 h
= php_ion_writer_buffer_handler
;
925 php_ion_writer_buffer_init(obj
);
928 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
932 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
935 ion_writer_close(obj
->writer
);
937 if (obj
->type
== STREAM_WRITER
) {
938 if (obj
->stream
.buf
.value
) {
939 efree(obj
->stream
.buf
.value
);
941 if (obj
->stream
.ptr
) {
942 zend_list_delete(obj
->stream
.ptr
->res
);
945 if (obj
->buffer
.str
.s
) {
946 smart_str_0(&obj
->buffer
.str
);
947 zend_string_release(obj
->buffer
.str
.s
);
949 zval_ptr_dtor(&obj
->buffer
.val
);
953 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
955 typedef struct php_ion_serializer_php
{
956 php_ion_serializer serializer
;
957 zend_object
*opt
, std
;
958 } php_ion_serializer_php
;
960 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
962 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
963 ser_obj
->serializer
.ids
= global_ser
->ids
;
964 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
966 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
967 ser_obj
->serializer
.call_magic
);
968 if (ser_obj
->serializer
.call_custom
) {
969 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
970 ser_obj
->serializer
.call_custom
);
971 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
973 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
976 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
977 ser_obj
->serializer
.options
= &o_woptions
->opt
;
978 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
980 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
984 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
986 if (obj
->serializer
.call_custom
) {
987 zend_string_release(obj
->serializer
.call_custom
);
991 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
993 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
995 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
999 zend_string
*k
= NULL
;
1000 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1004 const char *class_name
, *prop_name
;
1005 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1006 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("p"))));
1007 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1012 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, (char *) prop_name
, prop_len
)));
1014 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
1015 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1016 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_assign_cstr(&is
, ptr
, end
- ptr
)));
1019 php_ion_serialize_zval(ser
, v
);
1021 ZEND_HASH_FOREACH_END();
1023 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1026 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1028 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1031 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1032 php_ion_serialize_zval(ser
, v
);
1034 ZEND_HASH_FOREACH_END();
1036 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1039 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1041 if (zend_array_is_list(arr
)) {
1042 php_ion_serialize_list(ser
, arr
);
1044 php_ion_serialize_struct(ser
, arr
, false);
1048 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1054 ZVAL_OBJ(&tmp
, zobject
);
1055 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1057 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("S"))));
1058 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1059 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_assign_cstr(&is
, (char *) buf
, len
)));
1061 } else if (!EG(exception
)){
1062 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1063 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1067 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1072 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1075 if (IS_ARRAY
== Z_TYPE(rv
)) {
1077 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, fn
? "C" : "O", 1)));
1078 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1079 php_ion_serialize_zval(ser
, &rv
);
1082 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1083 "%s serializer %s::%s did not return an array",
1084 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1085 fn
? fn
->common
.function_name
->val
: "__serialize");
1089 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1092 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("E"))));
1094 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1095 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1096 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1099 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1103 if (zobject
->ce
!= zend_standard_class_def
) {
1104 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("c"))));
1105 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1107 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("o"))));
1111 ZVAL_OBJ(&zobj
, zobject
);
1112 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1114 php_ion_serialize_struct(ser
, props
, true);
1115 zend_release_properties(props
);
1117 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1118 "Could not get properties for serialization of class %s",
1119 zobject
->ce
->name
->val
);
1123 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1125 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1126 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1127 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1129 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1132 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1135 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1136 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1141 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1143 return ce
->__serialize
&& ser
->call_magic
;
1146 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1148 return !!ce
->serialize
;
1151 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1153 if (ser
->call_custom
) {
1154 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1159 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1162 zend_class_entry
*ce
= zobject
->ce
;
1165 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1166 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1167 "Serializing %s is not allowed", ce
->name
->val
);
1171 if (can_call_magic_serialize(ser
, ce
)) {
1172 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1173 } else if (can_call_iface_serialize(ser
, ce
)) {
1174 php_ion_serialize_object_iface(ser
, zobject
);
1175 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1176 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1177 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1178 php_ion_serialize_object_enum(ser
, zobject
);
1179 } else if (ce
== ce_Symbol
) {
1180 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1181 } else if (ce
== ce_Decimal
) {
1182 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1183 } else if (ce
== ce_Timestamp
) {
1185 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1186 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1187 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1188 } else if (ce
== ce_LOB
) {
1189 php_ion_serialize_object_lob(ser
, zobject
);
1191 php_ion_serialize_object_std(ser
, zobject
);
1195 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1197 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1200 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1201 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1203 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("r"))));
1204 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1208 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1209 zend_hash_index_add(ser
->ids
, idx
, &num
);
1212 zend_hash_next_index_insert(ser
->tmp
, zv
);
1214 switch (Z_TYPE_P(zv
)) {
1216 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1220 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1224 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1228 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("R"))));
1229 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1235 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1239 switch (Z_TYPE_P(zv
)) {
1241 ION_CHECK(ion_writer_write_null(ser
->writer
));
1244 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1247 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1250 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1253 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1259 php_ion_serialize_refcounted(ser
, zv
);
1262 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1263 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1267 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1269 static inline void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1272 HashTable
*arr
= NULL
;
1274 if (ser
->multi_seq
) {
1275 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1276 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1277 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1281 arr
= Z_ARRVAL_P(zv
);
1283 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1284 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1288 /* start off with a global PHP annotation instead of repeating it all over the place */
1289 if (0 == php_ion_globals_serializer_step()) {
1291 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, ZEND_STRL("PHP"))));
1293 php_ion_serialize_zval(ser
, zv
);
1294 php_ion_globals_serializer_exit();
1296 if (!ser
->multi_seq
) {
1299 zend_hash_move_forward_ex(arr
, &pos
);
1300 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1304 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1306 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1309 zo_ser
= create_ion_Serializer_PHP(NULL
);
1310 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1312 o_ser
->serializer
.call_magic
= true;
1313 php_ion_serializer_php_ctor(o_ser
);
1315 ser
= &o_ser
->serializer
;
1318 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1319 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1320 writer
->type
= BUFFER_WRITER
;
1323 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1324 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1327 php_ion_writer_ctor(writer
);
1328 ser
->writer
= writer
->writer
;
1329 ser
->buffer
= &writer
->buffer
.str
;
1331 if (!EG(exception
)) {
1332 php_ion_serialize_ex(ser
, zv
);
1335 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1336 ion_writer_flush(ser
->writer
, NULL
);
1337 RETVAL_STR_COPY(ser
->buffer
->s
);
1339 OBJ_RELEASE(zo_writer
);
1341 OBJ_RELEASE(zo_opt
);
1344 OBJ_RELEASE(zo_ser
);
1348 typedef struct php_ion_unserializer_php
{
1349 php_ion_unserializer unserializer
;
1350 zend_object
*opt
, std
;
1351 } php_ion_unserializer_php
;
1353 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1355 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1356 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1357 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1358 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1360 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1361 ser_obj
->unserializer
.call_magic
);
1362 if (ser_obj
->unserializer
.call_custom
) {
1363 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1364 ser_obj
->unserializer
.call_custom
);
1365 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1367 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1370 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1371 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1372 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1374 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1378 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1380 if (obj
->unserializer
.call_custom
) {
1381 zend_string_release(obj
->unserializer
.call_custom
);
1385 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1387 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1389 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1392 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1394 return (ce
&& ce
->unserialize
);
1397 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1399 if (ser
->call_custom
) {
1400 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1405 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1407 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1410 object_init_ex(return_value
, ce
);
1411 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1414 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1415 "Could not find class %s", ser
->annotations
.object_class
->val
);
1419 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1421 zend_string
*zs_case
= zval_get_string(return_value
);
1424 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1425 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1426 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1427 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1430 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1431 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1432 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1435 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1436 zend_hash_next_index_insert(ser
->ids
, return_value
);
1437 zend_string_release(zs_case
);
1440 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1442 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1443 if (can_call_iface_unserialize(ser
, ce
)) {
1444 zend_string
*s
= zval_get_string(return_value
);
1445 ZVAL_NULL(return_value
);
1446 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1447 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1448 RETVAL_ZVAL(backref
, 0, 0);
1449 } else if (!EG(exception
)) {
1450 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1451 "Failed to unserialize class %s", ce
->name
->val
);
1453 zend_string_release(s
);
1455 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1456 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1460 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1462 // FIXME: symbol table
1464 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1467 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1468 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1469 name
= is_ptr
->value
;
1470 } else if (is_ptr
) {
1471 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1472 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1474 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1478 *key
= zend_string_from_ion(&name
);
1481 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1483 zend_hash_next_index_insert(ser
->ids
, return_value
);
1485 ION_CHECK(ion_reader_step_in(ser
->reader
));
1489 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1491 if (typ
== tid_EOF
) {
1496 php_ion_unserialize_field_name(ser
, &key
);
1500 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1501 ION_CATCH(zend_string_release(key
));
1503 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1504 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1505 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1507 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1508 zval_ptr_dtor(&zvalue
);
1509 zend_string_release(key
);
1512 ION_CHECK(ion_reader_step_out(ser
->reader
));
1516 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
1517 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
1518 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
1519 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
1520 * nondeterministic behavior.
1522 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1524 zend_hash_next_index_insert(ser
->ids
, return_value
);
1526 ION_CHECK(ion_reader_step_in(ser
->reader
));
1530 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1532 if (typ
== tid_EOF
) {
1537 php_ion_unserialize_field_name(ser
, &key
);
1541 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1542 ION_CATCH(zend_string_release(key
));
1544 // FIXME:: too naive; b0rked if the previous value is an array
1545 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
1546 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
1547 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
1549 Z_TRY_ADDREF_P(prev
);
1550 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
1551 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
1553 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
1555 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1557 zend_string_release(key
);
1560 ION_CHECK(ion_reader_step_out(ser
->reader
));
1563 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1565 switch (ser
->annotations
.object_type
) {
1571 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1572 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1573 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1578 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1579 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1580 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1582 *fn
= zobject
->ce
->__unserialize
;
1586 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1587 "Invalid object type %c", ser
->annotations
.object_type
);
1591 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1593 // backup possible backref to array returned by magic/custom __serialize()
1594 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1596 php_ion_unserialize_class(ser
, return_value
);
1599 zend_function
*fn
= NULL
;
1600 zend_object
*zobject
= Z_OBJ_P(return_value
);
1601 verify_unserializer(ser
, zobject
, &fn
);
1606 php_ion_unserialize_props(ser
, return_value
);
1611 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1612 zval_ptr_dtor(input
);
1614 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1615 php_ion_unserialize_hash(ser
, input
);
1620 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1624 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1626 if (ser
->annotations
.object_class
) {
1627 switch (ser
->annotations
.object_type
) {
1629 php_ion_unserialize_object_iface(ser
, return_value
);
1632 php_ion_unserialize_object_enum(ser
, return_value
);
1635 php_ion_unserialize_object(ser
, return_value
);
1637 } else if (!ser
->annotations
.object_type
) {
1638 array_init(return_value
);
1639 php_ion_unserialize_hash(ser
, return_value
);
1640 } else if (ser
->annotations
.object_type
== 'o') {
1641 object_init(return_value
);
1642 php_ion_unserialize_hash(ser
, return_value
);
1644 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1645 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1649 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1651 ION_CHECK(ion_reader_step_in(ser
->reader
));
1652 array_init(return_value
);
1653 zend_hash_next_index_insert(ser
->ids
, return_value
);
1657 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1659 if (typ
== tid_EOF
) {
1664 php_ion_unserialize_zval(ser
, &next
, &typ
);
1667 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1670 ION_CHECK(ion_reader_step_out(ser
->reader
));
1673 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1675 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1678 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1679 if (err
== IERR_BUFFER_TOO_SMALL
) {
1680 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1683 ION_CHECK(err
, zend_string_release(zstr
));
1684 if (zstr
->len
> read
) {
1685 zstr
->val
[read
] = 0;
1686 zstr
= zend_string_truncate(zstr
, read
, 0);
1691 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1694 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1696 object_init_ex(return_value
, ce_Timestamp
);
1697 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1699 zend_string
*fmt
= NULL
;
1700 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1701 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1702 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1703 zend_string_release(fmt
);
1708 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1710 ION_INT
*num
= NULL
;
1711 ION_CHECK(ion_int_alloc(reader
, &num
));
1712 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1714 // TODO: SIZEOF_ZEND_LONG == 4
1716 iERR err
= ion_int_to_int64(num
, &i64
);
1722 case IERR_NUMERIC_OVERFLOW
:
1724 ION_CHECK(ion_int_char_length(num
, &max
));
1725 zend_string
*zs
= zend_string_alloc(max
, 0);
1727 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1728 zs
->val
[zs
->len
= len
] = 0;
1739 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1741 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1744 ZVAL_COPY_VALUE(return_value
, backref
);
1745 zend_hash_next_index_insert(ser
->addref
, return_value
);
1747 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1748 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1752 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1754 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1757 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1758 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1760 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1762 if (ann_str
.length
!= 1) {
1766 switch (*ann_str
.value
) {
1768 if (ser
->annotations
.makeref
) {
1769 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1770 "Invalid multiple reference annotations");
1773 ser
->annotations
.makeref
= true;
1777 if (ser
->annotations
.backref
) {
1778 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1779 "Invalid multiple back reference annotations");
1782 ser
->annotations
.backref
= true;
1786 if (ser
->annotations
.object_prop
) {
1787 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1788 "Invalid multiple object property annotations");
1791 ser
->annotations
.object_prop
= true;
1793 ION_STRING prop_class
;
1794 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1795 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1798 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1799 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1808 if (ser
->annotations
.object_type
) {
1809 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1810 "Invalid multiple object type annotations: %c::%c",
1811 ser
->annotations
.object_type
, *ann_str
.value
);
1814 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1815 ION_STRING class_name
;
1816 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1817 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1820 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1821 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1827 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1828 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1829 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1832 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1833 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1834 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1840 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1843 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1846 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1849 php_ion_unserialize_annotations(ser
);
1852 if (ser
->annotations
.makeref
) {
1853 ZVAL_MAKE_REF(return_value
);
1854 zend_hash_next_index_insert(ser
->ids
, return_value
);
1855 ZVAL_DEREF(return_value
);
1858 if (ION_TYPE_INT(*typ
) > 0) {
1860 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
1866 switch (ION_TYPE_INT(*typ
)) {
1868 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1873 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1877 php_ion_reader_read_int(ser
->reader
, return_value
);
1878 if (ser
->annotations
.backref
) {
1880 php_ion_unserialize_backref(ser
, return_value
);
1882 if (ser
->annotations
.object_type
) {
1883 if (!ser
->annotations
.backref
) {
1884 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1885 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1886 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1889 goto unserialize_struct
;
1895 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1898 case tid_DECIMAL_INT
:
1899 object_init_ex(return_value
, ce_Decimal
);
1900 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1901 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1902 php_ion_decimal_ctor(dec
);
1903 zend_hash_next_index_insert(ser
->ids
, return_value
);
1906 case tid_TIMESTAMP_INT
:
1907 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1908 zend_hash_next_index_insert(ser
->ids
, return_value
);
1911 case tid_SYMBOL_INT
:
1913 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1914 php_ion_symbol_zval(&sym
, return_value
);
1915 if (ser
->annotations
.object_type
) {
1916 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1917 goto unserialize_struct
;
1919 zend_hash_next_index_insert(ser
->ids
, return_value
);
1922 case tid_STRING_INT
:
1924 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1925 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1926 if (ser
->annotations
.object_type
) {
1927 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1928 goto unserialize_struct
;
1930 zend_hash_next_index_insert(ser
->ids
, return_value
);
1935 php_ion_reader_read_lob(ser
->reader
, return_value
);
1936 if (ser
->annotations
.object_type
) {
1937 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1938 goto unserialize_struct
;
1940 zend_hash_next_index_insert(ser
->ids
, return_value
);
1944 case tid_SEXP_INT
: // FIXME
1945 php_ion_unserialize_list(ser
, return_value
);
1946 if (!ser
->annotations
.object_type
) {
1951 case tid_STRUCT_INT
:
1952 unserialize_struct
: ;
1953 php_ion_unserialize_struct(ser
, return_value
);
1960 case tid_DATAGRAM_INT
:
1961 ZEND_ASSERT(!"datagram");
1967 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1969 static inline void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
1971 if (ser
->multi_seq
) {
1972 array_init(return_value
);
1978 php_ion_globals_unserializer_step();
1979 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
1980 php_ion_globals_unserializer_exit();
1981 ION_CATCH(zval_ptr_dtor(&tmp
));
1983 if (!ser
->multi_seq
) {
1984 RETURN_COPY_VALUE(&tmp
);
1985 } else if (ser
->type
!= tid_EOF
) {
1986 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
1988 } while (ser
->type
!= tid_EOF
);
1991 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1993 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1996 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1997 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1999 o_ser
->unserializer
.call_magic
= true;
2000 php_ion_unserializer_php_ctor(o_ser
);
2002 ser
= &o_ser
->unserializer
;
2005 zend_object
*zo_reader
;
2006 php_ion_reader
*reader
;
2009 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2010 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2011 reader
= php_ion_obj(reader
, zo_reader
);
2012 reader
->type
= STREAM_READER
;
2013 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2014 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2015 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2016 reader
= php_ion_obj(reader
, zo_reader
);
2017 reader
->type
= BUFFER_READER
;
2018 reader
->buffer
= zval_get_string(zdata
);
2020 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
2021 "Invalid source to unserialize; expected string or resource");
2023 OBJ_RELEASE(zo_ser
);
2029 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
2030 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
2033 php_ion_reader_ctor(reader
);
2034 ser
->reader
= reader
->reader
;
2036 if (!EG(exception
)) {
2037 php_ion_unserialize_ex(ser
, return_value
);
2040 OBJ_RELEASE(zo_reader
);
2042 OBJ_RELEASE(zo_opt
);
2045 OBJ_RELEASE(zo_ser
);