~blowry/file

95005fdccfb2b79257c9b53845494a49743d0d6e — Christos Zoulas 11 years ago ef51f41
mingw32 and escaping fixes
M ChangeLog => ChangeLog +6 -0
@@ 1,3 1,9 @@
2010-07-21  12:20  Christos Zoulas <christos@zoulas.com>

	* MINGW32 portability from LRN

	* Don't warn about escaping magic regex chars when we are in a regex.

2010-07-19  10:55  Christos Zoulas <christos@zoulas.com>

	* Only try to print prpsinfo for core files. (Jan Kaluza)

M Makefile.am => Makefile.am +1 -1
@@ 1,4 1,4 @@
#ACLOCAL_AMFLAGS = -I m4
ACLOCAL_AMFLAGS = -I m4

EXTRA_DIST = MAINT


M acinclude.m4 => acinclude.m4 +11 -8
@@ 10,15 10,16 @@ if test "$ac_cv_struct_tm_zone" = yes; then
  AC_DEFINE(HAVE_TM_ZONE,1,[HAVE_TM_ZONE])
fi

# On SGI, apparently tzname is a #define, but that's ok, AC_CHECK_DECL will
# consider it declared and we won't give our own extern.
AC_CHECK_DECLS([tzname], , , [#include <time.h>])
AC_CACHE_CHECK(for tzname, ac_cv_var_tzname,
[AC_TRY_LINK(
changequote(<<, >>)dnl
<<#include <time.h>
#ifndef tzname /* For SGI.  */
extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
#endif>>,
changequote([, ])dnl
[atoi(*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)])
[#include <time.h>
#if !HAVE_DECL_TZNAME
extern char *tzname[];
#endif],
[return tzname[0][0];], [ac_cv_var_tzname=yes], [ac_cv_var_tzname=no])])
  if test $ac_cv_var_tzname = yes; then
    AC_DEFINE(HAVE_TZNAME,1,[HAVE_TZNAME])
  fi


@@ 31,11 32,13 @@ if test "$ac_cv_struct_tm_isdst" = yes; then
  AC_DEFINE(HAVE_TM_ISDST,1,[HAVE_TM_ISDST])
fi


AC_CHECK_DECLS([daylight], , , [#include <time.h>])
AC_CACHE_CHECK(for daylight, ac_cv_var_daylight,
[AC_TRY_LINK(
changequote(<<, >>)dnl
<<#include <time.h>
#ifndef daylight /* In case IRIX #defines this, too  */
#if !HAVE_DECL_DAYLIGHT
extern int daylight;
#endif>>,
changequote([, ])dnl

M configure.ac => configure.ac +20 -3
@@ 1,8 1,10 @@
dnl Process this file with autoconf to produce a configure script.
AC_INIT(file, 5.04, christos@astron.com)
AM_INIT_AUTOMAKE
AM_INIT_AUTOMAKE()
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])

AM_CONFIG_HEADER(config.h)
#AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_MACRO_DIR([m4])

AC_MSG_CHECKING(for builtin ELF support)
AC_ARG_ENABLE(elf,


@@ 47,6 49,18 @@ fi], [
  fsect=4
])

AC_CANONICAL_HOST
case "$host_os" in
   mingw32*)
      MINGW=1
      ;;
   *)
      MINGW=0
      ;;
esac
AC_SUBST(MINGW)
AM_CONDITIONAL(MINGW, test "$MINGW" = 1)

AC_SUBST([pkgdatadir], ['$(datadir)/misc'])
AC_SUBST(fsect)
AM_CONDITIONAL(FSECT5, test x$fsect = x5)


@@ 142,13 156,16 @@ else
fi])

dnl Checks for functions
AC_CHECK_FUNCS(mmap strerror strndup strtoul mbrtowc mkstemp utimes utime wcwidth strtof)
AC_CHECK_FUNCS(mmap strerror strndup strtoul mbrtowc mkstemp utimes utime wcwidth strtof fork)

dnl Provide implementation of some required functions if necessary
AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat)

dnl Checks for libraries
AC_CHECK_LIB(z,gzopen)
if test "$MINGW" = 1; then
  AC_CHECK_LIB(gnurx,regexec,,AC_MSG_ERROR([libgnurx is required to build file(1) with MinGW]))
fi

dnl See if we are cross-compiling
AM_CONDITIONAL(IS_CROSS_COMPILE, test "$cross_compiling" = yes)

M magic/Magdir/ruby => magic/Magdir/ruby +2 -2
@@ 1,6 1,6 @@

#------------------------------------------------------------------------------
# $File: ruby,v 1.3 2009/09/19 16:28:12 christos Exp $
# $File: ruby,v 1.4 2010/07/08 20:24:13 christos Exp $
# ruby:  file(1) magic for Ruby scripting language
# URL:  http://www.ruby-lang.org/
# From: Reuben Thomas <rrt@sc3d.org>


