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