view src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItem.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // Archive/ZipItem.h
3 #ifndef __ARCHIVE_ZIP_ITEM_H
4 #define __ARCHIVE_ZIP_ITEM_H
6 #include "../../../Common/Types.h"
7 #include "../../../Common/MyString.h"
8 #include "../../../Common/Buffer.h"
9 #include "../../../Common/UTFConvert.h"
10 #include "../../../Common/StringConvert.h"
12 #include "ZipHeader.h"
14 namespace NArchive {
15 namespace NZip {
17 struct CVersion
18 {
19 Byte Version;
20 Byte HostOS;
21 };
23 bool operator==(const CVersion &v1, const CVersion &v2);
24 bool operator!=(const CVersion &v1, const CVersion &v2);
26 struct CExtraSubBlock
27 {
28 UInt16 ID;
29 CByteBuffer Data;
30 bool ExtractNtfsTime(int index, FILETIME &ft) const;
31 };
33 struct CWzAesExtraField
34 {
35 UInt16 VendorVersion; // 0x0001 - AE-1, 0x0002 - AE-2,
36 // UInt16 VendorId; // "AE"
37 Byte Strength; // 1 - 128-bit , 2 - 192-bit , 3 - 256-bit
38 UInt16 Method;
40 CWzAesExtraField(): VendorVersion(2), Strength(3), Method(0) {}
42 bool NeedCrc() const { return (VendorVersion == 1); }
44 bool ParseFromSubBlock(const CExtraSubBlock &sb)
45 {
46 if (sb.ID != NFileHeader::NExtraID::kWzAES)
47 return false;
48 if (sb.Data.GetCapacity() < 7)
49 return false;
50 const Byte *p = (const Byte *)sb.Data;
51 VendorVersion = (((UInt16)p[1]) << 8) | p[0];
52 if (p[2] != 'A' || p[3] != 'E')
53 return false;
54 Strength = p[4];
55 Method = (((UInt16)p[6]) << 16) | p[5];
56 return true;
57 }
58 void SetSubBlock(CExtraSubBlock &sb) const
59 {
60 sb.Data.SetCapacity(7);
61 sb.ID = NFileHeader::NExtraID::kWzAES;
62 Byte *p = (Byte *)sb.Data;
63 p[0] = (Byte)VendorVersion;
64 p[1] = (Byte)(VendorVersion >> 8);
65 p[2] = 'A';
66 p[3] = 'E';
67 p[4] = Strength;
68 p[5] = (Byte)Method;
69 p[6] = (Byte)(Method >> 8);
70 }
71 };
73 namespace NStrongCryptoFlags
74 {
75 const UInt16 kDES = 0x6601;
76 const UInt16 kRC2old = 0x6602;
77 const UInt16 k3DES168 = 0x6603;
78 const UInt16 k3DES112 = 0x6609;
79 const UInt16 kAES128 = 0x660E;
80 const UInt16 kAES192 = 0x660F;
81 const UInt16 kAES256 = 0x6610;
82 const UInt16 kRC2 = 0x6702;
83 const UInt16 kBlowfish = 0x6720;
84 const UInt16 kTwofish = 0x6721;
85 const UInt16 kRC4 = 0x6801;
86 }
88 struct CStrongCryptoField
89 {
90 UInt16 Format;
91 UInt16 AlgId;
92 UInt16 BitLen;
93 UInt16 Flags;
95 bool ParseFromSubBlock(const CExtraSubBlock &sb)
96 {
97 if (sb.ID != NFileHeader::NExtraID::kStrongEncrypt)
98 return false;
99 const Byte *p = (const Byte *)sb.Data;
100 if (sb.Data.GetCapacity() < 8)
101 return false;
102 Format = (((UInt16)p[1]) << 8) | p[0];
103 AlgId = (((UInt16)p[3]) << 8) | p[2];
104 BitLen = (((UInt16)p[5]) << 8) | p[4];
105 Flags = (((UInt16)p[7]) << 8) | p[6];
106 return (Format == 2);
107 }
108 };
110 struct CExtraBlock
111 {
112 CObjectVector<CExtraSubBlock> SubBlocks;
113 void Clear() { SubBlocks.Clear(); }
114 size_t GetSize() const
115 {
116 size_t res = 0;
117 for (int i = 0; i < SubBlocks.Size(); i++)
118 res += SubBlocks[i].Data.GetCapacity() + 2 + 2;
119 return res;
120 }
121 bool GetWzAesField(CWzAesExtraField &aesField) const
122 {
123 for (int i = 0; i < SubBlocks.Size(); i++)
124 if (aesField.ParseFromSubBlock(SubBlocks[i]))
125 return true;
126 return false;
127 }
129 bool GetStrongCryptoField(CStrongCryptoField &f) const
130 {
131 for (int i = 0; i < SubBlocks.Size(); i++)
132 if (f.ParseFromSubBlock(SubBlocks[i]))
133 return true;
134 return false;
135 }
137 bool HasWzAesField() const
138 {
139 CWzAesExtraField aesField;
140 return GetWzAesField(aesField);
141 }
143 bool GetNtfsTime(int index, FILETIME &ft) const
144 {
145 for (int i = 0; i < SubBlocks.Size(); i++)
146 {
147 const CExtraSubBlock &sb = SubBlocks[i];
148 if (sb.ID == NFileHeader::NExtraID::kNTFS)
149 return sb.ExtractNtfsTime(index, ft);
150 }
151 return false;
152 }
154 /*
155 bool HasStrongCryptoField() const
156 {
157 CStrongCryptoField f;
158 return GetStrongCryptoField(f);
159 }
160 */
162 void RemoveUnknownSubBlocks()
163 {
164 for (int i = SubBlocks.Size() - 1; i >= 0; i--)
165 if (SubBlocks[i].ID != NFileHeader::NExtraID::kWzAES)
166 SubBlocks.Delete(i);
167 }
168 };
171 class CLocalItem
172 {
173 public:
174 CVersion ExtractVersion;
175 UInt16 Flags;
176 UInt16 CompressionMethod;
177 UInt32 Time;
178 UInt32 FileCRC;
179 UInt64 PackSize;
180 UInt64 UnPackSize;
182 AString Name;
184 CExtraBlock LocalExtra;
186 bool IsUtf8() const { return (Flags & NFileHeader::NFlags::kUtf8) != 0; }
188 bool IsEncrypted() const { return (Flags & NFileHeader::NFlags::kEncrypted) != 0; }
189 bool IsStrongEncrypted() const { return IsEncrypted() && (Flags & NFileHeader::NFlags::kStrongEncrypted) != 0; };
191 bool IsLzmaEOS() const { return (Flags & NFileHeader::NFlags::kLzmaEOS) != 0; }
193 bool IsDir() const;
194 bool IgnoreItem() const { return false; }
195 UInt32 GetWinAttributes() const;
197 bool HasDescriptor() const { return (Flags & NFileHeader::NFlags::kDescriptorUsedMask) != 0; }
199 UString GetUnicodeString(const AString &s) const
200 {
201 UString res;
202 if (IsUtf8())
203 if (!ConvertUTF8ToUnicode(s, res))
204 res.Empty();
205 if (res.IsEmpty())
206 res = MultiByteToUnicodeString(s, GetCodePage());
207 return res;
208 }
210 private:
211 void SetFlagBits(int startBitNumber, int numBits, int value);
212 void SetBitMask(int bitMask, bool enable);
213 public:
214 void ClearFlags() { Flags = 0; }
215 void SetEncrypted(bool encrypted);
216 void SetUtf8(bool isUtf8);
218 WORD GetCodePage() const { return CP_OEMCP; }
219 };
221 class CItem: public CLocalItem
222 {
223 public:
224 CVersion MadeByVersion;
225 UInt16 InternalAttributes;
226 UInt32 ExternalAttributes;
228 UInt64 LocalHeaderPosition;
230 FILETIME NtfsMTime;
231 FILETIME NtfsATime;
232 FILETIME NtfsCTime;
234 CExtraBlock CentralExtra;
235 CByteBuffer Comment;
237 bool FromLocal;
238 bool FromCentral;
239 bool NtfsTimeIsDefined;
241 bool IsDir() const;
242 UInt32 GetWinAttributes() const;
244 bool IsThereCrc() const
245 {
246 if (CompressionMethod == NFileHeader::NCompressionMethod::kWzAES)
247 {
248 CWzAesExtraField aesField;
249 if (CentralExtra.GetWzAesField(aesField))
250 return aesField.NeedCrc();
251 }
252 return (FileCRC != 0 || !IsDir());
253 }
255 WORD GetCodePage() const
256 {
257 return (WORD)((MadeByVersion.HostOS == NFileHeader::NHostOS::kFAT
258 || MadeByVersion.HostOS == NFileHeader::NHostOS::kNTFS
259 ) ? CP_OEMCP : CP_ACP);
260 }
261 CItem() : FromLocal(false), FromCentral(false), NtfsTimeIsDefined(false) {}
262 };
264 }}
266 #endif