50 # define VERSIONMADEBY (0x0)
54 #define Z_BUFSIZE (64*1024) //(16384)
57 #ifndef Z_MAXFILENAMEINZIP
58 #define Z_MAXFILENAMEINZIP (256)
62 # define ALLOC(size) (malloc(size))
65 # define TRYFREE(p) {if (p) free(p);}
77 #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
92 #if MAX_MEM_LEVEL >= 8
93 # define DEF_MEM_LEVEL 8
95 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
98 const char zip_copyright[] =
" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
101 #define SIZEDATA_INDATABLOCK (4096-(4*4))
103 #define LOCALHEADERMAGIC (0x04034b50)
104 #define CENTRALHEADERMAGIC (0x02014b50)
105 #define ENDHEADERMAGIC (0x06054b50)
106 #define ZIP64ENDHEADERMAGIC (0x6064b50)
107 #define ZIP64ENDLOCHEADERMAGIC (0x7064b50)
109 #define FLAG_LOCALHEADER_OFFSET (0x06)
110 #define CRC_LOCALHEADER_OFFSET (0x0e)
112 #define SIZECENTRALHEADER (0x2e)
177 #ifndef NO_ADDFILEINEXISTINGZIP
185 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
215 ll->first_block = ll->last_block = NULL;
221 ll->first_block = ll->last_block = NULL;
228 const unsigned char* from_copy;
233 if (ll->last_block == NULL)
236 if (ll->first_block == NULL)
240 ldi = ll->last_block;
241 from_copy = (
unsigned char*)
buf;
247 unsigned char* to_copy;
255 ll->last_block = ldi;
265 for (
i=0;
i<copy_this;
i++)
266 *(to_copy+
i)=*(from_copy+
i);
270 from_copy += copy_this ;
280 #ifndef NO_ADDFILEINEXISTINGZIP
289 unsigned char buf[8];
291 for (
n = 0;
n < nbByte;
n++)
293 buf[
n] = (
unsigned char)(
x & 0xff);
298 for (
n = 0;
n < nbByte;
n++)
313 unsigned char*
buf=(
unsigned char*)
dest;
315 for (
n = 0;
n < nbByte;
n++) {
316 buf[
n] = (
unsigned char)(
x & 0xff);
322 for (
n = 0;
n < nbByte;
n++)
360 if (
ZERROR64(*pzlib_filefunc_def,filestream))
471 #ifndef BUFREADCOMMENT
472 #define BUFREADCOMMENT (0x400)
492 uSizeFile =
ZTELL64(*pzlib_filefunc_def,filestream);
494 if (uMaxBack>uSizeFile)
495 uMaxBack = uSizeFile;
502 while (uBackRead<uMaxBack)
508 uBackRead = uMaxBack;
511 uReadPos = uSizeFile-uBackRead ;
518 if (
ZREAD64(*pzlib_filefunc_def,filestream,
buf,uReadSize)!=uReadSize)
521 for (
i=(
int)uReadSize-3; (
i--)>0;)
522 if (((*(
buf+
i))==0x50) && ((*(
buf+
i+1))==0x4b) &&
523 ((*(
buf+
i+2))==0x05) && ((*(
buf+
i+3))==0x06))
525 uPosFound = uReadPos+
i;
555 uSizeFile =
ZTELL64(*pzlib_filefunc_def,filestream);
557 if (uMaxBack>uSizeFile)
558 uMaxBack = uSizeFile;
565 while (uBackRead<uMaxBack)
571 uBackRead = uMaxBack;
574 uReadPos = uSizeFile-uBackRead ;
581 if (
ZREAD64(*pzlib_filefunc_def,filestream,
buf,uReadSize)!=uReadSize)
584 for (
i=(
int)uReadSize-3; (
i--)>0;)
587 if (((*(
buf+
i))==0x50) && ((*(
buf+
i+1))==0x4b) && ((*(
buf+
i+2))==0x06) && ((*(
buf+
i+3))==0x07))
589 uPosFound = uReadPos+
i;
634 if (uL != 0x06064b50)
637 return relativeOffset;
652 uLong number_disk_with_CD;
662 int hasZIP64Record = 0;
670 else if(central_pos == 0)
718 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
764 number_entry_CD = uL;
766 if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
770 size_central_dir = 0;
774 size_central_dir = uL;
777 offset_central_dir = 0;
781 offset_central_dir = uL;
789 if ((central_pos<offset_central_dir+size_central_dir) &&
809 byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
813 ZPOS64_T size_central_dir_to_read = size_central_dir;
815 void* buf_read = (
void*)
ALLOC(buf_size);
819 while ((size_central_dir_to_read>0) && (
err==
ZIP_OK))
822 if (read_this > size_central_dir_to_read)
823 read_this = size_central_dir_to_read;
831 size_central_dir_to_read-=read_this;
835 pziinit->
begin_pos = byte_before_the_zipfile;
857 if (pzlib_filefunc64_32_def==NULL)
891 # ifndef NO_ADDFILEINEXISTINGZIP
907 # ifndef NO_ADDFILEINEXISTINGZIP
922 if (pzlib_filefunc32_def != NULL)
926 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
929 return zipOpen3(pathname, append, globalcomment, NULL);
934 if (pzlib_filefunc_def != NULL)
937 zlib_filefunc64_32_def_fill.
zfile_func64 = *pzlib_filefunc_def;
940 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
943 return zipOpen3(pathname, append, globalcomment, NULL);
950 return zipOpen3((
const void*)pathname,append,NULL,NULL);
955 return zipOpen3(pathname,append,NULL,NULL);
963 uInt size_extrafield = size_extrafield_local;
1007 size_extrafield += 20;
1013 if ((
err==
ZIP_OK) && (size_filename > 0))
1019 if ((
err==
ZIP_OK) && (size_extrafield_local > 0))
1030 short DataSize = 16;
1056 const void* extrafield_local,
uInt size_extrafield_local,
1057 const void* extrafield_global,
uInt size_extrafield_global,
1058 const char* comment,
int method,
int level,
int raw,
1059 int windowBits,
int memLevel,
int strategy,
1060 const char* password,
uLong crcForCrypting,
1061 uLong versionMadeBy,
uLong flagBase,
int zip64)
1071 if (password != NULL)
1101 size_comment = (
uInt)strlen(comment);
1122 if (password != NULL)
1169 for (
i=0;
i<size_filename;
i++)
1172 for (
i=0;
i<size_extrafield_global;
i++)
1174 *(((
const char*)extrafield_global)+
i);
1176 for (
i=0;
i<size_comment;
i++)
1178 size_extrafield_global+
i) = *(comment+
i);
1190 zi->
ci.bstream.avail_in = (
uInt)0;
1193 zi->
ci.bstream.total_in_hi32 = 0;
1194 zi->
ci.bstream.total_in_lo32 = 0;
1195 zi->
ci.bstream.total_out_hi32 = 0;
1196 zi->
ci.bstream.total_out_lo32 = 0;
1219 windowBits = -windowBits;
1230 zi->
ci.bstream.bzalloc = 0;
1231 zi->
ci.bstream.bzfree = 0;
1234 err = BZ2_bzCompressInit(&zi->
ci.bstream,
level, 0,35);
1244 if ((
err==
Z_OK) && (password != NULL))
1246 unsigned char bufHead[RAND_HEAD_LEN];
1247 unsigned int sizeHead;
1252 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->
ci.
keys,zi->
ci.
pcrc_32_tab,crcForCrypting);
1266 const void* extrafield_local,
uInt size_extrafield_local,
1267 const void* extrafield_global,
uInt size_extrafield_global,
1268 const char* comment,
int method,
int level,
int raw,
1269 int windowBits,
int memLevel,
int strategy,
1270 const char* password,
uLong crcForCrypting,
1274 extrafield_local, size_extrafield_local,
1275 extrafield_global, size_extrafield_global,
1277 windowBits, memLevel, strategy,
1278 password, crcForCrypting, versionMadeBy, flagBase, 0);
1282 const void* extrafield_local,
uInt size_extrafield_local,
1283 const void* extrafield_global,
uInt size_extrafield_global,
1284 const char* comment,
int method,
int level,
int raw,
1285 int windowBits,
int memLevel,
int strategy,
1286 const char* password,
uLong crcForCrypting)
1289 extrafield_local, size_extrafield_local,
1290 extrafield_global, size_extrafield_global,
1292 windowBits, memLevel, strategy,
1297 const void* extrafield_local,
uInt size_extrafield_local,
1298 const void* extrafield_global,
uInt size_extrafield_global,
1299 const char* comment,
int method,
int level,
int raw,
1300 int windowBits,
int memLevel,
int strategy,
1301 const char* password,
uLong crcForCrypting,
int zip64)
1304 extrafield_local, size_extrafield_local,
1305 extrafield_global, size_extrafield_global,
1307 windowBits, memLevel, strategy,
1312 const void* extrafield_local,
uInt size_extrafield_local,
1313 const void* extrafield_global,
uInt size_extrafield_global,
1314 const char* comment,
int method,
int level,
int raw)
1317 extrafield_local, size_extrafield_local,
1318 extrafield_global, size_extrafield_global,
1325 const void* extrafield_local,
uInt size_extrafield_local,
1326 const void* extrafield_global,
uInt size_extrafield_global,
1327 const char* comment,
int method,
int level,
int raw,
int zip64)
1330 extrafield_local, size_extrafield_local,
1331 extrafield_global, size_extrafield_global,
1338 const void* extrafield_local,
uInt size_extrafield_local,
1339 const void*extrafield_global,
uInt size_extrafield_global,
1340 const char* comment,
int method,
int level,
int zip64)
1343 extrafield_local, size_extrafield_local,
1344 extrafield_global, size_extrafield_global,
1351 const void* extrafield_local,
uInt size_extrafield_local,
1352 const void*extrafield_global,
uInt size_extrafield_global,
1356 extrafield_local, size_extrafield_local,
1357 extrafield_global, size_extrafield_global,
1386 zi->
ci.bstream.total_in_lo32 = 0;
1387 zi->
ci.bstream.total_in_hi32 = 0;
1419 zi->
ci.bstream.next_in = (
void*)
buf;
1420 zi->
ci.bstream.avail_in =
len;
1423 while ((
err==BZ_RUN_OK) && (zi->
ci.bstream.avail_in>0))
1425 if (zi->
ci.bstream.avail_out == 0)
1434 if(
err != BZ_RUN_OK)
1439 uLong uTotalOutBefore_lo = zi->
ci.bstream.total_out_lo32;
1441 err=BZ2_bzCompress(&zi->
ci.bstream, BZ_RUN);
1447 if(
err == BZ_RUN_OK)
1490 for (
i = 0;
i < copy_this;
i++)
1518 uLong invalidValue = 0xffffffff;
1534 uLong uTotalOutBefore;
1551 while (
err==BZ_FINISH_OK)
1553 uLong uTotalOutBefore;
1554 if (zi->
ci.bstream.avail_out == 0)
1561 uTotalOutBefore = zi->
ci.bstream.total_out_lo32;
1562 err=BZ2_bzCompress(&zi->
ci.bstream, BZ_FINISH);
1563 if(
err == BZ_STREAM_END)
1569 if(
err == BZ_FINISH_OK)
1593 int tmperr = BZ2_bzCompressEnd(&zi->
ci.bstream);
1612 if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->
ci.
pos_local_header >= 0xffffffff)
1624 if(compressed_size >= 0xffffffff)
1633 if(uncompressed_size >= 0xffffffff)
1639 if(uncompressed_size >= 0xffffffff)
1643 if(compressed_size >= 0xffffffff)
1668 if(uncompressed_size >= 0xffffffff)
1674 if(compressed_size >= 0xffffffff)
1714 if(uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff )
1781 uLong Zip64DataSize = 44;
1853 if(
pos >= 0xffffffff)
1867 uInt size_global_comment = 0;
1869 if(global_comment != NULL)
1870 size_global_comment = (
uInt)strlen(global_comment);
1874 if (
err ==
ZIP_OK && size_global_comment > 0)
1886 uLong size_centraldir = 0;
1900 #ifndef NO_ADDFILEINEXISTINGZIP
1901 if (global_comment==NULL)
1943 #ifndef NO_ADDFILEINEXISTINGZIP
1962 if(pData == NULL || *dataLen < 4)
1965 pNewHeader = (
char*)
ALLOC(*dataLen);
1968 while(
p < (pData + *dataLen))
1971 dataSize = *(((
short*)
p)+1);
1980 memcpy(pTmp,
p, dataSize + 4);
1982 size += dataSize + 4;
1990 memset(pData,0, *dataLen);