File indexing completed on 2025-01-26 04:24:56

0001 /* zip.c -- IO on .zip files using zlib
0002    Version 1.1, February 14h, 2010
0003    part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
0004 
0005          Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
0006 
0007          Modifications for Zip64 support
0008          Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
0009 
0010          For more info read MiniZip_info.txt
0011 
0012          Modifications for QIODevice support and other QuaZIP fixes
0013          Copyright (C) 2005-2014 Sergey A. Tachenov
0014 
0015          Changes
0016    Oct-2009 - Mathias Svensson - Remove old C style function prototypes
0017    Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives
0018    Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions.
0019    Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data
0020                                  It is used when recreting zip archive with RAW when deleting items from a zip.
0021                                  ZIP64 data is automaticly added to items that needs it, and existing ZIP64 data need to be removed.
0022    Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required)
0023    Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
0024 
0025 */
0026 
0027 
0028 #include <stdio.h>
0029 #include <stdlib.h>
0030 #include <string.h>
0031 #include <time.h>
0032 #include "zlib.h"
0033 #if (ZLIB_VERNUM < 0x1270)
0034 typedef uLongf z_crc_t;
0035 #endif
0036 #include "zip.h"
0037 
0038 #ifdef STDC
0039 #  include <stddef.h>
0040 #  include <string.h>
0041 #  include <stdlib.h>
0042 #endif
0043 #ifdef NO_ERRNO_H
0044     extern int errno;
0045 #else
0046 #   include <errno.h>
0047 #endif
0048 
0049 
0050 #ifndef local
0051 #  define local static
0052 #endif
0053 /* compile with -Dlocal if your debugger can't find static symbols */
0054 
0055 #ifndef VERSIONMADEBY
0056 # define VERSIONMADEBY   (0x031e) /* best for standard pkware crypt */
0057 #endif
0058 
0059 #ifndef Z_BUFSIZE
0060 #define Z_BUFSIZE (64*1024) /* (16384) */
0061 #endif
0062 
0063 #ifndef Z_MAXFILENAMEINZIP
0064 #define Z_MAXFILENAMEINZIP (256)
0065 #endif
0066 
0067 #ifndef ALLOC
0068 # define ALLOC(size) (malloc(size))
0069 #endif
0070 #ifndef TRYFREE
0071 # define TRYFREE(p) {if (p) free(p);}
0072 #endif
0073 
0074 /*
0075 #define SIZECENTRALDIRITEM (0x2e)
0076 #define SIZEZIPLOCALHEADER (0x1e)
0077 */
0078 
0079 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
0080 
0081 
0082 /* NOT sure that this work on ALL platform */
0083 #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
0084 
0085 #ifndef SEEK_CUR
0086 #define SEEK_CUR    1
0087 #endif
0088 
0089 #ifndef SEEK_END
0090 #define SEEK_END    2
0091 #endif
0092 
0093 #ifndef SEEK_SET
0094 #define SEEK_SET    0
0095 #endif
0096 
0097 #ifndef DEF_MEM_LEVEL
0098 #if MAX_MEM_LEVEL >= 8
0099 #  define DEF_MEM_LEVEL 8
0100 #else
0101 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
0102 #endif
0103 #endif
0104 const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
0105 
0106 
0107 #define SIZEDATA_INDATABLOCK (4096-(4*4))
0108 
0109 #define LOCALHEADERMAGIC    (0x04034b50)
0110 #define DESCRIPTORHEADERMAGIC    (0x08074b50)
0111 #define CENTRALHEADERMAGIC  (0x02014b50)
0112 #define ENDHEADERMAGIC      (0x06054b50)
0113 #define ZIP64ENDHEADERMAGIC      (0x6064b50)
0114 #define ZIP64ENDLOCHEADERMAGIC   (0x7064b50)
0115 
0116 #define FLAG_LOCALHEADER_OFFSET (0x06)
0117 #define CRC_LOCALHEADER_OFFSET  (0x0e)
0118 
0119 #define SIZECENTRALHEADER (0x2e) /* 46 */
0120 
0121 typedef struct linkedlist_datablock_internal_s
0122 {
0123   struct linkedlist_datablock_internal_s* next_datablock;
0124   uLong  avail_in_this_block;
0125   uLong  filled_in_this_block;
0126   uLong  unused; /* for future use and alignement */
0127   unsigned char data[SIZEDATA_INDATABLOCK];
0128 } linkedlist_datablock_internal;
0129 
0130 typedef struct linkedlist_data_s
0131 {
0132     linkedlist_datablock_internal* first_block;
0133     linkedlist_datablock_internal* last_block;
0134 } linkedlist_data;
0135 
0136 
0137 typedef struct
0138 {
0139     z_stream stream;            /* zLib stream structure for inflate */
0140 #ifdef HAVE_BZIP2
0141     bz_stream bstream;          /* bzLib stream structure for bziped */
0142 #endif
0143 
0144     int  stream_initialised;    /* 1 is stream is initialised */
0145     uInt pos_in_buffered_data;  /* last written byte in buffered_data */
0146 
0147     ZPOS64_T pos_local_header;     /* offset of the local header of the file
0148                                      currenty writing */
0149     char* central_header;       /* central header data for the current file */
0150     uLong size_centralExtra;
0151     uLong size_centralheader;   /* size of the central header for cur file */
0152     uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */
0153     uLong flag;                 /* flag of the file currently writing */
0154 
0155     int  method;                /* compression method of file currenty wr.*/
0156     int  raw;                   /* 1 for directly writing raw data */
0157     Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
0158     uLong dosDate;
0159     uLong crc32;
0160     int  encrypt;
0161     int  zip64;               /* Add ZIP64 extened information in the extra field */
0162     ZPOS64_T pos_zip64extrainfo;
0163     ZPOS64_T totalCompressedData;
0164     ZPOS64_T totalUncompressedData;
0165 #ifndef NOCRYPT
0166     unsigned long keys[3];     /* keys defining the pseudo-random sequence */
0167     const z_crc_t FAR * pcrc_32_tab;
0168     int crypt_header_size;
0169 #endif
0170 } curfile64_info;
0171 
0172 typedef struct
0173 {
0174     zlib_filefunc64_32_def z_filefunc;
0175     voidpf filestream;        /* io structore of the zipfile */
0176     linkedlist_data central_dir;/* datablock with central dir in construction*/
0177     int  in_opened_file_inzip;  /* 1 if a file in the zip is currently writ.*/
0178     curfile64_info ci;            /* info on the file curretly writing */
0179 
0180     ZPOS64_T begin_pos;            /* position of the beginning of the zipfile */
0181     ZPOS64_T add_position_when_writting_offset;
0182     ZPOS64_T number_entry;
0183 
0184 #ifndef NO_ADDFILEINEXISTINGZIP
0185     char *globalcomment;
0186 #endif
0187 
0188     unsigned flags;
0189 
0190 } zip64_internal;
0191 
0192 
0193 #ifndef NOCRYPT
0194 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
0195 #include "crypt.h"
0196 #endif
0197 
0198 local linkedlist_datablock_internal* allocate_new_datablock()
0199 {
0200     linkedlist_datablock_internal* ldi;
0201     ldi = (linkedlist_datablock_internal*)
0202                  ALLOC(sizeof(linkedlist_datablock_internal));
0203     if (ldi!=NULL)
0204     {
0205         ldi->next_datablock = NULL ;
0206         ldi->filled_in_this_block = 0 ;
0207         ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
0208     }
0209     return ldi;
0210 }
0211 
0212 local void free_datablock(linkedlist_datablock_internal* ldi)
0213 {
0214     while (ldi!=NULL)
0215     {
0216         linkedlist_datablock_internal* ldinext = ldi->next_datablock;
0217         TRYFREE(ldi);
0218         ldi = ldinext;
0219     }
0220 }
0221 
0222 local void init_linkedlist(linkedlist_data* ll)
0223 {
0224     ll->first_block = ll->last_block = NULL;
0225 }
0226 
0227 local void free_linkedlist(linkedlist_data* ll)
0228 {
0229     free_datablock(ll->first_block);
0230     ll->first_block = ll->last_block = NULL;
0231 }
0232 
0233 
0234 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len)
0235 {
0236     linkedlist_datablock_internal* ldi;
0237     const unsigned char* from_copy;
0238 
0239     if (ll==NULL)
0240         return ZIP_INTERNALERROR;
0241 
0242     if (ll->last_block == NULL)
0243     {
0244         ll->first_block = ll->last_block = allocate_new_datablock();
0245         if (ll->first_block == NULL)
0246             return ZIP_INTERNALERROR;
0247     }
0248 
0249     ldi = ll->last_block;
0250     from_copy = (unsigned char*)buf;
0251 
0252     while (len>0)
0253     {
0254         uInt copy_this;
0255         uInt i;
0256         unsigned char* to_copy;
0257 
0258         if (ldi->avail_in_this_block==0)
0259         {
0260             ldi->next_datablock = allocate_new_datablock();
0261             if (ldi->next_datablock == NULL)
0262                 return ZIP_INTERNALERROR;
0263             ldi = ldi->next_datablock ;
0264             ll->last_block = ldi;
0265         }
0266 
0267         if (ldi->avail_in_this_block < len)
0268             copy_this = (uInt)ldi->avail_in_this_block;
0269         else
0270             copy_this = (uInt)len;
0271 
0272         to_copy = &(ldi->data[ldi->filled_in_this_block]);
0273 
0274         for (i=0;i<copy_this;i++)
0275             *(to_copy+i)=*(from_copy+i);
0276 
0277         ldi->filled_in_this_block += copy_this;
0278         ldi->avail_in_this_block -= copy_this;
0279         from_copy += copy_this ;
0280         len -= copy_this;
0281     }
0282     return ZIP_OK;
0283 }
0284 
0285 
0286 
0287 /****************************************************************************/
0288 
0289 #ifndef NO_ADDFILEINEXISTINGZIP
0290 /* ===========================================================================
0291    Inputs a long in LSB order to the given file
0292    nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
0293 */
0294 
0295 local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte));
0296 local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)
0297 {
0298     unsigned char buf[8];
0299     int n;
0300     for (n = 0; n < nbByte; n++)
0301     {
0302         buf[n] = (unsigned char)(x & 0xff);
0303         x >>= 8;
0304     }
0305     if (x != 0)
0306       {     /* data overflow - hack for ZIP64 (X Roche) */
0307       for (n = 0; n < nbByte; n++)
0308         {
0309           buf[n] = 0xff;
0310         }
0311       }
0312 
0313     if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
0314         return ZIP_ERRNO;
0315     else
0316         return ZIP_OK;
0317 }
0318 
0319 local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte));
0320 local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte)
0321 {
0322     unsigned char* buf=(unsigned char*)dest;
0323     int n;
0324     for (n = 0; n < nbByte; n++) {
0325         buf[n] = (unsigned char)(x & 0xff);
0326         x >>= 8;
0327     }
0328 
0329     if (x != 0)
0330     {     /* data overflow - hack for ZIP64 */
0331        for (n = 0; n < nbByte; n++)
0332        {
0333           buf[n] = 0xff;
0334        }
0335     }
0336 }
0337 
0338 /****************************************************************************/
0339 
0340 
0341 local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm)
0342 {
0343     uLong year = (uLong)ptm->tm_year;
0344     if (year>=1980)
0345         year-=1980;
0346     else if (year>=80)
0347         year-=80;
0348     return
0349       (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
0350         ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
0351 }
0352 
0353 
0354 /****************************************************************************/
0355 
0356 local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi));
0357 
0358 local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi)
0359 {
0360     unsigned char c;
0361     int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
0362     if (err==1)
0363     {
0364         *pi = (int)c;
0365         return ZIP_OK;
0366     }
0367     else
0368     {
0369         if (ZERROR64(*pzlib_filefunc_def,filestream))
0370             return ZIP_ERRNO;
0371         else
0372             return ZIP_EOF;
0373     }
0374 }
0375 
0376 
0377 /* ===========================================================================
0378    Reads a long in LSB order from the given gz_stream. Sets
0379 */
0380 local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
0381 
0382 local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
0383 {
0384     uLong x ;
0385     int i = 0;
0386     int err;
0387 
0388     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0389     x = (uLong)i;
0390 
0391     if (err==ZIP_OK)
0392         err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0393     x += ((uLong)i)<<8;
0394 
0395     if (err==ZIP_OK)
0396         *pX = x;
0397     else
0398         *pX = 0;
0399     return err;
0400 }
0401 
0402 local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX));
0403 
0404 local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
0405 {
0406     uLong x ;
0407     int i = 0;
0408     int err;
0409 
0410     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0411     x = (uLong)i;
0412 
0413     if (err==ZIP_OK)
0414         err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0415     x += ((uLong)i)<<8;
0416 
0417     if (err==ZIP_OK)
0418         err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0419     x += ((uLong)i)<<16;
0420 
0421     if (err==ZIP_OK)
0422         err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0423     x += ((uLong)i)<<24;
0424 
0425     if (err==ZIP_OK)
0426         *pX = x;
0427     else
0428         *pX = 0;
0429     return err;
0430 }
0431 
0432 local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX));
0433 
0434 
0435 local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
0436 {
0437   ZPOS64_T x;
0438   int i = 0;
0439   int err;
0440 
0441   err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0442   x = (ZPOS64_T)i;
0443 
0444   if (err==ZIP_OK)
0445     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0446   x += ((ZPOS64_T)i)<<8;
0447 
0448   if (err==ZIP_OK)
0449     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0450   x += ((ZPOS64_T)i)<<16;
0451 
0452   if (err==ZIP_OK)
0453     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0454   x += ((ZPOS64_T)i)<<24;
0455 
0456   if (err==ZIP_OK)
0457     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0458   x += ((ZPOS64_T)i)<<32;
0459 
0460   if (err==ZIP_OK)
0461     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0462   x += ((ZPOS64_T)i)<<40;
0463 
0464   if (err==ZIP_OK)
0465     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0466   x += ((ZPOS64_T)i)<<48;
0467 
0468   if (err==ZIP_OK)
0469     err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
0470   x += ((ZPOS64_T)i)<<56;
0471 
0472   if (err==ZIP_OK)
0473     *pX = x;
0474   else
0475     *pX = 0;
0476 
0477   return err;
0478 }
0479 
0480 #ifndef BUFREADCOMMENT
0481 #define BUFREADCOMMENT (0x400)
0482 #endif
0483 /*
0484   Locate the Central directory of a zipfile (at the end, just before
0485     the global comment)
0486 */
0487 local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
0488 
0489 local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
0490 {
0491   unsigned char* buf;
0492   ZPOS64_T uSizeFile;
0493   ZPOS64_T uBackRead;
0494   ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
0495   ZPOS64_T uPosFound=0;
0496 
0497   if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
0498     return 0;
0499 
0500 
0501   uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
0502 
0503   if (uMaxBack>uSizeFile)
0504     uMaxBack = uSizeFile;
0505 
0506   buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
0507   if (buf==NULL)
0508     return 0;
0509 
0510   uBackRead = 4;
0511   while (uBackRead<uMaxBack)
0512   {
0513     uLong uReadSize;
0514     ZPOS64_T uReadPos ;
0515     int i;
0516     if (uBackRead+BUFREADCOMMENT>uMaxBack)
0517       uBackRead = uMaxBack;
0518     else
0519       uBackRead+=BUFREADCOMMENT;
0520     uReadPos = uSizeFile-uBackRead ;
0521 
0522     uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
0523       (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
0524     if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
0525       break;
0526 
0527     if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
0528       break;
0529 
0530     for (i=(int)uReadSize-3; (i--)>0;)
0531       if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
0532         ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
0533       {
0534         uPosFound = uReadPos+i;
0535         break;
0536       }
0537 
0538       if (uPosFound!=0)
0539         break;
0540   }
0541   TRYFREE(buf);
0542   return uPosFound;
0543 }
0544 
0545 /*
0546 Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before
0547 the global comment)
0548 */
0549 local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
0550 
0551 local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
0552 {
0553   unsigned char* buf;
0554   ZPOS64_T uSizeFile;
0555   ZPOS64_T uBackRead;
0556   ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
0557   ZPOS64_T uPosFound=0;
0558   uLong uL;
0559   ZPOS64_T relativeOffset;
0560 
0561   if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
0562     return 0;
0563 
0564   uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
0565 
0566   if (uMaxBack>uSizeFile)
0567     uMaxBack = uSizeFile;
0568 
0569   buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
0570   if (buf==NULL)
0571     return 0;
0572 
0573   uBackRead = 4;
0574   while (uBackRead<uMaxBack)
0575   {
0576     uLong uReadSize;
0577     ZPOS64_T uReadPos;
0578     int i;
0579     if (uBackRead+BUFREADCOMMENT>uMaxBack)
0580       uBackRead = uMaxBack;
0581     else
0582       uBackRead+=BUFREADCOMMENT;
0583     uReadPos = uSizeFile-uBackRead ;
0584 
0585     uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
0586       (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
0587     if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
0588       break;
0589 
0590     if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
0591       break;
0592 
0593     for (i=(int)uReadSize-3; (i--)>0;)
0594     {
0595       /* Signature "0x07064b50" Zip64 end of central directory locater */
0596       if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
0597       {
0598         uPosFound = uReadPos+i;
0599         break;
0600       }
0601     }
0602 
0603       if (uPosFound!=0)
0604         break;
0605   }
0606 
0607   TRYFREE(buf);
0608   if (uPosFound == 0)
0609     return 0;
0610 
0611   /* Zip64 end of central directory locator */
0612   if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
0613     return 0;
0614 
0615   /* the signature, already checked */
0616   if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
0617     return 0;
0618 
0619   /* number of the disk with the start of the zip64 end of  central directory */
0620   if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
0621     return 0;
0622   if (uL != 0)
0623     return 0;
0624 
0625   /* relative offset of the zip64 end of central directory record */
0626   if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK)
0627     return 0;
0628 
0629   /* total number of disks */
0630   if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
0631     return 0;
0632   if (uL != 1)
0633     return 0;
0634 
0635   /* Goto Zip64 end of central directory record */
0636   if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
0637     return 0;
0638 
0639   /* the signature */
0640   if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
0641     return 0;
0642 
0643   if (uL != 0x06064b50) /* signature of 'Zip64 end of central directory' */
0644     return 0;
0645 
0646   return relativeOffset;
0647 }
0648 
0649 int LoadCentralDirectoryRecord(zip64_internal* pziinit)
0650 {
0651   int err=ZIP_OK;
0652   ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
0653 
0654   ZPOS64_T size_central_dir;     /* size of the central directory  */
0655   ZPOS64_T offset_central_dir;   /* offset of start of central directory */
0656   ZPOS64_T central_pos;
0657   uLong uL;
0658 
0659   uLong number_disk;          /* number of the current dist, used for
0660                               spaning ZIP, unsupported, always 0*/
0661   uLong number_disk_with_CD;  /* number the the disk with central dir, used
0662                               for spaning ZIP, unsupported, always 0*/
0663   ZPOS64_T number_entry;
0664   ZPOS64_T number_entry_CD;      /* total number of entries in
0665                                 the central dir
0666                                 (same than number_entry on nospan) */
0667   uLong VersionMadeBy;
0668   uLong VersionNeeded;
0669   uLong size_comment;
0670 
0671   int hasZIP64Record = 0;
0672 
0673   /* check first if we find a ZIP64 record */
0674   central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream);
0675   if(central_pos > 0)
0676   {
0677     hasZIP64Record = 1;
0678   }
0679   else if(central_pos == 0)
0680   {
0681     central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream);
0682   }
0683 
0684 /* disable to allow appending to empty ZIP archive
0685         if (central_pos==0)
0686             err=ZIP_ERRNO;
0687 */
0688 
0689   if(hasZIP64Record)
0690   {
0691     ZPOS64_T sizeEndOfCentralDirectory;
0692     if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
0693       err=ZIP_ERRNO;
0694 
0695     /* the signature, already checked */
0696     if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
0697       err=ZIP_ERRNO;
0698 
0699     /* size of zip64 end of central directory record */
0700     if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK)
0701       err=ZIP_ERRNO;
0702 
0703     /* version made by */
0704     if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK)
0705       err=ZIP_ERRNO;
0706 
0707     /* version needed to extract */
0708     if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK)
0709       err=ZIP_ERRNO;
0710 
0711     /* number of this disk */
0712     if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
0713       err=ZIP_ERRNO;
0714 
0715     /* number of the disk with the start of the central directory */
0716     if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
0717       err=ZIP_ERRNO;
0718 
0719     /* total number of entries in the central directory on this disk */
0720     if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK)
0721       err=ZIP_ERRNO;
0722 
0723     /* total number of entries in the central directory */
0724     if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK)
0725       err=ZIP_ERRNO;
0726 
0727     if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
0728       err=ZIP_BADZIPFILE;
0729 
0730     /* size of the central directory */
0731     if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK)
0732       err=ZIP_ERRNO;
0733 
0734     /* offset of start of central directory with respect to the
0735     starting disk number */
0736     if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK)
0737       err=ZIP_ERRNO;
0738 
0739     /* TODO.. */
0740     /* read the comment from the standard central header. */
0741     size_comment = 0;
0742   }
0743   else
0744   {
0745     /* Read End of central Directory info */
0746     if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
0747       err=ZIP_ERRNO;
0748 
0749     /* the signature, already checked */
0750     if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
0751       err=ZIP_ERRNO;
0752 
0753     /* number of this disk */
0754     if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
0755       err=ZIP_ERRNO;
0756 
0757     /* number of the disk with the start of the central directory */
0758     if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
0759       err=ZIP_ERRNO;
0760 
0761     /* total number of entries in the central dir on this disk */
0762     number_entry = 0;
0763     if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
0764       err=ZIP_ERRNO;
0765     else
0766       number_entry = uL;
0767 
0768     /* total number of entries in the central dir */
0769     number_entry_CD = 0;
0770     if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
0771       err=ZIP_ERRNO;
0772     else
0773       number_entry_CD = uL;
0774 
0775     if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
0776       err=ZIP_BADZIPFILE;
0777 
0778     /* size of the central directory */
0779     size_central_dir = 0;
0780     if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
0781       err=ZIP_ERRNO;
0782     else
0783       size_central_dir = uL;
0784 
0785     /* offset of start of central directory with respect to the starting disk number */
0786     offset_central_dir = 0;
0787     if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
0788       err=ZIP_ERRNO;
0789     else
0790       offset_central_dir = uL;
0791 
0792 
0793     /* zipfile global comment length */
0794     if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK)
0795       err=ZIP_ERRNO;
0796   }
0797 
0798   if ((central_pos<offset_central_dir+size_central_dir) &&
0799     (err==ZIP_OK))
0800     err=ZIP_BADZIPFILE;
0801 
0802   if (err!=ZIP_OK)
0803   {
0804       if ((pziinit->flags & ZIP_AUTO_CLOSE) != 0) {
0805         ZCLOSE64(pziinit->z_filefunc, pziinit->filestream);
0806       } else {
0807         ZFAKECLOSE64(pziinit->z_filefunc, pziinit->filestream);
0808       }
0809     return ZIP_ERRNO;
0810   }
0811 
0812   if (size_comment>0)
0813   {
0814     pziinit->globalcomment = (char*)ALLOC(size_comment+1);
0815     if (pziinit->globalcomment)
0816     {
0817       size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment);
0818       pziinit->globalcomment[size_comment]=0;
0819     }
0820   }
0821 
0822   byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
0823   pziinit->add_position_when_writting_offset = byte_before_the_zipfile;
0824 
0825   {
0826     ZPOS64_T size_central_dir_to_read = size_central_dir;
0827     size_t buf_size = SIZEDATA_INDATABLOCK;
0828     void* buf_read = (void*)ALLOC(buf_size);
0829     if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
0830       err=ZIP_ERRNO;
0831 
0832     while ((size_central_dir_to_read>0) && (err==ZIP_OK))
0833     {
0834       ZPOS64_T read_this = SIZEDATA_INDATABLOCK;
0835       if (read_this > size_central_dir_to_read)
0836         read_this = size_central_dir_to_read;
0837 
0838       if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this)
0839         err=ZIP_ERRNO;
0840 
0841       if (err==ZIP_OK)
0842         err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
0843 
0844       size_central_dir_to_read-=read_this;
0845     }
0846     TRYFREE(buf_read);
0847   }
0848   pziinit->begin_pos = byte_before_the_zipfile;
0849   pziinit->number_entry = number_entry_CD;
0850 
0851   if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
0852     err=ZIP_ERRNO;
0853 
0854   return err;
0855 }
0856 
0857 
0858 #endif /* !NO_ADDFILEINEXISTINGZIP*/
0859 
0860 
0861 /************************************************************/
0862 extern zipFile ZEXPORT zipOpen3 (voidpf file, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def,
0863                                  unsigned flags)
0864 {
0865     zip64_internal ziinit;
0866     zip64_internal* zi;
0867     int err=ZIP_OK;
0868 
0869     ziinit.flags = flags;
0870     ziinit.z_filefunc.zseek32_file = NULL;
0871     ziinit.z_filefunc.ztell32_file = NULL;
0872     if (pzlib_filefunc64_32_def==NULL)
0873         fill_qiodevice64_filefunc(&ziinit.z_filefunc.zfile_func64);
0874     else
0875         ziinit.z_filefunc = *pzlib_filefunc64_32_def;
0876 
0877     ziinit.filestream = ZOPEN64(ziinit.z_filefunc,
0878                   file,
0879                   (append == APPEND_STATUS_CREATE) ?
0880                   (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
0881                     (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
0882 
0883     if (ziinit.filestream == NULL)
0884         return NULL;
0885 
0886     if (append == APPEND_STATUS_CREATEAFTER)
0887         ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
0888 
0889     ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream);
0890     ziinit.in_opened_file_inzip = 0;
0891     ziinit.ci.stream_initialised = 0;
0892     ziinit.number_entry = 0;
0893     ziinit.add_position_when_writting_offset = 0;
0894     init_linkedlist(&(ziinit.central_dir));
0895 
0896 
0897 
0898     zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
0899     if (zi==NULL)
0900     {
0901         if ((ziinit.flags & ZIP_AUTO_CLOSE) != 0) {
0902             ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
0903         } else {
0904             ZFAKECLOSE64(ziinit.z_filefunc,ziinit.filestream);
0905         }
0906         return NULL;
0907     }
0908 
0909     /* now we add file in a zipfile */
0910 #    ifndef NO_ADDFILEINEXISTINGZIP
0911     ziinit.globalcomment = NULL;
0912     if (append == APPEND_STATUS_ADDINZIP)
0913     {
0914       /* Read and Cache Central Directory Records */
0915       err = LoadCentralDirectoryRecord(&ziinit);
0916     }
0917 
0918     if (globalcomment)
0919     {
0920       *globalcomment = ziinit.globalcomment;
0921     }
0922 #    endif /* !NO_ADDFILEINEXISTINGZIP*/
0923 
0924     if (err != ZIP_OK)
0925     {
0926 #    ifndef NO_ADDFILEINEXISTINGZIP
0927         TRYFREE(ziinit.globalcomment);
0928 #    endif /* !NO_ADDFILEINEXISTINGZIP*/
0929         TRYFREE(zi);
0930         return NULL;
0931     }
0932     else
0933     {
0934         *zi = ziinit;
0935         return (zipFile)zi;
0936     }
0937 }
0938 
0939 extern zipFile ZEXPORT zipOpen2 (voidpf file, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def)
0940 {
0941     if (pzlib_filefunc32_def != NULL)
0942     {
0943         zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
0944         fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
0945         return zipOpen3(file, append, globalcomment, &zlib_filefunc64_32_def_fill, ZIP_DEFAULT_FLAGS);
0946     }
0947     else
0948         return zipOpen3(file, append, globalcomment, NULL, ZIP_DEFAULT_FLAGS);
0949 }
0950 
0951 extern zipFile ZEXPORT zipOpen2_64 (voidpf file, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)
0952 {
0953     if (pzlib_filefunc_def != NULL)
0954     {
0955         zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
0956         zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
0957         zlib_filefunc64_32_def_fill.ztell32_file = NULL;
0958         zlib_filefunc64_32_def_fill.zseek32_file = NULL;
0959         return zipOpen3(file, append, globalcomment, &zlib_filefunc64_32_def_fill, ZIP_DEFAULT_FLAGS);
0960     }
0961     else
0962         return zipOpen3(file, append, globalcomment, NULL, ZIP_DEFAULT_FLAGS);
0963 }
0964 
0965 
0966 
0967 extern zipFile ZEXPORT zipOpen (voidpf file, int append)
0968 {
0969     return zipOpen3(file,append,NULL,NULL, ZIP_DEFAULT_FLAGS);
0970 }
0971 
0972 extern zipFile ZEXPORT zipOpen64 (voidpf file, int append)
0973 {
0974     return zipOpen3(file,append,NULL,NULL, ZIP_DEFAULT_FLAGS);
0975 }
0976 
0977 int Write_LocalFileHeader(zip64_internal* zi, const char* filename,
0978                           uInt size_extrafield_local,
0979                           const void* extrafield_local,
0980                           uLong version_to_extract)
0981 {
0982   /* write the local header */
0983   int err;
0984   uInt size_filename = (uInt)strlen(filename);
0985   uInt size_extrafield = size_extrafield_local;
0986 
0987   err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4);
0988 
0989   if (err==ZIP_OK)
0990   {
0991     if(zi->ci.zip64)
0992       err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */
0993     else
0994       err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)version_to_extract,2);
0995   }
0996 
0997   if (err==ZIP_OK)
0998     err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
0999 
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) */
1057       zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
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
1145           zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
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;
1168     zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
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;
1176     zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
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);
1180     zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
1181     zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
1182     zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
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
1194         zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
1195 
1196     if (zipfi==NULL)
1197         zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
1198     else
1199         zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
1200 
1201     if(zi->ci.pos_local_header >= 0xffffffff)
1202       zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
1203     else
1204       zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset,4);
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)
1274             zi->ci.stream_initialised = Z_BZIP2ED;
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 
1403 local int zip64FlushWriteBuffer(zip64_internal* zi)
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 
1417     if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data)
1418       err = ZIP_ERRNO;
1419 
1420     zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data;
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                 {
1613         if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
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       {
1716         zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
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;
1728       zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
1729     }
1730 
1731     if (err==ZIP_OK)
1732         err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);
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 
1743             if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
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. */
1754                     if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
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)
1780                 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)DESCRIPTORHEADERMAGIC,4);
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 
1809 int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip)
1810 {
1811   int err = ZIP_OK;
1812   ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset;
1813 
1814   err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
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 
1837   err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
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     {
1963         linkedlist_datablock_internal* ldi = zi->central_dir.first_block;
1964         while (ldi!=NULL)
1965         {
1966             if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1967             {
1968                 if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block)
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) {
2080         zi->flags |= ZIP_WRITE_DATA_DESCRIPTOR;
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 }