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: 1498afedb499dc0fd557051f0d5e3f7c9c702f45 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Archive/ZipIn.h

#ifndef __ZIP_IN_H
#define __ZIP_IN_H

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

#include "../../IStream.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
  // int ParentOfAltStream; // -1, if not AltStream
  
  bool DescriptorWasRead;

  CItemEx():
    // ParentOfAltStream(-1),
    DescriptorWasRead(false) {}

  UInt64 GetLocalFullSize() const
    { return LocalFullHeaderSize + GetPackSizeWithDescriptor(); }
  UInt64 GetDataPosition() const
    { return LocalHeaderPos + LocalFullHeaderSize; }

  bool IsBadDescriptor() const
  {
    return !FromCentral && FromLocal && HasDescriptor() && !DescriptorWasRead;
  }
};


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 kSpan/kNoSpan signature
                        = MarkerPos2      in most archives
                        = MarkerPos2 - 4  if there is kSpan/kNoSpan signature */
  UInt64 MarkerPos2; // Pos of first local item signature in stream
  UInt64 FinishPos;  // Finish pos of archive data in starting volume
  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 */


  int MarkerVolIndex;

  bool CdWasRead;
  bool IsSpanMode;
  bool ThereIsTail;

  // UInt32 BaseVolIndex;

  CByteBuffer Comment;


  CInArchiveInfo():
      Base(0),
      MarkerPos(0),
      MarkerPos2(0),
      FinishPos(0),
      FileEndPos(0),
      FirstItemRelatOffset(0),
      MarkerVolIndex(-1),
      CdWasRead(false),
      IsSpanMode(false),
      ThereIsTail(false)
      // BaseVolIndex(0)
      {}
  
  void Clear()
  {
    // BaseVolIndex = 0;
    Base = 0;
    MarkerPos = 0;
    MarkerPos2 = 0;
    FinishPos = 0;
    FileEndPos = 0;
    MarkerVolIndex = -1;
    ThereIsTail = false;

    FirstItemRelatOffset = 0;

    CdWasRead = false;
    IsSpanMode = false;

    Comment.Free();
  }
};


struct CCdInfo
{
  bool IsFromEcd64;
  
  UInt16 CommentSize;
  
  // 64
  UInt16 VersionMade;
  UInt16 VersionNeedExtract;

  // old zip
  UInt32 ThisDisk;
  UInt32 CdDisk;
  UInt64 NumEntries_in_ThisDisk;
  UInt64 NumEntries;
  UInt64 Size;
  UInt64 Offset;

  CCdInfo() { memset(this, 0, sizeof(*this)); IsFromEcd64 = false; }

  void ParseEcd32(const Byte *p);   // (p) includes signature
  void ParseEcd64e(const Byte *p);  // (p) exclude signature

  bool IsEmptyArc() const
  {
    return ThisDisk == 0
        && CdDisk == 0
        && NumEntries_in_ThisDisk == 0
        && NumEntries == 0
        && Size == 0
        && Offset == 0 // test it
    ;
  }
};


struct CVols
{
  struct CSubStreamInfo
  {
    CMyComPtr<IInStream> Stream;
    UInt64 Size;

    HRESULT SeekToStart() const { return Stream->Seek(0, STREAM_SEEK_SET, NULL); }

    CSubStreamInfo(): Size(0) {}
  };
  
  CObjectVector<CSubStreamInfo> Streams;
  
  int StreamIndex;   // -1 for StartStream
                     // -2 for ZipStream at multivol detection code
                     // >=0 volume index in multivol
  
  bool NeedSeek;

  bool DisableVolsSearch;
  bool StartIsExe;  // is .exe
  bool StartIsZ;    // is .zip or .zNN
  bool StartIsZip;  // is .zip
  bool IsUpperCase;
  bool MissingZip;

  bool ecd_wasRead;

