Back to index

php5  5.3.10
Defines | Functions
funcs.c File Reference
#include "file.h"
#include "magic.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

Go to the source code of this file.

Defines

#define SIZE_MAX   ((size_t) -1)
#define OCTALIFY(n, o)

Functions

protected int file_printf (struct magic_set *ms, const char *fmt,...)
private void file_error_core (struct magic_set *ms, int error, const char *f, va_list va, uint32_t lineno)
protected void file_error (struct magic_set *ms, int error, const char *f,...)
protected void file_magerror (struct magic_set *ms, const char *f,...)
protected void file_oomem (struct magic_set *ms, size_t len)
protected void file_badseek (struct magic_set *ms)
protected void file_badread (struct magic_set *ms)
protected int file_buffer (struct magic_set *ms, php_stream *stream, const char *inname, const void *buf, size_t nb)
protected int file_reset (struct magic_set *ms)
protected const char * file_getbuffer (struct magic_set *ms)
protected int file_check_mem (struct magic_set *ms, unsigned int level)

Define Documentation

#define OCTALIFY (   n,
 
)
Value:
/*LINTED*/ \
       (void)(*(n)++ = '\\', \
       *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
       *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
       *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
       (o)++)

Definition at line 322 of file funcs.c.

#define SIZE_MAX   ((size_t) -1)

Definition at line 46 of file funcs.c.


Function Documentation

protected void file_badread ( struct magic_set ms)

Definition at line 150 of file funcs.c.

{
       file_error(ms, errno, "error reading");
}

Here is the call graph for this function:

Here is the caller graph for this function:

protected void file_badseek ( struct magic_set ms)

Definition at line 144 of file funcs.c.

{
       file_error(ms, errno, "error seeking");
}

Here is the call graph for this function:

Here is the caller graph for this function:

protected int file_buffer ( struct magic_set ms,
php_stream *  stream,
const char *  inname,
const void *  buf,
size_t  nb 
)

Definition at line 156 of file funcs.c.

{
       int m = 0, rv = 0, looks_text = 0;
       int mime = ms->flags & MAGIC_MIME;
       const unsigned char *ubuf = buf;
       unichar *u8buf = NULL;
       size_t ulen;
       const char *code = NULL;
       const char *code_mime = "binary";
       const char *type = NULL;



       if (nb == 0) {
              if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
                  file_printf(ms, mime ? "application/x-empty" :
                  "empty") == -1)
                     return -1;
              return 1;
       } else if (nb == 1) {
              if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
                  file_printf(ms, mime ? "application/octet-stream" :
                  "very short file (no magic)") == -1)
                     return -1;
              return 1;
       }

       if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
              looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
                  &code, &code_mime, &type);
       }

#if defined(__EMX__)
       if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
              switch (file_os2_apptype(ms, inname, buf, nb)) {
              case -1:
                     return -1;
              case 0:
                     break;
              default:
                     return 1;
              }
       }
#endif

#if PHP_FILEINFO_UNCOMPRESS
       if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
              if ((m = file_zmagic(ms, stream, inname, ubuf, nb)) != 0) {
                     if ((ms->flags & MAGIC_DEBUG) != 0)
                            (void)fprintf(stderr, "zmagic %d\n", m);
                     goto done;
       }
