[Midnightbsd-cvs] src [11115] trunk/contrib/file: remove several files.
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Tue Jun 19 15:06:15 EDT 2018
Revision: 11115
http://svnweb.midnightbsd.org/src/?rev=11115
Author: laffer1
Date: 2018-06-19 15:06:14 -0400 (Tue, 19 Jun 2018)
Log Message:
-----------
remove several files.
Removed Paths:
-------------
trunk/contrib/file/.cvsignore
trunk/contrib/file/FREEBSD-upgrade
trunk/contrib/file/Header
trunk/contrib/file/LEGAL.NOTICE
trunk/contrib/file/Localstuff
trunk/contrib/file/Magdir/
trunk/contrib/file/apprentice.c
trunk/contrib/file/apptype.c
trunk/contrib/file/ascmagic.c
trunk/contrib/file/asctime_r.c
trunk/contrib/file/asprintf.c
trunk/contrib/file/cdf.c
trunk/contrib/file/cdf.h
trunk/contrib/file/cdf_time.c
trunk/contrib/file/compress.c
trunk/contrib/file/ctime_r.c
trunk/contrib/file/elfclass.h
trunk/contrib/file/encoding.c
trunk/contrib/file/file.c
trunk/contrib/file/file.h
trunk/contrib/file/file.man
trunk/contrib/file/file_opts.h
trunk/contrib/file/fmtcheck.c
trunk/contrib/file/fsmagic.c
trunk/contrib/file/funcs.c
trunk/contrib/file/getline.c
trunk/contrib/file/getopt_long.c
trunk/contrib/file/is_tar.c
trunk/contrib/file/libmagic.man
Deleted: trunk/contrib/file/.cvsignore
===================================================================
--- trunk/contrib/file/.cvsignore 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/.cvsignore 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1 +0,0 @@
-autom4te.cache
Deleted: trunk/contrib/file/FREEBSD-upgrade
===================================================================
--- trunk/contrib/file/FREEBSD-upgrade 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/FREEBSD-upgrade 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,30 +0,0 @@
-# ex:ts=8
-$FreeBSD: src/contrib/file/FREEBSD-upgrade,v 1.7.4.1 2008/02/06 18:26:35 obrien Exp $
-
-Christos Zoulas `file'
- originals can be found at: ftp://ftp.astron.com/pub/file/
-
-Imported by:
-
- mv magic/* .
- rmdir magic
- mv -i src/* .
- rm -rf src
- mv -i doc/* .
- rm -rf doc
- rm -rf python
- rm -f lt*
- rm -f missing depcomp
- rm -f config.{guess,sub}
-
- cvs import -m "Virgin import of Christos Zoulas's FILE 4.23." \
- src/contrib/file ZOULAS file_4_23
-
-
-Never make local changes to ZOULAS `file'. Christos is very willing to
-work with us to meet our FreeBSD needs. Thus submit any desired changes
-to him <christos at zoulas.com> and wait for the next release and vendor
-import to get them.
-
-obrien at NUXI.com
-15-Sept-2002
Deleted: trunk/contrib/file/Header
===================================================================
--- trunk/contrib/file/Header 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/Header 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,5 +0,0 @@
-# Magic data for file(1) command.
-# Format is described in magic(files), where:
-# files is 5 on V7 and BSD, 4 on SV, and ?? on SVID.
-# Don't edit this file, edit /etc/magic or send your magic improvements
-# to the maintainers, at file at mx.gw.com
Deleted: trunk/contrib/file/LEGAL.NOTICE
===================================================================
--- trunk/contrib/file/LEGAL.NOTICE 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/LEGAL.NOTICE 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,29 +0,0 @@
-$File: LEGAL.NOTICE,v 1.15 2006/05/03 18:48:33 christos Exp $
-Copyright (c) Ian F. Darwin 1986, 1987, 1989, 1990, 1991, 1992, 1994, 1995.
-Software written by Ian F. Darwin and others;
-maintained 1994- Christos Zoulas.
-
-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.
Deleted: trunk/contrib/file/Localstuff
===================================================================
--- trunk/contrib/file/Localstuff 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/Localstuff 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,7 +0,0 @@
-
-#------------------------------------------------------------------------------
-# Localstuff: file(1) magic for locally observed files
-#
-# $File: Localstuff,v 1.4 2003/03/23 04:17:27 christos Exp $
-# Add any locally observed files here. Remember:
-# text if readable, executable if runnable binary, data if unreadable.
Deleted: trunk/contrib/file/apprentice.c
===================================================================
--- trunk/contrib/file/apprentice.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/apprentice.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,3078 +0,0 @@
-/*
- * 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 "file.h"
-
-#ifndef lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.211 2014/06/03 19:01:34 christos Exp $")
-#endif /* lint */
-
-#include "magic.h"
-#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#ifdef HAVE_STDDEF_H
-#include <stddef.h>
-#endif
-#include <string.h>
-#include <assert.h>
-#include <ctype.h>
-#include <fcntl.h>
-#ifdef QUICK
-#include <sys/mman.h>
-#endif
-#include <dirent.h>
-#if defined(HAVE_LIMITS_H)
-#include <limits.h>
-#endif
-
-#ifndef SSIZE_MAX
-#define MAXMAGIC_SIZE ((ssize_t)0x7fffffff)
-#else
-#define MAXMAGIC_SIZE SSIZE_MAX
-#endif
-
-#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
-
-#define ALLOC_CHUNK (size_t)10
-#define ALLOC_INCR (size_t)200
-
-struct magic_entry {
- struct magic *mp;
- uint32_t cont_count;
- uint32_t max_count;
-};
-
-struct magic_entry_set {
- struct magic_entry *me;
- uint32_t count;
- uint32_t max;
-};
-
-struct magic_map {
- void *p;
- size_t len;
- struct magic *magic[MAGIC_SETS];
- uint32_t nmagic[MAGIC_SETS];
-};
-
-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 *, const char *,
- size_t, int);
-private void eatsize(const char **);
-private int apprentice_1(struct magic_set *, const char *, int);
-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 struct magic_map *apprentice_load(struct magic_set *,
- const char *, int);
-private struct mlist *mlist_alloc(void);
-private void mlist_free(struct mlist *);
-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 struct magic_map *apprentice_map(struct magic_set *, const char *);
-private void apprentice_unmap(struct magic_map *);
-private int apprentice_compile(struct magic_set *, struct magic_map *,
- 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 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 }
-};
-
-#ifdef COMPILE_ONLY
-
-int main(int, char *[]);
-
-int
-main(int argc, char *argv[])
-{
- int ret;
- struct magic_set *ms;
- char *progname;
-
- if ((progname = strrchr(argv[0], '/')) != NULL)
- progname++;
- else
- progname = argv[0];
-
- if (argc != 2) {
- (void)fprintf(stderr, "Usage: %s file\n", progname);
- return 1;
- }
-
- if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
- (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
- return 1;
- }
- ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
- if (ret == 1)
- (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
- magic_close(ms);
- return ret;
-}
-#endif /* COMPILE_ONLY */
-
-struct type_tbl_s {
- const char name[16];
- const size_t len;
- const int type;
- const int format;
-};
-
-/*
- * XXX - the actual Single UNIX Specification says that "long" means "long",
- * as in the C data type, but we treat it as meaning "4-byte integer".
- * Given that the OS X version of file 5.04 did the same, I guess that passes
- * the actual test; having "long" be dependent on how big a "long" is on
- * the machine running "file" is silly.
- */
-static const struct type_tbl_s type_tbl[] = {
-# define XX(s) s, (sizeof(s) - 1)
-# define XX_NULL "", 0
- { XX("invalid"), FILE_INVALID, FILE_FMT_NONE },
- { XX("byte"), FILE_BYTE, FILE_FMT_NUM },
- { XX("short"), FILE_SHORT, FILE_FMT_NUM },
- { XX("default"), FILE_DEFAULT, FILE_FMT_NONE },
- { 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_NUM },
- { XX("qwdate"), FILE_QWDATE, FILE_FMT_STR },
- { XX("leqwdate"), FILE_LEQWDATE, FILE_FMT_STR },
- { XX("beqwdate"), FILE_BEQWDATE, FILE_FMT_STR },
- { XX("name"), FILE_NAME, FILE_FMT_NONE },
- { XX("use"), FILE_USE, FILE_FMT_NONE },
- { XX("clear"), FILE_CLEAR, FILE_FMT_NONE },
- { XX_NULL, FILE_INVALID, FILE_FMT_NONE },
-};
-
-/*
- * These are not types, and cannot be preceded by "u" to make them
- * unsigned.
- */
-static const struct type_tbl_s special_tbl[] = {
- { XX("name"), FILE_NAME, FILE_FMT_STR },
- { XX("use"), FILE_USE, FILE_FMT_STR },
- { XX_NULL, FILE_INVALID, FILE_FMT_NONE },
-};
-# undef XX
-# undef XX_NULL
-
-private int
-get_type(const struct type_tbl_s *tbl, const char *l, const char **t)
-{
- const struct type_tbl_s *p;
-
- for (p = tbl; p->len; p++) {
- if (strncmp(l, p->name, p->len) == 0) {
- if (t)
- *t = l + p->len;
- break;
- }
- }
- return p->type;
-}
-
-private int
-get_standard_integer_type(const char *l, const char **t)
-{
- int type;
-
- if (isalpha((unsigned char)l[1])) {
- switch (l[1]) {
- case 'C':
- /* "dC" and "uC" */
- type = FILE_BYTE;
- break;
- case 'S':
- /* "dS" and "uS" */
- type = FILE_SHORT;
- break;
- case 'I':
- case 'L':
- /*
- * "dI", "dL", "uI", and "uL".
- *
- * XXX - the actual Single UNIX Specification says
- * that "L" means "long", as in the C data type,
- * but we treat it as meaning "4-byte integer".
- * Given that the OS X version of file 5.04 did
- * the same, I guess that passes the actual SUS
- * validation suite; having "dL" be dependent on
- * how big a "long" is on the machine running
- * "file" is silly.
- */
- type = FILE_LONG;
- break;
- case 'Q':
- /* "dQ" and "uQ" */
- type = FILE_QUAD;
- break;
- default:
- /* "d{anything else}", "u{anything else}" */
- return FILE_INVALID;
- }
- l += 2;
- } else if (isdigit((unsigned char)l[1])) {
- /*
- * "d{num}" and "u{num}"; we only support {num} values
- * of 1, 2, 4, and 8 - the Single UNIX Specification
- * doesn't say anything about whether arbitrary
- * values should be supported, but both the Solaris 10
- * and OS X Mountain Lion versions of file passed the
- * Single UNIX Specification validation suite, and
- * neither of them support values bigger than 8 or
- * non-power-of-2 values.
- */
- if (isdigit((unsigned char)l[2])) {
- /* Multi-digit, so > 9 */
- return FILE_INVALID;
- }
- switch (l[1]) {
- case '1':
- type = FILE_BYTE;
- break;
- case '2':
- type = FILE_SHORT;
- break;
- case '4':
- type = FILE_LONG;
- break;
- case '8':
- type = FILE_QUAD;
- break;
- default:
- /* XXX - what about 3, 5, 6, or 7? */
- return FILE_INVALID;
- }
- l += 2;
- } else {
- /*
- * "d" or "u" by itself.
- */
- type = FILE_LONG;
- ++l;
- }
- if (t)
- *t = l;
- return 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;
- }
- assert(p - type_tbl == FILE_NAMES_SIZE);
-}
-
-private int
-add_mlist(struct mlist *mlp, struct magic_map *map, size_t idx)
-{
- struct mlist *ml;
-
- if ((ml = CAST(struct mlist *, malloc(sizeof(*ml)))) == NULL)
- return -1;
-
- ml->map = idx == 0 ? map : NULL;
- ml->magic = map->magic[idx];
- ml->nmagic = map->nmagic[idx];
-
- mlp->prev->next = ml;
- ml->prev = mlp->prev;
- ml->next = mlp;
- mlp->prev = ml;
- return 0;
-}
-
-/*
- * Handle one file or directory.
- */
-private int
-apprentice_1(struct magic_set *ms, const char *fn, int action)
-{
- struct mlist *ml;
- struct magic_map *map;
- size_t i;
-
- if (magicsize != FILE_MAGICSIZE) {
- file_error(ms, 0, "magic element size %lu != %lu",
- (unsigned long)sizeof(*map->magic[0]),
- (unsigned long)FILE_MAGICSIZE);
- return -1;
- }
-
- if (action == FILE_COMPILE) {
- map = apprentice_load(ms, fn, action);
- if (map == NULL)
- return -1;
- return apprentice_compile(ms, map, fn);
- }
-
-#ifndef COMPILE_ONLY
- map = apprentice_map(ms, fn);
- if (map == NULL) {
- if (ms->flags & MAGIC_CHECK)
- file_magwarn(ms, "using regular magic file `%s'", fn);
- map = apprentice_load(ms, fn, action);
- if (map == NULL)
- return -1;
- }
-
- for (i = 0; i < MAGIC_SETS; i++) {
- if (add_mlist(ms->mlist[i], map, i) == -1) {
- file_oomem(ms, sizeof(*ml));
- apprentice_unmap(map);
- return -1;
- }
- }
-
- if (action == FILE_LIST) {
- for (i = 0; i < MAGIC_SETS; i++) {
- printf("Set %zu:\nBinary patterns:\n", i);
- apprentice_list(ms->mlist[i], BINTEST);
- printf("Text patterns:\n");
- apprentice_list(ms->mlist[i], TEXTTEST);
- }
- }
-
- return 0;
-#endif /* COMPILE_ONLY */
-}
-
-protected void
-file_ms_free(struct magic_set *ms)
-{
- size_t i;
- if (ms == NULL)
- return;
- for (i = 0; i < MAGIC_SETS; i++)
- mlist_free(ms->mlist[i]);
- free(ms->o.pbuf);
- free(ms->o.buf);
- free(ms->c.li);
- free(ms);
-}
-
-protected struct magic_set *
-file_ms_alloc(int flags)
-{
- struct magic_set *ms;
- size_t i, len;
-
- if ((ms = CAST(struct magic_set *, calloc((size_t)1,
- sizeof(struct magic_set)))) == NULL)
- return NULL;
-
- if (magic_setflags(ms, flags) == -1) {
- errno = EINVAL;
- goto free;
- }
-
- ms->o.buf = ms->o.pbuf = NULL;
- len = (ms->c.len = 10) * sizeof(*ms->c.li);
-
- if ((ms->c.li = CAST(struct level_info *, malloc(len))) == NULL)
- goto free;
-
- ms->event_flags = 0;
- ms->error = -1;
- for (i = 0; i < MAGIC_SETS; i++)
- ms->mlist[i] = NULL;
- ms->file = "unknown";
- ms->line = 0;
- return ms;
-free:
- free(ms);
- return NULL;
-}
-
-private void
-apprentice_unmap(struct magic_map *map)
-{
- if (map == NULL)
- return;
- if (map->p != NULL) {
-#ifdef QUICK
- if (map->len)
- (void)munmap(map->p, map->len);
- else
-#endif
- free(map->p);
- } else {
- uint32_t j;
- for (j = 0; j < MAGIC_SETS; j++)
- free(map->magic[j]);
- }
- free(map);
-}
-
-private struct mlist *
-mlist_alloc(void)
-{
- struct mlist *mlist;
- if ((mlist = CAST(struct mlist *, calloc(1, sizeof(*mlist)))) == NULL) {
- return NULL;
- }
- mlist->next = mlist->prev = mlist;
- return mlist;
-}
-
-private void
-mlist_free(struct mlist *mlist)
-{
- struct mlist *ml;
-
- if (mlist == NULL)
- return;
-
- for (ml = mlist->next; ml != mlist;) {
- struct mlist *next = ml->next;
- if (ml->map)
- apprentice_unmap(ml->map);
- free(ml);
- ml = next;
- }
- free(ml);
-}
-
-/* const char *fn: list of magic files and directories */
-protected int
-file_apprentice(struct magic_set *ms, const char *fn, int action)
-{
- char *p, *mfn;
- int file_err, errs = -1;
- size_t i;
-
- if (ms->mlist[0] != NULL)
- file_reset(ms);
-
- if ((fn = magic_getpath(fn, action)) == NULL)
- return -1;
-
- init_file_tables();
-
- if ((mfn = strdup(fn)) == NULL) {
- file_oomem(ms, strlen(fn));
- return -1;
- }
-
- for (i = 0; i < MAGIC_SETS; i++) {
- mlist_free(ms->mlist[i]);
- if ((ms->mlist[i] = mlist_alloc()) == NULL) {
- file_oomem(ms, sizeof(*ms->mlist[i]));
- if (i != 0) {
- --i;
- do
- mlist_free(ms->mlist[i]);
- while (i != 0);
- }
- free(mfn);
- return -1;
- }
- }
- fn = mfn;
-
- while (fn) {
- p = strchr(fn, PATHSEP);
- if (p)
- *p++ = '\0';
- if (*fn == '\0')
- break;
- file_err = apprentice_1(ms, fn, action);
- errs = MAX(errs, file_err);
- fn = p;
- }
-
- free(mfn);
-
- if (errs == -1) {
- for (i = 0; i < MAGIC_SETS; i++) {
- mlist_free(ms->mlist[i]);
- ms->mlist[i] = NULL;
- }
- file_error(ms, 0, "could not find any valid magic files!");
- return -1;
- }
-
-#if 0
- /*
- * Always leave the database loaded
- */
- if (action == FILE_LOAD)
- return 0;
-
- for (i = 0; i < MAGIC_SETS; i++) {
- mlist_free(ms->mlist[i]);
- ms->mlist[i] = NULL;
- }
-#endif
-
- switch (action) {
- case FILE_LOAD:
- case FILE_COMPILE:
- case FILE_CHECK:
- case FILE_LIST:
- return 0;
- default:
- file_error(ms, 0, "Invalid action %d", action);
- return -1;
- }
-}
-
-/*
- * Compute the real length of a magic expression, for the purposes
- * of determining how "strong" a magic expression is (approximating
- * how specific its matches are):
- * - magic characters count 0 unless escaped.
- * - [] expressions count 1
- * - {} expressions count 0
- * - regular characters or escaped magic characters count 1
- * - 0 length expressions count as one
- */
-private size_t
-nonmagic(const char *str)
-{
- const char *p;
- size_t rv = 0;
-
- for (p = str; *p; p++)
- switch (*p) {
- case '\\': /* Escaped anything counts 1 */
- if (!*++p)
- p--;
- rv++;
- continue;
- case '?': /* Magic characters count 0 */
- case '*':
- case '.':
- case '+':
- case '^':
- case '$':
- continue;
- case '[': /* Bracketed expressions count 1 the ']' */
- while (*p && *p != ']')
- p++;
- p--;
- continue;
- case '{': /* Braced expressions count 0 */
- while (*p && *p != '}')
- p++;
- if (!*p)
- p--;
- continue;
- default: /* Anything else counts 1 */
- rv++;
- continue;
- }
-
- return rv == 0 ? 1 : rv; /* Return at least 1 */
-}
-
-/*
- * Get weight of this magic entry, for sorting purposes.
- */
-private size_t
-apprentice_magic_strength(const struct magic *m)
-{
-#define MULT 10
- size_t v, 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:
- val += m->vallen * MAX(MULT / m->vallen, 1);
- break;
-
- case FILE_REGEX:
- v = nonmagic(m->value.s);
- val += v * MAX(MULT / v, 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_QWDATE:
- case FILE_LEQWDATE:
- case FILE_BEQWDATE:
- case FILE_DOUBLE:
- case FILE_BEDOUBLE:
- case FILE_LEDOUBLE:
- val += 8 * MULT;
- break;
-
- case FILE_INDIRECT:
- case FILE_NAME:
- case FILE_USE:
- break;
-
- default:
- (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 = CAST(const struct magic_entry *, a);
- const struct magic_entry *mb = CAST(const struct magic_entry *, 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_QWDATE:
- case FILE_LEQWDATE:
- case FILE_BEQWDATE:
- 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;
- }
-}
-
-private int
-addentry(struct magic_set *ms, struct magic_entry *me,
- struct magic_entry_set *mset)
-{
- size_t i = me->mp->type == FILE_NAME ? 1 : 0;
- if (mset[i].count == mset[i].max) {
- struct magic_entry *mp;
-
- mset[i].max += ALLOC_INCR;
- if ((mp = CAST(struct magic_entry *,
- realloc(mset[i].me, sizeof(*mp) * mset[i].max))) ==
- NULL) {
- file_oomem(ms, sizeof(*mp) * mset[i].max);
- return -1;
- }
- (void)memset(&mp[mset[i].count], 0, sizeof(*mp) *
- ALLOC_INCR);
- mset[i].me = mp;
- }
- mset[i].me[mset[i].count++] = *me;
- memset(me, 0, sizeof(*me));
- return 0;
-}
-
-/*
- * Load and parse one file.
- */
-private void
-load_1(struct magic_set *ms, int action, const char *fn, int *errs,
- struct magic_entry_set *mset)
-{
- size_t lineno = 0, llen = 0;
- char *line = NULL;
- ssize_t len;
- struct magic_entry me;
-
- FILE *f = fopen(ms->file = fn, "r");
- if (f == NULL) {
- if (errno != ENOENT)
- file_error(ms, errno, "cannot read magic file `%s'",
- fn);
- (*errs)++;
- return;
- }
-
- memset(&me, 0, sizeof(me));
- /* read and parse this file */
- for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
- ms->line++) {
- 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 (me.mp == NULL) {
- file_error(ms, 0,
- "No current entry for :!%s type",
- bang[i].name);
- (*errs)++;
- continue;
- }
- if ((*bang[i].fun)(ms, &me,
- line + bang[i].len + 2) != 0) {
- (*errs)++;
- continue;
- }
- continue;
- }
- /*FALLTHROUGH*/
- default:
- again:
- switch (parse(ms, &me, line, lineno, action)) {
- case 0:
- continue;
- case 1:
- (void)addentry(ms, &me, mset);
- goto again;
- default:
- (*errs)++;
- break;
- }
- }
- }
- if (me.mp)
- (void)addentry(ms, &me, mset);
- free(line);
- (void)fclose(f);
-}
-
-/*
- * 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 uint32_t
-set_text_binary(struct magic_set *ms, struct magic_entry *me, uint32_t nme,
- uint32_t starttest)
-{
- static const char text[] = "text";
- static const char binary[] = "binary";
- static const size_t len = sizeof(text);
-
- uint32_t i = starttest;
-
- do {
- set_test_type(me[starttest].mp, me[i].mp);
- if ((ms->flags & MAGIC_DEBUG) == 0)
- continue;
- (void)fprintf(stderr, "%s%s%s: %s\n",
- me[i].mp->mimetype,
- me[i].mp->mimetype[0] == '\0' ? "" : "; ",
- me[i].mp->desc[0] ? me[i].mp->desc : "(no description)",
- me[i].mp->flag & BINTEST ? binary : text);
- if (me[i].mp->flag & BINTEST) {
- char *p = strstr(me[i].mp->desc, text);
- if (p && (p == me[i].mp->desc ||
- isspace((unsigned char)p[-1])) &&
- (p + len - me[i].mp->desc == MAXstring
- || (p[len] == '\0' ||
- isspace((unsigned char)p[len]))))
- (void)fprintf(stderr, "*** Possible "
- "binary test for text type\n");
- }
- } while (++i < nme && me[i].mp->cont_level != 0);
- return i;
-}
-
-private void
-set_last_default(struct magic_set *ms, struct magic_entry *me, uint32_t nme)
-{
- uint32_t i;
- for (i = 0; i < nme; i++) {
- if (me[i].mp->cont_level == 0 &&
- me[i].mp->type == FILE_DEFAULT) {
- while (++i < nme)
- if (me[i].mp->cont_level == 0)
- break;
- if (i != nme) {
- /* XXX - Ugh! */
- ms->line = me[i].mp->lineno;
- file_magwarn(ms,
- "level 0 \"default\" did not sort last");
- }
- return;
- }
- }
-}
-
-private int
-coalesce_entries(struct magic_set *ms, struct magic_entry *me, uint32_t nme,
- struct magic **ma, uint32_t *nma)
-{
- uint32_t i, mentrycount = 0;
- size_t slen;
-
- for (i = 0; i < nme; i++)
- mentrycount += me[i].cont_count;
-
- slen = sizeof(**ma) * mentrycount;
- if ((*ma = CAST(struct magic *, malloc(slen))) == NULL) {
- file_oomem(ms, slen);
- return -1;
- }
-
- mentrycount = 0;
- for (i = 0; i < nme; i++) {
- (void)memcpy(*ma + mentrycount, me[i].mp,
- me[i].cont_count * sizeof(**ma));
- mentrycount += me[i].cont_count;
- }
- *nma = mentrycount;
- return 0;
-}
-
-private void
-magic_entry_free(struct magic_entry *me, uint32_t nme)
-{
- uint32_t i;
- if (me == NULL)
- return;
- for (i = 0; i < nme; i++)
- free(me[i].mp);
- free(me);
-}
-
-private struct magic_map *
-apprentice_load(struct magic_set *ms, const char *fn, int action)
-{
- int errs = 0;
- uint32_t i, j;
- size_t files = 0, maxfiles = 0;
- char **filearr = NULL, *mfn;
- struct stat st;
- struct magic_map *map;
- struct magic_entry_set mset[MAGIC_SETS];
- DIR *dir;
- struct dirent *d;
-
- memset(mset, 0, sizeof(mset));
- ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
-
-
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL)
- {
- file_oomem(ms, sizeof(*map));
- return NULL;
- }
-
- /* print silly verbose header for USG compat. */
- if (action == FILE_CHECK)
- (void)fprintf(stderr, "%s\n", usg_hdr);
-
- /* load directory or file */
- if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) {
- dir = opendir(fn);
- if (!dir) {
- errs++;
- goto out;
- }
- while ((d = readdir(dir)) != NULL) {
- if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) {
- file_oomem(ms,
- strlen(fn) + strlen(d->d_name) + 2);
- errs++;
- closedir(dir);
- goto out;
- }
- if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) {
- free(mfn);
- 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);
- free(mfn);
- closedir(dir);
- errs++;
- goto out;
- }
- }
- filearr[files++] = mfn;
- }
- closedir(dir);
- qsort(filearr, files, sizeof(*filearr), cmpstrp);
- for (i = 0; i < files; i++) {
- load_1(ms, action, filearr[i], &errs, mset);
- free(filearr[i]);
- }
- free(filearr);
- } else
- load_1(ms, action, fn, &errs, mset);
- if (errs)
- goto out;
-
- for (j = 0; j < MAGIC_SETS; j++) {
- /* Set types of tests */
- for (i = 0; i < mset[j].count; ) {
- if (mset[j].me[i].mp->cont_level != 0) {
- i++;
- continue;
- }
- i = set_text_binary(ms, mset[j].me, mset[j].count, i);
- }
- qsort(mset[j].me, mset[j].count, sizeof(*mset[j].me),
- apprentice_sort);
-
- /*
- * Make sure that any level 0 "default" line is last
- * (if one exists).
- */
- set_last_default(ms, mset[j].me, mset[j].count);
-
- /* coalesce per file arrays into a single one */
- if (coalesce_entries(ms, mset[j].me, mset[j].count,
- &map->magic[j], &map->nmagic[j]) == -1) {
- errs++;
- goto out;
- }
- }
-
-out:
- for (j = 0; j < MAGIC_SETS; j++)
- magic_entry_free(mset[j].me, mset[j].count);
-
- if (errs) {
- apprentice_unmap(map);
- return NULL;
- }
- return map;
-}
-
-/*
- * 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_QWDATE:
- case FILE_BEQDATE:
- case FILE_BEQLDATE:
- case FILE_BEQWDATE:
- case FILE_LEQDATE:
- case FILE_LEQLDATE:
- case FILE_LEQWDATE:
- 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:
- case FILE_NAME:
- case FILE_USE:
- case FILE_CLEAR:
- 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_REGEX || (m->str_flags & REGEX_LINE_COUNT) == 0) &&
- (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 *me, const char *line,
- size_t lineno, int action)
-{
-#ifdef ENABLE_CONDITIONALS
- static uint32_t last_cont_level = 0;
-#endif
- size_t i;
- struct magic *m;
- const char *l = line;
- char *t;
- int op;
- uint32_t cont_level;
- int32_t diff;
-
- cont_level = 0;
-
- /*
- * Parse the offset.
- */
- 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
- if (cont_level != 0) {
- if (me->mp == NULL) {
- file_magerror(ms, "No current entry for continuation");
- return -1;
- }
- if (me->cont_count == 0) {
- file_magerror(ms, "Continuations present with 0 count");
- return -1;
- }
- m = &me->mp[me->cont_count - 1];
- diff = (int32_t)cont_level - (int32_t)m->cont_level;
- if (diff > 1)
- file_magwarn(ms, "New continuation level %u is more "
- "than one larger than current level %u", cont_level,
- m->cont_level);
- if (me->cont_count == me->max_count) {
- struct magic *nm;
- size_t cnt = me->max_count + ALLOC_CHUNK;
- if ((nm = CAST(struct magic *, realloc(me->mp,
- sizeof(*nm) * cnt))) == NULL) {
- file_oomem(ms, sizeof(*nm) * cnt);
- return -1;
- }
- 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 {
- static const size_t len = sizeof(*m) * ALLOC_CHUNK;
- if (me->mp != NULL)
- return 1;
- if ((m = CAST(struct magic *, malloc(len))) == NULL) {
- file_oomem(ms, len);
- return -1;
- }
- me->mp = m;
- me->max_count = ALLOC_CHUNK;
- (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
-
- /*
- * Parse the type.
- */
- if (*l == 'u') {
- /*
- * Try it as a keyword type prefixed by "u"; match what
- * follows the "u". If that fails, try it as an SUS
- * integer type.
- */
- m->type = get_type(type_tbl, l + 1, &l);
- if (m->type == FILE_INVALID) {
- /*
- * Not a keyword type; parse it as an SUS type,
- * 'u' possibly followed by a number or C/S/L.
- */
- m->type = get_standard_integer_type(l, &l);
- }
- /* It's unsigned. */
- if (m->type != FILE_INVALID)
- m->flag |= UNSIGNED;
- } else {
- /*
- * Try it as a keyword type. If that fails, try it as
- * an SUS integer type if it begins with "d" or as an
- * SUS string type if it begins with "s". In any case,
- * it's not unsigned.
- */
- m->type = get_type(type_tbl, l, &l);
- if (m->type == FILE_INVALID) {
- /*
- * Not a keyword type; parse it as an SUS type,
- * either 'd' possibly followed by a number or
- * C/S/L, or just 's'.
- */
- if (*l == 'd')
- m->type = get_standard_integer_type(l, &l);
- else if (*l == 's' && !isalpha((unsigned char)l[1])) {
- m->type = FILE_STRING;
- ++l;
- }
- }
- }
-
- if (m->type == FILE_INVALID) {
- /* Not found - try it as a special keyword. */
- m->type = get_type(special_tbl, l, &l);
- }
-
- if (m->type == FILE_INVALID) {
- if (ms->flags & MAGIC_CHECK)
- file_magwarn(ms, "type `%s' invalid", l);
- 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_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_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_TRIM:
- m->str_flags |= STRING_TRIM;
- 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:
- switch (m->type) {
- case FILE_PSTRING:
- case FILE_REGEX:
- break;
- default:
- 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;
- }
-#ifndef COMPILE_ONLY
- if (action == FILE_CHECK) {
- file_mdump(m);
- }
-#endif
- m->mimetype[0] = '\0'; /* initialise MIME type to none */
- 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;
- }
- if (m->type == FILE_NAME) {
- file_magwarn(ms, "%s: Strength setting is not supported in "
- "\"name\" magic entries", m->value.s);
- 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;
-}
-
-private int
-parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line,
- off_t off, size_t len, const char *name, int nt)
-{
- size_t i;
- const char *l = line;
- struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1];
- char *buf = (char *)m + off;
-
- if (buf[0] != '\0') {
- len = nt ? strlen(buf) : len;
- file_magwarn(ms, "Current entry already has a %s type "
- "`%.*s', new type `%s'", name, (int)len, buf, l);
- return -1;
- }
-
- if (*m->desc == '\0') {
- file_magwarn(ms, "Current entry does not yet have a "
- "description for adding a %s type", name);
- return -1;
- }
-
- EATAB;
- for (i = 0; *l && ((isascii((unsigned char)*l) &&
- isalnum((unsigned char)*l)) || strchr("-+/.", *l)) &&
- i < len; buf[i++] = *l++)
- continue;
-
- if (i == len && *l) {
- if (nt)
- buf[len - 1] = '\0';
- if (ms->flags & MAGIC_CHECK)
- file_magwarn(ms, "%s type `%s' truncated %"
- SIZE_T_FORMAT "u", name, line, i);
- } else {
- if (nt)
- buf[i] = '\0';
- }
-
- if (i > 0)
- return 0;
- else
- 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)
-{
- struct magic *m = &me->mp[0];
-
- return parse_extra(ms, me, line, offsetof(struct magic, apple),
- sizeof(m->apple), "APPLE", 0);
-}
-
-/*
- * 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)
-{
- struct magic *m = &me->mp[0];
-
- return parse_extra(ms, me, line, offsetof(struct magic, mimetype),
- sizeof(m->mimetype), "MIME", 1);
-}
-
-private int
-check_format_type(const char *ptr, int type)
-{
- int quad = 0, h;
- if (*ptr == '\0') {
- /* Missing format string; bad */
- return -1;
- }
-
- switch (file_formats[type]) {
- case FILE_FMT_QUAD:
- quad = 1;
- /*FALLTHROUGH*/
- case FILE_FMT_NUM:
- if (quad == 0) {
- switch (type) {
- case FILE_BYTE:
- h = 2;
- break;
- case FILE_SHORT:
- case FILE_BESHORT:
- case FILE_LESHORT:
- h = 1;
- break;
- case FILE_LONG:
- case FILE_BELONG:
- case FILE_LELONG:
- case FILE_MELONG:
- case FILE_LEID3:
- case FILE_BEID3:
- case FILE_INDIRECT:
- h = 0;
- break;
- default:
- abort();
- }
- } else
- h = 0;
- 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++) {
-#ifdef STRICT_FORMAT /* "long" formats are int formats for us */
- /* so don't accept the 'l' modifier */
- case 'l':
- switch (*ptr++) {
- case 'i':
- case 'd':
- case 'u':
- case 'o':
- case 'x':
- case 'X':
- return h != 0 ? -1 : 0;
- default:
- return -1;
- }
-
- /*
- * Don't accept h and hh modifiers. They make writing
- * magic entries more complicated, for very little benefit
- */
- case 'h':
- if (h-- <= 0)
- return -1;
- switch (*ptr++) {
- case 'h':
- if (h-- <= 0)
- return -1;
- switch (*ptr++) {
- case 'i':
- case 'd':
- case 'u':
- case 'o':
- case 'x':
- case 'X':
- return 0;
- default:
- return -1;
- }
- case 'i':
- case 'd':
- case 'u':
- case 'o':
- case 'x':
- case 'X':
- return h != 0 ? -1 : 0;
- default:
- return -1;
- }
-#endif
- case 'c':
- return h != 2 ? -1 : 0;
- case 'i':
- case 'd':
- case 'u':
- case 'o':
- case 'x':
- case 'X':
-#ifdef STRICT_FORMAT
- return h != 0 ? -1 : 0;
-#else
- return 0;
-#endif
- 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, 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:
- case FILE_NAME:
- case FILE_USE:
- *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;
- }
- if (m->type == FILE_REGEX) {
- file_regex_t rx;
- int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED);
- if (rc) {
- if (ms->flags & MAGIC_CHECK)
- file_regerror(&rx, rc, ms);
- }
- file_regfree(&rx);
- return rc ? -1 : 0;
- }
- 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.
- */
-
-private struct magic_map *
-apprentice_map(struct magic_set *ms, const char *fn)
-{
- int fd;
- struct stat st;
- uint32_t *ptr;
- uint32_t version, entries, nentries;
- int needsbyteswap;
- char *dbname = NULL;
- struct magic_map *map;
- size_t i;
-
- fd = -1;
- if ((map = CAST(struct magic_map *, calloc(1, sizeof(*map)))) == NULL) {
- file_oomem(ms, sizeof(*map));
- goto error;
- }
-
- dbname = mkdbname(ms, fn, 0);
- if (dbname == NULL)
- goto error;
-
- if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
- goto error;
-
- if (fstat(fd, &st) == -1) {
- file_error(ms, errno, "cannot stat `%s'", dbname);
- goto error;
- }
- if (st.st_size < 8 || st.st_size > MAXMAGIC_SIZE) {
- file_error(ms, 0, "file `%s' is too %s", dbname,
- st.st_size < 8 ? "small" : "large");
- goto error;
- }
-
- map->len = (size_t)st.st_size;
-#ifdef QUICK
- if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
- file_error(ms, errno, "cannot map `%s'", dbname);
- goto error;
- }
-#else
- if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
- file_oomem(ms, map->len);
- goto error;
- }
- if (read(fd, map->p, map->len) != (ssize_t)map->len) {
- file_badread(ms);
- goto error;
- }
- map->len = 0;
-#define RET 1
-#endif
- (void)close(fd);
- fd = -1;
- ptr = CAST(uint32_t *, map->p);
- if (*ptr != MAGICNO) {
- if (swap4(*ptr) != MAGICNO) {
- file_error(ms, 0, "bad magic in `%s'", dbname);
- goto error;
- }
- needsbyteswap = 1;
- } else
- needsbyteswap = 0;
- if (needsbyteswap)
- version = swap4(ptr[1]);
- else
- version = ptr[1];
- if (version != VERSIONNO) {
- file_error(ms, 0, "File %s supports only version %d magic "
- "files. `%s' is version %d", VERSION,
- VERSIONNO, dbname, version);
- goto error;
- }
- entries = (uint32_t)(st.st_size / sizeof(struct magic));
- if ((off_t)(entries * sizeof(struct magic)) != st.st_size) {
- file_error(ms, 0, "Size of `%s' %" INT64_T_FORMAT "u is not "
- "a multiple of %" SIZE_T_FORMAT "u",
- dbname, (unsigned long long)st.st_size,
- sizeof(struct magic));
- goto error;
- }
- map->magic[0] = CAST(struct magic *, map->p) + 1;
- nentries = 0;
- for (i = 0; i < MAGIC_SETS; i++) {
- if (needsbyteswap)
- map->nmagic[i] = swap4(ptr[i + 2]);
- else
- map->nmagic[i] = ptr[i + 2];
- if (i != MAGIC_SETS - 1)
- map->magic[i + 1] = map->magic[i] + map->nmagic[i];
- nentries += map->nmagic[i];
- }
- if (entries != nentries + 1) {
- file_error(ms, 0, "Inconsistent entries in `%s' %u != %u",
- dbname, entries, nentries + 1);
- goto error;
- }
- if (needsbyteswap)
- for (i = 0; i < MAGIC_SETS; i++)
- byteswap(map->magic[i], map->nmagic[i]);
- free(dbname);
- return map;
-
-error:
- if (fd != -1)
- (void)close(fd);
- apprentice_unmap(map);
- free(dbname);
- return NULL;
-}
-
-/*
- * handle an mmaped file.
- */
-private int
-apprentice_compile(struct magic_set *ms, struct magic_map *map, const char *fn)
-{
- static const size_t nm = sizeof(*map->nmagic) * MAGIC_SETS;
- static const size_t m = sizeof(**map->magic);
- int fd = -1;
- size_t len;
- char *dbname;
- int rv = -1;
- uint32_t i;
- union {
- struct magic m;
- uint32_t h[2 + MAGIC_SETS];
- } hdr;
-
- dbname = mkdbname(ms, fn, 1);
-
- if (dbname == NULL)
- goto out;
-
- if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1)
- {
- file_error(ms, errno, "cannot open `%s'", dbname);
- goto out;
- }
- memset(&hdr, 0, sizeof(hdr));
- hdr.h[0] = MAGICNO;
- hdr.h[1] = VERSIONNO;
- memcpy(hdr.h + 2, map->nmagic, nm);
-
- if (write(fd, &hdr, sizeof(hdr)) != (ssize_t)sizeof(hdr)) {
- file_error(ms, errno, "error writing `%s'", dbname);
- goto out;
- }
-
- for (i = 0; i < MAGIC_SETS; i++) {
- len = m * map->nmagic[i];
- if (write(fd, map->magic[i], len) != (ssize_t)len) {
- file_error(ms, errno, "error writing `%s'", dbname);
- goto out;
- }
- }
-
- if (fd != -1)
- (void)close(fd);
- rv = 0;
-out:
- free(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;
-
- 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) {
- if (asprintf(&buf, "%.*s.mime%s", (int)(q - fn), fn, ext) < 0)
- return NULL;
- if (access(buf, R_OK) != -1) {
- ms->flags &= MAGIC_MIME_TYPE;
- return buf;
- }
- free(buf);
- }
- if (asprintf(&buf, "%.*s%s", (int)(q - fn), fn, ext) < 0)
- return NULL;
-
- /* 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_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;
-}
-
-protected int
-file_magicfind(struct magic_set *ms, const char *name, struct mlist *v)
-{
- uint32_t i, j;
- struct mlist *mlist, *ml;
-
- mlist = ms->mlist[1];
-
- for (ml = mlist->next; ml != mlist; ml = ml->next) {
- struct magic *ma = ml->magic;
- uint32_t nma = ml->nmagic;
- for (i = 0; i < nma; i++) {
- if (ma[i].type != FILE_NAME)
- continue;
- if (strcmp(ma[i].value.s, name) == 0) {
- v->magic = &ma[i];
- for (j = i + 1; j < nma; j++)
- if (ma[j].cont_level == 0)
- break;
- v->nmagic = j - i;
- return 0;
- }
- }
- }
- return -1;
-}
Deleted: trunk/contrib/file/apptype.c
===================================================================
--- trunk/contrib/file/apptype.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/apptype.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,169 +0,0 @@
-/*
- * 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
Deleted: trunk/contrib/file/ascmagic.c
===================================================================
--- trunk/contrib/file/ascmagic.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/ascmagic.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,360 +0,0 @@
-/*
- * 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 at 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.88 2014/02/12 23:20:53 christos 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 = 0;
- 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 (ulen > 0 && (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;
- if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) {
- file_oomem(ms, mlen);
- goto done;
- }
- 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), 0, 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;
- break;
- 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:
- free(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;
-}
Deleted: trunk/contrib/file/asctime_r.c
===================================================================
--- trunk/contrib/file/asctime_r.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/asctime_r.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,19 +0,0 @@
-/* $File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $ */
-
-#include "file.h"
-#ifndef lint
-FILE_RCSID("@(#)$File: asctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $")
-#endif /* lint */
-#include <time.h>
-#include <string.h>
-
-/* asctime_r is not thread-safe anyway */
-char *
-asctime_r(const struct tm *t, char *dst)
-{
- char *p = asctime(t);
- if (p == NULL)
- return NULL;
- memcpy(dst, p, 26);
- return dst;
-}
Deleted: trunk/contrib/file/asprintf.c
===================================================================
--- trunk/contrib/file/asprintf.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/asprintf.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,45 +0,0 @@
-/*
- * 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.
- */
-
-#include "file.h"
-
-#ifndef lint
-FILE_RCSID("@(#)$File: asprintf.c,v 1.4 2010/07/21 16:47:17 christos Exp $")
-#endif
-
-int asprintf(char **ptr, const char *fmt, ...)
-{
- va_list vargs;
- int retval;
-
- va_start(vargs, fmt);
- retval = vasprintf(ptr, fmt, vargs);
- va_end(vargs);
-
- return retval;
-}
Deleted: trunk/contrib/file/cdf.c
===================================================================
--- trunk/contrib/file/cdf.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/cdf.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,1368 +0,0 @@
-/*-
- * 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.63 2014/06/09 13:04:37 christos Exp $")
-#endif
-
-#include <assert.h>
-#ifdef CDF_DEBUG
-#include <err.h>
-#endif
-#include <stdlib.h>
-#include <unistd.h>
-#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;
- size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
- CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
- (void)&line;
- if (e >= b && (size_t)(e - b) <= ss * 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),
- ss * sst->sst_len, ss, 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 (pread(info->i_fd, buf, len, off) != (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 + len > CDF_SEC_SIZE(h) * sst->sst_len) {
- DPRINTF(("Out of bounds read %" SIZE_T_FORMAT "u > %"
- SIZE_T_FORMAT "u\n",
- pos + len, 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)
- / sizeof(maxsector));
-
- 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]);
- }
- if (i == 0) {
- DPRINTF((" none, sid: %d\n", sid));
- return (size_t)-1;
-
- }
- 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,
- const cdf_directory_t **root)
-{
- size_t i;
- const cdf_directory_t *d;
-
- *root = NULL;
- 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];
- *root = d;
-
- /* 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)
-{
- return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
- "\05SummaryInformation", scn);
-}
-
-int
-cdf_read_user_stream(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, const char *name, cdf_stream_t *scn)
-{
- size_t i;
- const cdf_directory_t *d;
- size_t name_len = strlen(name) + 1;
-
- 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, name_len)
- == 0)
- break;
-
- if (i == 0) {
- DPRINTF(("Cannot find user stream `%s'\n", name));
- 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 tail = (i << 1) + 1;
- if (cdf_check_stream_offset(sst, h, p, tail * sizeof(uint32_t),
- __LINE__) == -1)
- goto out;
- size_t ofs = CDF_GETUINT32(p, tail);
- 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);
- if (nelements == 0) {
- DPRINTF(("CDF_VECTOR with nelements == 0\n"));
- goto out;
- }
- 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 && i < sh.sh_properties;
- 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 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_TOLE4(si->si_count);
- *count = 0;
- maxcount = 0;
- *info = NULL;
- 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 timespec ts;
-
- static const char *types[] = { "empty", "user storage",
- "user stream", "lockbytes", "property", "root storage" };
-
- for (i = 0; i < dir->dir_len; i++) {
- char buf[26];
- 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, buf));
- cdf_timestamp_to_timespec(&ts, d->d_modified);
- (void)fprintf(stderr, "Modified %s",
- cdf_ctime(&ts.tv_sec, buf));
- (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 timespec 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 (tp < 1000000000000000LL) {
- cdf_print_elapsed_time(buf, sizeof(buf), tp);
- (void)fprintf(stderr, "timestamp %s\n", buf);
- } else {
- char buf[26];
- cdf_timestamp_to_timespec(&ts, tp);
- (void)fprintf(stderr, "timestamp %s",
- cdf_ctime(&ts.tv_sec, buf));
- }
- 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
Deleted: trunk/contrib/file/cdf.h
===================================================================
--- trunk/contrib/file/cdf.h 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/cdf.h 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,329 +0,0 @@
-/*-
- * 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 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;
-#define CDF_MAGIC 0xE11AB1A1E011CFD0LL
- 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 timespec;
-int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
-int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
-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 *,
- const cdf_directory_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_user_stream(const cdf_info_t *, const cdf_header_t *,
- const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
- const cdf_dir_t *, const char *, cdf_stream_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 *, char *);
-
-#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_ */
Deleted: trunk/contrib/file/cdf_time.c
===================================================================
--- trunk/contrib/file/cdf_time.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/cdf_time.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,198 +0,0 @@
-/*-
- * 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.15 2014/05/14 23:15:42 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 timespec *ts, cdf_timestamp_t t)
-{
- struct tm tm;
-#ifdef HAVE_STRUCT_TM_TM_ZONE
- static char UTC[] = "UTC";
-#endif
- int rdays;
-
- /* Unit is 100's of nanoseconds */
- ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
-
- 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 timespec *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_nsec / 100) * 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, char *buf)
-{
- char *ptr = ctime_r(sec, buf);
- if (ptr != NULL)
- return buf;
- (void)snprintf(buf, 26, "*Bad* 0x%16.16" INT64_T_FORMAT "x\n",
- (long long)*sec);
- return buf;
-}
-
-
-#ifdef TEST_TIME
-int
-main(int argc, char *argv[])
-{
- struct timespec ts;
- char buf[25];
- 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, buf);
- 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
Deleted: trunk/contrib/file/compress.c
===================================================================
--- trunk/contrib/file/compress.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/compress.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,505 +0,0 @@
-/*
- * 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 "file.h"
-
-#ifndef lint
-FILE_RCSID("@(#)$File: compress.c,v 1.73 2014/01/05 15:55:21 christos Exp $")
-#endif
-
-#include "magic.h"
-#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <string.h>
-#include <errno.h>
-#ifndef __MINGW32__
-#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
-
-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 */
- { "\004\"M\030", 4, { "lz4", "-cd", NULL }, 1 }, /* LZ4 */
-};
-
-#define NODATA ((size_t)~0)
-
-private ssize_t swrite(int, const void *, size_t);
-#if HAVE_FORK
-private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
-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;
-
- if ((ms->flags & MAGIC_COMPRESS) == 0)
- return 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 (file_buffer(ms, -1, NULL, buf, nbytes) == -1)
- goto error;
- if (!mime && file_printf(ms, ")") == -1)
- goto error;
- }
-
- rv = 1;
- break;
- }
- }
-error:
- free(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 __attribute__((__unused__)))
-{
- 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
- ssize_t 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 = read(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;
-
- (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
- {
- int te;
- 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 (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
- file_badseek(ms);
- return -1;
- }
- return fd;
-}
-#if HAVE_FORK
-#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;
- if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL) {
- return 0;
- }
-
- /* 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) {
- if (dup(fd) == -1)
- _exit(1);
- (void) lseek(0, (off_t)0, SEEK_SET);
- } else {
- if (dup(fdin[0]) == -1)
- _exit(1);
- (void) close(fdin[0]);
- (void) close(fdin[1]);
- }
-
- (void) close(1);
- if (dup(fdout[1]) == -1)
- _exit(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;
- }
-
- if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
-#ifdef DEBUG
- (void)fprintf(stderr, "Malloc failed (%s)\n",
- strerror(errno));
-#endif
- n = 0;
- goto err;
- }
- if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) {
-#ifdef DEBUG
- (void)fprintf(stderr, "Read failed (%s)\n",
- strerror(errno));
-#endif
- free(*newch);
- n = 0;
- *newch = NULL;
- 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
Deleted: trunk/contrib/file/ctime_r.c
===================================================================
--- trunk/contrib/file/ctime_r.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/ctime_r.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,19 +0,0 @@
-/* $File: ctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $ */
-
-#include "file.h"
-#ifndef lint
-FILE_RCSID("@(#)$File: ctime_r.c,v 1.1 2012/05/15 17:14:36 christos Exp $")
-#endif /* lint */
-#include <time.h>
-#include <string.h>
-
-/* ctime_r is not thread-safe anyway */
-char *
-ctime_r(const time_t *t, char *dst)
-{
- char *p = ctime(t);
- if (p == NULL)
- return NULL;
- memcpy(dst, p, 26);
- return dst;
-}
Deleted: trunk/contrib/file/elfclass.h
===================================================================
--- trunk/contrib/file/elfclass.h 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/elfclass.h 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,78 +0,0 @@
-/*
- * 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:
- phnum = elf_getu16(swap, elfhdr.e_phnum);
- if (phnum > MAX_PHNUM)
- return toomany(ms, "program", phnum);
- flags |= FLAGS_IS_CORE;
- if (dophn_core(ms, clazz, swap, fd,
- (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
- (size_t)elf_getu16(swap, elfhdr.e_phentsize),
- fsize, &flags) == -1)
- return -1;
- break;
-#endif
- case ET_EXEC:
- case ET_DYN:
- phnum = elf_getu16(swap, elfhdr.e_phnum);
- if (phnum > MAX_PHNUM)
- return toomany(ms, "program", phnum);
- shnum = elf_getu16(swap, elfhdr.e_shnum);
- if (shnum > MAX_SHNUM)
- return toomany(ms, "section", shnum);
- if (dophn_exec(ms, clazz, swap, fd,
- (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
- (size_t)elf_getu16(swap, elfhdr.e_phentsize),
- fsize, &flags, shnum) == -1)
- return -1;
- /*FALLTHROUGH*/
- case ET_REL:
- shnum = elf_getu16(swap, elfhdr.e_shnum);
- if (shnum > MAX_SHNUM)
- return toomany(ms, "section", shnum);
- if (doshn(ms, clazz, swap, fd,
- (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
- (size_t)elf_getu16(swap, elfhdr.e_shentsize),
- fsize, &flags, elf_getu16(swap, elfhdr.e_machine),
- (int)elf_getu16(swap, elfhdr.e_shstrndx)) == -1)
- return -1;
- break;
-
- default:
- break;
- }
- return 1;
Deleted: trunk/contrib/file/encoding.c
===================================================================
--- trunk/contrib/file/encoding.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/encoding.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,504 +0,0 @@
-/*
- * 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 at freebsd.org> wrote the original support for 8-bit
- * international characters.
- */
-
-#include "file.h"
-
-#ifndef lint
-FILE_RCSID("@(#)$File: encoding.c,v 1.9 2013/11/19 20:45:50 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";
- *ulen = 0;
- *code = "unknown";
- *code_mime = "binary";
-
- mlen = (nbytes + 1) * sizeof((*ubuf)[0]);
- if ((*ubuf = CAST(unichar *, calloc((size_t)1, mlen))) == NULL) {
- file_oomem(ms, mlen);
- goto done;
- }
- mlen = (nbytes + 1) * sizeof(nbuf[0]);
- if ((nbuf = CAST(unsigned char *, 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]];
- }
-}
Deleted: trunk/contrib/file/file.c
===================================================================
--- trunk/contrib/file/file.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/file.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,540 +0,0 @@
-/*
- * 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 - find type of a file or files - main program.
- */
-
-#include "file.h"
-
-#ifndef lint
-FILE_RCSID("@(#)$File: file.c,v 1.153 2014/02/11 15:41:04 christos Exp $")
-#endif /* lint */
-
-#include "magic.h"
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#ifdef RESTORE_TIME
-# if (__COHERENT__ >= 0x420)
-# include <sys/utime.h>
-# else
-# ifdef USE_UTIMES
-# include <sys/time.h>
-# else
-# include <utime.h>
-# endif
-# endif
-#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h> /* for read() */
-#endif
-#ifdef HAVE_LOCALE_H
-#include <locale.h>
-#endif
-#ifdef HAVE_WCHAR_H
-#include <wchar.h>
-#endif
-
-#if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
-#include <getopt.h>
-#ifndef HAVE_GETOPT_LONG
-int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
-#endif
-#else
-#include "mygetopt.h"
-#endif
-
-#ifdef S_IFLNK
-#define FILE_FLAGS "-bcEhikLlNnprsvz0"
-#else
-#define FILE_FLAGS "-bcEiklNnprsvz0"
-#endif
-
-# define USAGE \
- "Usage: %s [" FILE_FLAGS \
- "] [--apple] [--mime-encoding] [--mime-type]\n" \
- " [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \
- "file ...\n" \
- " %s -C [-m magicfiles]\n" \
- " %s [--help]\n"
-
-private int /* Global command-line options */
- bflag = 0, /* brief output format */
- nopad = 0, /* Don't pad output */
- nobuffer = 0, /* Do not buffer stdout */
- nulsep = 0; /* Append '\0' to the separator */
-
-private const char *separator = ":"; /* Default field separator */
-private const struct option long_options[] = {
-#define OPT(shortname, longname, opt, doc) \
- {longname, opt, NULL, shortname},
-#define OPT_LONGONLY(longname, opt, doc) \
- {longname, opt, NULL, 0},
-#include "file_opts.h"
-#undef OPT
-#undef OPT_LONGONLY
- {0, 0, NULL, 0}
-};
-#define OPTSTRING "bcCde:Ef:F:hiklLm:nNprsvz0"
-
-private const struct {
- const char *name;
- int value;
-} nv[] = {
- { "apptype", MAGIC_NO_CHECK_APPTYPE },
- { "ascii", MAGIC_NO_CHECK_ASCII },
- { "cdf", MAGIC_NO_CHECK_CDF },
- { "compress", MAGIC_NO_CHECK_COMPRESS },
- { "elf", MAGIC_NO_CHECK_ELF },
- { "encoding", MAGIC_NO_CHECK_ENCODING },
- { "soft", MAGIC_NO_CHECK_SOFT },
- { "tar", MAGIC_NO_CHECK_TAR },
- { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */
- { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
-};
-
-private char *progname; /* used throughout */
-
-private void usage(void);
-private void docprint(const char *);
-private void help(void);
-
-private int unwrap(struct magic_set *, const char *);
-private int process(struct magic_set *ms, const char *, int);
-private struct magic_set *load(const char *, int);
-
-
-/*
- * main - parse arguments and handle options
- */
-int
-main(int argc, char *argv[])
-{
- int c;
- size_t i;
- int action = 0, didsomefiles = 0, errflg = 0;
- int flags = 0, e = 0;
- struct magic_set *magic = NULL;
- int longindex;
- const char *magicfile = NULL; /* where the magic is */
-
- /* makes islower etc work for other langs */
- (void)setlocale(LC_CTYPE, "");
-
-#ifdef __EMX__
- /* sh-like wildcard expansion! Shouldn't hurt at least ... */
- _wildcard(&argc, &argv);
-#endif
-
- if ((progname = strrchr(argv[0], '/')) != NULL)
- progname++;
- else
- progname = argv[0];
-
-#ifdef S_IFLNK
- flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
-#endif
- while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
- &longindex)) != -1)
- switch (c) {
- case 0 :
- switch (longindex) {
- case 0:
- help();
- break;
- case 10:
- flags |= MAGIC_APPLE;
- break;
- case 11:
- flags |= MAGIC_MIME_TYPE;
- break;
- case 12:
- flags |= MAGIC_MIME_ENCODING;
- break;
- }
- break;
- case '0':
- nulsep = 1;
- break;
- case 'b':
- bflag++;
- break;
- case 'c':
- action = FILE_CHECK;
- break;
- case 'C':
- action = FILE_COMPILE;
- break;
- case 'd':
- flags |= MAGIC_DEBUG|MAGIC_CHECK;
- break;
- case 'E':
- flags |= MAGIC_ERROR;
- break;
- case 'e':
- for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
- if (strcmp(nv[i].name, optarg) == 0)
- break;
-
- if (i == sizeof(nv) / sizeof(nv[0]))
- errflg++;
- else
- flags |= nv[i].value;
- break;
-
- case 'f':
- if(action)
- usage();
- if (magic == NULL)
- if ((magic = load(magicfile, flags)) == NULL)
- return 1;
- e |= unwrap(magic, optarg);
- ++didsomefiles;
- break;
- case 'F':
- separator = optarg;
- break;
- case 'i':
- flags |= MAGIC_MIME;
- break;
- case 'k':
- flags |= MAGIC_CONTINUE;
- break;
- case 'l':
- action = FILE_LIST;
- break;
- case 'm':
- magicfile = optarg;
- break;
- case 'n':
- ++nobuffer;
- break;
- case 'N':
- ++nopad;
- break;
-#if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
- case 'p':
- flags |= MAGIC_PRESERVE_ATIME;
- break;
-#endif
- case 'r':
- flags |= MAGIC_RAW;
- break;
- case 's':
- flags |= MAGIC_DEVICES;
- break;
- case 'v':
- if (magicfile == NULL)
- magicfile = magic_getpath(magicfile, action);
- (void)fprintf(stdout, "%s-%s\n", progname, VERSION);
- (void)fprintf(stdout, "magic file from %s\n",
- magicfile);
- return 0;
- case 'z':
- flags |= MAGIC_COMPRESS;
- break;
-#ifdef S_IFLNK
- case 'L':
- flags |= MAGIC_SYMLINK;
- break;
- case 'h':
- flags &= ~MAGIC_SYMLINK;
- break;
-#endif
- case '?':
- default:
- errflg++;
- break;
- }
-
- if (errflg) {
- usage();
- }
- if (e)
- return e;
-
- if (MAGIC_VERSION != magic_version())
- (void)fprintf(stderr, "%s: compiled magic version [%d] "
- "does not match with shared library magic version [%d]\n",
- progname, MAGIC_VERSION, magic_version());
-
- switch(action) {
- case FILE_CHECK:
- case FILE_COMPILE:
- case FILE_LIST:
- /*
- * Don't try to check/compile ~/.magic unless we explicitly
- * ask for it.
- */
- magic = magic_open(flags|MAGIC_CHECK);
- if (magic == NULL) {
- (void)fprintf(stderr, "%s: %s\n", progname,
- strerror(errno));
- return 1;
- }
- switch(action) {
- case FILE_CHECK:
- c = magic_check(magic, magicfile);
- break;
- case FILE_COMPILE:
- c = magic_compile(magic, magicfile);
- break;
- case FILE_LIST:
- c = magic_list(magic, magicfile);
- break;
- default:
- abort();
- }
- if (c == -1) {
- (void)fprintf(stderr, "%s: %s\n", progname,
- magic_error(magic));
- return 1;
- }
- return 0;
- default:
- if (magic == NULL)
- if ((magic = load(magicfile, flags)) == NULL)
- return 1;
- break;
- }
-
- if (optind == argc) {
- if (!didsomefiles)
- usage();
- }
- else {
- size_t j, wid, nw;
- for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
- nw = file_mbswidth(argv[j]);
- if (nw > wid)
- wid = nw;
- }
- /*
- * If bflag is only set twice, set it depending on
- * number of files [this is undocumented, and subject to change]
- */
- if (bflag == 2) {
- bflag = optind >= argc - 1;
- }
- for (; optind < argc; optind++)
- e |= process(magic, argv[optind], wid);
- }
-
- if (magic)
- magic_close(magic);
- return e;
-}
-
-
-private struct magic_set *
-/*ARGSUSED*/
-load(const char *magicfile, int flags)
-{
- struct magic_set *magic = magic_open(flags);
- if (magic == NULL) {
- (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
- return NULL;
- }
- if (magic_load(magic, magicfile) == -1) {
- (void)fprintf(stderr, "%s: %s\n",
- progname, magic_error(magic));
- magic_close(magic);
- return NULL;
- }
- return magic;
-}
-
-/*
- * unwrap -- read a file of filenames, do each one.
- */
-private int
-unwrap(struct magic_set *ms, const char *fn)
-{
- FILE *f;
- ssize_t len;
- char *line = NULL;
- size_t llen = 0;
- int wid = 0, cwid;
- int e = 0;
-
- if (strcmp("-", fn) == 0) {
- f = stdin;
- wid = 1;
- } else {
- if ((f = fopen(fn, "r")) == NULL) {
- (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
- progname, fn, strerror(errno));
- return 1;
- }
-
- while ((len = getline(&line, &llen, f)) > 0) {
- if (line[len - 1] == '\n')
- line[len - 1] = '\0';
- cwid = file_mbswidth(line);
- if (cwid > wid)
- wid = cwid;
- }
-
- rewind(f);
- }
-
- while ((len = getline(&line, &llen, f)) > 0) {
- if (line[len - 1] == '\n')
- line[len - 1] = '\0';
- e |= process(ms, line, wid);
- if(nobuffer)
- (void)fflush(stdout);
- }
-
- free(line);
- (void)fclose(f);
- return e;
-}
-
-/*
- * Called for each input file on the command line (or in a list of files)
- */
-private int
-process(struct magic_set *ms, const char *inname, int wid)
-{
- const char *type;
- int std_in = strcmp(inname, "-") == 0;
-
- if (wid > 0 && !bflag) {
- (void)printf("%s", std_in ? "/dev/stdin" : inname);
- if (nulsep)
- (void)putc('\0', stdout);
- (void)printf("%s", separator);
- (void)printf("%*s ",
- (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
- }
-
- type = magic_file(ms, std_in ? NULL : inname);
- if (type == NULL) {
- (void)printf("ERROR: %s\n", magic_error(ms));
- return 1;
- } else {
- (void)printf("%s\n", type);
- return 0;
- }
-}
-
-protected size_t
-file_mbswidth(const char *s)
-{
-#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
- size_t bytesconsumed, old_n, n, width = 0;
- mbstate_t state;
- wchar_t nextchar;
- (void)memset(&state, 0, sizeof(mbstate_t));
- old_n = n = strlen(s);
-
- while (n > 0) {
- bytesconsumed = mbrtowc(&nextchar, s, n, &state);
- if (bytesconsumed == (size_t)(-1) ||
- bytesconsumed == (size_t)(-2)) {
- /* Something went wrong, return something reasonable */
- return old_n;
- }
- if (s[0] == '\n') {
- /*
- * do what strlen() would do, so that caller
- * is always right
- */
- width++;
- } else {
- int w = wcwidth(nextchar);
- if (w > 0)
- width += w;
- }
-
- s += bytesconsumed, n -= bytesconsumed;
- }
- return width;
-#else
- return strlen(s);
-#endif
-}
-
-private void
-usage(void)
-{
- (void)fprintf(stderr, USAGE, progname, progname, progname);
- exit(1);
-}
-
-private void
-docprint(const char *opts)
-{
- size_t i;
- int comma;
- char *sp, *p;
-
- p = strstr(opts, "%o");
- if (p == NULL) {
- fprintf(stdout, "%s", opts);
- return;
- }
-
- for (sp = p - 1; sp > opts && *sp == ' '; sp--)
- continue;
-
- fprintf(stdout, "%.*s", (int)(p - opts), opts);
-
- comma = 0;
- for (i = 0; i < __arraycount(nv); i++) {
- fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
- if (i && i % 5 == 0) {
- fprintf(stdout, ",\n%*s", (int)(p - sp - 1), "");
- comma = 0;
- }
- }
-
- fprintf(stdout, "%s", opts + (p - opts) + 2);
-}
-
-private void
-help(void)
-{
- (void)fputs(
-"Usage: file [OPTION...] [FILE...]\n"
-"Determine type of FILEs.\n"
-"\n", stdout);
-#define OPT(shortname, longname, opt, doc) \
- fprintf(stdout, " -%c, --" longname, shortname), \
- docprint(doc);
-#define OPT_LONGONLY(longname, opt, doc) \
- fprintf(stdout, " --" longname), \
- docprint(doc);
-#include "file_opts.h"
-#undef OPT
-#undef OPT_LONGONLY
- fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n");
- exit(0);
-}
Deleted: trunk/contrib/file/file.h
===================================================================
--- trunk/contrib/file/file.h 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/file.h 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,557 +0,0 @@
-/*
- * 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.152 2014/06/03 19:01:34 christos Exp $
- */
-
-#ifndef __file_h__
-#define __file_h__
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#ifdef 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
-#include <regex.h>
-#include <time.h>
-#include <sys/types.h>
-#include <sys/param.h>
-/* 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 (WIN32)
-#define PATHSEP ';'
-#else
-#define PATHSEP ':'
-#endif
-
-#define private static
-
-#if HAVE_VISIBILITY && !defined(WIN32)
-#define public __attribute__ ((__visibility__("default")))
-#ifndef protected
-#define protected __attribute__ ((__visibility__("hidden")))
-#endif
-#else
-#define public
-#ifndef protected
-#define protected
-#endif
-#endif
-
-#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 __GNUC__
-#ifndef __attribute__
-#define __attribute__(a)
-#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 len of text description/MIME type */
-#define MAXMIME 80 /* max len of text MIME type */
-#define MAXstring 64 /* max len of "string" types */
-
-#define MAGICNO 0xF11E041C
-#define VERSIONNO 12
-#define FILE_MAGICSIZE 248
-
-#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_QWDATE 42
-#define FILE_LEQWDATE 43
-#define FILE_BEQWDATE 44
-#define FILE_NAME 45
-#define FILE_USE 46
-#define FILE_CLEAR 47
-#define FILE_NAMES_SIZE 48 /* size of array to contain all names */
-
-#define IS_STRING(t) \
- ((t) == FILE_STRING || \
- (t) == FILE_PSTRING || \
- (t) == FILE_BESTRING16 || \
- (t) == FILE_LESTRING16 || \
- (t) == FILE_REGEX || \
- (t) == FILE_SEARCH || \
- (t) == FILE_NAME || \
- (t) == FILE_USE)
-
-#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-52 */
- char mimetype[MAXMIME]; /* MIME type */
- /* Words 53-54 */
- 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 REGEX_LINE_COUNT 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 STRING_TRIM BIT(13)
-#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_TRIM '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 */
- void *map; /* internal resources used by entry */
- 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
-};
-
-#define MAGIC_SETS 2
-
-struct magic_set {
- struct mlist *mlist[MAGIC_SETS]; /* list of regular entries */
- 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;
-#define FILE_T_LOCAL 1
-#define FILE_T_WINDOWS 2
-protected const char *file_fmttime(uint64_t, int, char *);
-protected struct magic_set *file_ms_alloc(int);
-protected void file_ms_free(struct magic_set *);
-protected int file_buffer(struct magic_set *, int, const char *, const void *,
- size_t);
-protected int file_fsmagic(struct magic_set *, const char *, struct stat *);
-protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
-protected int file_vprintf(struct magic_set *, const char *, va_list)
- __attribute__((__format__(__printf__, 2, 0)));
-protected size_t file_printedlen(const struct magic_set *);
-protected int file_replace(struct magic_set *, const char *, const char *);
-protected int file_printf(struct magic_set *, const char *, ...)
- __attribute__((__format__(__printf__, 2, 3)));
-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);
-#if HAVE_FORK
-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,
- size_t, int, int);
-protected int file_apprentice(struct magic_set *, const char *, int);
-protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
-protected uint64_t file_signextend(struct magic_set *, struct magic *,
- uint64_t);
-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 *, ...)
- __attribute__((__format__(__printf__, 3, 4)));
-protected void file_magerror(struct magic_set *, const char *, ...)
- __attribute__((__format__(__printf__, 2, 3)));
-protected void file_magwarn(struct magic_set *, const char *, ...)
- __attribute__((__format__(__printf__, 2, 3)));
-protected void file_mdump(struct magic *);
-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 *);
-#ifdef __EMX__
-protected int file_os2_apptype(struct magic_set *, const char *, const void *,
- size_t);
-#endif /* __EMX__ */
-
-typedef struct {
- const char *pat;
- char *old_lc_ctype;
- int rc;
- regex_t rx;
-} file_regex_t;
-
-protected int file_regcomp(file_regex_t *, const char *, int);
-protected int file_regexec(file_regex_t *, const char *, size_t, regmatch_t *,
- int);
-protected void file_regfree(file_regex_t *);
-protected void file_regerror(file_regex_t *, int, struct magic_set *);
-
-#ifndef COMPILE_ONLY
-extern const char *file_names[];
-extern const size_t file_nnames;
-#endif
-
-#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 HAVE_PREAD
-ssize_t pread(int, void *, size_t, off_t);
-#endif
-#ifndef HAVE_VASPRINTF
-int vasprintf(char **, const char *, va_list);
-#endif
-#ifndef HAVE_ASPRINTF
-int asprintf(char **, const char *, ...);
-#endif
-
-#ifndef HAVE_STRLCPY
-size_t strlcpy(char *, const char *, size_t);
-#endif
-#ifndef HAVE_STRLCAT
-size_t strlcat(char *, const char *, size_t);
-#endif
-#ifndef HAVE_STRCASESTR
-char *strcasestr(const char *, const char *);
-#endif
-#ifndef HAVE_GETLINE
-ssize_t getline(char **, size_t *, FILE *);
-ssize_t getdelim(char **, size_t *, int, FILE *);
-#endif
-#ifndef HAVE_CTIME_R
-char *ctime_r(const time_t *, char *);
-#endif
-#ifndef HAVE_ASCTIME_R
-char *asctime_r(const struct tm *, char *);
-#endif
-#ifndef HAVE_FMTCHECK
-const char *fmtcheck(const char *, const char *)
- __attribute__((__format_arg__(2)));
-#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
-
-#endif /* __file_h__ */
Deleted: trunk/contrib/file/file.man
===================================================================
--- trunk/contrib/file/file.man 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/file.man 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,623 +0,0 @@
-.\" $File: file.man,v 1.106 2014/03/07 23:11:51 christos Exp $
-.Dd January 30, 2014
-.Dt FILE __CSECTION__
-.Os
-.Sh NAME
-.Nm file
-.Nd determine file type
-.Sh SYNOPSIS
-.Nm
-.Bk -words
-.Op Fl bcEhiklLNnprsvz0
-.Op Fl Fl apple
-.Op Fl Fl mime-encoding
-.Op Fl Fl mime-type
-.Op Fl e Ar testname
-.Op Fl F Ar separator
-.Op Fl f Ar namefile
-.Op Fl m Ar magicfiles
-.Ar
-.Ek
-.Nm
-.Fl C
-.Op Fl m Ar magicfiles
-.Nm
-.Op Fl Fl help
-.Sh DESCRIPTION
-This manual page documents version __VERSION__ of the
-.Nm
-command.
-.Pp
-.Nm
-tests each argument in an attempt to classify it.
-There are three sets of tests, performed in this order:
-filesystem tests, magic tests, and language tests.
-The
-.Em first
-test that succeeds causes the file type to be printed.
-.Pp
-The type printed will usually contain one of the words
-.Em text
-(the file contains only
-printing characters and a few common control
-characters and is probably safe to read on an
-.Dv ASCII
-terminal),
-.Em executable
-(the file contains the result of compiling a program
-in a form understandable to some
-.Tn UNIX
-kernel or another),
-or
-.Em data
-meaning anything else (data is usually
-.Dq binary
-or non-printable).
-Exceptions are well-known file formats (core files, tar archives)
-that are known to contain binary data.
-When modifying magic files or the program itself, make sure to
-.Em "preserve these keywords" .
-Users depend on knowing that all the readable files in a directory
-have the word
-.Dq text
-printed.
-Don't do as Berkeley did and change
-.Dq shell commands text
-to
-.Dq shell script .
-.Pp
-The filesystem tests are based on examining the return from a
-.Xr stat 2
-system call.
-The program checks to see if the file is empty,
-or if it's some sort of special file.
-Any known file types appropriate to the system you are running on
-(sockets, symbolic links, or named pipes (FIFOs) on those systems that
-implement them)
-are intuited if they are defined in the system header file
-.In sys/stat.h .
-.Pp
-The magic tests are used to check for files with data in
-particular fixed formats.
-The canonical example of this is a binary executable (compiled program)
-.Dv a.out
-file, whose format is defined in
-.In elf.h ,
-.In a.out.h
-and possibly
-.In exec.h
-in the standard include directory.
-These files have a
-.Dq "magic number"
-stored in a particular place
-near the beginning of the file that tells the
-.Tn UNIX
-operating system
-that the file is a binary executable, and which of several types thereof.
-The concept of a
-.Dq "magic"
-has been applied by extension to data files.
-Any file with some invariant identifier at a small fixed
-offset into the file can usually be described in this way.
-The information identifying these files is read from the compiled
-magic file
-.Pa __MAGIC__.mgc ,
-or the files in the directory
-.Pa __MAGIC__
-if the compiled file does not exist.
-In addition, if
-.Pa $HOME/.magic.mgc
-or
-.Pa $HOME/.magic
-exists, it will be used in preference to the system magic files.
-.Pp
-If a file does not match any of the entries in the magic file,
-it is examined to see if it seems to be a text file.
-ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
-(such as those used on Macintosh and IBM PC systems),
-UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
-character sets can be distinguished by the different
-ranges and sequences of bytes that constitute printable text
-in each set.
-If a file passes any of these tests, its character set is reported.
-ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
-as
-.Dq text
-because they will be mostly readable on nearly any terminal;
-UTF-16 and EBCDIC are only
-.Dq character data
-because, while
-they contain text, it is text that will require translation
-before it can be read.
-In addition,
-.Nm
-will attempt to determine other characteristics of text-type files.
-If the lines of a file are terminated by CR, CRLF, or NEL, instead
-of the Unix-standard LF, this will be reported.
-Files that contain embedded escape sequences or overstriking
-will also be identified.
-.Pp
-Once
-.Nm
-has determined the character set used in a text-type file,
-it will
-attempt to determine in what language the file is written.
-The language tests look for particular strings (cf.
-.In names.h )
-that can appear anywhere in the first few blocks of a file.
-For example, the keyword
-.Em .br
-indicates that the file is most likely a
-.Xr troff 1
-input file, just as the keyword
-.Em struct
-indicates a C program.
-These tests are less reliable than the previous
-two groups, so they are performed last.
-The language test routines also test for some miscellany
-(such as
-.Xr tar 1
-archives).
-.Pp
-Any file that cannot be identified as having been written
-in any of the character sets listed above is simply said to be
-.Dq data .
-.Sh OPTIONS
-.Bl -tag -width indent
-.It Fl Fl apple
-Causes the file command to output the file type and creator code as
-used by older MacOS versions. The code consists of eight letters,
-the first describing the file type, the latter the creator.
-.It Fl b , Fl Fl brief
-Do not prepend filenames to output lines (brief mode).
-.It Fl C , Fl Fl compile
-Write a
-.Pa magic.mgc
-output file that contains a pre-parsed version of the magic file or directory.
-.It Fl c , Fl Fl checking-printout
-Cause a checking printout of the parsed form of the magic file.
-This is usually used in conjunction with the
-.Fl m
-flag to debug a new magic file before installing it.
-.It Fl E
-On filesystem errors (file not found etc), instead of handling the error
-as regular output as POSIX mandates and keep going, issue an error message
-and exit.
-.It Fl e , Fl Fl exclude Ar testname
-Exclude the test named in
-.Ar testname
-from the list of tests made to determine the file type.
-Valid test names are:
-.Bl -tag -width compress
-.It apptype
-.Dv EMX
-application type (only on EMX).
-.It ascii
-Various types of text files (this test will try to guess the text
-encoding, irrespective of the setting of the
-.Sq encoding
-option).
-.It encoding
-Different text encodings for soft magic tests.
-.It tokens
-Ignored for backwards compatibility.
-.It cdf
-Prints details of Compound Document Files.
-.It compress
-Checks for, and looks inside, compressed files.
-.It elf
-Prints ELF file details.
-.It soft
-Consults magic files.
-.It tar
-Examines tar files.
-.El
-.It Fl F , Fl Fl separator Ar separator
-Use the specified string as the separator between the filename and the
-file result returned.
-Defaults to
-.Sq \&: .
-.It Fl f , Fl Fl files-from Ar namefile
-Read the names of the files to be examined from
-.Ar namefile
-(one per line)
-before the argument list.
-Either
-.Ar namefile
-or at least one filename argument must be present;
-to test the standard input, use
-.Sq -
-as a filename argument.
-Please note that
-.Ar namefile
-is unwrapped and the enclosed filenames are processed when this option is
-encountered and before any further options processing is done.
-This allows one to process multiple lists of files with different command line
-arguments on the same
-.Nm
-invocation.
-Thus if you want to set the delimiter, you need to do it before you specify
-the list of files, like:
-.Dq Fl F Ar @ Fl f Ar namefile ,
-instead of:
-.Dq Fl f Ar namefile Fl F Ar @ .
-.It Fl h , Fl Fl no-dereference
-option causes symlinks not to be followed
-(on systems that support symbolic links).
-This is the default if the environment variable
-.Dv POSIXLY_CORRECT
-is not defined.
-.It Fl i , Fl Fl mime
-Causes the file command to output mime type strings rather than the more
-traditional human readable ones.
-Thus it may say
-.Sq text/plain; charset=us-ascii
-rather than
-.Dq ASCII text .
-.It Fl Fl mime-type , Fl Fl mime-encoding
-Like
-.Fl i ,
-but print only the specified element(s).
-.It Fl k , Fl Fl keep-going
-Don't stop at the first match, keep going.
-Subsequent matches will be
-have the string
-.Sq "\[rs]012\- "
-prepended.
-(If you want a newline, see the
-.Fl r
-option.)
-The magic pattern with the highest strength (see the
-.Fl l
-option) comes first.
-.It Fl l , Fl Fl list
-Shows a list of patterns and their strength sorted descending by
-.Xr magic 4
-strength
-which is used for the matching (see also the
-.Fl k
-option).
-.It Fl L , Fl Fl dereference
-option causes symlinks to be followed, as the like-named option in
-.Xr ls 1
-(on systems that support symbolic links).
-This is the default if the environment variable
-.Ev POSIXLY_CORRECT
-is defined.
-.It Fl m , Fl Fl magic-file Ar magicfiles
-Specify an alternate list of files and directories containing magic.
-This can be a single item, or a colon-separated list.
-If a compiled magic file is found alongside a file or directory,
-it will be used instead.
-.It Fl N , Fl Fl no-pad
-Don't pad filenames so that they align in the output.
-.It Fl n , Fl Fl no-buffer
-Force stdout to be flushed after checking each file.
-This is only useful if checking a list of files.
-It is intended to be used by programs that want filetype output from a pipe.
-.It Fl p , Fl Fl preserve-date
-On systems that support
-.Xr utime 3
-or
-.Xr utimes 2 ,
-attempt to preserve the access time of files analyzed, to pretend that
-.Nm
-never read them.
-.It Fl r , Fl Fl raw
-Don't translate unprintable characters to \eooo.
-Normally
-.Nm
-translates unprintable characters to their octal representation.
-.It Fl s , Fl Fl special-files
-Normally,
-.Nm
-only attempts to read and determine the type of argument files which
-.Xr stat 2
-reports are ordinary files.
-This prevents problems, because reading special files may have peculiar
-consequences.
-Specifying the
-.Fl s
-option causes
-.Nm
-to also read argument files which are block or character special files.
-This is useful for determining the filesystem types of the data in raw
-disk partitions, which are block special files.
-This option also causes
-.Nm
-to disregard the file size as reported by
-.Xr stat 2
-since on some systems it reports a zero size for raw disk partitions.
-.It Fl v , Fl Fl version
-Print the version of the program and exit.
-.It Fl z , Fl Fl uncompress
-Try to look inside compressed files.
-.It Fl 0 , Fl Fl print0
-Output a null character
-.Sq \e0
-after the end of the filename.
-Nice to
-.Xr cut 1
-the output.
-This does not affect the separator, which is still printed.
-.It Fl -help
-Print a help message and exit.
-.El
-.Sh FILES
-.Bl -tag -width __MAGIC__.mgc -compact
-.It Pa __MAGIC__.mgc
-Default compiled list of magic.
-.It Pa __MAGIC__
-Directory containing default magic files.
-.El
-.Sh ENVIRONMENT
-The environment variable
-.Ev MAGIC
-can be used to set the default magic file name.
-If that variable is set, then
-.Nm
-will not attempt to open
-.Pa $HOME/.magic .
-.Nm
-adds
-.Dq Pa .mgc
-to the value of this variable as appropriate.
-However,
-.Pa file
-has to exist in order for
-.Pa file.mime
-to be considered.
-The environment variable
-.Ev POSIXLY_CORRECT
-controls (on systems that support symbolic links), whether
-.Nm
-will attempt to follow symlinks or not.
-If set, then
-.Nm
-follows symlink, otherwise it does not.
-This is also controlled by the
-.Fl L
-and
-.Fl h
-options.
-.Sh SEE ALSO
-.Xr magic __FSECTION__ ,
-.Xr hexdump 1 ,
-.Xr od 1 ,
-.Xr strings 1 ,
-.Sh STANDARDS CONFORMANCE
-This program is believed to exceed the System V Interface Definition
-of FILE(CMD), as near as one can determine from the vague language
-contained therein.
-Its behavior is mostly compatible with the System V program of the same name.
-This version knows more magic, however, so it will produce
-different (albeit more accurate) output in many cases.
-.\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
-.Pp
-The one significant difference
-between this version and System V
-is that this version treats any white space
-as a delimiter, so that spaces in pattern strings must be escaped.
-For example,
-.Bd -literal -offset indent
-\*[Gt]10 string language impress\ (imPRESS data)
-.Ed
-.Pp
-in an existing magic file would have to be changed to
-.Bd -literal -offset indent
-\*[Gt]10 string language\e impress (imPRESS data)
-.Ed
-.Pp
-In addition, in this version, if a pattern string contains a backslash,
-it must be escaped.
-For example
-.Bd -literal -offset indent
-0 string \ebegindata Andrew Toolkit document
-.Ed
-.Pp
-in an existing magic file would have to be changed to
-.Bd -literal -offset indent
-0 string \e\ebegindata Andrew Toolkit document
-.Ed
-.Pp
-SunOS releases 3.2 and later from Sun Microsystems include a
-.Nm
-command derived from the System V one, but with some extensions.
-This version differs from Sun's only in minor ways.
-It includes the extension of the
-.Sq \*[Am]
-operator, used as,
-for example,
-.Bd -literal -offset indent
-\*[Gt]16 long\*[Am]0x7fffffff \*[Gt]0 not stripped
-.Ed
-.Sh MAGIC DIRECTORY
-The magic file entries have been collected from various sources,
-mainly USENET, and contributed by various authors.
-Christos Zoulas (address below) will collect additional
-or corrected magic file entries.
-A consolidation of magic file entries
-will be distributed periodically.
-.Pp
-The order of entries in the magic file is significant.
-Depending on what system you are using, the order that
-they are put together may be incorrect.
-If your old
-.Nm
-command uses a magic file,
-keep the old magic file around for comparison purposes
-(rename it to
-.Pa __MAGIC__.orig ) .
-.Sh EXAMPLES
-.Bd -literal -offset indent
-$ file file.c file /dev/{wd0a,hda}
-file.c: C program text
-file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
- dynamically linked (uses shared libs), stripped
-/dev/wd0a: block special (0/0)
-/dev/hda: block special (3/0)
-
-$ file -s /dev/wd0{b,d}
-/dev/wd0b: data
-/dev/wd0d: x86 boot sector
-
-$ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
-/dev/hda: x86 boot sector
-/dev/hda1: Linux/i386 ext2 filesystem
-/dev/hda2: x86 boot sector
-/dev/hda3: x86 boot sector, extended partition table
-/dev/hda4: Linux/i386 ext2 filesystem
-/dev/hda5: Linux/i386 swap file
-/dev/hda6: Linux/i386 swap file
-/dev/hda7: Linux/i386 swap file
-/dev/hda8: Linux/i386 swap file
-/dev/hda9: empty
-/dev/hda10: empty
-
-$ file -i file.c file /dev/{wd0a,hda}
-file.c: text/x-c
-file: application/x-executable
-/dev/hda: application/x-not-regular-file
-/dev/wd0a: application/x-not-regular-file
-
-.Ed
-.Sh HISTORY
-There has been a
-.Nm
-command in every
-.Dv UNIX since at least Research Version 4
-(man page dated November, 1973).
-The System V version introduced one significant major change:
-the external list of magic types.
-This slowed the program down slightly but made it a lot more flexible.
-.Pp
-This program, based on the System V version,
-was written by Ian Darwin
-.Aq ian at darwinsys.com
-without looking at anybody else's source code.
-.Pp
-John Gilmore revised the code extensively, making it better than
-the first version.
-Geoff Collyer found several inadequacies
-and provided some magic file entries.
-Contributions by the
-.Sq \*[Am]
-operator by Rob McMahon,
-.Aq cudcv at warwick.ac.uk ,
-1989.
-.Pp
-Guy Harris,
-.Aq guy at netapp.com ,
-made many changes from 1993 to the present.
-1989.
-.Pp
-Primary development and maintenance from 1990 to the present by
-Christos Zoulas
-.Aq christos at astron.com .
-.Pp
-Altered by Chris Lowth
-.Aq chris at lowth.com ,
-2000: handle the
-.Fl i
-option to output mime type strings, using an alternative
-magic file and internal logic.
-.Pp
-Altered by Eric Fischer
-.Aq enf at pobox.com ,
-July, 2000,
-to identify character codes and attempt to identify the languages
-of non-ASCII files.
-.Pp
-Altered by Reuben Thomas
-.Aq rrt at sc3d.org ,
-2007-2011, to improve MIME support, merge MIME and non-MIME magic,
-support directories as well as files of magic, apply many bug fixes,
-update and fix a lot of magic, improve the build system, improve the
-documentation, and rewrite the Python bindings in pure Python.
-.Pp
-The list of contributors to the
-.Sq magic
-directory (magic files)
-is too long to include here.
-You know who you are; thank you.
-Many contributors are listed in the source files.
-.Sh LEGAL NOTICE
-Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
-Covered by the standard Berkeley Software Distribution copyright; see the file
-COPYING in the source distribution.
-.Pp
-The files
-.Pa tar.h
-and
-.Pa is_tar.c
-were written by John Gilmore from his public-domain
-.Xr tar 1
-program, and are not covered by the above license.
-.Sh RETURN CODE
-.Nm
-returns 0 on success, and non-zero on error.
-.Sh BUGS
-.Pp
-Please report bugs and send patches to the bug tracker at
-.Pa http://bugs.gw.com/
-or the mailing list at
-.Aq file at mx.gw.com
-(visit
-.Pa http://mx.gw.com/mailman/listinfo/file
-first to subscribe).
-.Sh TODO
-.Pp
-Fix output so that tests for MIME and APPLE flags are not needed all
-over the place, and actual output is only done in one place.
-This needs a design.
-Suggestion: push possible outputs on to a list, then pick the
-last-pushed (most specific, one hopes) value at the end, or
-use a default if the list is empty.
-This should not slow down evaluation.
-.Pp
-Continue to squash all magic bugs.
-See Debian BTS for a good source.
-.Pp
-Store arbitrarily long strings, for example for %s patterns, so that
-they can be printed out.
-Fixes Debian bug #271672.
-Would require more complex store/load code in apprentice.
-.Pp
-Add syntax for relative offsets after current level (Debian bug #466037).
-.Pp
-Make file -ki work, i.e. give multiple MIME types.
-.Pp
-Add a zip library so we can peek inside Office2007 documents to
-figure out what they are.
-.Pp
-Add an option to print URLs for the sources of the file descriptions.
-.Pp
-Combine script searches and add a way to map executable names to MIME
-types (e.g. have a magic value for !:mime which causes the resulting
-string to be looked up in a table).
-This would avoid adding the same magic repeatedly for each new
-hash-bang interpreter.
-.Pp
-Fix
-.Dq name
-and
-.Dq use
-to check for consistency at compile time (duplicate
-.Dq name ,
-.Dq use
-pointing to undefined
-.Dq name
-).
-Make
-.Dq name
-/
-.Dq use
-more efficient by keeping a sorted list of names.
-Special-case ^ to flip endianness in the parser so that it does not
-have to be escaped, and document it.
-.Sh AVAILABILITY
-You can obtain the original author's latest version by anonymous FTP
-on
-.Pa ftp.astron.com
-in the directory
-.Pa /pub/file/file-X.YZ.tar.gz .
Deleted: trunk/contrib/file/file_opts.h
===================================================================
--- trunk/contrib/file/file_opts.h 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/file_opts.h 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,50 +0,0 @@
-/*
- * Table of command-line options
- *
- * The first column specifies the short name, if any, or 0 if none.
- * The second column specifies the long name.
- * The third column specifies whether it takes a parameter.
- * The fourth column is the documentation.
- *
- * N.B. The long options' order must correspond to the code in file.c,
- * and OPTSTRING must be kept up-to-date with the short options.
- * Pay particular attention to the numbers of long-only options in the
- * switch statement!
- */
-
-OPT_LONGONLY("help", 0, " display this help and exit\n")
-OPT('v', "version", 0, " output version information and exit\n")
-OPT('m', "magic-file", 1, " LIST use LIST as a colon-separated list of magic\n"
- " number files\n")
-OPT('z', "uncompress", 0, " try to look inside compressed files\n")
-OPT('b', "brief", 0, " do not prepend filenames to output lines\n")
-OPT('c', "checking-printout", 0, " print the parsed form of the magic file, use in\n"
- " conjunction with -m to debug a new magic file\n"
- " before installing it\n")
-OPT('e', "exclude", 1, " TEST exclude TEST from the list of test to be\n"
- " performed for file. Valid tests are:\n"
- " %o\n")
-OPT('f', "files-from", 1, " FILE read the filenames to be examined from FILE\n")
-OPT('F', "separator", 1, " STRING use string as separator instead of `:'\n")
-OPT('i', "mime", 0, " output MIME type strings (--mime-type and\n"
- " --mime-encoding)\n")
-OPT_LONGONLY("apple", 0, " output the Apple CREATOR/TYPE\n")
-OPT_LONGONLY("mime-type", 0, " output the MIME type\n")
-OPT_LONGONLY("mime-encoding", 0, " output the MIME encoding\n")
-OPT('k', "keep-going", 0, " don't stop at the first match\n")
-OPT('l', "list", 0, " list magic strength\n")
-#ifdef S_IFLNK
-OPT('L', "dereference", 0, " follow symlinks (default)\n")
-OPT('h', "no-dereference", 0, " don't follow symlinks\n")
-#endif
-OPT('n', "no-buffer", 0, " do not buffer output\n")
-OPT('N', "no-pad", 0, " do not pad output\n")
-OPT('0', "print0", 0, " terminate filenames with ASCII NUL\n")
-#if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
-OPT('p', "preserve-date", 0, " preserve access times on files\n")
-#endif
-OPT('r', "raw", 0, " don't translate unprintable chars to \\ooo\n")
-OPT('s', "special-files", 0, " treat special (block/char devices) files as\n"
- " ordinary ones\n")
-OPT('C', "compile", 0, " compile file specified by -m\n")
-OPT('d', "debug", 0, " print debugging messages\n")
Deleted: trunk/contrib/file/fmtcheck.c
===================================================================
--- trunk/contrib/file/fmtcheck.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/fmtcheck.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,234 +0,0 @@
-/* $NetBSD: fmtcheck.c,v 1.8 2008/04/28 20:22:59 martin Exp $ */
-
-/*-
- * Copyright (c) 2000 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code was contributed to The NetBSD Foundation by Allen Briggs.
- *
- * 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"
-
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-
-enum __e_fmtcheck_types {
- FMTCHECK_START,
- FMTCHECK_SHORT,
- FMTCHECK_INT,
- FMTCHECK_LONG,
- FMTCHECK_QUAD,
- FMTCHECK_SHORTPOINTER,
- FMTCHECK_INTPOINTER,
- FMTCHECK_LONGPOINTER,
- FMTCHECK_QUADPOINTER,
- FMTCHECK_DOUBLE,
- FMTCHECK_LONGDOUBLE,
- FMTCHECK_STRING,
- FMTCHECK_WIDTH,
- FMTCHECK_PRECISION,
- FMTCHECK_DONE,
- FMTCHECK_UNKNOWN
-};
-typedef enum __e_fmtcheck_types EFT;
-
-#define RETURN(pf,f,r) do { \
- *(pf) = (f); \
- return r; \
- } /*NOTREACHED*/ /*CONSTCOND*/ while (0)
-
-static EFT
-get_next_format_from_precision(const char **pf)
-{
- int sh, lg, quad, longdouble;
- const char *f;
-
- sh = lg = quad = longdouble = 0;
-
- f = *pf;
- switch (*f) {
- case 'h':
- f++;
- sh = 1;
- break;
- case 'l':
- f++;
- if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
- if (*f == 'l') {
- f++;
- quad = 1;
- } else {
- lg = 1;
- }
- break;
- case 'q':
- f++;
- quad = 1;
- break;
- case 'L':
- f++;
- longdouble = 1;
- break;
- default:
- break;
- }
- if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
- if (strchr("diouxX", *f)) {
- if (longdouble)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- if (lg)
- RETURN(pf,f,FMTCHECK_LONG);
- if (quad)
- RETURN(pf,f,FMTCHECK_QUAD);
- RETURN(pf,f,FMTCHECK_INT);
- }
- if (*f == 'n') {
- if (longdouble)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- if (sh)
- RETURN(pf,f,FMTCHECK_SHORTPOINTER);
- if (lg)
- RETURN(pf,f,FMTCHECK_LONGPOINTER);
- if (quad)
- RETURN(pf,f,FMTCHECK_QUADPOINTER);
- RETURN(pf,f,FMTCHECK_INTPOINTER);
- }
- if (strchr("DOU", *f)) {
- if (sh + lg + quad + longdouble)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- RETURN(pf,f,FMTCHECK_LONG);
- }
- if (strchr("eEfg", *f)) {
- if (longdouble)
- RETURN(pf,f,FMTCHECK_LONGDOUBLE);
- if (sh + lg + quad)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- RETURN(pf,f,FMTCHECK_DOUBLE);
- }
- if (*f == 'c') {
- if (sh + lg + quad + longdouble)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- RETURN(pf,f,FMTCHECK_INT);
- }
- if (*f == 's') {
- if (sh + lg + quad + longdouble)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- RETURN(pf,f,FMTCHECK_STRING);
- }
- if (*f == 'p') {
- if (sh + lg + quad + longdouble)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- RETURN(pf,f,FMTCHECK_LONG);
- }
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- /*NOTREACHED*/
-}
-
-static EFT
-get_next_format_from_width(const char **pf)
-{
- const char *f;
-
- f = *pf;
- if (*f == '.') {
- f++;
- if (*f == '*') {
- RETURN(pf,f,FMTCHECK_PRECISION);
- }
- /* eat any precision (empty is allowed) */
- while (isdigit((unsigned char)*f)) f++;
- if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
- }
- RETURN(pf,f,get_next_format_from_precision(pf));
- /*NOTREACHED*/
-}
-
-static EFT
-get_next_format(const char **pf, EFT eft)
-{
- int infmt;
- const char *f;
-
- if (eft == FMTCHECK_WIDTH) {
- (*pf)++;
- return get_next_format_from_width(pf);
- } else if (eft == FMTCHECK_PRECISION) {
- (*pf)++;
- return get_next_format_from_precision(pf);
- }
-
- f = *pf;
- infmt = 0;
- while (!infmt) {
- f = strchr(f, '%');
- if (f == NULL)
- RETURN(pf,f,FMTCHECK_DONE);
- f++;
- if (!*f)
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- if (*f != '%')
- infmt = 1;
- else
- f++;
- }
-
- /* Eat any of the flags */
- while (*f && (strchr("#0- +", *f)))
- f++;
-
- if (*f == '*') {
- RETURN(pf,f,FMTCHECK_WIDTH);
- }
- /* eat any width */
- while (isdigit((unsigned char)*f)) f++;
- if (!*f) {
- RETURN(pf,f,FMTCHECK_UNKNOWN);
- }
-
- RETURN(pf,f,get_next_format_from_width(pf));
- /*NOTREACHED*/
-}
-
-const char *
-fmtcheck(const char *f1, const char *f2)
-{
- const char *f1p, *f2p;
- EFT f1t, f2t;
-
- if (!f1) return f2;
-
- f1p = f1;
- f1t = FMTCHECK_START;
- f2p = f2;
- f2t = FMTCHECK_START;
- while ((f1t = get_next_format(&f1p, f1t)) != FMTCHECK_DONE) {
- if (f1t == FMTCHECK_UNKNOWN)
- return f2;
- f2t = get_next_format(&f2p, f2t);
- if (f1t != f2t)
- return f2;
- }
- return f1;
-}
Deleted: trunk/contrib/file/fsmagic.c
===================================================================
--- trunk/contrib/file/fsmagic.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/fsmagic.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,407 +0,0 @@
-/*
- * 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.73 2014/05/14 23:15:42 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
-#ifdef WIN32
-# define WIN32_LEAN_AND_MEAN
-# include <windows.h>
-#endif
-
-#ifndef HAVE_MAJOR
-# define major(dev) (((dev) >> 8) & 0xff)
-# define minor(dev) ((dev) & 0xff)
-#endif
-#undef HAVE_MAJOR
-#ifdef S_IFLNK
-private int
-bad_link(struct magic_set *ms, int err, char *buf)
-{
- int mime = ms->flags & MAGIC_MIME;
- if ((mime & MAGIC_MIME_TYPE) &&
- file_printf(ms, "inode/symlink")
- == -1)
- return -1;
- else if (!mime) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, err,
- "broken symbolic link to `%s'", buf);
- return -1;
- }
- if (file_printf(ms, "broken symbolic link to `%s'", buf) == -1)
- return -1;
- }
- return 1;
-}
-#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)
-{
- int ret, did = 0;
- int mime = ms->flags & MAGIC_MIME;
-#ifdef S_IFLNK
- char buf[BUFSIZ+4];
- ssize_t nch;
- struct stat tstatbuf;
-#endif
-
- if (ms->flags & MAGIC_APPLE)
- return 0;
- if (fn == NULL)
- return 0;
-
-#define COMMA (did++ ? ", " : "")
- /*
- * Fstat is cheaper but fails for files you don't have read perms on.
- * On 4.2BSD and similar systems, use lstat() to identify symlinks.
- */
-#ifdef S_IFLNK
- if ((ms->flags & MAGIC_SYMLINK) == 0)
- ret = lstat(fn, sb);
- else
-#endif
- ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
-
-#ifdef WIN32
- {
- HANDLE hFile = CreateFile(fn, 0, FILE_SHARE_DELETE |
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
- NULL);
- if (hFile != INVALID_HANDLE_VALUE) {
- /*
- * Stat failed, but we can still open it - assume it's
- * a block device, if nothing else.
- */
- if (ret) {
- sb->st_mode = S_IFBLK;
- ret = 0;
- }
- switch (GetFileType(hFile)) {
- case FILE_TYPE_CHAR:
- sb->st_mode |= S_IFCHR;
- sb->st_mode &= ~S_IFREG;
- break;
- case FILE_TYPE_PIPE:
- sb->st_mode |= S_IFIFO;
- sb->st_mode &= ~S_IFREG;
- break;
- }
- CloseHandle(hFile);
- }
- }
-#endif
-
- if (ret) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, errno, "cannot stat `%s'", fn);
- return -1;
- }
- if (file_printf(ms, "cannot open `%s' (%s)",
- fn, strerror(errno)) == -1)
- return -1;
- return 0;
- }
-
- ret = 1;
- if (!mime) {
-#ifdef S_ISUID
- if (sb->st_mode & S_ISUID)
- if (file_printf(ms, "%ssetuid", COMMA) == -1)
- return -1;
-#endif
-#ifdef S_ISGID
- if (sb->st_mode & S_ISGID)
- if (file_printf(ms, "%ssetgid", COMMA) == -1)
- return -1;
-#endif
-#ifdef S_ISVTX
- if (sb->st_mode & S_ISVTX)
- if (file_printf(ms, "%ssticky", COMMA) == -1)
- return -1;
-#endif
- }
-
- switch (sb->st_mode & S_IFMT) {
- case S_IFDIR:
- if (mime) {
- if (handle_mime(ms, mime, "directory") == -1)
- return -1;
- } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
- return -1;
- break;
-#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) {
- ret = 0;
- break;
- }
- if (mime) {
- if (handle_mime(ms, mime, "chardevice") == -1)
- return -1;
- } else {
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
- if (file_printf(ms, "%scharacter special (%d/%d/%d)",
- COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
- dv_subunit(sb->st_rdev)) == -1)
- return -1;
-# else
- if (file_printf(ms, "%scharacter special (%ld/%ld)",
- COMMA, (long)major(sb->st_rdev),
- (long)minor(sb->st_rdev)) == -1)
- return -1;
-# endif
-#else
- if (file_printf(ms, "%scharacter special", COMMA) == -1)
- return -1;
-#endif
- }
- break;
-#endif
-#ifdef S_IFBLK
- case S_IFBLK:
- /*
- * If -s has been specified, treat block 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) {
- ret = 0;
- break;
- }
- if (mime) {
- if (handle_mime(ms, mime, "blockdevice") == -1)
- return -1;
- } else {
-#ifdef HAVE_STRUCT_STAT_ST_RDEV
-# ifdef dv_unit
- if (file_printf(ms, "%sblock special (%d/%d/%d)",
- COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
- dv_subunit(sb->st_rdev)) == -1)
- return -1;
-# else
- if (file_printf(ms, "%sblock special (%ld/%ld)",
- COMMA, (long)major(sb->st_rdev),
- (long)minor(sb->st_rdev)) == -1)
- return -1;
-# endif
-#else
- if (file_printf(ms, "%sblock special", COMMA) == -1)
- return -1;
-#endif
- }
- break;
-#endif
- /* TODO add code to handle V7 MUX and Blit MUX files */
-#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, "%sfifo (named pipe)", COMMA) == -1)
- return -1;
- break;
-#endif
-#ifdef S_IFDOOR
- case S_IFDOOR:
- if (mime) {
- if (handle_mime(ms, mime, "door") == -1)
- return -1;
- } else if (file_printf(ms, "%sdoor", COMMA) == -1)
- return -1;
- break;
-#endif
-#ifdef S_IFLNK
- case S_IFLNK:
- if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, errno, "unreadable symlink `%s'",
- fn);
- return -1;
- }
- if (mime) {
- if (handle_mime(ms, mime, "symlink") == -1)
- return -1;
- } else if (file_printf(ms,
- "%sunreadable symlink `%s' (%s)", COMMA, fn,
- strerror(errno)) == -1)
- return -1;
- break;
- }
- buf[nch] = '\0'; /* readlink(2) does not do this */
-
- /* If broken symlink, say so and quit early. */
- if (*buf == '/') {
- if (stat(buf, &tstatbuf) < 0)
- return bad_link(ms, errno, buf);
- } else {
- char *tmp;
- char buf2[BUFSIZ+BUFSIZ+4];
-
- if ((tmp = strrchr(fn, '/')) == NULL) {
- tmp = buf; /* in current directory anyway */
- } else {
- if (tmp - fn + 1 > BUFSIZ) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, 0,
- "path too long: `%s'", buf);
- return -1;
- }
- if (mime) {
- if (handle_mime(ms, mime,
- "x-path-too-long") == -1)
- return -1;
- } else if (file_printf(ms,
- "%spath too long: `%s'", COMMA,
- fn) == -1)
- return -1;
- break;
- }
- /* take dir part */
- (void)strlcpy(buf2, fn, sizeof buf2);
- buf2[tmp - fn + 1] = '\0';
- /* plus (rel) link */
- (void)strlcat(buf2, buf, sizeof buf2);
- tmp = buf2;
- }
- if (stat(tmp, &tstatbuf) < 0)
- return bad_link(ms, errno, buf);
- }
-
- /* Otherwise, handle it. */
- if ((ms->flags & MAGIC_SYMLINK) != 0) {
- const char *p;
- ms->flags &= MAGIC_SYMLINK;
- p = magic_file(ms, buf);
- ms->flags |= MAGIC_SYMLINK;
- if (p == NULL)
- return -1;
- } else { /* just print what it points to */
- if (mime) {
- if (handle_mime(ms, mime, "symlink") == -1)
- return -1;
- } else if (file_printf(ms, "%ssymbolic link to `%s'",
- COMMA, buf) == -1)
- return -1;
- }
- break;
-#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, "%ssocket", COMMA) == -1)
- return -1;
- break;
-#endif
-#endif
- case S_IFREG:
- /*
- * 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, "%sempty", COMMA) == -1)
- return -1;
- break;
- }
- ret = 0;
- break;
-
- default:
- file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
- return -1;
- /*NOTREACHED*/
- }
-
- if (!mime && did && ret == 0) {
- if (file_printf(ms, " ") == -1)
- return -1;
- }
- return ret;
-}
Deleted: trunk/contrib/file/funcs.c
===================================================================
--- trunk/contrib/file/funcs.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/funcs.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,493 +0,0 @@
-/*
- * 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.72 2014/05/14 23:15:42 christos Exp $")
-#endif /* lint */
-
-#include "magic.h"
-#include <assert.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
-#if defined(HAVE_LIMITS_H)
-#include <limits.h>
-#endif
-#if defined(HAVE_LOCALE_H)
-#include <locale.h>
-#endif
-
-#ifndef SIZE_MAX
-#define SIZE_MAX ((size_t)~0)
-#endif
-
-/*
- * Like printf, only we append to a buffer.
- */
-protected int
-file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
-{
- int len;
- char *buf, *newstr;
-
- if (ms->event_flags & EVENT_HAD_ERR)
- return 0;
- len = vasprintf(&buf, fmt, ap);
- if (len < 0)
- goto out;
-
- if (ms->o.buf != NULL) {
- len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
- free(buf);
- if (len < 0)
- goto out;
- free(ms->o.buf);
- buf = newstr;
- }
- ms->o.buf = buf;
- return 0;
-out:
- file_error(ms, errno, "vasprintf failed");
- return -1;
-}
-
-protected int
-file_printf(struct magic_set *ms, const char *fmt, ...)
-{
- int rv;
- va_list ap;
-
- va_start(ap, fmt);
- rv = file_vprintf(ms, fmt, ap);
- va_end(ap);
- return rv;
-}
-
-/*
- * error - print best error message possible
- */
-/*VARARGS*/
-__attribute__((__format__(__printf__, 3, 0)))
-private void
-file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
- size_t lineno)
-{
- /* Only the first error is ok */
- if (ms->event_flags & EVENT_HAD_ERR)
- return;
- if (lineno != 0) {
- free(ms->o.buf);
- ms->o.buf = NULL;
- file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno);
- }
- file_vprintf(ms, f, va);
- if (error > 0)
- file_printf(ms, " (%s)", strerror(error));
- 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");
-}
-
-#ifndef COMPILE_ONLY
-protected int
-file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unused)),
- 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 = "application/octet-stream";
- const char *def = "data";
- const char *ftype = NULL;
-
- if (nb == 0) {
- def = "empty";
- type = "application/x-empty";
- goto simple;
- } else if (nb == 1) {
- def = "very short file (no magic)";
- goto simple;
- }
-
- if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
- looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
- &code, &code_mime, &ftype);
- }
-
-#ifdef __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 HAVE_FORK
- /* try compression stuff */
- if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
- if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) {
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void)fprintf(stderr, "zmagic %d\n", m);
- goto done_encoding;
- }
-#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)
- 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, 0, 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;
- }
- }
-
-simple:
- /* give up */
- m = 1;
- if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
- file_printf(ms, "%s", mime ? type : def) == -1) {
- rv = -1;
- }
- done:
- if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
- if (ms->flags & MAGIC_MIME_TYPE)
- if (file_printf(ms, "; charset=") == -1)
- rv = -1;
- if (file_printf(ms, "%s", code_mime) == -1)
- rv = -1;
- }
-#if HAVE_FORK
- done_encoding:
-#endif
- free(u8buf);
- if (rv)
- return rv;
-
- return m;
-}
-#endif
-
-protected int
-file_reset(struct magic_set *ms)
-{
- if (ms->mlist[0] == NULL) {
- file_error(ms, 0, "no magic files loaded");
- return -1;
- }
- if (ms->o.buf) {
- free(ms->o.buf);
- ms->o.buf = NULL;
- }
- if (ms->o.pbuf) {
- free(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) {
- file_oomem(ms, len);
- return NULL;
- }
- psize = len * 4 + 1;
- if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
- file_oomem(ms, psize);
- return NULL;
- }
- 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 = CAST(struct level_info *, (ms->c.li == NULL) ?
- malloc(len) :
- realloc(ms->c.li, len));
- if (ms->c.li == NULL) {
- file_oomem(ms, len);
- return -1;
- }
- }
- 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)
-{
- file_regex_t rx;
- int rc, rv = -1;
-
- rc = file_regcomp(&rx, pat, REG_EXTENDED);
- if (rc) {
- file_regerror(&rx, rc, ms);
- } else {
- regmatch_t rm;
- int nm = 0;
- while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
- ms->o.buf[rm.rm_so] = '\0';
- if (file_printf(ms, "%s%s", rep,
- rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
- goto out;
- nm++;
- }
- rv = nm;
- }
-out:
- file_regfree(&rx);
- return rv;
-}
-
-protected int
-file_regcomp(file_regex_t *rx, const char *pat, int flags)
-{
- rx->old_lc_ctype = setlocale(LC_CTYPE, NULL);
- assert(rx->old_lc_ctype != NULL);
- rx->old_lc_ctype = strdup(rx->old_lc_ctype);
- assert(rx->old_lc_ctype != NULL);
- rx->pat = pat;
-
- (void)setlocale(LC_CTYPE, "C");
- return rx->rc = regcomp(&rx->rx, pat, flags);
-}
-
-protected int
-file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
- regmatch_t* pmatch, int eflags)
-{
- assert(rx->rc == 0);
- return regexec(&rx->rx, str, nmatch, pmatch, eflags);
-}
-
-protected void
-file_regfree(file_regex_t *rx)
-{
- if (rx->rc == 0)
- regfree(&rx->rx);
- (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
- free(rx->old_lc_ctype);
-}
-
-protected void
-file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
-{
- char errmsg[512];
-
- (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
- file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
- errmsg);
-}
Deleted: trunk/contrib/file/getline.c
===================================================================
--- trunk/contrib/file/getline.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/getline.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,100 +0,0 @@
-/* $NetBSD: fgetln.c,v 1.9 2008/04/29 06:53:03 martin Exp $ */
-
-/*-
- * Copyright (c) 2011 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Christos Zoulas.
- *
- * 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"
-#if !HAVE_GETLINE
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-
-public ssize_t
-getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
-{
- char *ptr, *eptr;
-
-
- if (*buf == NULL || *bufsiz == 0) {
- *bufsiz = BUFSIZ;
- if ((*buf = malloc(*bufsiz)) == NULL)
- return -1;
- }
-
- for (ptr = *buf, eptr = *buf + *bufsiz;;) {
- int c = fgetc(fp);
- if (c == -1) {
- if (feof(fp))
- return ptr == *buf ? -1 : ptr - *buf;
- else
- return -1;
- }
- *ptr++ = c;
- if (c == delimiter) {
- *ptr = '\0';
- return ptr - *buf;
- }
- if (ptr + 2 >= eptr) {
- char *nbuf;
- size_t nbufsiz = *bufsiz * 2;
- ssize_t d = ptr - *buf;
- if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
- return -1;
- *buf = nbuf;
- *bufsiz = nbufsiz;
- eptr = nbuf + nbufsiz;
- ptr = nbuf + d;
- }
- }
-}
-
-ssize_t
-getline(char **buf, size_t *bufsiz, FILE *fp)
-{
- return getdelim(buf, bufsiz, '\n', fp);
-}
-
-#endif
-
-#ifdef TEST
-int
-main(int argc, char *argv[])
-{
- char *p = NULL;
- ssize_t len;
- size_t n = 0;
-
- while ((len = getline(&p, &n, stdin)) != -1)
- (void)printf("%zd %s", len, p);
- free(p);
- return 0;
-}
-#endif
Deleted: trunk/contrib/file/getopt_long.c
===================================================================
--- trunk/contrib/file/getopt_long.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/getopt_long.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,498 +0,0 @@
-/* $NetBSD: getopt_long.c,v 1.21.4.1 2008/01/09 01:34:14 matt Exp $ */
-
-/*-
- * Copyright (c) 2000 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Dieter Baron and Thomas Klausner.
- *
- * 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: getopt_long.c,v 1.6 2009/02/13 18:48:05 christos Exp $")
-#endif /* lint */
-
-#include <assert.h>
-#ifdef HAVE_ERR_H
-#include <err.h>
-#else
-#define warnx printf
-#endif
-#include <errno.h>
-#if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
-#include <getopt.h>
-#else
-#include "mygetopt.h"
-#endif
-#include <stdlib.h>
-#include <string.h>
-
-#define REPLACE_GETOPT
-
-#ifndef _DIAGASSERT
-#define _DIAGASSERT assert
-#endif
-
-#ifdef REPLACE_GETOPT
-#ifdef __weak_alias
-__weak_alias(getopt,_getopt)
-#endif
-int opterr = 1; /* if error message should be printed */
-int optind = 1; /* index into parent argv vector */
-int optopt = '?'; /* character checked for validity */
-int optreset; /* reset getopt */
-char *optarg; /* argument associated with option */
-#elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
-static int optreset;
-#endif
-
-#ifdef __weak_alias
-__weak_alias(getopt_long,_getopt_long)
-#endif
-
-#define IGNORE_FIRST (*options == '-' || *options == '+')
-#define PRINT_ERROR ((opterr) && ((*options != ':') \
- || (IGNORE_FIRST && options[1] != ':')))
-#define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
-#define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
-/* XXX: GNU ignores PC if *options == '-' */
-#define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
-
-/* return values */
-#define BADCH (int)'?'
-#define BADARG ((IGNORE_FIRST && options[1] == ':') \
- || (*options == ':') ? (int)':' : (int)'?')
-#define INORDER (int)1
-
-#define EMSG ""
-
-static int getopt_internal(int, char **, const char *);
-static int gcd(int, int);
-static void permute_args(int, int, int, char **);
-
-static const char *place = EMSG; /* option letter processing */
-
-/* XXX: set optreset to 1 rather than these two */
-static int nonopt_start = -1; /* first non option argument (for permute) */
-static int nonopt_end = -1; /* first option after non options (for permute) */
-
-/* Error messages */
-static const char recargchar[] = "option requires an argument -- %c";
-static const char recargstring[] = "option requires an argument -- %s";
-static const char ambig[] = "ambiguous option -- %.*s";
-static const char noarg[] = "option doesn't take an argument -- %.*s";
-static const char illoptchar[] = "unknown option -- %c";
-static const char illoptstring[] = "unknown option -- %s";
-
-
-/*
- * Compute the greatest common divisor of a and b.
- */
-static int
-gcd(a, b)
- int a;
- int b;
-{
- int c;
-
- c = a % b;
- while (c != 0) {
- a = b;
- b = c;
- c = a % b;
- }
-
- return b;
-}
-
-/*
- * Exchange the block from nonopt_start to nonopt_end with the block
- * from nonopt_end to opt_end (keeping the same order of arguments
- * in each block).
- */
-static void
-permute_args(panonopt_start, panonopt_end, opt_end, nargv)
- int panonopt_start;
- int panonopt_end;
- int opt_end;
- char **nargv;
-{
- int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
- char *swap;
-
- _DIAGASSERT(nargv != NULL);
-
- /*
- * compute lengths of blocks and number and size of cycles
- */
- nnonopts = panonopt_end - panonopt_start;
- nopts = opt_end - panonopt_end;
- ncycle = gcd(nnonopts, nopts);
- cyclelen = (opt_end - panonopt_start) / ncycle;
-
- for (i = 0; i < ncycle; i++) {
- cstart = panonopt_end+i;
- pos = cstart;
- for (j = 0; j < cyclelen; j++) {
- if (pos >= panonopt_end)
- pos -= nnonopts;
- else
- pos += nopts;
- swap = nargv[pos];
- nargv[pos] = nargv[cstart];
- nargv[cstart] = swap;
- }
- }
-}
-
-/*
- * getopt_internal --
- * Parse argc/argv argument vector. Called by user level routines.
- * Returns -2 if -- is found (can be long option or end of options marker).
- */
-static int
-getopt_internal(nargc, nargv, options)
- int nargc;
- char **nargv;
- const char *options;
-{
- char *oli; /* option letter list index */
- int optchar;
-
- _DIAGASSERT(nargv != NULL);
- _DIAGASSERT(options != NULL);
-
- optarg = NULL;
-
- /*
- * XXX Some programs (like rsyncd) expect to be able to
- * XXX re-initialize optind to 0 and have getopt_long(3)
- * XXX properly function again. Work around this braindamage.
- */
- if (optind == 0)
- optind = 1;
-
- if (optreset)
- nonopt_start = nonopt_end = -1;
-start:
- if (optreset || !*place) { /* update scanning pointer */
- optreset = 0;
- if (optind >= nargc) { /* end of argument vector */
- place = EMSG;
- if (nonopt_end != -1) {
- /* do permutation, if we have to */
- permute_args(nonopt_start, nonopt_end,
- optind, nargv);
- optind -= nonopt_end - nonopt_start;
- }
- else if (nonopt_start != -1) {
- /*
- * If we skipped non-options, set optind
- * to the first of them.
- */
- optind = nonopt_start;
- }
- nonopt_start = nonopt_end = -1;
- return -1;
- }
- if ((*(place = nargv[optind]) != '-')
- || (place[1] == '\0')) { /* found non-option */
- place = EMSG;
- if (IN_ORDER) {
- /*
- * GNU extension:
- * return non-option as argument to option 1
- */
- optarg = nargv[optind++];
- return INORDER;
- }
- if (!PERMUTE) {
- /*
- * if no permutation wanted, stop parsing
- * at first non-option
- */
- return -1;
- }
- /* do permutation */
- if (nonopt_start == -1)
- nonopt_start = optind;
- else if (nonopt_end != -1) {
- permute_args(nonopt_start, nonopt_end,
- optind, nargv);
- nonopt_start = optind -
- (nonopt_end - nonopt_start);
- nonopt_end = -1;
- }
- optind++;
- /* process next argument */
- goto start;
- }
- if (nonopt_start != -1 && nonopt_end == -1)
- nonopt_end = optind;
- if (place[1] && *++place == '-') { /* found "--" */
- place++;
- return -2;
- }
- }
- if ((optchar = (int)*place++) == (int)':' ||
- (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
- /* option letter unknown or ':' */
- if (!*place)
- ++optind;
- if (PRINT_ERROR)
- warnx(illoptchar, optchar);
- optopt = optchar;
- return BADCH;
- }
- if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
- /* XXX: what if no long options provided (called by getopt)? */
- if (*place)
- return -2;
-
- if (++optind >= nargc) { /* no arg */
- place = EMSG;
- if (PRINT_ERROR)
- warnx(recargchar, optchar);
- optopt = optchar;
- return BADARG;
- } else /* white space */
- place = nargv[optind];
- /*
- * Handle -W arg the same as --arg (which causes getopt to
- * stop parsing).
- */
- return -2;
- }
- if (*++oli != ':') { /* doesn't take argument */
- if (!*place)
- ++optind;
- } else { /* takes (optional) argument */
- optarg = NULL;
- if (*place) /* no white space */
- optarg = (char *)place;
- /* XXX: disable test for :: if PC? (GNU doesn't) */
- else if (oli[1] != ':') { /* arg not optional */
- if (++optind >= nargc) { /* no arg */
- place = EMSG;
- if (PRINT_ERROR)
- warnx(recargchar, optchar);
- optopt = optchar;
- return BADARG;
- } else
- optarg = nargv[optind];
- }
- place = EMSG;
- ++optind;
- }
- /* dump back option letter */
- return optchar;
-}
-
-#ifdef REPLACE_GETOPT
-/*
- * getopt --
- * Parse argc/argv argument vector.
- *
- * [eventually this will replace the real getopt]
- */
-int
-getopt(nargc, nargv, options)
- int nargc;
- char * const *nargv;
- const char *options;
-{
- int retval;
-
- _DIAGASSERT(nargv != NULL);
- _DIAGASSERT(options != NULL);
-
- retval = getopt_internal(nargc, (char **)nargv, options);
- if (retval == -2) {
- ++optind;
- /*
- * We found an option (--), so if we skipped non-options,
- * we have to permute.
- */
- if (nonopt_end != -1) {
- permute_args(nonopt_start, nonopt_end, optind,
- (char **)nargv);
- optind -= nonopt_end - nonopt_start;
- }
- nonopt_start = nonopt_end = -1;
- retval = -1;
- }
- return retval;
-}
-#endif
-
-/*
- * getopt_long --
- * Parse argc/argv argument vector.
- */
-int
-getopt_long(nargc, nargv, options, long_options, idx)
- int nargc;
- char * const *nargv;
- const char *options;
- const struct option *long_options;
- int *idx;
-{
- int retval;
-
-#define IDENTICAL_INTERPRETATION(_x, _y) \
- (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
- long_options[(_x)].flag == long_options[(_y)].flag && \
- long_options[(_x)].val == long_options[(_y)].val)
-
- _DIAGASSERT(nargv != NULL);
- _DIAGASSERT(options != NULL);
- _DIAGASSERT(long_options != NULL);
- /* idx may be NULL */
-
- retval = getopt_internal(nargc, (char **)nargv, options);
- if (retval == -2) {
- char *current_argv, *has_equal;
- size_t current_argv_len;
- int i, ambiguous, match;
-
- current_argv = (char *)place;
- match = -1;
- ambiguous = 0;
-
- optind++;
- place = EMSG;
-
- if (*current_argv == '\0') { /* found "--" */
- /*
- * We found an option (--), so if we skipped
- * non-options, we have to permute.
- */
- if (nonopt_end != -1) {
- permute_args(nonopt_start, nonopt_end,
- optind, (char **)nargv);
- optind -= nonopt_end - nonopt_start;
- }
- nonopt_start = nonopt_end = -1;
- return -1;
- }
- if ((has_equal = strchr(current_argv, '=')) != NULL) {
- /* argument found (--option=arg) */
- current_argv_len = has_equal - current_argv;
- has_equal++;
- } else
- current_argv_len = strlen(current_argv);
-
- for (i = 0; long_options[i].name; i++) {
- /* find matching long option */
- if (strncmp(current_argv, long_options[i].name,
- current_argv_len))
- continue;
-
- if (strlen(long_options[i].name) ==
- (unsigned)current_argv_len) {
- /* exact match */
- match = i;
- ambiguous = 0;
- break;
- }
- if (match == -1) /* partial match */
- match = i;
- else if (!IDENTICAL_INTERPRETATION(i, match))
- ambiguous = 1;
- }
- if (ambiguous) {
- /* ambiguous abbreviation */
- if (PRINT_ERROR)
- warnx(ambig, (int)current_argv_len,
- current_argv);
- optopt = 0;
- return BADCH;
- }
- if (match != -1) { /* option found */
- if (long_options[match].has_arg == no_argument
- && has_equal) {
- if (PRINT_ERROR)
- warnx(noarg, (int)current_argv_len,
- current_argv);
- /*
- * XXX: GNU sets optopt to val regardless of
- * flag
- */
- if (long_options[match].flag == NULL)
- optopt = long_options[match].val;
- else
- optopt = 0;
- return BADARG;
- }
- if (long_options[match].has_arg == required_argument ||
- long_options[match].has_arg == optional_argument) {
- if (has_equal)
- optarg = has_equal;
- else if (long_options[match].has_arg ==
- required_argument) {
- /*
- * optional argument doesn't use
- * next nargv
- */
- optarg = nargv[optind++];
- }
- }
- if ((long_options[match].has_arg == required_argument)
- && (optarg == NULL)) {
- /*
- * Missing argument; leading ':'
- * indicates no error should be generated
- */
- if (PRINT_ERROR)
- warnx(recargstring, current_argv);
- /*
- * XXX: GNU sets optopt to val regardless
- * of flag
- */
- if (long_options[match].flag == NULL)
- optopt = long_options[match].val;
- else
- optopt = 0;
- --optind;
- return BADARG;
- }
- } else { /* unknown option */
- if (PRINT_ERROR)
- warnx(illoptstring, current_argv);
- optopt = 0;
- return BADCH;
- }
- if (long_options[match].flag) {
- *long_options[match].flag = long_options[match].val;
- retval = 0;
- } else
- retval = long_options[match].val;
- if (idx)
- *idx = match;
- }
- return retval;
-#undef IDENTICAL_INTERPRETATION
-}
Deleted: trunk/contrib/file/is_tar.c
===================================================================
--- trunk/contrib/file/is_tar.c 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/is_tar.c 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,152 +0,0 @@
-/*
- * 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;
-}
Deleted: trunk/contrib/file/libmagic.man
===================================================================
--- trunk/contrib/file/libmagic.man 2018-06-19 19:02:13 UTC (rev 11114)
+++ trunk/contrib/file/libmagic.man 2018-06-19 19:06:14 UTC (rev 11115)
@@ -1,320 +0,0 @@
-.\" $File: libmagic.man,v 1.28 2014/03/02 14:47:16 christos Exp $
-.\"
-.\" 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.
-.\"
-.Dd January 6, 2012
-.Dt LIBMAGIC 3
-.Os
-.Sh NAME
-.Nm magic_open ,
-.Nm magic_close ,
-.Nm magic_error ,
-.Nm magic_errno ,
-.Nm magic_descriptor ,
-.Nm magic_buffer ,
-.Nm magic_setflags ,
-.Nm magic_check ,
-.Nm magic_compile ,
-.Nm magic_list ,
-.Nm magic_load ,
-.Nm magic_version
-.Nd Magic number recognition library
-.Sh LIBRARY
-.Lb libmagic
-.Sh SYNOPSIS
-.In magic.h
-.Ft magic_t
-.Fn magic_open "int flags"
-.Ft void
-.Fn magic_close "magic_t cookie"
-.Ft const char *
-.Fn magic_error "magic_t cookie"
-.Ft int
-.Fn magic_errno "magic_t cookie"
-.Ft const char *
-.Fn magic_descriptor "magic_t cookie" "int fd"
-.Ft const char *
-.Fn magic_file "magic_t cookie" "const char *filename"
-.Ft const char *
-.Fn magic_buffer "magic_t cookie" "const void *buffer" "size_t length"
-.Ft int
-.Fn magic_setflags "magic_t cookie" "int flags"
-.Ft int
-.Fn magic_check "magic_t cookie" "const char *filename"
-.Ft int
-.Fn magic_compile "magic_t cookie" "const char *filename"
-.Ft int
-.Fn magic_list "magic_t cookie" "const char *filename"
-.Ft int
-.Fn magic_load "magic_t cookie" "const char *filename"
-.Ft int
-.Fn magic_version "void"
-.Sh DESCRIPTION
-These functions
-operate on the magic database file
-which is described
-in
-.Xr magic __FSECTION__ .
-.Pp
-The function
-.Fn magic_open
-creates a magic cookie pointer and returns it.
-It returns
-.Dv NULL
-if there was an error allocating the magic cookie.
-The
-.Ar flags
-argument specifies how the other magic functions should behave:
-.Bl -tag -width MAGIC_COMPRESS
-.It Dv MAGIC_NONE
-No special handling.
-.It Dv MAGIC_DEBUG
-Print debugging messages to stderr.
-.It Dv MAGIC_SYMLINK
-If the file queried is a symlink, follow it.
-.It Dv MAGIC_COMPRESS
-If the file is compressed, unpack it and look at the contents.
-.It Dv MAGIC_DEVICES
-If the file is a block or character special device, then open the device
-and try to look in its contents.
-.It Dv MAGIC_MIME_TYPE
-Return a MIME type string, instead of a textual description.
-.It Dv MAGIC_MIME_ENCODING
-Return a MIME encoding, instead of a textual description.
-.It Dv MAGIC_MIME
-A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
-.It Dv MAGIC_CONTINUE
-Return all matches, not just the first.
-.It Dv MAGIC_CHECK
-Check the magic database for consistency and print warnings to stderr.
-.It Dv MAGIC_PRESERVE_ATIME
-On systems that support
-.Xr utime 3
-or
-.Xr utimes 2 ,
-attempt to preserve the access time of files analysed.
-.It Dv MAGIC_RAW
-Don't translate unprintable characters to a \eooo octal representation.
-.It Dv MAGIC_ERROR
-Treat operating system errors while trying to open files and follow symlinks
-as real errors, instead of printing them in the magic buffer.
-.It Dv MAGIC_APPLE
-Return the Apple creator and type.
-.It Dv MAGIC_NO_CHECK_APPTYPE
-Don't check for
-.Dv EMX
-application type (only on EMX).
-.It Dv MAGIC_NO_CHECK_CDF
-Don't get extra information on MS Composite Document Files.
-.It Dv MAGIC_NO_CHECK_COMPRESS
-Don't look inside compressed files.
-.It Dv MAGIC_NO_CHECK_ELF
-Don't print ELF details.
-.It Dv MAGIC_NO_CHECK_ENCODING
-Don't check text encodings.
-.It Dv MAGIC_NO_CHECK_SOFT
-Don't consult magic files.
-.It Dv MAGIC_NO_CHECK_TAR
-Don't examine tar files.
-.It Dv MAGIC_NO_CHECK_TEXT
-Don't check for various types of text files.
-.It Dv MAGIC_NO_CHECK_TOKENS
-Don't look for known tokens inside ascii files.
-.El
-.Pp
-The
-.Fn magic_close
-function closes the
-.Xr magic __FSECTION__
-database and deallocates any resources used.
-.Pp
-The
-.Fn magic_error
-function returns a textual explanation of the last error, or
-.Dv NULL
-if there was no error.
-.Pp
-The
-.Fn magic_errno
-function returns the last operating system error number
-.Pq Xr errno 2
-that was encountered by a system call.
-.Pp
-The
-.Fn magic_file
-function returns a textual description of the contents of the
-.Ar filename
-argument, or
-.Dv NULL
-if an error occurred.
-If the
-.Ar filename
-is
-.Dv NULL ,
-then stdin is used.
-.Pp
-The
-.Fn magic_descriptor
-function returns a textual description of the contents of the
-.Ar fd
-argument, or
-.Dv NULL
-if an error occurred.
-.Pp
-The
-.Fn magic_buffer
-function returns a textual description of the contents of the
-.Ar buffer
-argument with
-.Ar length
-bytes size.
-.Pp
-The
-.Fn magic_setflags
-function sets the
-.Ar flags
-described above.
-Note that using both MIME flags together can also
-return extra information on the charset.
-.Pp
-The
-.Fn magic_check
-function can be used to check the validity of entries in the colon
-separated database files passed in as
-.Ar filename ,
-or
-.Dv NULL
-for the default database.
-It returns 0 on success and \-1 on failure.
-.Pp
-The
-.Fn magic_compile
-function can be used to compile the the colon
-separated list of database files passed in as
-.Ar filename ,
-or
-.Dv NULL
-for the default database.
-It returns 0 on success and \-1 on failure.
-The compiled files created are named from the
-.Xr basename 1
-of each file argument with
-.Dq .mgc
-appended to it.
-.Pp
-The
-.Fn magic_list
-function dumps all magic entries in a human readable format,
-dumping first the entries that are matched against binary files and then the
-ones that match text files.
-It takes and optional
-.Fa filename
-argument which is a colon separated list of database files, or
-.Dv NULL
-for the default database.
-.Pp
-The
-.Fn magic_load
-function must be used to load the the colon
-separated list of database files passed in as
-.Ar filename ,
-or
-.Dv NULL
-for the default database file before any magic queries can performed.
-.Pp
-The default database file is named by the MAGIC environment variable.
-If that variable is not set, the default database file name is __MAGIC__.
-.Fn magic_load
-adds
-.Dq .mgc
-to the database filename as appropriate.
-.Pp
-The
-.Fn magic_version
-command returns the version number of this library which is compiled into
-the shared library using the constant
-.Dv MAGIC_VERSION
-from
-.In magic.h .
-This can be used by client programs to verify that the version they compile
-against is the same as the version that they run against.
-.Sh RETURN VALUES
-The function
-.Fn magic_open
-returns a magic cookie on success and
-.Dv NULL
-on failure setting errno to an appropriate value.
-It will set errno to
-.Er EINVAL
-if an unsupported value for flags was given.
-The
-.Fn magic_list ,
-.Fn magic_load ,
-.Fn magic_compile ,
-and
-.Fn magic_check
-functions return 0 on success and \-1 on failure.
-The
-.Fn magic_buffer ,
-.Fn magic_getpath ,
-and
-.Fn magic_file ,
-functions return a string on success and
-.Dv NULL
-on failure.
-The
-.Fn magic_error
-function returns a textual description of the errors of the above
-functions, or
-.Dv NULL
-if there was no error.
-The
-.Fn magic_version
-always returns the version number of the library.
-Finally,
-.Fn magic_setflags
-returns \-1 on systems that don't support
-.Xr utime 3 ,
-or
-.Xr utimes 2
-when
-.Dv MAGIC_PRESERVE_ATIME
-is set.
-.Sh FILES
-.Bl -tag -width __MAGIC__.mgc -compact
-.It Pa __MAGIC__
-The non-compiled default magic database.
-.It Pa __MAGIC__.mgc
-The compiled default magic database.
-.El
-.Sh SEE ALSO
-.Xr file __CSECTION__ ,
-.Xr magic __FSECTION__
-.Sh AUTHORS
-.An M\(oans Rullg\(oard
-Initial libmagic implementation, and configuration.
-.An Christos Zoulas
-API cleanup, error code and allocation handling.
More information about the Midnightbsd-cvs
mailing list