kusano fc6ab3
/* ioapi.h -- IO base function header for compress/uncompress .zip
kusano fc6ab3
   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
kusano fc6ab3
kusano fc6ab3
         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
kusano fc6ab3
kusano fc6ab3
         Modifications for Zip64 support
kusano fc6ab3
         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
kusano fc6ab3
kusano fc6ab3
         For more info read MiniZip_info.txt
kusano fc6ab3
kusano fc6ab3
*/
kusano fc6ab3
kusano fc6ab3
#if defined(_WIN32) && (!(defined(_CRT_SECURE_NO_WARNINGS)))
kusano fc6ab3
        #define _CRT_SECURE_NO_WARNINGS
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
#if defined(__APPLE__) || defined(IOAPI_NO_64)
kusano fc6ab3
// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
kusano fc6ab3
#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
kusano fc6ab3
#define FTELLO_FUNC(stream) ftello(stream)
kusano fc6ab3
#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
kusano fc6ab3
#else
kusano fc6ab3
#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
kusano fc6ab3
#define FTELLO_FUNC(stream) ftello64(stream)
kusano fc6ab3
#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
#include "ioapi.h"
kusano fc6ab3
kusano fc6ab3
voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
kusano fc6ab3
{
kusano fc6ab3
    if (pfilefunc->zfile_func64.zopen64_file != NULL)
kusano fc6ab3
        return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
kusano fc6ab3
    }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
kusano fc6ab3
{
kusano fc6ab3
    if (pfilefunc->zfile_func64.zseek64_file != NULL)
kusano fc6ab3
        return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        uLong offsetTruncated = (uLong)offset;
kusano fc6ab3
        if (offsetTruncated != offset)
kusano fc6ab3
            return -1;
kusano fc6ab3
        else
kusano fc6ab3
            return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
kusano fc6ab3
    }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
kusano fc6ab3
{
kusano fc6ab3
    if (pfilefunc->zfile_func64.zseek64_file != NULL)
kusano fc6ab3
        return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
kusano fc6ab3
        if ((tell_uLong) == MAXU32)
kusano fc6ab3
            return (ZPOS64_T)-1;
kusano fc6ab3
        else
kusano fc6ab3
            return tell_uLong;
kusano fc6ab3
    }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