  Int32 StartVolIndex; // -1, if unknown vol index
                       // = (NN - 1), if StartStream is .zNN
                       // = 0, if start vol is exe

  Int32 StartParsingVol; // if we need local parsing, we must use that stream
  unsigned NumVols;

  int EndVolIndex; // index of last volume (ecd volume),
                   // -1, if is not multivol

  UString BaseName; // name of archive including '.'
  UString MissingName;

  CMyComPtr<IInStream> ZipStream;

  CCdInfo ecd;

  UInt64 TotalBytesSize; // for MultiVol only

  void ClearRefs()
  {
    Streams.Clear();
    ZipStream.Release();
    TotalBytesSize = 0;
  }

  void Clear()
  {
    StreamIndex = -1;
    NeedSeek = false;

    DisableVolsSearch = false;
    StartIsExe = false;
    StartIsZ = false;
    StartIsZip = false;
    IsUpperCase = false;

    StartVolIndex = -1;
    StartParsingVol = 0;
    NumVols = 0;
    EndVolIndex = -1;

    BaseName.Empty();
    MissingName.Empty();

    MissingZip = false;
    ecd_wasRead = false;

    ClearRefs();
  }

  HRESULT ParseArcName(IArchiveOpenVolumeCallback *volCallback);

  HRESULT Read(void *data, UInt32 size, UInt32 *processedSize);
};


class CVolStream:
  public ISequentialInStream,
  public CMyUnknownImp
{
public:
  CVols *Vols;
  
  MY_UNKNOWN_IMP1(ISequentialInStream)

  STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
};


class CInArchive
{
  CMidBuffer Buffer;
  size_t _bufPos;
  size_t _bufCached;

  UInt64 _streamPos;
  UInt64 _cnt;

  // UInt32 _startLocalFromCd_Disk;
  // UInt64 _startLocalFromCd_Offset;

  size_t GetAvail() const { return _bufCached - _bufPos; }

  void InitBuf() { _bufPos = 0; _bufCached = 0; }
  void DisableBufMode() { InitBuf(); _inBufMode = false; }

  void SkipLookahed(size_t skip)
  {
    _bufPos += skip;
    _cnt += skip;
  }

  UInt64 GetVirtStreamPos() { return _streamPos - _bufCached + _bufPos; }

  bool _inBufMode;

  bool IsArcOpen;
  bool CanStartNewVol;

  UInt32 _signature;

  CMyComPtr<IInStream> StreamRef;
  IInStream *Stream;
  IInStream *StartStream;
  IArchiveOpenCallback *Callback;

  HRESULT Seek_SavePos(UInt64 offset);
  HRESULT SeekToVol(int volIndex, UInt64 offset);

  HRESULT ReadFromCache(Byte *data, unsigned size, unsigned &processed);
  HRESULT ReadFromCache_FALSE(Byte *data, unsigned size);

  HRESULT ReadVols2(IArchiveOpenVolumeCallback *volCallback,
      unsigned start, int lastDisk, int zipDisk, unsigned numMissingVolsMax, unsigned &numMissingVols);
  HRESULT ReadVols();

  HRESULT FindMarker(const UInt64 *searchLimit);
  HRESULT IncreaseRealPosition(UInt64 addValue, bool &isFinished);

  HRESULT LookAhead(size_t minRequiredInBuffer);
  void SafeRead(Byte *data, unsigned size);
  void ReadBuffer(CByteBuffer &buffer, unsigned size);
  // Byte ReadByte();
  // UInt16 ReadUInt16();
  UInt32 ReadUInt32();
  UInt64 ReadUInt64();

  void ReadSignature();

  void Skip(size_t num);
  HRESULT Skip64(UInt64 num, unsigned numFiles);

  bool ReadFileName(unsigned nameSize, AString &dest);

