ZipFileHeader.h
Go to the documentation of this file.
1 
2 // This source file is part of the ZipArchive library source distribution and
3 // is Copyrighted 2000 - 2013 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  CZipFileHeader(const CZipFileHeader& header)
68  {
69  *this = header;
70  }
71  CZipFileHeader& operator=(const CZipFileHeader& header);
72  virtual ~CZipFileHeader();
73 
80  int PredictFileNameSize() const
81  {
82  if (m_fileName.HasBuffer())
83  {
84  return m_fileName.GetBufferSize();
85  }
86  CZipAutoBuffer buffer;
87  ConvertFileName(buffer);
88  return buffer.GetSize();
89  }
90 
97  int PredictCommentSize() const
98  {
99  if (m_comment.HasBuffer())
100  {
101  return m_comment.GetBufferSize();
102  }
103  CZipAutoBuffer buffer;
104  ConvertComment(buffer);
105  return buffer.GetSize();
106  }
107 
108 
131  const CZipString& GetFileName(bool bClearBuffer = true);
132 
157  bool SetFileName(LPCTSTR lpszFileName, bool bInCentralOnly = false);
158 
176  const CZipString& GetComment(bool bClearBuffer = false);
177 
192  bool SetComment(LPCTSTR lpszComment);
193 
200  bool IsDataDescriptor()const { return (m_uFlag & (WORD) 8) != 0;}
201 
213  WORD GetDataDescriptorSize(const CZipStorage* pStorage) const
214  {
215  return GetDataDescriptorSize(NeedsSignatureInDataDescriptor(pStorage));
216  }
217 
229  WORD GetDataDescriptorSize(bool bConsiderSignature = false) const;
230 
244  ZIP_SIZE_TYPE GetDataSize(bool bReal) const
245  {
246  DWORD uEncrSize = GetEncryptedInfoSize();
247  return bReal ? (m_uComprSize - uEncrSize) : (m_uComprSize + uEncrSize);
248  }
249 
256  DWORD GetEncryptedInfoSize() const
257  {
258  return CZipCryptograph::GetEncryptedInfoSize(m_uEncryptionMethod);
259  }
260 
267  DWORD GetSize() const;
268 
280  DWORD GetLocalSize(bool bReal) const;
281 
289  bool CompressionEfficient()
290  {
291  ZIP_SIZE_TYPE uBefore = m_uUncomprSize;
292  // ignore the length of encryption info
293  ZIP_SIZE_TYPE uAfter = GetDataSize(true);
294  return uAfter <= uBefore;
295  }
296 
303  float GetCompressionRatio()
304  {
305 #if _MSC_VER >= 1300 || !defined(_ZIP_ZIP64)
306  return m_uUncomprSize ? ((float)m_uComprSize * 100 ) / m_uUncomprSize: 0;
307 #else
308  return m_uUncomprSize ? ((float)(__int64)(m_uComprSize) / (float)(__int64)m_uUncomprSize) * 100: 0;
309 #endif
310  }
311 
325  void SetCreationTime(const time_t& ttime){m_tCreationTime = ttime;}
326 
337  time_t GetCreationTime()const{return m_tCreationTime;}
338 
356  void SetModificationTime(const time_t& ttime, bool bFullResolution = false );
357 
367  time_t GetModificationTime()const;
368 
379  time_t GetLastAccessTime()const{return m_tLastAccessTime;}
380 
394  void SetLastAccessTime(const time_t& ttime){m_tLastAccessTime = ttime;}
395 
410  {
411  return (int)m_iSystemCompatibility;
412  }
413 
427  DWORD GetSystemAttr();
428 
429 
442  bool SetSystemAttr(DWORD uAttr);
443 
456  DWORD GetOriginalAttributes() const {return m_uExternalAttr;}
457 
468  bool IsDirectory();
469 
470 #ifdef _ZIP_UNICODE_CUSTOM
471 
483  {
484  return m_stringSettings;
485  }
486 #endif
487 
499  bool IsEncrypted()const { return m_uEncryptionMethod != CZipCryptograph::encNone;}
500 
507  int GetEncryptionMethod() const {return m_uEncryptionMethod;}
508 
515  bool IsWinZipAesEncryption() const
516  {
517  return CZipCryptograph::IsWinZipAesEncryption(m_uEncryptionMethod);
518  }
519 
526  int GetCompressionLevel() const;
527 
534  bool HasTime() const
535  {
536  return m_uModTime != 0 || m_uModDate != 0;
537  }
538 
548  bool IsModified() const
549  {
550  return m_state.IsSetAny(sfModified);
551  }
552 
553 
560  const ZipArchiveLib::CBitFlag& GetState() const
561  {
562  return m_state;
563  }
564 
565  static char m_gszSignature[];
566  static char m_gszLocalSignature[];
567  unsigned char m_uVersionMadeBy;
569  WORD m_uFlag;
570  WORD m_uMethod;
571  WORD m_uModTime;
572  WORD m_uModDate;
573  DWORD m_uCrc32;
574  ZIP_SIZE_TYPE m_uComprSize;
575  ZIP_SIZE_TYPE m_uUncomprSize;
576  ZIP_VOLUME_TYPE m_uVolumeStart;
578  ZIP_SIZE_TYPE m_uLocalComprSize;
579  ZIP_SIZE_TYPE m_uLocalUncomprSize;
580  ZIP_SIZE_TYPE m_uOffset;
583 protected:
587 
592  DWORD m_uLocalHeaderSize;
593 
595 
596 
597 
610  void SetSystemCompatibility(int iSystemID, bool bUpdateAttr = false)
611  {
612  if (bUpdateAttr)
613  {
614  if ((int)m_iSystemCompatibility != iSystemID)
615  {
616  DWORD uAttr = GetSystemAttr();
617  m_iSystemCompatibility = (char)(iSystemID & 0xFF);
618  SetSystemAttr(uAttr & 0xFFFF);
619  }
620  return;
621  }
622  m_iSystemCompatibility = (char)(iSystemID & 0xFF);
623  }
624 
628  void PrepareStringBuffers()
629  {
630  if (!m_fileName.HasBuffer())
631  {
632  ConvertFileName(m_fileName.m_buffer);
633  }
634  if (!m_comment.HasBuffer())
635  {
636  ConvertComment(m_comment.m_buffer);
637  }
638  }
639 
649  bool CheckDataDescriptor(CZipStorage* pStorage) const;
650 
651 
662  void PrepareData(int iLevel, bool bSegm);
663 
672  void WriteLocal(CZipStorage *pStorage);
673 
687  bool ReadLocal(CZipCentralDir* pCentralDir);
688 
699  DWORD Write(CZipStorage *pStorage);
700 
712  bool Read(bool bReadSignature);
713 
714 
722  bool CheckLengths(bool local) const
723  {
724  if (m_comment.GetBufferSize() > (int)USHRT_MAX || m_fileName.GetBufferSize() > (int)USHRT_MAX)
725  return false;
726  else if (local)
727  return m_aLocalExtraData.Validate();
728  else
729  return m_aCentralExtraData.Validate();
730  }
731 
738  void WriteCrc32(char* pBuf) const;
739 
740 
748  bool NeedsDataDescriptor() const;
749 
750 
760  void WriteSmallDataDescriptor(char* pDest, bool bLocal = true);
761 
768  void WriteDataDescriptor(CZipStorage* pStorage);
769 
779  bool NeedsSignatureInDataDescriptor(const CZipStorage* pStorage) const
780  {
781  return pStorage->IsSegmented() || IsEncrypted();
782  }
783 
790  void UpdateLocalHeader(CZipStorage* pStorage);
791 
795  void AdjustLocalComprSize()
796  {
797  AdjustLocalComprSize(m_uLocalComprSize);
798  }
799 
806  void AdjustLocalComprSize(ZIP_SIZE_TYPE& uLocalComprSize)
807  {
808  uLocalComprSize += GetEncryptedInfoSize();
809  }
810 
820  static bool VerifySignature(CZipAutoBuffer& buf)
821  {
822  return memcmp(buf, m_gszSignature, 4) == 0;
823  }
824 
831  void UpdateFlag(bool bSegm)
832  {
833  if (bSegm || m_uEncryptionMethod == CZipCryptograph::encStandard)
834  m_uFlag |= 8; // data descriptor present
835 
836  if (IsEncrypted())
837  m_uFlag |= 1; // encrypted file
838  }
839 
840 
841 private:
842 
843  struct StringWithBuffer
844  {
845  StringWithBuffer()
846  {
847  m_pString = NULL;
848  }
849  CZipAutoBuffer m_buffer;
850  StringWithBuffer& operator = (const StringWithBuffer& original)
851  {
852  if (original.HasString())
853  {
854  SetString(original.GetString());
855  }
856  else
857  {
858  ClearString();
859  }
860  m_buffer = original.m_buffer;
861  return *this;
862  }
863  void AllocateString()
864  {
865  ClearString();
866  m_pString = new CZipString(_T(""));
867  }
868  bool HasString() const
869  {
870  return m_pString != NULL;
871  }
872  bool HasBuffer() const
873  {
874  return m_buffer.IsAllocated() && m_buffer.GetSize() > 0;
875  }
876  void ClearString()
877  {
878  if (HasString())
879  {
880  delete m_pString;
881  m_pString = NULL;
882  }
883  }
884  void ClearBuffer()
885  {
886  m_buffer.Release();
887  }
888 
889  const CZipString& GetString() const
890  {
891  ASSERT(HasString());
892  return *m_pString;
893  }
894 
895  CZipString& GetString()
896  {
897  ASSERT(HasString());
898  return *m_pString;
899  }
900 
901  void SetString(LPCTSTR value)
902  {
903  if (!HasString())
904  AllocateString();
905  *m_pString = value;
906  }
907 
908  int GetBufferSize() const
909  {
910  return m_buffer.GetSize();
911  }
912  ~StringWithBuffer()
913  {
914  ClearString();
915  }
916  protected:
917  CZipString* m_pString;
918  };
919 
920  ZipArchiveLib::CBitFlag m_state;
921 
922  void Initialize(CZipCentralDir* pCentralDir);
923 
924  void SetModified(bool bModified = true)
925  {
926  m_state.Change(sfModified, bModified);
927  }
928 
929  void ConvertFileName(CZipAutoBuffer& buffer) const;
930  void ConvertFileName(CZipString& szFileName) const;
931  void ConvertComment(CZipAutoBuffer& buffer) const;
932  void ConvertComment(CZipString& szComment) const;
933 
934  bool UpdateFileNameFlags(const CZipString* szNewFileName, bool bAllowRemoveCDir);
935  bool UpdateCommentFlags(const CZipString* szNewComment);
936  bool UpdateStringsFlags(bool bAllowRemoveCDir)
937  {
938  return UpdateFileNameFlags(NULL, bAllowRemoveCDir) | UpdateCommentFlags(NULL);
939  }
940 
941  UINT GetDefaultFileNameCodePage() const
942  {
943  return ZipCompatibility::GetDefaultNameCodePage(GetSystemCompatibility());
944  }
945 
946  UINT GetDefaultCommentCodePage() const
947  {
948  return ZipCompatibility::GetDefaultCommentCodePage(GetSystemCompatibility());
949  }
950 
951  void ClearFileName();
952 
953  void GetCrcAndSizes(char* pBuffer)const;
954 
955  bool NeedsZip64() const
956  {
957  return m_uComprSize >= UINT_MAX || m_uUncomprSize >= UINT_MAX || m_uVolumeStart >= USHRT_MAX || m_uOffset >= UINT_MAX;
958  }
959 
960  time_t ReadFileTime(const char* buffer);
961  void WriteFileTime(const time_t& ttime, char* buffer);
962 
963 
964  void OnNewFileClose(CZipStorage* pStorage)
965  {
966  UpdateLocalHeader(pStorage);
967  WriteDataDescriptor(pStorage);
968  pStorage->Flush();
969  }
970 
971 #ifdef _ZIP_UNICODE_CUSTOM
972  CZipStringStoreSettings m_stringSettings;
973 #endif
974  StringWithBuffer m_fileName;
975  StringWithBuffer m_comment;
976  char m_iSystemCompatibility;
977 };
978 
979 #endif // !defined(ZIPARCHIVE_ZIPFILEHEADER_DOT_H)

The ZipArchive Library Copyright © 2000 - 2013 Artpol Software - Tadeusz Dracz. Generated at Mon Feb 25 2013 16:29:21.