Changeset a0caafe in rtems


Ignore:
Timestamp:
Mar 18, 2011, 10:11:15 AM (9 years ago)
Author:
Ralf Corsepius <ralf.corsepius@…>
Branches:
4.11, master
Children:
5204405, ae044bc
Parents:
e8f3eb9
Message:

Import from zlib-1.2.4

Location:
cpukit/zlib/contrib/minizip
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • cpukit/zlib/contrib/minizip/ioapi.h

    re8f3eb9 ra0caafe  
    11/* ioapi.h -- IO base function header for compress/uncompress .zip
    2    files using zlib + zip or unzip API
    3 
    4    Version 1.01e, February 12th, 2005
    5 
    6    Copyright (C) 1998-2005 Gilles Vollant
     2   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     3
     4         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     5
     6         Modifications for Zip64 support
     7         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
     8
     9         For more info read MiniZip_info.txt
     10
     11         Changes
     12
     13    Oct-2009 - Defined ZPOS64_T to fpos_t on windows and u_int64_t on linux. (might need to find a better why for this)
     14    Oct-2009 - Change to fseeko64, ftello64 and fopen64 so large files would work on linux.
     15               More if/def section may be needed to support other platforms
     16    Oct-2009 - Defined fxxxx64 calls to normal fopen/ftell/fseek so they would compile on windows.
     17                          (but you should use iowin32.c for windows instead)
     18
    719*/
    820
    9 #ifndef _ZLIBIOAPI_H
    10 #define _ZLIBIOAPI_H
     21#ifndef _ZLIBIOAPI64_H
     22#define _ZLIBIOAPI64_H
     23
     24#if (!defined(_WIN32)) && (!defined(WIN32))
     25
     26  // Linux needs this to support file operation on files larger then 4+GB
     27  // But might need better if/def to select just the platforms that needs them.
     28
     29        #ifndef __USE_FILE_OFFSET64
     30                #define __USE_FILE_OFFSET64
     31        #endif
     32        #ifndef __USE_LARGEFILE64
     33                #define __USE_LARGEFILE64
     34        #endif
     35        #ifndef _LARGEFILE64_SOURCE
     36                #define _LARGEFILE64_SOURCE
     37        #endif
     38        #ifndef _FILE_OFFSET_BIT
     39                #define _FILE_OFFSET_BIT 64
     40        #endif
     41#endif
     42
     43#include <stdio.h>
     44#include <stdlib.h>
     45#include "zlib.h"
     46
     47#if defined(USE_FILE32API)
     48#define fopen64 fopen
     49#define ftello64 ftell
     50#define fseeko64 fseek
     51#else
     52#ifdef _MSC_VER
     53 #define fopen64 fopen
     54 #if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
     55  #define ftello64 _ftelli64
     56  #define fseeko64 _fseeki64
     57 #else // old MSC
     58  #define ftello64 ftell
     59  #define fseeko64 fseek
     60 #endif
     61#endif
     62#endif
     63
     64/*
     65#ifndef ZPOS64_T
     66  #ifdef _WIN32
     67                #define ZPOS64_T fpos_t
     68  #else
     69    #include <stdint.h>
     70    #define ZPOS64_T uint64_t
     71  #endif
     72#endif
     73*/
     74
     75#ifdef HAVE_MINIZIP64_CONF_H
     76#include "mz64conf.h"
     77#endif
     78
     79/* a type choosen by DEFINE */
     80#ifdef HAVE_64BIT_INT_CUSTOM
     81typedef  64BIT_INT_CUSTOM_TYPE ZPOS64_T;
     82#else
     83#ifdef HAS_STDINT_H
     84#include "stdint.h"
     85typedef uint64_t ZPOS64_T;
     86#else
     87
     88
     89#if defined(_MSC_VER) || defined(__BORLANDC__)
     90typedef unsigned __int64 ZPOS64_T;
     91#else
     92typedef unsigned long long int ZPOS64_T;
     93#endif
     94#endif
     95#endif
     96
     97
     98
     99#ifdef __cplusplus
     100extern "C" {
     101#endif
    11102
    12103
     
    24115
    25116#ifndef ZCALLBACK
    26 
    27 #if (defined(WIN32) || defined (WINDOWS) || defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK)
    28 #define ZCALLBACK CALLBACK
    29 #else
    30 #define ZCALLBACK
    31 #endif
    32 #endif
    33 
    34 #ifdef __cplusplus
    35 extern "C" {
    36 #endif
    37 
    38 typedef voidpf (ZCALLBACK *open_file_func) OF((voidpf opaque, const char* filename, int mode));
    39 typedef uLong  (ZCALLBACK *read_file_func) OF((voidpf opaque, voidpf stream, void* buf, uLong size));
    40 typedef uLong  (ZCALLBACK *write_file_func) OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
    41 typedef long   (ZCALLBACK *tell_file_func) OF((voidpf opaque, voidpf stream));
    42 typedef long   (ZCALLBACK *seek_file_func) OF((voidpf opaque, voidpf stream, uLong offset, int origin));
    43 typedef int    (ZCALLBACK *close_file_func) OF((voidpf opaque, voidpf stream));
    44 typedef int    (ZCALLBACK *testerror_file_func) OF((voidpf opaque, voidpf stream));
    45 
     117 #if (defined(WIN32) || defined(_WIN32) || defined (WINDOWS) || defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK)
     118   #define ZCALLBACK CALLBACK
     119 #else
     120   #define ZCALLBACK
     121 #endif
     122#endif
     123
     124
     125
     126
     127typedef voidpf   (ZCALLBACK *open_file_func)      OF((voidpf opaque, const char* filename, int mode));
     128typedef uLong    (ZCALLBACK *read_file_func)      OF((voidpf opaque, voidpf stream, void* buf, uLong size));
     129typedef uLong    (ZCALLBACK *write_file_func)     OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
     130typedef int      (ZCALLBACK *close_file_func)     OF((voidpf opaque, voidpf stream));
     131typedef int      (ZCALLBACK *testerror_file_func) OF((voidpf opaque, voidpf stream));
     132
     133typedef long     (ZCALLBACK *tell_file_func)      OF((voidpf opaque, voidpf stream));
     134typedef long     (ZCALLBACK *seek_file_func)      OF((voidpf opaque, voidpf stream, uLong offset, int origin));
     135
     136
     137/* here is the "old" 32 bits structure structure */
    46138typedef struct zlib_filefunc_def_s
    47139{
     
    56148} zlib_filefunc_def;
    57149
    58 
    59 
     150typedef ZPOS64_T (ZCALLBACK *tell64_file_func)    OF((voidpf opaque, voidpf stream));
     151typedef long     (ZCALLBACK *seek64_file_func)    OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
     152typedef voidpf   (ZCALLBACK *open64_file_func)    OF((voidpf opaque, const void* filename, int mode));
     153
     154typedef struct zlib_filefunc64_def_s
     155{
     156    open64_file_func    zopen64_file;
     157    read_file_func      zread_file;
     158    write_file_func     zwrite_file;
     159    tell64_file_func    ztell64_file;
     160    seek64_file_func    zseek64_file;
     161    close_file_func     zclose_file;
     162    testerror_file_func zerror_file;
     163    voidpf              opaque;
     164} zlib_filefunc64_def;
     165
     166void fill_fopen64_filefunc OF((zlib_filefunc64_def* pzlib_filefunc_def));
    60167void fill_fopen_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def));
    61168
    62 #define ZREAD(filefunc,filestream,buf,size) ((*((filefunc).zread_file))((filefunc).opaque,filestream,buf,size))
    63 #define ZWRITE(filefunc,filestream,buf,size) ((*((filefunc).zwrite_file))((filefunc).opaque,filestream,buf,size))
    64 #define ZTELL(filefunc,filestream) ((*((filefunc).ztell_file))((filefunc).opaque,filestream))
    65 #define ZSEEK(filefunc,filestream,pos,mode) ((*((filefunc).zseek_file))((filefunc).opaque,filestream,pos,mode))
    66 #define ZCLOSE(filefunc,filestream) ((*((filefunc).zclose_file))((filefunc).opaque,filestream))
    67 #define ZERROR(filefunc,filestream) ((*((filefunc).zerror_file))((filefunc).opaque,filestream))
    68 
     169/* now internal definition, only for zip.c and unzip.h */
     170typedef struct zlib_filefunc64_32_def_s
     171{
     172    zlib_filefunc64_def zfile_func64;
     173    open_file_func      zopen32_file;
     174    tell_file_func      ztell32_file;
     175    seek_file_func      zseek32_file;
     176} zlib_filefunc64_32_def;
     177
     178
     179#define ZREAD64(filefunc,filestream,buf,size)     ((*((filefunc).zfile_func64.zread_file))   ((filefunc).zfile_func64.opaque,filestream,buf,size))
     180#define ZWRITE64(filefunc,filestream,buf,size)    ((*((filefunc).zfile_func64.zwrite_file))  ((filefunc).zfile_func64.opaque,filestream,buf,size))
     181//#define ZTELL64(filefunc,filestream)            ((*((filefunc).ztell64_file)) ((filefunc).opaque,filestream))
     182//#define ZSEEK64(filefunc,filestream,pos,mode)   ((*((filefunc).zseek64_file)) ((filefunc).opaque,filestream,pos,mode))
     183#define ZCLOSE64(filefunc,filestream)             ((*((filefunc).zfile_func64.zclose_file))  ((filefunc).zfile_func64.opaque,filestream))
     184#define ZERROR64(filefunc,filestream)             ((*((filefunc).zfile_func64.zerror_file))  ((filefunc).zfile_func64.opaque,filestream))
     185
     186voidpf call_zopen64 OF((const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode));
     187long    call_zseek64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin));
     188ZPOS64_T call_ztell64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream));
     189
     190void    fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32);
     191
     192#define ZOPEN64(filefunc,filename,mode)         (call_zopen64((&(filefunc)),(filename),(mode)))
     193#define ZTELL64(filefunc,filestream)            (call_ztell64((&(filefunc)),(filestream)))
     194#define ZSEEK64(filefunc,filestream,pos,mode)   (call_zseek64((&(filefunc)),(filestream),(pos),(mode)))
    69195
    70196#ifdef __cplusplus
     
    73199
    74200#endif
    75 
  • cpukit/zlib/contrib/minizip/iowin32.c

    re8f3eb9 ra0caafe  
    11/* iowin32.c -- IO base function header for compress/uncompress .zip
    2    files using zlib + zip or unzip API
    3    This IO API version uses the Win32 API (for Microsoft Windows)
    4 
    5    Version 1.01e, February 12th, 2005
    6 
    7    Copyright (C) 1998-2005 Gilles Vollant
     2     Version 1.1, February 14h, 2010
     3     part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     4
     5         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     6
     7         Modifications for Zip64 support
     8         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
     9
     10     For more info read MiniZip_info.txt
     11
    812*/
    913
     
    2226#endif
    2327
    24 voidpf ZCALLBACK win32_open_file_func OF((
    25    voidpf opaque,
    26    const char* filename,
    27    int mode));
    28 
    29 uLong ZCALLBACK win32_read_file_func OF((
    30    voidpf opaque,
    31    voidpf stream,
    32    void* buf,
    33    uLong size));
    34 
    35 uLong ZCALLBACK win32_write_file_func OF((
    36    voidpf opaque,
    37    voidpf stream,
    38    const void* buf,
    39    uLong size));
    40 
    41 long ZCALLBACK win32_tell_file_func OF((
    42    voidpf opaque,
    43    voidpf stream));
    44 
    45 long ZCALLBACK win32_seek_file_func OF((
    46    voidpf opaque,
    47    voidpf stream,
    48    uLong offset,
    49    int origin));
    50 
    51 int ZCALLBACK win32_close_file_func OF((
    52    voidpf opaque,
    53    voidpf stream));
    54 
    55 int ZCALLBACK win32_error_file_func OF((
    56    voidpf opaque,
    57    voidpf stream));
     28voidpf  ZCALLBACK win32_open_file_func  OF((voidpf opaque, const char* filename, int mode));
     29uLong   ZCALLBACK win32_read_file_func  OF((voidpf opaque, voidpf stream, void* buf, uLong size));
     30uLong   ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
     31ZPOS64_T ZCALLBACK win32_tell64_file_func  OF((voidpf opaque, voidpf stream));
     32long    ZCALLBACK win32_seek64_file_func  OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
     33int     ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream));
     34int     ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream));
    5835
    5936typedef struct
     
    6340} WIN32FILE_IOWIN;
    6441
    65 voidpf ZCALLBACK win32_open_file_func (opaque, filename, mode)
    66    voidpf opaque;
    67    const char* filename;
    68    int mode;
    69 {
    70     const char* mode_fopen = NULL;
    71     DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
    72     HANDLE hFile = 0;
     42
     43static void win32_translate_open_mode(int mode,
     44                                      DWORD* lpdwDesiredAccess,
     45                                      DWORD* lpdwCreationDisposition,
     46                                      DWORD* lpdwShareMode,
     47                                      DWORD* lpdwFlagsAndAttributes)
     48{
     49    *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0;
     50
     51    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
     52    {
     53        *lpdwDesiredAccess = GENERIC_READ;
     54        *lpdwCreationDisposition = OPEN_EXISTING;
     55        *lpdwShareMode = FILE_SHARE_READ;
     56    }
     57    else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
     58    {
     59        *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
     60        *lpdwCreationDisposition = OPEN_EXISTING;
     61    }
     62    else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
     63    {
     64        *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
     65        *lpdwCreationDisposition = CREATE_ALWAYS;
     66    }
     67}
     68
     69static voidpf win32_build_iowin(HANDLE hFile)
     70{
    7371    voidpf ret=NULL;
    7472
    75     dwDesiredAccess = dwShareMode = dwFlagsAndAttributes = 0;
    76 
    77     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
    78     {
    79         dwDesiredAccess = GENERIC_READ;
    80         dwCreationDisposition = OPEN_EXISTING;
    81         dwShareMode = FILE_SHARE_READ;
    82     }
    83     else
    84     if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
    85     {
    86         dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
    87         dwCreationDisposition = OPEN_EXISTING;
    88     }
    89     else
    90     if (mode & ZLIB_FILEFUNC_MODE_CREATE)
    91     {
    92         dwDesiredAccess = GENERIC_WRITE | GENERIC_READ;
    93         dwCreationDisposition = CREATE_ALWAYS;
    94     }
    95 
    96     if ((filename!=NULL) && (dwDesiredAccess != 0))
    97         hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL,
    98                       dwCreationDisposition, dwFlagsAndAttributes, NULL);
    99 
    100     if (hFile == INVALID_HANDLE_VALUE)
    101         hFile = NULL;
    102 
    103     if (hFile != NULL)
     73    if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
    10474    {
    10575        WIN32FILE_IOWIN w32fiow;
     
    10777        w32fiow.error = 0;
    10878        ret = malloc(sizeof(WIN32FILE_IOWIN));
     79
    10980        if (ret==NULL)
    11081            CloseHandle(hFile);
    111         else *((WIN32FILE_IOWIN*)ret) = w32fiow;
    112     }
    113     return ret;
    114 }
    115 
    116 
    117 uLong ZCALLBACK win32_read_file_func (opaque, stream, buf, size)
    118    voidpf opaque;
    119    voidpf stream;
    120    void* buf;
    121    uLong size;
     82        else
     83            *((WIN32FILE_IOWIN*)ret) = w32fiow;
     84    }
     85    return ret;
     86}
     87
     88voidpf ZCALLBACK win32_open64_file_func (voidpf opaque,const void* filename,int mode)
     89{
     90    const char* mode_fopen = NULL;
     91    DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
     92    HANDLE hFile = NULL;
     93
     94    win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
     95
     96    if ((filename!=NULL) && (dwDesiredAccess != 0))
     97        hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
     98
     99    return win32_build_iowin(hFile);
     100}
     101
     102
     103voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque,const void* filename,int mode)
     104{
     105    const char* mode_fopen = NULL;
     106    DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
     107    HANDLE hFile = NULL;
     108
     109    win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
     110
     111    if ((filename!=NULL) && (dwDesiredAccess != 0))
     112        hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
     113
     114    return win32_build_iowin(hFile);
     115}
     116
     117
     118voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,const void* filename,int mode)
     119{
     120    const char* mode_fopen = NULL;
     121    DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
     122    HANDLE hFile = NULL;
     123
     124    win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
     125
     126    if ((filename!=NULL) && (dwDesiredAccess != 0))
     127        hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
     128
     129    return win32_build_iowin(hFile);
     130}
     131
     132
     133voidpf ZCALLBACK win32_open_file_func (voidpf opaque,const char* filename,int mode)
     134{
     135    const char* mode_fopen = NULL;
     136    DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ;
     137    HANDLE hFile = NULL;
     138
     139    win32_translate_open_mode(mode,&dwDesiredAccess,&dwCreationDisposition,&dwShareMode,&dwFlagsAndAttributes);
     140
     141    if ((filename!=NULL) && (dwDesiredAccess != 0))
     142        hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
     143
     144    return win32_build_iowin(hFile);
     145}
     146
     147
     148uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream, void* buf,uLong size)
    122149{
    123150    uLong ret=0;
     
    125152    if (stream!=NULL)
    126153        hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
     154
    127155    if (hFile != NULL)
     156    {
    128157        if (!ReadFile(hFile, buf, size, &ret, NULL))
    129158        {
     
    133162            ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
    134163        }
    135 
    136     return ret;
    137 }
    138 
    139 
    140 uLong ZCALLBACK win32_write_file_func (opaque, stream, buf, size)
    141    voidpf opaque;
    142    voidpf stream;
    143    const void* buf;
    144    uLong size;
     164    }
     165
     166    return ret;
     167}
     168
     169
     170uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size)
    145171{
    146172    uLong ret=0;
     
    149175        hFile = ((WIN32FILE_IOWIN*)stream) -> hf;
    150176
    151     if (hFile !=NULL)
     177    if (hFile != NULL)
     178    {
    152179        if (!WriteFile(hFile, buf, size, &ret, NULL))
    153180        {
     
    157184            ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
    158185        }
    159 
    160     return ret;
    161 }
    162 
    163 long ZCALLBACK win32_tell_file_func (opaque, stream)
    164    voidpf opaque;
    165    voidpf stream;
     186    }
     187
     188    return ret;
     189}
     190
     191long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream)
    166192{
    167193    long ret=-1;
     
    184210}
    185211
    186 long ZCALLBACK win32_seek_file_func (opaque, stream, offset, origin)
    187    voidpf opaque;
    188    voidpf stream;
    189    uLong offset;
    190    int origin;
     212ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream)
     213{
     214    ZPOS64_T ret= (ZPOS64_T)-1;
     215    HANDLE hFile = NULL;
     216    if (stream!=NULL)
     217        hFile = ((WIN32FILE_IOWIN*)stream)->hf;
     218
     219    if (hFile)
     220    {
     221        LARGE_INTEGER li;
     222        li.QuadPart = 0;
     223        li.u.LowPart = SetFilePointer(hFile, li.u.LowPart, &li.u.HighPart, FILE_CURRENT);
     224        if ( (li.LowPart == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
     225        {
     226            DWORD dwErr = GetLastError();
     227            ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
     228            ret = (ZPOS64_T)-1;
     229        }
     230        else
     231            ret=li.QuadPart;
     232    }
     233    return ret;
     234}
     235
     236
     237long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin)
    191238{
    192239    DWORD dwMoveMethod=0xFFFFFFFF;
     
    225272}
    226273
    227 int ZCALLBACK win32_close_file_func (opaque, stream)
    228    voidpf opaque;
    229    voidpf stream;
     274long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin)
     275{
     276    DWORD dwMoveMethod=0xFFFFFFFF;
     277    HANDLE hFile = NULL;
     278    long ret=-1;
     279
     280    if (stream!=NULL)
     281        hFile = ((WIN32FILE_IOWIN*)stream)->hf;
     282
     283    switch (origin)
     284    {
     285        case ZLIB_FILEFUNC_SEEK_CUR :
     286            dwMoveMethod = FILE_CURRENT;
     287            break;
     288        case ZLIB_FILEFUNC_SEEK_END :
     289            dwMoveMethod = FILE_END;
     290            break;
     291        case ZLIB_FILEFUNC_SEEK_SET :
     292            dwMoveMethod = FILE_BEGIN;
     293            break;
     294        default: return -1;
     295    }
     296
     297    if (hFile)
     298    {
     299        LARGE_INTEGER* li = (LARGE_INTEGER*)&offset;
     300        DWORD dwSet = SetFilePointer(hFile, li->u.LowPart, &li->u.HighPart, dwMoveMethod);
     301        if (dwSet == INVALID_SET_FILE_POINTER)
     302        {
     303            DWORD dwErr = GetLastError();
     304            ((WIN32FILE_IOWIN*)stream) -> error=(int)dwErr;
     305            ret = -1;
     306        }
     307        else
     308            ret=0;
     309    }
     310    return ret;
     311}
     312
     313int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream)
    230314{
    231315    int ret=-1;
     
    245329}
    246330
    247 int ZCALLBACK win32_error_file_func (opaque, stream)
    248    voidpf opaque;
    249    voidpf stream;
     331int ZCALLBACK win32_error_file_func (voidpf opaque,voidpf stream)
    250332{
    251333    int ret=-1;
     
    257339}
    258340
    259 void fill_win32_filefunc (pzlib_filefunc_def)
    260   zlib_filefunc_def* pzlib_filefunc_def;
     341void fill_win32_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
    261342{
    262343    pzlib_filefunc_def->zopen_file = win32_open_file_func;
     
    267348    pzlib_filefunc_def->zclose_file = win32_close_file_func;
    268349    pzlib_filefunc_def->zerror_file = win32_error_file_func;
    269     pzlib_filefunc_def->opaque=NULL;
    270 }
     350    pzlib_filefunc_def->opaque = NULL;
     351}
     352
     353void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def)
     354{
     355    pzlib_filefunc_def->zopen64_file = win32_open64_file_func;
     356    pzlib_filefunc_def->zread_file = win32_read_file_func;
     357    pzlib_filefunc_def->zwrite_file = win32_write_file_func;
     358    pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
     359    pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
     360    pzlib_filefunc_def->zclose_file = win32_close_file_func;
     361    pzlib_filefunc_def->zerror_file = win32_error_file_func;
     362    pzlib_filefunc_def->opaque = NULL;
     363}
     364
     365
     366void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def)
     367{
     368    pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA;
     369    pzlib_filefunc_def->zread_file = win32_read_file_func;
     370    pzlib_filefunc_def->zwrite_file = win32_write_file_func;
     371    pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
     372    pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
     373    pzlib_filefunc_def->zclose_file = win32_close_file_func;
     374    pzlib_filefunc_def->zerror_file = win32_error_file_func;
     375    pzlib_filefunc_def->opaque = NULL;
     376}
     377
     378
     379void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def)
     380{
     381    pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW;
     382    pzlib_filefunc_def->zread_file = win32_read_file_func;
     383    pzlib_filefunc_def->zwrite_file = win32_write_file_func;
     384    pzlib_filefunc_def->ztell64_file = win32_tell64_file_func;
     385    pzlib_filefunc_def->zseek64_file = win32_seek64_file_func;
     386    pzlib_filefunc_def->zclose_file = win32_close_file_func;
     387    pzlib_filefunc_def->zerror_file = win32_error_file_func;
     388    pzlib_filefunc_def->opaque = NULL;
     389}
  • cpukit/zlib/contrib/minizip/iowin32.h

    re8f3eb9 ra0caafe  
    11/* iowin32.h -- IO base function header for compress/uncompress .zip
    2    files using zlib + zip or unzip API
    3    This IO API version uses the Win32 API (for Microsoft Windows)
     2     Version 1.1, February 14h, 2010
     3     part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
    44
    5    Version 1.01e, February 12th, 2005
     5         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
    66
    7    Copyright (C) 1998-2005 Gilles Vollant
     7         Modifications for Zip64 support
     8         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
     9
     10         For more info read MiniZip_info.txt
     11
    812*/
    913
     
    1620
    1721void fill_win32_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def));
     22void fill_win32_filefunc64 OF((zlib_filefunc64_def* pzlib_filefunc_def));
     23void fill_win32_filefunc64A OF((zlib_filefunc64_def* pzlib_filefunc_def));
     24void fill_win32_filefunc64W OF((zlib_filefunc64_def* pzlib_filefunc_def));
    1825
    1926#ifdef __cplusplus
  • cpukit/zlib/contrib/minizip/miniunz.c

    re8f3eb9 ra0caafe  
    11/*
    22   miniunz.c
    3    Version 1.01e, February 12th, 2005
    4 
    5    Copyright (C) 1998-2005 Gilles Vollant
     3   Version 1.1, February 14h, 2010
     4   sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     5
     6         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     7
     8         Modifications of Unzip for Zip64
     9         Copyright (C) 2007-2008 Even Rouault
     10
     11         Modifications for Zip64 support on both zip and unzip
     12         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
    613*/
    714
     15#ifndef _WIN32
     16        #ifndef __USE_FILE_OFFSET64
     17                #define __USE_FILE_OFFSET64
     18        #endif
     19        #ifndef __USE_LARGEFILE64
     20                #define __USE_LARGEFILE64
     21        #endif
     22        #ifndef _LARGEFILE64_SOURCE
     23                #define _LARGEFILE64_SOURCE
     24        #endif
     25        #ifndef _FILE_OFFSET_BIT
     26                #define _FILE_OFFSET_BIT 64
     27        #endif
     28#endif
    829
    930#include <stdio.h>
     
    2849#define MAXFILENAME (256)
    2950
    30 #ifdef WIN32
     51#ifdef _WIN32
    3152#define USEWIN32IOAPI
    3253#include "iowin32.h"
     
    5273    tm_unz tmu_date;
    5374{
    54 #ifdef WIN32
     75#ifdef _WIN32
    5576  HANDLE hFile;
    5677  FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
    5778
    58   hFile = CreateFile(filename,GENERIC_READ | GENERIC_WRITE,
     79  hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
    5980                      0,NULL,OPEN_EXISTING,0,NULL);
    6081  GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
     
    92113{
    93114    int ret=0;
    94 #ifdef WIN32
    95     ret = mkdir(dirname);
     115#ifdef _WIN32
     116    ret = _mkdir(dirname);
    96117#else
    97118#ifdef unix
     
    113134
    114135  buffer = (char*)malloc(len+1);
     136        if (buffer==NULL)
     137        {
     138                printf("Error allocating memory\n");
     139                return UNZ_INTERNALERROR;
     140        }
    115141  strcpy(buffer,newdir);
    116142
     
    165191}
    166192
     193void Display64BitsSize(ZPOS64_T n, int size_char)
     194{
     195  /* to avoid compatibility problem , we do here the conversion */
     196  char number[21];
     197  int offset=19;
     198  int pos_string = 19;
     199  number[20]=0;
     200  for (;;) {
     201      number[offset]=(char)((n%10)+'0');
     202      if (number[offset] != '0')
     203          pos_string=offset;
     204      n/=10;
     205      if (offset==0)
     206          break;
     207      offset--;
     208  }
     209  {
     210      int size_display_string = 19-pos_string;
     211      while (size_char > size_display_string)
     212      {
     213          size_char--;
     214          printf(" ");
     215      }
     216  }
     217
     218  printf("%s",&number[pos_string]);
     219}
    167220
    168221int do_list(uf)
     
    170223{
    171224    uLong i;
    172     unz_global_info gi;
     225    unz_global_info64 gi;
    173226    int err;
    174227
    175     err = unzGetGlobalInfo (uf,&gi);
     228    err = unzGetGlobalInfo64(uf,&gi);
    176229    if (err!=UNZ_OK)
    177230        printf("error %d with zipfile in unzGetGlobalInfo \n",err);
    178     printf(" Length  Method   Size Ratio   Date    Time   CRC-32     Name\n");
    179     printf(" ------  ------   ---- -----   ----    ----   ------     ----\n");
     231    printf("  Length  Method     Size Ratio   Date    Time   CRC-32     Name\n");
     232    printf("  ------  ------     ---- -----   ----    ----   ------     ----\n");
    180233    for (i=0;i<gi.number_entry;i++)
    181234    {
    182235        char filename_inzip[256];
    183         unz_file_info file_info;
     236        unz_file_info64 file_info;
    184237        uLong ratio=0;
    185238        const char *string_method;
    186239        char charCrypt=' ';
    187         err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
     240        err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
    188241        if (err!=UNZ_OK)
    189242        {
     
    192245        }
    193246        if (file_info.uncompressed_size>0)
    194             ratio = (file_info.compressed_size*100)/file_info.uncompressed_size;
     247            ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
    195248
    196249        /* display a '*' if the file is crypted */
     
    212265        }
    213266        else
     267        if (file_info.compression_method==Z_BZIP2ED)
     268        {
     269              string_method="BZip2 ";
     270        }
     271        else
    214272            string_method="Unkn. ";
    215273
    216         printf("%7lu  %6s%c%7lu %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
    217                 file_info.uncompressed_size,string_method,
    218                 charCrypt,
    219                 file_info.compressed_size,
     274        Display64BitsSize(file_info.uncompressed_size,7);
     275        printf("  %6s%c",string_method,charCrypt);
     276        Display64BitsSize(file_info.compressed_size,7);
     277        printf(" %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
    220278                ratio,
    221279                (uLong)file_info.tmu_date.tm_mon + 1,
     
    253311    uInt size_buf;
    254312
    255     unz_file_info file_info;
     313    unz_file_info64 file_info;
    256314    uLong ratio=0;
    257     err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
     315    err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
    258316
    259317    if (err!=UNZ_OK)
     
    307365            char rep=0;
    308366            FILE* ftestexist;
    309             ftestexist = fopen(write_filename,"rb");
     367            ftestexist = fopen64(write_filename,"rb");
    310368            if (ftestexist!=NULL)
    311369            {
     
    318376                    printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
    319377                    ret = scanf("%1s",answer);
    320                     if (ret != 1) 
     378                    if (ret != 1)
    321379                    {
    322380                       exit(EXIT_FAILURE);
     
    338396        if ((skip==0) && (err==UNZ_OK))
    339397        {
    340             fout=fopen(write_filename,"wb");
     398            fout=fopen64(write_filename,"wb");
    341399
    342400            /* some zipfile don't contain directory alone before file */
     
    348406                makedir(write_filename);
    349407                *(filename_withoutpath-1)=c;
    350                 fout=fopen(write_filename,"wb");
     408                fout=fopen64(write_filename,"wb");
    351409            }
    352410
     
    410468{
    411469    uLong i;
    412     unz_global_info gi;
     470    unz_global_info64 gi;
    413471    int err;
    414472    FILE* fout=NULL;
    415473
    416     err = unzGetGlobalInfo (uf,&gi);
     474    err = unzGetGlobalInfo64(uf,&gi);
    417475    if (err!=UNZ_OK)
    418476        printf("error %d with zipfile in unzGetGlobalInfo \n",err);
     
    471529    char filename_try[MAXFILENAME+16] = "";
    472530    int i;
     531    int ret_value=0;
    473532    int opt_do_list=0;
    474533    int opt_do_extract=1;
     
    533592
    534593#        ifdef USEWIN32IOAPI
    535         zlib_filefunc_def ffunc;
     594        zlib_filefunc64_def ffunc;
    536595#        endif
    537596
     
    541600
    542601#        ifdef USEWIN32IOAPI
    543         fill_win32_filefunc(&ffunc);
    544         uf = unzOpen2(zipfilename,&ffunc);
     602        fill_win32_filefunc64A(&ffunc);
     603        uf = unzOpen2_64(zipfilename,&ffunc);
    545604#        else
    546         uf = unzOpen(zipfilename);
     605        uf = unzOpen64(zipfilename);
    547606#        endif
    548607        if (uf==NULL)
     
    550609            strcat(filename_try,".zip");
    551610#            ifdef USEWIN32IOAPI
    552             uf = unzOpen2(filename_try,&ffunc);
     611            uf = unzOpen2_64(filename_try,&ffunc);
    553612#            else
    554             uf = unzOpen(filename_try);
     613            uf = unzOpen64(filename_try);
    555614#            endif
    556615        }
     
    565624
    566625    if (opt_do_list==1)
    567         return do_list(uf);
     626        ret_value = do_list(uf);
    568627    else if (opt_do_extract==1)
    569628    {
    570         if (opt_extractdir && chdir(dirname))
     629#ifdef _WIN32
     630        if (opt_extractdir && _chdir(dirname))
     631#else
     632        if (opt_extractdir && chdir(dirname))
     633#endif
    571634        {
    572635          printf("Error changing into %s, aborting\n", dirname);
     
    575638
    576639        if (filename_to_extract == NULL)
    577             return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite,password);
     640            ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
    578641        else
    579             return do_extract_onefile(uf,filename_to_extract,
    580                                       opt_do_extract_withoutpath,opt_overwrite,password);
    581     }
    582     unzCloseCurrentFile(uf);
    583 
    584     return 0;
    585 }
     642            ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
     643    }
     644
     645    unzClose(uf);
     646
     647    return ret_value;
     648}
  • cpukit/zlib/contrib/minizip/minizip.c

    re8f3eb9 ra0caafe  
    11/*
    22   minizip.c
    3    Version 1.01e, February 12th, 2005
    4 
    5    Copyright (C) 1998-2005 Gilles Vollant
     3   Version 1.1, February 14h, 2010
     4   sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     5
     6         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     7
     8         Modifications of Unzip for Zip64
     9         Copyright (C) 2007-2008 Even Rouault
     10
     11         Modifications for Zip64 support on both zip and unzip
     12         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
    613*/
     14
     15
     16#ifndef _WIN32
     17        #ifndef __USE_FILE_OFFSET64
     18                #define __USE_FILE_OFFSET64
     19        #endif
     20        #ifndef __USE_LARGEFILE64
     21                #define __USE_LARGEFILE64
     22        #endif
     23        #ifndef _LARGEFILE64_SOURCE
     24                #define _LARGEFILE64_SOURCE
     25        #endif
     26        #ifndef _FILE_OFFSET_BIT
     27                #define _FILE_OFFSET_BIT 64
     28        #endif
     29#endif
    730
    831#include <stdio.h>
     
    2548#include "zip.h"
    2649
    27 #ifdef WIN32
    28 #define USEWIN32IOAPI
    29 #include "iowin32.h"
     50#ifdef _WIN32
     51        #define USEWIN32IOAPI
     52        #include "iowin32.h"
    3053#endif
    3154
     
    3558#define MAXFILENAME (256)
    3659
    37 #ifdef WIN32
     60#ifdef _WIN32
    3861uLong filetime(f, tmzip, dt)
    3962    char *f;                /* name of file to get info on */
     
    4568      FILETIME ftLocal;
    4669      HANDLE hFind;
    47       WIN32_FIND_DATA  ff32;
    48 
    49       hFind = FindFirstFile(f,&ff32);
     70      WIN32_FIND_DATAA ff32;
     71
     72      hFind = FindFirstFileA(f,&ff32);
    5073      if (hFind != INVALID_HANDLE_VALUE)
    5174      {
     
    120143    FILE* ftestexist;
    121144    int ret = 1;
    122     ftestexist = fopen(filename,"rb");
     145    ftestexist = fopen64(filename,"rb");
    123146    if (ftestexist==NULL)
    124147        ret = 0;
     
    130153void do_banner()
    131154{
    132     printf("MiniZip 1.01b, demo of zLib + Zip package written by Gilles Vollant\n");
    133     printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
     155    printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
     156    printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
    134157}
    135158
    136159void do_help()
    137160{
    138     printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" \
     161    printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
    139162           "  -o  Overwrite existing file.zip\n" \
    140163           "  -a  Append to existing file.zip\n" \
    141164           "  -0  Store only\n" \
    142165           "  -1  Compress faster\n" \
    143            "  -9  Compress better\n\n");
     166           "  -9  Compress better\n\n" \
     167           "  -j  exclude path. store only the file name.\n\n");
    144168}
    145169
     
    150174   unsigned long calculate_crc=0;
    151175   int err=ZIP_OK;
    152    FILE * fin = fopen(filenameinzip,"rb");
     176   FILE * fin = fopen64(filenameinzip,"rb");
    153177   unsigned long size_read = 0;
    154178   unsigned long total_read = 0;
     
    180204
    181205    *result_crc=calculate_crc;
    182     printf("file %s crc %x\n",filenameinzip,calculate_crc);
     206    printf("file %s crc %lx\n", filenameinzip, calculate_crc);
    183207    return err;
     208}
     209
     210int isLargeFile(const char* filename)
     211{
     212  int largeFile = 0;
     213  ZPOS64_T pos = 0;
     214  FILE* pFile = fopen64(filename, "rb");
     215
     216  if(pFile != NULL)
     217  {
     218    int n = fseeko64(pFile, 0, SEEK_END);
     219
     220    pos = ftello64(pFile);
     221
     222                printf("File : %s is %lld bytes\n", filename, pos);
     223
     224    if(pos >= 0xffffffff)
     225     largeFile = 1;
     226
     227                fclose(pFile);
     228  }
     229
     230 return largeFile;
    184231}
    185232
     
    191238    int opt_overwrite=0;
    192239    int opt_compress_level=Z_DEFAULT_COMPRESSION;
     240    int opt_exclude_path=0;
    193241    int zipfilenamearg = 0;
    194242    char filename_try[MAXFILENAME+16];
     
    223271                    if ((c>='0') && (c<='9'))
    224272                        opt_compress_level = c-'0';
     273                    if ((c=='j') || (c=='J'))
     274                        opt_exclude_path = 1;
    225275
    226276                    if (((c=='p') || (c=='P')) && (i+1<argc))
     
    232282            }
    233283            else
     284            {
    234285                if (zipfilenamearg == 0)
     286                {
    235287                    zipfilenamearg = i ;
     288                }
     289            }
    236290        }
    237291    }
     
    246300
    247301    if (zipfilenamearg==0)
     302    {
    248303        zipok=0;
     304    }
    249305    else
    250306    {
     
    303359        int errclose;
    304360#        ifdef USEWIN32IOAPI
    305         zlib_filefunc_def ffunc;
    306         fill_win32_filefunc(&ffunc);
    307         zf = zipOpen2(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
     361        zlib_filefunc64_def ffunc;
     362        fill_win32_filefunc64A(&ffunc);
     363        zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
    308364#        else
    309         zf = zipOpen(filename_try,(opt_overwrite==2) ? 2 : 0);
     365        zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
    310366#        endif
    311367
     
    330386                int size_read;
    331387                const char* filenameinzip = argv[i];
     388                const char *savefilenameinzip;
    332389                zip_fileinfo zi;
    333390                unsigned long crcFile=0;
     391                int zip64 = 0;
    334392
    335393                zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
     
    349407                    err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
    350408
    351                 err = zipOpenNewFileInZip3(zf,filenameinzip,&zi,
     409                zip64 = isLargeFile(filenameinzip);
     410
     411                                                         /* The path name saved, should not include a leading slash. */
     412               /*if it did, windows/xp and dynazip couldn't read the zip file. */
     413                 savefilenameinzip = filenameinzip;
     414                 while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
     415                 {
     416                     savefilenameinzip++;
     417                 }
     418
     419                 /*should the zip file contain any path at all?*/
     420                 if( opt_exclude_path )
     421                 {
     422                     const char *tmpptr;
     423                     const char *lastslash = 0;
     424                     for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
     425                     {
     426                         if( *tmpptr == '\\' || *tmpptr == '/')
     427                         {
     428                             lastslash = tmpptr;
     429                         }
     430                     }
     431                     if( lastslash != NULL )
     432                     {
     433                         savefilenameinzip = lastslash+1; // base filename follows last slash.
     434                     }
     435                 }
     436
     437                 /**/
     438                err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
    352439                                 NULL,0,NULL,0,NULL /* comment*/,
    353440                                 (opt_compress_level != 0) ? Z_DEFLATED : 0,
     
    355442                                 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
    356443                                 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
    357                                  password,crcFile);
     444                                 password,crcFile, zip64);
    358445
    359446                if (err != ZIP_OK)
     
    361448                else
    362449                {
    363                     fin = fopen(filenameinzip,"rb");
     450                    fin = fopen64(filenameinzip,"rb");
    364451                    if (fin==NULL)
    365452                    {
  • cpukit/zlib/contrib/minizip/unzip.c

    re8f3eb9 ra0caafe  
    11/* unzip.c -- IO for uncompress .zip files using zlib
    2    Version 1.01e, February 12th, 2005
    3 
    4    Copyright (C) 1998-2005 Gilles Vollant
    5 
    6    Read unzip.h for more info
    7 */
    8 
    9 /* Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of
    10 compatibility with older software. The following is from the original crypt.c. Code
    11 woven in by Terry Thorsen 1/2003.
    12 */
    13 /*
     2   Version 1.1, February 14h, 2010
     3   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     4
     5         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     6
     7         Modifications of Unzip for Zip64
     8         Copyright (C) 2007-2008 Even Rouault
     9
     10         Modifications for Zip64 support on both zip and unzip
     11         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
     12
     13         For more info read MiniZip_info.txt
     14
     15
     16  ------------------------------------------------------------------------------------
     17  Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of
     18  compatibility with older software. The following is from the original crypt.c.
     19  Code woven in by Terry Thorsen 1/2003.
     20
    1421  Copyright (c) 1990-2000 Info-ZIP.  All rights reserved.
    1522
     
    1825  If, for some reason, all these files are missing, the Info-ZIP license
    1926  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
    20 */
    21 /*
    22   crypt.c (full version) by Info-ZIP.      Last revised:  [see crypt.h]
     27
     28        crypt.c (full version) by Info-ZIP.      Last revised:  [see crypt.h]
    2329
    2430  The encryption/decryption parts of this source code (as opposed to the
     
    2632  whole source package can be freely distributed, including from the USA.
    2733  (Prior to January 2000, re-export from the US was a violation of US law.)
    28  */
    29 
    30 /*
    31   This encryption code is a direct transcription of the algorithm from
     34
     35        This encryption code is a direct transcription of the algorithm from
    3236  Roger Schlafly, described by Phil Katz in the file appnote.txt.  This
    3337  file (appnote.txt) is distributed with the PKZIP program (even in the
    3438  version without encryption capabilities).
    35  */
     39
     40        ------------------------------------------------------------------------------------
     41
     42        Changes in unzip.c
     43
     44        2007-2008 - Even Rouault - Addition of cpl_unzGetCurrentFileZStreamPos
     45  2007-2008 - Even Rouault - Decoration of symbol names unz* -> cpl_unz*
     46  2007-2008 - Even Rouault - Remove old C style function prototypes
     47  2007-2008 - Even Rouault - Add unzip support for ZIP64
     48
     49        Copyright (C) 2007-2008 Even Rouault
     50
     51
     52        Oct-2009 - Mathias Svensson - Removed cpl_* from symbol names (Even Rouault added them but since this is now moved to a new project (minizip64) I renamed them again).
     53  Oct-2009 - Mathias Svensson - Fixed problem if uncompressed size was > 4G and compressed size was <4G
     54                                should only read the compressed/uncompressed size from the Zip64 format if
     55                                the size from normal header was 0xFFFFFFFF
     56  Oct-2009 - Mathias Svensson - Applied some bug fixes from paches recived from Gilles Vollant
     57        Oct-2009 - Mathias Svensson - Applied support to unzip files with compression mathod BZIP2 (bzip2 lib is required)
     58                                Patch created by Daniel Borca
     59
     60  Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
     61
     62  Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson
     63
     64*/
    3665
    3766
     
    3968#include <stdlib.h>
    4069#include <string.h>
     70
     71#ifndef NOUNCRYPT
     72        #define NOUNCRYPT
     73#endif
     74
    4175#include "zlib.h"
    4276#include "unzip.h"
     
    86120
    87121
    88 
    89 
    90122const char unz_copyright[] =
    91123   " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
    92124
    93125/* unz_file_info_interntal contain internal info about a file in zipfile*/
    94 typedef struct unz_file_info_internal_s
    95 {
    96     uLong offset_curfile;/* relative offset of local header 4 bytes */
    97 } unz_file_info_internal;
     126typedef struct unz_file_info64_internal_s
     127{
     128    ZPOS64_T offset_curfile;/* relative offset of local header 8 bytes */
     129} unz_file_info64_internal;
    98130
    99131
     
    105137    z_stream stream;            /* zLib stream structure for inflate */
    106138
    107     uLong pos_in_zipfile;       /* position in byte on the zipfile, for fseek*/
     139#ifdef HAVE_BZIP2
     140    bz_stream bstream;          /* bzLib stream structure for bziped */
     141#endif
     142
     143    ZPOS64_T pos_in_zipfile;       /* position in byte on the zipfile, for fseek*/
    108144    uLong stream_initialised;   /* flag set if stream structure is initialised*/
    109145
    110     uLong offset_local_extrafield;/* offset of the local extra field */
     146    ZPOS64_T offset_local_extrafield;/* offset of the local extra field */
    111147    uInt  size_local_extrafield;/* size of the local extra field */
    112     uLong pos_local_extrafield;   /* position in the local extra field in read*/
     148    ZPOS64_T pos_local_extrafield;   /* position in the local extra field in read*/
     149    ZPOS64_T total_out_64;
    113150
    114151    uLong crc32;                /* crc32 of all data uncompressed */
    115152    uLong crc32_wait;           /* crc32 we must obtain after decompress all */
    116     uLong rest_read_compressed; /* number of byte to be decompressed */
    117     uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/
    118     zlib_filefunc_def z_filefunc;
     153    ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
     154    ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/
     155    zlib_filefunc64_32_def z_filefunc;
    119156    voidpf filestream;        /* io structore of the zipfile */
    120157    uLong compression_method;   /* compression method (0==store) */
    121     uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
     158    ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
    122159    int   raw;
    123 } file_in_zip_read_info_s;
    124 
    125 
    126 /* unz_s contain internal information about the zipfile
     160} file_in_zip64_read_info_s;
     161
     162
     163/* unz64_s contain internal information about the zipfile
    127164*/
    128165typedef struct
    129166{
    130     zlib_filefunc_def z_filefunc;
     167    zlib_filefunc64_32_def z_filefunc;
     168    int is64bitOpenFunction;
    131169    voidpf filestream;        /* io structore of the zipfile */
    132     unz_global_info gi;       /* public global information */
    133     uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
    134     uLong num_file;             /* number of the current file in the zipfile*/
    135     uLong pos_in_central_dir;   /* pos of the current file in the central dir*/
    136     uLong current_file_ok;      /* flag about the usability of the current file*/
    137     uLong central_pos;          /* position of the beginning of the central dir*/
    138 
    139     uLong size_central_dir;     /* size of the central directory  */
    140     uLong offset_central_dir;   /* offset of start of central directory with
     170    unz_global_info64 gi;       /* public global information */
     171    ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
     172    ZPOS64_T num_file;             /* number of the current file in the zipfile*/
     173    ZPOS64_T pos_in_central_dir;   /* pos of the current file in the central dir*/
     174    ZPOS64_T current_file_ok;      /* flag about the usability of the current file*/
     175    ZPOS64_T central_pos;          /* position of the beginning of the central dir*/
     176
     177    ZPOS64_T size_central_dir;     /* size of the central directory  */
     178    ZPOS64_T offset_central_dir;   /* offset of start of central directory with
    141179                                   respect to the starting disk number */
    142180
    143     unz_file_info cur_file_info; /* public info about the current file in zip*/
    144     unz_file_info_internal cur_file_info_internal; /* private info about it*/
    145     file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
     181    unz_file_info64 cur_file_info; /* public info about the current file in zip*/
     182    unz_file_info64_internal cur_file_info_internal; /* private info about it*/
     183    file_in_zip64_read_info_s* pfile_in_zip_read; /* structure about the current
    146184                                        file if we are decompressing it */
    147185    int encrypted;
     186
     187    int isZip64;
     188
    148189#    ifndef NOUNCRYPT
    149190    unsigned long keys[3];     /* keys defining the pseudo-random sequence */
    150191    const unsigned long* pcrc_32_tab;
    151192#    endif
    152 } unz_s;
     193} unz64_s;
    153194
    154195
     
    164205
    165206
    166 local int unzlocal_getByte OF((
    167     const zlib_filefunc_def* pzlib_filefunc_def,
     207local int unz64local_getByte OF((
     208    const zlib_filefunc64_32_def* pzlib_filefunc_def,
    168209    voidpf filestream,
    169210    int *pi));
    170211
    171 local int unzlocal_getByte(pzlib_filefunc_def,filestream,pi)
    172     const zlib_filefunc_def* pzlib_filefunc_def;
    173     voidpf filestream;
    174     int *pi;
     212local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)
    175213{
    176214    unsigned char c;
    177     int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
     215    int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
    178216    if (err==1)
    179217    {
     
    183221    else
    184222    {
    185         if (ZERROR(*pzlib_filefunc_def,filestream))
     223        if (ZERROR64(*pzlib_filefunc_def,filestream))
    186224            return UNZ_ERRNO;
    187225        else
     
    194232   Reads a long in LSB order from the given gz_stream. Sets
    195233*/
    196 local int unzlocal_getShort OF((
    197     const zlib_filefunc_def* pzlib_filefunc_def,
     234local int unz64local_getShort OF((
     235    const zlib_filefunc64_32_def* pzlib_filefunc_def,
    198236    voidpf filestream,
    199237    uLong *pX));
    200238
    201 local int unzlocal_getShort (pzlib_filefunc_def,filestream,pX)
    202     const zlib_filefunc_def* pzlib_filefunc_def;
    203     voidpf filestream;
    204     uLong *pX;
     239local int unz64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def,
     240                             voidpf filestream,
     241                             uLong *pX)
    205242{
    206243    uLong x ;
    207     int i;
     244    int i = 0;
    208245    int err;
    209246
    210     err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
     247    err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
    211248    x = (uLong)i;
    212249
    213250    if (err==UNZ_OK)
    214         err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
    215     x += ((uLong)i)<<8;
     251        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     252    x |= ((uLong)i)<<8;
    216253
    217254    if (err==UNZ_OK)
     
    222259}
    223260
    224 local int unzlocal_getLong OF((
    225     const zlib_filefunc_def* pzlib_filefunc_def,
     261local int unz64local_getLong OF((
     262    const zlib_filefunc64_32_def* pzlib_filefunc_def,
    226263    voidpf filestream,
    227264    uLong *pX));
    228265
    229 local int unzlocal_getLong (pzlib_filefunc_def,filestream,pX)
    230     const zlib_filefunc_def* pzlib_filefunc_def;
    231     voidpf filestream;
    232     uLong *pX;
     266local int unz64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def,
     267                            voidpf filestream,
     268                            uLong *pX)
    233269{
    234270    uLong x ;
    235     int i;
     271    int i = 0;
    236272    int err;
    237273
    238     err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
     274    err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
    239275    x = (uLong)i;
    240276
    241277    if (err==UNZ_OK)
    242         err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
    243     x += ((uLong)i)<<8;
     278        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     279    x |= ((uLong)i)<<8;
    244280
    245281    if (err==UNZ_OK)
    246         err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
    247     x += ((uLong)i)<<16;
     282        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     283    x |= ((uLong)i)<<16;
    248284
    249285    if (err==UNZ_OK)
    250         err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
     286        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
    251287    x += ((uLong)i)<<24;
    252288
     
    258294}
    259295
     296local int unz64local_getLong64 OF((
     297    const zlib_filefunc64_32_def* pzlib_filefunc_def,
     298    voidpf filestream,
     299    ZPOS64_T *pX));
     300
     301
     302local int unz64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def,
     303                            voidpf filestream,
     304                            ZPOS64_T *pX)
     305{
     306    ZPOS64_T x ;
     307    int i = 0;
     308    int err;
     309
     310    err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     311    x = (ZPOS64_T)i;
     312
     313    if (err==UNZ_OK)
     314        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     315    x |= ((ZPOS64_T)i)<<8;
     316
     317    if (err==UNZ_OK)
     318        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     319    x |= ((ZPOS64_T)i)<<16;
     320
     321    if (err==UNZ_OK)
     322        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     323    x |= ((ZPOS64_T)i)<<24;
     324
     325    if (err==UNZ_OK)
     326        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     327    x |= ((ZPOS64_T)i)<<32;
     328
     329    if (err==UNZ_OK)
     330        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     331    x |= ((ZPOS64_T)i)<<40;
     332
     333    if (err==UNZ_OK)
     334        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     335    x |= ((ZPOS64_T)i)<<48;
     336
     337    if (err==UNZ_OK)
     338        err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
     339    x |= ((ZPOS64_T)i)<<56;
     340
     341    if (err==UNZ_OK)
     342        *pX = x;
     343    else
     344        *pX = 0;
     345    return err;
     346}
    260347
    261348/* My own strcmpi / strcasecmp */
    262 local int strcmpcasenosensitive_internal (fileName1,fileName2)
    263     const char* fileName1;
    264     const char* fileName2;
     349local int strcmpcasenosensitive_internal (const char* fileName1, const char* fileName2)
    265350{
    266351    for (;;)
     
    303388
    304389*/
    305 extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity)
    306     const char* fileName1;
    307     const char* fileName2;
    308     int iCaseSensitivity;
     390extern int ZEXPORT unzStringFileNameCompare (const char*  fileName1,
     391                                                 const char*  fileName2,
     392                                                 int iCaseSensitivity)
     393
    309394{
    310395    if (iCaseSensitivity==0)
     
    325410    the global comment)
    326411*/
    327 local uLong unzlocal_SearchCentralDir OF((
    328     const zlib_filefunc_def* pzlib_filefunc_def,
    329     voidpf filestream));
    330 
    331 local uLong unzlocal_SearchCentralDir(pzlib_filefunc_def,filestream)
    332     const zlib_filefunc_def* pzlib_filefunc_def;
    333     voidpf filestream;
     412local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
     413local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
    334414{
    335415    unsigned char* buf;
    336     uLong uSizeFile;
    337     uLong uBackRead;
    338     uLong uMaxBack=0xffff; /* maximum size of global comment */
    339     uLong uPosFound=0;
    340 
    341     if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
     416    ZPOS64_T uSizeFile;
     417    ZPOS64_T uBackRead;
     418    ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
     419    ZPOS64_T uPosFound=0;
     420
     421    if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
    342422        return 0;
    343423
    344424
    345     uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
     425    uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
    346426
    347427    if (uMaxBack>uSizeFile)
     
    355435    while (uBackRead<uMaxBack)
    356436    {
    357         uLong uReadSize,uReadPos ;
     437        uLong uReadSize;
     438        ZPOS64_T uReadPos ;
    358439        int i;
    359440        if (uBackRead+BUFREADCOMMENT>uMaxBack)
     
    364445
    365446        uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
    366                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
    367         if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
     447                     (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
     448        if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
    368449            break;
    369450
    370         if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
     451        if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
    371452            break;
    372453
     
    386467}
    387468
     469
     470/*
     471  Locate the Central directory 64 of a zipfile (at the end, just before
     472    the global comment)
     473*/
     474local ZPOS64_T unz64local_SearchCentralDir64 OF((
     475    const zlib_filefunc64_32_def* pzlib_filefunc_def,
     476    voidpf filestream));
     477
     478local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
     479                                      voidpf filestream)
     480{
     481    unsigned char* buf;
     482    ZPOS64_T uSizeFile;
     483    ZPOS64_T uBackRead;
     484    ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
     485    ZPOS64_T uPosFound=0;
     486    uLong uL;
     487                ZPOS64_T relativeOffset;
     488
     489    if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
     490        return 0;
     491
     492
     493    uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
     494
     495    if (uMaxBack>uSizeFile)
     496        uMaxBack = uSizeFile;
     497
     498    buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
     499    if (buf==NULL)
     500        return 0;
     501
     502    uBackRead = 4;
     503    while (uBackRead<uMaxBack)
     504    {
     505        uLong uReadSize;
     506        ZPOS64_T uReadPos;
     507        int i;
     508        if (uBackRead+BUFREADCOMMENT>uMaxBack)
     509            uBackRead = uMaxBack;
     510        else
     511            uBackRead+=BUFREADCOMMENT;
     512        uReadPos = uSizeFile-uBackRead ;
     513
     514        uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
     515                     (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
     516        if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
     517            break;
     518
     519        if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
     520            break;
     521
     522        for (i=(int)uReadSize-3; (i--)>0;)
     523            if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
     524                ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
     525            {
     526                uPosFound = uReadPos+i;
     527                break;
     528            }
     529
     530        if (uPosFound!=0)
     531            break;
     532    }
     533    TRYFREE(buf);
     534    if (uPosFound == 0)
     535        return 0;
     536
     537    /* Zip64 end of central directory locator */
     538    if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
     539        return 0;
     540
     541    /* the signature, already checked */
     542    if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
     543        return 0;
     544
     545    /* number of the disk with the start of the zip64 end of  central directory */
     546    if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
     547        return 0;
     548    if (uL != 0)
     549        return 0;
     550
     551    /* relative offset of the zip64 end of central directory record */
     552    if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK)
     553        return 0;
     554
     555    /* total number of disks */
     556    if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
     557        return 0;
     558    if (uL != 1)
     559        return 0;
     560
     561    /* Goto end of central directory record */
     562    if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
     563        return 0;
     564
     565     /* the signature */
     566    if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
     567        return 0;
     568
     569    if (uL != 0x06064b50)
     570        return 0;
     571
     572    return relativeOffset;
     573}
     574
    388575/*
    389576  Open a Zip file. path contain the full pathname (by example,
     
    395582       of this unzip package.
    396583*/
    397 extern unzFile ZEXPORT unzOpen2 (path, pzlib_filefunc_def)
    398     const char *path;
    399     zlib_filefunc_def* pzlib_filefunc_def;
    400 {
    401     unz_s us;
    402     unz_s *s;
    403     uLong central_pos,uL;
     584local unzFile unzOpenInternal (const void *path,
     585                               zlib_filefunc64_32_def* pzlib_filefunc64_32_def,
     586                               int is64bitOpenFunction)
     587{
     588    unz64_s us;
     589    unz64_s *s;
     590    ZPOS64_T central_pos;
     591    uLong   uL;
    404592
    405593    uLong number_disk;          /* number of the current dist, used for
     
    407595    uLong number_disk_with_CD;  /* number the the disk with central dir, used
    408596                                   for spaning ZIP, unsupported, always 0*/
    409     uLong number_entry_CD;      /* total number of entries in
     597    ZPOS64_T number_entry_CD;      /* total number of entries in
    410598                                   the central dir
    411599                                   (same than number_entry on nospan) */
     
    416604        return NULL;
    417605
    418     if (pzlib_filefunc_def==NULL)
    419         fill_fopen_filefunc(&us.z_filefunc);
     606    us.z_filefunc.zseek32_file = NULL;
     607    us.z_filefunc.ztell32_file = NULL;
     608    if (pzlib_filefunc64_32_def==NULL)
     609        fill_fopen64_filefunc(&us.z_filefunc.zfile_func64);
    420610    else
    421         us.z_filefunc = *pzlib_filefunc_def;
    422 
    423     us.filestream= (*(us.z_filefunc.zopen_file))(us.z_filefunc.opaque,
     611        us.z_filefunc = *pzlib_filefunc64_32_def;
     612    us.is64bitOpenFunction = is64bitOpenFunction;
     613
     614
     615
     616    us.filestream = ZOPEN64(us.z_filefunc,
    424617                                                 path,
    425618                                                 ZLIB_FILEFUNC_MODE_READ |
     
    428621        return NULL;
    429622
    430     central_pos = unzlocal_SearchCentralDir(&us.z_filefunc,us.filestream);
    431     if (central_pos==0)
    432         err=UNZ_ERRNO;
    433 
    434     if (ZSEEK(us.z_filefunc, us.filestream,
     623    central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream);
     624    if (central_pos)
     625    {
     626        uLong uS;
     627        ZPOS64_T uL64;
     628
     629        us.isZip64 = 1;
     630
     631        if (ZSEEK64(us.z_filefunc, us.filestream,
    435632                                      central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
    436633        err=UNZ_ERRNO;
    437634
    438     /* the signature, already checked */
    439     if (unzlocal_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
    440         err=UNZ_ERRNO;
    441 
    442     /* number of this disk */
    443     if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
    444         err=UNZ_ERRNO;
    445 
    446     /* number of the disk with the start of the central directory */
    447     if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
    448         err=UNZ_ERRNO;
    449 
    450     /* total number of entries in the central dir on this disk */
    451     if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK)
    452         err=UNZ_ERRNO;
    453 
    454     /* total number of entries in the central dir */
    455     if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK)
    456         err=UNZ_ERRNO;
    457 
    458     if ((number_entry_CD!=us.gi.number_entry) ||
    459         (number_disk_with_CD!=0) ||
    460         (number_disk!=0))
    461         err=UNZ_BADZIPFILE;
    462 
    463     /* size of the central directory */
    464     if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK)
    465         err=UNZ_ERRNO;
    466 
    467     /* offset of start of central directory with respect to the
     635        /* the signature, already checked */
     636        if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
     637            err=UNZ_ERRNO;
     638
     639        /* size of zip64 end of central directory record */
     640        if (unz64local_getLong64(&us.z_filefunc, us.filestream,&uL64)!=UNZ_OK)
     641            err=UNZ_ERRNO;
     642
     643        /* version made by */
     644        if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
     645            err=UNZ_ERRNO;
     646
     647        /* version needed to extract */
     648        if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
     649            err=UNZ_ERRNO;
     650
     651        /* number of this disk */
     652        if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
     653            err=UNZ_ERRNO;
     654
     655        /* number of the disk with the start of the central directory */
     656        if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
     657            err=UNZ_ERRNO;
     658
     659        /* total number of entries in the central directory on this disk */
     660        if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK)
     661            err=UNZ_ERRNO;
     662
     663        /* total number of entries in the central directory */
     664        if (unz64local_getLong64(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK)
     665            err=UNZ_ERRNO;
     666
     667        if ((number_entry_CD!=us.gi.number_entry) ||
     668            (number_disk_with_CD!=0) ||
     669            (number_disk!=0))
     670            err=UNZ_BADZIPFILE;
     671
     672        /* size of the central directory */
     673        if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK)
     674            err=UNZ_ERRNO;
     675
     676        /* offset of start of central directory with respect to the
    468677          starting disk number */
    469     if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK)
    470         err=UNZ_ERRNO;
    471 
    472     /* zipfile comment length */
    473     if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK)
    474         err=UNZ_ERRNO;
     678        if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK)
     679            err=UNZ_ERRNO;
     680
     681        us.gi.size_comment = 0;
     682    }
     683    else
     684    {
     685        central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream);
     686        if (central_pos==0)
     687            err=UNZ_ERRNO;
     688
     689        us.isZip64 = 0;
     690
     691        if (ZSEEK64(us.z_filefunc, us.filestream,
     692                                        central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
     693            err=UNZ_ERRNO;
     694
     695        /* the signature, already checked */
     696        if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
     697            err=UNZ_ERRNO;
     698
     699        /* number of this disk */
     700        if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
     701            err=UNZ_ERRNO;
     702
     703        /* number of the disk with the start of the central directory */
     704        if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
     705            err=UNZ_ERRNO;
     706
     707        /* total number of entries in the central dir on this disk */
     708        if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
     709            err=UNZ_ERRNO;
     710        us.gi.number_entry = uL;
     711
     712        /* total number of entries in the central dir */
     713        if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
     714            err=UNZ_ERRNO;
     715        number_entry_CD = uL;
     716
     717        if ((number_entry_CD!=us.gi.number_entry) ||
     718            (number_disk_with_CD!=0) ||
     719            (number_disk!=0))
     720            err=UNZ_BADZIPFILE;
     721
     722        /* size of the central directory */
     723        if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
     724            err=UNZ_ERRNO;
     725        us.size_central_dir = uL;
     726
     727        /* offset of start of central directory with respect to the
     728            starting disk number */
     729        if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
     730            err=UNZ_ERRNO;
     731        us.offset_central_dir = uL;
     732
     733        /* zipfile comment length */
     734        if (unz64local_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK)
     735            err=UNZ_ERRNO;
     736    }
    475737
    476738    if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
     
    480742    if (err!=UNZ_OK)
    481743    {
    482         ZCLOSE(us.z_filefunc, us.filestream);
     744        ZCLOSE64(us.z_filefunc, us.filestream);
    483745        return NULL;
    484746    }
     
    491753
    492754
    493     s=(unz_s*)ALLOC(sizeof(unz_s));
    494     *s=us;
    495     unzGoToFirstFile((unzFile)s);
     755    s=(unz64_s*)ALLOC(sizeof(unz64_s));
     756    if( s != NULL)
     757    {
     758        *s=us;
     759        unzGoToFirstFile((unzFile)s);
     760    }
    496761    return (unzFile)s;
    497762}
    498763
    499764
    500 extern unzFile ZEXPORT unzOpen (path)
    501     const char *path;
    502 {
    503     return unzOpen2(path, NULL);
     765extern unzFile ZEXPORT unzOpen2 (const char *path,
     766                                        zlib_filefunc_def* pzlib_filefunc32_def)
     767{
     768    if (pzlib_filefunc32_def != NULL)
     769    {
     770        zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
     771        fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
     772        return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 0);
     773    }
     774    else
     775        return unzOpenInternal(path, NULL, 0);
     776}
     777
     778extern unzFile ZEXPORT unzOpen2_64 (const void *path,
     779                                     zlib_filefunc64_def* pzlib_filefunc_def)
     780{
     781    if (pzlib_filefunc_def != NULL)
     782    {
     783        zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
     784        zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
     785        zlib_filefunc64_32_def_fill.ztell32_file = NULL;
     786        zlib_filefunc64_32_def_fill.zseek32_file = NULL;
     787        return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 1);
     788    }
     789    else
     790        return unzOpenInternal(path, NULL, 1);
     791}
     792
     793extern unzFile ZEXPORT unzOpen (const char *path)
     794{
     795    return unzOpenInternal(path, NULL, 0);
     796}
     797
     798extern unzFile ZEXPORT unzOpen64 (const void *path)
     799{
     800    return unzOpenInternal(path, NULL, 1);
    504801}
    505802
     
    509806    these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
    510807  return UNZ_OK if there is no problem. */
    511 extern int ZEXPORT unzClose (file)
    512     unzFile file;
    513 {
    514     unz_s* s;
     808extern int ZEXPORT unzClose (unzFile file)
     809{
     810    unz64_s* s;
    515811    if (file==NULL)
    516812        return UNZ_PARAMERROR;
    517     s=(unz_s*)file;
     813    s=(unz64_s*)file;
    518814
    519815    if (s->pfile_in_zip_read!=NULL)
    520816        unzCloseCurrentFile(file);
    521817
    522     ZCLOSE(s->z_filefunc, s->filestream);
     818    ZCLOSE64(s->z_filefunc, s->filestream);
    523819    TRYFREE(s);
    524820    return UNZ_OK;
     
    530826  No preparation of the structure is needed
    531827  return UNZ_OK if there is no problem. */
    532 extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info)
    533     unzFile file;
    534     unz_global_info *pglobal_info;
    535 {
    536     unz_s* s;
     828extern int ZEXPORT unzGetGlobalInfo64 (unzFile file, unz_global_info64* pglobal_info)
     829{
     830    unz64_s* s;
    537831    if (file==NULL)
    538832        return UNZ_PARAMERROR;
    539     s=(unz_s*)file;
     833    s=(unz64_s*)file;
    540834    *pglobal_info=s->gi;
    541835    return UNZ_OK;
    542836}
    543837
    544 
     838extern int ZEXPORT unzGetGlobalInfo (unzFile file, unz_global_info* pglobal_info32)
     839{
     840    unz64_s* s;
     841    if (file==NULL)
     842        return UNZ_PARAMERROR;
     843    s=(unz64_s*)file;
     844    /* to do : check if number_entry is not truncated */
     845    pglobal_info32->number_entry = (uLong)s->gi.number_entry;
     846    pglobal_info32->size_comment = s->gi.size_comment;
     847    return UNZ_OK;
     848}
    545849/*
    546850   Translate date/time from Dos format to tm_unz (readable more easilty)
    547851*/
    548 local void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
    549     uLong ulDosDate;
    550     tm_unz* ptm;
    551 {
    552     uLong uDate;
    553     uDate = (uLong)(ulDosDate>>16);
     852local void unz64local_DosDateToTmuDate (ZPOS64_T ulDosDate, tm_unz* ptm)
     853{
     854    ZPOS64_T uDate;
     855    uDate = (ZPOS64_T)(ulDosDate>>16);
    554856    ptm->tm_mday = (uInt)(uDate&0x1f) ;
    555857    ptm->tm_mon =  (uInt)((((uDate)&0x1E0)/0x20)-1) ;
     
    564866  Get Info about the current file in the zipfile, with internal only info
    565867*/
    566 local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
    567                                                   unz_file_info *pfile_info,
    568                                                   unz_file_info_internal
     868local int unz64local_GetCurrentFileInfoInternal OF((unzFile file,
     869                                                  unz_file_info64 *pfile_info,
     870                                                  unz_file_info64_internal
    569871                                                  *pfile_info_internal,
    570872                                                  char *szFileName,
     
    575877                                                  uLong commentBufferSize));
    576878
    577 local int unzlocal_GetCurrentFileInfoInternal (file,
    578                                               pfile_info,
    579                                               pfile_info_internal,
    580                                               szFileName, fileNameBufferSize,
    581                                               extraField, extraFieldBufferSize,
    582                                               szComment,  commentBufferSize)
    583     unzFile file;
    584     unz_file_info *pfile_info;
    585     unz_file_info_internal *pfile_info_internal;
    586     char *szFileName;
    587     uLong fileNameBufferSize;
    588     void *extraField;
    589     uLong extraFieldBufferSize;
    590     char *szComment;
    591     uLong commentBufferSize;
    592 {
    593     unz_s* s;
    594     unz_file_info file_info;
    595     unz_file_info_internal file_info_internal;
     879local int unz64local_GetCurrentFileInfoInternal (unzFile file,
     880                                                  unz_file_info64 *pfile_info,
     881                                                  unz_file_info64_internal
     882                                                  *pfile_info_internal,
     883                                                  char *szFileName,
     884                                                  uLong fileNameBufferSize,
     885                                                  void *extraField,
     886                                                  uLong extraFieldBufferSize,
     887                                                  char *szComment,
     888                                                  uLong commentBufferSize)
     889{
     890    unz64_s* s;
     891    unz_file_info64 file_info;
     892    unz_file_info64_internal file_info_internal;
    596893    int err=UNZ_OK;
    597894    uLong uMagic;
    598895    long lSeek=0;
     896    uLong uL;
    599897
    600898    if (file==NULL)
    601899        return UNZ_PARAMERROR;
    602     s=(unz_s*)file;
    603     if (ZSEEK(s->z_filefunc, s->filestream,
     900    s=(unz64_s*)file;
     901    if (ZSEEK64(s->z_filefunc, s->filestream,
    604902              s->pos_in_central_dir+s->byte_before_the_zipfile,
    605903              ZLIB_FILEFUNC_SEEK_SET)!=0)
     
    609907    /* we check the magic */
    610908    if (err==UNZ_OK)
    611         if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
     909    {
     910        if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
    612911            err=UNZ_ERRNO;
    613912        else if (uMagic!=0x02014b50)
    614913            err=UNZ_BADZIPFILE;
    615 
    616     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK)
    617         err=UNZ_ERRNO;
    618 
    619     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK)
    620         err=UNZ_ERRNO;
    621 
    622     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK)
    623         err=UNZ_ERRNO;
    624 
    625     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK)
    626         err=UNZ_ERRNO;
    627 
    628     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK)
    629         err=UNZ_ERRNO;
    630 
    631     unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
    632 
    633     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK)
    634         err=UNZ_ERRNO;
    635 
    636     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
    637         err=UNZ_ERRNO;
    638 
    639     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
    640         err=UNZ_ERRNO;
    641 
    642     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK)
    643         err=UNZ_ERRNO;
    644 
    645     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK)
    646         err=UNZ_ERRNO;
    647 
    648     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK)
    649         err=UNZ_ERRNO;
    650 
    651     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
    652         err=UNZ_ERRNO;
    653 
    654     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK)
    655         err=UNZ_ERRNO;
    656 
    657     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK)
    658         err=UNZ_ERRNO;
    659 
    660     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
    661         err=UNZ_ERRNO;
     914    }
     915
     916    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK)
     917        err=UNZ_ERRNO;
     918
     919    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK)
     920        err=UNZ_ERRNO;
     921
     922    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK)
     923        err=UNZ_ERRNO;
     924
     925    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK)
     926        err=UNZ_ERRNO;
     927
     928    if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK)
     929        err=UNZ_ERRNO;
     930
     931    unz64local_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
     932
     933    if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK)
     934        err=UNZ_ERRNO;
     935
     936    if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
     937        err=UNZ_ERRNO;
     938    file_info.compressed_size = uL;
     939
     940    if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
     941        err=UNZ_ERRNO;
     942    file_info.uncompressed_size = uL;
     943
     944    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK)
     945        err=UNZ_ERRNO;
     946
     947    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK)
     948        err=UNZ_ERRNO;
     949
     950    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK)
     951        err=UNZ_ERRNO;
     952
     953    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
     954        err=UNZ_ERRNO;
     955
     956    if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK)
     957        err=UNZ_ERRNO;
     958
     959    if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK)
     960        err=UNZ_ERRNO;
     961
     962                // relative offset of local header
     963    if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
     964        err=UNZ_ERRNO;
     965    file_info_internal.offset_curfile = uL;
    662966
    663967    lSeek+=file_info.size_filename;
     
    674978
    675979        if ((file_info.size_filename>0) && (fileNameBufferSize>0))
    676             if (ZREAD(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead)
     980            if (ZREAD64(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead)
    677981                err=UNZ_ERRNO;
    678982        lSeek -= uSizeRead;
    679983    }
    680984
    681 
     985    // Read extrafield
    682986    if ((err==UNZ_OK) && (extraField!=NULL))
    683987    {
    684         uLong uSizeRead ;
     988        ZPOS64_T uSizeRead ;
    685989        if (file_info.size_file_extra<extraFieldBufferSize)
    686990            uSizeRead = file_info.size_file_extra;
     
    689993
    690994        if (lSeek!=0)
    691             if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
     995        {
     996            if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
    692997                lSeek=0;
    693998            else
    694999                err=UNZ_ERRNO;
     1000        }
     1001
    6951002        if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
    696             if (ZREAD(s->z_filefunc, s->filestream,extraField,uSizeRead)!=uSizeRead)
     1003            if (ZREAD64(s->z_filefunc, s->filestream,extraField,(uLong)uSizeRead)!=uSizeRead)
    6971004                err=UNZ_ERRNO;
    698         lSeek += file_info.size_file_extra - uSizeRead;
     1005
     1006        lSeek += file_info.size_file_extra - (uLong)uSizeRead;
    6991007    }
    7001008    else
    701         lSeek+=file_info.size_file_extra;
    702 
     1009        lSeek += file_info.size_file_extra;
     1010
     1011
     1012    if ((err==UNZ_OK) && (file_info.size_file_extra != 0))
     1013    {
     1014                                uLong acc = 0;
     1015
     1016        // since lSeek now points to after the extra field we need to move back
     1017        lSeek -= file_info.size_file_extra;
     1018
     1019        if (lSeek!=0)
     1020        {
     1021            if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
     1022                lSeek=0;
     1023            else
     1024                err=UNZ_ERRNO;
     1025        }
     1026
     1027        while(acc < file_info.size_file_extra)
     1028        {
     1029            uLong headerId;
     1030                                                uLong dataSize;
     1031
     1032            if (unz64local_getShort(&s->z_filefunc, s->filestream,&headerId) != UNZ_OK)
     1033                err=UNZ_ERRNO;
     1034
     1035            if (unz64local_getShort(&s->z_filefunc, s->filestream,&dataSize) != UNZ_OK)
     1036                err=UNZ_ERRNO;
     1037
     1038            /* ZIP64 extra fields */
     1039            if (headerId == 0x0001)
     1040            {
     1041                                                        uLong uL;
     1042
     1043                                                                if(file_info.uncompressed_size == (ZPOS64_T)(unsigned long)-1)
     1044                                                                {
     1045                                                                        if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
     1046                                                                                        err=UNZ_ERRNO;
     1047                                                                }
     1048
     1049                                                                if(file_info.compressed_size == (ZPOS64_T)(unsigned long)-1)
     1050                                                                {
     1051                                                                        if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
     1052                                                                                  err=UNZ_ERRNO;
     1053                                                                }
     1054
     1055                                                                if(file_info_internal.offset_curfile == (ZPOS64_T)(unsigned long)-1)
     1056                                                                {
     1057                                                                        /* Relative Header offset */
     1058                                                                        if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
     1059                                                                                err=UNZ_ERRNO;
     1060                                                                }
     1061
     1062                                                                if(file_info.disk_num_start == (unsigned long)-1)
     1063                                                                {
     1064                                                                        /* Disk Start Number */
     1065                                                                        if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
     1066                                                                                err=UNZ_ERRNO;
     1067                                                                }
     1068
     1069            }
     1070            else
     1071            {
     1072                if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0)
     1073                    err=UNZ_ERRNO;
     1074            }
     1075
     1076            acc += 2 + 2 + dataSize;
     1077        }
     1078    }
    7031079
    7041080    if ((err==UNZ_OK) && (szComment!=NULL))
     
    7141090
    7151091        if (lSeek!=0)
    716             if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
     1092        {
     1093            if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
    7171094                lSeek=0;
    7181095            else
    7191096                err=UNZ_ERRNO;
     1097        }
     1098
    7201099        if ((file_info.size_file_comment>0) && (commentBufferSize>0))
    721             if (ZREAD(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
     1100            if (ZREAD64(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
    7221101                err=UNZ_ERRNO;
    7231102        lSeek+=file_info.size_file_comment - uSizeRead;
     
    7251104    else
    7261105        lSeek+=file_info.size_file_comment;
     1106
    7271107
    7281108    if ((err==UNZ_OK) && (pfile_info!=NULL))
     
    7421122  return UNZ_OK if there is no problem.
    7431123*/
    744 extern int ZEXPORT unzGetCurrentFileInfo (file,
    745                                           pfile_info,
    746                                           szFileName, fileNameBufferSize,
    747                                           extraField, extraFieldBufferSize,
    748                                           szComment,  commentBufferSize)
    749     unzFile file;
    750     unz_file_info *pfile_info;
    751     char *szFileName;
    752     uLong fileNameBufferSize;
    753     void *extraField;
    754     uLong extraFieldBufferSize;
    755     char *szComment;
    756     uLong commentBufferSize;
    757 {
    758     return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
     1124extern int ZEXPORT unzGetCurrentFileInfo64 (unzFile file,
     1125                                          unz_file_info64 * pfile_info,
     1126                                          char * szFileName, uLong fileNameBufferSize,
     1127                                          void *extraField, uLong extraFieldBufferSize,
     1128                                          char* szComment,  uLong commentBufferSize)
     1129{
     1130    return unz64local_GetCurrentFileInfoInternal(file,pfile_info,NULL,
    7591131                                                szFileName,fileNameBufferSize,
    7601132                                                extraField,extraFieldBufferSize,
     
    7621134}
    7631135
     1136extern int ZEXPORT unzGetCurrentFileInfo (unzFile file,
     1137                                          unz_file_info * pfile_info,
     1138                                          char * szFileName, uLong fileNameBufferSize,
     1139                                          void *extraField, uLong extraFieldBufferSize,
     1140                                          char* szComment,  uLong commentBufferSize)
     1141{
     1142    int err;
     1143    unz_file_info64 file_info64;
     1144    err = unz64local_GetCurrentFileInfoInternal(file,&file_info64,NULL,
     1145                                                szFileName,fileNameBufferSize,
     1146                                                extraField,extraFieldBufferSize,
     1147                                                szComment,commentBufferSize);
     1148    if (err==UNZ_OK)
     1149    {
     1150        pfile_info->version = file_info64.version;
     1151        pfile_info->version_needed = file_info64.version_needed;
     1152        pfile_info->flag = file_info64.flag;
     1153        pfile_info->compression_method = file_info64.compression_method;
     1154        pfile_info->dosDate = file_info64.dosDate;
     1155        pfile_info->crc = file_info64.crc;
     1156
     1157        pfile_info->size_filename = file_info64.size_filename;
     1158        pfile_info->size_file_extra = file_info64.size_file_extra;
     1159        pfile_info->size_file_comment = file_info64.size_file_comment;
     1160
     1161        pfile_info->disk_num_start = file_info64.disk_num_start;
     1162        pfile_info->internal_fa = file_info64.internal_fa;
     1163        pfile_info->external_fa = file_info64.external_fa;
     1164
     1165        pfile_info->tmu_date = file_info64.tmu_date,
     1166
     1167
     1168        pfile_info->compressed_size = (uLong)file_info64.compressed_size;
     1169        pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size;
     1170
     1171    }
     1172    return err;
     1173}
    7641174/*
    7651175  Set the current file of the zipfile to the first file.
    7661176  return UNZ_OK if there is no problem
    7671177*/
    768 extern int ZEXPORT unzGoToFirstFile (file)
    769     unzFile file;
     1178extern int ZEXPORT unzGoToFirstFile (unzFile file)
    7701179{
    7711180    int err=UNZ_OK;
    772     unz_s* s;
     1181    unz64_s* s;
    7731182    if (file==NULL)
    7741183        return UNZ_PARAMERROR;
    775     s=(unz_s*)file;
     1184    s=(unz64_s*)file;
    7761185    s->pos_in_central_dir=s->offset_central_dir;
    7771186    s->num_file=0;
    778     err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
     1187    err=unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
    7791188                                             &s->cur_file_info_internal,
    7801189                                             NULL,0,NULL,0,NULL,0);
     
    7881197  return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
    7891198*/
    790 extern int ZEXPORT unzGoToNextFile (file)
    791     unzFile file;
    792 {
    793     unz_s* s;
     1199extern int ZEXPORT unzGoToNextFile (unzFile  file)
     1200{
     1201    unz64_s* s;
    7941202    int err;
    7951203
    7961204    if (file==NULL)
    7971205        return UNZ_PARAMERROR;
    798     s=(unz_s*)file;
     1206    s=(unz64_s*)file;
    7991207    if (!s->current_file_ok)
    8001208        return UNZ_END_OF_LIST_OF_FILE;
     
    8061214            s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
    8071215    s->num_file++;
    808     err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
     1216    err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
    8091217                                               &s->cur_file_info_internal,
    8101218                                               NULL,0,NULL,0,NULL,0);
     
    8221230  UNZ_END_OF_LIST_OF_FILE if the file is not found
    8231231*/
    824 extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
    825     unzFile file;
    826     const char *szFileName;
    827     int iCaseSensitivity;
    828 {
    829     unz_s* s;
     1232extern int ZEXPORT unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
     1233{
     1234    unz64_s* s;
    8301235    int err;
    8311236
     
    8331238     * back there if we fail.
    8341239     */
    835     unz_file_info cur_file_infoSaved;
    836     unz_file_info_internal cur_file_info_internalSaved;
    837     uLong num_fileSaved;
    838     uLong pos_in_central_dirSaved;
     1240    unz_file_info64 cur_file_infoSaved;
     1241    unz_file_info64_internal cur_file_info_internalSaved;
     1242    ZPOS64_T num_fileSaved;
     1243    ZPOS64_T pos_in_central_dirSaved;
    8391244
    8401245
     
    8451250        return UNZ_PARAMERROR;
    8461251
    847     s=(unz_s*)file;
     1252    s=(unz64_s*)file;
    8481253    if (!s->current_file_ok)
    8491254        return UNZ_END_OF_LIST_OF_FILE;
     
    8601265    {
    8611266        char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
    862         err = unzGetCurrentFileInfo(file,NULL,
     1267        err = unzGetCurrentFileInfo64(file,NULL,
    8631268                                    szCurrentFileName,sizeof(szCurrentFileName)-1,
    8641269                                    NULL,0,NULL,0);
     
    8961301typedef struct unz_file_pos_s
    8971302{
    898     uLong pos_in_zip_directory;   // offset in file
    899     uLong num_of_file;            // # of file
     1303    ZPOS64_T pos_in_zip_directory;   // offset in file
     1304    ZPOS64_T num_of_file;            // # of file
    9001305} unz_file_pos;
    9011306*/
    9021307
    903 extern int ZEXPORT unzGetFilePos(file, file_pos)
    904     unzFile file;
    905     unz_file_pos* file_pos;
    906 {
    907     unz_s* s;
     1308extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos*  file_pos)
     1309{
     1310    unz64_s* s;
    9081311
    9091312    if (file==NULL || file_pos==NULL)
    9101313        return UNZ_PARAMERROR;
    911     s=(unz_s*)file;
     1314    s=(unz64_s*)file;
    9121315    if (!s->current_file_ok)
    9131316        return UNZ_END_OF_LIST_OF_FILE;
     
    9191322}
    9201323
    921 extern int ZEXPORT unzGoToFilePos(file, file_pos)
    922     unzFile file;
    923     unz_file_pos* file_pos;
    924 {
    925     unz_s* s;
     1324extern int ZEXPORT unzGetFilePos(
     1325    unzFile file,
     1326    unz_file_pos* file_pos)
     1327{
     1328    unz64_file_pos file_pos64;
     1329    int err = unzGetFilePos64(file,&file_pos64);
     1330    if (err==UNZ_OK)
     1331    {
     1332        file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory;
     1333        file_pos->num_of_file = (uLong)file_pos64.num_of_file;
     1334    }
     1335    return err;
     1336}
     1337
     1338extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos)
     1339{
     1340    unz64_s* s;
    9261341    int err;
    9271342
    9281343    if (file==NULL || file_pos==NULL)
    9291344        return UNZ_PARAMERROR;
    930     s=(unz_s*)file;
     1345    s=(unz64_s*)file;
    9311346
    9321347    /* jump to the right spot */
     
    9351350
    9361351    /* set the current file */
    937     err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
     1352    err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
    9381353                                               &s->cur_file_info_internal,
    9391354                                               NULL,0,NULL,0,NULL,0);
     
    9431358}
    9441359
     1360extern int ZEXPORT unzGoToFilePos(
     1361    unzFile file,
     1362    unz_file_pos* file_pos)
     1363{
     1364    unz64_file_pos file_pos64;
     1365    if (file_pos == NULL)
     1366        return UNZ_PARAMERROR;
     1367
     1368    file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
     1369    file_pos64.num_of_file = file_pos->num_of_file;
     1370    return unzGoToFilePos64(file,&file_pos64);
     1371}
     1372
    9451373/*
    9461374// Unzip Helper Functions - should be here?
     
    9551383        (filename and size of extra field data)
    9561384*/
    957 local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
    958                                                     poffset_local_extrafield,
    959                                                     psize_local_extrafield)
    960     unz_s* s;
    961     uInt* piSizeVar;
    962     uLong *poffset_local_extrafield;
    963     uInt  *psize_local_extrafield;
     1385local int unz64local_CheckCurrentFileCoherencyHeader (unz64_s* s, uInt* piSizeVar,
     1386                                                    ZPOS64_T * poffset_local_extrafield,
     1387                                                    uInt  * psize_local_extrafield)
    9641388{
    9651389    uLong uMagic,uData,uFlags;
     
    9721396    *psize_local_extrafield = 0;
    9731397
    974     if (ZSEEK(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile +
     1398    if (ZSEEK64(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile +
    9751399                                s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
    9761400        return UNZ_ERRNO;
     
    9781402
    9791403    if (err==UNZ_OK)
    980         if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
     1404    {
     1405        if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
    9811406            err=UNZ_ERRNO;
    9821407        else if (uMagic!=0x04034b50)
    9831408            err=UNZ_BADZIPFILE;
    984 
    985     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
     1409    }
     1410
     1411    if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
    9861412        err=UNZ_ERRNO;
    9871413/*
     
    9891415        err=UNZ_BADZIPFILE;
    9901416*/
    991     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK)
    992         err=UNZ_ERRNO;
    993 
    994     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
     1417    if (unz64local_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK)
     1418        err=UNZ_ERRNO;
     1419
     1420    if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
    9951421        err=UNZ_ERRNO;
    9961422    else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
     
    9981424
    9991425    if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
     1426/* #ifdef HAVE_BZIP2 */
     1427                         (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
     1428/* #endif */
    10001429                         (s->cur_file_info.compression_method!=Z_DEFLATED))
    10011430        err=UNZ_BADZIPFILE;
    10021431
    1003     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */
    1004         err=UNZ_ERRNO;
    1005 
    1006     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */
    1007         err=UNZ_ERRNO;
    1008     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
    1009                               ((uFlags & 8)==0))
     1432    if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */
     1433        err=UNZ_ERRNO;
     1434
     1435    if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */
     1436        err=UNZ_ERRNO;
     1437    else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && ((uFlags & 8)==0))
    10101438        err=UNZ_BADZIPFILE;
    10111439
    1012     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */
    1013         err=UNZ_ERRNO;
    1014     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
    1015                               ((uFlags & 8)==0))
     1440    if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */
     1441        err=UNZ_ERRNO;
     1442    else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && ((uFlags & 8)==0))
    10161443        err=UNZ_BADZIPFILE;
    10171444
    1018     if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */
    1019         err=UNZ_ERRNO;
    1020     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
    1021                               ((uFlags & 8)==0))
     1445    if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */
     1446        err=UNZ_ERRNO;
     1447    else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && ((uFlags & 8)==0))
    10221448        err=UNZ_BADZIPFILE;
    10231449
    1024 
    1025     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK)
     1450    if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK)
    10261451        err=UNZ_ERRNO;
    10271452    else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
     
    10301455    *piSizeVar += (uInt)size_filename;
    10311456
    1032     if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK)
     1457    if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK)
    10331458        err=UNZ_ERRNO;
    10341459    *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
     
    10451470  If there is no error and the file is opened, the return value is UNZ_OK.
    10461471*/
    1047 extern int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw, password)
    1048     unzFile file;
    1049     int* method;
    1050     int* level;
    1051     int raw;
    1052     const char* password;
     1472extern int ZEXPORT unzOpenCurrentFile3 (unzFile file, int* method,
     1473                                            int* level, int raw, const char* password)
    10531474{
    10541475    int err=UNZ_OK;
    10551476    uInt iSizeVar;
    1056     unz_s* s;
    1057     file_in_zip_read_info_s* pfile_in_zip_read_info;
    1058     uLong offset_local_extrafield;  /* offset of the local extra field */
     1477    unz64_s* s;
     1478    file_in_zip64_read_info_s* pfile_in_zip_read_info;
     1479    ZPOS64_T offset_local_extrafield;  /* offset of the local extra field */
    10591480    uInt  size_local_extrafield;    /* size of the local extra field */
    10601481#    ifndef NOUNCRYPT
     
    10671488    if (file==NULL)
    10681489        return UNZ_PARAMERROR;
    1069     s=(unz_s*)file;
     1490    s=(unz64_s*)file;
    10701491    if (!s->current_file_ok)
    10711492        return UNZ_PARAMERROR;
     
    10741495        unzCloseCurrentFile(file);
    10751496
    1076     if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
    1077                 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
     1497    if (unz64local_CheckCurrentFileCoherencyHeader(s,&iSizeVar, &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
    10781498        return UNZ_BADZIPFILE;
    10791499
    1080     pfile_in_zip_read_info = (file_in_zip_read_info_s*)
    1081                                         ALLOC(sizeof(file_in_zip_read_info_s));
     1500    pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s));
    10821501    if (pfile_in_zip_read_info==NULL)
    10831502        return UNZ_INTERNALERROR;
     
    11121531
    11131532    if ((s->cur_file_info.compression_method!=0) &&
     1533/* #ifdef HAVE_BZIP2 */
     1534        (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
     1535/* #endif */
    11141536        (s->cur_file_info.compression_method!=Z_DEFLATED))
     1537
    11151538        err=UNZ_BADZIPFILE;
    11161539
    11171540    pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
    11181541    pfile_in_zip_read_info->crc32=0;
    1119     pfile_in_zip_read_info->compression_method =
    1120             s->cur_file_info.compression_method;
     1542    pfile_in_zip_read_info->total_out_64=0;
     1543    pfile_in_zip_read_info->compression_method = s->cur_file_info.compression_method;
    11211544    pfile_in_zip_read_info->filestream=s->filestream;
    11221545    pfile_in_zip_read_info->z_filefunc=s->z_filefunc;
     
    11251548    pfile_in_zip_read_info->stream.total_out = 0;
    11261549
    1127     if ((s->cur_file_info.compression_method==Z_DEFLATED) &&
    1128         (!raw))
    1129     {
     1550    if ((s->cur_file_info.compression_method==Z_BZIP2ED) && (!raw))
     1551    {
     1552#ifdef HAVE_BZIP2
     1553      pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
     1554      pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
     1555      pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
     1556      pfile_in_zip_read_info->bstream.state = (voidpf)0;
     1557
    11301558      pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
    11311559      pfile_in_zip_read_info->stream.zfree = (free_func)0;
     
    11341562      pfile_in_zip_read_info->stream.avail_in = 0;
    11351563
     1564      err=BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
     1565      if (err == Z_OK)
     1566        pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED;
     1567      else
     1568      {
     1569        TRYFREE(pfile_in_zip_read_info);
     1570        return err;
     1571      }
     1572#else
     1573      pfile_in_zip_read_info->raw=1;
     1574#endif
     1575    }
     1576    else if ((s->cur_file_info.compression_method==Z_DEFLATED) && (!raw))
     1577    {
     1578      pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
     1579      pfile_in_zip_read_info->stream.zfree = (free_func)0;
     1580      pfile_in_zip_read_info->stream.opaque = (voidpf)0;
     1581      pfile_in_zip_read_info->stream.next_in = 0;
     1582      pfile_in_zip_read_info->stream.avail_in = 0;
     1583
    11361584      err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
    11371585      if (err == Z_OK)
    1138         pfile_in_zip_read_info->stream_initialised=1;
     1586        pfile_in_zip_read_info->stream_initialised=Z_DEFLATED;
    11391587      else
    11401588      {
     
    11631611
    11641612    s->pfile_in_zip_read = pfile_in_zip_read_info;
     1613                s->encrypted = 0;
    11651614
    11661615#    ifndef NOUNCRYPT
     
    11701619        s->pcrc_32_tab = get_crc_table();
    11711620        init_keys(password,s->keys,s->pcrc_32_tab);
    1172         if (ZSEEK(s->z_filefunc, s->filestream,
     1621        if (ZSEEK64(s->z_filefunc, s->filestream,
    11731622                  s->pfile_in_zip_read->pos_in_zipfile +
    11741623                     s->pfile_in_zip_read->byte_before_the_zipfile,
    11751624                  SEEK_SET)!=0)
    11761625            return UNZ_INTERNALERROR;
    1177         if(ZREAD(s->z_filefunc, s->filestream,source, 12)<12)
     1626        if(ZREAD64(s->z_filefunc, s->filestream,source, 12)<12)
    11781627            return UNZ_INTERNALERROR;
    11791628
     
    11901639}
    11911640
    1192 extern int ZEXPORT unzOpenCurrentFile (file)
    1193     unzFile file;
     1641extern int ZEXPORT unzOpenCurrentFile (unzFile file)
    11941642{
    11951643    return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
    11961644}
    11971645
    1198 extern int ZEXPORT unzOpenCurrentFilePassword (file, password)
    1199     unzFile file;
    1200     const char* password;
     1646extern int ZEXPORT unzOpenCurrentFilePassword (unzFile file, const char*  password)
    12011647{
    12021648    return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
    12031649}
    12041650
    1205 extern int ZEXPORT unzOpenCurrentFile2 (file,method,level,raw)
    1206     unzFile file;
    1207     int* method;
    1208     int* level;
    1209     int raw;
     1651extern int ZEXPORT unzOpenCurrentFile2 (unzFile file, int* method, int* level, int raw)
    12101652{
    12111653    return unzOpenCurrentFile3(file, method, level, raw, NULL);
    12121654}
     1655
     1656/** Addition for GDAL : START */
     1657
     1658extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64( unzFile file)
     1659{
     1660    unz64_s* s;
     1661    file_in_zip64_read_info_s* pfile_in_zip_read_info;
     1662    s=(unz64_s*)file;
     1663    if (file==NULL)
     1664        return 0; //UNZ_PARAMERROR;
     1665    pfile_in_zip_read_info=s->pfile_in_zip_read;
     1666    if (pfile_in_zip_read_info==NULL)
     1667        return 0; //UNZ_PARAMERROR;
     1668    return pfile_in_zip_read_info->pos_in_zipfile +
     1669                         pfile_in_zip_read_info->byte_before_the_zipfile;
     1670}
     1671
     1672/** Addition for GDAL : END */
    12131673
    12141674/*
     
    12221682    (UNZ_ERRNO for IO error, or zLib error for uncompress error)
    12231683*/
    1224 extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
    1225     unzFile file;
    1226     voidp buf;
    1227     unsigned len;
     1684extern int ZEXPORT unzReadCurrentFile  (unzFile file, voidp buf, unsigned len)
    12281685{
    12291686    int err=UNZ_OK;
    12301687    uInt iRead = 0;
    1231     unz_s* s;
    1232     file_in_zip_read_info_s* pfile_in_zip_read_info;
     1688    unz64_s* s;
     1689    file_in_zip64_read_info_s* pfile_in_zip_read_info;
    12331690    if (file==NULL)
    12341691        return UNZ_PARAMERROR;
    1235     s=(unz_s*)file;
     1692    s=(unz64_s*)file;
    12361693    pfile_in_zip_read_info=s->pfile_in_zip_read;
    12371694
     
    12711728            if (uReadThis == 0)
    12721729                return UNZ_EOF;
    1273             if (ZSEEK(pfile_in_zip_read_info->z_filefunc,
     1730            if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
    12741731                      pfile_in_zip_read_info->filestream,
    12751732                      pfile_in_zip_read_info->pos_in_zipfile +
     
    12771734                         ZLIB_FILEFUNC_SEEK_SET)!=0)
    12781735                return UNZ_ERRNO;
    1279             if (ZREAD(pfile_in_zip_read_info->z_filefunc,
     1736            if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
    12801737                      pfile_in_zip_read_info->filestream,
    12811738                      pfile_in_zip_read_info->read_buffer,
     
    13221779                *(pfile_in_zip_read_info->stream.next_out+i) =
    13231780                        *(pfile_in_zip_read_info->stream.next_in+i);
     1781
     1782            pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uDoCopy;
    13241783
    13251784            pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
     
    13341793            iRead += uDoCopy;
    13351794        }
    1336         else
     1795        else if (pfile_in_zip_read_info->compression_method==Z_BZIP2ED)
    13371796        {
     1797#ifdef HAVE_BZIP2
    13381798            uLong uTotalOutBefore,uTotalOutAfter;
    13391799            const Bytef *bufBefore;
    13401800            uLong uOutThis;
     1801
     1802            pfile_in_zip_read_info->bstream.next_in        = (char*)pfile_in_zip_read_info->stream.next_in;
     1803            pfile_in_zip_read_info->bstream.avail_in       = pfile_in_zip_read_info->stream.avail_in;
     1804            pfile_in_zip_read_info->bstream.total_in_lo32  = pfile_in_zip_read_info->stream.total_in;
     1805            pfile_in_zip_read_info->bstream.total_in_hi32  = 0;
     1806            pfile_in_zip_read_info->bstream.next_out       = (char*)pfile_in_zip_read_info->stream.next_out;
     1807            pfile_in_zip_read_info->bstream.avail_out      = pfile_in_zip_read_info->stream.avail_out;
     1808            pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out;
     1809            pfile_in_zip_read_info->bstream.total_out_hi32 = 0;
     1810
     1811            uTotalOutBefore = pfile_in_zip_read_info->bstream.total_out_lo32;
     1812            bufBefore = (const Bytef *)pfile_in_zip_read_info->bstream.next_out;
     1813
     1814            err=BZ2_bzDecompress(&pfile_in_zip_read_info->bstream);
     1815
     1816            uTotalOutAfter = pfile_in_zip_read_info->bstream.total_out_lo32;
     1817            uOutThis = uTotalOutAfter-uTotalOutBefore;
     1818
     1819            pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
     1820
     1821            pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,bufBefore, (uInt)(uOutThis));
     1822            pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
     1823            iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
     1824
     1825            pfile_in_zip_read_info->stream.next_in   = (Bytef*)pfile_in_zip_read_info->bstream.next_in;
     1826            pfile_in_zip_read_info->stream.avail_in  = pfile_in_zip_read_info->bstream.avail_in;
     1827            pfile_in_zip_read_info->stream.total_in  = pfile_in_zip_read_info->bstream.total_in_lo32;
     1828            pfile_in_zip_read_info->stream.next_out  = (Bytef*)pfile_in_zip_read_info->bstream.next_out;
     1829            pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out;
     1830            pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32;
     1831
     1832            if (err==BZ_STREAM_END)
     1833              return (iRead==0) ? UNZ_EOF : iRead;
     1834            if (err!=BZ_OK)
     1835              break;
     1836#endif
     1837        } // end Z_BZIP2ED
     1838        else
     1839        {
     1840            ZPOS64_T uTotalOutBefore,uTotalOutAfter;
     1841            const Bytef *bufBefore;
     1842            ZPOS64_T uOutThis;
    13411843            int flush=Z_SYNC_FLUSH;
    13421844
     
    13581860            uOutThis = uTotalOutAfter-uTotalOutBefore;
    13591861
     1862            pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
     1863
    13601864            pfile_in_zip_read_info->crc32 =
    13611865                crc32(pfile_in_zip_read_info->crc32,bufBefore,
     
    13831887  Give the current position in uncompressed data
    13841888*/
    1385 extern z_off_t ZEXPORT unztell (file)
    1386     unzFile file;
    1387 {
    1388     unz_s* s;
    1389     file_in_zip_read_info_s* pfile_in_zip_read_info;
     1889extern z_off_t ZEXPORT unztell (unzFile file)
     1890{
     1891    unz64_s* s;
     1892    file_in_zip64_read_info_s* pfile_in_zip_read_info;
    13901893    if (file==NULL)
    13911894        return UNZ_PARAMERROR;
    1392     s=(unz_s*)file;
     1895    s=(unz64_s*)file;
    13931896    pfile_in_zip_read_info=s->pfile_in_zip_read;
    13941897
     
    13991902}
    14001903
     1904extern ZPOS64_T ZEXPORT unztell64 (unzFile file)
     1905{
     1906
     1907    unz64_s* s;
     1908    file_in_zip64_read_info_s* pfile_in_zip_read_info;
     1909    if (file==NULL)
     1910        return (ZPOS64_T)-1;
     1911    s=(unz64_s*)file;
     1912    pfile_in_zip_read_info=s->pfile_in_zip_read;
     1913
     1914    if (pfile_in_zip_read_info==NULL)
     1915        return (ZPOS64_T)-1;
     1916
     1917    return pfile_in_zip_read_info->total_out_64;
     1918}
     1919
    14011920
    14021921/*
    14031922  return 1 if the end of file was reached, 0 elsewhere
    14041923*/
    1405 extern int ZEXPORT unzeof (file)
    1406     unzFile file;
    1407 {
    1408     unz_s* s;
    1409     file_in_zip_read_info_s* pfile_in_zip_read_info;
     1924extern int ZEXPORT unzeof (unzFile file)
     1925{
     1926    unz64_s* s;
     1927    file_in_zip64_read_info_s* pfile_in_zip_read_info;
    14101928    if (file==NULL)
    14111929        return UNZ_PARAMERROR;
    1412     s=(unz_s*)file;
     1930    s=(unz64_s*)file;
    14131931    pfile_in_zip_read_info=s->pfile_in_zip_read;
    14141932
     
    14251943
    14261944/*
    1427   Read extra field from the current file (opened by unzOpenCurrentFile)
    1428   This is the local-header version of the extra field (sometimes, there is
    1429     more info in the local-header version than in the central-header)
     1945Read extra field from the current file (opened by unzOpenCurrentFile)
     1946This is the local-header version of the extra field (sometimes, there is
     1947more info in the local-header version than in the central-header)
    14301948
    14311949  if buf==NULL, it return the size of the local extra field that can be read
     
    14361954    the error code
    14371955*/
    1438 extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
    1439     unzFile file;
    1440     voidp buf;
    1441     unsigned len;
    1442 {
    1443     unz_s* s;
    1444     file_in_zip_read_info_s* pfile_in_zip_read_info;
     1956extern int ZEXPORT unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len)
     1957{
     1958    unz64_s* s;
     1959    file_in_zip64_read_info_s* pfile_in_zip_read_info;
    14451960    uInt read_now;
    1446     uLong size_to_read;
     1961    ZPOS64_T size_to_read;
    14471962
    14481963    if (file==NULL)
    14491964        return UNZ_PARAMERROR;
    1450     s=(unz_s*)file;
     1965    s=(unz64_s*)file;
    14511966    pfile_in_zip_read_info=s->pfile_in_zip_read;
    14521967
     
    14681983        return 0;
    14691984
    1470     if (ZSEEK(pfile_in_zip_read_info->z_filefunc,
     1985    if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
    14711986              pfile_in_zip_read_info->filestream,
    14721987              pfile_in_zip_read_info->offset_local_extrafield +
     
    14751990        return UNZ_ERRNO;
    14761991
    1477     if (ZREAD(pfile_in_zip_read_info->z_filefunc,
     1992    if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
    14781993              pfile_in_zip_read_info->filestream,
    14791994              buf,read_now)!=read_now)
     
    14872002  Return UNZ_CRCERROR if all the file was read but the CRC is not good
    14882003*/
    1489 extern int ZEXPORT unzCloseCurrentFile (file)
    1490     unzFile file;
     2004extern int ZEXPORT unzCloseCurrentFile (unzFile file)
    14912005{
    14922006    int err=UNZ_OK;
    14932007
    1494     unz_s* s;
    1495     file_in_zip_read_info_s* pfile_in_zip_read_info;
     2008    unz64_s* s;
     2009    file_in_zip64_read_info_s* pfile_in_zip_read_info;
    14962010    if (file==NULL)
    14972011        return UNZ_PARAMERROR;
    1498     s=(unz_s*)file;
     2012    s=(unz64_s*)file;
    14992013    pfile_in_zip_read_info=s->pfile_in_zip_read;
    15002014
     
    15132027    TRYFREE(pfile_in_zip_read_info->read_buffer);
    15142028    pfile_in_zip_read_info->read_buffer = NULL;
    1515     if (pfile_in_zip_read_info->stream_initialised)
     2029    if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
    15162030        inflateEnd(&pfile_in_zip_read_info->stream);
     2031#ifdef HAVE_BZIP2
     2032    else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
     2033        BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
     2034#endif
     2035
    15172036
    15182037    pfile_in_zip_read_info->stream_initialised = 0;
     
    15302049  return the number of byte copied or an error code <0
    15312050*/
    1532 extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
    1533     unzFile file;
    1534     char *szComment;
    1535     uLong uSizeBuf;
    1536 {
    1537     int err=UNZ_OK;
    1538     unz_s* s;
     2051extern int ZEXPORT unzGetGlobalComment (unzFile file, char * szComment, uLong uSizeBuf)
     2052{
     2053    unz64_s* s;
    15392054    uLong uReadThis ;
    15402055    if (file==NULL)
    1541         return UNZ_PARAMERROR;
    1542     s=(unz_s*)file;
     2056        return (int)UNZ_PARAMERROR;
     2057    s=(unz64_s*)file;
    15432058
    15442059    uReadThis = uSizeBuf;
     
    15462061        uReadThis = s->gi.size_comment;
    15472062
    1548     if (ZSEEK(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0)
     2063    if (ZSEEK64(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0)
    15492064        return UNZ_ERRNO;
    15502065
     
    15522067    {
    15532068      *szComment='\0';
    1554       if (ZREAD(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis)
     2069      if (ZREAD64(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis)
    15552070        return UNZ_ERRNO;
    15562071    }
     
    15622077
    15632078/* Additions by RX '2004 */
    1564 extern uLong ZEXPORT unzGetOffset (file)
    1565     unzFile file;
    1566 {
    1567     unz_s* s;
     2079extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file)
     2080{
     2081    unz64_s* s;
    15682082
    15692083    if (file==NULL)
    1570           return UNZ_PARAMERROR;
    1571     s=(unz_s*)file;
     2084          return 0; //UNZ_PARAMERROR;
     2085    s=(unz64_s*)file;
    15722086    if (!s->current_file_ok)
    15732087      return 0;
     
    15782092}
    15792093
    1580 extern int ZEXPORT unzSetOffset (file, pos)
    1581         unzFile file;
    1582         uLong pos;
    1583 {
    1584     unz_s* s;
     2094extern uLong ZEXPORT unzGetOffset (unzFile file)
     2095{
     2096    ZPOS64_T offset64;
     2097
     2098    if (file==NULL)
     2099          return 0; //UNZ_PARAMERROR;
     2100    offset64 = unzGetOffset64(file);
     2101    return (uLong)offset64;
     2102}
     2103
     2104extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos)
     2105{
     2106    unz64_s* s;
    15852107    int err;
    15862108
    15872109    if (file==NULL)
    15882110        return UNZ_PARAMERROR;
    1589     s=(unz_s*)file;
     2111    s=(unz64_s*)file;
    15902112
    15912113    s->pos_in_central_dir = pos;
    15922114    s->num_file = s->gi.number_entry;      /* hack */
    1593     err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
     2115    err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
    15942116                                              &s->cur_file_info_internal,
    15952117                                              NULL,0,NULL,0,NULL,0);
     
    15972119    return err;
    15982120}
     2121
     2122extern int ZEXPORT unzSetOffset (unzFile file, uLong pos)
     2123{
     2124    return unzSetOffset64(file,pos);
     2125}
  • cpukit/zlib/contrib/minizip/unzip.h

    re8f3eb9 ra0caafe  
    11/* unzip.h -- IO for uncompress .zip files using zlib
    2    Version 1.01e, February 12th, 2005
    3 
    4    Copyright (C) 1998-2005 Gilles Vollant
    5 
    6    This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
    7      WinZip, InfoZip tools and compatible.
    8 
    9    Multi volume ZipFile (span) are not supported.
    10    Encryption compatible with pkzip 2.04g only supported
    11    Old compressions used by old PKZip 1.x are not supported
    12 
    13 
    14    I WAIT FEEDBACK at mail info@winimage.com
    15    Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution
    16 
    17    Condition of use and distribution are the same than zlib :
     2   Version 1.1, February 14h, 2010
     3   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     4
     5         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     6
     7         Modifications of Unzip for Zip64
     8         Copyright (C) 2007-2008 Even Rouault
     9
     10         Modifications for Zip64 support on both zip and unzip
     11         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
     12
     13         For more info read MiniZip_info.txt
     14
     15         ---------------------------------------------------------------------------------
     16
     17        Condition of use and distribution are the same than zlib :
    1818
    1919  This software is provided 'as-is', without any express or implied
     
    3333  3. This notice may not be removed or altered from any source distribution.
    3434
    35 
    36 */
    37 
    38 /* for more info about .ZIP format, see
    39       http://www.info-zip.org/pub/infozip/doc/appnote-981119-iz.zip
    40       http://www.info-zip.org/pub/infozip/doc/
    41    PkWare has also a specification at :
    42       ftp://ftp.pkware.com/probdesc.zip
    43 */
    44 
    45 #ifndef _unz_H
    46 #define _unz_H
     35  ---------------------------------------------------------------------------------
     36
     37        Changes
     38
     39        See header of unzip64.c
     40
     41*/
     42
     43#ifndef _unz64_H
     44#define _unz64_H
    4745
    4846#ifdef __cplusplus
     
    5452#endif
    5553
    56 #ifndef _ZLIBIOAPI_H
     54#ifndef  _ZLIBIOAPI_H
    5755#include "ioapi.h"
    5856#endif
     57
     58#ifdef HAVE_BZIP2
     59#include "bzlib.h"
     60#endif
     61
     62#define Z_BZIP2ED 12
    5963
    6064#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
     
    9094/* unz_global_info structure contain global data about the ZIPfile
    9195   These data comes from the end of central dir */
     96typedef struct unz_global_info64_s
     97{
     98    ZPOS64_T number_entry;         /* total number of entries in
     99                                     the central dir on this disk */
     100    uLong size_comment;         /* size of the global comment of the zipfile */
     101} unz_global_info64;
     102
    92103typedef struct unz_global_info_s
    93104{
    94105    uLong number_entry;         /* total number of entries in
    95                        the central dir on this disk */
     106                                     the central dir on this disk */
    96107    uLong size_comment;         /* size of the global comment of the zipfile */
    97108} unz_global_info;
    98109
    99 
    100110/* unz_file_info contain information about a file in the zipfile */
     111typedef struct unz_file_info64_s
     112{
     113    uLong version;              /* version made by                 2 bytes */
     114    uLong version_needed;       /* version needed to extract       2 bytes */
     115    uLong flag;                 /* general purpose bit flag        2 bytes */
     116    uLong compression_method;   /* compression method              2 bytes */
     117    uLong dosDate;              /* last mod file date in Dos fmt   4 bytes */
     118    uLong crc;                  /* crc-32                          4 bytes */
     119    ZPOS64_T compressed_size;   /* compressed size                 8 bytes */
     120    ZPOS64_T uncompressed_size; /* uncompressed size               8 bytes */
     121    uLong size_filename;        /* filename length                 2 bytes */
     122    uLong size_file_extra;      /* extra field length              2 bytes */
     123    uLong size_file_comment;    /* file comment length             2 bytes */
     124
     125    uLong disk_num_start;       /* disk number start               2 bytes */
     126    uLong internal_fa;          /* internal file attributes        2 bytes */
     127    uLong external_fa;          /* external file attributes        4 bytes */
     128
     129    tm_unz tmu_date;
     130} unz_file_info64;
     131
    101132typedef struct unz_file_info_s
    102133{
     
    134165
    135166extern unzFile ZEXPORT unzOpen OF((const char *path));
     167extern unzFile ZEXPORT unzOpen64 OF((const void *path));
    136168/*
    137169  Open a Zip file. path contain the full pathname (by example,
     
    142174     Else, the return value is a unzFile Handle, usable with other function
    143175       of this unzip package.
    144 */
     176     the "64" function take a const void* pointer, because the path is just the
     177       value passed to the open64_file_func callback.
     178     Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path
     179       is a pointer to a wide unicode string (LPCTSTR is LPCWSTR), so const char*
     180       does not describe the reality
     181*/
     182
    145183
    146184extern unzFile ZEXPORT unzOpen2 OF((const char *path,
     
    148186/*
    149187   Open a Zip file, like unzOpen, but provide a set of file low level API
     188      for read/write the zip file (see ioapi.h)
     189*/
     190
     191extern unzFile ZEXPORT unzOpen2_64 OF((const void *path,
     192                                    zlib_filefunc64_def* pzlib_filefunc_def));
     193/*
     194   Open a Zip file, like unz64Open, but provide a set of file low level API
    150195      for read/write the zip file (see ioapi.h)
    151196*/
     
    160205extern int ZEXPORT unzGetGlobalInfo OF((unzFile file,
    161206                                        unz_global_info *pglobal_info));
     207
     208extern int ZEXPORT unzGetGlobalInfo64 OF((unzFile file,
     209                                        unz_global_info64 *pglobal_info));
    162210/*
    163211  Write info about the ZipFile in the *pglobal_info structure.
     
    222270    unz_file_pos* file_pos);
    223271
     272typedef struct unz64_file_pos_s
     273{
     274    ZPOS64_T pos_in_zip_directory;   /* offset in zip file directory */
     275    ZPOS64_T num_of_file;            /* # of file */
     276} unz64_file_pos;
     277
     278extern int ZEXPORT unzGetFilePos64(
     279    unzFile file,
     280    unz64_file_pos* file_pos);
     281
     282extern int ZEXPORT unzGoToFilePos64(
     283    unzFile file,
     284    const unz64_file_pos* file_pos);
     285
    224286/* ****************************************** */
     287
     288extern int ZEXPORT unzGetCurrentFileInfo64 OF((unzFile file,
     289                         unz_file_info64 *pfile_info,
     290                         char *szFileName,
     291                         uLong fileNameBufferSize,
     292                         void *extraField,
     293                         uLong extraFieldBufferSize,
     294                         char *szComment,
     295                         uLong commentBufferSize));
    225296
    226297extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file,
     
    245316*/
    246317
     318
     319/** Addition for GDAL : START */
     320
     321extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64 OF((unzFile file));
     322
     323/** Addition for GDAL : END */
     324
     325
    247326/***************************************************************************/
    248327/* for reading the content of the current zipfile, you can open it, read data
     
    313392
    314393extern z_off_t ZEXPORT unztell OF((unzFile file));
     394
     395extern ZPOS64_T ZEXPORT unztell64 OF((unzFile file));
    315396/*
    316397  Give the current position in uncompressed data
     
    341422
    342423/* Get the current file offset */
     424extern ZPOS64_T ZEXPORT unzGetOffset64 (unzFile file);
    343425extern uLong ZEXPORT unzGetOffset (unzFile file);
    344426
    345427/* Set the current file offset */
     428extern int ZEXPORT unzSetOffset64 (unzFile file, ZPOS64_T pos);
    346429extern int ZEXPORT unzSetOffset (unzFile file, uLong pos);
    347430
     
    352435#endif
    353436
    354 #endif /* _unz_H */
     437#endif /* _unz64_H */
  • cpukit/zlib/contrib/minizip/zip.c

    re8f3eb9 ra0caafe  
    11/* zip.c -- IO on .zip files using zlib
    2    Version 1.01e, February 12th, 2005
    3 
    4    27 Dec 2004 Rolf Kalbermatter
    5    Modification to zipOpen2 to support globalComment retrieval.
    6 
    7    Copyright (C) 1998-2005 Gilles Vollant
    8 
    9    Read zip.h for more info
     2   Version 1.1, February 14h, 2010
     3   part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
     4
     5         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
     6
     7         Modifications for Zip64 support
     8         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
     9
     10         For more info read MiniZip_info.txt
     11
     12         Changes
     13   Oct-2009 - Mathias Svensson - Remove old C style function prototypes
     14   Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives
     15   Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions.
     16   Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data
     17                                 It is used when recreting zip archive with RAW when deleting items from a zip.
     18                                 ZIP64 data is automaticly added to items that needs it, and existing ZIP64 data need to be removed.
     19   Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required)
     20   Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
     21
    1022*/
    1123
     
    4052
    4153#ifndef Z_BUFSIZE
    42 #define Z_BUFSIZE (16384)
     54#define Z_BUFSIZE (64*1024) //(16384)
    4355#endif
    4456
     
    6072
    6173/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
     74
     75
     76// NOT sure that this work on ALL platform
     77#define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
    6278
    6379#ifndef SEEK_CUR
     
    8096#endif
    8197#endif
    82 const char zip_copyright[] =
    83    " zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
     98const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
    8499
    85100
     
    89104#define CENTRALHEADERMAGIC  (0x02014b50)
    90105#define ENDHEADERMAGIC      (0x06054b50)
     106#define ZIP64ENDHEADERMAGIC      (0x6064b50)
     107#define ZIP64ENDLOCHEADERMAGIC   (0x7064b50)
    91108
    92109#define FLAG_LOCALHEADER_OFFSET (0x06)
     
    114131{
    115132    z_stream stream;            /* zLib stream structure for inflate */
     133#ifdef HAVE_BZIP2
     134    bz_stream bstream;          /* bzLib stream structure for bziped */
     135#endif
     136
    116137    int  stream_initialised;    /* 1 is stream is initialised */
    117138    uInt pos_in_buffered_data;  /* last written byte in buffered_data */
    118139
    119     uLong pos_local_header;     /* offset of the local header of the file
     140    ZPOS64_T pos_local_header;     /* offset of the local header of the file
    120141                                     currenty writing */
    121142    char* central_header;       /* central header data for the current file */
     143    uLong size_centralExtra;
    122144    uLong size_centralheader;   /* size of the central header for cur file */
     145    uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */
    123146    uLong flag;                 /* flag of the file currently writing */
    124147
     
    129152    uLong crc32;
    130153    int  encrypt;
     154    int  zip64;               /* Add ZIP64 extened information in the extra field */
     155    ZPOS64_T pos_zip64extrainfo;
     156    ZPOS64_T totalCompressedData;
     157    ZPOS64_T totalUncompressedData;
    131158#ifndef NOCRYPT
    132159    unsigned long keys[3];     /* keys defining the pseudo-random sequence */
     
    134161    int crypt_header_size;
    135162#endif
    136 } curfile_info;
     163} curfile64_info;
    137164
    138165typedef struct
    139166{
    140     zlib_filefunc_def z_filefunc;
     167    zlib_filefunc64_32_def z_filefunc;
    141168    voidpf filestream;        /* io structore of the zipfile */
    142169    linkedlist_data central_dir;/* datablock with central dir in construction*/
    143170    int  in_opened_file_inzip;  /* 1 if a file in the zip is currently writ.*/
    144     curfile_info ci;            /* info on the file curretly writing */
    145 
    146     uLong begin_pos;            /* position of the beginning of the zipfile */
    147     uLong add_position_when_writting_offset;
    148     uLong number_entry;
     171    curfile64_info ci;            /* info on the file curretly writing */
     172
     173    ZPOS64_T begin_pos;            /* position of the beginning of the zipfile */
     174    ZPOS64_T add_position_when_writting_offset;
     175    ZPOS64_T number_entry;
     176
    149177#ifndef NO_ADDFILEINEXISTINGZIP
    150178    char *globalcomment;
    151179#endif
    152 } zip_internal;
    153 
     180
     181} zip64_internal;
    154182
    155183
     
    173201}
    174202
    175 local void free_datablock(ldi)
    176     linkedlist_datablock_internal* ldi;
     203local void free_datablock(linkedlist_datablock_internal* ldi)
    177204{
    178205    while (ldi!=NULL)
     
    184211}
    185212
    186 local void init_linkedlist(ll)
    187     linkedlist_data* ll;
     213local void init_linkedlist(linkedlist_data* ll)
    188214{
    189215    ll->first_block = ll->last_block = NULL;
    190216}
    191217
    192 local void free_linkedlist(ll)
    193     linkedlist_data* ll;
     218local void free_linkedlist(linkedlist_data* ll)
    194219{
    195220    free_datablock(ll->first_block);
     
    198223
    199224
    200 local int add_data_in_datablock(ll,buf,len)
    201     linkedlist_data* ll;
    202     const void* buf;
    203     uLong len;
     225local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len)
    204226{
    205227    linkedlist_datablock_internal* ldi;
     
    259281/* ===========================================================================
    260282   Inputs a long in LSB order to the given file
    261    nbByte == 1, 2 or 4 (byte, short or long)
     283   nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
    262284*/
    263285
    264 local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def,
    265                                 voidpf filestream, uLong x, int nbByte));
    266 local int ziplocal_putValue (pzlib_filefunc_def, filestream, x, nbByte)
    267     const zlib_filefunc_def* pzlib_filefunc_def;
    268     voidpf filestream;
    269     uLong x;
    270     int nbByte;
    271 {
    272     unsigned char buf[4];
     286local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte));
     287local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)
     288{
     289    unsigned char buf[8];
    273290    int n;
    274291    for (n = 0; n < nbByte; n++)
     
    285302      }
    286303
    287     if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
     304    if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
    288305        return ZIP_ERRNO;
    289306    else
     
    291308}
    292309
    293 local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
    294 local void ziplocal_putValue_inmemory (dest, x, nbByte)
    295     void* dest;
    296     uLong x;
    297     int nbByte;
     310local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte));
     311local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte)
    298312{
    299313    unsigned char* buf=(unsigned char*)dest;
     
    316330
    317331
    318 local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
    319     const tm_zip* ptm;
    320     uLong dosDate;
     332local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm)
    321333{
    322334    uLong year = (uLong)ptm->tm_year;
    323     if (year>1980)
     335    if (year>=1980)
    324336        year-=1980;
    325     else if (year>80)
     337    else if (year>=80)
    326338        year-=80;
    327339    return
     
    333345/****************************************************************************/
    334346
    335 local int ziplocal_getByte OF((
    336     const zlib_filefunc_def* pzlib_filefunc_def,
    337     voidpf filestream,
    338     int *pi));
    339 
    340 local int ziplocal_getByte(pzlib_filefunc_def,filestream,pi)
    341     const zlib_filefunc_def* pzlib_filefunc_def;
    342     voidpf filestream;
    343     int *pi;
     347local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi));
     348
     349local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi)
    344350{
    345351    unsigned char c;
    346     int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
     352    int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
    347353    if (err==1)
    348354    {
     
    352358    else
    353359    {
    354         if (ZERROR(*pzlib_filefunc_def,filestream))
     360        if (ZERROR64(*pzlib_filefunc_def,filestream))
    355361            return ZIP_ERRNO;
    356362        else
     
    363369   Reads a long in LSB order from the given gz_stream. Sets
    364370*/
    365 local int ziplocal_getShort OF((
    366     const zlib_filefunc_def* pzlib_filefunc_def,
    367     voidpf filestream,
    368     uLong *pX));
    369 
    370 local int ziplocal_getShort (pzlib_filefunc_def,filestream,pX)
    371     const zlib_filefunc_def* pzlib_filefunc_def;
    372     voidpf filestream;
    373     uLong *pX;
     371local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
     372
     373local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
    374374{
    375375    uLong x ;
    376     int i;
     376    int i = 0;
    377377    int err;
    378378
    379     err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
     379    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
    380380    x = (uLong)i;
    381381
    382382    if (err==ZIP_OK)
    383         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
     383        err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
    384384    x += ((uLong)i)<<8;
    385385
     
    391391}
    392392
    393 local int ziplocal_getLong OF((
    394     const zlib_filefunc_def* pzlib_filefunc_def,
    395     voidpf filestream,
    396     uLong *pX));
    397 
    398 local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX)
    399     const zlib_filefunc_def* pzlib_filefunc_def;
    400     voidpf filestream;
    401     uLong *pX;
     393local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
     394
     395local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
    402396{
    403397    uLong x ;
    404     int i;
     398    int i = 0;
    405399    int err;
    406400
    407     err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
     401    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
    408402    x = (uLong)i;
    409403
    410404    if (err==ZIP_OK)
    411         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
     405        err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
    412406    x += ((uLong)i)<<8;
    413407
    414408    if (err==ZIP_OK)
    415         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
     409        err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
    416410    x += ((uLong)i)<<16;
    417411
    418412    if (err==ZIP_OK)
    419         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
     413        err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
    420414    x += ((uLong)i)<<24;
    421415
     
    425419        *pX = 0;
    426420    return err;
     421}
     422
     423local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX));
     424
     425
     426local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
     427{
     428  ZPOS64_T x;
     429  int i = 0;
     430  int err;
     431
     432  err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     433  x = (ZPOS64_T)i;
     434
     435  if (err==ZIP_OK)
     436    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     437  x += ((ZPOS64_T)i)<<8;
     438
     439  if (err==ZIP_OK)
     440    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     441  x += ((ZPOS64_T)i)<<16;
     442
     443  if (err==ZIP_OK)
     444    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     445  x += ((ZPOS64_T)i)<<24;
     446
     447  if (err==ZIP_OK)
     448    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     449  x += ((ZPOS64_T)i)<<32;
     450
     451  if (err==ZIP_OK)
     452    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     453  x += ((ZPOS64_T)i)<<40;
     454
     455  if (err==ZIP_OK)
     456    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     457  x += ((ZPOS64_T)i)<<48;
     458
     459  if (err==ZIP_OK)
     460    err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
     461  x += ((ZPOS64_T)i)<<56;
     462
     463  if (err==ZIP_OK)
     464    *pX = x;
     465  else
     466    *pX = 0;
     467
     468  return err;
    427469}
    428470
     
    434476    the global comment)
    435477*/
    436 local uLong ziplocal_SearchCentralDir OF((
    437     const zlib_filefunc_def* pzlib_filefunc_def,
    438     voidpf filestream));
    439 
    440 local uLong ziplocal_SearchCentralDir(pzlib_filefunc_def,filestream)
    441     const zlib_filefunc_def* pzlib_filefunc_def;
    442     voidpf filestream;
    443 {
    444     unsigned char* buf;
    445     uLong uSizeFile;
    446     uLong uBackRead;
    447     uLong uMaxBack=0xffff; /* maximum size of global comment */
    448     uLong uPosFound=0;
    449 
    450     if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
    451         return 0;
    452 
    453 
    454     uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
    455 
    456     if (uMaxBack>uSizeFile)
    457         uMaxBack = uSizeFile;
    458 
    459     buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
    460     if (buf==NULL)
    461         return 0;
    462 
    463     uBackRead = 4;
    464     while (uBackRead<uMaxBack)
    465     {
    466         uLong uReadSize,uReadPos ;
    467         int i;
    468         if (uBackRead+BUFREADCOMMENT>uMaxBack)
    469             uBackRead = uMaxBack;
    470         else
    471             uBackRead+=BUFREADCOMMENT;
    472         uReadPos = uSizeFile-uBackRead ;
    473 
    474         uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
    475                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
    476         if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
    477             break;
    478 
    479         if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
    480             break;
    481 
    482         for (i=(int)uReadSize-3; (i--)>0;)
    483             if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
    484                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
    485             {
    486                 uPosFound = uReadPos+i;
    487                 break;
    488             }
    489 
    490         if (uPosFound!=0)
    491             break;
    492     }
    493     TRYFREE(buf);
    494     return uPosFound;
    495 }
     478local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
     479
     480local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
     481{
     482  unsigned char* buf;
     483  ZPOS64_T uSizeFile;
     484  ZPOS64_T uBackRead;
     485  ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
     486  ZPOS64_T uPosFound=0;
     487
     488  if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
     489    return 0;
     490
     491
     492  uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
     493
     494  if (uMaxBack>uSizeFile)
     495    uMaxBack = uSizeFile;
     496
     497  buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
     498  if (buf==NULL)
     499    return 0;
     500
     501  uBackRead = 4;
     502  while (uBackRead<uMaxBack)
     503  {
     504    uLong uReadSize;
     505    ZPOS64_T uReadPos ;
     506    int i;
     507    if (uBackRead+BUFREADCOMMENT>uMaxBack)
     508      uBackRead = uMaxBack;
     509    else
     510      uBackRead+=BUFREADCOMMENT;
     511    uReadPos = uSizeFile-uBackRead ;
     512
     513    uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
     514      (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
     515    if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
     516      break;
     517
     518    if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
     519      break;
     520
     521    for (i=(int)uReadSize-3; (i--)>0;)
     522      if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
     523        ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
     524      {
     525        uPosFound = uReadPos+i;
     526        break;
     527      }
     528
     529      if (uPosFound!=0)
     530        break;
     531  }
     532  TRYFREE(buf);
     533  return uPosFound;
     534}
     535
     536/*
     537Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before
     538the global comment)
     539*/
     540local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
     541
     542local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
     543{
     544  unsigned char* buf;
     545  ZPOS64_T uSizeFile;
     546  ZPOS64_T uBackRead;
     547  ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
     548  ZPOS64_T uPosFound=0;
     549  uLong uL;
     550  ZPOS64_T relativeOffset;
     551
     552  if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
     553    return 0;
     554
     555  uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
     556
     557  if (uMaxBack>uSizeFile)
     558    uMaxBack = uSizeFile;
     559
     560  buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
     561  if (buf==NULL)
     562    return 0;
     563
     564  uBackRead = 4;
     565  while (uBackRead<uMaxBack)
     566  {
     567    uLong uReadSize;
     568    ZPOS64_T uReadPos;
     569    int i;
     570    if (uBackRead+BUFREADCOMMENT>uMaxBack)
     571      uBackRead = uMaxBack;
     572    else
     573      uBackRead+=BUFREADCOMMENT;
     574    uReadPos = uSizeFile-uBackRead ;
     575
     576    uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
     577      (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
     578    if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
     579      break;
     580
     581    if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
     582      break;
     583
     584    for (i=(int)uReadSize-3; (i--)>0;)
     585    {
     586      // Signature "0x07064b50" Zip64 end of central directory locater
     587      if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
     588      {
     589        uPosFound = uReadPos+i;
     590        break;
     591      }
     592    }
     593
     594      if (uPosFound!=0)
     595        break;
     596  }
     597
     598  TRYFREE(buf);
     599  if (uPosFound == 0)
     600    return 0;
     601
     602  /* Zip64 end of central directory locator */
     603  if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
     604    return 0;
     605
     606  /* the signature, already checked */
     607  if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
     608    return 0;
     609
     610  /* number of the disk with the start of the zip64 end of  central directory */
     611  if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
     612    return 0;
     613  if (uL != 0)
     614    return 0;
     615
     616  /* relative offset of the zip64 end of central directory record */
     617  if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK)
     618    return 0;
     619
     620  /* total number of disks */
     621  if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
     622    return 0;
     623  if (uL != 1)
     624    return 0;
     625
     626  /* Goto Zip64 end of central directory record */
     627  if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
     628    return 0;
     629
     630  /* the signature */
     631  if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
     632    return 0;
     633
     634  if (uL != 0x06064b50) // signature of 'Zip64 end of central directory'
     635    return 0;
     636
     637  return relativeOffset;
     638}
     639
     640int LoadCentralDirectoryRecord(zip64_internal* pziinit)
     641{
     642  int err=ZIP_OK;
     643  ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
     644
     645  ZPOS64_T size_central_dir;     /* size of the central directory  */
     646  ZPOS64_T offset_central_dir;   /* offset of start of central directory */
     647  ZPOS64_T central_pos;
     648  uLong uL;
     649
     650  uLong number_disk;          /* number of the current dist, used for
     651                              spaning ZIP, unsupported, always 0*/
     652  uLong number_disk_with_CD;  /* number the the disk with central dir, used
     653                              for spaning ZIP, unsupported, always 0*/
     654  ZPOS64_T number_entry;
     655  ZPOS64_T number_entry_CD;      /* total number of entries in
     656                                the central dir
     657                                (same than number_entry on nospan) */
     658  uLong VersionMadeBy;
     659  uLong VersionNeeded;
     660  uLong size_comment;
     661
     662  int hasZIP64Record = 0;
     663
     664  // check first if we find a ZIP64 record
     665  central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream);
     666  if(central_pos > 0)
     667  {
     668    hasZIP64Record = 1;
     669  }
     670  else if(central_pos == 0)
     671  {
     672    central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream);
     673  }
     674
     675/* disable to allow appending to empty ZIP archive
     676        if (central_pos==0)
     677            err=ZIP_ERRNO;
     678*/
     679
     680  if(hasZIP64Record)
     681  {
     682    ZPOS64_T sizeEndOfCentralDirectory;
     683    if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
     684      err=ZIP_ERRNO;
     685
     686    /* the signature, already checked */
     687    if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
     688      err=ZIP_ERRNO;
     689
     690    /* size of zip64 end of central directory record */
     691    if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK)
     692      err=ZIP_ERRNO;
     693
     694    /* version made by */
     695    if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK)
     696      err=ZIP_ERRNO;
     697
     698    /* version needed to extract */
     699    if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK)
     700      err=ZIP_ERRNO;
     701
     702    /* number of this disk */
     703    if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
     704      err=ZIP_ERRNO;
     705
     706    /* number of the disk with the start of the central directory */
     707    if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
     708      err=ZIP_ERRNO;
     709
     710    /* total number of entries in the central directory on this disk */
     711    if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK)
     712      err=ZIP_ERRNO;
     713
     714    /* total number of entries in the central directory */
     715    if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK)
     716      err=ZIP_ERRNO;
     717
     718    if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
     719      err=ZIP_BADZIPFILE;
     720
     721    /* size of the central directory */
     722    if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK)
     723      err=ZIP_ERRNO;
     724
     725    /* offset of start of central directory with respect to the
     726    starting disk number */
     727    if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK)
     728      err=ZIP_ERRNO;
     729
     730    // TODO..
     731    // read the comment from the standard central header.
     732    size_comment = 0;
     733  }
     734  else
     735  {
     736    // Read End of central Directory info
     737    if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
     738      err=ZIP_ERRNO;
     739
     740    /* the signature, already checked */
     741    if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
     742      err=ZIP_ERRNO;
     743
     744    /* number of this disk */
     745    if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
     746      err=ZIP_ERRNO;
     747
     748    /* number of the disk with the start of the central directory */
     749    if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
     750      err=ZIP_ERRNO;
     751
     752    /* total number of entries in the central dir on this disk */
     753    number_entry = 0;
     754    if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
     755      err=ZIP_ERRNO;
     756    else
     757      number_entry = uL;
     758
     759    /* total number of entries in the central dir */
     760    number_entry_CD = 0;
     761    if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
     762      err=ZIP_ERRNO;
     763    else
     764      number_entry_CD = uL;
     765
     766    if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
     767      err=ZIP_BADZIPFILE;
     768
     769    /* size of the central directory */
     770    size_central_dir = 0;
     771    if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
     772      err=ZIP_ERRNO;
     773    else
     774      size_central_dir = uL;
     775
     776    /* offset of start of central directory with respect to the starting disk number */
     777    offset_central_dir = 0;
     778    if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
     779      err=ZIP_ERRNO;
     780    else
     781      offset_central_dir = uL;
     782
     783
     784    /* zipfile global comment length */
     785    if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK)
     786      err=ZIP_ERRNO;
     787  }
     788
     789  if ((central_pos<offset_central_dir+size_central_dir) &&
     790    (err==ZIP_OK))
     791    err=ZIP_BADZIPFILE;
     792
     793  if (err!=ZIP_OK)
     794  {
     795    ZCLOSE64(pziinit->z_filefunc, pziinit->filestream);
     796    return ZIP_ERRNO;
     797  }
     798
     799  if (size_comment>0)
     800  {
     801    pziinit->globalcomment = (char*)ALLOC(size_comment+1);
     802    if (pziinit->globalcomment)
     803    {
     804      size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment);
     805      pziinit->globalcomment[size_comment]=0;
     806    }
     807  }
     808
     809  byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
     810  pziinit->add_position_when_writting_offset = byte_before_the_zipfile;
     811
     812  {
     813    ZPOS64_T size_central_dir_to_read = size_central_dir;
     814    size_t buf_size = SIZEDATA_INDATABLOCK;
     815    void* buf_read = (void*)ALLOC(buf_size);
     816    if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
     817      err=ZIP_ERRNO;
     818
     819    while ((size_central_dir_to_read>0) && (err==ZIP_OK))
     820    {
     821      ZPOS64_T read_this = SIZEDATA_INDATABLOCK;
     822      if (read_this > size_central_dir_to_read)
     823        read_this = size_central_dir_to_read;
     824
     825      if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this)
     826        err=ZIP_ERRNO;
     827
     828      if (err==ZIP_OK)
     829        err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
     830
     831      size_central_dir_to_read-=read_this;
     832    }
     833    TRYFREE(buf_read);
     834  }
     835  pziinit->begin_pos = byte_before_the_zipfile;
     836  pziinit->number_entry = number_entry_CD;
     837
     838  if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
     839    err=ZIP_ERRNO;
     840
     841  return err;
     842}
     843
     844
    496845#endif /* !NO_ADDFILEINEXISTINGZIP*/
    497846
     847
    498848/************************************************************/
    499 extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc_def)
    500     const char *pathname;
    501     int append;
    502     zipcharpc* globalcomment;
    503     zlib_filefunc_def* pzlib_filefunc_def;
    504 {
    505     zip_internal ziinit;
    506     zip_internal* zi;
     849extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def)
     850{
     851    zip64_internal ziinit;
     852    zip64_internal* zi;
    507853    int err=ZIP_OK;
    508854
    509 
    510     if (pzlib_filefunc_def==NULL)
    511         fill_fopen_filefunc(&ziinit.z_filefunc);
    512     else
    513         ziinit.z_filefunc = *pzlib_filefunc_def;
    514 
    515     ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
    516                  (ziinit.z_filefunc.opaque,
     855    ziinit.z_filefunc.zseek32_file = NULL;
     856    ziinit.z_filefunc.ztell32_file = NULL;
     857    if (pzlib_filefunc64_32_def==NULL)
     858        fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64);
     859    else
     860        ziinit.z_filefunc = *pzlib_filefunc64_32_def;
     861
     862    ziinit.filestream = ZOPEN64(ziinit.z_filefunc,
    517863                  pathname,
    518864                  (append == APPEND_STATUS_CREATE) ?
     
    522868    if (ziinit.filestream == NULL)
    523869        return NULL;
    524     ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
     870
     871    if (append == APPEND_STATUS_CREATEAFTER)
     872        ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
     873
     874    ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream);
    525875    ziinit.in_opened_file_inzip = 0;
    526876    ziinit.ci.stream_initialised = 0;
     
    530880
    531881
    532     zi = (zip_internal*)ALLOC(sizeof(zip_internal));
     882
     883    zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
    533884    if (zi==NULL)
    534885    {
    535         ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
     886        ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
    536887        return NULL;
    537888    }
     
    542893    if (append == APPEND_STATUS_ADDINZIP)
    543894    {
    544         uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
    545 
    546         uLong size_central_dir;     /* size of the central directory  */
    547         uLong offset_central_dir;   /* offset of start of central directory */
    548         uLong central_pos,uL;
    549 
    550         uLong number_disk;          /* number of the current dist, used for
    551                                     spaning ZIP, unsupported, always 0*/
    552         uLong number_disk_with_CD;  /* number the the disk with central dir, used
    553                                     for spaning ZIP, unsupported, always 0*/
    554         uLong number_entry;
    555         uLong number_entry_CD;      /* total number of entries in
    556                                     the central dir
    557                                     (same than number_entry on nospan) */
    558         uLong size_comment;
    559 
    560         central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
    561         if (central_pos==0)
    562             err=ZIP_ERRNO;
    563 
    564         if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
    565                                         central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
    566             err=ZIP_ERRNO;
    567 
    568         /* the signature, already checked */
    569         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
    570             err=ZIP_ERRNO;
    571 
    572         /* number of this disk */
    573         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
    574             err=ZIP_ERRNO;
    575 
    576         /* number of the disk with the start of the central directory */
    577         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
    578             err=ZIP_ERRNO;
    579 
    580         /* total number of entries in the central dir on this disk */
    581         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
    582             err=ZIP_ERRNO;
    583 
    584         /* total number of entries in the central dir */
    585         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
    586             err=ZIP_ERRNO;
    587 
    588         if ((number_entry_CD!=number_entry) ||
    589             (number_disk_with_CD!=0) ||
    590             (number_disk!=0))
    591             err=ZIP_BADZIPFILE;
    592 
    593         /* size of the central directory */
    594         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
    595             err=ZIP_ERRNO;
    596 
    597         /* offset of start of central directory with respect to the
    598             starting disk number */
    599         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
    600             err=ZIP_ERRNO;
    601 
    602         /* zipfile global comment length */
    603         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
    604             err=ZIP_ERRNO;
    605 
    606         if ((central_pos<offset_central_dir+size_central_dir) &&
    607             (err==ZIP_OK))
    608             err=ZIP_BADZIPFILE;
    609 
    610         if (err!=ZIP_OK)
    611         {
    612             ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
    613             return NULL;
    614         }
    615 
    616         if (size_comment>0)
    617         {
    618             ziinit.globalcomment = ALLOC(size_comment+1);
    619             if (ziinit.globalcomment)
    620             {
    621                size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
    622                ziinit.globalcomment[size_comment]=0;
    623             }
    624         }
    625 
    626         byte_before_the_zipfile = central_pos -
    627                                 (offset_central_dir+size_central_dir);
    628         ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
    629 
    630         {
    631             uLong size_central_dir_to_read = size_central_dir;
    632             size_t buf_size = SIZEDATA_INDATABLOCK;
    633             void* buf_read = (void*)ALLOC(buf_size);
    634             if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
    635                   offset_central_dir + byte_before_the_zipfile,
    636                   ZLIB_FILEFUNC_SEEK_SET) != 0)
    637                   err=ZIP_ERRNO;
    638 
    639             while ((size_central_dir_to_read>0) && (err==ZIP_OK))
    640             {
    641                 uLong read_this = SIZEDATA_INDATABLOCK;
    642                 if (read_this > size_central_dir_to_read)
    643                     read_this = size_central_dir_to_read;
    644                 if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
    645                     err=ZIP_ERRNO;
    646 
    647                 if (err==ZIP_OK)
    648                     err = add_data_in_datablock(&ziinit.central_dir,buf_read,
    649                                                 (uLong)read_this);
    650                 size_central_dir_to_read-=read_this;
    651             }
    652             TRYFREE(buf_read);
    653         }
    654         ziinit.begin_pos = byte_before_the_zipfile;
    655         ziinit.number_entry = number_entry_CD;
    656 
    657         if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
    658                   offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
    659             err=ZIP_ERRNO;
     895      // Read and Cache Central Directory Records
     896      err = LoadCentralDirectoryRecord(&ziinit);
    660897    }
    661898
     
    681918}
    682919
    683 extern zipFile ZEXPORT zipOpen (pathname, append)
    684     const char *pathname;
    685     int append;
    686 {
    687     return zipOpen2(pathname,append,NULL,NULL);
    688 }
    689 
    690 extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
    691                                          extrafield_local, size_extrafield_local,
    692                                          extrafield_global, size_extrafield_global,
    693                                          comment, method, level, raw,
    694                                          windowBits, memLevel, strategy,
    695                                          password, crcForCrypting)
    696     zipFile file;
    697     const char* filename;
    698     const zip_fileinfo* zipfi;
    699     const void* extrafield_local;
    700     uInt size_extrafield_local;
    701     const void* extrafield_global;
    702     uInt size_extrafield_global;
    703     const char* comment;
    704     int method;
    705     int level;
    706     int raw;
    707     int windowBits;
    708     int memLevel;
    709     int strategy;
    710     const char* password;
    711     uLong crcForCrypting;
    712 {
    713     zip_internal* zi;
     920extern zipFile ZEXPORT zipOpen2 (const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def)
     921{
     922    if (pzlib_filefunc32_def != NULL)
     923    {
     924        zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
     925        fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
     926        return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
     927    }
     928    else
     929        return zipOpen3(pathname, append, globalcomment, NULL);
     930}
     931
     932extern zipFile ZEXPORT zipOpen2_64 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)
     933{
     934    if (pzlib_filefunc_def != NULL)
     935    {
     936        zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
     937        zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
     938        zlib_filefunc64_32_def_fill.ztell32_file = NULL;
     939        zlib_filefunc64_32_def_fill.zseek32_file = NULL;
     940        return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
     941    }
     942    else
     943        return zipOpen3(pathname, append, globalcomment, NULL);
     944}
     945
     946
     947
     948extern zipFile ZEXPORT zipOpen (const char* pathname, int append)
     949{
     950    return zipOpen3((const void*)pathname,append,NULL,NULL);
     951}
     952
     953extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append)
     954{
     955    return zipOpen3(pathname,append,NULL,NULL);
     956}
     957
     958int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local)
     959{
     960  /* write the local header */
     961  int err;
     962  uInt size_filename = (uInt)strlen(filename);
     963  uInt size_extrafield = size_extrafield_local;
     964
     965  err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4);
     966
     967  if (err==ZIP_OK)
     968  {
     969    if(zi->ci.zip64)
     970      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */
     971    else
     972      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
     973  }
     974
     975  if (err==ZIP_OK)
     976    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
     977
     978  if (err==ZIP_OK)
     979    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
     980
     981  if (err==ZIP_OK)
     982    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
     983
     984  // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later
     985  if (err==ZIP_OK)
     986    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
     987  if (err==ZIP_OK)
     988  {
     989    if(zi->ci.zip64)
     990      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */
     991    else
     992      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
     993  }
     994  if (err==ZIP_OK)
     995  {
     996    if(zi->ci.zip64)
     997      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */
     998    else
     999      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
     1000  }
     1001
     1002  if (err==ZIP_OK)
     1003    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
     1004
     1005  if(zi->ci.zip64)
     1006  {
     1007    size_extrafield += 20;
     1008  }
     1009
     1010  if (err==ZIP_OK)
     1011    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2);
     1012
     1013  if ((err==ZIP_OK) && (size_filename > 0))
     1014  {
     1015    if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
     1016      err = ZIP_ERRNO;
     1017  }
     1018
     1019  if ((err==ZIP_OK) && (size_extrafield_local > 0))
     1020  {
     1021    if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local)
     1022      err = ZIP_ERRNO;
     1023  }
     1024
     1025
     1026  if ((err==ZIP_OK) && (zi->ci.zip64))
     1027  {
     1028      // write the Zip64 extended info
     1029      short HeaderID = 1;
     1030      short DataSize = 16;
     1031      ZPOS64_T CompressedSize = 0;
     1032      ZPOS64_T UncompressedSize = 0;
     1033
     1034      // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file)
     1035      zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
     1036
     1037      err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)HeaderID,2);
     1038      err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)DataSize,2);
     1039
     1040      err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8);
     1041      err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8);
     1042  }
     1043
     1044  return err;
     1045}
     1046
     1047/*
     1048 NOTE.
     1049 When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped
     1050 before calling this function it can be done with zipRemoveExtraInfoBlock
     1051
     1052 It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize
     1053 unnecessary allocations.
     1054 */
     1055extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1056                                         const void* extrafield_local, uInt size_extrafield_local,
     1057                                         const void* extrafield_global, uInt size_extrafield_global,
     1058                                         const char* comment, int method, int level, int raw,
     1059                                         int windowBits,int memLevel, int strategy,
     1060                                         const char* password, uLong crcForCrypting,
     1061                                         uLong versionMadeBy, uLong flagBase, int zip64)
     1062{
     1063    zip64_internal* zi;
    7141064    uInt size_filename;
    7151065    uInt size_comment;
     
    7241074    if (file == NULL)
    7251075        return ZIP_PARAMERROR;
     1076
     1077#ifdef HAVE_BZIP2
     1078    if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED))
     1079      return ZIP_PARAMERROR;
     1080#else
    7261081    if ((method!=0) && (method!=Z_DEFLATED))
    727         return ZIP_PARAMERROR;
    728 
    729     zi = (zip_internal*)file;
     1082      return ZIP_PARAMERROR;
     1083#endif
     1084
     1085    zi = (zip64_internal*)file;
    7301086
    7311087    if (zi->in_opened_file_inzip == 1)
     
    7361092    }
    7371093
    738 
    7391094    if (filename==NULL)
    7401095        filename="-";
     
    7531108        if (zipfi->dosDate != 0)
    7541109            zi->ci.dosDate = zipfi->dosDate;
    755         else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
    756     }
    757 
    758     zi->ci.flag = 0;
     1110        else
     1111          zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
     1112    }
     1113
     1114    zi->ci.flag = flagBase;
    7591115    if ((level==8) || (level==9))
    7601116      zi->ci.flag |= 2;
     
    7721128    zi->ci.pos_in_buffered_data = 0;
    7731129    zi->ci.raw = raw;
    774     zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
    775     zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
    776                                       size_extrafield_global + size_comment;
    777     zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
    778 
    779     ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
     1130    zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
     1131
     1132    zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment;
     1133    zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data
     1134
     1135    zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree);
     1136
     1137    zi->ci.size_centralExtra = size_extrafield_global;
     1138    zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
    7801139    /* version info */
    781     ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
    782     ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
    783     ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
    784     ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
    785     ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
    786     ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
    787     ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
    788     ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
    789     ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
    790     ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
    791     ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
    792     ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
     1140    zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
     1141    zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
     1142    zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
     1143    zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
     1144    zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
     1145    zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
     1146    zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
     1147    zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
     1148    zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
     1149    zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
     1150    zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
     1151    zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
    7931152
    7941153    if (zipfi==NULL)
    795         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
    796     else
    797         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
     1154        zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
     1155    else
     1156        zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
    7981157
    7991158    if (zipfi==NULL)
    800         ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
    801     else
    802         ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
    803 
    804     ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
     1159        zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
     1160    else
     1161        zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
     1162
     1163    if(zi->ci.pos_local_header >= 0xffffffff)
     1164      zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
     1165    else
     1166      zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset,4);
    8051167
    8061168    for (i=0;i<size_filename;i++)
     
    8171179        return ZIP_INTERNALERROR;
    8181180
    819     /* write the local header */
    820     err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
    821 
    822     if (err==ZIP_OK)
    823         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
    824     if (err==ZIP_OK)
    825         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
    826 
    827     if (err==ZIP_OK)
    828         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
    829 
    830     if (err==ZIP_OK)
    831         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
    832 
    833     if (err==ZIP_OK)
    834         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
    835     if (err==ZIP_OK)
    836         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
    837     if (err==ZIP_OK)
    838         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
    839 
    840     if (err==ZIP_OK)
    841         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
    842 
    843     if (err==ZIP_OK)
    844         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
    845 
    846     if ((err==ZIP_OK) && (size_filename>0))
    847         if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
    848                 err = ZIP_ERRNO;
    849 
    850     if ((err==ZIP_OK) && (size_extrafield_local>0))
    851         if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
    852                                                                            !=size_extrafield_local)
    853                 err = ZIP_ERRNO;
     1181    zi->ci.zip64 = zip64;
     1182    zi->ci.totalCompressedData = 0;
     1183    zi->ci.totalUncompressedData = 0;
     1184    zi->ci.pos_zip64extrainfo = 0;
     1185
     1186    err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
     1187
     1188#ifdef HAVE_BZIP2
     1189    zi->ci.bstream.avail_in = (uInt)0;
     1190    zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
     1191    zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
     1192    zi->ci.bstream.total_in_hi32 = 0;
     1193    zi->ci.bstream.total_in_lo32 = 0;
     1194    zi->ci.bstream.total_out_hi32 = 0;
     1195    zi->ci.bstream.total_out_lo32 = 0;
     1196#endif
    8541197
    8551198    zi->ci.stream.avail_in = (uInt)0;
     
    8581201    zi->ci.stream.total_in = 0;
    8591202    zi->ci.stream.total_out = 0;
    860 
     1203    zi->ci.stream.data_type = Z_BINARY;
     1204
     1205#ifdef HAVE_BZIP2
     1206    if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
     1207#else
    8611208    if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
    862     {
    863         zi->ci.stream.zalloc = (alloc_func)0;
    864         zi->ci.stream.zfree = (free_func)0;
    865         zi->ci.stream.opaque = (voidpf)0;
    866 
    867         if (windowBits>0)
    868             windowBits = -windowBits;
    869 
    870         err = deflateInit2(&zi->ci.stream, level,
    871                Z_DEFLATED, windowBits, memLevel, strategy);
    872 
    873         if (err==Z_OK)
    874             zi->ci.stream_initialised = 1;
    875     }
     1209#endif
     1210    {
     1211        if(zi->ci.method == Z_DEFLATED)
     1212        {
     1213          zi->ci.stream.zalloc = (alloc_func)0;
     1214          zi->ci.stream.zfree = (free_func)0;
     1215          zi->ci.stream.opaque = (voidpf)0;
     1216
     1217          if (windowBits>0)
     1218              windowBits = -windowBits;
     1219
     1220          err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
     1221
     1222          if (err==Z_OK)
     1223              zi->ci.stream_initialised = Z_DEFLATED;
     1224        }
     1225        else if(zi->ci.method == Z_BZIP2ED)
     1226        {
     1227#ifdef HAVE_BZIP2
     1228            // Init BZip stuff here
     1229          zi->ci.bstream.bzalloc = 0;
     1230          zi->ci.bstream.bzfree = 0;
     1231          zi->ci.bstream.opaque = (voidpf)0;
     1232
     1233          err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35);
     1234          if(err == BZ_OK)
     1235            zi->ci.stream_initialised = Z_BZIP2ED;
     1236#endif
     1237        }
     1238
     1239    }
     1240
    8761241#    ifndef NOCRYPT
    8771242    zi->ci.crypt_header_size = 0;
     
    8871252        zi->ci.crypt_header_size = sizeHead;
    8881253
    889         if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
     1254        if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
    8901255                err = ZIP_ERRNO;
    8911256    }
     
    8971262}
    8981263
    899 extern int ZEXPORT zipOpenNewFileInZip2(file, filename, zipfi,
    900                                         extrafield_local, size_extrafield_local,
    901                                         extrafield_global, size_extrafield_global,
    902                                         comment, method, level, raw)
    903     zipFile file;
    904     const char* filename;
    905     const zip_fileinfo* zipfi;
    906     const void* extrafield_local;
    907     uInt size_extrafield_local;
    908     const void* extrafield_global;
    909     uInt size_extrafield_global;
    910     const char* comment;
    911     int method;
    912     int level;
    913     int raw;
    914 {
    915     return zipOpenNewFileInZip3 (file, filename, zipfi,
     1264extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1265                                         const void* extrafield_local, uInt size_extrafield_local,
     1266                                         const void* extrafield_global, uInt size_extrafield_global,
     1267                                         const char* comment, int method, int level, int raw,
     1268                                         int windowBits,int memLevel, int strategy,
     1269                                         const char* password, uLong crcForCrypting,
     1270                                         uLong versionMadeBy, uLong flagBase)
     1271{
     1272    return zipOpenNewFileInZip4_64 (file, filename, zipfi,
     1273                                 extrafield_local, size_extrafield_local,
     1274                                 extrafield_global, size_extrafield_global,
     1275                                 comment, method, level, raw,
     1276                                 windowBits, memLevel, strategy,
     1277                                 password, crcForCrypting, versionMadeBy, flagBase, 0);
     1278}
     1279
     1280extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1281                                         const void* extrafield_local, uInt size_extrafield_local,
     1282                                         const void* extrafield_global, uInt size_extrafield_global,
     1283                                         const char* comment, int method, int level, int raw,
     1284                                         int windowBits,int memLevel, int strategy,
     1285                                         const char* password, uLong crcForCrypting)
     1286{
     1287    return zipOpenNewFileInZip4_64 (file, filename, zipfi,
     1288                                 extrafield_local, size_extrafield_local,
     1289                                 extrafield_global, size_extrafield_global,
     1290                                 comment, method, level, raw,
     1291                                 windowBits, memLevel, strategy,
     1292                                 password, crcForCrypting, VERSIONMADEBY, 0, 0);
     1293}
     1294
     1295extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1296                                         const void* extrafield_local, uInt size_extrafield_local,
     1297                                         const void* extrafield_global, uInt size_extrafield_global,
     1298                                         const char* comment, int method, int level, int raw,
     1299                                         int windowBits,int memLevel, int strategy,
     1300                                         const char* password, uLong crcForCrypting, int zip64)
     1301{
     1302    return zipOpenNewFileInZip4_64 (file, filename, zipfi,
     1303                                 extrafield_local, size_extrafield_local,
     1304                                 extrafield_global, size_extrafield_global,
     1305                                 comment, method, level, raw,
     1306                                 windowBits, memLevel, strategy,
     1307                                 password, crcForCrypting, VERSIONMADEBY, 0, zip64);
     1308}
     1309
     1310extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1311                                        const void* extrafield_local, uInt size_extrafield_local,
     1312                                        const void* extrafield_global, uInt size_extrafield_global,
     1313                                        const char* comment, int method, int level, int raw)
     1314{
     1315    return zipOpenNewFileInZip4_64 (file, filename, zipfi,
    9161316                                 extrafield_local, size_extrafield_local,
    9171317                                 extrafield_global, size_extrafield_global,
    9181318                                 comment, method, level, raw,
    9191319                                 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
    920                                  NULL, 0);
    921 }
    922 
    923 extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
    924                                         extrafield_local, size_extrafield_local,
    925                                         extrafield_global, size_extrafield_global,
    926                                         comment, method, level)
    927     zipFile file;
    928     const char* filename;
    929     const zip_fileinfo* zipfi;
    930     const void* extrafield_local;
    931     uInt size_extrafield_local;
    932     const void* extrafield_global;
    933     uInt size_extrafield_global;
    934     const char* comment;
    935     int method;
    936     int level;
    937 {
    938     return zipOpenNewFileInZip2 (file, filename, zipfi,
     1320                                 NULL, 0, VERSIONMADEBY, 0, 0);
     1321}
     1322
     1323extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1324                                        const void* extrafield_local, uInt size_extrafield_local,
     1325                                        const void* extrafield_global, uInt size_extrafield_global,
     1326                                        const char* comment, int method, int level, int raw, int zip64)
     1327{
     1328    return zipOpenNewFileInZip4_64 (file, filename, zipfi,
    9391329                                 extrafield_local, size_extrafield_local,
    9401330                                 extrafield_global, size_extrafield_global,
    941                                  comment, method, level, 0);
    942 }
    943 
    944 local int zipFlushWriteBuffer(zi)
    945   zip_internal* zi;
     1331                                 comment, method, level, raw,
     1332                                 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
     1333                                 NULL, 0, VERSIONMADEBY, 0, zip64);
     1334}
     1335
     1336extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1337                                        const void* extrafield_local, uInt size_extrafield_local,
     1338                                        const void*extrafield_global, uInt size_extrafield_global,
     1339                                        const char* comment, int method, int level, int zip64)
     1340{
     1341    return zipOpenNewFileInZip4_64 (file, filename, zipfi,
     1342                                 extrafield_local, size_extrafield_local,
     1343                                 extrafield_global, size_extrafield_global,
     1344                                 comment, method, level, 0,
     1345                                 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
     1346                                 NULL, 0, VERSIONMADEBY, 0, zip64);
     1347}
     1348
     1349extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi,
     1350                                        const void* extrafield_local, uInt size_extrafield_local,
     1351                                        const void*extrafield_global, uInt size_extrafield_global,
     1352                                        const char* comment, int method, int level)
     1353{
     1354    return zipOpenNewFileInZip4_64 (file, filename, zipfi,
     1355                                 extrafield_local, size_extrafield_local,
     1356                                 extrafield_global, size_extrafield_global,
     1357                                 comment, method, level, 0,
     1358                                 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
     1359                                 NULL, 0, VERSIONMADEBY, 0, 0);
     1360}
     1361
     1362local int zip64FlushWriteBuffer(zip64_internal* zi)
    9461363{
    9471364    int err=ZIP_OK;
     
    9531370        int t;
    9541371        for (i=0;i<zi->ci.pos_in_buffered_data;i++)
    955             zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
    956                                        zi->ci.buffered_data[i],t);
    957 #endif
    958     }
    959     if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
    960                                                                     !=zi->ci.pos_in_buffered_data)
     1372            zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t);
     1373#endif
     1374    }
     1375
     1376    if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data)
    9611377      err = ZIP_ERRNO;
     1378
     1379    zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data;
     1380
     1381#ifdef HAVE_BZIP2
     1382    if(zi->ci.method == Z_BZIP2ED)
     1383    {
     1384      zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32;
     1385      zi->ci.bstream.total_in_lo32 = 0;
     1386      zi->ci.bstream.total_in_hi32 = 0;
     1387    }
     1388    else
     1389#endif
     1390    {
     1391      zi->ci.totalUncompressedData += zi->ci.stream.total_in;
     1392      zi->ci.stream.total_in = 0;
     1393    }
     1394
     1395
    9621396    zi->ci.pos_in_buffered_data = 0;
     1397
    9631398    return err;
    9641399}
    9651400
    966 extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
    967     zipFile file;
    968     const void* buf;
    969     unsigned len;
    970 {
    971     zip_internal* zi;
     1401extern int ZEXPORT zipWriteInFileInZip (zipFile file,const void* buf,unsigned int len)
     1402{
     1403    zip64_internal* zi;
    9721404    int err=ZIP_OK;
    9731405
    9741406    if (file == NULL)
    9751407        return ZIP_PARAMERROR;
    976     zi = (zip_internal*)file;
     1408    zi = (zip64_internal*)file;
    9771409
    9781410    if (zi->in_opened_file_inzip == 0)
    9791411        return ZIP_PARAMERROR;
    9801412
    981     zi->ci.stream.next_in = (void*)buf;
    982     zi->ci.stream.avail_in = len;
    983     zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
    984 
    985     while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
    986     {
    987         if (zi->ci.stream.avail_out == 0)
     1413    zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len);
     1414
     1415#ifdef HAVE_BZIP2
     1416    if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw))
     1417    {
     1418      zi->ci.bstream.next_in = (void*)buf;
     1419      zi->ci.bstream.avail_in = len;
     1420      err = BZ_RUN_OK;
     1421
     1422      while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0))
     1423      {
     1424        if (zi->ci.bstream.avail_out == 0)
    9881425        {
    989             if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
    990                 err = ZIP_ERRNO;
    991             zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
    992             zi->ci.stream.next_out = zi->ci.buffered_data;
     1426          if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
     1427            err = ZIP_ERRNO;
     1428          zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
     1429          zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
    9931430        }
    9941431
    9951432
    996         if(err != ZIP_OK)
    997             break;
    998 
    999         if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
     1433        if(err != BZ_RUN_OK)
     1434          break;
     1435
     1436        if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
    10001437        {
    1001             uLong uTotalOutBefore = zi->ci.stream.total_out;
    1002             err=deflate(&zi->ci.stream,  Z_NO_FLUSH);
    1003             zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
    1004 
     1438          uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32;
     1439//          uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32;
     1440          err=BZ2_bzCompress(&zi->ci.bstream,  BZ_RUN);
     1441
     1442          zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ;
     1443        }
     1444      }
     1445
     1446      if(err == BZ_RUN_OK)
     1447        err = ZIP_OK;
     1448    }
     1449    else
     1450#endif
     1451    {
     1452      zi->ci.stream.next_in = (Bytef*)buf;
     1453      zi->ci.stream.avail_in = len;
     1454
     1455      while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
     1456      {
     1457          if (zi->ci.stream.avail_out == 0)
     1458          {
     1459              if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
     1460                  err = ZIP_ERRNO;
     1461              zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
     1462              zi->ci.stream.next_out = zi->ci.buffered_data;
     1463          }
     1464
     1465
     1466          if(err != ZIP_OK)
     1467              break;
     1468
     1469          if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
     1470          {
     1471              uLong uTotalOutBefore = zi->ci.stream.total_out;
     1472              err=deflate(&zi->ci.stream,  Z_NO_FLUSH);
     1473              if(uTotalOutBefore > zi->ci.stream.total_out)
     1474              {
     1475                int bBreak = 0;
     1476                bBreak++;
     1477              }
     1478
     1479              zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
     1480          }
     1481          else
     1482          {
     1483              uInt copy_this,i;
     1484              if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
     1485                  copy_this = zi->ci.stream.avail_in;
     1486              else
     1487                  copy_this = zi->ci.stream.avail_out;
     1488
     1489              for (i = 0; i < copy_this; i++)
     1490                  *(((char*)zi->ci.stream.next_out)+i) =
     1491                      *(((const char*)zi->ci.stream.next_in)+i);
     1492              {
     1493                  zi->ci.stream.avail_in -= copy_this;
     1494                  zi->ci.stream.avail_out-= copy_this;
     1495                  zi->ci.stream.next_in+= copy_this;
     1496                  zi->ci.stream.next_out+= copy_this;
     1497                  zi->ci.stream.total_in+= copy_this;
     1498                  zi->ci.stream.total_out+= copy_this;
     1499                  zi->ci.pos_in_buffered_data += copy_this;
     1500              }
     1501          }
     1502      }// while(...)
     1503    }
     1504
     1505    return err;
     1506}
     1507
     1508extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32)
     1509{
     1510    return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32);
     1511}
     1512
     1513extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32)
     1514{
     1515    zip64_internal* zi;
     1516    ZPOS64_T compressed_size;
     1517    uLong invalidValue = 0xffffffff;
     1518    short datasize = 0;
     1519    int err=ZIP_OK;
     1520
     1521    if (file == NULL)
     1522        return ZIP_PARAMERROR;
     1523    zi = (zip64_internal*)file;
     1524
     1525    if (zi->in_opened_file_inzip == 0)
     1526        return ZIP_PARAMERROR;
     1527    zi->ci.stream.avail_in = 0;
     1528
     1529    if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
     1530                {
     1531                        while (err==ZIP_OK)
     1532                        {
     1533                                uLong uTotalOutBefore;
     1534                                if (zi->ci.stream.avail_out == 0)
     1535                                {
     1536                                        if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
     1537                                                err = ZIP_ERRNO;
     1538                                        zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
     1539                                        zi->ci.stream.next_out = zi->ci.buffered_data;
     1540                                }
     1541                                uTotalOutBefore = zi->ci.stream.total_out;
     1542                                err=deflate(&zi->ci.stream,  Z_FINISH);
     1543                                zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
     1544                        }
     1545                }
     1546    else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
     1547    {
     1548#ifdef HAVE_BZIP2
     1549      err = BZ_FINISH_OK;
     1550      while (err==BZ_FINISH_OK)
     1551      {
     1552        uLong uTotalOutBefore;
     1553        if (zi->ci.bstream.avail_out == 0)
     1554        {
     1555          if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
     1556            err = ZIP_ERRNO;
     1557          zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
     1558          zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
     1559        }
     1560        uTotalOutBefore = zi->ci.bstream.total_out_lo32;
     1561        err=BZ2_bzCompress(&zi->ci.bstream,  BZ_FINISH);
     1562        if(err == BZ_STREAM_END)
     1563          err = Z_STREAM_END;
     1564
     1565        zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore);
     1566      }
     1567
     1568      if(err == BZ_FINISH_OK)
     1569        err = ZIP_OK;
     1570#endif
     1571    }
     1572
     1573    if (err==Z_STREAM_END)
     1574        err=ZIP_OK; /* this is normal */
     1575
     1576    if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
     1577                {
     1578        if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
     1579            err = ZIP_ERRNO;
     1580                }
     1581
     1582    if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
     1583    {
     1584        int tmp_err = deflateEnd(&zi->ci.stream);
     1585        if (err == ZIP_OK)
     1586            err = tmp_err;
     1587        zi->ci.stream_initialised = 0;
     1588    }
     1589#ifdef HAVE_BZIP2
     1590    else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
     1591    {
     1592      int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
     1593                        if (err==ZIP_OK)
     1594                                err = tmperr;
     1595                        zi->ci.stream_initialised = 0;
     1596    }
     1597#endif
     1598
     1599    if (!zi->ci.raw)
     1600    {
     1601        crc32 = (uLong)zi->ci.crc32;
     1602        uncompressed_size = zi->ci.totalUncompressedData;
     1603    }
     1604    compressed_size = zi->ci.totalCompressedData;
     1605
     1606#    ifndef NOCRYPT
     1607    compressed_size += zi->ci.crypt_header_size;
     1608#    endif
     1609
     1610    // update Current Item crc and sizes,
     1611    if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff)
     1612    {
     1613      /*version Made by*/
     1614      zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2);
     1615      /*version needed*/
     1616      zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2);
     1617
     1618    }
     1619
     1620    zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
     1621
     1622
     1623    if(compressed_size >= 0xffffffff)
     1624      zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/
     1625    else
     1626      zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/
     1627
     1628    /// set internal file attributes field
     1629    if (zi->ci.stream.data_type == Z_ASCII)
     1630        zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
     1631
     1632    if(uncompressed_size >= 0xffffffff)
     1633      zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/
     1634    else
     1635      zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/
     1636
     1637    // Add ZIP64 extra info field for uncompressed size
     1638    if(uncompressed_size >= 0xffffffff)
     1639      datasize += 8;
     1640
     1641    // Add ZIP64 extra info field for compressed size
     1642    if(compressed_size >= 0xffffffff)
     1643      datasize += 8;
     1644
     1645    // Add ZIP64 extra info field for relative offset to local file header of current file
     1646    if(zi->ci.pos_local_header >= 0xffffffff)
     1647      datasize += 8;
     1648
     1649    if(datasize > 0)
     1650    {
     1651      char* p = NULL;
     1652
     1653      if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree)
     1654      {
     1655        // we can not write more data to the buffer that we have room for.
     1656        return ZIP_BADZIPFILE;
     1657      }
     1658
     1659      p = zi->ci.central_header + zi->ci.size_centralheader;
     1660
     1661      // Add Extra Information Header for 'ZIP64 information'
     1662      zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID
     1663      p += 2;
     1664      zip64local_putValue_inmemory(p, datasize, 2); // DataSize
     1665      p += 2;
     1666
     1667      if(uncompressed_size >= 0xffffffff)
     1668      {
     1669        zip64local_putValue_inmemory(p, uncompressed_size, 8);
     1670        p += 8;
     1671      }
     1672
     1673      if(compressed_size >= 0xffffffff)
     1674      {
     1675        zip64local_putValue_inmemory(p, compressed_size, 8);
     1676        p += 8;
     1677      }
     1678
     1679      if(zi->ci.pos_local_header >= 0xffffffff)
     1680      {
     1681        zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
     1682        p += 8;
     1683      }
     1684
     1685      // Update how much extra free space we got in the memory buffer
     1686      // and increase the centralheader size so the new ZIP64 fields are included
     1687      // ( 4 below is the size of HeaderID and DataSize field )
     1688      zi->ci.size_centralExtraFree -= datasize + 4;
     1689      zi->ci.size_centralheader += datasize + 4;
     1690
     1691      // Update the extra info size field
     1692      zi->ci.size_centralExtra += datasize + 4;
     1693      zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
     1694    }
     1695
     1696    if (err==ZIP_OK)
     1697        err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);
     1698
     1699    free(zi->ci.central_header);
     1700
     1701    if (err==ZIP_OK)
     1702    {
     1703        // Update the LocalFileHeader with the new values.
     1704
     1705        ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
     1706
     1707        if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
     1708            err = ZIP_ERRNO;
     1709
     1710        if (err==ZIP_OK)
     1711            err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
     1712
     1713        if(uncompressed_size >= 0xffffffff)
     1714        {
     1715          if(zi->ci.pos_zip64extrainfo > 0)
     1716          {
     1717            // Update the size in the ZIP64 extended field.
     1718            if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
     1719              err = ZIP_ERRNO;
     1720
     1721            if (err==ZIP_OK) /* compressed size, unknown */
     1722              err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);
     1723
     1724            if (err==ZIP_OK) /* uncompressed size, unknown */
     1725              err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8);
     1726          }
    10051727        }
    10061728        else
    10071729        {
    1008             uInt copy_this,i;
    1009             if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
    1010                 copy_this = zi->ci.stream.avail_in;
    1011             else
    1012                 copy_this = zi->ci.stream.avail_out;
    1013             for (i=0;i<copy_this;i++)
    1014                 *(((char*)zi->ci.stream.next_out)+i) =
    1015                     *(((const char*)zi->ci.stream.next_in)+i);
    1016             {
    1017                 zi->ci.stream.avail_in -= copy_this;
    1018                 zi->ci.stream.avail_out-= copy_this;
    1019                 zi->ci.stream.next_in+= copy_this;
    1020                 zi->ci.stream.next_out+= copy_this;
    1021                 zi->ci.stream.total_in+= copy_this;
    1022                 zi->ci.stream.total_out+= copy_this;
    1023                 zi->ci.pos_in_buffered_data += copy_this;
    1024             }
     1730          if (err==ZIP_OK) /* compressed size, unknown */
     1731              err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
     1732
     1733          if (err==ZIP_OK) /* uncompressed size, unknown */
     1734              err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
    10251735        }
    1026     }
     1736
     1737        if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
     1738            err = ZIP_ERRNO;
     1739    }
     1740
     1741    zi->number_entry ++;
     1742    zi->in_opened_file_inzip = 0;
    10271743
    10281744    return err;
    10291745}
    10301746
    1031 extern int ZEXPORT zipCloseFileInZipRaw (file, uncompressed_size, crc32)
    1032     zipFile file;
    1033     uLong uncompressed_size;
    1034     uLong crc32;
    1035 {
    1036     zip_internal* zi;
    1037     uLong compressed_size;
    1038     int err=ZIP_OK;
     1747extern int ZEXPORT zipCloseFileInZip (zipFile file)
     1748{
     1749    return zipCloseFileInZipRaw (file,0,0);
     1750}
     1751
     1752int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip)
     1753{
     1754  int err = ZIP_OK;
     1755  ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset;
     1756
     1757  err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
     1758
     1759  /*num disks*/
     1760    if (err==ZIP_OK) /* number of the disk with the start of the central directory */
     1761      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
     1762
     1763  /*relative offset*/
     1764    if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */
     1765      err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8);
     1766
     1767  /*total disks*/ /* Do not support spawning of disk so always say 1 here*/
     1768    if (err==ZIP_OK) /* number of the disk with the start of the central directory */
     1769      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4);
     1770
     1771    return err;
     1772}
     1773
     1774int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
     1775{
     1776  int err = ZIP_OK;
     1777
     1778  uLong Zip64DataSize = 44;
     1779
     1780  err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
     1781
     1782  if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */
     1783    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ?
     1784
     1785  if (err==ZIP_OK) /* version made by */
     1786    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
     1787
     1788  if (err==ZIP_OK) /* version needed */
     1789    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
     1790
     1791  if (err==ZIP_OK) /* number of this disk */
     1792    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
     1793
     1794  if (err==ZIP_OK) /* number of the disk with the start of the central directory */
     1795    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
     1796
     1797  if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
     1798    err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
     1799
     1800  if (err==ZIP_OK) /* total number of entries in the central dir */
     1801    err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
     1802
     1803  if (err==ZIP_OK) /* size of the central directory */
     1804    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8);
     1805
     1806  if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
     1807  {
     1808    ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
     1809    err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8);
     1810  }
     1811  return err;
     1812}
     1813int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
     1814{
     1815  int err = ZIP_OK;
     1816
     1817  /*signature*/
     1818  err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
     1819
     1820  if (err==ZIP_OK) /* number of this disk */
     1821    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
     1822
     1823  if (err==ZIP_OK) /* number of the disk with the start of the central directory */
     1824    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
     1825
     1826  if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
     1827  {
     1828    {
     1829      if(zi->number_entry >= 0xFFFF)
     1830        err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
     1831      else
     1832        err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
     1833    }
     1834  }
     1835
     1836  if (err==ZIP_OK) /* total number of entries in the central dir */
     1837  {
     1838    if(zi->number_entry >= 0xFFFF)
     1839      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
     1840    else
     1841      err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
     1842  }
     1843
     1844  if (err==ZIP_OK) /* size of the central directory */
     1845    err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
     1846
     1847  if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
     1848  {
     1849    ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
     1850    if(pos >= 0xffffffff)
     1851    {
     1852      err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4);
     1853    }
     1854    else
     1855                  err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
     1856  }
     1857
     1858   return err;
     1859}
     1860
     1861int Write_GlobalComment(zip64_internal* zi, const char* global_comment)
     1862{
     1863  int err = ZIP_OK;
     1864  uInt size_global_comment = 0;
     1865
     1866  if(global_comment != NULL)
     1867    size_global_comment = (uInt)strlen(global_comment);
     1868
     1869  err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
     1870
     1871  if (err == ZIP_OK && size_global_comment > 0)
     1872  {
     1873    if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment)
     1874      err = ZIP_ERRNO;
     1875  }
     1876  return err;
     1877}
     1878
     1879extern int ZEXPORT zipClose (zipFile file, const char* global_comment)
     1880{
     1881    zip64_internal* zi;
     1882    int err = 0;
     1883    uLong size_centraldir = 0;
     1884    ZPOS64_T centraldir_pos_inzip;
     1885    ZPOS64_T pos;
    10391886
    10401887    if (file == NULL)
    10411888        return ZIP_PARAMERROR;
    1042     zi = (zip_internal*)file;
    1043 
    1044     if (zi->in_opened_file_inzip == 0)
    1045         return ZIP_PARAMERROR;
    1046     zi->ci.stream.avail_in = 0;
    1047 
    1048     if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
    1049         while (err==ZIP_OK)
    1050     {
    1051         uLong uTotalOutBefore;
    1052         if (zi->ci.stream.avail_out == 0)
    1053         {
    1054             if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
    1055                 err = ZIP_ERRNO;
    1056 &