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