cannam@89: /* unzip.c -- IO for uncompress .zip files using zlib cannam@89: Version 1.1, February 14h, 2010 cannam@89: part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) cannam@89: cannam@89: Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) cannam@89: cannam@89: Modifications of Unzip for Zip64 cannam@89: Copyright (C) 2007-2008 Even Rouault cannam@89: cannam@89: Modifications for Zip64 support on both zip and unzip cannam@89: Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) cannam@89: cannam@89: For more info read MiniZip_info.txt cannam@89: cannam@89: cannam@89: ------------------------------------------------------------------------------------ cannam@89: Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of cannam@89: compatibility with older software. The following is from the original crypt.c. cannam@89: Code woven in by Terry Thorsen 1/2003. cannam@89: cannam@89: Copyright (c) 1990-2000 Info-ZIP. All rights reserved. cannam@89: cannam@89: See the accompanying file LICENSE, version 2000-Apr-09 or later cannam@89: (the contents of which are also included in zip.h) for terms of use. cannam@89: If, for some reason, all these files are missing, the Info-ZIP license cannam@89: also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html cannam@89: cannam@89: crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h] cannam@89: cannam@89: The encryption/decryption parts of this source code (as opposed to the cannam@89: non-echoing password parts) were originally written in Europe. The cannam@89: whole source package can be freely distributed, including from the USA. cannam@89: (Prior to January 2000, re-export from the US was a violation of US law.) cannam@89: cannam@89: This encryption code is a direct transcription of the algorithm from cannam@89: Roger Schlafly, described by Phil Katz in the file appnote.txt. This cannam@89: file (appnote.txt) is distributed with the PKZIP program (even in the cannam@89: version without encryption capabilities). cannam@89: cannam@89: ------------------------------------------------------------------------------------ cannam@89: cannam@89: Changes in unzip.c cannam@89: cannam@89: 2007-2008 - Even Rouault - Addition of cpl_unzGetCurrentFileZStreamPos cannam@89: 2007-2008 - Even Rouault - Decoration of symbol names unz* -> cpl_unz* cannam@89: 2007-2008 - Even Rouault - Remove old C style function prototypes cannam@89: 2007-2008 - Even Rouault - Add unzip support for ZIP64 cannam@89: cannam@89: Copyright (C) 2007-2008 Even Rouault cannam@89: cannam@89: cannam@89: 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). cannam@89: Oct-2009 - Mathias Svensson - Fixed problem if uncompressed size was > 4G and compressed size was <4G cannam@89: should only read the compressed/uncompressed size from the Zip64 format if cannam@89: the size from normal header was 0xFFFFFFFF cannam@89: Oct-2009 - Mathias Svensson - Applied some bug fixes from paches recived from Gilles Vollant cannam@89: Oct-2009 - Mathias Svensson - Applied support to unzip files with compression mathod BZIP2 (bzip2 lib is required) cannam@89: Patch created by Daniel Borca cannam@89: cannam@89: Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer cannam@89: cannam@89: Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson cannam@89: cannam@89: */ cannam@89: cannam@89: cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: cannam@89: #ifndef NOUNCRYPT cannam@89: #define NOUNCRYPT cannam@89: #endif cannam@89: cannam@89: #include "zlib.h" cannam@89: #include "unzip.h" cannam@89: cannam@89: #ifdef STDC cannam@89: # include cannam@89: # include cannam@89: # include cannam@89: #endif cannam@89: #ifdef NO_ERRNO_H cannam@89: extern int errno; cannam@89: #else cannam@89: # include cannam@89: #endif cannam@89: cannam@89: cannam@89: #ifndef local cannam@89: # define local static cannam@89: #endif cannam@89: /* compile with -Dlocal if your debugger can't find static symbols */ cannam@89: cannam@89: cannam@89: #ifndef CASESENSITIVITYDEFAULT_NO cannam@89: # if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) cannam@89: # define CASESENSITIVITYDEFAULT_NO cannam@89: # endif cannam@89: #endif cannam@89: cannam@89: cannam@89: #ifndef UNZ_BUFSIZE cannam@89: #define UNZ_BUFSIZE (16384) cannam@89: #endif cannam@89: cannam@89: #ifndef UNZ_MAXFILENAMEINZIP cannam@89: #define UNZ_MAXFILENAMEINZIP (256) cannam@89: #endif cannam@89: cannam@89: #ifndef ALLOC cannam@89: # define ALLOC(size) (malloc(size)) cannam@89: #endif cannam@89: #ifndef TRYFREE cannam@89: # define TRYFREE(p) {if (p) free(p);} cannam@89: #endif cannam@89: cannam@89: #define SIZECENTRALDIRITEM (0x2e) cannam@89: #define SIZEZIPLOCALHEADER (0x1e) cannam@89: cannam@89: cannam@89: const char unz_copyright[] = cannam@89: " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll"; cannam@89: cannam@89: /* unz_file_info_interntal contain internal info about a file in zipfile*/ cannam@89: typedef struct unz_file_info64_internal_s cannam@89: { cannam@89: ZPOS64_T offset_curfile;/* relative offset of local header 8 bytes */ cannam@89: } unz_file_info64_internal; cannam@89: cannam@89: cannam@89: /* file_in_zip_read_info_s contain internal information about a file in zipfile, cannam@89: when reading and decompress it */ cannam@89: typedef struct cannam@89: { cannam@89: char *read_buffer; /* internal buffer for compressed data */ cannam@89: z_stream stream; /* zLib stream structure for inflate */ cannam@89: cannam@89: #ifdef HAVE_BZIP2 cannam@89: bz_stream bstream; /* bzLib stream structure for bziped */ cannam@89: #endif cannam@89: cannam@89: ZPOS64_T pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ cannam@89: uLong stream_initialised; /* flag set if stream structure is initialised*/ cannam@89: cannam@89: ZPOS64_T offset_local_extrafield;/* offset of the local extra field */ cannam@89: uInt size_local_extrafield;/* size of the local extra field */ cannam@89: ZPOS64_T pos_local_extrafield; /* position in the local extra field in read*/ cannam@89: ZPOS64_T total_out_64; cannam@89: cannam@89: uLong crc32; /* crc32 of all data uncompressed */ cannam@89: uLong crc32_wait; /* crc32 we must obtain after decompress all */ cannam@89: ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */ cannam@89: ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/ cannam@89: zlib_filefunc64_32_def z_filefunc; cannam@89: voidpf filestream; /* io structore of the zipfile */ cannam@89: uLong compression_method; /* compression method (0==store) */ cannam@89: ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ cannam@89: int raw; cannam@89: } file_in_zip64_read_info_s; cannam@89: cannam@89: cannam@89: /* unz64_s contain internal information about the zipfile cannam@89: */ cannam@89: typedef struct cannam@89: { cannam@89: zlib_filefunc64_32_def z_filefunc; cannam@89: int is64bitOpenFunction; cannam@89: voidpf filestream; /* io structore of the zipfile */ cannam@89: unz_global_info64 gi; /* public global information */ cannam@89: ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ cannam@89: ZPOS64_T num_file; /* number of the current file in the zipfile*/ cannam@89: ZPOS64_T pos_in_central_dir; /* pos of the current file in the central dir*/ cannam@89: ZPOS64_T current_file_ok; /* flag about the usability of the current file*/ cannam@89: ZPOS64_T central_pos; /* position of the beginning of the central dir*/ cannam@89: cannam@89: ZPOS64_T size_central_dir; /* size of the central directory */ cannam@89: ZPOS64_T offset_central_dir; /* offset of start of central directory with cannam@89: respect to the starting disk number */ cannam@89: cannam@89: unz_file_info64 cur_file_info; /* public info about the current file in zip*/ cannam@89: unz_file_info64_internal cur_file_info_internal; /* private info about it*/ cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read; /* structure about the current cannam@89: file if we are decompressing it */ cannam@89: int encrypted; cannam@89: cannam@89: int isZip64; cannam@89: cannam@89: # ifndef NOUNCRYPT cannam@89: unsigned long keys[3]; /* keys defining the pseudo-random sequence */ cannam@89: const unsigned long* pcrc_32_tab; cannam@89: # endif cannam@89: } unz64_s; cannam@89: cannam@89: cannam@89: #ifndef NOUNCRYPT cannam@89: #include "crypt.h" cannam@89: #endif cannam@89: cannam@89: /* =========================================================================== cannam@89: Read a byte from a gz_stream; update next_in and avail_in. Return EOF cannam@89: for end of file. cannam@89: IN assertion: the stream s has been sucessfully opened for reading. cannam@89: */ cannam@89: cannam@89: cannam@89: local int unz64local_getByte OF(( cannam@89: const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream, cannam@89: int *pi)); cannam@89: cannam@89: local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi) cannam@89: { cannam@89: unsigned char c; cannam@89: int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1); cannam@89: if (err==1) cannam@89: { cannam@89: *pi = (int)c; cannam@89: return UNZ_OK; cannam@89: } cannam@89: else cannam@89: { cannam@89: if (ZERROR64(*pzlib_filefunc_def,filestream)) cannam@89: return UNZ_ERRNO; cannam@89: else cannam@89: return UNZ_EOF; cannam@89: } cannam@89: } cannam@89: cannam@89: cannam@89: /* =========================================================================== cannam@89: Reads a long in LSB order from the given gz_stream. Sets cannam@89: */ cannam@89: local int unz64local_getShort OF(( cannam@89: const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream, cannam@89: uLong *pX)); cannam@89: cannam@89: local int unz64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream, cannam@89: uLong *pX) cannam@89: { cannam@89: uLong x ; cannam@89: int i = 0; cannam@89: int err; cannam@89: cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x = (uLong)i; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((uLong)i)<<8; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: *pX = x; cannam@89: else cannam@89: *pX = 0; cannam@89: return err; cannam@89: } cannam@89: cannam@89: local int unz64local_getLong OF(( cannam@89: const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream, cannam@89: uLong *pX)); cannam@89: cannam@89: local int unz64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream, cannam@89: uLong *pX) cannam@89: { cannam@89: uLong x ; cannam@89: int i = 0; cannam@89: int err; cannam@89: cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x = (uLong)i; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((uLong)i)<<8; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((uLong)i)<<16; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x += ((uLong)i)<<24; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: *pX = x; cannam@89: else cannam@89: *pX = 0; cannam@89: return err; cannam@89: } cannam@89: cannam@89: local int unz64local_getLong64 OF(( cannam@89: const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream, cannam@89: ZPOS64_T *pX)); cannam@89: cannam@89: cannam@89: local int unz64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream, cannam@89: ZPOS64_T *pX) cannam@89: { cannam@89: ZPOS64_T x ; cannam@89: int i = 0; cannam@89: int err; cannam@89: cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x = (ZPOS64_T)i; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((ZPOS64_T)i)<<8; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((ZPOS64_T)i)<<16; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((ZPOS64_T)i)<<24; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((ZPOS64_T)i)<<32; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((ZPOS64_T)i)<<40; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((ZPOS64_T)i)<<48; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: err = unz64local_getByte(pzlib_filefunc_def,filestream,&i); cannam@89: x |= ((ZPOS64_T)i)<<56; cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: *pX = x; cannam@89: else cannam@89: *pX = 0; cannam@89: return err; cannam@89: } cannam@89: cannam@89: /* My own strcmpi / strcasecmp */ cannam@89: local int strcmpcasenosensitive_internal (const char* fileName1, const char* fileName2) cannam@89: { cannam@89: for (;;) cannam@89: { cannam@89: char c1=*(fileName1++); cannam@89: char c2=*(fileName2++); cannam@89: if ((c1>='a') && (c1<='z')) cannam@89: c1 -= 0x20; cannam@89: if ((c2>='a') && (c2<='z')) cannam@89: c2 -= 0x20; cannam@89: if (c1=='\0') cannam@89: return ((c2=='\0') ? 0 : -1); cannam@89: if (c2=='\0') cannam@89: return 1; cannam@89: if (c1c2) cannam@89: return 1; cannam@89: } cannam@89: } cannam@89: cannam@89: cannam@89: #ifdef CASESENSITIVITYDEFAULT_NO cannam@89: #define CASESENSITIVITYDEFAULTVALUE 2 cannam@89: #else cannam@89: #define CASESENSITIVITYDEFAULTVALUE 1 cannam@89: #endif cannam@89: cannam@89: #ifndef STRCMPCASENOSENTIVEFUNCTION cannam@89: #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal cannam@89: #endif cannam@89: cannam@89: /* cannam@89: Compare two filename (fileName1,fileName2). cannam@89: If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) cannam@89: If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi cannam@89: or strcasecmp) cannam@89: If iCaseSenisivity = 0, case sensitivity is defaut of your operating system cannam@89: (like 1 on Unix, 2 on Windows) cannam@89: cannam@89: */ cannam@89: extern int ZEXPORT unzStringFileNameCompare (const char* fileName1, cannam@89: const char* fileName2, cannam@89: int iCaseSensitivity) cannam@89: cannam@89: { cannam@89: if (iCaseSensitivity==0) cannam@89: iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE; cannam@89: cannam@89: if (iCaseSensitivity==1) cannam@89: return strcmp(fileName1,fileName2); cannam@89: cannam@89: return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2); cannam@89: } cannam@89: cannam@89: #ifndef BUFREADCOMMENT cannam@89: #define BUFREADCOMMENT (0x400) cannam@89: #endif cannam@89: cannam@89: /* cannam@89: Locate the Central directory of a zipfile (at the end, just before cannam@89: the global comment) cannam@89: */ cannam@89: local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); cannam@89: local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) cannam@89: { cannam@89: unsigned char* buf; cannam@89: ZPOS64_T uSizeFile; cannam@89: ZPOS64_T uBackRead; cannam@89: ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ cannam@89: ZPOS64_T uPosFound=0; cannam@89: cannam@89: if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) cannam@89: return 0; cannam@89: cannam@89: cannam@89: uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); cannam@89: cannam@89: if (uMaxBack>uSizeFile) cannam@89: uMaxBack = uSizeFile; cannam@89: cannam@89: buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); cannam@89: if (buf==NULL) cannam@89: return 0; cannam@89: cannam@89: uBackRead = 4; cannam@89: while (uBackReaduMaxBack) cannam@89: uBackRead = uMaxBack; cannam@89: else cannam@89: uBackRead+=BUFREADCOMMENT; cannam@89: uReadPos = uSizeFile-uBackRead ; cannam@89: cannam@89: uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? cannam@89: (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); cannam@89: if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: break; cannam@89: cannam@89: if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) cannam@89: break; cannam@89: cannam@89: for (i=(int)uReadSize-3; (i--)>0;) cannam@89: if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && cannam@89: ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) cannam@89: { cannam@89: uPosFound = uReadPos+i; cannam@89: break; cannam@89: } cannam@89: cannam@89: if (uPosFound!=0) cannam@89: break; cannam@89: } cannam@89: TRYFREE(buf); cannam@89: return uPosFound; cannam@89: } cannam@89: cannam@89: cannam@89: /* cannam@89: Locate the Central directory 64 of a zipfile (at the end, just before cannam@89: the global comment) cannam@89: */ cannam@89: local ZPOS64_T unz64local_SearchCentralDir64 OF(( cannam@89: const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream)); cannam@89: cannam@89: local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, cannam@89: voidpf filestream) cannam@89: { cannam@89: unsigned char* buf; cannam@89: ZPOS64_T uSizeFile; cannam@89: ZPOS64_T uBackRead; cannam@89: ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ cannam@89: ZPOS64_T uPosFound=0; cannam@89: uLong uL; cannam@89: ZPOS64_T relativeOffset; cannam@89: cannam@89: if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) cannam@89: return 0; cannam@89: cannam@89: cannam@89: uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream); cannam@89: cannam@89: if (uMaxBack>uSizeFile) cannam@89: uMaxBack = uSizeFile; cannam@89: cannam@89: buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); cannam@89: if (buf==NULL) cannam@89: return 0; cannam@89: cannam@89: uBackRead = 4; cannam@89: while (uBackReaduMaxBack) cannam@89: uBackRead = uMaxBack; cannam@89: else cannam@89: uBackRead+=BUFREADCOMMENT; cannam@89: uReadPos = uSizeFile-uBackRead ; cannam@89: cannam@89: uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? cannam@89: (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos); cannam@89: if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: break; cannam@89: cannam@89: if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) cannam@89: break; cannam@89: cannam@89: for (i=(int)uReadSize-3; (i--)>0;) cannam@89: if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && cannam@89: ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07)) cannam@89: { cannam@89: uPosFound = uReadPos+i; cannam@89: break; cannam@89: } cannam@89: cannam@89: if (uPosFound!=0) cannam@89: break; cannam@89: } cannam@89: TRYFREE(buf); cannam@89: if (uPosFound == 0) cannam@89: return 0; cannam@89: cannam@89: /* Zip64 end of central directory locator */ cannam@89: if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: return 0; cannam@89: cannam@89: /* the signature, already checked */ cannam@89: if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) cannam@89: return 0; cannam@89: cannam@89: /* number of the disk with the start of the zip64 end of central directory */ cannam@89: if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) cannam@89: return 0; cannam@89: if (uL != 0) cannam@89: return 0; cannam@89: cannam@89: /* relative offset of the zip64 end of central directory record */ cannam@89: if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK) cannam@89: return 0; cannam@89: cannam@89: /* total number of disks */ cannam@89: if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) cannam@89: return 0; cannam@89: if (uL != 1) cannam@89: return 0; cannam@89: cannam@89: /* Goto end of central directory record */ cannam@89: if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: return 0; cannam@89: cannam@89: /* the signature */ cannam@89: if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK) cannam@89: return 0; cannam@89: cannam@89: if (uL != 0x06064b50) cannam@89: return 0; cannam@89: cannam@89: return relativeOffset; cannam@89: } cannam@89: cannam@89: /* cannam@89: Open a Zip file. path contain the full pathname (by example, cannam@89: on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer cannam@89: "zlib/zlib114.zip". cannam@89: If the zipfile cannot be opened (file doesn't exist or in not valid), the cannam@89: return value is NULL. cannam@89: Else, the return value is a unzFile Handle, usable with other function cannam@89: of this unzip package. cannam@89: */ cannam@89: local unzFile unzOpenInternal (const void *path, cannam@89: zlib_filefunc64_32_def* pzlib_filefunc64_32_def, cannam@89: int is64bitOpenFunction) cannam@89: { cannam@89: unz64_s us; cannam@89: unz64_s *s; cannam@89: ZPOS64_T central_pos; cannam@89: uLong uL; cannam@89: cannam@89: uLong number_disk; /* number of the current dist, used for cannam@89: spaning ZIP, unsupported, always 0*/ cannam@89: uLong number_disk_with_CD; /* number the the disk with central dir, used cannam@89: for spaning ZIP, unsupported, always 0*/ cannam@89: ZPOS64_T number_entry_CD; /* total number of entries in cannam@89: the central dir cannam@89: (same than number_entry on nospan) */ cannam@89: cannam@89: int err=UNZ_OK; cannam@89: cannam@89: if (unz_copyright[0]!=' ') cannam@89: return NULL; cannam@89: cannam@89: us.z_filefunc.zseek32_file = NULL; cannam@89: us.z_filefunc.ztell32_file = NULL; cannam@89: if (pzlib_filefunc64_32_def==NULL) cannam@89: fill_fopen64_filefunc(&us.z_filefunc.zfile_func64); cannam@89: else cannam@89: us.z_filefunc = *pzlib_filefunc64_32_def; cannam@89: us.is64bitOpenFunction = is64bitOpenFunction; cannam@89: cannam@89: cannam@89: cannam@89: us.filestream = ZOPEN64(us.z_filefunc, cannam@89: path, cannam@89: ZLIB_FILEFUNC_MODE_READ | cannam@89: ZLIB_FILEFUNC_MODE_EXISTING); cannam@89: if (us.filestream==NULL) cannam@89: return NULL; cannam@89: cannam@89: central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream); cannam@89: if (central_pos) cannam@89: { cannam@89: uLong uS; cannam@89: ZPOS64_T uL64; cannam@89: cannam@89: us.isZip64 = 1; cannam@89: cannam@89: if (ZSEEK64(us.z_filefunc, us.filestream, cannam@89: central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* the signature, already checked */ cannam@89: if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* size of zip64 end of central directory record */ cannam@89: if (unz64local_getLong64(&us.z_filefunc, us.filestream,&uL64)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* version made by */ cannam@89: if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* version needed to extract */ cannam@89: if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* number of this disk */ cannam@89: if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* number of the disk with the start of the central directory */ cannam@89: if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* total number of entries in the central directory on this disk */ cannam@89: if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* total number of entries in the central directory */ cannam@89: if (unz64local_getLong64(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if ((number_entry_CD!=us.gi.number_entry) || cannam@89: (number_disk_with_CD!=0) || cannam@89: (number_disk!=0)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: /* size of the central directory */ cannam@89: if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* offset of start of central directory with respect to the cannam@89: starting disk number */ cannam@89: if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: us.gi.size_comment = 0; cannam@89: } cannam@89: else cannam@89: { cannam@89: central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream); cannam@89: if (central_pos==0) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: us.isZip64 = 0; cannam@89: cannam@89: if (ZSEEK64(us.z_filefunc, us.filestream, cannam@89: central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* the signature, already checked */ cannam@89: if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* number of this disk */ cannam@89: if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* number of the disk with the start of the central directory */ cannam@89: if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* total number of entries in the central dir on this disk */ cannam@89: if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: us.gi.number_entry = uL; cannam@89: cannam@89: /* total number of entries in the central dir */ cannam@89: if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: number_entry_CD = uL; cannam@89: cannam@89: if ((number_entry_CD!=us.gi.number_entry) || cannam@89: (number_disk_with_CD!=0) || cannam@89: (number_disk!=0)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: /* size of the central directory */ cannam@89: if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: us.size_central_dir = uL; cannam@89: cannam@89: /* offset of start of central directory with respect to the cannam@89: starting disk number */ cannam@89: if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: us.offset_central_dir = uL; cannam@89: cannam@89: /* zipfile comment length */ cannam@89: if (unz64local_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: if ((central_pospfile_in_zip_read!=NULL) cannam@89: unzCloseCurrentFile(file); cannam@89: cannam@89: ZCLOSE64(s->z_filefunc, s->filestream); cannam@89: TRYFREE(s); cannam@89: return UNZ_OK; cannam@89: } cannam@89: cannam@89: cannam@89: /* cannam@89: Write info about the ZipFile in the *pglobal_info structure. cannam@89: No preparation of the structure is needed cannam@89: return UNZ_OK if there is no problem. */ cannam@89: extern int ZEXPORT unzGetGlobalInfo64 (unzFile file, unz_global_info64* pglobal_info) cannam@89: { cannam@89: unz64_s* s; cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: *pglobal_info=s->gi; cannam@89: return UNZ_OK; cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzGetGlobalInfo (unzFile file, unz_global_info* pglobal_info32) cannam@89: { cannam@89: unz64_s* s; cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: /* to do : check if number_entry is not truncated */ cannam@89: pglobal_info32->number_entry = (uLong)s->gi.number_entry; cannam@89: pglobal_info32->size_comment = s->gi.size_comment; cannam@89: return UNZ_OK; cannam@89: } cannam@89: /* cannam@89: Translate date/time from Dos format to tm_unz (readable more easilty) cannam@89: */ cannam@89: local void unz64local_DosDateToTmuDate (ZPOS64_T ulDosDate, tm_unz* ptm) cannam@89: { cannam@89: ZPOS64_T uDate; cannam@89: uDate = (ZPOS64_T)(ulDosDate>>16); cannam@89: ptm->tm_mday = (uInt)(uDate&0x1f) ; cannam@89: ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; cannam@89: ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; cannam@89: cannam@89: ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); cannam@89: ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; cannam@89: ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; cannam@89: } cannam@89: cannam@89: /* cannam@89: Get Info about the current file in the zipfile, with internal only info cannam@89: */ cannam@89: local int unz64local_GetCurrentFileInfoInternal OF((unzFile file, cannam@89: unz_file_info64 *pfile_info, cannam@89: unz_file_info64_internal cannam@89: *pfile_info_internal, cannam@89: char *szFileName, cannam@89: uLong fileNameBufferSize, cannam@89: void *extraField, cannam@89: uLong extraFieldBufferSize, cannam@89: char *szComment, cannam@89: uLong commentBufferSize)); cannam@89: cannam@89: local int unz64local_GetCurrentFileInfoInternal (unzFile file, cannam@89: unz_file_info64 *pfile_info, cannam@89: unz_file_info64_internal cannam@89: *pfile_info_internal, cannam@89: char *szFileName, cannam@89: uLong fileNameBufferSize, cannam@89: void *extraField, cannam@89: uLong extraFieldBufferSize, cannam@89: char *szComment, cannam@89: uLong commentBufferSize) cannam@89: { cannam@89: unz64_s* s; cannam@89: unz_file_info64 file_info; cannam@89: unz_file_info64_internal file_info_internal; cannam@89: int err=UNZ_OK; cannam@89: uLong uMagic; cannam@89: long lSeek=0; cannam@89: uLong uL; cannam@89: cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: if (ZSEEK64(s->z_filefunc, s->filestream, cannam@89: s->pos_in_central_dir+s->byte_before_the_zipfile, cannam@89: ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: cannam@89: /* we check the magic */ cannam@89: if (err==UNZ_OK) cannam@89: { cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: else if (uMagic!=0x02014b50) cannam@89: err=UNZ_BADZIPFILE; cannam@89: } cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: unz64local_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date); cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: file_info.compressed_size = uL; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: file_info.uncompressed_size = uL; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: // relative offset of local header cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: file_info_internal.offset_curfile = uL; cannam@89: cannam@89: lSeek+=file_info.size_filename; cannam@89: if ((err==UNZ_OK) && (szFileName!=NULL)) cannam@89: { cannam@89: uLong uSizeRead ; cannam@89: if (file_info.size_filename0) && (fileNameBufferSize>0)) cannam@89: if (ZREAD64(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead) cannam@89: err=UNZ_ERRNO; cannam@89: lSeek -= uSizeRead; cannam@89: } cannam@89: cannam@89: // Read extrafield cannam@89: if ((err==UNZ_OK) && (extraField!=NULL)) cannam@89: { cannam@89: ZPOS64_T uSizeRead ; cannam@89: if (file_info.size_file_extraz_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) cannam@89: lSeek=0; cannam@89: else cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) cannam@89: if (ZREAD64(s->z_filefunc, s->filestream,extraField,(uLong)uSizeRead)!=uSizeRead) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: lSeek += file_info.size_file_extra - (uLong)uSizeRead; cannam@89: } cannam@89: else cannam@89: lSeek += file_info.size_file_extra; cannam@89: cannam@89: cannam@89: if ((err==UNZ_OK) && (file_info.size_file_extra != 0)) cannam@89: { cannam@89: uLong acc = 0; cannam@89: cannam@89: // since lSeek now points to after the extra field we need to move back cannam@89: lSeek -= file_info.size_file_extra; cannam@89: cannam@89: if (lSeek!=0) cannam@89: { cannam@89: if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) cannam@89: lSeek=0; cannam@89: else cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: while(acc < file_info.size_file_extra) cannam@89: { cannam@89: uLong headerId; cannam@89: uLong dataSize; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&headerId) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&dataSize) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: /* ZIP64 extra fields */ cannam@89: if (headerId == 0x0001) cannam@89: { cannam@89: uLong uL; cannam@89: cannam@89: if(file_info.uncompressed_size == MAXU32) cannam@89: { cannam@89: if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: if(file_info.compressed_size == MAXU32) cannam@89: { cannam@89: if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: if(file_info_internal.offset_curfile == MAXU32) cannam@89: { cannam@89: /* Relative Header offset */ cannam@89: if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: if(file_info.disk_num_start == MAXU32) cannam@89: { cannam@89: /* Disk Start Number */ cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: } cannam@89: else cannam@89: { cannam@89: if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0) cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: acc += 2 + 2 + dataSize; cannam@89: } cannam@89: } cannam@89: cannam@89: if ((err==UNZ_OK) && (szComment!=NULL)) cannam@89: { cannam@89: uLong uSizeRead ; cannam@89: if (file_info.size_file_commentz_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) cannam@89: lSeek=0; cannam@89: else cannam@89: err=UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: if ((file_info.size_file_comment>0) && (commentBufferSize>0)) cannam@89: if (ZREAD64(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead) cannam@89: err=UNZ_ERRNO; cannam@89: lSeek+=file_info.size_file_comment - uSizeRead; cannam@89: } cannam@89: else cannam@89: lSeek+=file_info.size_file_comment; cannam@89: cannam@89: cannam@89: if ((err==UNZ_OK) && (pfile_info!=NULL)) cannam@89: *pfile_info=file_info; cannam@89: cannam@89: if ((err==UNZ_OK) && (pfile_info_internal!=NULL)) cannam@89: *pfile_info_internal=file_info_internal; cannam@89: cannam@89: return err; cannam@89: } cannam@89: cannam@89: cannam@89: cannam@89: /* cannam@89: Write info about the ZipFile in the *pglobal_info structure. cannam@89: No preparation of the structure is needed cannam@89: return UNZ_OK if there is no problem. cannam@89: */ cannam@89: extern int ZEXPORT unzGetCurrentFileInfo64 (unzFile file, cannam@89: unz_file_info64 * pfile_info, cannam@89: char * szFileName, uLong fileNameBufferSize, cannam@89: void *extraField, uLong extraFieldBufferSize, cannam@89: char* szComment, uLong commentBufferSize) cannam@89: { cannam@89: return unz64local_GetCurrentFileInfoInternal(file,pfile_info,NULL, cannam@89: szFileName,fileNameBufferSize, cannam@89: extraField,extraFieldBufferSize, cannam@89: szComment,commentBufferSize); cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzGetCurrentFileInfo (unzFile file, cannam@89: unz_file_info * pfile_info, cannam@89: char * szFileName, uLong fileNameBufferSize, cannam@89: void *extraField, uLong extraFieldBufferSize, cannam@89: char* szComment, uLong commentBufferSize) cannam@89: { cannam@89: int err; cannam@89: unz_file_info64 file_info64; cannam@89: err = unz64local_GetCurrentFileInfoInternal(file,&file_info64,NULL, cannam@89: szFileName,fileNameBufferSize, cannam@89: extraField,extraFieldBufferSize, cannam@89: szComment,commentBufferSize); cannam@89: if ((err==UNZ_OK) && (pfile_info != NULL)) cannam@89: { cannam@89: pfile_info->version = file_info64.version; cannam@89: pfile_info->version_needed = file_info64.version_needed; cannam@89: pfile_info->flag = file_info64.flag; cannam@89: pfile_info->compression_method = file_info64.compression_method; cannam@89: pfile_info->dosDate = file_info64.dosDate; cannam@89: pfile_info->crc = file_info64.crc; cannam@89: cannam@89: pfile_info->size_filename = file_info64.size_filename; cannam@89: pfile_info->size_file_extra = file_info64.size_file_extra; cannam@89: pfile_info->size_file_comment = file_info64.size_file_comment; cannam@89: cannam@89: pfile_info->disk_num_start = file_info64.disk_num_start; cannam@89: pfile_info->internal_fa = file_info64.internal_fa; cannam@89: pfile_info->external_fa = file_info64.external_fa; cannam@89: cannam@89: pfile_info->tmu_date = file_info64.tmu_date, cannam@89: cannam@89: cannam@89: pfile_info->compressed_size = (uLong)file_info64.compressed_size; cannam@89: pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size; cannam@89: cannam@89: } cannam@89: return err; cannam@89: } cannam@89: /* cannam@89: Set the current file of the zipfile to the first file. cannam@89: return UNZ_OK if there is no problem cannam@89: */ cannam@89: extern int ZEXPORT unzGoToFirstFile (unzFile file) cannam@89: { cannam@89: int err=UNZ_OK; cannam@89: unz64_s* s; cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: s->pos_in_central_dir=s->offset_central_dir; cannam@89: s->num_file=0; cannam@89: err=unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info, cannam@89: &s->cur_file_info_internal, cannam@89: NULL,0,NULL,0,NULL,0); cannam@89: s->current_file_ok = (err == UNZ_OK); cannam@89: return err; cannam@89: } cannam@89: cannam@89: /* cannam@89: Set the current file of the zipfile to the next file. cannam@89: return UNZ_OK if there is no problem cannam@89: return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. cannam@89: */ cannam@89: extern int ZEXPORT unzGoToNextFile (unzFile file) cannam@89: { cannam@89: unz64_s* s; cannam@89: int err; cannam@89: cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: if (!s->current_file_ok) cannam@89: return UNZ_END_OF_LIST_OF_FILE; cannam@89: if (s->gi.number_entry != 0xffff) /* 2^16 files overflow hack */ cannam@89: if (s->num_file+1==s->gi.number_entry) cannam@89: return UNZ_END_OF_LIST_OF_FILE; cannam@89: cannam@89: s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + cannam@89: s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; cannam@89: s->num_file++; cannam@89: err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info, cannam@89: &s->cur_file_info_internal, cannam@89: NULL,0,NULL,0,NULL,0); cannam@89: s->current_file_ok = (err == UNZ_OK); cannam@89: return err; cannam@89: } cannam@89: cannam@89: cannam@89: /* cannam@89: Try locate the file szFileName in the zipfile. cannam@89: For the iCaseSensitivity signification, see unzipStringFileNameCompare cannam@89: cannam@89: return value : cannam@89: UNZ_OK if the file is found. It becomes the current file. cannam@89: UNZ_END_OF_LIST_OF_FILE if the file is not found cannam@89: */ cannam@89: extern int ZEXPORT unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity) cannam@89: { cannam@89: unz64_s* s; cannam@89: int err; cannam@89: cannam@89: /* We remember the 'current' position in the file so that we can jump cannam@89: * back there if we fail. cannam@89: */ cannam@89: unz_file_info64 cur_file_infoSaved; cannam@89: unz_file_info64_internal cur_file_info_internalSaved; cannam@89: ZPOS64_T num_fileSaved; cannam@89: ZPOS64_T pos_in_central_dirSaved; cannam@89: cannam@89: cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: s=(unz64_s*)file; cannam@89: if (!s->current_file_ok) cannam@89: return UNZ_END_OF_LIST_OF_FILE; cannam@89: cannam@89: /* Save the current state */ cannam@89: num_fileSaved = s->num_file; cannam@89: pos_in_central_dirSaved = s->pos_in_central_dir; cannam@89: cur_file_infoSaved = s->cur_file_info; cannam@89: cur_file_info_internalSaved = s->cur_file_info_internal; cannam@89: cannam@89: err = unzGoToFirstFile(file); cannam@89: cannam@89: while (err == UNZ_OK) cannam@89: { cannam@89: char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; cannam@89: err = unzGetCurrentFileInfo64(file,NULL, cannam@89: szCurrentFileName,sizeof(szCurrentFileName)-1, cannam@89: NULL,0,NULL,0); cannam@89: if (err == UNZ_OK) cannam@89: { cannam@89: if (unzStringFileNameCompare(szCurrentFileName, cannam@89: szFileName,iCaseSensitivity)==0) cannam@89: return UNZ_OK; cannam@89: err = unzGoToNextFile(file); cannam@89: } cannam@89: } cannam@89: cannam@89: /* We failed, so restore the state of the 'current file' to where we cannam@89: * were. cannam@89: */ cannam@89: s->num_file = num_fileSaved ; cannam@89: s->pos_in_central_dir = pos_in_central_dirSaved ; cannam@89: s->cur_file_info = cur_file_infoSaved; cannam@89: s->cur_file_info_internal = cur_file_info_internalSaved; cannam@89: return err; cannam@89: } cannam@89: cannam@89: cannam@89: /* cannam@89: /////////////////////////////////////////// cannam@89: // Contributed by Ryan Haksi (mailto://cryogen@infoserve.net) cannam@89: // I need random access cannam@89: // cannam@89: // Further optimization could be realized by adding an ability cannam@89: // to cache the directory in memory. The goal being a single cannam@89: // comprehensive file read to put the file I need in a memory. cannam@89: */ cannam@89: cannam@89: /* cannam@89: typedef struct unz_file_pos_s cannam@89: { cannam@89: ZPOS64_T pos_in_zip_directory; // offset in file cannam@89: ZPOS64_T num_of_file; // # of file cannam@89: } unz_file_pos; cannam@89: */ cannam@89: cannam@89: extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos* file_pos) cannam@89: { cannam@89: unz64_s* s; cannam@89: cannam@89: if (file==NULL || file_pos==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: if (!s->current_file_ok) cannam@89: return UNZ_END_OF_LIST_OF_FILE; cannam@89: cannam@89: file_pos->pos_in_zip_directory = s->pos_in_central_dir; cannam@89: file_pos->num_of_file = s->num_file; cannam@89: cannam@89: return UNZ_OK; cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzGetFilePos( cannam@89: unzFile file, cannam@89: unz_file_pos* file_pos) cannam@89: { cannam@89: unz64_file_pos file_pos64; cannam@89: int err = unzGetFilePos64(file,&file_pos64); cannam@89: if (err==UNZ_OK) cannam@89: { cannam@89: file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory; cannam@89: file_pos->num_of_file = (uLong)file_pos64.num_of_file; cannam@89: } cannam@89: return err; cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos) cannam@89: { cannam@89: unz64_s* s; cannam@89: int err; cannam@89: cannam@89: if (file==NULL || file_pos==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: cannam@89: /* jump to the right spot */ cannam@89: s->pos_in_central_dir = file_pos->pos_in_zip_directory; cannam@89: s->num_file = file_pos->num_of_file; cannam@89: cannam@89: /* set the current file */ cannam@89: err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info, cannam@89: &s->cur_file_info_internal, cannam@89: NULL,0,NULL,0,NULL,0); cannam@89: /* return results */ cannam@89: s->current_file_ok = (err == UNZ_OK); cannam@89: return err; cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzGoToFilePos( cannam@89: unzFile file, cannam@89: unz_file_pos* file_pos) cannam@89: { cannam@89: unz64_file_pos file_pos64; cannam@89: if (file_pos == NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory; cannam@89: file_pos64.num_of_file = file_pos->num_of_file; cannam@89: return unzGoToFilePos64(file,&file_pos64); cannam@89: } cannam@89: cannam@89: /* cannam@89: // Unzip Helper Functions - should be here? cannam@89: /////////////////////////////////////////// cannam@89: */ cannam@89: cannam@89: /* cannam@89: Read the local header of the current zipfile cannam@89: Check the coherency of the local header and info in the end of central cannam@89: directory about this file cannam@89: store in *piSizeVar the size of extra info in local header cannam@89: (filename and size of extra field data) cannam@89: */ cannam@89: local int unz64local_CheckCurrentFileCoherencyHeader (unz64_s* s, uInt* piSizeVar, cannam@89: ZPOS64_T * poffset_local_extrafield, cannam@89: uInt * psize_local_extrafield) cannam@89: { cannam@89: uLong uMagic,uData,uFlags; cannam@89: uLong size_filename; cannam@89: uLong size_extra_field; cannam@89: int err=UNZ_OK; cannam@89: cannam@89: *piSizeVar = 0; cannam@89: *poffset_local_extrafield = 0; cannam@89: *psize_local_extrafield = 0; cannam@89: cannam@89: if (ZSEEK64(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile + cannam@89: s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: return UNZ_ERRNO; cannam@89: cannam@89: cannam@89: if (err==UNZ_OK) cannam@89: { cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: else if (uMagic!=0x04034b50) cannam@89: err=UNZ_BADZIPFILE; cannam@89: } cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: /* cannam@89: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: */ cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) && cannam@89: /* #ifdef HAVE_BZIP2 */ cannam@89: (s->cur_file_info.compression_method!=Z_BZIP2ED) && cannam@89: /* #endif */ cannam@89: (s->cur_file_info.compression_method!=Z_DEFLATED)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */ cannam@89: err=UNZ_ERRNO; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */ cannam@89: err=UNZ_ERRNO; cannam@89: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && ((uFlags & 8)==0)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */ cannam@89: err=UNZ_ERRNO; cannam@89: else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && ((uFlags & 8)==0)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */ cannam@89: err=UNZ_ERRNO; cannam@89: else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && ((uFlags & 8)==0)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename)) cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: *piSizeVar += (uInt)size_filename; cannam@89: cannam@89: if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK) cannam@89: err=UNZ_ERRNO; cannam@89: *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile + cannam@89: SIZEZIPLOCALHEADER + size_filename; cannam@89: *psize_local_extrafield = (uInt)size_extra_field; cannam@89: cannam@89: *piSizeVar += (uInt)size_extra_field; cannam@89: cannam@89: return err; cannam@89: } cannam@89: cannam@89: /* cannam@89: Open for reading data the current file in the zipfile. cannam@89: If there is no error and the file is opened, the return value is UNZ_OK. cannam@89: */ cannam@89: extern int ZEXPORT unzOpenCurrentFile3 (unzFile file, int* method, cannam@89: int* level, int raw, const char* password) cannam@89: { cannam@89: int err=UNZ_OK; cannam@89: uInt iSizeVar; cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: ZPOS64_T offset_local_extrafield; /* offset of the local extra field */ cannam@89: uInt size_local_extrafield; /* size of the local extra field */ cannam@89: # ifndef NOUNCRYPT cannam@89: char source[12]; cannam@89: # else cannam@89: if (password != NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: # endif cannam@89: cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: if (!s->current_file_ok) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: if (s->pfile_in_zip_read != NULL) cannam@89: unzCloseCurrentFile(file); cannam@89: cannam@89: if (unz64local_CheckCurrentFileCoherencyHeader(s,&iSizeVar, &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK) cannam@89: return UNZ_BADZIPFILE; cannam@89: cannam@89: pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s)); cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return UNZ_INTERNALERROR; cannam@89: cannam@89: pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE); cannam@89: pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; cannam@89: pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; cannam@89: pfile_in_zip_read_info->pos_local_extrafield=0; cannam@89: pfile_in_zip_read_info->raw=raw; cannam@89: cannam@89: if (pfile_in_zip_read_info->read_buffer==NULL) cannam@89: { cannam@89: TRYFREE(pfile_in_zip_read_info); cannam@89: return UNZ_INTERNALERROR; cannam@89: } cannam@89: cannam@89: pfile_in_zip_read_info->stream_initialised=0; cannam@89: cannam@89: if (method!=NULL) cannam@89: *method = (int)s->cur_file_info.compression_method; cannam@89: cannam@89: if (level!=NULL) cannam@89: { cannam@89: *level = 6; cannam@89: switch (s->cur_file_info.flag & 0x06) cannam@89: { cannam@89: case 6 : *level = 1; break; cannam@89: case 4 : *level = 2; break; cannam@89: case 2 : *level = 9; break; cannam@89: } cannam@89: } cannam@89: cannam@89: if ((s->cur_file_info.compression_method!=0) && cannam@89: /* #ifdef HAVE_BZIP2 */ cannam@89: (s->cur_file_info.compression_method!=Z_BZIP2ED) && cannam@89: /* #endif */ cannam@89: (s->cur_file_info.compression_method!=Z_DEFLATED)) cannam@89: cannam@89: err=UNZ_BADZIPFILE; cannam@89: cannam@89: pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc; cannam@89: pfile_in_zip_read_info->crc32=0; cannam@89: pfile_in_zip_read_info->total_out_64=0; cannam@89: pfile_in_zip_read_info->compression_method = s->cur_file_info.compression_method; cannam@89: pfile_in_zip_read_info->filestream=s->filestream; cannam@89: pfile_in_zip_read_info->z_filefunc=s->z_filefunc; cannam@89: pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile; cannam@89: cannam@89: pfile_in_zip_read_info->stream.total_out = 0; cannam@89: cannam@89: if ((s->cur_file_info.compression_method==Z_BZIP2ED) && (!raw)) cannam@89: { cannam@89: #ifdef HAVE_BZIP2 cannam@89: pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0; cannam@89: pfile_in_zip_read_info->bstream.bzfree = (free_func)0; cannam@89: pfile_in_zip_read_info->bstream.opaque = (voidpf)0; cannam@89: pfile_in_zip_read_info->bstream.state = (voidpf)0; cannam@89: cannam@89: pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; cannam@89: pfile_in_zip_read_info->stream.zfree = (free_func)0; cannam@89: pfile_in_zip_read_info->stream.opaque = (voidpf)0; cannam@89: pfile_in_zip_read_info->stream.next_in = (voidpf)0; cannam@89: pfile_in_zip_read_info->stream.avail_in = 0; cannam@89: cannam@89: err=BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0); cannam@89: if (err == Z_OK) cannam@89: pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED; cannam@89: else cannam@89: { cannam@89: TRYFREE(pfile_in_zip_read_info); cannam@89: return err; cannam@89: } cannam@89: #else cannam@89: pfile_in_zip_read_info->raw=1; cannam@89: #endif cannam@89: } cannam@89: else if ((s->cur_file_info.compression_method==Z_DEFLATED) && (!raw)) cannam@89: { cannam@89: pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; cannam@89: pfile_in_zip_read_info->stream.zfree = (free_func)0; cannam@89: pfile_in_zip_read_info->stream.opaque = (voidpf)0; cannam@89: pfile_in_zip_read_info->stream.next_in = 0; cannam@89: pfile_in_zip_read_info->stream.avail_in = 0; cannam@89: cannam@89: err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); cannam@89: if (err == Z_OK) cannam@89: pfile_in_zip_read_info->stream_initialised=Z_DEFLATED; cannam@89: else cannam@89: { cannam@89: TRYFREE(pfile_in_zip_read_info); cannam@89: return err; cannam@89: } cannam@89: /* windowBits is passed < 0 to tell that there is no zlib header. cannam@89: * Note that in this case inflate *requires* an extra "dummy" byte cannam@89: * after the compressed stream in order to complete decompression and cannam@89: * return Z_STREAM_END. cannam@89: * In unzip, i don't wait absolutely Z_STREAM_END because I known the cannam@89: * size of both compressed and uncompressed data cannam@89: */ cannam@89: } cannam@89: pfile_in_zip_read_info->rest_read_compressed = cannam@89: s->cur_file_info.compressed_size ; cannam@89: pfile_in_zip_read_info->rest_read_uncompressed = cannam@89: s->cur_file_info.uncompressed_size ; cannam@89: cannam@89: cannam@89: pfile_in_zip_read_info->pos_in_zipfile = cannam@89: s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + cannam@89: iSizeVar; cannam@89: cannam@89: pfile_in_zip_read_info->stream.avail_in = (uInt)0; cannam@89: cannam@89: s->pfile_in_zip_read = pfile_in_zip_read_info; cannam@89: s->encrypted = 0; cannam@89: cannam@89: # ifndef NOUNCRYPT cannam@89: if (password != NULL) cannam@89: { cannam@89: int i; cannam@89: s->pcrc_32_tab = get_crc_table(); cannam@89: init_keys(password,s->keys,s->pcrc_32_tab); cannam@89: if (ZSEEK64(s->z_filefunc, s->filestream, cannam@89: s->pfile_in_zip_read->pos_in_zipfile + cannam@89: s->pfile_in_zip_read->byte_before_the_zipfile, cannam@89: SEEK_SET)!=0) cannam@89: return UNZ_INTERNALERROR; cannam@89: if(ZREAD64(s->z_filefunc, s->filestream,source, 12)<12) cannam@89: return UNZ_INTERNALERROR; cannam@89: cannam@89: for (i = 0; i<12; i++) cannam@89: zdecode(s->keys,s->pcrc_32_tab,source[i]); cannam@89: cannam@89: s->pfile_in_zip_read->pos_in_zipfile+=12; cannam@89: s->encrypted=1; cannam@89: } cannam@89: # endif cannam@89: cannam@89: cannam@89: return UNZ_OK; cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzOpenCurrentFile (unzFile file) cannam@89: { cannam@89: return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL); cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzOpenCurrentFilePassword (unzFile file, const char* password) cannam@89: { cannam@89: return unzOpenCurrentFile3(file, NULL, NULL, 0, password); cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzOpenCurrentFile2 (unzFile file, int* method, int* level, int raw) cannam@89: { cannam@89: return unzOpenCurrentFile3(file, method, level, raw, NULL); cannam@89: } cannam@89: cannam@89: /** Addition for GDAL : START */ cannam@89: cannam@89: extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64( unzFile file) cannam@89: { cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: s=(unz64_s*)file; cannam@89: if (file==NULL) cannam@89: return 0; //UNZ_PARAMERROR; cannam@89: pfile_in_zip_read_info=s->pfile_in_zip_read; cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return 0; //UNZ_PARAMERROR; cannam@89: return pfile_in_zip_read_info->pos_in_zipfile + cannam@89: pfile_in_zip_read_info->byte_before_the_zipfile; cannam@89: } cannam@89: cannam@89: /** Addition for GDAL : END */ cannam@89: cannam@89: /* cannam@89: Read bytes from the current file. cannam@89: buf contain buffer where data must be copied cannam@89: len the size of buf. cannam@89: cannam@89: return the number of byte copied if somes bytes are copied cannam@89: return 0 if the end of file was reached cannam@89: return <0 with error code if there is an error cannam@89: (UNZ_ERRNO for IO error, or zLib error for uncompress error) cannam@89: */ cannam@89: extern int ZEXPORT unzReadCurrentFile (unzFile file, voidp buf, unsigned len) cannam@89: { cannam@89: int err=UNZ_OK; cannam@89: uInt iRead = 0; cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: pfile_in_zip_read_info=s->pfile_in_zip_read; cannam@89: cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: cannam@89: if (pfile_in_zip_read_info->read_buffer == NULL) cannam@89: return UNZ_END_OF_LIST_OF_FILE; cannam@89: if (len==0) cannam@89: return 0; cannam@89: cannam@89: pfile_in_zip_read_info->stream.next_out = (Bytef*)buf; cannam@89: cannam@89: pfile_in_zip_read_info->stream.avail_out = (uInt)len; cannam@89: cannam@89: if ((len>pfile_in_zip_read_info->rest_read_uncompressed) && cannam@89: (!(pfile_in_zip_read_info->raw))) cannam@89: pfile_in_zip_read_info->stream.avail_out = cannam@89: (uInt)pfile_in_zip_read_info->rest_read_uncompressed; cannam@89: cannam@89: if ((len>pfile_in_zip_read_info->rest_read_compressed+ cannam@89: pfile_in_zip_read_info->stream.avail_in) && cannam@89: (pfile_in_zip_read_info->raw)) cannam@89: pfile_in_zip_read_info->stream.avail_out = cannam@89: (uInt)pfile_in_zip_read_info->rest_read_compressed+ cannam@89: pfile_in_zip_read_info->stream.avail_in; cannam@89: cannam@89: while (pfile_in_zip_read_info->stream.avail_out>0) cannam@89: { cannam@89: if ((pfile_in_zip_read_info->stream.avail_in==0) && cannam@89: (pfile_in_zip_read_info->rest_read_compressed>0)) cannam@89: { cannam@89: uInt uReadThis = UNZ_BUFSIZE; cannam@89: if (pfile_in_zip_read_info->rest_read_compressedrest_read_compressed; cannam@89: if (uReadThis == 0) cannam@89: return UNZ_EOF; cannam@89: if (ZSEEK64(pfile_in_zip_read_info->z_filefunc, cannam@89: pfile_in_zip_read_info->filestream, cannam@89: pfile_in_zip_read_info->pos_in_zipfile + cannam@89: pfile_in_zip_read_info->byte_before_the_zipfile, cannam@89: ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: return UNZ_ERRNO; cannam@89: if (ZREAD64(pfile_in_zip_read_info->z_filefunc, cannam@89: pfile_in_zip_read_info->filestream, cannam@89: pfile_in_zip_read_info->read_buffer, cannam@89: uReadThis)!=uReadThis) cannam@89: return UNZ_ERRNO; cannam@89: cannam@89: cannam@89: # ifndef NOUNCRYPT cannam@89: if(s->encrypted) cannam@89: { cannam@89: uInt i; cannam@89: for(i=0;iread_buffer[i] = cannam@89: zdecode(s->keys,s->pcrc_32_tab, cannam@89: pfile_in_zip_read_info->read_buffer[i]); cannam@89: } cannam@89: # endif cannam@89: cannam@89: cannam@89: pfile_in_zip_read_info->pos_in_zipfile += uReadThis; cannam@89: cannam@89: pfile_in_zip_read_info->rest_read_compressed-=uReadThis; cannam@89: cannam@89: pfile_in_zip_read_info->stream.next_in = cannam@89: (Bytef*)pfile_in_zip_read_info->read_buffer; cannam@89: pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; cannam@89: } cannam@89: cannam@89: if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw)) cannam@89: { cannam@89: uInt uDoCopy,i ; cannam@89: cannam@89: if ((pfile_in_zip_read_info->stream.avail_in == 0) && cannam@89: (pfile_in_zip_read_info->rest_read_compressed == 0)) cannam@89: return (iRead==0) ? UNZ_EOF : iRead; cannam@89: cannam@89: if (pfile_in_zip_read_info->stream.avail_out < cannam@89: pfile_in_zip_read_info->stream.avail_in) cannam@89: uDoCopy = pfile_in_zip_read_info->stream.avail_out ; cannam@89: else cannam@89: uDoCopy = pfile_in_zip_read_info->stream.avail_in ; cannam@89: cannam@89: for (i=0;istream.next_out+i) = cannam@89: *(pfile_in_zip_read_info->stream.next_in+i); cannam@89: cannam@89: pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uDoCopy; cannam@89: cannam@89: pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, cannam@89: pfile_in_zip_read_info->stream.next_out, cannam@89: uDoCopy); cannam@89: pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; cannam@89: pfile_in_zip_read_info->stream.avail_in -= uDoCopy; cannam@89: pfile_in_zip_read_info->stream.avail_out -= uDoCopy; cannam@89: pfile_in_zip_read_info->stream.next_out += uDoCopy; cannam@89: pfile_in_zip_read_info->stream.next_in += uDoCopy; cannam@89: pfile_in_zip_read_info->stream.total_out += uDoCopy; cannam@89: iRead += uDoCopy; cannam@89: } cannam@89: else if (pfile_in_zip_read_info->compression_method==Z_BZIP2ED) cannam@89: { cannam@89: #ifdef HAVE_BZIP2 cannam@89: uLong uTotalOutBefore,uTotalOutAfter; cannam@89: const Bytef *bufBefore; cannam@89: uLong uOutThis; cannam@89: cannam@89: pfile_in_zip_read_info->bstream.next_in = (char*)pfile_in_zip_read_info->stream.next_in; cannam@89: pfile_in_zip_read_info->bstream.avail_in = pfile_in_zip_read_info->stream.avail_in; cannam@89: pfile_in_zip_read_info->bstream.total_in_lo32 = pfile_in_zip_read_info->stream.total_in; cannam@89: pfile_in_zip_read_info->bstream.total_in_hi32 = 0; cannam@89: pfile_in_zip_read_info->bstream.next_out = (char*)pfile_in_zip_read_info->stream.next_out; cannam@89: pfile_in_zip_read_info->bstream.avail_out = pfile_in_zip_read_info->stream.avail_out; cannam@89: pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out; cannam@89: pfile_in_zip_read_info->bstream.total_out_hi32 = 0; cannam@89: cannam@89: uTotalOutBefore = pfile_in_zip_read_info->bstream.total_out_lo32; cannam@89: bufBefore = (const Bytef *)pfile_in_zip_read_info->bstream.next_out; cannam@89: cannam@89: err=BZ2_bzDecompress(&pfile_in_zip_read_info->bstream); cannam@89: cannam@89: uTotalOutAfter = pfile_in_zip_read_info->bstream.total_out_lo32; cannam@89: uOutThis = uTotalOutAfter-uTotalOutBefore; cannam@89: cannam@89: pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis; cannam@89: cannam@89: pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,bufBefore, (uInt)(uOutThis)); cannam@89: pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis; cannam@89: iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); cannam@89: cannam@89: pfile_in_zip_read_info->stream.next_in = (Bytef*)pfile_in_zip_read_info->bstream.next_in; cannam@89: pfile_in_zip_read_info->stream.avail_in = pfile_in_zip_read_info->bstream.avail_in; cannam@89: pfile_in_zip_read_info->stream.total_in = pfile_in_zip_read_info->bstream.total_in_lo32; cannam@89: pfile_in_zip_read_info->stream.next_out = (Bytef*)pfile_in_zip_read_info->bstream.next_out; cannam@89: pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out; cannam@89: pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32; cannam@89: cannam@89: if (err==BZ_STREAM_END) cannam@89: return (iRead==0) ? UNZ_EOF : iRead; cannam@89: if (err!=BZ_OK) cannam@89: break; cannam@89: #endif cannam@89: } // end Z_BZIP2ED cannam@89: else cannam@89: { cannam@89: ZPOS64_T uTotalOutBefore,uTotalOutAfter; cannam@89: const Bytef *bufBefore; cannam@89: ZPOS64_T uOutThis; cannam@89: int flush=Z_SYNC_FLUSH; cannam@89: cannam@89: uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; cannam@89: bufBefore = pfile_in_zip_read_info->stream.next_out; cannam@89: cannam@89: /* cannam@89: if ((pfile_in_zip_read_info->rest_read_uncompressed == cannam@89: pfile_in_zip_read_info->stream.avail_out) && cannam@89: (pfile_in_zip_read_info->rest_read_compressed == 0)) cannam@89: flush = Z_FINISH; cannam@89: */ cannam@89: err=inflate(&pfile_in_zip_read_info->stream,flush); cannam@89: cannam@89: if ((err>=0) && (pfile_in_zip_read_info->stream.msg!=NULL)) cannam@89: err = Z_DATA_ERROR; cannam@89: cannam@89: uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; cannam@89: uOutThis = uTotalOutAfter-uTotalOutBefore; cannam@89: cannam@89: pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis; cannam@89: cannam@89: pfile_in_zip_read_info->crc32 = cannam@89: crc32(pfile_in_zip_read_info->crc32,bufBefore, cannam@89: (uInt)(uOutThis)); cannam@89: cannam@89: pfile_in_zip_read_info->rest_read_uncompressed -= cannam@89: uOutThis; cannam@89: cannam@89: iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); cannam@89: cannam@89: if (err==Z_STREAM_END) cannam@89: return (iRead==0) ? UNZ_EOF : iRead; cannam@89: if (err!=Z_OK) cannam@89: break; cannam@89: } cannam@89: } cannam@89: cannam@89: if (err==Z_OK) cannam@89: return iRead; cannam@89: return err; cannam@89: } cannam@89: cannam@89: cannam@89: /* cannam@89: Give the current position in uncompressed data cannam@89: */ cannam@89: extern z_off_t ZEXPORT unztell (unzFile file) cannam@89: { cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: pfile_in_zip_read_info=s->pfile_in_zip_read; cannam@89: cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: return (z_off_t)pfile_in_zip_read_info->stream.total_out; cannam@89: } cannam@89: cannam@89: extern ZPOS64_T ZEXPORT unztell64 (unzFile file) cannam@89: { cannam@89: cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: if (file==NULL) cannam@89: return (ZPOS64_T)-1; cannam@89: s=(unz64_s*)file; cannam@89: pfile_in_zip_read_info=s->pfile_in_zip_read; cannam@89: cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return (ZPOS64_T)-1; cannam@89: cannam@89: return pfile_in_zip_read_info->total_out_64; cannam@89: } cannam@89: cannam@89: cannam@89: /* cannam@89: return 1 if the end of file was reached, 0 elsewhere cannam@89: */ cannam@89: extern int ZEXPORT unzeof (unzFile file) cannam@89: { cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: pfile_in_zip_read_info=s->pfile_in_zip_read; cannam@89: cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: if (pfile_in_zip_read_info->rest_read_uncompressed == 0) cannam@89: return 1; cannam@89: else cannam@89: return 0; cannam@89: } cannam@89: cannam@89: cannam@89: cannam@89: /* cannam@89: Read extra field from the current file (opened by unzOpenCurrentFile) cannam@89: This is the local-header version of the extra field (sometimes, there is cannam@89: more info in the local-header version than in the central-header) cannam@89: cannam@89: if buf==NULL, it return the size of the local extra field that can be read cannam@89: cannam@89: if buf!=NULL, len is the size of the buffer, the extra header is copied in cannam@89: buf. cannam@89: the return value is the number of bytes copied in buf, or (if <0) cannam@89: the error code cannam@89: */ cannam@89: extern int ZEXPORT unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len) cannam@89: { cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: uInt read_now; cannam@89: ZPOS64_T size_to_read; cannam@89: cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: pfile_in_zip_read_info=s->pfile_in_zip_read; cannam@89: cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: size_to_read = (pfile_in_zip_read_info->size_local_extrafield - cannam@89: pfile_in_zip_read_info->pos_local_extrafield); cannam@89: cannam@89: if (buf==NULL) cannam@89: return (int)size_to_read; cannam@89: cannam@89: if (len>size_to_read) cannam@89: read_now = (uInt)size_to_read; cannam@89: else cannam@89: read_now = (uInt)len ; cannam@89: cannam@89: if (read_now==0) cannam@89: return 0; cannam@89: cannam@89: if (ZSEEK64(pfile_in_zip_read_info->z_filefunc, cannam@89: pfile_in_zip_read_info->filestream, cannam@89: pfile_in_zip_read_info->offset_local_extrafield + cannam@89: pfile_in_zip_read_info->pos_local_extrafield, cannam@89: ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: return UNZ_ERRNO; cannam@89: cannam@89: if (ZREAD64(pfile_in_zip_read_info->z_filefunc, cannam@89: pfile_in_zip_read_info->filestream, cannam@89: buf,read_now)!=read_now) cannam@89: return UNZ_ERRNO; cannam@89: cannam@89: return (int)read_now; cannam@89: } cannam@89: cannam@89: /* cannam@89: Close the file in zip opened with unzipOpenCurrentFile cannam@89: Return UNZ_CRCERROR if all the file was read but the CRC is not good cannam@89: */ cannam@89: extern int ZEXPORT unzCloseCurrentFile (unzFile file) cannam@89: { cannam@89: int err=UNZ_OK; cannam@89: cannam@89: unz64_s* s; cannam@89: file_in_zip64_read_info_s* pfile_in_zip_read_info; cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: pfile_in_zip_read_info=s->pfile_in_zip_read; cannam@89: cannam@89: if (pfile_in_zip_read_info==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: cannam@89: cannam@89: if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) && cannam@89: (!pfile_in_zip_read_info->raw)) cannam@89: { cannam@89: if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) cannam@89: err=UNZ_CRCERROR; cannam@89: } cannam@89: cannam@89: cannam@89: TRYFREE(pfile_in_zip_read_info->read_buffer); cannam@89: pfile_in_zip_read_info->read_buffer = NULL; cannam@89: if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED) cannam@89: inflateEnd(&pfile_in_zip_read_info->stream); cannam@89: #ifdef HAVE_BZIP2 cannam@89: else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED) cannam@89: BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream); cannam@89: #endif cannam@89: cannam@89: cannam@89: pfile_in_zip_read_info->stream_initialised = 0; cannam@89: TRYFREE(pfile_in_zip_read_info); cannam@89: cannam@89: s->pfile_in_zip_read=NULL; cannam@89: cannam@89: return err; cannam@89: } cannam@89: cannam@89: cannam@89: /* cannam@89: Get the global comment string of the ZipFile, in the szComment buffer. cannam@89: uSizeBuf is the size of the szComment buffer. cannam@89: return the number of byte copied or an error code <0 cannam@89: */ cannam@89: extern int ZEXPORT unzGetGlobalComment (unzFile file, char * szComment, uLong uSizeBuf) cannam@89: { cannam@89: unz64_s* s; cannam@89: uLong uReadThis ; cannam@89: if (file==NULL) cannam@89: return (int)UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: cannam@89: uReadThis = uSizeBuf; cannam@89: if (uReadThis>s->gi.size_comment) cannam@89: uReadThis = s->gi.size_comment; cannam@89: cannam@89: if (ZSEEK64(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0) cannam@89: return UNZ_ERRNO; cannam@89: cannam@89: if (uReadThis>0) cannam@89: { cannam@89: *szComment='\0'; cannam@89: if (ZREAD64(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis) cannam@89: return UNZ_ERRNO; cannam@89: } cannam@89: cannam@89: if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) cannam@89: *(szComment+s->gi.size_comment)='\0'; cannam@89: return (int)uReadThis; cannam@89: } cannam@89: cannam@89: /* Additions by RX '2004 */ cannam@89: extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file) cannam@89: { cannam@89: unz64_s* s; cannam@89: cannam@89: if (file==NULL) cannam@89: return 0; //UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: if (!s->current_file_ok) cannam@89: return 0; cannam@89: if (s->gi.number_entry != 0 && s->gi.number_entry != 0xffff) cannam@89: if (s->num_file==s->gi.number_entry) cannam@89: return 0; cannam@89: return s->pos_in_central_dir; cannam@89: } cannam@89: cannam@89: extern uLong ZEXPORT unzGetOffset (unzFile file) cannam@89: { cannam@89: ZPOS64_T offset64; cannam@89: cannam@89: if (file==NULL) cannam@89: return 0; //UNZ_PARAMERROR; cannam@89: offset64 = unzGetOffset64(file); cannam@89: return (uLong)offset64; cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos) cannam@89: { cannam@89: unz64_s* s; cannam@89: int err; cannam@89: cannam@89: if (file==NULL) cannam@89: return UNZ_PARAMERROR; cannam@89: s=(unz64_s*)file; cannam@89: cannam@89: s->pos_in_central_dir = pos; cannam@89: s->num_file = s->gi.number_entry; /* hack */ cannam@89: err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info, cannam@89: &s->cur_file_info_internal, cannam@89: NULL,0,NULL,0,NULL,0); cannam@89: s->current_file_ok = (err == UNZ_OK); cannam@89: return err; cannam@89: } cannam@89: cannam@89: extern int ZEXPORT unzSetOffset (unzFile file, uLong pos) cannam@89: { cannam@89: return unzSetOffset64(file,pos); cannam@89: }