packvfs

annotate test/zipcat/src/minizip/zip.c @ 3:ef6c1472607f

jesus fucking christ that was easy... written a test prog "zipcat" to try out zlib's contrib library "minizip", to list and read files out of zip archives directly...
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 04 Nov 2013 06:46:17 +0200
parents
children
rev   line source
nuclear@3 1 /* zip.c -- IO on .zip files using zlib
nuclear@3 2 Version 1.1, February 14h, 2010
nuclear@3 3 part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
nuclear@3 4
nuclear@3 5 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
nuclear@3 6
nuclear@3 7 Modifications for Zip64 support
nuclear@3 8 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
nuclear@3 9
nuclear@3 10 For more info read MiniZip_info.txt
nuclear@3 11
nuclear@3 12 Changes
nuclear@3 13 Oct-2009 - Mathias Svensson - Remove old C style function prototypes
nuclear@3 14 Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives
nuclear@3 15 Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions.
nuclear@3 16 Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data
nuclear@3 17 It is used when recreting zip archive with RAW when deleting items from a zip.
nuclear@3 18 ZIP64 data is automaticly added to items that needs it, and existing ZIP64 data need to be removed.
nuclear@3 19 Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required)
nuclear@3 20 Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
nuclear@3 21
nuclear@3 22 */
nuclear@3 23
nuclear@3 24
nuclear@3 25 #include <stdio.h>
nuclear@3 26 #include <stdlib.h>
nuclear@3 27 #include <string.h>
nuclear@3 28 #include <time.h>
nuclear@3 29 #include "zlib.h"
nuclear@3 30 #include "zip.h"
nuclear@3 31
nuclear@3 32 #ifdef STDC
nuclear@3 33 # include <stddef.h>
nuclear@3 34 # include <string.h>
nuclear@3 35 # include <stdlib.h>
nuclear@3 36 #endif
nuclear@3 37 #ifdef NO_ERRNO_H
nuclear@3 38 extern int errno;
nuclear@3 39 #else
nuclear@3 40 # include <errno.h>
nuclear@3 41 #endif
nuclear@3 42
nuclear@3 43
nuclear@3 44 #ifndef local
nuclear@3 45 # define local static
nuclear@3 46 #endif
nuclear@3 47 /* compile with -Dlocal if your debugger can't find static symbols */
nuclear@3 48
nuclear@3 49 #ifndef VERSIONMADEBY
nuclear@3 50 # define VERSIONMADEBY (0x0) /* platform depedent */
nuclear@3 51 #endif
nuclear@3 52
nuclear@3 53 #ifndef Z_BUFSIZE
nuclear@3 54 #define Z_BUFSIZE (64*1024) //(16384)
nuclear@3 55 #endif
nuclear@3 56
nuclear@3 57 #ifndef Z_MAXFILENAMEINZIP
nuclear@3 58 #define Z_MAXFILENAMEINZIP (256)
nuclear@3 59 #endif
nuclear@3 60
nuclear@3 61 #ifndef ALLOC
nuclear@3 62 # define ALLOC(size) (malloc(size))
nuclear@3 63 #endif
nuclear@3 64 #ifndef TRYFREE
nuclear@3 65 # define TRYFREE(p) {if (p) free(p);}
nuclear@3 66 #endif
nuclear@3 67
nuclear@3 68 /*
nuclear@3 69 #define SIZECENTRALDIRITEM (0x2e)
nuclear@3 70 #define SIZEZIPLOCALHEADER (0x1e)
nuclear@3 71 */
nuclear@3 72
nuclear@3 73 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
nuclear@3 74
nuclear@3 75
nuclear@3 76 // NOT sure that this work on ALL platform
nuclear@3 77 #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
nuclear@3 78
nuclear@3 79 #ifndef SEEK_CUR
nuclear@3 80 #define SEEK_CUR 1
nuclear@3 81 #endif
nuclear@3 82
nuclear@3 83 #ifndef SEEK_END
nuclear@3 84 #define SEEK_END 2
nuclear@3 85 #endif
nuclear@3 86
nuclear@3 87 #ifndef SEEK_SET
nuclear@3 88 #define SEEK_SET 0
nuclear@3 89 #endif
nuclear@3 90
nuclear@3 91 #ifndef DEF_MEM_LEVEL
nuclear@3 92 #if MAX_MEM_LEVEL >= 8
nuclear@3 93 # define DEF_MEM_LEVEL 8
nuclear@3 94 #else
nuclear@3 95 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
nuclear@3 96 #endif
nuclear@3 97 #endif
nuclear@3 98 const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
nuclear@3 99
nuclear@3 100
nuclear@3 101 #define SIZEDATA_INDATABLOCK (4096-(4*4))
nuclear@3 102
nuclear@3 103 #define LOCALHEADERMAGIC (0x04034b50)
nuclear@3 104 #define CENTRALHEADERMAGIC (0x02014b50)
nuclear@3 105 #define ENDHEADERMAGIC (0x06054b50)
nuclear@3 106 #define ZIP64ENDHEADERMAGIC (0x6064b50)
nuclear@3 107 #define ZIP64ENDLOCHEADERMAGIC (0x7064b50)
nuclear@3 108
nuclear@3 109 #define FLAG_LOCALHEADER_OFFSET (0x06)
nuclear@3 110 #define CRC_LOCALHEADER_OFFSET (0x0e)
nuclear@3 111
nuclear@3 112 #define SIZECENTRALHEADER (0x2e) /* 46 */
nuclear@3 113
nuclear@3 114 typedef struct linkedlist_datablock_internal_s
nuclear@3 115 {
nuclear@3 116 struct linkedlist_datablock_internal_s* next_datablock;
nuclear@3 117 uLong avail_in_this_block;
nuclear@3 118 uLong filled_in_this_block;
nuclear@3 119 uLong unused; /* for future use and alignement */
nuclear@3 120 unsigned char data[SIZEDATA_INDATABLOCK];
nuclear@3 121 } linkedlist_datablock_internal;
nuclear@3 122
nuclear@3 123 typedef struct linkedlist_data_s
nuclear@3 124 {
nuclear@3 125 linkedlist_datablock_internal* first_block;
nuclear@3 126 linkedlist_datablock_internal* last_block;
nuclear@3 127 } linkedlist_data;
nuclear@3 128
nuclear@3 129
nuclear@3 130 typedef struct
nuclear@3 131 {
nuclear@3 132 z_stream stream; /* zLib stream structure for inflate */
nuclear@3 133 #ifdef HAVE_BZIP2
nuclear@3 134 bz_stream bstream; /* bzLib stream structure for bziped */
nuclear@3 135 #endif
nuclear@3 136
nuclear@3 137 int stream_initialised; /* 1 is stream is initialised */
nuclear@3 138 uInt pos_in_buffered_data; /* last written byte in buffered_data */
nuclear@3 139
nuclear@3 140 ZPOS64_T pos_local_header; /* offset of the local header of the file
nuclear@3 141 currenty writing */
nuclear@3 142 char* central_header; /* central header data for the current file */
nuclear@3 143 uLong size_centralExtra;
nuclear@3 144 uLong size_centralheader; /* size of the central header for cur file */
nuclear@3 145 uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */
nuclear@3 146 uLong flag; /* flag of the file currently writing */
nuclear@3 147
nuclear@3 148 int method; /* compression method of file currenty wr.*/
nuclear@3 149 int raw; /* 1 for directly writing raw data */
nuclear@3 150 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
nuclear@3 151 uLong dosDate;
nuclear@3 152 uLong crc32;
nuclear@3 153 int encrypt;
nuclear@3 154 int zip64; /* Add ZIP64 extened information in the extra field */
nuclear@3 155 ZPOS64_T pos_zip64extrainfo;
nuclear@3 156 ZPOS64_T totalCompressedData;
nuclear@3 157 ZPOS64_T totalUncompressedData;
nuclear@3 158 #ifndef NOCRYPT
nuclear@3 159 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
nuclear@3 160 const z_crc_t* pcrc_32_tab;
nuclear@3 161 int crypt_header_size;
nuclear@3 162 #endif
nuclear@3 163 } curfile64_info;
nuclear@3 164
nuclear@3 165 typedef struct
nuclear@3 166 {
nuclear@3 167 zlib_filefunc64_32_def z_filefunc;
nuclear@3 168 voidpf filestream; /* io structore of the zipfile */
nuclear@3 169 linkedlist_data central_dir;/* datablock with central dir in construction*/
nuclear@3 170 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
nuclear@3 171 curfile64_info ci; /* info on the file curretly writing */
nuclear@3 172
nuclear@3 173 ZPOS64_T begin_pos; /* position of the beginning of the zipfile */
nuclear@3 174 ZPOS64_T add_position_when_writting_offset;
nuclear@3 175 ZPOS64_T number_entry;
nuclear@3 176
nuclear@3 177 #ifndef NO_ADDFILEINEXISTINGZIP
nuclear@3 178 char *globalcomment;
nuclear@3 179 #endif
nuclear@3 180
nuclear@3 181 } zip64_internal;
nuclear@3 182
nuclear@3 183
nuclear@3 184 #ifndef NOCRYPT
nuclear@3 185 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
nuclear@3 186 #include "crypt.h"
nuclear@3 187 #endif
nuclear@3 188
nuclear@3 189 local linkedlist_datablock_internal* allocate_new_datablock()
nuclear@3 190 {
nuclear@3 191 linkedlist_datablock_internal* ldi;
nuclear@3 192 ldi = (linkedlist_datablock_internal*)
nuclear@3 193 ALLOC(sizeof(linkedlist_datablock_internal));
nuclear@3 194 if (ldi!=NULL)
nuclear@3 195 {
nuclear@3 196 ldi->next_datablock = NULL ;
nuclear@3 197 ldi->filled_in_this_block = 0 ;
nuclear@3 198 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
nuclear@3 199 }
nuclear@3 200 return ldi;
nuclear@3 201 }
nuclear@3 202
nuclear@3 203 local void free_datablock(linkedlist_datablock_internal* ldi)
nuclear@3 204 {
nuclear@3 205 while (ldi!=NULL)
nuclear@3 206 {
nuclear@3 207 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
nuclear@3 208 TRYFREE(ldi);
nuclear@3 209 ldi = ldinext;
nuclear@3 210 }
nuclear@3 211 }
nuclear@3 212
nuclear@3 213 local void init_linkedlist(linkedlist_data* ll)
nuclear@3 214 {
nuclear@3 215 ll->first_block = ll->last_block = NULL;
nuclear@3 216 }
nuclear@3 217
nuclear@3 218 local void free_linkedlist(linkedlist_data* ll)
nuclear@3 219 {
nuclear@3 220 free_datablock(ll->first_block);
nuclear@3 221 ll->first_block = ll->last_block = NULL;
nuclear@3 222 }
nuclear@3 223
nuclear@3 224
nuclear@3 225 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len)
nuclear@3 226 {
nuclear@3 227 linkedlist_datablock_internal* ldi;
nuclear@3 228 const unsigned char* from_copy;
nuclear@3 229
nuclear@3 230 if (ll==NULL)
nuclear@3 231 return ZIP_INTERNALERROR;
nuclear@3 232
nuclear@3 233 if (ll->last_block == NULL)
nuclear@3 234 {
nuclear@3 235 ll->first_block = ll->last_block = allocate_new_datablock();
nuclear@3 236 if (ll->first_block == NULL)
nuclear@3 237 return ZIP_INTERNALERROR;
nuclear@3 238 }
nuclear@3 239
nuclear@3 240 ldi = ll->last_block;
nuclear@3 241 from_copy = (unsigned char*)buf;
nuclear@3 242
nuclear@3 243 while (len>0)
nuclear@3 244 {
nuclear@3 245 uInt copy_this;
nuclear@3 246 uInt i;
nuclear@3 247 unsigned char* to_copy;
nuclear@3 248
nuclear@3 249 if (ldi->avail_in_this_block==0)
nuclear@3 250 {
nuclear@3 251 ldi->next_datablock = allocate_new_datablock();
nuclear@3 252 if (ldi->next_datablock == NULL)
nuclear@3 253 return ZIP_INTERNALERROR;
nuclear@3 254 ldi = ldi->next_datablock ;
nuclear@3 255 ll->last_block = ldi;
nuclear@3 256 }
nuclear@3 257
nuclear@3 258 if (ldi->avail_in_this_block < len)
nuclear@3 259 copy_this = (uInt)ldi->avail_in_this_block;
nuclear@3 260 else
nuclear@3 261 copy_this = (uInt)len;
nuclear@3 262
nuclear@3 263 to_copy = &(ldi->data[ldi->filled_in_this_block]);
nuclear@3 264
nuclear@3 265 for (i=0;i<copy_this;i++)
nuclear@3 266 *(to_copy+i)=*(from_copy+i);
nuclear@3 267
nuclear@3 268 ldi->filled_in_this_block += copy_this;
nuclear@3 269 ldi->avail_in_this_block -= copy_this;
nuclear@3 270 from_copy += copy_this ;
nuclear@3 271 len -= copy_this;
nuclear@3 272 }
nuclear@3 273 return ZIP_OK;
nuclear@3 274 }
nuclear@3 275
nuclear@3 276
nuclear@3 277
nuclear@3 278 /****************************************************************************/
nuclear@3 279
nuclear@3 280 #ifndef NO_ADDFILEINEXISTINGZIP
nuclear@3 281 /* ===========================================================================
nuclear@3 282 Inputs a long in LSB order to the given file
nuclear@3 283 nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
nuclear@3 284 */
nuclear@3 285
nuclear@3 286 local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte));
nuclear@3 287 local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)
nuclear@3 288 {
nuclear@3 289 unsigned char buf[8];
nuclear@3 290 int n;
nuclear@3 291 for (n = 0; n < nbByte; n++)
nuclear@3 292 {
nuclear@3 293 buf[n] = (unsigned char)(x & 0xff);
nuclear@3 294 x >>= 8;
nuclear@3 295 }
nuclear@3 296 if (x != 0)
nuclear@3 297 { /* data overflow - hack for ZIP64 (X Roche) */
nuclear@3 298 for (n = 0; n < nbByte; n++)
nuclear@3 299 {
nuclear@3 300 buf[n] = 0xff;
nuclear@3 301 }
nuclear@3 302 }
nuclear@3 303
nuclear@3 304 if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
nuclear@3 305 return ZIP_ERRNO;
nuclear@3 306 else
nuclear@3 307 return ZIP_OK;
nuclear@3 308 }
nuclear@3 309
nuclear@3 310 local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte));
nuclear@3 311 local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte)
nuclear@3 312 {
nuclear@3 313 unsigned char* buf=(unsigned char*)dest;
nuclear@3 314 int n;
nuclear@3 315 for (n = 0; n < nbByte; n++) {
nuclear@3 316 buf[n] = (unsigned char)(x & 0xff);
nuclear@3 317 x >>= 8;
nuclear@3 318 }
nuclear@3 319
nuclear@3 320 if (x != 0)
nuclear@3 321 { /* data overflow - hack for ZIP64 */
nuclear@3 322 for (n = 0; n < nbByte; n++)
nuclear@3 323 {
nuclear@3 324 buf[n] = 0xff;
nuclear@3 325 }
nuclear@3 326 }
nuclear@3 327 }
nuclear@3 328
nuclear@3 329 /****************************************************************************/
nuclear@3 330
nuclear@3 331
nuclear@3 332 local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm)
nuclear@3 333 {
nuclear@3 334 uLong year = (uLong)ptm->tm_year;
nuclear@3 335 if (year>=1980)
nuclear@3 336 year-=1980;
nuclear@3 337 else if (year>=80)
nuclear@3 338 year-=80;
nuclear@3 339 return
nuclear@3 340 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
nuclear@3 341 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
nuclear@3 342 }
nuclear@3 343
nuclear@3 344
nuclear@3 345 /****************************************************************************/
nuclear@3 346
nuclear@3 347 local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi));
nuclear@3 348
nuclear@3 349 local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi)
nuclear@3 350 {
nuclear@3 351 unsigned char c;
nuclear@3 352 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
nuclear@3 353 if (err==1)
nuclear@3 354 {
nuclear@3 355 *pi = (int)c;
nuclear@3 356 return ZIP_OK;
nuclear@3 357 }
nuclear@3 358 else
nuclear@3 359 {
nuclear@3 360 if (ZERROR64(*pzlib_filefunc_def,filestream))
nuclear@3 361 return ZIP_ERRNO;
nuclear@3 362 else
nuclear@3 363 return ZIP_EOF;
nuclear@3 364 }
nuclear@3 365 }
nuclear@3 366
nuclear@3 367
nuclear@3 368 /* ===========================================================================
nuclear@3 369 Reads a long in LSB order from the given gz_stream. Sets
nuclear@3 370 */
nuclear@3 371 local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
nuclear@3 372
nuclear@3 373 local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
nuclear@3 374 {
nuclear@3 375 uLong x ;
nuclear@3 376 int i = 0;
nuclear@3 377 int err;
nuclear@3 378
nuclear@3 379 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 380 x = (uLong)i;
nuclear@3 381
nuclear@3 382 if (err==ZIP_OK)
nuclear@3 383 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 384 x += ((uLong)i)<<8;
nuclear@3 385
nuclear@3 386 if (err==ZIP_OK)
nuclear@3 387 *pX = x;
nuclear@3 388 else
nuclear@3 389 *pX = 0;
nuclear@3 390 return err;
nuclear@3 391 }
nuclear@3 392
nuclear@3 393 local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
nuclear@3 394
nuclear@3 395 local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
nuclear@3 396 {
nuclear@3 397 uLong x ;
nuclear@3 398 int i = 0;
nuclear@3 399 int err;
nuclear@3 400
nuclear@3 401 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 402 x = (uLong)i;
nuclear@3 403
nuclear@3 404 if (err==ZIP_OK)
nuclear@3 405 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 406 x += ((uLong)i)<<8;
nuclear@3 407
nuclear@3 408 if (err==ZIP_OK)
nuclear@3 409 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 410 x += ((uLong)i)<<16;
nuclear@3 411
nuclear@3 412 if (err==ZIP_OK)
nuclear@3 413 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 414 x += ((uLong)i)<<24;
nuclear@3 415
nuclear@3 416 if (err==ZIP_OK)
nuclear@3 417 *pX = x;
nuclear@3 418 else
nuclear@3 419 *pX = 0;
nuclear@3 420 return err;
nuclear@3 421 }
nuclear@3 422
nuclear@3 423 local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX));
nuclear@3 424
nuclear@3 425
nuclear@3 426 local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
nuclear@3 427 {
nuclear@3 428 ZPOS64_T x;
nuclear@3 429 int i = 0;
nuclear@3 430 int err;
nuclear@3 431
nuclear@3 432 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 433 x = (ZPOS64_T)i;
nuclear@3 434
nuclear@3 435 if (err==ZIP_OK)
nuclear@3 436 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 437 x += ((ZPOS64_T)i)<<8;
nuclear@3 438
nuclear@3 439 if (err==ZIP_OK)
nuclear@3 440 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 441 x += ((ZPOS64_T)i)<<16;
nuclear@3 442
nuclear@3 443 if (err==ZIP_OK)
nuclear@3 444 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 445 x += ((ZPOS64_T)i)<<24;
nuclear@3 446
nuclear@3 447 if (err==ZIP_OK)
nuclear@3 448 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 449 x += ((ZPOS64_T)i)<<32;
nuclear@3 450
nuclear@3 451 if (err==ZIP_OK)
nuclear@3 452 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 453 x += ((ZPOS64_T)i)<<40;
nuclear@3 454
nuclear@3 455 if (err==ZIP_OK)
nuclear@3 456 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 457 x += ((ZPOS64_T)i)<<48;
nuclear@3 458
nuclear@3 459 if (err==ZIP_OK)
nuclear@3 460 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
nuclear@3 461 x += ((ZPOS64_T)i)<<56;
nuclear@3 462
nuclear@3 463 if (err==ZIP_OK)
nuclear@3 464 *pX = x;
nuclear@3 465 else
nuclear@3 466 *pX = 0;
nuclear@3 467
nuclear@3 468 return err;
nuclear@3 469 }
nuclear@3 470
nuclear@3 471 #ifndef BUFREADCOMMENT
nuclear@3 472 #define BUFREADCOMMENT (0x400)
nuclear@3 473 #endif
nuclear@3 474 /*
nuclear@3 475 Locate the Central directory of a zipfile (at the end, just before
nuclear@3 476 the global comment)
nuclear@3 477 */
nuclear@3 478 local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
nuclear@3 479
nuclear@3 480 local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
nuclear@3 481 {
nuclear@3 482 unsigned char* buf;
nuclear@3 483 ZPOS64_T uSizeFile;
nuclear@3 484 ZPOS64_T uBackRead;
nuclear@3 485 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
nuclear@3 486 ZPOS64_T uPosFound=0;
nuclear@3 487
nuclear@3 488 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
nuclear@3 489 return 0;
nuclear@3 490
nuclear@3 491
nuclear@3 492 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
nuclear@3 493
nuclear@3 494 if (uMaxBack>uSizeFile)
nuclear@3 495 uMaxBack = uSizeFile;
nuclear@3 496
nuclear@3 497 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
nuclear@3 498 if (buf==NULL)
nuclear@3 499 return 0;
nuclear@3 500
nuclear@3 501 uBackRead = 4;
nuclear@3 502 while (uBackRead<uMaxBack)
nuclear@3 503 {
nuclear@3 504 uLong uReadSize;
nuclear@3 505 ZPOS64_T uReadPos ;
nuclear@3 506 int i;
nuclear@3 507 if (uBackRead+BUFREADCOMMENT>uMaxBack)
nuclear@3 508 uBackRead = uMaxBack;
nuclear@3 509 else
nuclear@3 510 uBackRead+=BUFREADCOMMENT;
nuclear@3 511 uReadPos = uSizeFile-uBackRead ;
nuclear@3 512
nuclear@3 513 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
nuclear@3 514 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
nuclear@3 515 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 516 break;
nuclear@3 517
nuclear@3 518 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
nuclear@3 519 break;
nuclear@3 520
nuclear@3 521 for (i=(int)uReadSize-3; (i--)>0;)
nuclear@3 522 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
nuclear@3 523 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
nuclear@3 524 {
nuclear@3 525 uPosFound = uReadPos+i;
nuclear@3 526 break;
nuclear@3 527 }
nuclear@3 528
nuclear@3 529 if (uPosFound!=0)
nuclear@3 530 break;
nuclear@3 531 }
nuclear@3 532 TRYFREE(buf);
nuclear@3 533 return uPosFound;
nuclear@3 534 }
nuclear@3 535
nuclear@3 536 /*
nuclear@3 537 Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before
nuclear@3 538 the global comment)
nuclear@3 539 */
nuclear@3 540 local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
nuclear@3 541
nuclear@3 542 local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
nuclear@3 543 {
nuclear@3 544 unsigned char* buf;
nuclear@3 545 ZPOS64_T uSizeFile;
nuclear@3 546 ZPOS64_T uBackRead;
nuclear@3 547 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
nuclear@3 548 ZPOS64_T uPosFound=0;
nuclear@3 549 uLong uL;
nuclear@3 550 ZPOS64_T relativeOffset;
nuclear@3 551
nuclear@3 552 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
nuclear@3 553 return 0;
nuclear@3 554
nuclear@3 555 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
nuclear@3 556
nuclear@3 557 if (uMaxBack>uSizeFile)
nuclear@3 558 uMaxBack = uSizeFile;
nuclear@3 559
nuclear@3 560 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
nuclear@3 561 if (buf==NULL)
nuclear@3 562 return 0;
nuclear@3 563
nuclear@3 564 uBackRead = 4;
nuclear@3 565 while (uBackRead<uMaxBack)
nuclear@3 566 {
nuclear@3 567 uLong uReadSize;
nuclear@3 568 ZPOS64_T uReadPos;
nuclear@3 569 int i;
nuclear@3 570 if (uBackRead+BUFREADCOMMENT>uMaxBack)
nuclear@3 571 uBackRead = uMaxBack;
nuclear@3 572 else
nuclear@3 573 uBackRead+=BUFREADCOMMENT;
nuclear@3 574 uReadPos = uSizeFile-uBackRead ;
nuclear@3 575
nuclear@3 576 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
nuclear@3 577 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
nuclear@3 578 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 579 break;
nuclear@3 580
nuclear@3 581 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
nuclear@3 582 break;
nuclear@3 583
nuclear@3 584 for (i=(int)uReadSize-3; (i--)>0;)
nuclear@3 585 {
nuclear@3 586 // Signature "0x07064b50" Zip64 end of central directory locater
nuclear@3 587 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
nuclear@3 588 {
nuclear@3 589 uPosFound = uReadPos+i;
nuclear@3 590 break;
nuclear@3 591 }
nuclear@3 592 }
nuclear@3 593
nuclear@3 594 if (uPosFound!=0)
nuclear@3 595 break;
nuclear@3 596 }
nuclear@3 597
nuclear@3 598 TRYFREE(buf);
nuclear@3 599 if (uPosFound == 0)
nuclear@3 600 return 0;
nuclear@3 601
nuclear@3 602 /* Zip64 end of central directory locator */
nuclear@3 603 if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 604 return 0;
nuclear@3 605
nuclear@3 606 /* the signature, already checked */
nuclear@3 607 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
nuclear@3 608 return 0;
nuclear@3 609
nuclear@3 610 /* number of the disk with the start of the zip64 end of central directory */
nuclear@3 611 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
nuclear@3 612 return 0;
nuclear@3 613 if (uL != 0)
nuclear@3 614 return 0;
nuclear@3 615
nuclear@3 616 /* relative offset of the zip64 end of central directory record */
nuclear@3 617 if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK)
nuclear@3 618 return 0;
nuclear@3 619
nuclear@3 620 /* total number of disks */
nuclear@3 621 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
nuclear@3 622 return 0;
nuclear@3 623 if (uL != 1)
nuclear@3 624 return 0;
nuclear@3 625
nuclear@3 626 /* Goto Zip64 end of central directory record */
nuclear@3 627 if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 628 return 0;
nuclear@3 629
nuclear@3 630 /* the signature */
nuclear@3 631 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
nuclear@3 632 return 0;
nuclear@3 633
nuclear@3 634 if (uL != 0x06064b50) // signature of 'Zip64 end of central directory'
nuclear@3 635 return 0;
nuclear@3 636
nuclear@3 637 return relativeOffset;
nuclear@3 638 }
nuclear@3 639
nuclear@3 640 int LoadCentralDirectoryRecord(zip64_internal* pziinit)
nuclear@3 641 {
nuclear@3 642 int err=ZIP_OK;
nuclear@3 643 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
nuclear@3 644
nuclear@3 645 ZPOS64_T size_central_dir; /* size of the central directory */
nuclear@3 646 ZPOS64_T offset_central_dir; /* offset of start of central directory */
nuclear@3 647 ZPOS64_T central_pos;
nuclear@3 648 uLong uL;
nuclear@3 649
nuclear@3 650 uLong number_disk; /* number of the current dist, used for
nuclear@3 651 spaning ZIP, unsupported, always 0*/
nuclear@3 652 uLong number_disk_with_CD; /* number the the disk with central dir, used
nuclear@3 653 for spaning ZIP, unsupported, always 0*/
nuclear@3 654 ZPOS64_T number_entry;
nuclear@3 655 ZPOS64_T number_entry_CD; /* total number of entries in
nuclear@3 656 the central dir
nuclear@3 657 (same than number_entry on nospan) */
nuclear@3 658 uLong VersionMadeBy;
nuclear@3 659 uLong VersionNeeded;
nuclear@3 660 uLong size_comment;
nuclear@3 661
nuclear@3 662 int hasZIP64Record = 0;
nuclear@3 663
nuclear@3 664 // check first if we find a ZIP64 record
nuclear@3 665 central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream);
nuclear@3 666 if(central_pos > 0)
nuclear@3 667 {
nuclear@3 668 hasZIP64Record = 1;
nuclear@3 669 }
nuclear@3 670 else if(central_pos == 0)
nuclear@3 671 {
nuclear@3 672 central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream);
nuclear@3 673 }
nuclear@3 674
nuclear@3 675 /* disable to allow appending to empty ZIP archive
nuclear@3 676 if (central_pos==0)
nuclear@3 677 err=ZIP_ERRNO;
nuclear@3 678 */
nuclear@3 679
nuclear@3 680 if(hasZIP64Record)
nuclear@3 681 {
nuclear@3 682 ZPOS64_T sizeEndOfCentralDirectory;
nuclear@3 683 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
nuclear@3 684 err=ZIP_ERRNO;
nuclear@3 685
nuclear@3 686 /* the signature, already checked */
nuclear@3 687 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
nuclear@3 688 err=ZIP_ERRNO;
nuclear@3 689
nuclear@3 690 /* size of zip64 end of central directory record */
nuclear@3 691 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK)
nuclear@3 692 err=ZIP_ERRNO;
nuclear@3 693
nuclear@3 694 /* version made by */
nuclear@3 695 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK)
nuclear@3 696 err=ZIP_ERRNO;
nuclear@3 697
nuclear@3 698 /* version needed to extract */
nuclear@3 699 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK)
nuclear@3 700 err=ZIP_ERRNO;
nuclear@3 701
nuclear@3 702 /* number of this disk */
nuclear@3 703 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
nuclear@3 704 err=ZIP_ERRNO;
nuclear@3 705
nuclear@3 706 /* number of the disk with the start of the central directory */
nuclear@3 707 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
nuclear@3 708 err=ZIP_ERRNO;
nuclear@3 709
nuclear@3 710 /* total number of entries in the central directory on this disk */
nuclear@3 711 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK)
nuclear@3 712 err=ZIP_ERRNO;
nuclear@3 713
nuclear@3 714 /* total number of entries in the central directory */
nuclear@3 715 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK)
nuclear@3 716 err=ZIP_ERRNO;
nuclear@3 717
nuclear@3 718 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
nuclear@3 719 err=ZIP_BADZIPFILE;
nuclear@3 720
nuclear@3 721 /* size of the central directory */
nuclear@3 722 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK)
nuclear@3 723 err=ZIP_ERRNO;
nuclear@3 724
nuclear@3 725 /* offset of start of central directory with respect to the
nuclear@3 726 starting disk number */
nuclear@3 727 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK)
nuclear@3 728 err=ZIP_ERRNO;
nuclear@3 729
nuclear@3 730 // TODO..
nuclear@3 731 // read the comment from the standard central header.
nuclear@3 732 size_comment = 0;
nuclear@3 733 }
nuclear@3 734 else
nuclear@3 735 {
nuclear@3 736 // Read End of central Directory info
nuclear@3 737 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 738 err=ZIP_ERRNO;
nuclear@3 739
nuclear@3 740 /* the signature, already checked */
nuclear@3 741 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
nuclear@3 742 err=ZIP_ERRNO;
nuclear@3 743
nuclear@3 744 /* number of this disk */
nuclear@3 745 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
nuclear@3 746 err=ZIP_ERRNO;
nuclear@3 747
nuclear@3 748 /* number of the disk with the start of the central directory */
nuclear@3 749 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
nuclear@3 750 err=ZIP_ERRNO;
nuclear@3 751
nuclear@3 752 /* total number of entries in the central dir on this disk */
nuclear@3 753 number_entry = 0;
nuclear@3 754 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
nuclear@3 755 err=ZIP_ERRNO;
nuclear@3 756 else
nuclear@3 757 number_entry = uL;
nuclear@3 758
nuclear@3 759 /* total number of entries in the central dir */
nuclear@3 760 number_entry_CD = 0;
nuclear@3 761 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
nuclear@3 762 err=ZIP_ERRNO;
nuclear@3 763 else
nuclear@3 764 number_entry_CD = uL;
nuclear@3 765
nuclear@3 766 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
nuclear@3 767 err=ZIP_BADZIPFILE;
nuclear@3 768
nuclear@3 769 /* size of the central directory */
nuclear@3 770 size_central_dir = 0;
nuclear@3 771 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
nuclear@3 772 err=ZIP_ERRNO;
nuclear@3 773 else
nuclear@3 774 size_central_dir = uL;
nuclear@3 775
nuclear@3 776 /* offset of start of central directory with respect to the starting disk number */
nuclear@3 777 offset_central_dir = 0;
nuclear@3 778 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
nuclear@3 779 err=ZIP_ERRNO;
nuclear@3 780 else
nuclear@3 781 offset_central_dir = uL;
nuclear@3 782
nuclear@3 783
nuclear@3 784 /* zipfile global comment length */
nuclear@3 785 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK)
nuclear@3 786 err=ZIP_ERRNO;
nuclear@3 787 }
nuclear@3 788
nuclear@3 789 if ((central_pos<offset_central_dir+size_central_dir) &&
nuclear@3 790 (err==ZIP_OK))
nuclear@3 791 err=ZIP_BADZIPFILE;
nuclear@3 792
nuclear@3 793 if (err!=ZIP_OK)
nuclear@3 794 {
nuclear@3 795 ZCLOSE64(pziinit->z_filefunc, pziinit->filestream);
nuclear@3 796 return ZIP_ERRNO;
nuclear@3 797 }
nuclear@3 798
nuclear@3 799 if (size_comment>0)
nuclear@3 800 {
nuclear@3 801 pziinit->globalcomment = (char*)ALLOC(size_comment+1);
nuclear@3 802 if (pziinit->globalcomment)
nuclear@3 803 {
nuclear@3 804 size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment);
nuclear@3 805 pziinit->globalcomment[size_comment]=0;
nuclear@3 806 }
nuclear@3 807 }
nuclear@3 808
nuclear@3 809 byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
nuclear@3 810 pziinit->add_position_when_writting_offset = byte_before_the_zipfile;
nuclear@3 811
nuclear@3 812 {
nuclear@3 813 ZPOS64_T size_central_dir_to_read = size_central_dir;
nuclear@3 814 size_t buf_size = SIZEDATA_INDATABLOCK;
nuclear@3 815 void* buf_read = (void*)ALLOC(buf_size);
nuclear@3 816 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
nuclear@3 817 err=ZIP_ERRNO;
nuclear@3 818
nuclear@3 819 while ((size_central_dir_to_read>0) && (err==ZIP_OK))
nuclear@3 820 {
nuclear@3 821 ZPOS64_T read_this = SIZEDATA_INDATABLOCK;
nuclear@3 822 if (read_this > size_central_dir_to_read)
nuclear@3 823 read_this = size_central_dir_to_read;
nuclear@3 824
nuclear@3 825 if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this)
nuclear@3 826 err=ZIP_ERRNO;
nuclear@3 827
nuclear@3 828 if (err==ZIP_OK)
nuclear@3 829 err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
nuclear@3 830
nuclear@3 831 size_central_dir_to_read-=read_this;
nuclear@3 832 }
nuclear@3 833 TRYFREE(buf_read);
nuclear@3 834 }
nuclear@3 835 pziinit->begin_pos = byte_before_the_zipfile;
nuclear@3 836 pziinit->number_entry = number_entry_CD;
nuclear@3 837
nuclear@3 838 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
nuclear@3 839 err=ZIP_ERRNO;
nuclear@3 840
nuclear@3 841 return err;
nuclear@3 842 }
nuclear@3 843
nuclear@3 844
nuclear@3 845 #endif /* !NO_ADDFILEINEXISTINGZIP*/
nuclear@3 846
nuclear@3 847
nuclear@3 848 /************************************************************/
nuclear@3 849 extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def)
nuclear@3 850 {
nuclear@3 851 zip64_internal ziinit;
nuclear@3 852 zip64_internal* zi;
nuclear@3 853 int err=ZIP_OK;
nuclear@3 854
nuclear@3 855 ziinit.z_filefunc.zseek32_file = NULL;
nuclear@3 856 ziinit.z_filefunc.ztell32_file = NULL;
nuclear@3 857 if (pzlib_filefunc64_32_def==NULL)
nuclear@3 858 fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64);
nuclear@3 859 else
nuclear@3 860 ziinit.z_filefunc = *pzlib_filefunc64_32_def;
nuclear@3 861
nuclear@3 862 ziinit.filestream = ZOPEN64(ziinit.z_filefunc,
nuclear@3 863 pathname,
nuclear@3 864 (append == APPEND_STATUS_CREATE) ?
nuclear@3 865 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
nuclear@3 866 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
nuclear@3 867
nuclear@3 868 if (ziinit.filestream == NULL)
nuclear@3 869 return NULL;
nuclear@3 870
nuclear@3 871 if (append == APPEND_STATUS_CREATEAFTER)
nuclear@3 872 ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
nuclear@3 873
nuclear@3 874 ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream);
nuclear@3 875 ziinit.in_opened_file_inzip = 0;
nuclear@3 876 ziinit.ci.stream_initialised = 0;
nuclear@3 877 ziinit.number_entry = 0;
nuclear@3 878 ziinit.add_position_when_writting_offset = 0;
nuclear@3 879 init_linkedlist(&(ziinit.central_dir));
nuclear@3 880
nuclear@3 881
nuclear@3 882
nuclear@3 883 zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
nuclear@3 884 if (zi==NULL)
nuclear@3 885 {
nuclear@3 886 ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
nuclear@3 887 return NULL;
nuclear@3 888 }
nuclear@3 889
nuclear@3 890 /* now we add file in a zipfile */
nuclear@3 891 # ifndef NO_ADDFILEINEXISTINGZIP
nuclear@3 892 ziinit.globalcomment = NULL;
nuclear@3 893 if (append == APPEND_STATUS_ADDINZIP)
nuclear@3 894 {
nuclear@3 895 // Read and Cache Central Directory Records
nuclear@3 896 err = LoadCentralDirectoryRecord(&ziinit);
nuclear@3 897 }
nuclear@3 898
nuclear@3 899 if (globalcomment)
nuclear@3 900 {
nuclear@3 901 *globalcomment = ziinit.globalcomment;
nuclear@3 902 }
nuclear@3 903 # endif /* !NO_ADDFILEINEXISTINGZIP*/
nuclear@3 904
nuclear@3 905 if (err != ZIP_OK)
nuclear@3 906 {
nuclear@3 907 # ifndef NO_ADDFILEINEXISTINGZIP
nuclear@3 908 TRYFREE(ziinit.globalcomment);
nuclear@3 909 # endif /* !NO_ADDFILEINEXISTINGZIP*/
nuclear@3 910 TRYFREE(zi);
nuclear@3 911 return NULL;
nuclear@3 912 }
nuclear@3 913 else
nuclear@3 914 {
nuclear@3 915 *zi = ziinit;
nuclear@3 916 return (zipFile)zi;
nuclear@3 917 }
nuclear@3 918 }
nuclear@3 919
nuclear@3 920 extern zipFile ZEXPORT zipOpen2 (const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def)
nuclear@3 921 {
nuclear@3 922 if (pzlib_filefunc32_def != NULL)
nuclear@3 923 {
nuclear@3 924 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
nuclear@3 925 fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
nuclear@3 926 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
nuclear@3 927 }
nuclear@3 928 else
nuclear@3 929 return zipOpen3(pathname, append, globalcomment, NULL);
nuclear@3 930 }
nuclear@3 931
nuclear@3 932 extern zipFile ZEXPORT zipOpen2_64 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)
nuclear@3 933 {
nuclear@3 934 if (pzlib_filefunc_def != NULL)
nuclear@3 935 {
nuclear@3 936 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
nuclear@3 937 zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
nuclear@3 938 zlib_filefunc64_32_def_fill.ztell32_file = NULL;
nuclear@3 939 zlib_filefunc64_32_def_fill.zseek32_file = NULL;
nuclear@3 940 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
nuclear@3 941 }
nuclear@3 942 else
nuclear@3 943 return zipOpen3(pathname, append, globalcomment, NULL);
nuclear@3 944 }
nuclear@3 945
nuclear@3 946
nuclear@3 947
nuclear@3 948 extern zipFile ZEXPORT zipOpen (const char* pathname, int append)
nuclear@3 949 {
nuclear@3 950 return zipOpen3((const void*)pathname,append,NULL,NULL);
nuclear@3 951 }
nuclear@3 952
nuclear@3 953 extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append)
nuclear@3 954 {
nuclear@3 955 return zipOpen3(pathname,append,NULL,NULL);
nuclear@3 956 }
nuclear@3 957
nuclear@3 958 int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local)
nuclear@3 959 {
nuclear@3 960 /* write the local header */
nuclear@3 961 int err;
nuclear@3 962 uInt size_filename = (uInt)strlen(filename);
nuclear@3 963 uInt size_extrafield = size_extrafield_local;
nuclear@3 964
nuclear@3 965 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4);
nuclear@3 966
nuclear@3 967 if (err==ZIP_OK)
nuclear@3 968 {
nuclear@3 969 if(zi->ci.zip64)
nuclear@3 970 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */
nuclear@3 971 else
nuclear@3 972 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
nuclear@3 973 }
nuclear@3 974
nuclear@3 975 if (err==ZIP_OK)
nuclear@3 976 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
nuclear@3 977
nuclear@3 978 if (err==ZIP_OK)
nuclear@3 979 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
nuclear@3 980
nuclear@3 981 if (err==ZIP_OK)
nuclear@3 982 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
nuclear@3 983
nuclear@3 984 // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later
nuclear@3 985 if (err==ZIP_OK)
nuclear@3 986 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
nuclear@3 987 if (err==ZIP_OK)
nuclear@3 988 {
nuclear@3 989 if(zi->ci.zip64)
nuclear@3 990 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */
nuclear@3 991 else
nuclear@3 992 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
nuclear@3 993 }
nuclear@3 994 if (err==ZIP_OK)
nuclear@3 995 {
nuclear@3 996 if(zi->ci.zip64)
nuclear@3 997 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */
nuclear@3 998 else
nuclear@3 999 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
nuclear@3 1000 }
nuclear@3 1001
nuclear@3 1002 if (err==ZIP_OK)
nuclear@3 1003 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
nuclear@3 1004
nuclear@3 1005 if(zi->ci.zip64)
nuclear@3 1006 {
nuclear@3 1007 size_extrafield += 20;
nuclear@3 1008 }
nuclear@3 1009
nuclear@3 1010 if (err==ZIP_OK)
nuclear@3 1011 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2);
nuclear@3 1012
nuclear@3 1013 if ((err==ZIP_OK) && (size_filename > 0))
nuclear@3 1014 {
nuclear@3 1015 if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
nuclear@3 1016 err = ZIP_ERRNO;
nuclear@3 1017 }
nuclear@3 1018
nuclear@3 1019 if ((err==ZIP_OK) && (size_extrafield_local > 0))
nuclear@3 1020 {
nuclear@3 1021 if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local)
nuclear@3 1022 err = ZIP_ERRNO;
nuclear@3 1023 }
nuclear@3 1024
nuclear@3 1025
nuclear@3 1026 if ((err==ZIP_OK) && (zi->ci.zip64))
nuclear@3 1027 {
nuclear@3 1028 // write the Zip64 extended info
nuclear@3 1029 short HeaderID = 1;
nuclear@3 1030 short DataSize = 16;
nuclear@3 1031 ZPOS64_T CompressedSize = 0;
nuclear@3 1032 ZPOS64_T UncompressedSize = 0;
nuclear@3 1033
nuclear@3 1034 // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file)
nuclear@3 1035 zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
nuclear@3 1036
nuclear@3 1037 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)HeaderID,2);
nuclear@3 1038 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)DataSize,2);
nuclear@3 1039
nuclear@3 1040 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8);
nuclear@3 1041 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8);
nuclear@3 1042 }
nuclear@3 1043
nuclear@3 1044 return err;
nuclear@3 1045 }
nuclear@3 1046
nuclear@3 1047 /*
nuclear@3 1048 NOTE.
nuclear@3 1049 When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped
nuclear@3 1050 before calling this function it can be done with zipRemoveExtraInfoBlock
nuclear@3 1051
nuclear@3 1052 It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize
nuclear@3 1053 unnecessary allocations.
nuclear@3 1054 */
nuclear@3 1055 extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1056 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1057 const void* extrafield_global, uInt size_extrafield_global,
nuclear@3 1058 const char* comment, int method, int level, int raw,
nuclear@3 1059 int windowBits,int memLevel, int strategy,
nuclear@3 1060 const char* password, uLong crcForCrypting,
nuclear@3 1061 uLong versionMadeBy, uLong flagBase, int zip64)
nuclear@3 1062 {
nuclear@3 1063 zip64_internal* zi;
nuclear@3 1064 uInt size_filename;
nuclear@3 1065 uInt size_comment;
nuclear@3 1066 uInt i;
nuclear@3 1067 int err = ZIP_OK;
nuclear@3 1068
nuclear@3 1069 # ifdef NOCRYPT
nuclear@3 1070 (crcForCrypting);
nuclear@3 1071 if (password != NULL)
nuclear@3 1072 return ZIP_PARAMERROR;
nuclear@3 1073 # endif
nuclear@3 1074
nuclear@3 1075 if (file == NULL)
nuclear@3 1076 return ZIP_PARAMERROR;
nuclear@3 1077
nuclear@3 1078 #ifdef HAVE_BZIP2
nuclear@3 1079 if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED))
nuclear@3 1080 return ZIP_PARAMERROR;
nuclear@3 1081 #else
nuclear@3 1082 if ((method!=0) && (method!=Z_DEFLATED))
nuclear@3 1083 return ZIP_PARAMERROR;
nuclear@3 1084 #endif
nuclear@3 1085
nuclear@3 1086 zi = (zip64_internal*)file;
nuclear@3 1087
nuclear@3 1088 if (zi->in_opened_file_inzip == 1)
nuclear@3 1089 {
nuclear@3 1090 err = zipCloseFileInZip (file);
nuclear@3 1091 if (err != ZIP_OK)
nuclear@3 1092 return err;
nuclear@3 1093 }
nuclear@3 1094
nuclear@3 1095 if (filename==NULL)
nuclear@3 1096 filename="-";
nuclear@3 1097
nuclear@3 1098 if (comment==NULL)
nuclear@3 1099 size_comment = 0;
nuclear@3 1100 else
nuclear@3 1101 size_comment = (uInt)strlen(comment);
nuclear@3 1102
nuclear@3 1103 size_filename = (uInt)strlen(filename);
nuclear@3 1104
nuclear@3 1105 if (zipfi == NULL)
nuclear@3 1106 zi->ci.dosDate = 0;
nuclear@3 1107 else
nuclear@3 1108 {
nuclear@3 1109 if (zipfi->dosDate != 0)
nuclear@3 1110 zi->ci.dosDate = zipfi->dosDate;
nuclear@3 1111 else
nuclear@3 1112 zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
nuclear@3 1113 }
nuclear@3 1114
nuclear@3 1115 zi->ci.flag = flagBase;
nuclear@3 1116 if ((level==8) || (level==9))
nuclear@3 1117 zi->ci.flag |= 2;
nuclear@3 1118 if (level==2)
nuclear@3 1119 zi->ci.flag |= 4;
nuclear@3 1120 if (level==1)
nuclear@3 1121 zi->ci.flag |= 6;
nuclear@3 1122 if (password != NULL)
nuclear@3 1123 zi->ci.flag |= 1;
nuclear@3 1124
nuclear@3 1125 zi->ci.crc32 = 0;
nuclear@3 1126 zi->ci.method = method;
nuclear@3 1127 zi->ci.encrypt = 0;
nuclear@3 1128 zi->ci.stream_initialised = 0;
nuclear@3 1129 zi->ci.pos_in_buffered_data = 0;
nuclear@3 1130 zi->ci.raw = raw;
nuclear@3 1131 zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
nuclear@3 1132
nuclear@3 1133 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment;
nuclear@3 1134 zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data
nuclear@3 1135
nuclear@3 1136 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree);
nuclear@3 1137
nuclear@3 1138 zi->ci.size_centralExtra = size_extrafield_global;
nuclear@3 1139 zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
nuclear@3 1140 /* version info */
nuclear@3 1141 zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
nuclear@3 1142 zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
nuclear@3 1143 zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
nuclear@3 1144 zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
nuclear@3 1145 zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
nuclear@3 1146 zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
nuclear@3 1147 zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
nuclear@3 1148 zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
nuclear@3 1149 zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
nuclear@3 1150 zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
nuclear@3 1151 zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
nuclear@3 1152 zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
nuclear@3 1153
nuclear@3 1154 if (zipfi==NULL)
nuclear@3 1155 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
nuclear@3 1156 else
nuclear@3 1157 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
nuclear@3 1158
nuclear@3 1159 if (zipfi==NULL)
nuclear@3 1160 zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
nuclear@3 1161 else
nuclear@3 1162 zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
nuclear@3 1163
nuclear@3 1164 if(zi->ci.pos_local_header >= 0xffffffff)
nuclear@3 1165 zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
nuclear@3 1166 else
nuclear@3 1167 zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset,4);
nuclear@3 1168
nuclear@3 1169 for (i=0;i<size_filename;i++)
nuclear@3 1170 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
nuclear@3 1171
nuclear@3 1172 for (i=0;i<size_extrafield_global;i++)
nuclear@3 1173 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
nuclear@3 1174 *(((const char*)extrafield_global)+i);
nuclear@3 1175
nuclear@3 1176 for (i=0;i<size_comment;i++)
nuclear@3 1177 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
nuclear@3 1178 size_extrafield_global+i) = *(comment+i);
nuclear@3 1179 if (zi->ci.central_header == NULL)
nuclear@3 1180 return ZIP_INTERNALERROR;
nuclear@3 1181
nuclear@3 1182 zi->ci.zip64 = zip64;
nuclear@3 1183 zi->ci.totalCompressedData = 0;
nuclear@3 1184 zi->ci.totalUncompressedData = 0;
nuclear@3 1185 zi->ci.pos_zip64extrainfo = 0;
nuclear@3 1186
nuclear@3 1187 err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
nuclear@3 1188
nuclear@3 1189 #ifdef HAVE_BZIP2
nuclear@3 1190 zi->ci.bstream.avail_in = (uInt)0;
nuclear@3 1191 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
nuclear@3 1192 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
nuclear@3 1193 zi->ci.bstream.total_in_hi32 = 0;
nuclear@3 1194 zi->ci.bstream.total_in_lo32 = 0;
nuclear@3 1195 zi->ci.bstream.total_out_hi32 = 0;
nuclear@3 1196 zi->ci.bstream.total_out_lo32 = 0;
nuclear@3 1197 #endif
nuclear@3 1198
nuclear@3 1199 zi->ci.stream.avail_in = (uInt)0;
nuclear@3 1200 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
nuclear@3 1201 zi->ci.stream.next_out = zi->ci.buffered_data;
nuclear@3 1202 zi->ci.stream.total_in = 0;
nuclear@3 1203 zi->ci.stream.total_out = 0;
nuclear@3 1204 zi->ci.stream.data_type = Z_BINARY;
nuclear@3 1205
nuclear@3 1206 #ifdef HAVE_BZIP2
nuclear@3 1207 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
nuclear@3 1208 #else
nuclear@3 1209 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
nuclear@3 1210 #endif
nuclear@3 1211 {
nuclear@3 1212 if(zi->ci.method == Z_DEFLATED)
nuclear@3 1213 {
nuclear@3 1214 zi->ci.stream.zalloc = (alloc_func)0;
nuclear@3 1215 zi->ci.stream.zfree = (free_func)0;
nuclear@3 1216 zi->ci.stream.opaque = (voidpf)0;
nuclear@3 1217
nuclear@3 1218 if (windowBits>0)
nuclear@3 1219 windowBits = -windowBits;
nuclear@3 1220
nuclear@3 1221 err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
nuclear@3 1222
nuclear@3 1223 if (err==Z_OK)
nuclear@3 1224 zi->ci.stream_initialised = Z_DEFLATED;
nuclear@3 1225 }
nuclear@3 1226 else if(zi->ci.method == Z_BZIP2ED)
nuclear@3 1227 {
nuclear@3 1228 #ifdef HAVE_BZIP2
nuclear@3 1229 // Init BZip stuff here
nuclear@3 1230 zi->ci.bstream.bzalloc = 0;
nuclear@3 1231 zi->ci.bstream.bzfree = 0;
nuclear@3 1232 zi->ci.bstream.opaque = (voidpf)0;
nuclear@3 1233
nuclear@3 1234 err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35);
nuclear@3 1235 if(err == BZ_OK)
nuclear@3 1236 zi->ci.stream_initialised = Z_BZIP2ED;
nuclear@3 1237 #endif
nuclear@3 1238 }
nuclear@3 1239
nuclear@3 1240 }
nuclear@3 1241
nuclear@3 1242 # ifndef NOCRYPT
nuclear@3 1243 zi->ci.crypt_header_size = 0;
nuclear@3 1244 if ((err==Z_OK) && (password != NULL))
nuclear@3 1245 {
nuclear@3 1246 unsigned char bufHead[RAND_HEAD_LEN];
nuclear@3 1247 unsigned int sizeHead;
nuclear@3 1248 zi->ci.encrypt = 1;
nuclear@3 1249 zi->ci.pcrc_32_tab = get_crc_table();
nuclear@3 1250 /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
nuclear@3 1251
nuclear@3 1252 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
nuclear@3 1253 zi->ci.crypt_header_size = sizeHead;
nuclear@3 1254
nuclear@3 1255 if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
nuclear@3 1256 err = ZIP_ERRNO;
nuclear@3 1257 }
nuclear@3 1258 # endif
nuclear@3 1259
nuclear@3 1260 if (err==Z_OK)
nuclear@3 1261 zi->in_opened_file_inzip = 1;
nuclear@3 1262 return err;
nuclear@3 1263 }
nuclear@3 1264
nuclear@3 1265 extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1266 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1267 const void* extrafield_global, uInt size_extrafield_global,
nuclear@3 1268 const char* comment, int method, int level, int raw,
nuclear@3 1269 int windowBits,int memLevel, int strategy,
nuclear@3 1270 const char* password, uLong crcForCrypting,
nuclear@3 1271 uLong versionMadeBy, uLong flagBase)
nuclear@3 1272 {
nuclear@3 1273 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
nuclear@3 1274 extrafield_local, size_extrafield_local,
nuclear@3 1275 extrafield_global, size_extrafield_global,
nuclear@3 1276 comment, method, level, raw,
nuclear@3 1277 windowBits, memLevel, strategy,
nuclear@3 1278 password, crcForCrypting, versionMadeBy, flagBase, 0);
nuclear@3 1279 }
nuclear@3 1280
nuclear@3 1281 extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1282 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1283 const void* extrafield_global, uInt size_extrafield_global,
nuclear@3 1284 const char* comment, int method, int level, int raw,
nuclear@3 1285 int windowBits,int memLevel, int strategy,
nuclear@3 1286 const char* password, uLong crcForCrypting)
nuclear@3 1287 {
nuclear@3 1288 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
nuclear@3 1289 extrafield_local, size_extrafield_local,
nuclear@3 1290 extrafield_global, size_extrafield_global,
nuclear@3 1291 comment, method, level, raw,
nuclear@3 1292 windowBits, memLevel, strategy,
nuclear@3 1293 password, crcForCrypting, VERSIONMADEBY, 0, 0);
nuclear@3 1294 }
nuclear@3 1295
nuclear@3 1296 extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1297 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1298 const void* extrafield_global, uInt size_extrafield_global,
nuclear@3 1299 const char* comment, int method, int level, int raw,
nuclear@3 1300 int windowBits,int memLevel, int strategy,
nuclear@3 1301 const char* password, uLong crcForCrypting, int zip64)
nuclear@3 1302 {
nuclear@3 1303 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
nuclear@3 1304 extrafield_local, size_extrafield_local,
nuclear@3 1305 extrafield_global, size_extrafield_global,
nuclear@3 1306 comment, method, level, raw,
nuclear@3 1307 windowBits, memLevel, strategy,
nuclear@3 1308 password, crcForCrypting, VERSIONMADEBY, 0, zip64);
nuclear@3 1309 }
nuclear@3 1310
nuclear@3 1311 extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1312 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1313 const void* extrafield_global, uInt size_extrafield_global,
nuclear@3 1314 const char* comment, int method, int level, int raw)
nuclear@3 1315 {
nuclear@3 1316 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
nuclear@3 1317 extrafield_local, size_extrafield_local,
nuclear@3 1318 extrafield_global, size_extrafield_global,
nuclear@3 1319 comment, method, level, raw,
nuclear@3 1320 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
nuclear@3 1321 NULL, 0, VERSIONMADEBY, 0, 0);
nuclear@3 1322 }
nuclear@3 1323
nuclear@3 1324 extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1325 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1326 const void* extrafield_global, uInt size_extrafield_global,
nuclear@3 1327 const char* comment, int method, int level, int raw, int zip64)
nuclear@3 1328 {
nuclear@3 1329 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
nuclear@3 1330 extrafield_local, size_extrafield_local,
nuclear@3 1331 extrafield_global, size_extrafield_global,
nuclear@3 1332 comment, method, level, raw,
nuclear@3 1333 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
nuclear@3 1334 NULL, 0, VERSIONMADEBY, 0, zip64);
nuclear@3 1335 }
nuclear@3 1336
nuclear@3 1337 extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1338 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1339 const void*extrafield_global, uInt size_extrafield_global,
nuclear@3 1340 const char* comment, int method, int level, int zip64)
nuclear@3 1341 {
nuclear@3 1342 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
nuclear@3 1343 extrafield_local, size_extrafield_local,
nuclear@3 1344 extrafield_global, size_extrafield_global,
nuclear@3 1345 comment, method, level, 0,
nuclear@3 1346 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
nuclear@3 1347 NULL, 0, VERSIONMADEBY, 0, zip64);
nuclear@3 1348 }
nuclear@3 1349
nuclear@3 1350 extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi,
nuclear@3 1351 const void* extrafield_local, uInt size_extrafield_local,
nuclear@3 1352 const void*extrafield_global, uInt size_extrafield_global,
nuclear@3 1353 const char* comment, int method, int level)
nuclear@3 1354 {
nuclear@3 1355 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
nuclear@3 1356 extrafield_local, size_extrafield_local,
nuclear@3 1357 extrafield_global, size_extrafield_global,
nuclear@3 1358 comment, method, level, 0,
nuclear@3 1359 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
nuclear@3 1360 NULL, 0, VERSIONMADEBY, 0, 0);
nuclear@3 1361 }
nuclear@3 1362
nuclear@3 1363 local int zip64FlushWriteBuffer(zip64_internal* zi)
nuclear@3 1364 {
nuclear@3 1365 int err=ZIP_OK;
nuclear@3 1366
nuclear@3 1367 if (zi->ci.encrypt != 0)
nuclear@3 1368 {
nuclear@3 1369 #ifndef NOCRYPT
nuclear@3 1370 uInt i;
nuclear@3 1371 int t;
nuclear@3 1372 for (i=0;i<zi->ci.pos_in_buffered_data;i++)
nuclear@3 1373 zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t);
nuclear@3 1374 #endif
nuclear@3 1375 }
nuclear@3 1376
nuclear@3 1377 if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data)
nuclear@3 1378 err = ZIP_ERRNO;
nuclear@3 1379
nuclear@3 1380 zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data;
nuclear@3 1381
nuclear@3 1382 #ifdef HAVE_BZIP2
nuclear@3 1383 if(zi->ci.method == Z_BZIP2ED)
nuclear@3 1384 {
nuclear@3 1385 zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32;
nuclear@3 1386 zi->ci.bstream.total_in_lo32 = 0;
nuclear@3 1387 zi->ci.bstream.total_in_hi32 = 0;
nuclear@3 1388 }
nuclear@3 1389 else
nuclear@3 1390 #endif
nuclear@3 1391 {
nuclear@3 1392 zi->ci.totalUncompressedData += zi->ci.stream.total_in;
nuclear@3 1393 zi->ci.stream.total_in = 0;
nuclear@3 1394 }
nuclear@3 1395
nuclear@3 1396
nuclear@3 1397 zi->ci.pos_in_buffered_data = 0;
nuclear@3 1398
nuclear@3 1399 return err;
nuclear@3 1400 }
nuclear@3 1401
nuclear@3 1402 extern int ZEXPORT zipWriteInFileInZip (zipFile file,const void* buf,unsigned int len)
nuclear@3 1403 {
nuclear@3 1404 zip64_internal* zi;
nuclear@3 1405 int err=ZIP_OK;
nuclear@3 1406
nuclear@3 1407 if (file == NULL)
nuclear@3 1408 return ZIP_PARAMERROR;
nuclear@3 1409 zi = (zip64_internal*)file;
nuclear@3 1410
nuclear@3 1411 if (zi->in_opened_file_inzip == 0)
nuclear@3 1412 return ZIP_PARAMERROR;
nuclear@3 1413
nuclear@3 1414 zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len);
nuclear@3 1415
nuclear@3 1416 #ifdef HAVE_BZIP2
nuclear@3 1417 if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw))
nuclear@3 1418 {
nuclear@3 1419 zi->ci.bstream.next_in = (void*)buf;
nuclear@3 1420 zi->ci.bstream.avail_in = len;
nuclear@3 1421 err = BZ_RUN_OK;
nuclear@3 1422
nuclear@3 1423 while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0))
nuclear@3 1424 {
nuclear@3 1425 if (zi->ci.bstream.avail_out == 0)
nuclear@3 1426 {
nuclear@3 1427 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
nuclear@3 1428 err = ZIP_ERRNO;
nuclear@3 1429 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
nuclear@3 1430 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
nuclear@3 1431 }
nuclear@3 1432
nuclear@3 1433
nuclear@3 1434 if(err != BZ_RUN_OK)
nuclear@3 1435 break;
nuclear@3 1436
nuclear@3 1437 if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
nuclear@3 1438 {
nuclear@3 1439 uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32;
nuclear@3 1440 // uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32;
nuclear@3 1441 err=BZ2_bzCompress(&zi->ci.bstream, BZ_RUN);
nuclear@3 1442
nuclear@3 1443 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ;
nuclear@3 1444 }
nuclear@3 1445 }
nuclear@3 1446
nuclear@3 1447 if(err == BZ_RUN_OK)
nuclear@3 1448 err = ZIP_OK;
nuclear@3 1449 }
nuclear@3 1450 else
nuclear@3 1451 #endif
nuclear@3 1452 {
nuclear@3 1453 zi->ci.stream.next_in = (Bytef*)buf;
nuclear@3 1454 zi->ci.stream.avail_in = len;
nuclear@3 1455
nuclear@3 1456 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
nuclear@3 1457 {
nuclear@3 1458 if (zi->ci.stream.avail_out == 0)
nuclear@3 1459 {
nuclear@3 1460 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
nuclear@3 1461 err = ZIP_ERRNO;
nuclear@3 1462 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
nuclear@3 1463 zi->ci.stream.next_out = zi->ci.buffered_data;
nuclear@3 1464 }
nuclear@3 1465
nuclear@3 1466
nuclear@3 1467 if(err != ZIP_OK)
nuclear@3 1468 break;
nuclear@3 1469
nuclear@3 1470 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
nuclear@3 1471 {
nuclear@3 1472 uLong uTotalOutBefore = zi->ci.stream.total_out;
nuclear@3 1473 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
nuclear@3 1474 if(uTotalOutBefore > zi->ci.stream.total_out)
nuclear@3 1475 {
nuclear@3 1476 int bBreak = 0;
nuclear@3 1477 bBreak++;
nuclear@3 1478 }
nuclear@3 1479
nuclear@3 1480 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
nuclear@3 1481 }
nuclear@3 1482 else
nuclear@3 1483 {
nuclear@3 1484 uInt copy_this,i;
nuclear@3 1485 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
nuclear@3 1486 copy_this = zi->ci.stream.avail_in;
nuclear@3 1487 else
nuclear@3 1488 copy_this = zi->ci.stream.avail_out;
nuclear@3 1489
nuclear@3 1490 for (i = 0; i < copy_this; i++)
nuclear@3 1491 *(((char*)zi->ci.stream.next_out)+i) =
nuclear@3 1492 *(((const char*)zi->ci.stream.next_in)+i);
nuclear@3 1493 {
nuclear@3 1494 zi->ci.stream.avail_in -= copy_this;
nuclear@3 1495 zi->ci.stream.avail_out-= copy_this;
nuclear@3 1496 zi->ci.stream.next_in+= copy_this;
nuclear@3 1497 zi->ci.stream.next_out+= copy_this;
nuclear@3 1498 zi->ci.stream.total_in+= copy_this;
nuclear@3 1499 zi->ci.stream.total_out+= copy_this;
nuclear@3 1500 zi->ci.pos_in_buffered_data += copy_this;
nuclear@3 1501 }
nuclear@3 1502 }
nuclear@3 1503 }// while(...)
nuclear@3 1504 }
nuclear@3 1505
nuclear@3 1506 return err;
nuclear@3 1507 }
nuclear@3 1508
nuclear@3 1509 extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32)
nuclear@3 1510 {
nuclear@3 1511 return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32);
nuclear@3 1512 }
nuclear@3 1513
nuclear@3 1514 extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32)
nuclear@3 1515 {
nuclear@3 1516 zip64_internal* zi;
nuclear@3 1517 ZPOS64_T compressed_size;
nuclear@3 1518 uLong invalidValue = 0xffffffff;
nuclear@3 1519 short datasize = 0;
nuclear@3 1520 int err=ZIP_OK;
nuclear@3 1521
nuclear@3 1522 if (file == NULL)
nuclear@3 1523 return ZIP_PARAMERROR;
nuclear@3 1524 zi = (zip64_internal*)file;
nuclear@3 1525
nuclear@3 1526 if (zi->in_opened_file_inzip == 0)
nuclear@3 1527 return ZIP_PARAMERROR;
nuclear@3 1528 zi->ci.stream.avail_in = 0;
nuclear@3 1529
nuclear@3 1530 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
nuclear@3 1531 {
nuclear@3 1532 while (err==ZIP_OK)
nuclear@3 1533 {
nuclear@3 1534 uLong uTotalOutBefore;
nuclear@3 1535 if (zi->ci.stream.avail_out == 0)
nuclear@3 1536 {
nuclear@3 1537 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
nuclear@3 1538 err = ZIP_ERRNO;
nuclear@3 1539 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
nuclear@3 1540 zi->ci.stream.next_out = zi->ci.buffered_data;
nuclear@3 1541 }
nuclear@3 1542 uTotalOutBefore = zi->ci.stream.total_out;
nuclear@3 1543 err=deflate(&zi->ci.stream, Z_FINISH);
nuclear@3 1544 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
nuclear@3 1545 }
nuclear@3 1546 }
nuclear@3 1547 else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
nuclear@3 1548 {
nuclear@3 1549 #ifdef HAVE_BZIP2
nuclear@3 1550 err = BZ_FINISH_OK;
nuclear@3 1551 while (err==BZ_FINISH_OK)
nuclear@3 1552 {
nuclear@3 1553 uLong uTotalOutBefore;
nuclear@3 1554 if (zi->ci.bstream.avail_out == 0)
nuclear@3 1555 {
nuclear@3 1556 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
nuclear@3 1557 err = ZIP_ERRNO;
nuclear@3 1558 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
nuclear@3 1559 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
nuclear@3 1560 }
nuclear@3 1561 uTotalOutBefore = zi->ci.bstream.total_out_lo32;
nuclear@3 1562 err=BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH);
nuclear@3 1563 if(err == BZ_STREAM_END)
nuclear@3 1564 err = Z_STREAM_END;
nuclear@3 1565
nuclear@3 1566 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore);
nuclear@3 1567 }
nuclear@3 1568
nuclear@3 1569 if(err == BZ_FINISH_OK)
nuclear@3 1570 err = ZIP_OK;
nuclear@3 1571 #endif
nuclear@3 1572 }
nuclear@3 1573
nuclear@3 1574 if (err==Z_STREAM_END)
nuclear@3 1575 err=ZIP_OK; /* this is normal */
nuclear@3 1576
nuclear@3 1577 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
nuclear@3 1578 {
nuclear@3 1579 if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
nuclear@3 1580 err = ZIP_ERRNO;
nuclear@3 1581 }
nuclear@3 1582
nuclear@3 1583 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
nuclear@3 1584 {
nuclear@3 1585 int tmp_err = deflateEnd(&zi->ci.stream);
nuclear@3 1586 if (err == ZIP_OK)
nuclear@3 1587 err = tmp_err;
nuclear@3 1588 zi->ci.stream_initialised = 0;
nuclear@3 1589 }
nuclear@3 1590 #ifdef HAVE_BZIP2
nuclear@3 1591 else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
nuclear@3 1592 {
nuclear@3 1593 int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
nuclear@3 1594 if (err==ZIP_OK)
nuclear@3 1595 err = tmperr;
nuclear@3 1596 zi->ci.stream_initialised = 0;
nuclear@3 1597 }
nuclear@3 1598 #endif
nuclear@3 1599
nuclear@3 1600 if (!zi->ci.raw)
nuclear@3 1601 {
nuclear@3 1602 crc32 = (uLong)zi->ci.crc32;
nuclear@3 1603 uncompressed_size = zi->ci.totalUncompressedData;
nuclear@3 1604 }
nuclear@3 1605 compressed_size = zi->ci.totalCompressedData;
nuclear@3 1606
nuclear@3 1607 # ifndef NOCRYPT
nuclear@3 1608 compressed_size += zi->ci.crypt_header_size;
nuclear@3 1609 # endif
nuclear@3 1610
nuclear@3 1611 // update Current Item crc and sizes,
nuclear@3 1612 if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff)
nuclear@3 1613 {
nuclear@3 1614 /*version Made by*/
nuclear@3 1615 zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2);
nuclear@3 1616 /*version needed*/
nuclear@3 1617 zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2);
nuclear@3 1618
nuclear@3 1619 }
nuclear@3 1620
nuclear@3 1621 zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
nuclear@3 1622
nuclear@3 1623
nuclear@3 1624 if(compressed_size >= 0xffffffff)
nuclear@3 1625 zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/
nuclear@3 1626 else
nuclear@3 1627 zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/
nuclear@3 1628
nuclear@3 1629 /// set internal file attributes field
nuclear@3 1630 if (zi->ci.stream.data_type == Z_ASCII)
nuclear@3 1631 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
nuclear@3 1632
nuclear@3 1633 if(uncompressed_size >= 0xffffffff)
nuclear@3 1634 zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/
nuclear@3 1635 else
nuclear@3 1636 zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/
nuclear@3 1637
nuclear@3 1638 // Add ZIP64 extra info field for uncompressed size
nuclear@3 1639 if(uncompressed_size >= 0xffffffff)
nuclear@3 1640 datasize += 8;
nuclear@3 1641
nuclear@3 1642 // Add ZIP64 extra info field for compressed size
nuclear@3 1643 if(compressed_size >= 0xffffffff)
nuclear@3 1644 datasize += 8;
nuclear@3 1645
nuclear@3 1646 // Add ZIP64 extra info field for relative offset to local file header of current file
nuclear@3 1647 if(zi->ci.pos_local_header >= 0xffffffff)
nuclear@3 1648 datasize += 8;
nuclear@3 1649
nuclear@3 1650 if(datasize > 0)
nuclear@3 1651 {
nuclear@3 1652 char* p = NULL;
nuclear@3 1653
nuclear@3 1654 if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree)
nuclear@3 1655 {
nuclear@3 1656 // we can not write more data to the buffer that we have room for.
nuclear@3 1657 return ZIP_BADZIPFILE;
nuclear@3 1658 }
nuclear@3 1659
nuclear@3 1660 p = zi->ci.central_header + zi->ci.size_centralheader;
nuclear@3 1661
nuclear@3 1662 // Add Extra Information Header for 'ZIP64 information'
nuclear@3 1663 zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID
nuclear@3 1664 p += 2;
nuclear@3 1665 zip64local_putValue_inmemory(p, datasize, 2); // DataSize
nuclear@3 1666 p += 2;
nuclear@3 1667
nuclear@3 1668 if(uncompressed_size >= 0xffffffff)
nuclear@3 1669 {
nuclear@3 1670 zip64local_putValue_inmemory(p, uncompressed_size, 8);
nuclear@3 1671 p += 8;
nuclear@3 1672 }
nuclear@3 1673
nuclear@3 1674 if(compressed_size >= 0xffffffff)
nuclear@3 1675 {
nuclear@3 1676 zip64local_putValue_inmemory(p, compressed_size, 8);
nuclear@3 1677 p += 8;
nuclear@3 1678 }
nuclear@3 1679
nuclear@3 1680 if(zi->ci.pos_local_header >= 0xffffffff)
nuclear@3 1681 {
nuclear@3 1682 zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
nuclear@3 1683 p += 8;
nuclear@3 1684 }
nuclear@3 1685
nuclear@3 1686 // Update how much extra free space we got in the memory buffer
nuclear@3 1687 // and increase the centralheader size so the new ZIP64 fields are included
nuclear@3 1688 // ( 4 below is the size of HeaderID and DataSize field )
nuclear@3 1689 zi->ci.size_centralExtraFree -= datasize + 4;
nuclear@3 1690 zi->ci.size_centralheader += datasize + 4;
nuclear@3 1691
nuclear@3 1692 // Update the extra info size field
nuclear@3 1693 zi->ci.size_centralExtra += datasize + 4;
nuclear@3 1694 zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
nuclear@3 1695 }
nuclear@3 1696
nuclear@3 1697 if (err==ZIP_OK)
nuclear@3 1698 err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);
nuclear@3 1699
nuclear@3 1700 free(zi->ci.central_header);
nuclear@3 1701
nuclear@3 1702 if (err==ZIP_OK)
nuclear@3 1703 {
nuclear@3 1704 // Update the LocalFileHeader with the new values.
nuclear@3 1705
nuclear@3 1706 ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
nuclear@3 1707
nuclear@3 1708 if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 1709 err = ZIP_ERRNO;
nuclear@3 1710
nuclear@3 1711 if (err==ZIP_OK)
nuclear@3 1712 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
nuclear@3 1713
nuclear@3 1714 if(uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff )
nuclear@3 1715 {
nuclear@3 1716 if(zi->ci.pos_zip64extrainfo > 0)
nuclear@3 1717 {
nuclear@3 1718 // Update the size in the ZIP64 extended field.
nuclear@3 1719 if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 1720 err = ZIP_ERRNO;
nuclear@3 1721
nuclear@3 1722 if (err==ZIP_OK) /* compressed size, unknown */
nuclear@3 1723 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);
nuclear@3 1724
nuclear@3 1725 if (err==ZIP_OK) /* uncompressed size, unknown */
nuclear@3 1726 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8);
nuclear@3 1727 }
nuclear@3 1728 else
nuclear@3 1729 err = ZIP_BADZIPFILE; // Caller passed zip64 = 0, so no room for zip64 info -> fatal
nuclear@3 1730 }
nuclear@3 1731 else
nuclear@3 1732 {
nuclear@3 1733 if (err==ZIP_OK) /* compressed size, unknown */
nuclear@3 1734 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
nuclear@3 1735
nuclear@3 1736 if (err==ZIP_OK) /* uncompressed size, unknown */
nuclear@3 1737 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
nuclear@3 1738 }
nuclear@3 1739
nuclear@3 1740 if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
nuclear@3 1741 err = ZIP_ERRNO;
nuclear@3 1742 }
nuclear@3 1743
nuclear@3 1744 zi->number_entry ++;
nuclear@3 1745 zi->in_opened_file_inzip = 0;
nuclear@3 1746
nuclear@3 1747 return err;
nuclear@3 1748 }
nuclear@3 1749
nuclear@3 1750 extern int ZEXPORT zipCloseFileInZip (zipFile file)
nuclear@3 1751 {
nuclear@3 1752 return zipCloseFileInZipRaw (file,0,0);
nuclear@3 1753 }
nuclear@3 1754
nuclear@3 1755 int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip)
nuclear@3 1756 {
nuclear@3 1757 int err = ZIP_OK;
nuclear@3 1758 ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset;
nuclear@3 1759
nuclear@3 1760 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
nuclear@3 1761
nuclear@3 1762 /*num disks*/
nuclear@3 1763 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
nuclear@3 1764 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
nuclear@3 1765
nuclear@3 1766 /*relative offset*/
nuclear@3 1767 if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */
nuclear@3 1768 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8);
nuclear@3 1769
nuclear@3 1770 /*total disks*/ /* Do not support spawning of disk so always say 1 here*/
nuclear@3 1771 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
nuclear@3 1772 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4);
nuclear@3 1773
nuclear@3 1774 return err;
nuclear@3 1775 }
nuclear@3 1776
nuclear@3 1777 int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
nuclear@3 1778 {
nuclear@3 1779 int err = ZIP_OK;
nuclear@3 1780
nuclear@3 1781 uLong Zip64DataSize = 44;
nuclear@3 1782
nuclear@3 1783 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
nuclear@3 1784
nuclear@3 1785 if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */
nuclear@3 1786 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ?
nuclear@3 1787
nuclear@3 1788 if (err==ZIP_OK) /* version made by */
nuclear@3 1789 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
nuclear@3 1790
nuclear@3 1791 if (err==ZIP_OK) /* version needed */
nuclear@3 1792 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
nuclear@3 1793
nuclear@3 1794 if (err==ZIP_OK) /* number of this disk */
nuclear@3 1795 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
nuclear@3 1796
nuclear@3 1797 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
nuclear@3 1798 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
nuclear@3 1799
nuclear@3 1800 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
nuclear@3 1801 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
nuclear@3 1802
nuclear@3 1803 if (err==ZIP_OK) /* total number of entries in the central dir */
nuclear@3 1804 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
nuclear@3 1805
nuclear@3 1806 if (err==ZIP_OK) /* size of the central directory */
nuclear@3 1807 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8);
nuclear@3 1808
nuclear@3 1809 if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
nuclear@3 1810 {
nuclear@3 1811 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
nuclear@3 1812 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8);
nuclear@3 1813 }
nuclear@3 1814 return err;
nuclear@3 1815 }
nuclear@3 1816 int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
nuclear@3 1817 {
nuclear@3 1818 int err = ZIP_OK;
nuclear@3 1819
nuclear@3 1820 /*signature*/
nuclear@3 1821 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
nuclear@3 1822
nuclear@3 1823 if (err==ZIP_OK) /* number of this disk */
nuclear@3 1824 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
nuclear@3 1825
nuclear@3 1826 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
nuclear@3 1827 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
nuclear@3 1828
nuclear@3 1829 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
nuclear@3 1830 {
nuclear@3 1831 {
nuclear@3 1832 if(zi->number_entry >= 0xFFFF)
nuclear@3 1833 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
nuclear@3 1834 else
nuclear@3 1835 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
nuclear@3 1836 }
nuclear@3 1837 }
nuclear@3 1838
nuclear@3 1839 if (err==ZIP_OK) /* total number of entries in the central dir */
nuclear@3 1840 {
nuclear@3 1841 if(zi->number_entry >= 0xFFFF)
nuclear@3 1842 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
nuclear@3 1843 else
nuclear@3 1844 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
nuclear@3 1845 }
nuclear@3 1846
nuclear@3 1847 if (err==ZIP_OK) /* size of the central directory */
nuclear@3 1848 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
nuclear@3 1849
nuclear@3 1850 if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
nuclear@3 1851 {
nuclear@3 1852 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
nuclear@3 1853 if(pos >= 0xffffffff)
nuclear@3 1854 {
nuclear@3 1855 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4);
nuclear@3 1856 }
nuclear@3 1857 else
nuclear@3 1858 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
nuclear@3 1859 }
nuclear@3 1860
nuclear@3 1861 return err;
nuclear@3 1862 }
nuclear@3 1863
nuclear@3 1864 int Write_GlobalComment(zip64_internal* zi, const char* global_comment)
nuclear@3 1865 {
nuclear@3 1866 int err = ZIP_OK;
nuclear@3 1867 uInt size_global_comment = 0;
nuclear@3 1868
nuclear@3 1869 if(global_comment != NULL)
nuclear@3 1870 size_global_comment = (uInt)strlen(global_comment);
nuclear@3 1871
nuclear@3 1872 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
nuclear@3 1873
nuclear@3 1874 if (err == ZIP_OK && size_global_comment > 0)
nuclear@3 1875 {
nuclear@3 1876 if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment)
nuclear@3 1877 err = ZIP_ERRNO;
nuclear@3 1878 }
nuclear@3 1879 return err;
nuclear@3 1880 }
nuclear@3 1881
nuclear@3 1882 extern int ZEXPORT zipClose (zipFile file, const char* global_comment)
nuclear@3 1883 {
nuclear@3 1884 zip64_internal* zi;
nuclear@3 1885 int err = 0;
nuclear@3 1886 uLong size_centraldir = 0;
nuclear@3 1887 ZPOS64_T centraldir_pos_inzip;
nuclear@3 1888 ZPOS64_T pos;
nuclear@3 1889
nuclear@3 1890 if (file == NULL)
nuclear@3 1891 return ZIP_PARAMERROR;
nuclear@3 1892
nuclear@3 1893 zi = (zip64_internal*)file;
nuclear@3 1894
nuclear@3 1895 if (zi->in_opened_file_inzip == 1)
nuclear@3 1896 {
nuclear@3 1897 err = zipCloseFileInZip (file);
nuclear@3 1898 }
nuclear@3 1899
nuclear@3 1900 #ifndef NO_ADDFILEINEXISTINGZIP
nuclear@3 1901 if (global_comment==NULL)
nuclear@3 1902 global_comment = zi->globalcomment;
nuclear@3 1903 #endif
nuclear@3 1904
nuclear@3 1905 centraldir_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
nuclear@3 1906
nuclear@3 1907 if (err==ZIP_OK)
nuclear@3 1908 {
nuclear@3 1909 linkedlist_datablock_internal* ldi = zi->central_dir.first_block;
nuclear@3 1910 while (ldi!=NULL)
nuclear@3 1911 {
nuclear@3 1912 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
nuclear@3 1913 {
nuclear@3 1914 if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block)
nuclear@3 1915 err = ZIP_ERRNO;
nuclear@3 1916 }
nuclear@3 1917
nuclear@3 1918 size_centraldir += ldi->filled_in_this_block;
nuclear@3 1919 ldi = ldi->next_datablock;
nuclear@3 1920 }
nuclear@3 1921 }
nuclear@3 1922 free_linkedlist(&(zi->central_dir));
nuclear@3 1923
nuclear@3 1924 pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
nuclear@3 1925 if(pos >= 0xffffffff || zi->number_entry > 0xFFFF)
nuclear@3 1926 {
nuclear@3 1927 ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream);
nuclear@3 1928 Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
nuclear@3 1929
nuclear@3 1930 Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos);
nuclear@3 1931 }
nuclear@3 1932
nuclear@3 1933 if (err==ZIP_OK)
nuclear@3 1934 err = Write_EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
nuclear@3 1935
nuclear@3 1936 if(err == ZIP_OK)
nuclear@3 1937 err = Write_GlobalComment(zi, global_comment);
nuclear@3 1938
nuclear@3 1939 if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0)
nuclear@3 1940 if (err == ZIP_OK)
nuclear@3 1941 err = ZIP_ERRNO;
nuclear@3 1942
nuclear@3 1943 #ifndef NO_ADDFILEINEXISTINGZIP
nuclear@3 1944 TRYFREE(zi->globalcomment);
nuclear@3 1945 #endif
nuclear@3 1946 TRYFREE(zi);
nuclear@3 1947
nuclear@3 1948 return err;
nuclear@3 1949 }
nuclear@3 1950
nuclear@3 1951 extern int ZEXPORT zipRemoveExtraInfoBlock (char* pData, int* dataLen, short sHeader)
nuclear@3 1952 {
nuclear@3 1953 char* p = pData;
nuclear@3 1954 int size = 0;
nuclear@3 1955 char* pNewHeader;
nuclear@3 1956 char* pTmp;
nuclear@3 1957 short header;
nuclear@3 1958 short dataSize;
nuclear@3 1959
nuclear@3 1960 int retVal = ZIP_OK;
nuclear@3 1961
nuclear@3 1962 if(pData == NULL || *dataLen < 4)
nuclear@3 1963 return ZIP_PARAMERROR;
nuclear@3 1964
nuclear@3 1965 pNewHeader = (char*)ALLOC(*dataLen);
nuclear@3 1966 pTmp = pNewHeader;
nuclear@3 1967
nuclear@3 1968 while(p < (pData + *dataLen))
nuclear@3 1969 {
nuclear@3 1970 header = *(short*)p;
nuclear@3 1971 dataSize = *(((short*)p)+1);
nuclear@3 1972
nuclear@3 1973 if( header == sHeader ) // Header found.
nuclear@3 1974 {
nuclear@3 1975 p += dataSize + 4; // skip it. do not copy to temp buffer
nuclear@3 1976 }
nuclear@3 1977 else
nuclear@3 1978 {
nuclear@3 1979 // Extra Info block should not be removed, So copy it to the temp buffer.
nuclear@3 1980 memcpy(pTmp, p, dataSize + 4);
nuclear@3 1981 p += dataSize + 4;
nuclear@3 1982 size += dataSize + 4;
nuclear@3 1983 }
nuclear@3 1984
nuclear@3 1985 }
nuclear@3 1986
nuclear@3 1987 if(size < *dataLen)
nuclear@3 1988 {
nuclear@3 1989 // clean old extra info block.
nuclear@3 1990 memset(pData,0, *dataLen);
nuclear@3 1991
nuclear@3 1992 // copy the new extra info block over the old
nuclear@3 1993 if(size > 0)
nuclear@3 1994 memcpy(pData, pNewHeader, size);
nuclear@3 1995
nuclear@3 1996 // set the new extra info size
nuclear@3 1997 *dataLen = size;
nuclear@3 1998
nuclear@3 1999 retVal = ZIP_OK;
nuclear@3 2000 }
nuclear@3 2001 else
nuclear@3 2002 retVal = ZIP_ERRNO;
nuclear@3 2003
nuclear@3 2004 TRYFREE(pNewHeader);
nuclear@3 2005
nuclear@3 2006 return retVal;
nuclear@3 2007 }