- Bundle the data file - you can still use the external file if you

specify that in the call to finfo_open().
This commit is contained in:
Derick Rethans 2008-07-24 13:40:21 +00:00
parent 5b494c0f16
commit 8c8b504bc4
6 changed files with 101899 additions and 6 deletions

View File

@ -0,0 +1,17 @@
/* This is a generated file, do not modify */
/* Usage: php create_data_file.php /path/to/magic.mgc > data_file.c */
<?php
$dta = file_get_contents( $argv[1] );
$dta_l = strlen($dta);
$j = 0;
echo "const unsigned char php_magic_database[$dta_l] = {\n";
for ($i = 0; $i < $dta_l; $i++) {
printf("0x%02X, ", ord($dta[$i]));
if ($j % 16 == 15) {
echo "\n";
}
$j++;
}
echo "};\n";
?>

101727
ext/fileinfo/data_file.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -251,7 +251,7 @@ PHP_MINFO_FUNCTION(fileinfo)
PHP_FUNCTION(finfo_open)
{
long options = MAGIC_NONE;
char *file = PHP_DEFAULT_MAGIC_FILE;
char *file = NULL;
int file_len = 0;
struct php_fileinfo *finfo;
FILEINFO_DECLARE_INIT_OBJECT(object)

115
ext/fileinfo/libmagic.patch Normal file
View File

@ -0,0 +1,115 @@
Index: libmagic/apprentice.c
===================================================================
RCS file: /repository/pecl/fileinfo/libmagic/apprentice.c,v
retrieving revision 1.1
diff -u -r1.1 apprentice.c
--- libmagic/apprentice.c 11 Jul 2008 14:13:50 -0000 1.1
+++ libmagic/apprentice.c 24 Jul 2008 13:34:23 -0000
@@ -157,6 +157,10 @@
}
#endif /* COMPILE_ONLY */
+#ifdef PHP_BUNDLE
+#include "../data_file.c"
+#endif
+
static const struct type_tbl_s {
const char name[16];
const size_t len;
@@ -310,6 +314,11 @@
if (p == NULL)
return;
switch (type) {
+#ifdef PHP_BUNDLE
+ case 3:
+ /* Do nothing, it's part of the code segment */
+ break;
+#endif
#ifdef QUICK
case 2:
p--;
@@ -339,8 +348,20 @@
if (fn == NULL)
fn = getenv("MAGIC");
- if (fn == NULL)
+ if (fn == NULL) {
+#ifdef PHP_BUNDLE
+ if ((mlist = malloc(sizeof(*mlist))) == NULL) {
+ free(mfn);
+ file_oomem(ms, sizeof(*mlist));
+ return NULL;
+ }
+ mlist->next = mlist->prev = mlist;
+ apprentice_1(ms, fn, action, mlist);
+ return mlist;
+#else
fn = MAGIC;
+#endif
+ }
if ((mfn = strdup(fn)) == NULL) {
file_oomem(ms, strlen(fn));
@@ -1886,6 +1907,15 @@
int needsbyteswap;
char *dbname = NULL;
void *mm = NULL;
+ int ret = 0;
+
+#ifdef PHP_BUNDLE
+ if (fn == NULL) {
+ mm = &php_magic_database;
+ ret = 3;
+ goto internal_loaded;
+ }
+#endif
mkdbname(fn, &dbname, 0);
if (dbname == NULL)
@@ -1909,7 +1939,7 @@
file_error(ms, errno, "cannot map `%s'", dbname);
goto error1;
}
-#define RET 2
+ ret = 2;
#else
if ((mm = malloc((size_t)st.st_size)) == NULL) {
file_oomem(ms, (size_t)st.st_size);
@@ -1919,11 +1949,14 @@
file_badread(ms);
goto error1;
}
-#define RET 1
+ ret = 1;
#endif
- *magicp = mm;
(void)close(fd);
fd = -1;
+#ifdef PHP_BUNDLE
+internal_loaded:
+#endif
+ *magicp = mm;
ptr = (uint32_t *)(void *)*magicp;
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
@@ -1950,7 +1983,7 @@
if (needsbyteswap)
byteswap(*magicp, *nmagicp);
free(dbname);
- return RET;
+ return ret;
error1:
if (fd != -1)
Index: libmagic/config.h
===================================================================
RCS file: /repository/pecl/fileinfo/libmagic/config.h,v
retrieving revision 1.2
diff -u -r1.2 config.h
--- libmagic/config.h 11 Jul 2008 14:49:45 -0000 1.2
+++ libmagic/config.h 24 Jul 2008 13:34:23 -0000
@@ -2,3 +2,4 @@
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
+#define PHP_BUNDLE

View File

@ -157,6 +157,10 @@ main(int argc, char *argv[])
}
#endif /* COMPILE_ONLY */
#ifdef PHP_BUNDLE
#include "../data_file.c"
#endif
static const struct type_tbl_s {
const char name[16];
const size_t len;
@ -310,6 +314,11 @@ file_delmagic(struct magic *p, int type, size_t entries)
if (p == NULL)
return;
switch (type) {
#ifdef PHP_BUNDLE
case 3:
/* Do nothing, it's part of the code segment */
break;
#endif
#ifdef QUICK
case 2:
p--;
@ -339,8 +348,20 @@ file_apprentice(struct magic_set *ms, const char *fn, int action)
if (fn == NULL)
fn = getenv("MAGIC");
if (fn == NULL)
if (fn == NULL) {
#ifdef PHP_BUNDLE
if ((mlist = malloc(sizeof(*mlist))) == NULL) {
free(mfn);
file_oomem(ms, sizeof(*mlist));
return NULL;
}
mlist->next = mlist->prev = mlist;
apprentice_1(ms, fn, action, mlist);
return mlist;
#else
fn = MAGIC;
#endif
}
if ((mfn = strdup(fn)) == NULL) {
file_oomem(ms, strlen(fn));
@ -1886,6 +1907,15 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
int needsbyteswap;
char *dbname = NULL;
void *mm = NULL;
int ret = 0;
#ifdef PHP_BUNDLE
if (fn == NULL) {
mm = &php_magic_database;
ret = 3;
goto internal_loaded;
}
#endif
mkdbname(fn, &dbname, 0);
if (dbname == NULL)
@ -1909,7 +1939,7 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
file_error(ms, errno, "cannot map `%s'", dbname);
goto error1;
}
#define RET 2
ret = 2;
#else
if ((mm = malloc((size_t)st.st_size)) == NULL) {
file_oomem(ms, (size_t)st.st_size);
@ -1919,11 +1949,14 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
file_badread(ms);
goto error1;
}
#define RET 1
ret = 1;
#endif
*magicp = mm;
(void)close(fd);
fd = -1;
#ifdef PHP_BUNDLE
internal_loaded:
#endif
*magicp = mm;
ptr = (uint32_t *)(void *)*magicp;
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
@ -1950,7 +1983,7 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
if (needsbyteswap)
byteswap(*magicp, *nmagicp);
free(dbname);
return RET;
return ret;
error1:
if (fd != -1)

View File

@ -2,3 +2,4 @@
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#define PHP_BUNDLE