annotate src/zlib-1.2.8/contrib/minizip/unzip.c @ 128:5b4145a0d408

Current zlib source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 14:33:52 +0100
parents
children
rev   line source
cannam@128 1 /* unzip.c -- IO for uncompress .zip files using zlib
cannam@128 2 Version 1.1, February 14h, 2010
cannam@128 3 part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
cannam@128 4
cannam@128 5 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
cannam@128 6
cannam@128 7 Modifications of Unzip for Zip64
cannam@128 8 Copyright (C) 2007-2008 Even Rouault
cannam@128 9
cannam@128 10 Modifications for Zip64 support on both zip and unzip
cannam@128 11 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
cannam@128 12
cannam@128 13 For more info read MiniZip_info.txt
cannam@128 14
cannam@128 15
cannam@128 16 ------------------------------------------------------------------------------------
cannam@128 17 Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of
cannam@128 18 compatibility with older software. The following is from the original crypt.c.
cannam@128 19 Code woven in by Terry Thorsen 1/2003.
cannam@128 20
cannam@128 21 Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
cannam@128 22
cannam@128 23 See the accompanying file LICENSE, version 2000-Apr-09 or later
cannam@128 24 (the contents of which are also included in zip.h) for terms of use.
cannam@128 25 If, for some reason, all these files are missing, the Info-ZIP license
cannam@128 26 also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
cannam@128 27
cannam@128 28 crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h]
cannam@128 29
cannam@128 30 The encryption/decryption parts of this source code (as opposed to the
cannam@128 31 non-echoing password parts) were originally written in Europe. The
cannam@128 32 whole source package can be freely distributed, including from the USA.
cannam@128 33 (Prior to January 2000, re-export from the US was a violation of US law.)
cannam@128 34
cannam@128 35 This encryption code is a direct transcription of the algorithm from
cannam@128 36 Roger Schlafly, described by Phil Katz in the file appnote.txt. This
cannam@128 37 file (appnote.txt) is distributed with the PKZIP program (even in the
cannam@128 38 version without encryption capabilities).
cannam@128 39
cannam@128 40 ------------------------------------------------------------------------------------
cannam@128 41
cannam@128 42 Changes in unzip.c
cannam@128 43
cannam@128 44 2007-2008 - Even Rouault - Addition of cpl_unzGetCurrentFileZStreamPos
cannam@128 45 2007-2008 - Even Rouault - Decoration of symbol names unz* -> cpl_unz*
cannam@128 46 2007-2008 - Even Rouault - Remove old C style function prototypes
cannam@128 47 2007-2008 - Even Rouault - Add unzip support for ZIP64
cannam@128 48
cannam@128 49 Copyright (C) 2007-2008 Even Rouault
cannam@128 50
cannam@128 51
cannam@128 52 Oct-2009 - Mathias Svensson - Removed cpl_* from symbol names (Even Rouault added them but since this is now moved to a new project (minizip64) I renamed them again).
cannam@128 53 Oct-2009 - Mathias Svensson - Fixed problem if uncompressed size was > 4G and compressed size was <4G
cannam@128 54 should only read the compressed/uncompressed size from the Zip64 format if
cannam@128 55 the size from normal header was 0xFFFFFFFF
cannam@128 56 Oct-2009 - Mathias Svensson - Applied some bug fixes from paches recived from Gilles Vollant
cannam@128 57 Oct-2009 - Mathias Svensson - Applied support to unzip files with compression mathod BZIP2 (bzip2 lib is required)
cannam@128 58 Patch created by Daniel Borca
cannam@128 59
cannam@128 60 Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
cannam@128 61
cannam@128 62 Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson
cannam@128 63
cannam@128 64 */
cannam@128 65
cannam@128 66
cannam@128 67 #include <stdio.h>
cannam@128 68 #include <stdlib.h>
cannam@128 69 #include <string.h>
cannam@128 70
cannam@128 71 #ifndef NOUNCRYPT
cannam@128 72 #define NOUNCRYPT
cannam@128 73 #endif
cannam@128 74
cannam@128 75 #include "zlib.h"
cannam@128 76 #include "unzip.h"
cannam@128 77
cannam@128 78 #ifdef STDC
cannam@128 79 # include <stddef.h>
cannam@128 80 # include <string.h>
cannam@128 81 # include <stdlib.h>
cannam@128 82 #endif
cannam@128 83 #ifdef NO_ERRNO_H
cannam@128 84 extern int errno;
cannam@128 85 #else
cannam@128 86 # include <errno.h>
cannam@128 87 #endif
cannam@128 88
cannam@128 89
cannam@128 90 #ifndef local
cannam@128 91 # define local static
cannam@128 92 #endif
cannam@128 93 /* compile with -Dlocal if your debugger can't find static symbols */
cannam@128 94
cannam@128 95
cannam@128 96 #ifndef CASESENSITIVITYDEFAULT_NO
cannam@128 97 # if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES)
cannam@128 98 # define CASESENSITIVITYDEFAULT_NO
cannam@128 99 # endif
cannam@128 100 #endif
cannam@128 101
cannam@128 102
cannam@128 103 #ifndef UNZ_BUFSIZE
cannam@128 104 #define UNZ_BUFSIZE (16384)
cannam@128 105 #endif
cannam@128 106
cannam@128 107 #ifndef UNZ_MAXFILENAMEINZIP
cannam@128 108 #define UNZ_MAXFILENAMEINZIP (256)
cannam@128 109 #endif
cannam@128 110
cannam@128 111 #ifndef ALLOC
cannam@128 112 # define ALLOC(size) (malloc(size))
cannam@128 113 #endif
cannam@128 114 #ifndef TRYFREE
cannam@128 115 # define TRYFREE(p) {if (p) free(p);}
cannam@128 116 #endif
cannam@128 117
cannam@128 118 #define SIZECENTRALDIRITEM (0x2e)
cannam@128 119 #define SIZEZIPLOCALHEADER (0x1e)
cannam@128 120
cannam@128 121
cannam@128 122 const char unz_copyright[] =
cannam@128 123 " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
cannam@128 124
cannam@128 125 /* unz_file_info_interntal contain internal info about a file in zipfile*/
cannam@128 126 typedef struct unz_file_info64_internal_s
cannam@128 127 {
cannam@128 128 ZPOS64_T offset_curfile;/* relative offset of local header 8 bytes */
cannam@128 129 } unz_file_info64_internal;
cannam@128 130
cannam@128 131
cannam@128 132 /* file_in_zip_read_info_s contain internal information about a file in zipfile,
cannam@128 133 when reading and decompress it */
cannam@128 134 typedef struct
cannam@128 135 {
cannam@128 136 char *read_buffer; /* internal buffer for compressed data */
cannam@128 137 z_stream stream; /* zLib stream structure for inflate */
cannam@128 138
cannam@128 139 #ifdef HAVE_BZIP2
cannam@128 140 bz_stream bstream; /* bzLib stream structure for bziped */
cannam@128 141 #endif
cannam@128 142
cannam@128 143 ZPOS64_T pos_in_zipfile; /* position in byte on the zipfile, for fseek*/
cannam@128 144 uLong stream_initialised; /* flag set if stream structure is initialised*/
cannam@128 145
cannam@128 146 ZPOS64_T offset_local_extrafield;/* offset of the local extra field */
cannam@128 147 uInt size_local_extrafield;/* size of the local extra field */
cannam@128 148 ZPOS64_T pos_local_extrafield; /* position in the local extra field in read*/
cannam@128 149 ZPOS64_T total_out_64;
cannam@128 150
cannam@128 151 uLong crc32; /* crc32 of all data uncompressed */
cannam@128 152 uLong crc32_wait; /* crc32 we must obtain after decompress all */
cannam@128 153 ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
cannam@128 154 ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/
cannam@128 155 zlib_filefunc64_32_def z_filefunc;
cannam@128 156 voidpf filestream; /* io structore of the zipfile */
cannam@128 157 uLong compression_method; /* compression method (0==store) */
cannam@128 158 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
cannam@128 159 int raw;
cannam@128 160 } file_in_zip64_read_info_s;
cannam@128 161
cannam@128 162
cannam@128 163 /* unz64_s contain internal information about the zipfile
cannam@128 164 */
cannam@128 165 typedef struct
cannam@128 166 {
cannam@128 167 zlib_filefunc64_32_def z_filefunc;
cannam@128 168 int is64bitOpenFunction;
cannam@128 169 voidpf filestream; /* io structore of the zipfile */
cannam@128 170 unz_global_info64 gi; /* public global information */
cannam@128 171 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
cannam@128 172 ZPOS64_T num_file; /* number of the current file in the zipfile*/
cannam@128 173 ZPOS64_T pos_in_central_dir; /* pos of the current file in the central dir*/
cannam@128 174 ZPOS64_T current_file_ok; /* flag about the usability of the current file*/
cannam@128 175 ZPOS64_T central_pos; /* position of the beginning of the central dir*/
cannam@128 176
cannam@128 177 ZPOS64_T size_central_dir; /* size of the central directory */
cannam@128 178 ZPOS64_T offset_central_dir; /* offset of start of central directory with
cannam@128 179 respect to the starting disk number */
cannam@128 180
cannam@128 181 unz_file_info64 cur_file_info; /* public info about the current file in zip*/
cannam@128 182 unz_file_info64_internal cur_file_info_internal; /* private info about it*/
cannam@128 183 file_in_zip64_read_info_s* pfile_in_zip_read; /* structure about the current
cannam@128 184 file if we are decompressing it */
cannam@128 185 int encrypted;
cannam@128 186
cannam@128 187 int isZip64;
cannam@128 188
cannam@128 189 # ifndef NOUNCRYPT
cannam@128 190 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
cannam@128 191 const z_crc_t* pcrc_32_tab;
cannam@128 192 # endif
cannam@128 193 } unz64_s;
cannam@128 194
cannam@128 195
cannam@128 196 #ifndef NOUNCRYPT
cannam@128 197 #include "crypt.h"
cannam@128 198 #endif
cannam@128 199
cannam@128 200 /* ===========================================================================
cannam@128 201 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
cannam@128 202 for end of file.
cannam@128 203 IN assertion: the stream s has been sucessfully opened for reading.
cannam@128 204 */
cannam@128 205
cannam@128 206
cannam@128 207 local int unz64local_getByte OF((
cannam@128 208 const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 209 voidpf filestream,
cannam@128 210 int *pi));
cannam@128 211
cannam@128 212 local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)
cannam@128 213 {
cannam@128 214 unsigned char c;
cannam@128 215 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
cannam@128 216 if (err==1)
cannam@128 217 {
cannam@128 218 *pi = (int)c;
cannam@128 219 return UNZ_OK;
cannam@128 220 }
cannam@128 221 else
cannam@128 222 {
cannam@128 223 if (ZERROR64(*pzlib_filefunc_def,filestream))
cannam@128 224 return UNZ_ERRNO;
cannam@128 225 else
cannam@128 226 return UNZ_EOF;
cannam@128 227 }
cannam@128 228 }
cannam@128 229
cannam@128 230
cannam@128 231 /* ===========================================================================
cannam@128 232 Reads a long in LSB order from the given gz_stream. Sets
cannam@128 233 */
cannam@128 234 local int unz64local_getShort OF((
cannam@128 235 const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 236 voidpf filestream,
cannam@128 237 uLong *pX));
cannam@128 238
cannam@128 239 local int unz64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 240 voidpf filestream,
cannam@128 241 uLong *pX)
cannam@128 242 {
cannam@128 243 uLong x ;
cannam@128 244 int i = 0;
cannam@128 245 int err;
cannam@128 246
cannam@128 247 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 248 x = (uLong)i;
cannam@128 249
cannam@128 250 if (err==UNZ_OK)
cannam@128 251 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 252 x |= ((uLong)i)<<8;
cannam@128 253
cannam@128 254 if (err==UNZ_OK)
cannam@128 255 *pX = x;
cannam@128 256 else
cannam@128 257 *pX = 0;
cannam@128 258 return err;
cannam@128 259 }
cannam@128 260
cannam@128 261 local int unz64local_getLong OF((
cannam@128 262 const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 263 voidpf filestream,
cannam@128 264 uLong *pX));
cannam@128 265
cannam@128 266 local int unz64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 267 voidpf filestream,
cannam@128 268 uLong *pX)
cannam@128 269 {
cannam@128 270 uLong x ;
cannam@128 271 int i = 0;
cannam@128 272 int err;
cannam@128 273
cannam@128 274 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 275 x = (uLong)i;
cannam@128 276
cannam@128 277 if (err==UNZ_OK)
cannam@128 278 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 279 x |= ((uLong)i)<<8;
cannam@128 280
cannam@128 281 if (err==UNZ_OK)
cannam@128 282 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 283 x |= ((uLong)i)<<16;
cannam@128 284
cannam@128 285 if (err==UNZ_OK)
cannam@128 286 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 287 x += ((uLong)i)<<24;
cannam@128 288
cannam@128 289 if (err==UNZ_OK)
cannam@128 290 *pX = x;
cannam@128 291 else
cannam@128 292 *pX = 0;
cannam@128 293 return err;
cannam@128 294 }
cannam@128 295
cannam@128 296 local int unz64local_getLong64 OF((
cannam@128 297 const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 298 voidpf filestream,
cannam@128 299 ZPOS64_T *pX));
cannam@128 300
cannam@128 301
cannam@128 302 local int unz64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 303 voidpf filestream,
cannam@128 304 ZPOS64_T *pX)
cannam@128 305 {
cannam@128 306 ZPOS64_T x ;
cannam@128 307 int i = 0;
cannam@128 308 int err;
cannam@128 309
cannam@128 310 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 311 x = (ZPOS64_T)i;
cannam@128 312
cannam@128 313 if (err==UNZ_OK)
cannam@128 314 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 315 x |= ((ZPOS64_T)i)<<8;
cannam@128 316
cannam@128 317 if (err==UNZ_OK)
cannam@128 318 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 319 x |= ((ZPOS64_T)i)<<16;
cannam@128 320
cannam@128 321 if (err==UNZ_OK)
cannam@128 322 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 323 x |= ((ZPOS64_T)i)<<24;
cannam@128 324
cannam@128 325 if (err==UNZ_OK)
cannam@128 326 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 327 x |= ((ZPOS64_T)i)<<32;
cannam@128 328
cannam@128 329 if (err==UNZ_OK)
cannam@128 330 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 331 x |= ((ZPOS64_T)i)<<40;
cannam@128 332
cannam@128 333 if (err==UNZ_OK)
cannam@128 334 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 335 x |= ((ZPOS64_T)i)<<48;
cannam@128 336
cannam@128 337 if (err==UNZ_OK)
cannam@128 338 err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
cannam@128 339 x |= ((ZPOS64_T)i)<<56;
cannam@128 340
cannam@128 341 if (err==UNZ_OK)
cannam@128 342 *pX = x;
cannam@128 343 else
cannam@128 344 *pX = 0;
cannam@128 345 return err;
cannam@128 346 }
cannam@128 347
cannam@128 348 /* My own strcmpi / strcasecmp */
cannam@128 349 local int strcmpcasenosensitive_internal (const char* fileName1, const char* fileName2)
cannam@128 350 {
cannam@128 351 for (;;)
cannam@128 352 {
cannam@128 353 char c1=*(fileName1++);
cannam@128 354 char c2=*(fileName2++);
cannam@128 355 if ((c1>='a') && (c1<='z'))
cannam@128 356 c1 -= 0x20;
cannam@128 357 if ((c2>='a') && (c2<='z'))
cannam@128 358 c2 -= 0x20;
cannam@128 359 if (c1=='\0')
cannam@128 360 return ((c2=='\0') ? 0 : -1);
cannam@128 361 if (c2=='\0')
cannam@128 362 return 1;
cannam@128 363 if (c1<c2)
cannam@128 364 return -1;
cannam@128 365 if (c1>c2)
cannam@128 366 return 1;
cannam@128 367 }
cannam@128 368 }
cannam@128 369
cannam@128 370
cannam@128 371 #ifdef CASESENSITIVITYDEFAULT_NO
cannam@128 372 #define CASESENSITIVITYDEFAULTVALUE 2
cannam@128 373 #else
cannam@128 374 #define CASESENSITIVITYDEFAULTVALUE 1
cannam@128 375 #endif
cannam@128 376
cannam@128 377 #ifndef STRCMPCASENOSENTIVEFUNCTION
cannam@128 378 #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
cannam@128 379 #endif
cannam@128 380
cannam@128 381 /*
cannam@128 382 Compare two filename (fileName1,fileName2).
cannam@128 383 If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
cannam@128 384 If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
cannam@128 385 or strcasecmp)
cannam@128 386 If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
cannam@128 387 (like 1 on Unix, 2 on Windows)
cannam@128 388
cannam@128 389 */
cannam@128 390 extern int ZEXPORT unzStringFileNameCompare (const char* fileName1,
cannam@128 391 const char* fileName2,
cannam@128 392 int iCaseSensitivity)
cannam@128 393
cannam@128 394 {
cannam@128 395 if (iCaseSensitivity==0)
cannam@128 396 iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
cannam@128 397
cannam@128 398 if (iCaseSensitivity==1)
cannam@128 399 return strcmp(fileName1,fileName2);
cannam@128 400
cannam@128 401 return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
cannam@128 402 }
cannam@128 403
cannam@128 404 #ifndef BUFREADCOMMENT
cannam@128 405 #define BUFREADCOMMENT (0x400)
cannam@128 406 #endif
cannam@128 407
cannam@128 408 /*
cannam@128 409 Locate the Central directory of a zipfile (at the end, just before
cannam@128 410 the global comment)
cannam@128 411 */
cannam@128 412 local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
cannam@128 413 local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
cannam@128 414 {
cannam@128 415 unsigned char* buf;
cannam@128 416 ZPOS64_T uSizeFile;
cannam@128 417 ZPOS64_T uBackRead;
cannam@128 418 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
cannam@128 419 ZPOS64_T uPosFound=0;
cannam@128 420
cannam@128 421 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
cannam@128 422 return 0;
cannam@128 423
cannam@128 424
cannam@128 425 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
cannam@128 426
cannam@128 427 if (uMaxBack>uSizeFile)
cannam@128 428 uMaxBack = uSizeFile;
cannam@128 429
cannam@128 430 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
cannam@128 431 if (buf==NULL)
cannam@128 432 return 0;
cannam@128 433
cannam@128 434 uBackRead = 4;
cannam@128 435 while (uBackRead<uMaxBack)
cannam@128 436 {
cannam@128 437 uLong uReadSize;
cannam@128 438 ZPOS64_T uReadPos ;
cannam@128 439 int i;
cannam@128 440 if (uBackRead+BUFREADCOMMENT>uMaxBack)
cannam@128 441 uBackRead = uMaxBack;
cannam@128 442 else
cannam@128 443 uBackRead+=BUFREADCOMMENT;
cannam@128 444 uReadPos = uSizeFile-uBackRead ;
cannam@128 445
cannam@128 446 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
cannam@128 447 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
cannam@128 448 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 449 break;
cannam@128 450
cannam@128 451 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
cannam@128 452 break;
cannam@128 453
cannam@128 454 for (i=(int)uReadSize-3; (i--)>0;)
cannam@128 455 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
cannam@128 456 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
cannam@128 457 {
cannam@128 458 uPosFound = uReadPos+i;
cannam@128 459 break;
cannam@128 460 }
cannam@128 461
cannam@128 462 if (uPosFound!=0)
cannam@128 463 break;
cannam@128 464 }
cannam@128 465 TRYFREE(buf);
cannam@128 466 return uPosFound;
cannam@128 467 }
cannam@128 468
cannam@128 469
cannam@128 470 /*
cannam@128 471 Locate the Central directory 64 of a zipfile (at the end, just before
cannam@128 472 the global comment)
cannam@128 473 */
cannam@128 474 local ZPOS64_T unz64local_SearchCentralDir64 OF((
cannam@128 475 const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 476 voidpf filestream));
cannam@128 477
cannam@128 478 local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
cannam@128 479 voidpf filestream)
cannam@128 480 {
cannam@128 481 unsigned char* buf;
cannam@128 482 ZPOS64_T uSizeFile;
cannam@128 483 ZPOS64_T uBackRead;
cannam@128 484 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
cannam@128 485 ZPOS64_T uPosFound=0;
cannam@128 486 uLong uL;
cannam@128 487 ZPOS64_T relativeOffset;
cannam@128 488
cannam@128 489 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
cannam@128 490 return 0;
cannam@128 491
cannam@128 492
cannam@128 493 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
cannam@128 494
cannam@128 495 if (uMaxBack>uSizeFile)
cannam@128 496 uMaxBack = uSizeFile;
cannam@128 497
cannam@128 498 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
cannam@128 499 if (buf==NULL)
cannam@128 500 return 0;
cannam@128 501
cannam@128 502 uBackRead = 4;
cannam@128 503 while (uBackRead<uMaxBack)
cannam@128 504 {
cannam@128 505 uLong uReadSize;
cannam@128 506 ZPOS64_T uReadPos;
cannam@128 507 int i;
cannam@128 508 if (uBackRead+BUFREADCOMMENT>uMaxBack)
cannam@128 509 uBackRead = uMaxBack;
cannam@128 510 else
cannam@128 511 uBackRead+=BUFREADCOMMENT;
cannam@128 512 uReadPos = uSizeFile-uBackRead ;
cannam@128 513
cannam@128 514 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
cannam@128 515 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
cannam@128 516 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 517 break;
cannam@128 518
cannam@128 519 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
cannam@128 520 break;
cannam@128 521
cannam@128 522 for (i=(int)uReadSize-3; (i--)>0;)
cannam@128 523 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
cannam@128 524 ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
cannam@128 525 {
cannam@128 526 uPosFound = uReadPos+i;
cannam@128 527 break;
cannam@128 528 }
cannam@128 529
cannam@128 530 if (uPosFound!=0)
cannam@128 531 break;
cannam@128 532 }
cannam@128 533 TRYFREE(buf);
cannam@128 534 if (uPosFound == 0)
cannam@128 535 return 0;
cannam@128 536
cannam@128 537 /* Zip64 end of central directory locator */
cannam@128 538 if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 539 return 0;
cannam@128 540
cannam@128 541 /* the signature, already checked */
cannam@128 542 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
cannam@128 543 return 0;
cannam@128 544
cannam@128 545 /* number of the disk with the start of the zip64 end of central directory */
cannam@128 546 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
cannam@128 547 return 0;
cannam@128 548 if (uL != 0)
cannam@128 549 return 0;
cannam@128 550
cannam@128 551 /* relative offset of the zip64 end of central directory record */
cannam@128 552 if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK)
cannam@128 553 return 0;
cannam@128 554
cannam@128 555 /* total number of disks */
cannam@128 556 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
cannam@128 557 return 0;
cannam@128 558 if (uL != 1)
cannam@128 559 return 0;
cannam@128 560
cannam@128 561 /* Goto end of central directory record */
cannam@128 562 if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 563 return 0;
cannam@128 564
cannam@128 565 /* the signature */
cannam@128 566 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
cannam@128 567 return 0;
cannam@128 568
cannam@128 569 if (uL != 0x06064b50)
cannam@128 570 return 0;
cannam@128 571
cannam@128 572 return relativeOffset;
cannam@128 573 }
cannam@128 574
cannam@128 575 /*
cannam@128 576 Open a Zip file. path contain the full pathname (by example,
cannam@128 577 on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer
cannam@128 578 "zlib/zlib114.zip".
cannam@128 579 If the zipfile cannot be opened (file doesn't exist or in not valid), the
cannam@128 580 return value is NULL.
cannam@128 581 Else, the return value is a unzFile Handle, usable with other function
cannam@128 582 of this unzip package.
cannam@128 583 */
cannam@128 584 local unzFile unzOpenInternal (const void *path,
cannam@128 585 zlib_filefunc64_32_def* pzlib_filefunc64_32_def,
cannam@128 586 int is64bitOpenFunction)
cannam@128 587 {
cannam@128 588 unz64_s us;
cannam@128 589 unz64_s *s;
cannam@128 590 ZPOS64_T central_pos;
cannam@128 591 uLong uL;
cannam@128 592
cannam@128 593 uLong number_disk; /* number of the current dist, used for
cannam@128 594 spaning ZIP, unsupported, always 0*/
cannam@128 595 uLong number_disk_with_CD; /* number the the disk with central dir, used
cannam@128 596 for spaning ZIP, unsupported, always 0*/
cannam@128 597 ZPOS64_T number_entry_CD; /* total number of entries in
cannam@128 598 the central dir
cannam@128 599 (same than number_entry on nospan) */
cannam@128 600
cannam@128 601 int err=UNZ_OK;
cannam@128 602
cannam@128 603 if (unz_copyright[0]!=' ')
cannam@128 604 return NULL;
cannam@128 605
cannam@128 606 us.z_filefunc.zseek32_file = NULL;
cannam@128 607 us.z_filefunc.ztell32_file = NULL;
cannam@128 608 if (pzlib_filefunc64_32_def==NULL)
cannam@128 609 fill_fopen64_filefunc(&us.z_filefunc.zfile_func64);
cannam@128 610 else
cannam@128 611 us.z_filefunc = *pzlib_filefunc64_32_def;
cannam@128 612 us.is64bitOpenFunction = is64bitOpenFunction;
cannam@128 613
cannam@128 614
cannam@128 615
cannam@128 616 us.filestream = ZOPEN64(us.z_filefunc,
cannam@128 617 path,
cannam@128 618 ZLIB_FILEFUNC_MODE_READ |
cannam@128 619 ZLIB_FILEFUNC_MODE_EXISTING);
cannam@128 620 if (us.filestream==NULL)
cannam@128 621 return NULL;
cannam@128 622
cannam@128 623 central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream);
cannam@128 624 if (central_pos)
cannam@128 625 {
cannam@128 626 uLong uS;
cannam@128 627 ZPOS64_T uL64;
cannam@128 628
cannam@128 629 us.isZip64 = 1;
cannam@128 630
cannam@128 631 if (ZSEEK64(us.z_filefunc, us.filestream,
cannam@128 632 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 633 err=UNZ_ERRNO;
cannam@128 634
cannam@128 635 /* the signature, already checked */
cannam@128 636 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
cannam@128 637 err=UNZ_ERRNO;
cannam@128 638
cannam@128 639 /* size of zip64 end of central directory record */
cannam@128 640 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&uL64)!=UNZ_OK)
cannam@128 641 err=UNZ_ERRNO;
cannam@128 642
cannam@128 643 /* version made by */
cannam@128 644 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
cannam@128 645 err=UNZ_ERRNO;
cannam@128 646
cannam@128 647 /* version needed to extract */
cannam@128 648 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
cannam@128 649 err=UNZ_ERRNO;
cannam@128 650
cannam@128 651 /* number of this disk */
cannam@128 652 if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
cannam@128 653 err=UNZ_ERRNO;
cannam@128 654
cannam@128 655 /* number of the disk with the start of the central directory */
cannam@128 656 if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
cannam@128 657 err=UNZ_ERRNO;
cannam@128 658
cannam@128 659 /* total number of entries in the central directory on this disk */
cannam@128 660 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK)
cannam@128 661 err=UNZ_ERRNO;
cannam@128 662
cannam@128 663 /* total number of entries in the central directory */
cannam@128 664 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK)
cannam@128 665 err=UNZ_ERRNO;
cannam@128 666
cannam@128 667 if ((number_entry_CD!=us.gi.number_entry) ||
cannam@128 668 (number_disk_with_CD!=0) ||
cannam@128 669 (number_disk!=0))
cannam@128 670 err=UNZ_BADZIPFILE;
cannam@128 671
cannam@128 672 /* size of the central directory */
cannam@128 673 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK)
cannam@128 674 err=UNZ_ERRNO;
cannam@128 675
cannam@128 676 /* offset of start of central directory with respect to the
cannam@128 677 starting disk number */
cannam@128 678 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK)
cannam@128 679 err=UNZ_ERRNO;
cannam@128 680
cannam@128 681 us.gi.size_comment = 0;
cannam@128 682 }
cannam@128 683 else
cannam@128 684 {
cannam@128 685 central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream);
cannam@128 686 if (central_pos==0)
cannam@128 687 err=UNZ_ERRNO;
cannam@128 688
cannam@128 689 us.isZip64 = 0;
cannam@128 690
cannam@128 691 if (ZSEEK64(us.z_filefunc, us.filestream,
cannam@128 692 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 693 err=UNZ_ERRNO;
cannam@128 694
cannam@128 695 /* the signature, already checked */
cannam@128 696 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
cannam@128 697 err=UNZ_ERRNO;
cannam@128 698
cannam@128 699 /* number of this disk */
cannam@128 700 if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
cannam@128 701 err=UNZ_ERRNO;
cannam@128 702
cannam@128 703 /* number of the disk with the start of the central directory */
cannam@128 704 if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
cannam@128 705 err=UNZ_ERRNO;
cannam@128 706
cannam@128 707 /* total number of entries in the central dir on this disk */
cannam@128 708 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
cannam@128 709 err=UNZ_ERRNO;
cannam@128 710 us.gi.number_entry = uL;
cannam@128 711
cannam@128 712 /* total number of entries in the central dir */
cannam@128 713 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
cannam@128 714 err=UNZ_ERRNO;
cannam@128 715 number_entry_CD = uL;
cannam@128 716
cannam@128 717 if ((number_entry_CD!=us.gi.number_entry) ||
cannam@128 718 (number_disk_with_CD!=0) ||
cannam@128 719 (number_disk!=0))
cannam@128 720 err=UNZ_BADZIPFILE;
cannam@128 721
cannam@128 722 /* size of the central directory */
cannam@128 723 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
cannam@128 724 err=UNZ_ERRNO;
cannam@128 725 us.size_central_dir = uL;
cannam@128 726
cannam@128 727 /* offset of start of central directory with respect to the
cannam@128 728 starting disk number */
cannam@128 729 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
cannam@128 730 err=UNZ_ERRNO;
cannam@128 731 us.offset_central_dir = uL;
cannam@128 732
cannam@128 733 /* zipfile comment length */
cannam@128 734 if (unz64local_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK)
cannam@128 735 err=UNZ_ERRNO;
cannam@128 736 }
cannam@128 737
cannam@128 738 if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
cannam@128 739 (err==UNZ_OK))
cannam@128 740 err=UNZ_BADZIPFILE;
cannam@128 741
cannam@128 742 if (err!=UNZ_OK)
cannam@128 743 {
cannam@128 744 ZCLOSE64(us.z_filefunc, us.filestream);
cannam@128 745 return NULL;
cannam@128 746 }
cannam@128 747
cannam@128 748 us.byte_before_the_zipfile = central_pos -
cannam@128 749 (us.offset_central_dir+us.size_central_dir);
cannam@128 750 us.central_pos = central_pos;
cannam@128 751 us.pfile_in_zip_read = NULL;
cannam@128 752 us.encrypted = 0;
cannam@128 753
cannam@128 754
cannam@128 755 s=(unz64_s*)ALLOC(sizeof(unz64_s));
cannam@128 756 if( s != NULL)
cannam@128 757 {
cannam@128 758 *s=us;
cannam@128 759 unzGoToFirstFile((unzFile)s);
cannam@128 760 }
cannam@128 761 return (unzFile)s;
cannam@128 762 }
cannam@128 763
cannam@128 764
cannam@128 765 extern unzFile ZEXPORT unzOpen2 (const char *path,
cannam@128 766 zlib_filefunc_def* pzlib_filefunc32_def)
cannam@128 767 {
cannam@128 768 if (pzlib_filefunc32_def != NULL)
cannam@128 769 {
cannam@128 770 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
cannam@128 771 fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
cannam@128 772 return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 0);
cannam@128 773 }
cannam@128 774 else
cannam@128 775 return unzOpenInternal(path, NULL, 0);
cannam@128 776 }
cannam@128 777
cannam@128 778 extern unzFile ZEXPORT unzOpen2_64 (const void *path,
cannam@128 779 zlib_filefunc64_def* pzlib_filefunc_def)
cannam@128 780 {
cannam@128 781 if (pzlib_filefunc_def != NULL)
cannam@128 782 {
cannam@128 783 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
cannam@128 784 zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
cannam@128 785 zlib_filefunc64_32_def_fill.ztell32_file = NULL;
cannam@128 786 zlib_filefunc64_32_def_fill.zseek32_file = NULL;
cannam@128 787 return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 1);
cannam@128 788 }
cannam@128 789 else
cannam@128 790 return unzOpenInternal(path, NULL, 1);
cannam@128 791 }
cannam@128 792
cannam@128 793 extern unzFile ZEXPORT unzOpen (const char *path)
cannam@128 794 {
cannam@128 795 return unzOpenInternal(path, NULL, 0);
cannam@128 796 }
cannam@128 797
cannam@128 798 extern unzFile ZEXPORT unzOpen64 (const void *path)
cannam@128 799 {
cannam@128 800 return unzOpenInternal(path, NULL, 1);
cannam@128 801 }
cannam@128 802
cannam@128 803 /*
cannam@128 804 Close a ZipFile opened with unzOpen.
cannam@128 805 If there is files inside the .Zip opened with unzOpenCurrentFile (see later),
cannam@128 806 these files MUST be closed with unzCloseCurrentFile before call unzClose.
cannam@128 807 return UNZ_OK if there is no problem. */
cannam@128 808 extern int ZEXPORT unzClose (unzFile file)
cannam@128 809 {
cannam@128 810 unz64_s* s;
cannam@128 811 if (file==NULL)
cannam@128 812 return UNZ_PARAMERROR;
cannam@128 813 s=(unz64_s*)file;
cannam@128 814
cannam@128 815 if (s->pfile_in_zip_read!=NULL)
cannam@128 816 unzCloseCurrentFile(file);
cannam@128 817
cannam@128 818 ZCLOSE64(s->z_filefunc, s->filestream);
cannam@128 819 TRYFREE(s);
cannam@128 820 return UNZ_OK;
cannam@128 821 }
cannam@128 822
cannam@128 823
cannam@128 824 /*
cannam@128 825 Write info about the ZipFile in the *pglobal_info structure.
cannam@128 826 No preparation of the structure is needed
cannam@128 827 return UNZ_OK if there is no problem. */
cannam@128 828 extern int ZEXPORT unzGetGlobalInfo64 (unzFile file, unz_global_info64* pglobal_info)
cannam@128 829 {
cannam@128 830 unz64_s* s;
cannam@128 831 if (file==NULL)
cannam@128 832 return UNZ_PARAMERROR;
cannam@128 833 s=(unz64_s*)file;
cannam@128 834 *pglobal_info=s->gi;
cannam@128 835 return UNZ_OK;
cannam@128 836 }
cannam@128 837
cannam@128 838 extern int ZEXPORT unzGetGlobalInfo (unzFile file, unz_global_info* pglobal_info32)
cannam@128 839 {
cannam@128 840 unz64_s* s;
cannam@128 841 if (file==NULL)
cannam@128 842 return UNZ_PARAMERROR;
cannam@128 843 s=(unz64_s*)file;
cannam@128 844 /* to do : check if number_entry is not truncated */
cannam@128 845 pglobal_info32->number_entry = (uLong)s->gi.number_entry;
cannam@128 846 pglobal_info32->size_comment = s->gi.size_comment;
cannam@128 847 return UNZ_OK;
cannam@128 848 }
cannam@128 849 /*
cannam@128 850 Translate date/time from Dos format to tm_unz (readable more easilty)
cannam@128 851 */
cannam@128 852 local void unz64local_DosDateToTmuDate (ZPOS64_T ulDosDate, tm_unz* ptm)
cannam@128 853 {
cannam@128 854 ZPOS64_T uDate;
cannam@128 855 uDate = (ZPOS64_T)(ulDosDate>>16);
cannam@128 856 ptm->tm_mday = (uInt)(uDate&0x1f) ;
cannam@128 857 ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
cannam@128 858 ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
cannam@128 859
cannam@128 860 ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
cannam@128 861 ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
cannam@128 862 ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
cannam@128 863 }
cannam@128 864
cannam@128 865 /*
cannam@128 866 Get Info about the current file in the zipfile, with internal only info
cannam@128 867 */
cannam@128 868 local int unz64local_GetCurrentFileInfoInternal OF((unzFile file,
cannam@128 869 unz_file_info64 *pfile_info,
cannam@128 870 unz_file_info64_internal
cannam@128 871 *pfile_info_internal,
cannam@128 872 char *szFileName,
cannam@128 873 uLong fileNameBufferSize,
cannam@128 874 void *extraField,
cannam@128 875 uLong extraFieldBufferSize,
cannam@128 876 char *szComment,
cannam@128 877 uLong commentBufferSize));
cannam@128 878
cannam@128 879 local int unz64local_GetCurrentFileInfoInternal (unzFile file,
cannam@128 880 unz_file_info64 *pfile_info,
cannam@128 881 unz_file_info64_internal
cannam@128 882 *pfile_info_internal,
cannam@128 883 char *szFileName,
cannam@128 884 uLong fileNameBufferSize,
cannam@128 885 void *extraField,
cannam@128 886 uLong extraFieldBufferSize,
cannam@128 887 char *szComment,
cannam@128 888 uLong commentBufferSize)
cannam@128 889 {
cannam@128 890 unz64_s* s;
cannam@128 891 unz_file_info64 file_info;
cannam@128 892 unz_file_info64_internal file_info_internal;
cannam@128 893 int err=UNZ_OK;
cannam@128 894 uLong uMagic;
cannam@128 895 long lSeek=0;
cannam@128 896 uLong uL;
cannam@128 897
cannam@128 898 if (file==NULL)
cannam@128 899 return UNZ_PARAMERROR;
cannam@128 900 s=(unz64_s*)file;
cannam@128 901 if (ZSEEK64(s->z_filefunc, s->filestream,
cannam@128 902 s->pos_in_central_dir+s->byte_before_the_zipfile,
cannam@128 903 ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 904 err=UNZ_ERRNO;
cannam@128 905
cannam@128 906
cannam@128 907 /* we check the magic */
cannam@128 908 if (err==UNZ_OK)
cannam@128 909 {
cannam@128 910 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
cannam@128 911 err=UNZ_ERRNO;
cannam@128 912 else if (uMagic!=0x02014b50)
cannam@128 913 err=UNZ_BADZIPFILE;
cannam@128 914 }
cannam@128 915
cannam@128 916 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK)
cannam@128 917 err=UNZ_ERRNO;
cannam@128 918
cannam@128 919 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK)
cannam@128 920 err=UNZ_ERRNO;
cannam@128 921
cannam@128 922 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK)
cannam@128 923 err=UNZ_ERRNO;
cannam@128 924
cannam@128 925 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK)
cannam@128 926 err=UNZ_ERRNO;
cannam@128 927
cannam@128 928 if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK)
cannam@128 929 err=UNZ_ERRNO;
cannam@128 930
cannam@128 931 unz64local_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
cannam@128 932
cannam@128 933 if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK)
cannam@128 934 err=UNZ_ERRNO;
cannam@128 935
cannam@128 936 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
cannam@128 937 err=UNZ_ERRNO;
cannam@128 938 file_info.compressed_size = uL;
cannam@128 939
cannam@128 940 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
cannam@128 941 err=UNZ_ERRNO;
cannam@128 942 file_info.uncompressed_size = uL;
cannam@128 943
cannam@128 944 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK)
cannam@128 945 err=UNZ_ERRNO;
cannam@128 946
cannam@128 947 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK)
cannam@128 948 err=UNZ_ERRNO;
cannam@128 949
cannam@128 950 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK)
cannam@128 951 err=UNZ_ERRNO;
cannam@128 952
cannam@128 953 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
cannam@128 954 err=UNZ_ERRNO;
cannam@128 955
cannam@128 956 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK)
cannam@128 957 err=UNZ_ERRNO;
cannam@128 958
cannam@128 959 if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK)
cannam@128 960 err=UNZ_ERRNO;
cannam@128 961
cannam@128 962 // relative offset of local header
cannam@128 963 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
cannam@128 964 err=UNZ_ERRNO;
cannam@128 965 file_info_internal.offset_curfile = uL;
cannam@128 966
cannam@128 967 lSeek+=file_info.size_filename;
cannam@128 968 if ((err==UNZ_OK) && (szFileName!=NULL))
cannam@128 969 {
cannam@128 970 uLong uSizeRead ;
cannam@128 971 if (file_info.size_filename<fileNameBufferSize)
cannam@128 972 {
cannam@128 973 *(szFileName+file_info.size_filename)='\0';
cannam@128 974 uSizeRead = file_info.size_filename;
cannam@128 975 }
cannam@128 976 else
cannam@128 977 uSizeRead = fileNameBufferSize;
cannam@128 978
cannam@128 979 if ((file_info.size_filename>0) && (fileNameBufferSize>0))
cannam@128 980 if (ZREAD64(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead)
cannam@128 981 err=UNZ_ERRNO;
cannam@128 982 lSeek -= uSizeRead;
cannam@128 983 }
cannam@128 984
cannam@128 985 // Read extrafield
cannam@128 986 if ((err==UNZ_OK) && (extraField!=NULL))
cannam@128 987 {
cannam@128 988 ZPOS64_T uSizeRead ;
cannam@128 989 if (file_info.size_file_extra<extraFieldBufferSize)
cannam@128 990 uSizeRead = file_info.size_file_extra;
cannam@128 991 else
cannam@128 992 uSizeRead = extraFieldBufferSize;
cannam@128 993
cannam@128 994 if (lSeek!=0)
cannam@128 995 {
cannam@128 996 if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
cannam@128 997 lSeek=0;
cannam@128 998 else
cannam@128 999 err=UNZ_ERRNO;
cannam@128 1000 }
cannam@128 1001
cannam@128 1002 if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
cannam@128 1003 if (ZREAD64(s->z_filefunc, s->filestream,extraField,(uLong)uSizeRead)!=uSizeRead)
cannam@128 1004 err=UNZ_ERRNO;
cannam@128 1005
cannam@128 1006 lSeek += file_info.size_file_extra - (uLong)uSizeRead;
cannam@128 1007 }
cannam@128 1008 else
cannam@128 1009 lSeek += file_info.size_file_extra;
cannam@128 1010
cannam@128 1011
cannam@128 1012 if ((err==UNZ_OK) && (file_info.size_file_extra != 0))
cannam@128 1013 {
cannam@128 1014 uLong acc = 0;
cannam@128 1015
cannam@128 1016 // since lSeek now points to after the extra field we need to move back
cannam@128 1017 lSeek -= file_info.size_file_extra;
cannam@128 1018
cannam@128 1019 if (lSeek!=0)
cannam@128 1020 {
cannam@128 1021 if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
cannam@128 1022 lSeek=0;
cannam@128 1023 else
cannam@128 1024 err=UNZ_ERRNO;
cannam@128 1025 }
cannam@128 1026
cannam@128 1027 while(acc < file_info.size_file_extra)
cannam@128 1028 {
cannam@128 1029 uLong headerId;
cannam@128 1030 uLong dataSize;
cannam@128 1031
cannam@128 1032 if (unz64local_getShort(&s->z_filefunc, s->filestream,&headerId) != UNZ_OK)
cannam@128 1033 err=UNZ_ERRNO;
cannam@128 1034
cannam@128 1035 if (unz64local_getShort(&s->z_filefunc, s->filestream,&dataSize) != UNZ_OK)
cannam@128 1036 err=UNZ_ERRNO;
cannam@128 1037
cannam@128 1038 /* ZIP64 extra fields */
cannam@128 1039 if (headerId == 0x0001)
cannam@128 1040 {
cannam@128 1041 uLong uL;
cannam@128 1042
cannam@128 1043 if(file_info.uncompressed_size == MAXU32)
cannam@128 1044 {
cannam@128 1045 if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
cannam@128 1046 err=UNZ_ERRNO;
cannam@128 1047 }
cannam@128 1048
cannam@128 1049 if(file_info.compressed_size == MAXU32)
cannam@128 1050 {
cannam@128 1051 if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
cannam@128 1052 err=UNZ_ERRNO;
cannam@128 1053 }
cannam@128 1054
cannam@128 1055 if(file_info_internal.offset_curfile == MAXU32)
cannam@128 1056 {
cannam@128 1057 /* Relative Header offset */
cannam@128 1058 if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
cannam@128 1059 err=UNZ_ERRNO;
cannam@128 1060 }
cannam@128 1061
cannam@128 1062 if(file_info.disk_num_start == MAXU32)
cannam@128 1063 {
cannam@128 1064 /* Disk Start Number */
cannam@128 1065 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
cannam@128 1066 err=UNZ_ERRNO;
cannam@128 1067 }
cannam@128 1068
cannam@128 1069 }
cannam@128 1070 else
cannam@128 1071 {
cannam@128 1072 if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0)
cannam@128 1073 err=UNZ_ERRNO;
cannam@128 1074 }
cannam@128 1075
cannam@128 1076 acc += 2 + 2 + dataSize;
cannam@128 1077 }
cannam@128 1078 }
cannam@128 1079
cannam@128 1080 if ((err==UNZ_OK) && (szComment!=NULL))
cannam@128 1081 {
cannam@128 1082 uLong uSizeRead ;
cannam@128 1083 if (file_info.size_file_comment<commentBufferSize)
cannam@128 1084 {
cannam@128 1085 *(szComment+file_info.size_file_comment)='\0';
cannam@128 1086 uSizeRead = file_info.size_file_comment;
cannam@128 1087 }
cannam@128 1088 else
cannam@128 1089 uSizeRead = commentBufferSize;
cannam@128 1090
cannam@128 1091 if (lSeek!=0)
cannam@128 1092 {
cannam@128 1093 if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
cannam@128 1094 lSeek=0;
cannam@128 1095 else
cannam@128 1096 err=UNZ_ERRNO;
cannam@128 1097 }
cannam@128 1098
cannam@128 1099 if ((file_info.size_file_comment>0) && (commentBufferSize>0))
cannam@128 1100 if (ZREAD64(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
cannam@128 1101 err=UNZ_ERRNO;
cannam@128 1102 lSeek+=file_info.size_file_comment - uSizeRead;
cannam@128 1103 }
cannam@128 1104 else
cannam@128 1105 lSeek+=file_info.size_file_comment;
cannam@128 1106
cannam@128 1107
cannam@128 1108 if ((err==UNZ_OK) && (pfile_info!=NULL))
cannam@128 1109 *pfile_info=file_info;
cannam@128 1110
cannam@128 1111 if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
cannam@128 1112 *pfile_info_internal=file_info_internal;
cannam@128 1113
cannam@128 1114 return err;
cannam@128 1115 }
cannam@128 1116
cannam@128 1117
cannam@128 1118
cannam@128 1119 /*
cannam@128 1120 Write info about the ZipFile in the *pglobal_info structure.
cannam@128 1121 No preparation of the structure is needed
cannam@128 1122 return UNZ_OK if there is no problem.
cannam@128 1123 */
cannam@128 1124 extern int ZEXPORT unzGetCurrentFileInfo64 (unzFile file,
cannam@128 1125 unz_file_info64 * pfile_info,
cannam@128 1126 char * szFileName, uLong fileNameBufferSize,
cannam@128 1127 void *extraField, uLong extraFieldBufferSize,
cannam@128 1128 char* szComment, uLong commentBufferSize)
cannam@128 1129 {
cannam@128 1130 return unz64local_GetCurrentFileInfoInternal(file,pfile_info,NULL,
cannam@128 1131 szFileName,fileNameBufferSize,
cannam@128 1132 extraField,extraFieldBufferSize,
cannam@128 1133 szComment,commentBufferSize);
cannam@128 1134 }
cannam@128 1135
cannam@128 1136 extern int ZEXPORT unzGetCurrentFileInfo (unzFile file,
cannam@128 1137 unz_file_info * pfile_info,
cannam@128 1138 char * szFileName, uLong fileNameBufferSize,
cannam@128 1139 void *extraField, uLong extraFieldBufferSize,
cannam@128 1140 char* szComment, uLong commentBufferSize)
cannam@128 1141 {
cannam@128 1142 int err;
cannam@128 1143 unz_file_info64 file_info64;
cannam@128 1144 err = unz64local_GetCurrentFileInfoInternal(file,&file_info64,NULL,
cannam@128 1145 szFileName,fileNameBufferSize,
cannam@128 1146 extraField,extraFieldBufferSize,
cannam@128 1147 szComment,commentBufferSize);
cannam@128 1148 if ((err==UNZ_OK) && (pfile_info != NULL))
cannam@128 1149 {
cannam@128 1150 pfile_info->version = file_info64.version;
cannam@128 1151 pfile_info->version_needed = file_info64.version_needed;
cannam@128 1152 pfile_info->flag = file_info64.flag;
cannam@128 1153 pfile_info->compression_method = file_info64.compression_method;
cannam@128 1154 pfile_info->dosDate = file_info64.dosDate;
cannam@128 1155 pfile_info->crc = file_info64.crc;
cannam@128 1156
cannam@128 1157 pfile_info->size_filename = file_info64.size_filename;
cannam@128 1158 pfile_info->size_file_extra = file_info64.size_file_extra;
cannam@128 1159 pfile_info->size_file_comment = file_info64.size_file_comment;
cannam@128 1160
cannam@128 1161 pfile_info->disk_num_start = file_info64.disk_num_start;
cannam@128 1162 pfile_info->internal_fa = file_info64.internal_fa;
cannam@128 1163 pfile_info->external_fa = file_info64.external_fa;
cannam@128 1164
cannam@128 1165 pfile_info->tmu_date = file_info64.tmu_date,
cannam@128 1166
cannam@128 1167
cannam@128 1168 pfile_info->compressed_size = (uLong)file_info64.compressed_size;
cannam@128 1169 pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size;
cannam@128 1170
cannam@128 1171 }
cannam@128 1172 return err;
cannam@128 1173 }
cannam@128 1174 /*
cannam@128 1175 Set the current file of the zipfile to the first file.
cannam@128 1176 return UNZ_OK if there is no problem
cannam@128 1177 */
cannam@128 1178 extern int ZEXPORT unzGoToFirstFile (unzFile file)
cannam@128 1179 {
cannam@128 1180 int err=UNZ_OK;
cannam@128 1181 unz64_s* s;
cannam@128 1182 if (file==NULL)
cannam@128 1183 return UNZ_PARAMERROR;
cannam@128 1184 s=(unz64_s*)file;
cannam@128 1185 s->pos_in_central_dir=s->offset_central_dir;
cannam@128 1186 s->num_file=0;
cannam@128 1187 err=unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
cannam@128 1188 &s->cur_file_info_internal,
cannam@128 1189 NULL,0,NULL,0,NULL,0);
cannam@128 1190 s->current_file_ok = (err == UNZ_OK);
cannam@128 1191 return err;
cannam@128 1192 }
cannam@128 1193
cannam@128 1194 /*
cannam@128 1195 Set the current file of the zipfile to the next file.
cannam@128 1196 return UNZ_OK if there is no problem
cannam@128 1197 return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
cannam@128 1198 */
cannam@128 1199 extern int ZEXPORT unzGoToNextFile (unzFile file)
cannam@128 1200 {
cannam@128 1201 unz64_s* s;
cannam@128 1202 int err;
cannam@128 1203
cannam@128 1204 if (file==NULL)
cannam@128 1205 return UNZ_PARAMERROR;
cannam@128 1206 s=(unz64_s*)file;
cannam@128 1207 if (!s->current_file_ok)
cannam@128 1208 return UNZ_END_OF_LIST_OF_FILE;
cannam@128 1209 if (s->gi.number_entry != 0xffff) /* 2^16 files overflow hack */
cannam@128 1210 if (s->num_file+1==s->gi.number_entry)
cannam@128 1211 return UNZ_END_OF_LIST_OF_FILE;
cannam@128 1212
cannam@128 1213 s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
cannam@128 1214 s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
cannam@128 1215 s->num_file++;
cannam@128 1216 err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
cannam@128 1217 &s->cur_file_info_internal,
cannam@128 1218 NULL,0,NULL,0,NULL,0);
cannam@128 1219 s->current_file_ok = (err == UNZ_OK);
cannam@128 1220 return err;
cannam@128 1221 }
cannam@128 1222
cannam@128 1223
cannam@128 1224 /*
cannam@128 1225 Try locate the file szFileName in the zipfile.
cannam@128 1226 For the iCaseSensitivity signification, see unzStringFileNameCompare
cannam@128 1227
cannam@128 1228 return value :
cannam@128 1229 UNZ_OK if the file is found. It becomes the current file.
cannam@128 1230 UNZ_END_OF_LIST_OF_FILE if the file is not found
cannam@128 1231 */
cannam@128 1232 extern int ZEXPORT unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
cannam@128 1233 {
cannam@128 1234 unz64_s* s;
cannam@128 1235 int err;
cannam@128 1236
cannam@128 1237 /* We remember the 'current' position in the file so that we can jump
cannam@128 1238 * back there if we fail.
cannam@128 1239 */
cannam@128 1240 unz_file_info64 cur_file_infoSaved;
cannam@128 1241 unz_file_info64_internal cur_file_info_internalSaved;
cannam@128 1242 ZPOS64_T num_fileSaved;
cannam@128 1243 ZPOS64_T pos_in_central_dirSaved;
cannam@128 1244
cannam@128 1245
cannam@128 1246 if (file==NULL)
cannam@128 1247 return UNZ_PARAMERROR;
cannam@128 1248
cannam@128 1249 if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
cannam@128 1250 return UNZ_PARAMERROR;
cannam@128 1251
cannam@128 1252 s=(unz64_s*)file;
cannam@128 1253 if (!s->current_file_ok)
cannam@128 1254 return UNZ_END_OF_LIST_OF_FILE;
cannam@128 1255
cannam@128 1256 /* Save the current state */
cannam@128 1257 num_fileSaved = s->num_file;
cannam@128 1258 pos_in_central_dirSaved = s->pos_in_central_dir;
cannam@128 1259 cur_file_infoSaved = s->cur_file_info;
cannam@128 1260 cur_file_info_internalSaved = s->cur_file_info_internal;
cannam@128 1261
cannam@128 1262 err = unzGoToFirstFile(file);
cannam@128 1263
cannam@128 1264 while (err == UNZ_OK)
cannam@128 1265 {
cannam@128 1266 char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
cannam@128 1267 err = unzGetCurrentFileInfo64(file,NULL,
cannam@128 1268 szCurrentFileName,sizeof(szCurrentFileName)-1,
cannam@128 1269 NULL,0,NULL,0);
cannam@128 1270 if (err == UNZ_OK)
cannam@128 1271 {
cannam@128 1272 if (unzStringFileNameCompare(szCurrentFileName,
cannam@128 1273 szFileName,iCaseSensitivity)==0)
cannam@128 1274 return UNZ_OK;
cannam@128 1275 err = unzGoToNextFile(file);
cannam@128 1276 }
cannam@128 1277 }
cannam@128 1278
cannam@128 1279 /* We failed, so restore the state of the 'current file' to where we
cannam@128 1280 * were.
cannam@128 1281 */
cannam@128 1282 s->num_file = num_fileSaved ;
cannam@128 1283 s->pos_in_central_dir = pos_in_central_dirSaved ;
cannam@128 1284 s->cur_file_info = cur_file_infoSaved;
cannam@128 1285 s->cur_file_info_internal = cur_file_info_internalSaved;
cannam@128 1286 return err;
cannam@128 1287 }
cannam@128 1288
cannam@128 1289
cannam@128 1290 /*
cannam@128 1291 ///////////////////////////////////////////
cannam@128 1292 // Contributed by Ryan Haksi (mailto://cryogen@infoserve.net)
cannam@128 1293 // I need random access
cannam@128 1294 //
cannam@128 1295 // Further optimization could be realized by adding an ability
cannam@128 1296 // to cache the directory in memory. The goal being a single
cannam@128 1297 // comprehensive file read to put the file I need in a memory.
cannam@128 1298 */
cannam@128 1299
cannam@128 1300 /*
cannam@128 1301 typedef struct unz_file_pos_s
cannam@128 1302 {
cannam@128 1303 ZPOS64_T pos_in_zip_directory; // offset in file
cannam@128 1304 ZPOS64_T num_of_file; // # of file
cannam@128 1305 } unz_file_pos;
cannam@128 1306 */
cannam@128 1307
cannam@128 1308 extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos* file_pos)
cannam@128 1309 {
cannam@128 1310 unz64_s* s;
cannam@128 1311
cannam@128 1312 if (file==NULL || file_pos==NULL)
cannam@128 1313 return UNZ_PARAMERROR;
cannam@128 1314 s=(unz64_s*)file;
cannam@128 1315 if (!s->current_file_ok)
cannam@128 1316 return UNZ_END_OF_LIST_OF_FILE;
cannam@128 1317
cannam@128 1318 file_pos->pos_in_zip_directory = s->pos_in_central_dir;
cannam@128 1319 file_pos->num_of_file = s->num_file;
cannam@128 1320
cannam@128 1321 return UNZ_OK;
cannam@128 1322 }
cannam@128 1323
cannam@128 1324 extern int ZEXPORT unzGetFilePos(
cannam@128 1325 unzFile file,
cannam@128 1326 unz_file_pos* file_pos)
cannam@128 1327 {
cannam@128 1328 unz64_file_pos file_pos64;
cannam@128 1329 int err = unzGetFilePos64(file,&file_pos64);
cannam@128 1330 if (err==UNZ_OK)
cannam@128 1331 {
cannam@128 1332 file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory;
cannam@128 1333 file_pos->num_of_file = (uLong)file_pos64.num_of_file;
cannam@128 1334 }
cannam@128 1335 return err;
cannam@128 1336 }
cannam@128 1337
cannam@128 1338 extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos)
cannam@128 1339 {
cannam@128 1340 unz64_s* s;
cannam@128 1341 int err;
cannam@128 1342
cannam@128 1343 if (file==NULL || file_pos==NULL)
cannam@128 1344 return UNZ_PARAMERROR;
cannam@128 1345 s=(unz64_s*)file;
cannam@128 1346
cannam@128 1347 /* jump to the right spot */
cannam@128 1348 s->pos_in_central_dir = file_pos->pos_in_zip_directory;
cannam@128 1349 s->num_file = file_pos->num_of_file;
cannam@128 1350
cannam@128 1351 /* set the current file */
cannam@128 1352 err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
cannam@128 1353 &s->cur_file_info_internal,
cannam@128 1354 NULL,0,NULL,0,NULL,0);
cannam@128 1355 /* return results */
cannam@128 1356 s->current_file_ok = (err == UNZ_OK);
cannam@128 1357 return err;
cannam@128 1358 }
cannam@128 1359
cannam@128 1360 extern int ZEXPORT unzGoToFilePos(
cannam@128 1361 unzFile file,
cannam@128 1362 unz_file_pos* file_pos)
cannam@128 1363 {
cannam@128 1364 unz64_file_pos file_pos64;
cannam@128 1365 if (file_pos == NULL)
cannam@128 1366 return UNZ_PARAMERROR;
cannam@128 1367
cannam@128 1368 file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
cannam@128 1369 file_pos64.num_of_file = file_pos->num_of_file;
cannam@128 1370 return unzGoToFilePos64(file,&file_pos64);
cannam@128 1371 }
cannam@128 1372
cannam@128 1373 /*
cannam@128 1374 // Unzip Helper Functions - should be here?
cannam@128 1375 ///////////////////////////////////////////
cannam@128 1376 */
cannam@128 1377
cannam@128 1378 /*
cannam@128 1379 Read the local header of the current zipfile
cannam@128 1380 Check the coherency of the local header and info in the end of central
cannam@128 1381 directory about this file
cannam@128 1382 store in *piSizeVar the size of extra info in local header
cannam@128 1383 (filename and size of extra field data)
cannam@128 1384 */
cannam@128 1385 local int unz64local_CheckCurrentFileCoherencyHeader (unz64_s* s, uInt* piSizeVar,
cannam@128 1386 ZPOS64_T * poffset_local_extrafield,
cannam@128 1387 uInt * psize_local_extrafield)
cannam@128 1388 {
cannam@128 1389 uLong uMagic,uData,uFlags;
cannam@128 1390 uLong size_filename;
cannam@128 1391 uLong size_extra_field;
cannam@128 1392 int err=UNZ_OK;
cannam@128 1393
cannam@128 1394 *piSizeVar = 0;
cannam@128 1395 *poffset_local_extrafield = 0;
cannam@128 1396 *psize_local_extrafield = 0;
cannam@128 1397
cannam@128 1398 if (ZSEEK64(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile +
cannam@128 1399 s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 1400 return UNZ_ERRNO;
cannam@128 1401
cannam@128 1402
cannam@128 1403 if (err==UNZ_OK)
cannam@128 1404 {
cannam@128 1405 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
cannam@128 1406 err=UNZ_ERRNO;
cannam@128 1407 else if (uMagic!=0x04034b50)
cannam@128 1408 err=UNZ_BADZIPFILE;
cannam@128 1409 }
cannam@128 1410
cannam@128 1411 if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
cannam@128 1412 err=UNZ_ERRNO;
cannam@128 1413 /*
cannam@128 1414 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
cannam@128 1415 err=UNZ_BADZIPFILE;
cannam@128 1416 */
cannam@128 1417 if (unz64local_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK)
cannam@128 1418 err=UNZ_ERRNO;
cannam@128 1419
cannam@128 1420 if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
cannam@128 1421 err=UNZ_ERRNO;
cannam@128 1422 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
cannam@128 1423 err=UNZ_BADZIPFILE;
cannam@128 1424
cannam@128 1425 if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
cannam@128 1426 /* #ifdef HAVE_BZIP2 */
cannam@128 1427 (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
cannam@128 1428 /* #endif */
cannam@128 1429 (s->cur_file_info.compression_method!=Z_DEFLATED))
cannam@128 1430 err=UNZ_BADZIPFILE;
cannam@128 1431
cannam@128 1432 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */
cannam@128 1433 err=UNZ_ERRNO;
cannam@128 1434
cannam@128 1435 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */
cannam@128 1436 err=UNZ_ERRNO;
cannam@128 1437 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && ((uFlags & 8)==0))
cannam@128 1438 err=UNZ_BADZIPFILE;
cannam@128 1439
cannam@128 1440 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */
cannam@128 1441 err=UNZ_ERRNO;
cannam@128 1442 else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && ((uFlags & 8)==0))
cannam@128 1443 err=UNZ_BADZIPFILE;
cannam@128 1444
cannam@128 1445 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */
cannam@128 1446 err=UNZ_ERRNO;
cannam@128 1447 else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && ((uFlags & 8)==0))
cannam@128 1448 err=UNZ_BADZIPFILE;
cannam@128 1449
cannam@128 1450 if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK)
cannam@128 1451 err=UNZ_ERRNO;
cannam@128 1452 else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
cannam@128 1453 err=UNZ_BADZIPFILE;
cannam@128 1454
cannam@128 1455 *piSizeVar += (uInt)size_filename;
cannam@128 1456
cannam@128 1457 if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK)
cannam@128 1458 err=UNZ_ERRNO;
cannam@128 1459 *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
cannam@128 1460 SIZEZIPLOCALHEADER + size_filename;
cannam@128 1461 *psize_local_extrafield = (uInt)size_extra_field;
cannam@128 1462
cannam@128 1463 *piSizeVar += (uInt)size_extra_field;
cannam@128 1464
cannam@128 1465 return err;
cannam@128 1466 }
cannam@128 1467
cannam@128 1468 /*
cannam@128 1469 Open for reading data the current file in the zipfile.
cannam@128 1470 If there is no error and the file is opened, the return value is UNZ_OK.
cannam@128 1471 */
cannam@128 1472 extern int ZEXPORT unzOpenCurrentFile3 (unzFile file, int* method,
cannam@128 1473 int* level, int raw, const char* password)
cannam@128 1474 {
cannam@128 1475 int err=UNZ_OK;
cannam@128 1476 uInt iSizeVar;
cannam@128 1477 unz64_s* s;
cannam@128 1478 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 1479 ZPOS64_T offset_local_extrafield; /* offset of the local extra field */
cannam@128 1480 uInt size_local_extrafield; /* size of the local extra field */
cannam@128 1481 # ifndef NOUNCRYPT
cannam@128 1482 char source[12];
cannam@128 1483 # else
cannam@128 1484 if (password != NULL)
cannam@128 1485 return UNZ_PARAMERROR;
cannam@128 1486 # endif
cannam@128 1487
cannam@128 1488 if (file==NULL)
cannam@128 1489 return UNZ_PARAMERROR;
cannam@128 1490 s=(unz64_s*)file;
cannam@128 1491 if (!s->current_file_ok)
cannam@128 1492 return UNZ_PARAMERROR;
cannam@128 1493
cannam@128 1494 if (s->pfile_in_zip_read != NULL)
cannam@128 1495 unzCloseCurrentFile(file);
cannam@128 1496
cannam@128 1497 if (unz64local_CheckCurrentFileCoherencyHeader(s,&iSizeVar, &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
cannam@128 1498 return UNZ_BADZIPFILE;
cannam@128 1499
cannam@128 1500 pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s));
cannam@128 1501 if (pfile_in_zip_read_info==NULL)
cannam@128 1502 return UNZ_INTERNALERROR;
cannam@128 1503
cannam@128 1504 pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
cannam@128 1505 pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
cannam@128 1506 pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
cannam@128 1507 pfile_in_zip_read_info->pos_local_extrafield=0;
cannam@128 1508 pfile_in_zip_read_info->raw=raw;
cannam@128 1509
cannam@128 1510 if (pfile_in_zip_read_info->read_buffer==NULL)
cannam@128 1511 {
cannam@128 1512 TRYFREE(pfile_in_zip_read_info);
cannam@128 1513 return UNZ_INTERNALERROR;
cannam@128 1514 }
cannam@128 1515
cannam@128 1516 pfile_in_zip_read_info->stream_initialised=0;
cannam@128 1517
cannam@128 1518 if (method!=NULL)
cannam@128 1519 *method = (int)s->cur_file_info.compression_method;
cannam@128 1520
cannam@128 1521 if (level!=NULL)
cannam@128 1522 {
cannam@128 1523 *level = 6;
cannam@128 1524 switch (s->cur_file_info.flag & 0x06)
cannam@128 1525 {
cannam@128 1526 case 6 : *level = 1; break;
cannam@128 1527 case 4 : *level = 2; break;
cannam@128 1528 case 2 : *level = 9; break;
cannam@128 1529 }
cannam@128 1530 }
cannam@128 1531
cannam@128 1532 if ((s->cur_file_info.compression_method!=0) &&
cannam@128 1533 /* #ifdef HAVE_BZIP2 */
cannam@128 1534 (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
cannam@128 1535 /* #endif */
cannam@128 1536 (s->cur_file_info.compression_method!=Z_DEFLATED))
cannam@128 1537
cannam@128 1538 err=UNZ_BADZIPFILE;
cannam@128 1539
cannam@128 1540 pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
cannam@128 1541 pfile_in_zip_read_info->crc32=0;
cannam@128 1542 pfile_in_zip_read_info->total_out_64=0;
cannam@128 1543 pfile_in_zip_read_info->compression_method = s->cur_file_info.compression_method;
cannam@128 1544 pfile_in_zip_read_info->filestream=s->filestream;
cannam@128 1545 pfile_in_zip_read_info->z_filefunc=s->z_filefunc;
cannam@128 1546 pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
cannam@128 1547
cannam@128 1548 pfile_in_zip_read_info->stream.total_out = 0;
cannam@128 1549
cannam@128 1550 if ((s->cur_file_info.compression_method==Z_BZIP2ED) && (!raw))
cannam@128 1551 {
cannam@128 1552 #ifdef HAVE_BZIP2
cannam@128 1553 pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
cannam@128 1554 pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
cannam@128 1555 pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
cannam@128 1556 pfile_in_zip_read_info->bstream.state = (voidpf)0;
cannam@128 1557
cannam@128 1558 pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
cannam@128 1559 pfile_in_zip_read_info->stream.zfree = (free_func)0;
cannam@128 1560 pfile_in_zip_read_info->stream.opaque = (voidpf)0;
cannam@128 1561 pfile_in_zip_read_info->stream.next_in = (voidpf)0;
cannam@128 1562 pfile_in_zip_read_info->stream.avail_in = 0;
cannam@128 1563
cannam@128 1564 err=BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
cannam@128 1565 if (err == Z_OK)
cannam@128 1566 pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED;
cannam@128 1567 else
cannam@128 1568 {
cannam@128 1569 TRYFREE(pfile_in_zip_read_info);
cannam@128 1570 return err;
cannam@128 1571 }
cannam@128 1572 #else
cannam@128 1573 pfile_in_zip_read_info->raw=1;
cannam@128 1574 #endif
cannam@128 1575 }
cannam@128 1576 else if ((s->cur_file_info.compression_method==Z_DEFLATED) && (!raw))
cannam@128 1577 {
cannam@128 1578 pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
cannam@128 1579 pfile_in_zip_read_info->stream.zfree = (free_func)0;
cannam@128 1580 pfile_in_zip_read_info->stream.opaque = (voidpf)0;
cannam@128 1581 pfile_in_zip_read_info->stream.next_in = 0;
cannam@128 1582 pfile_in_zip_read_info->stream.avail_in = 0;
cannam@128 1583
cannam@128 1584 err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
cannam@128 1585 if (err == Z_OK)
cannam@128 1586 pfile_in_zip_read_info->stream_initialised=Z_DEFLATED;
cannam@128 1587 else
cannam@128 1588 {
cannam@128 1589 TRYFREE(pfile_in_zip_read_info);
cannam@128 1590 return err;
cannam@128 1591 }
cannam@128 1592 /* windowBits is passed < 0 to tell that there is no zlib header.
cannam@128 1593 * Note that in this case inflate *requires* an extra "dummy" byte
cannam@128 1594 * after the compressed stream in order to complete decompression and
cannam@128 1595 * return Z_STREAM_END.
cannam@128 1596 * In unzip, i don't wait absolutely Z_STREAM_END because I known the
cannam@128 1597 * size of both compressed and uncompressed data
cannam@128 1598 */
cannam@128 1599 }
cannam@128 1600 pfile_in_zip_read_info->rest_read_compressed =
cannam@128 1601 s->cur_file_info.compressed_size ;
cannam@128 1602 pfile_in_zip_read_info->rest_read_uncompressed =
cannam@128 1603 s->cur_file_info.uncompressed_size ;
cannam@128 1604
cannam@128 1605
cannam@128 1606 pfile_in_zip_read_info->pos_in_zipfile =
cannam@128 1607 s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
cannam@128 1608 iSizeVar;
cannam@128 1609
cannam@128 1610 pfile_in_zip_read_info->stream.avail_in = (uInt)0;
cannam@128 1611
cannam@128 1612 s->pfile_in_zip_read = pfile_in_zip_read_info;
cannam@128 1613 s->encrypted = 0;
cannam@128 1614
cannam@128 1615 # ifndef NOUNCRYPT
cannam@128 1616 if (password != NULL)
cannam@128 1617 {
cannam@128 1618 int i;
cannam@128 1619 s->pcrc_32_tab = get_crc_table();
cannam@128 1620 init_keys(password,s->keys,s->pcrc_32_tab);
cannam@128 1621 if (ZSEEK64(s->z_filefunc, s->filestream,
cannam@128 1622 s->pfile_in_zip_read->pos_in_zipfile +
cannam@128 1623 s->pfile_in_zip_read->byte_before_the_zipfile,
cannam@128 1624 SEEK_SET)!=0)
cannam@128 1625 return UNZ_INTERNALERROR;
cannam@128 1626 if(ZREAD64(s->z_filefunc, s->filestream,source, 12)<12)
cannam@128 1627 return UNZ_INTERNALERROR;
cannam@128 1628
cannam@128 1629 for (i = 0; i<12; i++)
cannam@128 1630 zdecode(s->keys,s->pcrc_32_tab,source[i]);
cannam@128 1631
cannam@128 1632 s->pfile_in_zip_read->pos_in_zipfile+=12;
cannam@128 1633 s->encrypted=1;
cannam@128 1634 }
cannam@128 1635 # endif
cannam@128 1636
cannam@128 1637
cannam@128 1638 return UNZ_OK;
cannam@128 1639 }
cannam@128 1640
cannam@128 1641 extern int ZEXPORT unzOpenCurrentFile (unzFile file)
cannam@128 1642 {
cannam@128 1643 return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
cannam@128 1644 }
cannam@128 1645
cannam@128 1646 extern int ZEXPORT unzOpenCurrentFilePassword (unzFile file, const char* password)
cannam@128 1647 {
cannam@128 1648 return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
cannam@128 1649 }
cannam@128 1650
cannam@128 1651 extern int ZEXPORT unzOpenCurrentFile2 (unzFile file, int* method, int* level, int raw)
cannam@128 1652 {
cannam@128 1653 return unzOpenCurrentFile3(file, method, level, raw, NULL);
cannam@128 1654 }
cannam@128 1655
cannam@128 1656 /** Addition for GDAL : START */
cannam@128 1657
cannam@128 1658 extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64( unzFile file)
cannam@128 1659 {
cannam@128 1660 unz64_s* s;
cannam@128 1661 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 1662 s=(unz64_s*)file;
cannam@128 1663 if (file==NULL)
cannam@128 1664 return 0; //UNZ_PARAMERROR;
cannam@128 1665 pfile_in_zip_read_info=s->pfile_in_zip_read;
cannam@128 1666 if (pfile_in_zip_read_info==NULL)
cannam@128 1667 return 0; //UNZ_PARAMERROR;
cannam@128 1668 return pfile_in_zip_read_info->pos_in_zipfile +
cannam@128 1669 pfile_in_zip_read_info->byte_before_the_zipfile;
cannam@128 1670 }
cannam@128 1671
cannam@128 1672 /** Addition for GDAL : END */
cannam@128 1673
cannam@128 1674 /*
cannam@128 1675 Read bytes from the current file.
cannam@128 1676 buf contain buffer where data must be copied
cannam@128 1677 len the size of buf.
cannam@128 1678
cannam@128 1679 return the number of byte copied if somes bytes are copied
cannam@128 1680 return 0 if the end of file was reached
cannam@128 1681 return <0 with error code if there is an error
cannam@128 1682 (UNZ_ERRNO for IO error, or zLib error for uncompress error)
cannam@128 1683 */
cannam@128 1684 extern int ZEXPORT unzReadCurrentFile (unzFile file, voidp buf, unsigned len)
cannam@128 1685 {
cannam@128 1686 int err=UNZ_OK;
cannam@128 1687 uInt iRead = 0;
cannam@128 1688 unz64_s* s;
cannam@128 1689 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 1690 if (file==NULL)
cannam@128 1691 return UNZ_PARAMERROR;
cannam@128 1692 s=(unz64_s*)file;
cannam@128 1693 pfile_in_zip_read_info=s->pfile_in_zip_read;
cannam@128 1694
cannam@128 1695 if (pfile_in_zip_read_info==NULL)
cannam@128 1696 return UNZ_PARAMERROR;
cannam@128 1697
cannam@128 1698
cannam@128 1699 if (pfile_in_zip_read_info->read_buffer == NULL)
cannam@128 1700 return UNZ_END_OF_LIST_OF_FILE;
cannam@128 1701 if (len==0)
cannam@128 1702 return 0;
cannam@128 1703
cannam@128 1704 pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
cannam@128 1705
cannam@128 1706 pfile_in_zip_read_info->stream.avail_out = (uInt)len;
cannam@128 1707
cannam@128 1708 if ((len>pfile_in_zip_read_info->rest_read_uncompressed) &&
cannam@128 1709 (!(pfile_in_zip_read_info->raw)))
cannam@128 1710 pfile_in_zip_read_info->stream.avail_out =
cannam@128 1711 (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
cannam@128 1712
cannam@128 1713 if ((len>pfile_in_zip_read_info->rest_read_compressed+
cannam@128 1714 pfile_in_zip_read_info->stream.avail_in) &&
cannam@128 1715 (pfile_in_zip_read_info->raw))
cannam@128 1716 pfile_in_zip_read_info->stream.avail_out =
cannam@128 1717 (uInt)pfile_in_zip_read_info->rest_read_compressed+
cannam@128 1718 pfile_in_zip_read_info->stream.avail_in;
cannam@128 1719
cannam@128 1720 while (pfile_in_zip_read_info->stream.avail_out>0)
cannam@128 1721 {
cannam@128 1722 if ((pfile_in_zip_read_info->stream.avail_in==0) &&
cannam@128 1723 (pfile_in_zip_read_info->rest_read_compressed>0))
cannam@128 1724 {
cannam@128 1725 uInt uReadThis = UNZ_BUFSIZE;
cannam@128 1726 if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
cannam@128 1727 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
cannam@128 1728 if (uReadThis == 0)
cannam@128 1729 return UNZ_EOF;
cannam@128 1730 if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
cannam@128 1731 pfile_in_zip_read_info->filestream,
cannam@128 1732 pfile_in_zip_read_info->pos_in_zipfile +
cannam@128 1733 pfile_in_zip_read_info->byte_before_the_zipfile,
cannam@128 1734 ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 1735 return UNZ_ERRNO;
cannam@128 1736 if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
cannam@128 1737 pfile_in_zip_read_info->filestream,
cannam@128 1738 pfile_in_zip_read_info->read_buffer,
cannam@128 1739 uReadThis)!=uReadThis)
cannam@128 1740 return UNZ_ERRNO;
cannam@128 1741
cannam@128 1742
cannam@128 1743 # ifndef NOUNCRYPT
cannam@128 1744 if(s->encrypted)
cannam@128 1745 {
cannam@128 1746 uInt i;
cannam@128 1747 for(i=0;i<uReadThis;i++)
cannam@128 1748 pfile_in_zip_read_info->read_buffer[i] =
cannam@128 1749 zdecode(s->keys,s->pcrc_32_tab,
cannam@128 1750 pfile_in_zip_read_info->read_buffer[i]);
cannam@128 1751 }
cannam@128 1752 # endif
cannam@128 1753
cannam@128 1754
cannam@128 1755 pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
cannam@128 1756
cannam@128 1757 pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
cannam@128 1758
cannam@128 1759 pfile_in_zip_read_info->stream.next_in =
cannam@128 1760 (Bytef*)pfile_in_zip_read_info->read_buffer;
cannam@128 1761 pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
cannam@128 1762 }
cannam@128 1763
cannam@128 1764 if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw))
cannam@128 1765 {
cannam@128 1766 uInt uDoCopy,i ;
cannam@128 1767
cannam@128 1768 if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
cannam@128 1769 (pfile_in_zip_read_info->rest_read_compressed == 0))
cannam@128 1770 return (iRead==0) ? UNZ_EOF : iRead;
cannam@128 1771
cannam@128 1772 if (pfile_in_zip_read_info->stream.avail_out <
cannam@128 1773 pfile_in_zip_read_info->stream.avail_in)
cannam@128 1774 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
cannam@128 1775 else
cannam@128 1776 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
cannam@128 1777
cannam@128 1778 for (i=0;i<uDoCopy;i++)
cannam@128 1779 *(pfile_in_zip_read_info->stream.next_out+i) =
cannam@128 1780 *(pfile_in_zip_read_info->stream.next_in+i);
cannam@128 1781
cannam@128 1782 pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uDoCopy;
cannam@128 1783
cannam@128 1784 pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
cannam@128 1785 pfile_in_zip_read_info->stream.next_out,
cannam@128 1786 uDoCopy);
cannam@128 1787 pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
cannam@128 1788 pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
cannam@128 1789 pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
cannam@128 1790 pfile_in_zip_read_info->stream.next_out += uDoCopy;
cannam@128 1791 pfile_in_zip_read_info->stream.next_in += uDoCopy;
cannam@128 1792 pfile_in_zip_read_info->stream.total_out += uDoCopy;
cannam@128 1793 iRead += uDoCopy;
cannam@128 1794 }
cannam@128 1795 else if (pfile_in_zip_read_info->compression_method==Z_BZIP2ED)
cannam@128 1796 {
cannam@128 1797 #ifdef HAVE_BZIP2
cannam@128 1798 uLong uTotalOutBefore,uTotalOutAfter;
cannam@128 1799 const Bytef *bufBefore;
cannam@128 1800 uLong uOutThis;
cannam@128 1801
cannam@128 1802 pfile_in_zip_read_info->bstream.next_in = (char*)pfile_in_zip_read_info->stream.next_in;
cannam@128 1803 pfile_in_zip_read_info->bstream.avail_in = pfile_in_zip_read_info->stream.avail_in;
cannam@128 1804 pfile_in_zip_read_info->bstream.total_in_lo32 = pfile_in_zip_read_info->stream.total_in;
cannam@128 1805 pfile_in_zip_read_info->bstream.total_in_hi32 = 0;
cannam@128 1806 pfile_in_zip_read_info->bstream.next_out = (char*)pfile_in_zip_read_info->stream.next_out;
cannam@128 1807 pfile_in_zip_read_info->bstream.avail_out = pfile_in_zip_read_info->stream.avail_out;
cannam@128 1808 pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out;
cannam@128 1809 pfile_in_zip_read_info->bstream.total_out_hi32 = 0;
cannam@128 1810
cannam@128 1811 uTotalOutBefore = pfile_in_zip_read_info->bstream.total_out_lo32;
cannam@128 1812 bufBefore = (const Bytef *)pfile_in_zip_read_info->bstream.next_out;
cannam@128 1813
cannam@128 1814 err=BZ2_bzDecompress(&pfile_in_zip_read_info->bstream);
cannam@128 1815
cannam@128 1816 uTotalOutAfter = pfile_in_zip_read_info->bstream.total_out_lo32;
cannam@128 1817 uOutThis = uTotalOutAfter-uTotalOutBefore;
cannam@128 1818
cannam@128 1819 pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
cannam@128 1820
cannam@128 1821 pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,bufBefore, (uInt)(uOutThis));
cannam@128 1822 pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
cannam@128 1823 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
cannam@128 1824
cannam@128 1825 pfile_in_zip_read_info->stream.next_in = (Bytef*)pfile_in_zip_read_info->bstream.next_in;
cannam@128 1826 pfile_in_zip_read_info->stream.avail_in = pfile_in_zip_read_info->bstream.avail_in;
cannam@128 1827 pfile_in_zip_read_info->stream.total_in = pfile_in_zip_read_info->bstream.total_in_lo32;
cannam@128 1828 pfile_in_zip_read_info->stream.next_out = (Bytef*)pfile_in_zip_read_info->bstream.next_out;
cannam@128 1829 pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out;
cannam@128 1830 pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32;
cannam@128 1831
cannam@128 1832 if (err==BZ_STREAM_END)
cannam@128 1833 return (iRead==0) ? UNZ_EOF : iRead;
cannam@128 1834 if (err!=BZ_OK)
cannam@128 1835 break;
cannam@128 1836 #endif
cannam@128 1837 } // end Z_BZIP2ED
cannam@128 1838 else
cannam@128 1839 {
cannam@128 1840 ZPOS64_T uTotalOutBefore,uTotalOutAfter;
cannam@128 1841 const Bytef *bufBefore;
cannam@128 1842 ZPOS64_T uOutThis;
cannam@128 1843 int flush=Z_SYNC_FLUSH;
cannam@128 1844
cannam@128 1845 uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
cannam@128 1846 bufBefore = pfile_in_zip_read_info->stream.next_out;
cannam@128 1847
cannam@128 1848 /*
cannam@128 1849 if ((pfile_in_zip_read_info->rest_read_uncompressed ==
cannam@128 1850 pfile_in_zip_read_info->stream.avail_out) &&
cannam@128 1851 (pfile_in_zip_read_info->rest_read_compressed == 0))
cannam@128 1852 flush = Z_FINISH;
cannam@128 1853 */
cannam@128 1854 err=inflate(&pfile_in_zip_read_info->stream,flush);
cannam@128 1855
cannam@128 1856 if ((err>=0) && (pfile_in_zip_read_info->stream.msg!=NULL))
cannam@128 1857 err = Z_DATA_ERROR;
cannam@128 1858
cannam@128 1859 uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
cannam@128 1860 uOutThis = uTotalOutAfter-uTotalOutBefore;
cannam@128 1861
cannam@128 1862 pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
cannam@128 1863
cannam@128 1864 pfile_in_zip_read_info->crc32 =
cannam@128 1865 crc32(pfile_in_zip_read_info->crc32,bufBefore,
cannam@128 1866 (uInt)(uOutThis));
cannam@128 1867
cannam@128 1868 pfile_in_zip_read_info->rest_read_uncompressed -=
cannam@128 1869 uOutThis;
cannam@128 1870
cannam@128 1871 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
cannam@128 1872
cannam@128 1873 if (err==Z_STREAM_END)
cannam@128 1874 return (iRead==0) ? UNZ_EOF : iRead;
cannam@128 1875 if (err!=Z_OK)
cannam@128 1876 break;
cannam@128 1877 }
cannam@128 1878 }
cannam@128 1879
cannam@128 1880 if (err==Z_OK)
cannam@128 1881 return iRead;
cannam@128 1882 return err;
cannam@128 1883 }
cannam@128 1884
cannam@128 1885
cannam@128 1886 /*
cannam@128 1887 Give the current position in uncompressed data
cannam@128 1888 */
cannam@128 1889 extern z_off_t ZEXPORT unztell (unzFile file)
cannam@128 1890 {
cannam@128 1891 unz64_s* s;
cannam@128 1892 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 1893 if (file==NULL)
cannam@128 1894 return UNZ_PARAMERROR;
cannam@128 1895 s=(unz64_s*)file;
cannam@128 1896 pfile_in_zip_read_info=s->pfile_in_zip_read;
cannam@128 1897
cannam@128 1898 if (pfile_in_zip_read_info==NULL)
cannam@128 1899 return UNZ_PARAMERROR;
cannam@128 1900
cannam@128 1901 return (z_off_t)pfile_in_zip_read_info->stream.total_out;
cannam@128 1902 }
cannam@128 1903
cannam@128 1904 extern ZPOS64_T ZEXPORT unztell64 (unzFile file)
cannam@128 1905 {
cannam@128 1906
cannam@128 1907 unz64_s* s;
cannam@128 1908 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 1909 if (file==NULL)
cannam@128 1910 return (ZPOS64_T)-1;
cannam@128 1911 s=(unz64_s*)file;
cannam@128 1912 pfile_in_zip_read_info=s->pfile_in_zip_read;
cannam@128 1913
cannam@128 1914 if (pfile_in_zip_read_info==NULL)
cannam@128 1915 return (ZPOS64_T)-1;
cannam@128 1916
cannam@128 1917 return pfile_in_zip_read_info->total_out_64;
cannam@128 1918 }
cannam@128 1919
cannam@128 1920
cannam@128 1921 /*
cannam@128 1922 return 1 if the end of file was reached, 0 elsewhere
cannam@128 1923 */
cannam@128 1924 extern int ZEXPORT unzeof (unzFile file)
cannam@128 1925 {
cannam@128 1926 unz64_s* s;
cannam@128 1927 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 1928 if (file==NULL)
cannam@128 1929 return UNZ_PARAMERROR;
cannam@128 1930 s=(unz64_s*)file;
cannam@128 1931 pfile_in_zip_read_info=s->pfile_in_zip_read;
cannam@128 1932
cannam@128 1933 if (pfile_in_zip_read_info==NULL)
cannam@128 1934 return UNZ_PARAMERROR;
cannam@128 1935
cannam@128 1936 if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
cannam@128 1937 return 1;
cannam@128 1938 else
cannam@128 1939 return 0;
cannam@128 1940 }
cannam@128 1941
cannam@128 1942
cannam@128 1943
cannam@128 1944 /*
cannam@128 1945 Read extra field from the current file (opened by unzOpenCurrentFile)
cannam@128 1946 This is the local-header version of the extra field (sometimes, there is
cannam@128 1947 more info in the local-header version than in the central-header)
cannam@128 1948
cannam@128 1949 if buf==NULL, it return the size of the local extra field that can be read
cannam@128 1950
cannam@128 1951 if buf!=NULL, len is the size of the buffer, the extra header is copied in
cannam@128 1952 buf.
cannam@128 1953 the return value is the number of bytes copied in buf, or (if <0)
cannam@128 1954 the error code
cannam@128 1955 */
cannam@128 1956 extern int ZEXPORT unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len)
cannam@128 1957 {
cannam@128 1958 unz64_s* s;
cannam@128 1959 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 1960 uInt read_now;
cannam@128 1961 ZPOS64_T size_to_read;
cannam@128 1962
cannam@128 1963 if (file==NULL)
cannam@128 1964 return UNZ_PARAMERROR;
cannam@128 1965 s=(unz64_s*)file;
cannam@128 1966 pfile_in_zip_read_info=s->pfile_in_zip_read;
cannam@128 1967
cannam@128 1968 if (pfile_in_zip_read_info==NULL)
cannam@128 1969 return UNZ_PARAMERROR;
cannam@128 1970
cannam@128 1971 size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
cannam@128 1972 pfile_in_zip_read_info->pos_local_extrafield);
cannam@128 1973
cannam@128 1974 if (buf==NULL)
cannam@128 1975 return (int)size_to_read;
cannam@128 1976
cannam@128 1977 if (len>size_to_read)
cannam@128 1978 read_now = (uInt)size_to_read;
cannam@128 1979 else
cannam@128 1980 read_now = (uInt)len ;
cannam@128 1981
cannam@128 1982 if (read_now==0)
cannam@128 1983 return 0;
cannam@128 1984
cannam@128 1985 if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
cannam@128 1986 pfile_in_zip_read_info->filestream,
cannam@128 1987 pfile_in_zip_read_info->offset_local_extrafield +
cannam@128 1988 pfile_in_zip_read_info->pos_local_extrafield,
cannam@128 1989 ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 1990 return UNZ_ERRNO;
cannam@128 1991
cannam@128 1992 if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
cannam@128 1993 pfile_in_zip_read_info->filestream,
cannam@128 1994 buf,read_now)!=read_now)
cannam@128 1995 return UNZ_ERRNO;
cannam@128 1996
cannam@128 1997 return (int)read_now;
cannam@128 1998 }
cannam@128 1999
cannam@128 2000 /*
cannam@128 2001 Close the file in zip opened with unzOpenCurrentFile
cannam@128 2002 Return UNZ_CRCERROR if all the file was read but the CRC is not good
cannam@128 2003 */
cannam@128 2004 extern int ZEXPORT unzCloseCurrentFile (unzFile file)
cannam@128 2005 {
cannam@128 2006 int err=UNZ_OK;
cannam@128 2007
cannam@128 2008 unz64_s* s;
cannam@128 2009 file_in_zip64_read_info_s* pfile_in_zip_read_info;
cannam@128 2010 if (file==NULL)
cannam@128 2011 return UNZ_PARAMERROR;
cannam@128 2012 s=(unz64_s*)file;
cannam@128 2013 pfile_in_zip_read_info=s->pfile_in_zip_read;
cannam@128 2014
cannam@128 2015 if (pfile_in_zip_read_info==NULL)
cannam@128 2016 return UNZ_PARAMERROR;
cannam@128 2017
cannam@128 2018
cannam@128 2019 if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
cannam@128 2020 (!pfile_in_zip_read_info->raw))
cannam@128 2021 {
cannam@128 2022 if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
cannam@128 2023 err=UNZ_CRCERROR;
cannam@128 2024 }
cannam@128 2025
cannam@128 2026
cannam@128 2027 TRYFREE(pfile_in_zip_read_info->read_buffer);
cannam@128 2028 pfile_in_zip_read_info->read_buffer = NULL;
cannam@128 2029 if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
cannam@128 2030 inflateEnd(&pfile_in_zip_read_info->stream);
cannam@128 2031 #ifdef HAVE_BZIP2
cannam@128 2032 else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
cannam@128 2033 BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
cannam@128 2034 #endif
cannam@128 2035
cannam@128 2036
cannam@128 2037 pfile_in_zip_read_info->stream_initialised = 0;
cannam@128 2038 TRYFREE(pfile_in_zip_read_info);
cannam@128 2039
cannam@128 2040 s->pfile_in_zip_read=NULL;
cannam@128 2041
cannam@128 2042 return err;
cannam@128 2043 }
cannam@128 2044
cannam@128 2045
cannam@128 2046 /*
cannam@128 2047 Get the global comment string of the ZipFile, in the szComment buffer.
cannam@128 2048 uSizeBuf is the size of the szComment buffer.
cannam@128 2049 return the number of byte copied or an error code <0
cannam@128 2050 */
cannam@128 2051 extern int ZEXPORT unzGetGlobalComment (unzFile file, char * szComment, uLong uSizeBuf)
cannam@128 2052 {
cannam@128 2053 unz64_s* s;
cannam@128 2054 uLong uReadThis ;
cannam@128 2055 if (file==NULL)
cannam@128 2056 return (int)UNZ_PARAMERROR;
cannam@128 2057 s=(unz64_s*)file;
cannam@128 2058
cannam@128 2059 uReadThis = uSizeBuf;
cannam@128 2060 if (uReadThis>s->gi.size_comment)
cannam@128 2061 uReadThis = s->gi.size_comment;
cannam@128 2062
cannam@128 2063 if (ZSEEK64(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0)
cannam@128 2064 return UNZ_ERRNO;
cannam@128 2065
cannam@128 2066 if (uReadThis>0)
cannam@128 2067 {
cannam@128 2068 *szComment='\0';
cannam@128 2069 if (ZREAD64(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis)
cannam@128 2070 return UNZ_ERRNO;
cannam@128 2071 }
cannam@128 2072
cannam@128 2073 if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
cannam@128 2074 *(szComment+s->gi.size_comment)='\0';
cannam@128 2075 return (int)uReadThis;
cannam@128 2076 }
cannam@128 2077
cannam@128 2078 /* Additions by RX '2004 */
cannam@128 2079 extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file)
cannam@128 2080 {
cannam@128 2081 unz64_s* s;
cannam@128 2082
cannam@128 2083 if (file==NULL)
cannam@128 2084 return 0; //UNZ_PARAMERROR;
cannam@128 2085 s=(unz64_s*)file;
cannam@128 2086 if (!s->current_file_ok)
cannam@128 2087 return 0;
cannam@128 2088 if (s->gi.number_entry != 0 && s->gi.number_entry != 0xffff)
cannam@128 2089 if (s->num_file==s->gi.number_entry)
cannam@128 2090 return 0;
cannam@128 2091 return s->pos_in_central_dir;
cannam@128 2092 }
cannam@128 2093
cannam@128 2094 extern uLong ZEXPORT unzGetOffset (unzFile file)
cannam@128 2095 {
cannam@128 2096 ZPOS64_T offset64;
cannam@128 2097
cannam@128 2098 if (file==NULL)
cannam@128 2099 return 0; //UNZ_PARAMERROR;
cannam@128 2100 offset64 = unzGetOffset64(file);
cannam@128 2101 return (uLong)offset64;
cannam@128 2102 }
cannam@128 2103
cannam@128 2104 extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos)
cannam@128 2105 {
cannam@128 2106 unz64_s* s;
cannam@128 2107 int err;
cannam@128 2108
cannam@128 2109 if (file==NULL)
cannam@128 2110 return UNZ_PARAMERROR;
cannam@128 2111 s=(unz64_s*)file;
cannam@128 2112
cannam@128 2113 s->pos_in_central_dir = pos;
cannam@128 2114 s->num_file = s->gi.number_entry; /* hack */
cannam@128 2115 err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
cannam@128 2116 &s->cur_file_info_internal,
cannam@128 2117 NULL,0,NULL,0,NULL,0);
cannam@128 2118 s->current_file_ok = (err == UNZ_OK);
cannam@128 2119 return err;
cannam@128 2120 }
cannam@128 2121
cannam@128 2122 extern int ZEXPORT unzSetOffset (unzFile file, uLong pos)
cannam@128 2123 {
cannam@128 2124 return unzSetOffset64(file,pos);
cannam@128 2125 }