#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: magic.c,v 1.78 2013/01/07 18:20:19 christos Exp $")
#endif
#include "magic.h"
#include <stdlib.h>
#ifdef PHP_WIN32
#include "win32/unistd.h"
#else
#include <unistd.h>
#endif
#include <string.h>
#ifdef PHP_WIN32
# include "config.w32.h"
#else
# include "php_config.h"
#endif
#ifdef PHP_WIN32
#include <shlwapi.h>
#endif
#include <limits.h>
#if defined(HAVE_UTIMES)
# include <sys/time.h>
#elif defined(HAVE_UTIME)
# if defined(HAVE_SYS_UTIME_H)
# include <sys/utime.h>
# elif defined(HAVE_UTIME_H)
# include <utime.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef PIPE_BUF
#ifdef _PC_PIPE_BUF
#define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
#else
#define PIPE_BUF 512
#endif
#endif
#ifdef PHP_WIN32
# undef S_IFLNK
# undef S_IFIFO
#endif
private void close_and_restore(const struct magic_set *, const char *, int,
const struct stat *);
private int unreadable_info(struct magic_set *, mode_t, const char *);
#if 0
private const char* get_default_magic(void);
#endif
private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#if 0
private const char *
get_default_magic(void)
{
static const char hmagic[] = "/.magic/magic.mgc";
static char *default_magic;
char *home, *hmagicpath;
#ifndef PHP_WIN32
struct stat st;
if (default_magic) {
free(default_magic);
default_magic = NULL;
}
if ((home = getenv("HOME")) == NULL)
return MAGIC;
if (asprintf(&hmagicpath, "%s/.magic.mgc", home) < 0)
return MAGIC;
if (stat(hmagicpath, &st) == -1) {
free(hmagicpath);
if (asprintf(&hmagicpath, "%s/.magic", home) < 0)
return MAGIC;
if (stat(hmagicpath, &st) == -1)
goto out;
if (S_ISDIR(st.st_mode)) {
free(hmagicpath);
if (asprintf(&hmagicpath, "%s/%s", home, hmagic) < 0)
return MAGIC;
if (access(hmagicpath, R_OK) == -1)
goto out;
}
}
if (asprintf(&default_magic, "%s:%s", hmagicpath, MAGIC) < 0)
goto out;
free(hmagicpath);
return default_magic;
out:
default_magic = NULL;
free(hmagicpath);
return MAGIC;
#else
char *hmagicp = hmagicpath;
char *tmppath = NULL;
LPTSTR dllpath;
#define APPENDPATH() \
do { \
if (tmppath && access(tmppath, R_OK) != -1) { \
if (hmagicpath == NULL) \
hmagicpath = tmppath; \
else { \
if (asprintf(&hmagicp, "%s%c%s", hmagicpath, \
PATHSEP, tmppath) >= 0) { \
free(hmagicpath); \
hmagicpath = hmagicp; \
} \
free(tmppath); \
} \
tmppath = NULL; \
} \
} while (0)
if (default_magic) {
free(default_magic);
default_magic = NULL;
}
if ((home = getenv("LOCALAPPDATA")) == NULL) {
if ((home = getenv("USERPROFILE")) != NULL)
if (asprintf(&tmppath,
"%s/Local Settings/Application Data%s", home,
hmagic) < 0)
tmppath = NULL;
} else {
if (asprintf(&tmppath, "%s%s", home, hmagic) < 0)
tmppath = NULL;
}
APPENDPATH();
if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0)
APPENDPATH();
}
dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1));
dllpath[MAX_PATH] = 0;
if (GetModuleFileNameA(NULL, dllpath, MAX_PATH)){
PathRemoveFileSpecA(dllpath);
if (strlen(dllpath) > 3 &&
stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
if (asprintf(&tmppath,
"%s/../share/misc/magic.mgc", dllpath) >= 0)
APPENDPATH();
} else {
if (asprintf(&tmppath,
"%s/share/misc/magic.mgc", dllpath) >= 0)
APPENDPATH();
else if (asprintf(&tmppath,
"%s/magic.mgc", dllpath) >= 0)
APPENDPATH();
}
}
default_magic = hmagicpath;
return default_magic;
#endif
}
public const char *
magic_getpath(const char *magicfile, int action)
{
if (magicfile != NULL)
return magicfile;
magicfile = getenv("MAGIC");
if (magicfile != NULL)
return magicfile;
return action == FILE_LOAD ? get_default_magic() : MAGIC;
}
#endif
public struct magic_set *
magic_open(int flags)
{
return file_ms_alloc(flags);
}
private int
unreadable_info(struct magic_set *ms, mode_t md, const char *file)
{
if (access(file, W_OK) == 0)
if (file_printf(ms, "writable, ") == -1)
return -1;
if (access(file, X_OK) == 0)
if (file_printf(ms, "executable, ") == -1)
return -1;
if (S_ISREG(md))
if (file_printf(ms, "regular file, ") == -1)
return -1;
if (file_printf(ms, "no read permission") == -1)
return -1;
return 0;
}
public void
magic_close(struct magic_set *ms)
{
if (ms == NULL)
return;
file_ms_free(ms);
}
public int
magic_load(struct magic_set *ms, const char *magicfile)
{
if (ms == NULL)
return -1;
return file_apprentice(ms, magicfile, FILE_LOAD);
}
public int
magic_compile(struct magic_set *ms, const char *magicfile)
{
if (ms == NULL)
return -1;
return file_apprentice(ms, magicfile, FILE_COMPILE);
}
public int
magic_list(struct magic_set *ms, const char *magicfile)
{
if (ms == NULL)
return -1;
return file_apprentice(ms, magicfile, FILE_LIST);
}
private void
close_and_restore(const struct magic_set *ms, const char *name, int fd,
const struct stat *sb)
{
if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
#ifdef HAVE_UTIMES
struct timeval utsbuf[2];
(void)memset(utsbuf, 0, sizeof(utsbuf));
utsbuf[0].tv_sec = sb->st_atime;
utsbuf[1].tv_sec = sb->st_mtime;
(void) utimes(name, utsbuf);
#elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
struct utimbuf utbuf;
(void)memset(&utbuf, 0, sizeof(utbuf));
utbuf.actime = sb->st_atime;
utbuf.modtime = sb->st_mtime;
(void) utime(name, &utbuf);
#endif
}
}
public const char *
magic_descriptor(struct magic_set *ms, int fd)
{
if (ms == NULL)
return NULL;
return file_or_stream(ms, NULL, NULL);
}
public const char *
magic_file(struct magic_set *ms, const char *inname)
{
if (ms == NULL)
return NULL;
return file_or_stream(ms, inname, NULL);
}
public const char *
magic_stream(struct magic_set *ms, php_stream *stream)
{
if (ms == NULL)
return NULL;
return file_or_stream(ms, NULL, stream);
}
private const char *
file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
{
int rv = -1;
unsigned char *buf;
struct stat sb;
ssize_t nbytes = 0;
int no_in_stream = 0;
TSRMLS_FETCH();
if (!inname && !stream) {
return NULL;
}
#define SLOP (1 + sizeof(union VALUETYPE))
buf = emalloc(HOWMANY + SLOP);
if (file_reset(ms) == -1)
goto done;
switch (file_fsmagic(ms, inname, &sb, stream)) {
case -1:
goto done;
case 0:
break;
default:
rv = 0;
goto done;
}
errno = 0;
if (!stream && inname) {
no_in_stream = 1;
#if PHP_API_VERSION < 20100412
stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
#else
stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
#endif
}
if (!stream) {
if (unreadable_info(ms, sb.st_mode, inname) == -1)
goto done;
rv = 0;
goto done;
}
#ifdef O_NONBLOCK
#endif
if ((nbytes = php_stream_read(stream, (char *)buf, HOWMANY)) < 0) {
file_error(ms, errno, "cannot read `%s'", inname);
goto done;
}
(void)memset(buf + nbytes, 0, SLOP);
if (file_buffer(ms, stream, inname, buf, (size_t)nbytes) == -1)
goto done;
rv = 0;
done:
efree(buf);
if (no_in_stream && stream) {
php_stream_close(stream);
}
close_and_restore(ms, inname, 0, &sb);
return rv == 0 ? file_getbuffer(ms) : NULL;
}
public const char *
magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
{
if (ms == NULL)
return NULL;
if (file_reset(ms) == -1)
return NULL;
if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
return NULL;
}
return file_getbuffer(ms);
}
public const char *
magic_error(struct magic_set *ms)
{
if (ms == NULL)
return "Magic database is not open";
return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
}
public int
magic_errno(struct magic_set *ms)
{
if (ms == NULL)
return EINVAL;
return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
}
public int
magic_setflags(struct magic_set *ms, int flags)
{
if (ms == NULL)
return -1;
#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
if (flags & MAGIC_PRESERVE_ATIME)
return -1;
#endif
ms->flags = flags;
return 0;
}
public int
magic_version(void)
{
return MAGIC_VERSION;
}