Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. #include "dz_errs.h"
  4.  
  5. #undef _DZ_FILE_
  6. #define _DZ_FILE_ DZ_UNZCRYPT_CPP
  7. /*
  8. * crypt.c (full version) by Info-ZIP.      Last revised:  [see crypt.h]
  9. *
  10. * This code is not copyrighted and is put in the public domain.  The
  11. * encryption/decryption parts (as opposed to the non-echoing password
  12. * parts) were originally written in Europe; the whole file can there-
  13. * fore be freely distributed from any country except the USA.  If this
  14. * code is imported into the USA, it cannot be re-exported from from
  15. * there to another country.  (This restriction might seem curious, but
  16. * this is what US law requires.) */
  17.  
  18. /* This version modified by Chris Vleghert and Eric W. Engler
  19. * for BCB/Delphi Zip, Sep 22, 2000. */
  20.  
  21. /* This encryption code is a direct transcription of the algorithm from
  22. * Roger Schlafly, described by Phil Katz in the file appnote.txt.  This
  23. * file (appnote.txt) is distributed with the PKZIP program (even in the
  24. * version without encryption capabilities). *
  25.  
  26.   Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.
  27.  
  28.   See the accompanying file LICENSE, version 2007-Mar-4 or later
  29.   (the contents of which are also included in zip.h) for terms of use.
  30.   If, for some reason, all these files are missing, the Info-ZIP license
  31.   also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
  32.  
  33.   parts Copyright (C) 1997 Mike White, Eric W. Engler
  34. ************************************************************************
  35.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  36.  
  37.    This file is part of TZipMaster Version 1.9.
  38.  
  39.     TZipMaster is free software: you can redistribute it and/or modify
  40.     it under the terms of the GNU Lesser General Public License as published by
  41.     the Free Software Foundation, either version 3 of the License, or
  42.     (at your option) any later version.
  43.  
  44.     TZipMaster is distributed in the hope that it will be useful,
  45.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  46.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  47.     GNU Lesser General Public License for more details.
  48.  
  49.     You should have received a copy of the GNU Lesser General Public License
  50.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  51.  
  52.     contact: problems@delphizip.org (include ZipMaster in the subject).
  53.     updates: http://www.delphizip.org
  54.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  55. ************************************************************************/
  56.  
  57. #define ZCRYPT_INTERNAL
  58.  
  59. #include "UnzInf.h"
  60.  
  61. #include "crypt.h"
  62. //#include "ucrypt.h"
  63.  
  64. //static int testp(UGlobals * pG, uch * h);
  65. //static int testkey(UGlobals * pG, uch * h, char * key);
  66.  
  67. //#  define NEXTBYTE  (--fincnt >= 0 ? (int)(*finptr++) : readbyte())
  68. /* ===========================================================================
  69. * Get the password and set up keys for current zipfile member.  Return * PK_ class error. */
  70. int UnzInf::decrypt(ulg crc)
  71. {
  72.   ush b;
  73.   int n, n1;//, r;
  74.   char h[RAND_HEAD_LEN];
  75.  
  76.   if (Verbose < 0)
  77.     Notify(ITRACE,  _T("in crypt.c, [incnt = %d]: "), fincnt); //stout
  78.  
  79.   /* get header once (turn off "encrypted" flag temporarily so we don't * try to decrypt the same data twice) */
  80. //  fpInfo->encrypted = false;
  81.   Encrypted = false;
  82.   defer_leftover_input();
  83.   for (n = 0; n < RAND_HEAD_LEN; n++)
  84.   {
  85.     b = (ush)NEXTBYTE;
  86.     /* RCV: ush added */
  87.     h[n] = (uch)b;
  88.     //Trace((pG, " (%02x)", h[n]));  //stdout
  89.   }
  90.   undefer_input();
  91.   Encrypted = true;
  92.  
  93.   if (Verbose < 0)
  94.     Notify(ITRACE, _T("EWE - in crypt.c, near fnewzip test"));
  95.  
  96.   if (!fPwdReqCount)
  97.         return PK_WARN;
  98.   if (fnewzip)
  99.   {
  100.     fnewzip = false;
  101.     if (Verbose < 0)
  102.       Notify(ITRACE, _T("newzip was set to false"));
  103.  
  104.     if (fP_flag == true)
  105.     {
  106.       /* user gave password on command line */
  107. #ifdef ZDEBUG
  108.       if (Verbose < 0)
  109.         Notify(ITRACE, _T("user set password=%s"), fpwdarg.c_str());
  110. #endif
  111.     }
  112.   }
  113.  
  114.   if (Verbose < 0)
  115.     Notify(ITRACE, _T("EWE- near 'have key already' test"));
  116.   /* if have key already, test it; else allocate memory for it */
  117.   if (fkey)
  118.   {
  119.         if (!testkey(h, fkey, crc))
  120.     {
  121.       if (Verbose < 0)
  122.         Notify(ITRACE, _T("existing pwd OK"));
  123.       return PK_COOL; // existing password OK (else prompt for new)
  124.     }
  125.     else
  126.       if (fnopwd)
  127.         return PK_WARN; // user indicated no more prompting
  128.   }
  129.     else
  130.       if (fnopwd)
  131.                 return PK_WARN; // user indicated no more prompting
  132.   if (Verbose < 0)
  133.     Notify(ITRACE, _T("EWE- near 'try a few keys' test"));
  134.  
  135.   /* try a few keys */
  136.   n = fPwdReqCount;
  137.   n1 = 15;
  138.   do
  139.   {
  140.     // Call the component with a request for a password
  141.     CB->Arg1 = n;
  142.     CB->Msg = ffilename;
  143.     int ucb = CB->UserCB(zacPassword);
  144.     if (ucb == CALLBACK_TRUE)        // have password
  145.     {
  146.       fkey = DupStr(DZStrA(CB->Msg));
  147.           if (!testkey(h, fkey, crc))   // test local
  148.         return PK_COOL;
  149.     }
  150.     if (ucb == CALLBACK_3)         // gave no password
  151.     {
  152.       fnopwd = true;
  153.       n1 = 1;
  154.     }
  155.         n = MIN((int)CB->Arg1 & 0x0F, n1);
  156.   }
  157.   while (--n > 0 && --n1 > 0 && !Abort_Flag);
  158.  
  159.   return PK_WARN;
  160. }
  161. /* end function decrypt() */
  162.  
  163.  
  164. /* ===========================================================================
  165. *h   :: Decrypted header.
  166. *key :: Decryption password to test. */
  167. int UnzInf::testkey(const char *h, const char *key, ulg crc)
  168. {
  169.   ush b;
  170.  
  171.   int n;
  172.   uch * p;
  173.   uch hh[RAND_HEAD_LEN];
  174.   /* decrypted header */
  175.  
  176.   /* set keys and save the encrypted header */
  177.   init_keys(key, fkeys);
  178.   memcpy(hh, h, RAND_HEAD_LEN);
  179.  
  180.   /* check password */
  181.   for (n = 0; n < RAND_HEAD_LEN; n++)
  182.   {
  183.     hh[n] = (uch) zdecode(hh[n], fkeys);
  184.   }
  185.  
  186.   b = hh[RAND_HEAD_LEN - 1];
  187.   if (b != (ush)crc)
  188.         return -1; // bad
  189. //#endif
  190.   /* password OK:  decrypt current buffer contents before leaving */
  191.   for (n = (unsigned)fincnt > (unsigned) fcsize ? (int)fcsize : fincnt, p = finptr; n--; p++)
  192.         *p = (uch) zdecode(*p, fkeys);
  193.   return 0;  // OK
  194. }
  195. /* end function testkey() */
  196.  
  197.  
  198.  
  199.  
  200.  
  201.