Upload 2.0.2
[physicsfs] / lzma / CPP / 7zip / Archive / 7z / 7zIn.h
1 // 7zIn.h
2
3 #ifndef __7Z_IN_H
4 #define __7Z_IN_H
5
6 #include "../../../Common/MyCom.h"
7 #include "../../IStream.h"
8 #include "../../IPassword.h"
9 #include "../../Common/CreateCoder.h"
10 #include "../../Common/InBuffer.h"
11
12 #include "7zItem.h"
13  
14 namespace NArchive {
15 namespace N7z {
16   
17 struct CInArchiveInfo
18 {
19   CArchiveVersion Version;
20   UInt64 StartPosition;
21   UInt64 StartPositionAfterHeader;
22   UInt64 DataStartPosition;
23   UInt64 DataStartPosition2;
24   CRecordVector<UInt64> FileInfoPopIDs;
25   void Clear()
26   {
27     FileInfoPopIDs.Clear();
28   }
29 };
30
31 struct CArchiveDatabaseEx: public CArchiveDatabase
32 {
33   CInArchiveInfo ArchiveInfo;
34   CRecordVector<UInt64> PackStreamStartPositions;
35   CRecordVector<CNum> FolderStartPackStreamIndex;
36   CRecordVector<CNum> FolderStartFileIndex;
37   CRecordVector<CNum> FileIndexToFolderIndexMap;
38
39   void Clear()
40   {
41     CArchiveDatabase::Clear();
42     ArchiveInfo.Clear();
43     PackStreamStartPositions.Clear();
44     FolderStartPackStreamIndex.Clear();
45     FolderStartFileIndex.Clear();
46     FileIndexToFolderIndexMap.Clear();
47   }
48
49   void FillFolderStartPackStream();
50   void FillStartPos();
51   void FillFolderStartFileIndex();
52
53   void Fill()
54   {
55     FillFolderStartPackStream();
56     FillStartPos();
57     FillFolderStartFileIndex();
58   }
59   
60   UInt64 GetFolderStreamPos(int folderIndex, int indexInFolder) const
61   {
62     return ArchiveInfo.DataStartPosition +
63         PackStreamStartPositions[FolderStartPackStreamIndex[folderIndex] + indexInFolder];
64   }
65   
66   UInt64 GetFolderFullPackSize(int folderIndex) const 
67   {
68     CNum packStreamIndex = FolderStartPackStreamIndex[folderIndex];
69     const CFolder &folder = Folders[folderIndex];
70     UInt64 size = 0;
71     for (int i = 0; i < folder.PackStreams.Size(); i++)
72       size += PackSizes[packStreamIndex + i];
73     return size;
74   }
75   
76   UInt64 GetFolderPackStreamSize(int folderIndex, int streamIndex) const 
77   {
78     return PackSizes[FolderStartPackStreamIndex[folderIndex] + streamIndex];
79   }
80
81   UInt64 GetFilePackSize(CNum fileIndex) const
82   {
83     CNum folderIndex = FileIndexToFolderIndexMap[fileIndex];
84     if (folderIndex != kNumNoIndex)
85       if (FolderStartFileIndex[folderIndex] == fileIndex)
86         return GetFolderFullPackSize(folderIndex);
87     return 0;
88   }
89 };
90
91 class CInByte2
92 {
93   const Byte *_buffer;
94   size_t _size;
95   size_t _pos;
96 public:
97   void Init(const Byte *buffer, size_t size)
98   {
99     _buffer = buffer;
100     _size = size;
101     _pos = 0;
102   }
103   Byte ReadByte();
104   void ReadBytes(Byte *data, size_t size);
105   void SkeepData(UInt64 size);
106   void SkeepData();
107   UInt64 ReadNumber();
108   CNum ReadNum();
109   UInt32 ReadUInt32();
110   UInt64 ReadUInt64();
111   void ReadString(UString &s);
112 };
113
114 class CStreamSwitch;
115
116 const UInt32 kHeaderSize = 32;
117
118 class CInArchive
119 {
120   friend class CStreamSwitch;
121
122   CMyComPtr<IInStream> _stream;
123
124   CObjectVector<CInByte2> _inByteVector;
125   CInByte2 *_inByteBack;
126  
127   UInt64 _arhiveBeginStreamPosition;
128
129   Byte _header[kHeaderSize];
130
131   void AddByteStream(const Byte *buffer, size_t size)
132   {
133     _inByteVector.Add(CInByte2());
134     _inByteBack = &_inByteVector.Back();
135     _inByteBack->Init(buffer, size);
136   }
137   
138   void DeleteByteStream()
139   {
140     _inByteVector.DeleteBack();
141     if (!_inByteVector.IsEmpty())
142       _inByteBack = &_inByteVector.Back();
143   }
144
145 private:
146   HRESULT FindAndReadSignature(IInStream *stream, const UInt64 *searchHeaderSizeLimit);
147   
148   void ReadBytes(Byte *data, size_t size) { _inByteBack->ReadBytes(data, size); }
149   Byte ReadByte() { return _inByteBack->ReadByte(); }
150   UInt64 ReadNumber() { return _inByteBack->ReadNumber(); }
151   CNum ReadNum() { return _inByteBack->ReadNum(); }
152   UInt64 ReadID() { return _inByteBack->ReadNumber(); }
153   UInt32 ReadUInt32() { return _inByteBack->ReadUInt32(); }
154   UInt64 ReadUInt64() { return _inByteBack->ReadUInt64(); }
155   void SkeepData(UInt64 size) { _inByteBack->SkeepData(size); }
156   void SkeepData() { _inByteBack->SkeepData(); }
157   void WaitAttribute(UInt64 attribute);
158
159   void ReadArchiveProperties(CInArchiveInfo &archiveInfo);
160   void GetNextFolderItem(CFolder &itemInfo);
161   void ReadHashDigests(int numItems,
162       CRecordVector<bool> &digestsDefined, CRecordVector<UInt32> &digests);
163   
164   void ReadPackInfo(
165       UInt64 &dataOffset,
166       CRecordVector<UInt64> &packSizes,
167       CRecordVector<bool> &packCRCsDefined,
168       CRecordVector<UInt32> &packCRCs);
169   
170   void ReadUnPackInfo(
171       const CObjectVector<CByteBuffer> *dataVector,
172       CObjectVector<CFolder> &folders);
173   
174   void ReadSubStreamsInfo(
175       const CObjectVector<CFolder> &folders,
176       CRecordVector<CNum> &numUnPackStreamsInFolders,
177       CRecordVector<UInt64> &unPackSizes,
178       CRecordVector<bool> &digestsDefined, 
179       CRecordVector<UInt32> &digests);
180
181   void ReadStreamsInfo(
182       const CObjectVector<CByteBuffer> *dataVector,
183       UInt64 &dataOffset,
184       CRecordVector<UInt64> &packSizes,
185       CRecordVector<bool> &packCRCsDefined,
186       CRecordVector<UInt32> &packCRCs,
187       CObjectVector<CFolder> &folders,
188       CRecordVector<CNum> &numUnPackStreamsInFolders,
189       CRecordVector<UInt64> &unPackSizes,
190       CRecordVector<bool> &digestsDefined, 
191       CRecordVector<UInt32> &digests);
192
193
194   void ReadBoolVector(int numItems, CBoolVector &v);
195   void ReadBoolVector2(int numItems, CBoolVector &v);
196   void ReadTime(const CObjectVector<CByteBuffer> &dataVector,
197       CObjectVector<CFileItem> &files, UInt32 type);
198   HRESULT ReadAndDecodePackedStreams(
199       DECL_EXTERNAL_CODECS_LOC_VARS
200       UInt64 baseOffset, UInt64 &dataOffset,
201       CObjectVector<CByteBuffer> &dataVector
202       #ifndef _NO_CRYPTO
203       , ICryptoGetTextPassword *getTextPassword
204       #endif
205       );
206   HRESULT ReadHeader(
207       DECL_EXTERNAL_CODECS_LOC_VARS
208       CArchiveDatabaseEx &database
209       #ifndef _NO_CRYPTO
210       ,ICryptoGetTextPassword *getTextPassword
211       #endif
212       );
213   HRESULT ReadDatabase2(
214       DECL_EXTERNAL_CODECS_LOC_VARS
215       CArchiveDatabaseEx &database 
216       #ifndef _NO_CRYPTO
217       ,ICryptoGetTextPassword *getTextPassword
218       #endif
219       );
220 public:
221   HRESULT Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit); // S_FALSE means is not archive
222   void Close();
223
224   HRESULT ReadDatabase(
225       DECL_EXTERNAL_CODECS_LOC_VARS
226       CArchiveDatabaseEx &database 
227       #ifndef _NO_CRYPTO
228       ,ICryptoGetTextPassword *getTextPassword
229       #endif
230       );
231 };
232   
233 }}
234   
235 #endif