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