ZipFileHeader.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 - 2019 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: http://www.artpol-software.com
14 
20 #if !defined(ZIPARCHIVE_ZIPFILEHEADER_DOT_H)
21 #define ZIPARCHIVE_ZIPFILEHEADER_DOT_H
22 
23 #if _MSC_VER > 1000
24 #pragma once
25 #endif
26 
27 #include "ZipExport.h"
28 #include "ZipStorage.h"
29 #include "ZipAutoBuffer.h"
30 #include "sys/types.h"
31 #include "ZipCompatibility.h"
32 #include "ZipCollections.h"
33 #include "ZipExtraField.h"
34 #include "ZipStringStoreSettings.h"
35 #include "ZipCryptograph.h"
36 #include "BitFlag.h"
37 
38 
39 class CZipCentralDir;
40 
44 class ZIP_API CZipFileHeader
45 {
46  friend class CZipCentralDir;
47  friend class CZipArchive;
48 protected:
49  CZipFileHeader(CZipCentralDir* pCentralDir);
50 public:
58  {
59  sfNone = 0x00,
60 #ifdef _ZIP_UNICODE_CUSTOM
61  sfCustomUnicode = 0x10,
62 #endif
63  sfModified = 0x20
64  };
65 
67 #pragma warning(suppress: 26495)
68  CZipFileHeader(const CZipFileHeader& header)
69  {
70  *this = header;
71  }
72  CZipFileHeader& operator=(const CZipFileHeader& header);
73  virtual ~CZipFileHeader();
74 
81  int PredictFileNameSize() const
82  {
83  if (m_fileName.HasBuffer())
84  {
85  return m_fileName.GetBufferSize();
86  }
87  CZipAutoBuffer buffer;
88  ConvertFileName(buffer);
89  return buffer.GetSize();
90  }
91 
98  int PredictCommentSize() const
99  {
100  if (m_comment.HasBuffer())
101  {
102  return m_comment.GetBufferSize();
103  }
104  CZipAutoBuffer buffer;
105  ConvertComment(buffer);
106  return buffer.GetSize();
107  }
108 
109 
132  const CZipString& GetFileName(bool bClearBuffer = true);
133 
158  bool SetFileName(LPCTSTR lpszFileName, bool bInCentralOnly = false);
159 
177  const CZipString& GetComment(bool bClearBuffer = false);
178 
193  bool SetComment(LPCTSTR lpszComment);
194 
201  bool IsDataDescriptor()const { return (m_uFlag & (WORD) 8) != 0;}
202 
214  WORD GetDataDescriptorSize(const CZipStorage* pStorage) const
215  {
216  return GetDataDescriptorSize(NeedsSignatureInDataDescriptor(pStorage));
217  }
218 
230  WORD GetDataDescriptorSize(bool bConsiderSignature = false) const;
231 
245  ZIP_SIZE_TYPE GetDataSize(bool bReal) const
246  {
247  DWORD uEncrSize = GetEncryptedInfoSize();
248  return bReal ? (m_uComprSize - uEncrSize) : (m_uComprSize + uEncrSize);
249  }
250 
257  DWORD GetEncryptedInfoSize() const
258  {
259  return CZipCryptograph::GetEncryptedInfoSize(m_uEncryptionMethod);
260  }
261 
268  DWORD GetSize() const;
269 
281  DWORD GetLocalSize(bool bReal) const;
282 
290  bool CompressionEfficient()
291  {
292  ZIP_SIZE_TYPE uBefore = m_uUncomprSize;
293  // ignore the length of encryption info
294  ZIP_SIZE_TYPE uAfter = GetDataSize(true);
295  return uAfter <= uBefore;
296  }
297 
304  float GetCompressionRatio()
305  {
306 #if _MSC_VER >= 1300 || !defined(_ZIP_ZIP64)
307  return m_uUncomprSize ? ((float)m_uComprSize * 100 ) / m_uUncomprSize: 0;
308 #else
309  return m_uUncomprSize ? ((float)(__int64)(m_uComprSize) / (float)(__int64)m_uUncomprSize) * 100: 0;
310 #endif
311  }
312 
326  void SetCreationTime(const time_t& ttime){m_tCreationTime = ttime;}
327 
338  time_t GetCreationTime()const{return m_tCreationTime;}
339 
359  void SetModificationTime(const time_t& ttime, bool bFullResolution = false, bool bUseUtcTime = false );
360 
370  time_t GetModificationTime()const;
371 
382  time_t GetLastAccessTime()const{return m_tLastAccessTime;}
383 
397  void SetLastAccessTime(const time_t& ttime){m_tLastAccessTime = ttime;}
398 
413  {
414  return (int)m_iSystemCompatibility;
415  }
416 
430  DWORD GetSystemAttr();
431 
432 
445  bool SetSystemAttr(DWORD uAttr);
446 
459  DWORD GetOriginalAttributes() const {return m_uExternalAttr;}
460 
471  bool IsDirectory();
472 
473 #ifdef _ZIP_UNICODE_CUSTOM
474 
486  {
487  return m_stringSettings;
488  }
489 #endif
490 
502  bool IsEncrypted()const { return m_uEncryptionMethod != CZipCryptograph::encNone;}
503 
510  int GetEncryptionMethod() const {return m_uEncryptionMethod;}
511 
518  bool IsWinZipAesEncryption() const
519  {
520  return CZipCryptograph::IsWinZipAesEncryption(m_uEncryptionMethod);
521  }
522 
529  int GetCompressionLevel() const;
530 
537  bool HasTime() const
538  {
539  return m_uModTime != 0 || m_uModDate != 0;
540  }
541 
551  bool IsModified() const
552  {
553  return m_state.IsSetAny(sfModified);
554  }
555 
556 
563  const ZipArchiveLib::CBitFlag& GetState() const
564  {
565  return m_state;
566  }
567 
568  static char m_gszSignature[];
569  static char m_gszLocalSignature[];
570  unsigned char m_uVersionMadeBy;
572  WORD m_uFlag;
573  WORD m_uMethod;
574  WORD m_uModTime;
575  WORD m_uModDate;
576  DWORD m_uCrc32;
577  ZIP_SIZE_TYPE m_uComprSize;
578  ZIP_SIZE_TYPE m_uUncomprSize;
579  ZIP_VOLUME_TYPE m_uVolumeStart;
581  ZIP_SIZE_TYPE m_uLocalComprSize;
582  ZIP_SIZE_TYPE m_uLocalUncomprSize;
583  ZIP_SIZE_TYPE m_uOffset;
586 protected:
590 
595  DWORD m_uLocalHeaderSize;
596 
598 
599 
600 
613  void SetSystemCompatibility(int iSystemID, bool bUpdateAttr = false)
614  {
615  if (bUpdateAttr)
616  {
617  if ((int)m_iSystemCompatibility != iSystemID)
618  {
619  DWORD uAttr = GetSystemAttr();
620  m_iSystemCompatibility = (char)(iSystemID & 0xFF);
621  SetSystemAttr(uAttr & 0xFFFF);
622  }
623  return;
624  }
625  m_iSystemCompatibility = (char)(iSystemID & 0xFF);
626  }
627 
631  void PrepareStringBuffers()
632  {
633  if (!m_fileName.HasBuffer())
634  {
635  ConvertFileName(m_fileName.m_buffer);
636  }
637  if (!m_comment.HasBuffer())
638  {
639  ConvertComment(m_comment.m_buffer);
640  }
641  }
642 
652  bool CheckDataDescriptor(CZipStorage* pStorage) const;
653 
654 
665  void PrepareData(int iLevel, bool bSegm);
666 
675  void WriteLocal(CZipStorage *pStorage);
676 
690  bool ReadLocal(CZipCentralDir* pCentralDir);
691 
702  DWORD Write(CZipCentralDir* pCentralDir);
703 
715  bool Read(bool bReadSignature);
716 
717 
725  bool CheckLengths(bool local) const
726  {
727  if (m_comment.GetBufferSize() > (int)USHRT_MAX || m_fileName.GetBufferSize() > (int)USHRT_MAX)
728  return false;
729  else if (local)
730  return m_aLocalExtraData.Validate();
731  else
732  return m_aCentralExtraData.Validate();
733  }
734 
741  void WriteCrc32(char* pBuf) const;
742 
743 
751  bool NeedsDataDescriptor() const;
752 
753 
763  void WriteSmallDataDescriptor(char* pDest, bool bLocal = true);
764 
771  void WriteDataDescriptor(CZipStorage* pStorage);
772 
782  bool NeedsSignatureInDataDescriptor(const CZipStorage* pStorage) const
783  {
784  return pStorage->IsSegmented() || IsEncrypted();
785  }
786 
793  void UpdateLocalHeader(CZipStorage* pStorage);
794 
798  void AdjustLocalComprSize()
799  {
800  AdjustLocalComprSize(m_uLocalComprSize);
801  }
802 
809  void AdjustLocalComprSize(ZIP_SIZE_TYPE& uLocalComprSize)
810  {
811  uLocalComprSize += GetEncryptedInfoSize();
812  }
813 
823  static bool VerifySignature(CZipAutoBuffer& buf)
824  {
825  return memcmp(buf, m_gszSignature, 4) == 0;
826  }
827 
834  void UpdateFlag(bool bSegm)
835  {
836  if (bSegm || m_uEncryptionMethod == CZipCryptograph::encStandard)
837  m_uFlag |= 8; // data descriptor present
838 
839  if (IsEncrypted())
840  m_uFlag |= 1; // encrypted file
841  }
842 
843 
844 private:
845 
846  struct StringWithBuffer
847  {
848  StringWithBuffer()
849  {
850  m_pString = NULL;
851  }
852  CZipAutoBuffer m_buffer;
853  StringWithBuffer& operator = (const StringWithBuffer& original)
854  {
855  if (original.HasString())
856  {
857  SetString(original.GetString());
858  }
859  else
860  {
861  ClearString();
862  }
863  m_buffer = original.m_buffer;
864  return *this;
865  }
866  void AllocateString()
867  {
868  ClearString();
869  m_pString = new CZipString(_T(""));
870  }
871  bool HasString() const
872  {
873  return m_pString != NULL;
874  }
875  bool HasBuffer() const
876  {
877  return m_buffer.IsAllocated() && m_buffer.GetSize() > 0;
878  }
879  void ClearString()
880  {
881  if (HasString())
882  {
883  delete m_pString;
884  m_pString = NULL;
885  }
886  }
887  void ClearBuffer()
888  {
889  m_buffer.Release();
890  }
891 
892  const CZipString& GetString() const
893  {
894  ASSERT(HasString());
895  return *m_pString;
896  }
897 
898  CZipString& GetString()
899  {
900  ASSERT(HasString());
901  return *m_pString;
902  }
903 
904  void SetString(LPCTSTR value)
905  {
906  if (!HasString())
907  AllocateString();
908  *m_pString = value;
909  }
910 
911  int GetBufferSize() const
912  {
913  return m_buffer.GetSize();
914  }
915  ~StringWithBuffer()
916  {
917  ClearString();
918  }
919  protected:
920  CZipString* m_pString;
921  };
922 
923  ZipArchiveLib::CBitFlag m_state;
924 
925  void Initialize(CZipCentralDir* pCentralDir);
926 
927  void SetModified(bool bModified = true)
928  {
929  m_state.Change(sfModified, bModified);
930  }
931 
932  void ConvertFileName(CZipAutoBuffer& buffer) const;
933  void ConvertFileName(CZipString& szFileName) const;
934  void ConvertComment(CZipAutoBuffer& buffer) const;
935  void ConvertComment(CZipString& szComment) const;
936 
937  bool UpdateFileNameFlags(const CZipString* szNewFileName, bool bAllowRemoveCDir);
938  bool UpdateCommentFlags(const CZipString* szNewComment);
939  bool UpdateStringsFlags(bool bAllowRemoveCDir)
940  {
941  return UpdateFileNameFlags(NULL, bAllowRemoveCDir) | UpdateCommentFlags(NULL);
942  }
943 
944  UINT GetDefaultFileNameCodePage() const
945  {
946  return ZipCompatibility::GetDefaultNameCodePage(GetSystemCompatibility());
947  }
948 
949  UINT GetDefaultCommentCodePage() const
950  {
951  return ZipCompatibility::GetDefaultCommentCodePage(GetSystemCompatibility());
952  }
953 
954  void ClearFileName();
955 
956  void GetCrcAndSizes(char* pBuffer)const;
957 
958  bool NeedsZip64() const
959  {
960  return m_uComprSize >= UINT_MAX || m_uUncomprSize >= UINT_MAX || m_uVolumeStart >= USHRT_MAX || m_uOffset >= UINT_MAX;
961  }
962 
963  time_t ReadFileTime(const char* buffer);
964  void WriteFileTime(const time_t& ttime, char* buffer, bool bUseUtcTime);
965 
966 
967  void OnNewFileClose(CZipStorage* pStorage)
968  {
969  UpdateLocalHeader(pStorage);
970  WriteDataDescriptor(pStorage);
971  pStorage->Flush();
972  }
973 
974 #ifdef _ZIP_UNICODE_CUSTOM
975  CZipStringStoreSettings m_stringSettings;
976 #endif
977  StringWithBuffer m_fileName;
978  StringWithBuffer m_comment;
979  char m_iSystemCompatibility;
980 };
981 
982 #endif // !defined(ZIPARCHIVE_ZIPFILEHEADER_DOT_H)

The ZipArchive Library Copyright © 2000 - 2019 Artpol Software - Tadeusz Dracz. Generated at Sat Apr 20 2019 18:40:36.