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