Skip to content

Commit 4506396

Browse files
committedMar 25, 2021
fixed #9 support check buffer
1 parent cbfb538 commit 4506396

File tree

6 files changed

+94
-24
lines changed

6 files changed

+94
-24
lines changed
 

‎README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Name | Description
8989

9090
## Usage
9191

92-
**Most common usage** :
92+
**Most common usage :**
9393
```php
9494
<?php
9595
if ( ($buf = filemagic ('modules/magic.so')) != false )
@@ -100,7 +100,7 @@ Resutl:
100100
ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0b1c92efa1398676c226544835a64d0edd68f491, not stripped
101101
```
102102

103-
**Execute with custom MAGIC file** :
103+
**Execute with custom MAGIC file :**
104104
```php
105105
<?php
106106
if ( ($buf = filemagic ('modules/magic.so', '/usr/share/misc/magic.mgc')) != false )
@@ -111,7 +111,7 @@ Result:
111111
ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0b1c92efa1398676c226544835a64d0edd68f491, not stripped
112112
```
113113

114-
**Get only file mime type** :
114+
**Get only file mime type :**
115115
```php
116116
if ( ($buf = filemagic ('modules/magic.so', MAGIC_MIME_ENCODING)) != false )
117117
echo "$buf\n";
@@ -121,7 +121,7 @@ Result:
121121
binary
122122
```
123123

124-
**Get only file mime type with custom MAGIC file** :
124+
**Get only file mime type with custom MAGIC file :**
125125
```php
126126
if ( ($buf = filemagic ('modules/magic.so', MAGIC_MIME, '/usr/share/misc/magic.mgc')) != false )
127127
echo "$buf\n";
@@ -131,7 +131,7 @@ Result:
131131
application/x-sharedlib; charset=binary
132132
```
133133

134-
**Can enable to swap 2th and 3th argument each other** :
134+
**Can enable to swap 2th and 3th argument each other :**
135135
```php
136136
if ( ($buf = filemagic ('modules/magic.so', MAGIC_MIME, '/usr/share/misc/magic.mgc')) != false )
137137
echo "$buf\n";
@@ -141,6 +141,19 @@ Resut:
141141
application/x-sharedlib; charset=binary
142142
```
143143

