libxlsxwriter/third_party/minizip/unzip.c in fast_excel-0.4.1 vs libxlsxwriter/third_party/minizip/unzip.c in fast_excel-0.5.0
- old
+ new
@@ -47,16 +47,16 @@
2007-2008 - Even Rouault - Add unzip support for ZIP64
Copyright (C) 2007-2008 Even Rouault
- 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).
+ 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).
Oct-2009 - Mathias Svensson - Fixed problem if uncompressed size was > 4G and compressed size was <4G
should only read the compressed/uncompressed size from the Zip64 format if
the size from normal header was 0xFFFFFFFF
- Oct-2009 - Mathias Svensson - Applied some bug fixes from paches recived from Gilles Vollant
- Oct-2009 - Mathias Svensson - Applied support to unzip files with compression mathod BZIP2 (bzip2 lib is required)
+ Oct-2009 - Mathias Svensson - Applied some bug fixes from patches received from Gilles Vollant
+ Oct-2009 - Mathias Svensson - Applied support to unzip files with compression method BZIP2 (bzip2 lib is required)
Patch created by Daniel Borca
Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson
@@ -75,12 +75,10 @@
#include "zlib.h"
#include "unzip.h"
#ifdef STDC
# include <stddef.h>
-# include <string.h>
-# include <stdlib.h>
#endif
#ifdef NO_ERRNO_H
extern int errno;
#else
# include <errno.h>
@@ -109,13 +107,10 @@
#endif
#ifndef ALLOC
# define ALLOC(size) (malloc(size))
#endif
-#ifndef TRYFREE
-# define TRYFREE(p) {if (p) free(p);}
-#endif
#define SIZECENTRALDIRITEM (0x2e)
#define SIZEZIPLOCALHEADER (0x1e)
@@ -151,11 +146,11 @@
uLong crc32; /* crc32 of all data uncompressed */
uLong crc32_wait; /* crc32 we must obtain after decompress all */
ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/
zlib_filefunc64_32_def z_filefunc;
- voidpf filestream; /* io structore of the zipfile */
+ voidpf filestream; /* io structure of the zipfile */
uLong compression_method; /* compression method (0==store) */
ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
int raw;
} file_in_zip64_read_info_s;
@@ -164,11 +159,11 @@
*/
typedef struct
{
zlib_filefunc64_32_def z_filefunc;
int is64bitOpenFunction;
- voidpf filestream; /* io structore of the zipfile */
+ voidpf filestream; /* io structure of the zipfile */
unz_global_info64 gi; /* public global information */
ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
ZPOS64_T num_file; /* number of the current file in the zipfile*/
ZPOS64_T pos_in_central_dir; /* pos of the current file in the central dir*/
ZPOS64_T current_file_ok; /* flag about the usability of the current file*/
@@ -195,161 +190,79 @@
#ifndef NOUNCRYPT
#include "crypt.h"
#endif
+
/* ===========================================================================
- Read a byte from a gz_stream; update next_in and avail_in. Return EOF
- for end of file.
- IN assertion: the stream s has been sucessfully opened for reading.
+ Reads a long in LSB order from the given gz_stream. Sets
*/
-
-local int unz64local_getByte OF((
- const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream,
- int *pi));
-
-local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)
-{
- unsigned char c;
- int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
- if (err==1)
+local int unz64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def,
+ voidpf filestream,
+ uLong *pX) {
+ unsigned char c[2];
+ int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,2);
+ if (err==2)
{
- *pi = (int)c;
+ *pX = c[0] | ((uLong)c[1] << 8);
return UNZ_OK;
}
else
{
+ *pX = 0;
if (ZERROR64(*pzlib_filefunc_def,filestream))
return UNZ_ERRNO;
else
return UNZ_EOF;
}
}
-
-/* ===========================================================================
- Reads a long in LSB order from the given gz_stream. Sets
-*/
-local int unz64local_getShort OF((
- const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream,
- uLong *pX));
-
-local int unz64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def,
+local int unz64local_getLong(const zlib_filefunc64_32_def* pzlib_filefunc_def,
voidpf filestream,
- uLong *pX)
-{
- uLong x ;
- int i = 0;
- int err;
-
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x = (uLong)i;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((uLong)i)<<8;
-
- if (err==UNZ_OK)
- *pX = x;
+ uLong *pX) {
+ unsigned char c[4];
+ int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,4);
+ if (err==4)
+ {
+ *pX = c[0] | ((uLong)c[1] << 8) | ((uLong)c[2] << 16) | ((uLong)c[3] << 24);
+ return UNZ_OK;
+ }
else
+ {
*pX = 0;
- return err;
+ if (ZERROR64(*pzlib_filefunc_def,filestream))
+ return UNZ_ERRNO;
+ else
+ return UNZ_EOF;
+ }
}
-local int unz64local_getLong OF((
- const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream,
- uLong *pX));
-local int unz64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream,
- uLong *pX)
-{
- uLong x ;
- int i = 0;
- int err;
-
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x = (uLong)i;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((uLong)i)<<8;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((uLong)i)<<16;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x += ((uLong)i)<<24;
-
- if (err==UNZ_OK)
- *pX = x;
+local int unz64local_getLong64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
+ voidpf filestream,
+ ZPOS64_T *pX) {
+ unsigned char c[8];
+ int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,8);
+ if (err==8)
+ {
+ *pX = c[0] | ((ZPOS64_T)c[1] << 8) | ((ZPOS64_T)c[2] << 16) | ((ZPOS64_T)c[3] << 24)
+ | ((ZPOS64_T)c[4] << 32) | ((ZPOS64_T)c[5] << 40) | ((ZPOS64_T)c[6] << 48) | ((ZPOS64_T)c[7] << 56);
+ return UNZ_OK;
+ }
else
+ {
*pX = 0;
- return err;
+ if (ZERROR64(*pzlib_filefunc_def,filestream))
+ return UNZ_ERRNO;
+ else
+ return UNZ_EOF;
+ }
}
-local int unz64local_getLong64 OF((
- const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream,
- ZPOS64_T *pX));
-
-
-local int unz64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream,
- ZPOS64_T *pX)
-{
- ZPOS64_T x ;
- int i = 0;
- int err;
-
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x = (ZPOS64_T)i;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((ZPOS64_T)i)<<8;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((ZPOS64_T)i)<<16;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((ZPOS64_T)i)<<24;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((ZPOS64_T)i)<<32;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((ZPOS64_T)i)<<40;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((ZPOS64_T)i)<<48;
-
- if (err==UNZ_OK)
- err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
- x |= ((ZPOS64_T)i)<<56;
-
- if (err==UNZ_OK)
- *pX = x;
- else
- *pX = 0;
- return err;
-}
-
/* My own strcmpi / strcasecmp */
-local int strcmpcasenosensitive_internal (const char* fileName1, const char* fileName2)
-{
+local int strcmpcasenosensitive_internal(const char* fileName1, const char* fileName2) {
for (;;)
{
char c1=*(fileName1++);
char c2=*(fileName2++);
if ((c1>='a') && (c1<='z'))
@@ -377,23 +290,21 @@
#ifndef STRCMPCASENOSENTIVEFUNCTION
#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
#endif
/*
- Compare two filename (fileName1,fileName2).
- If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
- If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
+ Compare two filenames (fileName1,fileName2).
+ If iCaseSensitivity = 1, comparison is case sensitive (like strcmp)
+ If iCaseSensitivity = 2, comparison is not case sensitive (like strcmpi
or strcasecmp)
- If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
+ If iCaseSensitivity = 0, case sensitivity is default of your operating system
(like 1 on Unix, 2 on Windows)
*/
extern int ZEXPORT unzStringFileNameCompare (const char* fileName1,
- const char* fileName2,
- int iCaseSensitivity)
-
-{
+ const char* fileName2,
+ int iCaseSensitivity) {
if (iCaseSensitivity==0)
iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
if (iCaseSensitivity==1)
return strcmp(fileName1,fileName2);
@@ -403,35 +314,37 @@
#ifndef BUFREADCOMMENT
#define BUFREADCOMMENT (0x400)
#endif
+#ifndef CENTRALDIRINVALID
+#define CENTRALDIRINVALID ((ZPOS64_T)(-1))
+#endif
+
/*
Locate the Central directory of a zipfile (at the end, just before
the global comment)
*/
-local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
-local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
-{
+local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
unsigned char* buf;
ZPOS64_T uSizeFile;
ZPOS64_T uBackRead;
ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
- ZPOS64_T uPosFound=0;
+ ZPOS64_T uPosFound=CENTRALDIRINVALID;
if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
- return 0;
+ return CENTRALDIRINVALID;
uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
if (uMaxBack>uSizeFile)
uMaxBack = uSizeFile;
buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
if (buf==NULL)
- return 0;
+ return CENTRALDIRINVALID;
uBackRead = 4;
while (uBackRead<uMaxBack)
{
uLong uReadSize;
@@ -453,53 +366,48 @@
for (i=(int)uReadSize-3; (i--)>0;)
if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
{
- uPosFound = uReadPos+i;
+ uPosFound = uReadPos+(unsigned)i;
break;
}
- if (uPosFound!=0)
+ if (uPosFound!=CENTRALDIRINVALID)
break;
}
- TRYFREE(buf);
+ free(buf);
return uPosFound;
}
/*
Locate the Central directory 64 of a zipfile (at the end, just before
the global comment)
*/
-local ZPOS64_T unz64local_SearchCentralDir64 OF((
- const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream));
-
local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
- voidpf filestream)
-{
+ voidpf filestream) {
unsigned char* buf;
ZPOS64_T uSizeFile;
ZPOS64_T uBackRead;
ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
- ZPOS64_T uPosFound=0;
+ ZPOS64_T uPosFound=CENTRALDIRINVALID;
uLong uL;
ZPOS64_T relativeOffset;
if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
- return 0;
+ return CENTRALDIRINVALID;
uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
if (uMaxBack>uSizeFile)
uMaxBack = uSizeFile;
buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
if (buf==NULL)
- return 0;
+ return CENTRALDIRINVALID;
uBackRead = 4;
while (uBackRead<uMaxBack)
{
uLong uReadSize;
@@ -521,55 +429,55 @@
for (i=(int)uReadSize-3; (i--)>0;)
if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
{
- uPosFound = uReadPos+i;
+ uPosFound = uReadPos+(unsigned)i;
break;
}
- if (uPosFound!=0)
+ if (uPosFound!=CENTRALDIRINVALID)
break;
}
- TRYFREE(buf);
- if (uPosFound == 0)
- return 0;
+ free(buf);
+ if (uPosFound == CENTRALDIRINVALID)
+ return CENTRALDIRINVALID;
/* Zip64 end of central directory locator */
if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
- return 0;
+ return CENTRALDIRINVALID;
/* the signature, already checked */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
- return 0;
+ return CENTRALDIRINVALID;
/* number of the disk with the start of the zip64 end of central directory */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
- return 0;
+ return CENTRALDIRINVALID;
if (uL != 0)
- return 0;
+ return CENTRALDIRINVALID;
/* relative offset of the zip64 end of central directory record */
if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK)
- return 0;
+ return CENTRALDIRINVALID;
/* total number of disks */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
- return 0;
+ return CENTRALDIRINVALID;
if (uL != 1)
- return 0;
+ return CENTRALDIRINVALID;
/* Goto end of central directory record */
if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
- return 0;
+ return CENTRALDIRINVALID;
/* the signature */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
- return 0;
+ return CENTRALDIRINVALID;
if (uL != 0x06064b50)
- return 0;
+ return CENTRALDIRINVALID;
return relativeOffset;
}
/*
@@ -579,23 +487,22 @@
If the zipfile cannot be opened (file doesn't exist or in not valid), the
return value is NULL.
Else, the return value is a unzFile Handle, usable with other function
of this unzip package.
*/
-local unzFile unzOpenInternal (const void *path,
- zlib_filefunc64_32_def* pzlib_filefunc64_32_def,
- int is64bitOpenFunction)
-{
+local unzFile unzOpenInternal(const void *path,
+ zlib_filefunc64_32_def* pzlib_filefunc64_32_def,
+ int is64bitOpenFunction) {
unz64_s us;
unz64_s *s;
ZPOS64_T central_pos;
uLong uL;
uLong number_disk; /* number of the current dist, used for
- spaning ZIP, unsupported, always 0*/
+ spanning ZIP, unsupported, always 0*/
uLong number_disk_with_CD; /* number the the disk with central dir, used
- for spaning ZIP, unsupported, always 0*/
+ for spanning ZIP, unsupported, always 0*/
ZPOS64_T number_entry_CD; /* total number of entries in
the central dir
(same than number_entry on nospan) */
int err=UNZ_OK;
@@ -619,11 +526,11 @@
ZLIB_FILEFUNC_MODE_EXISTING);
if (us.filestream==NULL)
return NULL;
central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream);
- if (central_pos)
+ if (central_pos!=CENTRALDIRINVALID)
{
uLong uS;
ZPOS64_T uL64;
us.isZip64 = 1;
@@ -681,11 +588,11 @@
us.gi.size_comment = 0;
}
else
{
central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream);
- if (central_pos==0)
+ if (central_pos==CENTRALDIRINVALID)
err=UNZ_ERRNO;
us.isZip64 = 0;
if (ZSEEK64(us.z_filefunc, us.filestream,
@@ -760,26 +667,24 @@
}
return (unzFile)s;
}
-extern unzFile ZEXPORT unzOpen2 (const char *path,
- zlib_filefunc_def* pzlib_filefunc32_def)
-{
+extern unzFile ZEXPORT unzOpen2(const char *path,
+ zlib_filefunc_def* pzlib_filefunc32_def) {
if (pzlib_filefunc32_def != NULL)
{
zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 0);
}
else
return unzOpenInternal(path, NULL, 0);
}
-extern unzFile ZEXPORT unzOpen2_64 (const void *path,
- zlib_filefunc64_def* pzlib_filefunc_def)
-{
+extern unzFile ZEXPORT unzOpen2_64(const void *path,
+ zlib_filefunc64_def* pzlib_filefunc_def) {
if (pzlib_filefunc_def != NULL)
{
zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
zlib_filefunc64_32_def_fill.ztell32_file = NULL;
@@ -788,107 +693,89 @@
}
else
return unzOpenInternal(path, NULL, 1);
}
-extern unzFile ZEXPORT unzOpen (const char *path)
-{
+extern unzFile ZEXPORT unzOpen(const char *path) {
return unzOpenInternal(path, NULL, 0);
}
-extern unzFile ZEXPORT unzOpen64 (const void *path)
-{
+extern unzFile ZEXPORT unzOpen64(const void *path) {
return unzOpenInternal(path, NULL, 1);
}
/*
Close a ZipFile opened with unzOpen.
If there is files inside the .Zip opened with unzOpenCurrentFile (see later),
these files MUST be closed with unzCloseCurrentFile before call unzClose.
return UNZ_OK if there is no problem. */
-extern int ZEXPORT unzClose (unzFile file)
-{
+extern int ZEXPORT unzClose(unzFile file) {
unz64_s* s;
if (file==NULL)
return UNZ_PARAMERROR;
s=(unz64_s*)file;
if (s->pfile_in_zip_read!=NULL)
unzCloseCurrentFile(file);
ZCLOSE64(s->z_filefunc, s->filestream);
- TRYFREE(s);
+ free(s);
return UNZ_OK;
}
/*
Write info about the ZipFile in the *pglobal_info structure.
No preparation of the structure is needed
return UNZ_OK if there is no problem. */
-extern int ZEXPORT unzGetGlobalInfo64 (unzFile file, unz_global_info64* pglobal_info)
-{
+extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64* pglobal_info) {
unz64_s* s;
if (file==NULL)
return UNZ_PARAMERROR;
s=(unz64_s*)file;
*pglobal_info=s->gi;
return UNZ_OK;
}
-extern int ZEXPORT unzGetGlobalInfo (unzFile file, unz_global_info* pglobal_info32)
-{
+extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32) {
unz64_s* s;
if (file==NULL)
return UNZ_PARAMERROR;
s=(unz64_s*)file;
/* to do : check if number_entry is not truncated */
pglobal_info32->number_entry = (uLong)s->gi.number_entry;
pglobal_info32->size_comment = s->gi.size_comment;
return UNZ_OK;
}
/*
- Translate date/time from Dos format to tm_unz (readable more easilty)
+ Translate date/time from Dos format to tm_unz (readable more easily)
*/
-local void unz64local_DosDateToTmuDate (ZPOS64_T ulDosDate, tm_unz* ptm)
-{
+local void unz64local_DosDateToTmuDate(ZPOS64_T ulDosDate, tm_unz* ptm) {
ZPOS64_T uDate;
uDate = (ZPOS64_T)(ulDosDate>>16);
- ptm->tm_mday = (uInt)(uDate&0x1f) ;
- ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
- ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
+ ptm->tm_mday = (int)(uDate&0x1f) ;
+ ptm->tm_mon = (int)((((uDate)&0x1E0)/0x20)-1) ;
+ ptm->tm_year = (int)(((uDate&0x0FE00)/0x0200)+1980) ;
- ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
- ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
- ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
+ ptm->tm_hour = (int) ((ulDosDate &0xF800)/0x800);
+ ptm->tm_min = (int) ((ulDosDate&0x7E0)/0x20) ;
+ ptm->tm_sec = (int) (2*(ulDosDate&0x1f)) ;
}
/*
Get Info about the current file in the zipfile, with internal only info
*/
-local int unz64local_GetCurrentFileInfoInternal OF((unzFile file,
- unz_file_info64 *pfile_info,
- unz_file_info64_internal
- *pfile_info_internal,
- char *szFileName,
- uLong fileNameBufferSize,
- void *extraField,
- uLong extraFieldBufferSize,
- char *szComment,
- uLong commentBufferSize));
-
-local int unz64local_GetCurrentFileInfoInternal (unzFile file,
- unz_file_info64 *pfile_info,
- unz_file_info64_internal
- *pfile_info_internal,
- char *szFileName,
- uLong fileNameBufferSize,
- void *extraField,
- uLong extraFieldBufferSize,
- char *szComment,
- uLong commentBufferSize)
-{
+local int unz64local_GetCurrentFileInfoInternal(unzFile file,
+ unz_file_info64 *pfile_info,
+ unz_file_info64_internal
+ *pfile_info_internal,
+ char *szFileName,
+ uLong fileNameBufferSize,
+ void *extraField,
+ uLong extraFieldBufferSize,
+ char *szComment,
+ uLong commentBufferSize) {
unz64_s* s;
unz_file_info64 file_info;
unz_file_info64_internal file_info_internal;
int err=UNZ_OK;
uLong uMagic;
@@ -991,11 +878,11 @@
else
uSizeRead = extraFieldBufferSize;
if (lSeek!=0)
{
- if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
+ if (ZSEEK64(s->z_filefunc, s->filestream,(ZPOS64_T)lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
lSeek=0;
else
err=UNZ_ERRNO;
}
@@ -1016,11 +903,11 @@
// since lSeek now points to after the extra field we need to move back
lSeek -= file_info.size_file_extra;
if (lSeek!=0)
{
- if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
+ if (ZSEEK64(s->z_filefunc, s->filestream,(ZPOS64_T)lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
lSeek=0;
else
err=UNZ_ERRNO;
}
@@ -1036,38 +923,36 @@
err=UNZ_ERRNO;
/* ZIP64 extra fields */
if (headerId == 0x0001)
{
- uLong uL;
+ if(file_info.uncompressed_size == MAXU32)
+ {
+ if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
+ err=UNZ_ERRNO;
+ }
- if(file_info.uncompressed_size == MAXU32)
- {
- if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
- err=UNZ_ERRNO;
- }
+ if(file_info.compressed_size == MAXU32)
+ {
+ if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
+ err=UNZ_ERRNO;
+ }
- if(file_info.compressed_size == MAXU32)
- {
- if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
- err=UNZ_ERRNO;
- }
+ if(file_info_internal.offset_curfile == MAXU32)
+ {
+ /* Relative Header offset */
+ if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
+ err=UNZ_ERRNO;
+ }
- if(file_info_internal.offset_curfile == MAXU32)
- {
- /* Relative Header offset */
- if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
- err=UNZ_ERRNO;
- }
+ if(file_info.disk_num_start == 0xffff)
+ {
+ /* Disk Start Number */
+ if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
+ err=UNZ_ERRNO;
+ }
- if(file_info.disk_num_start == MAXU32)
- {
- /* Disk Start Number */
- if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
- err=UNZ_ERRNO;
- }
-
}
else
{
if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0)
err=UNZ_ERRNO;
@@ -1088,11 +973,11 @@
else
uSizeRead = commentBufferSize;
if (lSeek!=0)
{
- if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
+ if (ZSEEK64(s->z_filefunc, s->filestream,(ZPOS64_T)lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
lSeek=0;
else
err=UNZ_ERRNO;
}
@@ -1119,28 +1004,26 @@
/*
Write info about the ZipFile in the *pglobal_info structure.
No preparation of the structure is needed
return UNZ_OK if there is no problem.
*/
-extern int ZEXPORT unzGetCurrentFileInfo64 (unzFile file,
- unz_file_info64 * pfile_info,
- char * szFileName, uLong fileNameBufferSize,
- void *extraField, uLong extraFieldBufferSize,
- char* szComment, uLong commentBufferSize)
-{
+extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file,
+ unz_file_info64 * pfile_info,
+ char * szFileName, uLong fileNameBufferSize,
+ void *extraField, uLong extraFieldBufferSize,
+ char* szComment, uLong commentBufferSize) {
return unz64local_GetCurrentFileInfoInternal(file,pfile_info,NULL,
- szFileName,fileNameBufferSize,
- extraField,extraFieldBufferSize,
- szComment,commentBufferSize);
+ szFileName,fileNameBufferSize,
+ extraField,extraFieldBufferSize,
+ szComment,commentBufferSize);
}
-extern int ZEXPORT unzGetCurrentFileInfo (unzFile file,
- unz_file_info * pfile_info,
- char * szFileName, uLong fileNameBufferSize,
- void *extraField, uLong extraFieldBufferSize,
- char* szComment, uLong commentBufferSize)
-{
+extern int ZEXPORT unzGetCurrentFileInfo(unzFile file,
+ unz_file_info * pfile_info,
+ char * szFileName, uLong fileNameBufferSize,
+ void *extraField, uLong extraFieldBufferSize,
+ char* szComment, uLong commentBufferSize) {
int err;
unz_file_info64 file_info64;
err = unz64local_GetCurrentFileInfoInternal(file,&file_info64,NULL,
szFileName,fileNameBufferSize,
extraField,extraFieldBufferSize,
@@ -1160,11 +1043,11 @@
pfile_info->disk_num_start = file_info64.disk_num_start;
pfile_info->internal_fa = file_info64.internal_fa;
pfile_info->external_fa = file_info64.external_fa;
- pfile_info->tmu_date = file_info64.tmu_date,
+ pfile_info->tmu_date = file_info64.tmu_date;
pfile_info->compressed_size = (uLong)file_info64.compressed_size;
pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size;
@@ -1173,12 +1056,11 @@
}
/*
Set the current file of the zipfile to the first file.
return UNZ_OK if there is no problem
*/
-extern int ZEXPORT unzGoToFirstFile (unzFile file)
-{
+extern int ZEXPORT unzGoToFirstFile(unzFile file) {
int err=UNZ_OK;
unz64_s* s;
if (file==NULL)
return UNZ_PARAMERROR;
s=(unz64_s*)file;
@@ -1194,12 +1076,11 @@
/*
Set the current file of the zipfile to the next file.
return UNZ_OK if there is no problem
return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
*/
-extern int ZEXPORT unzGoToNextFile (unzFile file)
-{
+extern int ZEXPORT unzGoToNextFile(unzFile file) {
unz64_s* s;
int err;
if (file==NULL)
return UNZ_PARAMERROR;
@@ -1227,12 +1108,11 @@
return value :
UNZ_OK if the file is found. It becomes the current file.
UNZ_END_OF_LIST_OF_FILE if the file is not found
*/
-extern int ZEXPORT unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
-{
+extern int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity) {
unz64_s* s;
int err;
/* We remember the 'current' position in the file so that we can jump
* back there if we fail.
@@ -1303,12 +1183,11 @@
ZPOS64_T pos_in_zip_directory; // offset in file
ZPOS64_T num_of_file; // # of file
} unz_file_pos;
*/
-extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos* file_pos)
-{
+extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos* file_pos) {
unz64_s* s;
if (file==NULL || file_pos==NULL)
return UNZ_PARAMERROR;
s=(unz64_s*)file;
@@ -1319,26 +1198,22 @@
file_pos->num_of_file = s->num_file;
return UNZ_OK;
}
-extern int ZEXPORT unzGetFilePos(
- unzFile file,
- unz_file_pos* file_pos)
-{
+extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos* file_pos) {
unz64_file_pos file_pos64;
int err = unzGetFilePos64(file,&file_pos64);
if (err==UNZ_OK)
{
file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory;
file_pos->num_of_file = (uLong)file_pos64.num_of_file;
}
return err;
}
-extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos)
-{
+extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos) {
unz64_s* s;
int err;
if (file==NULL || file_pos==NULL)
return UNZ_PARAMERROR;
@@ -1355,14 +1230,11 @@
/* return results */
s->current_file_ok = (err == UNZ_OK);
return err;
}
-extern int ZEXPORT unzGoToFilePos(
- unzFile file,
- unz_file_pos* file_pos)
-{
+extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos* file_pos) {
unz64_file_pos file_pos64;
if (file_pos == NULL)
return UNZ_PARAMERROR;
file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
@@ -1380,14 +1252,13 @@
Check the coherency of the local header and info in the end of central
directory about this file
store in *piSizeVar the size of extra info in local header
(filename and size of extra field data)
*/
-local int unz64local_CheckCurrentFileCoherencyHeader (unz64_s* s, uInt* piSizeVar,
- ZPOS64_T * poffset_local_extrafield,
- uInt * psize_local_extrafield)
-{
+local int unz64local_CheckCurrentFileCoherencyHeader(unz64_s* s, uInt* piSizeVar,
+ ZPOS64_T * poffset_local_extrafield,
+ uInt * psize_local_extrafield) {
uLong uMagic,uData,uFlags;
uLong size_filename;
uLong size_extra_field;
int err=UNZ_OK;
@@ -1467,13 +1338,12 @@
/*
Open for reading data the current file in the zipfile.
If there is no error and the file is opened, the return value is UNZ_OK.
*/
-extern int ZEXPORT unzOpenCurrentFile3 (unzFile file, int* method,
- int* level, int raw, const char* password)
-{
+extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int* method,
+ int* level, int raw, const char* password) {
int err=UNZ_OK;
uInt iSizeVar;
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
ZPOS64_T offset_local_extrafield; /* offset of the local extra field */
@@ -1507,11 +1377,11 @@
pfile_in_zip_read_info->pos_local_extrafield=0;
pfile_in_zip_read_info->raw=raw;
if (pfile_in_zip_read_info->read_buffer==NULL)
{
- TRYFREE(pfile_in_zip_read_info);
+ free(pfile_in_zip_read_info);
return UNZ_INTERNALERROR;
}
pfile_in_zip_read_info->stream_initialised=0;
@@ -1564,11 +1434,12 @@
err=BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
if (err == Z_OK)
pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED;
else
{
- TRYFREE(pfile_in_zip_read_info);
+ free(pfile_in_zip_read_info->read_buffer);
+ free(pfile_in_zip_read_info);
return err;
}
#else
pfile_in_zip_read_info->raw=1;
#endif
@@ -1584,11 +1455,12 @@
err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
if (err == Z_OK)
pfile_in_zip_read_info->stream_initialised=Z_DEFLATED;
else
{
- TRYFREE(pfile_in_zip_read_info);
+ free(pfile_in_zip_read_info->read_buffer);
+ free(pfile_in_zip_read_info);
return err;
}
/* windowBits is passed < 0 to tell that there is no zlib header.
* Note that in this case inflate *requires* an extra "dummy" byte
* after the compressed stream in order to complete decompression and
@@ -1636,29 +1508,25 @@
return UNZ_OK;
}
-extern int ZEXPORT unzOpenCurrentFile (unzFile file)
-{
+extern int ZEXPORT unzOpenCurrentFile(unzFile file) {
return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
}
-extern int ZEXPORT unzOpenCurrentFilePassword (unzFile file, const char* password)
-{
+extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char* password) {
return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
}
-extern int ZEXPORT unzOpenCurrentFile2 (unzFile file, int* method, int* level, int raw)
-{
+extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int* method, int* level, int raw) {
return unzOpenCurrentFile3(file, method, level, raw, NULL);
}
/** Addition for GDAL : START */
-extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64( unzFile file)
-{
+extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64(unzFile file) {
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
s=(unz64_s*)file;
if (file==NULL)
return 0; //UNZ_PARAMERROR;
@@ -1674,17 +1542,16 @@
/*
Read bytes from the current file.
buf contain buffer where data must be copied
len the size of buf.
- return the number of byte copied if somes bytes are copied
+ return the number of byte copied if some bytes are copied
return 0 if the end of file was reached
return <0 with error code if there is an error
(UNZ_ERRNO for IO error, or zLib error for uncompress error)
*/
-extern int ZEXPORT unzReadCurrentFile (unzFile file, voidp buf, unsigned len)
-{
+extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) {
int err=UNZ_OK;
uInt iRead = 0;
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
if (file==NULL)
@@ -1765,11 +1632,11 @@
{
uInt uDoCopy,i ;
if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
(pfile_in_zip_read_info->rest_read_compressed == 0))
- return (iRead==0) ? UNZ_EOF : iRead;
+ return (iRead==0) ? UNZ_EOF : (int)iRead;
if (pfile_in_zip_read_info->stream.avail_out <
pfile_in_zip_read_info->stream.avail_in)
uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
else
@@ -1855,10 +1722,13 @@
if ((err>=0) && (pfile_in_zip_read_info->stream.msg!=NULL))
err = Z_DATA_ERROR;
uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
+ /* Detect overflow, because z_stream.total_out is uLong (32 bits) */
+ if (uTotalOutAfter<uTotalOutBefore)
+ uTotalOutAfter += 1LL << 32; /* Add maximum value of uLong + 1 */
uOutThis = uTotalOutAfter-uTotalOutBefore;
pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
pfile_in_zip_read_info->crc32 =
@@ -1869,27 +1739,26 @@
uOutThis;
iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
if (err==Z_STREAM_END)
- return (iRead==0) ? UNZ_EOF : iRead;
+ return (iRead==0) ? UNZ_EOF : (int)iRead;
if (err!=Z_OK)
break;
}
}
if (err==Z_OK)
- return iRead;
+ return (int)iRead;
return err;
}
/*
Give the current position in uncompressed data
*/
-extern z_off_t ZEXPORT unztell (unzFile file)
-{
+extern z_off_t ZEXPORT unztell(unzFile file) {
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
if (file==NULL)
return UNZ_PARAMERROR;
s=(unz64_s*)file;
@@ -1899,12 +1768,11 @@
return UNZ_PARAMERROR;
return (z_off_t)pfile_in_zip_read_info->stream.total_out;
}
-extern ZPOS64_T ZEXPORT unztell64 (unzFile file)
-{
+extern ZPOS64_T ZEXPORT unztell64(unzFile file) {
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
if (file==NULL)
return (ZPOS64_T)-1;
@@ -1919,12 +1787,11 @@
/*
return 1 if the end of file was reached, 0 elsewhere
*/
-extern int ZEXPORT unzeof (unzFile file)
-{
+extern int ZEXPORT unzeof(unzFile file) {
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
if (file==NULL)
return UNZ_PARAMERROR;
s=(unz64_s*)file;
@@ -1951,12 +1818,11 @@
if buf!=NULL, len is the size of the buffer, the extra header is copied in
buf.
the return value is the number of bytes copied in buf, or (if <0)
the error code
*/
-extern int ZEXPORT unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len)
-{
+extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len) {
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
uInt read_now;
ZPOS64_T size_to_read;
@@ -1999,12 +1865,11 @@
/*
Close the file in zip opened with unzOpenCurrentFile
Return UNZ_CRCERROR if all the file was read but the CRC is not good
*/
-extern int ZEXPORT unzCloseCurrentFile (unzFile file)
-{
+extern int ZEXPORT unzCloseCurrentFile(unzFile file) {
int err=UNZ_OK;
unz64_s* s;
file_in_zip64_read_info_s* pfile_in_zip_read_info;
if (file==NULL)
@@ -2022,22 +1887,22 @@
if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
err=UNZ_CRCERROR;
}
- TRYFREE(pfile_in_zip_read_info->read_buffer);
+ free(pfile_in_zip_read_info->read_buffer);
pfile_in_zip_read_info->read_buffer = NULL;
if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
inflateEnd(&pfile_in_zip_read_info->stream);
#ifdef HAVE_BZIP2
else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
#endif
pfile_in_zip_read_info->stream_initialised = 0;
- TRYFREE(pfile_in_zip_read_info);
+ free(pfile_in_zip_read_info);
s->pfile_in_zip_read=NULL;
return err;
}
@@ -2046,12 +1911,11 @@
/*
Get the global comment string of the ZipFile, in the szComment buffer.
uSizeBuf is the size of the szComment buffer.
return the number of byte copied or an error code <0
*/
-extern int ZEXPORT unzGetGlobalComment (unzFile file, char * szComment, uLong uSizeBuf)
-{
+extern int ZEXPORT unzGetGlobalComment(unzFile file, char * szComment, uLong uSizeBuf) {
unz64_s* s;
uLong uReadThis ;
if (file==NULL)
return (int)UNZ_PARAMERROR;
s=(unz64_s*)file;
@@ -2074,12 +1938,11 @@
*(szComment+s->gi.size_comment)='\0';
return (int)uReadThis;
}
/* Additions by RX '2004 */
-extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file)
-{
+extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file) {
unz64_s* s;
if (file==NULL)
return 0; //UNZ_PARAMERROR;
s=(unz64_s*)file;
@@ -2089,22 +1952,20 @@
if (s->num_file==s->gi.number_entry)
return 0;
return s->pos_in_central_dir;
}
-extern uLong ZEXPORT unzGetOffset (unzFile file)
-{
+extern uLong ZEXPORT unzGetOffset(unzFile file) {
ZPOS64_T offset64;
if (file==NULL)
return 0; //UNZ_PARAMERROR;
offset64 = unzGetOffset64(file);
return (uLong)offset64;
}
-extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos)
-{
+extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos) {
unz64_s* s;
int err;
if (file==NULL)
return UNZ_PARAMERROR;
@@ -2117,9 +1978,8 @@
NULL,0,NULL,0,NULL,0);
s->current_file_ok = (err == UNZ_OK);
return err;
}
-extern int ZEXPORT unzSetOffset (unzFile file, uLong pos)
-{
+extern int ZEXPORT unzSetOffset (unzFile file, uLong pos) {
return unzSetOffset64(file,pos);
}