X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=ion_private.h;h=57aaa6f9f886787e20b80036149482e14b89a2c2;hb=d31afba7b6dc66b25f6070b5fc5195e4a1c8a85c;hp=93b3058280c29d12149229564aeaa29d70794c9c;hpb=90a4807d7d93e65b90a646a4744e2bac36d9bb80;p=awesomized%2Fext-ion diff --git a/ion_private.h b/ion_private.h index 93b3058..57aaa6f 100644 --- a/ion_private.h +++ b/ion_private.h @@ -10,11 +10,118 @@ +--------------------------------------------------------------------+ */ -#include "ionc/ion.h" - #include "php.h" #include "ext/date/php_date.h" +#define DECNUMDIGITS 34 /* DECQUAD_Pmax */ +#include "ionc/ion.h" + +#define PHP_ION_SYMBOL_TABLE_VERSION 1 +#define PHP_ION_SYMBOL(c, s) { \ + { \ + 0, \ + { sizeof(s)-1, (BYTE *) s }, \ + { { 0, NULL }, 0 }, \ + 0 \ + }, \ + { sizeof(c)-1, (BYTE *) c } \ +}, + +typedef struct php_ion_global_symbol { + ION_SYMBOL s; + ION_STRING e; +} php_ion_global_symbol; + +static php_ion_global_symbol g_sym_tab_php_sym[] = { +#define PHP_ION_SYMBOL_PHP (g_sym_tab_php_sym[0]).s + PHP_ION_SYMBOL("PHP", "PHP") +#define PHP_ION_SYMBOL_REFERENCE (g_sym_tab_php_sym[1]).s + PHP_ION_SYMBOL("Reference", "R") +#define PHP_ION_SYMBOL_BACKREF (g_sym_tab_php_sym[2]).s + PHP_ION_SYMBOL("Backref", "r") +#define PHP_ION_SYMBOL_PROPERTY (g_sym_tab_php_sym[3]).s + PHP_ION_SYMBOL("Property", "p") +#define PHP_ION_SYMBOL_CLASS_OBJECT (g_sym_tab_php_sym[4]).s + PHP_ION_SYMBOL("ClassObject", "c") +#define PHP_ION_SYMBOL_CUSTOM_OBJECT (g_sym_tab_php_sym[5]).s + PHP_ION_SYMBOL("CustomObject", "C") +#define PHP_ION_SYMBOL_OBJECT (g_sym_tab_php_sym[6]).s + PHP_ION_SYMBOL("Object", "o") +#define PHP_ION_SYMBOL_MAGIC_OBJECT (g_sym_tab_php_sym[7]).s + PHP_ION_SYMBOL("MagicObject", "O") +#define PHP_ION_SYMBOL_SERIALIZEABLE (g_sym_tab_php_sym[8]).s + PHP_ION_SYMBOL("Serializable", "S") +#define PHP_ION_SYMBOL_ENUM (g_sym_tab_php_sym[9]).s + PHP_ION_SYMBOL("Enum", "E") + {{0}, {0}} +}; +#undef PHP_ION_SYMBOL + +static ION_SYMBOL_TABLE *g_sym_tab_php; + +/* [SID => STRING, STRING => SID] */ +static HashTable g_sym_hash; +/* [enum_case_name => SID] */ +static HashTable g_sym_map; + +#define ION_SYS_SYMBOL_SYMBOL_TABLE \ + ION_SYS_SYMBOL_ION_SYMBOL_TABLE +#define g_sym_add(enum_name, c_name) do { \ + g_sym_hash_add(ION_SYS_SID_ ## c_name, ION_SYS_SYMBOL_ ## c_name, ION_SYS_STRLEN_ ## c_name); \ + g_sym_map_add(ION_SYS_SID_ ## c_name, enum_name, sizeof(enum_name)-1); \ +} while (0) + +static inline void g_sym_hash_add(int sid, const char *str, size_t len) +{ + zval zl, zs; + ZVAL_LONG(&zl, sid); + ZVAL_STR(&zs, zend_string_init_interned(str, len ,1)); + zend_hash_add(&g_sym_hash, Z_STR(zs), &zl); + zend_hash_index_add(&g_sym_hash, sid, &zs); +} + +static inline void g_sym_map_add(int sid, const char *str, size_t len) +{ + zval zv; + ZVAL_LONG(&zv, sid); + zend_hash_str_add(&g_sym_map, str, len, &zv); +} + +static inline int g_sym_init(void) +{ + zend_hash_init(&g_sym_hash, 0, NULL, NULL, 1); + zend_hash_init(&g_sym_map, 0, NULL, NULL, 1); + + g_sym_hash_add(0, ZEND_STRL("")); + g_sym_map_add(0, ZEND_STRL("")); + g_sym_add("Ion", ION); + g_sym_add("Ivm_1_0", IVM); + g_sym_add("IonSymbolTable", SYMBOL_TABLE); + g_sym_add("Name", NAME); + g_sym_add("Version", VERSION); + g_sym_add("Imports", IMPORTS); + g_sym_add("Symbols", SYMBOLS); + g_sym_add("MaxId", MAX_ID); + g_sym_add("SharedSymbolTable", SHARED_SYMBOL_TABLE); + + int sys_max_id = ION_SYS_SID_SHARED_SYMBOL_TABLE; + + if (IERR_OK != ion_symbol_table_open_with_type(&g_sym_tab_php, NULL, ist_SHARED)) { + return FAILURE; + } + php_ion_global_symbol *ptr = g_sym_tab_php_sym; + ion_symbol_table_set_version(g_sym_tab_php, PHP_ION_SYMBOL_TABLE_VERSION); + ion_symbol_table_set_name(g_sym_tab_php, &ptr->s.value); + while (ptr->e.value) { + ion_symbol_table_add_symbol(g_sym_tab_php, &ptr->s.value, &ptr->s.sid); + g_sym_hash_add(sys_max_id + ptr->s.sid, (const char *) ptr->s.value.value, ptr->s.value.length); + g_sym_map_add(sys_max_id + ptr->s.sid, (const char *) ptr->e.value, ptr->e.length); + ++ptr; + } + ion_symbol_table_lock(g_sym_tab_php); + return SUCCESS; +} + typedef struct php_ion_serializer { ION_WRITER *writer; ION_WRITER_OPTIONS *options; @@ -22,6 +129,7 @@ typedef struct php_ion_serializer { zend_string *call_custom; zend_bool call_magic; + zend_bool multi_seq; uint32_t level; HashTable *ids; @@ -29,6 +137,7 @@ typedef struct php_ion_serializer { } php_ion_serializer; typedef struct php_ion_annotaions { + uint8_t shared_symtab:1; uint8_t backref:1; uint8_t makeref:1; uint8_t object_prop:1; @@ -44,6 +153,7 @@ typedef struct php_ion_unserializer { zend_string *call_custom; zend_bool call_magic; + zend_bool multi_seq; uint32_t level; HashTable *ids; @@ -55,6 +165,16 @@ typedef struct php_ion_unserializer { ZEND_BEGIN_MODULE_GLOBALS(ion) + struct { + decContext ctx; + ION_DECIMAL zend_max; + ION_DECIMAL zend_min; + } decimal; + + struct { + HashTable cache; + } symbol; + php_ion_serializer serializer; php_ion_unserializer unserializer; @@ -73,6 +193,53 @@ ZEND_END_MODULE_GLOBALS(ion); ZEND_DECLARE_MODULE_GLOBALS(ion); +static zend_class_entry + *ce_Catalog, + *ce_Decimal, + *ce_Decimal_Context, + *ce_Decimal_Context_Rounding, + *ce_LOB, + *ce_Reader, + *ce_Reader_Options, + *ce_Reader_Reader, + *ce_Reader_Buffer, + *ce_Reader_Stream, + *ce_Reader_Buffer_Reader, + *ce_Reader_Stream_Reader, + *ce_Serializer, + *ce_Serializer_PHP, + *ce_Symbol, + *ce_Symbol_ImportLocation, + *ce_Symbol_Enum, + *ce_Symbol_Table, + *ce_Symbol_Table_Local, + *ce_Symbol_Table_PHP, + *ce_Symbol_Table_Shared, + *ce_Symbol_Table_System, + *ce_Timestamp, + *ce_Timestamp_Precision, + *ce_Type, + *ce_Unserializer, + *ce_Unserializer_PHP, + *ce_Writer, + *ce_Writer_Options, + *ce_Writer_Buffer, + *ce_Writer_Buffer_Writer, + *ce_Writer_Stream, + *ce_Writer_Stream_Writer, + *ce_Writer_Writer + ; + +static inline void php_ion_globals_symbols_init(void) +{ + zend_hash_init(&php_ion_globals.symbol.cache, 0, NULL, ZVAL_PTR_DTOR, 0); +} + +static inline void php_ion_globals_symbols_dtor(void) +{ + zend_hash_destroy(&php_ion_globals.symbol.cache); +} + static inline void php_ion_globals_serializer_init(void) { php_ion_serializer *s = &php_ion_globals.serializer; @@ -164,41 +331,6 @@ static inline void php_ion_globals_unserializer_dtor(void) zend_hash_destroy(s->tmp); } -static zend_class_entry - *ce_Annotation, - *ce_Catalog, - *ce_Collection, - *ce_Decimal, - *ce_Decimal_Context, - *ce_LOB, - *ce_Reader, - *ce_Reader_Options, - *ce_Reader_Reader, - *ce_Reader_Buffer, - *ce_Reader_Stream, - *ce_Reader_Buffer_Reader, - *ce_Reader_Stream_Reader, - *ce_Serializer, - *ce_Serializer_PHP, - *ce_Symbol, - *ce_Symbol_ImportLocation, - *ce_Symbol_System, - *ce_Symbol_System_SID, - *ce_Symbol_Table, - *ce_Timestamp, - *ce_Timestamp_Precision, - *ce_Type, - *ce_Unserializer, - *ce_Unserializer_PHP, - *ce_Writer, - *ce_Writer_Options, - *ce_Writer_Buffer, - *ce_Writer_Buffer_Writer, - *ce_Writer_Stream, - *ce_Writer_Stream_Writer, - *ce_Writer_Writer - ; - #define php_ion_decl(type, cname, ...) \ static zend_object_handlers oh_ ## cname; \ static inline zend_object *create_ion_ ## cname(zend_class_entry *ce) \ @@ -226,17 +358,20 @@ static zend_class_entry } while (0) #define php_ion_obj(type, obj) \ - ((php_ion_ ## type *) (obj ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL)) + ((php_ion_ ## type *) ((obj) ? ((char *)(obj) - XtOffsetOf(php_ion_ ## type, std)) : NULL)) -#define ION_CHECK(err, ...) do { \ +#define ION_CHECK_RETURN(r, err, ...) do { \ iERR __err = err; \ if (__err) { \ zend_throw_exception_ex(spl_ce_RuntimeException, __err, "%s: %s", ion_error_to_str(__err), #err); \ __VA_ARGS__; \ - return; \ + return r; \ } \ } while (0) +#define ION_CHECK(err, ...) \ + ION_CHECK_RETURN(, err, __VA_ARGS__) + #define ION_CATCH(...) do { \ if (EG(exception)) { \ __VA_ARGS__; \ @@ -244,29 +379,24 @@ static zend_class_entry } \ } while (0) -#define PTR_CHECK(ptr, ...) do { \ +#define PTR_CHECK_RETURN(ret, ptr, ...) do { \ if (!(ptr)) { \ zend_throw_error(NULL, "Uninitialized object"); \ __VA_ARGS__; \ - return; \ + return ret; \ } \ } while (0) +#define PTR_CHECK(ptr, ...) PTR_CHECK_RETURN(, ptr, __VA_ARGS__) -#define OBJ_CHECK(obj) do { \ - PTR_CHECK(obj); \ - PTR_CHECK(*((void **)obj)); \ +#define OBJ_CHECK_RETURN(ret, obj, ...) do { \ + PTR_CHECK_RETURN(ret, obj, __VA_ARGS__); \ + PTR_CHECK_RETURN(ret, *((void **)obj), __VA_ARGS__); \ } while (0) - -static inline ION_STRING *ion_string_from_cstr(ION_STRING *is, const char *s, size_t l) -{ - is->length = l; - is->value = (BYTE *) s; - return is; -} +#define OBJ_CHECK(obj, ...) OBJ_CHECK_RETURN(, obj, __VA_ARGS__) static inline ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs) { - is->length = zs ? zs->len : 0; + is->length = zs ? (SIZE) zs->len : 0; is->value = (BYTE *) (zs ? zs->val : NULL); return is; } @@ -283,13 +413,6 @@ static inline void update_property_obj(zend_object *obj, const char *n, size_t l zend_update_property(obj->ce, obj, n, l, &zobj); } -typedef struct php_ion_type { - ION_TYPE typ; - zend_object std; -} php_ion_type; - -php_ion_decl(type, Type); - #define RETURN_IONTYPE(typ) do { \ zend_object *__zo = php_ion_type_fetch(typ); \ if (UNEXPECTED(!__zo)) { \ @@ -310,6 +433,11 @@ static inline zend_object *php_ion_type_fetch(ION_TYPE typ) return zend_enum_get_case(ce_Type, Z_STR_P(ztype)); } +static inline ION_TYPE ion_type_from_enum(zend_object *zo) +{ + return (ION_TYPE) Z_LVAL_P(zend_enum_fetch_case_value(zo)); +} + typedef struct php_ion_symbol_iloc { ION_SYMBOL_IMPORT_LOCATION loc; zend_string *name; @@ -336,6 +464,27 @@ typedef struct php_ion_symbol { zend_object *iloc, std; } php_ion_symbol; +static inline int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) { + zend_string *zs1 = zval_get_string(zv1); + zend_string *zs2 = zval_get_string(zv2); + + if (EG(exception)) { + return 0; + } + + int result; + if (zs1->len > zs2->len) { + result = 1; + } else if (zs2->len > zs1->len) { + result = -1; + } else { + result = memcmp(zs1->val, zs2->val, zs1->len); + } + zend_string_release(zs1); + zend_string_release(zs2); + return result; +} + static inline void php_ion_symbol_ctor(php_ion_symbol *obj) { zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("sid"), @@ -387,38 +536,133 @@ static inline void php_ion_symbol_zval(ION_SYMBOL *sym_ptr, zval *return_value) } } +static inline zval *php_ion_global_symbol_fetch_by_enum(zend_string *name) +{ + zval *zgs = zend_hash_find(&php_ion_globals.symbol.cache, name); + if (!zgs) { + zval *zid = zend_hash_find(&g_sym_map, name); + if (zid) { + zval *zss = zend_hash_index_find(&g_sym_hash, Z_LVAL_P(zid)); + if (zss) { + zval zsym; + object_init_ex(&zsym, ce_Symbol); + php_ion_symbol *sym = php_ion_obj(symbol, Z_OBJ(zsym)); + sym->sym.sid = Z_LVAL_P(zid); + sym->value = zval_get_string(zss); + php_ion_symbol_ctor(sym); + zgs = zend_hash_add(&php_ion_globals.symbol.cache, name, &zsym); + } + } + } + return zgs; +} + php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj)); typedef struct php_ion_symbol_table { + ION_SYMBOL_TABLE *tab; + int (*dtor)(ION_SYMBOL_TABLE *); zend_object std; } php_ion_symbol_table; -php_ion_decl(symbol_table, Symbol_Table); +static inline void php_ion_symbol_table_ctor(php_ion_symbol_table *obj) +{ + OBJ_CHECK(obj); + + ION_STRING is; + if (IERR_OK == ion_symbol_table_get_name(obj->tab, &is)) { + zend_update_property_stringl(obj->std.ce, &obj->std, ZEND_STRL("name"), (char *) is.value, is.length); + } + int32_t iv; + if (IERR_OK == ion_symbol_table_get_version(obj->tab, &iv)) { + zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("version"), iv); + } +} + +static inline void php_ion_symbol_table_dtor(php_ion_symbol_table *obj) +{ + if (obj->tab) { + if (obj->dtor) { + obj->dtor(obj->tab); + } + obj->tab = NULL; + } +} + +static inline void php_ion_symbol_table_import(php_ion_symbol_table *obj, php_ion_symbol_table *import) +{ + OBJ_CHECK(obj); + OBJ_CHECK(import); + + zval tmp; + zval *zimports = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), 0, &tmp); + if (zimports) { + zend_ulong idx = (uintptr_t) &import->std.gc; + if (!zend_hash_index_exists(Z_ARRVAL_P(zimports), idx)) { + ION_CHECK(ion_symbol_table_import_symbol_table(obj->tab, import->tab)); + + SEPARATE_ARRAY(zimports); + GC_ADDREF(&import->std); + add_index_object(zimports, idx, &import->std); + zend_update_property(obj->std.ce, &obj->std, ZEND_STRL("imports"), zimports); + } + } +} + +static inline void php_ion_symbol_table_symbol_zval(php_ion_symbol_table *obj, ION_SYMBOL *sym, zval *return_value) +{ + zval tmp; + zval *zsyms = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbols"), 0, &tmp); + if (zsyms) { + zval *zsym = zend_hash_index_find(Z_ARRVAL_P(zsyms), sym->sid); + if (zsym) { + RETURN_COPY(zsym); + } + } + + php_ion_symbol_zval(sym, return_value); + + if (zsyms) { + SEPARATE_ARRAY(zsyms); + ZVAL_ADDREF(return_value); + add_index_zval(zsyms, sym->sid, return_value); + } +} + +php_ion_decl(symbol_table, Symbol_Table, php_ion_symbol_table_dtor(obj)); typedef struct php_ion_decimal_ctx { decContext ctx; zend_object std; } php_ion_decimal_ctx; -static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj) { - zval tmp, *zbits = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("bits"), 1, &tmp); +#define php_ion_decimal_ctx_init_max(c, rounding) \ + php_ion_decimal_ctx_init((c), DEC_MAX_DIGITS, DEC_MAX_EMAX, DEC_MIN_EMIN, (rounding), false) +static inline void php_ion_decimal_ctx_init(decContext *ctx, + int digits, int emax, int emin, enum rounding round, zend_bool clamp) +{ + memset(ctx, 0, sizeof(*ctx)); + ctx->digits = digits; + ctx->emax = emax; + ctx->emin = emin; + ctx->round = round; + ctx->clamp = clamp; +} - int bits = 128; - if (zbits != &EG(uninitialized_zval)) { - bits = Z_LVAL_P(zbits); - } else { - zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("bits"), bits); +static inline void php_ion_decimal_ctx_ctor(php_ion_decimal_ctx *obj, zend_object *o_round) +{ + if (!obj->ctx.digits) { + php_ion_decimal_ctx_init_max(&obj->ctx, DEC_ROUND_HALF_EVEN); } - switch (bits) { - case 32: - case 64: - case 128: - decContextDefault(&obj->ctx, bits); - break; - default: - zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG, - "Decimal context only allows 32, 64 or 128 bits"); + if (o_round) { + update_property_obj(&obj->std, ZEND_STRL("round"), o_round); + } else { + zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("round"), obj->ctx.round); } + zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("digits"), obj->ctx.digits); + zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("eMax"), obj->ctx.emax); + zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("eMin"), obj->ctx.emin); + zend_update_property_bool(obj->std.ce, &obj->std, ZEND_STRL("clamp"), obj->ctx.clamp); } php_ion_decl(decimal_ctx, Decimal_Context); @@ -428,6 +672,25 @@ typedef struct php_ion_decimal { zend_object *ctx, std; } php_ion_decimal; +static inline void php_ion_decimal_from_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long num) +{ + if (num <= INT32_MAX && num >= INT32_MIN) { + ION_CHECK(ion_decimal_from_int32(dec, num)); + } else if (num > 0 && num <= UINT32_MAX) { + ION_CHECK(ion_decimal_from_uint32(dec, num)); + } else { + ION_INT *iint; + ION_CHECK(ion_int_alloc(NULL, &iint)); + ION_CHECK(ion_int_from_long(iint, num), + ion_int_free(iint)); + /* WATCH OUT: BS API */ + dec->type = ION_DECIMAL_TYPE_QUAD; + ION_CHECK(ion_decimal_from_ion_int(dec, ctx, iint), + ion_int_free(iint)); + ion_int_free(iint); + } +} + static inline zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec) { zend_string *zstr = zend_string_alloc(ION_DECIMAL_STRLEN(dec), 0); @@ -435,7 +698,7 @@ static inline zend_string *php_ion_decimal_to_string(ION_DECIMAL *dec) return zend_string_truncate(zstr, strlen(zstr->val), 0); } -static inline void php_ion_decimal_to_int(ION_DECIMAL *dec, decContext *ctx, zend_long *l) +static inline void php_ion_decimal_to_zend_long(ION_DECIMAL *dec, decContext *ctx, zend_long *l) { ION_INT *ii = NULL; ION_CHECK(ion_int_alloc(NULL, &ii)); @@ -454,13 +717,13 @@ static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj) return false; } - result = 1; - ion_decimal_compare(&obj->dec, &g_ion_dec_zend_max, &g_dec_ctx, &result); + result = 1; + ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_max, &php_ion_globals.decimal.ctx, &result); if (result == 1) { return false; } result = -1; - ion_decimal_compare(&obj->dec, &g_ion_dec_zend_min, &g_dec_ctx, &result); + ion_decimal_compare(&obj->dec, &php_ion_globals.decimal.zend_min, &php_ion_globals.decimal.ctx, &result); if (result == -1) { return false; } @@ -473,14 +736,14 @@ static inline void php_ion_decimal_ctor(php_ion_decimal *obj) zval zdc; object_init_ex(&zdc, ce_Decimal_Context); obj->ctx = Z_OBJ(zdc); - php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx)); + php_ion_decimal_ctx_ctor(php_ion_obj(decimal_ctx, obj->ctx), NULL); GC_DELREF(obj->ctx); } update_property_obj(&obj->std, ZEND_STRL("context"), obj->ctx); if (php_ion_decimal_fits_zend_long(obj)) { zend_long l; - php_ion_decimal_to_int(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l); + php_ion_decimal_to_zend_long(&obj->dec, &php_ion_obj(decimal_ctx, obj->ctx)->ctx, &l); zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("number"), l); } else { zend_string *zstr = php_ion_decimal_to_string(&obj->dec); @@ -500,20 +763,26 @@ typedef php_date_obj php_ion_timestamp; static inline zend_long php_usec_from_ion(const decQuad *frac, decContext *ctx) { + if (!ctx) { + ctx = &php_ion_globals.decimal.ctx; + } decQuad microsecs, result; decQuadMultiply(&result, decQuadFromInt32(µsecs, 1000000), frac, ctx); return (zend_long) decQuadToUInt32(&result, ctx, DEC_ROUND_HALF_EVEN); } -static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, zend_long usec, decContext *ctx) +static inline decQuad *ion_ts_frac_from_usec(decQuad *frac, int usec, decContext *ctx) { + if (!ctx) { + ctx = &php_ion_globals.decimal.ctx; + } decQuad microsecs, us; return decQuadDivide(frac, decQuadFromInt32(&us, usec), decQuadFromInt32(µsecs, 1000000), ctx); } static inline zend_string *php_dt_format_from_precision(uint8_t precision) { - switch (precision) { + switch (precision & 0x7f) { case ION_TS_FRAC: return zend_string_init(ZEND_STRL("c"), 0); case ION_TS_SEC: @@ -533,9 +802,11 @@ static inline zend_string *php_dt_format_from_precision(uint8_t precision) static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContext *ctx, zend_string **fmt) { - timelib_time *time = timelib_time_ctor(); + timelib_time *time = ecalloc(1, sizeof(*time)); - switch (ts->precision) { + int precision = ION_TS_FRAC; + ion_timestamp_get_precision(ts, &precision); + switch (precision) { case ION_TS_FRAC: time->us = php_usec_from_ion(&ts->fraction, ctx); /* fallthrough */ @@ -557,10 +828,16 @@ static inline timelib_time* php_time_from_ion(const ION_TIMESTAMP *ts, decContex /* fallthrough */ default: time->z = ts->tz_offset * 60; + if (time->z) { + time->zone_type = TIMELIB_ZONETYPE_OFFSET; + } else { + time->zone_type = TIMELIB_ZONETYPE_ID; + time->tz_info = get_timezone_info(); + } } if (fmt) { - *fmt = php_dt_format_from_precision(ts->precision); + *fmt = php_dt_format_from_precision(precision); } return time; } @@ -570,14 +847,14 @@ static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_ memset(buf, 0, sizeof(*buf)); zval tmp; - uint8_t precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp)); + int precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp)); if (!precision || precision > ION_TS_FRAC) { zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG, - "Invalid precision (%u) of ion\\Timestamp", (unsigned) precision); + "Invalid precision (%d) of ion\\Timestamp", precision); } else switch ((buf->precision = precision)) { case ION_TS_FRAC: - ion_ts_frac_from_usec(&buf->fraction, ts->time->us, ctx); + ion_ts_frac_from_usec(&buf->fraction, (int) ts->time->us, ctx); /* fallthrough */ case ION_TS_SEC: buf->seconds = ts->time->s; @@ -596,7 +873,10 @@ static inline ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_ buf->year = ts->time->y; /* fallthrough */ default: - buf->tz_offset = ts->time->z / 60; + buf->tz_offset = (short) (ts->time->z / 60); + if (buf->tz_offset) { + buf->precision |= 0x80; + } } return buf; @@ -614,28 +894,126 @@ static inline void php_ion_timestamp_ctor(php_ion_timestamp *obj, zend_long prec zend_string_release(fmt); } -static inline void php_ion_timestamp_dtor(php_ion_timestamp *obj) +typedef struct php_ion_catalog { + ION_CATALOG *cat; + zend_object std; +} php_ion_catalog; + +static inline void php_ion_catalog_ctor(php_ion_catalog *obj) +{ + ION_CHECK(ion_catalog_open(&obj->cat)); +} + +static inline void php_ion_catalog_dtor(php_ion_catalog *obj) { - if (obj->time) { - timelib_time_dtor(obj->time); + if (obj->cat) { + ion_catalog_close(obj->cat); } } -php_ion_decl(timestamp, Timestamp, php_ion_timestamp_dtor(obj)); +static inline zend_string *ion_symbol_table_to_key(ION_SYMBOL_TABLE *tab) +{ + int32_t version; + ION_STRING is; + ION_CHECK_RETURN(NULL, ion_symbol_table_get_name(tab, &is)); + ION_CHECK_RETURN(NULL, ion_symbol_table_get_version(tab, &version)); -typedef struct php_ion_catalog { - ION_CATALOG *cat; - zend_object std; -} php_ion_catalog; + smart_str s = {0}; + smart_str_appendl(&s, (char *) is.value, is.length); + smart_str_appendc(&s, ':'); + smart_str_append_long(&s, version); + smart_str_0(&s); -php_ion_decl(catalog, Catalog); + return s.s; +} + +static inline void php_ion_catalog_add_symbol_table(php_ion_catalog *obj, php_ion_symbol_table *tab) +{ + OBJ_CHECK(obj); + OBJ_CHECK(tab); + + zval tmp; + zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp); + if (ztabs) { + zend_ulong idx = (uintptr_t) &tab->std.gc; + if (!zend_hash_index_exists(Z_ARRVAL_P(ztabs), idx)) { + zend_string *key = ion_symbol_table_to_key(tab->tab); + if (key) { + ION_CHECK(ion_catalog_add_symbol_table(obj->cat, tab->tab), + zend_string_release(key)); + SEPARATE_ARRAY(ztabs); + GC_ADDREF(&tab->std); + add_index_object(ztabs, idx, &tab->std); + GC_ADDREF(&tab->std); + add_assoc_object_ex(ztabs, key->val, key->len, &tab->std); + zend_update_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), ztabs); + zend_string_release(key); + } + } + } +} + +static inline void php_ion_catalog_symbol_table_zval(php_ion_catalog *obj, ION_SYMBOL_TABLE *tab, zval *return_value) +{ + zend_string *key = ion_symbol_table_to_key(tab); + PTR_CHECK(key); + + zval tmp; + zval *ztabs = zend_read_property(obj->std.ce, &obj->std, ZEND_STRL("symbolTables"), 0, &tmp); + if (ztabs) { + zval *ztab = zend_hash_find(Z_ARRVAL_P(ztabs), key); + if (ztab) { + zend_string_release(key); + RETURN_COPY(ztab); + } + } + + php_error_docref(NULL, E_NOTICE, "Previously unknown ion\\Symbol\\Table encountered: %s", key->val); + + object_init_ex(return_value, ce_Symbol_Table_Shared); + php_ion_symbol_table *o_tab = php_ion_obj(symbol_table, Z_OBJ_P(return_value)); + OBJ_CHECK(o_tab, zend_string_release(key)); + php_ion_symbol_table_ctor(o_tab); + + if (ztabs) { + SEPARATE_ARRAY(ztabs); + ZVAL_ADDREF(return_value); + add_index_zval(ztabs, (uintptr_t) &o_tab->std.gc, return_value); + ZVAL_ADDREF(return_value); + add_assoc_zval_ex(ztabs, key->val, key->len, return_value); + } + zend_string_release(key); +} + +php_ion_decl(catalog, Catalog, php_ion_catalog_dtor(obj)); + +typedef struct php_ion_reader_options_ccn_ctx { + zend_object *obj; + zend_fcall_info fci; + zend_fcall_info_cache fcc; +} php_ion_reader_options_ccn_ctx; typedef struct php_ion_reader_options { ION_READER_OPTIONS opt; + php_ion_reader_options_ccn_ctx ccn; zend_object *cat, *dec_ctx, *cb, std; } php_ion_reader_options; -php_ion_decl(reader_options, Reader_Options); +static inline void php_ion_reader_options_dtor(php_ion_reader_options *obj) +{ + if (obj->cb) { + zend_fcall_info_args_clear(&obj->ccn.fci, true); + } +} + +php_ion_decl(reader_options, Reader_Options, php_ion_reader_options_dtor(obj)); + +static inline zend_object *php_ion_reader_options_new(void) +{ + zend_object *obj = create_ion_Reader_Options(NULL); + zend_call_known_instance_method_with_0_params(obj->ce->constructor, obj, NULL); + return obj; +} typedef struct php_ion_reader { ION_READER *reader; @@ -682,42 +1060,42 @@ static inline iERR php_ion_reader_stream_handler(struct _ion_user_stream *user) static inline iERR on_context_change(void *context, ION_COLLECTION *imports) { + iERR e = IERR_OK; + if (context) { - php_ion_reader *obj = php_ion_obj(reader, context); - (void) obj; + php_ion_reader_options_ccn_ctx *ctx = context; + + zval zobj; + ZVAL_OBJ(&zobj, ctx->obj); + zend_fcall_info_argn(&ctx->fci, 1, &zobj); + if (SUCCESS != zend_fcall_info_call(&ctx->fci, &ctx->fcc, NULL, NULL)) { + e = IERR_INTERNAL_ERROR; + } + zend_fcall_info_args_clear(&ctx->fci, false); } - return IERR_OK; + return e; } -static ION_READER_CONTEXT_CHANGE_NOTIFIER EMPTY_READER_CHANGE_NOTIFIER = { - on_context_change, - NULL -}; - static inline void php_ion_reader_ctor(php_ion_reader *obj) { iERR err; php_ion_reader_options *opt = php_ion_obj(reader_options, obj->opt); - if (opt) { - opt->opt.context_change_notifier.context = obj; + if (opt && opt->opt.context_change_notifier.context) { + php_ion_reader_options_ccn_ctx *ctx = opt->opt.context_change_notifier.context; + ctx->obj = &obj->std; + opt->opt.context_change_notifier.notify = on_context_change; } if (obj->type == STREAM_READER) { PTR_CHECK(obj->stream.ptr); GC_ADDREF(obj->stream.ptr->res); - php_ion_reader_options *opt = php_ion_obj(reader_options, obj->opt); - obj->stream.buf.length = opt ? opt->opt.allocation_page_size : 0x1000; + obj->stream.buf.length = opt && opt->opt.allocation_page_size ? opt->opt.allocation_page_size : 0x10000; obj->stream.buf.value = emalloc(obj->stream.buf.length); err = ion_reader_open_stream(&obj->reader, obj, php_ion_reader_stream_handler, opt ? &opt->opt : NULL); } else { - err = ion_reader_open_buffer(&obj->reader, - (BYTE *) obj->buffer->val, obj->buffer->len, - opt ? &opt->opt : NULL); - } - if (opt) { - opt->opt.context_change_notifier.context = NULL; + err = ion_reader_open_buffer(&obj->reader, (BYTE *) obj->buffer->val, (SIZE) obj->buffer->len, opt ? &opt->opt : NULL); } ION_CHECK(err); @@ -746,11 +1124,18 @@ php_ion_decl(reader, Reader_Reader, php_ion_reader_dtor(obj)); typedef struct php_ion_writer_options { ION_WRITER_OPTIONS opt; - zend_object *cat, *dec_ctx, *col, std; + zend_object *cat, *dec_ctx, std; } php_ion_writer_options; php_ion_decl(writer_options, Writer_Options); +static inline zend_object *php_ion_writer_options_new(void) +{ + zend_object *obj = create_ion_Writer_Options(NULL); + zend_call_known_instance_method_with_0_params(obj->ce->constructor, obj, NULL); + return obj; +} + typedef struct php_ion_writer { ION_WRITER *writer; enum { @@ -865,13 +1250,56 @@ static inline iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user) return IERR_OK; } +static inline ION_COLLECTION *php_ion_catalog_collection(php_ion_catalog *cat) +{ + /* do not look too close */ + struct { + void *owner; + ION_SYMBOL_TABLE *sys; + ION_COLLECTION collection; + } *cat_ptr = (void *) cat->cat; + return &cat_ptr->collection; +} + +static inline void php_ion_writer_options_init_shared_imports(php_ion_writer_options *opt) +{ + php_ion_catalog *cat = php_ion_obj(catalog, opt->cat); + OBJ_CHECK(cat); + + ION_CHECK(ion_writer_options_initialize_shared_imports(&opt->opt)); + + ION_COLLECTION *col = php_ion_catalog_collection(cat); + if (!ION_COLLECTION_IS_EMPTY(col)) { + // holy, nah, forget it batman... +#ifndef IPCN_pNODE_TO_pDATA +# define IPCN_pNODE_TO_pDATA(x) (&((x)->_data[0])) +#endif + ION_COLLECTION_CURSOR cur; + ION_COLLECTION_OPEN(col, cur); + while (cur) { + ION_SYMBOL_TABLE **ptr; + ION_COLLECTION_NEXT(cur, ptr); + if (*ptr) { + ION_CHECK(ion_writer_options_add_shared_imports_symbol_tables(&opt->opt, ptr, 1)); + } else { + break; + } + } + } +} + static inline void php_ion_writer_ctor(php_ion_writer *obj) { + php_ion_writer_options *opt = NULL; + if (obj->opt) { update_property_obj(&obj->std, ZEND_STRL("options"), obj->opt); + opt = php_ion_obj(writer_options, obj->opt); + if (opt->cat) { + php_ion_writer_options_init_shared_imports(opt); + } } - php_ion_writer_options *opt = php_ion_obj(writer_options, obj->opt); ION_STREAM_HANDLER h; if (obj->type == STREAM_WRITER) { h = php_ion_writer_stream_handler; @@ -881,6 +1309,7 @@ static inline void php_ion_writer_ctor(php_ion_writer *obj) php_ion_writer_buffer_init(obj); } + ION_CHECK(ion_writer_open_stream(&obj->writer, h, obj, opt ? &opt->opt : NULL)); OBJ_CHECK(obj); } @@ -890,6 +1319,12 @@ static inline void php_ion_writer_dtor(php_ion_writer *obj) if (obj->writer) { ion_writer_close(obj->writer); } + if (obj->opt) { + php_ion_writer_options *opt = php_ion_obj(writer_options, obj->opt); + if (opt->cat) { + ion_writer_options_close_shared_imports(&opt->opt); + } + } if (obj->type == STREAM_WRITER) { if (obj->stream.buf.value) { efree(obj->stream.buf.value); @@ -919,6 +1354,8 @@ static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj) ser_obj->serializer.ids = global_ser->ids; ser_obj->serializer.tmp = global_ser->tmp; + zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("multiSequence"), + ser_obj->serializer.multi_seq); zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"), ser_obj->serializer.call_magic); if (ser_obj->serializer.call_custom) { @@ -928,13 +1365,14 @@ static inline void php_ion_serializer_php_ctor(php_ion_serializer_php *ser_obj) } else { zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize")); } - if (ser_obj->opt) { - php_ion_writer_options *o_woptions = php_ion_obj(writer_options, ser_obj->opt); - ser_obj->serializer.options = &o_woptions->opt; - update_property_obj(&ser_obj->std, ZEND_STRL("writerOptions"), ser_obj->opt); + if (!ser_obj->opt) { + ser_obj->opt = php_ion_writer_options_new(); } else { - zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("writerOptions")); + GC_ADDREF(ser_obj->opt); } + ser_obj->serializer.options = &php_ion_obj(writer_options, ser_obj->opt)->opt; + update_property_obj(&ser_obj->std, ZEND_STRL("writerOptions"), ser_obj->opt); + GC_DELREF(ser_obj->opt); } static inline void php_ion_serializer_php_dtor(php_ion_serializer_php *obj) @@ -946,7 +1384,7 @@ static inline void php_ion_serializer_php_dtor(php_ion_serializer_php *obj) static inline void php_ion_serialize_zval(php_ion_serializer *, zval *); -static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array *arr, bool props) +static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array *arr, bool unmangle_props, bool annotate_props) { ION_CHECK(ion_writer_start_container(ser->writer, tid_STRUCT)); @@ -954,24 +1392,32 @@ static inline void php_ion_serialize_struct(php_ion_serializer *ser, zend_array zend_ulong h; zend_string *k = NULL; if (arr) ZEND_HASH_FOREACH_KEY_VAL_IND(arr, h, k, v) + char buf[MAX_LENGTH_OF_LONG + 1]; ION_STRING is; if (k) { size_t prop_len; const char *class_name, *prop_name; - if (props && (SUCCESS == zend_unmangle_property_name_ex(k, &class_name, &prop_name, &prop_len)) && class_name) { - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("p")))); - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, class_name, prop_name - class_name - 1))); + if (unmangle_props && (SUCCESS == zend_unmangle_property_name_ex(k, &class_name, &prop_name, &prop_len)) && class_name) { + if (annotate_props) { + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_PROPERTY)); + ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, (char *) class_name, prop_name - class_name - 1))); + } } else { prop_name = k->val; prop_len = k->len; } - ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_from_cstr(&is, prop_name, prop_len))); + ion_string_assign_cstr(&is, (char *) prop_name, (SIZE) prop_len); } else { - char buf[MAX_LENGTH_OF_LONG + 1], *end = buf + sizeof(buf) - 1; + char *end = buf + sizeof(buf) - 1; char *ptr = zend_print_long_to_buf(end, (zend_long) h); - ION_CHECK(ion_writer_write_field_name(ser->writer, ion_string_from_cstr(&is, ptr, end - ptr))); + ion_string_assign_cstr(&is, ptr, (SIZE) (end - ptr)); } + // WATCH OUT: field names need to be copied + ION_STRING fn; + ION_CHECK(ion_string_copy_to_owner(ser->writer, &fn, &is)); + ION_CHECK(ion_writer_write_field_name(ser->writer, &fn)); + php_ion_serialize_zval(ser, v); ION_CATCH(); ZEND_HASH_FOREACH_END(); @@ -997,7 +1443,7 @@ static inline void php_ion_serialize_array(php_ion_serializer *ser, zend_array * if (zend_array_is_list(arr)) { php_ion_serialize_list(ser, arr); } else { - php_ion_serialize_struct(ser, arr, false); + php_ion_serialize_struct(ser, arr, false, false); } } @@ -1010,9 +1456,9 @@ static inline void php_ion_serialize_object_iface(php_ion_serializer *ser, zend_ ZVAL_OBJ(&tmp, zobject); if (SUCCESS == zobject->ce->serialize(&tmp, &buf, &len, NULL)) { ION_STRING is; - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("S")))); + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_SERIALIZEABLE)); ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name))); - ION_CHECK(ion_writer_write_string(ser->writer, ion_string_from_cstr(&is, (char *) buf, len))); + ION_CHECK(ion_writer_write_clob(ser->writer, buf, len)); efree(buf); } else if (!EG(exception)){ zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR, @@ -1030,7 +1476,7 @@ static inline void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_ if (IS_ARRAY == Z_TYPE(rv)) { ION_STRING is; - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, fn ? "C" : "O", 1))); + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, fn ? &PHP_ION_SYMBOL_CUSTOM_OBJECT : &PHP_ION_SYMBOL_MAGIC_OBJECT)); ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name))); php_ion_serialize_zval(ser, &rv); zval_ptr_dtor(&rv); @@ -1045,7 +1491,7 @@ static inline void php_ion_serialize_object_magic(php_ion_serializer *ser, zend_ static inline void php_ion_serialize_object_enum(php_ion_serializer *ser, zend_object *zobject) { ION_STRING is; - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("E")))); + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_ENUM)); ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name))); zval *z_cname = zend_enum_fetch_case_name(zobject); @@ -1057,17 +1503,17 @@ static inline void php_ion_serialize_object_std(php_ion_serializer *ser, zend_ob ION_STRING is; if (zobject->ce != zend_standard_class_def) { - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("c")))); + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_CLASS_OBJECT)); ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_zend(&is, zobject->ce->name))); } else { - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("o")))); + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_OBJECT)); } zval zobj; ZVAL_OBJ(&zobj, zobject); HashTable *props = zend_get_properties_for(&zobj, ZEND_PROP_PURPOSE_SERIALIZE); if (props) { - php_ion_serialize_struct(ser, props, true); + php_ion_serialize_struct(ser, props, true, true); zend_release_properties(props); } else { zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR, @@ -1099,15 +1545,16 @@ static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_ return ce->__serialize && ser->call_magic; } -static inline bool can_call_iface_serialize(php_ion_serializer *, zend_class_entry *ce) +static inline bool can_call_iface_serialize(php_ion_serializer *ser, zend_class_entry *ce) { - return !!ce->serialize; + (void) ser; + return !!ce->serialize; // NOLINT } static inline bool can_call_custom_serialize(php_ion_serializer *ser, zend_object *zobject, zend_function **fn) { if (ser->call_custom) { - return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); + return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); // NOLINT } return false; } @@ -1148,15 +1595,33 @@ static inline void php_ion_serialize_object(php_ion_serializer *ser, zend_object } } +static inline bool php_ion_serialize_system_value(php_ion_serializer *ser, zval *zv) +{ + if (1 == php_ion_globals.serializer.level) { + if (Z_TYPE_P(zv) == IS_OBJECT) { + if (Z_OBJCE_P(zv) == ce_Symbol_Table_Shared) { + php_ion_symbol_table *obj = php_ion_obj(symbol_table, Z_OBJ_P(zv)); + ION_CHECK_RETURN(true, ion_symbol_table_unload(obj->tab, ser->writer)); + return true; + } + } + } + return false; +} + static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *zv) { + if (php_ion_serialize_system_value(ser, zv)) { + return; + } + zend_ulong idx = (zend_ulong) (uintptr_t) Z_COUNTED_P(zv); ION_STRING is; if (zend_hash_index_exists(ser->ids, idx)) { zval *num = zend_hash_index_find(ser->ids, idx); - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("r")))); + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_BACKREF)); ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(num))); } else { zval num; @@ -1181,7 +1646,7 @@ static inline void php_ion_serialize_refcounted(php_ion_serializer *ser, zval *z break; case IS_REFERENCE: - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("R")))); + ION_CHECK(ion_writer_add_annotation_symbol(ser->writer, &PHP_ION_SYMBOL_REFERENCE)); php_ion_serialize_zval(ser, Z_REFVAL_P(zv)); break; } @@ -1222,6 +1687,37 @@ static inline void php_ion_serialize_zval(php_ion_serializer *ser, zval *zv) php_ion_decl(serializer_php, Serializer_PHP, php_ion_serializer_php_dtor(obj)); +static inline void php_ion_serialize_ex(php_ion_serializer *ser, zval *zv) +{ + HashPosition pos; + HashTable *arr = NULL; + + if (ser->multi_seq) { + if (Z_TYPE_P(zv) != IS_ARRAY || !zend_array_is_list(Z_ARRVAL_P(zv))) { + zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG, + "Expected a packed, consecutively numerically indexed array as argument to the multi sequence serializer"); + return; + } + + arr = Z_ARRVAL_P(zv); + + zend_hash_internal_pointer_reset_ex(arr, &pos); + zv = zend_hash_get_current_data_ex(arr, &pos); + } + + while (zv) { + php_ion_globals_serializer_step(); + php_ion_serialize_zval(ser, zv); + php_ion_globals_serializer_exit(); + + if (!ser->multi_seq) { + break; + } + zend_hash_move_forward_ex(arr, &pos); + zv = zend_hash_get_current_data_ex(arr, &pos); + } +} + void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value) { zend_object *zo_opt = NULL, *zo_ser = NULL; @@ -1241,7 +1737,7 @@ void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value) writer->type = BUFFER_WRITER; if (ser->options) { - zo_opt = writer->opt = create_ion_Writer_Options(NULL); + zo_opt = writer->opt = php_ion_writer_options_new(); php_ion_obj(writer_options, writer->opt)->opt = *ser->options; } @@ -1249,14 +1745,9 @@ void php_ion_serialize(php_ion_serializer *ser, zval *zv, zval *return_value) ser->writer = writer->writer; ser->buffer = &writer->buffer.str; - /* start off with a global PHP annotation instead of repeating it all over the place */ - if (0 == php_ion_globals_serializer_step()) { - ION_STRING is; - ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_from_cstr(&is, ZEND_STRL("PHP"))), - if (zo_ser) OBJ_RELEASE(zo_ser)); + if (!EG(exception)) { + php_ion_serialize_ex(ser, zv); } - php_ion_serialize_zval(ser, zv); - php_ion_globals_serializer_exit(); /* make sure to flush when done, else str.s might not contain everything until the writer is closed */ ion_writer_flush(ser->writer, NULL); @@ -1283,22 +1774,25 @@ static inline void php_ion_unserializer_php_ctor(php_ion_unserializer_php *ser_o ser_obj->unserializer.tmp = global_ser->tmp; ser_obj->unserializer.addref = global_ser->addref; - zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicSerialize"), + zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("multiSequence"), + ser_obj->unserializer.multi_seq); + zend_update_property_bool(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callMagicUnserialize"), ser_obj->unserializer.call_magic); if (ser_obj->unserializer.call_custom) { - zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize"), + zend_update_property_str(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomUnserialize"), ser_obj->unserializer.call_custom); ser_obj->unserializer.call_custom = zend_string_tolower(ser_obj->unserializer.call_custom); } else { - zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomSerialize")); + zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("callCustomUnserialize")); } - if (ser_obj->opt) { - php_ion_reader_options *o_roptions = php_ion_obj(reader_options, ser_obj->opt); - ser_obj->unserializer.options = &o_roptions->opt; - update_property_obj(&ser_obj->std, ZEND_STRL("readerOptions"), ser_obj->opt); + if (!ser_obj->opt) { + ser_obj->opt = php_ion_reader_options_new(); } else { - zend_update_property_null(ser_obj->std.ce, &ser_obj->std, ZEND_STRL("readerOptions")); + GC_ADDREF(ser_obj->opt); } + ser_obj->unserializer.options = &php_ion_obj(reader_options, ser_obj->opt)->opt; + update_property_obj(&ser_obj->std, ZEND_STRL("readerOptions"), ser_obj->opt); + GC_DELREF(ser_obj->opt); } static inline void php_ion_unserializer_php_dtor(php_ion_unserializer_php *obj) @@ -1323,7 +1817,7 @@ static inline bool can_call_iface_unserialize(php_ion_unserializer *ser, zend_cl static inline bool can_call_custom_unserialize(php_ion_unserializer *ser, zend_object *zobject, zend_function **fn) { if (ser->call_custom) { - return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); + return !!(*fn = zend_hash_find_ptr(&zobject->ce->function_table, ser->call_custom)); // NOLINT } return false; } @@ -1438,6 +1932,13 @@ static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_va ION_CHECK(ion_reader_step_out(ser->reader)); } +/** + * @link https://amzn.github.io/ion-docs/docs/spec.html#struct + * When two fields in the same struct have the same name [...] Implementations must preserve all such fields, + * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields + * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to + * nondeterministic behavior. + */ static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *return_value) { zend_hash_next_index_insert(ser->ids, return_value); @@ -1460,8 +1961,19 @@ static inline void php_ion_unserialize_hash(php_ion_unserializer *ser, zval *ret php_ion_unserialize_zval(ser, &zvalue, &typ); ION_CATCH(zend_string_release(key)); - zend_symtable_update(HASH_OF(return_value), key, &zvalue); - + // FIXME:: too naive; b0rked if the previous value is an array + if (zend_symtable_exists(HASH_OF(return_value), key)) { + zval tmp, *prev = zend_hash_find(HASH_OF(return_value), key); + if (Z_TYPE_P(prev) != IS_ARRAY) { + array_init(&tmp); + Z_TRY_ADDREF_P(prev); + zend_hash_next_index_insert(Z_ARRVAL(tmp), prev); + prev = zend_hash_update(HASH_OF(return_value), key, &tmp); + } + zend_hash_next_index_insert(Z_ARRVAL_P(prev), &zvalue); + } else { + zend_symtable_update(HASH_OF(return_value), key, &zvalue); + } zend_string_release(key); } @@ -1543,8 +2055,16 @@ static inline void php_ion_unserialize_struct(php_ion_unserializer *ser, zval *r php_ion_unserialize_object(ser, return_value); } } else if (!ser->annotations.object_type) { + bool is_shared_symtab = ser->annotations.shared_symtab; array_init(return_value); php_ion_unserialize_hash(ser, return_value); + if (is_shared_symtab) { + zval zobj; + object_init_ex(&zobj, ce_Symbol_Table_Shared); + zend_call_known_function(Z_OBJCE(zobj)->constructor, Z_OBJ(zobj), Z_OBJCE(zobj), NULL, 0, NULL, Z_ARRVAL_P(return_value)); + zval_ptr_dtor(return_value); + RETURN_COPY_VALUE(&zobj); + } } else if (ser->annotations.object_type == 'o') { object_init(return_value); php_ion_unserialize_hash(ser, return_value); @@ -1581,9 +2101,9 @@ static inline void php_ion_unserialize_list(php_ion_unserializer *ser, zval *ret static inline void php_ion_reader_read_lob(ION_READER *reader, zval *return_value) { zend_string *zstr = zend_string_alloc(0x1000, 0); -again: +again: ; SIZE read = 0; - iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, zstr->len, &read); + iERR err = ion_reader_read_lob_bytes(reader, (BYTE *) zstr->val, (SIZE) zstr->len, &read); if (err == IERR_BUFFER_TOO_SMALL) { zstr = zend_string_extend(zstr, zstr->len << 2, 0); goto again; @@ -1627,13 +2147,13 @@ static inline void php_ion_reader_read_int(ION_READER *reader, zval *return_valu RETVAL_LONG(i64); goto done; - case IERR_NUMERIC_OVERFLOW: + case IERR_NUMERIC_OVERFLOW: ; SIZE max, len; ION_CHECK(ion_int_char_length(num, &max)); - zend_string *zs = zend_string_alloc(max-1, 0); + zend_string *zs = zend_string_alloc(max, 0); err = ion_int_to_char(num, (BYTE *) zs->val, max, &len); - ZEND_ASSERT(len == zs->len); + zs->val[zs->len = len] = 0; RETVAL_STR(zs); /* fall through */ @@ -1653,7 +2173,7 @@ static inline void php_ion_unserialize_backref(php_ion_unserializer *ser, zval * zend_hash_next_index_insert(ser->addref, return_value); } else { zend_throw_exception_ex(spl_ce_RuntimeException, IERR_INTERNAL_ERROR, - "Could not find back reference %ld", Z_LVAL_P(return_value)); + "Could not find back reference " ZEND_LONG_FMT, Z_LVAL_P(return_value)); } } @@ -1667,7 +2187,10 @@ static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser) ION_STRING ann_str; ION_CHECK(ion_reader_get_an_annotation(ser->reader, i, &ann_str)); - if (ann_str.length > 1) { + if (ann_str.length != 1) { + if (ION_STRING_EQUALS(&ann_str, &ION_SYMBOL_SHARED_SYMBOL_TABLE_STRING)) { + ser->annotations.shared_symtab = true; + } continue; } @@ -1748,7 +2271,7 @@ static inline void php_ion_unserialize_annotations(php_ion_unserializer *ser) static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *return_value, ION_TYPE *typ) { if (typ) { - memcpy(&ser->type, typ, sizeof(ser->type)); + memcpy(&ser->type, typ, sizeof(ser->type)); // NOLINT } else { typ = &ser->type; ION_CHECK(ion_reader_next(ser->reader, typ)); @@ -1763,12 +2286,20 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret ZVAL_DEREF(return_value); } + if (ION_TYPE_INT(*typ) > 0) { + BOOL is_null; + ION_CHECK(ion_reader_is_null(ser->reader, &is_null)); + if (is_null) { + RETURN_NULL(); + } + } + switch (ION_TYPE_INT(*typ)) { case tid_NULL_INT: ION_CHECK(ion_reader_read_null(ser->reader, typ)); RETURN_NULL(); - case tid_BOOL_INT: + case tid_BOOL_INT: ; BOOL bval; ION_CHECK(ion_reader_read_bool(ser->reader, &bval)); RETURN_BOOL(bval); @@ -1790,7 +2321,7 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret } return; - case tid_FLOAT_INT: + case tid_FLOAT_INT: ; double d; ION_CHECK(ion_reader_read_double(ser->reader, &d)); RETURN_DOUBLE(d); @@ -1808,7 +2339,7 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret zend_hash_next_index_insert(ser->ids, return_value); return; - case tid_SYMBOL_INT: + case tid_SYMBOL_INT: ; ION_SYMBOL sym; ION_CHECK(ion_reader_read_ion_symbol(ser->reader, &sym)); php_ion_symbol_zval(&sym, return_value); @@ -1819,7 +2350,7 @@ static inline void php_ion_unserialize_zval(php_ion_unserializer *ser, zval *ret zend_hash_next_index_insert(ser->ids, return_value); return; - case tid_STRING_INT: + case tid_STRING_INT: ; ION_STRING str; ION_CHECK(ion_reader_read_string(ser->reader, &str)); RETVAL_STRINGL((char *) str.value, str.length); @@ -1866,6 +2397,28 @@ unserialize_struct: ; php_ion_decl(unserializer_php, Unserializer_PHP, php_ion_unserializer_php_dtor(obj)); +static inline void php_ion_unserialize_ex(php_ion_unserializer *ser, zval *return_value) +{ + if (ser->multi_seq) { + array_init(return_value); + } + + do { + zval tmp; + ZVAL_NULL(&tmp); + php_ion_globals_unserializer_step(); + php_ion_unserialize_zval(ser, &tmp, NULL); + php_ion_globals_unserializer_exit(); + ION_CATCH(zval_ptr_dtor(&tmp)); + + if (!ser->multi_seq) { + RETURN_COPY_VALUE(&tmp); + } else if (ser->type != tid_EOF) { + zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); + } + } while (ser->type != tid_EOF); +} + void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_value) { zend_object *zo_opt = NULL, *zo_ser = NULL; @@ -1883,36 +2436,37 @@ void php_ion_unserialize(php_ion_unserializer *ser, zval *zdata, zval *return_va zend_object *zo_reader; php_ion_reader *reader; ZVAL_DEREF(zdata); - switch (Z_TYPE_P(zdata)) { - case IS_STRING: - zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader); - reader = php_ion_obj(reader, zo_reader); - reader->type = BUFFER_READER; - reader->buffer = zend_string_copy(Z_STR_P(zdata)); - break; - case IS_RESOURCE: + if (Z_TYPE_P(zdata) == IS_RESOURCE) { zo_reader = create_ion_Reader_Reader(ce_Reader_Stream_Reader); reader = php_ion_obj(reader, zo_reader); reader->type = STREAM_READER; php_stream_from_zval_no_verify(reader->stream.ptr, zdata); - break; - - default: - ZEND_ASSERT(!IS_STRING && !IS_RESOURCE); + } else if (Z_TYPE_P(zdata) <= IS_STRING) { + zo_reader = create_ion_Reader_Reader(ce_Reader_Buffer_Reader); + reader = php_ion_obj(reader, zo_reader); + reader->type = BUFFER_READER; + reader->buffer = zval_get_string(zdata); + } else { + zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG, + "Invalid source to unserialize; expected string or resource"); + if (zo_ser) { + OBJ_RELEASE(zo_ser); + } + return; } if (ser->options) { - zo_opt = reader->opt = create_ion_Reader_Options(NULL); + zo_opt = reader->opt = php_ion_reader_options_new(); php_ion_obj(reader_options, reader->opt)->opt = *ser->options; } php_ion_reader_ctor(reader); ser->reader = reader->reader; - php_ion_globals_unserializer_step(); - php_ion_unserialize_zval(ser, return_value, NULL); - php_ion_globals_unserializer_exit(); + if (!EG(exception)) { + php_ion_unserialize_ex(ser, return_value); + } OBJ_RELEASE(zo_reader); if (zo_opt) {