From 46758f44f9f39d3a39cf581a2f5bf37f73da515c Mon Sep 17 00:00:00 2001 From: Guido Draheim Date: Mon, 18 Aug 2003 16:16:03 +0000 Subject: [PATCH] This commit was generated by cvs2svn to compensate for changes in r107, which included commits to RCS files with non-trunk default branches. --- configure.ac | 315 ++++++++++++++++++++++++++++++++++++++++++++++++ zzip/conf.h | 230 +++++++++++++++++++++++++++++++++++ zzipwrap/wrap.c | 223 ++++++++++++++++++++++++++++++++++ 3 files changed, 768 insertions(+) create mode 100644 configure.ac create mode 100644 zzip/conf.h create mode 100644 zzipwrap/wrap.c diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..88aa1e9 --- /dev/null +++ b/configure.ac @@ -0,0 +1,315 @@ +AC_INIT(zziplib.spec) +AC_PREREQ(2.49) +AC_COPYRIGHT([Guido Draheim for ZZipLib.SF.net]) +AC_REVISION($Revision: 1.1.1.1 $) +AC_CONFIG_AUX_DIR(uses) +# ======================================================================= +AC_CANONICAL_SYSTEM +dnl AM_ENABLE_MULTILIB([Makefile],[$host]) +AX_ENABLE_BUILDDIR +AC_SET_DEFAULT_PATHS_SYSTEM +# ----------------------------------------------------------------------- +AC_ARG_ENABLE(thread-safe,AC_HELP_STRING( + [--disable-thread-safe],[disable thread-safe libtool option]) + ,,[ enable_thread_safe="yes"]) +THREAD_SAFE="" +test "$enable_thread_safe" = "yes" && THREAD_SAFE="-thread-safe" +test "$enable_thread_safe" = "$host_os" && THREAD_SAFE="-thread-safe" +AC_SUBST(THREAD_SAFE) +if test -n "$THREAD_SAFE" ; then + AC_MSG_RESULT(...using thread-safe linkage) +else + AC_MSG_RESULT(disabled thread-safe linkage) +fi +# ---------------------------------------------------------------- +AC_MSG_CHECKING(zlib install prefix) +AC_ARG_WITH(zlib, AC_HELP_STRING( + [--with-zlib],[zlib prefix (e.g. '$HOME/software')]) + ,,[with_zlib="no"]) +if test "${with_zlib}" = yes ; then # choose --prefix paths + if test "${prefix}" = NONE ; then + AC_MSG_RESULT(oops, but only itself...) + AC_MSG_ERROR(--with-zlib requires a path argument or a --prefix path set) + fi + with_zlib="$prefix" +fi + +case ".$with_zlib" in + .no|.no,*) + AC_MSG_RESULT(none) + with_zlib="no, not configured" ;; + ./usr) + AC_MSG_RESULT([oops, ignored (may try --with-zlib=/usr/.)]) + with_zlib="no, /usr ignored" ;; + *) if test ! -d "$with_zlib/include" ; then + AC_MSG_RESULT(oops, given but...) + AC_MSG_ERROR("$with_zlib/include" is not a directory) + fi + if test ! -d "$with_zlib/lib" ; then + AC_MSG_RESULT(oops, given but...) + AC_MSG_ERROR("$with_zlib/lib" is not a directory) + fi + AC_MSG_RESULT($with_zlib) + ZLIB_INCL="-I$with_zlib/include" + ZLIB_LDIR="-L$with_zlib/lib" + AC_MSG_RESULT(..adding CPPFLAGS $ZLIB_INCL) + AC_MSG_RESULT(..adding LDFLAGS $ZLIB_LDIR) + CPPFLAGS="$CPPFLAGS $ZLIB_INCL" + LDFLAGS="$LDFLAGS $ZLIB_LDIR" + ;; +esac +AC_SUBST(ZLIB_INCL) +AC_SUBST(ZLIB_LDIR) +# ----------------------------------------------------------------------- +AX_SPEC_DEFAULTS +AM_INIT_AUTOMAKE($PACKAGE,$VERSION) +dnl test ".$CFLAGS" = "." && CFLAGS="" +AC_PROG_CC +dnl test ".$CFLAGS" = "." && test "$GCC" = "yes" && CFLAGS="-O3" +AC_PROG_INSTALL +AC_LIBTOOL_WIN32_DLL +AM_PROG_LIBTOOL +AX_SET_VERSION_INFO + +# ----------------------------------------------------------------------- +AC_MSG_RESULT([creating zzip/_msvc.in via zzip/_msvc.sed of config.h.in]) +test -d zzip || mkdir zzip +sed -f $srcdir/zzip/_msvc.sed $srcdir/config.h.in >zzip/_msvc.in +AM_CONFIG_HEADER(config.h) +AX_PREFIX_CONFIG_H([zzip/_config.h],[zzip],[config.h]) +AX_PREFIX_CONFIG_H([zzip/_msvc.h],[zzip],[zzip/_msvc.in]) +AM_MAINTAINER_MODE + +AX_CREATE_PKGCONFIG_INFO(dnl +[zzip/zziplib.pc], [zzip-zlib-config],[-lzzip]) +# ----------------------------------------------------------------------- +AC_PATH_PROGS(PERL, perl5 perl, echo no perl found for) +AC_PATH_PROGS(PYTHON, python2.2 python2.3 python, echo no python found for) +AC_PATH_PROGS(MKZIP, zip pkzip, :) +AC_PATH_PROGS(XMLTO, xmlto, :) + +AC_C_INLINE +AC_C_CONST +AC_STDC_HEADERS +AC_HEADER_DIRENT +AC_CHECK_HEADERS(stdint.h unistd.h windows.h winnt.h winbase.h sys/int_types.h) +AC_CHECK_HEADERS(sys/types.h sys/mman.h sys/stat.h sys/param.h) dnl posix'ish +AC_CHECK_HEADERS(io.h direct.h zlib.h) +AC_CHECK_FUNCS( strcasecmp ) + +AC_TYPE_OFF_T +AC_TYPE_SIZE_T +dnl AC_TYPE_SSIZE_T +AC_CHECK_TYPE(ssize_t,int) +AC_COMPILE_CHECK_SIZEOF(short) +AC_COMPILE_CHECK_SIZEOF(int) +AC_COMPILE_CHECK_SIZEOF(long) +dnl ------------------------------------------------------------- +# the default is _disabled_!! anyway, result is prefixed in zzip/_config.h!! +AC_SYS_LARGEFILE_SENSITIVE +test ".$LARGEFILE_CFLAGS" = "." && LARGEFILE_CFLAGS="" +AC_ARG_WITH(largefile,AC_HELP_STRING( + [--with-largefile],[compile as off64_t instead of 32bit off_t]) + ,,[with_largefile="no"]) +if test ".$ac_cv_sys_largefile_sensitive" = ".no" ; then + AC_MSG_RESULT(compiles library with the only off_t seen) +elif test ".$with_largefile" != ".no" ; then + AC_MSG_RESULT(compiles library as 64bit off_t variant dnl +- and renaming some function names) + LARGEFILE_CFLAGS="$LARGEFILE_CFLAGS -D_LARGEFILE_SOURCE" + AC_MSG_RESULT(..adding CFLAGS $LARGEFILE_CFLAGS) + CFLAGS="$CFLAGS $LARGEFILE_CFLAGS" + AC_MSG_RESULT(..adding 64 into RELEASE_INFO for the libraries) + RELEASE_INFO="$RELEASE_INFO-64" +else + AC_MSG_RESULT(compiles library as 32bit off_t variant dnl +- largefile is not the default here) +fi +AC_SUBST(LARGEFILE_CFLAGS) +dnl ------------------------------------------------------------- +AC_MSG_CHECKING(for zlib being found) +if test "_$ac_cv_header_zlib_h" != "_yes" ; then + AC_MSG_RESULT(dnl +...oops: zlib.h not found - try adding some CPPFLAGS to configure call) + AC_MSG_RESULT(dnl +...note: and when linkage fails you might also need to set some LDFLAGS) + case "$prefix" in + NONE|/usr) + AC_MSG_RESULT(dnl +...note: both can be set by specifying the install prefix of the zlib library) + AC_MSG_RESULT(dnl +...note: via --with-zlib=path ... it will test for -Ipath/include -Lpath/lib) + ;; + *) if test -d $prefix/include ; then + AC_MSG_RESULT(dnl +...note: -I$prefix/include is not added to CPPFLAGS automatically and) + AC_MSG_RESULT(dnl +...note: neither is -L$prefix/lib for LDFLAGS .. both will be if you specify) + AC_MSG_RESULT(dnl +...note: --with-zlib to configure call, or use --with-zlib=installprefix) + fi + ;; + esac + AC_MSG_ERROR(zlib.h not found: "zlib" is required to build "$PACKAGE") + exit 1 +fi +cat >conftest.c < +const char* zlib_VeRsIoN = ZLIB_VERSION "dEtEcT"; +EOF +ZLIB_VERSION=`(eval "$ac_cpp conftest.c") 2>/dev/null \ +| grep zlib_VeRsIoN | sed -e 's,.*= *.,,' -e 's,. *.dEtEcT.*,,'` +rm conftest.c +AC_SUBST(ZLIB_VERSION) +AC_MSG_RESULT(yes - $ZLIB_VERSION) +# ---------------------------------------------------------------- +AC_MSG_CHECKING(docdir default) +AC_ARG_WITH(docdir, AC_HELP_STRING( + [--with-docdir],[docdir prefix (prefix/share/doc)]) + ,,[with_docdir="no"]) +case "${with_docdir}" in + prefix/*) docdir=` echo "$withval" | sed 's/^prefix/${prefix}'` ;; + datadir/*) docdir=` echo "$withval" | sed 's/^datadir/${datadir}'` ;; + yes) docdir='${prefix}/doc' ;; + no) docdir='${datadir}/doc' ;; + /*|\\*|[a-zA-Z]:*) docdir="$with_docdir" ;; + *) AC_MSG_RESULT(oops) + AC_MSG_ERROR(bad value ${withval} for --with-docdir) + ;; +esac +AC_SUBST(docdir) +AC_MSG_RESULT($docdir) +# ---------------------------------------------------------------- +AC_MSG_CHECKING(whether using debugging messages in libraries) +AC_ARG_WITH(debug, AC_HELP_STRING( + [--with-debug],[debug messages in library]) + , [case "${withval}" in + yes|no) with_debug="$withval" ;; + *) AC_MSG_RESULT(oops) + AC_MSG_ERROR(bad value ${withval} for --with-debug) ;; + esac], [with_debug="no"]) +test "$with_debug" != "yes" && with_debug="no" # yes or no +test "$with_debug" = "yes" && export CFLAGS="$CFLAGS -DDEBUG" +result="$with_debug" ; test "$result" = "yes" && result="yes, added -DDEBUG" +AC_MSG_RESULT($result) +if test "$with_debug" = "yes" ; then if test -n "$RELEASE_INFO" ; then + RELEASE_INFO="$RELEASE_INFO-dbg" + AC_MSG_RESULT(modified release, $RELEASE_INFO) +fi fi +# ---------------------------------------------------------------- +AC_MSG_CHECKING(whether thread stack is too small for bufsiz buffers) + # many embedded platforms, superthreaded systems and kernel usage + # will severely limit the stackspace for its threads. Use _LOWSTK + # compile-define on those platforms. +AC_ARG_WITH(lowstk, AC_HELP_STRING( + [--with-lowstk], [limited thread stack]) + , [case "${withval}" in + yes|no|auto) with_lowstk="$withval" ;; + *) AC_MSG_ERROR(bad value ${withval} for --with-lowstk) ;; + esac], [with_lowstk="auto"]) +if test "$with_lowstk" = "auto"; then + case $target in + *-palmos-*) $with_lowstk="yes" ;; + esac +fi +test "$with_lowstk" != "yes" && with_lowstk="no" # yes or no +test "$with_lowstk" = "yes" && CFLAGS="$CFLAGS -D_LOWSTK" +result="$with_lowstk" ; test "$result" = "yes" && result="yes, added -D_LOWSTK" +AC_MSG_RESULT($with_lowstk) +# --------------------------------------------------------------- +AC_MSG_CHECKING(whether can use mmap for file scanning) + # system with mmap are probably better performing an searching + # and parsing - atleast memory consumption is far better +AC_ARG_ENABLE(mmap, AC_HELP_STRING( + [--disable-mmap],[disable mmap usage]) + , [case "${enableval}" in + yes|no|auto) enable_mmap="${enableval}" ;; + *) AC_MSG_RESULT(oops) + AC_MSG_ERROR(bad value ${enableval} for --disable-mmap) ;; + esac], [enable_mmap="auto"]) +if test "$enable_mmap" = "auto" +then + test "$ac_cv_header_winbase_h" && enable_mmap="yes, winbase.h" + test "$ac_cv_header_sys_mman_h" && enable_mmap="yes, sys/mman.h" +fi +case "$enable_mmap" in + yes|yes,*) CFLAGS="$CFLAGS -D_USE_MMAP" + AC_MSG_RESULT($enable_mmap, added -D_USE_MMAP) ;; + *) enable_mmap="no" + AC_MSG_RESULT(no (just a bit slower)) ;; +esac +# --------------------------------------------------------------- +AC_MSG_CHECKING(whether making SDL parts) +SDL="" +AC_ARG_ENABLE(sdl, AC_HELP_STRING( + [--enable-sdl],[enable SDL usage (if present)]) + ,,[enable_sdl="no"]) +if test "$enable_sdl" = "no" ; then + AC_MSG_RESULT([no, disabled]) +else + enable_sdl=`sdl-config --version 2>/dev/null` + case ".$enable_sdl" in + .1.*) AC_MSG_RESULT([yes, for SDL $enable_sdl]) + SDL="SDL" ;; + .2.*) AC_MSG_RESULT([yes, untested $enable_sdl]) + SDL="SDL" ;; + *) AC_MSG_RESULT([no, $enable_sdl]) ;; + esac +fi +AC_SUBST(SDL) +# --------------------------------------------------------------- +AC_MSG_CHECKING(running extra tests on make check) +if test "MKZIP" != ":" ; then + AC_MSG_RESULT(yes) + ZIPTESTS="" +else + AC_MSG_RESULT(no) + ZIPTESTS="#" +fi +AC_SUBST(ZIPTESTS) +# --------------------------------------------------------------- +AC_MSG_CHECKING(link options) +case "$host_os" in + mingw*) + ZZIPLIB_LDFLAGS="-no-undefined -export-all-symbols -mconsole" + RESOLVES=' ' + ;; + *) + ZZIPLIB_LDFLAGS="--export-dynamic" + RESOLVES=' # ' + ;; +esac +AC_MSG_RESULT([$ZZIPLIB_LDFLAGS $RESOLVES]) +AC_SUBST([ZZIPLIB_LDFLAGS]) +AC_SUBST([RESOLVES]) +# --------------------------------------------------------------- +AX_MAINTAINER_MODE_AUTO_SILENT +AX_NOT_ENABLE_FRAME_POINTER +AX_CFLAGS_WARN_ALL +AX_CFLAGS_NO_WRITABLE_STRINGS +AX_CFLAGS_STRICT_PROTOTYPES +AX_CFLAGS_GCC_OPTION(,-Wpointer-arith,) +AX_CFLAGS_GCC_OPTION(,-Wsign-compare,) +AX_CFLAGS_GCC_OPTION(,-Wmissing-declarations,) +# --------------------------------------------------------------- +AX_WARNING_DEFAULT_PKGCONFIGDIR +AX_WARNING_DEFAULT_ACLOCALDIR +AC_SUBST([CONFIG_FILES]) +AC_OUTPUT([Makefile zzip/Makefile zzipwrap/Makefile SDL/Makefile + docs/Makefile test/Makefile bins/Makefile]) +dnl ------------------------------------------------------------- +PATCH_LIBTOOL_ON_DARWIN_ZSH_OVERQUOTING +PATCH_LIBTOOL_SYS_LIB_SEARCH_PATH_SPEC +PATCH_LIBTOOL_TO_ADD_HOST_CC +# --------------------------------------------------------------- +echo '-----' $PACKAGE $VERSION $prefix +$SHELL libtool --features || true +echo "CFLAGS=$CFLAGS" +test "$enable_mmap" != "no" && echo "enable mmap ($enable_mmap)" +test "$enable_mmap" = "no" && echo "disable mmap ($enable_mmap)" +test "$with_lowstk" != "no" && echo "with lowstk ($with_lowstk)" +test "$with_lowstk" = "no" && echo "without lowstk ($with_lowstk)" +test "$with_debug" != "no" && echo "with debug ($with_debug)" +test "$with_debug" = "no" && echo "without debug ($with_debug)" +# test -f ./zziplib.spec && echo warning - do not build in the source dirs +echo '# make && make check && make install' diff --git a/zzip/conf.h b/zzip/conf.h new file mode 100644 index 0000000..c0b89e2 --- /dev/null +++ b/zzip/conf.h @@ -0,0 +1,230 @@ +/* + * Author: + * Guido Draheim + * + * Copyright (c) 2001,2002,2003 Guido Draheim + * All rights reserved, + * use under the restrictions of the + * Lesser GNU General Public License + * or alternatively the restrictions + * of the Mozilla Public License 1.1 + */ + +#ifndef _ZZIP_CONF_H +#define _ZZIP_CONF_H 1 + +#if !defined ZZIP_OMIT_CONFIG_H +# if defined _MSC_VER || defined __BORLANDC__ || defined __WATCOMC__ +# include +# elif defined ZZIP_1_H +# include "zzip-1.h" +# elif defined ZZIP_2_H +# include "zzip-2.h" +# elif defined ZZIP_3_H +# include "zzip-3.h" +# elif defined ZZIP_4_H +# include "zzip-4.h" +# elif defined ZZIP_5_H +# include "zzip-5.h" +# else /* autoconf generated */ +# include +# endif +#endif + +/* especially win32 platforms do not declare off_t so far - see zzip-msvc.h */ +#ifndef _zzip_off_t +#ifdef ZZIP_off_t +#define _zzip_off_t ZZIP_off_t +#else +#define _zzip_off_t off_t +#endif +#endif + +/* currently unused, all current zziplib-users do have ansi-C94 compilers. */ +#ifndef _zzip_const +#ifdef ZZIP_const +#define _zzip_const ZZIP_const +#else +#define _zzip_const const +#endif +#endif +#ifndef _zzip_inline +#ifdef ZZIP_inline +#define _zzip_inline ZZIP_inline +#else +#define _zzip_inline inline +#endif +#endif +#ifndef _zzip_size_t +#ifdef ZZIP_size_t +#define _zzip_size_t ZZIP_size_t +#else +#define _zzip_size_t size_t +#endif +#endif +#ifndef _zzip_ssize_t +#ifdef ZZIP_ssize_t +#define _zzip_ssize_t ZZIP_ssize_t +#else +#define _zzip_ssize_t ssize_t +#endif +#endif + +/* whether this library shall use a 64bit off_t largefile variant in 64on32: */ +/* (some exported names must be renamed to avoid bad calls after linking) */ +#if defined ZZIP_LARGEFILE_SENSITIVE +# if _FILE_OFFSET_BITS+0 == 64 +# define ZZIP_LARGEFILE_RENAME +# elif defined _LARGE_FILES /* used on older AIX to get at 64bit off_t */ +# define ZZIP_LARGEFILE_RENAME +# elif defined _ZZIP_LARGEFILE /* or simply use this one for zzip64 runs */ +# define ZZIP_LARGEFILE_RENAME +# endif +#endif + +/* if the environment did not setup these for 64bit off_t largefile... */ +#ifdef ZZIP_LARGEFILE_RENAME +# ifndef _FILE_OFFSET_BITS +# ifdef ZZIP__FILE_OFFSET_BITS /* == 64 */ +# define _FILE_OFFSET_BITS ZZIP__FILE_OFFSET_BITS +# endif +# endif +# ifndef _LARGE_FILES +# ifdef ZZIP__LARGE_FILES /* == 1 */ +# define _LARGE_FILES ZZIP__LARGE_FILES +# endif +# endif +# ifndef _LARGEFILE_SOURCE +# ifdef ZZIP__LARGEFILE_SOURCE /* == 1 */ +# define _LARGEFILE_SOURCE ZZIP__LARGEFILE_SOURCE +# endif +# endif +#endif + +#include + +/* mingw32msvc errno : would be in winsock.h */ +#ifndef EREMOTE +#define EREMOTE ESPIPE +#endif + +#ifndef ELOOP +#if defined EILSEQ +#define ELOOP EILSEQ +#else +#define ELOOP ENOEXEC +#endif +#endif + +#if defined __WATCOMC__ +#undef _zzip_inline +#define _zzip_inline static +#endif + +#if defined _MSC_VER || defined __WATCOMC__ +#include +#endif + +#ifdef _MSC_VER +# if !__STDC__ +# ifndef _zzip_lseek +# define _zzip_lseek _lseek +# endif +# ifndef _zzip_read +# define _zzip_read _read +# endif +# ifndef _zzip_write +# define _zzip_write _write +# endif +/* +# ifndef _zzip_stat +# define _zzip_stat _stat +# endif +*/ +# endif /* !__STDC__ */ +#endif + /*MSVC*/ + +#if defined _MSC_VER || defined __WATCOMC__ +# ifndef strcasecmp +# define strcasecmp _stricmp +# endif +#endif + +# ifndef _zzip_lseek +# define _zzip_lseek lseek +# endif + +# ifndef _zzip_read +# define _zzip_read read +# endif + +# ifndef _zzip_write +# define _zzip_write write +# endif + +/* +# ifndef _zzip_stat +# define _zzip_stat stat +# endif +*/ + + +#if !defined __GNUC__ && !defined __attribute__ +#define __attribute__(X) +#endif + +#if defined ZZIP_EXPORTS || defined ZZIPLIB_EXPORTS +# undef ZZIP_DLL +#define ZZIP_DLL 1 +#endif + +/* based on zconf.h : */ +/* compile with -DZZIP_DLL for Windows DLL support */ +#if defined ZZIP_DLL +# if defined _WINDOWS || defined WINDOWS || defined _WIN32 +/*# include */ +# endif +# if !defined _zzip_export && defined _MSC_VER && (defined WIN32 || defined _WIN32) +# define _zzip_export __declspec(dllexport) /*WINAPI*/ +# endif +# if !defined _zzip_export && defined __BORLANDC__ +# if __BORLANDC__ >= 0x0500 && defined WIN32 +# include +# define _zzip_export __declspec(dllexport) /*WINAPI*/ +# else +# if defined _Windows && defined __DLL__ +# define _zzip_export _export +# endif +# endif +# endif +# if !defined _zzip_export && defined __GNUC__ +# if defined __declspec +# define _zzip_export extern __declspec(dllexport) +# else +# define _zzip_export extern +# endif +# endif +# if !defined _zzip_export && defined __BEOS__ +# define _zzip_export extern __declspec(export) +# endif +# if !defined _zzip_export && defined __WATCOMC__ +# define _zzip_export extern __declspec(dllexport) +# define ZEXPORT __syscall +# define ZEXTERN extern +# endif +#endif + +#if !defined _zzip_export +# if defined __GNUC__ /* || !defined HAVE_LIBZZIP */ +# define _zzip_export extern +# elif defined __declspec || (defined _MSC_VER && defined ZZIP_DLL) +# define _zzip_export extern __declspec(dllimport) +# else +# define _zzip_export extern +# endif +#endif + +#endif + + diff --git a/zzipwrap/wrap.c b/zzipwrap/wrap.c new file mode 100644 index 0000000..2bea0c3 --- /dev/null +++ b/zzipwrap/wrap.c @@ -0,0 +1,223 @@ + +/* Wrapper interface for zziplib's file IO functions. + * + * -- In memory-access IO function -- + * Provides default fileio functions open/read/lseek/close and functions + * that enable blocked, memory buffered IO. + * + * (c) A. Schiffler, aschiffler@home.com + * Released under same license as zziplib (LGPL), Oct 2001 + * + * modified 2002 to use new plugin_io interface by Guido Draheim + */ + +#include +#include +/* #incl */ +#include "wrap.h" + +#include +#include +#include + +#ifdef ZZIP_HAVE_UNISTD_H +#include +#endif +#include +#include +#include + +/* Some globals that we use for operation */ + +static int zzip_memory_blocksize=0; +static zzipwrap_pfn_t zzip_memory_callback=NULL; +static void *zzip_memory_callbackdata=NULL; +static char *zzip_memory_buffer=NULL; +static int zzip_memory_bufferblock=0; +static int zzip_memory_fd=-1; +static int zzip_memory_pos=0; +static int zzip_memory_size=0; + +/* at the moment, we do not export the five handlers of zzip_memry plugin_io */ +#define ZZIP_memory static + +ZZIP_memory void zzip_memory_reset(void) +{ + zzip_memory_buffer=NULL; + zzip_memory_bufferblock=0; + zzip_memory_pos=0; + zzip_memory_size=0; + zzip_memory_fd=-1; +} + +ZZIP_memory int zzip_memory_read(int fd, char *buf, size_t count) +{ + int requested_block; + int transfer_start; + int transfer_bytes; + int bytes_read; + int bytes_to_write; + int bytes_written; + + /* Check file descriptor */ + if (fd!=zzip_memory_fd) { + return(-1); + } + + if ((int)count<0) + return -1; + + /* Work out how many bytes we have to read based */ + /* on filepos and requested buffersize. */ + bytes_to_write=zzip_memory_size-zzip_memory_pos; + if (bytes_to_write>=(int)count) { + bytes_to_write=(int)count; + } + + /* Read/transfer loop */ + bytes_written=0; + while (bytes_to_write>0) { + /* Check which block we are reading from */ + requested_block=zzip_memory_pos/zzip_memory_blocksize; + /* Check if we have this block available */ + if (zzip_memory_bufferblock!=requested_block) { + /* Read this block */ + lseek(fd,requested_block*zzip_memory_blocksize,SEEK_SET); + bytes_read=read(fd,zzip_memory_buffer,zzip_memory_blocksize); + /* Clear unused bytes */ + if ((bytes_read>=0) && (bytes_read 0) { + /* Copy data to output buffer */ + transfer_start=zzip_memory_pos-zzip_memory_bufferblock*zzip_memory_blocksize; + transfer_bytes=zzip_memory_blocksize-transfer_start; + if (transfer_bytes>bytes_to_write) { + transfer_bytes=bytes_to_write; + } + memcpy (&buf[bytes_written], &zzip_memory_buffer[transfer_start],transfer_bytes); + zzip_memory_pos += transfer_bytes; + bytes_written += transfer_bytes; + bytes_to_write -= transfer_bytes; + } else { + /* Exit on error during read */ + bytes_to_write=0; + } + } + return(bytes_written); +} + +ZZIP_memory int zzip_memory_open(const char *pathname, int flags) +{ + int fd; + struct stat stat_buffer; + + /* Deallocate any memory buffer laying around */ + if (zzip_memory_buffer) { + free(zzip_memory_buffer); + } + /* Reset variables */ + zzip_memory_reset(); + /* Open file */ + fd=open(pathname, flags); + if (fd<0) { + return(-1); + } else { + /* Get filesize */ + if (fstat(fd, &stat_buffer)<0) { + return(-1); + } + if (stat_buffer.st_size<1) { + return(-1); + } + /* Store filesize */ + zzip_memory_size = stat_buffer.st_size; + /* Allocate readbuffer */ + zzip_memory_buffer = malloc(zzip_memory_blocksize); + /* Invalidate buffer block */ + zzip_memory_bufferblock = -1; + /* Store file descriptor */ + zzip_memory_fd=fd; + /* Return file descriptor */ + return(fd); + } +} + +ZZIP_memory int zzip_memory_close(int fd) +{ + /* Check file descriptor */ + if (fd!=zzip_memory_fd) { + return(-1); + } + /* Clear any memory buffer that might be laying around */ + if (zzip_memory_buffer) { + free(zzip_memory_buffer); + } + /* Reset variables */ + zzip_memory_reset(); + /* Close file */ + return(close(fd)); +} + +ZZIP_memory off_t zzip_memory_lseek(int fildes, off_t offset, int whence) +{ + /* Check file descriptor */ + if (fildes!=zzip_memory_fd) { + return(-1); + } + /* Change position pointer */ + switch (whence) { + case SEEK_SET: + zzip_memory_pos=offset; + break; + case SEEK_CUR: + zzip_memory_pos += offset; + break; + case SEEK_END: + zzip_memory_pos = zzip_memory_size-1; + break; + } + /* Limit position to the last byte of the file */ + if (zzip_memory_pos>(zzip_memory_size-1)) { + zzip_memory_pos=zzip_memory_size-1; + } + /* Return current position */ + return(zzip_memory_pos); +} + +/* -------- Control wrapper usage */ + + +zzip_plugin_io_t +zzipwrap_use_memory_io(int blocksize, + zzipwrap_pfn_t callback, void *callbackdata) +{ + static const struct zzip_plugin_io zzip_memory_io = + { + (void *) zzip_memory_open, + (void *) zzip_memory_close, + (void *) zzip_memory_read, + (void *) zzip_memory_lseek, + (void *) zzip_filesize, + 0 + }; + + /* Store blocksize and block-processing callback data */ + + zzip_memory_blocksize = blocksize; + zzip_memory_callback = callback; + zzip_memory_callbackdata = callbackdata; + + return (zzip_plugin_io_t) &zzip_memory_io; +} + -- 2.40.0