144+
**filemagic buffer control :**
145+
```php
146+
$buf = file_get_contents ('./php_magic.h');
147+
if ( ($buf = filemagic ('DATA:' . $fbuf, MAGIC_MIME) != false ) {
148+
echo $buf . "\n";
149+
}
150+
151+
Result:
152+
text/x-c; charset=us-ascii
153+
```
154+
- if want to get file magic information with buffer data instead of file, use 'DATA:' prefix.
155+
156+
144157
**filemagic function error control :**
145158
```php
146159
<?php

‎magic.c

+24-10
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,18 @@ static void magic_set_error (int type, const char * format, ...) {
249249
ZEND_FUNCTION(filemagic) {
250250
zval * zflag;
251251
zval * zpath; // path of Magic file
252-
zend_string * path = NULL;
253-
char * mpath = NULL;
254-
const char * type;
255-
int flags = 0,
256-
flag = 0,
252+
zend_string * path = NULL;
253+
char * mpath = NULL;
254+
const char * buf;
255+
int type = MAGIC_FILE_SET,
256+
flags = 0,
257+
flag = 0,
257258
action = 0, // FILE_LOAD
258-
fargs = ZEND_NUM_ARGS ();
259+
fargs = ZEND_NUM_ARGS ();
259260

260261
struct stat filestat;
261262
struct magic_set * mp = NULL;
262263

263-
264264
HashTable * args_arr = NULL;
265265
HashPosition pos;
266266

@@ -279,7 +279,16 @@ ZEND_FUNCTION(filemagic) {
279279
RETURN_FALSE;
280280
}
281281

282-
if ( stat (ZSTR_VAL (path), &filestat) != 0 ) {
282+
if ( strncmp (ZSTR_VAL (path), "DATA:", 5) == 0 ) {
283+
type = MAGIC_DATA_SET;
284+
285+
if ( (ZSTR_LEN (path) - 5) < 1 ) {
286+
magic_set_error (E_WARNING, "The value of 1st argument was empty.");
287+
RETURN_FALSE;
288+
}
289+
}
290+
291+
if ( type == MAGIC_FILE_SET && stat (ZSTR_VAL (path), &filestat) != 0 ) {
283292
magic_set_error (E_WARNING, "%s file not found.", ZSTR_VAL (path));
284293
RETURN_FALSE;
285294
}
@@ -328,13 +337,18 @@ ZEND_FUNCTION(filemagic) {
328337
RETURN_FALSE;
329338
}
330339

331-
if ( (type = magic_file (mp, ZSTR_VAL (path))) == NULL ) {
340+
if ( type == MAGIC_FILE_SET )
341+
buf = magic_file (mp, ZSTR_VAL (path));
342+
else
343+
buf = magic_buffer (mp, ZSTR_VAL (path) + 5, ZSTR_LEN (path) - 5);
344+
345+
if ( buf == NULL ) {
332346
magic_set_error (E_WARNING, magic_error(mp));
333347
magic_close (mp);
334348
RETURN_FALSE;
335349
}
336350

337-
RETVAL_STRING (type);
351+
RETVAL_STRING (buf);
338352
magic_close (mp);
339353
}
340354
/* }}} */

‎package.xml.tmpl

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
- #5 enhanced Warning messges
3030
- #6 change package name to mod_magic from mod_filebin
3131
- #7 improved usage documents
32+
- #8 change return type of filemagic function
33+
- #9 suport check buffer
3234
</notes>
3335
<contents>
3436
<dir name="/">
@@ -71,6 +73,8 @@
7173
- #5 enhanced Warning messges
7274
- #6 change package name to mod_magic from mod_filebin
7375
- #7 improved usage documents
76+
- #8 change return type of filemagic function
77+
- #9 suport check buffer
7478
</notes>
7579
</release>
7680
<release>

‎php_magic.h

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ ZEND_FUNCTION(filemagic);
8989
#endif
9090
*/
9191

92+
#define MAGIC_FILE_SET 0
93+
#define MAGIC_DATA_SET 1
94+
9295
#endif /* _MOD_MAGIC_H */
9396

9497
/*

‎sample.php

+25-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* execute with filemagic ('modules/magic.so');
1616
*
17-
17+
1818
EOF;
1919

2020
echo filemagic ('modules/magic.so') . "\n";
@@ -24,7 +24,7 @@
2424
*
2525
* execute with filemagic ('modules/magic.so', '/usr/share/misc/magic.mgc');
2626
*
27-
27+
2828
EOF;
2929

3030
echo filemagic ('modules/magic.so', '/usr/share/misc/magic.mgc') . "\n";
@@ -35,7 +35,7 @@
3535
*
3636
* execute with filemagic ('modules/magic.so', MAGIC_MIME_ENCODING);
3737
*
38-
38+
3939
EOF;
4040

4141
echo filemagic ('modules/magic.so', MAGIC_MIME_ENCODING) . "\n";
@@ -45,7 +45,7 @@
4545
*
4646
* execute with filemagic ('modules/magic.so', MAGIC_MIME, '/usr/share/misc/magic.mgc');
4747
*
48-
48+
4949
EOF;
5050

5151
echo filemagic ('modules/magic.so', MAGIC_MIME, '/usr/share/misc/magic.mgc') . "\n";
@@ -55,28 +55,44 @@
5555
*
5656
* execute with filemagic ('modules/magic.so', '/usr/share/misc/magic.mgc', MAGIC_MIME);
5757
*
58-
58+
5959
EOF;
6060

6161
echo filemagic ('modules/magic.so', '/usr/share/misc/magic.mgc', MAGIC_MIME) . "\n";
6262

63+
echo <<<EOF
64+
65+
*
66+
* \$buf = file_get_contents ('modules/magic.so');
67+
* execute with filemagic ('DATA:' . \$buf, MAGIC_MIME);
68+
*
69+
70+
EOF;
71+
72+
$buf = file_get_contents ('modules/magic.so');
73+
echo filemagic ('DATA:' . $buf, '/usr/share/misc/magic.mgc', MAGIC_MIME) . "\n";
74+
6375
echo <<<EOF
6476
6577
*
6678
* error controls:
6779
*
6880
* if ( filemagic ('wrong_path', MAGIC_MIME) == false ) {
69-
* ini_set ('track_errors', true);
70-
* echo "## php_errormsg : " . \$php_errormsg . "\\n";
81+
* if ( version_compare (PHP_VERSION, '7.2.0', "<" ) ) {
82+
* ini_set ('track_errors', true);
83+
* echo "## php_errormsg : " . \$php_errormsg . "\\n";
84+
* }
7185
* print_r (error_get_last ());
7286
* }
7387
*
7488
7589
EOF;
7690

7791
if ( filemagic ('wrong_path', MAGIC_MIME) == false ) {
78-
ini_set ('track_errors', true);
79-
echo "## php_errormsg : " . $php_errormsg . "\n";
92+
if ( version_compare (PHP_VERSION, '7.2.0', "<" ) ) {
93+
ini_set ('track_errors', true);
94+
echo "## php_errormsg : " . $php_errormsg . "\n";
95+
}
8096
print_r (error_get_last ());
8197
}
8298
?>

‎tests/009-data-control.phpt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Check for data control
3+
--SKIPIF--
4+
<?php
5+
if ( ! extension_loaded ('magic') ) {
6+
print 'skip';
7+
}
8+
?>
9+
--POST--
10+
--GET--
11+
--INI--
12+
--FILE--
13+
<?php
14+
$tmp = file_get_contents ('modules/magic.so');
15+
if ( ($buf = filemagic ('DATA:'. $tmp, MAGIC_MIME)) != false ) {
16+
echo "$buf\n";
17+
}
18+
?>
19+
--EXPECT--
20+
application/x-sharedlib; charset=binary

0 commit comments

Comments
 (0)