#endif

       /* Check if we have a tar file */
       if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
              if ((m = file_is_tar(ms, ubuf, nb)) != 0) {
                     if ((ms->flags & MAGIC_DEBUG) != 0)
                            (void)fprintf(stderr, "tar %d\n", m);
                     goto done;
              }

       /* Check if we have a CDF file */
       if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
              int fd;
              TSRMLS_FETCH();
              if (stream && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, 0)) {
                     if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {
                            if ((ms->flags & MAGIC_DEBUG) != 0)
                                   (void)fprintf(stderr, "cdf %d\n", m);
                            goto done;
                     }
              }
       }

       /* try soft magic tests */
       if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
              if ((m = file_softmagic(ms, ubuf, nb, BINTEST)) != 0) {
                     if ((ms->flags & MAGIC_DEBUG) != 0)
                            (void)fprintf(stderr, "softmagic %d\n", m);
#ifdef BUILTIN_ELF
                     if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
                         nb > 5 && fd != -1) {
                            /*
                             * We matched something in the file, so this
                             * *might* be an ELF file, and the file is at
                             * least 5 bytes long, so if it's an ELF file
                             * it has at least one byte past the ELF magic
                             * number - try extracting information from the
                             * ELF headers that cannot easily * be
                             * extracted with rules in the magic file.
                             */
                            if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
                                   if ((ms->flags & MAGIC_DEBUG) != 0)
                                          (void)fprintf(stderr,
                                              "elf %d\n", m);
                     }
#endif
                     goto done;
              }

       /* try text properties (and possibly text tokens) */
       if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {

              if ((m = file_ascmagic(ms, ubuf, nb)) != 0) {
                     if ((ms->flags & MAGIC_DEBUG) != 0)
                            (void)fprintf(stderr, "ascmagic %d\n", m);
                     goto done;
              }

              /* try to discover text encoding */
              if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
                     if (looks_text == 0)
                            if ((m = file_ascmagic_with_encoding( ms, ubuf,
                                nb, u8buf, ulen, code, type)) != 0) {
                                   if ((ms->flags & MAGIC_DEBUG) != 0)
                                          (void)fprintf(stderr,
                                              "ascmagic/enc %d\n", m);
                                   goto done;
                            }
              }
       }

       /* give up */
       m = 1;
       if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
           file_printf(ms, mime ? "application/octet-stream" : "data") == -1) {
           rv = -1;
       }
 done:
       if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
              if (ms->flags & MAGIC_MIME_TYPE)
                     if (file_printf(ms, "; charset=") == -1)
                            rv = -1;
              if (file_printf(ms, "%s", code_mime) == -1)
                     rv = -1;
       }
       if (u8buf)
              free(u8buf);
       if (rv)
              return rv;

       return m;
}

Here is the call graph for this function:

Here is the caller graph for this function:

protected int file_check_mem ( struct magic_set ms,
unsigned int  level 
)

Definition at line 405 of file funcs.c.

{
       size_t len;

       if (level >= ms->c.len) {
              len = (ms->c.len += 20) * sizeof(*ms->c.li);
              ms->c.li = (ms->c.li == NULL) ? emalloc(len) : erealloc(ms->c.li, len);
       }
       ms->c.li[level].got_match = 0;
#ifdef ENABLE_CONDITIONALS
       ms->c.li[level].last_match = 0;
       ms->c.li[level].last_cond = COND_NONE;
#endif /* ENABLE_CONDITIONALS */
       return 0;
}

Here is the caller graph for this function:

protected void file_error ( struct magic_set ms,
int  error,
const char *  f,
  ... 
)

Definition at line 116 of file funcs.c.

{
       va_list va;
       va_start(va, f);
       file_error_core(ms, error, f, va, 0);
       va_end(va);
}

Here is the call graph for this function:

Here is the caller graph for this function:

private void file_error_core ( struct magic_set ms,
int  error,
const char *  f,
va_list  va,
uint32_t  lineno 
)

Definition at line 81 of file funcs.c.

{
       char *buf = NULL;
       
       /* Only the first error is ok */
       if (ms->event_flags & EVENT_HAD_ERR) {
              return;
       }
       
       if (lineno != 0) {
              efree(ms->o.buf);
              ms->o.buf = NULL;
              file_printf(ms, "line %u: ", lineno);
       }

       vspprintf(&buf, 0, f, va);
       va_end(va);
       
       if (error > 0) {
              file_printf(ms, "%s (%s)", (*buf ? buf : ""), strerror(error));
       } else if (*buf) {
              file_printf(ms, "%s", buf);
       }
       
       if (buf) {
              efree(buf);
       }

       ms->event_flags |= EVENT_HAD_ERR;
       ms->error = error;
}

