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
);
385 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
386 GC_DELREF(sym
->iloc
);
390 php_ion_decl(symbol
, Symbol
, php_ion_symbol_dtor(obj
));
392 typedef struct php_ion_symbol_table
{
394 } php_ion_symbol_table
;
396 php_ion_decl(symbol_table
, Symbol_Table
);
398 typedef struct php_ion_decimal_ctx
{
401 } php_ion_decimal_ctx
;
403 static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
) {
404 zval tmp
, *zbits
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), 1, &tmp
);
407 if (zbits
!= &EG(uninitialized_zval
)) {
408 bits
= Z_LVAL_P(zbits
);
410 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("bits"), bits
);
416 decContextDefault(&obj
->ctx
, bits
);
419 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
420 "Decimal context only allows 32, 64 or 128 bits");
424 php_ion_decl(decimal_ctx
, Decimal_Context
);
426 typedef struct php_ion_decimal
{
428 zend_object
*ctx
, std
;
431 static inline zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
433 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
434 (void) ion_decimal_to_string(dec
, zstr
->val
);
435 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
438 static inline void php_ion_decimal_to_int(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
441 ION_CHECK(ion_int_alloc(NULL
, &ii
));
442 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
444 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
449 static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
453 if (!ion_decimal_is_integer(&obj
->dec
)) {
458 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_max
, &g_dec_ctx
, &result
);
463 ion_decimal_compare(&obj
->dec
, &g_ion_dec_zend_min
, &g_dec_ctx
, &result
);
470 static inline void php_ion_decimal_ctor(php_ion_decimal
*obj
)
474 object_init_ex(&zdc
, ce_Decimal_Context
);
475 obj
->ctx
= Z_OBJ(zdc
);
476 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
));
479 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
481 if (php_ion_decimal_fits_zend_long(obj
)) {
483 php_ion_decimal_to_int(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
484 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
486 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
487 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
488 zend_string_release(zstr
);
492 static inline void php_ion_decimal_dtor(php_ion_decimal
*obj
)
494 ion_decimal_free(&obj
->dec
);
497 php_ion_decl(decimal
, Decimal
, php_ion_decimal_dtor(obj
));
499 typedef php_date_obj php_ion_timestamp
;
501 static inline zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
503 decQuad microsecs
, result
;
504 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
505 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
508 static inline decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, zend_long usec
, decContext
*ctx
)
510 decQuad microsecs
, us
;
511 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
514 static inline zend_string
*php_dt_format_from_precision(uint8_t precision
)
518 return zend_string_init(ZEND_STRL("c"), 0);
520 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
522 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
524 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
526 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
528 return zend_string_init(ZEND_STRL("Y\\T"), 0);
530 return zend_string_init(ZEND_STRL("c"), 0);
534 static inline timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
536 timelib_time
*time
= timelib_time_ctor();
538 switch (ts
->precision
) {
540 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
543 time
->s
= ts
->seconds
;
546 time
->i
= ts
->minutes
;
559 time
->z
= ts
->tz_offset
* 60;
563 *fmt
= php_dt_format_from_precision(ts
->precision
);
568 static inline ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
570 memset(buf
, 0, sizeof(*buf
));
573 uint8_t precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
575 if (!precision
|| precision
> ION_TS_FRAC
) {
576 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
577 "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision
);
578 } else switch ((buf
->precision
= precision
)) {
580 ion_ts_frac_from_usec(&buf
->fraction
, ts
->time
->us
, ctx
);
583 buf
->seconds
= ts
->time
->s
;
586 buf
->minutes
= ts
->time
->i
;
589 buf
->hours
= ts
->time
->h
;
590 buf
->day
= ts
->time
->d
;
593 buf
->month
= ts
->time
->m
;
596 buf
->year
= ts
->time
->y
;
599 buf
->tz_offset
= ts
->time
->z
/ 60;
605 static inline void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
608 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
610 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
612 fmt
= php_dt_format_from_precision(precision
);
613 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
614 zend_string_release(fmt
);
617 static inline void php_ion_timestamp_dtor(php_ion_timestamp
*obj
)
620 timelib_time_dtor(obj
->time
);
624 php_ion_decl(timestamp
, Timestamp
, php_ion_timestamp_dtor(obj
));
626 typedef struct php_ion_catalog
{
631 php_ion_decl(catalog
, Catalog
);
633 typedef struct php_ion_reader_options
{
634 ION_READER_OPTIONS opt
;
635 zend_object
*cat
, *dec_ctx
, *cb
, std
;
636 } php_ion_reader_options
;
638 php_ion_decl(reader_options
, Reader_Options
);
640 typedef struct php_ion_reader
{
654 zend_object
*opt
, std
;
657 static inline iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
659 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
660 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
662 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
663 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
664 user
->limit
-= remaining
;
667 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
670 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
671 if (EXPECTED(read
> 0)) {
676 if (EXPECTED(read
== 0)) {
680 return IERR_READ_ERROR
;
683 static inline iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
686 php_ion_reader
*obj
= php_ion_obj(reader
, context
);
692 static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER
= {
697 static inline void php_ion_reader_ctor(php_ion_reader
*obj
)
700 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
703 opt
->opt
.context_change_notifier
.context
= obj
;
705 if (obj
->type
== STREAM_READER
) {
706 PTR_CHECK(obj
->stream
.ptr
);
707 GC_ADDREF(obj
->stream
.ptr
->res
);
709 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
710 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
711 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
712 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
715 err
= ion_reader_open_buffer(&obj
->reader
,
716 (BYTE
*) obj
->buffer
->val
, obj
->buffer
->len
,
717 opt
? &opt
->opt
: NULL
);
720 opt
->opt
.context_change_notifier
.context
= NULL
;
726 static inline void php_ion_reader_dtor(php_ion_reader
*obj
)
729 ion_reader_close(obj
->reader
);
731 if (obj
->type
== STREAM_READER
) {
732 if (obj
->stream
.buf
.value
) {
733 efree(obj
->stream
.buf
.value
);
735 if (obj
->stream
.ptr
) {
736 zend_list_delete(obj
->stream
.ptr
->res
);
740 zend_string_release(obj
->buffer
);
745 php_ion_decl(reader
, Reader_Reader
, php_ion_reader_dtor(obj
));
747 typedef struct php_ion_writer_options
{
748 ION_WRITER_OPTIONS opt
;
749 zend_object
*cat
, *dec_ctx
, *col
, std
;
750 } php_ion_writer_options
;
752 php_ion_decl(writer_options
, Writer_Options
);
754 typedef struct php_ion_writer
{
770 zend_object
*opt
, std
;
774 static inline iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
776 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
778 if (EXPECTED(user
->limit
&& user
->curr
)) {
779 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
780 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
781 return IERR_WRITE_ERROR
;
784 user
->curr
= writer
->stream
.buf
.value
;
785 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
789 #define REF_STR() do { \
790 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
791 GC_ADDREF(obj->buffer.str.s); \
794 #define NEW_REF_STR() do {\
795 if (Z_STR_P(ref) != obj->buffer.str.s) { \
796 zval_ptr_dtor(ref); \
801 static inline void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
803 PTR_CHECK(obj
->stream
.ptr
);
804 GC_ADDREF(obj
->stream
.ptr
->res
);
806 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
807 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
809 static inline void php_ion_writer_buffer_init(php_ion_writer
*obj
)
811 zval
*ref
= &obj
->buffer
.val
;
814 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
815 smart_str_0(&obj
->buffer
.str
);
819 static inline void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
821 zval
*ref
= &obj
->buffer
.val
;
824 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
829 // we've been separated
830 GC_ADDREF(obj
->buffer
.str
.s
);
833 // we have to separate
834 fprintf(stderr
, "SEPARATE\n");
835 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
839 zend_string
*old
= obj
->buffer
.str
.s
;
841 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
842 if (old
== obj
->buffer
.str
.s
) {
844 } else if(old
== Z_STR_P(ref
)) {
851 static inline iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
853 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
856 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
857 smart_str_0(&writer
->buffer
.str
);
858 if (user
->limit
== user
->curr
) {
859 php_ion_writer_buffer_grow(writer
);
862 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
863 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
868 static inline void php_ion_writer_ctor(php_ion_writer
*obj
)
871 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
874 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
875 ION_STREAM_HANDLER h
;
876 if (obj
->type
== STREAM_WRITER
) {
877 h
= php_ion_writer_stream_handler
;
878 php_ion_writer_stream_init(obj
, opt
);
880 h
= php_ion_writer_buffer_handler
;
881 php_ion_writer_buffer_init(obj
);
884 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
888 static inline void php_ion_writer_dtor(php_ion_writer
*obj
)
891 ion_writer_close(obj
->writer
);
893 if (obj
->type
== STREAM_WRITER
) {
894 if (obj
->stream
.buf
.value
) {
895 efree(obj
->stream
.buf
.value
);
897 if (obj
->stream
.ptr
) {
898 zend_list_delete(obj
->stream
.ptr
->res
);
901 if (obj
->buffer
.str
.s
) {
902 smart_str_0(&obj
->buffer
.str
);
903 zend_string_release(obj
->buffer
.str
.s
);
905 zval_ptr_dtor(&obj
->buffer
.val
);
909 php_ion_decl(writer
, Writer_Writer
, php_ion_writer_dtor(obj
));
911 typedef struct php_ion_serializer_php
{
912 php_ion_serializer serializer
;
913 zend_object
*opt
, std
;
914 } php_ion_serializer_php
;
916 static inline void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
918 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
919 ser_obj
->serializer
.ids
= global_ser
->ids
;
920 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
922 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
923 ser_obj
->serializer
.call_magic
);
924 if (ser_obj
->serializer
.call_custom
) {
925 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
926 ser_obj
->serializer
.call_custom
);
927 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
929 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
932 php_ion_writer_options
*o_woptions
= php_ion_obj(writer_options
, ser_obj
->opt
);
933 ser_obj
->serializer
.options
= &o_woptions
->opt
;
934 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
936 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("writerOptions"));
940 static inline void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
942 if (obj
->serializer
.call_custom
) {
943 zend_string_release(obj
->serializer
.call_custom
);
947 static inline void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
949 static inline void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool props
)
951 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
955 zend_string
*k
= NULL
;
956 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
960 const char *class_name
, *prop_name
;
961 if (props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
962 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("p"))));
963 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, class_name
, prop_name
- class_name
- 1)));
968 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, prop_name
, prop_len
)));
970 char buf
[MAX_LENGTH_OF_LONG
+ 1], *end
= buf
+ sizeof(buf
) - 1;
971 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
972 ION_CHECK(ion_writer_write_field_name(ser
->writer
, ion_string_from_cstr(&is
, ptr
, end
- ptr
)));
975 php_ion_serialize_zval(ser
, v
);
977 ZEND_HASH_FOREACH_END();
979 ION_CHECK(ion_writer_finish_container(ser
->writer
));
982 static inline void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
984 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
987 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
988 php_ion_serialize_zval(ser
, v
);
990 ZEND_HASH_FOREACH_END();
992 ION_CHECK(ion_writer_finish_container(ser
->writer
));
995 static inline void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
997 if (zend_array_is_list(arr
)) {
998 php_ion_serialize_list(ser
, arr
);
1000 php_ion_serialize_struct(ser
, arr
, false);
1004 static inline void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1010 ZVAL_OBJ(&tmp
, zobject
);
1011 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1013 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("S"))));
1014 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1015 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_cstr(&is
, (char *) buf
, len
)));
1017 } else if (!EG(exception
)){
1018 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1019 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1023 static inline void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1028 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1031 if (IS_ARRAY
== Z_TYPE(rv
)) {
1033 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, fn
? "C" : "O", 1)));
1034 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1035 php_ion_serialize_zval(ser
, &rv
);
1038 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1039 "%s serializer %s::%s did not return an array",
1040 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1041 fn
? fn
->common
.function_name
->val
: "__serialize");
1045 static inline void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1048 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("E"))));
1050 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1051 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1052 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1055 static inline void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1059 if (zobject
->ce
!= zend_standard_class_def
) {
1060 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("c"))));
1061 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1063 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("o"))));
1067 ZVAL_OBJ(&zobj
, zobject
);
1068 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1070 php_ion_serialize_struct(ser
, props
, true);
1071 zend_release_properties(props
);
1073 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1074 "Could not get properties for serialization of class %s",
1075 zobject
->ce
->name
->val
);
1079 static inline void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1081 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1082 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1083 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1085 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1088 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1091 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1092 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1097 static inline bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1099 return ce
->__serialize
&& ser
->call_magic
;
1102 static inline bool can_call_iface_serialize(php_ion_serializer
*, zend_class_entry
*ce
)
1104 return !!ce
->serialize
;
1107 static inline bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1109 if (ser
->call_custom
) {
1110 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1115 static inline void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1118 zend_class_entry
*ce
= zobject
->ce
;
1121 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1122 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1123 "Serializing %s is not allowed", ce
->name
->val
);
1127 if (can_call_magic_serialize(ser
, ce
)) {
1128 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1129 } else if (can_call_iface_serialize(ser
, ce
)) {
1130 php_ion_serialize_object_iface(ser
, zobject
);
1131 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1132 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1133 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1134 php_ion_serialize_object_enum(ser
, zobject
);
1135 } else if (ce
== ce_Symbol
) {
1136 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1137 } else if (ce
== ce_Decimal
) {
1138 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1139 } else if (ce
== ce_Timestamp
) {
1141 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1142 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1143 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1144 } else if (ce
== ce_LOB
) {
1145 php_ion_serialize_object_lob(ser
, zobject
);
1147 php_ion_serialize_object_std(ser
, zobject
);
1151 static inline void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1153 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1156 if (zend_hash_index_exists(ser
->ids
, idx
)) {
1157 zval
*num
= zend_hash_index_find(ser
->ids
, idx
);
1159 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("r"))));
1160 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(num
)));
1164 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1165 zend_hash_index_add(ser
->ids
, idx
, &num
);
1168 zend_hash_next_index_insert(ser
->tmp
, zv
);
1170 switch (Z_TYPE_P(zv
)) {
1172 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1176 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1180 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1184 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("R"))));
1185 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1191 static inline void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1195 switch (Z_TYPE_P(zv
)) {
1197 ION_CHECK(ion_writer_write_null(ser
->writer
));
1200 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1203 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1206 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1209 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1215 php_ion_serialize_refcounted(ser
, zv
);
1218 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1219 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1223 php_ion_decl(serializer_php
, Serializer_PHP
, php_ion_serializer_php_dtor(obj
));
1225 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1227 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1230 zo_ser
= create_ion_Serializer_PHP(NULL
);
1231 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1233 o_ser
->serializer
.call_magic
= true;
1234 php_ion_serializer_php_ctor(o_ser
);
1236 ser
= &o_ser
->serializer
;
1239 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1240 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1241 writer
->type
= BUFFER_WRITER
;
1244 zo_opt
= writer
->opt
= create_ion_Writer_Options(NULL
);
1245 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1248 php_ion_writer_ctor(writer
);
1249 ser
->writer
= writer
->writer
;
1250 ser
->buffer
= &writer
->buffer
.str
;
1252 /* start off with a global PHP annotation instead of repeating it all over the place */
1253 if (0 == php_ion_globals_serializer_step()) {
1255 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_cstr(&is
, ZEND_STRL("PHP"))),
1256 if (zo_ser
) OBJ_RELEASE(zo_ser
));
1258 php_ion_serialize_zval(ser
, zv
);
1259 php_ion_globals_serializer_exit();
1261 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1262 ion_writer_flush(ser
->writer
, NULL
);
1263 RETVAL_STR_COPY(ser
->buffer
->s
);
1265 OBJ_RELEASE(zo_writer
);
1267 OBJ_RELEASE(zo_opt
);
1270 OBJ_RELEASE(zo_ser
);
1274 typedef struct php_ion_unserializer_php
{
1275 php_ion_unserializer unserializer
;
1276 zend_object
*opt
, std
;
1277 } php_ion_unserializer_php
;
1279 static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1281 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1282 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1283 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1284 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1286 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1287 ser_obj
->unserializer
.call_magic
);
1288 if (ser_obj
->unserializer
.call_custom
) {
1289 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1290 ser_obj
->unserializer
.call_custom
);
1291 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1293 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1296 php_ion_reader_options
*o_roptions
= php_ion_obj(reader_options
, ser_obj
->opt
);
1297 ser_obj
->unserializer
.options
= &o_roptions
->opt
;
1298 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1300 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("readerOptions"));
1304 static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1306 if (obj
->unserializer
.call_custom
) {
1307 zend_string_release(obj
->unserializer
.call_custom
);
1311 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1313 static inline bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1315 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1318 static inline bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1320 return (ce
&& ce
->unserialize
);
1323 static inline bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1325 if (ser
->call_custom
) {
1326 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
));
1331 static inline zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1333 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1336 object_init_ex(return_value
, ce
);
1337 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1340 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1341 "Could not find class %s", ser
->annotations
.object_class
->val
);
1345 static inline void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1347 zend_string
*zs_case
= zval_get_string(return_value
);
1350 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1351 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
1352 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1353 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
1356 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
1357 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1358 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
1361 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
1362 zend_hash_next_index_insert(ser
->ids
, return_value
);
1363 zend_string_release(zs_case
);
1366 static inline void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
1368 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1369 if (can_call_iface_unserialize(ser
, ce
)) {
1370 zend_string
*s
= zval_get_string(return_value
);
1371 ZVAL_NULL(return_value
);
1372 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
1373 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
1374 RETVAL_ZVAL(backref
, 0, 0);
1375 } else if (!EG(exception
)) {
1376 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1377 "Failed to unserialize class %s", ce
->name
->val
);
1379 zend_string_release(s
);
1381 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1382 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
1386 static inline void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
1388 // FIXME: symbol table
1390 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
1393 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
1394 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
1395 name
= is_ptr
->value
;
1396 } else if (is_ptr
) {
1397 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
1398 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
1400 *key
= zend_string_init(ptr
, end
- ptr
, 0);
1404 *key
= zend_string_from_ion(&name
);
1407 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
1409 zend_hash_next_index_insert(ser
->ids
, return_value
);
1411 ION_CHECK(ion_reader_step_in(ser
->reader
));
1415 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1417 if (typ
== tid_EOF
) {
1422 php_ion_unserialize_field_name(ser
, &key
);
1426 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1427 ION_CATCH(zend_string_release(key
));
1429 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
1430 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
1431 ce
= zend_lookup_class(ser
->annotations
.property_class
);
1433 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
1434 zval_ptr_dtor(&zvalue
);
1435 zend_string_release(key
);
1438 ION_CHECK(ion_reader_step_out(ser
->reader
));
1441 static inline void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
1443 zend_hash_next_index_insert(ser
->ids
, return_value
);
1445 ION_CHECK(ion_reader_step_in(ser
->reader
));
1449 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1451 if (typ
== tid_EOF
) {
1456 php_ion_unserialize_field_name(ser
, &key
);
1460 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
1461 ION_CATCH(zend_string_release(key
));
1463 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
1465 zend_string_release(key
);
1468 ION_CHECK(ion_reader_step_out(ser
->reader
));
1471 static inline void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1473 switch (ser
->annotations
.object_type
) {
1479 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
1480 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1481 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
1486 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
1487 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1488 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
1490 *fn
= zobject
->ce
->__unserialize
;
1494 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1495 "Invalid object type %c", ser
->annotations
.object_type
);
1499 static inline void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
1501 // backup possible backref to array returned by magic/custom __serialize()
1502 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
1504 php_ion_unserialize_class(ser
, return_value
);
1507 zend_function
*fn
= NULL
;
1508 zend_object
*zobject
= Z_OBJ_P(return_value
);
1509 verify_unserializer(ser
, zobject
, &fn
);
1514 php_ion_unserialize_props(ser
, return_value
);
1519 if (Z_TYPE_P(input
) != IS_ARRAY
) {
1520 zval_ptr_dtor(input
);
1522 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
1523 php_ion_unserialize_hash(ser
, input
);
1528 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
1532 static inline void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
1534 if (ser
->annotations
.object_class
) {
1535 switch (ser
->annotations
.object_type
) {
1537 php_ion_unserialize_object_iface(ser
, return_value
);
1540 php_ion_unserialize_object_enum(ser
, return_value
);
1543 php_ion_unserialize_object(ser
, return_value
);
1545 } else if (!ser
->annotations
.object_type
) {
1546 array_init(return_value
);
1547 php_ion_unserialize_hash(ser
, return_value
);
1548 } else if (ser
->annotations
.object_type
== 'o') {
1549 object_init(return_value
);
1550 php_ion_unserialize_hash(ser
, return_value
);
1552 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
1553 "Invalid object annotation %c::", ser
->annotations
.object_type
);
1557 static inline void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
1559 ION_CHECK(ion_reader_step_in(ser
->reader
));
1560 array_init(return_value
);
1561 zend_hash_next_index_insert(ser
->ids
, return_value
);
1565 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
1567 if (typ
== tid_EOF
) {
1572 php_ion_unserialize_zval(ser
, &next
, &typ
);
1575 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
1578 ION_CHECK(ion_reader_step_out(ser
->reader
));
1581 static inline void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
1583 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
1586 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, zstr
->len
, &read
);
1587 if (err
== IERR_BUFFER_TOO_SMALL
) {
1588 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
1591 ION_CHECK(err
, zend_string_release(zstr
));
1592 if (zstr
->len
> read
) {
1593 zstr
->val
[read
] = 0;
1594 zstr
= zend_string_truncate(zstr
, read
, 0);
1599 static inline void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
1602 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
1604 object_init_ex(return_value
, ce_Timestamp
);
1605 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
1607 zend_string
*fmt
= NULL
;
1608 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
1609 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
1610 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
1611 zend_string_release(fmt
);
1616 static inline void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
1618 ION_INT
*num
= NULL
;
1619 ION_CHECK(ion_int_alloc(reader
, &num
));
1620 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
1622 // TODO: SIZEOF_ZEND_LONG == 4
1624 iERR err
= ion_int_to_int64(num
, &i64
);
1630 case IERR_NUMERIC_OVERFLOW
:
1632 ION_CHECK(ion_int_char_length(num
, &max
));
1633 zend_string
*zs
= zend_string_alloc(max
-1, 0);
1635 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
1636 ZEND_ASSERT(len
== zs
->len
);
1647 static inline void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
1649 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
1652 ZVAL_COPY_VALUE(return_value
, backref
);
1653 zend_hash_next_index_insert(ser
->addref
, return_value
);
1655 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
1656 "Could not find back reference %ld", Z_LVAL_P(return_value
));
1660 static inline void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
1662 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
1665 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
1666 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
1668 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
1670 if (ann_str
.length
!= 1) {
1674 switch (*ann_str
.value
) {
1676 if (ser
->annotations
.makeref
) {
1677 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1678 "Invalid multiple reference annotations");
1681 ser
->annotations
.makeref
= true;
1685 if (ser
->annotations
.backref
) {
1686 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1687 "Invalid multiple back reference annotations");
1690 ser
->annotations
.backref
= true;
1694 if (ser
->annotations
.object_prop
) {
1695 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1696 "Invalid multiple object property annotations");
1699 ser
->annotations
.object_prop
= true;
1701 ION_STRING prop_class
;
1702 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
1703 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
1706 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
1707 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
1716 if (ser
->annotations
.object_type
) {
1717 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1718 "Invalid multiple object type annotations: %c::%c",
1719 ser
->annotations
.object_type
, *ann_str
.value
);
1722 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
1723 ION_STRING class_name
;
1724 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
1725 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
1728 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
1729 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
1735 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
1736 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1737 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
1740 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
1741 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1742 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
1748 static inline void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
1751 memcpy(&ser
->type
, typ
, sizeof(ser
->type
));
1754 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
1757 php_ion_unserialize_annotations(ser
);
1760 if (ser
->annotations
.makeref
) {
1761 ZVAL_MAKE_REF(return_value
);
1762 zend_hash_next_index_insert(ser
->ids
, return_value
);
1763 ZVAL_DEREF(return_value
);
1766 if (ION_TYPE_INT(*typ
) > 0) {
1768 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
1774 switch (ION_TYPE_INT(*typ
)) {
1776 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
1781 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
1785 php_ion_reader_read_int(ser
->reader
, return_value
);
1786 if (ser
->annotations
.backref
) {
1788 php_ion_unserialize_backref(ser
, return_value
);
1790 if (ser
->annotations
.object_type
) {
1791 if (!ser
->annotations
.backref
) {
1792 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
1793 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
1794 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
1797 goto unserialize_struct
;
1803 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
1806 case tid_DECIMAL_INT
:
1807 object_init_ex(return_value
, ce_Decimal
);
1808 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
1809 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
1810 php_ion_decimal_ctor(dec
);
1811 zend_hash_next_index_insert(ser
->ids
, return_value
);
1814 case tid_TIMESTAMP_INT
:
1815 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
1816 zend_hash_next_index_insert(ser
->ids
, return_value
);
1819 case tid_SYMBOL_INT
:
1821 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
1822 php_ion_symbol_zval(&sym
, return_value
);
1823 if (ser
->annotations
.object_type
) {
1824 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1825 goto unserialize_struct
;
1827 zend_hash_next_index_insert(ser
->ids
, return_value
);
1830 case tid_STRING_INT
:
1832 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
1833 RETVAL_STRINGL((char *) str
.value
, str
.length
);
1834 if (ser
->annotations
.object_type
) {
1835 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1836 goto unserialize_struct
;
1838 zend_hash_next_index_insert(ser
->ids
, return_value
);
1843 php_ion_reader_read_lob(ser
->reader
, return_value
);
1844 if (ser
->annotations
.object_type
) {
1845 zend_hash_next_index_insert(ser
->tmp
, return_value
);
1846 goto unserialize_struct
;
1848 zend_hash_next_index_insert(ser
->ids
, return_value
);
1852 case tid_SEXP_INT
: // FIXME
1853 php_ion_unserialize_list(ser
, return_value
);
1854 if (!ser
->annotations
.object_type
) {
1859 case tid_STRUCT_INT
:
1860 unserialize_struct
: ;
1861 php_ion_unserialize_struct(ser
, return_value
);
1868 case tid_DATAGRAM_INT
:
1869 ZEND_ASSERT(!"datagram");
1875 php_ion_decl(unserializer_php
, Unserializer_PHP
, php_ion_unserializer_php_dtor(obj
));
1877 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
1879 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1882 zo_ser
= create_ion_Unserializer_PHP(NULL
);
1883 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
1885 o_ser
->unserializer
.call_magic
= true;
1886 php_ion_unserializer_php_ctor(o_ser
);
1888 ser
= &o_ser
->unserializer
;
1891 zend_object
*zo_reader
;
1892 php_ion_reader
*reader
;
1895 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
1896 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
1897 reader
= php_ion_obj(reader
, zo_reader
);
1898 reader
->type
= STREAM_READER
;
1899 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
1900 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
1901 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
1902 reader
= php_ion_obj(reader
, zo_reader
);
1903 reader
->type
= BUFFER_READER
;
1904 reader
->buffer
= zval_get_string(zdata
);
1906 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1907 "Invalid source to unserialize; expected string or resource");
1909 OBJ_RELEASE(zo_ser
);
1915 zo_opt
= reader
->opt
= create_ion_Reader_Options(NULL
);
1916 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
1919 php_ion_reader_ctor(reader
);
1920 ser
->reader
= reader
->reader
;
1922 php_ion_globals_unserializer_step();
1923 php_ion_unserialize_zval(ser
, return_value
, NULL
);
1924 php_ion_globals_unserializer_exit();
1926 OBJ_RELEASE(zo_reader
);
1928 OBJ_RELEASE(zo_opt
);
1931 OBJ_RELEASE(zo_ser
);