diff options
Diffstat (limited to 'ext/fileinfo/libmagic')
25 files changed, 12760 insertions, 0 deletions
diff --git a/ext/fileinfo/libmagic/LICENSE b/ext/fileinfo/libmagic/LICENSE new file mode 100644 index 0000000..5095010 --- /dev/null +++ b/ext/fileinfo/libmagic/LICENSE @@ -0,0 +1,34 @@ +Author: Christos Zoulas <christos@zoulas.com> +Download: ftp://ftp.astron.com/pub/file/ + +Files: * +Copyright: + (C) 1985-1995 Ian F. Darwin + (C) 1994-2009 Christos Zoulas <christos@zoulas.com> +License: BSD + This software is not subject to any export provision of the United States + Department of Commerce, and may be exported to any country or planet. + . + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + . + 1. Redistributions of source code must retain the above copyright + notice immediately at the beginning of the file, without modification, + this list of conditions, and the following disclaimer. + . + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + . + THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. diff --git a/ext/fileinfo/libmagic/apprentice.c b/ext/fileinfo/libmagic/apprentice.c new file mode 100644 index 0000000..09e1812 --- /dev/null +++ b/ext/fileinfo/libmagic/apprentice.c @@ -0,0 +1,2566 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * apprentice - make one pass through /etc/magic, learning its secrets. + */ + +#include "php.h" + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: apprentice.c,v 1.173 2011/12/08 12:38:24 rrt Exp $") +#endif /* lint */ + +#include "magic.h" +#include "patchlevel.h" +#include <stdlib.h> + +#if defined(__hpux) && !defined(HAVE_STRTOULL) +#if SIZEOF_LONG == 8 +# define strtoull strtoul +#else +# define strtoull __strtoull +#endif +#endif + +#ifdef PHP_WIN32 +#include "win32/unistd.h" +#if _MSC_VER <= 1300 +# include "win32/php_strtoi64.h" +#endif +#define strtoull _strtoui64 +#else +#include <unistd.h> +#endif + +#include <string.h> +#include <assert.h> +#include <ctype.h> +#include <fcntl.h> + +#define EATAB {while (isascii((unsigned char) *l) && \ + isspace((unsigned char) *l)) ++l;} +#define LOWCASE(l) (isupper((unsigned char) (l)) ? \ + tolower((unsigned char) (l)) : (l)) +/* + * Work around a bug in headers on Digital Unix. + * At least confirmed for: OSF1 V4.0 878 + */ +#if defined(__osf__) && defined(__DECC) +#ifdef MAP_FAILED +#undef MAP_FAILED +#endif +#endif + +#ifndef MAP_FAILED +#define MAP_FAILED (void *) -1 +#endif + +#ifndef MAP_FILE +#define MAP_FILE 0 +#endif + +struct magic_entry { + struct magic *mp; + uint32_t cont_count; + uint32_t max_count; +}; + +int file_formats[FILE_NAMES_SIZE]; +const size_t file_nformats = FILE_NAMES_SIZE; +const char *file_names[FILE_NAMES_SIZE]; +const size_t file_nnames = FILE_NAMES_SIZE; + +private int getvalue(struct magic_set *ms, struct magic *, const char **, int); +private int hextoint(int); +private const char *getstr(struct magic_set *, struct magic *, const char *, + int); +private int parse(struct magic_set *, struct magic_entry **, uint32_t *, + const char *, size_t, int); +private void eatsize(const char **); +private int apprentice_1(struct magic_set *, const char *, int, struct mlist *); +private size_t apprentice_magic_strength(const struct magic *); +private int apprentice_sort(const void *, const void *); +private void apprentice_list(struct mlist *, int ); +private int apprentice_load(struct magic_set *, struct magic **, uint32_t *, + const char *, int); +private void byteswap(struct magic *, uint32_t); +private void bs1(struct magic *); +private uint16_t swap2(uint16_t); +private uint32_t swap4(uint32_t); +private uint64_t swap8(uint64_t); +private char *mkdbname(struct magic_set *, const char *, int); +private int apprentice_map(struct magic_set *, struct magic **, uint32_t *, + const char *); +private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *, + const char *); +private int check_format_type(const char *, int); +private int check_format(struct magic_set *, struct magic *); +private int get_op(char); +private int parse_mime(struct magic_set *, struct magic_entry *, const char *); +private int parse_strength(struct magic_set *, struct magic_entry *, const char *); +private int parse_apple(struct magic_set *, struct magic_entry *, const char *); + +private size_t maxmagic = 0; +private size_t magicsize = sizeof(struct magic); + +private const char usg_hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc"; +private struct { + const char *name; + size_t len; + int (*fun)(struct magic_set *, struct magic_entry *, const char *); +} bang[] = { +#define DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name } + DECLARE_FIELD(mime), + DECLARE_FIELD(apple), + DECLARE_FIELD(strength), +#undef DECLARE_FIELD + { NULL, 0, NULL } +}; + +#include "../data_file.c" + +static const struct type_tbl_s { + const char name[16]; + const size_t len; + const int type; + const int format; +} type_tbl[] = { +# define XX(s) s, (sizeof(s) - 1) +# define XX_NULL "", 0 + { XX("byte"), FILE_BYTE, FILE_FMT_NUM }, + { XX("short"), FILE_SHORT, FILE_FMT_NUM }, + { XX("default"), FILE_DEFAULT, FILE_FMT_STR }, + { XX("long"), FILE_LONG, FILE_FMT_NUM }, + { XX("string"), FILE_STRING, FILE_FMT_STR }, + { XX("date"), FILE_DATE, FILE_FMT_STR }, + { XX("beshort"), FILE_BESHORT, FILE_FMT_NUM }, + { XX("belong"), FILE_BELONG, FILE_FMT_NUM }, + { XX("bedate"), FILE_BEDATE, FILE_FMT_STR }, + { XX("leshort"), FILE_LESHORT, FILE_FMT_NUM }, + { XX("lelong"), FILE_LELONG, FILE_FMT_NUM }, + { XX("ledate"), FILE_LEDATE, FILE_FMT_STR }, + { XX("pstring"), FILE_PSTRING, FILE_FMT_STR }, + { XX("ldate"), FILE_LDATE, FILE_FMT_STR }, + { XX("beldate"), FILE_BELDATE, FILE_FMT_STR }, + { XX("leldate"), FILE_LELDATE, FILE_FMT_STR }, + { XX("regex"), FILE_REGEX, FILE_FMT_STR }, + { XX("bestring16"), FILE_BESTRING16, FILE_FMT_STR }, + { XX("lestring16"), FILE_LESTRING16, FILE_FMT_STR }, + { XX("search"), FILE_SEARCH, FILE_FMT_STR }, + { XX("medate"), FILE_MEDATE, FILE_FMT_STR }, + { XX("meldate"), FILE_MELDATE, FILE_FMT_STR }, + { XX("melong"), FILE_MELONG, FILE_FMT_NUM }, + { XX("quad"), FILE_QUAD, FILE_FMT_QUAD }, + { XX("lequad"), FILE_LEQUAD, FILE_FMT_QUAD }, + { XX("bequad"), FILE_BEQUAD, FILE_FMT_QUAD }, + { XX("qdate"), FILE_QDATE, FILE_FMT_STR }, + { XX("leqdate"), FILE_LEQDATE, FILE_FMT_STR }, + { XX("beqdate"), FILE_BEQDATE, FILE_FMT_STR }, + { XX("qldate"), FILE_QLDATE, FILE_FMT_STR }, + { XX("leqldate"), FILE_LEQLDATE, FILE_FMT_STR }, + { XX("beqldate"), FILE_BEQLDATE, FILE_FMT_STR }, + { XX("float"), FILE_FLOAT, FILE_FMT_FLOAT }, + { XX("befloat"), FILE_BEFLOAT, FILE_FMT_FLOAT }, + { XX("lefloat"), FILE_LEFLOAT, FILE_FMT_FLOAT }, + { XX("double"), FILE_DOUBLE, FILE_FMT_DOUBLE }, + { XX("bedouble"), FILE_BEDOUBLE, FILE_FMT_DOUBLE }, + { XX("ledouble"), FILE_LEDOUBLE, FILE_FMT_DOUBLE }, + { XX("leid3"), FILE_LEID3, FILE_FMT_NUM }, + { XX("beid3"), FILE_BEID3, FILE_FMT_NUM }, + { XX("indirect"), FILE_INDIRECT, FILE_FMT_NONE }, + { XX_NULL, FILE_INVALID, FILE_FMT_NONE }, +# undef XX +# undef XX_NULL +}; + +#ifndef S_ISDIR +#define S_ISDIR(mode) ((mode) & _S_IFDIR) +#endif + +private int +get_type(const char *l, const char **t) +{ + const struct type_tbl_s *p; + + for (p = type_tbl; p->len; p++) { + if (strncmp(l, p->name, p->len) == 0) { + if (t) + *t = l + p->len; + break; + } + } + return p->type; +} + +private void +init_file_tables(void) +{ + static int done = 0; + const struct type_tbl_s *p; + + if (done) + return; + done++; + + for (p = type_tbl; p->len; p++) { + assert(p->type < FILE_NAMES_SIZE); + file_names[p->type] = p->name; + file_formats[p->type] = p->format; + } +} + +/* + * Handle one file or directory. + */ +private int +apprentice_1(struct magic_set *ms, const char *fn, int action, + struct mlist *mlist) +{ + struct magic *magic = NULL; + uint32_t nmagic = 0; + struct mlist *ml; + int rv = -1; + int mapped; + + if (magicsize != FILE_MAGICSIZE) { + file_error(ms, 0, "magic element size %lu != %lu", + (unsigned long)sizeof(*magic), + (unsigned long)FILE_MAGICSIZE); + return -1; + } + + if (action == FILE_COMPILE) { + rv = apprentice_load(ms, &magic, &nmagic, fn, action); + if (rv != 0) + return -1; + rv = apprentice_compile(ms, &magic, &nmagic, fn); + efree(magic); + return rv; + } + + if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) { + if (fn) { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "using regular magic file `%s'", fn); + rv = apprentice_load(ms, &magic, &nmagic, fn, action); + } + + if (rv != 0) + return -1; + } + + mapped = rv; + + if (magic == NULL) { + file_delmagic(magic, mapped, nmagic); + return -1; + } + + ml = emalloc(sizeof(*ml)); + + ml->magic = magic; + ml->nmagic = nmagic; + ml->mapped = mapped; + + mlist->prev->next = ml; + ml->prev = mlist->prev; + ml->next = mlist; + mlist->prev = ml; + + if (action == FILE_LIST) { + printf("Binary patterns:\n"); + apprentice_list(mlist, BINTEST); + printf("Text patterns:\n"); + apprentice_list(mlist, TEXTTEST); + } + + return 0; +} + +protected void +file_delmagic(struct magic *p, int type, size_t entries) +{ + if (p == NULL) + return; + switch (type) { + case 3: + /* Do nothing, it's part of the code segment */ + break; + + case 1: + p--; + /*FALLTHROUGH*/ + + case 0: + efree(p); + break; + + default: + abort(); + } +} + +/* const char *fn: list of magic files and directories */ +protected struct mlist * +file_apprentice(struct magic_set *ms, const char *fn, int action) +{ + char *p, *mfn; + int file_err, errs = -1; + struct mlist *mlist; +/* XXX disabling default magic loading so the compiled in data is used */ +#if 0 + if ((fn = magic_getpath(fn, action)) == NULL) + return NULL; +#endif + + init_file_tables(); + + if (fn == NULL) + fn = getenv("MAGIC"); + if (fn == NULL) { + mlist = emalloc(sizeof(*mlist)); + mlist->next = mlist->prev = mlist; + apprentice_1(ms, fn, action, mlist); + return mlist; + } + + mfn = estrdup(fn); + fn = mfn; + + mlist = emalloc(sizeof(*mlist)); + mlist->next = mlist->prev = mlist; + + while (fn) { + p = strchr(fn, PATHSEP); + if (p) + *p++ = '\0'; + if (*fn == '\0') + break; + file_err = apprentice_1(ms, fn, action, mlist); + errs = MAX(errs, file_err); + fn = p; + } + if (errs == -1) { + efree(mfn); + efree(mlist); + mlist = NULL; + file_error(ms, 0, "could not find any magic files!"); + return NULL; + } + efree(mfn); + return mlist; +} + +/* + * Get weight of this magic entry, for sorting purposes. + */ +private size_t +apprentice_magic_strength(const struct magic *m) +{ +#define MULT 10 + size_t val = 2 * MULT; /* baseline strength */ + + switch (m->type) { + case FILE_DEFAULT: /* make sure this sorts last */ + if (m->factor_op != FILE_FACTOR_OP_NONE) + abort(); + return 0; + + case FILE_BYTE: + val += 1 * MULT; + break; + + case FILE_SHORT: + case FILE_LESHORT: + case FILE_BESHORT: + val += 2 * MULT; + break; + + case FILE_LONG: + case FILE_LELONG: + case FILE_BELONG: + case FILE_MELONG: + val += 4 * MULT; + break; + + case FILE_PSTRING: + case FILE_STRING: + val += m->vallen * MULT; + break; + + case FILE_BESTRING16: + case FILE_LESTRING16: + val += m->vallen * MULT / 2; + break; + + case FILE_SEARCH: + case FILE_REGEX: + val += m->vallen * MAX(MULT / m->vallen, 1); + break; + + case FILE_DATE: + case FILE_LEDATE: + case FILE_BEDATE: + case FILE_MEDATE: + case FILE_LDATE: + case FILE_LELDATE: + case FILE_BELDATE: + case FILE_MELDATE: + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + val += 4 * MULT; + break; + + case FILE_QUAD: + case FILE_BEQUAD: + case FILE_LEQUAD: + case FILE_QDATE: + case FILE_LEQDATE: + case FILE_BEQDATE: + case FILE_QLDATE: + case FILE_LEQLDATE: + case FILE_BEQLDATE: + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + val += 8 * MULT; + break; + + default: + val = 0; + (void)fprintf(stderr, "Bad type %d\n", m->type); + abort(); + } + + switch (m->reln) { + case 'x': /* matches anything penalize */ + case '!': /* matches almost anything penalize */ + val = 0; + break; + + case '=': /* Exact match, prefer */ + val += MULT; + break; + + case '>': + case '<': /* comparison match reduce strength */ + val -= 2 * MULT; + break; + + case '^': + case '&': /* masking bits, we could count them too */ + val -= MULT; + break; + + default: + (void)fprintf(stderr, "Bad relation %c\n", m->reln); + abort(); + } + + if (val == 0) /* ensure we only return 0 for FILE_DEFAULT */ + val = 1; + + switch (m->factor_op) { + case FILE_FACTOR_OP_NONE: + break; + case FILE_FACTOR_OP_PLUS: + val += m->factor; + break; + case FILE_FACTOR_OP_MINUS: + val -= m->factor; + break; + case FILE_FACTOR_OP_TIMES: + val *= m->factor; + break; + case FILE_FACTOR_OP_DIV: + val /= m->factor; + break; + default: + abort(); + } + + + /* + * Magic entries with no description get a bonus because they depend + * on subsequent magic entries to print something. + */ + if (m->desc[0] == '\0') + val++; + return val; +} + +/* + * Sort callback for sorting entries by "strength" (basically length) + */ +private int +apprentice_sort(const void *a, const void *b) +{ + const struct magic_entry *ma = a; + const struct magic_entry *mb = b; + size_t sa = apprentice_magic_strength(ma->mp); + size_t sb = apprentice_magic_strength(mb->mp); + if (sa == sb) + return 0; + else if (sa > sb) + return -1; + else + return 1; +} + +/* + * Shows sorted patterns list in the order which is used for the matching + */ +private void +apprentice_list(struct mlist *mlist, int mode) +{ + uint32_t magindex = 0; + struct mlist *ml; + for (ml = mlist->next; ml != mlist; ml = ml->next) { + for (magindex = 0; magindex < ml->nmagic; magindex++) { + struct magic *m = &ml->magic[magindex]; + if ((m->flag & mode) != mode) { + /* Skip sub-tests */ + while (magindex + 1 < ml->nmagic && + ml->magic[magindex + 1].cont_level != 0) + ++magindex; + continue; /* Skip to next top-level test*/ + } + + /* + * Try to iterate over the tree until we find item with + * description/mimetype. + */ + while (magindex + 1 < ml->nmagic && + ml->magic[magindex + 1].cont_level != 0 && + *ml->magic[magindex].desc == '\0' && + *ml->magic[magindex].mimetype == '\0') + magindex++; + + printf("Strength = %3" SIZE_T_FORMAT "u : %s [%s]\n", + apprentice_magic_strength(m), + ml->magic[magindex].desc, + ml->magic[magindex].mimetype); + } + } +} + +private void +set_test_type(struct magic *mstart, struct magic *m) +{ + switch (m->type) { + case FILE_BYTE: + case FILE_SHORT: + case FILE_LONG: + case FILE_DATE: + case FILE_BESHORT: + case FILE_BELONG: + case FILE_BEDATE: + case FILE_LESHORT: + case FILE_LELONG: + case FILE_LEDATE: + case FILE_LDATE: + case FILE_BELDATE: + case FILE_LELDATE: + case FILE_MEDATE: + case FILE_MELDATE: + case FILE_MELONG: + case FILE_QUAD: + case FILE_LEQUAD: + case FILE_BEQUAD: + case FILE_QDATE: + case FILE_LEQDATE: + case FILE_BEQDATE: + case FILE_QLDATE: + case FILE_LEQLDATE: + case FILE_BEQLDATE: + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + mstart->flag |= BINTEST; + break; + case FILE_STRING: + case FILE_PSTRING: + case FILE_BESTRING16: + case FILE_LESTRING16: + /* Allow text overrides */ + if (mstart->str_flags & STRING_TEXTTEST) + mstart->flag |= TEXTTEST; + else + mstart->flag |= BINTEST; + break; + case FILE_REGEX: + case FILE_SEARCH: + /* Check for override */ + if (mstart->str_flags & STRING_BINTEST) + mstart->flag |= BINTEST; + if (mstart->str_flags & STRING_TEXTTEST) + mstart->flag |= TEXTTEST; + + if (mstart->flag & (TEXTTEST|BINTEST)) + break; + + /* binary test if pattern is not text */ + if (file_looks_utf8(m->value.us, (size_t)m->vallen, NULL, + NULL) <= 0) + mstart->flag |= BINTEST; + else + mstart->flag |= TEXTTEST; + break; + case FILE_DEFAULT: + /* can't deduce anything; we shouldn't see this at the + top level anyway */ + break; + case FILE_INVALID: + default: + /* invalid search type, but no need to complain here */ + break; + } +} + +/* + * Load and parse one file. + */ +private void +load_1(struct magic_set *ms, int action, const char *fn, int *errs, + struct magic_entry **marray, uint32_t *marraycount) +{ + char buffer[BUFSIZ + 1]; + char *line = NULL; + size_t len; + size_t lineno = 0; + + php_stream *stream; + + TSRMLS_FETCH(); + +#if PHP_API_VERSION < 20100412 + stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); +#else + stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL); +#endif + + if (stream == NULL) { + if (errno != ENOENT) + file_error(ms, errno, "cannot read magic file `%s'", + fn); + (*errs)++; + return; + } + + /* read and parse this file */ +#if (PHP_MAJOR_VERSION < 6) + for (ms->line = 1; (line = php_stream_get_line(stream, buffer , BUFSIZ, &len)) != NULL; ms->line++) { +#else + for (ms->line = 1; (line = php_stream_get_line(stream, ZSTR(buffer), BUFSIZ, &len)) != NULL; ms->line++) { +#endif + if (len == 0) /* null line, garbage, etc */ + continue; + if (line[len - 1] == '\n') { + lineno++; + line[len - 1] = '\0'; /* delete newline */ + } + switch (line[0]) { + case '\0': /* empty, do not parse */ + case '#': /* comment, do not parse */ + continue; + case '!': + if (line[1] == ':') { + size_t i; + + for (i = 0; bang[i].name != NULL; i++) { + if ((size_t)(len - 2) > bang[i].len && + memcmp(bang[i].name, line + 2, + bang[i].len) == 0) + break; + } + if (bang[i].name == NULL) { + file_error(ms, 0, + "Unknown !: entry `%s'", line); + (*errs)++; + continue; + } + if (*marraycount == 0) { + file_error(ms, 0, + "No current entry for :!%s type", + bang[i].name); + (*errs)++; + continue; + } + if ((*bang[i].fun)(ms, + &(*marray)[*marraycount - 1], + line + bang[i].len + 2) != 0) { + (*errs)++; + continue; + } + continue; + } + /*FALLTHROUGH*/ + default: + if (parse(ms, marray, marraycount, line, lineno, + action) != 0) + (*errs)++; + break; + } + } + php_stream_close(stream); +} + +/* + * parse a file or directory of files + * const char *fn: name of magic file or directory + */ +private int +cmpstrp(const void *p1, const void *p2) +{ + return strcmp(*(char *const *)p1, *(char *const *)p2); +} + +private int +apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, + const char *fn, int action) +{ + int errs = 0; + struct magic_entry *marray; + uint32_t marraycount, i, mentrycount = 0, starttest; + size_t files = 0, maxfiles = 0; + char **filearr = NULL; + struct stat st; + php_stream *dir; + php_stream_dirent d; + + TSRMLS_FETCH(); + + ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */ + + maxmagic = MAXMAGIS; + marray = ecalloc(maxmagic, sizeof(*marray)); + marraycount = 0; + + /* print silly verbose header for USG compat. */ + if (action == FILE_CHECK) + (void)fprintf(stderr, "%s\n", usg_hdr); + + /* load directory or file */ + /* FIXME: Read file names and sort them to prevent + non-determinism. See Debian bug #488562. */ + if (php_sys_stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) { + int mflen; + char mfn[MAXPATHLEN]; + + dir = php_stream_opendir(fn, REPORT_ERRORS, NULL); + if (!dir) { + errs++; + goto out; + } + while (php_stream_readdir(dir, &d)) { + if ((mflen = snprintf(mfn, sizeof(mfn), "%s/%s", fn, d.d_name)) < 0) { + file_oomem(ms, + strlen(fn) + strlen(d.d_name) + 2); + errs++; + php_stream_closedir(dir); + goto out; + } + if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { + continue; + } + if (files >= maxfiles) { + size_t mlen; + maxfiles = (maxfiles + 1) * 2; + mlen = maxfiles * sizeof(*filearr); + if ((filearr = CAST(char **, + realloc(filearr, mlen))) == NULL) { + file_oomem(ms, mlen); + php_stream_closedir(dir); + errs++; + goto out; + } + } + filearr[files++] = estrndup(mfn, (mflen > sizeof(mfn) - 1)? sizeof(mfn) - 1: mflen); + } + php_stream_closedir(dir); + qsort(filearr, files, sizeof(*filearr), cmpstrp); + for (i = 0; i < files; i++) { + load_1(ms, action, filearr[i], &errs, &marray, + &marraycount); + efree(filearr[i]); + } + free(filearr); + } else + load_1(ms, action, fn, &errs, &marray, &marraycount); + if (errs) + goto out; + + /* Set types of tests */ + for (i = 0; i < marraycount; ) { + if (marray[i].mp->cont_level != 0) { + i++; + continue; + } + + starttest = i; + do { + static const char text[] = "text"; + static const char binary[] = "binary"; + static const size_t len = sizeof(text); + set_test_type(marray[starttest].mp, marray[i].mp); + if ((ms->flags & MAGIC_DEBUG) == 0) + continue; + (void)fprintf(stderr, "%s%s%s: %s\n", + marray[i].mp->mimetype, + marray[i].mp->mimetype[0] == '\0' ? "" : "; ", + marray[i].mp->desc[0] ? marray[i].mp->desc : + "(no description)", + marray[i].mp->flag & BINTEST ? binary : text); + if (marray[i].mp->flag & BINTEST) { + char *p = strstr(marray[i].mp->desc, text); + if (p && (p == marray[i].mp->desc || + isspace((unsigned char)p[-1])) && + (p + len - marray[i].mp->desc == + MAXstring || (p[len] == '\0' || + isspace((unsigned char)p[len])))) + (void)fprintf(stderr, "*** Possible " + "binary test for text type\n"); + } + } while (++i < marraycount && marray[i].mp->cont_level != 0); + } + + qsort(marray, marraycount, sizeof(*marray), apprentice_sort); + + /* + * Make sure that any level 0 "default" line is last (if one exists). + */ + for (i = 0; i < marraycount; i++) { + if (marray[i].mp->cont_level == 0 && + marray[i].mp->type == FILE_DEFAULT) { + while (++i < marraycount) + if (marray[i].mp->cont_level == 0) + break; + if (i != marraycount) { + /* XXX - Ugh! */ + ms->line = marray[i].mp->lineno; + file_magwarn(ms, + "level 0 \"default\" did not sort last"); + } + break; + } + } + + for (i = 0; i < marraycount; i++) + mentrycount += marray[i].cont_count; + + *magicp = emalloc(sizeof(**magicp) * mentrycount); + + mentrycount = 0; + for (i = 0; i < marraycount; i++) { + (void)memcpy(*magicp + mentrycount, marray[i].mp, + marray[i].cont_count * sizeof(**magicp)); + mentrycount += marray[i].cont_count; + } +out: + for (i = 0; i < marraycount; i++) { + if (marray[i].mp) { + efree(marray[i].mp); + } + } + if (marray) { + efree(marray); + } + if (errs) { + *magicp = NULL; + *nmagicp = 0; + return errs; + } else { + *nmagicp = mentrycount; + return 0; + } + +} + +/* + * extend the sign bit if the comparison is to be signed + */ +protected uint64_t +file_signextend(struct magic_set *ms, struct magic *m, uint64_t v) +{ + if (!(m->flag & UNSIGNED)) { + switch(m->type) { + /* + * Do not remove the casts below. They are + * vital. When later compared with the data, + * the sign extension must have happened. + */ + case FILE_BYTE: + v = (char) v; + break; + case FILE_SHORT: + case FILE_BESHORT: + case FILE_LESHORT: + v = (short) v; + break; + case FILE_DATE: + case FILE_BEDATE: + case FILE_LEDATE: + case FILE_MEDATE: + case FILE_LDATE: + case FILE_BELDATE: + case FILE_LELDATE: + case FILE_MELDATE: + case FILE_LONG: + case FILE_BELONG: + case FILE_LELONG: + case FILE_MELONG: + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + v = (int32_t) v; + break; + case FILE_QUAD: + case FILE_BEQUAD: + case FILE_LEQUAD: + case FILE_QDATE: + case FILE_QLDATE: + case FILE_BEQDATE: + case FILE_BEQLDATE: + case FILE_LEQDATE: + case FILE_LEQLDATE: + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + v = (int64_t) v; + break; + case FILE_STRING: + case FILE_PSTRING: + case FILE_BESTRING16: + case FILE_LESTRING16: + case FILE_REGEX: + case FILE_SEARCH: + case FILE_DEFAULT: + case FILE_INDIRECT: + break; + default: + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "cannot happen: m->type=%d\n", + m->type); + return ~0U; + } + } + return v; +} + +private int +string_modifier_check(struct magic_set *ms, struct magic *m) +{ + if ((ms->flags & MAGIC_CHECK) == 0) + return 0; + + if (m->type != FILE_PSTRING && (m->str_flags & PSTRING_LEN) != 0) { + file_magwarn(ms, + "'/BHhLl' modifiers are only allowed for pascal strings\n"); + return -1; + } + switch (m->type) { + case FILE_BESTRING16: + case FILE_LESTRING16: + if (m->str_flags != 0) { + file_magwarn(ms, + "no modifiers allowed for 16-bit strings\n"); + return -1; + } + break; + case FILE_STRING: + case FILE_PSTRING: + if ((m->str_flags & REGEX_OFFSET_START) != 0) { + file_magwarn(ms, + "'/%c' only allowed on regex and search\n", + CHAR_REGEX_OFFSET_START); + return -1; + } + break; + case FILE_SEARCH: + if (m->str_range == 0) { + file_magwarn(ms, + "missing range; defaulting to %d\n", + STRING_DEFAULT_RANGE); + m->str_range = STRING_DEFAULT_RANGE; + return -1; + } + break; + case FILE_REGEX: + if ((m->str_flags & STRING_COMPACT_WHITESPACE) != 0) { + file_magwarn(ms, "'/%c' not allowed on regex\n", + CHAR_COMPACT_WHITESPACE); + return -1; + } + if ((m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) != 0) { + file_magwarn(ms, "'/%c' not allowed on regex\n", + CHAR_COMPACT_OPTIONAL_WHITESPACE); + return -1; + } + break; + default: + file_magwarn(ms, "coding error: m->type=%d\n", + m->type); + return -1; + } + return 0; +} + +private int +get_op(char c) +{ + switch (c) { + case '&': + return FILE_OPAND; + case '|': + return FILE_OPOR; + case '^': + return FILE_OPXOR; + case '+': + return FILE_OPADD; + case '-': + return FILE_OPMINUS; + case '*': + return FILE_OPMULTIPLY; + case '/': + return FILE_OPDIVIDE; + case '%': + return FILE_OPMODULO; + default: + return -1; + } +} + +#ifdef ENABLE_CONDITIONALS +private int +get_cond(const char *l, const char **t) +{ + static const struct cond_tbl_s { + char name[8]; + size_t len; + int cond; + } cond_tbl[] = { + { "if", 2, COND_IF }, + { "elif", 4, COND_ELIF }, + { "else", 4, COND_ELSE }, + { "", 0, COND_NONE }, + }; + const struct cond_tbl_s *p; + + for (p = cond_tbl; p->len; p++) { + if (strncmp(l, p->name, p->len) == 0 && + isspace((unsigned char)l[p->len])) { + if (t) + *t = l + p->len; + break; + } + } + return p->cond; +} + +private int +check_cond(struct magic_set *ms, int cond, uint32_t cont_level) +{ + int last_cond; + last_cond = ms->c.li[cont_level].last_cond; + + switch (cond) { + case COND_IF: + if (last_cond != COND_NONE && last_cond != COND_ELIF) { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "syntax error: `if'"); + return -1; + } + last_cond = COND_IF; + break; + + case COND_ELIF: + if (last_cond != COND_IF && last_cond != COND_ELIF) { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "syntax error: `elif'"); + return -1; + } + last_cond = COND_ELIF; + break; + + case COND_ELSE: + if (last_cond != COND_IF && last_cond != COND_ELIF) { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "syntax error: `else'"); + return -1; + } + last_cond = COND_NONE; + break; + + case COND_NONE: + last_cond = COND_NONE; + break; + } + + ms->c.li[cont_level].last_cond = last_cond; + return 0; +} +#endif /* ENABLE_CONDITIONALS */ + +/* + * parse one line from magic file, put into magic[index++] if valid + */ +private int +parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp, + const char *line, size_t lineno, int action) +{ +#ifdef ENABLE_CONDITIONALS + static uint32_t last_cont_level = 0; +#endif + size_t i; + struct magic_entry *me; + struct magic *m; + const char *l = line; + char *t; + int op; + uint32_t cont_level; + + cont_level = 0; + + while (*l == '>') { + ++l; /* step over */ + cont_level++; + } +#ifdef ENABLE_CONDITIONALS + if (cont_level == 0 || cont_level > last_cont_level) + if (file_check_mem(ms, cont_level) == -1) + return -1; + last_cont_level = cont_level; +#endif + +#define ALLOC_CHUNK (size_t)10 +#define ALLOC_INCR (size_t)200 + + if (cont_level != 0) { + if (*nmentryp == 0) { + file_error(ms, 0, "No current entry for continuation"); + return -1; + } + me = &(*mentryp)[*nmentryp - 1]; + if (me->mp == NULL) { + return -1; + } + if (me->cont_count == me->max_count) { + struct magic *nm; + size_t cnt = me->max_count + ALLOC_CHUNK; + nm = erealloc(me->mp, sizeof(*nm) * cnt); + me->mp = m = nm; + me->max_count = CAST(uint32_t, cnt); + } + m = &me->mp[me->cont_count++]; + (void)memset(m, 0, sizeof(*m)); + m->cont_level = cont_level; + } else { + if (*nmentryp == maxmagic) { + struct magic_entry *mp; + + maxmagic += ALLOC_INCR; + mp = erealloc(*mentryp, sizeof(*mp) * maxmagic); + (void)memset(&mp[*nmentryp], 0, sizeof(*mp) * ALLOC_INCR); + *mentryp = mp; + } + me = &(*mentryp)[*nmentryp]; + if (me->mp == NULL) { + m = safe_emalloc(sizeof(*m), ALLOC_CHUNK, 0); + me->mp = m; + me->max_count = ALLOC_CHUNK; + } else + m = me->mp; + (void)memset(m, 0, sizeof(*m)); + m->factor_op = FILE_FACTOR_OP_NONE; + m->cont_level = 0; + me->cont_count = 1; + } + m->lineno = CAST(uint32_t, lineno); + + if (*l == '&') { /* m->cont_level == 0 checked below. */ + ++l; /* step over */ + m->flag |= OFFADD; + } + if (*l == '(') { + ++l; /* step over */ + m->flag |= INDIR; + if (m->flag & OFFADD) + m->flag = (m->flag & ~OFFADD) | INDIROFFADD; + + if (*l == '&') { /* m->cont_level == 0 checked below */ + ++l; /* step over */ + m->flag |= OFFADD; + } + } + /* Indirect offsets are not valid at level 0. */ + if (m->cont_level == 0 && (m->flag & (OFFADD | INDIROFFADD))) + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "relative offset at level 0"); + + /* get offset, then skip over it */ + m->offset = (uint32_t)strtoul(l, &t, 0); + if (l == t) + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "offset `%s' invalid", l); + l = t; + + if (m->flag & INDIR) { + m->in_type = FILE_LONG; + m->in_offset = 0; + /* + * read [.lbs][+-]nnnnn) + */ + if (*l == '.') { + l++; + switch (*l) { + case 'l': + m->in_type = FILE_LELONG; + break; + case 'L': + m->in_type = FILE_BELONG; + break; + case 'm': + m->in_type = FILE_MELONG; + break; + case 'h': + case 's': + m->in_type = FILE_LESHORT; + break; + case 'H': + case 'S': + m->in_type = FILE_BESHORT; + break; + case 'c': + case 'b': + case 'C': + case 'B': + m->in_type = FILE_BYTE; + break; + case 'e': + case 'f': + case 'g': + m->in_type = FILE_LEDOUBLE; + break; + case 'E': + case 'F': + case 'G': + m->in_type = FILE_BEDOUBLE; + break; + case 'i': + m->in_type = FILE_LEID3; + break; + case 'I': + m->in_type = FILE_BEID3; + break; + default: + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, + "indirect offset type `%c' invalid", + *l); + break; + } + l++; + } + + m->in_op = 0; + if (*l == '~') { + m->in_op |= FILE_OPINVERSE; + l++; + } + if ((op = get_op(*l)) != -1) { + m->in_op |= op; + l++; + } + if (*l == '(') { + m->in_op |= FILE_OPINDIRECT; + l++; + } + if (isdigit((unsigned char)*l) || *l == '-') { + m->in_offset = (int32_t)strtol(l, &t, 0); + if (l == t) + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, + "in_offset `%s' invalid", l); + l = t; + } + if (*l++ != ')' || + ((m->in_op & FILE_OPINDIRECT) && *l++ != ')')) + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, + "missing ')' in indirect offset"); + } + EATAB; + +#ifdef ENABLE_CONDITIONALS + m->cond = get_cond(l, &l); + if (check_cond(ms, m->cond, cont_level) == -1) + return -1; + + EATAB; +#endif + + if (*l == 'u') { + ++l; + m->flag |= UNSIGNED; + } + + m->type = get_type(l, &l); + if (m->type == FILE_INVALID) { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "type `%s' invalid", l); + if (me->mp) { + efree(me->mp); + me->mp = NULL; + } + return -1; + } + + /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */ + /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */ + + m->mask_op = 0; + if (*l == '~') { + if (!IS_LIBMAGIC_STRING(m->type)) + m->mask_op |= FILE_OPINVERSE; + else if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "'~' invalid for string types"); + ++l; + } + m->str_range = 0; + m->str_flags = m->type == FILE_PSTRING ? PSTRING_1_LE : 0; + if ((op = get_op(*l)) != -1) { + if (!IS_LIBMAGIC_STRING(m->type)) { + uint64_t val; + ++l; + m->mask_op |= op; + val = (uint64_t)strtoull(l, &t, 0); + l = t; + m->num_mask = file_signextend(ms, m, val); + eatsize(&l); + } + else if (op == FILE_OPDIVIDE) { + int have_range = 0; + while (!isspace((unsigned char)*++l)) { + switch (*l) { + case '0': case '1': case '2': + case '3': case '4': case '5': + case '6': case '7': case '8': + case '9': + if (have_range && + (ms->flags & MAGIC_CHECK)) + file_magwarn(ms, + "multiple ranges"); + have_range = 1; + m->str_range = CAST(uint32_t, + strtoul(l, &t, 0)); + if (m->str_range == 0) + file_magwarn(ms, + "zero range"); + l = t - 1; + break; + case CHAR_COMPACT_WHITESPACE: + m->str_flags |= + STRING_COMPACT_WHITESPACE; + break; + case CHAR_COMPACT_OPTIONAL_WHITESPACE: + m->str_flags |= + STRING_COMPACT_OPTIONAL_WHITESPACE; + break; + case CHAR_IGNORE_LOWERCASE: + m->str_flags |= STRING_IGNORE_LOWERCASE; + break; + case CHAR_IGNORE_UPPERCASE: + m->str_flags |= STRING_IGNORE_UPPERCASE; + break; + case CHAR_REGEX_OFFSET_START: + m->str_flags |= REGEX_OFFSET_START; + break; + case CHAR_BINTEST: + m->str_flags |= STRING_BINTEST; + break; + case CHAR_TEXTTEST: + m->str_flags |= STRING_TEXTTEST; + break; + case CHAR_PSTRING_1_LE: + if (m->type != FILE_PSTRING) + goto bad; + m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_1_LE; + break; + case CHAR_PSTRING_2_BE: + if (m->type != FILE_PSTRING) + goto bad; + m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_BE; + break; + case CHAR_PSTRING_2_LE: + if (m->type != FILE_PSTRING) + goto bad; + m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_2_LE; + break; + case CHAR_PSTRING_4_BE: + if (m->type != FILE_PSTRING) + goto bad; + m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_BE; + break; + case CHAR_PSTRING_4_LE: + if (m->type != FILE_PSTRING) + goto bad; + m->str_flags = (m->str_flags & ~PSTRING_LEN) | PSTRING_4_LE; + break; + case CHAR_PSTRING_LENGTH_INCLUDES_ITSELF: + if (m->type != FILE_PSTRING) + goto bad; + m->str_flags |= PSTRING_LENGTH_INCLUDES_ITSELF; + break; + default: + bad: + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, + "string extension `%c' " + "invalid", *l); + return -1; + } + /* allow multiple '/' for readability */ + if (l[1] == '/' && + !isspace((unsigned char)l[2])) + l++; + } + if (string_modifier_check(ms, m) == -1) + return -1; + } + else { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "invalid string op: %c", *t); + return -1; + } + } + /* + * We used to set mask to all 1's here, instead let's just not do + * anything if mask = 0 (unless you have a better idea) + */ + EATAB; + + switch (*l) { + case '>': + case '<': + m->reln = *l; + ++l; + if (*l == '=') { + if (ms->flags & MAGIC_CHECK) { + file_magwarn(ms, "%c= not supported", + m->reln); + return -1; + } + ++l; + } + break; + /* Old-style anding: "0 byte &0x80 dynamically linked" */ + case '&': + case '^': + case '=': + m->reln = *l; + ++l; + if (*l == '=') { + /* HP compat: ignore &= etc. */ + ++l; + } + break; + case '!': + m->reln = *l; + ++l; + break; + default: + m->reln = '='; /* the default relation */ + if (*l == 'x' && ((isascii((unsigned char)l[1]) && + isspace((unsigned char)l[1])) || !l[1])) { + m->reln = *l; + ++l; + } + break; + } + /* + * Grab the value part, except for an 'x' reln. + */ + if (m->reln != 'x' && getvalue(ms, m, &l, action)) + return -1; + + /* + * TODO finish this macro and start using it! + * #define offsetcheck {if (offset > HOWMANY-1) + * magwarn("offset too big"); } + */ + + /* + * Now get last part - the description + */ + EATAB; + if (l[0] == '\b') { + ++l; + m->flag |= NOSPACE; + } else if ((l[0] == '\\') && (l[1] == 'b')) { + ++l; + ++l; + m->flag |= NOSPACE; + } + for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); ) + continue; + if (i == sizeof(m->desc)) { + m->desc[sizeof(m->desc) - 1] = '\0'; + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "description `%s' truncated", m->desc); + } + + /* + * We only do this check while compiling, or if any of the magic + * files were not compiled. + */ + if (ms->flags & MAGIC_CHECK) { + if (check_format(ms, m) == -1) + return -1; + } + m->mimetype[0] = '\0'; /* initialise MIME type to none */ + if (m->cont_level == 0) + ++(*nmentryp); /* make room for next */ + return 0; +} + +/* + * parse a STRENGTH annotation line from magic file, put into magic[index - 1] + * if valid + */ +private int +parse_strength(struct magic_set *ms, struct magic_entry *me, const char *line) +{ + const char *l = line; + char *el; + unsigned long factor; + struct magic *m = &me->mp[0]; + + if (m->factor_op != FILE_FACTOR_OP_NONE) { + file_magwarn(ms, + "Current entry already has a strength type: %c %d", + m->factor_op, m->factor); + return -1; + } + EATAB; + switch (*l) { + case FILE_FACTOR_OP_NONE: + case FILE_FACTOR_OP_PLUS: + case FILE_FACTOR_OP_MINUS: + case FILE_FACTOR_OP_TIMES: + case FILE_FACTOR_OP_DIV: + m->factor_op = *l++; + break; + default: + file_magwarn(ms, "Unknown factor op `%c'", *l); + return -1; + } + EATAB; + factor = strtoul(l, &el, 0); + if (factor > 255) { + file_magwarn(ms, "Too large factor `%lu'", factor); + goto out; + } + if (*el && !isspace((unsigned char)*el)) { + file_magwarn(ms, "Bad factor `%s'", l); + goto out; + } + m->factor = (uint8_t)factor; + if (m->factor == 0 && m->factor_op == FILE_FACTOR_OP_DIV) { + file_magwarn(ms, "Cannot have factor op `%c' and factor %u", + m->factor_op, m->factor); + goto out; + } + return 0; +out: + m->factor_op = FILE_FACTOR_OP_NONE; + m->factor = 0; + return -1; +} + +/* + * Parse an Apple CREATOR/TYPE annotation from magic file and put it into + * magic[index - 1] + */ +private int +parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) +{ + size_t i; + const char *l = line; + struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; + + if (m->apple[0] != '\0') { + file_magwarn(ms, "Current entry already has a APPLE type " + "`%.8s', new type `%s'", m->mimetype, l); + return -1; + } + + EATAB; + for (i = 0; *l && ((isascii((unsigned char)*l) && + isalnum((unsigned char)*l)) || strchr("-+/.", *l)) && + i < sizeof(m->apple); m->apple[i++] = *l++) + continue; + if (i == sizeof(m->apple) && *l) { + /* We don't need to NUL terminate here, printing handles it */ + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "APPLE type `%s' truncated %" + SIZE_T_FORMAT "u", line, i); + } + + if (i > 0) + return 0; + else + return -1; +} + +/* + * parse a MIME annotation line from magic file, put into magic[index - 1] + * if valid + */ +private int +parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line) +{ + size_t i; + const char *l = line; + struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; + + if (m->mimetype[0] != '\0') { + file_magwarn(ms, "Current entry already has a MIME type `%s'," + " new type `%s'", m->mimetype, l); + return -1; + } + + EATAB; + for (i = 0; *l && ((isascii((unsigned char)*l) && + isalnum((unsigned char)*l)) || strchr("-+/.", *l)) && + i < sizeof(m->mimetype); m->mimetype[i++] = *l++) + continue; + if (i == sizeof(m->mimetype)) { + m->mimetype[sizeof(m->mimetype) - 1] = '\0'; + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "MIME type `%s' truncated %" + SIZE_T_FORMAT "u", m->mimetype, i); + } else + m->mimetype[i] = '\0'; + + if (i > 0) + return 0; + else + return -1; +} + +private int +check_format_type(const char *ptr, int type) +{ + int quad = 0; + if (*ptr == '\0') { + /* Missing format string; bad */ + return -1; + } + + switch (type) { + case FILE_FMT_QUAD: + quad = 1; + /*FALLTHROUGH*/ + case FILE_FMT_NUM: + if (*ptr == '-') + ptr++; + if (*ptr == '.') + ptr++; + while (isdigit((unsigned char)*ptr)) ptr++; + if (*ptr == '.') + ptr++; + while (isdigit((unsigned char)*ptr)) ptr++; + if (quad) { + if (*ptr++ != 'l') + return -1; + if (*ptr++ != 'l') + return -1; + } + + switch (*ptr++) { + case 'l': + switch (*ptr++) { + case 'i': + case 'd': + case 'u': + case 'x': + case 'X': + return 0; + default: + return -1; + } + + case 'h': + switch (*ptr++) { + case 'h': + switch (*ptr++) { + case 'i': + case 'd': + case 'u': + case 'x': + case 'X': + return 0; + default: + return -1; + } + case 'd': + return 0; + default: + return -1; + } + + case 'i': + case 'c': + case 'd': + case 'u': + case 'x': + case 'X': + return 0; + + default: + return -1; + } + + case FILE_FMT_FLOAT: + case FILE_FMT_DOUBLE: + if (*ptr == '-') + ptr++; + if (*ptr == '.') + ptr++; + while (isdigit((unsigned char)*ptr)) ptr++; + if (*ptr == '.') + ptr++; + while (isdigit((unsigned char)*ptr)) ptr++; + + switch (*ptr++) { + case 'e': + case 'E': + case 'f': + case 'F': + case 'g': + case 'G': + return 0; + + default: + return -1; + } + + + case FILE_FMT_STR: + if (*ptr == '-') + ptr++; + while (isdigit((unsigned char )*ptr)) + ptr++; + if (*ptr == '.') { + ptr++; + while (isdigit((unsigned char )*ptr)) + ptr++; + } + + switch (*ptr++) { + case 's': + return 0; + default: + return -1; + } + + default: + /* internal error */ + abort(); + } + /*NOTREACHED*/ + return -1; +} + +/* + * Check that the optional printf format in description matches + * the type of the magic. + */ +private int +check_format(struct magic_set *ms, struct magic *m) +{ + char *ptr; + + for (ptr = m->desc; *ptr; ptr++) + if (*ptr == '%') + break; + if (*ptr == '\0') { + /* No format string; ok */ + return 1; + } + + assert(file_nformats == file_nnames); + + if (m->type >= file_nformats) { + file_magwarn(ms, "Internal error inconsistency between " + "m->type and format strings"); + return -1; + } + if (file_formats[m->type] == FILE_FMT_NONE) { + file_magwarn(ms, "No format string for `%s' with description " + "`%s'", m->desc, file_names[m->type]); + return -1; + } + + ptr++; + if (check_format_type(ptr, file_formats[m->type]) == -1) { + /* + * TODO: this error message is unhelpful if the format + * string is not one character long + */ + file_magwarn(ms, "Printf format `%c' is not valid for type " + "`%s' in description `%s'", *ptr ? *ptr : '?', + file_names[m->type], m->desc); + return -1; + } + + for (; *ptr; ptr++) { + if (*ptr == '%') { + file_magwarn(ms, + "Too many format strings (should have at most one) " + "for `%s' with description `%s'", + file_names[m->type], m->desc); + return -1; + } + } + return 0; +} + +/* + * Read a numeric value from a pointer, into the value union of a magic + * pointer, according to the magic type. Update the string pointer to point + * just after the number read. Return 0 for success, non-zero for failure. + */ +private int +getvalue(struct magic_set *ms, struct magic *m, const char **p, int action) +{ + switch (m->type) { + case FILE_BESTRING16: + case FILE_LESTRING16: + case FILE_STRING: + case FILE_PSTRING: + case FILE_REGEX: + case FILE_SEARCH: + *p = getstr(ms, m, *p, action == FILE_COMPILE); + if (*p == NULL) { + if (ms->flags & MAGIC_CHECK) + file_magwarn(ms, "cannot get string from `%s'", + m->value.s); + return -1; + } + return 0; + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + if (m->reln != 'x') { + char *ep; +#ifdef HAVE_STRTOF + m->value.f = strtof(*p, &ep); +#else + m->value.f = (float)strtod(*p, &ep); +#endif + *p = ep; + } + return 0; + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + if (m->reln != 'x') { + char *ep; + m->value.d = strtod(*p, &ep); + *p = ep; + } + return 0; + default: + if (m->reln != 'x') { + char *ep; + m->value.q = file_signextend(ms, m, + (uint64_t)strtoull(*p, &ep, 0)); + *p = ep; + eatsize(p); + } + return 0; + } +} + +/* + * Convert a string containing C character escapes. Stop at an unescaped + * space or tab. + * Copy the converted version to "m->value.s", and the length in m->vallen. + * Return updated scan pointer as function result. Warn if set. + */ +private const char * +getstr(struct magic_set *ms, struct magic *m, const char *s, int warn) +{ + const char *origs = s; + char *p = m->value.s; + size_t plen = sizeof(m->value.s); + char *origp = p; + char *pmax = p + plen - 1; + int c; + int val; + + while ((c = *s++) != '\0') { + if (isspace((unsigned char) c)) + break; + if (p >= pmax) { + file_error(ms, 0, "string too long: `%s'", origs); + return NULL; + } + if (c == '\\') { + switch(c = *s++) { + + case '\0': + if (warn) + file_magwarn(ms, "incomplete escape"); + goto out; + + case '\t': + if (warn) { + file_magwarn(ms, + "escaped tab found, use \\t instead"); + warn = 0; /* already did */ + } + /*FALLTHROUGH*/ + default: + if (warn) { + if (isprint((unsigned char)c)) { + /* Allow escaping of + * ``relations'' */ + if (strchr("<>&^=!", c) == NULL + && (m->type != FILE_REGEX || + strchr("[]().*?^$|{}", c) + == NULL)) { + file_magwarn(ms, "no " + "need to escape " + "`%c'", c); + } + } else { + file_magwarn(ms, + "unknown escape sequence: " + "\\%03o", c); + } + } + /*FALLTHROUGH*/ + /* space, perhaps force people to use \040? */ + case ' ': +#if 0 + /* + * Other things people escape, but shouldn't need to, + * so we disallow them + */ + case '\'': + case '"': + case '?': +#endif + /* Relations */ + case '>': + case '<': + case '&': + case '^': + case '=': + case '!': + /* and baskslash itself */ + case '\\': + *p++ = (char) c; + break; + + case 'a': + *p++ = '\a'; + break; + + case 'b': + *p++ = '\b'; + break; + + case 'f': + *p++ = '\f'; + break; + + case 'n': + *p++ = '\n'; + break; + + case 'r': + *p++ = '\r'; + break; + + case 't': + *p++ = '\t'; + break; + + case 'v': + *p++ = '\v'; + break; + + /* \ and up to 3 octal digits */ + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + val = c - '0'; + c = *s++; /* try for 2 */ + if (c >= '0' && c <= '7') { + val = (val << 3) | (c - '0'); + c = *s++; /* try for 3 */ + if (c >= '0' && c <= '7') + val = (val << 3) | (c-'0'); + else + --s; + } + else + --s; + *p++ = (char)val; + break; + + /* \x and up to 2 hex digits */ + case 'x': + val = 'x'; /* Default if no digits */ + c = hextoint(*s++); /* Get next char */ + if (c >= 0) { + val = c; + c = hextoint(*s++); + if (c >= 0) + val = (val << 4) + c; + else + --s; + } else + --s; + *p++ = (char)val; + break; + } + } else + *p++ = (char)c; + } +out: + *p = '\0'; + m->vallen = CAST(unsigned char, (p - origp)); + if (m->type == FILE_PSTRING) + m->vallen += (unsigned char)file_pstring_length_size(m); + return s; +} + + +/* Single hex char to int; -1 if not a hex char. */ +private int +hextoint(int c) +{ + if (!isascii((unsigned char) c)) + return -1; + if (isdigit((unsigned char) c)) + return c - '0'; + if ((c >= 'a') && (c <= 'f')) + return c + 10 - 'a'; + if (( c>= 'A') && (c <= 'F')) + return c + 10 - 'A'; + return -1; +} + + +/* + * Print a string containing C character escapes. + */ +protected void +file_showstr(FILE *fp, const char *s, size_t len) +{ + char c; + + for (;;) { + if (len == ~0U) { + c = *s++; + if (c == '\0') + break; + } + else { + if (len-- == 0) + break; + c = *s++; + } + if (c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */ + (void) fputc(c, fp); + else { + (void) fputc('\\', fp); + switch (c) { + case '\a': + (void) fputc('a', fp); + break; + + case '\b': + (void) fputc('b', fp); + break; + + case '\f': + (void) fputc('f', fp); + break; + + case '\n': + (void) fputc('n', fp); + break; + + case '\r': + (void) fputc('r', fp); + break; + + case '\t': + (void) fputc('t', fp); + break; + + case '\v': + (void) fputc('v', fp); + break; + + default: + (void) fprintf(fp, "%.3o", c & 0377); + break; + } + } + } +} + +/* + * eatsize(): Eat the size spec from a number [eg. 10UL] + */ +private void +eatsize(const char **p) +{ + const char *l = *p; + + if (LOWCASE(*l) == 'u') + l++; + + switch (LOWCASE(*l)) { + case 'l': /* long */ + case 's': /* short */ + case 'h': /* short */ + case 'b': /* char/byte */ + case 'c': /* char/byte */ + l++; + /*FALLTHROUGH*/ + default: + break; + } + + *p = l; +} + +/* + * handle a compiled file. + * return -1 = error + * return 1 = memory structure you can free + * return 3 = bundled library from PHP + */ +private int +apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, + const char *fn) +{ + uint32_t *ptr; + uint32_t version; + int needsbyteswap; + char *dbname = NULL; + void *mm = NULL; + int ret = 0; + php_stream *stream = NULL; + php_stream_statbuf st; + + + TSRMLS_FETCH(); + + if (fn == NULL) { + mm = (void *)&php_magic_database; + ret = 3; + goto internal_loaded; + } + +#ifdef PHP_WIN32 + /* Don't bother on windows with php_stream_open_wrapper, + return to give apprentice_load() a chance. */ + if (php_stream_stat_path_ex(fn, 0, &st, NULL) == SUCCESS) { + if (st.sb.st_mode & S_IFDIR) { + goto error2; + } + } +#endif + + dbname = mkdbname(ms, fn, 0); + if (dbname == NULL) + goto error2; + +#if PHP_API_VERSION < 20100412 + stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); +#else + stream = php_stream_open_wrapper((char *)fn, "rb", REPORT_ERRORS, NULL); +#endif + + if (!stream) { + goto error2; + } + + if (php_stream_stat(stream, &st) < 0) { + file_error(ms, errno, "cannot stat `%s'", dbname); + goto error1; + } + + if (st.sb.st_size < 8) { + file_error(ms, 0, "file `%s' is too small", dbname); + goto error1; + } + + mm = emalloc((size_t)st.sb.st_size); + if (php_stream_read(stream, mm, (size_t)st.sb.st_size) != (size_t)st.sb.st_size) { + file_badread(ms); + ret = 1; + goto error1; + } + ret = 1; + + php_stream_close(stream); + stream = NULL; + +internal_loaded: + *magicp = mm; + ptr = (uint32_t *)(void *)*magicp; + if (*ptr != MAGICNO) { + if (swap4(*ptr) != MAGICNO) { + file_error(ms, 0, "bad magic in `%s'", dbname); + goto error1; + } + needsbyteswap = 1; + } else + needsbyteswap = 0; + if (needsbyteswap) + version = swap4(ptr[1]); + else + version = ptr[1]; + if (version != VERSIONNO) { + file_error(ms, 0, "File %d.%d supports only version %d magic " + "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel, + VERSIONNO, dbname, version); + goto error1; + } + + /* php_magic_database is a const, performing writes will segfault. This is for big-endian + machines only, PPC and Sparc specifically. Consider static variable or MINIT in + future. */ + if (needsbyteswap && fn == NULL) { + mm = emalloc(sizeof(php_magic_database)); + mm = memcpy(mm, php_magic_database, sizeof(php_magic_database)); + *magicp = mm; + ret = 1; + } + + if (fn == NULL) { + *nmagicp = (sizeof(php_magic_database) / sizeof(struct magic)); + } else { + *nmagicp = (uint32_t)(st.sb.st_size / sizeof(struct magic)); + } + if (*nmagicp > 0) { + (*nmagicp)--; + } + (*magicp)++; + if (needsbyteswap) { + byteswap(*magicp, *nmagicp); + } + + if (dbname) { + efree(dbname); + } + return ret; + +error1: + if (stream) { + php_stream_close(stream); + } + + if (mm && ret == 1) { + efree(mm); + } else { + *magicp = NULL; + *nmagicp = 0; + } +error2: + if (dbname) { + efree(dbname); + } + return -1; +} + +private const uint32_t ar[] = { + MAGICNO, VERSIONNO +}; +/* + * handle an mmaped file. + */ +private int +apprentice_compile(struct magic_set *ms, struct magic **magicp, + uint32_t *nmagicp, const char *fn) +{ + char *dbname; + int rv = -1; + php_stream *stream; + + TSRMLS_FETCH(); + + dbname = mkdbname(ms, fn, 0); + + if (dbname == NULL) + goto out; + +/* wb+ == O_WRONLY|O_CREAT|O_TRUNC|O_BINARY */ +#if PHP_API_VERSION < 20100412 + stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); +#else + stream = php_stream_open_wrapper((char *)fn, "wb+", REPORT_ERRORS, NULL); +#endif + + if (!stream) { + file_error(ms, errno, "cannot open `%s'", dbname); + goto out; + } + + if (php_stream_write(stream, (char *)ar, sizeof(ar)) != (ssize_t)sizeof(ar)) { + file_error(ms, errno, "error writing `%s'", dbname); + goto out; + } + + if (php_stream_seek(stream,(off_t)sizeof(struct magic), SEEK_SET) != sizeof(struct magic)) { + file_error(ms, errno, "error seeking `%s'", dbname); + goto out; + } + + if (php_stream_write(stream, (char *)*magicp, (sizeof(struct magic) * *nmagicp) != (ssize_t)(sizeof(struct magic) * *nmagicp))) { + file_error(ms, errno, "error writing `%s'", dbname); + goto out; + } + + php_stream_close(stream); + + rv = 0; +out: + efree(dbname); + return rv; +} + +private const char ext[] = ".mgc"; +/* + * make a dbname + */ +private char * +mkdbname(struct magic_set *ms, const char *fn, int strip) +{ + const char *p, *q; + char *buf; + TSRMLS_FETCH(); + + if (strip) { + if ((p = strrchr(fn, '/')) != NULL) + fn = ++p; + } + + for (q = fn; *q; q++) + continue; + /* Look for .mgc */ + for (p = ext + sizeof(ext) - 1; p >= ext && q >= fn; p--, q--) + if (*p != *q) + break; + + /* Did not find .mgc, restore q */ + if (p >= ext) + while (*q) + q++; + + q++; + /* Compatibility with old code that looked in .mime */ + if (ms->flags & MAGIC_MIME) { + spprintf(&buf, MAXPATHLEN, "%.*s.mime%s", (int)(q - fn), fn, ext); +#ifdef PHP_WIN32 + if (VCWD_ACCESS(buf, R_OK) == 0) { +#else + if (VCWD_ACCESS(buf, R_OK) != -1) { +#endif + ms->flags &= MAGIC_MIME_TYPE; + return buf; + } + efree(buf); + } + spprintf(&buf, MAXPATHLEN, "%.*s%s", (int)(q - fn), fn, ext); + + /* Compatibility with old code that looked in .mime */ + if (strstr(p, ".mime") != NULL) + ms->flags &= MAGIC_MIME_TYPE; + return buf; +} + +/* + * Byteswap an mmap'ed file if needed + */ +private void +byteswap(struct magic *magic, uint32_t nmagic) +{ + uint32_t i; + for (i = 0; i < nmagic; i++) + bs1(&magic[i]); +} + +/* + * swap a short + */ +private uint16_t +swap2(uint16_t sv) +{ + uint16_t rv; + uint8_t *s = (uint8_t *)(void *)&sv; + uint8_t *d = (uint8_t *)(void *)&rv; + d[0] = s[1]; + d[1] = s[0]; + return rv; +} + +/* + * swap an int + */ +private uint32_t +swap4(uint32_t sv) +{ + uint32_t rv; + uint8_t *s = (uint8_t *)(void *)&sv; + uint8_t *d = (uint8_t *)(void *)&rv; + d[0] = s[3]; + d[1] = s[2]; + d[2] = s[1]; + d[3] = s[0]; + return rv; +} + +/* + * swap a quad + */ +private uint64_t +swap8(uint64_t sv) +{ + uint64_t rv; + uint8_t *s = (uint8_t *)(void *)&sv; + uint8_t *d = (uint8_t *)(void *)&rv; +#if 0 + d[0] = s[3]; + d[1] = s[2]; + d[2] = s[1]; + d[3] = s[0]; + d[4] = s[7]; + d[5] = s[6]; + d[6] = s[5]; + d[7] = s[4]; +#else + d[0] = s[7]; + d[1] = s[6]; + d[2] = s[5]; + d[3] = s[4]; + d[4] = s[3]; + d[5] = s[2]; + d[6] = s[1]; + d[7] = s[0]; +#endif + return rv; +} + +/* + * byteswap a single magic entry + */ +private void +bs1(struct magic *m) +{ + m->cont_level = swap2(m->cont_level); + m->offset = swap4((uint32_t)m->offset); + m->in_offset = swap4((uint32_t)m->in_offset); + m->lineno = swap4((uint32_t)m->lineno); + if (IS_LIBMAGIC_STRING(m->type)) { + m->str_range = swap4(m->str_range); + m->str_flags = swap4(m->str_flags); + } + else { + m->value.q = swap8(m->value.q); + m->num_mask = swap8(m->num_mask); + } +} + +protected size_t +file_pstring_length_size(const struct magic *m) +{ + switch (m->str_flags & PSTRING_LEN) { + case PSTRING_1_LE: + return 1; + case PSTRING_2_LE: + case PSTRING_2_BE: + return 2; + case PSTRING_4_LE: + case PSTRING_4_BE: + return 4; + default: + abort(); /* Impossible */ + return 1; + } +} +protected size_t +file_pstring_get_length(const struct magic *m, const char *s) +{ + size_t len = 0; + + switch (m->str_flags & PSTRING_LEN) { + case PSTRING_1_LE: + len = *s; + break; + case PSTRING_2_LE: + len = (s[1] << 8) | s[0]; + break; + case PSTRING_2_BE: + len = (s[0] << 8) | s[1]; + break; + case PSTRING_4_LE: + len = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0]; + break; + case PSTRING_4_BE: + len = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; + break; + default: + abort(); /* Impossible */ + } + + if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF) + len -= file_pstring_length_size(m); + + return len; +} diff --git a/ext/fileinfo/libmagic/apptype.c b/ext/fileinfo/libmagic/apptype.c new file mode 100644 index 0000000..fcd764e --- /dev/null +++ b/ext/fileinfo/libmagic/apptype.c @@ -0,0 +1,169 @@ +/* + * Adapted from: apptype.c, Written by Eberhard Mattes and put into the + * public domain + * + * Notes: 1. Qualify the filename so that DosQueryAppType does not do extraneous + * searches. + * + * 2. DosQueryAppType will return FAPPTYP_DOS on a file ending with ".com" + * (other than an OS/2 exe or Win exe with this name). Eberhard Mattes + * remarks Tue, 6 Apr 93: Moreover, it reports the type of the (new and very + * bug ridden) Win Emacs as "OS/2 executable". + * + * 3. apptype() uses the filename if given, otherwise a tmp file is created with + * the contents of buf. If buf is not the complete file, apptype can + * incorrectly identify the exe type. The "-z" option of "file" is the reason + * for this ugly code. + */ + +/* + * amai: Darrel Hankerson did the changes described here. + * + * It remains to check the validity of comments (2.) since it's referred to an + * "old" OS/2 version. + * + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: apptype.c,v 1.13 2011/09/07 21:57:15 christos Exp $") +#endif /* lint */ + +#include <stdlib.h> +#include <string.h> + +#ifdef __EMX__ +#include <io.h> +#define INCL_DOSSESMGR +#define INCL_DOSERRORS +#define INCL_DOSFILEMGR +#include <os2.h> +typedef ULONG APPTYPE; + +protected int +file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf, + size_t nb) +{ + APPTYPE rc, type; + char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR], + fname[_MAX_FNAME], ext[_MAX_EXT]; + char *filename; + FILE *fp; + + if (fn) + filename = strdup(fn); + else if ((filename = tempnam("./", "tmp")) == NULL) { + file_error(ms, errno, "cannot create tempnam"); + return -1; + } + /* qualify the filename to prevent extraneous searches */ + _splitpath(filename, drive, dir, fname, ext); + (void)sprintf(path, "%s%s%s%s", drive, + (*dir == '\0') ? "./" : dir, + fname, + (*ext == '\0') ? "." : ext); + + if (fn == NULL) { + if ((fp = fopen(path, "wb")) == NULL) { + file_error(ms, errno, "cannot open tmp file `%s'", path); + return -1; + } + if (fwrite(buf, 1, nb, fp) != nb) { + file_error(ms, errno, "cannot write tmp file `%s'", + path); + (void)fclose(fp); + return -1; + } + (void)fclose(fp); + } + rc = DosQueryAppType((unsigned char *)path, &type); + + if (fn == NULL) { + unlink(path); + free(filename); + } +#if 0 + if (rc == ERROR_INVALID_EXE_SIGNATURE) + printf("%s: not an executable file\n", fname); + else if (rc == ERROR_FILE_NOT_FOUND) + printf("%s: not found\n", fname); + else if (rc == ERROR_ACCESS_DENIED) + printf("%s: access denied\n", fname); + else if (rc != 0) + printf("%s: error code = %lu\n", fname, rc); + else +#else + + /* + * for our purpose here it's sufficient to just ignore the error and + * return w/o success (=0) + */ + + if (rc) + return (0); + +#endif + + if (type & FAPPTYP_32BIT) + if (file_printf(ms, "32-bit ") == -1) + return -1; + if (type & FAPPTYP_PHYSDRV) { + if (file_printf(ms, "physical device driver") == -1) + return -1; + } else if (type & FAPPTYP_VIRTDRV) { + if (file_printf(ms, "virtual device driver") == -1) + return -1; + } else if (type & FAPPTYP_DLL) { + if (type & FAPPTYP_PROTDLL) + if (file_printf(ms, "protected ") == -1) + return -1; + if (file_printf(ms, "DLL") == -1) + return -1; + } else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) { + if (file_printf(ms, "Windows executable") == -1) + return -1; + } else if (type & FAPPTYP_DOS) { + /* + * The API routine is partially broken on filenames ending + * ".com". + */ + if (stricmp(ext, ".com") == 0) + if (strncmp((const char *)buf, "MZ", 2)) + return (0); + if (file_printf(ms, "DOS executable") == -1) + return -1; + /* ---------------------------------------- */ + /* Might learn more from the magic(4) entry */ + if (file_printf(ms, ", magic(4)-> ") == -1) + return -1; + return (0); + /* ---------------------------------------- */ + } else if (type & FAPPTYP_BOUND) { + if (file_printf(ms, "bound executable") == -1) + return -1; + } else if ((type & 7) == FAPPTYP_WINDOWAPI) { + if (file_printf(ms, "PM executable") == -1) + return -1; + } else if (file_printf(ms, "OS/2 executable") == -1) + return -1; + + switch (type & (FAPPTYP_NOTWINDOWCOMPAT | + FAPPTYP_WINDOWCOMPAT | + FAPPTYP_WINDOWAPI)) { + case FAPPTYP_NOTWINDOWCOMPAT: + if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1) + return -1; + break; + case FAPPTYP_WINDOWCOMPAT: + if (file_printf(ms, " [WINDOWCOMPAT]") == -1) + return -1; + break; + case FAPPTYP_WINDOWAPI: + if (file_printf(ms, " [WINDOWAPI]") == -1) + return -1; + break; + } + return 1; +} +#endif diff --git a/ext/fileinfo/libmagic/ascmagic.c b/ext/fileinfo/libmagic/ascmagic.c new file mode 100644 index 0000000..9d8d150 --- /dev/null +++ b/ext/fileinfo/libmagic/ascmagic.c @@ -0,0 +1,358 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * ASCII magic -- try to detect text encoding. + * + * Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000, + * to handle character codes other than ASCII on a unified basis. + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: ascmagic.c,v 1.84 2011/12/08 12:38:24 rrt Exp $") +#endif /* lint */ + +#include "magic.h" +#include <string.h> +#include <memory.h> +#include <ctype.h> +#include <stdlib.h> +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif + +#define MAXLINELEN 300 /* longest sane line length */ +#define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \ + || (x) == 0x85 || (x) == '\f') + +private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t); +private size_t trim_nuls(const unsigned char *, size_t); + +/* + * Undo the NUL-termination kindly provided by process() + * but leave at least one byte to look at + */ +private size_t +trim_nuls(const unsigned char *buf, size_t nbytes) +{ + while (nbytes > 1 && buf[nbytes - 1] == '\0') + nbytes--; + + return nbytes; +} + +protected int +file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, + int text) +{ + unichar *ubuf = NULL; + size_t ulen; + int rv = 1; + + const char *code = NULL; + const char *code_mime = NULL; + const char *type = NULL; + + if (ms->flags & MAGIC_APPLE) + return 0; + + nbytes = trim_nuls(buf, nbytes); + + /* If file doesn't look like any sort of text, give up. */ + if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime, + &type) == 0) + rv = 0; + else + rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code, + type, text); + + free(ubuf); + + return rv; +} + +protected int +file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf, + size_t nbytes, unichar *ubuf, size_t ulen, const char *code, + const char *type, int text) +{ + unsigned char *utf8_buf = NULL, *utf8_end; + size_t mlen, i; + int rv = -1; + int mime = ms->flags & MAGIC_MIME; + + const char *subtype = NULL; + const char *subtype_mime = NULL; + + int has_escapes = 0; + int has_backspace = 0; + int seen_cr = 0; + + int n_crlf = 0; + int n_lf = 0; + int n_cr = 0; + int n_nel = 0; + int executable = 0; + + size_t last_line_end = (size_t)-1; + int has_long_lines = 0; + + if (ms->flags & MAGIC_APPLE) + return 0; + + nbytes = trim_nuls(buf, nbytes); + + /* If we have fewer than 2 bytes, give up. */ + if (nbytes <= 1) { + rv = 0; + goto done; + } + + if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { + /* Convert ubuf to UTF-8 and try text soft magic */ + /* malloc size is a conservative overestimate; could be + improved, or at least realloced after conversion. */ + mlen = ulen * 6; + utf8_buf = emalloc(mlen); + + if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) + == NULL) + goto done; + if ((rv = file_softmagic(ms, utf8_buf, + (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0) + rv = -1; + } + + /* Now try to discover other details about the file. */ + for (i = 0; i < ulen; i++) { + if (ubuf[i] == '\n') { + if (seen_cr) + n_crlf++; + else + n_lf++; + last_line_end = i; + } else if (seen_cr) + n_cr++; + + seen_cr = (ubuf[i] == '\r'); + if (seen_cr) + last_line_end = i; + + if (ubuf[i] == 0x85) { /* X3.64/ECMA-43 "next line" character */ + n_nel++; + last_line_end = i; + } + + /* If this line is _longer_ than MAXLINELEN, remember it. */ + if (i > last_line_end + MAXLINELEN) + has_long_lines = 1; + + if (ubuf[i] == '\033') + has_escapes = 1; + if (ubuf[i] == '\b') + has_backspace = 1; + } + + /* Beware, if the data has been truncated, the final CR could have + been followed by a LF. If we have HOWMANY bytes, it indicates + that the data might have been truncated, probably even before + this function was called. */ + if (seen_cr && nbytes < HOWMANY) + n_cr++; + + if (strcmp(type, "binary") == 0) { + rv = 0; + goto done; + } + if (mime) { + if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) { + if (subtype_mime) { + if (file_printf(ms, "%s", subtype_mime) == -1) + goto done; + } else { + if (file_printf(ms, "text/plain") == -1) + goto done; + } + } + } else { + if (file_printedlen(ms)) { + switch (file_replace(ms, " text$", ", ")) { + case 0: + switch (file_replace(ms, " text executable$", + ", ")) { + case 0: + if (file_printf(ms, ", ") == -1) + goto done; + case -1: + goto done; + default: + executable = 1; + break; + } + break; + case -1: + goto done; + default: + break; + } + } + + if (file_printf(ms, "%s", code) == -1) + goto done; + + if (subtype) { + if (file_printf(ms, " %s", subtype) == -1) + goto done; + } + + if (file_printf(ms, " %s", type) == -1) + goto done; + + if (executable) + if (file_printf(ms, " executable") == -1) + goto done; + + if (has_long_lines) + if (file_printf(ms, ", with very long lines") == -1) + goto done; + + /* + * Only report line terminators if we find one other than LF, + * or if we find none at all. + */ + if ((n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) || + (n_crlf != 0 || n_cr != 0 || n_nel != 0)) { + if (file_printf(ms, ", with") == -1) + goto done; + + if (n_crlf == 0 && n_cr == 0 && n_nel == 0 && n_lf == 0) { + if (file_printf(ms, " no") == -1) + goto done; + } else { + if (n_crlf) { + if (file_printf(ms, " CRLF") == -1) + goto done; + if (n_cr || n_lf || n_nel) + if (file_printf(ms, ",") == -1) + goto done; + } + if (n_cr) { + if (file_printf(ms, " CR") == -1) + goto done; + if (n_lf || n_nel) + if (file_printf(ms, ",") == -1) + goto done; + } + if (n_lf) { + if (file_printf(ms, " LF") == -1) + goto done; + if (n_nel) + if (file_printf(ms, ",") == -1) + goto done; + } + if (n_nel) + if (file_printf(ms, " NEL") == -1) + goto done; + } + + if (file_printf(ms, " line terminators") == -1) + goto done; + } + + if (has_escapes) + if (file_printf(ms, ", with escape sequences") == -1) + goto done; + if (has_backspace) + if (file_printf(ms, ", with overstriking") == -1) + goto done; + } + rv = 1; +done: + if (utf8_buf) + efree(utf8_buf); + + return rv; +} + +/* + * Encode Unicode string as UTF-8, returning pointer to character + * after end of string, or NULL if an invalid character is found. + */ +private unsigned char * +encode_utf8(unsigned char *buf, size_t len, unichar *ubuf, size_t ulen) +{ + size_t i; + unsigned char *end = buf + len; + + for (i = 0; i < ulen; i++) { + if (ubuf[i] <= 0x7f) { + if (end - buf < 1) + return NULL; + *buf++ = (unsigned char)ubuf[i]; + } else if (ubuf[i] <= 0x7ff) { + if (end - buf < 2) + return NULL; + *buf++ = (unsigned char)((ubuf[i] >> 6) + 0xc0); + *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); + } else if (ubuf[i] <= 0xffff) { + if (end - buf < 3) + return NULL; + *buf++ = (unsigned char)((ubuf[i] >> 12) + 0xe0); + *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); + *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); + } else if (ubuf[i] <= 0x1fffff) { + if (end - buf < 4) + return NULL; + *buf++ = (unsigned char)((ubuf[i] >> 18) + 0xf0); + *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); + *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); + *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); + } else if (ubuf[i] <= 0x3ffffff) { + if (end - buf < 5) + return NULL; + *buf++ = (unsigned char)((ubuf[i] >> 24) + 0xf8); + *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80); + *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); + *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); + *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); + } else if (ubuf[i] <= 0x7fffffff) { + if (end - buf < 6) + return NULL; + *buf++ = (unsigned char)((ubuf[i] >> 30) + 0xfc); + *buf++ = (unsigned char)(((ubuf[i] >> 24) & 0x3f) + 0x80); + *buf++ = (unsigned char)(((ubuf[i] >> 18) & 0x3f) + 0x80); + *buf++ = (unsigned char)(((ubuf[i] >> 12) & 0x3f) + 0x80); + *buf++ = (unsigned char)(((ubuf[i] >> 6) & 0x3f) + 0x80); + *buf++ = (unsigned char)((ubuf[i] & 0x3f) + 0x80); + } else /* Invalid character */ + return NULL; + } + + return buf; +} diff --git a/ext/fileinfo/libmagic/cdf.c b/ext/fileinfo/libmagic/cdf.c new file mode 100644 index 0000000..7efa43e --- /dev/null +++ b/ext/fileinfo/libmagic/cdf.c @@ -0,0 +1,1360 @@ +/*- + * Copyright (c) 2008 Christos Zoulas + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +/* + * Parse Composite Document Files, the format used in Microsoft Office + * document files before they switched to zipped XML. + * Info from: http://sc.openoffice.org/compdocfileformat.pdf + * + * N.B. This is the "Composite Document File" format, and not the + * "Compound Document Format", nor the "Channel Definition Format". + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: cdf.c,v 1.50 2012/02/20 22:35:29 christos Exp $") +#endif + +#include <assert.h> +#ifdef CDF_DEBUG +#include <err.h> +#endif +#include <stdlib.h> + +#ifdef PHP_WIN32 +#include "win32/unistd.h" +#else +#include <unistd.h> +#endif + +#ifndef UINT32_MAX +# define UINT32_MAX (0xffffffff) +#endif + +#include <string.h> +#include <time.h> +#include <ctype.h> +#ifdef HAVE_LIMITS_H +#include <limits.h> +#endif + +#ifndef EFTYPE +#define EFTYPE EINVAL +#endif + +#include "cdf.h" + +#ifdef CDF_DEBUG +#define DPRINTF(a) printf a, fflush(stdout) +#else +#define DPRINTF(a) +#endif + +static union { + char s[4]; + uint32_t u; +} cdf_bo; + +#define NEED_SWAP (cdf_bo.u == (uint32_t)0x01020304) + +#define CDF_TOLE8(x) ((uint64_t)(NEED_SWAP ? _cdf_tole8(x) : (uint64_t)(x))) +#define CDF_TOLE4(x) ((uint32_t)(NEED_SWAP ? _cdf_tole4(x) : (uint32_t)(x))) +#define CDF_TOLE2(x) ((uint16_t)(NEED_SWAP ? _cdf_tole2(x) : (uint16_t)(x))) +#define CDF_GETUINT32(x, y) cdf_getuint32(x, y) + + +/* + * swap a short + */ +static uint16_t +_cdf_tole2(uint16_t sv) +{ + uint16_t rv; + uint8_t *s = (uint8_t *)(void *)&sv; + uint8_t *d = (uint8_t *)(void *)&rv; + d[0] = s[1]; + d[1] = s[0]; + return rv; +} + +/* + * swap an int + */ +static uint32_t +_cdf_tole4(uint32_t sv) +{ + uint32_t rv; + uint8_t *s = (uint8_t *)(void *)&sv; + uint8_t *d = (uint8_t *)(void *)&rv; + d[0] = s[3]; + d[1] = s[2]; + d[2] = s[1]; + d[3] = s[0]; + return rv; +} + +/* + * swap a quad + */ +static uint64_t +_cdf_tole8(uint64_t sv) +{ + uint64_t rv; + uint8_t *s = (uint8_t *)(void *)&sv; + uint8_t *d = (uint8_t *)(void *)&rv; + d[0] = s[7]; + d[1] = s[6]; + d[2] = s[5]; + d[3] = s[4]; + d[4] = s[3]; + d[5] = s[2]; + d[6] = s[1]; + d[7] = s[0]; + return rv; +} + +/* + * grab a uint32_t from a possibly unaligned address, and return it in + * the native host order. + */ +static uint32_t +cdf_getuint32(const uint8_t *p, size_t offs) +{ + uint32_t rv; + (void)memcpy(&rv, p + offs * sizeof(uint32_t), sizeof(rv)); + return CDF_TOLE4(rv); +} + +#define CDF_UNPACK(a) \ + (void)memcpy(&(a), &buf[len], sizeof(a)), len += sizeof(a) +#define CDF_UNPACKA(a) \ + (void)memcpy((a), &buf[len], sizeof(a)), len += sizeof(a) + +uint16_t +cdf_tole2(uint16_t sv) +{ + return CDF_TOLE2(sv); +} + +uint32_t +cdf_tole4(uint32_t sv) +{ + return CDF_TOLE4(sv); +} + +uint64_t +cdf_tole8(uint64_t sv) +{ + return CDF_TOLE8(sv); +} + +void +cdf_swap_header(cdf_header_t *h) +{ + size_t i; + + h->h_magic = CDF_TOLE8(h->h_magic); + h->h_uuid[0] = CDF_TOLE8(h->h_uuid[0]); + h->h_uuid[1] = CDF_TOLE8(h->h_uuid[1]); + h->h_revision = CDF_TOLE2(h->h_revision); + h->h_version = CDF_TOLE2(h->h_version); + h->h_byte_order = CDF_TOLE2(h->h_byte_order); + h->h_sec_size_p2 = CDF_TOLE2(h->h_sec_size_p2); + h->h_short_sec_size_p2 = CDF_TOLE2(h->h_short_sec_size_p2); + h->h_num_sectors_in_sat = CDF_TOLE4(h->h_num_sectors_in_sat); + h->h_secid_first_directory = CDF_TOLE4(h->h_secid_first_directory); + h->h_min_size_standard_stream = + CDF_TOLE4(h->h_min_size_standard_stream); + h->h_secid_first_sector_in_short_sat = + CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_short_sat); + h->h_num_sectors_in_short_sat = + CDF_TOLE4(h->h_num_sectors_in_short_sat); + h->h_secid_first_sector_in_master_sat = + CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_master_sat); + h->h_num_sectors_in_master_sat = + CDF_TOLE4(h->h_num_sectors_in_master_sat); + for (i = 0; i < __arraycount(h->h_master_sat); i++) + h->h_master_sat[i] = CDF_TOLE4((uint32_t)h->h_master_sat[i]); +} + +void +cdf_unpack_header(cdf_header_t *h, char *buf) +{ + size_t i; + size_t len = 0; + + CDF_UNPACK(h->h_magic); + CDF_UNPACKA(h->h_uuid); + CDF_UNPACK(h->h_revision); + CDF_UNPACK(h->h_version); + CDF_UNPACK(h->h_byte_order); + CDF_UNPACK(h->h_sec_size_p2); + CDF_UNPACK(h->h_short_sec_size_p2); + CDF_UNPACKA(h->h_unused0); + CDF_UNPACK(h->h_num_sectors_in_sat); + CDF_UNPACK(h->h_secid_first_directory); + CDF_UNPACKA(h->h_unused1); + CDF_UNPACK(h->h_min_size_standard_stream); + CDF_UNPACK(h->h_secid_first_sector_in_short_sat); + CDF_UNPACK(h->h_num_sectors_in_short_sat); + CDF_UNPACK(h->h_secid_first_sector_in_master_sat); + CDF_UNPACK(h->h_num_sectors_in_master_sat); + for (i = 0; i < __arraycount(h->h_master_sat); i++) + CDF_UNPACK(h->h_master_sat[i]); +} + +void +cdf_swap_dir(cdf_directory_t *d) +{ + d->d_namelen = CDF_TOLE2(d->d_namelen); + d->d_left_child = CDF_TOLE4((uint32_t)d->d_left_child); + d->d_right_child = CDF_TOLE4((uint32_t)d->d_right_child); + d->d_storage = CDF_TOLE4((uint32_t)d->d_storage); + d->d_storage_uuid[0] = CDF_TOLE8(d->d_storage_uuid[0]); + d->d_storage_uuid[1] = CDF_TOLE8(d->d_storage_uuid[1]); + d->d_flags = CDF_TOLE4(d->d_flags); + d->d_created = CDF_TOLE8((uint64_t)d->d_created); + d->d_modified = CDF_TOLE8((uint64_t)d->d_modified); + d->d_stream_first_sector = CDF_TOLE4((uint32_t)d->d_stream_first_sector); + d->d_size = CDF_TOLE4(d->d_size); +} + +void +cdf_swap_class(cdf_classid_t *d) +{ + d->cl_dword = CDF_TOLE4(d->cl_dword); + d->cl_word[0] = CDF_TOLE2(d->cl_word[0]); + d->cl_word[1] = CDF_TOLE2(d->cl_word[1]); +} + +void +cdf_unpack_dir(cdf_directory_t *d, char *buf) +{ + size_t len = 0; + + CDF_UNPACKA(d->d_name); + CDF_UNPACK(d->d_namelen); + CDF_UNPACK(d->d_type); + CDF_UNPACK(d->d_color); + CDF_UNPACK(d->d_left_child); + CDF_UNPACK(d->d_right_child); + CDF_UNPACK(d->d_storage); + CDF_UNPACKA(d->d_storage_uuid); + CDF_UNPACK(d->d_flags); + CDF_UNPACK(d->d_created); + CDF_UNPACK(d->d_modified); + CDF_UNPACK(d->d_stream_first_sector); + CDF_UNPACK(d->d_size); + CDF_UNPACK(d->d_unused0); +} + +static int +cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h, + const void *p, size_t tail, int line) +{ + const char *b = (const char *)sst->sst_tab; + const char *e = ((const char *)p) + tail; + (void)&line; + if (e >= b && (size_t)(e - b) < CDF_SEC_SIZE(h) * sst->sst_len) + return 0; + DPRINTF(("%d: offset begin %p end %p %" SIZE_T_FORMAT "u" + " >= %" SIZE_T_FORMAT "u [%" SIZE_T_FORMAT "u %" + SIZE_T_FORMAT "u]\n", line, b, e, (size_t)(e - b), + CDF_SEC_SIZE(h) * sst->sst_len, CDF_SEC_SIZE(h), sst->sst_len)); + errno = EFTYPE; + return -1; +} + +static ssize_t +cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len) +{ + size_t siz = (size_t)off + len; + + if ((off_t)(off + len) != (off_t)siz) { + errno = EINVAL; + return -1; + } + + if (info->i_buf != NULL && info->i_len >= siz) { + (void)memcpy(buf, &info->i_buf[off], len); + return (ssize_t)len; + } + + if (info->i_fd == -1) + return -1; + + if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (off_t)-1) + return -1; + + if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len) + return -1; + + return (ssize_t)len; +} + +int +cdf_read_header(const cdf_info_t *info, cdf_header_t *h) +{ + char buf[512]; + + (void)memcpy(cdf_bo.s, "\01\02\03\04", 4); + if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1) + return -1; + cdf_unpack_header(h, buf); + cdf_swap_header(h); + if (h->h_magic != CDF_MAGIC) { + DPRINTF(("Bad magic 0x%" INT64_T_FORMAT "x != 0x%" + INT64_T_FORMAT "x\n", + (unsigned long long)h->h_magic, + (unsigned long long)CDF_MAGIC)); + goto out; + } + if (h->h_sec_size_p2 > 20) { + DPRINTF(("Bad sector size 0x%u\n", h->h_sec_size_p2)); + goto out; + } + if (h->h_short_sec_size_p2 > 20) { + DPRINTF(("Bad short sector size 0x%u\n", + h->h_short_sec_size_p2)); + goto out; + } + return 0; +out: + errno = EFTYPE; + return -1; +} + + +ssize_t +cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len, + const cdf_header_t *h, cdf_secid_t id) +{ + size_t ss = CDF_SEC_SIZE(h); + size_t pos = CDF_SEC_POS(h, id); + assert(ss == len); + return cdf_read(info, (off_t)pos, ((char *)buf) + offs, len); +} + +ssize_t +cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs, + size_t len, const cdf_header_t *h, cdf_secid_t id) +{ + size_t ss = CDF_SHORT_SEC_SIZE(h); + size_t pos = CDF_SHORT_SEC_POS(h, id); + assert(ss == len); + if (pos > CDF_SEC_SIZE(h) * sst->sst_len) { + DPRINTF(("Out of bounds read %" SIZE_T_FORMAT "u > %" + SIZE_T_FORMAT "u\n", + pos, CDF_SEC_SIZE(h) * sst->sst_len)); + return -1; + } + (void)memcpy(((char *)buf) + offs, + ((const char *)sst->sst_tab) + pos, len); + return len; +} + +/* + * Read the sector allocation table. + */ +int +cdf_read_sat(const cdf_info_t *info, cdf_header_t *h, cdf_sat_t *sat) +{ + size_t i, j, k; + size_t ss = CDF_SEC_SIZE(h); + cdf_secid_t *msa, mid, sec; + size_t nsatpersec = (ss / sizeof(mid)) - 1; + + for (i = 0; i < __arraycount(h->h_master_sat); i++) + if (h->h_master_sat[i] == CDF_SECID_FREE) + break; + +#define CDF_SEC_LIMIT (UINT32_MAX / (4 * ss)) + if ((nsatpersec > 0 && + h->h_num_sectors_in_master_sat > CDF_SEC_LIMIT / nsatpersec) || + i > CDF_SEC_LIMIT) { + DPRINTF(("Number of sectors in master SAT too big %u %" + SIZE_T_FORMAT "u\n", h->h_num_sectors_in_master_sat, i)); + errno = EFTYPE; + return -1; + } + + sat->sat_len = h->h_num_sectors_in_master_sat * nsatpersec + i; + DPRINTF(("sat_len = %" SIZE_T_FORMAT "u ss = %" SIZE_T_FORMAT "u\n", + sat->sat_len, ss)); + if ((sat->sat_tab = CAST(cdf_secid_t *, calloc(sat->sat_len, ss))) + == NULL) + return -1; + + for (i = 0; i < __arraycount(h->h_master_sat); i++) { + if (h->h_master_sat[i] < 0) + break; + if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h, + h->h_master_sat[i]) != (ssize_t)ss) { + DPRINTF(("Reading sector %d", h->h_master_sat[i])); + goto out1; + } + } + + if ((msa = CAST(cdf_secid_t *, calloc(1, ss))) == NULL) + goto out1; + + mid = h->h_secid_first_sector_in_master_sat; + for (j = 0; j < h->h_num_sectors_in_master_sat; j++) { + if (mid < 0) + goto out; + if (j >= CDF_LOOP_LIMIT) { + DPRINTF(("Reading master sector loop limit")); + errno = EFTYPE; + goto out2; + } + if (cdf_read_sector(info, msa, 0, ss, h, mid) != (ssize_t)ss) { + DPRINTF(("Reading master sector %d", mid)); + goto out2; + } + for (k = 0; k < nsatpersec; k++, i++) { + sec = CDF_TOLE4((uint32_t)msa[k]); + if (sec < 0) + goto out; + if (i >= sat->sat_len) { + DPRINTF(("Out of bounds reading MSA %" SIZE_T_FORMAT + "u >= %" SIZE_T_FORMAT "u", i, sat->sat_len)); + errno = EFTYPE; + goto out2; + } + if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h, + sec) != (ssize_t)ss) { + DPRINTF(("Reading sector %d", + CDF_TOLE4(msa[k]))); + goto out2; + } + } + mid = CDF_TOLE4((uint32_t)msa[nsatpersec]); + } +out: + sat->sat_len = i; + free(msa); + return 0; +out2: + free(msa); +out1: + free(sat->sat_tab); + return -1; +} + +size_t +cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size) +{ + size_t i, j; + cdf_secid_t maxsector = (cdf_secid_t)(sat->sat_len * size); + + DPRINTF(("Chain:")); + for (j = i = 0; sid >= 0; i++, j++) { + DPRINTF((" %d", sid)); + if (j >= CDF_LOOP_LIMIT) { + DPRINTF(("Counting chain loop limit")); + errno = EFTYPE; + return (size_t)-1; + } + if (sid > maxsector) { + DPRINTF(("Sector %d > %d\n", sid, maxsector)); + errno = EFTYPE; + return (size_t)-1; + } + sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]); + } + DPRINTF(("\n")); + return i; +} + +int +cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h, + const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn) +{ + size_t ss = CDF_SEC_SIZE(h), i, j; + ssize_t nr; + scn->sst_len = cdf_count_chain(sat, sid, ss); + scn->sst_dirlen = len; + + if (scn->sst_len == (size_t)-1) + return -1; + + scn->sst_tab = calloc(scn->sst_len, ss); + if (scn->sst_tab == NULL) + return -1; + + for (j = i = 0; sid >= 0; i++, j++) { + if (j >= CDF_LOOP_LIMIT) { + DPRINTF(("Read long sector chain loop limit")); + errno = EFTYPE; + goto out; + } + if (i >= scn->sst_len) { + DPRINTF(("Out of bounds reading long sector chain " + "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i, + scn->sst_len)); + errno = EFTYPE; + goto out; + } + if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h, + sid)) != (ssize_t)ss) { + if (i == scn->sst_len - 1 && nr > 0) { + /* Last sector might be truncated */ + return 0; + } + DPRINTF(("Reading long sector chain %d", sid)); + goto out; + } + sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]); + } + return 0; +out: + free(scn->sst_tab); + return -1; +} + +int +cdf_read_short_sector_chain(const cdf_header_t *h, + const cdf_sat_t *ssat, const cdf_stream_t *sst, + cdf_secid_t sid, size_t len, cdf_stream_t *scn) +{ + size_t ss = CDF_SHORT_SEC_SIZE(h), i, j; + scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h)); + scn->sst_dirlen = len; + + if (sst->sst_tab == NULL || scn->sst_len == (size_t)-1) + return -1; + + scn->sst_tab = calloc(scn->sst_len, ss); + if (scn->sst_tab == NULL) + return -1; + + for (j = i = 0; sid >= 0; i++, j++) { + if (j >= CDF_LOOP_LIMIT) { + DPRINTF(("Read short sector chain loop limit")); + errno = EFTYPE; + goto out; + } + if (i >= scn->sst_len) { + DPRINTF(("Out of bounds reading short sector chain " + "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", + i, scn->sst_len)); + errno = EFTYPE; + goto out; + } + if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h, + sid) != (ssize_t)ss) { + DPRINTF(("Reading short sector chain %d", sid)); + goto out; + } + sid = CDF_TOLE4((uint32_t)ssat->sat_tab[sid]); + } + return 0; +out: + free(scn->sst_tab); + return -1; +} + +int +cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h, + const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst, + cdf_secid_t sid, size_t len, cdf_stream_t *scn) +{ + + if (len < h->h_min_size_standard_stream && sst->sst_tab != NULL) + return cdf_read_short_sector_chain(h, ssat, sst, sid, len, + scn); + else + return cdf_read_long_sector_chain(info, h, sat, sid, len, scn); +} + +int +cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h, + const cdf_sat_t *sat, cdf_dir_t *dir) +{ + size_t i, j; + size_t ss = CDF_SEC_SIZE(h), ns, nd; + char *buf; + cdf_secid_t sid = h->h_secid_first_directory; + + ns = cdf_count_chain(sat, sid, ss); + if (ns == (size_t)-1) + return -1; + + nd = ss / CDF_DIRECTORY_SIZE; + + dir->dir_len = ns * nd; + dir->dir_tab = CAST(cdf_directory_t *, + calloc(dir->dir_len, sizeof(dir->dir_tab[0]))); + if (dir->dir_tab == NULL) + return -1; + + if ((buf = CAST(char *, malloc(ss))) == NULL) { + free(dir->dir_tab); + return -1; + } + + for (j = i = 0; i < ns; i++, j++) { + if (j >= CDF_LOOP_LIMIT) { + DPRINTF(("Read dir loop limit")); + errno = EFTYPE; + goto out; + } + if (cdf_read_sector(info, buf, 0, ss, h, sid) != (ssize_t)ss) { + DPRINTF(("Reading directory sector %d", sid)); + goto out; + } + for (j = 0; j < nd; j++) { + cdf_unpack_dir(&dir->dir_tab[i * nd + j], + &buf[j * CDF_DIRECTORY_SIZE]); + } + sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]); + } + if (NEED_SWAP) + for (i = 0; i < dir->dir_len; i++) + cdf_swap_dir(&dir->dir_tab[i]); + free(buf); + return 0; +out: + free(dir->dir_tab); + free(buf); + return -1; +} + + +int +cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h, + const cdf_sat_t *sat, cdf_sat_t *ssat) +{ + size_t i, j; + size_t ss = CDF_SEC_SIZE(h); + cdf_secid_t sid = h->h_secid_first_sector_in_short_sat; + + ssat->sat_len = cdf_count_chain(sat, sid, CDF_SEC_SIZE(h)); + if (ssat->sat_len == (size_t)-1) + return -1; + + ssat->sat_tab = CAST(cdf_secid_t *, calloc(ssat->sat_len, ss)); + if (ssat->sat_tab == NULL) + return -1; + + for (j = i = 0; sid >= 0; i++, j++) { + if (j >= CDF_LOOP_LIMIT) { + DPRINTF(("Read short sat sector loop limit")); + errno = EFTYPE; + goto out; + } + if (i >= ssat->sat_len) { + DPRINTF(("Out of bounds reading short sector chain " + "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i, + ssat->sat_len)); + errno = EFTYPE; + goto out; + } + if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) != + (ssize_t)ss) { + DPRINTF(("Reading short sat sector %d", sid)); + goto out; + } + sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]); + } + return 0; +out: + free(ssat->sat_tab); + return -1; +} + +int +cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h, + const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn) +{ + size_t i; + const cdf_directory_t *d; + + for (i = 0; i < dir->dir_len; i++) + if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE) + break; + + /* If the it is not there, just fake it; some docs don't have it */ + if (i == dir->dir_len) + goto out; + d = &dir->dir_tab[i]; + + /* If the it is not there, just fake it; some docs don't have it */ + if (d->d_stream_first_sector < 0) + goto out; + + return cdf_read_long_sector_chain(info, h, sat, + d->d_stream_first_sector, d->d_size, scn); +out: + scn->sst_tab = NULL; + scn->sst_len = 0; + scn->sst_dirlen = 0; + return 0; +} + +static int +cdf_namecmp(const char *d, const uint16_t *s, size_t l) +{ + for (; l--; d++, s++) + if (*d != CDF_TOLE2(*s)) + return (unsigned char)*d - CDF_TOLE2(*s); + return 0; +} + +int +cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h, + const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst, + const cdf_dir_t *dir, cdf_stream_t *scn) +{ + size_t i; + const cdf_directory_t *d; + static const char name[] = "\05SummaryInformation"; + + for (i = dir->dir_len; i > 0; i--) + if (dir->dir_tab[i - 1].d_type == CDF_DIR_TYPE_USER_STREAM && + cdf_namecmp(name, dir->dir_tab[i - 1].d_name, sizeof(name)) + == 0) + break; + + if (i == 0) { + DPRINTF(("Cannot find summary information section\n")); + errno = ESRCH; + return -1; + } + d = &dir->dir_tab[i - 1]; + return cdf_read_sector_chain(info, h, sat, ssat, sst, + d->d_stream_first_sector, d->d_size, scn); +} + +int +cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h, + uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount) +{ + const cdf_section_header_t *shp; + cdf_section_header_t sh; + const uint8_t *p, *q, *e; + int16_t s16; + int32_t s32; + uint32_t u32; + int64_t s64; + uint64_t u64; + cdf_timestamp_t tp; + size_t i, o, o4, nelements, j; + cdf_property_info_t *inp; + + if (offs > UINT32_MAX / 4) { + errno = EFTYPE; + goto out; + } + shp = CAST(const cdf_section_header_t *, (const void *) + ((const char *)sst->sst_tab + offs)); + if (cdf_check_stream_offset(sst, h, shp, sizeof(*shp), __LINE__) == -1) + goto out; + sh.sh_len = CDF_TOLE4(shp->sh_len); +#define CDF_SHLEN_LIMIT (UINT32_MAX / 8) + if (sh.sh_len > CDF_SHLEN_LIMIT) { + errno = EFTYPE; + goto out; + } + sh.sh_properties = CDF_TOLE4(shp->sh_properties); +#define CDF_PROP_LIMIT (UINT32_MAX / (4 * sizeof(*inp))) + if (sh.sh_properties > CDF_PROP_LIMIT) + goto out; + DPRINTF(("section len: %u properties %u\n", sh.sh_len, + sh.sh_properties)); + if (*maxcount) { + if (*maxcount > CDF_PROP_LIMIT) + goto out; + *maxcount += sh.sh_properties; + inp = CAST(cdf_property_info_t *, + realloc(*info, *maxcount * sizeof(*inp))); + } else { + *maxcount = sh.sh_properties; + inp = CAST(cdf_property_info_t *, + malloc(*maxcount * sizeof(*inp))); + } + if (inp == NULL) + goto out; + *info = inp; + inp += *count; + *count += sh.sh_properties; + p = CAST(const uint8_t *, (const void *) + ((const char *)(const void *)sst->sst_tab + + offs + sizeof(sh))); + e = CAST(const uint8_t *, (const void *) + (((const char *)(const void *)shp) + sh.sh_len)); + if (cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1) + goto out; + for (i = 0; i < sh.sh_properties; i++) { + size_t ofs = CDF_GETUINT32(p, (i << 1) + 1); + q = (const uint8_t *)(const void *) + ((const char *)(const void *)p + ofs + - 2 * sizeof(uint32_t)); + if (q > e) { + DPRINTF(("Ran of the end %p > %p\n", q, e)); + goto out; + } + inp[i].pi_id = CDF_GETUINT32(p, i << 1); + inp[i].pi_type = CDF_GETUINT32(q, 0); + DPRINTF(("%" SIZE_T_FORMAT "u) id=%x type=%x offs=0x%tx,0x%x\n", + i, inp[i].pi_id, inp[i].pi_type, q - p, offs)); + if (inp[i].pi_type & CDF_VECTOR) { + nelements = CDF_GETUINT32(q, 1); + o = 2; + } else { + nelements = 1; + o = 1; + } + o4 = o * sizeof(uint32_t); + if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED)) + goto unknown; + switch (inp[i].pi_type & CDF_TYPEMASK) { + case CDF_NULL: + case CDF_EMPTY: + break; + case CDF_SIGNED16: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&s16, &q[o4], sizeof(s16)); + inp[i].pi_s16 = CDF_TOLE2(s16); + break; + case CDF_SIGNED32: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&s32, &q[o4], sizeof(s32)); + inp[i].pi_s32 = CDF_TOLE4((uint32_t)s32); + break; + case CDF_BOOL: + case CDF_UNSIGNED32: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&u32, &q[o4], sizeof(u32)); + inp[i].pi_u32 = CDF_TOLE4(u32); + break; + case CDF_SIGNED64: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&s64, &q[o4], sizeof(s64)); + inp[i].pi_s64 = CDF_TOLE8((uint64_t)s64); + break; + case CDF_UNSIGNED64: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&u64, &q[o4], sizeof(u64)); + inp[i].pi_u64 = CDF_TOLE8((uint64_t)u64); + break; + case CDF_FLOAT: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&u32, &q[o4], sizeof(u32)); + u32 = CDF_TOLE4(u32); + memcpy(&inp[i].pi_f, &u32, sizeof(inp[i].pi_f)); + break; + case CDF_DOUBLE: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&u64, &q[o4], sizeof(u64)); + u64 = CDF_TOLE8((uint64_t)u64); + memcpy(&inp[i].pi_d, &u64, sizeof(inp[i].pi_d)); + break; + case CDF_LENGTH32_STRING: + case CDF_LENGTH32_WSTRING: + if (nelements > 1) { + size_t nelem = inp - *info; + if (*maxcount > CDF_PROP_LIMIT + || nelements > CDF_PROP_LIMIT) + goto out; + *maxcount += nelements; + inp = CAST(cdf_property_info_t *, + realloc(*info, *maxcount * sizeof(*inp))); + if (inp == NULL) + goto out; + *info = inp; + inp = *info + nelem; + } + DPRINTF(("nelements = %" SIZE_T_FORMAT "u\n", + nelements)); + for (j = 0; j < nelements; j++, i++) { + uint32_t l = CDF_GETUINT32(q, o); + inp[i].pi_str.s_len = l; + inp[i].pi_str.s_buf = (const char *) + (const void *)(&q[o4 + sizeof(l)]); + DPRINTF(("l = %d, r = %" SIZE_T_FORMAT + "u, s = %s\n", l, + CDF_ROUND(l, sizeof(l)), + inp[i].pi_str.s_buf)); + if (l & 1) + l++; + o += l >> 1; + if (q + o >= e) + goto out; + o4 = o * sizeof(uint32_t); + } + i--; + break; + case CDF_FILETIME: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + (void)memcpy(&tp, &q[o4], sizeof(tp)); + inp[i].pi_tp = CDF_TOLE8((uint64_t)tp); + break; + case CDF_CLIPBOARD: + if (inp[i].pi_type & CDF_VECTOR) + goto unknown; + break; + default: + unknown: + DPRINTF(("Don't know how to deal with %x\n", + inp[i].pi_type)); + break; + } + } + return 0; +out: + free(*info); + return -1; +} + +int +cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h, + cdf_summary_info_header_t *ssi, cdf_property_info_t **info, size_t *count) +{ + size_t i, maxcount; + const cdf_summary_info_header_t *si = + CAST(const cdf_summary_info_header_t *, sst->sst_tab); + const cdf_section_declaration_t *sd = + CAST(const cdf_section_declaration_t *, (const void *) + ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET)); + + if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 || + cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1) + return -1; + ssi->si_byte_order = CDF_TOLE2(si->si_byte_order); + ssi->si_os_version = CDF_TOLE2(si->si_os_version); + ssi->si_os = CDF_TOLE2(si->si_os); + ssi->si_class = si->si_class; + cdf_swap_class(&ssi->si_class); + ssi->si_count = CDF_TOLE2(si->si_count); + *count = 0; + maxcount = 0; + *info = NULL; + for (i = 0; i < CDF_TOLE4(si->si_count); i++) { + if (i >= CDF_LOOP_LIMIT) { + DPRINTF(("Unpack summary info loop limit")); + errno = EFTYPE; + return -1; + } + if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset), + info, count, &maxcount) == -1) { + return -1; + } + } + return 0; +} + + + +int +cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id) +{ + return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-" + "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0], + id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0], + id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4], + id->cl_six[5]); +} + +static const struct { + uint32_t v; + const char *n; +} vn[] = { + { CDF_PROPERTY_CODE_PAGE, "Code page" }, + { CDF_PROPERTY_TITLE, "Title" }, + { CDF_PROPERTY_SUBJECT, "Subject" }, + { CDF_PROPERTY_AUTHOR, "Author" }, + { CDF_PROPERTY_KEYWORDS, "Keywords" }, + { CDF_PROPERTY_COMMENTS, "Comments" }, + { CDF_PROPERTY_TEMPLATE, "Template" }, + { CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" }, + { CDF_PROPERTY_REVISION_NUMBER, "Revision Number" }, + { CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" }, + { CDF_PROPERTY_LAST_PRINTED, "Last Printed" }, + { CDF_PROPERTY_CREATE_TIME, "Create Time/Date" }, + { CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" }, + { CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" }, + { CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" }, + { CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" }, + { CDF_PROPERTY_THUMBNAIL, "Thumbnail" }, + { CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" }, + { CDF_PROPERTY_SECURITY, "Security" }, + { CDF_PROPERTY_LOCALE_ID, "Locale ID" }, +}; + +int +cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p) +{ + size_t i; + + for (i = 0; i < __arraycount(vn); i++) + if (vn[i].v == p) + return snprintf(buf, bufsiz, "%s", vn[i].n); + return snprintf(buf, bufsiz, "0x%x", p); +} + +int +cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts) +{ + int len = 0; + int days, hours, mins, secs; + + ts /= CDF_TIME_PREC; + secs = (int)(ts % 60); + ts /= 60; + mins = (int)(ts % 60); + ts /= 60; + hours = (int)(ts % 24); + ts /= 24; + days = (int)ts; + + if (days) { + len += snprintf(buf + len, bufsiz - len, "%dd+", days); + if ((size_t)len >= bufsiz) + return len; + } + + if (days || hours) { + len += snprintf(buf + len, bufsiz - len, "%.2d:", hours); + if ((size_t)len >= bufsiz) + return len; + } + + len += snprintf(buf + len, bufsiz - len, "%.2d:", mins); + if ((size_t)len >= bufsiz) + return len; + + len += snprintf(buf + len, bufsiz - len, "%.2d", secs); + return len; +} + + +#ifdef CDF_DEBUG +void +cdf_dump_header(const cdf_header_t *h) +{ + size_t i; + +#define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b) +#define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \ + h->h_ ## b, 1 << h->h_ ## b) + DUMP("%d", revision); + DUMP("%d", version); + DUMP("0x%x", byte_order); + DUMP2("%d", sec_size_p2); + DUMP2("%d", short_sec_size_p2); + DUMP("%d", num_sectors_in_sat); + DUMP("%d", secid_first_directory); + DUMP("%d", min_size_standard_stream); + DUMP("%d", secid_first_sector_in_short_sat); + DUMP("%d", num_sectors_in_short_sat); + DUMP("%d", secid_first_sector_in_master_sat); + DUMP("%d", num_sectors_in_master_sat); + for (i = 0; i < __arraycount(h->h_master_sat); i++) { + if (h->h_master_sat[i] == CDF_SECID_FREE) + break; + (void)fprintf(stderr, "%35.35s[%.3zu] = %d\n", + "master_sat", i, h->h_master_sat[i]); + } +} + +void +cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size) +{ + size_t i, j, s = size / sizeof(cdf_secid_t); + + for (i = 0; i < sat->sat_len; i++) { + (void)fprintf(stderr, "%s[%" SIZE_T_FORMAT "u]:\n%.6" + SIZE_T_FORMAT "u: ", prefix, i, i * s); + for (j = 0; j < s; j++) { + (void)fprintf(stderr, "%5d, ", + CDF_TOLE4(sat->sat_tab[s * i + j])); + if ((j + 1) % 10 == 0) + (void)fprintf(stderr, "\n%.6" SIZE_T_FORMAT + "u: ", i * s + j + 1); + } + (void)fprintf(stderr, "\n"); + } +} + +void +cdf_dump(void *v, size_t len) +{ + size_t i, j; + unsigned char *p = v; + char abuf[16]; + (void)fprintf(stderr, "%.4x: ", 0); + for (i = 0, j = 0; i < len; i++, p++) { + (void)fprintf(stderr, "%.2x ", *p); + abuf[j++] = isprint(*p) ? *p : '.'; + if (j == 16) { + j = 0; + abuf[15] = '\0'; + (void)fprintf(stderr, "%s\n%.4" SIZE_T_FORMAT "x: ", + abuf, i + 1); + } + } + (void)fprintf(stderr, "\n"); +} + +void +cdf_dump_stream(const cdf_header_t *h, const cdf_stream_t *sst) +{ + size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ? + CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h); + cdf_dump(sst->sst_tab, ss * sst->sst_len); +} + +void +cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h, + const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst, + const cdf_dir_t *dir) +{ + size_t i, j; + cdf_directory_t *d; + char name[__arraycount(d->d_name)]; + cdf_stream_t scn; + struct timeval ts; + + static const char *types[] = { "empty", "user storage", + "user stream", "lockbytes", "property", "root storage" }; + + for (i = 0; i < dir->dir_len; i++) { + d = &dir->dir_tab[i]; + for (j = 0; j < sizeof(name); j++) + name[j] = (char)CDF_TOLE2(d->d_name[j]); + (void)fprintf(stderr, "Directory %" SIZE_T_FORMAT "u: %s\n", + i, name); + if (d->d_type < __arraycount(types)) + (void)fprintf(stderr, "Type: %s\n", types[d->d_type]); + else + (void)fprintf(stderr, "Type: %d\n", d->d_type); + (void)fprintf(stderr, "Color: %s\n", + d->d_color ? "black" : "red"); + (void)fprintf(stderr, "Left child: %d\n", d->d_left_child); + (void)fprintf(stderr, "Right child: %d\n", d->d_right_child); + (void)fprintf(stderr, "Flags: 0x%x\n", d->d_flags); + cdf_timestamp_to_timespec(&ts, d->d_created); + (void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec)); + cdf_timestamp_to_timespec(&ts, d->d_modified); + (void)fprintf(stderr, "Modified %s", cdf_ctime(&ts.tv_sec)); + (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector); + (void)fprintf(stderr, "Size %d\n", d->d_size); + switch (d->d_type) { + case CDF_DIR_TYPE_USER_STORAGE: + (void)fprintf(stderr, "Storage: %d\n", d->d_storage); + break; + case CDF_DIR_TYPE_USER_STREAM: + if (sst == NULL) + break; + if (cdf_read_sector_chain(info, h, sat, ssat, sst, + d->d_stream_first_sector, d->d_size, &scn) == -1) { + warn("Can't read stream for %s at %d len %d", + name, d->d_stream_first_sector, d->d_size); + break; + } + cdf_dump_stream(h, &scn); + free(scn.sst_tab); + break; + default: + break; + } + + } +} + +void +cdf_dump_property_info(const cdf_property_info_t *info, size_t count) +{ + cdf_timestamp_t tp; + struct timeval ts; + char buf[64]; + size_t i, j; + + for (i = 0; i < count; i++) { + cdf_print_property_name(buf, sizeof(buf), info[i].pi_id); + (void)fprintf(stderr, "%" SIZE_T_FORMAT "u) %s: ", i, buf); + switch (info[i].pi_type) { + case CDF_NULL: + break; + case CDF_SIGNED16: + (void)fprintf(stderr, "signed 16 [%hd]\n", + info[i].pi_s16); + break; + case CDF_SIGNED32: + (void)fprintf(stderr, "signed 32 [%d]\n", + info[i].pi_s32); + break; + case CDF_UNSIGNED32: + (void)fprintf(stderr, "unsigned 32 [%u]\n", + info[i].pi_u32); + break; + case CDF_FLOAT: + (void)fprintf(stderr, "float [%g]\n", + info[i].pi_f); + break; + case CDF_DOUBLE: + (void)fprintf(stderr, "double [%g]\n", + info[i].pi_d); + break; + case CDF_LENGTH32_STRING: + (void)fprintf(stderr, "string %u [%.*s]\n", + info[i].pi_str.s_len, + info[i].pi_str.s_len, info[i].pi_str.s_buf); + break; + case CDF_LENGTH32_WSTRING: + (void)fprintf(stderr, "string %u [", + info[i].pi_str.s_len); + for (j = 0; j < info[i].pi_str.s_len - 1; j++) + (void)fputc(info[i].pi_str.s_buf[j << 1], stderr); + (void)fprintf(stderr, "]\n"); + break; + case CDF_FILETIME: + tp = info[i].pi_tp; +#if defined(PHP_WIN32) && _MSC_VER <= 1500 + if (tp < 1000000000000000i64) { +#else + if (tp < 1000000000000000LL) { +#endif + cdf_print_elapsed_time(buf, sizeof(buf), tp); + (void)fprintf(stderr, "timestamp %s\n", buf); + } else { + cdf_timestamp_to_timespec(&ts, tp); + (void)fprintf(stderr, "timestamp %s", + cdf_ctime(&ts.tv_sec)); + } + break; + case CDF_CLIPBOARD: + (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32); + break; + default: + DPRINTF(("Don't know how to deal with %x\n", + info[i].pi_type)); + break; + } + } +} + + +void +cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst) +{ + char buf[128]; + cdf_summary_info_header_t ssi; + cdf_property_info_t *info; + size_t count; + + (void)&h; + if (cdf_unpack_summary_info(sst, h, &ssi, &info, &count) == -1) + return; + (void)fprintf(stderr, "Endian: %x\n", ssi.si_byte_order); + (void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff, + ssi.si_os_version >> 8); + (void)fprintf(stderr, "Os %d\n", ssi.si_os); + cdf_print_classid(buf, sizeof(buf), &ssi.si_class); + (void)fprintf(stderr, "Class %s\n", buf); + (void)fprintf(stderr, "Count %d\n", ssi.si_count); + cdf_dump_property_info(info, count); + free(info); +} + +#endif + +#ifdef TEST +int +main(int argc, char *argv[]) +{ + int i; + cdf_header_t h; + cdf_sat_t sat, ssat; + cdf_stream_t sst, scn; + cdf_dir_t dir; + cdf_info_t info; + + if (argc < 2) { + (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname()); + return -1; + } + + info.i_buf = NULL; + info.i_len = 0; + for (i = 1; i < argc; i++) { + if ((info.i_fd = open(argv[1], O_RDONLY)) == -1) + err(1, "Cannot open `%s'", argv[1]); + + if (cdf_read_header(&info, &h) == -1) + err(1, "Cannot read header"); +#ifdef CDF_DEBUG + cdf_dump_header(&h); +#endif + + if (cdf_read_sat(&info, &h, &sat) == -1) + err(1, "Cannot read sat"); +#ifdef CDF_DEBUG + cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h)); +#endif + + if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1) + err(1, "Cannot read ssat"); +#ifdef CDF_DEBUG + cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h)); +#endif + + if (cdf_read_dir(&info, &h, &sat, &dir) == -1) + err(1, "Cannot read dir"); + + if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst) == -1) + err(1, "Cannot read short stream"); +#ifdef CDF_DEBUG + cdf_dump_stream(&h, &sst); +#endif + +#ifdef CDF_DEBUG + cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir); +#endif + + + if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir, + &scn) == -1) + err(1, "Cannot read summary info"); +#ifdef CDF_DEBUG + cdf_dump_summary_info(&h, &scn); +#endif + + (void)close(info.i_fd); + } + + return 0; +} +#endif diff --git a/ext/fileinfo/libmagic/cdf.h b/ext/fileinfo/libmagic/cdf.h new file mode 100644 index 0000000..84a2f95 --- /dev/null +++ b/ext/fileinfo/libmagic/cdf.h @@ -0,0 +1,329 @@ +/*- + * Copyright (c) 2008 Christos Zoulas + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +/* + * Parse Composite Document Files, the format used in Microsoft Office + * document files before they switched to zipped XML. + * Info from: http://sc.openoffice.org/compdocfileformat.pdf + * + * N.B. This is the "Composite Document File" format, and not the + * "Compound Document Format", nor the "Channel Definition Format". + */ + +#ifndef _H_CDF_ +#define _H_CDF_ + +#ifdef PHP_WIN32 +#include <winsock2.h> +#define timespec timeval +#define tv_nsec tv_usec +#endif +#ifdef __DJGPP__ +#define timespec timeval +#define tv_nsec tv_usec +#endif + +typedef int32_t cdf_secid_t; + +#define CDF_LOOP_LIMIT 10000 + +#define CDF_SECID_NULL 0 +#define CDF_SECID_FREE -1 +#define CDF_SECID_END_OF_CHAIN -2 +#define CDF_SECID_SECTOR_ALLOCATION_TABLE -3 +#define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE -4 + +typedef struct { + uint64_t h_magic; +#if defined(PHP_WIN32) && _MSC_VER <= 1500 +# define CDF_MAGIC 0xE11AB1A1E011CFD0i64 +#else +# define CDF_MAGIC 0xE11AB1A1E011CFD0LL +#endif + uint64_t h_uuid[2]; + uint16_t h_revision; + uint16_t h_version; + uint16_t h_byte_order; + uint16_t h_sec_size_p2; + uint16_t h_short_sec_size_p2; + uint8_t h_unused0[10]; + uint32_t h_num_sectors_in_sat; + uint32_t h_secid_first_directory; + uint8_t h_unused1[4]; + uint32_t h_min_size_standard_stream; + cdf_secid_t h_secid_first_sector_in_short_sat; + uint32_t h_num_sectors_in_short_sat; + cdf_secid_t h_secid_first_sector_in_master_sat; + uint32_t h_num_sectors_in_master_sat; + cdf_secid_t h_master_sat[436/4]; +} cdf_header_t; + +#define CDF_SEC_SIZE(h) ((size_t)(1 << (h)->h_sec_size_p2)) +#define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h)) +#define CDF_SHORT_SEC_SIZE(h) ((size_t)(1 << (h)->h_short_sec_size_p2)) +#define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h)) + +typedef int32_t cdf_dirid_t; +#define CDF_DIRID_NULL -1 + +typedef int64_t cdf_timestamp_t; +#define CDF_BASE_YEAR 1601 +#define CDF_TIME_PREC 10000000 + +typedef struct { + uint16_t d_name[32]; + uint16_t d_namelen; + uint8_t d_type; +#define CDF_DIR_TYPE_EMPTY 0 +#define CDF_DIR_TYPE_USER_STORAGE 1 +#define CDF_DIR_TYPE_USER_STREAM 2 +#define CDF_DIR_TYPE_LOCKBYTES 3 +#define CDF_DIR_TYPE_PROPERTY 4 +#define CDF_DIR_TYPE_ROOT_STORAGE 5 + uint8_t d_color; +#define CDF_DIR_COLOR_READ 0 +#define CDF_DIR_COLOR_BLACK 1 + cdf_dirid_t d_left_child; + cdf_dirid_t d_right_child; + cdf_dirid_t d_storage; + uint64_t d_storage_uuid[2]; + uint32_t d_flags; + cdf_timestamp_t d_created; + cdf_timestamp_t d_modified; + cdf_secid_t d_stream_first_sector; + uint32_t d_size; + uint32_t d_unused0; +} cdf_directory_t; + +#define CDF_DIRECTORY_SIZE 128 + +typedef struct { + cdf_secid_t *sat_tab; + size_t sat_len; +} cdf_sat_t; + +typedef struct { + cdf_directory_t *dir_tab; + size_t dir_len; +} cdf_dir_t; + +typedef struct { + void *sst_tab; + size_t sst_len; + size_t sst_dirlen; +} cdf_stream_t; + +typedef struct { + uint32_t cl_dword; + uint16_t cl_word[2]; + uint8_t cl_two[2]; + uint8_t cl_six[6]; +} cdf_classid_t; + +typedef struct { + uint16_t si_byte_order; + uint16_t si_zero; + uint16_t si_os_version; + uint16_t si_os; + cdf_classid_t si_class; + uint32_t si_count; +} cdf_summary_info_header_t; + +#define CDF_SECTION_DECLARATION_OFFSET 0x1c + +typedef struct { + cdf_classid_t sd_class; + uint32_t sd_offset; +} cdf_section_declaration_t; + +typedef struct { + uint32_t sh_len; + uint32_t sh_properties; +} cdf_section_header_t; + +typedef struct { + uint32_t pi_id; + uint32_t pi_type; + union { + uint16_t _pi_u16; + int16_t _pi_s16; + uint32_t _pi_u32; + int32_t _pi_s32; + uint64_t _pi_u64; + int64_t _pi_s64; + cdf_timestamp_t _pi_tp; + float _pi_f; + double _pi_d; + struct { + uint32_t s_len; + const char *s_buf; + } _pi_str; + } pi_val; +#define pi_u64 pi_val._pi_u64 +#define pi_s64 pi_val._pi_s64 +#define pi_u32 pi_val._pi_u32 +#define pi_s32 pi_val._pi_s32 +#define pi_u16 pi_val._pi_u16 +#define pi_s16 pi_val._pi_s16 +#define pi_f pi_val._pi_f +#define pi_d pi_val._pi_d +#define pi_tp pi_val._pi_tp +#define pi_str pi_val._pi_str +} cdf_property_info_t; + +#define CDF_ROUND(val, by) (((val) + (by) - 1) & ~((by) - 1)) + +/* Variant type definitions */ +#define CDF_EMPTY 0x00000000 +#define CDF_NULL 0x00000001 +#define CDF_SIGNED16 0x00000002 +#define CDF_SIGNED32 0x00000003 +#define CDF_FLOAT 0x00000004 +#define CDF_DOUBLE 0x00000005 +#define CDF_CY 0x00000006 +#define CDF_DATE 0x00000007 +#define CDF_BSTR 0x00000008 +#define CDF_DISPATCH 0x00000009 +#define CDF_ERROR 0x0000000a +#define CDF_BOOL 0x0000000b +#define CDF_VARIANT 0x0000000c +#define CDF_UNKNOWN 0x0000000d +#define CDF_DECIMAL 0x0000000e +#define CDF_SIGNED8 0x00000010 +#define CDF_UNSIGNED8 0x00000011 +#define CDF_UNSIGNED16 0x00000012 +#define CDF_UNSIGNED32 0x00000013 +#define CDF_SIGNED64 0x00000014 +#define CDF_UNSIGNED64 0x00000015 +#define CDF_INT 0x00000016 +#define CDF_UINT 0x00000017 +#define CDF_VOID 0x00000018 +#define CDF_HRESULT 0x00000019 +#define CDF_PTR 0x0000001a +#define CDF_SAFEARRAY 0x0000001b +#define CDF_CARRAY 0x0000001c +#define CDF_USERDEFINED 0x0000001d +#define CDF_LENGTH32_STRING 0x0000001e +#define CDF_LENGTH32_WSTRING 0x0000001f +#define CDF_FILETIME 0x00000040 +#define CDF_BLOB 0x00000041 +#define CDF_STREAM 0x00000042 +#define CDF_STORAGE 0x00000043 +#define CDF_STREAMED_OBJECT 0x00000044 +#define CDF_STORED_OBJECT 0x00000045 +#define CDF_BLOB_OBJECT 0x00000046 +#define CDF_CLIPBOARD 0x00000047 +#define CDF_CLSID 0x00000048 +#define CDF_VECTOR 0x00001000 +#define CDF_ARRAY 0x00002000 +#define CDF_BYREF 0x00004000 +#define CDF_RESERVED 0x00008000 +#define CDF_ILLEGAL 0x0000ffff +#define CDF_ILLEGALMASKED 0x00000fff +#define CDF_TYPEMASK 0x00000fff + +#define CDF_PROPERTY_CODE_PAGE 0x00000001 +#define CDF_PROPERTY_TITLE 0x00000002 +#define CDF_PROPERTY_SUBJECT 0x00000003 +#define CDF_PROPERTY_AUTHOR 0x00000004 +#define CDF_PROPERTY_KEYWORDS 0x00000005 +#define CDF_PROPERTY_COMMENTS 0x00000006 +#define CDF_PROPERTY_TEMPLATE 0x00000007 +#define CDF_PROPERTY_LAST_SAVED_BY 0x00000008 +#define CDF_PROPERTY_REVISION_NUMBER 0x00000009 +#define CDF_PROPERTY_TOTAL_EDITING_TIME 0x0000000a +#define CDF_PROPERTY_LAST_PRINTED 0X0000000b +#define CDF_PROPERTY_CREATE_TIME 0x0000000c +#define CDF_PROPERTY_LAST_SAVED_TIME 0x0000000d +#define CDF_PROPERTY_NUMBER_OF_PAGES 0x0000000e +#define CDF_PROPERTY_NUMBER_OF_WORDS 0x0000000f +#define CDF_PROPERTY_NUMBER_OF_CHARACTERS 0x00000010 +#define CDF_PROPERTY_THUMBNAIL 0x00000011 +#define CDF_PROPERTY_NAME_OF_APPLICATION 0x00000012 +#define CDF_PROPERTY_SECURITY 0x00000013 +#define CDF_PROPERTY_LOCALE_ID 0x80000000 + +typedef struct { + int i_fd; + const unsigned char *i_buf; + size_t i_len; +} cdf_info_t; + +struct timeval; +int cdf_timestamp_to_timespec(struct timeval *, cdf_timestamp_t); +int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timeval *); +int cdf_read_header(const cdf_info_t *, cdf_header_t *); +void cdf_swap_header(cdf_header_t *); +void cdf_unpack_header(cdf_header_t *, char *); +void cdf_swap_dir(cdf_directory_t *); +void cdf_unpack_dir(cdf_directory_t *, char *); +void cdf_swap_class(cdf_classid_t *); +ssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t, + const cdf_header_t *, cdf_secid_t); +ssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t, + const cdf_header_t *, cdf_secid_t); +int cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *); +size_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t); +int cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *, + const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *); +int cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *, + const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *); +int cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *, + const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t, + size_t, cdf_stream_t *); +int cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, + cdf_dir_t *); +int cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, + cdf_sat_t *); +int cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *, + const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *); +int cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t, + cdf_property_info_t **, size_t *, size_t *); +int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *, + const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, + const cdf_dir_t *, cdf_stream_t *); +int cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *, + cdf_summary_info_header_t *, cdf_property_info_t **, size_t *); +int cdf_print_classid(char *, size_t, const cdf_classid_t *); +int cdf_print_property_name(char *, size_t, uint32_t); +int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t); +uint16_t cdf_tole2(uint16_t); +uint32_t cdf_tole4(uint32_t); +uint64_t cdf_tole8(uint64_t); +char *cdf_ctime(const time_t *); + +#ifdef CDF_DEBUG +void cdf_dump_header(const cdf_header_t *); +void cdf_dump_sat(const char *, const cdf_sat_t *, size_t); +void cdf_dump(void *, size_t); +void cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *); +void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, + const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *); +void cdf_dump_property_info(const cdf_property_info_t *, size_t); +void cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *); +#endif + + +#endif /* _H_CDF_ */ diff --git a/ext/fileinfo/libmagic/cdf_time.c b/ext/fileinfo/libmagic/cdf_time.c new file mode 100644 index 0000000..b75aa5b --- /dev/null +++ b/ext/fileinfo/libmagic/cdf_time.c @@ -0,0 +1,198 @@ +/*- + * Copyright (c) 2008 Christos Zoulas + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: cdf_time.c,v 1.11 2011/12/13 13:48:41 christos Exp $") +#endif + +#include <time.h> +#ifdef TEST +#include <err.h> +#endif +#include <string.h> + +#include "cdf.h" + +#define isleap(y) ((((y) % 4) == 0) && \ + ((((y) % 100) != 0) || (((y) % 400) == 0))) + +static const int mdays[] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +}; + +/* + * Return the number of days between jan 01 1601 and jan 01 of year. + */ +static int +cdf_getdays(int year) +{ + int days = 0; + int y; + + for (y = CDF_BASE_YEAR; y < year; y++) + days += isleap(y) + 365; + + return days; +} + +/* + * Return the day within the month + */ +static int +cdf_getday(int year, int days) +{ + size_t m; + + for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) { + int sub = mdays[m] + (m == 1 && isleap(year)); + if (days < sub) + return days; + days -= sub; + } + return days; +} + +/* + * Return the 0...11 month number. + */ +static int +cdf_getmonth(int year, int days) +{ + size_t m; + + for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) { + days -= mdays[m]; + if (m == 1 && isleap(year)) + days--; + if (days <= 0) + return (int)m; + } + return (int)m; +} + +int +cdf_timestamp_to_timespec(struct timeval *ts, cdf_timestamp_t t) +{ + struct tm tm; +#ifdef HAVE_STRUCT_TM_TM_ZONE + static char UTC[] = "UTC"; +#endif + int rdays; + + /* Time interval, in microseconds */ + ts->tv_usec = (t % CDF_TIME_PREC) * CDF_TIME_PREC; + + t /= CDF_TIME_PREC; + tm.tm_sec = (int)(t % 60); + t /= 60; + + tm.tm_min = (int)(t % 60); + t /= 60; + + tm.tm_hour = (int)(t % 24); + t /= 24; + + /* XXX: Approx */ + tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365)); + + rdays = cdf_getdays(tm.tm_year); + t -= rdays - 1; + tm.tm_mday = cdf_getday(tm.tm_year, (int)t); + tm.tm_mon = cdf_getmonth(tm.tm_year, (int)t); + tm.tm_wday = 0; + tm.tm_yday = 0; + tm.tm_isdst = 0; +#ifdef HAVE_STRUCT_TM_TM_GMTOFF + tm.tm_gmtoff = 0; +#endif +#ifdef HAVE_STRUCT_TM_TM_ZONE + tm.tm_zone = UTC; +#endif + tm.tm_year -= 1900; + ts->tv_sec = mktime(&tm); + if (ts->tv_sec == -1) { + errno = EINVAL; + return -1; + } + return 0; +} + +int +/*ARGSUSED*/ +cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timeval *ts) +{ +#ifndef __lint__ + (void)&t; + (void)&ts; +#endif +#ifdef notyet + struct tm tm; + if (gmtime_r(&ts->ts_sec, &tm) == NULL) { + errno = EINVAL; + return -1; + } + *t = (ts->ts_usec / CDF_TIME_PREC) * CDF_TIME_PREC; + *t = tm.tm_sec; + *t += tm.tm_min * 60; + *t += tm.tm_hour * 60 * 60; + *t += tm.tm_mday * 60 * 60 * 24; +#endif + return 0; +} + +char * +cdf_ctime(const time_t *sec) +{ + static char ctbuf[26]; + char *ptr = ctime(sec); + if (ptr != NULL) + return ptr; + (void)snprintf(ctbuf, sizeof(ctbuf), "*Bad* 0x%16.16llx\n", + (long long)*sec); + return ctbuf; +} + + +#ifdef TEST +int +main(int argc, char *argv[]) +{ + struct timeval ts; + static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL; + static const char *ref = "Sat Apr 23 01:30:00 1977"; + char *p, *q; + + cdf_timestamp_to_timespec(&ts, tst); + p = cdf_ctime(&ts.tv_sec); + if ((q = strchr(p, '\n')) != NULL) + *q = '\0'; + if (strcmp(ref, p) != 0) + errx(1, "Error date %s != %s\n", ref, p); + return 0; +} +#endif diff --git a/ext/fileinfo/libmagic/compress.c b/ext/fileinfo/libmagic/compress.c new file mode 100644 index 0000000..4d349cd --- /dev/null +++ b/ext/fileinfo/libmagic/compress.c @@ -0,0 +1,504 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * compress routines: + * zmagic() - returns 0 if not recognized, uncompresses and prints + * information if recognized + * uncompress(method, old, n, newch) - uncompress old into new, + * using method, return sizeof new + */ +#include "config.h" +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: compress.c,v 1.68 2011/12/08 12:38:24 rrt Exp $") +#endif + +#include "magic.h" +#include <stdlib.h> +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <string.h> +#include <errno.h> +#include <sys/types.h> +#ifndef PHP_WIN32 +#include <sys/ioctl.h> +#endif +#ifdef HAVE_SYS_WAIT_H +#include <sys/wait.h> +#endif +#if defined(HAVE_SYS_TIME_H) +#include <sys/time.h> +#endif +#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ) +#define BUILTIN_DECOMPRESS +#include <zlib.h> +#endif + +#undef FIONREAD + + +private const struct { + const char magic[8]; + size_t maglen; + const char *argv[3]; + int silent; +} compr[] = { + { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */ + /* Uncompress can get stuck; so use gzip first if we have it + * Idea from Damien Clark, thanks! */ + { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */ + { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */ + { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */ + { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */ + /* the standard pack utilities do not accept standard input */ + { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */ + { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */ + /* ...only first file examined */ + { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */ + { "LZIP", 4, { "lzip", "-cdq", NULL }, 1 }, + { "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 }, /* XZ Utils */ + { "LRZI", 4, { "lrzip", "-dqo-", NULL }, 1 }, /* LRZIP */ +}; + +#define NODATA ((size_t)~0) + +private ssize_t swrite(int, const void *, size_t); +#ifdef PHP_FILEINFO_UNCOMPRESS +private size_t uncompressbuf(struct magic_set *, int, size_t, + const unsigned char *, unsigned char **, size_t); +#ifdef BUILTIN_DECOMPRESS +private size_t uncompressgzipped(struct magic_set *, const unsigned char *, + unsigned char **, size_t); +#endif + +protected int +file_zmagic(struct magic_set *ms, int fd, const char *name, + const unsigned char *buf, size_t nbytes) +{ + unsigned char *newbuf = NULL; + size_t i, nsz; + int rv = 0; + int mime = ms->flags & MAGIC_MIME; + size_t ncompr; + + if ((ms->flags & MAGIC_COMPRESS) == 0) + return 0; + + ncompr = sizeof(compr) / sizeof(compr[0]); + + for (i = 0; i < ncompr; i++) { + if (nbytes < compr[i].maglen) + continue; + if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 && + (nsz = uncompressbuf(ms, fd, i, buf, &newbuf, + nbytes)) != NODATA) { + ms->flags &= ~MAGIC_COMPRESS; + rv = -1; + if (file_buffer(ms, -1, name, newbuf, nsz) == -1) + goto error; + + if (mime == MAGIC_MIME || mime == 0) { + if (file_printf(ms, mime ? + " compressed-encoding=" : " (") == -1) + goto error; + } + + if ((mime == 0 || mime & MAGIC_MIME_ENCODING) && + file_buffer(ms, -1, NULL, buf, nbytes) == -1) + goto error; + + if (!mime && file_printf(ms, ")") == -1) + goto error; + rv = 1; + break; + } + } +error: + if (newbuf) + efree(newbuf); + ms->flags |= MAGIC_COMPRESS; + return rv; +} +#endif +/* + * `safe' write for sockets and pipes. + */ +private ssize_t +swrite(int fd, const void *buf, size_t n) +{ + ssize_t rv; + size_t rn = n; + + do + switch (rv = write(fd, buf, n)) { + case -1: + if (errno == EINTR) + continue; + return -1; + default: + n -= rv; + buf = CAST(const char *, buf) + rv; + break; + } + while (n > 0); + return rn; +} + + +/* + * `safe' read for sockets and pipes. + */ +protected ssize_t +sread(int fd, void *buf, size_t n, int canbepipe) +{ + ssize_t rv; +#ifdef FIONREAD + int t = 0; +#endif + size_t rn = n; + + if (fd == STDIN_FILENO) + goto nocheck; + +#ifdef FIONREAD + if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) { +#ifdef FD_ZERO + int cnt; + for (cnt = 0;; cnt++) { + fd_set check; + struct timeval tout = {0, 100 * 1000}; + int selrv; + + FD_ZERO(&check); + FD_SET(fd, &check); + + /* + * Avoid soft deadlock: do not read if there + * is nothing to read from sockets and pipes. + */ + selrv = select(fd + 1, &check, NULL, NULL, &tout); + if (selrv == -1) { + if (errno == EINTR || errno == EAGAIN) + continue; + } else if (selrv == 0 && cnt >= 5) { + return 0; + } else + break; + } +#endif + (void)ioctl(fd, FIONREAD, &t); + } + + if (t > 0 && (size_t)t < n) { + n = t; + rn = n; + } +#endif + +nocheck: + do + switch ((rv = FINFO_READ_FUNC(fd, buf, n))) { + case -1: + if (errno == EINTR) + continue; + return -1; + case 0: + return rn - n; + default: + n -= rv; + buf = ((char *)buf) + rv; + break; + } + while (n > 0); + return rn; +} + +protected int +file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, + size_t nbytes) +{ + char buf[4096]; + ssize_t r; + int tfd; +#ifdef HAVE_MKSTEMP + int te; +#endif + + (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf); +#ifndef HAVE_MKSTEMP + { + char *ptr = mktemp(buf); + tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600); + r = errno; + (void)unlink(ptr); + errno = r; + } +#else + tfd = mkstemp(buf); + te = errno; + (void)unlink(buf); + errno = te; +#endif + if (tfd == -1) { + file_error(ms, errno, + "cannot create temporary file for pipe copy"); + return -1; + } + + if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes) + r = 1; + else { + while ((r = sread(fd, buf, sizeof(buf), 1)) > 0) + if (swrite(tfd, buf, (size_t)r) != r) + break; + } + + switch (r) { + case -1: + file_error(ms, errno, "error copying from pipe to temp file"); + return -1; + case 0: + break; + default: + file_error(ms, errno, "error while writing to temp file"); + return -1; + } + + /* + * We duplicate the file descriptor, because fclose on a + * tmpfile will delete the file, but any open descriptors + * can still access the phantom inode. + */ + if ((fd = dup2(tfd, fd)) == -1) { + file_error(ms, errno, "could not dup descriptor for temp file"); + return -1; + } + (void)close(tfd); + if (FINFO_LSEEK_FUNC(fd, (off_t)0, SEEK_SET) == (off_t)-1) { + file_badseek(ms); + return -1; + } + return fd; +} + +#ifdef PHP_FILEINFO_UNCOMPRESS +#ifdef BUILTIN_DECOMPRESS + +#define FHCRC (1 << 1) +#define FEXTRA (1 << 2) +#define FNAME (1 << 3) +#define FCOMMENT (1 << 4) + +private size_t +uncompressgzipped(struct magic_set *ms, const unsigned char *old, + unsigned char **newch, size_t n) +{ + unsigned char flg = old[3]; + size_t data_start = 10; + z_stream z; + int rc; + + if (flg & FEXTRA) { + if (data_start+1 >= n) + return 0; + data_start += 2 + old[data_start] + old[data_start + 1] * 256; + } + if (flg & FNAME) { + while(data_start < n && old[data_start]) + data_start++; + data_start++; + } + if(flg & FCOMMENT) { + while(data_start < n && old[data_start]) + data_start++; + data_start++; + } + if(flg & FHCRC) + data_start += 2; + + if (data_start >= n) + return 0; + *newch = (unsigned char *)emalloc(HOWMANY + 1)); + + /* XXX: const castaway, via strchr */ + z.next_in = (Bytef *)strchr((const char *)old + data_start, + old[data_start]); + z.avail_in = CAST(uint32_t, (n - data_start)); + z.next_out = *newch; + z.avail_out = HOWMANY; + z.zalloc = Z_NULL; + z.zfree = Z_NULL; + z.opaque = Z_NULL; + + /* LINTED bug in header macro */ + rc = inflateInit2(&z, -15); + if (rc != Z_OK) { + file_error(ms, 0, "zlib: %s", z.msg); + return 0; + } + + rc = inflate(&z, Z_SYNC_FLUSH); + if (rc != Z_OK && rc != Z_STREAM_END) { + file_error(ms, 0, "zlib: %s", z.msg); + return 0; + } + + n = (size_t)z.total_out; + (void)inflateEnd(&z); + + /* let's keep the nul-terminate tradition */ + (*newch)[n] = '\0'; + + return n; +} +#endif + +private size_t +uncompressbuf(struct magic_set *ms, int fd, size_t method, + const unsigned char *old, unsigned char **newch, size_t n) +{ + int fdin[2], fdout[2]; + ssize_t r; + pid_t pid; + +#ifdef BUILTIN_DECOMPRESS + /* FIXME: This doesn't cope with bzip2 */ + if (method == 2) + return uncompressgzipped(ms, old, newch, n); +#endif + (void)fflush(stdout); + (void)fflush(stderr); + + if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) { + file_error(ms, errno, "cannot create pipe"); + return NODATA; + } + switch (pid = fork()) { + case 0: /* child */ + (void) close(0); + if (fd != -1) { + (void) dup(fd); + (void) FINFO_LSEEK_FUNC(0, (off_t)0, SEEK_SET); + } else { + (void) dup(fdin[0]); + (void) close(fdin[0]); + (void) close(fdin[1]); + } + + (void) close(1); + (void) dup(fdout[1]); + (void) close(fdout[0]); + (void) close(fdout[1]); +#ifndef DEBUG + if (compr[method].silent) + (void)close(2); +#endif + + (void)execvp(compr[method].argv[0], + (char *const *)(intptr_t)compr[method].argv); +#ifdef DEBUG + (void)fprintf(stderr, "exec `%s' failed (%s)\n", + compr[method].argv[0], strerror(errno)); +#endif + exit(1); + /*NOTREACHED*/ + case -1: + file_error(ms, errno, "could not fork"); + return NODATA; + + default: /* parent */ + (void) close(fdout[1]); + if (fd == -1) { + (void) close(fdin[0]); + /* + * fork again, to avoid blocking because both + * pipes filled + */ + switch (fork()) { + case 0: /* child */ + (void)close(fdout[0]); + if (swrite(fdin[1], old, n) != (ssize_t)n) { +#ifdef DEBUG + (void)fprintf(stderr, + "Write failed (%s)\n", + strerror(errno)); +#endif + exit(1); + } + exit(0); + /*NOTREACHED*/ + + case -1: +#ifdef DEBUG + (void)fprintf(stderr, "Fork failed (%s)\n", + strerror(errno)); +#endif + exit(1); + /*NOTREACHED*/ + + default: /* parent */ + break; + } + (void) close(fdin[1]); + fdin[1] = -1; + } + + *newch = (unsigned char *) emalloc(HOWMANY + 1); + + if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) { +#ifdef DEBUG + (void)fprintf(stderr, "Read failed (%s)\n", + strerror(errno)); +#endif + efree(*newch); + n = 0; + newch[0] = '\0'; + goto err; + } else { + n = r; + } + /* NUL terminate, as every buffer is handled here. */ + (*newch)[n] = '\0'; +err: + if (fdin[1] != -1) + (void) close(fdin[1]); + (void) close(fdout[0]); +#ifdef WNOHANG + while (waitpid(pid, NULL, WNOHANG) != -1) + continue; +#else + (void)wait(NULL); +#endif + (void) close(fdin[0]); + + return n; + } +} +#endif /* if PHP_FILEINFO_UNCOMPRESS */ diff --git a/ext/fileinfo/libmagic/config.h b/ext/fileinfo/libmagic/config.h new file mode 100644 index 0000000..22ce7f6 --- /dev/null +++ b/ext/fileinfo/libmagic/config.h @@ -0,0 +1 @@ +#include "php.h" diff --git a/ext/fileinfo/libmagic/elfclass.h b/ext/fileinfo/libmagic/elfclass.h new file mode 100644 index 0000000..2e7741b --- /dev/null +++ b/ext/fileinfo/libmagic/elfclass.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) Christos Zoulas 2008. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + if (nbytes <= sizeof(elfhdr)) + return 0; + + u.l = 1; + (void)memcpy(&elfhdr, buf, sizeof elfhdr); + swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[EI_DATA]; + + type = elf_getu16(swap, elfhdr.e_type); + switch (type) { +#ifdef ELFCORE + case ET_CORE: + flags |= FLAGS_IS_CORE; + if (dophn_core(ms, clazz, swap, fd, + (off_t)elf_getu(swap, elfhdr.e_phoff), + elf_getu16(swap, elfhdr.e_phnum), + (size_t)elf_getu16(swap, elfhdr.e_phentsize), + fsize, &flags) == -1) + return -1; + break; +#endif + case ET_EXEC: + case ET_DYN: + if (dophn_exec(ms, clazz, swap, fd, + (off_t)elf_getu(swap, elfhdr.e_phoff), + elf_getu16(swap, elfhdr.e_phnum), + (size_t)elf_getu16(swap, elfhdr.e_phentsize), + fsize, &flags, elf_getu16(swap, elfhdr.e_shnum)) + == -1) + return -1; + /*FALLTHROUGH*/ + case ET_REL: + if (doshn(ms, clazz, swap, fd, + (off_t)elf_getu(swap, elfhdr.e_shoff), + elf_getu16(swap, elfhdr.e_shnum), + (size_t)elf_getu16(swap, elfhdr.e_shentsize), + fsize, &flags, elf_getu16(swap, elfhdr.e_machine)) == -1) + return -1; + break; + + default: + break; + } + return 1; diff --git a/ext/fileinfo/libmagic/encoding.c b/ext/fileinfo/libmagic/encoding.c new file mode 100644 index 0000000..dee57a6 --- /dev/null +++ b/ext/fileinfo/libmagic/encoding.c @@ -0,0 +1,500 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * Encoding -- determine the character encoding of a text file. + * + * Joerg Wunsch <joerg@freebsd.org> wrote the original support for 8-bit + * international characters. + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: encoding.c,v 1.7 2012/01/24 19:02:02 christos Exp $") +#endif /* lint */ + +#include "magic.h" +#include <string.h> +#include <memory.h> +#include <stdlib.h> + + +private int looks_ascii(const unsigned char *, size_t, unichar *, size_t *); +private int looks_utf8_with_BOM(const unsigned char *, size_t, unichar *, + size_t *); +private int looks_ucs16(const unsigned char *, size_t, unichar *, size_t *); +private int looks_latin1(const unsigned char *, size_t, unichar *, size_t *); +private int looks_extended(const unsigned char *, size_t, unichar *, size_t *); +private void from_ebcdic(const unsigned char *, size_t, unsigned char *); + +#ifdef DEBUG_ENCODING +#define DPRINTF(a) printf a +#else +#define DPRINTF(a) +#endif + +/* + * Try to determine whether text is in some character code we can + * identify. Each of these tests, if it succeeds, will leave + * the text converted into one-unichar-per-character Unicode in + * ubuf, and the number of characters converted in ulen. + */ +protected int +file_encoding(struct magic_set *ms, const unsigned char *buf, size_t nbytes, unichar **ubuf, size_t *ulen, const char **code, const char **code_mime, const char **type) +{ + size_t mlen; + int rv = 1, ucs_type; + unsigned char *nbuf = NULL; + + *type = "text"; + mlen = (nbytes + 1) * sizeof(nbuf[0]); + if ((nbuf = CAST(unsigned char *, calloc((size_t)1, mlen))) == NULL) { + file_oomem(ms, mlen); + goto done; + } + mlen = (nbytes + 1) * sizeof((*ubuf)[0]); + if ((*ubuf = CAST(unichar *, calloc((size_t)1, mlen))) == NULL) { + file_oomem(ms, mlen); + goto done; + } + + if (looks_ascii(buf, nbytes, *ubuf, ulen)) { + DPRINTF(("ascii %" SIZE_T_FORMAT "u\n", *ulen)); + *code = "ASCII"; + *code_mime = "us-ascii"; + } else if (looks_utf8_with_BOM(buf, nbytes, *ubuf, ulen) > 0) { + DPRINTF(("utf8/bom %" SIZE_T_FORMAT "u\n", *ulen)); + *code = "UTF-8 Unicode (with BOM)"; + *code_mime = "utf-8"; + } else if (file_looks_utf8(buf, nbytes, *ubuf, ulen) > 1) { + DPRINTF(("utf8 %" SIZE_T_FORMAT "u\n", *ulen)); + *code = "UTF-8 Unicode (with BOM)"; + *code = "UTF-8 Unicode"; + *code_mime = "utf-8"; + } else if ((ucs_type = looks_ucs16(buf, nbytes, *ubuf, ulen)) != 0) { + if (ucs_type == 1) { + *code = "Little-endian UTF-16 Unicode"; + *code_mime = "utf-16le"; + } else { + *code = "Big-endian UTF-16 Unicode"; + *code_mime = "utf-16be"; + } + DPRINTF(("ucs16 %" SIZE_T_FORMAT "u\n", *ulen)); + } else if (looks_latin1(buf, nbytes, *ubuf, ulen)) { + DPRINTF(("latin1 %" SIZE_T_FORMAT "u\n", *ulen)); + *code = "ISO-8859"; + *code_mime = "iso-8859-1"; + } else if (looks_extended(buf, nbytes, *ubuf, ulen)) { + DPRINTF(("extended %" SIZE_T_FORMAT "u\n", *ulen)); + *code = "Non-ISO extended-ASCII"; + *code_mime = "unknown-8bit"; + } else { + from_ebcdic(buf, nbytes, nbuf); + + if (looks_ascii(nbuf, nbytes, *ubuf, ulen)) { + DPRINTF(("ebcdic %" SIZE_T_FORMAT "u\n", *ulen)); + *code = "EBCDIC"; + *code_mime = "ebcdic"; + } else if (looks_latin1(nbuf, nbytes, *ubuf, ulen)) { + DPRINTF(("ebcdic/international %" SIZE_T_FORMAT "u\n", + *ulen)); + *code = "International EBCDIC"; + *code_mime = "ebcdic"; + } else { /* Doesn't look like text at all */ + DPRINTF(("binary\n")); + rv = 0; + *type = "binary"; + } + } + + done: + free(nbuf); + + return rv; +} + +/* + * This table reflects a particular philosophy about what constitutes + * "text," and there is room for disagreement about it. + * + * Version 3.31 of the file command considered a file to be ASCII if + * each of its characters was approved by either the isascii() or + * isalpha() function. On most systems, this would mean that any + * file consisting only of characters in the range 0x00 ... 0x7F + * would be called ASCII text, but many systems might reasonably + * consider some characters outside this range to be alphabetic, + * so the file command would call such characters ASCII. It might + * have been more accurate to call this "considered textual on the + * local system" than "ASCII." + * + * It considered a file to be "International language text" if each + * of its characters was either an ASCII printing character (according + * to the real ASCII standard, not the above test), a character in + * the range 0x80 ... 0xFF, or one of the following control characters: + * backspace, tab, line feed, vertical tab, form feed, carriage return, + * escape. No attempt was made to determine the language in which files + * of this type were written. + * + * + * The table below considers a file to be ASCII if all of its characters + * are either ASCII printing characters (again, according to the X3.4 + * standard, not isascii()) or any of the following controls: bell, + * backspace, tab, line feed, form feed, carriage return, esc, nextline. + * + * I include bell because some programs (particularly shell scripts) + * use it literally, even though it is rare in normal text. I exclude + * vertical tab because it never seems to be used in real text. I also + * include, with hesitation, the X3.64/ECMA-43 control nextline (0x85), + * because that's what the dd EBCDIC->ASCII table maps the EBCDIC newline + * character to. It might be more appropriate to include it in the 8859 + * set instead of the ASCII set, but it's got to be included in *something* + * we recognize or EBCDIC files aren't going to be considered textual. + * Some old Unix source files use SO/SI (^N/^O) to shift between Greek + * and Latin characters, so these should possibly be allowed. But they + * make a real mess on VT100-style displays if they're not paired properly, + * so we are probably better off not calling them text. + * + * A file is considered to be ISO-8859 text if its characters are all + * either ASCII, according to the above definition, or printing characters + * from the ISO-8859 8-bit extension, characters 0xA0 ... 0xFF. + * + * Finally, a file is considered to be international text from some other + * character code if its characters are all either ISO-8859 (according to + * the above definition) or characters in the range 0x80 ... 0x9F, which + * ISO-8859 considers to be control characters but the IBM PC and Macintosh + * consider to be printing characters. + */ + +#define F 0 /* character never appears in text */ +#define T 1 /* character appears in plain ASCII text */ +#define I 2 /* character appears in ISO-8859 text */ +#define X 3 /* character appears in non-ISO extended ASCII (Mac, IBM PC) */ + +private char text_chars[256] = { + /* BEL BS HT LF FF CR */ + F, F, F, F, F, F, F, T, T, T, T, F, T, T, F, F, /* 0x0X */ + /* ESC */ + F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F, /* 0x1X */ + T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x2X */ + T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x3X */ + T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x4X */ + T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x5X */ + T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x6X */ + T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F, /* 0x7X */ + /* NEL */ + X, X, X, X, X, T, X, X, X, X, X, X, X, X, X, X, /* 0x8X */ + X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, /* 0x9X */ + I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, /* 0xaX */ + I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, /* 0xbX */ + I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, /* 0xcX */ + I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, /* 0xdX */ + I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, /* 0xeX */ + I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I /* 0xfX */ +}; + +private int +looks_ascii(const unsigned char *buf, size_t nbytes, unichar *ubuf, + size_t *ulen) +{ + size_t i; + + *ulen = 0; + + for (i = 0; i < nbytes; i++) { + int t = text_chars[buf[i]]; + + if (t != T) + return 0; + + ubuf[(*ulen)++] = buf[i]; + } + + return 1; +} + +private int +looks_latin1(const unsigned char *buf, size_t nbytes, unichar *ubuf, size_t *ulen) +{ + size_t i; + + *ulen = 0; + + for (i = 0; i < nbytes; i++) { + int t = text_chars[buf[i]]; + + if (t != T && t != I) + return 0; + + ubuf[(*ulen)++] = buf[i]; + } + + return 1; +} + +private int +looks_extended(const unsigned char *buf, size_t nbytes, unichar *ubuf, + size_t *ulen) +{ + size_t i; + + *ulen = 0; + + for (i = 0; i < nbytes; i++) { + int t = text_chars[buf[i]]; + + if (t != T && t != I && t != X) + return 0; + + ubuf[(*ulen)++] = buf[i]; + } + + return 1; +} + +/* + * Decide whether some text looks like UTF-8. Returns: + * + * -1: invalid UTF-8 + * 0: uses odd control characters, so doesn't look like text + * 1: 7-bit text + * 2: definitely UTF-8 text (valid high-bit set bytes) + * + * If ubuf is non-NULL on entry, text is decoded into ubuf, *ulen; + * ubuf must be big enough! + */ +protected int +file_looks_utf8(const unsigned char *buf, size_t nbytes, unichar *ubuf, size_t *ulen) +{ + size_t i; + int n; + unichar c; + int gotone = 0, ctrl = 0; + + if (ubuf) + *ulen = 0; + + for (i = 0; i < nbytes; i++) { + if ((buf[i] & 0x80) == 0) { /* 0xxxxxxx is plain ASCII */ + /* + * Even if the whole file is valid UTF-8 sequences, + * still reject it if it uses weird control characters. + */ + + if (text_chars[buf[i]] != T) + ctrl = 1; + + if (ubuf) + ubuf[(*ulen)++] = buf[i]; + } else if ((buf[i] & 0x40) == 0) { /* 10xxxxxx never 1st byte */ + return -1; + } else { /* 11xxxxxx begins UTF-8 */ + int following; + + if ((buf[i] & 0x20) == 0) { /* 110xxxxx */ + c = buf[i] & 0x1f; + following = 1; + } else if ((buf[i] & 0x10) == 0) { /* 1110xxxx */ + c = buf[i] & 0x0f; + following = 2; + } else if ((buf[i] & 0x08) == 0) { /* 11110xxx */ + c = buf[i] & 0x07; + following = 3; + } else if ((buf[i] & 0x04) == 0) { /* 111110xx */ + c = buf[i] & 0x03; + following = 4; + } else if ((buf[i] & 0x02) == 0) { /* 1111110x */ + c = buf[i] & 0x01; + following = 5; + } else + return -1; + + for (n = 0; n < following; n++) { + i++; + if (i >= nbytes) + goto done; + + if ((buf[i] & 0x80) == 0 || (buf[i] & 0x40)) + return -1; + + c = (c << 6) + (buf[i] & 0x3f); + } + + if (ubuf) + ubuf[(*ulen)++] = c; + gotone = 1; + } + } +done: + return ctrl ? 0 : (gotone ? 2 : 1); +} + +/* + * Decide whether some text looks like UTF-8 with BOM. If there is no + * BOM, return -1; otherwise return the result of looks_utf8 on the + * rest of the text. + */ +private int +looks_utf8_with_BOM(const unsigned char *buf, size_t nbytes, unichar *ubuf, + size_t *ulen) +{ + if (nbytes > 3 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf) + return file_looks_utf8(buf + 3, nbytes - 3, ubuf, ulen); + else + return -1; +} + +private int +looks_ucs16(const unsigned char *buf, size_t nbytes, unichar *ubuf, + size_t *ulen) +{ + int bigend; + size_t i; + + if (nbytes < 2) + return 0; + + if (buf[0] == 0xff && buf[1] == 0xfe) + bigend = 0; + else if (buf[0] == 0xfe && buf[1] == 0xff) + bigend = 1; + else + return 0; + + *ulen = 0; + + for (i = 2; i + 1 < nbytes; i += 2) { + /* XXX fix to properly handle chars > 65536 */ + + if (bigend) + ubuf[(*ulen)++] = buf[i + 1] + 256 * buf[i]; + else + ubuf[(*ulen)++] = buf[i] + 256 * buf[i + 1]; + + if (ubuf[*ulen - 1] == 0xfffe) + return 0; + if (ubuf[*ulen - 1] < 128 && + text_chars[(size_t)ubuf[*ulen - 1]] != T) + return 0; + } + + return 1 + bigend; +} + +#undef F +#undef T +#undef I +#undef X + +/* + * This table maps each EBCDIC character to an (8-bit extended) ASCII + * character, as specified in the rationale for the dd(1) command in + * draft 11.2 (September, 1991) of the POSIX P1003.2 standard. + * + * Unfortunately it does not seem to correspond exactly to any of the + * five variants of EBCDIC documented in IBM's _Enterprise Systems + * Architecture/390: Principles of Operation_, SA22-7201-06, Seventh + * Edition, July, 1999, pp. I-1 - I-4. + * + * Fortunately, though, all versions of EBCDIC, including this one, agree + * on most of the printing characters that also appear in (7-bit) ASCII. + * Of these, only '|', '!', '~', '^', '[', and ']' are in question at all. + * + * Fortunately too, there is general agreement that codes 0x00 through + * 0x3F represent control characters, 0x41 a nonbreaking space, and the + * remainder printing characters. + * + * This is sufficient to allow us to identify EBCDIC text and to distinguish + * between old-style and internationalized examples of text. + */ + +private unsigned char ebcdic_to_ascii[] = { + 0, 1, 2, 3, 156, 9, 134, 127, 151, 141, 142, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 157, 133, 8, 135, 24, 25, 146, 143, 28, 29, 30, 31, +128, 129, 130, 131, 132, 10, 23, 27, 136, 137, 138, 139, 140, 5, 6, 7, +144, 145, 22, 147, 148, 149, 150, 4, 152, 153, 154, 155, 20, 21, 158, 26, +' ', 160, 161, 162, 163, 164, 165, 166, 167, 168, 213, '.', '<', '(', '+', '|', +'&', 169, 170, 171, 172, 173, 174, 175, 176, 177, '!', '$', '*', ')', ';', '~', +'-', '/', 178, 179, 180, 181, 182, 183, 184, 185, 203, ',', '%', '_', '>', '?', +186, 187, 188, 189, 190, 191, 192, 193, 194, '`', ':', '#', '@', '\'','=', '"', +195, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 196, 197, 198, 199, 200, 201, +202, 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', '^', 204, 205, 206, 207, 208, +209, 229, 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 210, 211, 212, '[', 214, 215, +216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, ']', 230, 231, +'{', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 232, 233, 234, 235, 236, 237, +'}', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 238, 239, 240, 241, 242, 243, +'\\',159, 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 244, 245, 246, 247, 248, 249, +'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 250, 251, 252, 253, 254, 255 +}; + +#ifdef notdef +/* + * The following EBCDIC-to-ASCII table may relate more closely to reality, + * or at least to modern reality. It comes from + * + * http://ftp.s390.ibm.com/products/oe/bpxqp9.html + * + * and maps the characters of EBCDIC code page 1047 (the code used for + * Unix-derived software on IBM's 390 systems) to the corresponding + * characters from ISO 8859-1. + * + * If this table is used instead of the above one, some of the special + * cases for the NEL character can be taken out of the code. + */ + +private unsigned char ebcdic_1047_to_8859[] = { +0x00,0x01,0x02,0x03,0x9C,0x09,0x86,0x7F,0x97,0x8D,0x8E,0x0B,0x0C,0x0D,0x0E,0x0F, +0x10,0x11,0x12,0x13,0x9D,0x0A,0x08,0x87,0x18,0x19,0x92,0x8F,0x1C,0x1D,0x1E,0x1F, +0x80,0x81,0x82,0x83,0x84,0x85,0x17,0x1B,0x88,0x89,0x8A,0x8B,0x8C,0x05,0x06,0x07, +0x90,0x91,0x16,0x93,0x94,0x95,0x96,0x04,0x98,0x99,0x9A,0x9B,0x14,0x15,0x9E,0x1A, +0x20,0xA0,0xE2,0xE4,0xE0,0xE1,0xE3,0xE5,0xE7,0xF1,0xA2,0x2E,0x3C,0x28,0x2B,0x7C, +0x26,0xE9,0xEA,0xEB,0xE8,0xED,0xEE,0xEF,0xEC,0xDF,0x21,0x24,0x2A,0x29,0x3B,0x5E, +0x2D,0x2F,0xC2,0xC4,0xC0,0xC1,0xC3,0xC5,0xC7,0xD1,0xA6,0x2C,0x25,0x5F,0x3E,0x3F, +0xF8,0xC9,0xCA,0xCB,0xC8,0xCD,0xCE,0xCF,0xCC,0x60,0x3A,0x23,0x40,0x27,0x3D,0x22, +0xD8,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0xAB,0xBB,0xF0,0xFD,0xFE,0xB1, +0xB0,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0xAA,0xBA,0xE6,0xB8,0xC6,0xA4, +0xB5,0x7E,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0xA1,0xBF,0xD0,0x5B,0xDE,0xAE, +0xAC,0xA3,0xA5,0xB7,0xA9,0xA7,0xB6,0xBC,0xBD,0xBE,0xDD,0xA8,0xAF,0x5D,0xB4,0xD7, +0x7B,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0xAD,0xF4,0xF6,0xF2,0xF3,0xF5, +0x7D,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0xB9,0xFB,0xFC,0xF9,0xFA,0xFF, +0x5C,0xF7,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0xB2,0xD4,0xD6,0xD2,0xD3,0xD5, +0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0xB3,0xDB,0xDC,0xD9,0xDA,0x9F +}; +#endif + +/* + * Copy buf[0 ... nbytes-1] into out[], translating EBCDIC to ASCII. + */ +private void +from_ebcdic(const unsigned char *buf, size_t nbytes, unsigned char *out) +{ + size_t i; + + for (i = 0; i < nbytes; i++) { + out[i] = ebcdic_to_ascii[buf[i]]; + } +} diff --git a/ext/fileinfo/libmagic/file.h b/ext/fileinfo/libmagic/file.h new file mode 100644 index 0000000..3879b47 --- /dev/null +++ b/ext/fileinfo/libmagic/file.h @@ -0,0 +1,498 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * file.h - definitions for file(1) program + * @(#)$File: file.h,v 1.135 2011/09/20 15:30:14 christos Exp $ + */ + +#ifndef __file_h__ +#define __file_h__ + +#include "config.h" + +#ifdef PHP_WIN32 + #ifdef _WIN64 + #define SIZE_T_FORMAT "I64" + #else + #define SIZE_T_FORMAT "" + #endif + #define INT64_T_FORMAT "I64" +#else + #define SIZE_T_FORMAT "z" + #define INT64_T_FORMAT "ll" +#endif + +#include <stdio.h> /* Include that here, to make sure __P gets defined */ +#include <errno.h> +#include <fcntl.h> /* For open and flags */ +#ifdef HAVE_STDINT_H +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS +#endif +#include <stdint.h> +#endif +#ifdef HAVE_INTTYPES_H +#include <inttypes.h> +#endif +#ifdef PHP_WIN32 +#include "win32/php_stdint.h" +#endif + +#include "php.h" +#include "ext/standard/php_string.h" +#include "ext/pcre/php_pcre.h" + +#include <sys/types.h> +#ifdef PHP_WIN32 +#include "win32/param.h" +#else +#include <sys/param.h> +#endif +/* Do this here and now, because struct stat gets re-defined on solaris */ +#include <sys/stat.h> +#include <stdarg.h> + +#define ENABLE_CONDITIONALS + +#ifndef MAGIC +#define MAGIC "/etc/magic" +#endif + +#if defined(__EMX__) || defined(PHP_WIN32) +#define PATHSEP ';' +#else +#define PATHSEP ':' +#endif + +#define private static +#ifndef protected +#define protected +#endif +#define public + +#ifndef __arraycount +#define __arraycount(a) (sizeof(a) / sizeof(a[0])) +#endif + +#ifndef __GNUC_PREREQ__ +#ifdef __GNUC__ +#define __GNUC_PREREQ__(x, y) \ + ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || \ + (__GNUC__ > (x))) +#else +#define __GNUC_PREREQ__(x, y) 0 +#endif +#endif + +#ifndef MIN +#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#endif + +#ifndef MAX +#define MAX(a,b) (((a) > (b)) ? (a) : (b)) +#endif + +#ifndef HOWMANY +# define HOWMANY (256 * 1024) /* how much of the file to look at */ +#endif +#define MAXMAGIS 8192 /* max entries in any one magic file + or directory */ +#define MAXDESC 64 /* max leng of text description/MIME type */ +#define MAXstring 64 /* max leng of "string" types */ + +#define MAGICNO 0xF11E041C +#define VERSIONNO 8 +#define FILE_MAGICSIZE 232 + +#define FILE_LOAD 0 +#define FILE_CHECK 1 +#define FILE_COMPILE 2 +#define FILE_LIST 3 + +union VALUETYPE { + uint8_t b; + uint16_t h; + uint32_t l; + uint64_t q; + uint8_t hs[2]; /* 2 bytes of a fixed-endian "short" */ + uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */ + uint8_t hq[8]; /* 8 bytes of a fixed-endian "quad" */ + char s[MAXstring]; /* the search string or regex pattern */ + unsigned char us[MAXstring]; + float f; + double d; +}; + +struct magic { + /* Word 1 */ + uint16_t cont_level; /* level of ">" */ + uint8_t flag; +#define INDIR 0x01 /* if '(...)' appears */ +#define OFFADD 0x02 /* if '>&' or '>...(&' appears */ +#define INDIROFFADD 0x04 /* if '>&(' appears */ +#define UNSIGNED 0x08 /* comparison is unsigned */ +#define NOSPACE 0x10 /* suppress space character before output */ +#define BINTEST 0x20 /* test is for a binary type (set only + for top-level tests) */ +#define TEXTTEST 0x40 /* for passing to file_softmagic */ + + uint8_t factor; + + /* Word 2 */ + uint8_t reln; /* relation (0=eq, '>'=gt, etc) */ + uint8_t vallen; /* length of string value, if any */ + uint8_t type; /* comparison type (FILE_*) */ + uint8_t in_type; /* type of indirection */ +#define FILE_INVALID 0 +#define FILE_BYTE 1 +#define FILE_SHORT 2 +#define FILE_DEFAULT 3 +#define FILE_LONG 4 +#define FILE_STRING 5 +#define FILE_DATE 6 +#define FILE_BESHORT 7 +#define FILE_BELONG 8 +#define FILE_BEDATE 9 +#define FILE_LESHORT 10 +#define FILE_LELONG 11 +#define FILE_LEDATE 12 +#define FILE_PSTRING 13 +#define FILE_LDATE 14 +#define FILE_BELDATE 15 +#define FILE_LELDATE 16 +#define FILE_REGEX 17 +#define FILE_BESTRING16 18 +#define FILE_LESTRING16 19 +#define FILE_SEARCH 20 +#define FILE_MEDATE 21 +#define FILE_MELDATE 22 +#define FILE_MELONG 23 +#define FILE_QUAD 24 +#define FILE_LEQUAD 25 +#define FILE_BEQUAD 26 +#define FILE_QDATE 27 +#define FILE_LEQDATE 28 +#define FILE_BEQDATE 29 +#define FILE_QLDATE 30 +#define FILE_LEQLDATE 31 +#define FILE_BEQLDATE 32 +#define FILE_FLOAT 33 +#define FILE_BEFLOAT 34 +#define FILE_LEFLOAT 35 +#define FILE_DOUBLE 36 +#define FILE_BEDOUBLE 37 +#define FILE_LEDOUBLE 38 +#define FILE_BEID3 39 +#define FILE_LEID3 40 +#define FILE_INDIRECT 41 +#define FILE_NAMES_SIZE 42/* size of array to contain all names */ + +#define IS_LIBMAGIC_STRING(t) \ + ((t) == FILE_STRING || \ + (t) == FILE_PSTRING || \ + (t) == FILE_BESTRING16 || \ + (t) == FILE_LESTRING16 || \ + (t) == FILE_REGEX || \ + (t) == FILE_SEARCH || \ + (t) == FILE_DEFAULT) + +#define FILE_FMT_NONE 0 +#define FILE_FMT_NUM 1 /* "cduxXi" */ +#define FILE_FMT_STR 2 /* "s" */ +#define FILE_FMT_QUAD 3 /* "ll" */ +#define FILE_FMT_FLOAT 4 /* "eEfFgG" */ +#define FILE_FMT_DOUBLE 5 /* "eEfFgG" */ + + /* Word 3 */ + uint8_t in_op; /* operator for indirection */ + uint8_t mask_op; /* operator for mask */ +#ifdef ENABLE_CONDITIONALS + uint8_t cond; /* conditional type */ +#else + uint8_t dummy; +#endif + uint8_t factor_op; +#define FILE_FACTOR_OP_PLUS '+' +#define FILE_FACTOR_OP_MINUS '-' +#define FILE_FACTOR_OP_TIMES '*' +#define FILE_FACTOR_OP_DIV '/' +#define FILE_FACTOR_OP_NONE '\0' + +#define FILE_OPS "&|^+-*/%" +#define FILE_OPAND 0 +#define FILE_OPOR 1 +#define FILE_OPXOR 2 +#define FILE_OPADD 3 +#define FILE_OPMINUS 4 +#define FILE_OPMULTIPLY 5 +#define FILE_OPDIVIDE 6 +#define FILE_OPMODULO 7 +#define FILE_OPS_MASK 0x07 /* mask for above ops */ +#define FILE_UNUSED_1 0x08 +#define FILE_UNUSED_2 0x10 +#define FILE_UNUSED_3 0x20 +#define FILE_OPINVERSE 0x40 +#define FILE_OPINDIRECT 0x80 + +#ifdef ENABLE_CONDITIONALS +#define COND_NONE 0 +#define COND_IF 1 +#define COND_ELIF 2 +#define COND_ELSE 3 +#endif /* ENABLE_CONDITIONALS */ + + /* Word 4 */ + uint32_t offset; /* offset to magic number */ + /* Word 5 */ + int32_t in_offset; /* offset from indirection */ + /* Word 6 */ + uint32_t lineno; /* line number in magic file */ + /* Word 7,8 */ + union { + uint64_t _mask; /* for use with numeric and date types */ + struct { + uint32_t _count; /* repeat/line count */ + uint32_t _flags; /* modifier flags */ + } _s; /* for use with string types */ + } _u; +#define num_mask _u._mask +#define str_range _u._s._count +#define str_flags _u._s._flags + /* Words 9-16 */ + union VALUETYPE value; /* either number or string */ + /* Words 17-32 */ + char desc[MAXDESC]; /* description */ + /* Words 33-48 */ + char mimetype[MAXDESC]; /* MIME type */ + /* Words 49-50 */ + char apple[8]; +}; + +#define BIT(A) (1 << (A)) +#define STRING_COMPACT_WHITESPACE BIT(0) +#define STRING_COMPACT_OPTIONAL_WHITESPACE BIT(1) +#define STRING_IGNORE_LOWERCASE BIT(2) +#define STRING_IGNORE_UPPERCASE BIT(3) +#define REGEX_OFFSET_START BIT(4) +#define STRING_TEXTTEST BIT(5) +#define STRING_BINTEST BIT(6) +#define PSTRING_1_BE BIT(7) +#define PSTRING_1_LE BIT(7) +#define PSTRING_2_BE BIT(8) +#define PSTRING_2_LE BIT(9) +#define PSTRING_4_BE BIT(10) +#define PSTRING_4_LE BIT(11) +#define PSTRING_LEN \ + (PSTRING_1_BE|PSTRING_2_LE|PSTRING_2_BE|PSTRING_4_LE|PSTRING_4_BE) +#define PSTRING_LENGTH_INCLUDES_ITSELF BIT(12) +#define CHAR_COMPACT_WHITESPACE 'W' +#define CHAR_COMPACT_OPTIONAL_WHITESPACE 'w' +#define CHAR_IGNORE_LOWERCASE 'c' +#define CHAR_IGNORE_UPPERCASE 'C' +#define CHAR_REGEX_OFFSET_START 's' +#define CHAR_TEXTTEST 't' +#define CHAR_BINTEST 'b' +#define CHAR_PSTRING_1_BE 'B' +#define CHAR_PSTRING_1_LE 'B' +#define CHAR_PSTRING_2_BE 'H' +#define CHAR_PSTRING_2_LE 'h' +#define CHAR_PSTRING_4_BE 'L' +#define CHAR_PSTRING_4_LE 'l' +#define CHAR_PSTRING_LENGTH_INCLUDES_ITSELF 'J' +#define STRING_IGNORE_CASE (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE) +#define STRING_DEFAULT_RANGE 100 + + +/* list of magic entries */ +struct mlist { + struct magic *magic; /* array of magic entries */ + uint32_t nmagic; /* number of entries in array */ + int mapped; /* allocation type: 0 => apprentice_file + * 1 => apprentice_map + malloc + * 2 => apprentice_map + mmap */ + struct mlist *next, *prev; +}; + +#ifdef __cplusplus +#define CAST(T, b) static_cast<T>(b) +#define RCAST(T, b) reinterpret_cast<T>(b) +#else +#define CAST(T, b) (T)(b) +#define RCAST(T, b) (T)(b) +#endif + +struct level_info { + int32_t off; + int got_match; +#ifdef ENABLE_CONDITIONALS + int last_match; + int last_cond; /* used for error checking by parse() */ +#endif +}; +struct magic_set { + struct mlist *mlist; + struct cont { + size_t len; + struct level_info *li; + } c; + struct out { + char *buf; /* Accumulation buffer */ + char *pbuf; /* Printable buffer */ + } o; + uint32_t offset; + int error; + int flags; /* Control magic tests. */ + int event_flags; /* Note things that happened. */ +#define EVENT_HAD_ERR 0x01 + const char *file; + size_t line; /* current magic line number */ + + /* data for searches */ + struct { + const char *s; /* start of search in original source */ + size_t s_len; /* length of search region */ + size_t offset; /* starting offset in source: XXX - should this be off_t? */ + size_t rm_len; /* match length */ + } search; + + /* FIXME: Make the string dynamically allocated so that e.g. + strings matched in files can be longer than MAXstring */ + union VALUETYPE ms_value; /* either number or string */ +}; + +/* Type for Unicode characters */ +typedef unsigned long unichar; + +struct stat; +protected const char *file_fmttime(uint32_t, int); +protected int file_buffer(struct magic_set *, php_stream *, const char *, const void *, + size_t); +protected int file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream); +protected int file_pipe2file(struct magic_set *, int, const void *, size_t); +protected int file_replace(struct magic_set *, const char *, const char *); +protected int file_printf(struct magic_set *, const char *, ...); +protected int file_reset(struct magic_set *); +protected int file_tryelf(struct magic_set *, int, const unsigned char *, + size_t); +protected int file_trycdf(struct magic_set *, int, const unsigned char *, + size_t); +#ifdef PHP_FILEINFO_UNCOMPRESS +protected int file_zmagic(struct magic_set *, int, const char *, + const unsigned char *, size_t); +#endif +protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t, + int); +protected int file_ascmagic_with_encoding(struct magic_set *, + const unsigned char *, size_t, unichar *, size_t, const char *, + const char *, int); +protected int file_encoding(struct magic_set *, const unsigned char *, size_t, + unichar **, size_t *, const char **, const char **, const char **); +protected int file_is_tar(struct magic_set *, const unsigned char *, size_t); +protected int file_softmagic(struct magic_set *, const unsigned char *, size_t, + int, int); +protected struct mlist *file_apprentice(struct magic_set *, const char *, int); +protected uint64_t file_signextend(struct magic_set *, struct magic *, + uint64_t); +protected void file_delmagic(struct magic *, int type, size_t entries); +protected void file_badread(struct magic_set *); +protected void file_badseek(struct magic_set *); +protected void file_oomem(struct magic_set *, size_t); +protected void file_error(struct magic_set *, int, const char *, ...); +protected void file_magerror(struct magic_set *, const char *, ...); +protected void file_magwarn(struct magic_set *, const char *, ...); +protected void file_showstr(FILE *, const char *, size_t); +protected size_t file_mbswidth(const char *); +protected const char *file_getbuffer(struct magic_set *); +protected ssize_t sread(int, void *, size_t, int); +protected int file_check_mem(struct magic_set *, unsigned int); +protected int file_looks_utf8(const unsigned char *, size_t, unichar *, + size_t *); +protected size_t file_pstring_length_size(const struct magic *); +protected size_t file_pstring_get_length(const struct magic *, const char *); +protected size_t file_printedlen(const struct magic_set *ms); +#ifdef __EMX__ +protected int file_os2_apptype(struct magic_set *, const char *, const void *, + size_t); +#endif /* __EMX__ */ + +extern const char *file_names[]; +extern const size_t file_nnames; + +#ifndef HAVE_STRERROR +extern int sys_nerr; +extern char *sys_errlist[]; +#define strerror(e) \ + (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error") +#endif + +#ifndef HAVE_STRTOUL +#define strtoul(a, b, c) strtol(a, b, c) +#endif + +#ifndef strlcpy +size_t strlcpy(char *dst, const char *src, size_t siz); +#endif +#ifndef strlcat +size_t strlcat(char *dst, const char *src, size_t siz); +#endif +#ifndef HAVE_GETLINE +ssize_t getline(char **dst, size_t *len, FILE *fp); +ssize_t getdelim(char **dst, size_t *len, int delimiter, FILE *fp); +#endif + +#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK) +#define QUICK +#endif + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +#ifndef __cplusplus +#if defined(__GNUC__) && (__GNUC__ >= 3) +#define FILE_RCSID(id) \ +static const char rcsid[] __attribute__((__used__)) = id; +#else +#define FILE_RCSID(id) \ +static const char *rcsid(const char *p) { \ + return rcsid(p = id); \ +} +#endif +#else +#define FILE_RCSID(id) +#endif + +#ifdef PHP_WIN32 +#define FINFO_LSEEK_FUNC _lseek +#define FINFO_READ_FUNC _read +#else +#define FINFO_LSEEK_FUNC lseek +#define FINFO_READ_FUNC read +#endif + +#endif /* __file_h__ */ diff --git a/ext/fileinfo/libmagic/fsmagic.c b/ext/fileinfo/libmagic/fsmagic.c new file mode 100644 index 0000000..a762e59 --- /dev/null +++ b/ext/fileinfo/libmagic/fsmagic.c @@ -0,0 +1,252 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * fsmagic - magic based on filesystem info - directory, special files, etc. + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: fsmagic.c,v 1.64 2011/08/14 09:03:12 christos Exp $") +#endif /* lint */ + +#include "magic.h" +#include <string.h> +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <stdlib.h> +/* Since major is a function on SVR4, we cannot use `ifndef major'. */ +#ifdef MAJOR_IN_MKDEV +# include <sys/mkdev.h> +# define HAVE_MAJOR +#endif +#ifdef MAJOR_IN_SYSMACROS +# include <sys/sysmacros.h> +# define HAVE_MAJOR +#endif +#ifdef major /* Might be defined in sys/types.h. */ +# define HAVE_MAJOR +#endif + +#ifndef HAVE_MAJOR +# define major(dev) (((dev) >> 8) & 0xff) +# define minor(dev) ((dev) & 0xff) +#endif +#undef HAVE_MAJOR + +#ifdef PHP_WIN32 + +# undef S_IFIFO +#endif + + +#ifndef S_ISDIR +#define S_ISDIR(mode) ((mode) & _S_IFDIR) +#endif + +#ifndef S_ISREG +#define S_ISREG(mode) ((mode) & _S_IFREG) +#endif + +private int +handle_mime(struct magic_set *ms, int mime, const char *str) +{ + if ((mime & MAGIC_MIME_TYPE)) { + if (file_printf(ms, "inode/%s", str) == -1) + return -1; + if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, + "; charset=") == -1) + return -1; + } + if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1) + return -1; + return 0; +} + +protected int +file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb, php_stream *stream) +{ + int mime = ms->flags & MAGIC_MIME; + TSRMLS_FETCH(); + + if (ms->flags & MAGIC_APPLE) + return 0; + + if (!fn && !stream) { + return 0; + } + + if (stream) { + php_stream_statbuf ssb; + if (php_stream_stat(stream, &ssb) < 0) { + if (ms->flags & MAGIC_ERROR) { + file_error(ms, errno, "cannot stat `%s'", fn); + return -1; + } + return 1; + } + memcpy(sb, &ssb.sb, sizeof(struct stat)); + } else { + if (php_sys_stat(fn, sb) != 0) { + if (ms->flags & MAGIC_ERROR) { + file_error(ms, errno, "cannot stat `%s'", fn); + return -1; + } + return 1; + } + } + + if (!mime) { +#ifdef S_ISUID + if (sb->st_mode & S_ISUID) + if (file_printf(ms, "setuid ") == -1) + return -1; +#endif +#ifdef S_ISGID + if (sb->st_mode & S_ISGID) + if (file_printf(ms, "setgid ") == -1) + return -1; +#endif +#ifdef S_ISVTX + if (sb->st_mode & S_ISVTX) + if (file_printf(ms, "sticky ") == -1) + return -1; +#endif + } + + switch (sb->st_mode & S_IFMT) { +#ifndef PHP_WIN32 +# ifdef S_IFCHR + case S_IFCHR: + /* + * If -s has been specified, treat character special files + * like ordinary files. Otherwise, just report that they + * are block special files and go on to the next file. + */ + if ((ms->flags & MAGIC_DEVICES) != 0) { + break; + } + if (mime) { + if (handle_mime(ms, mime, "x-character-device") == -1) + return -1; + } else { +# ifdef HAVE_STAT_ST_RDEV +# ifdef dv_unit + if (file_printf(ms, "character special (%d/%d/%d)", + major(sb->st_rdev), dv_unit(sb->st_rdev), + dv_subunit(sb->st_rdev)) == -1) + return -1; +# else + if (file_printf(ms, "character special (%ld/%ld)", + (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) + == -1) + return -1; +# endif +# else + if (file_printf(ms, "character special") == -1) + return -1; +# endif + } + return 1; +# endif +#endif + +#ifdef S_IFIFO + case S_IFIFO: + if((ms->flags & MAGIC_DEVICES) != 0) + break; + if (mime) { + if (handle_mime(ms, mime, "fifo") == -1) + return -1; + } else if (file_printf(ms, "fifo (named pipe)") == -1) + return -1; + return 1; +#endif +#ifdef S_IFDOOR + case S_IFDOOR: + if (mime) { + if (handle_mime(ms, mime, "door") == -1) + return -1; + } else if (file_printf(ms, "door") == -1) + return -1; + return 1; +#endif +#ifdef S_IFLNK + case S_IFLNK: + /* stat is used, if it made here then the link is broken */ + if (ms->flags & MAGIC_ERROR) { + file_error(ms, errno, "unreadable symlink `%s'", fn); + return -1; + } + return 1; +#endif + +#ifdef S_IFSOCK +#ifndef __COHERENT__ + case S_IFSOCK: + if (mime) { + if (handle_mime(ms, mime, "socket") == -1) + return -1; + } else if (file_printf(ms, "socket") == -1) + return -1; + return 1; +#endif +#endif + + case S_IFREG: + break; + + default: + file_error(ms, 0, "invalid mode 0%o", sb->st_mode); + return -1; + /*NOTREACHED*/ + } + + /* + * regular file, check next possibility + * + * If stat() tells us the file has zero length, report here that + * the file is empty, so we can skip all the work of opening and + * reading the file. + * But if the -s option has been given, we skip this optimization, + * since on some systems, stat() reports zero size for raw disk + * partitions. (If the block special device really has zero length, + * the fact that it is empty will be detected and reported correctly + * when we read the file.) + */ + if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) { + if (mime) { + if (handle_mime(ms, mime, "x-empty") == -1) + return -1; + } else if (file_printf(ms, "empty") == -1) + return -1; + return 1; + } + return 0; +} diff --git a/ext/fileinfo/libmagic/funcs.c b/ext/fileinfo/libmagic/funcs.c new file mode 100644 index 0000000..2b37929 --- /dev/null +++ b/ext/fileinfo/libmagic/funcs.c @@ -0,0 +1,479 @@ +/* + * Copyright (c) Christos Zoulas 2003. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: funcs.c,v 1.60 2011/12/08 12:38:24 rrt Exp $") +#endif /* lint */ + +#include "magic.h" +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#if defined(HAVE_WCHAR_H) +#include <wchar.h> +#endif +#if defined(HAVE_WCTYPE_H) +#include <wctype.h> +#endif + +#ifndef SIZE_MAX +# define SIZE_MAX ((size_t) -1) +#endif + +#ifndef PREG_OFFSET_CAPTURE +# define PREG_OFFSET_CAPTURE (1<<8) +#endif + +extern public void convert_libmagic_pattern(zval *pattern, int options); + +/* + * Like printf, only we append to a buffer. + */ +protected int +file_printf(struct magic_set *ms, const char *fmt, ...) +{ + 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; +} + +/* + * error - print best error message possible + */ +/*VARARGS*/ +private void +file_error_core(struct magic_set *ms, int error, const char *f, va_list va, + size_t lineno) +{ + 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 %" SIZE_T_FORMAT "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; +} + +/*VARARGS*/ +protected void +file_error(struct magic_set *ms, int error, const char *f, ...) +{ + va_list va; + va_start(va, f); + file_error_core(ms, error, f, va, 0); + va_end(va); +} + +/* + * Print an error with magic line number. + */ +/*VARARGS*/ +protected void +file_magerror(struct magic_set *ms, const char *f, ...) +{ + va_list va; + va_start(va, f); + file_error_core(ms, 0, f, va, ms->line); + va_end(va); +} + +protected void +file_oomem(struct magic_set *ms, size_t len) +{ + file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes", + len); +} + +protected void +file_badseek(struct magic_set *ms) +{ + file_error(ms, errno, "error seeking"); +} + +protected void +file_badread(struct magic_set *ms) +{ + file_error(ms, errno, "error reading"); +} + +protected int +file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const void *buf, + size_t nb) +{ + int m = 0, rv = 0, looks_text = 0; + int mime = ms->flags & MAGIC_MIME; + const unsigned char *ubuf = CAST(const unsigned char *, 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, + looks_text)) != 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 */ + if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { + + if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 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, looks_text)) + != 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; + } + free(u8buf); + if (rv) + return rv; + + return m; +} + +protected int +file_reset(struct magic_set *ms) +{ + 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; +} + +#define OCTALIFY(n, o) \ + /*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)++) + +protected const char * +file_getbuffer(struct magic_set *ms) +{ + 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;) { + if (isprint((unsigned char)*op)) { + *np++ = *op++; + } else { + OCTALIFY(np, op); + } + } + *np = '\0'; + return ms->o.pbuf; +} + +protected int +file_check_mem(struct magic_set *ms, unsigned int level) +{ + 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; +} + +protected size_t +file_printedlen(const struct magic_set *ms) +{ + return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); +} + + +protected int +file_replace(struct magic_set *ms, const char *pat, const char *rep) +{ + zval *patt; + int opts = 0; + pcre_cache_entry *pce; + char *res; + zval *repl; + int res_len, rep_cnt = 0; + TSRMLS_FETCH(); + + MAKE_STD_ZVAL(patt); + ZVAL_STRINGL(patt, pat, strlen(pat), 0); + opts |= PCRE_MULTILINE; + convert_libmagic_pattern(patt, opts); +#if (PHP_MAJOR_VERSION < 6) + if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(patt), Z_STRLEN_P(patt) TSRMLS_CC)) == NULL) { +#else + if ((pce = pcre_get_compiled_regex_cache(IS_STRING, Z_STRVAL_P(patt), Z_STRLEN_P(patt) TSRMLS_CC)) == NULL) { +#endif + zval_dtor(patt); + FREE_ZVAL(patt); + return -1; + } + + MAKE_STD_ZVAL(repl); + ZVAL_STRINGL(repl, rep, strlen(rep), 0); + + res = php_pcre_replace_impl(pce, ms->o.buf, strlen(ms->o.buf), repl, + 0, &res_len, -1, &rep_cnt TSRMLS_CC); + + FREE_ZVAL(repl); + zval_dtor(patt); + FREE_ZVAL(patt); + + if (NULL == res) { + return -1; + } + + strncpy(ms->o.buf, res, res_len); + ms->o.buf[res_len] = '\0'; + + efree(res); + + return rep_cnt; +} + diff --git a/ext/fileinfo/libmagic/is_tar.c b/ext/fileinfo/libmagic/is_tar.c new file mode 100644 index 0000000..876c631 --- /dev/null +++ b/ext/fileinfo/libmagic/is_tar.c @@ -0,0 +1,152 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * is_tar() -- figure out whether file is a tar archive. + * + * Stolen (by the author!) from the public domain tar program: + * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu). + * + * @(#)list.c 1.18 9/23/86 Public Domain - gnu + * + * Comments changed and some code/comments reformatted + * for file command by Ian Darwin. + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: is_tar.c,v 1.37 2010/11/30 14:58:53 rrt Exp $") +#endif + +#include "magic.h" +#include <string.h> +#include <ctype.h> +#include "tar.h" + +#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') ) + +private int is_tar(const unsigned char *, size_t); +private int from_oct(int, const char *); /* Decode octal number */ + +static const char tartype[][32] = { + "tar archive", + "POSIX tar archive", + "POSIX tar archive (GNU)", +}; + +protected int +file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes) +{ + /* + * Do the tar test first, because if the first file in the tar + * archive starts with a dot, we can confuse it with an nroff file. + */ + int tar; + int mime = ms->flags & MAGIC_MIME; + + if ((ms->flags & MAGIC_APPLE) != 0) + return 0; + + tar = is_tar(buf, nbytes); + if (tar < 1 || tar > 3) + return 0; + + if (file_printf(ms, "%s", mime ? "application/x-tar" : + tartype[tar - 1]) == -1) + return -1; + return 1; +} + +/* + * Return + * 0 if the checksum is bad (i.e., probably not a tar archive), + * 1 for old UNIX tar file, + * 2 for Unix Std (POSIX) tar file, + * 3 for GNU tar file. + */ +private int +is_tar(const unsigned char *buf, size_t nbytes) +{ + const union record *header = (const union record *)(const void *)buf; + int i; + int sum, recsum; + const unsigned char *p; + + if (nbytes < sizeof(union record)) + return 0; + + recsum = from_oct(8, header->header.chksum); + + sum = 0; + p = header->charptr; + for (i = sizeof(union record); --i >= 0;) + sum += *p++; + + /* Adjust checksum to count the "chksum" field as blanks. */ + for (i = sizeof(header->header.chksum); --i >= 0;) + sum -= header->header.chksum[i]; + sum += ' ' * sizeof header->header.chksum; + + if (sum != recsum) + return 0; /* Not a tar archive */ + + if (strcmp(header->header.magic, GNUTMAGIC) == 0) + return 3; /* GNU Unix Standard tar archive */ + if (strcmp(header->header.magic, TMAGIC) == 0) + return 2; /* Unix Standard tar archive */ + + return 1; /* Old fashioned tar archive */ +} + + +/* + * Quick and dirty octal conversion. + * + * Result is -1 if the field is invalid (all blank, or non-octal). + */ +private int +from_oct(int digs, const char *where) +{ + int value; + + while (isspace((unsigned char)*where)) { /* Skip spaces */ + where++; + if (--digs <= 0) + return -1; /* All blank field */ + } + value = 0; + while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */ + value = (value << 3) | (*where++ - '0'); + --digs; + } + + if (digs > 0 && *where && !isspace((unsigned char)*where)) + return -1; /* Ended on non-(space/NUL) */ + + return value; +} diff --git a/ext/fileinfo/libmagic/magic.c b/ext/fileinfo/libmagic/magic.c new file mode 100644 index 0000000..b4818ae --- /dev/null +++ b/ext/fileinfo/libmagic/magic.c @@ -0,0 +1,505 @@ +/* + * Copyright (c) Christos Zoulas 2003. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: magic.c,v 1.74 2011/05/26 01:27:59 christos Exp $") +#endif /* lint */ + +#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> /* for PIPE_BUF */ + +#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> /* for read() */ +#endif + +#ifndef PIPE_BUF +/* Get the PIPE_BUF from pathconf */ +#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 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* get_default_magic(void); +private const char *file_or_stream(struct magic_set *, const char *, php_stream *); + +#ifndef STDIN_FILENO +#define STDIN_FILENO 0 +#endif + +/* XXX this functionality is excluded in php, enable it in apprentice.c:340 */ +#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", 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 (/*CONSTCOND*/0) + + if (default_magic) { + free(default_magic); + default_magic = NULL; + } + + /* First, try to get user-specific magic file */ + 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(); + + /* Second, try to get a magic file from Common Files */ + if ((home = getenv("COMMONPROGRAMFILES")) != NULL) { + if (asprintf(&tmppath, "%s%s", home, hmagic) >= 0) + APPENDPATH(); + } + + /* Third, try to get magic file relative to dll location */ + dllpath = malloc(sizeof(*dllpath) * (MAX_PATH + 1)); + dllpath[MAX_PATH] = 0; /* just in case long path gets truncated and not null terminated */ + 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(); + } + } + + /* Don't put MAGIC constant - it likely points to a file within MSys + tree */ + 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) +{ + 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; +} + +private void +free_mlist(struct mlist *mlist) +{ + 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); +} + +private int +unreadable_info(struct magic_set *ms, mode_t md, const char *file) +{ + /* 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; +} + +public void +magic_close(struct magic_set *ms) +{ + 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); +} + +/* + * load a magic file + */ +public int +magic_load(struct magic_set *ms, const char *magicfile) +{ + struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD); + if (ml) { + free_mlist(ms->mlist); + ms->mlist = ml; + return 0; + } + return -1; +} + +public int +magic_compile(struct magic_set *ms, const char *magicfile) +{ + struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE); + free_mlist(ml); + return ml ? 0 : -1; +} + + +public int +magic_list(struct magic_set *ms, const char *magicfile) +{ + struct mlist *ml = file_apprentice(ms, magicfile, FILE_LIST); + free_mlist(ml); + return ml ? 0 : -1; +} + +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) { + /* + * 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 + } +} + + +/* + * find type of descriptor + */ +public const char * +magic_descriptor(struct magic_set *ms, int fd) +{ + return file_or_stream(ms, NULL, NULL); +} + +/* + * find type of named file + */ +public const char * +magic_file(struct magic_set *ms, const char *inname) +{ + return file_or_stream(ms, inname, NULL); +} + +public const char * +magic_stream(struct magic_set *ms, php_stream *stream) +{ + 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; /* 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; +} + + +public const char * +magic_buffer(struct magic_set *ms, const void *buf, size_t nb) +{ + 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); +} + +public const char * +magic_error(struct magic_set *ms) +{ + return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL; +} + +public int +magic_errno(struct magic_set *ms) +{ + return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0; +} + +public int +magic_setflags(struct magic_set *ms, int flags) +{ +#if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES) + if (flags & MAGIC_PRESERVE_ATIME) + return -1; +#endif + ms->flags = flags; + return 0; +} diff --git a/ext/fileinfo/libmagic/magic.h b/ext/fileinfo/libmagic/magic.h new file mode 100644 index 0000000..9996c48 --- /dev/null +++ b/ext/fileinfo/libmagic/magic.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) Christos Zoulas 2003. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#ifndef _MAGIC_H +#define _MAGIC_H + +#include <sys/types.h> + +#define MAGIC_NONE 0x000000 /* No flags */ +#define MAGIC_DEBUG 0x000001 /* Turn on debugging */ +#define MAGIC_SYMLINK 0x000002 /* Follow symlinks */ +#define MAGIC_COMPRESS 0x000004 /* Check inside compressed files */ +#define MAGIC_DEVICES 0x000008 /* Look at the contents of devices */ +#define MAGIC_MIME_TYPE 0x000010 /* Return the MIME type */ +#define MAGIC_CONTINUE 0x000020 /* Return all matches */ +#define MAGIC_CHECK 0x000040 /* Print warnings to stderr */ +#define MAGIC_PRESERVE_ATIME 0x000080 /* Restore access time on exit */ +#define MAGIC_RAW 0x000100 /* Don't translate unprintable chars */ +#define MAGIC_ERROR 0x000200 /* Handle ENOENT etc as real errors */ +#define MAGIC_MIME_ENCODING 0x000400 /* Return the MIME encoding */ +#define MAGIC_MIME (MAGIC_MIME_TYPE|MAGIC_MIME_ENCODING) +#define MAGIC_APPLE 0x000800 /* Return the Apple creator and type */ + +#define MAGIC_NO_CHECK_COMPRESS 0x001000 /* Don't check for compressed files */ +#define MAGIC_NO_CHECK_TAR 0x002000 /* Don't check for tar files */ +#define MAGIC_NO_CHECK_SOFT 0x004000 /* Don't check magic entries */ +#define MAGIC_NO_CHECK_APPTYPE 0x008000 /* Don't check application type */ +#define MAGIC_NO_CHECK_ELF 0x010000 /* Don't check for elf details */ +#define MAGIC_NO_CHECK_TEXT 0x020000 /* Don't check for text files */ +#define MAGIC_NO_CHECK_CDF 0x040000 /* Don't check for cdf files */ +#define MAGIC_NO_CHECK_TOKENS 0x100000 /* Don't check tokens */ +#define MAGIC_NO_CHECK_ENCODING 0x200000 /* Don't check text encodings */ + +/* No built-in tests; only consult the magic file */ +#define MAGIC_NO_CHECK_BUILTIN ( \ + MAGIC_NO_CHECK_COMPRESS | \ + MAGIC_NO_CHECK_TAR | \ +/* MAGIC_NO_CHECK_SOFT | */ \ + MAGIC_NO_CHECK_APPTYPE | \ + MAGIC_NO_CHECK_ELF | \ + MAGIC_NO_CHECK_TEXT | \ + MAGIC_NO_CHECK_CDF | \ + MAGIC_NO_CHECK_TOKENS | \ + MAGIC_NO_CHECK_ENCODING | \ +) + +/* Defined for backwards compatibility (renamed) */ +#define MAGIC_NO_CHECK_ASCII MAGIC_NO_CHECK_TEXT + +/* Defined for backwards compatibility; do nothing */ +#define MAGIC_NO_CHECK_FORTRAN 0x000000 /* Don't check ascii/fortran */ +#define MAGIC_NO_CHECK_TROFF 0x000000 /* Don't check ascii/troff */ + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct magic_set *magic_t; +magic_t magic_open(int); +void magic_close(magic_t); + +const char *magic_getpath(const char *, int); +const char *magic_file(magic_t, const char *); +const char *magic_stream(magic_t, php_stream *); +const char *magic_descriptor(magic_t, int); +const char *magic_buffer(magic_t, const void *, size_t); + +const char *magic_error(magic_t); +int magic_setflags(magic_t, int); + +int magic_load(magic_t, const char *); +int magic_compile(magic_t, const char *); +int magic_list(magic_t, const char *); +int magic_errno(magic_t); + +#ifdef __cplusplus +}; +#endif + +#endif /* _MAGIC_H */ diff --git a/ext/fileinfo/libmagic/names.h b/ext/fileinfo/libmagic/names.h new file mode 100644 index 0000000..2682edc --- /dev/null +++ b/ext/fileinfo/libmagic/names.h @@ -0,0 +1,173 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * Names.h - names and types used by ascmagic in file(1). + * These tokens are here because they can appear anywhere in + * the first HOWMANY bytes, while tokens in MAGIC must + * appear at fixed offsets into the file. Don't make HOWMANY + * too high unless you have a very fast CPU. + * + * $File: names.h,v 1.32 2008/02/11 00:19:29 rrt Exp $ + */ + +/* + modified by Chris Lowth - 9 April 2000 + to add mime type strings to the types table. +*/ + +/* these types are used to index the table 'types': keep em in sync! */ +#define L_C 0 /* first and foremost on UNIX */ +#define L_CC 1 /* Bjarne's postincrement */ +#define L_MAKE 2 /* Makefiles */ +#define L_PLI 3 /* PL/1 */ +#define L_MACH 4 /* some kinda assembler */ +#define L_ENG 5 /* English */ +#define L_PAS 6 /* Pascal */ +#define L_MAIL 7 /* Electronic mail */ +#define L_NEWS 8 /* Usenet Netnews */ +#define L_JAVA 9 /* Java code */ +#define L_HTML 10 /* HTML */ +#define L_BCPL 11 /* BCPL */ +#define L_M4 12 /* M4 */ +#define L_PO 13 /* PO */ + +static const struct { + char human[48]; + char mime[16]; +} types[] = { + { "C program", "text/x-c", }, + { "C++ program", "text/x-c++" }, + { "make commands", "text/x-makefile" }, + { "PL/1 program", "text/x-pl1" }, + { "assembler program", "text/x-asm" }, + { "English", "text/plain" }, + { "Pascal program", "text/x-pascal" }, + { "mail", "text/x-mail" }, + { "news", "text/x-news" }, + { "Java program", "text/x-java" }, + { "HTML document", "text/html", }, + { "BCPL program", "text/x-bcpl" }, + { "M4 macro language pre-processor", "text/x-m4" }, + { "PO (gettext message catalogue)", "text/x-po" }, + { "cannot happen error on names.h/types", "error/x-error" } +}; + +/* + * XXX - how should we distinguish Java from C++? + * The trick used in a Debian snapshot, of having "extends" or "implements" + * as tags for Java, doesn't work very well, given that those keywords + * are often preceded by "class", which flags it as C++. + * + * Perhaps we need to be able to say + * + * If "class" then + * + * if "extends" or "implements" then + * Java + * else + * C++ + * endif + * + * Or should we use other keywords, such as "package" or "import"? + * Unfortunately, Ada95 uses "package", and Modula-3 uses "import", + * although I infer from the language spec at + * + * http://www.research.digital.com/SRC/m3defn/html/m3.html + * + * that Modula-3 uses "IMPORT" rather than "import", i.e. it must be + * in all caps. + * + * So, for now, we go with "import". We must put it before the C++ + * stuff, so that we don't misidentify Java as C++. Not using "package" + * means we won't identify stuff that defines a package but imports + * nothing; hopefully, very little Java code imports nothing (one of the + * reasons for doing OO programming is to import as much as possible + * and write only what you need to, right?). + * + * Unfortunately, "import" may cause us to misidentify English text + * as Java, as it comes after "the" and "The". Perhaps we need a fancier + * heuristic to identify Java? + */ +static const struct names { + char name[14]; + short type; +} names[] = { + /* These must be sorted by eye for optimal hit rate */ + /* Add to this list only after substantial meditation */ + {"msgid", L_PO}, + {"dnl", L_M4}, + {"import", L_JAVA}, + {"\"libhdr\"", L_BCPL}, + {"\"LIBHDR\"", L_BCPL}, + {"//", L_CC}, + {"template", L_CC}, + {"virtual", L_CC}, + {"class", L_CC}, + {"public:", L_CC}, + {"private:", L_CC}, + {"/*", L_C}, /* must precede "The", "the", etc. */ + {"#include", L_C}, + {"char", L_C}, + {"The", L_ENG}, + {"the", L_ENG}, + {"double", L_C}, + {"extern", L_C}, + {"float", L_C}, + {"struct", L_C}, + {"union", L_C}, + {"CFLAGS", L_MAKE}, + {"LDFLAGS", L_MAKE}, + {"all:", L_MAKE}, + {".PRECIOUS", L_MAKE}, + {".ascii", L_MACH}, + {".asciiz", L_MACH}, + {".byte", L_MACH}, + {".even", L_MACH}, + {".globl", L_MACH}, + {".text", L_MACH}, + {"clr", L_MACH}, + {"(input,", L_PAS}, + {"program", L_PAS}, + {"record", L_PAS}, + {"dcl", L_PLI}, + {"Received:", L_MAIL}, + {">From", L_MAIL}, + {"Return-Path:",L_MAIL}, + {"Cc:", L_MAIL}, + {"Newsgroups:", L_NEWS}, + {"Path:", L_NEWS}, + {"Organization:",L_NEWS}, + {"href=", L_HTML}, + {"HREF=", L_HTML}, + {"<body", L_HTML}, + {"<BODY", L_HTML}, + {"<html", L_HTML}, + {"<HTML", L_HTML}, + {"<!--", L_HTML}, +}; +#define NNAMES (sizeof(names)/sizeof(struct names)) diff --git a/ext/fileinfo/libmagic/patchlevel.h b/ext/fileinfo/libmagic/patchlevel.h new file mode 100644 index 0000000..f4afed6 --- /dev/null +++ b/ext/fileinfo/libmagic/patchlevel.h @@ -0,0 +1,358 @@ +#define FILE_VERSION_MAJOR 5 +#define patchlevel 11 + +/* + * Patchlevel file for Ian Darwin's MAGIC command. + * $File: patchlevel.h,v 1.68 2008/03/22 21:39:43 christos Exp $ + * + * $Log$ + * Revision 1.6 2012/03/26 21:01:37 ab + * Update libmagic to 5.11 + * + * Revision 1.5 2012/03/25 13:54:37 ab + * Update libmagic to 5.04 + * + * Revision 1.4 2009/05/04 20:52:43 scottmac + * Update libmagic to 5.02 + * + * Revision 1.3 2009/03/15 23:02:35 scottmac + * Update fileinfo to libmagic 5.00 and remove dependency on dirent.h on Windows + * + * Revision 1.2 2008/11/02 16:09:27 scottmac + * Update libmagic to 4.26 and add support for v6 of the magic file format. + * + * Revision 1.1 2008/07/11 14:13:50 derick + * - Move lib to libmagic + * + * Revision 1.1 2008/07/11 14:10:50 derick + * - Step one for bundling the libmagic library. Some config.m4 issues left. + * + * Revision 1.69 2008/07/02 15:27:05 christos + * welcome to 4.25 + * + * Revision 1.68 2008/03/22 21:39:43 christos + * file 4.24 + * + * Revision 1.67 2007/12/28 20:08:40 christos + * welcome to 4.23. + * + * Revision 1.66 2007/12/27 16:38:24 christos + * welcome to 4.22 + * + * Revision 1.65 2007/05/24 17:22:27 christos + * Welcome to 4.21 + * + * Revision 1.64 2007/03/01 22:14:55 christos + * welcome to 4.20 + * + * Revision 1.63 2007/01/12 17:38:28 christos + * Use File id. + * + * Revision 1.62 2006/12/11 21:49:58 christos + * time for 4.19 + * + * Revision 1.61 2006/10/31 21:18:09 christos + * bump + * + * Revision 1.60 2006/03/02 22:15:12 christos + * welcome to 4.17 + * + * Revision 1.59 2005/10/17 17:15:21 christos + * welcome to 4.16 + * + * Revision 1.58 2005/08/18 15:52:56 christos + * welcome to 4.15 + * + * Revision 1.57 2005/06/25 15:52:14 christos + * Welcome to 4.14 + * + * Revision 1.56 2005/02/09 19:25:13 christos + * Welcome to 4.13 + * + * Revision 1.55 2004/11/24 18:57:47 christos + * Re-do the autoconf stuff once more; passes make dist now. + * + * Revision 1.54 2004/11/21 05:52:05 christos + * ready for 4.11 + * + * Revision 1.53 2004/07/24 20:40:46 christos + * welcome to 4.10 + * + * Revision 1.52 2004/04/07 00:32:25 christos + * welcome to 4.09 + * + * Revision 1.51 2004/03/22 21:17:11 christos + * welcome to 4.08. + * + * Revision 1.50 2003/12/23 17:34:04 christos + * 4.07 + * + * Revision 1.49 2003/10/15 02:08:27 christos + * welcome to 4.06 + * + * Revision 1.48 2003/09/12 19:41:14 christos + * this is 4.04 + * + * Revision 1.47 2003/05/23 21:38:21 christos + * welcome to 4.03 + * + * Revision 1.46 2003/04/02 18:57:43 christos + * prepare for 4.02 + * + * Revision 1.45 2003/03/26 15:37:25 christos + * - Pass lint + * - make NULL in magic_file mean stdin + * - Fix "-" argument to file to pass NULL to magic_file + * - avoid pointer casts by using memcpy + * - rename magic_buf -> magic_buffer + * - keep only the first error + * - manual page: new sentence, new line + * - fix typo in api function (magic_buf -> magic_buffer) + * + * Revision 1.44 2003/03/23 22:23:31 christos + * finish librarification. + * + * Revision 1.43 2003/03/23 21:16:26 christos + * update copyrights. + * + * Revision 1.42 2003/03/23 04:06:05 christos + * Library re-organization + * + * Revision 1.41 2003/02/27 20:53:45 christos + * - fix memory allocation problem (Jeff Johnson) + * - fix stack overflow corruption (David Endler) + * - fixes from NetBSD source (Antti Kantee) + * - magic fixes + * + * Revision 1.40 2003/02/08 18:33:53 christos + * - detect inttypes.h too (Dave Love <d.love@dl.ac.uk>) + * - eliminate unsigned char warnings (Petter Reinholdtsen <pere@hungry.com>) + * - better elf PT_NOTE handling (Nalin Dahyabhai <nalin@redhat.com>) + * - add options to format the output differently + * - much more magic. + * + * Revision 1.39 2002/07/03 18:57:52 christos + * - ansify/c99ize + * - more magic + * - better COMPILE_ONLY support. + * - new magic files. + * - fix solaris compilation problems. + * + * Revision 1.38 2002/05/16 18:45:56 christos + * - pt_note elf additions from NetBSD + * - EMX os specific changes (Alexander Mai) + * - stdint.h detection, acconfig.h fixes (Maciej W. Rozycki, Franz Korntner) + * - regex file additions (Kim Cromie) + * - getopt_long support and misc cleanups (Michael Piefel) + * - many magic fixes and additions + * + * Revision 1.37 2001/09/03 14:44:22 christos + * daylight/tm_isdst detection + * magic fixes + * don't eat the whole file if it has only nulls + * + * Revision 1.36 2001/07/22 21:04:15 christos + * - magic fixes + * - add new operators, pascal strings, UTC date printing, $HOME/.magic + * [from "Tom N Harris" <telliamed@mac.com>] + * + * Revision 1.35 2001/04/24 14:40:25 christos + * - rename magic file sgi to mips and fix it + * - add support for building magic.mgc + * - portability fixes for mmap() + * - try gzip before uncompress, because uncompress sometimes hangs + * - be more conservative about pipe reads and writes + * - many magic fixes + * + * Revision 1.34 2001/03/12 05:05:57 christos + * - new compiled magic format + * - lots of magic additions + * + * Revision 1.33 2000/11/13 00:30:50 christos + * - wordperfect magic fix: freebsd pr 9388 + * - more msdos fixes from freebsd pr's 20131 and 20812 + * - sas and spss magic [Bruce Foster] + * - mkinstalldirs [John Fremlin] + * - sgi opengl fixes [Michael Pruett] + * - netbsd magic fixes [Ignatios Souvatzis] + * - audio additions [Michael Pruett] + * - fix problem with non ansi RCSID [Andreas Ley] + * - oggs magic [Felix von Leitner] + * - gmon magic [Eugen Dedu] + * - TNEF magic [Joomy] + * - netpbm magic and misc other image stuff [Bryan Henderson] + * + * Revision 1.32 2000/08/05 18:24:18 christos + * Correct indianness detection in elf (Charles Hannum) + * FreeBSD elf core support (Guy Harris) + * Use gzip in systems that don't have uncompress (Anthon van der Neut) + * Internationalization/EBCDIC support (Eric Fisher) + * Many many magic changes + * + * Revision 1.31 2000/05/14 17:58:36 christos + * - new magic for claris files + * - new magic for mathematica and maple files + * - new magic for msvc files + * - new -k flag to keep going matching all possible entries + * - add the word executable on #! magic files, and fix the usage of + * the word script + * - lots of other magic fixes + * - fix typo test -> text + * + * Revision 1.30 2000/04/11 02:41:17 christos + * - add support for mime output (-i) + * - make sure we free memory in case realloc fails + * - magic fixes + * + * Revision 1.29 1999/11/28 20:02:29 christos + * new string/[Bcb] magic from anthon, and adjustments to the magic files to + * use it. + * + * Revision 1.28 1999/10/31 22:11:48 christos + * - add "char" type for compatibility with HP/UX + * - recognize HP/UX syntax &=n etc. + * - include errno.h for CYGWIN + * - conditionalize the S_IS* macros + * - revert the SHT_DYNSYM test that broke the linux stripped binaries test + * - lots of Magdir changes + * + * Revision 1.27 1999/02/14 17:21:41 christos + * Automake support and misc cleanups from Rainer Orth + * Enable reading character and block special files from Dale R. Worley + * + * Revision 1.26 1998/09/12 13:19:39 christos + * - add support for bi-endian indirect offsets (Richard Verhoeven) + * - add recognition for bcpl (Joseph Myers) + * - remove non magic files from Magdir to avoid difficulties building + * on os2 where files are case independent + * - magic fixes. + * + * Revision 1.25 1998/06/27 14:04:04 christos + * OLF patch Guy Harris + * Recognize java/html (debian linux) + * Const poisoning (debian linux) + * More magic! + * + * Revision 1.24 1998/02/15 23:20:38 christos + * Autoconf patch: Felix von Leitner <leitner@math.fu-berlin.de> + * More magic fixes + * Elf64 fixes + * + * Revision 1.23 1997/11/05 16:03:37 christos + * - correct elf prps offset for SunOS-2.5.1 [guy@netapp.com] + * - handle 64 bit time_t's correctly [ewt@redhat.com] + * - new mime style magic [clarosse@netvista.net] + * - new TI calculator magic [rmcguire@freenet.columbus.oh.us] + * - new figlet fonts [obrien@freebsd.org] + * - new cisco magic, and elf fixes [jhawk@bbnplanet.com] + * - -b flag addition, and x86 filesystem magic [vax@linkhead.paranoia.com] + * - s/Mpeg/MPEG, header and elf typo fixes [guy@netapp.com] + * - Windows/NT registry files, audio code [guy@netapp.com] + * - libGrx graphics lib fonts [guy@netapp.com] + * - PNG fixes [guy@netapp.com] + * - more m$ document magic [guy@netapp.com] + * - PPD files [guy@netapp.com] + * - archive magic cleanup [guy@netapp.com] + * - linux kernel magic cleanup [guy@netapp.com] + * - lecter magic [guy@netapp.com] + * - vgetty magic [guy@netapp.com] + * - sniffer additions [guy@netapp.com] + * + * Revision 1.22 1997/01/15 17:23:24 christos + * - add support for elf core files: find the program name under SVR4 [Ken Pizzini] + * - print strings only up to the first carriage return [various] + * - freebsd international ascii support [J Wunsch] + * - magic fixes and additions [Guy Harris] + * - 64 bit fixes [Larry Schwimmer] + * - support for both utime and utimes, but don't restore file access times + * by default [various] + * - \xXX only takes 2 hex digits, not 3. + * - re-implement support for core files [Guy Harris] + * + * Revision 1.21 1996/10/05 18:15:29 christos + * Segregate elf stuff and conditionally enable it with -DBUILTIN_ELF + * More magic fixes + * + * Revision 1.20 1996/06/22 22:15:52 christos + * - support relative offsets of the form >& + * - fix bug with truncating magic strings that contain \n + * - file -f - did not read from stdin as documented + * - support elf file parsing using our own elf support. + * - as always magdir fixes and additions. + * + * Revision 1.19 1995/10/27 23:14:46 christos + * Ability to parse colon separated list of magic files + * New LEGAL.NOTICE + * Various magic file changes + * + * Revision 1.18 1995/05/20 22:09:21 christos + * Passed incorrect argument to eatsize(). + * Use %ld and %lx where appropriate. + * Remove unused variables + * ELF support for both big and little endian + * Fixes for small files again. + * + * Revision 1.17 1995/04/28 17:29:13 christos + * - Incorrect nroff detection fix from der Mouse + * - Lost and incorrect magic entries. + * - Added ELF stripped binary detection [in C; ugh] + * - Look for $MAGIC to find the magic file. + * - Eat trailing size specifications from numbers i.e. ignore 10L + * - More fixes for very short files + * + * Revision 1.16 1995/03/25 22:06:45 christos + * - use strtoul() where it exists. + * - fix sign-extend bug + * - try to detect tar archives before nroff files, otherwise + * tar files where the first file starts with a . will not work + * + * Revision 1.15 1995/01/21 21:03:35 christos + * Added CSECTION for the file man page + * Added version flag -v + * Fixed bug with -f input flag (from iorio@violet.berkeley.edu) + * Lots of magic fixes and reorganization... + * + * Revision 1.14 1994/05/03 17:58:23 christos + * changes from mycroft@gnu.ai.mit.edu (Charles Hannum) for unsigned + * + * Revision 1.13 1994/01/21 01:27:01 christos + * Fixed null termination bug from Don Seeley at BSDI in ascmagic.c + * + * Revision 1.12 1993/10/27 20:59:05 christos + * Changed -z flag to understand gzip format too. + * Moved builtin compression detection to a table, and move + * the compress magic entry out of the source. + * Made printing of numbers unsigned, and added the mask to it. + * Changed the buffer size to 8k, because gzip will refuse to + * unzip just a few bytes. + * + * Revision 1.11 1993/09/24 18:49:06 christos + * Fixed small bug in softmagic.c introduced by + * copying the data to be examined out of the input + * buffer. Changed the Makefile to use sed to create + * the correct man pages. + * + * Revision 1.10 1993/09/23 21:56:23 christos + * Passed purify. Fixed indirections. Fixed byte order printing. + * Fixed segmentation faults caused by referencing past the end + * of the magic buffer. Fixed bus errors caused by referencing + * unaligned shorts or longs. + * + * Revision 1.9 1993/03/24 14:23:40 ian + * Batch of minor changes from several contributors. + * + * Revision 1.8 93/02/19 15:01:26 ian + * Numerous changes from Guy Harris too numerous to mention but including + * byte-order independance, fixing "old-style masking", etc. etc. A bugfix + * for broken symlinks from martin@@d255s004.zfe.siemens.de. + * + * Revision 1.7 93/01/05 14:57:27 ian + * Couple of nits picked by Christos (again, thanks). + * + * Revision 1.6 93/01/05 13:51:09 ian + * Lotsa work on the Magic directory. + * + * Revision 1.5 92/09/14 14:54:51 ian + * Fix a tiny null-pointer bug in previous fix for tar archive + uncompress. + * + */ diff --git a/ext/fileinfo/libmagic/print.c b/ext/fileinfo/libmagic/print.c new file mode 100644 index 0000000..8370f50 --- /dev/null +++ b/ext/fileinfo/libmagic/print.c @@ -0,0 +1,108 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * print.c - debugging printout routines + */ + +#define _GNU_SOURCE +#include "php.h" + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: print.c,v 1.71 2011/09/20 15:28:09 christos Exp $") +#endif /* lint */ + +#include <stdio.h> +#include <string.h> +#include <stdarg.h> +#include <stdlib.h> +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <time.h> + +#define SZOF(a) (sizeof(a) / sizeof(a[0])) + +/*VARARGS*/ +protected void +file_magwarn(struct magic_set *ms, const char *f, ...) +{ + va_list va; + char *expanded_format; + TSRMLS_FETCH(); + + va_start(va, f); + vasprintf(&expanded_format, f, va); + va_end(va); + + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Warning: %s", expanded_format); + + free(expanded_format); +} + +protected const char * +file_fmttime(uint32_t v, int local) +{ + char *pp; + time_t t = (time_t)v; + struct tm *tm; + + if (local) { + pp = ctime(&t); + } else { +#ifndef HAVE_DAYLIGHT + private int daylight = 0; +#ifdef HAVE_TM_ISDST + private time_t now = (time_t)0; + + if (now == (time_t)0) { + struct tm *tm1; + (void)time(&now); + tm1 = localtime(&now); + if (tm1 == NULL) + goto out; + daylight = tm1->tm_isdst; + } +#endif /* HAVE_TM_ISDST */ +#endif /* HAVE_DAYLIGHT */ + if (daylight) + t += 3600; + tm = gmtime(&t); + if (tm == NULL) + goto out; + pp = asctime(tm); + } + + if (pp == NULL) + goto out; + pp[strcspn(pp, "\n")] = '\0'; + return pp; +out: + return "*Invalid time*"; +} diff --git a/ext/fileinfo/libmagic/readcdf.c b/ext/fileinfo/libmagic/readcdf.c new file mode 100644 index 0000000..0c28ffb --- /dev/null +++ b/ext/fileinfo/libmagic/readcdf.c @@ -0,0 +1,328 @@ +/*- + * Copyright (c) 2008 Christos Zoulas + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: readcdf.c,v 1.29 2012/02/20 20:04:58 christos Exp $") +#endif + +#include <stdlib.h> +#ifdef PHP_WIN32 +#include "win32/unistd.h" +#else +#include <unistd.h> +#endif +#include <string.h> +#include <time.h> +#include <ctype.h> + +#include "cdf.h" +#include "magic.h" + +#define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0) + +private int +cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info, + size_t count) +{ + size_t i; + cdf_timestamp_t tp; + struct timeval ts; + char buf[64]; + const char *str = NULL; + const char *s; + int len; + + for (i = 0; i < count; i++) { + cdf_print_property_name(buf, sizeof(buf), info[i].pi_id); + switch (info[i].pi_type) { + case CDF_NULL: + break; + case CDF_SIGNED16: + if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf, + info[i].pi_s16) == -1) + return -1; + break; + case CDF_SIGNED32: + if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf, + info[i].pi_s32) == -1) + return -1; + break; + case CDF_UNSIGNED32: + if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf, + info[i].pi_u32) == -1) + return -1; + break; + case CDF_FLOAT: + if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf, + info[i].pi_f) == -1) + return -1; + break; + case CDF_DOUBLE: + if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf, + info[i].pi_d) == -1) + return -1; + break; + case CDF_LENGTH32_STRING: + case CDF_LENGTH32_WSTRING: + len = info[i].pi_str.s_len; + if (len > 1) { + char vbuf[1024]; + size_t j, k = 1; + + if (info[i].pi_type == CDF_LENGTH32_WSTRING) + k++; + s = info[i].pi_str.s_buf; + for (j = 0; j < sizeof(vbuf) && len--; + j++, s += k) { + if (*s == '\0') + break; + if (isprint((unsigned char)*s)) + vbuf[j] = *s; + } + if (j == sizeof(vbuf)) + --j; + vbuf[j] = '\0'; + if (NOTMIME(ms)) { + if (vbuf[0]) { + if (file_printf(ms, ", %s: %s", + buf, vbuf) == -1) + return -1; + } + } else if (info[i].pi_id == + CDF_PROPERTY_NAME_OF_APPLICATION) { + if (strstr(vbuf, "Word")) + str = "msword"; + else if (strstr(vbuf, "Excel")) + str = "vnd.ms-excel"; + else if (strstr(vbuf, "Powerpoint")) + str = "vnd.ms-powerpoint"; + else if (strstr(vbuf, + "Crystal Reports")) + str = "x-rpt"; + } + } + break; + case CDF_FILETIME: + tp = info[i].pi_tp; + if (tp != 0) { +#if defined(PHP_WIN32) && _MSC_VER <= 1500 + if (tp < 1000000000000000i64) { +#else + if (tp < 1000000000000000LL) { +#endif + char tbuf[64]; + cdf_print_elapsed_time(tbuf, + sizeof(tbuf), tp); + if (NOTMIME(ms) && file_printf(ms, + ", %s: %s", buf, tbuf) == -1) + return -1; + } else { + char *c, *ec; + if (cdf_timestamp_to_timespec(&ts, tp) == -1) { + return -1; + } + c = cdf_ctime(&ts.tv_sec); + if ((ec = strchr(c, '\n')) != NULL) + *ec = '\0'; + + if (NOTMIME(ms) && file_printf(ms, + ", %s: %s", buf, c) == -1) + return -1; + } + } + break; + case CDF_CLIPBOARD: + break; + default: + return -1; + } + } + if (!NOTMIME(ms)) { + if (str == NULL) + return 0; + if (file_printf(ms, "application/%s", str) == -1) + return -1; + } + return 1; +} + +private int +cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h, + const cdf_stream_t *sst) +{ + cdf_summary_info_header_t si; + cdf_property_info_t *info; + size_t count; + int m; + + if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1) + return -1; + + if (NOTMIME(ms)) { + if (file_printf(ms, "Composite Document File V2 Document") + == -1) + return -1; + + if (file_printf(ms, ", %s Endian", + si.si_byte_order == 0xfffe ? "Little" : "Big") == -1) + return -2; + switch (si.si_os) { + case 2: + if (file_printf(ms, ", Os: Windows, Version %d.%d", + si.si_os_version & 0xff, + (uint32_t)si.si_os_version >> 8) == -1) + return -2; + break; + case 1: + if (file_printf(ms, ", Os: MacOS, Version %d.%d", + (uint32_t)si.si_os_version >> 8, + si.si_os_version & 0xff) == -1) + return -2; + break; + default: + if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os, + si.si_os_version & 0xff, + (uint32_t)si.si_os_version >> 8) == -1) + return -2; + break; + } + } + + m = cdf_file_property_info(ms, info, count); + free(info); + + return m == -1 ? -2 : m; +} + +protected int +file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf, + size_t nbytes) +{ + cdf_info_t info; + cdf_header_t h; + cdf_sat_t sat, ssat; + cdf_stream_t sst, scn; + cdf_dir_t dir; + int i; + const char *expn = ""; + const char *corrupt = "corrupt: "; + + info.i_fd = fd; + info.i_buf = buf; + info.i_len = nbytes; + if (ms->flags & MAGIC_APPLE) + return 0; + if (cdf_read_header(&info, &h) == -1) + return 0; +#ifdef CDF_DEBUG + cdf_dump_header(&h); +#endif + + if ((i = cdf_read_sat(&info, &h, &sat)) == -1) { + expn = "Can't read SAT"; + goto out0; + } +#ifdef CDF_DEBUG + cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h)); +#endif + + if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) { + expn = "Can't read SSAT"; + goto out1; + } +#ifdef CDF_DEBUG + cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h)); +#endif + + if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) { + expn = "Can't read directory"; + goto out2; + } + + if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst)) == -1) { + expn = "Cannot read short stream"; + goto out3; + } +#ifdef CDF_DEBUG + cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir); +#endif + + if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir, + &scn)) == -1) { + if (errno == ESRCH) { + corrupt = expn; + expn = "No summary info"; + } else { + expn = "Cannot read summary info"; + } + goto out4; + } +#ifdef CDF_DEBUG + cdf_dump_summary_info(&h, &scn); +#endif + if ((i = cdf_file_summary_info(ms, &h, &scn)) < 0) + expn = "Can't expand summary_info"; + if (i == 0) { + const char *str = "vnd.ms-office"; + cdf_directory_t *d; + char name[__arraycount(d->d_name)]; + size_t j, k; + for (j = 0; j < dir.dir_len; j++) { + d = &dir.dir_tab[j]; + for (k = 0; k < sizeof(name); k++) + name[k] = (char)cdf_tole2(d->d_name[k]); + if (strstr(name, "WordDocument") == 0) { + str = "msword"; + break; + } + } + if (file_printf(ms, "application/%s", str) == -1) + return -1; + i = 1; + } + free(scn.sst_tab); +out4: + free(sst.sst_tab); +out3: + free(dir.dir_tab); +out2: + free(ssat.sat_tab); +out1: + free(sat.sat_tab); +out0: + if (i != 1) { + if (i == -1) + if (file_printf(ms, "Composite Document File V2 Document") + == -1) + return -1; + if (*expn) + if (file_printf(ms, ", %s%s", corrupt, expn) == -1) + return -1; + i = 1; + } + return i; +} diff --git a/ext/fileinfo/libmagic/readelf.c b/ext/fileinfo/libmagic/readelf.c new file mode 100644 index 0000000..f18f7d2 --- /dev/null +++ b/ext/fileinfo/libmagic/readelf.c @@ -0,0 +1,1190 @@ +/* + * Copyright (c) Christos Zoulas 2003. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: readelf.c,v 1.90 2011/08/23 08:01:12 christos Exp $") +#endif + +#ifdef BUILTIN_ELF +#include <string.h> +#include <ctype.h> +#include <stdlib.h> +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif + +#include "readelf.h" +#include "magic.h" + +#ifdef ELFCORE +private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t, + off_t, int *); +#endif +private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t, + off_t, int *, int); +private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, + off_t, int *, int); +private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int, + int, size_t, int *); + +#define ELF_ALIGN(a) ((((a) + align - 1) / align) * align) + +#define isquote(c) (strchr("'\"`", (c)) != NULL) + +private uint16_t getu16(int, uint16_t); +private uint32_t getu32(int, uint32_t); +private uint64_t getu64(int, uint64_t); + +private uint16_t +getu16(int swap, uint16_t value) +{ + union { + uint16_t ui; + char c[2]; + } retval, tmpval; + + if (swap) { + tmpval.ui = value; + + retval.c[0] = tmpval.c[1]; + retval.c[1] = tmpval.c[0]; + + return retval.ui; + } else + return value; +} + +private uint32_t +getu32(int swap, uint32_t value) +{ + union { + uint32_t ui; + char c[4]; + } retval, tmpval; + + if (swap) { + tmpval.ui = value; + + retval.c[0] = tmpval.c[3]; + retval.c[1] = tmpval.c[2]; + retval.c[2] = tmpval.c[1]; + retval.c[3] = tmpval.c[0]; + + return retval.ui; + } else + return value; +} + +private uint64_t +getu64(int swap, uint64_t value) +{ + union { + uint64_t ui; + char c[8]; + } retval, tmpval; + + if (swap) { + tmpval.ui = value; + + retval.c[0] = tmpval.c[7]; + retval.c[1] = tmpval.c[6]; + retval.c[2] = tmpval.c[5]; + retval.c[3] = tmpval.c[4]; + retval.c[4] = tmpval.c[3]; + retval.c[5] = tmpval.c[2]; + retval.c[6] = tmpval.c[1]; + retval.c[7] = tmpval.c[0]; + + return retval.ui; + } else + return value; +} + +#define elf_getu16(swap, value) getu16(swap, value) +#define elf_getu32(swap, value) getu32(swap, value) +#ifdef USE_ARRAY_FOR_64BIT_TYPES +# define elf_getu64(swap, array) \ + ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \ + (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32))) +#else +# define elf_getu64(swap, value) getu64(swap, value) +#endif + +#define xsh_addr (clazz == ELFCLASS32 \ + ? (void *) &sh32 \ + : (void *) &sh64) +#define xsh_sizeof (clazz == ELFCLASS32 \ + ? sizeof sh32 \ + : sizeof sh64) +#define xsh_size (clazz == ELFCLASS32 \ + ? elf_getu32(swap, sh32.sh_size) \ + : elf_getu64(swap, sh64.sh_size)) +#define xsh_offset (off_t)(clazz == ELFCLASS32 \ + ? elf_getu32(swap, sh32.sh_offset) \ + : elf_getu64(swap, sh64.sh_offset)) +#define xsh_type (clazz == ELFCLASS32 \ + ? elf_getu32(swap, sh32.sh_type) \ + : elf_getu32(swap, sh64.sh_type)) +#define xph_addr (clazz == ELFCLASS32 \ + ? (void *) &ph32 \ + : (void *) &ph64) +#define xph_sizeof (clazz == ELFCLASS32 \ + ? sizeof ph32 \ + : sizeof ph64) +#define xph_type (clazz == ELFCLASS32 \ + ? elf_getu32(swap, ph32.p_type) \ + : elf_getu32(swap, ph64.p_type)) +#define xph_offset (off_t)(clazz == ELFCLASS32 \ + ? elf_getu32(swap, ph32.p_offset) \ + : elf_getu64(swap, ph64.p_offset)) +#define xph_align (size_t)((clazz == ELFCLASS32 \ + ? (off_t) (ph32.p_align ? \ + elf_getu32(swap, ph32.p_align) : 4) \ + : (off_t) (ph64.p_align ? \ + elf_getu64(swap, ph64.p_align) : 4))) +#define xph_filesz (size_t)((clazz == ELFCLASS32 \ + ? elf_getu32(swap, ph32.p_filesz) \ + : elf_getu64(swap, ph64.p_filesz))) +#define xnh_addr (clazz == ELFCLASS32 \ + ? (void *) &nh32 \ + : (void *) &nh64) +#define xph_memsz (size_t)((clazz == ELFCLASS32 \ + ? elf_getu32(swap, ph32.p_memsz) \ + : elf_getu64(swap, ph64.p_memsz))) +#define xnh_sizeof (clazz == ELFCLASS32 \ + ? sizeof nh32 \ + : sizeof nh64) +#define xnh_type (clazz == ELFCLASS32 \ + ? elf_getu32(swap, nh32.n_type) \ + : elf_getu32(swap, nh64.n_type)) +#define xnh_namesz (clazz == ELFCLASS32 \ + ? elf_getu32(swap, nh32.n_namesz) \ + : elf_getu32(swap, nh64.n_namesz)) +#define xnh_descsz (clazz == ELFCLASS32 \ + ? elf_getu32(swap, nh32.n_descsz) \ + : elf_getu32(swap, nh64.n_descsz)) +#define prpsoffsets(i) (clazz == ELFCLASS32 \ + ? prpsoffsets32[i] \ + : prpsoffsets64[i]) +#define xcap_addr (clazz == ELFCLASS32 \ + ? (void *) &cap32 \ + : (void *) &cap64) +#define xcap_sizeof (clazz == ELFCLASS32 \ + ? sizeof cap32 \ + : sizeof cap64) +#define xcap_tag (clazz == ELFCLASS32 \ + ? elf_getu32(swap, cap32.c_tag) \ + : elf_getu64(swap, cap64.c_tag)) +#define xcap_val (clazz == ELFCLASS32 \ + ? elf_getu32(swap, cap32.c_un.c_val) \ + : elf_getu64(swap, cap64.c_un.c_val)) + +#ifdef ELFCORE +/* + * Try larger offsets first to avoid false matches + * from earlier data that happen to look like strings. + */ +static const size_t prpsoffsets32[] = { +#ifdef USE_NT_PSINFO + 104, /* SunOS 5.x (command line) */ + 88, /* SunOS 5.x (short name) */ +#endif /* USE_NT_PSINFO */ + + 100, /* SunOS 5.x (command line) */ + 84, /* SunOS 5.x (short name) */ + + 44, /* Linux (command line) */ + 28, /* Linux 2.0.36 (short name) */ + + 8, /* FreeBSD */ +}; + +static const size_t prpsoffsets64[] = { +#ifdef USE_NT_PSINFO + 152, /* SunOS 5.x (command line) */ + 136, /* SunOS 5.x (short name) */ +#endif /* USE_NT_PSINFO */ + + 136, /* SunOS 5.x, 64-bit (command line) */ + 120, /* SunOS 5.x, 64-bit (short name) */ + + 56, /* Linux (command line) */ + 40, /* Linux (tested on core from 2.4.x, short name) */ + + 16, /* FreeBSD, 64-bit */ +}; + +#define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0]) +#define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0]) + +#define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64) + +/* + * Look through the program headers of an executable image, searching + * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or + * "FreeBSD"; if one is found, try looking in various places in its + * contents for a 16-character string containing only printable + * characters - if found, that string should be the name of the program + * that dropped core. Note: right after that 16-character string is, + * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and + * Linux, a longer string (80 characters, in 5.x, probably other + * SVR4-flavored systems, and Linux) containing the start of the + * command line for that program. + * + * SunOS 5.x core files contain two PT_NOTE sections, with the types + * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the + * same info about the command name and command line, so it probably + * isn't worthwhile to look for NT_PSINFO, but the offsets are provided + * above (see USE_NT_PSINFO), in case we ever decide to do so. The + * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent; + * the SunOS 5.x file command relies on this (and prefers the latter). + * + * The signal number probably appears in a section of type NT_PRSTATUS, + * but that's also rather OS-dependent, in ways that are harder to + * dissect with heuristics, so I'm not bothering with the signal number. + * (I suppose the signal number could be of interest in situations where + * you don't have the binary of the program that dropped core; if you + * *do* have that binary, the debugger will probably tell you what + * signal it was.) + */ + +#define OS_STYLE_SVR4 0 +#define OS_STYLE_FREEBSD 1 +#define OS_STYLE_NETBSD 2 + +private const char os_style_names[][8] = { + "SVR4", + "FreeBSD", + "NetBSD", +}; + +#define FLAGS_DID_CORE 0x01 +#define FLAGS_DID_NOTE 0x02 +#define FLAGS_DID_BUILD_ID 0x04 +#define FLAGS_DID_CORE_STYLE 0x08 +#define FLAGS_IS_CORE 0x10 + +private int +dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off, + int num, size_t size, off_t fsize, int *flags) +{ + Elf32_Phdr ph32; + Elf64_Phdr ph64; + size_t offset; + unsigned char nbuf[BUFSIZ]; + ssize_t bufsize; + + if (size != xph_sizeof) { + if (file_printf(ms, ", corrupted program header size") == -1) + return -1; + return 0; + } + + /* + * Loop through all the program headers. + */ + for ( ; num; num--) { + if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) { + file_badseek(ms); + return -1; + } + if (FINFO_READ_FUNC(fd, xph_addr, xph_sizeof) == -1) { + file_badread(ms); + return -1; + } + off += size; + + if (xph_offset > fsize) { + /* Perhaps warn here */ + continue; + } + + if (xph_type != PT_NOTE) + continue; + + /* + * This is a PT_NOTE section; loop through all the notes + * in the section. + */ + if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (off_t)-1) { + file_badseek(ms); + return -1; + } + bufsize = FINFO_READ_FUNC(fd, nbuf, + ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf))); + if (bufsize == -1) { + file_badread(ms); + return -1; + } + offset = 0; + for (;;) { + if (offset >= (size_t)bufsize) + break; + offset = donote(ms, nbuf, offset, (size_t)bufsize, + clazz, swap, 4, flags); + if (offset == 0) + break; + + } + } + return 0; +} +#endif + +private size_t +donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size, + int clazz, int swap, size_t align, int *flags) +{ + Elf32_Nhdr nh32; + Elf64_Nhdr nh64; + size_t noff, doff; +#ifdef ELFCORE + int os_style = -1; +#endif + uint32_t namesz, descsz; + + (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof); + offset += xnh_sizeof; + + namesz = xnh_namesz; + descsz = xnh_descsz; + if ((namesz == 0) && (descsz == 0)) { + /* + * We're out of note headers. + */ + return (offset >= size) ? offset : size; + } + + if (namesz & 0x80000000) { + (void)file_printf(ms, ", bad note name size 0x%lx", + (unsigned long)namesz); + return offset; + } + + if (descsz & 0x80000000) { + (void)file_printf(ms, ", bad note description size 0x%lx", + (unsigned long)descsz); + return offset; + } + + + noff = offset; + doff = ELF_ALIGN(offset + namesz); + + if (offset + namesz > size) { + /* + * We're past the end of the buffer. + */ + return doff; + } + + offset = ELF_ALIGN(doff + descsz); + if (doff + descsz > size) { + /* + * We're past the end of the buffer. + */ + return (offset >= size) ? offset : size; + } + + if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) == + (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) + goto core; + + if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 && + xnh_type == NT_GNU_VERSION && descsz == 16) { + uint32_t desc[4]; + (void)memcpy(desc, &nbuf[doff], sizeof(desc)); + + if (file_printf(ms, ", for GNU/") == -1) + return size; + switch (elf_getu32(swap, desc[0])) { + case GNU_OS_LINUX: + if (file_printf(ms, "Linux") == -1) + return size; + break; + case GNU_OS_HURD: + if (file_printf(ms, "Hurd") == -1) + return size; + break; + case GNU_OS_SOLARIS: + if (file_printf(ms, "Solaris") == -1) + return size; + break; + case GNU_OS_KFREEBSD: + if (file_printf(ms, "kFreeBSD") == -1) + return size; + break; + case GNU_OS_KNETBSD: + if (file_printf(ms, "kNetBSD") == -1) + return size; + break; + default: + if (file_printf(ms, "<unknown>") == -1) + return size; + } + if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]), + elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1) + return size; + *flags |= FLAGS_DID_NOTE; + return size; + } + + if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 && + xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) { + uint32_t desc[5], i; + if (file_printf(ms, ", BuildID[%s]=0x", descsz == 16 ? "md5/uuid" : + "sha1") == -1) + return size; + (void)memcpy(desc, &nbuf[doff], descsz); + for (i = 0; i < descsz >> 2; i++) + if (file_printf(ms, "%.8x", desc[i]) == -1) + return size; + *flags |= FLAGS_DID_BUILD_ID; + } + + if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 && + xnh_type == NT_NETBSD_VERSION && descsz == 4) { + uint32_t desc; + (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); + desc = elf_getu32(swap, desc); + + if (file_printf(ms, ", for NetBSD") == -1) + return size; + /* + * The version number used to be stuck as 199905, and was thus + * basically content-free. Newer versions of NetBSD have fixed + * this and now use the encoding of __NetBSD_Version__: + * + * MMmmrrpp00 + * + * M = major version + * m = minor version + * r = release ["",A-Z,Z[A-Z] but numeric] + * p = patchlevel + */ + if (desc > 100000000U) { + uint32_t ver_patch = (desc / 100) % 100; + uint32_t ver_rel = (desc / 10000) % 100; + uint32_t ver_min = (desc / 1000000) % 100; + uint32_t ver_maj = desc / 100000000; + + if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1) + return size; + if (ver_rel == 0 && ver_patch != 0) { + if (file_printf(ms, ".%u", ver_patch) == -1) + return size; + } else if (ver_rel != 0) { + while (ver_rel > 26) { + if (file_printf(ms, "Z") == -1) + return size; + ver_rel -= 26; + } + if (file_printf(ms, "%c", 'A' + ver_rel - 1) + == -1) + return size; + } + } + *flags |= FLAGS_DID_NOTE; + return size; + } + + if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 && + xnh_type == NT_FREEBSD_VERSION && descsz == 4) { + uint32_t desc; + (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); + desc = elf_getu32(swap, desc); + if (file_printf(ms, ", for FreeBSD") == -1) + return size; + + /* + * Contents is __FreeBSD_version, whose relation to OS + * versions is defined by a huge table in the Porter's + * Handbook. This is the general scheme: + * + * Releases: + * Mmp000 (before 4.10) + * Mmi0p0 (before 5.0) + * Mmm0p0 + * + * Development branches: + * Mmpxxx (before 4.6) + * Mmp1xx (before 4.10) + * Mmi1xx (before 5.0) + * M000xx (pre-M.0) + * Mmm1xx + * + * M = major version + * m = minor version + * i = minor version increment (491000 -> 4.10) + * p = patchlevel + * x = revision + * + * The first release of FreeBSD to use ELF by default + * was version 3.0. + */ + if (desc == 460002) { + if (file_printf(ms, " 4.6.2") == -1) + return size; + } else if (desc < 460100) { + if (file_printf(ms, " %d.%d", desc / 100000, + desc / 10000 % 10) == -1) + return size; + if (desc / 1000 % 10 > 0) + if (file_printf(ms, ".%d", desc / 1000 % 10) + == -1) + return size; + if ((desc % 1000 > 0) || (desc % 100000 == 0)) + if (file_printf(ms, " (%d)", desc) == -1) + return size; + } else if (desc < 500000) { + if (file_printf(ms, " %d.%d", desc / 100000, + desc / 10000 % 10 + desc / 1000 % 10) == -1) + return size; + if (desc / 100 % 10 > 0) { + if (file_printf(ms, " (%d)", desc) == -1) + return size; + } else if (desc / 10 % 10 > 0) { + if (file_printf(ms, ".%d", desc / 10 % 10) + == -1) + return size; + } + } else { + if (file_printf(ms, " %d.%d", desc / 100000, + desc / 1000 % 100) == -1) + return size; + if ((desc / 100 % 10 > 0) || + (desc % 100000 / 100 == 0)) { + if (file_printf(ms, " (%d)", desc) == -1) + return size; + } else if (desc / 10 % 10 > 0) { + if (file_printf(ms, ".%d", desc / 10 % 10) + == -1) + return size; + } + } + *flags |= FLAGS_DID_NOTE; + return size; + } + + if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 && + xnh_type == NT_OPENBSD_VERSION && descsz == 4) { + if (file_printf(ms, ", for OpenBSD") == -1) + return size; + /* Content of note is always 0 */ + *flags |= FLAGS_DID_NOTE; + return size; + } + + if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 && + xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) { + uint32_t desc; + if (file_printf(ms, ", for DragonFly") == -1) + return size; + (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); + desc = elf_getu32(swap, desc); + if (file_printf(ms, " %d.%d.%d", desc / 100000, + desc / 10000 % 10, desc % 10000) == -1) + return size; + *flags |= FLAGS_DID_NOTE; + return size; + } + +core: + /* + * Sigh. The 2.0.36 kernel in Debian 2.1, at + * least, doesn't correctly implement name + * sections, in core dumps, as specified by + * the "Program Linking" section of "UNIX(R) System + * V Release 4 Programmer's Guide: ANSI C and + * Programming Support Tools", because my copy + * clearly says "The first 'namesz' bytes in 'name' + * contain a *null-terminated* [emphasis mine] + * character representation of the entry's owner + * or originator", but the 2.0.36 kernel code + * doesn't include the terminating null in the + * name.... + */ + if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) || + (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) { + os_style = OS_STYLE_SVR4; + } + + if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) { + os_style = OS_STYLE_FREEBSD; + } + + if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11) + == 0)) { + os_style = OS_STYLE_NETBSD; + } + +#ifdef ELFCORE + if ((*flags & FLAGS_DID_CORE) != 0) + return size; + + if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) { + if (file_printf(ms, ", %s-style", os_style_names[os_style]) + == -1) + return size; + *flags |= FLAGS_DID_CORE_STYLE; + } + + switch (os_style) { + case OS_STYLE_NETBSD: + if (xnh_type == NT_NETBSD_CORE_PROCINFO) { + uint32_t signo; + /* + * Extract the program name. It is at + * offset 0x7c, and is up to 32-bytes, + * including the terminating NUL. + */ + if (file_printf(ms, ", from '%.31s'", + &nbuf[doff + 0x7c]) == -1) + return size; + + /* + * Extract the signal number. It is at + * offset 0x08. + */ + (void)memcpy(&signo, &nbuf[doff + 0x08], + sizeof(signo)); + if (file_printf(ms, " (signal %u)", + elf_getu32(swap, signo)) == -1) + return size; + *flags |= FLAGS_DID_CORE; + return size; + } + break; + + default: + if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) { + size_t i, j; + unsigned char c; + /* + * Extract the program name. We assume + * it to be 16 characters (that's what it + * is in SunOS 5.x and Linux). + * + * Unfortunately, it's at a different offset + * in various OSes, so try multiple offsets. + * If the characters aren't all printable, + * reject it. + */ + for (i = 0; i < NOFFSETS; i++) { + unsigned char *cname, *cp; + size_t reloffset = prpsoffsets(i); + size_t noffset = doff + reloffset; + size_t k; + for (j = 0; j < 16; j++, noffset++, + reloffset++) { + /* + * Make sure we're not past + * the end of the buffer; if + * we are, just give up. + */ + if (noffset >= size) + goto tryanother; + + /* + * Make sure we're not past + * the end of the contents; + * if we are, this obviously + * isn't the right offset. + */ + if (reloffset >= descsz) + goto tryanother; + + c = nbuf[noffset]; + if (c == '\0') { + /* + * A '\0' at the + * beginning is + * obviously wrong. + * Any other '\0' + * means we're done. + */ + if (j == 0) + goto tryanother; + else + break; + } else { + /* + * A nonprintable + * character is also + * wrong. + */ + if (!isprint(c) || isquote(c)) + goto tryanother; + } + } + /* + * Well, that worked. + */ + + /* + * Try next offsets, in case this match is + * in the middle of a string. + */ + for (k = i + 1 ; k < NOFFSETS ; k++) { + size_t no; + int adjust = 1; + if (prpsoffsets(k) >= prpsoffsets(i)) + continue; + for (no = doff + prpsoffsets(k); + no < doff + prpsoffsets(i); no++) + adjust = adjust + && isprint(nbuf[no]); + if (adjust) + i = k; + } + + cname = (unsigned char *) + &nbuf[doff + prpsoffsets(i)]; + for (cp = cname; *cp && isprint(*cp); cp++) + continue; + /* + * Linux apparently appends a space at the end + * of the command line: remove it. + */ + while (cp > cname && isspace(cp[-1])) + cp--; + if (file_printf(ms, ", from '%.*s'", + (int)(cp - cname), cname) == -1) + return size; + *flags |= FLAGS_DID_CORE; + return size; + + tryanother: + ; + } + } + break; + } +#endif + return offset; +} + +/* SunOS 5.x hardware capability descriptions */ +typedef struct cap_desc { + uint64_t cd_mask; + const char *cd_name; +} cap_desc_t; + +static const cap_desc_t cap_desc_sparc[] = { + { AV_SPARC_MUL32, "MUL32" }, + { AV_SPARC_DIV32, "DIV32" }, + { AV_SPARC_FSMULD, "FSMULD" }, + { AV_SPARC_V8PLUS, "V8PLUS" }, + { AV_SPARC_POPC, "POPC" }, + { AV_SPARC_VIS, "VIS" }, + { AV_SPARC_VIS2, "VIS2" }, + { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" }, + { AV_SPARC_FMAF, "FMAF" }, + { AV_SPARC_FJFMAU, "FJFMAU" }, + { AV_SPARC_IMA, "IMA" }, + { 0, NULL } +}; + +static const cap_desc_t cap_desc_386[] = { + { AV_386_FPU, "FPU" }, + { AV_386_TSC, "TSC" }, + { AV_386_CX8, "CX8" }, + { AV_386_SEP, "SEP" }, + { AV_386_AMD_SYSC, "AMD_SYSC" }, + { AV_386_CMOV, "CMOV" }, + { AV_386_MMX, "MMX" }, + { AV_386_AMD_MMX, "AMD_MMX" }, + { AV_386_AMD_3DNow, "AMD_3DNow" }, + { AV_386_AMD_3DNowx, "AMD_3DNowx" }, + { AV_386_FXSR, "FXSR" }, + { AV_386_SSE, "SSE" }, + { AV_386_SSE2, "SSE2" }, + { AV_386_PAUSE, "PAUSE" }, + { AV_386_SSE3, "SSE3" }, + { AV_386_MON, "MON" }, + { AV_386_CX16, "CX16" }, + { AV_386_AHF, "AHF" }, + { AV_386_TSCP, "TSCP" }, + { AV_386_AMD_SSE4A, "AMD_SSE4A" }, + { AV_386_POPCNT, "POPCNT" }, + { AV_386_AMD_LZCNT, "AMD_LZCNT" }, + { AV_386_SSSE3, "SSSE3" }, + { AV_386_SSE4_1, "SSE4.1" }, + { AV_386_SSE4_2, "SSE4.2" }, + { 0, NULL } +}; + +private int +doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num, + size_t size, off_t fsize, int *flags, int mach) +{ + Elf32_Shdr sh32; + Elf64_Shdr sh64; + int stripped = 1; + void *nbuf; + off_t noff, coff; + uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */ + uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */ + + if (size != xsh_sizeof) { + if (file_printf(ms, ", corrupted section header size") == -1) + return -1; + return 0; + } + + for ( ; num; num--) { + if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) { + file_badseek(ms); + return -1; + } + if (FINFO_READ_FUNC(fd, xsh_addr, xsh_sizeof) == -1) { + file_badread(ms); + return -1; + } + off += size; + + /* Things we can determine before we seek */ + switch (xsh_type) { + case SHT_SYMTAB: +#if 0 + case SHT_DYNSYM: +#endif + stripped = 0; + break; + default: + if (xsh_offset > fsize) { + /* Perhaps warn here */ + continue; + } + break; + } + + /* Things we can determine when we seek */ + switch (xsh_type) { + case SHT_NOTE: + nbuf = emalloc((size_t)xsh_size); + if ((noff = FINFO_LSEEK_FUNC(fd, (off_t)xsh_offset, SEEK_SET)) == + (off_t)-1) { + file_badread(ms); + efree(nbuf); + return -1; + } + if (FINFO_READ_FUNC(fd, nbuf, (size_t)xsh_size) != + (ssize_t)xsh_size) { + efree(nbuf); + file_badread(ms); + return -1; + } + + noff = 0; + for (;;) { + if (noff >= (off_t)xsh_size) + break; + noff = donote(ms, nbuf, (size_t)noff, + (size_t)xsh_size, clazz, swap, 4, + flags); + if (noff == 0) + break; + } + efree(nbuf); + break; + case SHT_SUNW_cap: + if (FINFO_LSEEK_FUNC(fd, (off_t)xsh_offset, SEEK_SET) == + (off_t)-1) { + file_badseek(ms); + return -1; + } + coff = 0; + for (;;) { + Elf32_Cap cap32; + Elf64_Cap cap64; + char cbuf[/*CONSTCOND*/ + MAX(sizeof cap32, sizeof cap64)]; + if ((coff += xcap_sizeof) > (off_t)xsh_size) + break; + if (FINFO_READ_FUNC(fd, cbuf, (size_t)xcap_sizeof) != + (ssize_t)xcap_sizeof) { + file_badread(ms); + return -1; + } + (void)memcpy(xcap_addr, cbuf, xcap_sizeof); + switch (xcap_tag) { + case CA_SUNW_NULL: + break; + case CA_SUNW_HW_1: + cap_hw1 |= xcap_val; + break; + case CA_SUNW_SF_1: + cap_sf1 |= xcap_val; + break; + default: + if (file_printf(ms, + ", with unknown capability " + "0x%" INT64_T_FORMAT "x = 0x%" + INT64_T_FORMAT "x", + (unsigned long long)xcap_tag, + (unsigned long long)xcap_val) == -1) + return -1; + break; + } + } + break; + + default: + break; + } + } + if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1) + return -1; + if (cap_hw1) { + const cap_desc_t *cdp; + switch (mach) { + case EM_SPARC: + case EM_SPARC32PLUS: + case EM_SPARCV9: + cdp = cap_desc_sparc; + break; + case EM_386: + case EM_IA_64: + case EM_AMD64: + cdp = cap_desc_386; + break; + default: + cdp = NULL; + break; + } + if (file_printf(ms, ", uses") == -1) + return -1; + if (cdp) { + while (cdp->cd_name) { + if (cap_hw1 & cdp->cd_mask) { + if (file_printf(ms, + " %s", cdp->cd_name) == -1) + return -1; + cap_hw1 &= ~cdp->cd_mask; + } + ++cdp; + } + if (cap_hw1) + if (file_printf(ms, + " unknown hardware capability 0x%" + INT64_T_FORMAT "x", + (unsigned long long)cap_hw1) == -1) + return -1; + } else { + if (file_printf(ms, + " hardware capability 0x%" INT64_T_FORMAT "x", + (unsigned long long)cap_hw1) == -1) + return -1; + } + } + if (cap_sf1) { + if (cap_sf1 & SF1_SUNW_FPUSED) { + if (file_printf(ms, + (cap_sf1 & SF1_SUNW_FPKNWN) + ? ", uses frame pointer" + : ", not known to use frame pointer") == -1) + return -1; + } + cap_sf1 &= ~SF1_SUNW_MASK; + if (cap_sf1) + if (file_printf(ms, + ", with unknown software capability 0x%" + INT64_T_FORMAT "x", + (unsigned long long)cap_sf1) == -1) + return -1; + } + return 0; +} + +/* + * Look through the program headers of an executable image, searching + * for a PT_INTERP section; if one is found, it's dynamically linked, + * otherwise it's statically linked. + */ +private int +dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off, + int num, size_t size, off_t fsize, int *flags, int sh_num) +{ + Elf32_Phdr ph32; + Elf64_Phdr ph64; + const char *linking_style = "statically"; + const char *shared_libraries = ""; + unsigned char nbuf[BUFSIZ]; + ssize_t bufsize; + size_t offset, align; + + if (size != xph_sizeof) { + if (file_printf(ms, ", corrupted program header size") == -1) + return -1; + return 0; + } + + for ( ; num; num--) { + if (FINFO_LSEEK_FUNC(fd, off, SEEK_SET) == (off_t)-1) { + file_badseek(ms); + return -1; + } + + if (FINFO_READ_FUNC(fd, xph_addr, xph_sizeof) == -1) { + file_badread(ms); + return -1; + } + + off += size; + + /* Things we can determine before we seek */ + switch (xph_type) { + case PT_DYNAMIC: + linking_style = "dynamically"; + break; + case PT_INTERP: + shared_libraries = " (uses shared libs)"; + break; + default: + if (xph_offset > fsize) { + /* Maybe warn here? */ + continue; + } + break; + } + + /* Things we can determine when we seek */ + switch (xph_type) { + case PT_NOTE: + if ((align = xph_align) & 0x80000000UL) { + if (file_printf(ms, + ", invalid note alignment 0x%lx", + (unsigned long)align) == -1) + return -1; + align = 4; + } + if (sh_num) + break; + /* + * This is a PT_NOTE section; loop through all the notes + * in the section. + */ + if (FINFO_LSEEK_FUNC(fd, xph_offset, SEEK_SET) == (off_t)-1) { + file_badseek(ms); + return -1; + } + bufsize = FINFO_READ_FUNC(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ? + xph_filesz : sizeof(nbuf))); + if (bufsize == -1) { + file_badread(ms); + return -1; + } + offset = 0; + for (;;) { + if (offset >= (size_t)bufsize) + break; + offset = donote(ms, nbuf, offset, + (size_t)bufsize, clazz, swap, align, + flags); + if (offset == 0) + break; + } + break; + default: + break; + } + } + if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries) + == -1) + return -1; + return 0; +} + + +protected int +file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf, + size_t nbytes) +{ + union { + int32_t l; + char c[sizeof (int32_t)]; + } u; + int clazz; + int swap; + struct stat st; + off_t fsize; + int flags = 0; + Elf32_Ehdr elf32hdr; + Elf64_Ehdr elf64hdr; + uint16_t type; + + if (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) + return 0; + /* + * ELF executables have multiple section headers in arbitrary + * file locations and thus file(1) cannot determine it from easily. + * Instead we traverse thru all section headers until a symbol table + * one is found or else the binary is stripped. + * Return immediately if it's not ELF (so we avoid pipe2file unless needed). + */ + if (buf[EI_MAG0] != ELFMAG0 + || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1) + || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3) + return 0; + + /* + * If we cannot seek, it must be a pipe, socket or fifo. + */ + if((FINFO_LSEEK_FUNC(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE)) + fd = file_pipe2file(ms, fd, buf, nbytes); + + if (fstat(fd, &st) == -1) { + file_badread(ms); + return -1; + } + fsize = st.st_size; + + clazz = buf[EI_CLASS]; + + switch (clazz) { + case ELFCLASS32: +#undef elf_getu +#define elf_getu(a, b) elf_getu32(a, b) +#undef elfhdr +#define elfhdr elf32hdr +#include "elfclass.h" + case ELFCLASS64: +#undef elf_getu +#define elf_getu(a, b) elf_getu64(a, b) +#undef elfhdr +#define elfhdr elf64hdr +#include "elfclass.h" + default: + if (file_printf(ms, ", unknown class %d", clazz) == -1) + return -1; + break; + } + return 0; +} +#endif diff --git a/ext/fileinfo/libmagic/readelf.h b/ext/fileinfo/libmagic/readelf.h new file mode 100644 index 0000000..4308e6a --- /dev/null +++ b/ext/fileinfo/libmagic/readelf.h @@ -0,0 +1,333 @@ +/* + * Copyright (c) Christos Zoulas 2003. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * @(#)Id: readelf.h,v 1.9 2002/05/16 18:45:56 christos Exp + * + * Provide elf data structures for non-elf machines, allowing file + * non-elf hosts to determine if an elf binary is stripped. + * Note: cobbled from the linux header file, with modifications + */ +#ifndef __fake_elf_h__ +#define __fake_elf_h__ + +#if HAVE_STDINT_H +#include <stdint.h> +#endif + +typedef uint32_t Elf32_Addr; +typedef uint32_t Elf32_Off; +typedef uint16_t Elf32_Half; +typedef uint32_t Elf32_Word; +typedef uint8_t Elf32_Char; + +#if SIZEOF_LONG_LONG != 8 +#define USE_ARRAY_FOR_64BIT_TYPES +typedef uint32_t Elf64_Addr[2]; +typedef uint32_t Elf64_Off[2]; +typedef uint32_t Elf64_Xword[2]; +#else +#undef USE_ARRAY_FOR_64BIT_TYPES +typedef uint64_t Elf64_Addr; +typedef uint64_t Elf64_Off; +typedef uint64_t Elf64_Xword; +#endif +typedef uint16_t Elf64_Half; +typedef uint32_t Elf64_Word; +typedef uint8_t Elf64_Char; + +#define EI_NIDENT 16 + +typedef struct { + Elf32_Char e_ident[EI_NIDENT]; + Elf32_Half e_type; + Elf32_Half e_machine; + Elf32_Word e_version; + Elf32_Addr e_entry; /* Entry point */ + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf32_Word e_flags; + Elf32_Half e_ehsize; + Elf32_Half e_phentsize; + Elf32_Half e_phnum; + Elf32_Half e_shentsize; + Elf32_Half e_shnum; + Elf32_Half e_shstrndx; +} Elf32_Ehdr; + +typedef struct { + Elf64_Char e_ident[EI_NIDENT]; + Elf64_Half e_type; + Elf64_Half e_machine; + Elf64_Word e_version; + Elf64_Addr e_entry; /* Entry point */ + Elf64_Off e_phoff; + Elf64_Off e_shoff; + Elf64_Word e_flags; + Elf64_Half e_ehsize; + Elf64_Half e_phentsize; + Elf64_Half e_phnum; + Elf64_Half e_shentsize; + Elf64_Half e_shnum; + Elf64_Half e_shstrndx; +} Elf64_Ehdr; + +/* e_type */ +#define ET_REL 1 +#define ET_EXEC 2 +#define ET_DYN 3 +#define ET_CORE 4 + +/* e_machine (used only for SunOS 5.x hardware capabilities) */ +#define EM_SPARC 2 +#define EM_386 3 +#define EM_SPARC32PLUS 18 +#define EM_SPARCV9 43 +#define EM_IA_64 50 +#define EM_AMD64 62 + +/* sh_type */ +#define SHT_SYMTAB 2 +#define SHT_NOTE 7 +#define SHT_DYNSYM 11 +#define SHT_SUNW_cap 0x6ffffff5 /* SunOS 5.x hw/sw capabilites */ + +/* elf type */ +#define ELFDATANONE 0 /* e_ident[EI_DATA] */ +#define ELFDATA2LSB 1 +#define ELFDATA2MSB 2 + +/* elf class */ +#define ELFCLASSNONE 0 +#define ELFCLASS32 1 +#define ELFCLASS64 2 + +/* magic number */ +#define EI_MAG0 0 /* e_ident[] indexes */ +#define EI_MAG1 1 +#define EI_MAG2 2 +#define EI_MAG3 3 +#define EI_CLASS 4 +#define EI_DATA 5 +#define EI_VERSION 6 +#define EI_PAD 7 + +#define ELFMAG0 0x7f /* EI_MAG */ +#define ELFMAG1 'E' +#define ELFMAG2 'L' +#define ELFMAG3 'F' +#define ELFMAG "\177ELF" + +#define OLFMAG1 'O' +#define OLFMAG "\177OLF" + +typedef struct { + Elf32_Word p_type; + Elf32_Off p_offset; + Elf32_Addr p_vaddr; + Elf32_Addr p_paddr; + Elf32_Word p_filesz; + Elf32_Word p_memsz; + Elf32_Word p_flags; + Elf32_Word p_align; +} Elf32_Phdr; + +typedef struct { + Elf64_Word p_type; + Elf64_Word p_flags; + Elf64_Off p_offset; + Elf64_Addr p_vaddr; + Elf64_Addr p_paddr; + Elf64_Xword p_filesz; + Elf64_Xword p_memsz; + Elf64_Xword p_align; +} Elf64_Phdr; + +#define PT_NULL 0 /* p_type */ +#define PT_LOAD 1 +#define PT_DYNAMIC 2 +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 +#define PT_NUM 7 + +typedef struct { + Elf32_Word sh_name; + Elf32_Word sh_type; + Elf32_Word sh_flags; + Elf32_Addr sh_addr; + Elf32_Off sh_offset; + Elf32_Word sh_size; + Elf32_Word sh_link; + Elf32_Word sh_info; + Elf32_Word sh_addralign; + Elf32_Word sh_entsize; +} Elf32_Shdr; + +typedef struct { + Elf64_Word sh_name; + Elf64_Word sh_type; + Elf64_Off sh_flags; + Elf64_Addr sh_addr; + Elf64_Off sh_offset; + Elf64_Off sh_size; + Elf64_Word sh_link; + Elf64_Word sh_info; + Elf64_Off sh_addralign; + Elf64_Off sh_entsize; +} Elf64_Shdr; + +#define NT_NETBSD_CORE_PROCINFO 1 + +/* Note header in a PT_NOTE section */ +typedef struct elf_note { + Elf32_Word n_namesz; /* Name size */ + Elf32_Word n_descsz; /* Content size */ + Elf32_Word n_type; /* Content type */ +} Elf32_Nhdr; + +typedef struct { + Elf64_Word n_namesz; + Elf64_Word n_descsz; + Elf64_Word n_type; +} Elf64_Nhdr; + +/* Notes used in ET_CORE */ +#define NT_PRSTATUS 1 +#define NT_PRFPREG 2 +#define NT_PRPSINFO 3 +#define NT_PRXREG 4 +#define NT_TASKSTRUCT 4 +#define NT_PLATFORM 5 +#define NT_AUXV 6 + +/* Note types used in executables */ +/* NetBSD executables (name = "NetBSD") */ +#define NT_NETBSD_VERSION 1 +#define NT_NETBSD_EMULATION 2 +#define NT_FREEBSD_VERSION 1 +#define NT_OPENBSD_VERSION 1 +#define NT_DRAGONFLY_VERSION 1 +/* + * GNU executables (name = "GNU") + * word[0]: GNU OS tags + * word[1]: major version + * word[2]: minor version + * word[3]: tiny version + */ +#define NT_GNU_VERSION 1 + +/* GNU OS tags */ +#define GNU_OS_LINUX 0 +#define GNU_OS_HURD 1 +#define GNU_OS_SOLARIS 2 +#define GNU_OS_KFREEBSD 3 +#define GNU_OS_KNETBSD 4 + +/* + * GNU Hardware capability information + * word[0]: Number of entries + * word[1]: Bitmask of enabled entries + * Followed by a byte id, and a NUL terminated string per entry + */ +#define NT_GNU_HWCAP 2 + +/* + * GNU Build ID generated by ld + * 160 bit SHA1 [default] + * 128 bit md5 or uuid + */ +#define NT_GNU_BUILD_ID 3 + +/* SunOS 5.x hardware/software capabilities */ +typedef struct { + Elf32_Word c_tag; + union { + Elf32_Word c_val; + Elf32_Addr c_ptr; + } c_un; +} Elf32_Cap; + +typedef struct { + Elf64_Xword c_tag; + union { + Elf64_Xword c_val; + Elf64_Addr c_ptr; + } c_un; +} Elf64_Cap; + +/* SunOS 5.x hardware/software capability tags */ +#define CA_SUNW_NULL 0 +#define CA_SUNW_HW_1 1 +#define CA_SUNW_SF_1 2 + +/* SunOS 5.x software capabilities */ +#define SF1_SUNW_FPKNWN 0x01 +#define SF1_SUNW_FPUSED 0x02 +#define SF1_SUNW_MASK 0x03 + +/* SunOS 5.x hardware capabilities: sparc */ +#define AV_SPARC_MUL32 0x0001 +#define AV_SPARC_DIV32 0x0002 +#define AV_SPARC_FSMULD 0x0004 +#define AV_SPARC_V8PLUS 0x0008 +#define AV_SPARC_POPC 0x0010 +#define AV_SPARC_VIS 0x0020 +#define AV_SPARC_VIS2 0x0040 +#define AV_SPARC_ASI_BLK_INIT 0x0080 +#define AV_SPARC_FMAF 0x0100 +#define AV_SPARC_FJFMAU 0x4000 +#define AV_SPARC_IMA 0x8000 + +/* SunOS 5.x hardware capabilities: 386 */ +#define AV_386_FPU 0x00000001 +#define AV_386_TSC 0x00000002 +#define AV_386_CX8 0x00000004 +#define AV_386_SEP 0x00000008 +#define AV_386_AMD_SYSC 0x00000010 +#define AV_386_CMOV 0x00000020 +#define AV_386_MMX 0x00000040 +#define AV_386_AMD_MMX 0x00000080 +#define AV_386_AMD_3DNow 0x00000100 +#define AV_386_AMD_3DNowx 0x00000200 +#define AV_386_FXSR 0x00000400 +#define AV_386_SSE 0x00000800 +#define AV_386_SSE2 0x00001000 +#define AV_386_PAUSE 0x00002000 +#define AV_386_SSE3 0x00004000 +#define AV_386_MON 0x00008000 +#define AV_386_CX16 0x00010000 +#define AV_386_AHF 0x00020000 +#define AV_386_TSCP 0x00040000 +#define AV_386_AMD_SSE4A 0x00080000 +#define AV_386_POPCNT 0x00100000 +#define AV_386_AMD_LZCNT 0x00200000 +#define AV_386_SSSE3 0x00400000 +#define AV_386_SSE4_1 0x00800000 +#define AV_386_SSE4_2 0x01000000 + +#endif diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c new file mode 100644 index 0000000..f9c2836 --- /dev/null +++ b/ext/fileinfo/libmagic/softmagic.c @@ -0,0 +1,2119 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * softmagic - interpret variable magic from MAGIC + */ + +#include "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: softmagic.c,v 1.147 2011/11/05 15:44:22 rrt Exp $") +#endif /* lint */ + +#include "magic.h" +#include <string.h> +#include <ctype.h> +#include <stdlib.h> +#include <time.h> + +#ifndef PREG_OFFSET_CAPTURE +# define PREG_OFFSET_CAPTURE (1<<8) +#endif + + + +private int match(struct magic_set *, struct magic *, uint32_t, + const unsigned char *, size_t, int, int); +private int mget(struct magic_set *, const unsigned char *, + struct magic *, size_t, unsigned int, int); +private int magiccheck(struct magic_set *, struct magic *); +private int32_t mprint(struct magic_set *, struct magic *); +private int32_t moffset(struct magic_set *, struct magic *); +private void mdebug(uint32_t, const char *, size_t); +private int mcopy(struct magic_set *, union VALUETYPE *, int, int, + const unsigned char *, uint32_t, size_t, size_t); +private int mconvert(struct magic_set *, struct magic *); +private int print_sep(struct magic_set *, int); +private int handle_annotation(struct magic_set *, struct magic *); +private void cvt_8(union VALUETYPE *, const struct magic *); +private void cvt_16(union VALUETYPE *, const struct magic *); +private void cvt_32(union VALUETYPE *, const struct magic *); +private void cvt_64(union VALUETYPE *, const struct magic *); + +/* + * softmagic - lookup one file in parsed, in-memory copy of database + * Passed the name and FILE * of one file to be typed. + */ +/*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */ +protected int +file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, + int mode, int text) +{ + struct mlist *ml; + int rv; + for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next) + if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode, + text)) != 0) + return rv; + + return 0; +} + +/* + * Go through the whole list, stopping if you find a match. Process all + * the continuations of that match before returning. + * + * We support multi-level continuations: + * + * At any time when processing a successful top-level match, there is a + * current continuation level; it represents the level of the last + * successfully matched continuation. + * + * Continuations above that level are skipped as, if we see one, it + * means that the continuation that controls them - i.e, the + * lower-level continuation preceding them - failed to match. + * + * Continuations below that level are processed as, if we see one, + * it means we've finished processing or skipping higher-level + * continuations under the control of a successful or unsuccessful + * lower-level continuation, and are now seeing the next lower-level + * continuation and should process it. The current continuation + * level reverts to the level of the one we're seeing. + * + * Continuations at the current level are processed as, if we see + * one, there's no lower-level continuation that may have failed. + * + * If a continuation matches, we bump the current continuation level + * so that higher-level continuations are processed. + */ +private int +match(struct magic_set *ms, struct magic *magic, uint32_t nmagic, + const unsigned char *s, size_t nbytes, int mode, int text) +{ + uint32_t magindex = 0; + unsigned int cont_level = 0; + int need_separator = 0; + int returnval = 0, e; /* if a match is found it is set to 1*/ + int firstline = 1; /* a flag to print X\n X\n- X */ + int printed_something = 0; + int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0; + + if (file_check_mem(ms, cont_level) == -1) + return -1; + + for (magindex = 0; magindex < nmagic; magindex++) { + int flush = 0; + struct magic *m = &magic[magindex]; + + if ((IS_LIBMAGIC_STRING(m->type) && + ((text && (m->str_flags & (STRING_BINTEST | STRING_TEXTTEST)) == STRING_BINTEST) || + (!text && (m->str_flags & (STRING_TEXTTEST | STRING_BINTEST)) == STRING_TEXTTEST))) || + (m->flag & mode) != mode) { + /* Skip sub-tests */ + while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) { + magindex++; + } + continue; /* Skip to next top-level test*/ + } + + ms->offset = m->offset; + ms->line = m->lineno; + + /* if main entry matches, print it... */ + switch (mget(ms, s, m, nbytes, cont_level, text)) { + case -1: + return -1; + case 0: + flush = m->reln != '!'; + break; + default: + if (m->type == FILE_INDIRECT) + returnval = 1; + + switch (magiccheck(ms, m)) { + case -1: + return -1; + case 0: + flush++; + break; + default: + flush = 0; + break; + } + break; + } + if (flush) { + /* + * main entry didn't match, + * flush its continuations + */ + while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) { + magindex++; + } + continue; + } + + if ((e = handle_annotation(ms, m)) != 0) + return e; + /* + * If we are going to print something, we'll need to print + * a blank before we print something else. + */ + if (*m->desc) { + need_separator = 1; + printed_something = 1; + if (print_sep(ms, firstline) == -1) + return -1; + } + + + if (print && mprint(ms, m) == -1) + return -1; + + ms->c.li[cont_level].off = moffset(ms, m); + + /* and any continuations that match */ + if (file_check_mem(ms, ++cont_level) == -1) + return -1; + + while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) { + magindex++; + m = &magic[magindex]; + ms->line = m->lineno; /* for messages */ + + if (cont_level < m->cont_level) + continue; + if (cont_level > m->cont_level) { + /* + * We're at the end of the level + * "cont_level" continuations. + */ + cont_level = m->cont_level; + } + ms->offset = m->offset; + if (m->flag & OFFADD) { + ms->offset += ms->c.li[cont_level - 1].off; + } + +#ifdef ENABLE_CONDITIONALS + if (m->cond == COND_ELSE || + m->cond == COND_ELIF) { + if (ms->c.li[cont_level].last_match == 1) + continue; + } +#endif + switch (mget(ms, s, m, nbytes, cont_level, text)) { + case -1: + return -1; + case 0: + if (m->reln != '!') + continue; + flush = 1; + break; + default: + if (m->type == FILE_INDIRECT) + returnval = 1; + flush = 0; + break; + } + + switch (flush ? 1 : magiccheck(ms, m)) { + case -1: + return -1; + case 0: +#ifdef ENABLE_CONDITIONALS + ms->c.li[cont_level].last_match = 0; +#endif + break; + default: +#ifdef ENABLE_CONDITIONALS + ms->c.li[cont_level].last_match = 1; +#endif + if (m->type != FILE_DEFAULT) + ms->c.li[cont_level].got_match = 1; + else if (ms->c.li[cont_level].got_match) { + ms->c.li[cont_level].got_match = 0; + break; + } + if ((e = handle_annotation(ms, m)) != 0) + return e; + /* + * If we are going to print something, + * make sure that we have a separator first. + */ + if (*m->desc) { + if (!printed_something) { + printed_something = 1; + if (print_sep(ms, firstline) + == -1) + return -1; + } + } + /* + * This continuation matched. Print + * its message, with a blank before it + * if the previous item printed and + * this item isn't empty. + */ + /* space if previous printed */ + if (need_separator + && ((m->flag & NOSPACE) == 0) + && *m->desc) { + if (print && + file_printf(ms, " ") == -1) + return -1; + need_separator = 0; + } + if (print && mprint(ms, m) == -1) + return -1; + + ms->c.li[cont_level].off = moffset(ms, m); + + if (*m->desc) + need_separator = 1; + + /* + * If we see any continuations + * at a higher level, + * process them. + */ + if (file_check_mem(ms, ++cont_level) == -1) + return -1; + break; + } + } + if (printed_something) { + firstline = 0; + if (print) + returnval = 1; + } + if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) { + return returnval; /* don't keep searching */ + } + } + return returnval; /* This is hit if -k is set or there is no match */ +} + +private int +check_fmt(struct magic_set *ms, struct magic *m) +{ + pcre *pce; + int re_options; + pcre_extra *re_extra; + TSRMLS_FETCH(); + + if (strchr(m->desc, '%') == NULL) { + return 0; + } + + if ((pce = pcre_get_compiled_regex("~%[-0-9.]*s~", &re_extra, &re_options TSRMLS_CC)) == NULL) { + return -1; + } else { + return !pcre_exec(pce, re_extra, m->desc, strlen(m->desc), 0, re_options, NULL, 0); + } +} + +private int32_t +mprint(struct magic_set *ms, struct magic *m) +{ + uint64_t v; + float vf; + double vd; + int64_t t = 0; + char buf[128]; + union VALUETYPE *p = &ms->ms_value; + + switch (m->type) { + case FILE_BYTE: + v = file_signextend(ms, m, (uint64_t)p->b); + switch (check_fmt(ms, m)) { + case -1: + return -1; + case 1: + (void)snprintf(buf, sizeof(buf), "%c", + (unsigned char)v); + if (file_printf(ms, m->desc, buf) == -1) + return -1; + break; + default: + if (file_printf(ms, m->desc, (unsigned char) v) == -1) + return -1; + break; + } + t = ms->offset + sizeof(char); + break; + + case FILE_SHORT: + case FILE_BESHORT: + case FILE_LESHORT: + v = file_signextend(ms, m, (uint64_t)p->h); + switch (check_fmt(ms, m)) { + case -1: + return -1; + case 1: + (void)snprintf(buf, sizeof(buf), "%hu", + (unsigned short)v); + if (file_printf(ms, m->desc, buf) == -1) + return -1; + break; + default: + if ( + file_printf(ms, m->desc, (unsigned short) v) == -1) + return -1; + break; + } + t = ms->offset + sizeof(short); + break; + + case FILE_LONG: + case FILE_BELONG: + case FILE_LELONG: + case FILE_MELONG: + v = file_signextend(ms, m, (uint64_t)p->l); + switch (check_fmt(ms, m)) { + case -1: + return -1; + case 1: + (void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v); + if (file_printf(ms, m->desc, buf) == -1) + return -1; + break; + default: + if (file_printf(ms, m->desc, (uint32_t) v) == -1) + return -1; + break; + } + t = ms->offset + sizeof(int32_t); + break; + + case FILE_QUAD: + case FILE_BEQUAD: + case FILE_LEQUAD: + v = file_signextend(ms, m, p->q); + if (file_printf(ms, m->desc, (uint64_t) v) == -1) + return -1; + t = ms->offset + sizeof(int64_t); + break; + + case FILE_STRING: + case FILE_PSTRING: + case FILE_BESTRING16: + case FILE_LESTRING16: + if (m->reln == '=' || m->reln == '!') { + if (file_printf(ms, m->desc, m->value.s) == -1) + return -1; + t = ms->offset + m->vallen; + } + else { + if (*m->value.s == '\0') + p->s[strcspn(p->s, "\n")] = '\0'; + if (file_printf(ms, m->desc, p->s) == -1) + return -1; + t = ms->offset + strlen(p->s); + if (m->type == FILE_PSTRING) + t += file_pstring_length_size(m); + } + break; + + case FILE_DATE: + case FILE_BEDATE: + case FILE_LEDATE: + case FILE_MEDATE: + if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1) + return -1; + t = ms->offset + sizeof(time_t); + break; + + case FILE_LDATE: + case FILE_BELDATE: + case FILE_LELDATE: + case FILE_MELDATE: + if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1) + return -1; + t = ms->offset + sizeof(time_t); + break; + + case FILE_QDATE: + case FILE_BEQDATE: + case FILE_LEQDATE: + if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, + 1)) == -1) + return -1; + t = ms->offset + sizeof(uint64_t); + break; + + case FILE_QLDATE: + case FILE_BEQLDATE: + case FILE_LEQLDATE: + if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, + 0)) == -1) + return -1; + t = ms->offset + sizeof(uint64_t); + break; + + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + vf = p->f; + switch (check_fmt(ms, m)) { + case -1: + return -1; + case 1: + (void)snprintf(buf, sizeof(buf), "%g", vf); + if (file_printf(ms, m->desc, buf) == -1) + return -1; + break; + default: + if (file_printf(ms, m->desc, vf) == -1) + return -1; + break; + } + t = ms->offset + sizeof(float); + break; + + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + vd = p->d; + switch (check_fmt(ms, m)) { + case -1: + return -1; + case 1: + (void)snprintf(buf, sizeof(buf), "%g", vd); + if (file_printf(ms, m->desc, buf) == -1) + return -1; + break; + default: + if (file_printf(ms, m->desc, vd) == -1) + return -1; + break; + } + t = ms->offset + sizeof(double); + break; + + case FILE_REGEX: { + char *cp; + int rval; + + cp = estrndup((const char *)ms->search.s, ms->search.rm_len); + + rval = file_printf(ms, m->desc, cp); + efree(cp); + + if (rval == -1) + return -1; + + if ((m->str_flags & REGEX_OFFSET_START)) + t = ms->search.offset; + else + t = ms->search.offset + ms->search.rm_len; + break; + } + + case FILE_SEARCH: + if (file_printf(ms, m->desc, m->value.s) == -1) + return -1; + if ((m->str_flags & REGEX_OFFSET_START)) + t = ms->search.offset; + else + t = ms->search.offset + m->vallen; + break; + + case FILE_DEFAULT: + if (file_printf(ms, m->desc, m->value.s) == -1) + return -1; + t = ms->offset; + break; + + case FILE_INDIRECT: + t = ms->offset; + break; + + default: + file_magerror(ms, "invalid m->type (%d) in mprint()", m->type); + return -1; + } + return (int32_t)t; +} + +private int32_t +moffset(struct magic_set *ms, struct magic *m) +{ + switch (m->type) { + case FILE_BYTE: + return CAST(int32_t, (ms->offset + sizeof(char))); + + case FILE_SHORT: + case FILE_BESHORT: + case FILE_LESHORT: + return CAST(int32_t, (ms->offset + sizeof(short))); + + case FILE_LONG: + case FILE_BELONG: + case FILE_LELONG: + case FILE_MELONG: + return CAST(int32_t, (ms->offset + sizeof(int32_t))); + + case FILE_QUAD: + case FILE_BEQUAD: + case FILE_LEQUAD: + return CAST(int32_t, (ms->offset + sizeof(int64_t))); + + case FILE_STRING: + case FILE_PSTRING: + case FILE_BESTRING16: + case FILE_LESTRING16: + if (m->reln == '=' || m->reln == '!') + return ms->offset + m->vallen; + else { + union VALUETYPE *p = &ms->ms_value; + uint32_t t; + + if (*m->value.s == '\0') + p->s[strcspn(p->s, "\n")] = '\0'; + t = CAST(uint32_t, (ms->offset + strlen(p->s))); + if (m->type == FILE_PSTRING) + t += file_pstring_length_size(m); + return t; + } + + case FILE_DATE: + case FILE_BEDATE: + case FILE_LEDATE: + case FILE_MEDATE: + return CAST(int32_t, (ms->offset + sizeof(time_t))); + + case FILE_LDATE: + case FILE_BELDATE: + case FILE_LELDATE: + case FILE_MELDATE: + return CAST(int32_t, (ms->offset + sizeof(time_t))); + + case FILE_QDATE: + case FILE_BEQDATE: + case FILE_LEQDATE: + return CAST(int32_t, (ms->offset + sizeof(uint64_t))); + + case FILE_QLDATE: + case FILE_BEQLDATE: + case FILE_LEQLDATE: + return CAST(int32_t, (ms->offset + sizeof(uint64_t))); + + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + return CAST(int32_t, (ms->offset + sizeof(float))); + + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + return CAST(int32_t, (ms->offset + sizeof(double))); + + case FILE_REGEX: + if ((m->str_flags & REGEX_OFFSET_START) != 0) + return CAST(int32_t, ms->search.offset); + else + return CAST(int32_t, (ms->search.offset + + ms->search.rm_len)); + + case FILE_SEARCH: + if ((m->str_flags & REGEX_OFFSET_START) != 0) + return CAST(int32_t, ms->search.offset); + else + return CAST(int32_t, (ms->search.offset + m->vallen)); + + case FILE_DEFAULT: + return ms->offset; + + case FILE_INDIRECT: + return ms->offset; + + default: + return 0; + } +} + +#define DO_CVT(fld, cast) \ + if (m->num_mask) \ + switch (m->mask_op & FILE_OPS_MASK) { \ + case FILE_OPAND: \ + p->fld &= cast m->num_mask; \ + break; \ + case FILE_OPOR: \ + p->fld |= cast m->num_mask; \ + break; \ + case FILE_OPXOR: \ + p->fld ^= cast m->num_mask; \ + break; \ + case FILE_OPADD: \ + p->fld += cast m->num_mask; \ + break; \ + case FILE_OPMINUS: \ + p->fld -= cast m->num_mask; \ + break; \ + case FILE_OPMULTIPLY: \ + p->fld *= cast m->num_mask; \ + break; \ + case FILE_OPDIVIDE: \ + p->fld /= cast m->num_mask; \ + break; \ + case FILE_OPMODULO: \ + p->fld %= cast m->num_mask; \ + break; \ + } \ + if (m->mask_op & FILE_OPINVERSE) \ + p->fld = ~p->fld \ + +private void +cvt_8(union VALUETYPE *p, const struct magic *m) +{ + DO_CVT(b, (uint8_t)); +} + +private void +cvt_16(union VALUETYPE *p, const struct magic *m) +{ + DO_CVT(h, (uint16_t)); +} + +private void +cvt_32(union VALUETYPE *p, const struct magic *m) +{ + DO_CVT(l, (uint32_t)); +} + +private void +cvt_64(union VALUETYPE *p, const struct magic *m) +{ + DO_CVT(q, (uint64_t)); +} + +#define DO_CVT2(fld, cast) \ + if (m->num_mask) \ + switch (m->mask_op & FILE_OPS_MASK) { \ + case FILE_OPADD: \ + p->fld += cast (int64_t)m->num_mask; \ + break; \ + case FILE_OPMINUS: \ + p->fld -= cast (int64_t)m->num_mask; \ + break; \ + case FILE_OPMULTIPLY: \ + p->fld *= cast (int64_t)m->num_mask; \ + break; \ + case FILE_OPDIVIDE: \ + p->fld /= cast (int64_t)m->num_mask; \ + break; \ + } \ + +private void +cvt_float(union VALUETYPE *p, const struct magic *m) +{ + DO_CVT2(f, (float)); +} + +private void +cvt_double(union VALUETYPE *p, const struct magic *m) +{ + DO_CVT2(d, (double)); +} + +/* + * Convert the byte order of the data we are looking at + * While we're here, let's apply the mask operation + * (unless you have a better idea) + */ +private int +mconvert(struct magic_set *ms, struct magic *m) +{ + union VALUETYPE *p = &ms->ms_value; + + switch (m->type) { + case FILE_BYTE: + cvt_8(p, m); + return 1; + case FILE_SHORT: + cvt_16(p, m); + return 1; + case FILE_LONG: + case FILE_DATE: + case FILE_LDATE: + cvt_32(p, m); + return 1; + case FILE_QUAD: + case FILE_QDATE: + case FILE_QLDATE: + cvt_64(p, m); + return 1; + case FILE_STRING: + case FILE_BESTRING16: + case FILE_LESTRING16: { + /* Null terminate and eat *trailing* return */ + p->s[sizeof(p->s) - 1] = '\0'; + return 1; + } + case FILE_PSTRING: { + char *ptr1 = p->s, *ptr2 = ptr1 + file_pstring_length_size(m); + size_t len = file_pstring_get_length(m, ptr1); + if (len >= sizeof(p->s)) + len = sizeof(p->s) - 1; + while (len--) + *ptr1++ = *ptr2++; + *ptr1 = '\0'; + return 1; + } + case FILE_BESHORT: + p->h = (short)((p->hs[0]<<8)|(p->hs[1])); + cvt_16(p, m); + return 1; + case FILE_BELONG: + case FILE_BEDATE: + case FILE_BELDATE: + p->l = (int32_t) + ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3])); + cvt_32(p, m); + return 1; + case FILE_BEQUAD: + case FILE_BEQDATE: + case FILE_BEQLDATE: + p->q = (uint64_t) + (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)| + ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)| + ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)| + ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7])); + cvt_64(p, m); + return 1; + case FILE_LESHORT: + p->h = (short)((p->hs[1]<<8)|(p->hs[0])); + cvt_16(p, m); + return 1; + case FILE_LELONG: + case FILE_LEDATE: + case FILE_LELDATE: + p->l = (int32_t) + ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0])); + cvt_32(p, m); + return 1; + case FILE_LEQUAD: + case FILE_LEQDATE: + case FILE_LEQLDATE: + p->q = (uint64_t) + (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)| + ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)| + ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)| + ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0])); + cvt_64(p, m); + return 1; + case FILE_MELONG: + case FILE_MEDATE: + case FILE_MELDATE: + p->l = (int32_t) + ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2])); + cvt_32(p, m); + return 1; + case FILE_FLOAT: + cvt_float(p, m); + return 1; + case FILE_BEFLOAT: + p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)| + ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]); + cvt_float(p, m); + return 1; + case FILE_LEFLOAT: + p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)| + ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]); + cvt_float(p, m); + return 1; + case FILE_DOUBLE: + cvt_double(p, m); + return 1; + case FILE_BEDOUBLE: + p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)| + ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)| + ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)| + ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]); + cvt_double(p, m); + return 1; + case FILE_LEDOUBLE: + p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)| + ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)| + ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)| + ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]); + cvt_double(p, m); + return 1; + case FILE_REGEX: + case FILE_SEARCH: + case FILE_DEFAULT: + return 1; + default: + file_magerror(ms, "invalid type %d in mconvert()", m->type); + return 0; + } +} + + +private void +mdebug(uint32_t offset, const char *str, size_t len) +{ + (void) fprintf(stderr, "mget @%d: ", offset); + file_showstr(stderr, str, len); + (void) fputc('\n', stderr); + (void) fputc('\n', stderr); +} + +private int +mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir, + const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt) +{ + /* + * Note: FILE_SEARCH and FILE_REGEX do not actually copy + * anything, but setup pointers into the source + */ + if (indir == 0) { + switch (type) { + case FILE_SEARCH: + ms->search.s = RCAST(const char *, s) + offset; + ms->search.s_len = nbytes - offset; + ms->search.offset = offset; + return 0; + + case FILE_REGEX: { + const char *b; + const char *c; + const char *last; /* end of search region */ + const char *buf; /* start of search region */ + const char *end; + size_t lines; + + if (s == NULL) { + ms->search.s_len = 0; + ms->search.s = NULL; + return 0; + } + buf = RCAST(const char *, s) + offset; + end = last = RCAST(const char *, s) + nbytes; + /* mget() guarantees buf <= last */ + for (lines = linecnt, b = buf; lines && b < end && + ((b = CAST(const char *, + memchr(c = b, '\n', CAST(size_t, (end - b))))) + || (b = CAST(const char *, + memchr(c, '\r', CAST(size_t, (end - c)))))); + lines--, b++) { + last = b; + if (b[0] == '\r' && b[1] == '\n') + b++; + } + if (lines) + last = RCAST(const char *, s) + nbytes; + + ms->search.s = buf; + ms->search.s_len = last - buf; + ms->search.offset = offset; + ms->search.rm_len = 0; + return 0; + } + case FILE_BESTRING16: + case FILE_LESTRING16: { + const unsigned char *src = s + offset; + const unsigned char *esrc = s + nbytes; + char *dst = p->s; + char *edst = &p->s[sizeof(p->s) - 1]; + + if (type == FILE_BESTRING16) + src++; + + /* check for pointer overflow */ + if (src < s) { + file_magerror(ms, "invalid offset %u in mcopy()", + offset); + return -1; + } + for (/*EMPTY*/; src < esrc; src += 2, dst++) { + if (dst < edst) + *dst = *src; + else + break; + if (*dst == '\0') { + if (type == FILE_BESTRING16 ? + *(src - 1) != '\0' : + *(src + 1) != '\0') + *dst = ' '; + } + } + *edst = '\0'; + return 0; + } + case FILE_STRING: /* XXX - these two should not need */ + case FILE_PSTRING: /* to copy anything, but do anyway. */ + default: + break; + } + } + + if (offset >= nbytes) { + (void)memset(p, '\0', sizeof(*p)); + return 0; + } + if (nbytes - offset < sizeof(*p)) + nbytes = nbytes - offset; + else + nbytes = sizeof(*p); + + (void)memcpy(p, s + offset, nbytes); + + /* + * the usefulness of padding with zeroes eludes me, it + * might even cause problems + */ + if (nbytes < sizeof(*p)) + (void)memset(((char *)(void *)p) + nbytes, '\0', + sizeof(*p) - nbytes); + return 0; +} + +private int +mget(struct magic_set *ms, const unsigned char *s, + struct magic *m, size_t nbytes, unsigned int cont_level, int text) +{ + uint32_t offset = ms->offset; + uint32_t count = m->str_range; + union VALUETYPE *p = &ms->ms_value; + + if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1) + return -1; + + if ((ms->flags & MAGIC_DEBUG) != 0) { + mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE)); + } + + if (m->flag & INDIR) { + int off = m->in_offset; + if (m->in_op & FILE_OPINDIRECT) { + const union VALUETYPE *q = + ((const void *)(s + offset + off)); + switch (m->in_type) { + case FILE_BYTE: + off = q->b; + break; + case FILE_SHORT: + off = q->h; + break; + case FILE_BESHORT: + off = (short)((q->hs[0]<<8)|(q->hs[1])); + break; + case FILE_LESHORT: + off = (short)((q->hs[1]<<8)|(q->hs[0])); + break; + case FILE_LONG: + off = q->l; + break; + case FILE_BELONG: + case FILE_BEID3: + off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)| + (q->hl[2]<<8)|(q->hl[3])); + break; + case FILE_LEID3: + case FILE_LELONG: + off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)| + (q->hl[1]<<8)|(q->hl[0])); + break; + case FILE_MELONG: + off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)| + (q->hl[3]<<8)|(q->hl[2])); + break; + } + } + switch (m->in_type) { + case FILE_BYTE: + if (nbytes < (offset + 1)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = p->b & off; + break; + case FILE_OPOR: + offset = p->b | off; + break; + case FILE_OPXOR: + offset = p->b ^ off; + break; + case FILE_OPADD: + offset = p->b + off; + break; + case FILE_OPMINUS: + offset = p->b - off; + break; + case FILE_OPMULTIPLY: + offset = p->b * off; + break; + case FILE_OPDIVIDE: + offset = p->b / off; + break; + case FILE_OPMODULO: + offset = p->b % off; + break; + } + } else + offset = p->b; + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + case FILE_BESHORT: + if (nbytes < (offset + 2)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) & + off; + break; + case FILE_OPOR: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) | + off; + break; + case FILE_OPXOR: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) ^ + off; + break; + case FILE_OPADD: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) + + off; + break; + case FILE_OPMINUS: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) - + off; + break; + case FILE_OPMULTIPLY: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) * + off; + break; + case FILE_OPDIVIDE: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) / + off; + break; + case FILE_OPMODULO: + offset = (short)((p->hs[0]<<8)| + (p->hs[1])) % + off; + break; + } + } else + offset = (short)((p->hs[0]<<8)| + (p->hs[1])); + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + case FILE_LESHORT: + if (nbytes < (offset + 2)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) & + off; + break; + case FILE_OPOR: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) | + off; + break; + case FILE_OPXOR: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) ^ + off; + break; + case FILE_OPADD: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) + + off; + break; + case FILE_OPMINUS: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) - + off; + break; + case FILE_OPMULTIPLY: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) * + off; + break; + case FILE_OPDIVIDE: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) / + off; + break; + case FILE_OPMODULO: + offset = (short)((p->hs[1]<<8)| + (p->hs[0])) % + off; + break; + } + } else + offset = (short)((p->hs[1]<<8)| + (p->hs[0])); + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + case FILE_SHORT: + if (nbytes < (offset + 2)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = p->h & off; + break; + case FILE_OPOR: + offset = p->h | off; + break; + case FILE_OPXOR: + offset = p->h ^ off; + break; + case FILE_OPADD: + offset = p->h + off; + break; + case FILE_OPMINUS: + offset = p->h - off; + break; + case FILE_OPMULTIPLY: + offset = p->h * off; + break; + case FILE_OPDIVIDE: + offset = p->h / off; + break; + case FILE_OPMODULO: + offset = p->h % off; + break; + } + } + else + offset = p->h; + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + case FILE_BELONG: + case FILE_BEID3: + if (nbytes < (offset + 4)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) & + off; + break; + case FILE_OPOR: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) | + off; + break; + case FILE_OPXOR: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) ^ + off; + break; + case FILE_OPADD: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) + + off; + break; + case FILE_OPMINUS: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) - + off; + break; + case FILE_OPMULTIPLY: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) * + off; + break; + case FILE_OPDIVIDE: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) / + off; + break; + case FILE_OPMODULO: + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])) % + off; + break; + } + } else + offset = (int32_t)((p->hl[0]<<24)| + (p->hl[1]<<16)| + (p->hl[2]<<8)| + (p->hl[3])); + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + case FILE_LELONG: + case FILE_LEID3: + if (nbytes < (offset + 4)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) & + off; + break; + case FILE_OPOR: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) | + off; + break; + case FILE_OPXOR: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) ^ + off; + break; + case FILE_OPADD: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) + + off; + break; + case FILE_OPMINUS: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) - + off; + break; + case FILE_OPMULTIPLY: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) * + off; + break; + case FILE_OPDIVIDE: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) / + off; + break; + case FILE_OPMODULO: + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])) % + off; + break; + } + } else + offset = (int32_t)((p->hl[3]<<24)| + (p->hl[2]<<16)| + (p->hl[1]<<8)| + (p->hl[0])); + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + case FILE_MELONG: + if (nbytes < (offset + 4)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) & + off; + break; + case FILE_OPOR: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) | + off; + break; + case FILE_OPXOR: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) ^ + off; + break; + case FILE_OPADD: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) + + off; + break; + case FILE_OPMINUS: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) - + off; + break; + case FILE_OPMULTIPLY: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) * + off; + break; + case FILE_OPDIVIDE: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) / + off; + break; + case FILE_OPMODULO: + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])) % + off; + break; + } + } else + offset = (int32_t)((p->hl[1]<<24)| + (p->hl[0]<<16)| + (p->hl[3]<<8)| + (p->hl[2])); + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + case FILE_LONG: + if (nbytes < (offset + 4)) + return 0; + if (off) { + switch (m->in_op & FILE_OPS_MASK) { + case FILE_OPAND: + offset = p->l & off; + break; + case FILE_OPOR: + offset = p->l | off; + break; + case FILE_OPXOR: + offset = p->l ^ off; + break; + case FILE_OPADD: + offset = p->l + off; + break; + case FILE_OPMINUS: + offset = p->l - off; + break; + case FILE_OPMULTIPLY: + offset = p->l * off; + break; + case FILE_OPDIVIDE: + offset = p->l / off; + break; + case FILE_OPMODULO: + offset = p->l % off; + break; + } + } else + offset = p->l; + if (m->in_op & FILE_OPINVERSE) + offset = ~offset; + break; + } + + switch (m->in_type) { + case FILE_LEID3: + case FILE_BEID3: + offset = ((((offset >> 0) & 0x7f) << 0) | + (((offset >> 8) & 0x7f) << 7) | + (((offset >> 16) & 0x7f) << 14) | + (((offset >> 24) & 0x7f) << 21)) + 10; + break; + default: + break; + } + + if (m->flag & INDIROFFADD) { + offset += ms->c.li[cont_level-1].off; + } + if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1) + return -1; + ms->offset = offset; + + if ((ms->flags & MAGIC_DEBUG) != 0) { + mdebug(offset, (char *)(void *)p, + sizeof(union VALUETYPE)); + } + } + + /* Verify we have enough data to match magic type */ + switch (m->type) { + case FILE_BYTE: + if (nbytes < (offset + 1)) /* should alway be true */ + return 0; + break; + + case FILE_SHORT: + case FILE_BESHORT: + case FILE_LESHORT: + if (nbytes < (offset + 2)) + return 0; + break; + + case FILE_LONG: + case FILE_BELONG: + case FILE_LELONG: + case FILE_MELONG: + case FILE_DATE: + case FILE_BEDATE: + case FILE_LEDATE: + case FILE_MEDATE: + case FILE_LDATE: + case FILE_BELDATE: + case FILE_LELDATE: + case FILE_MELDATE: + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + if (nbytes < (offset + 4)) + return 0; + break; + + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + if (nbytes < (offset + 8)) + return 0; + break; + + case FILE_STRING: + case FILE_PSTRING: + case FILE_SEARCH: + if (nbytes < (offset + m->vallen)) + return 0; + break; + + case FILE_REGEX: + if (nbytes < offset) + return 0; + break; + + case FILE_INDIRECT: + if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 && + file_printf(ms, "%s", m->desc) == -1) + return -1; + if (nbytes < offset) + return 0; + return file_softmagic(ms, s + offset, nbytes - offset, + BINTEST, text); + + case FILE_DEFAULT: /* nothing to check */ + default: + break; + } + if (!mconvert(ms, m)) + return 0; + return 1; +} + +private uint64_t +file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags) +{ + /* + * Convert the source args to unsigned here so that (1) the + * compare will be unsigned as it is in strncmp() and (2) so + * the ctype functions will work correctly without extra + * casting. + */ + const unsigned char *a = (const unsigned char *)s1; + const unsigned char *b = (const unsigned char *)s2; + uint64_t v; + + /* + * What we want here is v = strncmp(s1, s2, len), + * but ignoring any nulls. + */ + v = 0; + if (0L == flags) { /* normal string: do it fast */ + while (len-- > 0) + if ((v = *b++ - *a++) != '\0') + break; + } + else { /* combine the others */ + while (len-- > 0) { + if ((flags & STRING_IGNORE_LOWERCASE) && + islower(*a)) { + if ((v = tolower(*b++) - *a++) != '\0') + break; + } + else if ((flags & STRING_IGNORE_UPPERCASE) && + isupper(*a)) { + if ((v = toupper(*b++) - *a++) != '\0') + break; + } + else if ((flags & STRING_COMPACT_WHITESPACE) && + isspace(*a)) { + a++; + if (isspace(*b++)) { + if (!isspace(*a)) + while (isspace(*b)) + b++; + } + else { + v = 1; + break; + } + } + else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) && + isspace(*a)) { + a++; + while (isspace(*b)) + b++; + } + else { + if ((v = *b++ - *a++) != '\0') + break; + } + } + } + return v; +} + +private uint64_t +file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags) +{ + /* + * XXX - The 16-bit string compare probably needs to be done + * differently, especially if the flags are to be supported. + * At the moment, I am unsure. + */ + flags = 0; + return file_strncmp(a, b, len, flags); +} + +public void +convert_libmagic_pattern(zval *pattern, int options) +{ + int i, j=0; + char *t; + + t = (char *) safe_emalloc(Z_STRLEN_P(pattern), 2, 5); + + t[j++] = '~'; + + for (i=0; i<Z_STRLEN_P(pattern); i++, j++) { + switch (Z_STRVAL_P(pattern)[i]) { + case '~': + t[j++] = '\\'; + t[j] = '~'; + break; + default: + t[j] = Z_STRVAL_P(pattern)[i]; + break; + } + } + t[j++] = '~'; + + if (options & PCRE_CASELESS) + t[j++] = 'i'; + + if (options & PCRE_MULTILINE) + t[j++] = 'm'; + + t[j]='\0'; + + Z_STRVAL_P(pattern) = t; + Z_STRLEN_P(pattern) = j; + +} + +private int +magiccheck(struct magic_set *ms, struct magic *m) +{ + uint64_t l = m->value.q; + uint64_t v; + float fl, fv; + double dl, dv; + int matched; + union VALUETYPE *p = &ms->ms_value; + + switch (m->type) { + case FILE_BYTE: + v = p->b; + break; + + case FILE_SHORT: + case FILE_BESHORT: + case FILE_LESHORT: + v = p->h; + break; + + case FILE_LONG: + case FILE_BELONG: + case FILE_LELONG: + case FILE_MELONG: + case FILE_DATE: + case FILE_BEDATE: + case FILE_LEDATE: + case FILE_MEDATE: + case FILE_LDATE: + case FILE_BELDATE: + case FILE_LELDATE: + case FILE_MELDATE: + v = p->l; + break; + + case FILE_QUAD: + case FILE_LEQUAD: + case FILE_BEQUAD: + case FILE_QDATE: + case FILE_BEQDATE: + case FILE_LEQDATE: + case FILE_QLDATE: + case FILE_BEQLDATE: + case FILE_LEQLDATE: + v = p->q; + break; + + case FILE_FLOAT: + case FILE_BEFLOAT: + case FILE_LEFLOAT: + fl = m->value.f; + fv = p->f; + switch (m->reln) { + case 'x': + matched = 1; + break; + + case '!': + matched = fv != fl; + break; + + case '=': + matched = fv == fl; + break; + + case '>': + matched = fv > fl; + break; + + case '<': + matched = fv < fl; + break; + + default: + matched = 0; + file_magerror(ms, "cannot happen with float: invalid relation `%c'", + m->reln); + return -1; + } + return matched; + + case FILE_DOUBLE: + case FILE_BEDOUBLE: + case FILE_LEDOUBLE: + dl = m->value.d; + dv = p->d; + switch (m->reln) { + case 'x': + matched = 1; + break; + + case '!': + matched = dv != dl; + break; + + case '=': + matched = dv == dl; + break; + + case '>': + matched = dv > dl; + break; + + case '<': + matched = dv < dl; + break; + + default: + matched = 0; + file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln); + return -1; + } + return matched; + + case FILE_DEFAULT: + l = 0; + v = 0; + break; + + case FILE_STRING: + case FILE_PSTRING: + l = 0; + v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags); + break; + + case FILE_BESTRING16: + case FILE_LESTRING16: + l = 0; + v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags); + break; + + case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */ + size_t slen; + size_t idx; + + if (ms->search.s == NULL) + return 0; + + slen = MIN(m->vallen, sizeof(m->value.s)); + l = 0; + v = 0; + + for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) { + if (slen + idx > ms->search.s_len) + break; + + v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags); + if (v == 0) { /* found match */ + ms->search.offset += idx; + break; + } + } + break; + } + case FILE_REGEX: { + zval *pattern; + int options = 0; + pcre_cache_entry *pce; + TSRMLS_FETCH(); + + MAKE_STD_ZVAL(pattern); + ZVAL_STRINGL(pattern, (char *)m->value.s, m->vallen, 0); + + options |= PCRE_MULTILINE; + + if (m->str_flags & STRING_IGNORE_CASE) { + options |= PCRE_CASELESS; + } + + convert_libmagic_pattern(pattern, options); + + l = v = 0; +#if (PHP_MAJOR_VERSION < 6) + if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) { +#else + if ((pce = pcre_get_compiled_regex_cache(IS_STRING, Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) { +#endif + zval_dtor(pattern); + FREE_ZVAL(pattern); + return -1; + } else { + /* pce now contains the compiled regex */ + zval *retval; + zval *subpats; + char *haystack; + + MAKE_STD_ZVAL(retval); + ALLOC_INIT_ZVAL(subpats); + + /* Cut the search len from haystack, equals to REG_STARTEND */ + haystack = estrndup(ms->search.s, ms->search.s_len); + + /* match v = 0, no match v = 1 */ +#if (PHP_MAJOR_VERSION < 6) + php_pcre_match_impl(pce, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC); +#else + php_pcre_match_impl(pce, IS_STRING, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC); +#endif + /* Free haystack */ + efree(haystack); + + if (Z_LVAL_P(retval) < 0) { + zval_ptr_dtor(&subpats); + FREE_ZVAL(retval); + zval_dtor(pattern); + FREE_ZVAL(pattern); + return -1; + } else if ((Z_LVAL_P(retval) > 0) && (Z_TYPE_P(subpats) == IS_ARRAY)) { + + /* Need to fetch global match which equals pmatch[0] */ + HashTable *ht = Z_ARRVAL_P(subpats); + HashPosition outer_pos; + zval *pattern_match = NULL, *pattern_offset = NULL; + + zend_hash_internal_pointer_reset_ex(ht, &outer_pos); + + if (zend_hash_has_more_elements_ex(ht, &outer_pos) == SUCCESS && + zend_hash_move_forward_ex(ht, &outer_pos)) { + + zval **ppzval; + + /* The first element (should be) is the global match + Need to move to the inner array to get the global match */ + + if (zend_hash_get_current_data_ex(ht, (void**)&ppzval, &outer_pos) != FAILURE) { + + HashTable *inner_ht; + HashPosition inner_pos; + zval **match, **offset; + zval tmpcopy = **ppzval, matchcopy, offsetcopy; + + zval_copy_ctor(&tmpcopy); + INIT_PZVAL(&tmpcopy); + + inner_ht = Z_ARRVAL(tmpcopy); + + /* If everything goes according to the master plan + tmpcopy now contains two elements: + 0 = the match + 1 = starting position of the match */ + zend_hash_internal_pointer_reset_ex(inner_ht, &inner_pos); + + if (zend_hash_has_more_elements_ex(inner_ht, &inner_pos) == SUCCESS && + zend_hash_move_forward_ex(inner_ht, &inner_pos)) { + + if (zend_hash_get_current_data_ex(inner_ht, (void**)&match, &inner_pos) != FAILURE) { + + matchcopy = **match; + zval_copy_ctor(&matchcopy); + INIT_PZVAL(&matchcopy); + convert_to_string(&matchcopy); + + MAKE_STD_ZVAL(pattern_match); + Z_STRVAL_P(pattern_match) = (char *)Z_STRVAL(matchcopy); + Z_STRLEN_P(pattern_match) = Z_STRLEN(matchcopy); + Z_TYPE_P(pattern_match) = IS_STRING; + + zval_dtor(&matchcopy); + } + } + + if (zend_hash_has_more_elements_ex(inner_ht, &inner_pos) == SUCCESS && + zend_hash_move_forward_ex(inner_ht, &inner_pos)) { + + if (zend_hash_get_current_data_ex(inner_ht, (void**)&offset, &inner_pos) != FAILURE) { + + offsetcopy = **offset; + zval_copy_ctor(&offsetcopy); + INIT_PZVAL(&offsetcopy); + convert_to_long(&offsetcopy); + + MAKE_STD_ZVAL(pattern_offset); + Z_LVAL_P(pattern_offset) = Z_LVAL(offsetcopy); + Z_TYPE_P(pattern_offset) = IS_LONG; + + zval_dtor(&offsetcopy); + } + } + zval_dtor(&tmpcopy); + } + + if ((pattern_match != NULL) && (pattern_offset != NULL)) { + ms->search.s += (int)Z_LVAL_P(pattern_offset); /* this is where the match starts */ + ms->search.offset += (size_t)Z_LVAL_P(pattern_offset); /* this is where the match starts as size_t */ + ms->search.rm_len = Z_STRLEN_P(pattern_match) /* This is the length of the matched pattern */; + v = 0; + + efree(pattern_match); + efree(pattern_offset); + + } else { + zval_ptr_dtor(&subpats); + FREE_ZVAL(retval); + zval_dtor(pattern); + FREE_ZVAL(pattern); + return -1; + } + } + + + } else { + v = 1; + } + zval_ptr_dtor(&subpats); + FREE_ZVAL(retval); + } + zval_dtor(pattern); + FREE_ZVAL(pattern); + break; + } + case FILE_INDIRECT: + return 1; + default: + file_magerror(ms, "invalid type %d in magiccheck()", m->type); + return -1; + } + + v = file_signextend(ms, m, v); + + switch (m->reln) { + case 'x': + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "%" INT64_T_FORMAT + "u == *any* = 1\n", (unsigned long long)v); + matched = 1; + break; + + case '!': + matched = v != l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %" + INT64_T_FORMAT "u = %d\n", (unsigned long long)v, + (unsigned long long)l, matched); + break; + + case '=': + matched = v == l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %" + INT64_T_FORMAT "u = %d\n", (unsigned long long)v, + (unsigned long long)l, matched); + break; + + case '>': + if (m->flag & UNSIGNED) { + matched = v > l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "%" INT64_T_FORMAT + "u > %" INT64_T_FORMAT "u = %d\n", + (unsigned long long)v, + (unsigned long long)l, matched); + } + else { + matched = (int64_t) v > (int64_t) l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "%" INT64_T_FORMAT + "d > %" INT64_T_FORMAT "d = %d\n", + (long long)v, (long long)l, matched); + } + break; + + case '<': + if (m->flag & UNSIGNED) { + matched = v < l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "%" INT64_T_FORMAT + "u < %" INT64_T_FORMAT "u = %d\n", + (unsigned long long)v, + (unsigned long long)l, matched); + } + else { + matched = (int64_t) v < (int64_t) l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "%" INT64_T_FORMAT + "d < %" INT64_T_FORMAT "d = %d\n", + (long long)v, (long long)l, matched); + } + break; + + case '&': + matched = (v & l) == l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %" + INT64_T_FORMAT "x) == %" INT64_T_FORMAT + "x) = %d\n", (unsigned long long)v, + (unsigned long long)l, (unsigned long long)l, + matched); + break; + + case '^': + matched = (v & l) != l; + if ((ms->flags & MAGIC_DEBUG) != 0) + (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %" + INT64_T_FORMAT "x) != %" INT64_T_FORMAT + "x) = %d\n", (unsigned long long)v, + (unsigned long long)l, (unsigned long long)l, + matched); + break; + + default: + matched = 0; + file_magerror(ms, "cannot happen: invalid relation `%c'", + m->reln); + return -1; + } + + return matched; +} + +private int +handle_annotation(struct magic_set *ms, struct magic *m) +{ + if (ms->flags & MAGIC_APPLE) { + if (file_printf(ms, "%.8s", m->apple) == -1) + return -1; + return 1; + } + if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) { + if (file_printf(ms, "%s", m->mimetype) == -1) + return -1; + return 1; + } + return 0; +} + +private int +print_sep(struct magic_set *ms, int firstline) +{ + if (ms->flags & MAGIC_MIME) + return 0; + if (firstline) + return 0; + /* + * we found another match + * put a newline and '-' to do some simple formatting + */ + return file_printf(ms, "\n- "); +} diff --git a/ext/fileinfo/libmagic/tar.h b/ext/fileinfo/libmagic/tar.h new file mode 100644 index 0000000..854d455 --- /dev/null +++ b/ext/fileinfo/libmagic/tar.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) Ian F. Darwin 1986-1995. + * Software written by Ian F. Darwin and others; + * maintained 1995-present by Christos Zoulas and others. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * Header file for public domain tar (tape archive) program. + * + * @(#)tar.h 1.20 86/10/29 Public Domain. + * + * Created 25 August 1985 by John Gilmore, ihnp4!hoptoad!gnu. + * + * $File: tar.h,v 1.13 2010/11/30 14:58:53 rrt Exp $ # checkin only + */ + +/* + * Header block on tape. + * + * I'm going to use traditional DP naming conventions here. + * A "block" is a big chunk of stuff that we do I/O on. + * A "record" is a piece of info that we care about. + * Typically many "record"s fit into a "block". + */ +#define RECORDSIZE 512 +#define NAMSIZ 100 +#define TUNMLEN 32 +#define TGNMLEN 32 + +union record { + unsigned char charptr[RECORDSIZE]; + struct header { + char name[NAMSIZ]; + char mode[8]; + char uid[8]; + char gid[8]; + char size[12]; + char mtime[12]; + char chksum[8]; + char linkflag; + char linkname[NAMSIZ]; + char magic[8]; + char uname[TUNMLEN]; + char gname[TGNMLEN]; + char devmajor[8]; + char devminor[8]; + } header; +}; + +/* The magic field is filled with this if uname and gname are valid. */ +#define TMAGIC "ustar" /* 5 chars and a null */ +#define GNUTMAGIC "ustar " /* 7 chars and a null */ |