Here is the call graph for this function:

Here is the caller graph for this function:

protected const char* file_getbuffer ( struct magic_set ms)

Definition at line 331 of file funcs.c.

{
       char *pbuf, *op, *np;
       size_t psize, len;

       if (ms->event_flags & EVENT_HAD_ERR)
              return NULL;

       if (ms->flags & MAGIC_RAW)
              return ms->o.buf;

       if (ms->o.buf == NULL)
              return NULL;

       /* * 4 is for octal representation, + 1 is for NUL */
       len = strlen(ms->o.buf);
       if (len > (SIZE_MAX - 1) / 4) {
              return NULL;
       }
       psize = len * 4 + 1;
       pbuf = erealloc(ms->o.pbuf, psize);
       ms->o.pbuf = pbuf;

#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
       {
              mbstate_t state;
              wchar_t nextchar;
              int mb_conv = 1;
              size_t bytesconsumed;
              char *eop;
              (void)memset(&state, 0, sizeof(mbstate_t));

              np = ms->o.pbuf;
              op = ms->o.buf;
              eop = op + len;

              while (op < eop) {
                     bytesconsumed = mbrtowc(&nextchar, op,
                         (size_t)(eop - op), &state);
                     if (bytesconsumed == (size_t)(-1) ||
                         bytesconsumed == (size_t)(-2)) {
                            mb_conv = 0;
                            break;
                     }

                     if (iswprint(nextchar)) {
                            (void)memcpy(np, op, bytesconsumed);
                            op += bytesconsumed;
                            np += bytesconsumed;
                     } else {
                            while (bytesconsumed-- > 0)
                                   OCTALIFY(np, op);
                     }
              }
              *np = '\0';

              /* Parsing succeeded as a multi-byte sequence */
              if (mb_conv != 0)
                     return ms->o.pbuf;
       }
#endif

       for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
              if (isprint((unsigned char)*op)) {
                     *np++ = *op;
              } else {
                     OCTALIFY(np, op);
              }
       }
       *np = '\0';
       return ms->o.pbuf;
}

Here is the caller graph for this function:

protected void file_magerror ( struct magic_set ms,
const char *  f,
  ... 
)

Definition at line 129 of file funcs.c.

{
       va_list va;
       va_start(va, f);
       file_error_core(ms, 0, f, va, ms->line);
       va_end(va);
}

Here is the call graph for this function:

Here is the caller graph for this function:

protected void file_oomem ( struct magic_set ms,
size_t  len 
)

Definition at line 138 of file funcs.c.

{
       file_error(ms, errno, "cannot allocate %zu bytes", len);
}

Here is the call graph for this function:

Here is the caller graph for this function:

protected int file_printf ( struct magic_set ms,
const char *  fmt,
  ... 
)

Definition at line 53 of file funcs.c.

{
       va_list ap;
       int len;
       char *buf = NULL, *newstr;

       va_start(ap, fmt);
       len = vspprintf(&buf, 0, fmt, ap);
       va_end(ap);

       if (ms->o.buf != NULL) {
              len = spprintf(&newstr, 0, "%s%s", ms->o.buf, (buf ? buf : ""));
              if (buf) {
                     efree(buf);
              }
              efree(ms->o.buf);
              ms->o.buf = newstr;
       } else {
              ms->o.buf = buf;
       }
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

protected int file_reset ( struct magic_set ms)

Definition at line 303 of file funcs.c.

{
       if (ms->mlist == NULL) {
              file_error(ms, 0, "no magic files loaded");
              return -1;
       }
       if (ms->o.buf) {
              efree(ms->o.buf);
              ms->o.buf = NULL;
       }
       if (ms->o.pbuf) {
              efree(ms->o.pbuf);
              ms->o.pbuf = NULL;
       }
       ms->event_flags &= ~EVENT_HAD_ERR;
       ms->error = -1;
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: