2009-10-01 01:10:58 +02:00
|
|
|
/*
|
2010-09-24 02:48:03 +02:00
|
|
|
Additional tools for Minizip
|
|
|
|
Code: Xavier Roche '2004
|
|
|
|
License: Same as ZLIB (www.gzip.org)
|
|
|
|
*/
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
/* Code */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "zlib.h"
|
|
|
|
#include "unzip.h"
|
|
|
|
|
|
|
|
#define READ_8(adr) ((unsigned char)*(adr))
|
|
|
|
#define READ_16(adr) ( READ_8(adr) | (READ_8(adr+1) << 8) )
|
|
|
|
#define READ_32(adr) ( READ_16(adr) | (READ_16((adr)+2) << 16) )
|
|
|
|
|
|
|
|
#define WRITE_8(buff, n) do { \
|
|
|
|
*((unsigned char*)(buff)) = (unsigned char) ((n) & 0xff); \
|
|
|
|
} while(0)
|
|
|
|
#define WRITE_16(buff, n) do { \
|
|
|
|
WRITE_8((unsigned char*)(buff), n); \
|
|
|
|
WRITE_8(((unsigned char*)(buff)) + 1, (n) >> 8); \
|
|
|
|
} while(0)
|
|
|
|
#define WRITE_32(buff, n) do { \
|
|
|
|
WRITE_16((unsigned char*)(buff), (n) & 0xffff); \
|
|
|
|
WRITE_16((unsigned char*)(buff) + 2, (n) >> 16); \
|
|
|
|
} while(0)
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
extern int ZEXPORT unzRepair(file, fileOut, fileOutTmp, nRecovered, bytesRecovered)
|
|
|
|
const char* file;const char* fileOut;const char* fileOutTmp;uLong* nRecovered;uLong* bytesRecovered;
|
2009-10-01 01:10:58 +02:00
|
|
|
{
|
|
|
|
int err = Z_OK;
|
2010-09-24 02:48:03 +02:00
|
|
|
FILE* fpZip = fopen(file, "rb");
|
|
|
|
FILE* fpOut = fopen(fileOut, "wb");
|
|
|
|
FILE* fpOutCD = fopen(fileOutTmp, "wb");
|
|
|
|
if (fpZip != NULL && fpOut != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
int entries = 0;
|
|
|
|
uLong totalBytes = 0;
|
|
|
|
char header[30];
|
|
|
|
char filename[256];
|
|
|
|
char extra[1024];
|
|
|
|
int offset = 0;
|
|
|
|
int offsetCD = 0;
|
2010-09-24 02:48:03 +02:00
|
|
|
while (fread(header, 1, 30, fpZip) == 30)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
int currentOffset = offset;
|
|
|
|
|
|
|
|
/* File entry */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (READ_32( header ) == 0x04034b50)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
unsigned int version = READ_16( header + 4 );
|
|
|
|
unsigned int gpflag = READ_16( header + 6 );
|
|
|
|
unsigned int method = READ_16( header + 8 );
|
|
|
|
unsigned int filetime = READ_16( header + 10 );
|
|
|
|
unsigned int filedate = READ_16( header + 12 );
|
|
|
|
unsigned int crc = READ_32( header + 14 ); /* crc */
|
|
|
|
unsigned int cpsize = READ_32( header + 18 ); /* compressed size */
|
|
|
|
unsigned int uncpsize = READ_32( header + 22 ); /* uncompressed sz */
|
|
|
|
unsigned int fnsize = READ_16( header + 26 ); /* file name length */
|
|
|
|
unsigned int extsize = READ_16( header + 28 ); /* extra field length */
|
2009-10-01 01:10:58 +02:00
|
|
|
filename[0] = extra[0] = '\0';
|
|
|
|
|
|
|
|
/* Header */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fwrite(header, 1, 30, fpOut) == 30)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offset += 30;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Filename */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fnsize > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fread(filename, 1, fnsize, fpZip) == fnsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fwrite(filename, 1, fnsize, fpOut) == fnsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offset += fnsize;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_STREAM_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extra field */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (extsize > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fread(extra, 1, extsize, fpZip) == extsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fwrite(extra, 1, extsize, fpOut) == extsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offset += extsize;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Data */
|
|
|
|
{
|
|
|
|
int dataSize = cpsize;
|
2010-09-24 02:48:03 +02:00
|
|
|
if (dataSize == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
dataSize = uncpsize;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (dataSize > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
char* data = malloc(dataSize);
|
|
|
|
if (data != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if ((int) fread(data, 1, dataSize, fpZip) == dataSize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if ((int) fwrite(data, 1, dataSize, fpOut) == dataSize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offset += dataSize;
|
|
|
|
totalBytes += dataSize;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
free(data);
|
|
|
|
if (err != Z_OK)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_MEM_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Central directory entry */
|
|
|
|
{
|
|
|
|
char header[46];
|
|
|
|
char* comment = "";
|
2010-09-24 02:48:03 +02:00
|
|
|
int comsize = (int) strlen(comment);
|
2010-09-19 01:16:05 +02:00
|
|
|
WRITE_32( header, 0x02014b50 );
|
|
|
|
WRITE_16( header + 4, version );
|
|
|
|
WRITE_16( header + 6, version );
|
|
|
|
WRITE_16( header + 8, gpflag );
|
|
|
|
WRITE_16( header + 10, method );
|
|
|
|
WRITE_16( header + 12, filetime );
|
|
|
|
WRITE_16( header + 14, filedate );
|
|
|
|
WRITE_32( header + 16, crc );
|
|
|
|
WRITE_32( header + 20, cpsize );
|
|
|
|
WRITE_32( header + 24, uncpsize );
|
|
|
|
WRITE_16( header + 28, fnsize );
|
|
|
|
WRITE_16( header + 30, extsize );
|
|
|
|
WRITE_16( header + 32, comsize );
|
2010-09-24 02:48:03 +02:00
|
|
|
WRITE_16( header + 34, 0 ); /* disk # */
|
|
|
|
WRITE_16( header + 36, 0 ); /* int attrb */
|
|
|
|
WRITE_32( header + 38, 0 ); /* ext attrb */
|
2010-09-19 01:16:05 +02:00
|
|
|
WRITE_32( header + 42, currentOffset );
|
2009-10-01 01:10:58 +02:00
|
|
|
/* Header */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fwrite(header, 1, 46, fpOutCD) == 46)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offsetCD += 46;
|
|
|
|
|
|
|
|
/* Filename */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fnsize > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fwrite(filename, 1, fnsize, fpOutCD) == fnsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offsetCD += fnsize;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_STREAM_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extra field */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (extsize > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fwrite(extra, 1, extsize, fpOutCD) == extsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offsetCD += extsize;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Comment field */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (comsize > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if ((int) fwrite(comment, 1, comsize, fpOutCD) == comsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
offsetCD += comsize;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success */
|
|
|
|
entries++;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Final central directory */
|
|
|
|
{
|
|
|
|
int entriesZip = entries;
|
|
|
|
char header[22];
|
|
|
|
char* comment = ""; // "ZIP File recovered by zlib/minizip/mztools";
|
2010-09-24 02:48:03 +02:00
|
|
|
int comsize = (int) strlen(comment);
|
|
|
|
if (entriesZip > 0xffff)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
entriesZip = 0xffff;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
WRITE_32( header, 0x06054b50 );
|
2010-09-24 02:48:03 +02:00
|
|
|
WRITE_16( header + 4, 0 ); /* disk # */
|
|
|
|
WRITE_16( header + 6, 0 ); /* disk # */
|
2010-09-19 01:16:05 +02:00
|
|
|
WRITE_16( header + 8, entriesZip ); /* hack */
|
2010-09-24 02:48:03 +02:00
|
|
|
WRITE_16( header + 10, entriesZip ); /* hack */
|
|
|
|
WRITE_32( header + 12, offsetCD ); /* size of CD */
|
|
|
|
WRITE_32( header + 16, offset ); /* offset to CD */
|
|
|
|
WRITE_16( header + 20, comsize ); /* comment */
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
/* Header */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fwrite(header, 1, 22, fpOutCD) == 22)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
/* Comment field */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (comsize > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if ((int) fwrite(comment, 1, comsize, fpOutCD) != comsize)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Final merge (file + central directory) */
|
2010-09-24 02:48:03 +02:00
|
|
|
fclose(fpOutCD);
|
|
|
|
if (err == Z_OK)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
fpOutCD = fopen(fileOutTmp, "rb");
|
|
|
|
if (fpOutCD != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
int nRead;
|
|
|
|
char buffer[8192];
|
2010-09-24 02:48:03 +02:00
|
|
|
while ((nRead = (int) fread(buffer, 1, sizeof(buffer), fpOutCD)) > 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if ((int) fwrite(buffer, 1, nRead, fpOut) != nRead)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
fclose(fpOutCD);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close */
|
2010-09-24 02:48:03 +02:00
|
|
|
fclose(fpZip);
|
|
|
|
fclose(fpOut);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
/* Wipe temporary file */
|
2010-09-24 02:48:03 +02:00
|
|
|
(void) remove(fileOutTmp);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
/* Number of recovered entries */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (err == Z_OK)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (nRecovered != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
*nRecovered = entries;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (bytesRecovered != NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
*bytesRecovered = totalBytes;
|
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
err = Z_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|