diff options
author | Xinchen Hui <laruence@php.net> | 2014-05-07 13:50:29 +0800 |
---|---|---|
committer | Xinchen Hui <laruence@php.net> | 2014-05-07 13:50:29 +0800 |
commit | 9ef21b7606f00a879f15e236c9c1cdf0fa685e31 (patch) | |
tree | 2882b99453e871b4a807770fbb8b02f56b920c17 /ext/fileinfo/fileinfo.c | |
parent | 478a8b2ebbd6a5fd2e49c3e1069b284b1a38c018 (diff) | |
download | php-git-9ef21b7606f00a879f15e236c9c1cdf0fa685e31.tar.gz |
Refactor fileinfo
Diffstat (limited to 'ext/fileinfo/fileinfo.c')
-rw-r--r-- | ext/fileinfo/fileinfo.c | 89 |
1 files changed, 44 insertions, 45 deletions
diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index 2f61944ad7..86cd02a613 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -43,32 +43,38 @@ #endif /* {{{ macros and type definitions */ -struct php_fileinfo { +typedef struct _php_fileinfo { long options; struct magic_set *magic; -}; +} php_fileinfo; static zend_object_handlers finfo_object_handlers; zend_class_entry *finfo_class_entry; -struct finfo_object { +typedef struct _finfo_object { + php_fileinfo *ptr; zend_object zo; - struct php_fileinfo *ptr; -}; +} finfo_object; #define FILEINFO_DECLARE_INIT_OBJECT(object) \ zval *object = getThis(); +static inline finfo_object *php_finfo_fetch_object(zend_object *obj) { + return (finfo_object *)((char*)(obj) - XtOffsetOf(finfo_object, zo)); +} + +#define Z_FINFO_P(zv) php_finfo_fetch_object(Z_OBJ_P((zv))) + #define FILEINFO_REGISTER_OBJECT(_object, _ptr) \ { \ - struct finfo_object *obj; \ - obj = (struct finfo_object*)zend_object_store_get_object(_object TSRMLS_CC); \ - obj->ptr = _ptr; \ + finfo_object *obj; \ + obj = Z_FINFO_P(_object); \ + obj->ptr = _ptr; \ } #define FILEINFO_FROM_OBJECT(finfo, object) \ { \ - struct finfo_object *obj = zend_object_store_get_object(object TSRMLS_CC); \ + finfo_object *obj = Z_FINFO_P(object); \ finfo = obj->ptr; \ if (!finfo) { \ php_error_docref(NULL TSRMLS_CC, E_WARNING, "The invalid fileinfo object."); \ @@ -78,9 +84,9 @@ struct finfo_object { /* {{{ finfo_objects_free */ -static void finfo_objects_free(void *object TSRMLS_DC) +static void finfo_objects_free(zend_object *object TSRMLS_DC) { - struct finfo_object *intern = (struct finfo_object *) object; + finfo_object *intern = php_finfo_fetch_object(object); if (intern->ptr) { magic_close(intern->ptr->magic); @@ -88,30 +94,22 @@ static void finfo_objects_free(void *object TSRMLS_DC) } zend_object_std_dtor(&intern->zo TSRMLS_CC); - efree(intern); } /* }}} */ /* {{{ finfo_objects_new */ -PHP_FILEINFO_API zend_object_value finfo_objects_new(zend_class_entry *class_type TSRMLS_DC) +PHP_FILEINFO_API zend_object *finfo_objects_new(zend_class_entry *class_type TSRMLS_DC) { - zend_object_value retval; - struct finfo_object *intern; + finfo_object *intern; - intern = emalloc(sizeof(struct finfo_object)); - memset(intern, 0, sizeof(struct finfo_object)); + intern = ecalloc(1, sizeof(finfo_object) + sizeof(zval) * (class_type->default_properties_count - 1)); zend_object_std_init(&intern->zo, class_type TSRMLS_CC); object_properties_init(&intern->zo, class_type); + intern->zo.handlers = &finfo_object_handlers; - intern->ptr = NULL; - - retval.handle = zend_objects_store_put(intern, NULL, - finfo_objects_free, NULL TSRMLS_CC); - retval.handlers = (zend_object_handlers *) &finfo_object_handlers; - - return retval; + return &intern->zo; } /* }}} */ @@ -187,10 +185,10 @@ zend_function_entry finfo_class_functions[] = { static int le_fileinfo; /* }}} */ -void finfo_resource_destructor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ +void finfo_resource_destructor(zend_resource *rsrc TSRMLS_DC) /* {{{ */ { if (rsrc->ptr) { - struct php_fileinfo *finfo = (struct php_fileinfo *) rsrc->ptr; + php_fileinfo *finfo = (php_fileinfo *) rsrc->ptr; magic_close(finfo->magic); efree(rsrc->ptr); rsrc->ptr = NULL; @@ -223,6 +221,8 @@ PHP_MINIT_FUNCTION(finfo) /* copy the standard object handlers to you handler table */ memcpy(&finfo_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + finfo_object_handlers.offset = XtOffsetOf(finfo_object, zo); + finfo_object_handlers.free_obj = finfo_objects_free; le_fileinfo = zend_register_list_destructors_ex(finfo_resource_destructor, NULL, "file_info", module_number); @@ -282,13 +282,12 @@ PHP_MINFO_FUNCTION(fileinfo) } /* }}} */ -#define FILEINFO_DESTROY_OBJECT(object) \ - do { \ - if (object) { \ - zend_object_store_ctor_failed(object TSRMLS_CC); \ - zval_dtor(object); \ - ZVAL_NULL(object); \ - } \ +#define FILEINFO_DESTROY_OBJECT(object) \ + do { \ + if (object) { \ + zend_object_store_ctor_failed(Z_OBJ_P(object) TSRMLS_CC); \ + Z_OBJ_P(object) = NULL; \ + } \ } while (0) /* {{{ proto resource finfo_open([int options [, string arg]]) @@ -298,7 +297,7 @@ PHP_FUNCTION(finfo_open) long options = MAGIC_NONE; char *file = NULL; int file_len = 0; - struct php_fileinfo *finfo; + php_fileinfo *finfo; FILEINFO_DECLARE_INIT_OBJECT(object) char resolved_path[MAXPATHLEN]; @@ -308,7 +307,7 @@ PHP_FUNCTION(finfo_open) } if (object) { - struct finfo_object *finfo_obj = (struct finfo_object*)zend_object_store_get_object(object TSRMLS_CC); + finfo_object *finfo_obj = Z_FINFO_P(object); if (finfo_obj->ptr) { magic_close(finfo_obj->ptr->magic); @@ -332,7 +331,7 @@ PHP_FUNCTION(finfo_open) file = resolved_path; } - finfo = emalloc(sizeof(struct php_fileinfo)); + finfo = emalloc(sizeof(php_fileinfo)); finfo->options = options; finfo->magic = magic_open(options); @@ -364,15 +363,15 @@ PHP_FUNCTION(finfo_open) Close fileinfo resource. */ PHP_FUNCTION(finfo_close) { - struct php_fileinfo *finfo; + php_fileinfo *finfo; zval *zfinfo; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zfinfo) == FAILURE) { RETURN_FALSE; } - ZEND_FETCH_RESOURCE(finfo, struct php_fileinfo *, &zfinfo, -1, "file_info", le_fileinfo); + ZEND_FETCH_RESOURCE(finfo, php_fileinfo *, zfinfo, -1, "file_info", le_fileinfo); - zend_list_delete(Z_RESVAL_P(zfinfo)); + zend_list_close(Z_RES_P(zfinfo)); RETURN_TRUE; } @@ -383,7 +382,7 @@ PHP_FUNCTION(finfo_close) PHP_FUNCTION(finfo_set_flags) { long options; - struct php_fileinfo *finfo; + php_fileinfo *finfo; zval *zfinfo; FILEINFO_DECLARE_INIT_OBJECT(object) @@ -396,7 +395,7 @@ PHP_FUNCTION(finfo_set_flags) if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zfinfo, &options) == FAILURE) { RETURN_FALSE; } - ZEND_FETCH_RESOURCE(finfo, struct php_fileinfo *, &zfinfo, -1, "file_info", le_fileinfo); + ZEND_FETCH_RESOURCE(finfo, php_fileinfo *, zfinfo, -1, "file_info", le_fileinfo); } FINFO_SET_OPTION(finfo->magic, options) @@ -415,7 +414,7 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime long options = 0; char *ret_val = NULL, *buffer = NULL; int buffer_len; - struct php_fileinfo *finfo = NULL; + php_fileinfo *finfo = NULL; zval *zfinfo, *zcontext = NULL; zval *what; char mime_directory[] = "directory"; @@ -461,7 +460,7 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|lr", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) { RETURN_FALSE; } - ZEND_FETCH_RESOURCE(finfo, struct php_fileinfo *, &zfinfo, -1, "file_info", le_fileinfo); + ZEND_FETCH_RESOURCE(finfo, php_fileinfo *, zfinfo, -1, "file_info", le_fileinfo); magic = finfo->magic; } @@ -482,7 +481,7 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime php_stream *stream; off_t streampos; - php_stream_from_zval_no_verify(stream, &what); + php_stream_from_zval_no_verify(stream, what); if (!stream) { goto common; } @@ -554,7 +553,7 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime common: if (ret_val) { - RETVAL_STRING(ret_val, 1); + RETVAL_STRING(ret_val); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); RETVAL_FALSE; |