Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "stdafx.h"
  2. #pragma hdrstop        
  3. #include "common.h"
  4. #include "crypt.h"
  5. #include "dz_errs.h"
  6.  
  7. #undef _DZ_FILE_
  8. #define _DZ_FILE_ DZ_CRYPT_CPP
  9.  
  10. //#include "ZipErr.h"
  11. /* crypt.c (full version) by Info-ZIP.      Last revised:  [see crypt.h]
  12.  *
  13.  * This code is not copyrighted and is put in the public domain.  The
  14.  * encryption/decryption parts (as opposed to the non-echoing password
  15.  * parts) were originally written in Europe; the whole file can there-
  16.  * fore be freely distributed from any country except the USA.  If this
  17.  * code is imported into the USA, it cannot be re-exported from from
  18.  * there to another country.  (This restriction might seem curious, but
  19.  * this is what US law requires.)
  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.  * This version modified by Chris Vleghert and Eric Engler for BCB/Delphi Zip.
  26.   ** distributed under LGPL license ** see license.txt for details
  27.  */
  28.  
  29. /* For the encoding task used in Zip (and ZipCloak), we want to initialize
  30.  * the crypt algorithm with some reasonably unpredictable bytes, see
  31.  * the crypthead() function. The standard rand() library function is
  32.  * used to supply these 'random' bytes, which in turn is initialized by
  33.  * a srand() call. The srand() function takes an "unsigned" (at least 16bit)
  34.  * seed value as argument to determine the starting point of the rand()
  35.  * pseudo-random number generator.
  36.  * This seed number is constructed as "Seed = Seed1 .XOR. Seed2" with
  37.  * Seed1 supplied by the current time (= "(unsigned)time()") and Seed2
  38.  * as some (hopefully) nondeterministic bitmask. On many (most) systems,
  39.  * we use some "process specific" number, as the PID or something similar,
  40.  * but when nothing unpredictable is available, a fixed number may be
  41.  * sufficient.
  42.  * NOTE:
  43.  * 1.) This implementation requires the availability of the following
  44.  *     standard UNIX C runtime library functions: time(), rand(), srand().
  45.  *     On systems where some of them are missing, the environment that
  46.  *     incorporates the crypt routines must supply suitable replacement
  47.  *     functions.
  48.  * 2.) It is a very bad idea to use a second call to time() to set the
  49.  *     "Seed2" number! In this case, both "Seed1" and "Seed2" would be
  50.  *     (almost) identical, resulting in a (mostly) "zero" constant seed
  51.  *     number passed to srand().
  52.  *
  53.  * The implementation environment defined in the "zip.h" header should
  54.  * supply a reasonable definition for ZCR_SEED2 (an unsigned number; for
  55.  * most implementations of rand() and srand(), only the lower 16 bits are
  56.  * significant!). An example that works on many systems would be
  57.  *      "#define ZCR_SEED2  (unsigned)getpid()".
  58.  * The default definition for ZCR_SEED2 supplied below should be regarded
  59.  * as a fallback to allow successful compilation in "beta state"
  60.  * environments.
  61.  *
  62.  
  63.   Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.
  64.  
  65.   See the accompanying file LICENSE, version 2007-Mar-4 or later
  66.   (the contents of which are also included in zip.h) for terms of use.
  67.   If, for some reason, all these files are missing, the Info-ZIP license
  68.   also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
  69.  
  70.   parts Copyright (C) 1997 Mike White, Eric W. Engler
  71. ************************************************************************
  72.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  73.  
  74.    This file is part of TZipMaster Version 1.9.
  75.  
  76.     TZipMaster is free software: you can redistribute it and/or modify
  77.     it under the terms of the GNU Lesser General Public License as published by
  78.     the Free Software Foundation, either version 3 of the License, or
  79.     (at your option) any later version.
  80.  
  81.     TZipMaster is distributed in the hope that it will be useful,
  82.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  83.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  84.     GNU Lesser General Public License for more details.
  85.  
  86.     You should have received a copy of the GNU Lesser General Public License
  87.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  88.  
  89.     contact: problems@delphizip.org (include ZipMaster in the subject).
  90.     updates: http://www.delphizip.org
  91.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  92. ************************************************************************/
  93.  
  94. // "last resort" source for second part of crypt seed pattern
  95. #ifndef ZCR_SEED2 // RCV: Is now defined in Zip.h
  96. #define ZCR_SEED2 (unsigned)3141592654L // use PI as default pattern
  97. #endif
  98.  
  99. #define CRC32(c, b) (crc_table[((int)(c) ^ (b)) & 0xFF] ^ ((c) >> 8))
  100.  
  101. // Return the next byte in the pseudo-random sequence
  102. int _fastcall decrypt_byte(Keys keys)
  103. {
  104.     unsigned  temp;
  105.     temp = ((unsigned)keys[2] & 0xFFFF) | 2;
  106.     return (int)(((temp *(temp ^ 1)) >> 8) & 0xFF);
  107. }
  108.  
  109. // Update the encryption keys with the next byte of plain text c :: Byte of
  110. //   plain text.
  111. int  _fastcall update_keys(int c, Keys keys)
  112. {
  113.     keys[0] = CRC32(keys[0], c);
  114.     keys[1] += keys[0] & 0xFF;
  115.     keys[1] = keys[1] * 134775813L + 1;
  116.     {
  117.         register int  keyshift = (int)(keys[1] >> 24);
  118.         keys[2] = CRC32(keys[2], keyshift);
  119.     }
  120.  
  121.     return c;
  122. }
  123.  
  124. // Initialize the encryption keys and the random header according to the
  125. //   given password. passwd :: Password string with which to modify keys.
  126. void _fastcall init_keys(const char *passwd, Keys keys)
  127. {
  128.     keys[0] = 305419896L;
  129.     keys[1] = 591751049L;
  130.     keys[2] = 878082192L;
  131.  
  132.     while (*passwd != '\0')
  133.     {
  134.         update_keys((int) *passwd, keys);
  135.         passwd++;
  136.     }
  137. }
  138.  
  139. int _fastcall zencode(int c, Keys keys)
  140. {
  141.     int t;
  142.     unsigned  temp;
  143.     temp = ((unsigned)keys[2] & 0xFFFF) | 2;
  144.     t = (int)(((temp * (temp ^ 1)) >> 8) & 0xFF);
  145.  
  146.     keys[0] = CRC32(keys[0], c);
  147.     keys[1] += keys[0] & 0xFF;
  148.     keys[1] = keys[1] * 134775813L + 1;
  149.     {
  150.         register int  keyshift = (int)(keys[1] >> 24);
  151.         keys[2] = CRC32(keys[2], keyshift);
  152.     }
  153.  
  154.     return t ^ c;
  155. }
  156.  
  157. int _fastcall zdecode(int c, Keys keys)
  158. {
  159.     int t;
  160.     unsigned  temp;
  161.     temp = ((unsigned)keys[2] & 0xFFFF) | 2;
  162.     t = (int)(((temp * (temp ^ 1)) >> 8) & 0xFF);
  163.     t ^= c;
  164.     keys[0] = CRC32(keys[0], t);
  165.     keys[1] += keys[0] & 0xFF;
  166.         keys[1] = keys[1] * 134775813L + 1;
  167.     {
  168.         register int  keyshift = (int)(keys[1] >> 24);
  169.         keys[2] = CRC32(keys[2], keyshift);
  170.     }
  171.  
  172.     return t;
  173. }
  174.  
  175.  
  176.  
  177.  
  178.