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