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