@@ 18,7 18,7 @@
# What looks like ruby, but does not have a shebang
# (modules and such)
# From: Lubomir Rintel <lkundrak@v3.sk>
0	regex		\^[\ \t]*require[\ \t]'[A-Za-z_\/]+'
0	regex		\^[\ \t]*require[\ \t]'[A-Za-z_/]+'
>0	regex		include\ [A-Z]|def\ [a-z]|\ do$
>>0	regex		\^[\ \t]*end([\ \t]*[;#].*)?$		Ruby script text
!:mime	text/x-ruby

M src/Makefile.am => src/Makefile.am +6 -1
@@ 12,7 12,12 @@ libmagic_la_SOURCES = magic.c apprentice.c softmagic.c ascmagic.c \
	funcs.c file.h names.h patchlevel.h readelf.h tar.h apptype.c \
	file_opts.h elfclass.h mygetopt.h cdf.c cdf_time.c readcdf.c cdf.h
libmagic_la_LDFLAGS = -no-undefined -version-info 1:0:0
libmagic_la_LIBADD = $(LTLIBOBJS)
if MINGW
MINGWLIBS = -lgnurx -lshlwapi
else
MINGWLIBS = 
endif
libmagic_la_LIBADD = $(LTLIBOBJS) $(MINGWLIBS)

file_SOURCES = file.c
file_LDADD = libmagic.la

M src/apprentice.c => src/apprentice.c +9 -7
@@ 32,7 32,7 @@
#include "file.h"

#ifndef	lint
FILE_RCSID("@(#)$File: apprentice.c,v 1.157 2009/09/14 17:50:38 christos Exp $")
FILE_RCSID("@(#)$File: apprentice.c,v 1.158 2009/10/19 13:10:20 christos Exp $")
#endif	/* lint */

#include "magic.h"


@@ 1555,8 1555,8 @@ parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
	if (i == sizeof(m->apple) && *l) {
		/* We don't need to NUL terminate here, printing handles it */
		if (ms->flags & MAGIC_CHECK)
			file_magwarn(ms, "APPLE type `%s' truncated %zu",
			    line, i);
			file_magwarn(ms, "APPLE type `%s' truncated %"
			    SIZE_T_FORMAT "u", line, i);
	}

	if (i > 0)


@@ 1589,8 1589,8 @@ parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line)
	if (i == sizeof(m->mimetype)) {
		m->mimetype[sizeof(m->mimetype) - 1] = '\0';
		if (ms->flags & MAGIC_CHECK)
			file_magwarn(ms, "MIME type `%s' truncated %zu",
			    m->mimetype, i);
			file_magwarn(ms, "MIME type `%s' truncated %"
			    SIZE_T_FORMAT "u", m->mimetype, i);
	} else
		m->mimetype[i] = '\0';



@@ 1879,8 1879,10 @@ getstr(struct magic_set *ms, struct magic *m, const char *s, int warn)
					if (isprint((unsigned char)c)) {
						/* Allow escaping of 
						 * ``relations'' */
						if (strchr("<>&^=!", c)
						    == NULL) {
						if (strchr("<>&^=!", c) == NULL
						    && (m->type != FILE_REGEX ||
						    strchr("[]().*?^$|{}", c)
						    == NULL)) {
							file_magwarn(ms, "no "
							    "need to escape "
							    "`%c'", c);

M src/asprintf.c => src/asprintf.c +1 -3
@@ 29,11 29,9 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: asprintf.c,v 1.2 2008/11/04 16:38:28 christos Exp $")
FILE_RCSID("@(#)$File: asprintf.c,v 1.3 2009/02/03 20:27:51 christos Exp $")
#endif

int vasprintf(char **ptr, const char *format_string, va_list vargs);

int asprintf(char **ptr, const char *fmt, ...)
{
  va_list vargs;

M src/cdf.c => src/cdf.c +916 -911
@@ 35,7 35,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: cdf.c,v 1.36 2010/01/22 20:56:26 christos Exp $")
FILE_RCSID("@(#)$File: cdf.c,v 1.37 2010/02/20 15:19:53 rrt Exp $")
#endif

#include <assert.h>


@@ 68,8 68,8 @@ FILE_RCSID("@(#)$File: cdf.c,v 1.36 2010/01/22 20:56:26 christos Exp $")
#endif

static union {
        char s[4];
        uint32_t u;
	char s[4];
	uint32_t u;
} cdf_bo;

#define NEED_SWAP	(cdf_bo.u == (uint32_t)0x01020304)


@@ 84,12 84,12 @@ static union {
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;
	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;
}

/*


@@ 98,14 98,14 @@ cdf_tole2(uint16_t sv)
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;
	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;
}

/*


@@ 114,18 114,18 @@ cdf_tole4(uint32_t sv)
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;
	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;
}

#define CDF_UNPACK(a)	\


@@ 136,175 136,177 @@ cdf_tole8(uint64_t 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]);
	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]);
	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);
	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]);
	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);
	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 void *p, size_t tail,
    int line)
{
        const char *b = (const char *)sst->sst_tab;
        const char *e = ((const char *)p) + tail;
        (void)&line;
        if (e >= b && (size_t)(e - b) < sst->sst_dirlen * sst->sst_len)
                return 0;
        DPRINTF(("%d: offset begin %p end %p %zu >= %zu [%zu %zu]\n",
            line, b, e, (size_t)(e - b), sst->sst_dirlen * sst->sst_len,
            sst->sst_dirlen, sst->sst_len));
        errno = EFTYPE;
        return -1;
	const char *b = (const char *)sst->sst_tab;
	const char *e = ((const char *)p) + tail;
	(void)&line;
	if (e >= b && (size_t)(e - b) < sst->sst_dirlen * 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),
	    sst->sst_dirlen * sst->sst_len, sst->sst_dirlen, 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;
	size_t siz = (size_t)off + len;

        if ((off_t)(off + len) != (off_t)siz) {
                errno = EINVAL;
                return -1;
        }
	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_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 (info->i_fd == -1)
		return -1;

        if (lseek(info->i_fd, off, SEEK_SET) == (off_t)-1)
                return -1;
	if (lseek(info->i_fd, off, SEEK_SET) == (off_t)-1)
		return -1;

        if (read(info->i_fd, buf, len) != (ssize_t)len)
                return -1;
	if (read(info->i_fd, buf, len) != (ssize_t)len)
		return -1;

        return (ssize_t)len;
	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%llx != 0x%llx\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;
	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;
	errno = EFTYPE;
	return -1;
}




@@ 312,19 314,19 @@ 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)
{
        assert((size_t)CDF_SEC_SIZE(h) == len);
        return cdf_read(info, (off_t)CDF_SEC_POS(h, id),
            ((char *)buf) + offs, len);
	assert((size_t)CDF_SEC_SIZE(h) == len);
	return cdf_read(info, (off_t)CDF_SEC_POS(h, id),
	    ((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)
{
        assert((size_t)CDF_SHORT_SEC_SIZE(h) == len);
        (void)memcpy(((char *)buf) + offs,
            ((const char *)sst->sst_tab) + CDF_SHORT_SEC_POS(h, id), len);
        return len;
	assert((size_t)CDF_SHORT_SEC_SIZE(h) == len);
	(void)memcpy(((char *)buf) + offs,
	    ((const char *)sst->sst_tab) + CDF_SHORT_SEC_POS(h, id), len);
	return len;
}

/*


@@ 333,154 335,155 @@ cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
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;
	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;
	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 (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 %zu\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 = %zu ss = %zu\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 %u >= %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]);
        }
	if (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 %u >= %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;
	sat->sat_len = i;
	free(msa);
	return 0;
out2:
        free(msa);
	free(msa);
out1:
        free(sat->sat_tab);
        return -1;
	free(sat->sat_tab);
	return -1;
}

size_t
cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
{
        size_t i, j;
        cdf_secid_t maxsector = (cdf_secid_t)(sat->sat_len * size);

        DPRINTF(("Chain:"));
        for (j = i = 0; sid >= 0; i++, j++) {
                DPRINTF((" %d", sid));
                if (j >= CDF_LOOP_LIMIT) {
                        DPRINTF(("Counting chain loop limit"));
                        errno = EFTYPE;
                        return (size_t)-1;
                }
                if (sid > maxsector) {
                        DPRINTF(("Sector %d > %d\n", sid, maxsector));
                        errno = EFTYPE;
                        return (size_t)-1;
                }
                sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
        }
        DPRINTF(("\n"));
        return i;
	size_t i, j;
	cdf_secid_t maxsector = (cdf_secid_t)(sat->sat_len * size);

	DPRINTF(("Chain:"));
	for (j = i = 0; sid >= 0; i++, j++) {
		DPRINTF((" %d", sid));
		if (j >= CDF_LOOP_LIMIT) {
			DPRINTF(("Counting chain loop limit"));
			errno = EFTYPE;
			return (size_t)-1;
		}
		if (sid > maxsector) {
			DPRINTF(("Sector %d > %d\n", sid, maxsector));
			errno = EFTYPE;
			return (size_t)-1;
		}
		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
	}
	DPRINTF(("\n"));
	return i;
}

int
cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
    const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
{
        size_t ss = CDF_SEC_SIZE(h), i, j;
        ssize_t nr;
        scn->sst_len = cdf_count_chain(sat, sid, ss);
        scn->sst_dirlen = len;

        if (scn->sst_len == (size_t)-1)
                return -1;

        scn->sst_tab = calloc(scn->sst_len, ss);
        if (scn->sst_tab == NULL)
                return -1;

        for (j = i = 0; sid >= 0; i++, j++) {
                if (j >= CDF_LOOP_LIMIT) {
                        DPRINTF(("Read long sector chain loop limit"));
                        errno = EFTYPE;
                        goto out;
                }
                if (i >= scn->sst_len) {
                        DPRINTF(("Out of bounds reading long sector chain "
                            "%u > %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;
	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 "
			    "%u > %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;
	free(scn->sst_tab);
	return -1;
}

int


@@ 488,40 491,40 @@ 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 "
                            "%u > %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;
	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 "
			    "%u > %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;
	free(scn->sst_tab);
	return -1;
}

int


@@ 530,64 533,64 @@ cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
    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);
	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;
	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;
	free(dir->dir_tab);
	free(buf);
	return -1;
}




@@ 595,79 598,79 @@ 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 "
                            "%u > %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;
	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 "
			    "%u > %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;
	free(ssat->sat_tab);
	return -1;
}

int
cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
    const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn)
{
        size_t i;
        const cdf_directory_t *d;
	size_t i;
	const cdf_directory_t *d;

        for (i = 0; i < dir->dir_len; i++)
                if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
                        break;
	for (i = 0; i < dir->dir_len; i++)
		if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
			break;

        /* If the it is not there, just fake it; some docs don't have it */
        if (i == dir->dir_len)
                goto out;
        d = &dir->dir_tab[i];
	/* If the it is not there, just fake it; some docs don't have it */
	if (i == dir->dir_len)
		goto out;
	d = &dir->dir_tab[i];

        /* If the it is not there, just fake it; some docs don't have it */
        if (d->d_stream_first_sector < 0)
                goto out;
	/* 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);
	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;
	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;
	for (; l--; d++, s++)
		if (*d != CDF_TOLE2(*s))
			return (unsigned char)*d - CDF_TOLE2(*s);
	return 0;
}

int


@@ 675,227 678,227 @@ cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
    const cdf_dir_t *dir, cdf_stream_t *scn)
{
        size_t i;
        const cdf_directory_t *d;
        static const char name[] = "\05SummaryInformation";

        for (i = dir->dir_len; i > 0; i--)
                if (dir->dir_tab[i - 1].d_type == CDF_DIR_TYPE_USER_STREAM &&
                    cdf_namecmp(name, dir->dir_tab[i - 1].d_name, sizeof(name))
                    == 0)
                        break;

        if (i == 0) {
                DPRINTF(("Cannot find summary information section\n"));
                errno = ESRCH;
                return -1;
        }
        d = &dir->dir_tab[i - 1];
        return cdf_read_sector_chain(info, h, sat, ssat, sst,
            d->d_stream_first_sector, d->d_size, scn);
	size_t i;
	const cdf_directory_t *d;
	static const char name[] = "\05SummaryInformation";

	for (i = dir->dir_len; i > 0; i--)
		if (dir->dir_tab[i - 1].d_type == CDF_DIR_TYPE_USER_STREAM &&
		    cdf_namecmp(name, dir->dir_tab[i - 1].d_name, sizeof(name))
		    == 0)
			break;

	if (i == 0) {
		DPRINTF(("Cannot find summary information section\n"));
		errno = ESRCH;
		return -1;
	}
	d = &dir->dir_tab[i - 1];
	return cdf_read_sector_chain(info, h, sat, ssat, sst,
	    d->d_stream_first_sector, d->d_size, scn);
}

int
cdf_read_property_info(const cdf_stream_t *sst, 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 uint32_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, 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, shp, sizeof(*shp), __LINE__) == -1)
                goto out;
        sh.sh_len = CDF_TOLE4(shp->sh_len);
	const cdf_section_header_t *shp;
	cdf_section_header_t sh;
	const uint32_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, 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, 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);
	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 uint32_t *, (const void *)
            ((const char *)(const void *)sst->sst_tab +
            offs + sizeof(sh)));
        e = CAST(const uint32_t *, (const void *)
            (((const char *)(const void *)shp) + sh.sh_len));
        if (cdf_check_stream_offset(sst, e, 0, __LINE__) == -1)
                goto out;
        for (i = 0; i < sh.sh_properties; i++) {
                q = (const uint32_t *)(const void *)
                    ((const char *)(const void *)p +
                    CDF_TOLE4(p[(i << 1) + 1])) - 2;
                if (q > e) {
                        DPRINTF(("Ran of the end %p > %p\n", q, e));
                        goto out;
                }
                inp[i].pi_id = CDF_TOLE4(p[i << 1]);
                inp[i].pi_type = CDF_TOLE4(q[0]);
                DPRINTF(("%d) id=%x type=%x offs=%x\n", i, inp[i].pi_id,
                    inp[i].pi_type, (const char *)q - (const char *)p));
                if (inp[i].pi_type & CDF_VECTOR) {
                        nelements = CDF_TOLE4(q[1]);
                        o = 2;
                } else {
                        nelements = 1;
                        o = 1;
                }
                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[o], 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[o], 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[o], 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[o], 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[o], sizeof(u64));
                        inp[i].pi_u64 = CDF_TOLE8((uint64_t)u64);
                        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 = %d\n", nelements));
                        for (j = 0; j < nelements; j++, i++) {
                                uint32_t l = CDF_TOLE4(q[o]);
                                inp[i].pi_str.s_len = l;
                                inp[i].pi_str.s_buf =
                                    (const char *)(const void *)(&q[o+1]);
                                DPRINTF(("l = %d, r = %d, s = %s\n", l,
                                    CDF_ROUND(l, sizeof(l)),
                                    inp[i].pi_str.s_buf));
                                l = 4 + (uint32_t)CDF_ROUND(l, sizeof(l));
                                o += l >> 2;
                        }
                        i--;
                        break;
                case CDF_FILETIME:
                        if (inp[i].pi_type & CDF_VECTOR)
                                goto unknown;
                        (void)memcpy(&tp, &q[o], 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));
                        goto out;
                }
        }
        return 0;
	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 uint32_t *, (const void *)
	    ((const char *)(const void *)sst->sst_tab +
	    offs + sizeof(sh)));
	e = CAST(const uint32_t *, (const void *)
	    (((const char *)(const void *)shp) + sh.sh_len));
	if (cdf_check_stream_offset(sst, e, 0, __LINE__) == -1)
		goto out;
	for (i = 0; i < sh.sh_properties; i++) {
		q = (const uint32_t *)(const void *)
		    ((const char *)(const void *)p +
		    CDF_TOLE4(p[(i << 1) + 1])) - 2;
		if (q > e) {
			DPRINTF(("Ran of the end %p > %p\n", q, e));
			goto out;
		}
		inp[i].pi_id = CDF_TOLE4(p[i << 1]);
		inp[i].pi_type = CDF_TOLE4(q[0]);
		DPRINTF(("%d) id=%x type=%x offs=%x\n", i, inp[i].pi_id,
		    inp[i].pi_type, (const char *)q - (const char *)p));
		if (inp[i].pi_type & CDF_VECTOR) {
			nelements = CDF_TOLE4(q[1]);
			o = 2;
		} else {
			nelements = 1;
			o = 1;
		}
		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[o], 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[o], 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[o], 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[o], 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[o], sizeof(u64));
			inp[i].pi_u64 = CDF_TOLE8((uint64_t)u64);
			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 = %d\n", nelements));
			for (j = 0; j < nelements; j++, i++) {
				uint32_t l = CDF_TOLE4(q[o]);
				inp[i].pi_str.s_len = l;
				inp[i].pi_str.s_buf =
				    (const char *)(const void *)(&q[o+1]);
				DPRINTF(("l = %d, r = %d, s = %s\n", l,
				    CDF_ROUND(l, sizeof(l)),
				    inp[i].pi_str.s_buf));
				l = 4 + (uint32_t)CDF_ROUND(l, sizeof(l));
				o += l >> 2;
			}
			i--;
			break;
		case CDF_FILETIME:
			if (inp[i].pi_type & CDF_VECTOR)
				goto unknown;
			(void)memcpy(&tp, &q[o], 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));
			goto out;
		}
	}
	return 0;
out:
        free(*info);
        return -1;
	free(*info);
	return -1;
}

int
cdf_unpack_summary_info(const cdf_stream_t *sst, cdf_summary_info_header_t *ssi,
    cdf_property_info_t **info, size_t *count)
{
        size_t i, maxcount;
        const cdf_summary_info_header_t *si =
            CAST(const cdf_summary_info_header_t *, sst->sst_tab);
        const cdf_section_declaration_t *sd =
            CAST(const cdf_section_declaration_t *, (const void *)
            ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET));

        if (cdf_check_stream_offset(sst, si, sizeof(*si), __LINE__) == -1 ||
            cdf_check_stream_offset(sst, sd, sizeof(*sd), __LINE__) == -1)
                return -1;
        ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
        ssi->si_os_version = CDF_TOLE2(si->si_os_version);
        ssi->si_os = CDF_TOLE2(si->si_os);
        ssi->si_class = si->si_class;
        cdf_swap_class(&ssi->si_class);
        ssi->si_count = CDF_TOLE2(si->si_count);
        *count = 0;
        maxcount = 0;
        *info = NULL;
        for (i = 0; i < CDF_TOLE4(si->si_count); i++) {
                if (i >= CDF_LOOP_LIMIT) {
                        DPRINTF(("Unpack summary info loop limit"));
                        errno = EFTYPE;
                        return -1;
                }
                if (cdf_read_property_info(sst, CDF_TOLE4(sd->sd_offset),
                    info, count, &maxcount) == -1)
                        return -1;
        }
        return 0;
	size_t i, maxcount;
	const cdf_summary_info_header_t *si =
	    CAST(const cdf_summary_info_header_t *, sst->sst_tab);
	const cdf_section_declaration_t *sd =
	    CAST(const cdf_section_declaration_t *, (const void *)
	    ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET));

	if (cdf_check_stream_offset(sst, si, sizeof(*si), __LINE__) == -1 ||
	    cdf_check_stream_offset(sst, sd, sizeof(*sd), __LINE__) == -1)
		return -1;
	ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
	ssi->si_os_version = CDF_TOLE2(si->si_os_version);
	ssi->si_os = CDF_TOLE2(si->si_os);
	ssi->si_class = si->si_class;
	cdf_swap_class(&ssi->si_class);
	ssi->si_count = CDF_TOLE2(si->si_count);
	*count = 0;
	maxcount = 0;
	*info = NULL;
	for (i = 0; i < CDF_TOLE4(si->si_count); i++) {
		if (i >= CDF_LOOP_LIMIT) {
			DPRINTF(("Unpack summary info loop limit"));
			errno = EFTYPE;
			return -1;
		}
		if (cdf_read_property_info(sst, CDF_TOLE4(sd->sd_offset),
		    info, count, &maxcount) == -1)
			return -1;
	}
	return 0;
}




@@ 903,83 906,83 @@ cdf_unpack_summary_info(const cdf_stream_t *sst, cdf_summary_info_header_t *ssi,
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]);
	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;
	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" },
	{ 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;
	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);
	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;
	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;
}




@@ 987,74 990,75 @@ cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
void
cdf_dump_header(const cdf_header_t *h)
{
        size_t i;
	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]);
        }
	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[%zu]:\n%.6d: ", 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%.6d: ",
                                    i * s + j + 1);
                }
                (void)fprintf(stderr, "\n");
        }
	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%.6d: ",
		    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%.6d: ",
				    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%.4x: ", abuf, i + 1);
                }
        }
        (void)fprintf(stderr, "\n");
	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%.4x: ", 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);
	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


@@ 1062,139 1066,140 @@ 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++) {
                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 %zu: %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", ctime(&ts.tv_sec));
                cdf_timestamp_to_timespec(&ts, d->d_modified);
                (void)fprintf(stderr, "Modified %s", ctime(&ts.tv_sec));
                (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
                (void)fprintf(stderr, "Size %d\n", d->d_size);
                switch (d->d_type) {
                case CDF_DIR_TYPE_USER_STORAGE:
                        (void)fprintf(stderr, "Storage: %d\n", d->d_storage);
                        break;
                case CDF_DIR_TYPE_USER_STREAM:
                        if (sst == NULL)
                                break;
                        if (cdf_read_sector_chain(info, h, sat, ssat, sst,
                            d->d_stream_first_sector, d->d_size, &scn) == -1) {
                                warn("Can't read stream for %s at %d len %d",
                                    name, d->d_stream_first_sector, d->d_size);
                                break;
                        }
                        cdf_dump_stream(h, &scn);
                        free(scn.sst_tab);
                        break;
                default:
                        break;
                }

        }
	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++) {
		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", ctime(&ts.tv_sec));
		cdf_timestamp_to_timespec(&ts, d->d_modified);
		(void)fprintf(stderr, "Modified %s", ctime(&ts.tv_sec));
		(void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
		(void)fprintf(stderr, "Size %d\n", d->d_size);
		switch (d->d_type) {
		case CDF_DIR_TYPE_USER_STORAGE:
			(void)fprintf(stderr, "Storage: %d\n", d->d_storage);
			break;
		case CDF_DIR_TYPE_USER_STREAM:
			if (sst == NULL)
				break;
			if (cdf_read_sector_chain(info, h, sat, ssat, sst,
			    d->d_stream_first_sector, d->d_size, &scn) == -1) {
				warn("Can't read stream for %s at %d len %d",
				    name, d->d_stream_first_sector, d->d_size);
				break;
			}
			cdf_dump_stream(h, &scn);
			free(scn.sst_tab);
			break;
		default:
			break;
		}

	}
}

void
cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
{
        cdf_timestamp_t tp;
        struct 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, "%zu) %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_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 {
                                cdf_timestamp_to_timespec(&ts, tp);
                                (void)fprintf(stderr, "timestamp %s",
                                    ctime(&ts.tv_sec));
                        }
                        break;
                case CDF_CLIPBOARD:
                        (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
                        break;
                default:
                        DPRINTF(("Don't know how to deal with %x\n",
                            info[i].pi_type));
                        break;
                }
        }
	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_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 {
				cdf_timestamp_to_timespec(&ts, tp);
				(void)fprintf(stderr, "timestamp %s",
				    ctime(&ts.tv_sec));
			}
			break;
		case CDF_CLIPBOARD:
			(void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
			break;
		default:
			DPRINTF(("Don't know how to deal with %x\n",
			    info[i].pi_type));
			break;
		}
	}
}


void
cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
{
        char buf[128];
        cdf_summary_info_header_t ssi;
        cdf_property_info_t *info;
        size_t count;

        (void)&h;
        if (cdf_unpack_summary_info(sst, &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);
	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, &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


@@ 1203,66 1208,66 @@ cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
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");
	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);
		cdf_dump_header(&h);
#endif

                if (cdf_read_sat(&info, &h, &sat) == -1)
                        err(1, "Cannot read sat");
		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));
		cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
#endif

                if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
                        err(1, "Cannot read ssat");
		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));
		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_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");
		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);
		cdf_dump_stream(&h, &sst);
#endif

#ifdef CDF_DEBUG
                cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
		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");
		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);
		cdf_dump_summary_info(&h, &scn);
#endif

                (void)close(info.i_fd);
        }
		(void)close(info.i_fd);
	}

        return 0;
	return 0;
}
#endif

M src/cdf.h => src/cdf.h +6 -0
@@ 35,6 35,12 @@
#ifndef _H_CDF_
#define _H_CDF_

#ifdef WIN32
#include <winsock2.h>
#define timespec timeval
#define tv_nsec tv_usec
#endif

typedef int32_t cdf_secid_t;

#define CDF_LOOP_LIMIT					10000

M src/compress.c => src/compress.c +17 -9
@@ 35,7 35,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: compress.c,v 1.63 2009/03/23 14:21:51 christos Exp $")
FILE_RCSID("@(#)$File: compress.c,v 1.64 2009/05/08 17:41:58 christos Exp $")
#endif

#include "magic.h"


@@ 45,7 45,9 @@ FILE_RCSID("@(#)$File: compress.c,v 1.63 2009/03/23 14:21:51 christos Exp $")
#endif
#include <string.h>
#include <errno.h>
#ifndef __MINGW32__
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif


@@ 79,12 81,11 @@ private const struct {
 	{ "\3757zXZ\0",6,{ "xz", "-cd", NULL }, 1 },		/* XZ Utils */
};

private size_t ncompr = sizeof(compr) / sizeof(compr[0]);

#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


@@ 137,7 138,7 @@ error:
	ms->flags |= MAGIC_COMPRESS;
	return rv;
}

#endif
/*
 * `safe' write for sockets and pipes.
 */


@@ 167,9 168,12 @@ swrite(int fd, const void *buf, size_t n)
 * `safe' read for sockets and pipes.
 */
protected ssize_t
sread(int fd, void *buf, size_t n, int canbepipe)
sread(int fd, void *buf, size_t n, int canbepipe __attribute__ ((unused)))
{
	ssize_t rv, cnt;
	ssize_t rv;
#ifdef FD_ZERO
	ssize_t cnt;
#endif
#ifdef FIONREAD
	int t = 0;
#endif


@@ 236,7 240,10 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
{
	char buf[4096];
	ssize_t r;
	int tfd, te;
	int tfd;
#ifdef HAVE_MKSTEMP
	int te;
#endif

	(void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
#ifndef HAVE_MKSTEMP


@@ 294,7 301,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
	}
	return fd;
}

#if HAVE_FORK
#ifdef BUILTIN_DECOMPRESS

#define FHCRC		(1 << 1)


@@ 494,3 501,4 @@ err:
		return n;
	}
}
#endif

M src/encoding.c => src/encoding.c +10 -9
@@ 35,7 35,7 @@
#include "file.h"

#ifndef	lint
FILE_RCSID("@(#)$File: encoding.c,v 1.3 2009/02/03 20:27:51 christos Exp $")
FILE_RCSID("@(#)$File: encoding.c,v 1.4 2009/09/13 19:02:22 christos Exp $")
#endif	/* lint */

#include "magic.h"


@@ 84,15 84,15 @@ file_encoding(struct magic_set *ms, const unsigned char *buf, size_t nbytes, uni

	*type = "text";
	if (looks_ascii(buf, nbytes, *ubuf, ulen)) {
		DPRINTF(("ascii %zu\n", *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 %zu\n", *ulen));
		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 %zu\n", *ulen));
		DPRINTF(("utf8 %" SIZE_T_FORMAT "u\n", *ulen));
		*code = "UTF-8 Unicode (with BOM)";
		*code = "UTF-8 Unicode";
		*code_mime = "utf-8";


@@ 104,24 104,25 @@ file_encoding(struct magic_set *ms, const unsigned char *buf, size_t nbytes, uni
			*code = "Big-endian UTF-16 Unicode";
			*code_mime = "utf-16be";
		}
		DPRINTF(("ucs16 %zu\n", *ulen));
		DPRINTF(("ucs16 %" SIZE_T_FORMAT "u\n", *ulen));
	} else if (looks_latin1(buf, nbytes, *ubuf, ulen)) {
		DPRINTF(("latin1 %zu\n", *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 %zu\n", *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 %zu\n", *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 %zu\n", *ulen));
			DPRINTF(("ebcdic/international %" SIZE_T_FORMAT "u\n",
			    *ulen));
			*code = "International EBCDIC";
			*code_mime = "ebcdic";
		} else { /* Doesn't look like text at all */

M src/file.h => src/file.h +16 -2
@@ 27,7 27,7 @@
 */
/*
 * file.h - definitions for file(1) program
 * @(#)$File: file.h,v 1.123 2009/10/19 13:10:20 christos Exp $
 * @(#)$File: file.h,v 1.124 2010/01/16 17:45:12 chl Exp $
 */

#ifndef __file_h__


@@ 37,6 37,18 @@
#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 */


@@ 62,7 74,7 @@
#define MAGIC "/etc/magic"
#endif

#ifdef __EMX__
#if defined(__EMX__) || defined (WIN32)
#define PATHSEP	';'
#else
#define PATHSEP	':'


@@ 364,8 376,10 @@ 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);
protected int file_ascmagic_with_encoding(struct magic_set *,
    const unsigned char *, size_t, unichar *, size_t, const char *,

M src/fsmagic.c => src/fsmagic.c +3 -3
@@ 32,7 32,7 @@
#include "file.h"

#ifndef	lint
FILE_RCSID("@(#)$File: fsmagic.c,v 1.59 2009/02/03 20:27:51 christos Exp $")
FILE_RCSID("@(#)$File: fsmagic.c,v 1.60 2009/05/08 17:41:59 christos Exp $")
#endif	/* lint */

#include "magic.h"


@@ 59,7 59,7 @@ FILE_RCSID("@(#)$File: fsmagic.c,v 1.59 2009/02/03 20:27:51 christos Exp $")
# define minor(dev)  ((dev) & 0xff)
#endif
#undef HAVE_MAJOR

#ifdef	S_IFLNK
private int
bad_link(struct magic_set *ms, int err, char *buf)
{


@@ 83,7 83,7 @@ bad_link(struct magic_set *ms, int err, char *buf)
	}
	return 1;
}

#endif
private int
handle_mime(struct magic_set *ms, int mime, const char *str)
{

M src/funcs.c => src/funcs.c +8 -7
@@ 27,7 27,7 @@
#include "file.h"

#ifndef	lint
FILE_RCSID("@(#)$File: funcs.c,v 1.53 2009/04/07 11:07:00 christos Exp $")
FILE_RCSID("@(#)$File: funcs.c,v 1.54 2009/05/08 17:41:59 christos Exp $")
#endif	/* lint */

#include "magic.h"


@@ 103,7 103,7 @@ file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
	if (lineno != 0) {
		free(ms->o.buf);
		ms->o.buf = NULL;
		file_printf(ms, "line %zu: ", lineno);
		file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno);
	}
	file_vprintf(ms, f, va);
	if (error > 0)


@@ 138,7 138,8 @@ file_magerror(struct magic_set *ms, const char *f, ...)
protected void
file_oomem(struct magic_set *ms, size_t len)
{
	file_error(ms, errno, "cannot allocate %zu bytes", len);
	file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
	    len);
}

protected void


@@ 155,8 156,8 @@ file_badread(struct magic_set *ms)

#ifndef COMPILE_ONLY
protected int
file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
    size_t nb)
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;


@@ 200,7 201,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
		}
	}
#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) {


@@ 208,7 209,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
				(void)fprintf(stderr, "zmagic %d\n", m);
			goto done;
		}

#endif
	/* Check if we have a tar file */
	if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
		if ((m = file_is_tar(ms, ubuf, nb)) != 0) {

M src/magic.c => src/magic.c +81 -2
@@ 25,10 25,15 @@
 * SUCH DAMAGE.
 */

#ifdef WIN32
#include <windows.h>
#include <shlwapi.h>
#endif

#include "file.h"

#ifndef	lint
FILE_RCSID("@(#)$File: magic.c,v 1.64 2009/07/15 14:08:43 christos Exp $")
FILE_RCSID("@(#)$File: magic.c,v 1.65 2009/09/14 17:50:38 christos Exp $")
#endif	/* lint */

#include "magic.h"


@@ 81,14 86,33 @@ private const char *file_or_fd(struct magic_set *, const char *, int);
#define	STDIN_FILENO	0
#endif

#ifdef WIN32
BOOL WINAPI DllMain(HINSTANCE hinstDLL,
    DWORD fdwReason __attribute__((__unused__)),
    LPVOID lpvReserved __attribute__((__unused__)));

CHAR dllpath[MAX_PATH + 1] = { 0 };

BOOL WINAPI DllMain(HINSTANCE hinstDLL,
    DWORD fdwReason __attribute__((__unused__)),
    LPVOID lpvReserved __attribute__((__unused__)))
{
	if (dllpath[0] == 0 &&
	    GetModuleFileNameA(hinstDLL, dllpath, MAX_PATH) != 0)
		PathRemoveFileSpec(dllpath);
	return TRUE;
}
#endif

private const char *
get_default_magic(void)
{
	static const char hmagic[] = "/.magic";
	static char default_magic[2 * MAXPATHLEN + 2];
	char *home;
	char hmagicpath[MAXPATHLEN + 1];
	char hmagicpath[MAXPATHLEN + 1] = {0};

#ifndef WIN32
	if ((home = getenv("HOME")) == NULL)
		return MAGIC;



@@ 99,6 123,59 @@ get_default_magic(void)

	(void)snprintf(default_magic, sizeof(default_magic), "%s:%s",
	    hmagicpath, MAGIC);
#else
	char *hmagicp = hmagicpath;
	char tmppath[MAXPATHLEN + 1] = {0};
	char *hmagicend = &hmagicpath[sizeof(hmagicpath) - 1];

#define APPENDPATH() \
	if (access(tmppath, R_OK) != -1)
		hmagicp += snprintf(hmagicp, hmagicend - hmagicp, \
		    "%s%s", hmagicp == hmagicpath ? "" : ":", tmppath)
	/* First, try to get user-specific magic file */
	if ((home = getenv("LOCALAPPDATA")) == NULL) {
		if ((home = getenv("USERPROFILE")) != NULL)
			(void)snprintf(tmppath, sizeof(tmppath),
			    "%s/Local Settings/Application Data%s", home,
			    hmagic);
	} else {
		(void)snprintf(tmppath, sizeof(tmppath), "%s%s",
		    home, hmagic);
	}
	if (tmppath[0] != '\0') {
		APPENDPATH();
	}

	/* Second, try to get a magic file from Common Files */
	if ((home = getenv("COMMONPROGRAMFILES")) != NULL) {
		(void)snprintf(tmppath, sizeof(tmppath), "%s%s", home, hmagic);
		APPENDPATH();
	}


	/* Third, try to get magic file relative to dll location */
	if (dllpath[0] != 0) {
		if (strlen(dllpath) > 3 &&
		    stricmp(&dllpath[strlen(dllpath) - 3], "bin") == 0) {
			(void)snprintf(tmppath, sizeof(tmppath),
			    "%s/../share/misc/magic.mgc", dllpath);
			APPENDPATH();
		} else {
			(void)snprintf(tmppath, sizeof(tmppath),
			    "%s/share/misc/magic.mgc", dllpath);
			APPENDPATH()
			else {
				(void)snprintf(tmppath, sizeof(tmppath),
				    "%s/magic.mgc", dllpath);
				APPENDPATH();
			}
		}
	}

	/* Don't put MAGIC constant - it likely points to a file within MSys
	tree */
	(void)strlcpy(default_magic, hmagicpath, sizeof(default_magic));
#endif

	return default_magic;
}


@@ 315,7 392,9 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
		int flags = O_RDONLY|O_BINARY;

		if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
#ifdef O_NONBLOCK
			flags |= O_NONBLOCK;
#endif
			ispipe = 1;
		}


M src/print.c => src/print.c +2 -2
@@ 32,7 32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: print.c,v 1.67 2009/10/19 13:10:20 christos Exp $")
FILE_RCSID("@(#)$File: print.c,v 1.68 2009/11/18 23:35:14 christos Exp $")
#endif  /* lint */

#include <string.h>


@@ 120,7 120,7 @@ file_mdump(struct magic *m)
		case FILE_BEQUAD:
		case FILE_LEQUAD:
		case FILE_QUAD:
			(void) fprintf(stderr, "%lld",
			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
			    (unsigned long long)m->value.q);
			break;
		case FILE_PSTRING:

M src/readelf.c => src/readelf.c +8 -5
@@ 27,7 27,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: readelf.c,v 1.84 2010/04/22 16:54:17 christos Exp $")
FILE_RCSID("@(#)$File: readelf.c,v 1.85 2010/07/19 14:56:53 christos Exp $")
#endif

#ifdef BUILTIN_ELF


@@ 949,7 949,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
				default:
					if (file_printf(ms,
					    ", with unknown capability "
					    "0x%llx = 0x%llx",
					    "0x%" INT64_T_FORMAT "x = 0x%"
					    INT64_T_FORMAT "x",
					    (unsigned long long)xcap_tag,
					    (unsigned long long)xcap_val) == -1)
						return -1;


@@ 997,12 998,13 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
			}
			if (cap_hw1)
				if (file_printf(ms,
				    " unknown hardware capability 0x%llx",
				    " unknown hardware capability 0x%"
				    INT64_T_FORMAT "x",
				    (unsigned long long)cap_hw1) == -1)
					return -1;
		} else {
			if (file_printf(ms,
			    " hardware capability 0x%llx",
			    " hardware capability 0x%" INT64_T_FORMAT "x",
			    (unsigned long long)cap_hw1) == -1)
				return -1;
		}


@@ 1018,7 1020,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
		cap_sf1 &= ~SF1_SUNW_MASK;
		if (cap_sf1)
			if (file_printf(ms,
			    ", with unknown software capability 0x%llx",
			    ", with unknown software capability 0x%"
			    INT64_T_FORMAT "x",
			    (unsigned long long)cap_sf1) == -1)
				return -1;
	}

M src/softmagic.c => src/softmagic.c +28 -20
@@ 32,7 32,7 @@
#include "file.h"

#ifndef	lint
FILE_RCSID("@(#)$File: softmagic.c,v 1.138 2009/10/19 13:10:20 christos Exp $")
FILE_RCSID("@(#)$File: softmagic.c,v 1.139 2010/01/28 23:25:18 christos Exp $")
#endif	/* lint */

#include "magic.h"


@@ 1902,39 1902,41 @@ magiccheck(struct magic_set *ms, struct magic *m)
	switch (m->reln) {
	case 'x':
		if ((ms->flags & MAGIC_DEBUG) != 0)
			(void) fprintf(stderr, "%llu == *any* = 1\n",
			    (unsigned long long)v);
			(void) fprintf(stderr, "%" INT64_T_FORMAT
			    "u == *any* = 1\n", (unsigned long long)v);
		matched = 1;
		break;

	case '!':
		matched = v != l;
		if ((ms->flags & MAGIC_DEBUG) != 0)
			(void) fprintf(stderr, "%llu != %llu = %d\n",
			    (unsigned long long)v, (unsigned long long)l,
			    matched);
			(void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
			    INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
			    (unsigned long long)l, matched);
		break;

	case '=':
		matched = v == l;
		if ((ms->flags & MAGIC_DEBUG) != 0)
			(void) fprintf(stderr, "%llu == %llu = %d\n",
			    (unsigned long long)v, (unsigned long long)l,
			    matched);
			(void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
			    INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
			    (unsigned long long)l, matched);
		break;

	case '>':
		if (m->flag & UNSIGNED) {
			matched = v > l;
			if ((ms->flags & MAGIC_DEBUG) != 0)
				(void) fprintf(stderr, "%llu > %llu = %d\n",
				(void) fprintf(stderr, "%" INT64_T_FORMAT
				    "u > %" INT64_T_FORMAT "u = %d\n",
				    (unsigned long long)v,
				    (unsigned long long)l, matched);
		}
		else {
			matched = (int64_t) v > (int64_t) l;
			if ((ms->flags & MAGIC_DEBUG) != 0)
				(void) fprintf(stderr, "%lld > %lld = %d\n",
				(void) fprintf(stderr, "%" INT64_T_FORMAT
				    "d > %" INT64_T_FORMAT "d = %d\n",
				    (long long)v, (long long)l, matched);
		}
		break;


@@ 1943,32 1945,38 @@ magiccheck(struct magic_set *ms, struct magic *m)
		if (m->flag & UNSIGNED) {
			matched = v < l;
			if ((ms->flags & MAGIC_DEBUG) != 0)
				(void) fprintf(stderr, "%llu < %llu = %d\n",
				(void) fprintf(stderr, "%" INT64_T_FORMAT
				    "u < %" INT64_T_FORMAT "u = %d\n",
				    (unsigned long long)v,
				    (unsigned long long)l, matched);
		}
		else {
			matched = (int64_t) v < (int64_t) l;
			if ((ms->flags & MAGIC_DEBUG) != 0)
				(void) fprintf(stderr, "%lld < %lld = %d\n",
				       (long long)v, (long long)l, matched);
				(void) fprintf(stderr, "%" INT64_T_FORMAT
				    "d < %" INT64_T_FORMAT "d = %d\n",
				     (long long)v, (long long)l, matched);
		}
		break;

	case '&':
		matched = (v & l) == l;
		if ((ms->flags & MAGIC_DEBUG) != 0)
			(void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
			    (unsigned long long)v, (unsigned long long)l,
			    (unsigned long long)l, matched);
			(void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
			    INT64_T_FORMAT "x) == %" INT64_T_FORMAT
			    "x) = %d\n", (unsigned long long)v,
			    (unsigned long long)l, (unsigned long long)l,
			    matched);
		break;

	case '^':
		matched = (v & l) != l;
		if ((ms->flags & MAGIC_DEBUG) != 0)
			(void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
			    (unsigned long long)v, (unsigned long long)l,
			    (unsigned long long)l, matched);
			(void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
			    INT64_T_FORMAT "x) != %" INT64_T_FORMAT
			    "x) = %d\n", (unsigned long long)v,
			    (unsigned long long)l, (unsigned long long)l,
			    matched);
		break;

	default: