"Fossies" - the Fresh Open Source Software Archive

Member "p7zip_16.02/CPP/7zip/Archive/Zip/ZipIn.h" (18 May 2016, 8320 Bytes) of package /linux/misc/p7zip_16.02_src_all.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "ZipIn.h": 15.14.1_src_all_vs_16.02_src_all.

    1 // Archive/ZipIn.h
    2 
    3 #ifndef __ZIP_IN_H
    4 #define __ZIP_IN_H
    5 
    6 #include "../../../Common/MyCom.h"
    7 
    8 #include "../../IStream.h"
    9 
   10 #include "../../Common/InBuffer.h"
   11 
   12 #include "ZipHeader.h"
   13 #include "ZipItem.h"
   14 
   15 API_FUNC_IsArc IsArc_Zip(const Byte *p, size_t size);
   16 
   17 namespace NArchive {
   18 namespace NZip {
   19   
   20 class CItemEx: public CItem
   21 {
   22 public:
   23   UInt32 LocalFullHeaderSize; // including Name and Extra
   24   
   25   UInt64 GetLocalFullSize() const
   26     { return LocalFullHeaderSize + PackSize + (HasDescriptor() ? kDataDescriptorSize : 0); }
   27   UInt64 GetDataPosition() const
   28     { return LocalHeaderPos + LocalFullHeaderSize; }
   29 };
   30 
   31 
   32 struct CInArchiveInfo
   33 {
   34   Int64 Base; /* Base offset of start of archive in stream.
   35                  Offsets in headers must be calculated from that Base.
   36                  Base is equal to MarkerPos for normal ZIPs.
   37                  Base can point to PE stub for some ZIP SFXs.
   38                  if CentralDir was read,
   39                    Base can be negative, if start of data is not available,
   40                  if CentralDirs was not read,
   41                    Base = ArcInfo.MarkerPos; */
   42 
   43   /* The following *Pos variables contain absolute offsets in Stream */
   44 
   45   UInt64 MarkerPos;  /* Pos of first signature, it can point to kSpan/kNoSpan signature
   46                         = MarkerPos2      in most archives
   47                         = MarkerPos2 - 4  if there is kSpan/kNoSpan signature */
   48   UInt64 MarkerPos2; // Pos of first local item signature in stream
   49   UInt64 FinishPos;  // Finish pos of archive data in starting volume
   50   UInt64 FileEndPos; // Finish pos of stream
   51 
   52   UInt64 FirstItemRelatOffset; /* Relative offset of first local (read from cd) (relative to Base).
   53                                   = 0 in most archives
   54                                   = size of stub for some SFXs */
   55   bool CdWasRead;
   56   bool IsSpanMode;
   57   bool ThereIsTail;
   58 
   59   // UInt32 BaseVolIndex;
   60 
   61   CByteBuffer Comment;
   62 
   63 
   64   CInArchiveInfo():
   65       Base(0),
   66       MarkerPos(0),
   67       MarkerPos2(0),
   68       FinishPos(0),
   69       FileEndPos(0),
   70       FirstItemRelatOffset(0),
   71       CdWasRead(false),
   72       IsSpanMode(false),
   73       ThereIsTail(false)
   74       // BaseVolIndex(0)
   75       {}
   76   
   77   void Clear()
   78   {
   79     // BaseVolIndex = 0;
   80     Base = 0;
   81     MarkerPos = 0;
   82     MarkerPos2 = 0;
   83     FinishPos = 0;
   84     FileEndPos = 0;
   85     ThereIsTail = false;
   86 
   87     FirstItemRelatOffset = 0;
   88 
   89     CdWasRead = false;
   90     IsSpanMode = false;
   91 
   92     Comment.Free();
   93   }
   94 };
   95 
   96 
   97 struct CCdInfo
   98 {
   99   // 64
  100   UInt16 VersionMade;
  101   UInt16 VersionNeedExtract;
  102 
  103   // old zip
  104   UInt32 ThisDisk;
  105   UInt32 CdDisk;
  106   UInt64 NumEntries_in_ThisDisk;
  107   UInt64 NumEntries;
  108   UInt64 Size;
  109   UInt64 Offset;
  110 
  111   UInt16 CommentSize;
  112 
  113   CCdInfo() { memset(this, 0, sizeof(*this)); }
  114 
  115   void ParseEcd32(const Byte *p);   // (p) includes signature
  116   void ParseEcd64e(const Byte *p);  // (p) exclude signature
  117 };
  118 
  119 
  120 class CVols
  121 {
  122 public:
  123 
  124   struct CSubStreamInfo
  125   {
  126     CMyComPtr<IInStream> Stream;
  127     UInt64 Size;
  128 
  129     CSubStreamInfo(): Size(0) {}
  130   };
  131   
  132   CObjectVector<CSubStreamInfo> Streams;
  133   int StreamIndex;
  134   bool NeedSeek;
  135 
  136   CMyComPtr<IInStream> ZipStream;
  137 
  138   bool StartIsExe;  // is .exe
  139   bool StartIsZ;    // is .zip or .zNN
  140   bool StartIsZip;  // is .zip
  141   bool IsUpperCase;
  142   Int32 StartVolIndex; // = (NN - 1), if StartStream is .zNN
  143 
  144   Int32 StartParsingVol; // if we need local parsing, we must use that stream
  145   unsigned NumVols;
  146 
  147   int EndVolIndex; // index of last volume (ecd volume),
  148                    // -1, if is not multivol
  149 
  150   UString BaseName; // including '.'
  151 
  152   UString MissingName;
  153 
  154   CCdInfo ecd;
  155   bool ecd_wasRead;
  156 
  157   void Clear()
  158   {
  159     StreamIndex = -1;
  160     NeedSeek = false;
  161 
  162 
  163     StartIsExe = false;
  164     StartIsZ = false;
  165     StartIsZip = false;
  166     IsUpperCase = false;
  167 
  168     StartVolIndex = -1;
  169     StartParsingVol = 0;
  170     NumVols = 0;
  171     EndVolIndex = -1;
  172 
  173     BaseName.Empty();
  174     MissingName.Empty();
  175 
  176     ecd_wasRead = false;
  177 
  178     Streams.Clear();
  179     ZipStream.Release();
  180   }
  181 
  182   HRESULT ParseArcName(IArchiveOpenVolumeCallback *volCallback);
  183 
  184   HRESULT Read(void *data, UInt32 size, UInt32 *processedSize);
  185   
  186   UInt64 GetTotalSize() const
  187   {
  188     UInt64 total = 0;
  189     FOR_VECTOR (i, Streams)
  190       total += Streams[i].Size;
  191     return total;
  192   }
  193 };
  194 
  195 
  196 class CVolStream:
  197   public ISequentialInStream,
  198   public CMyUnknownImp
  199 {
  200 public:
  201   CVols *Vols;
  202   
  203   MY_UNKNOWN_IMP1(ISequentialInStream)
  204 
  205   STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
  206 };
  207 
  208 
  209 class CInArchive
  210 {
  211   CInBuffer _inBuffer;
  212   bool _inBufMode;
  213   UInt32 m_Signature;
  214   UInt64 m_Position;
  215 
  216   UInt64 _processedCnt;
  217   
  218   bool CanStartNewVol;
  219 
  220   CMyComPtr<IInStream> StreamRef;
  221   IInStream *Stream;
  222   IInStream *StartStream;
  223 
  224   bool IsArcOpen;
  225 
  226   HRESULT ReadVols2(IArchiveOpenVolumeCallback *volCallback,
  227       unsigned start, int lastDisk, int zipDisk, unsigned numMissingVolsMax, unsigned &numMissingVols);
  228   HRESULT ReadVols();
  229 
  230   HRESULT Seek(UInt64 offset);
  231   HRESULT FindMarker(IInStream *stream, const UInt64 *searchLimit);
  232   HRESULT IncreaseRealPosition(Int64 addValue, bool &isFinished);
  233 
  234   HRESULT ReadBytes(void *data, UInt32 size, UInt32 *processedSize);
  235   void SafeReadBytes(void *data, unsigned size);
  236   void ReadBuffer(CByteBuffer &buffer, unsigned size);
  237   Byte ReadByte();
  238   UInt16 ReadUInt16();
  239   UInt32 ReadUInt32();
  240   UInt64 ReadUInt64();
  241   void Skip(unsigned num);
  242   void Skip64(UInt64 num);
  243   void ReadFileName(unsigned nameSize, AString &dest);
  244 
  245   bool ReadExtra(unsigned extraSize, CExtraBlock &extraBlock,
  246       UInt64 &unpackSize, UInt64 &packSize, UInt64 &localHeaderOffset, UInt32 &diskStartNumber);
  247   bool ReadLocalItem(CItemEx &item);
  248   HRESULT ReadLocalItemDescriptor(CItemEx &item);
  249   HRESULT ReadCdItem(CItemEx &item);
  250   HRESULT TryEcd64(UInt64 offset, CCdInfo &cdInfo);
  251   HRESULT FindCd(bool checkOffsetMode);
  252   HRESULT TryReadCd(CObjectVector<CItemEx> &items, const CCdInfo &cdInfo, UInt64 cdOffset, UInt64 cdSize);
  253   HRESULT ReadCd(CObjectVector<CItemEx> &items, UInt32 &cdDisk, UInt64 &cdOffset, UInt64 &cdSize);
  254   HRESULT ReadLocals(CObjectVector<CItemEx> &localItems);
  255 
  256   HRESULT ReadHeaders2(CObjectVector<CItemEx> &items);
  257 
  258   HRESULT GetVolStream(unsigned vol, UInt64 pos, CMyComPtr<ISequentialInStream> &stream);
  259 public:
  260   CInArchiveInfo ArcInfo;
  261   
  262   bool IsArc;
  263   bool IsZip64;
  264   bool HeadersError;
  265   bool HeadersWarning;
  266   bool ExtraMinorError;
  267   bool UnexpectedEnd;
  268   bool NoCentralDir;
  269 
  270   bool MarkerIsFound;
  271 
  272   bool IsMultiVol;
  273   bool UseDisk_in_SingleVol;
  274   UInt32 EcdVolIndex;
  275 
  276   CVols Vols;
  277  
  278   IArchiveOpenCallback *Callback;
  279 
  280   CInArchive(): Stream(NULL), Callback(NULL), IsArcOpen(false) {}
  281 
  282   UInt64 GetPhySize() const
  283   {
  284     if (IsMultiVol)
  285       return ArcInfo.FinishPos;
  286     else
  287       return ArcInfo.FinishPos - ArcInfo.Base;
  288   }
  289 
  290   UInt64 GetOffset() const
  291   {
  292     if (IsMultiVol)
  293       return 0;
  294     else
  295       return ArcInfo.Base;
  296   }
  297 
  298   
  299   void ClearRefs();
  300   void Close();
  301   HRESULT Open(IInStream *stream, const UInt64 *searchLimit, IArchiveOpenCallback *callback, CObjectVector<CItemEx> &items);
  302   HRESULT ReadHeaders(CObjectVector<CItemEx> &items);
  303 
  304   bool IsOpen() const { return IsArcOpen; }
  305   
  306   bool AreThereErrors() const
  307   {
  308     return HeadersError
  309         || UnexpectedEnd
  310         || !Vols.MissingName.IsEmpty();
  311   }
  312 
  313   bool IsLocalOffsetOK(const CItemEx &item) const
  314   {
  315     if (item.FromLocal)
  316       return true;
  317     return (Int64)GetOffset() + (Int64)item.LocalHeaderPos >= 0;
  318   }
  319 
  320   UInt64 GetEmbeddedStubSize() const
  321   {
  322     if (ArcInfo.CdWasRead)
  323       return ArcInfo.FirstItemRelatOffset;
  324     if (IsMultiVol)
  325       return 0;
  326     return ArcInfo.MarkerPos2 - ArcInfo.Base;
  327   }
  328 
  329 
  330   HRESULT ReadLocalItemAfterCdItem(CItemEx &item, bool &isAvail);
  331   HRESULT ReadLocalItemAfterCdItemFull(CItemEx &item);
  332 
  333   HRESULT GetItemStream(const CItemEx &item, bool seekPackData, CMyComPtr<ISequentialInStream> &stream);
  334 
  335   IInStream *GetBaseStream() { return StreamRef; }
  336 
  337   bool CanUpdate() const
  338   {
  339     if (AreThereErrors()
  340        || IsMultiVol
  341        || ArcInfo.Base < 0
  342        || (Int64)ArcInfo.MarkerPos2 < ArcInfo.Base
  343        || ArcInfo.ThereIsTail
  344        || GetEmbeddedStubSize() != 0)
  345       return false;
  346    
  347     // 7-zip probably can update archives with embedded stubs.
  348     // we just disable that feature for more safety.
  349 
  350     return true;
  351   }
  352 };
  353   
  354 }}
  355   
  356 #endif