Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ZipIn.h « Zip « Archive « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6b349b1d21006dbb52f669c5c5190e3a2c6e9c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Archive/ZipIn.h

#ifndef __ZIP_IN_H
#define __ZIP_IN_H

#include "../../../Common/MyCom.h"

#include "../../IStream.h"

#include "../../Common/InBuffer.h"

#include "ZipHeader.h"
#include "ZipItem.h"

API_FUNC_IsArc IsArc_Zip(const Byte *p, size_t size);

namespace NArchive {
namespace NZip {
  
class CItemEx: public CItem
{
public:
  UInt32 LocalFullHeaderSize; // including Name and Extra
  
  UInt64 GetLocalFullSize() const
    { return LocalFullHeaderSize + PackSize + (HasDescriptor() ? kDataDescriptorSize : 0); }
  UInt64 GetDataPosition() const
    { return LocalHeaderPos + LocalFullHeaderSize; };
};

struct CInArchiveInfo
{
  Int64 Base; /* Base offset of start of archive in stream.
                 Offsets in headers must be calculated from that Base.
                 Base is equal to MarkerPos for normal ZIPs.
                 Base can point to PE stub for some ZIP SFXs.
                 if CentralDir was read,
                   Base can be negative, if start of data is not available,
                 if CentralDirs was not read,
                   Base = ArcInfo.MarkerPos; */

  /* The following *Pos variables contain absolute offsets in Stream */
  UInt64 MarkerPos;  /* Pos of first signature, it can point to PK00 signature
                        = MarkerPos2      in most archives
                        = MarkerPos2 - 4  if there is PK00 signature */
  UInt64 MarkerPos2; // Pos of first local item signature in stream
  UInt64 FinishPos;  // Finish pos of archive data
  UInt64 FileEndPos; // Finish pos of stream

  UInt64 FirstItemRelatOffset; /* Relative offset of first local (read from cd) (relative to Base).
                                  = 0 in most archives
                                  = size of stub for some SFXs */
  bool CdWasRead;

  CByteBuffer Comment;

  CInArchiveInfo(): Base(0), MarkerPos(0), MarkerPos2(0), FinishPos(0), FileEndPos(0),
      FirstItemRelatOffset(0), CdWasRead(false) {}
  
  UInt64 GetPhySize() const { return FinishPos - Base; }
  UInt64 GetEmbeddedStubSize() const
  {
    if (CdWasRead)
      return FirstItemRelatOffset;
    return MarkerPos2 - Base;
  }
  bool ThereIsTail() const { return FileEndPos > FinishPos; }

  void Clear()
  {
    Base = 0;
    MarkerPos = 0;
    MarkerPos2 = 0;
    FinishPos = 0;
    FileEndPos = 0;

    FirstItemRelatOffset = 0;
    CdWasRead = false;

    Comment.Free();
  }
};

struct CProgressVirt
{
  virtual HRESULT SetCompletedLocal(UInt64 numFiles, UInt64 numBytes) = 0;
  virtual HRESULT SetTotalCD(UInt64 numFiles) = 0;
  virtual HRESULT SetCompletedCD(UInt64 numFiles) = 0;
};

struct CCdInfo
{
  UInt64 NumEntries;
  UInt64 Size;
  UInt64 Offset;

  void ParseEcd(const Byte *p);
  void ParseEcd64(const Byte *p);
};

class CInArchive
{
  CInBuffer _inBuffer;
  bool _inBufMode;
  UInt32 m_Signature;
  UInt64 m_Position;
  
  HRESULT Seek(UInt64 offset);
  HRESULT FindAndReadMarker(IInStream *stream, const UInt64 *searchHeaderSizeLimit);
  HRESULT IncreaseRealPosition(UInt64 addValue);

  HRESULT ReadBytes(void *data, UInt32 size, UInt32 *processedSize);
  void SafeReadBytes(void *data, unsigned size);
  void ReadBuffer(CByteBuffer &buffer, unsigned size);
  Byte ReadByte();
  UInt16 ReadUInt16();
  UInt32 ReadUInt32();
  UInt64 ReadUInt64();
  void Skip(unsigned num);
  void Skip64(UInt64 num);
  void ReadFileName(unsigned nameSize, AString &dest);

  bool ReadExtra(unsigned extraSize, CExtraBlock &extraBlock,
      UInt64 &unpackSize, UInt64 &packSize, UInt64 &localHeaderOffset, UInt32 &diskStartNumber);
  bool ReadLocalItem(CItemEx &item);
  HRESULT ReadLocalItemDescriptor(CItemEx &item);
  HRESULT ReadCdItem(CItemEx &item);
  HRESULT TryEcd64(UInt64 offset, CCdInfo &cdInfo);
  HRESULT FindCd(CCdInfo &cdInfo);
  HRESULT TryReadCd(CObjectVector<CItemEx> &items, UInt64 cdOffset, UInt64 cdSize, CProgressVirt *progress);
  HRESULT ReadCd(CObjectVector<CItemEx> &items, UInt64 &cdOffset, UInt64 &cdSize, CProgressVirt *progress);
  HRESULT ReadLocals(CObjectVector<CItemEx> &localItems, CProgressVirt *progress);

  HRESULT ReadHeaders2(CObjectVector<CItemEx> &items, CProgressVirt *progress);
public:
  CInArchiveInfo ArcInfo;
  
  bool IsArc;
  bool IsZip64;
  bool HeadersError;
  bool HeadersWarning;
  bool ExtraMinorError;
  bool UnexpectedEnd;
  bool NoCentralDir;
  
  CMyComPtr<IInStream> Stream;
  
  void Close();
  HRESULT Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit);
  HRESULT ReadHeaders(CObjectVector<CItemEx> &items, CProgressVirt *progress);

  bool IsOpen() const { return Stream != NULL; }
  bool AreThereErrors() const { return HeadersError || UnexpectedEnd; }

  bool IsLocalOffsetOK(const CItemEx &item) const
  {
    if (item.FromLocal)
      return true;
    return /* ArcInfo.Base >= 0 || */ ArcInfo.Base + (Int64)item.LocalHeaderPos >= 0;
  }

  HRESULT ReadLocalItemAfterCdItem(CItemEx &item);
  HRESULT ReadLocalItemAfterCdItemFull(CItemEx &item);

  ISequentialInStream *CreateLimitedStream(UInt64 position, UInt64 size);

  UInt64 GetOffsetInStream(UInt64 offsetFromArc) const { return ArcInfo.Base + offsetFromArc; }

  bool CanUpdate() const
  {
    if (AreThereErrors())
      return false;
    if (ArcInfo.Base < 0)
      return false;
    if ((Int64)ArcInfo.MarkerPos2 < ArcInfo.Base)
      return false;
   
    // 7-zip probably can update archives with embedded stubs.
    // we just disable that feature for more safety.
    if (ArcInfo.GetEmbeddedStubSize() != 0)
      return false;

    if (ArcInfo.ThereIsTail())
      return false;
    return true;
  }
};
  
}}
  
#endif