Back to index

php5  5.3.10
Defines | Functions
magic.c File Reference
#include "file.h"
#include "magic.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "php_config.h"
#include <limits.h>
#include <netinet/in.h>
#include "patchlevel.h"

Go to the source code of this file.

Defines

#define PIPE_BUF   512
#define STDIN_FILENO   0
#define SLOP   (1 + sizeof(union VALUETYPE))

Functions

private void free_mlist (struct mlist *)
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 *)
private const char * file_or_stream (struct magic_set *, const char *, php_stream *)
public struct magic_setmagic_open (int flags)
public void magic_close (struct magic_set *ms)
public int magic_load (struct magic_set *ms, const char *magicfile)
public int magic_compile (struct magic_set *ms, const char *magicfile)
public const char * magic_descriptor (struct magic_set *ms, int fd)
public const char * magic_file (struct magic_set *ms, const char *inname)
public const char * magic_stream (struct magic_set *ms, php_stream *stream)
public const char * magic_buffer (struct magic_set *ms, const void *buf, size_t nb)
public const char * magic_error (struct magic_set *ms)
public int magic_errno (struct magic_set *ms)
public int magic_setflags (struct magic_set *ms, int flags)

Define Documentation

#define PIPE_BUF   512

Definition at line 76 of file magic.c.

#define SLOP   (1 + sizeof(union VALUETYPE))
#define STDIN_FILENO   0

Definition at line 92 of file magic.c.


Function Documentation

private void close_and_restore ( const struct magic_set ms,
const char *  name,
int  fd,
const struct stat *  sb 
)

Definition at line 200 of file magic.c.

{

       if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
              /*
               * Try to restore access, modification times if read it.
               * This is really *bad* because it will modify the status
               * time of the file... And of course this will affect
               * backup programs
               */
#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); /* don't care if loses */
#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); /* don't care if loses */
#endif
       }
}

Here is the caller graph for this function:

private const char * file_or_stream ( struct magic_set ms,
const char *  inname,
php_stream *  stream 
)

Definition at line 255 of file magic.c.

{
       int    rv = -1;
       unsigned char *buf;
       struct stat   sb;
       ssize_t nbytes = 0;  /* number of bytes read from a datafile */
       int no_in_stream = 0;
       TSRMLS_FETCH();

       if (!inname && !stream) {
              return NULL;
       }

       /*
        * one extra for terminating '\0', and
        * some overlapping space for matches near EOF
        */
#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:             /* error */
                     goto done;
              case 0:                     /* nothing found */
                     break;
              default:             /* matched it and printed type */
                     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
/* we should be already be in non blocking mode for network socket */
#endif

       /*
        * try looking at the first HOWMANY bytes
        */
       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); /* NUL terminate */
       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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

private void free_mlist ( struct mlist mlist)

Definition at line 123 of file magic.c.

{
       struct mlist *ml;

       if (mlist == NULL)
              return;

       for (ml = mlist->next; ml != mlist;) {
              struct mlist *next = ml->next;
              struct magic *mg = ml->magic;
              file_delmagic(mg, ml->mapped, ml->nmagic);
              efree(ml);
              ml = next;
       }
       efree(ml);
}

Here is the call graph for this function:

Here is the caller graph for this function:

public const char* magic_buffer ( struct magic_set ms,
const void *  buf,
size_t  nb 
)

Definition at line 335 of file magic.c.

{
       if (file_reset(ms) == -1)
              return NULL;
       /*
        * The main work is done here!
        * We have the file name and/or the data buffer to be identified. 
        */
       if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
              return NULL;
       }
       return file_getbuffer(ms);
}

Here is the call graph for this function:

Here is the caller graph for this function:

public void magic_close ( struct magic_set ms)

Definition at line 159 of file magic.c.

{
       if (ms->mlist) {
              free_mlist(ms->mlist);
       }
       if (ms->o.pbuf) {
              efree(ms->o.pbuf);
       }
       if (ms->o.buf) {
              efree(ms->o.buf);
       }
       if (ms->c.li) {
              efree(ms->c.li);
       }
       efree(ms);
}

Here is the call graph for this function:

Here is the caller graph for this function:

public int magic_compile ( struct magic_set ms,
const char *  magicfile 
)

Definition at line 192 of file magic.c.

{
       struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
       free_mlist(ml);
       return ml ? 0 : -1;
}

Here is the call graph for this function:

public const char* magic_descriptor ( struct magic_set ms,
int  fd 
)

Definition at line 234 of file magic.c.

{
       return file_or_stream(ms, NULL, NULL);
}

Here is the call graph for this function:

public int magic_errno ( struct magic_set ms)

Definition at line 356 of file magic.c.

{
       return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
}

Here is the caller graph for this function:

public const char* magic_error ( struct magic_set ms)

Definition at line 350 of file magic.c.

{
       return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
}

Here is the caller graph for this function:

public const char* magic_file ( struct magic_set ms,
const char *  inname 
)

Definition at line 243 of file magic.c.

{
       return file_or_stream(ms, inname, NULL);
}

Here is the call graph for this function:

public int magic_load ( struct magic_set ms,
const char *  magicfile 
)

Definition at line 180 of file magic.c.

{
       struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
       if (ml) {
              free_mlist(ms->mlist);
              ms->mlist = ml;
              return 0;
       }
       return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

public struct magic_set* magic_open ( int  flags) [read]

Definition at line 96 of file magic.c.

{
       struct magic_set *ms;

       ms = ecalloc((size_t)1, sizeof(struct magic_set));

       if (magic_setflags(ms, flags) == -1) {
              errno = EINVAL;
              goto free;
       }

       ms->o.buf = ms->o.pbuf = NULL;

       ms->c.li = emalloc((ms->c.len = 10) * sizeof(*ms->c.li));
       
       ms->event_flags = 0;
       ms->error = -1;
       ms->mlist = NULL;
       ms->file = "unknown";
       ms->line = 0;
       return ms;
free:
       efree(ms);
       return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

public int magic_setflags ( struct magic_set ms,
int  flags 
)

Definition at line 362 of file magic.c.

{
#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
       if (flags & MAGIC_PRESERVE_ATIME)
              return -1;
#endif
       ms->flags = flags;
       return 0;
}

Here is the caller graph for this function:

public const char* magic_stream ( struct magic_set ms,
php_stream *  stream 
)

Definition at line 249 of file magic.c.

{
       return file_or_stream(ms, NULL, stream);
}

Here is the call graph for this function:

Here is the caller graph for this function:

private int unreadable_info ( struct magic_set ms,
mode_t  md,
const char *  file 
)

Definition at line 141 of file magic.c.

{
       /* We cannot open it, but we were able to stat it. */
       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;
}

Here is the call graph for this function:

Here is the caller graph for this function: