diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItem.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,268 @@
     1.4 +// Archive/ZipItem.h
     1.5 +
     1.6 +#ifndef __ARCHIVE_ZIP_ITEM_H
     1.7 +#define __ARCHIVE_ZIP_ITEM_H
     1.8 +
     1.9 +#include "../../../Common/Types.h"
    1.10 +#include "../../../Common/MyString.h"
    1.11 +#include "../../../Common/Buffer.h"
    1.12 +#include "../../../Common/UTFConvert.h"
    1.13 +#include "../../../Common/StringConvert.h"
    1.14 +
    1.15 +#include "ZipHeader.h"
    1.16 +
    1.17 +namespace NArchive {
    1.18 +namespace NZip {
    1.19 +
    1.20 +struct CVersion
    1.21 +{
    1.22 +  Byte Version;
    1.23 +  Byte HostOS;
    1.24 +};
    1.25 +
    1.26 +bool operator==(const CVersion &v1, const CVersion &v2);
    1.27 +bool operator!=(const CVersion &v1, const CVersion &v2);
    1.28 +
    1.29 +struct CExtraSubBlock
    1.30 +{
    1.31 +  UInt16 ID;
    1.32 +  CByteBuffer Data;
    1.33 +  bool ExtractNtfsTime(int index, FILETIME &ft) const;
    1.34 +};
    1.35 +
    1.36 +struct CWzAesExtraField
    1.37 +{
    1.38 +  UInt16 VendorVersion; // 0x0001 - AE-1, 0x0002 - AE-2,
    1.39 +  // UInt16 VendorId; // "AE"
    1.40 +  Byte Strength; // 1 - 128-bit , 2 - 192-bit , 3 - 256-bit
    1.41 +  UInt16 Method;
    1.42 +
    1.43 +  CWzAesExtraField(): VendorVersion(2), Strength(3), Method(0) {}
    1.44 +
    1.45 +  bool NeedCrc() const { return (VendorVersion == 1); }
    1.46 +
    1.47 +  bool ParseFromSubBlock(const CExtraSubBlock &sb)
    1.48 +  {
    1.49 +    if (sb.ID != NFileHeader::NExtraID::kWzAES)
    1.50 +      return false;
    1.51 +    if (sb.Data.GetCapacity() < 7)
    1.52 +      return false;
    1.53 +    const Byte *p = (const Byte *)sb.Data;
    1.54 +    VendorVersion = (((UInt16)p[1]) << 8) | p[0];
    1.55 +    if (p[2] != 'A' || p[3] != 'E')
    1.56 +      return false;
    1.57 +    Strength = p[4];
    1.58 +    Method = (((UInt16)p[6]) << 16) | p[5];
    1.59 +    return true;
    1.60 +  }
    1.61 +  void SetSubBlock(CExtraSubBlock &sb) const
    1.62 +  {
    1.63 +    sb.Data.SetCapacity(7);
    1.64 +    sb.ID = NFileHeader::NExtraID::kWzAES;
    1.65 +    Byte *p = (Byte *)sb.Data;
    1.66 +    p[0] = (Byte)VendorVersion;
    1.67 +    p[1] = (Byte)(VendorVersion >> 8);
    1.68 +    p[2] = 'A';
    1.69 +    p[3] = 'E';
    1.70 +    p[4] = Strength;
    1.71 +    p[5] = (Byte)Method;
    1.72 +    p[6] = (Byte)(Method >> 8);
    1.73 +  }
    1.74 +};
    1.75 +
    1.76 +namespace NStrongCryptoFlags
    1.77 +{
    1.78 +  const UInt16 kDES = 0x6601;
    1.79 +  const UInt16 kRC2old = 0x6602;
    1.80 +  const UInt16 k3DES168 = 0x6603;
    1.81 +  const UInt16 k3DES112 = 0x6609;
    1.82 +  const UInt16 kAES128 = 0x660E;
    1.83 +  const UInt16 kAES192 = 0x660F;
    1.84 +  const UInt16 kAES256 = 0x6610;
    1.85 +  const UInt16 kRC2 = 0x6702;
    1.86 +  const UInt16 kBlowfish = 0x6720;
    1.87 +  const UInt16 kTwofish = 0x6721;
    1.88 +  const UInt16 kRC4 = 0x6801;
    1.89 +}
    1.90 +
    1.91 +struct CStrongCryptoField
    1.92 +{
    1.93 +  UInt16 Format;
    1.94 +  UInt16 AlgId;
    1.95 +  UInt16 BitLen;
    1.96 +  UInt16 Flags;
    1.97 +
    1.98 +  bool ParseFromSubBlock(const CExtraSubBlock &sb)
    1.99 +  {
   1.100 +    if (sb.ID != NFileHeader::NExtraID::kStrongEncrypt)
   1.101 +      return false;
   1.102 +    const Byte *p = (const Byte *)sb.Data;
   1.103 +    if (sb.Data.GetCapacity() < 8)
   1.104 +      return false;
   1.105 +    Format = (((UInt16)p[1]) << 8) | p[0];
   1.106 +    AlgId  = (((UInt16)p[3]) << 8) | p[2];
   1.107 +    BitLen = (((UInt16)p[5]) << 8) | p[4];
   1.108 +    Flags  = (((UInt16)p[7]) << 8) | p[6];
   1.109 +    return (Format == 2);
   1.110 +  }
   1.111 +};
   1.112 +
   1.113 +struct CExtraBlock
   1.114 +{
   1.115 +  CObjectVector<CExtraSubBlock> SubBlocks;
   1.116 +  void Clear() { SubBlocks.Clear(); }
   1.117 +  size_t GetSize() const
   1.118 +  {
   1.119 +    size_t res = 0;
   1.120 +    for (int i = 0; i < SubBlocks.Size(); i++)
   1.121 +      res += SubBlocks[i].Data.GetCapacity() + 2 + 2;
   1.122 +    return res;
   1.123 +  }
   1.124 +  bool GetWzAesField(CWzAesExtraField &aesField) const
   1.125 +  {
   1.126 +    for (int i = 0; i < SubBlocks.Size(); i++)
   1.127 +      if (aesField.ParseFromSubBlock(SubBlocks[i]))
   1.128 +        return true;
   1.129 +    return false;
   1.130 +  }
   1.131 +
   1.132 +  bool GetStrongCryptoField(CStrongCryptoField &f) const
   1.133 +  {
   1.134 +    for (int i = 0; i < SubBlocks.Size(); i++)
   1.135 +      if (f.ParseFromSubBlock(SubBlocks[i]))
   1.136 +        return true;
   1.137 +    return false;
   1.138 +  }
   1.139 +
   1.140 +  bool HasWzAesField() const
   1.141 +  {
   1.142 +    CWzAesExtraField aesField;
   1.143 +    return GetWzAesField(aesField);
   1.144 +  }
   1.145 +
   1.146 +  bool GetNtfsTime(int index, FILETIME &ft) const
   1.147 +  {
   1.148 +    for (int i = 0; i < SubBlocks.Size(); i++)
   1.149 +    {
   1.150 +      const CExtraSubBlock &sb = SubBlocks[i];
   1.151 +      if (sb.ID == NFileHeader::NExtraID::kNTFS)
   1.152 +        return sb.ExtractNtfsTime(index, ft);
   1.153 +    }
   1.154 +    return false;
   1.155 +  }
   1.156 +
   1.157 +  /*
   1.158 +  bool HasStrongCryptoField() const
   1.159 +  {
   1.160 +    CStrongCryptoField f;
   1.161 +    return GetStrongCryptoField(f);
   1.162 +  }
   1.163 +  */
   1.164 +
   1.165 +  void RemoveUnknownSubBlocks()
   1.166 +  {
   1.167 +    for (int i = SubBlocks.Size() - 1; i >= 0; i--)
   1.168 +      if (SubBlocks[i].ID != NFileHeader::NExtraID::kWzAES)
   1.169 +        SubBlocks.Delete(i);
   1.170 +  }
   1.171 +};
   1.172 +
   1.173 +
   1.174 +class CLocalItem
   1.175 +{
   1.176 +public:
   1.177 +  CVersion ExtractVersion;
   1.178 +  UInt16 Flags;
   1.179 +  UInt16 CompressionMethod;
   1.180 +  UInt32 Time;
   1.181 +  UInt32 FileCRC;
   1.182 +  UInt64 PackSize;
   1.183 +  UInt64 UnPackSize;
   1.184 +  
   1.185 +  AString Name;
   1.186 +
   1.187 +  CExtraBlock LocalExtra;
   1.188 +
   1.189 +  bool IsUtf8() const { return (Flags & NFileHeader::NFlags::kUtf8) != 0; }
   1.190 +  
   1.191 +  bool IsEncrypted() const { return (Flags & NFileHeader::NFlags::kEncrypted) != 0; }
   1.192 +  bool IsStrongEncrypted() const { return IsEncrypted() && (Flags & NFileHeader::NFlags::kStrongEncrypted) != 0; };
   1.193 +  
   1.194 +  bool IsLzmaEOS() const { return (Flags & NFileHeader::NFlags::kLzmaEOS) != 0; }
   1.195 +  
   1.196 +  bool IsDir() const;
   1.197 +  bool IgnoreItem() const { return false; }
   1.198 +  UInt32 GetWinAttributes() const;
   1.199 +  
   1.200 +  bool HasDescriptor() const  { return (Flags & NFileHeader::NFlags::kDescriptorUsedMask) != 0; }
   1.201 +
   1.202 +  UString GetUnicodeString(const AString &s) const
   1.203 +  {
   1.204 +    UString res;
   1.205 +    if (IsUtf8())
   1.206 +      if (!ConvertUTF8ToUnicode(s, res))
   1.207 +        res.Empty();
   1.208 +    if (res.IsEmpty())
   1.209 +      res = MultiByteToUnicodeString(s, GetCodePage());
   1.210 +    return res;
   1.211 +  }
   1.212 +  
   1.213 +private:
   1.214 +  void SetFlagBits(int startBitNumber, int numBits, int value);
   1.215 +  void SetBitMask(int bitMask, bool enable);
   1.216 +public:
   1.217 +  void ClearFlags() { Flags = 0; }
   1.218 +  void SetEncrypted(bool encrypted);
   1.219 +  void SetUtf8(bool isUtf8);
   1.220 +
   1.221 +  WORD GetCodePage() const { return  CP_OEMCP; }
   1.222 +};
   1.223 +
   1.224 +class CItem: public CLocalItem
   1.225 +{
   1.226 +public:
   1.227 +  CVersion MadeByVersion;
   1.228 +  UInt16 InternalAttributes;
   1.229 +  UInt32 ExternalAttributes;
   1.230 +  
   1.231 +  UInt64 LocalHeaderPosition;
   1.232 +  
   1.233 +  FILETIME NtfsMTime;
   1.234 +  FILETIME NtfsATime;
   1.235 +  FILETIME NtfsCTime;
   1.236 +
   1.237 +  CExtraBlock CentralExtra;
   1.238 +  CByteBuffer Comment;
   1.239 +
   1.240 +  bool FromLocal;
   1.241 +  bool FromCentral;
   1.242 +  bool NtfsTimeIsDefined;
   1.243 +  
   1.244 +  bool IsDir() const;
   1.245 +  UInt32 GetWinAttributes() const;
   1.246 +
   1.247 +  bool IsThereCrc() const
   1.248 +  {
   1.249 +    if (CompressionMethod == NFileHeader::NCompressionMethod::kWzAES)
   1.250 +    {
   1.251 +      CWzAesExtraField aesField;
   1.252 +      if (CentralExtra.GetWzAesField(aesField))
   1.253 +        return aesField.NeedCrc();
   1.254 +    }
   1.255 +    return (FileCRC != 0 || !IsDir());
   1.256 +  }
   1.257 +  
   1.258 +  WORD GetCodePage() const
   1.259 +  {
   1.260 +    return (WORD)((MadeByVersion.HostOS == NFileHeader::NHostOS::kFAT
   1.261 +        || MadeByVersion.HostOS == NFileHeader::NHostOS::kNTFS
   1.262 +        ) ? CP_OEMCP : CP_ACP);
   1.263 +  }
   1.264 +  CItem() : FromLocal(false), FromCentral(false), NtfsTimeIsDefined(false) {}
   1.265 +};
   1.266 +
   1.267 +}}
   1.268 +
   1.269 +#endif
   1.270 +
   1.271 +