kusano fc6ab3
{
kusano fc6ab3
    p_filefunc64_32->zfile_func64.zopen64_file = NULL;
kusano fc6ab3
    p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.ztell64_file = NULL;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.zseek64_file = NULL;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
kusano fc6ab3
    p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
kusano fc6ab3
    p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
kusano fc6ab3
    p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
static voidpf  ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
kusano fc6ab3
static uLong   ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
kusano fc6ab3
static uLong   ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
kusano fc6ab3
static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
kusano fc6ab3
static long    ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
kusano fc6ab3
static int     ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
kusano fc6ab3
static int     ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
kusano fc6ab3
kusano fc6ab3
static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
kusano fc6ab3
{
kusano fc6ab3
    FILE* file = NULL;
kusano fc6ab3
    const char* mode_fopen = NULL;
kusano fc6ab3
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
kusano fc6ab3
        mode_fopen = "rb";
kusano fc6ab3
    else
kusano fc6ab3
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
kusano fc6ab3
        mode_fopen = "r+b";
kusano fc6ab3
    else
kusano fc6ab3
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
kusano fc6ab3
        mode_fopen = "wb";
kusano fc6ab3
kusano fc6ab3
    if ((filename!=NULL) && (mode_fopen != NULL))
kusano fc6ab3
        file = fopen(filename, mode_fopen);
kusano fc6ab3
    return file;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
kusano fc6ab3
{
kusano fc6ab3
    FILE* file = NULL;
kusano fc6ab3
    const char* mode_fopen = NULL;
kusano fc6ab3
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
kusano fc6ab3
        mode_fopen = "rb";
kusano fc6ab3
    else
kusano fc6ab3
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
kusano fc6ab3
        mode_fopen = "r+b";
kusano fc6ab3
    else
kusano fc6ab3
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
kusano fc6ab3
        mode_fopen = "wb";
kusano fc6ab3
kusano fc6ab3
    if ((filename!=NULL) && (mode_fopen != NULL))
kusano fc6ab3
        file = FOPEN_FUNC((const char*)filename, mode_fopen);
kusano fc6ab3
    return file;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
kusano fc6ab3
{
kusano fc6ab3
    uLong ret;
kusano fc6ab3
    ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
kusano fc6ab3
{
kusano fc6ab3
    uLong ret;
kusano fc6ab3
    ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
kusano fc6ab3
{
kusano fc6ab3
    long ret;
kusano fc6ab3
    ret = ftell((FILE *)stream);
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
kusano fc6ab3
{
kusano fc6ab3
    ZPOS64_T ret;
kusano fc6ab3
    ret = FTELLO_FUNC((FILE *)stream);
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
static long ZCALLBACK fseek_file_func (voidpf  opaque, voidpf stream, uLong offset, int origin)
kusano fc6ab3
{
kusano fc6ab3
    int fseek_origin=0;
kusano fc6ab3
    long ret;
kusano fc6ab3
    switch (origin)
kusano fc6ab3
    {
kusano fc6ab3
    case ZLIB_FILEFUNC_SEEK_CUR :
kusano fc6ab3
        fseek_origin = SEEK_CUR;
kusano fc6ab3
        break;
kusano fc6ab3
    case ZLIB_FILEFUNC_SEEK_END :
kusano fc6ab3
        fseek_origin = SEEK_END;
kusano fc6ab3
        break;
kusano fc6ab3
    case ZLIB_FILEFUNC_SEEK_SET :
kusano fc6ab3
        fseek_origin = SEEK_SET;
kusano fc6ab3
        break;
kusano fc6ab3
    default: return -1;
kusano fc6ab3
    }
kusano fc6ab3
    ret = 0;
kusano fc6ab3
    if (fseek((FILE *)stream, offset, fseek_origin) != 0)
kusano fc6ab3
        ret = -1;
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
static long ZCALLBACK fseek64_file_func (voidpf  opaque, voidpf stream, ZPOS64_T offset, int origin)
kusano fc6ab3
{
kusano fc6ab3
    int fseek_origin=0;
kusano fc6ab3
    long ret;
kusano fc6ab3
    switch (origin)
kusano fc6ab3
    {
kusano fc6ab3
    case ZLIB_FILEFUNC_SEEK_CUR :
kusano fc6ab3
        fseek_origin = SEEK_CUR;
kusano fc6ab3
        break;
kusano fc6ab3
    case ZLIB_FILEFUNC_SEEK_END :
kusano fc6ab3
        fseek_origin = SEEK_END;
kusano fc6ab3
        break;
kusano fc6ab3
    case ZLIB_FILEFUNC_SEEK_SET :
kusano fc6ab3
        fseek_origin = SEEK_SET;
kusano fc6ab3
        break;
kusano fc6ab3
    default: return -1;
kusano fc6ab3
    }
kusano fc6ab3
    ret = 0;
kusano fc6ab3
kusano fc6ab3
    if(FSEEKO_FUNC((FILE *)stream, offset, fseek_origin) != 0)
kusano fc6ab3
                        ret = -1;
kusano fc6ab3
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
kusano fc6ab3
{
kusano fc6ab3
    int ret;
kusano fc6ab3
    ret = fclose((FILE *)stream);
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
kusano fc6ab3
{
kusano fc6ab3
    int ret;
kusano fc6ab3
    ret = ferror((FILE *)stream);
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void fill_fopen_filefunc (pzlib_filefunc_def)
kusano fc6ab3
  zlib_filefunc_def* pzlib_filefunc_def;
kusano fc6ab3
{
kusano fc6ab3
    pzlib_filefunc_def->zopen_file = fopen_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zread_file = fread_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
kusano fc6ab3
    pzlib_filefunc_def->ztell_file = ftell_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zseek_file = fseek_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zclose_file = fclose_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zerror_file = ferror_file_func;
kusano fc6ab3
    pzlib_filefunc_def->opaque = NULL;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void fill_fopen64_filefunc (zlib_filefunc64_def*  pzlib_filefunc_def)
kusano fc6ab3
{
kusano fc6ab3
    pzlib_filefunc_def->zopen64_file = fopen64_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zread_file = fread_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
kusano fc6ab3
    pzlib_filefunc_def->ztell64_file = ftell64_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zseek64_file = fseek64_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zclose_file = fclose_file_func;
kusano fc6ab3
    pzlib_filefunc_def->zerror_file = ferror_file_func;
kusano fc6ab3
    pzlib_filefunc_def->opaque = NULL;
kusano fc6ab3
}