packio-simple

annotate src/minizip/unzip.c @ 2:4767e7769c32

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