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
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 recreating zip archive with RAW when deleting items from a zip.
18 ZIP64 data is automatically 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
46 /* compile with -Dlocal if your debugger can't find static symbols */
49 # define VERSIONMADEBY (0x0) /* platform dependent */
53 #define Z_BUFSIZE (64*1024) //(16384)
56 #ifndef Z_MAXFILENAMEINZIP
57 #define Z_MAXFILENAMEINZIP (256)
61 # define ALLOC(size) (malloc(size))
65 #define SIZECENTRALDIRITEM (0x2e)
66 #define SIZEZIPLOCALHEADER (0x1e)
69 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
72 // NOT sure that this work on ALL platform
73 #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
88 #if MAX_MEM_LEVEL >= 8
89 # define DEF_MEM_LEVEL 8
91 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
94 const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
97 #define SIZEDATA_INDATABLOCK (4096-(4*4))
99 #define LOCALHEADERMAGIC (0x04034b50)
100 #define CENTRALHEADERMAGIC (0x02014b50)
101 #define ENDHEADERMAGIC (0x06054b50)
102 #define ZIP64ENDHEADERMAGIC (0x6064b50)
103 #define ZIP64ENDLOCHEADERMAGIC (0x7064b50)
105 #define FLAG_LOCALHEADER_OFFSET (0x06)
106 #define CRC_LOCALHEADER_OFFSET (0x0e)
108 #define SIZECENTRALHEADER (0x2e) /* 46 */
110 typedef struct linkedlist_datablock_internal_s
112 struct linkedlist_datablock_internal_s* next_datablock;
113 uLong avail_in_this_block;
114 uLong filled_in_this_block;
115 uLong unused; /* for future use and alignment */
116 unsigned char data[SIZEDATA_INDATABLOCK];
117 } linkedlist_datablock_internal;
119 typedef struct linkedlist_data_s
121 linkedlist_datablock_internal* first_block;
122 linkedlist_datablock_internal* last_block;
128 z_stream stream; /* zLib stream structure for inflate */
130 bz_stream bstream; /* bzLib stream structure for bziped */
133 int stream_initialised; /* 1 is stream is initialised */
134 uInt pos_in_buffered_data; /* last written byte in buffered_data */
136 ZPOS64_T pos_local_header; /* offset of the local header of the file
138 char* central_header; /* central header data for the current file */
139 uLong size_centralExtra;
140 uLong size_centralheader; /* size of the central header for cur file */
141 uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */
142 uLong flag; /* flag of the file currently writing */
144 int method; /* compression method of file currently wr.*/
145 int raw; /* 1 for directly writing raw data */
146 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
150 int zip64; /* Add ZIP64 extended information in the extra field */
151 ZPOS64_T pos_zip64extrainfo;
152 ZPOS64_T totalCompressedData;
153 ZPOS64_T totalUncompressedData;
155 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
156 const z_crc_t* pcrc_32_tab;
157 unsigned crypt_header_size;
163 zlib_filefunc64_32_def z_filefunc;
164 voidpf filestream; /* io structure of the zipfile */
165 linkedlist_data central_dir;/* datablock with central dir in construction*/
166 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
167 curfile64_info ci; /* info on the file currently writing */
169 ZPOS64_T begin_pos; /* position of the beginning of the zipfile */
170 ZPOS64_T add_position_when_writing_offset;
171 ZPOS64_T number_entry;
173 #ifndef NO_ADDFILEINEXISTINGZIP
181 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
185 local linkedlist_datablock_internal* allocate_new_datablock(void) {
186 linkedlist_datablock_internal* ldi;
187 ldi = (linkedlist_datablock_internal*)
188 ALLOC(sizeof(linkedlist_datablock_internal));
191 ldi->next_datablock = NULL ;
192 ldi->filled_in_this_block = 0 ;
193 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
198 local void free_datablock(linkedlist_datablock_internal* ldi) {
201 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
207 local void init_linkedlist(linkedlist_data* ll) {
208 ll->first_block = ll->last_block = NULL;
211 local void free_linkedlist(linkedlist_data* ll) {
212 free_datablock(ll->first_block);
213 ll->first_block = ll->last_block = NULL;
217 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len) {
218 linkedlist_datablock_internal* ldi;
219 const unsigned char* from_copy;
222 return ZIP_INTERNALERROR;
224 if (ll->last_block == NULL)
226 ll->first_block = ll->last_block = allocate_new_datablock();
227 if (ll->first_block == NULL)
228 return ZIP_INTERNALERROR;
231 ldi = ll->last_block;
232 from_copy = (const unsigned char*)buf;
238 unsigned char* to_copy;
240 if (ldi->avail_in_this_block==0)
242 ldi->next_datablock = allocate_new_datablock();
243 if (ldi->next_datablock == NULL)
244 return ZIP_INTERNALERROR;
245 ldi = ldi->next_datablock ;
246 ll->last_block = ldi;
249 if (ldi->avail_in_this_block < len)
250 copy_this = (uInt)ldi->avail_in_this_block;
252 copy_this = (uInt)len;
254 to_copy = &(ldi->data[ldi->filled_in_this_block]);
256 for (i=0;i<copy_this;i++)
257 *(to_copy+i)=*(from_copy+i);
259 ldi->filled_in_this_block += copy_this;
260 ldi->avail_in_this_block -= copy_this;
261 from_copy += copy_this ;
269 /****************************************************************************/
271 #ifndef NO_ADDFILEINEXISTINGZIP
272 /* ===========================================================================
273 Inputs a long in LSB order to the given file
274 nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
277 local int zip64local_putValue(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte) {
278 unsigned char buf[8];
280 for (n = 0; n < nbByte; n++)
282 buf[n] = (unsigned char)(x & 0xff);
286 { /* data overflow - hack for ZIP64 (X Roche) */
287 for (n = 0; n < nbByte; n++)
293 if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,(uLong)nbByte)!=(uLong)nbByte)
299 local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte) {
300 unsigned char* buf=(unsigned char*)dest;
302 for (n = 0; n < nbByte; n++) {
303 buf[n] = (unsigned char)(x & 0xff);
308 { /* data overflow - hack for ZIP64 */
309 for (n = 0; n < nbByte; n++)
316 /****************************************************************************/
319 local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) {
320 uLong year = (uLong)ptm->tm_year;
326 (uLong) (((uLong)(ptm->tm_mday) + (32 * (uLong)(ptm->tm_mon+1)) + (512 * year)) << 16) |
327 (((uLong)ptm->tm_sec/2) + (32 * (uLong)ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
331 /****************************************************************************/
333 local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int* pi) {
335 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
343 if (ZERROR64(*pzlib_filefunc_def,filestream))
351 /* ===========================================================================
352 Reads a long in LSB order from the given gz_stream. Sets
354 local int zip64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) {
359 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
363 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
373 local int zip64local_getLong(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) {
378 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
382 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
386 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
390 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
401 local int zip64local_getLong64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) {
406 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
410 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
411 x += ((ZPOS64_T)i)<<8;
414 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
415 x += ((ZPOS64_T)i)<<16;
418 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
419 x += ((ZPOS64_T)i)<<24;
422 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
423 x += ((ZPOS64_T)i)<<32;
426 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
427 x += ((ZPOS64_T)i)<<40;
430 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
431 x += ((ZPOS64_T)i)<<48;
434 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
435 x += ((ZPOS64_T)i)<<56;
445 #ifndef BUFREADCOMMENT
446 #define BUFREADCOMMENT (0x400)
449 Locate the Central directory of a zipfile (at the end, just before
452 local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
456 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
457 ZPOS64_T uPosFound=0;
459 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
463 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
465 if (uMaxBack>uSizeFile)
466 uMaxBack = uSizeFile;
468 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
473 while (uBackRead<uMaxBack)
478 if (uBackRead+BUFREADCOMMENT>uMaxBack)
479 uBackRead = uMaxBack;
481 uBackRead+=BUFREADCOMMENT;
482 uReadPos = uSizeFile-uBackRead ;
484 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
485 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
486 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
489 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
492 for (i=(int)uReadSize-3; (i--)>0;)
493 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
494 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
496 uPosFound = uReadPos+(unsigned)i;
508 Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before
511 local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
515 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
516 ZPOS64_T uPosFound=0;
518 ZPOS64_T relativeOffset;
520 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
523 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
525 if (uMaxBack>uSizeFile)
526 uMaxBack = uSizeFile;
528 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
533 while (uBackRead<uMaxBack)
538 if (uBackRead+BUFREADCOMMENT>uMaxBack)
539 uBackRead = uMaxBack;
541 uBackRead+=BUFREADCOMMENT;
542 uReadPos = uSizeFile-uBackRead ;
544 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
545 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
546 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
549 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
552 for (i=(int)uReadSize-3; (i--)>0;)
554 // Signature "0x07064b50" Zip64 end of central directory locater
555 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
557 uPosFound = uReadPos+(unsigned)i;
570 /* Zip64 end of central directory locator */
571 if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
574 /* the signature, already checked */
575 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
578 /* number of the disk with the start of the zip64 end of central directory */
579 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
584 /* relative offset of the zip64 end of central directory record */
585 if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK)
588 /* total number of disks */
589 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
594 /* Goto Zip64 end of central directory record */
595 if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
599 if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
602 if (uL != 0x06064b50) // signature of 'Zip64 end of central directory'
605 return relativeOffset;
608 local int LoadCentralDirectoryRecord(zip64_internal* pziinit) {
610 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
612 ZPOS64_T size_central_dir; /* size of the central directory */
613 ZPOS64_T offset_central_dir; /* offset of start of central directory */
614 ZPOS64_T central_pos;
617 uLong number_disk; /* number of the current disk, used for
618 spanning ZIP, unsupported, always 0*/
619 uLong number_disk_with_CD; /* number of the disk with central dir, used
620 for spanning ZIP, unsupported, always 0*/
621 ZPOS64_T number_entry;
622 ZPOS64_T number_entry_CD; /* total number of entries in
624 (same than number_entry on nospan) */
629 int hasZIP64Record = 0;
631 // check first if we find a ZIP64 record
632 central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream);
637 else if(central_pos == 0)
639 central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream);
642 /* disable to allow appending to empty ZIP archive
649 ZPOS64_T sizeEndOfCentralDirectory;
650 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
653 /* the signature, already checked */
654 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
657 /* size of zip64 end of central directory record */
658 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK)
661 /* version made by */
662 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK)
665 /* version needed to extract */
666 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK)
669 /* number of this disk */
670 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
673 /* number of the disk with the start of the central directory */
674 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
677 /* total number of entries in the central directory on this disk */
678 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK)
681 /* total number of entries in the central directory */
682 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK)
685 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
688 /* size of the central directory */
689 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK)
692 /* offset of start of central directory with respect to the
693 starting disk number */
694 if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK)
698 // read the comment from the standard central header.
703 // Read End of central Directory info
704 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
707 /* the signature, already checked */
708 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
711 /* number of this disk */
712 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
715 /* number of the disk with the start of the central directory */
716 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
719 /* total number of entries in the central dir on this disk */
721 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
726 /* total number of entries in the central dir */
728 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
731 number_entry_CD = uL;
733 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
736 /* size of the central directory */
737 size_central_dir = 0;
738 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
741 size_central_dir = uL;
743 /* offset of start of central directory with respect to the starting disk number */
744 offset_central_dir = 0;
745 if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
748 offset_central_dir = uL;
751 /* zipfile global comment length */
752 if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK)
756 if ((central_pos<offset_central_dir+size_central_dir) &&
762 ZCLOSE64(pziinit->z_filefunc, pziinit->filestream);
768 pziinit->globalcomment = (char*)ALLOC(size_comment+1);
769 if (pziinit->globalcomment)
771 size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment);
772 pziinit->globalcomment[size_comment]=0;
776 byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
777 pziinit->add_position_when_writing_offset = byte_before_the_zipfile;
780 ZPOS64_T size_central_dir_to_read = size_central_dir;
781 size_t buf_size = SIZEDATA_INDATABLOCK;
782 void* buf_read = (void*)ALLOC(buf_size);
783 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
786 while ((size_central_dir_to_read>0) && (err==ZIP_OK))
788 ZPOS64_T read_this = SIZEDATA_INDATABLOCK;
789 if (read_this > size_central_dir_to_read)
790 read_this = size_central_dir_to_read;
792 if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this)
796 err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
798 size_central_dir_to_read-=read_this;
802 pziinit->begin_pos = byte_before_the_zipfile;
803 pziinit->number_entry = number_entry_CD;
805 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
812 #endif /* !NO_ADDFILEINEXISTINGZIP*/
815 /************************************************************/
816 extern zipFile ZEXPORT zipOpen3(const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def) {
817 zip64_internal ziinit;
821 ziinit.z_filefunc.zseek32_file = NULL;
822 ziinit.z_filefunc.ztell32_file = NULL;
823 if (pzlib_filefunc64_32_def==NULL)
824 fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64);
826 ziinit.z_filefunc = *pzlib_filefunc64_32_def;
828 ziinit.filestream = ZOPEN64(ziinit.z_filefunc,
830 (append == APPEND_STATUS_CREATE) ?
831 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
832 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
834 if (ziinit.filestream == NULL)
837 if (append == APPEND_STATUS_CREATEAFTER)
838 ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
840 ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream);
841 ziinit.in_opened_file_inzip = 0;
842 ziinit.ci.stream_initialised = 0;
843 ziinit.number_entry = 0;
844 ziinit.add_position_when_writing_offset = 0;
845 init_linkedlist(&(ziinit.central_dir));
849 zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
852 ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
856 /* now we add file in a zipfile */
857 # ifndef NO_ADDFILEINEXISTINGZIP
858 ziinit.globalcomment = NULL;
859 if (append == APPEND_STATUS_ADDINZIP)
861 // Read and Cache Central Directory Records
862 err = LoadCentralDirectoryRecord(&ziinit);
867 *globalcomment = ziinit.globalcomment;
869 # endif /* !NO_ADDFILEINEXISTINGZIP*/
873 # ifndef NO_ADDFILEINEXISTINGZIP
874 free(ziinit.globalcomment);
875 # endif /* !NO_ADDFILEINEXISTINGZIP*/
886 extern zipFile ZEXPORT zipOpen2(const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def) {
887 if (pzlib_filefunc32_def != NULL)
889 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
890 fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
891 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
894 return zipOpen3(pathname, append, globalcomment, NULL);
897 extern zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def) {
898 if (pzlib_filefunc_def != NULL)
900 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
901 zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
902 zlib_filefunc64_32_def_fill.ztell32_file = NULL;
903 zlib_filefunc64_32_def_fill.zseek32_file = NULL;
904 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
907 return zipOpen3(pathname, append, globalcomment, NULL);
912 extern zipFile ZEXPORT zipOpen(const char* pathname, int append) {
913 return zipOpen3((const void*)pathname,append,NULL,NULL);
916 extern zipFile ZEXPORT zipOpen64(const void* pathname, int append) {
917 return zipOpen3(pathname,append,NULL,NULL);
920 local int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local) {
921 /* write the local header */
923 uInt size_filename = (uInt)strlen(filename);
924 uInt size_extrafield = size_extrafield_local;
926 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4);
931 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */
933 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
937 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
940 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
943 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
945 // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later
947 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
951 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */
953 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
958 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */
960 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
964 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
968 size_extrafield += 20;
972 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2);
974 if ((err==ZIP_OK) && (size_filename > 0))
976 if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
980 if ((err==ZIP_OK) && (size_extrafield_local > 0))
982 if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local)
987 if ((err==ZIP_OK) && (zi->ci.zip64))
989 // write the Zip64 extended info
992 ZPOS64_T CompressedSize = 0;
993 ZPOS64_T UncompressedSize = 0;
995 // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file)
996 zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
998 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)HeaderID,2);
999 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)DataSize,2);
1001 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8);
1002 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8);
1010 When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped
1011 before calling this function it can be done with zipRemoveExtraInfoBlock
1013 It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize
1014 unnecessary allocations.
1016 extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1017 const void* extrafield_local, uInt size_extrafield_local,
1018 const void* extrafield_global, uInt size_extrafield_global,
1019 const char* comment, int method, int level, int raw,
1020 int windowBits,int memLevel, int strategy,
1021 const char* password, uLong crcForCrypting,
1022 uLong versionMadeBy, uLong flagBase, int zip64) {
1031 if (password != NULL)
1032 return ZIP_PARAMERROR;
1036 return ZIP_PARAMERROR;
1039 if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED))
1040 return ZIP_PARAMERROR;
1042 if ((method!=0) && (method!=Z_DEFLATED))
1043 return ZIP_PARAMERROR;
1046 // The filename and comment length must fit in 16 bits.
1047 if ((filename!=NULL) && (strlen(filename)>0xffff))
1048 return ZIP_PARAMERROR;
1049 if ((comment!=NULL) && (strlen(comment)>0xffff))
1050 return ZIP_PARAMERROR;
1051 // The extra field length must fit in 16 bits. If the member also requires
1052 // a Zip64 extra block, that will also need to fit within that 16-bit
1053 // length, but that will be checked for later.
1054 if ((size_extrafield_local>0xffff) || (size_extrafield_global>0xffff))
1055 return ZIP_PARAMERROR;
1057 zi = (zip64_internal*)file;
1059 if (zi->in_opened_file_inzip == 1)
1061 err = zipCloseFileInZip (file);
1072 size_comment = (uInt)strlen(comment);
1074 size_filename = (uInt)strlen(filename);
1080 if (zipfi->dosDate != 0)
1081 zi->ci.dosDate = zipfi->dosDate;
1083 zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
1086 zi->ci.flag = flagBase;
1087 if ((level==8) || (level==9))
1093 if (password != NULL)
1097 zi->ci.method = method;
1099 zi->ci.stream_initialised = 0;
1100 zi->ci.pos_in_buffered_data = 0;
1102 zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
1104 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment;
1105 zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data
1107 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree);
1109 zi->ci.size_centralExtra = size_extrafield_global;
1110 zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
1112 zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
1113 zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
1114 zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
1115 zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
1116 zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
1117 zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
1118 zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
1119 zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
1120 zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
1121 zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
1122 zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
1123 zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
1126 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
1128 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
1131 zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
1133 zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
1135 if(zi->ci.pos_local_header >= 0xffffffff)
1136 zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
1138 zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writing_offset,4);
1140 for (i=0;i<size_filename;i++)
1141 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
1143 for (i=0;i<size_extrafield_global;i++)
1144 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
1145 *(((const char*)extrafield_global)+i);
1147 for (i=0;i<size_comment;i++)
1148 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
1149 size_extrafield_global+i) = *(comment+i);
1150 if (zi->ci.central_header == NULL)
1151 return ZIP_INTERNALERROR;
1153 zi->ci.zip64 = zip64;
1154 zi->ci.totalCompressedData = 0;
1155 zi->ci.totalUncompressedData = 0;
1156 zi->ci.pos_zip64extrainfo = 0;
1158 err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
1161 zi->ci.bstream.avail_in = (uInt)0;
1162 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1163 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1164 zi->ci.bstream.total_in_hi32 = 0;
1165 zi->ci.bstream.total_in_lo32 = 0;
1166 zi->ci.bstream.total_out_hi32 = 0;
1167 zi->ci.bstream.total_out_lo32 = 0;
1170 zi->ci.stream.avail_in = (uInt)0;
1171 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1172 zi->ci.stream.next_out = zi->ci.buffered_data;
1173 zi->ci.stream.total_in = 0;
1174 zi->ci.stream.total_out = 0;
1175 zi->ci.stream.data_type = Z_BINARY;
1178 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1180 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1183 if(zi->ci.method == Z_DEFLATED)
1185 zi->ci.stream.zalloc = (alloc_func)0;
1186 zi->ci.stream.zfree = (free_func)0;
1187 zi->ci.stream.opaque = (voidpf)0;
1190 windowBits = -windowBits;
1192 err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
1195 zi->ci.stream_initialised = Z_DEFLATED;
1197 else if(zi->ci.method == Z_BZIP2ED)
1200 // Init BZip stuff here
1201 zi->ci.bstream.bzalloc = 0;
1202 zi->ci.bstream.bzfree = 0;
1203 zi->ci.bstream.opaque = (voidpf)0;
1205 err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35);
1207 zi->ci.stream_initialised = Z_BZIP2ED;
1214 zi->ci.crypt_header_size = 0;
1215 if ((err==Z_OK) && (password != NULL))
1217 unsigned char bufHead[RAND_HEAD_LEN];
1218 unsigned int sizeHead;
1220 zi->ci.pcrc_32_tab = get_crc_table();
1221 /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
1223 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
1224 zi->ci.crypt_header_size = sizeHead;
1226 if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
1232 zi->in_opened_file_inzip = 1;
1236 extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1237 const void* extrafield_local, uInt size_extrafield_local,
1238 const void* extrafield_global, uInt size_extrafield_global,
1239 const char* comment, int method, int level, int raw,
1240 int windowBits,int memLevel, int strategy,
1241 const char* password, uLong crcForCrypting,
1242 uLong versionMadeBy, uLong flagBase) {
1243 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1244 extrafield_local, size_extrafield_local,
1245 extrafield_global, size_extrafield_global,
1246 comment, method, level, raw,
1247 windowBits, memLevel, strategy,
1248 password, crcForCrypting, versionMadeBy, flagBase, 0);
1251 extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1252 const void* extrafield_local, uInt size_extrafield_local,
1253 const void* extrafield_global, uInt size_extrafield_global,
1254 const char* comment, int method, int level, int raw,
1255 int windowBits,int memLevel, int strategy,
1256 const char* password, uLong crcForCrypting) {
1257 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1258 extrafield_local, size_extrafield_local,
1259 extrafield_global, size_extrafield_global,
1260 comment, method, level, raw,
1261 windowBits, memLevel, strategy,
1262 password, crcForCrypting, VERSIONMADEBY, 0, 0);
1265 extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1266 const void* extrafield_local, uInt size_extrafield_local,
1267 const void* extrafield_global, uInt size_extrafield_global,
1268 const char* comment, int method, int level, int raw,
1269 int windowBits,int memLevel, int strategy,
1270 const char* password, uLong crcForCrypting, int zip64) {
1271 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1272 extrafield_local, size_extrafield_local,
1273 extrafield_global, size_extrafield_global,
1274 comment, method, level, raw,
1275 windowBits, memLevel, strategy,
1276 password, crcForCrypting, VERSIONMADEBY, 0, zip64);
1279 extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1280 const void* extrafield_local, uInt size_extrafield_local,
1281 const void* extrafield_global, uInt size_extrafield_global,
1282 const char* comment, int method, int level, int raw) {
1283 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1284 extrafield_local, size_extrafield_local,
1285 extrafield_global, size_extrafield_global,
1286 comment, method, level, raw,
1287 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1288 NULL, 0, VERSIONMADEBY, 0, 0);
1291 extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1292 const void* extrafield_local, uInt size_extrafield_local,
1293 const void* extrafield_global, uInt size_extrafield_global,
1294 const char* comment, int method, int level, int raw, int zip64) {
1295 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1296 extrafield_local, size_extrafield_local,
1297 extrafield_global, size_extrafield_global,
1298 comment, method, level, raw,
1299 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1300 NULL, 0, VERSIONMADEBY, 0, zip64);
1303 extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1304 const void* extrafield_local, uInt size_extrafield_local,
1305 const void*extrafield_global, uInt size_extrafield_global,
1306 const char* comment, int method, int level, int zip64) {
1307 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1308 extrafield_local, size_extrafield_local,
1309 extrafield_global, size_extrafield_global,
1310 comment, method, level, 0,
1311 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1312 NULL, 0, VERSIONMADEBY, 0, zip64);
1315 extern int ZEXPORT zipOpenNewFileInZip(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1316 const void* extrafield_local, uInt size_extrafield_local,
1317 const void*extrafield_global, uInt size_extrafield_global,
1318 const char* comment, int method, int level) {
1319 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1320 extrafield_local, size_extrafield_local,
1321 extrafield_global, size_extrafield_global,
1322 comment, method, level, 0,
1323 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1324 NULL, 0, VERSIONMADEBY, 0, 0);
1327 local int zip64FlushWriteBuffer(zip64_internal* zi) {
1330 if (zi->ci.encrypt != 0)
1335 for (i=0;i<zi->ci.pos_in_buffered_data;i++)
1336 zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t);
1340 if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data)
1343 zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data;
1346 if(zi->ci.method == Z_BZIP2ED)
1348 zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32;
1349 zi->ci.bstream.total_in_lo32 = 0;
1350 zi->ci.bstream.total_in_hi32 = 0;
1355 zi->ci.totalUncompressedData += zi->ci.stream.total_in;
1356 zi->ci.stream.total_in = 0;
1360 zi->ci.pos_in_buffered_data = 0;
1365 extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void* buf, unsigned int len) {
1370 return ZIP_PARAMERROR;
1371 zi = (zip64_internal*)file;
1373 if (zi->in_opened_file_inzip == 0)
1374 return ZIP_PARAMERROR;
1376 zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len);
1379 if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw))
1381 zi->ci.bstream.next_in = (void*)buf;
1382 zi->ci.bstream.avail_in = len;
1385 while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0))
1387 if (zi->ci.bstream.avail_out == 0)
1389 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1391 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1392 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1396 if(err != BZ_RUN_OK)
1399 if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1401 uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32;
1402 // uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32;
1403 err=BZ2_bzCompress(&zi->ci.bstream, BZ_RUN);
1405 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ;
1409 if(err == BZ_RUN_OK)
1415 zi->ci.stream.next_in = (Bytef*)(uintptr_t)buf;
1416 zi->ci.stream.avail_in = len;
1418 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
1420 if (zi->ci.stream.avail_out == 0)
1422 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1424 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1425 zi->ci.stream.next_out = zi->ci.buffered_data;
1432 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1434 uLong uTotalOutBefore = zi->ci.stream.total_out;
1435 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
1437 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1442 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
1443 copy_this = zi->ci.stream.avail_in;
1445 copy_this = zi->ci.stream.avail_out;
1447 for (i = 0; i < copy_this; i++)
1448 *(((char*)zi->ci.stream.next_out)+i) =
1449 *(((const char*)zi->ci.stream.next_in)+i);
1451 zi->ci.stream.avail_in -= copy_this;
1452 zi->ci.stream.avail_out-= copy_this;
1453 zi->ci.stream.next_in+= copy_this;
1454 zi->ci.stream.next_out+= copy_this;
1455 zi->ci.stream.total_in+= copy_this;
1456 zi->ci.stream.total_out+= copy_this;
1457 zi->ci.pos_in_buffered_data += copy_this;
1466 extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uLong uncompressed_size, uLong crc32) {
1467 return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32);
1470 extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_size, uLong crc32) {
1472 ZPOS64_T compressed_size;
1473 uLong invalidValue = 0xffffffff;
1474 unsigned datasize = 0;
1478 return ZIP_PARAMERROR;
1479 zi = (zip64_internal*)file;
1481 if (zi->in_opened_file_inzip == 0)
1482 return ZIP_PARAMERROR;
1483 zi->ci.stream.avail_in = 0;
1485 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1489 uLong uTotalOutBefore;
1490 if (zi->ci.stream.avail_out == 0)
1492 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1494 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1495 zi->ci.stream.next_out = zi->ci.buffered_data;
1497 uTotalOutBefore = zi->ci.stream.total_out;
1498 err=deflate(&zi->ci.stream, Z_FINISH);
1499 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1502 else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1506 while (err==BZ_FINISH_OK)
1508 uLong uTotalOutBefore;
1509 if (zi->ci.bstream.avail_out == 0)
1511 if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1513 zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1514 zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1516 uTotalOutBefore = zi->ci.bstream.total_out_lo32;
1517 err=BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH);
1518 if(err == BZ_STREAM_END)
1521 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore);
1524 if(err == BZ_FINISH_OK)
1529 if (err==Z_STREAM_END)
1530 err=ZIP_OK; /* this is normal */
1532 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1534 if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
1538 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1540 int tmp_err = deflateEnd(&zi->ci.stream);
1543 zi->ci.stream_initialised = 0;
1546 else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1548 int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
1551 zi->ci.stream_initialised = 0;
1557 crc32 = (uLong)zi->ci.crc32;
1558 uncompressed_size = zi->ci.totalUncompressedData;
1560 compressed_size = zi->ci.totalCompressedData;
1563 compressed_size += zi->ci.crypt_header_size;
1566 // update Current Item crc and sizes,
1567 if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff)
1570 zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2);
1572 zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2);
1576 zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1579 if(compressed_size >= 0xffffffff)
1580 zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/
1582 zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/
1584 /// set internal file attributes field
1585 if (zi->ci.stream.data_type == Z_ASCII)
1586 zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1588 if(uncompressed_size >= 0xffffffff)
1589 zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/
1591 zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/
1593 // Add ZIP64 extra info field for uncompressed size
1594 if(uncompressed_size >= 0xffffffff)
1597 // Add ZIP64 extra info field for compressed size
1598 if(compressed_size >= 0xffffffff)
1601 // Add ZIP64 extra info field for relative offset to local file header of current file
1602 if(zi->ci.pos_local_header >= 0xffffffff)
1609 if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree)
1611 // we cannot write more data to the buffer that we have room for.
1612 return ZIP_BADZIPFILE;
1615 p = zi->ci.central_header + zi->ci.size_centralheader;
1617 // Add Extra Information Header for 'ZIP64 information'
1618 zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID
1620 zip64local_putValue_inmemory(p, datasize, 2); // DataSize
1623 if(uncompressed_size >= 0xffffffff)
1625 zip64local_putValue_inmemory(p, uncompressed_size, 8);
1629 if(compressed_size >= 0xffffffff)
1631 zip64local_putValue_inmemory(p, compressed_size, 8);
1635 if(zi->ci.pos_local_header >= 0xffffffff)
1637 zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
1641 // Update how much extra free space we got in the memory buffer
1642 // and increase the centralheader size so the new ZIP64 fields are included
1643 // ( 4 below is the size of HeaderID and DataSize field )
1644 zi->ci.size_centralExtraFree -= datasize + 4;
1645 zi->ci.size_centralheader += datasize + 4;
1647 // Update the extra info size field
1648 zi->ci.size_centralExtra += datasize + 4;
1649 zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
1653 err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);
1655 free(zi->ci.central_header);
1659 // Update the LocalFileHeader with the new values.
1661 ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
1663 if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1667 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1669 if(uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff )
1671 if(zi->ci.pos_zip64extrainfo > 0)
1673 // Update the size in the ZIP64 extended field.
1674 if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
1677 if (err==ZIP_OK) /* compressed size, unknown */
1678 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);
1680 if (err==ZIP_OK) /* uncompressed size, unknown */
1681 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8);
1684 err = ZIP_BADZIPFILE; // Caller passed zip64 = 0, so no room for zip64 info -> fatal
1688 if (err==ZIP_OK) /* compressed size, unknown */
1689 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1691 if (err==ZIP_OK) /* uncompressed size, unknown */
1692 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1695 if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1699 zi->number_entry ++;
1700 zi->in_opened_file_inzip = 0;
1705 extern int ZEXPORT zipCloseFileInZip(zipFile file) {
1706 return zipCloseFileInZipRaw (file,0,0);
1709 local int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip) {
1711 ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writing_offset;
1713 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
1716 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1717 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1720 if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */
1721 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8);
1723 /*total disks*/ /* Do not support spawning of disk so always say 1 here*/
1724 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1725 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4);
1730 local int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) {
1733 uLong Zip64DataSize = 44;
1735 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
1737 if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */
1738 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ?
1740 if (err==ZIP_OK) /* version made by */
1741 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
1743 if (err==ZIP_OK) /* version needed */
1744 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
1746 if (err==ZIP_OK) /* number of this disk */
1747 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1749 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1750 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1752 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1753 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
1755 if (err==ZIP_OK) /* total number of entries in the central dir */
1756 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
1758 if (err==ZIP_OK) /* size of the central directory */
1759 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8);
1761 if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
1763 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
1764 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8);
1769 local int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) {
1773 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1775 if (err==ZIP_OK) /* number of this disk */
1776 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1778 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1779 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1781 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1784 if(zi->number_entry >= 0xFFFF)
1785 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
1787 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1791 if (err==ZIP_OK) /* total number of entries in the central dir */
1793 if(zi->number_entry >= 0xFFFF)
1794 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
1796 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1799 if (err==ZIP_OK) /* size of the central directory */
1800 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1802 if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
1804 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
1805 if(pos >= 0xffffffff)
1807 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4);
1810 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writing_offset),4);
1816 local int Write_GlobalComment(zip64_internal* zi, const char* global_comment) {
1818 uInt size_global_comment = 0;
1820 if(global_comment != NULL)
1821 size_global_comment = (uInt)strlen(global_comment);
1823 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1825 if (err == ZIP_OK && size_global_comment > 0)
1827 if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment)
1833 extern int ZEXPORT zipClose(zipFile file, const char* global_comment) {
1836 uLong size_centraldir = 0;
1837 ZPOS64_T centraldir_pos_inzip;
1841 return ZIP_PARAMERROR;
1843 zi = (zip64_internal*)file;
1845 if (zi->in_opened_file_inzip == 1)
1847 err = zipCloseFileInZip (file);
1850 #ifndef NO_ADDFILEINEXISTINGZIP
1851 if (global_comment==NULL)
1852 global_comment = zi->globalcomment;
1855 centraldir_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
1859 linkedlist_datablock_internal* ldi = zi->central_dir.first_block;
1862 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1864 if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block)
1868 size_centraldir += ldi->filled_in_this_block;
1869 ldi = ldi->next_datablock;
1872 free_linkedlist(&(zi->central_dir));
1874 pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
1875 if(pos >= 0xffffffff || zi->number_entry >= 0xFFFF)
1877 ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream);
1878 Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
1880 Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos);
1884 err = Write_EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
1887 err = Write_GlobalComment(zi, global_comment);
1889 if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0)
1893 #ifndef NO_ADDFILEINEXISTINGZIP
1894 free(zi->globalcomment);
1901 extern int ZEXPORT zipRemoveExtraInfoBlock(char* pData, int* dataLen, short sHeader) {
1909 int retVal = ZIP_OK;
1911 if(pData == NULL || dataLen == NULL || *dataLen < 4)
1912 return ZIP_PARAMERROR;
1914 pNewHeader = (char*)ALLOC((unsigned)*dataLen);
1917 while(p < (pData + *dataLen))
1919 header = *(short*)p;
1920 dataSize = *(((short*)p)+1);
1922 if( header == sHeader ) // Header found.
1924 p += dataSize + 4; // skip it. do not copy to temp buffer
1928 // Extra Info block should not be removed, So copy it to the temp buffer.
1929 memcpy(pTmp, p, dataSize + 4);
1931 size += dataSize + 4;
1938 // clean old extra info block.
1939 memset(pData,0, *dataLen);
1941 // copy the new extra info block over the old
1943 memcpy(pData, pNewHeader, size);
1945 // set the new extra info size