2012-11-26 18:58:24 +00:00
|
|
|
#include "../idlib/precompiled.h"
|
|
|
|
#pragma hdrstop
|
|
|
|
|
|
|
|
#include "Unzip.h"
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
/* unzip.h -- IO for uncompress .zip files using zlib
|
2012-11-26 18:58:24 +00:00
|
|
|
Version 0.15 beta, Mar 19th, 1998,
|
|
|
|
|
|
|
|
Copyright (C) 1998 Gilles Vollant
|
|
|
|
|
|
|
|
This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
|
|
|
|
WinZip, InfoZip tools and compatible.
|
|
|
|
Encryption and multi volume ZipFile (span) are not supported.
|
|
|
|
Old compressions used by old PKZip 1.x are not supported
|
|
|
|
|
|
|
|
THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE
|
|
|
|
CAN CHANGE IN FUTURE VERSION !!
|
|
|
|
I WAIT FEEDBACK at mail info@winimage.com
|
|
|
|
Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution
|
|
|
|
|
|
|
|
Condition of use and distribution are the same than zlib :
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
/* for more info about .ZIP format, see
|
2012-11-26 18:58:24 +00:00
|
|
|
ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
|
|
|
|
PkWare has also a specification at :
|
|
|
|
ftp://ftp.pkware.com/probdesc.zip */
|
|
|
|
|
|
|
|
|
|
|
|
#if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
|
|
|
|
!defined(CASESENSITIVITYDEFAULT_NO)
|
|
|
|
#define CASESENSITIVITYDEFAULT_NO
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef UNZ_BUFSIZE
|
|
|
|
#define UNZ_BUFSIZE (65536)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UNZ_MAXFILENAMEINZIP
|
|
|
|
#define UNZ_MAXFILENAMEINZIP (256)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ALLOC
|
|
|
|
# define ALLOC(size) (Mem_Alloc(size, TAG_IDFILE))
|
|
|
|
#endif
|
|
|
|
#ifndef TRYFREE
|
|
|
|
# define TRYFREE(p) {if (p) Mem_Free(p);}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SIZECENTRALDIRITEM (0x2e)
|
|
|
|
#define SIZEZIPLOCALHEADER (0x1e)
|
|
|
|
|
|
|
|
|
|
|
|
idCVar zip_numSeeks( "zip_numSeeks", "0", CVAR_INTEGER, "" );
|
|
|
|
idCVar zip_skippedSeeks( "zip_skippedSeeks", "0", CVAR_INTEGER, "" );
|
|
|
|
idCVar zip_seeksForward( "zip_seeksForward", "0", CVAR_INTEGER, "" );
|
|
|
|
idCVar zip_seeksBackward( "zip_seeksBackward", "0", CVAR_INTEGER, "" );
|
|
|
|
idCVar zip_avgSeekDistance( "zip_avgSeekDistance", "0", CVAR_INTEGER, "" );
|
|
|
|
|
|
|
|
/* ===========================================================================
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
static int unzlocal_getByte(FILE *fin,int *pi)
|
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
int err = fread(&c, 1, 1, fin);
|
|
|
|
if (err==1)
|
|
|
|
{
|
|
|
|
*pi = (int)c;
|
|
|
|
return UNZ_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if (ferror(fin))
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_ERRNO;
|
|
|
|
else
|
|
|
|
return UNZ_EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* ===========================================================================
|
2012-11-28 15:47:07 +00:00
|
|
|
Reads a long in LSB order from the given gz_stream. Sets
|
2012-11-26 18:58:24 +00:00
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
static int unzlocal_getShort( idFile* fin, uLong* pX )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
byte s[2];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fin->Read( s, 2 ) != 2 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*pX = 0;
|
|
|
|
return UNZ_EOF;
|
|
|
|
}
|
|
|
|
*pX = ( s[1] << 8 ) | s[0];
|
|
|
|
return UNZ_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
static int unzlocal_getLong( idFile* fin, uLong* pX )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
byte s[4];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fin->Read( s, 4 ) != 4 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*pX = 0;
|
|
|
|
return UNZ_EOF;
|
|
|
|
}
|
|
|
|
*pX = ( s[3] << 24 ) | ( s[2] << 16 ) | ( s[1] << 8 ) | s[0];
|
|
|
|
return UNZ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* My own strcmpi / strcasecmp */
|
2012-11-28 15:47:07 +00:00
|
|
|
static int strcmpcasenosensitive_internal( const char* fileName1, const char* fileName2 )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ;; )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
char c1 = *( fileName1++ );
|
|
|
|
char c2 = *( fileName2++ );
|
|
|
|
if( ( c1 >= 'a' ) && ( c1 <= 'z' ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
c1 -= 0x20;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( c2 >= 'a' ) && ( c2 <= 'z' ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
c2 -= 0x20;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c1 == '\0' )
|
|
|
|
return ( ( c2 == '\0' ) ? 0 : -1 );
|
|
|
|
if( c2 == '\0' )
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c1 < c2 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c1 > c2 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CASESENSITIVITYDEFAULT_NO
|
|
|
|
#define CASESENSITIVITYDEFAULTVALUE 2
|
|
|
|
#else
|
|
|
|
#define CASESENSITIVITYDEFAULTVALUE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef STRCMPCASENOSENTIVEFUNCTION
|
|
|
|
#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
|
|
|
|
#endif
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
/*
|
2012-11-26 18:58:24 +00:00
|
|
|
Compare two filename (fileName1,fileName2).
|
|
|
|
If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
|
|
|
|
If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
|
|
|
|
or strcasecmp)
|
|
|
|
If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
|
|
|
|
(like 1 on Unix, 2 on Windows)
|
|
|
|
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzStringFileNameCompare( const char* fileName1, const char* fileName2, int iCaseSensitivity )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( iCaseSensitivity == 0 )
|
|
|
|
iCaseSensitivity = CASESENSITIVITYDEFAULTVALUE;
|
|
|
|
|
|
|
|
if( iCaseSensitivity == 1 )
|
|
|
|
return strcmp( fileName1, fileName2 );
|
|
|
|
|
|
|
|
return STRCMPCASENOSENTIVEFUNCTION( fileName1, fileName2 );
|
|
|
|
}
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
#define BUFREADCOMMENT (0x400)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Locate the Central directory of a zipfile (at the end, just before
|
|
|
|
the global comment)
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
static uLong unzlocal_SearchCentralDir( idFile* fin )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unsigned char* buf;
|
|
|
|
uLong uSizeFile;
|
|
|
|
uLong uBackRead;
|
2012-11-28 15:47:07 +00:00
|
|
|
uLong uMaxBack = 0xffff; /* maximum size of global comment */
|
|
|
|
uLong uPosFound = 0;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fin->Seek( 0, FS_SEEK_END ) != 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
uSizeFile = fin->Tell();
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( uMaxBack > uSizeFile )
|
2012-11-26 18:58:24 +00:00
|
|
|
uMaxBack = uSizeFile;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
buf = ( unsigned char* )ALLOC( BUFREADCOMMENT + 4 );
|
|
|
|
if( buf == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
uBackRead = 4;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( uBackRead < uMaxBack )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
uLong uReadSize, uReadPos ;
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( uBackRead + BUFREADCOMMENT > uMaxBack )
|
2012-11-26 18:58:24 +00:00
|
|
|
uBackRead = uMaxBack;
|
|
|
|
else
|
2012-11-28 15:47:07 +00:00
|
|
|
uBackRead += BUFREADCOMMENT;
|
|
|
|
uReadPos = uSizeFile - uBackRead ;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
uReadSize = ( ( BUFREADCOMMENT + 4 ) < ( uSizeFile - uReadPos ) ) ?
|
|
|
|
( BUFREADCOMMENT + 4 ) : ( uSizeFile - uReadPos );
|
|
|
|
|
|
|
|
if( fin->Seek( uReadPos, FS_SEEK_SET ) != 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( fin->Read( buf, uReadSize ) != ( int )uReadSize )
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = ( int )uReadSize - 3; ( i-- ) > 0; )
|
|
|
|
if( ( ( *( buf + i ) ) == 0x50 ) && ( ( *( buf + i + 1 ) ) == 0x4b ) &&
|
|
|
|
( ( *( buf + i + 2 ) ) == 0x05 ) && ( ( *( buf + i + 3 ) ) == 0x06 ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
uPosFound = uReadPos + i;
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( uPosFound != 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
TRYFREE( buf );
|
2012-11-26 18:58:24 +00:00
|
|
|
return uPosFound;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
extern unzFile unzReOpen( const char* path, unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
unz_s* s;
|
|
|
|
idFile_Cached* fin;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
fin = fileSystem->OpenExplicitPakFile( path );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fin == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
s = ( unz_s* )ALLOC( sizeof( unz_s ) );
|
|
|
|
memcpy( s, ( unz_s* )file, sizeof( unz_s ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
s->file = fin;
|
2012-11-28 15:47:07 +00:00
|
|
|
s->pfile_in_zip_read = NULL;
|
|
|
|
|
|
|
|
return ( unzFile )s;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Open a Zip file. path contain the full pathname (by example,
|
|
|
|
on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
|
|
|
|
"zlib/zlib109.zip".
|
|
|
|
If the zipfile cannot be opened (file don'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.
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern unzFile unzOpen( const char* path )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s us;
|
2012-11-28 15:47:07 +00:00
|
|
|
unz_s* s;
|
|
|
|
uLong central_pos, uL;
|
|
|
|
idFile_Cached* fin ;
|
|
|
|
|
|
|
|
uLong number_disk; /* number of the current dist, used for
|
2012-11-26 18:58:24 +00:00
|
|
|
spaning ZIP, unsupported, always 0*/
|
|
|
|
uLong number_disk_with_CD; /* number the the disk with central dir, used
|
|
|
|
for spaning ZIP, unsupported, always 0*/
|
|
|
|
uLong number_entry_CD; /* total number of entries in
|
2012-11-28 15:47:07 +00:00
|
|
|
the central dir
|
2012-11-26 18:58:24 +00:00
|
|
|
(same than number_entry on nospan) */
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
int err = UNZ_OK;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
fin = fileSystem->OpenExplicitPakFile( path );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( fin == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
central_pos = unzlocal_SearchCentralDir( fin );
|
|
|
|
if( central_pos == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
err = UNZ_ERRNO;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( fin->Seek( central_pos, FS_SEEK_SET ) != 0 )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* the signature, already checked */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getLong( fin, &uL ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* number of this disk */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getShort( fin, &number_disk ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* number of the disk with the start of the central directory */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getShort( fin, &number_disk_with_CD ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* total number of entries in the central dir on this disk */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getShort( fin, &us.gi.number_entry ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* total number of entries in the central dir */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getShort( fin, &number_entry_CD ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( ( number_entry_CD != us.gi.number_entry ) ||
|
|
|
|
( number_disk_with_CD != 0 ) ||
|
|
|
|
( number_disk != 0 ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* size of the central directory */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getLong( fin, &us.size_central_dir ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
/* offset of start of central directory with respect to the
|
2012-11-26 18:58:24 +00:00
|
|
|
starting disk number */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getLong( fin, &us.offset_central_dir ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* zipfile comment length */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getShort( fin, &us.gi.size_comment ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( ( central_pos < us.offset_central_dir + us.size_central_dir ) &&
|
|
|
|
( err == UNZ_OK ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
|
|
|
if( err != UNZ_OK )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
fileSystem->CloseFile( fin );
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
us.file = fin;
|
2012-11-26 18:58:24 +00:00
|
|
|
us.byte_before_the_zipfile = central_pos -
|
2012-11-28 15:47:07 +00:00
|
|
|
( us.offset_central_dir + us.size_central_dir );
|
2012-11-26 18:58:24 +00:00
|
|
|
us.central_pos = central_pos;
|
2012-11-28 15:47:07 +00:00
|
|
|
us.pfile_in_zip_read = NULL;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
us.file->CacheData( us.offset_central_dir, us.size_central_dir );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
s = ( unz_s* )ALLOC( sizeof( unz_s ) );
|
|
|
|
*s = us;
|
|
|
|
|
|
|
|
// unzGoToFirstFile((unzFile)s);
|
|
|
|
return ( unzFile )s;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Close a ZipFile opened with unzipOpen.
|
|
|
|
If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
|
|
|
|
these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
|
|
|
|
return UNZ_OK if there is no problem. */
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzClose( unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s* s;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
|
|
|
|
if( s->pfile_in_zip_read != NULL )
|
|
|
|
unzCloseCurrentFile( file );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
fileSystem->CloseFile( s->file );
|
2012-11-28 15:47:07 +00:00
|
|
|
TRYFREE( s );
|
2012-11-26 18:58:24 +00:00
|
|
|
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. */
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzGetGlobalInfo( unzFile file, unz_global_info* pglobal_info )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s* s;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
*pglobal_info = s->gi;
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Translate date/time from Dos format to tm_unz (readable more easilty)
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
static void unzlocal_DosDateToTmuDate( uLong ulDosDate, tm_unz* ptm )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
uLong uDate;
|
|
|
|
uDate = ( uLong )( 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_hour = ( uInt )( ( ulDosDate & 0xF800 ) / 0x800 );
|
|
|
|
ptm->tm_min = ( uInt )( ( ulDosDate & 0x7E0 ) / 0x20 ) ;
|
|
|
|
ptm->tm_sec = ( uInt )( 2 * ( ulDosDate & 0x1f ) ) ;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get Info about the current file in the zipfile, with internal only info
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
static int unzlocal_GetCurrentFileInfoInternal( unzFile file,
|
|
|
|
unz_file_info* pfile_info,
|
|
|
|
unz_file_info_internal
|
|
|
|
*pfile_info_internal,
|
|
|
|
char* szFileName,
|
|
|
|
uLong fileNameBufferSize,
|
|
|
|
void* extraField,
|
|
|
|
uLong extraFieldBufferSize,
|
|
|
|
char* szComment,
|
|
|
|
uLong commentBufferSize )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s* s;
|
|
|
|
unz_file_info file_info;
|
|
|
|
unz_file_info_internal file_info_internal;
|
2012-11-28 15:47:07 +00:00
|
|
|
int err = UNZ_OK;
|
2012-11-26 18:58:24 +00:00
|
|
|
uLong uMagic;
|
2012-11-28 15:47:07 +00:00
|
|
|
long lSeek = 0;
|
|
|
|
|
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int tellpos = s->file->Tell() - s->pos_in_central_dir + s->byte_before_the_zipfile;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tellpos != 0 )
|
|
|
|
{
|
|
|
|
if( s->file->Seek( s->pos_in_central_dir + s->byte_before_the_zipfile, FS_SEEK_SET ) != 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
err = UNZ_ERRNO;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tellpos < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
zip_seeksForward.SetInteger( zip_seeksForward.GetInteger() + 1 );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
zip_seeksBackward.SetInteger( zip_seeksBackward.GetInteger() + 1 );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
static long zip_totalSeekSize = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( zip_numSeeks.GetInteger() == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
zip_totalSeekSize = 0;
|
|
|
|
}
|
|
|
|
zip_totalSeekSize += abs( tellpos );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
zip_numSeeks.SetInteger( zip_numSeeks.GetInteger() + 1 );
|
|
|
|
zip_avgSeekDistance.SetInteger( zip_totalSeekSize / zip_numSeeks.GetInteger() );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
zip_skippedSeeks.SetInteger( zip_skippedSeeks.GetInteger() + 1 );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* we check the magic */
|
2012-11-28 15:47:07 +00:00
|
|
|
if( err == UNZ_OK )
|
2012-12-11 22:48:55 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getLong( s->file, &uMagic ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
else if( uMagic != 0x02014b50 )
|
|
|
|
err = UNZ_BADZIPFILE;
|
2012-12-11 22:48:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getShort( s->file, &file_info.version ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.version_needed ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.flag ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.compression_method ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &file_info.dosDate ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
unzlocal_DosDateToTmuDate( file_info.dosDate, &file_info.tmu_date );
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &file_info.crc ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &file_info.compressed_size ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &file_info.uncompressed_size ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.size_filename ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.size_file_extra ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.size_file_comment ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.disk_num_start ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &file_info.internal_fa ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &file_info.external_fa ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &file_info_internal.offset_curfile ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
lSeek += file_info.size_filename;
|
|
|
|
if( ( err == UNZ_OK ) && ( szFileName != NULL ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
uLong uSizeRead ;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file_info.size_filename < fileNameBufferSize )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
*( szFileName + file_info.size_filename ) = '\0';
|
2012-11-26 18:58:24 +00:00
|
|
|
uSizeRead = file_info.size_filename;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
uSizeRead = fileNameBufferSize;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ( file_info.size_filename > 0 ) && ( fileNameBufferSize > 0 ) )
|
|
|
|
if( s->file->Read( szFileName, uSizeRead ) != ( int )uSizeRead )
|
|
|
|
err = UNZ_ERRNO;
|
2012-11-26 18:58:24 +00:00
|
|
|
lSeek -= uSizeRead;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ( err == UNZ_OK ) && ( extraField != NULL ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
uLong uSizeRead ;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file_info.size_file_extra < extraFieldBufferSize )
|
2012-11-26 18:58:24 +00:00
|
|
|
uSizeRead = file_info.size_file_extra;
|
|
|
|
else
|
|
|
|
uSizeRead = extraFieldBufferSize;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( lSeek != 0 )
|
2012-12-11 22:48:55 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( s->file->Seek( lSeek, FS_SEEK_CUR ) == 0 )
|
|
|
|
lSeek = 0;
|
2012-11-26 18:58:24 +00:00
|
|
|
else
|
2012-11-28 15:47:07 +00:00
|
|
|
err = UNZ_ERRNO;
|
2012-12-11 22:48:55 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( file_info.size_file_extra > 0 ) && ( extraFieldBufferSize > 0 ) )
|
|
|
|
if( s->file->Read( extraField, uSizeRead ) != ( int )uSizeRead )
|
|
|
|
err = UNZ_ERRNO;
|
2012-11-26 18:58:24 +00:00
|
|
|
lSeek += file_info.size_file_extra - uSizeRead;
|
|
|
|
}
|
|
|
|
else
|
2012-11-28 15:47:07 +00:00
|
|
|
lSeek += file_info.size_file_extra;
|
|
|
|
|
|
|
|
|
|
|
|
if( ( err == UNZ_OK ) && ( szComment != NULL ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
uLong uSizeRead ;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file_info.size_file_comment < commentBufferSize )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
*( szComment + file_info.size_file_comment ) = '\0';
|
2012-11-26 18:58:24 +00:00
|
|
|
uSizeRead = file_info.size_file_comment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
uSizeRead = commentBufferSize;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( lSeek != 0 )
|
2012-12-11 22:48:55 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( s->file->Seek( lSeek, FS_SEEK_CUR ) == 0 )
|
|
|
|
lSeek = 0;
|
2012-11-26 18:58:24 +00:00
|
|
|
else
|
2012-11-28 15:47:07 +00:00
|
|
|
err = UNZ_ERRNO;
|
2012-12-11 22:48:55 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( file_info.size_file_comment > 0 ) && ( commentBufferSize > 0 ) )
|
|
|
|
if( s->file->Read( szComment, uSizeRead ) != ( int )uSizeRead )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
lSeek += file_info.size_file_comment - uSizeRead;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
else
|
2012-11-28 15:47:07 +00:00
|
|
|
lSeek += file_info.size_file_comment;
|
|
|
|
|
|
|
|
if( ( err == UNZ_OK ) && ( pfile_info != NULL ) )
|
|
|
|
*pfile_info = file_info;
|
|
|
|
|
|
|
|
if( ( err == UNZ_OK ) && ( pfile_info_internal != NULL ) )
|
|
|
|
*pfile_info_internal = file_info_internal;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzGetCurrentFileInfo( unzFile file, unz_file_info* pfile_info,
|
|
|
|
char* szFileName, uLong fileNameBufferSize,
|
|
|
|
void* extraField, uLong extraFieldBufferSize,
|
|
|
|
char* szComment, uLong commentBufferSize )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
return unzlocal_GetCurrentFileInfoInternal( file, pfile_info, NULL,
|
|
|
|
szFileName, fileNameBufferSize,
|
|
|
|
extraField, extraFieldBufferSize,
|
|
|
|
szComment, commentBufferSize );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Set the current file of the zipfile to the first file.
|
|
|
|
return UNZ_OK if there is no problem
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzGoToFirstFile( unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
int err = UNZ_OK;
|
2012-11-26 18:58:24 +00:00
|
|
|
unz_s* s;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
s->pos_in_central_dir = s->offset_central_dir;
|
|
|
|
s->num_file = 0;
|
|
|
|
err = unzlocal_GetCurrentFileInfoInternal( file, &s->cur_file_info,
|
|
|
|
&s->cur_file_info_internal,
|
|
|
|
NULL, 0, NULL, 0, NULL, 0 );
|
|
|
|
s->current_file_ok = ( err == UNZ_OK );
|
2012-11-26 18:58:24 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzGoToNextFile( unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
unz_s* s;
|
2012-11-26 18:58:24 +00:00
|
|
|
int err;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
if( !s->current_file_ok )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_END_OF_LIST_OF_FILE;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( s->num_file + 1 == s->gi.number_entry )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_END_OF_LIST_OF_FILE;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
|
2012-11-28 15:47:07 +00:00
|
|
|
s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
|
2012-11-26 18:58:24 +00:00
|
|
|
s->num_file++;
|
2012-11-28 15:47:07 +00:00
|
|
|
err = unzlocal_GetCurrentFileInfoInternal( file, &s->cur_file_info,
|
|
|
|
&s->cur_file_info_internal,
|
|
|
|
NULL, 0, NULL, 0, NULL, 0 );
|
|
|
|
s->current_file_ok = ( err == UNZ_OK );
|
2012-11-26 18:58:24 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the position of the info of the current file in the zip.
|
|
|
|
return UNZ_OK if there is no problem
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzGetCurrentFileInfoPosition( unzFile file, unsigned long* pos )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
unz_s* s;
|
|
|
|
|
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
*pos = s->pos_in_central_dir;
|
|
|
|
return UNZ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Set the position of the info of the current file in the zip.
|
|
|
|
return UNZ_OK if there is no problem
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzSetCurrentFileInfoPosition( unzFile file, unsigned long pos )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
unz_s* s;
|
2012-11-26 18:58:24 +00:00
|
|
|
int err;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
s->pos_in_central_dir = pos;
|
2012-11-28 15:47:07 +00:00
|
|
|
err = unzlocal_GetCurrentFileInfoInternal( file, &s->cur_file_info,
|
|
|
|
&s->cur_file_info_internal,
|
|
|
|
NULL, 0, NULL, 0, NULL, 0 );
|
|
|
|
s->current_file_ok = ( err == UNZ_OK );
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try locate the file szFileName in the zipfile.
|
|
|
|
For the iCaseSensitivity signification, see unzipStringFileNameCompare
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzLocateFile( unzFile file, const char* szFileName, int iCaseSensitivity )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
unz_s* s;
|
2012-11-26 18:58:24 +00:00
|
|
|
int err;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
uLong num_fileSaved;
|
|
|
|
uLong pos_in_central_dirSaved;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( strlen( szFileName ) >= UNZ_MAXFILENAMEINZIP )
|
|
|
|
return UNZ_PARAMERROR;
|
|
|
|
|
|
|
|
s = ( unz_s* )file;
|
|
|
|
if( !s->current_file_ok )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_END_OF_LIST_OF_FILE;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
num_fileSaved = s->num_file;
|
|
|
|
pos_in_central_dirSaved = s->pos_in_central_dir;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
err = unzGoToFirstFile( file );
|
|
|
|
|
|
|
|
while( err == UNZ_OK )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1];
|
|
|
|
unzGetCurrentFileInfo( file, NULL,
|
|
|
|
szCurrentFileName, sizeof( szCurrentFileName ) - 1,
|
|
|
|
NULL, 0, NULL, 0 );
|
|
|
|
if( unzStringFileNameCompare( szCurrentFileName,
|
|
|
|
szFileName, iCaseSensitivity ) == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_OK;
|
2012-11-28 15:47:07 +00:00
|
|
|
err = unzGoToNextFile( file );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
s->num_file = num_fileSaved ;
|
|
|
|
s->pos_in_central_dir = pos_in_central_dirSaved ;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read the static header of the current zipfile
|
|
|
|
Check the coherency of the static header and info in the end of central
|
|
|
|
directory about this file
|
|
|
|
store in *piSizeVar the size of extra info in static header
|
|
|
|
(filename and size of extra field data)
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
static int unzlocal_CheckCurrentFileCoherencyHeader( unz_s* s, uInt* piSizeVar,
|
|
|
|
uLong* poffset_local_extrafield,
|
|
|
|
uInt* psize_local_extrafield )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
uLong uMagic, uData, uFlags;
|
2012-11-26 18:58:24 +00:00
|
|
|
uLong size_filename;
|
|
|
|
uLong size_extra_field;
|
2012-11-28 15:47:07 +00:00
|
|
|
int err = UNZ_OK;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
*piSizeVar = 0;
|
|
|
|
*poffset_local_extrafield = 0;
|
|
|
|
*psize_local_extrafield = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( s->file->Seek( s->cur_file_info_internal.offset_curfile + s->byte_before_the_zipfile, FS_SEEK_SET ) != 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_ERRNO;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( err == UNZ_OK )
|
2012-12-11 22:48:55 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getLong( s->file, &uMagic ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
else if( uMagic != 0x04034b50 )
|
|
|
|
err = UNZ_BADZIPFILE;
|
2012-12-11 22:48:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( unzlocal_getShort( s->file, &uData ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
/*
|
|
|
|
else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
|
2012-11-26 18:58:24 +00:00
|
|
|
err=UNZ_BADZIPFILE;
|
2012-11-28 15:47:07 +00:00
|
|
|
*/
|
|
|
|
if( unzlocal_getShort( s->file, &uFlags ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &uData ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
else if( ( err == UNZ_OK ) && ( uData != s->cur_file_info.compression_method ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
|
|
|
if( ( err == UNZ_OK ) && ( s->cur_file_info.compression_method != 0 ) &&
|
|
|
|
( s->cur_file_info.compression_method != Z_DEFLATED ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &uData ) != UNZ_OK ) /* date/time */
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &uData ) != UNZ_OK ) /* crc */
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
else if( ( err == UNZ_OK ) && ( uData != s->cur_file_info.crc ) &&
|
|
|
|
( ( uFlags & 8 ) == 0 ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &uData ) != UNZ_OK ) /* size compr */
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
else if( ( err == UNZ_OK ) && ( uData != s->cur_file_info.compressed_size ) &&
|
|
|
|
( ( uFlags & 8 ) == 0 ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
|
|
|
if( unzlocal_getLong( s->file, &uData ) != UNZ_OK ) /* size uncompr */
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
else if( ( err == UNZ_OK ) && ( uData != s->cur_file_info.uncompressed_size ) &&
|
|
|
|
( ( uFlags & 8 ) == 0 ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &size_filename ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
else if( ( err == UNZ_OK ) && ( size_filename != s->cur_file_info.size_filename ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
|
|
|
|
*piSizeVar += ( uInt )size_filename;
|
|
|
|
|
|
|
|
if( unzlocal_getShort( s->file, &size_extra_field ) != UNZ_OK )
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
*poffset_local_extrafield = s->cur_file_info_internal.offset_curfile +
|
|
|
|
SIZEZIPLOCALHEADER + size_filename;
|
|
|
|
*psize_local_extrafield = ( uInt )size_extra_field;
|
|
|
|
|
|
|
|
*piSizeVar += ( uInt )size_extra_field;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return err;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzOpenCurrentFile( unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
int err = UNZ_OK;
|
2012-11-26 18:58:24 +00:00
|
|
|
int Store;
|
|
|
|
uInt iSizeVar;
|
|
|
|
unz_s* s;
|
|
|
|
file_in_zip_read_info_s* pfile_in_zip_read_info;
|
|
|
|
uLong offset_local_extrafield; /* offset of the static extra field */
|
|
|
|
uInt size_local_extrafield; /* size of the static extra field */
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
if( !s->current_file_ok )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( s->pfile_in_zip_read != NULL )
|
|
|
|
unzCloseCurrentFile( file );
|
|
|
|
|
|
|
|
if( unzlocal_CheckCurrentFileCoherencyHeader( s, &iSizeVar,
|
|
|
|
&offset_local_extrafield, &size_local_extrafield ) != UNZ_OK )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_BADZIPFILE;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
pfile_in_zip_read_info = ( file_in_zip_read_info_s* )
|
|
|
|
ALLOC( sizeof( file_in_zip_read_info_s ) );
|
|
|
|
if( pfile_in_zip_read_info == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_INTERNALERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
pfile_in_zip_read_info->read_buffer = ( char* )ALLOC( UNZ_BUFSIZE );
|
2012-11-26 18:58:24 +00:00
|
|
|
pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
|
|
|
|
pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->pos_local_extrafield = 0;
|
|
|
|
|
|
|
|
if( pfile_in_zip_read_info->read_buffer == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
TRYFREE( pfile_in_zip_read_info );
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_INTERNALERROR;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->stream_initialised = 0;
|
|
|
|
|
|
|
|
if( ( s->cur_file_info.compression_method != 0 ) &&
|
|
|
|
( s->cur_file_info.compression_method != Z_DEFLATED ) )
|
|
|
|
err = UNZ_BADZIPFILE;
|
|
|
|
Store = s->cur_file_info.compression_method == 0;
|
|
|
|
|
|
|
|
pfile_in_zip_read_info->crc32_wait = s->cur_file_info.crc;
|
|
|
|
pfile_in_zip_read_info->crc32 = 0;
|
2012-11-26 18:58:24 +00:00
|
|
|
pfile_in_zip_read_info->compression_method =
|
2012-11-28 15:47:07 +00:00
|
|
|
s->cur_file_info.compression_method;
|
|
|
|
pfile_in_zip_read_info->file = s->file;
|
|
|
|
pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile;
|
|
|
|
|
|
|
|
pfile_in_zip_read_info->stream.total_out = 0;
|
|
|
|
|
|
|
|
if( !Store )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->stream.zalloc = ( alloc_func )0;
|
|
|
|
pfile_in_zip_read_info->stream.zfree = ( free_func )0;
|
|
|
|
pfile_in_zip_read_info->stream.opaque = ( voidp )0;
|
|
|
|
|
|
|
|
err = inflateInit2( &pfile_in_zip_read_info->stream, -MAX_WBITS );
|
|
|
|
if( err == Z_OK )
|
|
|
|
pfile_in_zip_read_info->stream_initialised = 1;
|
|
|
|
/* 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
|
|
|
|
* return Z_STREAM_END.
|
|
|
|
* In unzip, i don't wait absolutely Z_STREAM_END because I known the
|
|
|
|
* size of both compressed and uncompressed data
|
|
|
|
*/
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->rest_read_compressed =
|
|
|
|
s->cur_file_info.compressed_size ;
|
|
|
|
pfile_in_zip_read_info->rest_read_uncompressed =
|
|
|
|
s->cur_file_info.uncompressed_size ;
|
|
|
|
|
|
|
|
|
|
|
|
pfile_in_zip_read_info->pos_in_zipfile =
|
|
|
|
s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
|
|
|
|
iSizeVar;
|
|
|
|
|
|
|
|
pfile_in_zip_read_info->stream.avail_in = ( uInt )0;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
s->pfile_in_zip_read = pfile_in_zip_read_info;
|
2012-11-28 15:47:07 +00:00
|
|
|
return UNZ_OK;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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 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)
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzReadCurrentFile( unzFile file, void* buf, unsigned len )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
int err = UNZ_OK;
|
2012-11-26 18:58:24 +00:00
|
|
|
uInt iRead = 0;
|
|
|
|
unz_s* s;
|
|
|
|
file_in_zip_read_info_s* pfile_in_zip_read_info;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
pfile_in_zip_read_info = s->pfile_in_zip_read;
|
|
|
|
|
|
|
|
if( pfile_in_zip_read_info == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( ( pfile_in_zip_read_info->read_buffer == NULL ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_END_OF_LIST_OF_FILE;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( len == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
pfile_in_zip_read_info->stream.next_out = ( Byte* )buf;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->stream.avail_out = ( uInt )len;
|
|
|
|
|
|
|
|
if( len > pfile_in_zip_read_info->rest_read_uncompressed )
|
|
|
|
pfile_in_zip_read_info->stream.avail_out =
|
|
|
|
( uInt )pfile_in_zip_read_info->rest_read_uncompressed;
|
|
|
|
|
|
|
|
while( pfile_in_zip_read_info->stream.avail_out > 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( pfile_in_zip_read_info->stream.avail_in == 0 ) &&
|
|
|
|
( pfile_in_zip_read_info->rest_read_compressed > 0 ) )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
uInt uReadThis = UNZ_BUFSIZE;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pfile_in_zip_read_info->rest_read_compressed < uReadThis )
|
|
|
|
uReadThis = ( uInt )pfile_in_zip_read_info->rest_read_compressed;
|
|
|
|
if( uReadThis == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_EOF;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( s->cur_file_info.compressed_size == pfile_in_zip_read_info->rest_read_compressed )
|
|
|
|
if( pfile_in_zip_read_info->file->Seek( pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile, FS_SEEK_SET ) != 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_ERRNO;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pfile_in_zip_read_info->file->Read( pfile_in_zip_read_info->read_buffer, uReadThis ) != ( int )uReadThis )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_ERRNO;
|
|
|
|
pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->rest_read_compressed -= uReadThis;
|
|
|
|
|
|
|
|
pfile_in_zip_read_info->stream.next_in =
|
|
|
|
( Byte* )pfile_in_zip_read_info->read_buffer;
|
|
|
|
pfile_in_zip_read_info->stream.avail_in = ( uInt )uReadThis;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( pfile_in_zip_read_info->compression_method == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
uInt uDoCopy, i ;
|
|
|
|
if( pfile_in_zip_read_info->stream.avail_out <
|
|
|
|
pfile_in_zip_read_info->stream.avail_in )
|
2012-11-26 18:58:24 +00:00
|
|
|
uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
|
|
|
|
else
|
|
|
|
uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < uDoCopy; i++ )
|
|
|
|
*( pfile_in_zip_read_info->stream.next_out + i ) =
|
|
|
|
*( pfile_in_zip_read_info->stream.next_in + i );
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->crc32 = crc32( pfile_in_zip_read_info->crc32,
|
|
|
|
pfile_in_zip_read_info->stream.next_out,
|
|
|
|
uDoCopy );
|
|
|
|
pfile_in_zip_read_info->rest_read_uncompressed -= uDoCopy;
|
2012-11-26 18:58:24 +00:00
|
|
|
pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
|
|
|
|
pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
|
|
|
|
pfile_in_zip_read_info->stream.next_out += uDoCopy;
|
|
|
|
pfile_in_zip_read_info->stream.next_in += uDoCopy;
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->stream.total_out += uDoCopy;
|
2012-11-26 18:58:24 +00:00
|
|
|
iRead += uDoCopy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
uLong uTotalOutBefore, uTotalOutAfter;
|
|
|
|
const Byte* bufBefore;
|
2012-11-26 18:58:24 +00:00
|
|
|
uLong uOutThis;
|
2012-11-28 15:47:07 +00:00
|
|
|
int flush = Z_SYNC_FLUSH;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
|
|
|
|
bufBefore = pfile_in_zip_read_info->stream.next_out;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
if ((pfile_in_zip_read_info->rest_read_uncompressed ==
|
|
|
|
pfile_in_zip_read_info->stream.avail_out) &&
|
|
|
|
(pfile_in_zip_read_info->rest_read_compressed == 0))
|
|
|
|
flush = Z_FINISH;
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
err = inflate( &pfile_in_zip_read_info->stream, flush );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
|
2012-11-28 15:47:07 +00:00
|
|
|
uOutThis = uTotalOutAfter - uTotalOutBefore;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
pfile_in_zip_read_info->crc32 =
|
|
|
|
crc32( pfile_in_zip_read_info->crc32, bufBefore,
|
|
|
|
( uInt )( uOutThis ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
pfile_in_zip_read_info->rest_read_uncompressed -=
|
2012-11-28 15:47:07 +00:00
|
|
|
uOutThis;
|
|
|
|
|
|
|
|
iRead += ( uInt )( uTotalOutAfter - uTotalOutBefore );
|
|
|
|
|
|
|
|
if( err == Z_STREAM_END )
|
|
|
|
return ( iRead == 0 ) ? UNZ_EOF : iRead;
|
|
|
|
if( err != Z_OK )
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( err == Z_OK )
|
2012-11-26 18:58:24 +00:00
|
|
|
return iRead;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Give the current position in uncompressed data
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern long unztell( unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s* s;
|
|
|
|
file_in_zip_read_info_s* pfile_in_zip_read_info;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
pfile_in_zip_read_info = s->pfile_in_zip_read;
|
|
|
|
|
|
|
|
if( pfile_in_zip_read_info == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
return ( long )pfile_in_zip_read_info->stream.total_out;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-11-28 15:47:07 +00:00
|
|
|
return 1 if the end of file was reached, 0 elsewhere
|
2012-11-26 18:58:24 +00:00
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzeof( unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s* s;
|
|
|
|
file_in_zip_read_info_s* pfile_in_zip_read_info;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
pfile_in_zip_read_info = s->pfile_in_zip_read;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pfile_in_zip_read_info == NULL )
|
|
|
|
return UNZ_PARAMERROR;
|
|
|
|
|
|
|
|
if( pfile_in_zip_read_info->rest_read_uncompressed == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read extra field from the current file (opened by unzOpenCurrentFile)
|
|
|
|
This is the static-header version of the extra field (sometimes, there is
|
|
|
|
more info in the static-header version than in the central-header)
|
|
|
|
|
|
|
|
if buf==NULL, it return the size of the static extra field that can be read
|
|
|
|
|
|
|
|
if buf!=NULL, len is the size of the buffer, the extra header is copied in
|
|
|
|
buf.
|
2012-11-28 15:47:07 +00:00
|
|
|
the return value is the number of bytes copied in buf, or (if <0)
|
2012-11-26 18:58:24 +00:00
|
|
|
the error code
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzGetLocalExtrafield( unzFile file, void* buf, unsigned len )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s* s;
|
|
|
|
file_in_zip_read_info_s* pfile_in_zip_read_info;
|
|
|
|
uInt read_now;
|
|
|
|
uLong size_to_read;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
pfile_in_zip_read_info = s->pfile_in_zip_read;
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pfile_in_zip_read_info == NULL )
|
|
|
|
return UNZ_PARAMERROR;
|
|
|
|
|
|
|
|
size_to_read = ( pfile_in_zip_read_info->size_local_extrafield -
|
|
|
|
pfile_in_zip_read_info->pos_local_extrafield );
|
|
|
|
|
|
|
|
if( buf == NULL )
|
|
|
|
return ( int )size_to_read;
|
|
|
|
|
|
|
|
if( len > size_to_read )
|
|
|
|
read_now = ( uInt )size_to_read;
|
2012-11-26 18:58:24 +00:00
|
|
|
else
|
2012-11-28 15:47:07 +00:00
|
|
|
read_now = ( uInt )len ;
|
|
|
|
|
|
|
|
if( read_now == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( pfile_in_zip_read_info->file->Seek( pfile_in_zip_read_info->offset_local_extrafield + pfile_in_zip_read_info->pos_local_extrafield, FS_SEEK_SET ) != 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_ERRNO;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( pfile_in_zip_read_info->file->Read( buf, size_to_read ) != ( int )size_to_read )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_ERRNO;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
return ( int )read_now;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Close the file in zip opened with unzipOpenCurrentFile
|
|
|
|
Return UNZ_CRCERROR if all the file was read but the CRC is not good
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzCloseCurrentFile( unzFile file )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
int err = UNZ_OK;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
unz_s* s;
|
|
|
|
file_in_zip_read_info_s* pfile_in_zip_read_info;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
pfile_in_zip_read_info = s->pfile_in_zip_read;
|
|
|
|
|
|
|
|
if( pfile_in_zip_read_info == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( pfile_in_zip_read_info->rest_read_uncompressed == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait )
|
|
|
|
err = UNZ_CRCERROR;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
TRYFREE( pfile_in_zip_read_info->read_buffer );
|
2012-11-26 18:58:24 +00:00
|
|
|
pfile_in_zip_read_info->read_buffer = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( pfile_in_zip_read_info->stream_initialised )
|
|
|
|
inflateEnd( &pfile_in_zip_read_info->stream );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
pfile_in_zip_read_info->stream_initialised = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
TRYFREE( pfile_in_zip_read_info );
|
|
|
|
|
|
|
|
s->pfile_in_zip_read = NULL;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
extern int unzGetGlobalComment( unzFile file, char* szComment, uLong uSizeBuf )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
|
|
|
unz_s* s;
|
|
|
|
uLong uReadThis ;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( file == NULL )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_PARAMERROR;
|
2012-11-28 15:47:07 +00:00
|
|
|
s = ( unz_s* )file;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
uReadThis = uSizeBuf;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( uReadThis > s->gi.size_comment )
|
2012-11-26 18:58:24 +00:00
|
|
|
uReadThis = s->gi.size_comment;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( s->file->Seek( s->central_pos + 22, FS_SEEK_SET ) == 0 )
|
2012-11-26 18:58:24 +00:00
|
|
|
return UNZ_ERRNO;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( uReadThis > 0 )
|
|
|
|
{
|
|
|
|
if( szComment == NULL )
|
|
|
|
{
|
|
|
|
return ( int )uReadThis;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
*szComment = '\0';
|
|
|
|
if( s->file->Read( szComment, uReadThis ) != ( int )uReadThis )
|
|
|
|
return UNZ_ERRNO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( szComment != NULL ) && ( uSizeBuf > s->gi.size_comment ) )
|
|
|
|
*( szComment + s->gi.size_comment ) = '\0';
|
|
|
|
return ( int )uReadThis;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|