annotate src/zlib-1.2.7/contrib/minizip/unzip.c @ 89:8a15ff55d9af

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