*ce_Collection,
*ce_Decimal,
*ce_Decimal_Context,
+ *ce_LOB,
*ce_Reader,
*ce_Reader_Options,
*ce_Reader_Reader,
PTR_CHECK(*((void **)obj)); \
} 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;
-}
-
static inline ION_STRING *ion_string_from_zend(ION_STRING *is, const zend_string *zs)
{
is->length = zs ? zs->len : 0;
}
php_ion_symbol_ctor(sym);
+
+ if (!ION_SYMBOL_IMPORT_LOCATION_IS_NULL(sym_ptr)) {
+ GC_DELREF(sym->iloc);
+ }
}
php_ion_decl(symbol, Symbol, php_ion_symbol_dtor(obj));
ion_int_free(ii);
}
+static inline bool php_ion_decimal_fits_zend_long(php_ion_decimal *obj)
+{
+ int32_t result;
+
+ if (!ion_decimal_is_integer(&obj->dec)) {
+ return false;
+ }
+
+ result = 1;
+ ion_decimal_compare(&obj->dec, &g_ion_dec_zend_max, &g_dec_ctx, &result);
+ if (result == 1) {
+ return false;
+ }
+ result = -1;
+ ion_decimal_compare(&obj->dec, &g_ion_dec_zend_min, &g_dec_ctx, &result);
+ if (result == -1) {
+ return false;
+ }
+ return true;
+}
+
static inline void php_ion_decimal_ctor(php_ion_decimal *obj)
{
if (!obj->ctx) {
}
update_property_obj(&obj->std, ZEND_STRL("context"), obj->ctx);
- if (ion_decimal_is_integer(&obj->dec)) {
+ 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);
zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("number"), l);
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)));
+ ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("p"))));
+ 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_CHECK(ion_writer_write_field_name(ser->writer, ion_string_assign_cstr(&is, (char *) prop_name, prop_len)));
} else {
char buf[MAX_LENGTH_OF_LONG + 1], *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_CHECK(ion_writer_write_field_name(ser->writer, ion_string_assign_cstr(&is, ptr, end - ptr)));
}
php_ion_serialize_zval(ser, v);
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(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("S"))));
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_string(ser->writer, ion_string_assign_cstr(&is, (char *) buf, len)));
efree(buf);
} else if (!EG(exception)){
zend_throw_exception_ex(spl_ce_UnexpectedValueException, IERR_INTERNAL_ERROR,
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(ser->writer, ion_string_assign_cstr(&is, fn ? "C" : "O", 1)));
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);
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(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("E"))));
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);
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(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("c"))));
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(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("o"))));
}
zval zobj;
}
}
+static inline void php_ion_serialize_object_lob(php_ion_serializer *ser, zend_object *zobject)
+{
+ zval tmp_type, *type = zend_read_property(NULL, zobject, ZEND_STRL("type"), 0, &tmp_type);
+ zval tmp_value, *value = zend_read_property(NULL, zobject, ZEND_STRL("value"), 0, &tmp_value);
+ switch (Z_LVAL_P(zend_enum_fetch_case_value(Z_OBJ_P(type)))) {
+ case tid_BLOB_INT:
+ ION_CHECK(ion_writer_write_blob(ser->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
+ break;
+ case tid_CLOB_INT:
+ ION_CHECK(ion_writer_write_clob(ser->writer, (BYTE *) Z_STRVAL_P(value), Z_STRLEN_P(value)));
+ break;
+ default:
+ zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
+ "Unsupported LOB type: ion\\Type::%s", Z_STRVAL_P(zend_enum_fetch_case_name(Z_OBJ_P(type))));
+ break;
+ }
+}
+
static inline bool can_call_magic_serialize(php_ion_serializer *ser, zend_class_entry *ce)
{
return ce->__serialize && ser->call_magic;
php_ion_timestamp *pts = php_ion_obj(timestamp, zobject);
decContext *ctx = ser->options ? ser->options->decimal_context : NULL;
ION_CHECK(ion_writer_write_timestamp(ser->writer, ion_timestamp_from_php(&its, pts, ctx)));
+ } else if (ce == ce_LOB) {
+ php_ion_serialize_object_lob(ser, zobject);
} else {
php_ion_serialize_object_std(ser, zobject);
}
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(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("r"))));
ION_CHECK(ion_writer_write_int64(ser->writer, Z_LVAL_P(num)));
} else {
zval num;
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(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("R"))));
php_ion_serialize_zval(ser, Z_REFVAL_P(zv));
break;
}
/* 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"))),
+ ION_CHECK(ion_writer_add_annotation(ser->writer, ion_string_assign_cstr(&is, ZEND_STRL("PHP"))),
if (zo_ser) OBJ_RELEASE(zo_ser));
}
php_ion_serialize_zval(ser, zv);
}
}
+static inline void php_ion_unserialize_field_name(php_ion_unserializer *ser, zend_string **key)
+{
+ // FIXME: symbol table
+ ION_STRING name;
+ ION_CHECK(ion_reader_get_field_name(ser->reader, &name));
+ if (!name.length) {
+ ION_SYMBOL *is_ptr;
+ ION_CHECK(ion_reader_get_field_name_symbol(ser->reader, &is_ptr));
+ if (!ION_SYMBOL_IS_NULL(is_ptr) && is_ptr->value.length) {
+ name = is_ptr->value;
+ } else if (is_ptr) {
+ char buf[MAX_LENGTH_OF_LONG + 1 + 1] = {0}, *end = buf + sizeof(buf) - 1, *ptr;
+ ptr = zend_print_long_to_buf(end, is_ptr->sid);
+ *--ptr = '$';
+ *key = zend_string_init(ptr, end - ptr, 0);
+ return;
+ }
+ }
+ *key = zend_string_from_ion(&name);
+}
+
static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_value)
{
zend_hash_next_index_insert(ser->ids, return_value);
break;
}
- ION_STRING is;
- ION_CHECK(ion_reader_get_field_name(ser->reader, &is));
- zend_string *key = zend_string_from_ion(&is);
+ zend_string *key;
+ php_ion_unserialize_field_name(ser, &key);
+ ION_CATCH();
zval zvalue;
php_ion_unserialize_zval(ser, &zvalue, &typ);
ION_TYPE typ;
ION_CHECK(ion_reader_next(ser->reader, &typ));
- ION_STRING name;
- ION_CHECK(ion_reader_get_field_name(ser->reader, &name));
- zend_string *key = zend_string_from_ion(&name);
+ if (typ == tid_EOF) {
+ break;
+ }
+
+ zend_string *key;
+ php_ion_unserialize_field_name(ser, &key);
+ ION_CATCH();
zval zvalue;
php_ion_unserialize_zval(ser, &zvalue, &typ);
ION_CATCH(zend_string_release(key));
- if (typ == tid_EOF) {
- zend_string_release(key);
- break;
- }
-
zend_symtable_update(HASH_OF(return_value), key, &zvalue);
zend_string_release(key);
ION_TYPE typ;
ION_CHECK(ion_reader_next(ser->reader, &typ));
- zval next;
- php_ion_unserialize_zval(ser, &next, &typ);
- ION_CATCH();
-
if (typ == tid_EOF) {
break;
}
+ zval next;
+ php_ion_unserialize_zval(ser, &next, &typ);
+ ION_CATCH();
+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &next);
}
}
ION_CHECK(err, zend_string_release(zstr));
if (zstr->len > read) {
+ zstr->val[read] = 0;
zstr = zend_string_truncate(zstr, read, 0);
}
RETURN_STR(zstr);
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 */
ION_STRING ann_str;
ION_CHECK(ion_reader_get_an_annotation(ser->reader, i, &ann_str));
+ if (ann_str.length != 1) {
+ continue;
+ }
+
switch (*ann_str.value) {
case 'R':
if (ser->annotations.makeref) {
ZVAL_DEREF(return_value);
}
- BOOL bval;
- ION_CHECK(ion_reader_is_null(ser->reader, &bval));
- if (bval) {
- goto read_null;
+ 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:
-read_null: ;
ION_CHECK(ion_reader_read_null(ser->reader, typ));
RETURN_NULL();
case tid_BOOL_INT:
+ BOOL bval;
ION_CHECK(ion_reader_read_bool(ser->reader, &bval));
RETURN_BOOL(bval);
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) {
php_ion_reader_ctor(reader);
ser->reader = reader->reader;
+ ION_CATCH();
php_ion_globals_unserializer_step();
php_ion_unserialize_zval(ser, return_value, NULL);