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) 114 typedef struct linkedlist_datablock_internal_s
123 typedef struct linkedlist_data_s
159 unsigned long keys[3];
177 #ifndef NO_ADDFILEINEXISTINGZIP 185 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED 228 const unsigned char* from_copy;
241 from_copy = (
unsigned char*)buf;
247 unsigned char* to_copy;
261 copy_this = (
uInt)len;
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++)
304 if (
ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(
uLong)nbByte)
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++)
352 int err = (int)
ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
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);
962 uInt size_filename = (
uInt)strlen(filename);
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);
1103 size_filename = (
uInt)strlen(filename);
1116 if ((level==8) || (level==9))
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,
1276 comment, method, level, raw,
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,
1291 comment, method, level, raw,
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,
1306 comment, method, level, raw,
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,
1319 comment, method, level, raw,
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,
1332 comment, method, level, raw,
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,
1345 comment, method, level, 0,
1351 const void* extrafield_local,
uInt size_extrafield_local,
1352 const void*extrafield_global,
uInt size_extrafield_global,
1353 const char* comment,
int method,
int level)
1356 extrafield_local, size_extrafield_local,
1357 extrafield_global, size_extrafield_global,
1358 comment, method, level, 0,
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))
1970 header = *(
short*)p;
1971 dataSize = *(((
short*)p)+1);
1973 if( header == sHeader )
1980 memcpy(pTmp, p, dataSize + 4);
1982 size += dataSize + 4;
1990 memset(pData,0, *dataLen);
1994 memcpy(pData, pNewHeader, size);
static void zip64local_putValue_inmemory(void *dest, ZPOS64_T x, int nbByte)
static int zip64local_getLong64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
Byte buffered_data[(64 *1024)]
#define ZSEEK64(filefunc, filestream, pos, mode)
#define ZIP64ENDLOCHEADERMAGIC
static int zip64local_getShort(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX)
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
#define ZIP_INTERNALERROR
static int zip64local_getByte(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, int *pi)
#define SIZEDATA_INDATABLOCK
#define ZCLOSE64(filefunc, filestream)
static void free_datablock(linkedlist_datablock_internal *ldi)
int deflateEnd(z_streamp strm)
uLong avail_in_this_block
int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_size, uLong crc32)
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)
#define SIZECENTRALHEADER
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)
#define ZREAD64(filefunc, filestream, buf, size)
#define APPEND_STATUS_CREATEAFTER
#define ZLIB_FILEFUNC_MODE_READ
int Write_EndOfCentralDirectoryRecord(zip64_internal *zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
ZPOS64_T pos_zip64extrainfo
int ZEXPORT zipRemoveExtraInfoBlock(char *pData, int *dataLen, short sHeader)
z_crc_t * get_crc_table()
static linkedlist_datablock_internal * allocate_new_datablock()
ZPOS64_T totalCompressedData
linkedlist_datablock_internal * last_block
zipFile ZEXPORT zipOpen(const char *pathname, int append)
uLong size_centralExtraFree
unsigned long crc32(unsigned long crc, unsigned char *buf, uInt len)
int ZEXPORT zipCloseFileInZipRaw(zipFile file, uLong uncompressed_size, uLong crc32)
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)
seek_file_func zseek32_file
uInt pos_in_buffered_data
unsigned char data[(4096-(4 *4))]
zipFile ZEXPORT zipOpen2(const char *pathname, int append, zipcharpc *globalcomment, zlib_filefunc_def *pzlib_filefunc32_def)
int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, unsigned int len)
#define ZWRITE64(filefunc, filestream, buf, size)
static void free_linkedlist(linkedlist_data *ll)
static ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream)
static int zip64local_getLong(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX)
struct linkedlist_datablock_internal_s * next_datablock
int Write_LocalFileHeader(zip64_internal *zi, const char *filename, uInt size_extrafield_local, const void *extrafield_local)
ZPOS64_T add_position_when_writting_offset
static uLong zip64local_TmzDateToDosDate(const tm_zip *ptm)
int LoadCentralDirectoryRecord(zip64_internal *pziinit)
linkedlist_data central_dir
static int zip64local_putValue OF((const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte))
#define zencode(pkeys, pcrc_32_tab, c, t)
#define ZLIB_FILEFUNC_MODE_CREATE
unsigned long long int ZPOS64_T
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def)
#define ZLIB_FILEFUNC_MODE_EXISTING
#define ZIP64ENDHEADERMAGIC
zlib_filefunc64_32_def z_filefunc
static void init_linkedlist(linkedlist_data *ll)
int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal *zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
tell_file_func ztell32_file
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)
#define ZTELL64(filefunc, filestream)
zipFile ZEXPORT zipOpen64(const void *pathname, int append)
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)
int Write_GlobalComment(zip64_internal *zi, const char *global_comment)
static int zip64FlushWriteBuffer(zip64_internal *zi)
int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal *zi, ZPOS64_T zip64eocd_pos_inzip)
uLong filled_in_this_block
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)
const z_crc_t * pcrc_32_tab
zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, zipcharpc *globalcomment, zlib_filefunc64_def *pzlib_filefunc_def)
zlib_filefunc64_def zfile_func64
#define ZLIB_FILEFUNC_MODE_WRITE
#define ZERROR64(filefunc, filestream)
int ZEXPORT zipCloseFileInZip(zipFile file)
#define ZOPEN64(filefunc, filename, mode)
#define Z_DEFAULT_STRATEGY
#define ZLIB_FILEFUNC_SEEK_END
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)
static ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream)
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)
static int add_data_in_datablock(linkedlist_data *ll, const void *buf, uLong len)
#define APPEND_STATUS_ADDINZIP
ZPOS64_T totalUncompressedData
const char zip_copyright[]
zipFile ZEXPORT zipOpen3(const void *pathname, int append, zipcharpc *globalcomment, zlib_filefunc64_32_def *pzlib_filefunc64_32_def)
#define ZLIB_FILEFUNC_SEEK_SET
linkedlist_datablock_internal * first_block
#define APPEND_STATUS_CREATE
int ZEXPORT zipClose(zipFile file, const char *global_comment)
int deflate(z_streamp strm, int flush)
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def *p_filefunc64_32, const zlib_filefunc_def *p_filefunc32)
#define CENTRALHEADERMAGIC
static int zip64local_putValue(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)
ZPOS64_T pos_local_header