  bool ReadExtra(const CLocalItem &item, unsigned extraSize, CExtraBlock &extra,
      UInt64 &unpackSize, UInt64 &packSize, CItem *cdItem);
  bool ReadLocalItem(CItemEx &item);
  HRESULT FindDescriptor(CItemEx &item, unsigned numFiles);
  HRESULT ReadCdItem(CItemEx &item);
  HRESULT TryEcd64(UInt64 offset, CCdInfo &cdInfo);
  HRESULT FindCd(bool checkOffsetMode);
  HRESULT TryReadCd(CObjectVector<CItemEx> &items, const CCdInfo &cdInfo, UInt64 cdOffset, UInt64 cdSize);
  HRESULT ReadCd(CObjectVector<CItemEx> &items, UInt32 &cdDisk, UInt64 &cdOffset, UInt64 &cdSize);
  HRESULT ReadLocals(CObjectVector<CItemEx> &localItems);

  HRESULT ReadHeaders(CObjectVector<CItemEx> &items);

  HRESULT GetVolStream(unsigned vol, UInt64 pos, CMyComPtr<ISequentialInStream> &stream);

public:
  CInArchiveInfo ArcInfo;
  
  bool IsArc;
  bool IsZip64;

  bool IsApk;
  bool IsCdUnsorted;
  
  bool HeadersError;
  bool HeadersWarning;
  bool ExtraMinorError;
  bool UnexpectedEnd;
  bool LocalsWereRead;
  bool LocalsCenterMerged;
  bool NoCentralDir;
  bool Overflow32bit; // = true, if zip without Zip64 extension support and it has some fields values truncated to 32-bits.
  bool Cd_NumEntries_Overflow_16bit; // = true, if no Zip64 and 16-bit ecd:NumEntries was overflowed.

  bool MarkerIsFound;
  bool MarkerIsSafe;

  bool IsMultiVol;
  bool UseDisk_in_SingleVol;
  UInt32 EcdVolIndex;

  CVols Vols;
 
  CInArchive():
      IsArcOpen(false),
      Stream(NULL),
      StartStream(NULL),
      Callback(NULL)
      {}

  UInt64 GetPhySize() const
  {
    if (IsMultiVol)
      return ArcInfo.FinishPos;
    else
      return (UInt64)((Int64)ArcInfo.FinishPos - ArcInfo.Base);
  }

  UInt64 GetOffset() const
  {
    if (IsMultiVol)
      return 0;
    else
      return (UInt64)ArcInfo.Base;
  }

  
  void ClearRefs();
  void Close();
  HRESULT Open(IInStream *stream, const UInt64 *searchLimit, IArchiveOpenCallback *callback, CObjectVector<CItemEx> &items);

  bool IsOpen() const { return IsArcOpen; }
  
  bool AreThereErrors() const
  {
    return HeadersError
        || UnexpectedEnd
        || !Vols.MissingName.IsEmpty();
  }

  bool IsLocalOffsetOK(const CItemEx &item) const
  {
    if (item.FromLocal)
      return true;
    return (Int64)GetOffset() + (Int64)item.LocalHeaderPos >= 0;
  }

  UInt64 GetEmbeddedStubSize() const
  {
    // it's possible that first item in CD doesn refers to first local item
    // so FirstItemRelatOffset is not first local item

    if (ArcInfo.CdWasRead)
      return ArcInfo.FirstItemRelatOffset;
    if (IsMultiVol)
      return 0;
    return (UInt64)((Int64)ArcInfo.MarkerPos2 - ArcInfo.Base);
  }


  HRESULT CheckDescriptor(const CItemEx &item);
  HRESULT ReadLocalItemAfterCdItem(CItemEx &item, bool &isAvail, bool &headersError);
  HRESULT ReadLocalItemAfterCdItemFull(CItemEx &item);

  HRESULT GetItemStream(const CItemEx &item, bool seekPackData, CMyComPtr<ISequentialInStream> &stream);

  IInStream *GetBaseStream() { return StreamRef; }

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

    return true;
  }
};
  
}}
  
#endif