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 +--------------------------------------------------------------------+
14 #include "ext/date/php_date.h"
19 # define LOCAL static inline
22 #define DECNUMDIGITS 34 /* DECQUAD_Pmax */
25 #define PHP_ION_SYMBOL_TABLE_VERSION 1
26 #define PHP_ION_SYMBOL(c, s) { \
29 { sizeof(s)-1, (BYTE *) s }, \
33 { sizeof(c)-1, (BYTE *) c } \
36 typedef struct php_ion_global_symbol
{
39 } php_ion_global_symbol
;
41 static php_ion_global_symbol g_sym_tab_php_sym
[] = {
42 #define PHP_ION_SYMBOL_PHP (g_sym_tab_php_sym[0]).s
43 PHP_ION_SYMBOL("PHP", "PHP")
44 #define PHP_ION_SYMBOL_REFERENCE (g_sym_tab_php_sym[1]).s
45 PHP_ION_SYMBOL("Reference", "R")
46 #define PHP_ION_SYMBOL_BACKREF (g_sym_tab_php_sym[2]).s
47 PHP_ION_SYMBOL("Backref", "r")
48 #define PHP_ION_SYMBOL_PROPERTY (g_sym_tab_php_sym[3]).s
49 PHP_ION_SYMBOL("Property", "p")
50 #define PHP_ION_SYMBOL_CLASS_OBJECT (g_sym_tab_php_sym[4]).s
51 PHP_ION_SYMBOL("ClassObject", "c")
52 #define PHP_ION_SYMBOL_CUSTOM_OBJECT (g_sym_tab_php_sym[5]).s
53 PHP_ION_SYMBOL("CustomObject", "C")
54 #define PHP_ION_SYMBOL_OBJECT (g_sym_tab_php_sym[6]).s
55 PHP_ION_SYMBOL("Object", "o")
56 #define PHP_ION_SYMBOL_MAGIC_OBJECT (g_sym_tab_php_sym[7]).s
57 PHP_ION_SYMBOL("MagicObject", "O")
58 #define PHP_ION_SYMBOL_SERIALIZEABLE (g_sym_tab_php_sym[8]).s
59 PHP_ION_SYMBOL("Serializable", "S")
60 #define PHP_ION_SYMBOL_ENUM (g_sym_tab_php_sym[9]).s
61 PHP_ION_SYMBOL("Enum", "E")
66 static ION_SYMBOL_TABLE
*g_sym_tab_php
;
68 /* [SID => STRING, STRING => SID] */
69 static HashTable g_sym_hash
;
70 /* [enum_case_name => SID] */
71 static HashTable g_sym_map
;
73 #define ION_SYS_SYMBOL_SYMBOL_TABLE \
74 ION_SYS_SYMBOL_ION_SYMBOL_TABLE
75 #define g_sym_add(enum_name, c_name) do { \
76 g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \
77 g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \
80 LOCAL
void g_sym_hash_add(int sid
, const char *str
, size_t len
)
84 ZVAL_STR(&zs
, zend_string_init_interned(str
, len
,1));
85 zend_hash_add(&g_sym_hash
, Z_STR(zs
), &zl
);
86 zend_hash_index_add(&g_sym_hash
, sid
, &zs
);
89 LOCAL
void g_sym_map_add(int sid
, const char *str
, size_t len
)
93 zend_hash_str_add(&g_sym_map
, str
, len
, &zv
);
96 LOCAL
int g_sym_init(void)
98 zend_hash_init(&g_sym_hash
, 0, NULL
, NULL
, 1);
99 zend_hash_init(&g_sym_map
, 0, NULL
, NULL
, 1);
101 g_sym_hash_add(0, ZEND_STRL(""));
102 g_sym_map_add(0, ZEND_STRL(""));
103 g_sym_add("Ion", ION
);
104 g_sym_add("Ivm_1_0", IVM
);
105 g_sym_add("IonSymbolTable", SYMBOL_TABLE
);
106 g_sym_add("Name", NAME
);
107 g_sym_add("Version", VERSION
);
108 g_sym_add("Imports", IMPORTS
);
109 g_sym_add("Symbols", SYMBOLS
);
110 g_sym_add("MaxId", MAX_ID
);
111 g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE
);
113 int sys_max_id
= ION_SYS_SID_SHARED_SYMBOL_TABLE
;
115 if (IERR_OK
!= ion_symbol_table_open_with_type(&g_sym_tab_php
, NULL
, ist_SHARED
)) {
118 php_ion_global_symbol
*ptr
= g_sym_tab_php_sym
;
119 ion_symbol_table_set_version(g_sym_tab_php
, PHP_ION_SYMBOL_TABLE_VERSION
);
120 ion_symbol_table_set_name(g_sym_tab_php
, &ptr
->s
.value
);
121 while (ptr
->e
.value
) {
122 ion_symbol_table_add_symbol(g_sym_tab_php
, &ptr
->s
.value
, &ptr
->s
.sid
);
123 g_sym_hash_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->s
.value
.value
, ptr
->s
.value
.length
);
124 g_sym_map_add(sys_max_id
+ ptr
->s
.sid
, (const char *) ptr
->e
.value
, ptr
->e
.length
);
127 ion_symbol_table_lock(g_sym_tab_php
);
131 typedef struct php_ion_serializer
{
133 ION_WRITER_OPTIONS
*options
;
136 zend_string
*call_custom
;
137 zend_bool call_magic
;
143 } php_ion_serializer
;
145 typedef struct php_ion_annotations
{
146 uint8_t shared_symtab
:1;
149 uint8_t object_prop
:1;
151 zend_string
*object_class
;
152 zend_string
*property_class
;
153 } php_ion_annotations
;
155 typedef struct php_ion_unserializer
{
157 ION_READER_OPTIONS
*options
;
160 zend_string
*call_custom
;
161 zend_bool call_magic
;
169 php_ion_annotations annotations
;
170 } php_ion_unserializer
;
172 ZEND_BEGIN_MODULE_GLOBALS(ion
)
176 ION_DECIMAL zend_max
;
177 ION_DECIMAL zend_min
;
184 php_ion_serializer serializer
;
185 php_ion_unserializer unserializer
;
188 HashTable serializer
[2];
189 HashTable unserializer
[3];
192 ZEND_END_MODULE_GLOBALS(ion
);
195 # define php_ion_globals (*((zend_ion_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(ion_globals_id)]))
197 # define php_ion_globals ion_globals
200 ZEND_DECLARE_MODULE_GLOBALS(ion
);
202 static zend_class_entry
206 *ce_Decimal_Context_Rounding
,
213 *ce_Reader_Buffer_Reader
,
214 *ce_Reader_Stream_Reader
,
218 *ce_Symbol_ImportLocation
,
221 *ce_Symbol_Table_Local
,
222 *ce_Symbol_Table_PHP
,
223 *ce_Symbol_Table_Shared
,
224 *ce_Symbol_Table_System
,
226 *ce_Timestamp_Precision
,
229 *ce_Unserializer_PHP
,
233 *ce_Writer_Buffer_Writer
,
235 *ce_Writer_Stream_Writer
,
239 LOCAL
void php_ion_globals_symbols_init(void)
241 zend_hash_init(&php_ion_globals
.symbol
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
244 LOCAL
void php_ion_globals_symbols_dtor(void)
246 zend_hash_destroy(&php_ion_globals
.symbol
.cache
);
249 LOCAL
void php_ion_globals_serializer_init(void)
251 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
252 HashTable
*h
= php_ion_globals
._ht
.serializer
;
254 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
255 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
258 LOCAL
uint32_t php_ion_globals_serializer_step(void)
260 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
263 if (!(level
= s
->level
++)) {
264 zend_hash_clean(s
->ids
);
265 zend_hash_clean(s
->tmp
);
270 LOCAL
uint32_t php_ion_globals_serializer_exit(void)
272 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
274 ZEND_ASSERT(s
->level
);
276 zend_hash_clean(s
->ids
);
277 zend_hash_clean(s
->tmp
);
282 LOCAL
void php_ion_globals_serializer_dtor(void)
284 php_ion_serializer
*s
= &php_ion_globals
.serializer
;
286 zend_hash_destroy(s
->tmp
);
287 zend_hash_destroy(s
->ids
);
290 void ZVAL_ADDREF(zval
*zv
)
293 Z_TRY_ADDREF_P(Z_REFVAL_P(zv
));
298 LOCAL
void php_ion_globals_unserializer_init(void)
300 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
301 HashTable
*h
= php_ion_globals
._ht
.unserializer
;
303 zend_hash_init(s
->tmp
= &h
[0], 0, NULL
, ZVAL_PTR_DTOR
, 0);
304 zend_hash_init(s
->ids
= &h
[1], 0, NULL
, NULL
, 0);
305 zend_hash_init(s
->addref
= &h
[2], 0, NULL
, ZVAL_ADDREF
, 0);
308 LOCAL
void php_ion_globals_unserializer_step(void)
310 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
313 zend_hash_clean(s
->addref
);
314 zend_hash_clean(s
->ids
);
315 zend_hash_clean(s
->tmp
);
319 LOCAL
void php_ion_globals_unserializer_exit(void)
321 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
323 ZEND_ASSERT(s
->level
);
325 zend_hash_clean(s
->addref
);
326 zend_hash_clean(s
->ids
);
327 zend_hash_clean(s
->tmp
);
331 LOCAL
void php_ion_globals_unserializer_dtor(void)
333 php_ion_unserializer
*s
= &php_ion_globals
.unserializer
;
335 zend_hash_destroy(s
->addref
);
336 zend_hash_destroy(s
->ids
);
337 zend_hash_destroy(s
->tmp
);
340 #define php_ion_obj(type, zo) \
341 ((php_ion_ ##type *) php_ion_obj_ex(zo, XtOffsetOf(php_ion_ ## type, std)))
342 LOCAL
void *php_ion_obj_ex(void *obj
, ptrdiff_t offset
) {
344 return ((char *) obj
) - offset
;
349 #define php_ion_decl(type, cname) \
350 static zend_object_handlers oh_ ## cname; \
351 LOCAL zend_object *create_ion_ ## cname(zend_class_entry *ce) \
353 if (!ce) ce = ce_ ## cname; \
354 php_ion_ ## type *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); \
355 zend_object_std_init(&o->std, ce); \
356 object_properties_init(&o->std, ce); \
357 o->std.handlers = &oh_ ## cname; \
360 LOCAL void free_ion_ ## cname(zend_object *std) \
362 php_ion_ ## type *obj = php_ion_obj(type, std); \
363 php_ion_ ## type ## _dtor(obj); \
364 zend_object_std_dtor(std); \
366 LOCAL zend_object *clone_ion_ ## cname(zend_object *std) \
368 php_ion_ ## type *old_obj = php_ion_obj(type, std), \
369 *new_obj = php_ion_obj(type, create_ion_ ## cname(std->ce)); \
370 php_ion_ ## type ## _copy(new_obj, old_obj); \
371 return &new_obj->std; \
373 #define php_ion_register(type, cname, ...) do { \
374 ce_ ## cname = register_class_ion_ ## cname(__VA_ARGS__); \
375 ce_ ## cname ->create_object = create_ion_ ## cname; \
376 memcpy(&oh_ ## cname, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
377 oh_ ## cname .offset = offsetof(php_ion_ ## type, std); \
378 oh_ ## cname .free_obj = free_ion_ ## cname; \
379 oh_ ## cname .clone_obj = clone_ion_ ## cname; \
382 #define ION_CHECK_RETURN(r, err, ...) do { \
385 zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \
391 #define ION_CHECK(err, ...) \
392 ION_CHECK_RETURN(, err, __VA_ARGS__)
394 #define ION_CATCH(...) do { \
395 if (EG(exception)) { \
401 #define PTR_CHECK_RETURN(ret, ptr, ...) do { \
403 zend_throw_error(NULL, "Uninitialized object"); \
408 #define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__)
410 #define OBJ_CHECK_RETURN(ret, obj, ...) do { \
411 PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \
412 PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \
414 #define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__)
416 LOCAL ION_STRING
*ion_string_from_zend(ION_STRING
*is
, const zend_string
*zs
)
418 is
->length
= zs
? (SIZE
) zs
->len
: 0;
419 is
->value
= (BYTE
*) (zs
? zs
->val
: NULL
);
423 LOCAL zend_string
*zend_string_from_ion(const ION_STRING
*s
)
425 return zend_string_init((const char *) s
->value
, s
->length
, 0);
428 LOCAL
void update_property_obj(zend_object
*obj
, const char *n
, size_t l
, zend_object
*p
)
432 zend_update_property(obj
->ce
, obj
, n
, l
, &zobj
);
435 LOCAL zend_object
*get_property_obj(zend_object
*obj
, const char *n
, size_t l
, int type
)
437 zval tmp
, *zv
= zend_read_property(obj
->ce
, obj
, n
, l
, 0, &tmp
);
438 if (zv
&& type
& Z_TYPE_P(zv
)) {
444 #define RETURN_IONTYPE(typ) do { \
445 zend_object *__zo = php_ion_type_fetch(typ); \
446 if (UNEXPECTED(!__zo)) { \
449 RETURN_OBJ_COPY(__zo); \
452 LOCAL zend_object
*php_ion_type_fetch(ION_TYPE typ
)
454 zend_long index
= ION_TYPE_INT(typ
);
455 zval
*ztype
= zend_hash_index_find(ce_Type
->backed_enum_table
, index
);
457 if (UNEXPECTED(!ztype
|| Z_TYPE_P(ztype
) != IS_STRING
)) {
458 zend_value_error(ZEND_LONG_FMT
" is not a valid backing value for enum \"%s\"", index
, ZSTR_VAL(ce_Type
->name
));
461 return zend_enum_get_case(ce_Type
, Z_STR_P(ztype
));
464 LOCAL ION_TYPE
ion_type_from_enum(zend_object
*zo
)
466 return (ION_TYPE
) Z_LVAL_P(zend_enum_fetch_case_value(zo
));
469 typedef struct php_ion_symbol_iloc
{
470 ION_SYMBOL_IMPORT_LOCATION loc
;
473 } php_ion_symbol_iloc
;
475 LOCAL
void php_ion_symbol_iloc_ctor(php_ion_symbol_iloc
*obj
)
477 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("location"), obj
->loc
.location
);
478 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), obj
->name
);
479 ion_string_from_zend(&obj
->loc
.name
, obj
->name
);
482 LOCAL
void php_ion_symbol_iloc_dtor(php_ion_symbol_iloc
*obj
)
484 zend_string_release(obj
->name
);
487 LOCAL
void php_ion_symbol_iloc_copy(php_ion_symbol_iloc
*new_obj
, php_ion_symbol_iloc
*old_obj
)
489 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
490 new_obj
->name
= zend_string_copy(old_obj
->name
);
491 ion_string_from_zend(&new_obj
->loc
.name
, new_obj
->name
);
492 new_obj
->loc
.location
= old_obj
->loc
.location
;
495 php_ion_decl(symbol_iloc
, Symbol_ImportLocation
);
497 typedef struct php_ion_symbol
{
500 zend_object
*iloc
, std
;
503 LOCAL
int php_ion_symbol_zval_compare(zval
*zv1
, zval
*zv2
) {
504 zend_string
*zs1
= zval_get_string(zv1
);
505 zend_string
*zs2
= zval_get_string(zv2
);
512 if (zs1
->len
> zs2
->len
) {
514 } else if (zs2
->len
> zs1
->len
) {
517 result
= memcmp(zs1
->val
, zs2
->val
, zs1
->len
);
519 zend_string_release(zs1
);
520 zend_string_release(zs2
);
524 LOCAL
void php_ion_symbol_ctor(php_ion_symbol
*obj
)
526 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("sid"),
529 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"), obj
->value
);
531 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("value"));
533 ion_string_from_zend(&obj
->sym
.value
, obj
->value
);
535 update_property_obj(&obj
->std
, ZEND_STRL("importLocation"), obj
->iloc
);
536 obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, obj
->iloc
)->loc
;
538 zend_update_property_null(obj
->std
.ce
, &obj
->std
, ZEND_STRL("importLocation"));
542 LOCAL
void php_ion_symbol_dtor(php_ion_symbol
*obj
)
545 zend_string_release(obj
->value
);
549 LOCAL
void php_ion_symbol_copy(php_ion_symbol
*new_obj
, php_ion_symbol
*old_obj
)
551 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
552 new_obj
->sym
= old_obj
->sym
;
553 if (old_obj
->value
) {
554 new_obj
->value
= zend_string_copy(old_obj
->value
);
555 ion_string_from_zend(&new_obj
->sym
.value
, new_obj
->value
);
558 if ((new_obj
->iloc
= get_property_obj(&new_obj
->std
, ZEND_STRL("importLocation"), IS_OBJECT
))) {
559 new_obj
->sym
.import_location
= php_ion_obj(symbol_iloc
, new_obj
->iloc
)->loc
;
563 LOCAL
void php_ion_symbol_zval(ION_SYMBOL
*sym_ptr
, zval
*return_value
)
565 object_init_ex(return_value
, ce_Symbol
);
566 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ_P(return_value
));
568 sym
->sym
.sid
= sym_ptr
->sid
;
569 sym
->value
= zend_string_from_ion(&sym_ptr
->value
);
570 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
572 object_init_ex(&ziloc
, ce_Symbol_ImportLocation
);
573 sym
->iloc
= Z_OBJ(ziloc
);
575 php_ion_symbol_iloc
*iloc
= php_ion_obj(symbol_iloc
, sym
->iloc
);
576 iloc
->loc
.location
= sym_ptr
->import_location
.location
;
577 iloc
->name
= zend_string_from_ion(&sym_ptr
->import_location
.name
);
579 php_ion_symbol_iloc_ctor(iloc
);
582 php_ion_symbol_ctor(sym
);
584 if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr
)) {
585 GC_DELREF(sym
->iloc
);
589 LOCAL zval
*php_ion_global_symbol_fetch_by_enum(zend_string
*name
)
591 zval
*zgs
= zend_hash_find(&php_ion_globals
.symbol
.cache
, name
);
593 zval
*zid
= zend_hash_find(&g_sym_map
, name
);
595 zval
*zss
= zend_hash_index_find(&g_sym_hash
, Z_LVAL_P(zid
));
598 object_init_ex(&zsym
, ce_Symbol
);
599 php_ion_symbol
*sym
= php_ion_obj(symbol
, Z_OBJ(zsym
));
600 sym
->sym
.sid
= Z_LVAL_P(zid
);
601 sym
->value
= zval_get_string(zss
);
602 php_ion_symbol_ctor(sym
);
603 zgs
= zend_hash_add(&php_ion_globals
.symbol
.cache
, name
, &zsym
);
610 php_ion_decl(symbol
, Symbol
);
612 typedef struct php_ion_symbol_table
{
613 ION_SYMBOL_TABLE
*tab
;
614 int (*dtor
)(ION_SYMBOL_TABLE
*);
616 } php_ion_symbol_table
;
618 LOCAL
void php_ion_symbol_table_ctor(php_ion_symbol_table
*obj
)
623 if (IERR_OK
== ion_symbol_table_get_name(obj
->tab
, &is
)) {
624 zend_update_property_stringl(obj
->std
.ce
, &obj
->std
, ZEND_STRL("name"), (char *) is
.value
, is
.length
);
627 if (IERR_OK
== ion_symbol_table_get_version(obj
->tab
, &iv
)) {
628 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("version"), iv
);
632 LOCAL
void php_ion_symbol_table_dtor(php_ion_symbol_table
*obj
)
642 LOCAL
void php_ion_symbol_table_copy(php_ion_symbol_table
*new_obj
, php_ion_symbol_table
*old_obj
)
644 // do not clone cache members
645 // zend_objects_clone_members(...)
646 if ((new_obj
->dtor
= old_obj
->dtor
)) {
647 ION_CHECK(ion_symbol_table_clone_with_owner(old_obj
->tab
, &new_obj
->tab
, NULL
));
649 new_obj
->tab
= old_obj
->tab
;
651 // update non-cache members
652 php_ion_symbol_table_ctor(new_obj
);
655 LOCAL
void php_ion_symbol_table_import(php_ion_symbol_table
*obj
, php_ion_symbol_table
*import
)
661 zval
*zimports
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), 0, &tmp
);
663 zend_ulong idx
= (uintptr_t) &import
->std
.gc
;
664 if (!zend_hash_index_exists(Z_ARRVAL_P(zimports
), idx
)) {
665 ION_CHECK(ion_symbol_table_import_symbol_table(obj
->tab
, import
->tab
));
667 SEPARATE_ARRAY(zimports
);
668 GC_ADDREF(&import
->std
);
669 add_index_object(zimports
, idx
, &import
->std
);
670 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("imports"), zimports
);
675 LOCAL
void php_ion_symbol_table_symbol_zval(php_ion_symbol_table
*obj
, ION_SYMBOL
*sym
, zval
*return_value
)
678 zval
*zsyms
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbols"), 0, &tmp
);
680 zval
*zsym
= zend_hash_index_find(Z_ARRVAL_P(zsyms
), sym
->sid
);
686 php_ion_symbol_zval(sym
, return_value
);
689 SEPARATE_ARRAY(zsyms
);
690 ZVAL_ADDREF(return_value
);
691 add_index_zval(zsyms
, sym
->sid
, return_value
);
695 php_ion_decl(symbol_table
, Symbol_Table
);
697 typedef struct php_ion_decimal_ctx
{
700 } php_ion_decimal_ctx
;
702 #define php_ion_decimal_ctx_init_max(c, rounding) \
703 php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false)
704 LOCAL
void php_ion_decimal_ctx_init(decContext
*ctx
,
705 int digits
, int emax
, int emin
, enum rounding round
, zend_bool clamp
)
707 memset(ctx
, 0, sizeof(*ctx
));
708 ctx
->digits
= digits
;
715 LOCAL
void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx
*obj
, zend_object
*o_round
)
717 if (!obj
->ctx
.digits
) {
718 php_ion_decimal_ctx_init_max(&obj
->ctx
, DEC_ROUND_HALF_EVEN
);
721 update_property_obj(&obj
->std
, ZEND_STRL("round"), o_round
);
723 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("round"), obj
->ctx
.round
);
725 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("digits"), obj
->ctx
.digits
);
726 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMax"), obj
->ctx
.emax
);
727 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("eMin"), obj
->ctx
.emin
);
728 zend_update_property_bool(obj
->std
.ce
, &obj
->std
, ZEND_STRL("clamp"), obj
->ctx
.clamp
);
731 LOCAL
void php_ion_decimal_ctx_dtor(php_ion_decimal_ctx
*obj
)
735 LOCAL
void php_ion_decimal_ctx_copy(php_ion_decimal_ctx
*new_obj
, php_ion_decimal_ctx
*old_obj
)
737 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
738 new_obj
->ctx
= old_obj
->ctx
;
741 php_ion_decl(decimal_ctx
, Decimal_Context
);
743 typedef struct php_ion_decimal
{
745 zend_object
*ctx
, std
;
748 LOCAL
void php_ion_decimal_from_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long num
)
750 if (num
<= INT32_MAX
&& num
>= INT32_MIN
) {
751 ION_CHECK(ion_decimal_from_int32(dec
, num
));
752 } else if (num
> 0 && num
<= UINT32_MAX
) {
753 ION_CHECK(ion_decimal_from_uint32(dec
, num
));
756 ION_CHECK(ion_int_alloc(NULL
, &iint
));
757 ION_CHECK(ion_int_from_long(iint
, num
),
759 /* WATCH OUT: BS API */
760 dec
->type
= ION_DECIMAL_TYPE_QUAD
;
761 ION_CHECK(ion_decimal_from_ion_int(dec
, ctx
, iint
),
767 LOCAL zend_string
*php_ion_decimal_to_string(ION_DECIMAL
*dec
)
769 zend_string
*zstr
= zend_string_alloc(ION_DECIMAL_STRLEN(dec
), 0);
770 (void) ion_decimal_to_string(dec
, zstr
->val
);
771 return zend_string_truncate(zstr
, strlen(zstr
->val
), 0);
774 LOCAL
void php_ion_decimal_to_zend_long(ION_DECIMAL
*dec
, decContext
*ctx
, zend_long
*l
)
777 ION_CHECK(ion_int_alloc(NULL
, &ii
));
778 ION_CHECK(ion_decimal_to_ion_int(dec
, ctx
, ii
), ion_int_free(ii
));
780 ION_CHECK(ion_int_to_int64(ii
, &i64
), ion_int_free(ii
));
785 LOCAL
bool php_ion_decimal_fits_zend_long(php_ion_decimal
*obj
)
789 if (!ion_decimal_is_integer(&obj
->dec
)) {
794 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_max
, &php_ion_globals
.decimal
.ctx
, &result
);
799 ion_decimal_compare(&obj
->dec
, &php_ion_globals
.decimal
.zend_min
, &php_ion_globals
.decimal
.ctx
, &result
);
806 LOCAL
void php_ion_decimal_ctor(php_ion_decimal
*obj
)
810 object_init_ex(&zdc
, ce_Decimal_Context
);
811 obj
->ctx
= Z_OBJ(zdc
);
812 php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx
, obj
->ctx
), NULL
);
815 update_property_obj(&obj
->std
, ZEND_STRL("context"), obj
->ctx
);
817 if (php_ion_decimal_fits_zend_long(obj
)) {
819 php_ion_decimal_to_zend_long(&obj
->dec
, &php_ion_obj(decimal_ctx
, obj
->ctx
)->ctx
, &l
);
820 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), l
);
822 zend_string
*zstr
= php_ion_decimal_to_string(&obj
->dec
);
823 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("number"), zstr
);
824 zend_string_release(zstr
);
828 LOCAL
void php_ion_decimal_dtor(php_ion_decimal
*obj
)
830 ion_decimal_free(&obj
->dec
);
833 LOCAL
void php_ion_decimal_copy(php_ion_decimal
*new_obj
, php_ion_decimal
*old_obj
)
835 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
836 new_obj
->ctx
= get_property_obj(&new_obj
->std
, ZEND_STRL("context"), IS_OBJECT
);
837 ION_CHECK(ion_decimal_copy(&new_obj
->dec
, &old_obj
->dec
));
840 php_ion_decl(decimal
, Decimal
);
842 typedef php_date_obj php_ion_timestamp
;
844 LOCAL zend_long
php_usec_from_ion(const decQuad
*frac
, decContext
*ctx
)
847 ctx
= &php_ion_globals
.decimal
.ctx
;
849 decQuad microsecs
, result
;
850 decQuadMultiply(&result
, decQuadFromInt32(µsecs
, 1000000), frac
, ctx
);
851 return (zend_long
) decQuadToUInt32(&result
, ctx
, DEC_ROUND_HALF_EVEN
);
854 LOCAL decQuad
*ion_ts_frac_from_usec(decQuad
*frac
, int usec
, decContext
*ctx
)
857 ctx
= &php_ion_globals
.decimal
.ctx
;
859 decQuad microsecs
, us
;
860 return decQuadDivide(frac
, decQuadFromInt32(&us
, usec
), decQuadFromInt32(µsecs
, 1000000), ctx
);
863 LOCAL zend_string
*php_dt_format_from_precision(uint8_t precision
)
865 switch (precision
& 0x7f) {
867 return zend_string_init(ZEND_STRL("c"), 0);
869 return zend_string_init(ZEND_STRL("Y-m-d\\TH:i:sP"), 0);
871 return zend_string_init(ZEND_STRL("Y-m-d\\TH:iP"), 0);
873 return zend_string_init(ZEND_STRL("Y-m-d\\T"), 0);
875 return zend_string_init(ZEND_STRL("Y-m\\T"), 0);
877 return zend_string_init(ZEND_STRL("Y\\T"), 0);
879 return zend_string_init(ZEND_STRL("c"), 0);
883 LOCAL timelib_time
* php_time_from_ion(const ION_TIMESTAMP
*ts
, decContext
*ctx
, zend_string
**fmt
)
885 timelib_time
*time
= ecalloc(1, sizeof(*time
));
887 int precision
= ION_TS_FRAC
;
888 ion_timestamp_get_precision(ts
, &precision
);
891 time
->us
= php_usec_from_ion(&ts
->fraction
, ctx
);
894 time
->s
= ts
->seconds
;
897 time
->i
= ts
->minutes
;
910 time
->z
= ts
->tz_offset
* 60;
912 time
->zone_type
= TIMELIB_ZONETYPE_OFFSET
;
914 time
->zone_type
= TIMELIB_ZONETYPE_ID
;
915 time
->tz_info
= get_timezone_info();
920 *fmt
= php_dt_format_from_precision(precision
);
925 LOCAL ION_TIMESTAMP
*ion_timestamp_from_php(ION_TIMESTAMP
*buf
, php_ion_timestamp
*ts
, decContext
*ctx
)
927 memset(buf
, 0, sizeof(*buf
));
930 int precision
= Z_LVAL_P(zend_read_property(ts
->std
.ce
, &ts
->std
, ZEND_STRL("precision"), 0, &tmp
));
932 if (!precision
|| precision
> (ION_TS_FRAC
|0x80)) {
933 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
934 "Invalid precision (%d) of ion\\Timestamp", precision
);
935 } else switch ((buf
->precision
= precision
) & 0x7f) {
937 ion_ts_frac_from_usec(&buf
->fraction
, (int) ts
->time
->us
, ctx
);
940 buf
->seconds
= ts
->time
->s
;
943 buf
->minutes
= ts
->time
->i
;
946 buf
->hours
= ts
->time
->h
;
947 buf
->day
= ts
->time
->d
;
950 buf
->month
= ts
->time
->m
;
953 buf
->year
= ts
->time
->y
;
956 buf
->tz_offset
= (short) (ts
->time
->z
/ 60);
957 if (buf
->tz_offset
) {
958 buf
->precision
|= 0x80;
965 LOCAL
void php_ion_timestamp_ctor(php_ion_timestamp
*obj
, zend_long precision
, zend_string
*fmt
, zend_string
*dt
, zval
*tz
)
968 php_date_initialize(obj
, dt
? dt
->val
: "", dt
? dt
->len
: 0, fmt
? fmt
->val
: NULL
, tz
, PHP_DATE_INIT_CTOR
);
970 zend_update_property_long(obj
->std
.ce
, &obj
->std
, ZEND_STRL("precision"), precision
);
972 fmt
= php_dt_format_from_precision(precision
);
973 zend_update_property_str(obj
->std
.ce
, &obj
->std
, ZEND_STRL("format"), fmt
);
974 zend_string_release(fmt
);
977 typedef struct php_ion_catalog
{
982 LOCAL
void php_ion_catalog_ctor(php_ion_catalog
*obj
)
984 ION_CHECK(ion_catalog_open(&obj
->cat
));
987 LOCAL
void php_ion_catalog_dtor(php_ion_catalog
*obj
)
990 ion_catalog_close(obj
->cat
);
994 LOCAL ION_COLLECTION
*php_ion_catalog_collection(php_ion_catalog
*cat
)
996 /* do not look too close */
999 ION_SYMBOL_TABLE
*sys
;
1000 ION_COLLECTION collection
;
1001 } *cat_ptr
= (void *) cat
->cat
;
1002 return &cat_ptr
->collection
;
1004 // see https://github.com/amzn/ion-c/issues/269
1005 #ifndef IPCN_pNODE_TO_pDATA
1006 # define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0]))
1009 LOCAL
void php_ion_catalog_copy(php_ion_catalog
*new_obj
, php_ion_catalog
*old_obj
)
1011 // do not clone members; they're only caches
1013 php_ion_catalog_ctor(new_obj
);
1016 ION_COLLECTION
*col
= php_ion_catalog_collection(old_obj
);
1017 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1018 ION_COLLECTION_CURSOR cur
;
1019 ION_COLLECTION_OPEN(col
, cur
);
1021 ION_SYMBOL_TABLE
**ptr
;
1022 ION_COLLECTION_NEXT(cur
, ptr
);
1024 ION_CHECK(ion_catalog_add_symbol_table(new_obj
->cat
, *ptr
));
1030 LOCAL zend_string
*ion_symbol_table_to_key(ION_SYMBOL_TABLE
*tab
)
1034 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_name(tab
, &is
));
1035 ION_CHECK_RETURN(NULL
, ion_symbol_table_get_version(tab
, &version
));
1038 smart_str_appendl(&s
, (char *) is
.value
, is
.length
);
1039 smart_str_appendc(&s
, ':');
1040 smart_str_append_long(&s
, version
);
1046 LOCAL
void php_ion_catalog_add_symbol_table(php_ion_catalog
*obj
, php_ion_symbol_table
*tab
)
1052 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1054 zend_ulong idx
= (uintptr_t) &tab
->std
.gc
;
1055 if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs
), idx
)) {
1056 zend_string
*key
= ion_symbol_table_to_key(tab
->tab
);
1058 ION_CHECK(ion_catalog_add_symbol_table(obj
->cat
, tab
->tab
),
1059 zend_string_release(key
));
1060 SEPARATE_ARRAY(ztabs
);
1061 GC_ADDREF(&tab
->std
);
1062 add_index_object(ztabs
, idx
, &tab
->std
);
1063 GC_ADDREF(&tab
->std
);
1064 add_assoc_object_ex(ztabs
, key
->val
, key
->len
, &tab
->std
);
1065 zend_update_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), ztabs
);
1066 zend_string_release(key
);
1072 LOCAL
void php_ion_catalog_symbol_table_zval(php_ion_catalog
*obj
, ION_SYMBOL_TABLE
*tab
, zval
*return_value
)
1074 zend_string
*key
= ion_symbol_table_to_key(tab
);
1078 zval
*ztabs
= zend_read_property(obj
->std
.ce
, &obj
->std
, ZEND_STRL("symbolTables"), 0, &tmp
);
1080 zval
*ztab
= zend_hash_find(Z_ARRVAL_P(ztabs
), key
);
1082 zend_string_release(key
);
1087 php_error_docref(NULL
, E_NOTICE
, "Previously unknown ion\\Symbol\\Table encountered: %s", key
->val
);
1089 object_init_ex(return_value
, ce_Symbol_Table_Shared
);
1090 php_ion_symbol_table
*o_tab
= php_ion_obj(symbol_table
, Z_OBJ_P(return_value
));
1091 OBJ_CHECK(o_tab
, zend_string_release(key
));
1092 php_ion_symbol_table_ctor(o_tab
);
1095 SEPARATE_ARRAY(ztabs
);
1096 ZVAL_ADDREF(return_value
);
1097 add_index_zval(ztabs
, (uintptr_t) &o_tab
->std
.gc
, return_value
);
1098 ZVAL_ADDREF(return_value
);
1099 add_assoc_zval_ex(ztabs
, key
->val
, key
->len
, return_value
);
1101 zend_string_release(key
);
1104 php_ion_decl(catalog
, Catalog
);
1106 typedef struct php_ion_reader_options_ccn_ctx
{
1108 zend_fcall_info fci
;
1109 zend_fcall_info_cache fcc
;
1110 } php_ion_reader_options_ccn_ctx
;
1112 typedef struct php_ion_reader_options
{
1113 ION_READER_OPTIONS opt
;
1114 php_ion_reader_options_ccn_ctx ccn
;
1115 zend_object
*cat
, *dec_ctx
, *cb
, std
;
1116 } php_ion_reader_options
;
1118 LOCAL
void php_ion_reader_options_dtor(php_ion_reader_options
*obj
)
1121 zend_fcall_info_args_clear(&obj
->ccn
.fci
, true);
1125 LOCAL
void php_ion_reader_options_copy(php_ion_reader_options
*new_obj
, php_ion_reader_options
*old_obj
)
1127 zend_objects_clone_members(&new_obj
->std
, &old_obj
->std
);
1129 new_obj
->opt
= old_obj
->opt
;
1130 new_obj
->cat
= get_property_obj(&new_obj
->std
, ZEND_STRL("catalog"), IS_OBJECT
);
1131 new_obj
->dec_ctx
= get_property_obj(&new_obj
->std
, ZEND_STRL("decimalContext"), IS_OBJECT
);
1132 new_obj
->cb
= get_property_obj(&new_obj
->std
, ZEND_STRL("onContextChange"), IS_OBJECT
);
1135 ZVAL_OBJ(&zcb
, new_obj
->cb
);
1136 zend_fcall_info_init(&zcb
, 0, &new_obj
->ccn
.fci
, &new_obj
->ccn
.fcc
, NULL
, NULL
);
1137 new_obj
->opt
.context_change_notifier
.context
= &new_obj
->ccn
;
1141 php_ion_decl(reader_options
, Reader_Options
);
1143 LOCAL zend_object
*php_ion_reader_options_new(void)
1145 zend_object
*obj
= create_ion_Reader_Options(NULL
);
1146 zend_call_known_instance_method_with_0_params(obj
->ce
->constructor
, obj
, NULL
);
1150 typedef struct php_ion_reader
{
1158 zend_string
*buffer
;
1164 zend_object
*opt
, std
;
1167 LOCAL iERR
php_ion_reader_stream_handler(struct _ion_user_stream
*user
)
1169 php_ion_reader
*reader
= (php_ion_reader
*) user
->handler_state
;
1170 size_t remaining
= 0, spare
= reader
->stream
.buf
.length
;
1172 if (user
->curr
&& user
->limit
&& (remaining
= user
->limit
- user
->curr
)) {
1173 memmove(reader
->stream
.buf
.value
, user
->curr
, remaining
);
1174 user
->limit
-= remaining
;
1177 user
->curr
= user
->limit
= reader
->stream
.buf
.value
;
1180 ssize_t read
= php_stream_read(reader
->stream
.ptr
, (char *) user
->limit
, spare
);
1181 if (EXPECTED(read
> 0)) {
1182 user
->limit
+= read
;
1186 if (EXPECTED(read
== 0)) {
1190 return IERR_READ_ERROR
;
1193 LOCAL iERR
on_context_change(void *context
, ION_COLLECTION
*imports
)
1198 php_ion_reader_options_ccn_ctx
*ctx
= context
;
1201 ZVAL_OBJ(&zobj
, ctx
->obj
);
1202 zend_fcall_info_argn(&ctx
->fci
, 1, &zobj
);
1203 if (SUCCESS
!= zend_fcall_info_call(&ctx
->fci
, &ctx
->fcc
, NULL
, NULL
)) {
1204 e
= IERR_INTERNAL_ERROR
;
1206 zend_fcall_info_args_clear(&ctx
->fci
, false);
1211 LOCAL
void php_ion_reader_ctor(php_ion_reader
*obj
)
1214 php_ion_reader_options
*opt
= php_ion_obj(reader_options
, obj
->opt
);
1216 if (opt
&& opt
->opt
.context_change_notifier
.context
) {
1217 php_ion_reader_options_ccn_ctx
*ctx
= opt
->opt
.context_change_notifier
.context
;
1218 ctx
->obj
= &obj
->std
;
1219 opt
->opt
.context_change_notifier
.notify
= on_context_change
;
1221 if (obj
->type
== STREAM_READER
) {
1222 PTR_CHECK(obj
->stream
.ptr
);
1223 GC_ADDREF(obj
->stream
.ptr
->res
);
1225 obj
->stream
.buf
.length
= opt
&& opt
->opt
.allocation_page_size
? opt
->opt
.allocation_page_size
: 0x10000;
1226 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1227 err
= ion_reader_open_stream(&obj
->reader
, obj
, php_ion_reader_stream_handler
, opt
? &opt
->opt
: NULL
);
1230 err
= ion_reader_open_buffer(&obj
->reader
, (BYTE
*) obj
->buffer
->val
, (SIZE
) obj
->buffer
->len
, opt
? &opt
->opt
: NULL
);
1237 LOCAL
void php_ion_reader_dtor(php_ion_reader
*obj
)
1240 ion_reader_close(obj
->reader
);
1242 if (obj
->type
== STREAM_READER
) {
1243 if (obj
->stream
.buf
.value
) {
1244 efree(obj
->stream
.buf
.value
);
1246 if (obj
->stream
.ptr
) {
1247 zend_list_delete(obj
->stream
.ptr
->res
);
1251 zend_string_release(obj
->buffer
);
1256 #define php_ion_reader_copy(n,o)
1257 php_ion_decl(reader
, Reader_Reader
);
1258 #define clone_ion_Reader_Reader NULL
1260 typedef struct php_ion_writer_options
{
1261 ION_WRITER_OPTIONS opt
;
1262 zend_object
*cat
, *dec_ctx
, std
;
1263 } php_ion_writer_options
;
1265 LOCAL
void php_ion_writer_options_copy(php_ion_writer_options
*new_obj
, php_ion_writer_options
*old_obj
)
1267 new_obj
->opt
= old_obj
->opt
;
1270 new_obj
->cat
= old_obj
->cat
;
1271 GC_ADDREF(new_obj
->cat
);
1273 if (old_obj
->dec_ctx
) {
1274 new_obj
->dec_ctx
= old_obj
->dec_ctx
;
1275 GC_ADDREF(new_obj
->dec_ctx
);
1279 LOCAL
void php_ion_writer_options_dtor(php_ion_writer_options
*obj
)
1283 php_ion_decl(writer_options
, Writer_Options
);
1285 LOCAL zend_object
*php_ion_writer_options_new(void)
1287 zend_object
*obj
= create_ion_Writer_Options(NULL
);
1288 zend_call_known_instance_method_with_0_params(obj
->ce
->constructor
, obj
, NULL
);
1292 typedef struct php_ion_writer
{
1308 zend_object
*opt
, std
;
1312 LOCAL iERR
php_ion_writer_stream_handler(struct _ion_user_stream
*user
)
1314 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1316 if (EXPECTED(user
->limit
&& user
->curr
)) {
1317 ptrdiff_t len
= user
->curr
- writer
->stream
.buf
.value
;
1318 if (len
!= php_stream_write(writer
->stream
.ptr
, (char *) writer
->stream
.buf
.value
, len
)) {
1319 return IERR_WRITE_ERROR
;
1322 user
->curr
= writer
->stream
.buf
.value
;
1323 user
->limit
= writer
->stream
.buf
.value
+ writer
->stream
.buf
.length
;
1327 #define REF_STR() do { \
1328 ZVAL_NEW_STR(ref, obj->buffer.str.s); \
1329 GC_ADDREF(obj->buffer.str.s); \
1332 #define NEW_REF_STR() do {\
1333 if (Z_STR_P(ref) != obj->buffer.str.s) { \
1334 zval_ptr_dtor(ref); \
1339 LOCAL
void php_ion_writer_stream_init(php_ion_writer
*obj
, php_ion_writer_options
*opt
)
1341 PTR_CHECK(obj
->stream
.ptr
);
1342 GC_ADDREF(obj
->stream
.ptr
->res
);
1344 obj
->stream
.buf
.length
= opt
? opt
->opt
.allocation_page_size
: 0x1000;
1345 obj
->stream
.buf
.value
= emalloc(obj
->stream
.buf
.length
);
1347 LOCAL
void php_ion_writer_buffer_init(php_ion_writer
*obj
)
1349 zval
*ref
= &obj
->buffer
.val
;
1352 smart_str_alloc(&obj
->buffer
.str
, 0, 0);
1353 smart_str_0(&obj
->buffer
.str
);
1357 LOCAL
void php_ion_writer_buffer_grow(php_ion_writer
*obj
)
1359 zval
*ref
= &obj
->buffer
.val
;
1362 switch (GC_REFCOUNT(obj
->buffer
.str
.s
)) {
1367 // we've been separated
1368 GC_ADDREF(obj
->buffer
.str
.s
);
1371 // we have to separate
1372 fprintf(stderr
, "SEPARATE\n");
1373 obj
->buffer
.str
.s
= zend_string_dup(obj
->buffer
.str
.s
, 0);
1377 zend_string
*old
= obj
->buffer
.str
.s
;
1379 smart_str_erealloc(&obj
->buffer
.str
, obj
->buffer
.str
.a
<< 1);
1380 if (old
== obj
->buffer
.str
.s
) {
1382 } else if(old
== Z_STR_P(ref
)) {
1389 LOCAL iERR
php_ion_writer_buffer_handler(struct _ion_user_stream
*user
)
1391 php_ion_writer
*writer
= (php_ion_writer
*) user
->handler_state
;
1394 writer
->buffer
.str
.s
->len
+= user
->curr
- (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1395 smart_str_0(&writer
->buffer
.str
);
1396 if (user
->limit
== user
->curr
) {
1397 php_ion_writer_buffer_grow(writer
);
1400 user
->curr
= (BYTE
*) &writer
->buffer
.str
.s
->val
[writer
->buffer
.str
.s
->len
];
1401 user
->limit
= user
->curr
+ writer
->buffer
.str
.a
- writer
->buffer
.str
.s
->len
;
1406 LOCAL
void php_ion_writer_options_init_shared_imports(php_ion_writer_options
*opt
)
1408 php_ion_catalog
*cat
= php_ion_obj(catalog
, opt
->cat
);
1411 ION_CHECK(ion_writer_options_initialize_shared_imports(&opt
->opt
));
1413 ION_COLLECTION
*col
= php_ion_catalog_collection(cat
);
1414 if (!ION_COLLECTION_IS_EMPTY(col
)) {
1415 // holy, nah, forget it batman...
1416 ION_COLLECTION_CURSOR cur
;
1417 ION_COLLECTION_OPEN(col
, cur
);
1419 ION_SYMBOL_TABLE
**ptr
;
1420 ION_COLLECTION_NEXT(cur
, ptr
);
1422 ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt
->opt
, ptr
, 1));
1430 LOCAL
void php_ion_writer_ctor(php_ion_writer
*obj
)
1432 php_ion_writer_options
*opt
= NULL
;
1435 update_property_obj(&obj
->std
, ZEND_STRL("options"), obj
->opt
);
1436 opt
= php_ion_obj(writer_options
, obj
->opt
);
1438 php_ion_writer_options_init_shared_imports(opt
);
1442 ION_STREAM_HANDLER h
;
1443 if (obj
->type
== STREAM_WRITER
) {
1444 h
= php_ion_writer_stream_handler
;
1445 php_ion_writer_stream_init(obj
, opt
);
1447 h
= php_ion_writer_buffer_handler
;
1448 php_ion_writer_buffer_init(obj
);
1452 ION_CHECK(ion_writer_open_stream(&obj
->writer
, h
, obj
, opt
? &opt
->opt
: NULL
));
1456 LOCAL
void php_ion_writer_dtor(php_ion_writer
*obj
)
1459 ion_writer_close(obj
->writer
);
1462 php_ion_writer_options
*opt
= php_ion_obj(writer_options
, obj
->opt
);
1464 ion_writer_options_close_shared_imports(&opt
->opt
);
1467 if (obj
->type
== STREAM_WRITER
) {
1468 if (obj
->stream
.buf
.value
) {
1469 efree(obj
->stream
.buf
.value
);
1471 if (obj
->stream
.ptr
) {
1472 zend_list_delete(obj
->stream
.ptr
->res
);
1475 if (obj
->buffer
.str
.s
) {
1476 smart_str_0(&obj
->buffer
.str
);
1477 zend_string_release(obj
->buffer
.str
.s
);
1479 zval_ptr_dtor(&obj
->buffer
.val
);
1483 #define php_ion_writer_copy(o,n)
1484 php_ion_decl(writer
, Writer_Writer
);
1485 #define clone_ion_Writer_Writer NULL
1487 typedef struct php_ion_serializer_php
{
1488 php_ion_serializer serializer
;
1489 zend_object
*opt
, std
;
1490 } php_ion_serializer_php
;
1492 LOCAL
void php_ion_serializer_php_ctor(php_ion_serializer_php
*ser_obj
)
1494 php_ion_serializer
*global_ser
= &php_ion_globals
.serializer
;
1495 ser_obj
->serializer
.ids
= global_ser
->ids
;
1496 ser_obj
->serializer
.tmp
= global_ser
->tmp
;
1498 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
1499 ser_obj
->serializer
.multi_seq
);
1500 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicSerialize"),
1501 ser_obj
->serializer
.call_magic
);
1502 if (ser_obj
->serializer
.call_custom
) {
1503 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"),
1504 ser_obj
->serializer
.call_custom
);
1505 ser_obj
->serializer
.call_custom
= zend_string_tolower(ser_obj
->serializer
.call_custom
);
1507 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomSerialize"));
1509 if (!ser_obj
->opt
) {
1510 ser_obj
->opt
= php_ion_writer_options_new();
1512 GC_ADDREF(ser_obj
->opt
);
1514 ser_obj
->serializer
.options
= &php_ion_obj(writer_options
, ser_obj
->opt
)->opt
;
1515 update_property_obj(&ser_obj
->std
, ZEND_STRL("writerOptions"), ser_obj
->opt
);
1516 GC_DELREF(ser_obj
->opt
);
1519 LOCAL
void php_ion_serializer_php_dtor(php_ion_serializer_php
*obj
)
1521 if (obj
->serializer
.call_custom
) {
1522 zend_string_release(obj
->serializer
.call_custom
);
1526 LOCAL
void php_ion_serialize_zval(php_ion_serializer
*, zval
*);
1528 LOCAL
void php_ion_serialize_struct(php_ion_serializer
*ser
, zend_array
*arr
, bool unmangle_props
, bool annotate_props
)
1530 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_STRUCT
));
1534 zend_string
*k
= NULL
;
1535 if (arr
) ZEND_HASH_FOREACH_KEY_VAL_IND(arr
, h
, k
, v
)
1536 char buf
[MAX_LENGTH_OF_LONG
+ 1];
1540 const char *class_name
, *prop_name
;
1541 if (unmangle_props
&& (SUCCESS
== zend_unmangle_property_name_ex(k
, &class_name
, &prop_name
, &prop_len
)) && class_name
) {
1542 if (annotate_props
) {
1543 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_PROPERTY
));
1544 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_assign_cstr(&is
, (char *) class_name
, prop_name
- class_name
- 1)));
1550 ion_string_assign_cstr(&is
, (char *) prop_name
, (SIZE
) prop_len
);
1552 char *end
= buf
+ sizeof(buf
) - 1;
1553 char *ptr
= zend_print_long_to_buf(end
, (zend_long
) h
);
1554 ion_string_assign_cstr(&is
, ptr
, (SIZE
) (end
- ptr
));
1557 // WATCH OUT: field names need to be copied
1559 ION_CHECK(ion_string_copy_to_owner(ser
->writer
, &fn
, &is
));
1560 ION_CHECK(ion_writer_write_field_name(ser
->writer
, &fn
));
1562 php_ion_serialize_zval(ser
, v
);
1564 ZEND_HASH_FOREACH_END();
1566 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1569 LOCAL
void php_ion_serialize_list(php_ion_serializer
*ser
, zend_array
*arr
)
1571 ION_CHECK(ion_writer_start_container(ser
->writer
, tid_LIST
));
1574 ZEND_HASH_FOREACH_VAL_IND(arr
, v
)
1575 php_ion_serialize_zval(ser
, v
);
1577 ZEND_HASH_FOREACH_END();
1579 ION_CHECK(ion_writer_finish_container(ser
->writer
));
1582 LOCAL
void php_ion_serialize_array(php_ion_serializer
*ser
, zend_array
*arr
)
1584 if (zend_array_is_list(arr
)) {
1585 php_ion_serialize_list(ser
, arr
);
1587 php_ion_serialize_struct(ser
, arr
, false, false);
1591 LOCAL
void php_ion_serialize_object_iface(php_ion_serializer
*ser
, zend_object
*zobject
)
1597 ZVAL_OBJ(&tmp
, zobject
);
1598 if (SUCCESS
== zobject
->ce
->serialize(&tmp
, &buf
, &len
, NULL
)) {
1600 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_SERIALIZEABLE
));
1601 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1602 ION_CHECK(ion_writer_write_clob(ser
->writer
, buf
, len
));
1604 } else if (!EG(exception
)){
1605 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1606 "Failed to serialize class %s", zobject
->ce
->name
->val
);
1610 LOCAL
void php_ion_serialize_object_magic(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
*fn
)
1615 zend_call_known_instance_method_with_0_params(fn
? fn
: zobject
->ce
->__serialize
, zobject
, &rv
);
1618 if (IS_ARRAY
== Z_TYPE(rv
)) {
1620 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, fn
? &PHP_ION_SYMBOL_CUSTOM_OBJECT
: &PHP_ION_SYMBOL_MAGIC_OBJECT
));
1621 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1622 php_ion_serialize_zval(ser
, &rv
);
1625 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1626 "%s serializer %s::%s did not return an array",
1627 fn
? "Custom" : "Magic", zobject
->ce
->name
->val
,
1628 fn
? fn
->common
.function_name
->val
: "__serialize");
1632 LOCAL
void php_ion_serialize_object_enum(php_ion_serializer
*ser
, zend_object
*zobject
)
1635 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_ENUM
));
1637 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1638 zval
*z_cname
= zend_enum_fetch_case_name(zobject
);
1639 ION_CHECK(ion_writer_write_symbol(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(z_cname
))));
1642 LOCAL
void php_ion_serialize_object_std(php_ion_serializer
*ser
, zend_object
*zobject
)
1646 if (zobject
->ce
!= zend_standard_class_def
) {
1647 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_CLASS_OBJECT
));
1648 ION_CHECK(ion_writer_add_annotation(ser
->writer
, ion_string_from_zend(&is
, zobject
->ce
->name
)));
1650 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_OBJECT
));
1654 ZVAL_OBJ(&zobj
, zobject
);
1655 HashTable
*props
= zend_get_properties_for(&zobj
, ZEND_PROP_PURPOSE_SERIALIZE
);
1657 php_ion_serialize_struct(ser
, props
, true, true);
1658 zend_release_properties(props
);
1660 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
1661 "Could not get properties for serialization of class %s",
1662 zobject
->ce
->name
->val
);
1666 LOCAL
void php_ion_serialize_object_lob(php_ion_serializer
*ser
, zend_object
*zobject
)
1668 zval tmp_type
, *type
= zend_read_property(NULL
, zobject
, ZEND_STRL("type"), 0, &tmp_type
);
1669 zval tmp_value
, *value
= zend_read_property(NULL
, zobject
, ZEND_STRL("value"), 0, &tmp_value
);
1670 switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type
)))) {
1672 ION_CHECK(ion_writer_write_blob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1675 ION_CHECK(ion_writer_write_clob(ser
->writer
, (BYTE
*) Z_STRVAL_P(value
), Z_STRLEN_P(value
)));
1678 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1679 "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type
))));
1684 LOCAL
bool can_call_magic_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1686 return ce
->__serialize
&& ser
->call_magic
;
1689 LOCAL
bool can_call_iface_serialize(php_ion_serializer
*ser
, zend_class_entry
*ce
)
1692 return !!ce
->serialize
; // NOLINT
1695 LOCAL
bool can_call_custom_serialize(php_ion_serializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1697 if (ser
->call_custom
) {
1698 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
1703 LOCAL
void php_ion_serialize_object(php_ion_serializer
*ser
, zend_object
*zobject
)
1706 zend_class_entry
*ce
= zobject
->ce
;
1709 if (ce
->ce_flags
& ZEND_ACC_NOT_SERIALIZABLE
) {
1710 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1711 "Serializing %s is not allowed", ce
->name
->val
);
1715 if (can_call_magic_serialize(ser
, ce
)) {
1716 php_ion_serialize_object_magic(ser
, zobject
, NULL
);
1717 } else if (can_call_iface_serialize(ser
, ce
)) {
1718 php_ion_serialize_object_iface(ser
, zobject
);
1719 } else if (can_call_custom_serialize(ser
, zobject
, &fn
)) {
1720 php_ion_serialize_object_magic(ser
, zobject
, fn
);
1721 } else if (zobject
->ce
->ce_flags
& ZEND_ACC_ENUM
) {
1722 php_ion_serialize_object_enum(ser
, zobject
);
1723 } else if (ce
== ce_Symbol
) {
1724 ION_CHECK(ion_writer_write_ion_symbol(ser
->writer
, &php_ion_obj(symbol
, zobject
)->sym
));
1725 } else if (ce
== ce_Decimal
) {
1726 ION_CHECK(ion_writer_write_ion_decimal(ser
->writer
, &php_ion_obj(decimal
, zobject
)->dec
));
1727 } else if (ce
== ce_Timestamp
) {
1729 php_ion_timestamp
*pts
= php_ion_obj(timestamp
, zobject
);
1730 decContext
*ctx
= ser
->options
? ser
->options
->decimal_context
: NULL
;
1731 ION_CHECK(ion_writer_write_timestamp(ser
->writer
, ion_timestamp_from_php(&its
, pts
, ctx
)));
1732 } else if (ce
== ce_LOB
) {
1733 php_ion_serialize_object_lob(ser
, zobject
);
1735 php_ion_serialize_object_std(ser
, zobject
);
1739 LOCAL
bool php_ion_serialize_system_value(php_ion_serializer
*ser
, zval
*zv
)
1741 if (1 == php_ion_globals
.serializer
.level
) {
1742 if (Z_TYPE_P(zv
) == IS_OBJECT
) {
1743 if (Z_OBJCE_P(zv
) == ce_Symbol_Table_Shared
) {
1744 php_ion_symbol_table
*obj
= php_ion_obj(symbol_table
, Z_OBJ_P(zv
));
1745 ION_CHECK_RETURN(true, ion_symbol_table_unload(obj
->tab
, ser
->writer
));
1753 LOCAL
bool php_ion_serialize_backref(php_ion_serializer
*ser
, zval
*zv
)
1755 if (Z_TYPE_P(zv
) == IS_STRING
&& Z_STR_P(zv
) == zend_empty_string
) {
1758 if (Z_TYPE_P(zv
) == IS_ARRAY
&& Z_ARR_P(zv
) == &zend_empty_array
) {
1762 zend_ulong idx
= (zend_ulong
) (uintptr_t) Z_COUNTED_P(zv
);
1763 zval
*ref
= zend_hash_index_find(ser
->ids
, idx
);
1767 ZVAL_LONG(&num
, zend_hash_num_elements(ser
->ids
));
1768 zend_hash_index_add(ser
->ids
, idx
, &num
);
1771 zend_hash_next_index_insert(ser
->tmp
, zv
);
1776 ION_CHECK_RETURN(true, ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_BACKREF
));
1777 ION_CHECK_RETURN(true, ion_writer_write_int64(ser
->writer
, Z_LVAL_P(ref
)));
1781 LOCAL
void php_ion_serialize_refcounted(php_ion_serializer
*ser
, zval
*zv
)
1783 if (php_ion_serialize_system_value(ser
, zv
)) {
1786 if (php_ion_serialize_backref(ser
, zv
)) {
1791 switch (Z_TYPE_P(zv
)) {
1793 ION_CHECK(ion_writer_write_string(ser
->writer
, ion_string_from_zend(&is
, Z_STR_P(zv
))));
1797 php_ion_serialize_array(ser
, Z_ARRVAL_P(zv
));
1801 php_ion_serialize_object(ser
, Z_OBJ_P(zv
));
1805 ION_CHECK(ion_writer_add_annotation_symbol(ser
->writer
, &PHP_ION_SYMBOL_REFERENCE
));
1806 php_ion_serialize_zval(ser
, Z_REFVAL_P(zv
));
1811 LOCAL
void php_ion_serialize_zval(php_ion_serializer
*ser
, zval
*zv
)
1815 switch (Z_TYPE_P(zv
)) {
1817 ION_CHECK(ion_writer_write_null(ser
->writer
));
1820 ION_CHECK(ion_writer_write_bool(ser
->writer
, TRUE
));
1823 ION_CHECK(ion_writer_write_bool(ser
->writer
, FALSE
));
1826 ION_CHECK(ion_writer_write_int64(ser
->writer
, Z_LVAL_P(zv
)));
1829 ION_CHECK(ion_writer_write_double(ser
->writer
, Z_DVAL_P(zv
)));
1835 php_ion_serialize_refcounted(ser
, zv
);
1838 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1839 "Failed to serialize value of type %s", zend_zval_type_name(zv
));
1843 #define php_ion_serializer_php_copy(o,n)
1844 php_ion_decl(serializer_php
, Serializer_PHP
);
1845 #define clone_ion_Serializer_PHP NULL
1847 LOCAL
void php_ion_serialize_ex(php_ion_serializer
*ser
, zval
*zv
)
1850 HashTable
*arr
= NULL
;
1852 if (ser
->multi_seq
) {
1853 if (Z_TYPE_P(zv
) != IS_ARRAY
|| !zend_array_is_list(Z_ARRVAL_P(zv
))) {
1854 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
1855 "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer");
1859 arr
= Z_ARRVAL_P(zv
);
1861 zend_hash_internal_pointer_reset_ex(arr
, &pos
);
1862 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1866 php_ion_globals_serializer_step();
1867 php_ion_serialize_zval(ser
, zv
);
1868 php_ion_globals_serializer_exit();
1870 if (!ser
->multi_seq
) {
1873 zend_hash_move_forward_ex(arr
, &pos
);
1874 zv
= zend_hash_get_current_data_ex(arr
, &pos
);
1878 void php_ion_serialize(php_ion_serializer
*ser
, zval
*zv
, zval
*return_value
)
1880 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
1883 zo_ser
= create_ion_Serializer_PHP(NULL
);
1884 php_ion_serializer_php
*o_ser
= php_ion_obj(serializer_php
, zo_ser
);
1886 o_ser
->serializer
.call_magic
= true;
1887 php_ion_serializer_php_ctor(o_ser
);
1889 ser
= &o_ser
->serializer
;
1892 zend_object
*zo_writer
= create_ion_Writer_Writer(ce_Writer_Buffer_Writer
);
1893 php_ion_writer
*writer
= php_ion_obj(writer
, zo_writer
);
1894 writer
->type
= BUFFER_WRITER
;
1897 zo_opt
= writer
->opt
= php_ion_writer_options_new();
1898 php_ion_obj(writer_options
, writer
->opt
)->opt
= *ser
->options
;
1901 php_ion_writer_ctor(writer
);
1902 ser
->writer
= writer
->writer
;
1903 ser
->buffer
= &writer
->buffer
.str
;
1905 if (!EG(exception
)) {
1906 php_ion_serialize_ex(ser
, zv
);
1909 /* make sure to flush when done, else str.s might not contain everything until the writer is closed */
1910 ion_writer_flush(ser
->writer
, NULL
);
1911 RETVAL_STR_COPY(ser
->buffer
->s
);
1913 OBJ_RELEASE(zo_writer
);
1915 OBJ_RELEASE(zo_opt
);
1918 OBJ_RELEASE(zo_ser
);
1922 typedef struct php_ion_unserializer_php
{
1923 php_ion_unserializer unserializer
;
1924 zend_object
*opt
, std
;
1925 } php_ion_unserializer_php
;
1927 LOCAL
void php_ion_unserializer_php_ctor(php_ion_unserializer_php
*ser_obj
)
1929 php_ion_unserializer
*global_ser
= &php_ion_globals
.unserializer
;
1930 ser_obj
->unserializer
.ids
= global_ser
->ids
;
1931 ser_obj
->unserializer
.tmp
= global_ser
->tmp
;
1932 ser_obj
->unserializer
.addref
= global_ser
->addref
;
1934 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("multiSequence"),
1935 ser_obj
->unserializer
.multi_seq
);
1936 zend_update_property_bool(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callMagicUnserialize"),
1937 ser_obj
->unserializer
.call_magic
);
1938 if (ser_obj
->unserializer
.call_custom
) {
1939 zend_update_property_str(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"),
1940 ser_obj
->unserializer
.call_custom
);
1941 ser_obj
->unserializer
.call_custom
= zend_string_tolower(ser_obj
->unserializer
.call_custom
);
1943 zend_update_property_null(ser_obj
->std
.ce
, &ser_obj
->std
, ZEND_STRL("callCustomUnserialize"));
1945 if (!ser_obj
->opt
) {
1946 ser_obj
->opt
= php_ion_reader_options_new();
1948 GC_ADDREF(ser_obj
->opt
);
1950 ser_obj
->unserializer
.options
= &php_ion_obj(reader_options
, ser_obj
->opt
)->opt
;
1951 update_property_obj(&ser_obj
->std
, ZEND_STRL("readerOptions"), ser_obj
->opt
);
1952 GC_DELREF(ser_obj
->opt
);
1955 LOCAL
void php_ion_unserializer_php_dtor(php_ion_unserializer_php
*obj
)
1957 if (obj
->unserializer
.call_custom
) {
1958 zend_string_release(obj
->unserializer
.call_custom
);
1962 LOCAL
void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
);
1964 LOCAL
bool can_call_magic_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1966 return (ce
&& ce
->__unserialize
&& ser
->call_magic
);
1969 LOCAL
bool can_call_iface_unserialize(php_ion_unserializer
*ser
, zend_class_entry
*ce
)
1971 return (ce
&& ce
->unserialize
);
1974 LOCAL
bool can_call_custom_unserialize(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
1976 if (ser
->call_custom
) {
1977 return !!(*fn
= zend_hash_find_ptr(&zobject
->ce
->function_table
, ser
->call_custom
)); // NOLINT
1982 LOCAL zval
*php_ion_unserialize_class(php_ion_unserializer
*ser
, zval
*return_value
)
1984 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
1987 object_init_ex(return_value
, ce
);
1988 return zend_hash_next_index_insert(ser
->ids
, return_value
);
1991 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_IMPORT_NOT_FOUND
,
1992 "Could not find class %s", ser
->annotations
.object_class
->val
);
1996 LOCAL
void php_ion_unserialize_object_enum(php_ion_unserializer
*ser
, zval
*return_value
)
1998 zend_string
*zs_case
= zval_get_string(return_value
);
2001 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2002 if (!ce
|| !(ce
->ce_flags
& ZEND_ACC_ENUM
)) {
2003 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2004 "Not a valid enum: %s", ser
->annotations
.object_class
->val
);
2007 if (!zend_hash_exists(CE_CONSTANTS_TABLE(ce
), zs_case
)) {
2008 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2009 "Not a valid enum case: %s::%s", ser
->annotations
.object_class
->val
, zs_case
->val
);
2012 RETVAL_OBJ_COPY(zend_enum_get_case(ce
, zs_case
));
2013 zend_hash_next_index_insert(ser
->ids
, return_value
);
2014 zend_string_release(zs_case
);
2017 LOCAL
void php_ion_unserialize_object_iface(php_ion_unserializer
*ser
, zval
*return_value
)
2019 zend_class_entry
*ce
= zend_lookup_class(ser
->annotations
.object_class
);
2020 if (can_call_iface_unserialize(ser
, ce
)) {
2021 zend_string
*s
= zval_get_string(return_value
);
2022 ZVAL_NULL(return_value
);
2023 zval
*backref
= zend_hash_next_index_insert(ser
->ids
, return_value
);
2024 if (SUCCESS
== ce
->unserialize(backref
, ce
, (BYTE
*) s
->val
, s
->len
, NULL
)) {
2025 RETVAL_ZVAL(backref
, 0, 0);
2026 } else if (!EG(exception
)) {
2027 zend_throw_exception_ex(spl_ce_UnexpectedValueException
, IERR_INTERNAL_ERROR
,
2028 "Failed to unserialize class %s", ce
->name
->val
);
2030 zend_string_release(s
);
2032 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2033 "Class %s does not implement Serializable", ser
->annotations
.object_class
->val
);
2037 LOCAL
void php_ion_unserialize_field_name(php_ion_unserializer
*ser
, zend_string
**key
)
2039 // FIXME: symbol table
2041 ION_CHECK(ion_reader_get_field_name(ser
->reader
, &name
));
2044 ION_CHECK(ion_reader_get_field_name_symbol(ser
->reader
, &is_ptr
));
2045 if (!ION_SYMBOL_IS_NULL(is_ptr
) && is_ptr
->value
.length
) {
2046 name
= is_ptr
->value
;
2047 } else if (is_ptr
) {
2048 char buf
[MAX_LENGTH_OF_LONG
+ 1 + 1] = {0}, *end
= buf
+ sizeof(buf
) - 1, *ptr
;
2049 ptr
= zend_print_long_to_buf(end
, is_ptr
->sid
);
2051 *key
= zend_string_init(ptr
, end
- ptr
, 0);
2055 *key
= zend_string_from_ion(&name
);
2058 static void php_ion_unserialize_props(php_ion_unserializer
*ser
, zval
*return_value
)
2060 zend_hash_next_index_insert(ser
->ids
, return_value
);
2062 ION_CHECK(ion_reader_step_in(ser
->reader
));
2066 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
2068 if (typ
== tid_EOF
) {
2073 php_ion_unserialize_field_name(ser
, &key
);
2077 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2078 ION_CATCH(zend_string_release(key
));
2080 zend_class_entry
*ce
= Z_OBJCE_P(return_value
);
2081 if (ser
->annotations
.object_prop
&& ser
->annotations
.property_class
->val
[0] != '*') {
2082 ce
= zend_lookup_class(ser
->annotations
.property_class
);
2084 zend_update_property_ex(ce
, Z_OBJ_P(return_value
), key
, &zvalue
);
2085 zval_ptr_dtor(&zvalue
);
2086 zend_string_release(key
);
2089 ION_CHECK(ion_reader_step_out(ser
->reader
));
2093 * @link https://amzn.github.io/ion-docs/docs/spec.html#struct
2094 * When two fields in the same struct have the same name [...] Implementations must preserve all such fields,
2095 * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields
2096 * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to
2097 * nondeterministic behavior.
2099 LOCAL
void php_ion_unserialize_hash(php_ion_unserializer
*ser
, zval
*return_value
)
2101 zend_hash_next_index_insert(ser
->ids
, return_value
);
2103 ION_CHECK(ion_reader_step_in(ser
->reader
));
2107 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
2109 if (typ
== tid_EOF
) {
2114 php_ion_unserialize_field_name(ser
, &key
);
2118 php_ion_unserialize_zval(ser
, &zvalue
, &typ
);
2119 ION_CATCH(zend_string_release(key
));
2121 // FIXME:: too naive; b0rked if the previous value is an array
2122 if (zend_symtable_exists(HASH_OF(return_value
), key
)) {
2123 zval tmp
, *prev
= zend_hash_find(HASH_OF(return_value
), key
);
2124 if (Z_TYPE_P(prev
) != IS_ARRAY
) {
2126 Z_TRY_ADDREF_P(prev
);
2127 zend_hash_next_index_insert(Z_ARRVAL(tmp
), prev
);
2128 prev
= zend_hash_update(HASH_OF(return_value
), key
, &tmp
);
2130 zend_hash_next_index_insert(Z_ARRVAL_P(prev
), &zvalue
);
2132 zend_symtable_update(HASH_OF(return_value
), key
, &zvalue
);
2134 zend_string_release(key
);
2137 ION_CHECK(ion_reader_step_out(ser
->reader
));
2140 LOCAL
void verify_unserializer(php_ion_unserializer
*ser
, zend_object
*zobject
, zend_function
**fn
)
2142 switch (ser
->annotations
.object_type
) {
2148 if (!can_call_custom_unserialize(ser
, zobject
, fn
)) {
2149 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2150 "Could not find custom serializer method of %s", ser
->annotations
.object_class
->val
);
2155 if (!can_call_magic_unserialize(ser
, zobject
->ce
)) {
2156 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2157 "Could not find method %s::__unserialize()", ser
->annotations
.object_class
->val
);
2159 *fn
= zobject
->ce
->__unserialize
;
2163 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2164 "Invalid object type %c", ser
->annotations
.object_type
);
2168 LOCAL
void php_ion_unserialize_object(php_ion_unserializer
*ser
, zval
*return_value
)
2170 // backup possible backref to array returned by magic/custom __serialize()
2171 zval
*input
= zend_hash_next_index_insert(ser
->tmp
, return_value
);
2173 php_ion_unserialize_class(ser
, return_value
);
2176 zend_function
*fn
= NULL
;
2177 zend_object
*zobject
= Z_OBJ_P(return_value
);
2178 verify_unserializer(ser
, zobject
, &fn
);
2183 php_ion_unserialize_props(ser
, return_value
);
2188 if (Z_TYPE_P(input
) != IS_ARRAY
) {
2189 zval_ptr_dtor(input
);
2191 zend_hash_real_init_mixed(Z_ARRVAL_P(input
));
2192 php_ion_unserialize_hash(ser
, input
);
2197 zend_call_method_with_1_params(zobject
, zobject
->ce
, &fn
, "", &rv
, input
);
2201 LOCAL
void php_ion_unserialize_struct(php_ion_unserializer
*ser
, zval
*return_value
)
2203 if (ser
->annotations
.object_class
) {
2204 switch (ser
->annotations
.object_type
) {
2206 php_ion_unserialize_object_iface(ser
, return_value
);
2209 php_ion_unserialize_object_enum(ser
, return_value
);
2212 php_ion_unserialize_object(ser
, return_value
);
2214 } else if (!ser
->annotations
.object_type
) {
2215 bool is_shared_symtab
= ser
->annotations
.shared_symtab
;
2216 array_init(return_value
);
2217 php_ion_unserialize_hash(ser
, return_value
);
2218 if (is_shared_symtab
) {
2220 object_init_ex(&zobj
, ce_Symbol_Table_Shared
);
2221 zend_call_known_function(Z_OBJCE(zobj
)->constructor
, Z_OBJ(zobj
), Z_OBJCE(zobj
), NULL
, 0, NULL
, Z_ARRVAL_P(return_value
));
2222 zval_ptr_dtor(return_value
);
2223 RETURN_COPY_VALUE(&zobj
);
2225 } else if (ser
->annotations
.object_type
== 'o') {
2226 object_init(return_value
);
2227 php_ion_unserialize_hash(ser
, return_value
);
2229 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_TOKEN
,
2230 "Invalid object annotation %c::", ser
->annotations
.object_type
);
2234 LOCAL
void php_ion_unserialize_list(php_ion_unserializer
*ser
, zval
*return_value
)
2236 ION_CHECK(ion_reader_step_in(ser
->reader
));
2237 array_init(return_value
);
2238 zend_hash_next_index_insert(ser
->ids
, return_value
);
2242 ION_CHECK(ion_reader_next(ser
->reader
, &typ
));
2244 if (typ
== tid_EOF
) {
2249 php_ion_unserialize_zval(ser
, &next
, &typ
);
2252 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &next
);
2255 ION_CHECK(ion_reader_step_out(ser
->reader
));
2258 LOCAL
void php_ion_reader_read_lob(ION_READER
*reader
, zval
*return_value
)
2260 zend_string
*zstr
= zend_string_alloc(0x1000, 0);
2263 iERR err
= ion_reader_read_lob_bytes(reader
, (BYTE
*) zstr
->val
, (SIZE
) zstr
->len
, &read
);
2264 if (err
== IERR_BUFFER_TOO_SMALL
) {
2265 zstr
= zend_string_extend(zstr
, zstr
->len
<< 2, 0);
2268 ION_CHECK(err
, zend_string_release(zstr
));
2269 if (zstr
->len
> read
) {
2270 zstr
->val
[read
] = 0;
2271 zstr
= zend_string_truncate(zstr
, read
, 0);
2276 LOCAL
void php_ion_reader_read_timestamp(ION_READER
*reader
, ION_READER_OPTIONS
*opt
, zval
*return_value
)
2279 ION_CHECK(ion_reader_read_timestamp(reader
, &ts
));
2281 object_init_ex(return_value
, ce_Timestamp
);
2282 php_ion_timestamp
*ts_obj
= php_ion_obj(timestamp
, Z_OBJ_P(return_value
));
2284 zend_string
*fmt
= NULL
;
2285 decContext
*ctx
= opt
? opt
->decimal_context
: NULL
;
2286 ts_obj
->time
= php_time_from_ion(&ts
, ctx
, &fmt
);
2287 php_ion_timestamp_ctor(ts_obj
, ts
.precision
, fmt
, NULL
, NULL
);
2288 zend_string_release(fmt
);
2293 LOCAL
void php_ion_reader_read_int(ION_READER
*reader
, zval
*return_value
)
2295 ION_INT
*num
= NULL
;
2296 ION_CHECK(ion_int_alloc(reader
, &num
));
2297 ION_CHECK(ion_reader_read_ion_int(reader
, num
));
2299 // TODO: SIZEOF_ZEND_LONG == 4
2301 iERR err
= ion_int_to_int64(num
, &i64
);
2307 case IERR_NUMERIC_OVERFLOW
: ;
2309 ION_CHECK(ion_int_char_length(num
, &max
));
2310 zend_string
*zs
= zend_string_alloc(max
, 0);
2312 err
= ion_int_to_char(num
, (BYTE
*) zs
->val
, max
, &len
);
2313 zs
->val
[zs
->len
= len
] = 0;
2324 LOCAL
void php_ion_unserialize_backref(php_ion_unserializer
*ser
, zval
*return_value
)
2326 zval
*backref
= zend_hash_index_find(ser
->ids
, Z_LVAL_P(return_value
));
2329 ZVAL_COPY_VALUE(return_value
, backref
);
2330 zend_hash_next_index_insert(ser
->addref
, return_value
);
2332 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INTERNAL_ERROR
,
2333 "Could not find back reference " ZEND_LONG_FMT
, Z_LVAL_P(return_value
));
2337 LOCAL
void php_ion_unserialize_annotations(php_ion_unserializer
*ser
)
2339 memset(&ser
->annotations
, 0, sizeof(ser
->annotations
));
2342 ION_CHECK(ion_reader_get_annotation_count(ser
->reader
, &ann_cnt
));
2343 for (int32_t i
= 0; i
< ann_cnt
; ++i
) {
2345 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, i
, &ann_str
));
2347 if (ann_str
.length
!= 1) {
2348 if (ION_STRING_EQUALS(&ann_str
, &ION_SYMBOL_SHARED_SYMBOL_TABLE_STRING
)) {
2349 ser
->annotations
.shared_symtab
= true;
2354 switch (*ann_str
.value
) {
2356 if (ser
->annotations
.makeref
) {
2357 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2358 "Invalid multiple reference annotations");
2361 ser
->annotations
.makeref
= true;
2365 if (ser
->annotations
.backref
) {
2366 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2367 "Invalid multiple back reference annotations");
2370 ser
->annotations
.backref
= true;
2374 if (ser
->annotations
.object_prop
) {
2375 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2376 "Invalid multiple object property annotations");
2379 ser
->annotations
.object_prop
= true;
2381 ION_STRING prop_class
;
2382 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &prop_class
));
2383 ser
->annotations
.property_class
= zend_string_from_ion(&prop_class
);
2386 ZVAL_STR(&zptmp
, ser
->annotations
.property_class
);
2387 zend_hash_next_index_insert(ser
->tmp
, &zptmp
);
2396 if (ser
->annotations
.object_type
) {
2397 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2398 "Invalid multiple object type annotations: %c::%c",
2399 ser
->annotations
.object_type
, *ann_str
.value
);
2402 if ('o' != (ser
->annotations
.object_type
= *ann_str
.value
)) {
2403 ION_STRING class_name
;
2404 ION_CHECK(ion_reader_get_an_annotation(ser
->reader
, ++i
, &class_name
));
2405 ser
->annotations
.object_class
= zend_string_from_ion(&class_name
);
2408 ZVAL_STR(&zctmp
, ser
->annotations
.object_class
);
2409 zend_hash_next_index_insert(ser
->tmp
, &zctmp
);
2415 if (ser
->annotations
.object_type
&& ser
->annotations
.object_type
!= 'o' && !ser
->annotations
.object_class
) {
2416 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2417 "Invalid object annotation without class name: %c::", ser
->annotations
.object_type
);
2420 if (ser
->annotations
.object_type
== 'o' && ser
->annotations
.object_class
) {
2421 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2422 "Invalid object annotation with class name: o::%s", ser
->annotations
.object_class
->val
);
2428 LOCAL
void php_ion_unserialize_zval(php_ion_unserializer
*ser
, zval
*return_value
, ION_TYPE
*typ
)
2431 memcpy(&ser
->type
, typ
, sizeof(ser
->type
)); // NOLINT
2434 ION_CHECK(ion_reader_next(ser
->reader
, typ
));
2437 php_ion_unserialize_annotations(ser
);
2440 if (ser
->annotations
.makeref
) {
2441 ZVAL_MAKE_REF(return_value
);
2442 zend_hash_next_index_insert(ser
->ids
, return_value
);
2443 ZVAL_DEREF(return_value
);
2446 if (ION_TYPE_INT(*typ
) > 0) {
2448 ION_CHECK(ion_reader_is_null(ser
->reader
, &is_null
));
2454 switch (ION_TYPE_INT(*typ
)) {
2456 ION_CHECK(ion_reader_read_null(ser
->reader
, typ
));
2459 case tid_BOOL_INT
: ;
2461 ION_CHECK(ion_reader_read_bool(ser
->reader
, &bval
));
2465 php_ion_reader_read_int(ser
->reader
, return_value
);
2466 if (ser
->annotations
.backref
) {
2468 php_ion_unserialize_backref(ser
, return_value
);
2470 if (ser
->annotations
.object_type
) {
2471 if (!ser
->annotations
.backref
) {
2472 zend_throw_exception_ex(spl_ce_RuntimeException
, IERR_INVALID_SYNTAX
,
2473 "Invalid object type annotation: %c::" ZEND_LONG_FMT
,
2474 ser
->annotations
.object_type
, Z_LVAL_P(return_value
));
2477 goto unserialize_struct
;
2481 case tid_FLOAT_INT
: ;
2483 ION_CHECK(ion_reader_read_double(ser
->reader
, &d
));
2486 case tid_DECIMAL_INT
:
2487 object_init_ex(return_value
, ce_Decimal
);
2488 php_ion_decimal
*dec
= php_ion_obj(decimal
, Z_OBJ_P(return_value
));
2489 ION_CHECK(ion_reader_read_ion_decimal(ser
->reader
, &dec
->dec
));
2490 php_ion_decimal_ctor(dec
);
2491 zend_hash_next_index_insert(ser
->ids
, return_value
);
2494 case tid_TIMESTAMP_INT
:
2495 php_ion_reader_read_timestamp(ser
->reader
, ser
->options
, return_value
);
2496 zend_hash_next_index_insert(ser
->ids
, return_value
);
2499 case tid_SYMBOL_INT
: ;
2501 ION_CHECK(ion_reader_read_ion_symbol(ser
->reader
, &sym
));
2502 php_ion_symbol_zval(&sym
, return_value
);
2503 if (ser
->annotations
.object_type
) {
2504 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2505 goto unserialize_struct
;
2507 zend_hash_next_index_insert(ser
->ids
, return_value
);
2510 case tid_STRING_INT
: ;
2512 ION_CHECK(ion_reader_read_string(ser
->reader
, &str
));
2513 RETVAL_STRINGL((char *) str
.value
, str
.length
);
2514 if (ser
->annotations
.object_type
) {
2515 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2516 goto unserialize_struct
;
2518 zend_hash_next_index_insert(ser
->ids
, return_value
);
2523 php_ion_reader_read_lob(ser
->reader
, return_value
);
2524 if (ser
->annotations
.object_type
) {
2525 zend_hash_next_index_insert(ser
->tmp
, return_value
);
2526 goto unserialize_struct
;
2528 zend_hash_next_index_insert(ser
->ids
, return_value
);
2532 case tid_SEXP_INT
: // FIXME
2533 php_ion_unserialize_list(ser
, return_value
);
2534 if (!ser
->annotations
.object_type
) {
2539 case tid_STRUCT_INT
:
2540 unserialize_struct
: ;
2541 php_ion_unserialize_struct(ser
, return_value
);
2548 case tid_DATAGRAM_INT
:
2549 ZEND_ASSERT(!"datagram");
2555 #define php_ion_unserializer_php_copy(o,n)
2556 php_ion_decl(unserializer_php
, Unserializer_PHP
);
2557 #define clone_ion_Unserializer_PHP NULL
2559 LOCAL
void php_ion_unserialize_ex(php_ion_unserializer
*ser
, zval
*return_value
)
2561 if (ser
->multi_seq
) {
2562 array_init(return_value
);
2568 php_ion_globals_unserializer_step();
2569 php_ion_unserialize_zval(ser
, &tmp
, NULL
);
2570 php_ion_globals_unserializer_exit();
2571 ION_CATCH(zval_ptr_dtor(&tmp
));
2573 if (!ser
->multi_seq
) {
2574 RETURN_COPY_VALUE(&tmp
);
2575 } else if (ser
->type
!= tid_EOF
) {
2576 zend_hash_next_index_insert(Z_ARRVAL_P(return_value
), &tmp
);
2578 } while (ser
->type
!= tid_EOF
);
2581 void php_ion_unserialize(php_ion_unserializer
*ser
, zval
*zdata
, zval
*return_value
)
2583 zend_object
*zo_opt
= NULL
, *zo_ser
= NULL
;
2586 zo_ser
= create_ion_Unserializer_PHP(NULL
);
2587 php_ion_unserializer_php
*o_ser
= php_ion_obj(unserializer_php
, zo_ser
);
2589 o_ser
->unserializer
.call_magic
= true;
2590 php_ion_unserializer_php_ctor(o_ser
);
2592 ser
= &o_ser
->unserializer
;
2595 zend_object
*zo_reader
;
2596 php_ion_reader
*reader
;
2599 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
2600 zo_reader
= create_ion_Reader_Reader(ce_Reader_Stream_Reader
);
2601 reader
= php_ion_obj(reader
, zo_reader
);
2602 reader
->type
= STREAM_READER
;
2603 php_stream_from_zval_no_verify(reader
->stream
.ptr
, zdata
);
2604 } else if (Z_TYPE_P(zdata
) <= IS_STRING
) {
2605 zo_reader
= create_ion_Reader_Reader(ce_Reader_Buffer_Reader
);
2606 reader
= php_ion_obj(reader
, zo_reader
);
2607 reader
->type
= BUFFER_READER
;
2608 reader
->buffer
= zval_get_string(zdata
);
2610 zend_throw_exception_ex(spl_ce_InvalidArgumentException
, IERR_INVALID_ARG
,
2611 "Invalid source to unserialize; expected string or resource");
2613 OBJ_RELEASE(zo_ser
);
2619 zo_opt
= reader
->opt
= php_ion_reader_options_new();
2620 php_ion_obj(reader_options
, reader
->opt
)->opt
= *ser
->options
;
2623 php_ion_reader_ctor(reader
);
2624 ser
->reader
= reader
->reader
;
2626 if (!EG(exception
)) {
2627 php_ion_unserialize_ex(ser
, return_value
);
2630 OBJ_RELEASE(zo_reader
);
2632 OBJ_RELEASE(zo_opt
);
2635 OBJ_RELEASE(zo_ser
);