started to patch for libmagic.next

everything is broken yet
This commit is contained in:
Anatol Belski 2015-03-05 13:50:29 +01:00
parent 332bbfa23b
commit 1b77e51ad7
5 changed files with 30282 additions and 2473 deletions

File diff suppressed because it is too large Load Diff

View File

@ -58,6 +58,9 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.230 2015/01/02 21:29:39 christos Exp $")
#else
#include <unistd.h>
#endif
#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif
#include <string.h>
#include <assert.h>
#include <ctype.h>
@ -438,8 +441,7 @@ apprentice_1(struct magic_set *ms, const char *fn, int action)
for (i = 0; i < MAGIC_SETS; i++) {
if (add_mlist(ms->mlist[i], map, i) == -1) {
file_oomem(ms, sizeof(*ml));
apprentice_unmap(map);
return -1;
goto fail;
}
}
@ -453,6 +455,12 @@ apprentice_1(struct magic_set *ms, const char *fn, int action)
}
}
return 0;
fail:
for (i = 0; i < MAGIC_SETS; i++) {
mlist_free(ms->mlist[i]);
ms->mlist[i] = NULL;
}
return -1;
}
protected void
@ -561,6 +569,7 @@ mlist_free(struct mlist *mlist)
break;
}
}
#endif
/* const char *fn: list of magic files and directories */
protected int
@ -3000,6 +3009,62 @@ check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
return 0;
}
private int
check_buffer(struct magic_set *ms, struct magic_map *map, const char *dbname)
{
uint32_t *ptr;
uint32_t entries, nentries;
uint32_t version;
int i, needsbyteswap;
ptr = CAST(uint32_t *, map->p);
if (*ptr != MAGICNO) {
if (swap4(*ptr) != MAGICNO) {
file_error(ms, 0, "bad magic in `%s'", dbname);
return -1;
}
needsbyteswap = 1;
} else
needsbyteswap = 0;
if (needsbyteswap)
version = swap4(ptr[1]);
else
version = ptr[1];
if (version != VERSIONNO) {
file_error(ms, 0, "File %s supports only version %d magic "
"files. `%s' is version %d", FILE_VERSION_MAJOR,
VERSIONNO, dbname, version);
return -1;
}
entries = (uint32_t)(map->len / sizeof(struct magic));
if ((entries * sizeof(struct magic)) != map->len) {
file_error(ms, 0, "Size of `%s' %" SIZE_T_FORMAT "u is not "
"a multiple of %" SIZE_T_FORMAT "u",
dbname, map->len, sizeof(struct magic));
return -1;
}
map->magic[0] = CAST(struct magic *, map->p) + 1;
nentries = 0;
for (i = 0; i < MAGIC_SETS; i++) {
if (needsbyteswap)
map->nmagic[i] = swap4(ptr[i + 2]);
else
map->nmagic[i] = ptr[i + 2];
if (i != MAGIC_SETS - 1)
map->magic[i + 1] = map->magic[i] + map->nmagic[i];
nentries += map->nmagic[i];
}
if (entries != nentries + 1) {
file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
dbname, entries, nentries + 1);
return -1;
}
if (needsbyteswap)
for (i = 0; i < MAGIC_SETS; i++)
byteswap(map->magic[i], map->nmagic[i]);
return 0;
}
/*
* handle an mmaped file.
*/

View File

@ -107,6 +107,16 @@ file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb, php_stream *
}
#define COMMA (did++ ? ", " : "")
/*
* Fstat is cheaper but fails for files you don't have read perms on.
* On 4.2BSD and similar systems, use lstat() to identify symlinks.
*/
#ifdef S_IFLNK
if ((ms->flags & MAGIC_SYMLINK) == 0)
ret = lstat(fn, sb);
else
#endif
ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
if (stream) {
php_stream_statbuf ssb;

View File

@ -269,6 +269,18 @@ magic_load(struct magic_set *ms, const char *magicfile)
return file_apprentice(ms, magicfile, FILE_LOAD);
}
/*
* Install a set of compiled magic buffers.
*/
public int
magic_load_buffers(struct magic_set *ms, void **bufs, size_t *sizes,
size_t nbufs)
{
if (ms == NULL)
return -1;
return buffer_apprentice(ms, (struct magic **)bufs, sizes, nbufs);
}
public int
magic_compile(struct magic_set *ms, const char *magicfile)
{

View File

@ -629,7 +629,7 @@ mprint(struct magic_set *ms, struct magic *m)
char *cp;
int rval;
cp = estrndup((const char *)ms->search.s, ms->search.rm_len);
cp = strndup((const char *)ms->search.s, ms->search.rm_len);
if (cp == NULL) {
file_oomem(ms, ms->search.rm_len);
return -1;
@ -1642,7 +1642,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
break;
case FILE_REGEX:
if (OFFSET_OOB(nbytes, offset, 0))
if (nbytes < offset)
return 0;
break;
@ -1651,7 +1651,8 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
offset += CAST(uint32_t, o);
if (offset == 0)
return 0;
if (OFFSET_OOB(nbytes, offset, 0))
if (nbytes < offset)
return 0;
if ((pb = file_push_buffer(ms)) == NULL)
@ -1682,7 +1683,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
return rv;
case FILE_USE:
if (OFFSET_OOB(nbytes, offset, 0))
if (nbytes < offset)
return 0;
rbuf = m->value.s;
if (*rbuf == '^') {