Merge pull request #110 from tesselode/fixes
[wdl/wdl-ol.git] / WDL / zlib / zip.c
blob3af20849e2972e9945703ed55669ac87880c160b
1 /* zip.c -- IO on .zip files using zlib
2 Version 1.1, February 14h, 2010
3 part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
5 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
7 Modifications for Zip64 support
8 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
10 For more info read MiniZip_info.txt
12 Changes
13 Oct-2009 - Mathias Svensson - Remove old C style function prototypes
14 Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives
15 Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions.
16 Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data
17 It is used when recreting zip archive with RAW when deleting items from a zip.
18 ZIP64 data is automaticly added to items that needs it, and existing ZIP64 data need to be removed.
19 Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required)
20 Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include "zlib.h"
30 #include "zip.h"
32 #ifdef STDC
33 # include <stddef.h>
34 # include <string.h>
35 # include <stdlib.h>
36 #endif
37 #ifdef NO_ERRNO_H
38 extern int errno;
39 #else
40 # include <errno.h>
41 #endif
43 #if !defined(NOCRYPT) && !defined(WANTCRYPT)
44 #define NOCRYPT
45 #endif
47 #ifndef local
48 # define local static
49 #endif
50 /* compile with -Dlocal if your debugger can't find static symbols */
52 #ifndef VERSIONMADEBY
53 # define VERSIONMADEBY (0x0) /* platform depedent */
54 #endif
56 #ifndef Z_BUFSIZE
57 #define Z_BUFSIZE (64*1024) //(16384)
58 #endif
60 #ifndef Z_MAXFILENAMEINZIP
61 #define Z_MAXFILENAMEINZIP (256)
62 #endif
64 #ifndef ALLOC
65 # define ALLOC(size) (malloc(size))
66 #endif
67 #ifndef TRYFREE
68 # define TRYFREE(p) {if (p) free(p);}
69 #endif
72 #define SIZECENTRALDIRITEM (0x2e)
73 #define SIZEZIPLOCALHEADER (0x1e)
76 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
79 // NOT sure that this work on ALL platform
80 #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
82 #ifndef SEEK_CUR
83 #define SEEK_CUR 1
84 #endif
86 #ifndef SEEK_END
87 #define SEEK_END 2
88 #endif
90 #ifndef SEEK_SET
91 #define SEEK_SET 0
92 #endif
94 #ifndef DEF_MEM_LEVEL
95 #if MAX_MEM_LEVEL >= 8
96 # define DEF_MEM_LEVEL 8
97 #else
98 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
99 #endif
100 #endif
101 const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
104 #define SIZEDATA_INDATABLOCK (4096-(4*4))
106 #define LOCALHEADERMAGIC (0x04034b50)
107 #define CENTRALHEADERMAGIC (0x02014b50)
108 #define ENDHEADERMAGIC (0x06054b50)
109 #define ZIP64ENDHEADERMAGIC (0x6064b50)
110 #define ZIP64ENDLOCHEADERMAGIC (0x7064b50)
112 #define FLAG_LOCALHEADER_OFFSET (0x06)
113 #define CRC_LOCALHEADER_OFFSET (0x0e)
115 #define SIZECENTRALHEADER (0x2e) /* 46 */
117 typedef struct linkedlist_datablock_internal_s
119 struct linkedlist_datablock_internal_s* next_datablock;
120 uLong avail_in_this_block;
121 uLong filled_in_this_block;
122 uLong unused; /* for future use and alignement */
123 unsigned char data[SIZEDATA_INDATABLOCK];
124 } linkedlist_datablock_internal;
126 typedef struct linkedlist_data_s
128 linkedlist_datablock_internal* first_block;
129 linkedlist_datablock_internal* last_block;
130 } linkedlist_data;
133 typedef struct
135 z_stream stream; /* zLib stream structure for inflate */
136 #ifdef HAVE_BZIP2
137 bz_stream bstream; /* bzLib stream structure for bziped */
138 #endif
140 int stream_initialised; /* 1 is stream is initialised */
141 uInt pos_in_buffered_data; /* last written byte in buffered_data */
143 ZPOS64_T pos_local_header; /* offset of the local header of the file
144 currenty writing */
145 char* central_header; /* central header data for the current file */
146 uLong size_centralExtra;
147 uLong size_centralheader; /* size of the central header for cur file */
148 uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */
149 uLong flag; /* flag of the file currently writing */
151 int method; /* compression method of file currenty wr.*/
152 int raw; /* 1 for directly writing raw data */
153 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
154 uLong dosDate;
155 uLong crc32;
156 int encrypt;
157 int zip64; /* Add ZIP64 extened information in the extra field */
158 ZPOS64_T pos_zip64extrainfo;
159 ZPOS64_T totalCompressedData;
160 ZPOS64_T totalUncompressedData;
161 #ifndef NOCRYPT
162 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
163 const z_crc_t* pcrc_32_tab;
164 int crypt_header_size;
165 #endif
166 } curfile64_info;
168 typedef struct
170 zlib_filefunc64_32_def z_filefunc;
171 voidpf filestream; /* io structore of the zipfile */
172 linkedlist_data central_dir;/* datablock with central dir in construction*/
173 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
174 curfile64_info ci; /* info on the file curretly writing */
176 ZPOS64_T begin_pos; /* position of the beginning of the zipfile */
177 ZPOS64_T add_position_when_writting_offset;
178 ZPOS64_T number_entry;
180 #ifndef NO_ADDFILEINEXISTINGZIP
181 char *globalcomment;
182 #endif
184 } zip64_internal;
187 #ifndef NOCRYPT
188 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
189 #include "crypt.h"
190 #endif
192 local linkedlist_datablock_internal* allocate_new_datablock()
194 linkedlist_datablock_internal* ldi;
195 ldi = (linkedlist_datablock_internal*)
196 ALLOC(sizeof(linkedlist_datablock_internal));
197 if (ldi!=NULL)
199 ldi->next_datablock = NULL ;
200 ldi->filled_in_this_block = 0 ;
201 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
203 return ldi;
206 local void free_datablock(linkedlist_datablock_internal* ldi)
208 while (ldi!=NULL)
210 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
211 TRYFREE(ldi);
212 ldi = ldinext;
216 local void init_linkedlist(linkedlist_data* ll)
218 ll->first_block = ll->last_block = NULL;
221 local void free_linkedlist(linkedlist_data* ll)
223 free_datablock(ll->first_block);
224 ll->first_block = ll->last_block = NULL;
228 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len)
230 linkedlist_datablock_internal* ldi;
231 const unsigned char* from_copy;
233 if (ll==NULL)
234 return ZIP_INTERNALERROR;
236 if (ll->last_block == NULL)
238 ll->first_block = ll->last_block = allocate_new_datablock();
239 if (ll->first_block == NULL)
240 return ZIP_INTERNALERROR;
243 ldi = ll->last_block;
244 from_copy = (unsigned char*)buf;
246 while (len>0)
248 uInt copy_this;
249 uInt i;
250 unsigned char* to_copy;
252 if (ldi->avail_in_this_block==0)
254 ldi->next_datablock = allocate_new_datablock();
255 if (ldi->next_datablock == NULL)
256 return ZIP_INTERNALERROR;
257 ldi = ldi->next_datablock ;
258 ll->last_block = ldi;
261 if (ldi->avail_in_this_block < len)
262 copy_this = (uInt)ldi->avail_in_this_block;
263 else
264 copy_this = (uInt)len;
266 to_copy = &(ldi->data[ldi->filled_in_this_block]);
268 for (i=0;i<copy_this;i++)
269 *(to_copy+i)=*(from_copy+i);
271 ldi->filled_in_this_block += copy_this;
272 ldi->avail_in_this_block -= copy_this;
273 from_copy += copy_this ;
274 len -= copy_this;
276 return ZIP_OK;
281 /****************************************************************************/
283 #ifndef NO_ADDFILEINEXISTINGZIP
284 /* ===========================================================================
285 Inputs a long in LSB order to the given file
286 nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
289 local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte));
290 local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)
292 unsigned char buf[8];
293 int n;
294 for (n = 0; n < nbByte; n++)
296 buf[n] = (unsigned char)(x & 0xff);
297 x >>= 8;
299 if (x != 0)
300 { /* data overflow - hack for ZIP64 (X Roche) */
301 for (n = 0; n < nbByte; n++)
303 buf[n] = 0xff;
307 if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
308 return ZIP_ERRNO;
309 else
310 return ZIP_OK;
313 local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte));
314 local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte)
316 unsigned char* buf=(unsigned char*)dest;
317 int n;
318 for (n = 0; n < nbByte; n++) {
319 buf[n] = (unsigned char)(x & 0xff);
320 x >>= 8;
323 if (x != 0)
324 { /* data overflow - hack for ZIP64 */
325 for (n = 0; n < nbByte; n++)
327 buf[n] = 0xff;
332 /****************************************************************************/
335 local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm)
337 uLong year = (uLong)ptm->tm_year;
338 if (year>=1980)
339 year-=1980;
340 else if (year>=80)
341 year-=80;
342 return
343 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
344 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
348 /****************************************************************************/
350 local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi));
352 local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi)
354 unsigned char c;
355 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
356 if (err==1)
358 *pi = (int)c;
359 return ZIP_OK;
361 else
363 if (ZERROR64(*pzlib_filefunc_def,filestream))
364 return ZIP_ERRNO;
365 else
366 return ZIP_EOF;
371 /* ===========================================================================
372 Reads a long in LSB order from the given gz_stream. Sets
374 local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
376 local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
378 uLong x ;
379 int i = 0;
380 int err;
382 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
383 x = (uLong)i;
385 if (err==ZIP_OK)
386 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
387 x += ((uLong)i)<<8;
389 if (err==ZIP_OK)
390 *pX = x;
391 else
392 *pX = 0;
393 return err;
396 local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
398 local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
400 uLong x ;
401 int i = 0;
402 int err;
404 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
405 x = (uLong)i;
407 if (err==ZIP_OK)
408 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
409 x += ((uLong)i)<<8;
411 if (err==ZIP_OK)
412 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
413 x += ((uLong)i)<<16;
415 if (err==ZIP_OK)
416 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
417 x += ((uLong)i)<<24;
419 if (err==ZIP_OK)
420 *pX = x;
421 else
422 *pX = 0;
423 return err;
426 local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX));
429 local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
431 ZPOS64_T x;
432 int i = 0;
433 int err;
435 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
436 x = (ZPOS64_T)i;
438 if (err==ZIP_OK)
439 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
440 x += ((ZPOS64_T)i)<<8;
442 if (err==ZIP_OK)
443 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
444 x += ((ZPOS64_T)i)<<16;
446 if (err==ZIP_OK)
447 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
448 x += ((ZPOS64_T)i)<<24;
450 if (err==ZIP_OK)
451 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
452 x += ((ZPOS64_T)i)<<32;
454 if (err==ZIP_OK)
455 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
456 x += ((ZPOS64_T)i)<<40;
458 if (err==ZIP_OK)
459 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
460 x += ((ZPOS64_T)i)<<48;
462 if (err==ZIP_OK)
463 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
464 x += ((ZPOS64_T)i)<<56;
466 if (err==ZIP_OK)
467 *pX = x;
468 else
469 *pX = 0;
471 return err;
474 #ifndef BUFREADCOMMENT
475 #define BUFREADCOMMENT (0x400)
476 #endif
478 Locate the Central directory of a zipfile (at the end, just before
479 the global comment)
481 local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
483 local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
485 unsigned char* buf;
486 ZPOS64_T uSizeFile;
487 ZPOS64_T uBackRead;
488 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
489 ZPOS64_T uPosFound=0;
491 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
492 return 0;
495 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
497 if (uMaxBack>uSizeFile)
498 uMaxBack = uSizeFile;
500 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
501 if (buf==NULL)
502 return 0;
504 uBackRead = 4;
505 while (uBackRead<uMaxBack)
507 uLong uReadSize;
508 ZPOS64_T uReadPos ;
509 int i;
510 if (uBackRead+BUFREADCOMMENT>uMaxBack)
511 uBackRead = uMaxBack;
512 else
513 uBackRead+=BUFREADCOMMENT;
514 uReadPos = uSizeFile-uBackRead ;
516 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
517 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
518 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
519 break;
521 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
522 break;
524 for (i=(int)uReadSize-3; (i--)>0;)
525 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
526 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
528 uPosFound = uReadPos+i;
529 break;
532 if (uPosFound!=0)
533 break;
535 TRYFREE(buf);
536 return uPosFound;
540 Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before
541 the global comment)
543 local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
545 local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
547 unsigned char* buf;
548 ZPOS64_T uSizeFile;
549 ZPOS64_T uBackRead;
550 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
551 ZPOS64_T uPosFound=0;
552 uLong uL;
553 ZPOS64_T relativeOffset;
555 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
556 return 0;
558 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
560 if (uMaxBack>uSizeFile)
561 uMaxBack = uSizeFile;
563 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
564 if (buf==NULL)
565 return 0;
567 uBackRead = 4;
568 while (uBackRead<uMaxBack)
570 uLong uReadSize;
571 ZPOS64_T uReadPos;
572 int i;
573 if (uBackRead+BUFREADCOMMENT>uMaxBack)
574 uBackRead = uMaxBack;
575 else
576 uBackRead+=BUFREADCOMMENT;
577 uReadPos = uSizeFile-uBackRead ;
579 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
580 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
581 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
582 break;
584 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
585 break;
587 for (i=(int)uReadSize-3; (i--)>0;)
589 // Signature "0x07064b50" Zip64 end of central directory locater
590 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
592 uPosFound = uReadPos+i;
593 break;
597 if (uPosFound!=0)
598 break;
601 TRYFREE(buf);
602 if (uPosFound == 0)
603 return 0;
605 /* Zip64 end of central directory locator */
606 if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
607 return 0;
609 /* the signature, already checked */
610 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
611 return 0;
613 /* number of the disk with the start of the zip64 end of central directory */
614 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
615 return 0;
616 if (uL != 0)
617 return 0;
619 /* relative offset of the zip64 end of central directory record */
620 if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK)
621 return 0;
623 /* total number of disks */
624 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
625 return 0;
626 if (uL != 1)
627 return 0;
629 /* Goto Zip64 end of central directory record */
630 if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
631 return 0;
633 /* the signature */
634 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
635 return 0;
637 if (uL != 0x06064b50) // signature of 'Zip64 end of central directory'
638 return 0;
640 return relativeOffset;
643 int LoadCentralDirectoryRecord(zip64_internal* pziinit)
645 int err=ZIP_OK;
646 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
648 ZPOS64_T size_central_dir; /* size of the central directory */
649 ZPOS64_T offset_central_dir; /* offset of start of central directory */
650 ZPOS64_T central_pos;
651 uLong uL;
653 uLong number_disk; /* number of the current dist, used for
654 spaning ZIP, unsupported, always 0*/
655 uLong number_disk_with_CD; /* number the the disk with central dir, used
656 for spaning ZIP, unsupported, always 0*/
657 ZPOS64_T number_entry;
658 ZPOS64_T number_entry_CD; /* total number of entries in
659 the central dir
660 (same than number_entry on nospan) */
661 uLong VersionMadeBy;
662 uLong VersionNeeded;
663 uLong size_comment;
665 int hasZIP64Record = 0;
667 // check first if we find a ZIP64 record
668 central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream);
669 if(central_pos > 0)
671 hasZIP64Record = 1;
673 else if(central_pos == 0)
675 central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream);
678 /* disable to allow appending to empty ZIP archive
679 if (central_pos==0)
680 err=ZIP_ERRNO;
683 if(hasZIP64Record)
685 ZPOS64_T sizeEndOfCentralDirectory;
686 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
687 err=ZIP_ERRNO;
689 /* the signature, already checked */
690 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
691 err=ZIP_ERRNO;
693 /* size of zip64 end of central directory record */
694 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK)
695 err=ZIP_ERRNO;
697 /* version made by */
698 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK)
699 err=ZIP_ERRNO;
701 /* version needed to extract */
702 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK)
703 err=ZIP_ERRNO;
705 /* number of this disk */
706 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
707 err=ZIP_ERRNO;
709 /* number of the disk with the start of the central directory */
710 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
711 err=ZIP_ERRNO;
713 /* total number of entries in the central directory on this disk */
714 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK)
715 err=ZIP_ERRNO;
717 /* total number of entries in the central directory */
718 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK)
719 err=ZIP_ERRNO;
721 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
722 err=ZIP_BADZIPFILE;
724 /* size of the central directory */
725 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK)
726 err=ZIP_ERRNO;
728 /* offset of start of central directory with respect to the
729 starting disk number */
730 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK)
731 err=ZIP_ERRNO;
733 // TODO..
734 // read the comment from the standard central header.
735 size_comment = 0;
737 else
739 // Read End of central Directory info
740 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
741 err=ZIP_ERRNO;
743 /* the signature, already checked */
744 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
745 err=ZIP_ERRNO;
747 /* number of this disk */
748 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
749 err=ZIP_ERRNO;
751 /* number of the disk with the start of the central directory */
752 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
753 err=ZIP_ERRNO;
755 /* total number of entries in the central dir on this disk */
756 number_entry = 0;
757 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
758 err=ZIP_ERRNO;
759 else
760 number_entry = uL;
762 /* total number of entries in the central dir */
763 number_entry_CD = 0;
764 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
765 err=ZIP_ERRNO;
766 else
767 number_entry_CD = uL;
769 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
770 err=ZIP_BADZIPFILE;
772 /* size of the central directory */
773 size_central_dir = 0;
774 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
775 err=ZIP_ERRNO;
776 else
777 size_central_dir = uL;
779 /* offset of start of central directory with respect to the starting disk number */
780 offset_central_dir = 0;
781 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
782 err=ZIP_ERRNO;
783 else
784 offset_central_dir = uL;
787 /* zipfile global comment length */
788 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK)
789 err=ZIP_ERRNO;
792 if ((central_pos<offset_central_dir+size_central_dir) &&
793 (err==ZIP_OK))
794 err=ZIP_BADZIPFILE;
796 if (err!=ZIP_OK)
798 ZCLOSE64(pziinit->z_filefunc, pziinit->filestream);
799 return ZIP_ERRNO;
802 if (size_comment>0)
804 pziinit->globalcomment = (char*)ALLOC(size_comment+1);
805 if (pziinit->globalcomment)
807 size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment);
808 pziinit->globalcomment[size_comment]=0;
812 byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
813 pziinit->add_position_when_writting_offset = byte_before_the_zipfile;
816 ZPOS64_T size_central_dir_to_read = size_central_dir;
817 size_t buf_size = SIZEDATA_INDATABLOCK;
818 void* buf_read = (void*)ALLOC(buf_size);
819 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
820 err=ZIP_ERRNO;
822 while ((size_central_dir_to_read>0) && (err==ZIP_OK))
824 ZPOS64_T read_this = SIZEDATA_INDATABLOCK;
825 if (read_this > size_central_dir_to_read)
826 read_this = size_central_dir_to_read;
828 if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this)
829 err=ZIP_ERRNO;
831 if (err==ZIP_OK)
832 err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
834 size_central_dir_to_read-=read_this;
836 TRYFREE(buf_read);
838 pziinit->begin_pos = byte_before_the_zipfile;
839 pziinit->number_entry = number_entry_CD;
841 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
842 err=ZIP_ERRNO;
844 return err;
848 #endif /* !NO_ADDFILEINEXISTINGZIP*/
851 /************************************************************/
852 extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def)
854 zip64_internal ziinit;
855 zip64_internal* zi;
856 int err=ZIP_OK;
858 ziinit.z_filefunc.zseek32_file = NULL;
859 ziinit.z_filefunc.ztell32_file = NULL;
860 if (pzlib_filefunc64_32_def==NULL)
861 fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64);
862 else
863 ziinit.z_filefunc = *pzlib_filefunc64_32_def;
865 ziinit.filestream = ZOPEN64(ziinit.z_filefunc,
866 pathname,
867 (append == APPEND_STATUS_CREATE) ?
868 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
869 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
871 if (ziinit.filestream == NULL)
872 return NULL;
874 if (append == APPEND_STATUS_CREATEAFTER)
875 ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
877 ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream);
878 ziinit.in_opened_file_inzip = 0;
879 ziinit.ci.stream_initialised = 0;
880 ziinit.number_entry = 0;
881 ziinit.add_position_when_writting_offset = 0;
882 init_linkedlist(&(ziinit.central_dir));
886 zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
887 if (zi==NULL)
889 ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
890 return NULL;
893 /* now we add file in a zipfile */
894 # ifndef NO_ADDFILEINEXISTINGZIP
895 ziinit.globalcomment = NULL;
896 if (append == APPEND_STATUS_ADDINZIP)
898 // Read and Cache Central Directory Records
899 err = LoadCentralDirectoryRecord(&ziinit);
902 if (globalcomment)
904 *globalcomment = ziinit.globalcomment;
906 # endif /* !NO_ADDFILEINEXISTINGZIP*/
908 if (err != ZIP_OK)
910 # ifndef NO_ADDFILEINEXISTINGZIP
911 TRYFREE(ziinit.globalcomment);
912 # endif /* !NO_ADDFILEINEXISTINGZIP*/
913 TRYFREE(zi);
914 return NULL;
916 else
918 *zi = ziinit;
919 return (zipFile)zi;
923 extern zipFile ZEXPORT zipOpen2 (const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def)
925 if (pzlib_filefunc32_def != NULL)
927 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
928 fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
929 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
931 else
932 return zipOpen3(pathname, append, globalcomment, NULL);
935 extern zipFile ZEXPORT zipOpen2_64 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)
937 if (pzlib_filefunc_def != NULL)
939 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
940 zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
941 zlib_filefunc64_32_def_fill.ztell32_file = NULL;
942 zlib_filefunc64_32_def_fill.zseek32_file = NULL;
943 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
945 else
946 return zipOpen3(pathname, append, globalcomment, NULL);
951 extern zipFile ZEXPORT zipOpen (const char* pathname, int append)
953 return zipOpen3((const void*)pathname,append,NULL,NULL);
956 extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append)
958 return zipOpen3(pathname,append,NULL,NULL);
961 int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local)
963 /* write the local header */
964 int err;
965 uInt size_filename = (uInt)strlen(filename);
966 uInt size_extrafield = size_extrafield_local;
968 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4);
970 if (err==ZIP_OK)
972 if(zi->ci.zip64)
973 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */
974 else
975 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
978 if (err==ZIP_OK)
979 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
981 if (err==ZIP_OK)
982 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
984 if (err==ZIP_OK)
985 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
987 // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later
988 if (err==ZIP_OK)
989 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
990 if (err==ZIP_OK)
992 if(zi->ci.zip64)
993 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */
994 else
995 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
997 if (err==ZIP_OK)
999 if(zi->ci.zip64)
1000 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */
1001 else
1002 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
1005 if (err==ZIP_OK)
1006 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
1008 if(zi->ci.zip64)
1010 size_extrafield += 20;
1013 if (err==ZIP_OK)
1014 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2);
1016 if ((err==ZIP_OK) && (size_filename > 0))
1018 if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
1019 err = ZIP_ERRNO;
1022 if ((err==ZIP_OK) && (size_extrafield_local > 0))
1024 if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local)
1025 err = ZIP_ERRNO;
1029 if ((err==ZIP_OK) && (zi->ci.zip64))
1031 // write the Zip64 extended info
1032 short HeaderID = 1;
1033 short DataSize = 16;
1034 ZPOS64_T CompressedSize = 0;
1035 ZPOS64_T UncompressedSize = 0;
1037 // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file)
1038 zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
1040 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)HeaderID,2);
1041 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)DataSize,2);
1043 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8);
1044 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8);
1047 return err;
1051 NOTE.
1052 When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped
1053 before calling this function it can be done with zipRemoveExtraInfoBlock
1055 It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize
1056 unnecessary allocations.
1058 extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1059 const void* extrafield_local, uInt size_extrafield_local,
1060 const void* extrafield_global, uInt size_extrafield_global,
1061 const char* comment, int method, int level, int raw,
1062 int windowBits,int memLevel, int strategy,
1063 const char* password, uLong crcForCrypting,
1064 uLong versionMadeBy, uLong flagBase, int zip64)
1066 zip64_internal* zi;
1067 uInt size_filename;
1068 uInt size_comment;
1069 uInt i;
1070 int err = ZIP_OK;
1072 # ifdef NOCRYPT
1073 // (crcForCrypting);
1074 if (password != NULL)
1075 return ZIP_PARAMERROR;
1076 # endif
1078 if (file == NULL)
1079 return ZIP_PARAMERROR;
1081 #ifdef HAVE_BZIP2
1082 if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED))
1083 return ZIP_PARAMERROR;
1084 #else
1085 if ((method!=0) && (method!=Z_DEFLATED))
1086 return ZIP_PARAMERROR;
1087 #endif
1089 zi = (zip64_internal*)file;
1091 if (zi->in_opened_file_inzip == 1)
1093 err = zipCloseFileInZip (file);
1094 if (err != ZIP_OK)
1095 return err;
1098 if (filename==NULL)
1099 filename="-";
1101 if (comment==NULL)
1102 size_comment = 0;
1103 else
1104 size_comment = (uInt)strlen(comment);
1106 size_filename = (uInt)strlen(filename);
1108 if (zipfi == NULL)
1109 zi->ci.dosDate = 0;
1110 else
1112 if (zipfi->dosDate != 0)
1113 zi->ci.dosDate = zipfi->dosDate;
1114 else
1115 zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
1118 zi->ci.flag = flagBase;
1119 if ((level==8) || (level==9))
1120 zi->ci.flag |= 2;
1121 if (level==2)
1122 zi->ci.flag |= 4;
1123 if (level==1)
1124 zi->ci.flag |= 6;
1125 if (password != NULL)
1126 zi->ci.flag |= 1;
1128 zi->ci.crc32 = 0;
1129 zi->ci.method = method;
1130 zi->ci.encrypt = 0;
1131 zi->ci.stream_initialised = 0;
1132 zi->ci.pos_in_buffered_data = 0;
1133 zi->ci.raw = raw;
1134 zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
1136 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment;
1137 zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data
1139 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree);
1141 zi->ci.size_centralExtra = size_extrafield_global;
1142 zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
1143 /* version info */
1144 zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
1145 zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
1146 zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
1147 zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
1148 zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
1149 zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
1150 zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
1151 zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
1152 zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
1153 zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
1154 zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
1155 zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
1157 if (zipfi==NULL)
1158 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
1159 else
1160 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
1162 if (zipfi==NULL)
1163 zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
1164 else
1165 zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
1167 if(zi->ci.pos_local_header >= 0xffffffff)
1168 zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
1169 else
1170 zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset,4);
1172 for (i=0;i<size_filename;i++)
1173 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
1175 for (i=0;i<size_extrafield_global;i++)
1176 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
1177 *(((const char*)extrafield_global)+i);
1179 for (i=0;i<size_comment;i++)
1180 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
1181 size_extrafield_global+i) = *(comment+i);
1182 if (zi->ci.central_header == NULL)
1183 return ZIP_INTERNALERROR;
1185 zi->ci.zip64 = zip64;
1186 zi->ci.totalCompressedData = 0;
1187 zi->ci.totalUncompressedData = 0;
1188 zi->ci.pos_zip64extrainfo = 0;
1190 err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
1192 #ifdef HAVE_BZIP2
1193 zi->ci.bstream.avail_in = (uInt)0;
1194 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1195 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1196 zi->ci.bstream.total_in_hi32 = 0;
1197 zi->ci.bstream.total_in_lo32 = 0;
1198 zi->ci.bstream.total_out_hi32 = 0;
1199 zi->ci.bstream.total_out_lo32 = 0;
1200 #endif
1202 zi->ci.stream.avail_in = (uInt)0;
1203 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1204 zi->ci.stream.next_out = zi->ci.buffered_data;
1205 zi->ci.stream.total_in = 0;
1206 zi->ci.stream.total_out = 0;
1207 zi->ci.stream.data_type = Z_BINARY;
1209 #ifdef HAVE_BZIP2
1210 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1211 #else
1212 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1213 #endif
1215 if(zi->ci.method == Z_DEFLATED)
1217 zi->ci.stream.zalloc = (alloc_func)0;
1218 zi->ci.stream.zfree = (free_func)0;
1219 zi->ci.stream.opaque = (voidpf)0;
1221 if (windowBits>0)
1222 windowBits = -windowBits;
1224 err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
1226 if (err==Z_OK)
1227 zi->ci.stream_initialised = Z_DEFLATED;
1229 else if(zi->ci.method == Z_BZIP2ED)
1231 #ifdef HAVE_BZIP2
1232 // Init BZip stuff here
1233 zi->ci.bstream.bzalloc = 0;
1234 zi->ci.bstream.bzfree = 0;
1235 zi->ci.bstream.opaque = (voidpf)0;
1237 err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35);
1238 if(err == BZ_OK)
1239 zi->ci.stream_initialised = Z_BZIP2ED;
1240 #endif
1245 # ifndef NOCRYPT
1246 zi->ci.crypt_header_size = 0;
1247 if ((err==Z_OK) && (password != NULL))
1249 unsigned char bufHead[RAND_HEAD_LEN];
1250 unsigned int sizeHead;
1251 zi->ci.encrypt = 1;
1252 zi->ci.pcrc_32_tab = get_crc_table();
1253 /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
1255 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
1256 zi->ci.crypt_header_size = sizeHead;
1258 if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
1259 err = ZIP_ERRNO;
1261 # endif
1263 if (err==Z_OK)
1264 zi->in_opened_file_inzip = 1;
1265 return err;
1268 extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1269 const void* extrafield_local, uInt size_extrafield_local,
1270 const void* extrafield_global, uInt size_extrafield_global,
1271 const char* comment, int method, int level, int raw,
1272 int windowBits,int memLevel, int strategy,
1273 const char* password, uLong crcForCrypting,
1274 uLong versionMadeBy, uLong flagBase)
1276 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1277 extrafield_local, size_extrafield_local,
1278 extrafield_global, size_extrafield_global,
1279 comment, method, level, raw,
1280 windowBits, memLevel, strategy,
1281 password, crcForCrypting, versionMadeBy, flagBase, 0);
1284 extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1285 const void* extrafield_local, uInt size_extrafield_local,
1286 const void* extrafield_global, uInt size_extrafield_global,
1287 const char* comment, int method, int level, int raw,
1288 int windowBits,int memLevel, int strategy,
1289 const char* password, uLong crcForCrypting)
1291 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1292 extrafield_local, size_extrafield_local,
1293 extrafield_global, size_extrafield_global,
1294 comment, method, level, raw,
1295 windowBits, memLevel, strategy,
1296 password, crcForCrypting, VERSIONMADEBY, 0, 0);
1299 extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1300 const void* extrafield_local, uInt size_extrafield_local,
1301 const void* extrafield_global, uInt size_extrafield_global,
1302 const char* comment, int method, int level, int raw,
1303 int windowBits,int memLevel, int strategy,
1304 const char* password, uLong crcForCrypting, int zip64)
1306 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1307 extrafield_local, size_extrafield_local,
1308 extrafield_global, size_extrafield_global,
1309 comment, method, level, raw,
1310 windowBits, memLevel, strategy,
1311 password, crcForCrypting, VERSIONMADEBY, 0, zip64);
1314 extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1315 const void* extrafield_local, uInt size_extrafield_local,
1316 const void* extrafield_global, uInt size_extrafield_global,
1317 const char* comment, int method, int level, int raw)
1319 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1320 extrafield_local, size_extrafield_local,
1321 extrafield_global, size_extrafield_global,
1322 comment, method, level, raw,
1323 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1324 NULL, 0, VERSIONMADEBY, 0, 0);
1327 extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1328 const void* extrafield_local, uInt size_extrafield_local,
1329 const void* extrafield_global, uInt size_extrafield_global,
1330 const char* comment, int method, int level, int raw, int zip64)
1332 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1333 extrafield_local, size_extrafield_local,
1334 extrafield_global, size_extrafield_global,
1335 comment, method, level, raw,
1336 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1337 NULL, 0, VERSIONMADEBY, 0, zip64);
1340 extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1341 const void* extrafield_local, uInt size_extrafield_local,
1342 const void*extrafield_global, uInt size_extrafield_global,
1343 const char* comment, int method, int level, int zip64)
1345 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1346 extrafield_local, size_extrafield_local,
1347 extrafield_global, size_extrafield_global,
1348 comment, method, level, 0,
1349 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1350 NULL, 0, VERSIONMADEBY, 0, zip64);
1353 extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1354 const void* extrafield_local, uInt size_extrafield_local,
1355 const void*extrafield_global, uInt size_extrafield_global,
1356 const char* comment, int method, int level)
1358 return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1359 extrafield_local, size_extrafield_local,
1360 extrafield_global, size_extrafield_global,
1361 comment, method, level, 0,
1362 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1363 NULL, 0, VERSIONMADEBY, 0, 0);
1366 local int zip64FlushWriteBuffer(zip64_internal* zi)
1368 int err=ZIP_OK;
1370 if (zi->ci.encrypt != 0)
1372 #ifndef NOCRYPT
1373 uInt i;
1374 int t;
1375 for (i=0;i<zi->ci.pos_in_buffered_data;i++)
1376 zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t);
1377 #endif
1380 if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data)
1381 err = ZIP_ERRNO;
1383 zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data;
1385 #ifdef HAVE_BZIP2
1386 if(zi->ci.method == Z_BZIP2ED)
1388 zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32;
1389 zi->ci.bstream.total_in_lo32 = 0;
1390 zi->ci.bstream.total_in_hi32 = 0;
1392 else
1393 #endif
1395 zi->ci.totalUncompressedData += zi->ci.stream.total_in;
1396 zi->ci.stream.total_in = 0;
1400 zi->ci.pos_in_buffered_data = 0;
1402 return err;
1405 extern int ZEXPORT zipWriteInFileInZip (zipFile file,const void* buf,unsigned int len)
1407 zip64_internal* zi;
1408 int err=ZIP_OK;
1410 if (file == NULL)
1411 return ZIP_PARAMERROR;
1412 zi = (zip64_internal*)file;
1414 if (zi->in_opened_file_inzip == 0)
1415 return ZIP_PARAMERROR;
1417 zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len);
1419 #ifdef HAVE_BZIP2
1420 if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw))
1422 zi->ci.bstream.next_in = (void*)buf;
1423 zi->ci.bstream.avail_in = len;
1424 err = BZ_RUN_OK;
1426 while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0))
1428 if (zi->ci.bstream.avail_out == 0)
1430 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1431 err = ZIP_ERRNO;
1432 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1433 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1437 if(err != BZ_RUN_OK)
1438 break;
1440 if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1442 uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32;
1443 // uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32;
1444 err=BZ2_bzCompress(&zi->ci.bstream, BZ_RUN);
1446 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ;
1450 if(err == BZ_RUN_OK)
1451 err = ZIP_OK;
1453 else
1454 #endif
1456 zi->ci.stream.next_in = (Bytef*)buf;
1457 zi->ci.stream.avail_in = len;
1459 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
1461 if (zi->ci.stream.avail_out == 0)
1463 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1464 err = ZIP_ERRNO;
1465 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1466 zi->ci.stream.next_out = zi->ci.buffered_data;
1470 if(err != ZIP_OK)
1471 break;
1473 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1475 uLong uTotalOutBefore = zi->ci.stream.total_out;
1476 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
1477 if(uTotalOutBefore > zi->ci.stream.total_out)
1479 int bBreak = 0;
1480 bBreak++;
1483 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1485 else
1487 uInt copy_this,i;
1488 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
1489 copy_this = zi->ci.stream.avail_in;
1490 else
1491 copy_this = zi->ci.stream.avail_out;
1493 for (i = 0; i < copy_this; i++)
1494 *(((char*)zi->ci.stream.next_out)+i) =
1495 *(((const char*)zi->ci.stream.next_in)+i);
1497 zi->ci.stream.avail_in -= copy_this;
1498 zi->ci.stream.avail_out-= copy_this;
1499 zi->ci.stream.next_in+= copy_this;
1500 zi->ci.stream.next_out+= copy_this;
1501 zi->ci.stream.total_in+= copy_this;
1502 zi->ci.stream.total_out+= copy_this;
1503 zi->ci.pos_in_buffered_data += copy_this;
1506 }// while(...)
1509 return err;
1512 extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32)
1514 return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32);
1517 extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32)
1519 zip64_internal* zi;
1520 ZPOS64_T compressed_size;
1521 uLong invalidValue = 0xffffffff;
1522 short datasize = 0;
1523 int err=ZIP_OK;
1525 if (file == NULL)
1526 return ZIP_PARAMERROR;
1527 zi = (zip64_internal*)file;
1529 if (zi->in_opened_file_inzip == 0)
1530 return ZIP_PARAMERROR;
1531 zi->ci.stream.avail_in = 0;
1533 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1535 while (err==ZIP_OK)
1537 uLong uTotalOutBefore;
1538 if (zi->ci.stream.avail_out == 0)
1540 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1541 err = ZIP_ERRNO;
1542 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1543 zi->ci.stream.next_out = zi->ci.buffered_data;
1545 uTotalOutBefore = zi->ci.stream.total_out;
1546 err=deflate(&zi->ci.stream, Z_FINISH);
1547 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1550 else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1552 #ifdef HAVE_BZIP2
1553 err = BZ_FINISH_OK;
1554 while (err==BZ_FINISH_OK)
1556 uLong uTotalOutBefore;
1557 if (zi->ci.bstream.avail_out == 0)
1559 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1560 err = ZIP_ERRNO;
1561 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1562 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1564 uTotalOutBefore = zi->ci.bstream.total_out_lo32;
1565 err=BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH);
1566 if(err == BZ_STREAM_END)
1567 err = Z_STREAM_END;
1569 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore);
1572 if(err == BZ_FINISH_OK)
1573 err = ZIP_OK;
1574 #endif
1577 if (err==Z_STREAM_END)
1578 err=ZIP_OK; /* this is normal */
1580 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1582 if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
1583 err = ZIP_ERRNO;
1586 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1588 int tmp_err = deflateEnd(&zi->ci.stream);
1589 if (err == ZIP_OK)
1590 err = tmp_err;
1591 zi->ci.stream_initialised = 0;
1593 #ifdef HAVE_BZIP2
1594 else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1596 int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
1597 if (err==ZIP_OK)
1598 err = tmperr;
1599 zi->ci.stream_initialised = 0;
1601 #endif
1603 if (!zi->ci.raw)
1605 crc32 = (uLong)zi->ci.crc32;
1606 uncompressed_size = zi->ci.totalUncompressedData;
1608 compressed_size = zi->ci.totalCompressedData;
1610 # ifndef NOCRYPT
1611 compressed_size += zi->ci.crypt_header_size;
1612 # endif
1614 // update Current Item crc and sizes,
1615 if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff)
1617 /*version Made by*/
1618 zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2);
1619 /*version needed*/
1620 zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2);
1624 zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1627 if(compressed_size >= 0xffffffff)
1628 zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/
1629 else
1630 zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/
1632 /// set internal file attributes field
1633 if (zi->ci.stream.data_type == Z_ASCII)
1634 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1636 if(uncompressed_size >= 0xffffffff)
1637 zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/
1638 else
1639 zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/
1641 // Add ZIP64 extra info field for uncompressed size
1642 if(uncompressed_size >= 0xffffffff)
1643 datasize += 8;
1645 // Add ZIP64 extra info field for compressed size
1646 if(compressed_size >= 0xffffffff)
1647 datasize += 8;
1649 // Add ZIP64 extra info field for relative offset to local file header of current file
1650 if(zi->ci.pos_local_header >= 0xffffffff)
1651 datasize += 8;
1653 if(datasize > 0)
1655 char* p = NULL;
1657 if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree)
1659 // we can not write more data to the buffer that we have room for.
1660 return ZIP_BADZIPFILE;
1663 p = zi->ci.central_header + zi->ci.size_centralheader;
1665 // Add Extra Information Header for 'ZIP64 information'
1666 zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID
1667 p += 2;
1668 zip64local_putValue_inmemory(p, datasize, 2); // DataSize
1669 p += 2;
1671 if(uncompressed_size >= 0xffffffff)
1673 zip64local_putValue_inmemory(p, uncompressed_size, 8);
1674 p += 8;
1677 if(compressed_size >= 0xffffffff)
1679 zip64local_putValue_inmemory(p, compressed_size, 8);
1680 p += 8;
1683 if(zi->ci.pos_local_header >= 0xffffffff)
1685 zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
1686 p += 8;
1689 // Update how much extra free space we got in the memory buffer
1690 // and increase the centralheader size so the new ZIP64 fields are included
1691 // ( 4 below is the size of HeaderID and DataSize field )
1692 zi->ci.size_centralExtraFree -= datasize + 4;
1693 zi->ci.size_centralheader += datasize + 4;
1695 // Update the extra info size field
1696 zi->ci.size_centralExtra += datasize + 4;
1697 zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
1700 if (err==ZIP_OK)
1701 err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);
1703 free(zi->ci.central_header);
1705 if (err==ZIP_OK)
1707 // Update the LocalFileHeader with the new values.
1709 ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
1711 if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1712 err = ZIP_ERRNO;
1714 if (err==ZIP_OK)
1715 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1717 if(uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff )
1719 if(zi->ci.pos_zip64extrainfo > 0)
1721 // Update the size in the ZIP64 extended field.
1722 if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
1723 err = ZIP_ERRNO;
1725 if (err==ZIP_OK) /* compressed size, unknown */
1726 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);
1728 if (err==ZIP_OK) /* uncompressed size, unknown */
1729 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8);
1731 else
1732 err = ZIP_BADZIPFILE; // Caller passed zip64 = 0, so no room for zip64 info -> fatal
1734 else
1736 if (err==ZIP_OK) /* compressed size, unknown */
1737 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1739 if (err==ZIP_OK) /* uncompressed size, unknown */
1740 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1743 if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1744 err = ZIP_ERRNO;
1747 zi->number_entry ++;
1748 zi->in_opened_file_inzip = 0;
1750 return err;
1753 extern int ZEXPORT zipCloseFileInZip (zipFile file)
1755 return zipCloseFileInZipRaw (file,0,0);
1758 int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip)
1760 int err = ZIP_OK;
1761 ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset;
1763 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
1765 /*num disks*/
1766 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1767 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1769 /*relative offset*/
1770 if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */
1771 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8);
1773 /*total disks*/ /* Do not support spawning of disk so always say 1 here*/
1774 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1775 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4);
1777 return err;
1780 int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
1782 int err = ZIP_OK;
1784 uLong Zip64DataSize = 44;
1786 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
1788 if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */
1789 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ?
1791 if (err==ZIP_OK) /* version made by */
1792 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
1794 if (err==ZIP_OK) /* version needed */
1795 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
1797 if (err==ZIP_OK) /* number of this disk */
1798 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1800 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1801 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1803 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1804 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
1806 if (err==ZIP_OK) /* total number of entries in the central dir */
1807 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
1809 if (err==ZIP_OK) /* size of the central directory */
1810 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8);
1812 if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
1814 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
1815 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8);
1817 return err;
1819 int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
1821 int err = ZIP_OK;
1823 /*signature*/
1824 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1826 if (err==ZIP_OK) /* number of this disk */
1827 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1829 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1830 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1832 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1835 if(zi->number_entry >= 0xFFFF)
1836 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
1837 else
1838 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1842 if (err==ZIP_OK) /* total number of entries in the central dir */
1844 if(zi->number_entry >= 0xFFFF)
1845 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
1846 else
1847 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1850 if (err==ZIP_OK) /* size of the central directory */
1851 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1853 if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
1855 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
1856 if(pos >= 0xffffffff)
1858 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4);
1860 else
1861 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1864 return err;
1867 int Write_GlobalComment(zip64_internal* zi, const char* global_comment)
1869 int err = ZIP_OK;
1870 uInt size_global_comment = 0;
1872 if(global_comment != NULL)
1873 size_global_comment = (uInt)strlen(global_comment);
1875 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1877 if (err == ZIP_OK && size_global_comment > 0)
1879 if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment)
1880 err = ZIP_ERRNO;
1882 return err;
1885 extern int ZEXPORT zipClose (zipFile file, const char* global_comment)
1887 zip64_internal* zi;
1888 int err = 0;
1889 uLong size_centraldir = 0;
1890 ZPOS64_T centraldir_pos_inzip;
1891 ZPOS64_T pos;
1893 if (file == NULL)
1894 return ZIP_PARAMERROR;
1896 zi = (zip64_internal*)file;
1898 if (zi->in_opened_file_inzip == 1)
1900 err = zipCloseFileInZip (file);
1903 #ifndef NO_ADDFILEINEXISTINGZIP
1904 if (global_comment==NULL)
1905 global_comment = zi->globalcomment;
1906 #endif
1908 centraldir_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
1910 if (err==ZIP_OK)
1912 linkedlist_datablock_internal* ldi = zi->central_dir.first_block;
1913 while (ldi!=NULL)
1915 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1917 if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block)
1918 err = ZIP_ERRNO;
1921 size_centraldir += ldi->filled_in_this_block;
1922 ldi = ldi->next_datablock;
1925 free_linkedlist(&(zi->central_dir));
1927 pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
1928 if(pos >= 0xffffffff || zi->number_entry > 0xFFFF)
1930 ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream);
1931 Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
1933 Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos);
1936 if (err==ZIP_OK)
1937 err = Write_EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
1939 if(err == ZIP_OK)
1940 err = Write_GlobalComment(zi, global_comment);
1942 if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0)
1943 if (err == ZIP_OK)
1944 err = ZIP_ERRNO;
1946 #ifndef NO_ADDFILEINEXISTINGZIP
1947 TRYFREE(zi->globalcomment);
1948 #endif
1949 TRYFREE(zi);
1951 return err;
1954 extern int ZEXPORT zipRemoveExtraInfoBlock (char* pData, int* dataLen, short sHeader)
1956 char* p = pData;
1957 int size = 0;
1958 char* pNewHeader;
1959 char* pTmp;
1960 short header;
1961 short dataSize;
1963 int retVal = ZIP_OK;
1965 if(pData == NULL || *dataLen < 4)
1966 return ZIP_PARAMERROR;
1968 pNewHeader = (char*)ALLOC(*dataLen);
1969 pTmp = pNewHeader;
1971 while(p < (pData + *dataLen))
1973 header = *(short*)p;
1974 dataSize = *(((short*)p)+1);
1976 if( header == sHeader ) // Header found.
1978 p += dataSize + 4; // skip it. do not copy to temp buffer
1980 else
1982 // Extra Info block should not be removed, So copy it to the temp buffer.
1983 memcpy(pTmp, p, dataSize + 4);
1984 p += dataSize + 4;
1985 size += dataSize + 4;
1990 if(size < *dataLen)
1992 // clean old extra info block.
1993 memset(pData,0, *dataLen);
1995 // copy the new extra info block over the old
1996 if(size > 0)
1997 memcpy(pData, pNewHeader, size);
1999 // set the new extra info size
2000 *dataLen = size;
2002 retVal = ZIP_OK;
2004 else
2005 retVal = ZIP_ERRNO;
2007 TRYFREE(pNewHeader);
2009 return retVal;