ZipCrc32Cryptograph.h
Go to the documentation of this file.
1 
2 // This source file is part of the ZipArchive Library Open Source distribution
3 // and is Copyrighted 2000 - 2022 by Artpol Software - Tadeusz Dracz
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // For the licensing details refer to the License.txt file.
11 //
12 // Web Site: https://www.artpol-software.com
14 
21 #if !defined(ZIPARCHIVE_ZIPCRC32CRYPTOGRAPH_DOT_H)
22 #define ZIPARCHIVE_ZIPCRC32CRYPTOGRAPH_DOT_H
23 
24 #if _MSC_VER > 1000
25  #pragma once
26 #endif
27 
28 #include "zlib/zlib.h"
29 
30 #include "ZipCryptograph.h"
31 #include "ZipFileHeader.h"
32 #include "ZipStorage.h"
33 
34 
35 #define ZIPARCHIVE_ENCR_HEADER_LEN 12
36 
43 class ZIP_API CZipCrc32Cryptograph : public CZipCryptograph
44 {
45 public:
47  {
48  memset(m_keys, 0, 3 * sizeof(DWORD));
49  }
50 
51  bool InitDecode(CZipAutoBuffer& password, CZipFileHeader& currentFile, CZipStorage& storage, bool ignoreCheck);
52  void InitEncode(CZipAutoBuffer& password, CZipFileHeader& currentFile, CZipStorage& storage);
53  void Decode(char* pBuffer, DWORD uSize)
54  {
55  for (DWORD i = 0; i < uSize; i++)
56  CryptDecode(pBuffer[i]);
57  }
58  void Encode(char* pBuffer, DWORD uSize)
59  {
60  for (DWORD i = 0; i < uSize; i++)
61  CryptEncode(pBuffer[i]);
62  }
63 
64  bool CanHandle(int iEncryptionMethod)
65  {
66  return iEncryptionMethod == CZipCryptograph::encStandard;
67  }
68 
73  {
74  return ZIPARCHIVE_ENCR_HEADER_LEN;
75  }
76 
81  {
82  return 0;
83  }
84 
88  static const z_crc_t* GetCRCTable()
89  {
90  return zarch_get_crc_table();
91  }
92 private:
93 
94  void CryptDecode(char &c)
95  {
96  c ^= CryptDecryptByte();
97  CryptUpdateKeys(c);
98  }
99 
100  char CryptDecryptByte()
101  {
102  int temp = (m_keys[2] & 0xffff) | 2;
103  return (char)(((temp * (temp ^ 1)) >> 8) & 0xff);
104  }
105  void CryptInitKeys(CZipAutoBuffer& password);
106  void CryptUpdateKeys(char c);
107  DWORD CryptCRC32(DWORD l, char c)
108  {
109  const z_crc_t* CRC_TABLE = zarch_get_crc_table();
110  return (DWORD)CRC_TABLE[(l ^ c) & 0xff] ^ (l >> 8);
111  }
112  void CryptEncode(char &c)
113  {
114  char t = CryptDecryptByte();
115  CryptUpdateKeys(c);
116  c ^= t;
117  }
118  DWORD m_keys[3];
119 public:
121 };
122 
123 #endif

The ZipArchive Library Copyright © 2000 - 2022 Artpol Software - Tadeusz Dracz. Generated at Sat Dec 17 2022 19:57:03.