36
36
#include "fopen_wrappers.h" /* needed for is_url */
37
37
#include "Zend/zend_exceptions.h"
38
38
39
- /* {{{ macros and type definitions */
40
- typedef struct _php_fileinfo {
41
- zend_long options ;
42
- struct magic_set * magic ;
43
- } php_fileinfo ;
44
-
45
39
static zend_object_handlers finfo_object_handlers ;
46
40
zend_class_entry * finfo_class_entry ;
47
41
48
42
typedef struct _finfo_object {
49
- php_fileinfo * ptr ;
43
+ zend_long options ;
44
+ struct magic_set * magic ;
50
45
zend_object zo ;
51
46
} finfo_object ;
52
47
@@ -56,26 +51,12 @@ static inline finfo_object *php_finfo_fetch_object(zend_object *obj) {
56
51
57
52
#define Z_FINFO_P (zv ) php_finfo_fetch_object(Z_OBJ_P((zv)))
58
53
59
- #define FILEINFO_FROM_OBJECT (finfo , object ) \
60
- { \
61
- finfo_object *obj = Z_FINFO_P(object); \
62
- finfo = obj->ptr; \
63
- if (!finfo) { \
64
- zend_throw_error(NULL, "Invalid finfo object"); \
65
- RETURN_THROWS(); \
66
- } \
67
- }
68
-
69
54
/* {{{ finfo_objects_free */
70
55
static void finfo_objects_free (zend_object * object )
71
56
{
72
57
finfo_object * intern = php_finfo_fetch_object (object );
73
58
74
- if (intern -> ptr ) {
75
- magic_close (intern -> ptr -> magic );
76
- efree (intern -> ptr );
77
- }
78
-
59
+ magic_close (intern -> magic );
79
60
zend_object_std_dtor (& intern -> zo );
80
61
}
81
62
/* }}} */
@@ -153,7 +134,6 @@ PHP_FUNCTION(finfo_open)
153
134
zend_long options = MAGIC_NONE ;
154
135
char * file = NULL ;
155
136
size_t file_len = 0 ;
156
- php_fileinfo * finfo ;
157
137
zval * object = getThis ();
158
138
char resolved_path [MAXPATHLEN ];
159
139
zend_error_handling zeh ;
@@ -163,15 +143,10 @@ PHP_FUNCTION(finfo_open)
163
143
}
164
144
165
145
if (object ) {
166
- finfo_object * finfo_obj = Z_FINFO_P (object );
167
-
168
146
zend_replace_error_handling (EH_THROW , NULL , & zeh );
169
147
170
- if (finfo_obj -> ptr ) {
171
- magic_close (finfo_obj -> ptr -> magic );
172
- efree (finfo_obj -> ptr );
173
- finfo_obj -> ptr = NULL ;
174
- }
148
+ magic_close (Z_FINFO_P (object )-> magic );
149
+ Z_FINFO_P (object )-> magic = NULL ;
175
150
}
176
151
177
152
if (file_len == 0 ) {
@@ -199,13 +174,9 @@ PHP_FUNCTION(finfo_open)
199
174
file = resolved_path ;
200
175
}
201
176
202
- finfo = emalloc (sizeof (php_fileinfo ));
203
-
204
- finfo -> options = options ;
205
- finfo -> magic = magic_open (options );
177
+ struct magic_set * magic = magic_open (options );
206
178
207
- if (finfo -> magic == NULL ) {
208
- efree (finfo );
179
+ if (magic == NULL ) {
209
180
php_error_docref (NULL , E_WARNING , "Invalid mode '" ZEND_LONG_FMT "'." , options );
210
181
if (object ) {
211
182
zend_restore_error_handling (& zeh );
@@ -216,10 +187,9 @@ PHP_FUNCTION(finfo_open)
216
187
RETURN_FALSE ;
217
188
}
218
189
219
- if (magic_load (finfo -> magic , file ) == -1 ) {
190
+ if (magic_load (magic , file ) == -1 ) {
220
191
php_error_docref (NULL , E_WARNING , "Failed to load magic database at \"%s\"" , file );
221
- magic_close (finfo -> magic );
222
- efree (finfo );
192
+ magic_close (magic );
223
193
if (object ) {
224
194
zend_restore_error_handling (& zeh );
225
195
if (!EG (exception )) {
@@ -230,14 +200,15 @@ PHP_FUNCTION(finfo_open)
230
200
}
231
201
232
202
if (object ) {
233
- finfo_object * obj ;
234
203
zend_restore_error_handling (& zeh );
235
- obj = Z_FINFO_P (object );
236
- obj -> ptr = finfo ;
204
+ finfo_object * obj = Z_FINFO_P (object );
205
+ obj -> magic = magic ;
206
+ obj -> options = options ;
237
207
} else {
238
208
zend_object * zobj = finfo_objects_new (finfo_class_entry );
239
209
finfo_object * obj = php_finfo_fetch_object (zobj );
240
- obj -> ptr = finfo ;
210
+ obj -> magic = magic ;
211
+ obj -> options = options ;
241
212
RETURN_OBJ (zobj );
242
213
}
243
214
}
@@ -260,18 +231,16 @@ PHP_FUNCTION(finfo_close)
260
231
PHP_FUNCTION (finfo_set_flags )
261
232
{
262
233
zend_long options ;
263
- php_fileinfo * finfo ;
264
234
zval * self ;
265
235
266
236
if (zend_parse_method_parameters (ZEND_NUM_ARGS (), getThis (), "Ol" , & self , finfo_class_entry , & options ) == FAILURE ) {
267
237
RETURN_THROWS ();
268
238
}
269
- FILEINFO_FROM_OBJECT (finfo , self );
270
239
271
240
/* We do not check the return value as it can only ever fail if options contains MAGIC_PRESERVE_ATIME
272
241
* and the system neither has utime(3) nor utimes(2). Something incredibly unlikely. */
273
- magic_setflags (finfo -> magic , options );
274
- finfo -> options = options ;
242
+ magic_setflags (Z_FINFO_P ( self ) -> magic , options );
243
+ Z_FINFO_P ( self ) -> options = options ;
275
244
276
245
RETURN_TRUE ;
277
246
}
@@ -331,13 +300,12 @@ PHP_FUNCTION(finfo_file)
331
300
zend_string * path = NULL ;
332
301
zend_long options = 0 ;
333
302
zval * zcontext = NULL ;
334
- php_fileinfo * finfo = NULL ;
335
303
336
304
if (zend_parse_method_parameters (ZEND_NUM_ARGS (), getThis (), "OP|lr!" , & self , finfo_class_entry , & path , & options , & zcontext ) == FAILURE ) {
337
305
RETURN_THROWS ();
338
306
}
339
- FILEINFO_FROM_OBJECT ( finfo , self );
340
- struct magic_set * magic = finfo -> magic ;
307
+
308
+ struct magic_set * magic = Z_FINFO_P ( self ) -> magic ;
341
309
342
310
if (UNEXPECTED (ZSTR_LEN (path ) == 0 )) {
343
311
zend_argument_must_not_be_empty_error (2 );
@@ -358,7 +326,7 @@ PHP_FUNCTION(finfo_file)
358
326
const char * ret_val = php_fileinfo_from_path (magic , path , context );
359
327
/* Restore options */
360
328
if (options ) {
361
- magic_setflags (magic , finfo -> options );
329
+ magic_setflags (magic , Z_FINFO_P ( self ) -> options );
362
330
}
363
331
364
332
if (UNEXPECTED (ret_val == NULL )) {
@@ -375,13 +343,12 @@ PHP_FUNCTION(finfo_buffer)
375
343
zend_string * buffer = NULL ;
376
344
zend_long options = 0 ;
377
345
zval * dummy_context = NULL ;
378
- php_fileinfo * finfo = NULL ;
379
346
380
347
if (zend_parse_method_parameters (ZEND_NUM_ARGS (), getThis (), "OS|lr!" , & self , finfo_class_entry , & buffer , & options , & dummy_context ) == FAILURE ) {
381
348
RETURN_THROWS ();
382
349
}
383
- FILEINFO_FROM_OBJECT ( finfo , self );
384
- struct magic_set * magic = finfo -> magic ;
350
+
351
+ struct magic_set * magic = Z_FINFO_P ( self ) -> magic ;
385
352
386
353
/* Set options for the current file/buffer. */
387
354
if (options ) {
@@ -392,7 +359,7 @@ PHP_FUNCTION(finfo_buffer)
392
359
393
360
/* Restore options */
394
361
if (options ) {
395
- magic_setflags (magic , finfo -> options );
362
+ magic_setflags (magic , Z_FINFO_P ( self ) -> options );
396
363
}
397
364
398
365
if (UNEXPECTED (ret_val == NULL )) {
0 commit comments