Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. #include "DZRaw.h"
  4. #include "ZipDflt.h"
  5. #include "ZipOp.h"
  6. //#include "Zip.h"
  7. //#include "ZipErr.h"
  8. #include "dz_errs.h"
  9.  
  10. #undef _DZ_FILE_
  11. #define _DZ_FILE_ DZ_ZIPSS_CPP
  12.  
  13. /* Win32Zip.c
  14.  * Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  15.  * Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
  16.  * This version modified by Chris Vleghert and Eric Engler for BCB/Delphi Zip.
  17.  ** distributed under LGPL license ** see license.txt for details
  18.  
  19.   Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.
  20.  
  21.   See the accompanying file LICENSE, version 2007-Mar-4 or later
  22.   (the contents of which are also included in zip.h) for terms of use.
  23.   If, for some reason, all these files are missing, the Info-ZIP license
  24.   also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
  25.  
  26.   parts Copyright (C) 1997 Mike White, Eric W. Engler
  27. ************************************************************************
  28.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  29.  
  30.    This file is part of TZipMaster Version 1.9.
  31.  
  32.     TZipMaster is free software: you can redistribute it and/or modify
  33.     it under the terms of the GNU Lesser General Public License as published by
  34.     the Free Software Foundation, either version 3 of the License, or
  35.     (at your option) any later version.
  36.  
  37.     TZipMaster is distributed in the hope that it will be useful,
  38.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  39.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  40.     GNU Lesser General Public License for more details.
  41.  
  42.     You should have received a copy of the GNU Lesser General Public License
  43.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  44.  
  45.     contact: problems@delphizip.org (include ZipMaster in the subject).
  46.     updates: http://www.delphizip.org
  47.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  48. ************************************************************************/
  49.  
  50. int ZipOp::ZipStreamStream(void)
  51. {
  52.     fwindow_size = 0L;
  53.  
  54.     if (fGEncrypt)
  55.     {
  56.         fkey = fGPassword;
  57.  
  58.         if (!fkey || !*(fkey))
  59.         {
  60.             // use global
  61.             if (GetUserPW() != DZ_ERR_GOOD)
  62.                 return -1;  // error
  63.  
  64.             fkey = fuser_key;
  65.         }
  66.     }
  67.  
  68.     fZipInfile = new ZStream(this, _T("0:<INSTREAM>"), fSS->fSSInput);
  69.  
  70.     AutoStream inz(&fZipInfile);
  71.     fimax = fSS->Size;
  72.     fcrc = crc32(0L, NULL, 0);
  73.     fisize = 0;
  74.     CB->Arg1 = 1;
  75.     CB->UserCB(zacCount);
  76.  
  77.     // Pass total filesize.
  78.     CB->FileSize = fimax;
  79.     CB->UserCB(zacSize);
  80.     ulg f_crc = 0;
  81.     __int64 fsz = 0;
  82.     bool haveCRC = false;
  83.  
  84.     if (fkey)
  85.     {
  86.         if (!fNoPrecalc)
  87.         {
  88.             if (Verbose < 0)
  89.                 Notify(ITRACE,  _T("about to call Precalculate CRC"));
  90.  
  91.             // +++++ get CRC before we start
  92.                         CB->UserXItem(fimax, 13, _T("*PreCalculate"));
  93.             __int64 pos1 = 0;
  94.  
  95.             if (!fZipInfile->IsFile)
  96.                 pos1 = fZipInfile->SetPosition(0, FILE_CURRENT); // get start posn
  97.  
  98.                         f_crc = crc32(0L, NULL, 0);
  99.             unsigned long byts;
  100.  
  101.             while (true)
  102.             {
  103.                 unsigned ToRead = sizeof(fwindow);
  104.  
  105.                 if (fimax > 0 && (fsz + ToRead) > fimax)
  106.                 {
  107.                     ToRead = (unsigned)(fimax - fsz);
  108.  
  109.                     if (!ToRead)
  110.                         break;
  111.                 }
  112.  
  113.                 if (!fZipInfile->Read(fwindow, ToRead, &byts) || !byts)
  114.                     break;
  115.  
  116.                                 fsz += byts;
  117.                                 f_crc = crc32(f_crc, (const uch*)fwindow, byts);
  118.                                 CB->UserXProgress(byts, 13);
  119.  
  120.                 if (Abort_Flag)
  121.                                         Fatal(DZ_ERM_ABORT, 0);
  122.             }
  123.  
  124.             fSS->CRC = f_crc;
  125.  
  126.             haveCRC = true;
  127.             // reposition
  128.  
  129.             if (fZipInfile->SetPosition(pos1, FILE_BEGIN) != pos1)
  130.             {
  131.                 if (Verbose)
  132.                                         Notify(IVERBOSE, _T("Could not reposition %s [%s]"),
  133.                                                 fZipInfile->fname.c_str(), SysMsg().c_str());
  134.  
  135.                                 return  DZError(DZ_ERM_ERROR_SEEK);
  136.                         }
  137.  
  138.             if (fimax > fsz)
  139.                 fimax = fsz;
  140.         }
  141.  
  142.         // ----- get CRC before we start
  143.         // Since we do not yet know the crc here, we pretend that the crc is the
  144.         //   modification time:
  145. //    if (!haveCRC)
  146. //      fSS->CRC = z->tim << 16;
  147.         if (Verbose < 0)
  148.             Notify(ITRACE,  _T("using supplied CRC %lu"), fSS->CRC);
  149.     }
  150.  
  151.     // connect to output
  152.     fZipOutfile = new ZStream(this, _T("0:<OUTSTREAM>"), fSS->fSSOutput);
  153.  
  154.     AutoStream outz(&fZipOutfile);
  155.  
  156.     CB->UserItem(fimax, _T("<INSTREAM>"));
  157.  
  158.         if (fkey)
  159.                 crypthead(fkey, fSS->CRC);  // write
  160.  
  161.     // Write stored or deflated file to zip file
  162.     fSS->Method &= 0xFF;
  163.  
  164.     if (fSS->Method != DEFLATE)
  165.         fSS->Method = 0;
  166.  
  167.     if (flevel < 1)
  168.         fSS->Method = 0;
  169.  
  170.     int mthd = fSS->Method;
  171.  
  172.     if (mthd == DEFLATE)
  173.     {
  174.         if (Verbose < 0)
  175.             Notify(ITRACE,  _T("about to call Deflate"));
  176.  
  177.         bi_init();
  178.         ush att = BINARY;
  179.         ush flg = FLAG_ENCRYPT_BIT;
  180.  
  181.         // will be changed in deflate()
  182.         ct_init(&att, &mthd);
  183.         lm_init(flevel, &flg);
  184.  
  185.         // PERFORM THE DEFLATE
  186.         fSS->Size = deflate();
  187.  
  188.         if (Abort_Flag)
  189.                         Fatal(DZ_ERM_ABORT, 0);
  190.     }
  191.     else
  192.     {
  193.         int k;
  194.  
  195.         if (Verbose)
  196.             Notify(IVERBOSE, _T("Storing %s "), fZipInfile->fname.c_str());
  197.  
  198.         while ((k = read_buf(fwindow, sizeof(fwindow))) > 0
  199.                 && k != EOF)
  200.         {
  201.             if (Abort_Flag)
  202.                                 Fatal(DZ_ERM_ABORT, 0);
  203.  
  204.             if (!zfwrite(fwindow,  k))
  205.                                 return DZ_ERM_TEMP_FAILED;
  206.         }
  207.  
  208.     }
  209.     /* TODO 1 -oRP -cenhancement : Finished Item */
  210.     CB->UserCB(zacEndOfBatch); // done with stream compression
  211.  
  212.     if (haveCRC)
  213.     {
  214.         if (f_crc != fcrc)
  215.             Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File CRC changed while zipping: %s"),
  216.                    fZipInfile->fname.c_str());
  217.  
  218.         if (fisize != fsz)
  219.             Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File size changed while zipping: %s"),
  220.                    fZipInfile->fname.c_str());
  221.     }
  222.  
  223.     fSS->Size = fisize;
  224.  
  225.     fSS->CRC = fcrc;
  226.     fSS->Method = mthd | (fkey ? 0xff00 : 0);
  227.     return DZ_ERR_GOOD;
  228. }
  229.  
  230.  
  231.