-
Notifications
You must be signed in to change notification settings - Fork 43
/
qopen.cpp
300 lines (247 loc) · 6.75 KB
/
qopen.cpp
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
#include "rar.hpp"
QuickOpen::QuickOpen()
{
Buf=NULL;
Init(NULL,false);
}
QuickOpen::~QuickOpen()
{
Close();
delete[] Buf;
}
void QuickOpen::Init(Archive *Arc,bool WriteMode)
{
if (Arc!=NULL) // Unless called from constructor.
Close();
QuickOpen::Arc=Arc;
QuickOpen::WriteMode=WriteMode;
ListStart=NULL;
ListEnd=NULL;
if (Buf==NULL)
Buf=new byte[MaxBufSize];
CurBufSize=0; // Current size of buffered data in write mode.
Loaded=false;
}
void QuickOpen::Close()
{
QuickOpenItem *Item=ListStart;
while (Item!=NULL)
{
QuickOpenItem *Next=Item->Next;
delete[] Item->Header;
delete Item;
Item=Next;
}
}
void QuickOpen::Load(uint64 BlockPos)
{
if (!Loaded)
{
// If loading for the first time, perform additional intialization.
SeekPos=Arc->Tell();
UnsyncSeekPos=false;
int64 SavePos=SeekPos;
Arc->Seek(BlockPos,SEEK_SET);
// If BlockPos points to original main header, we'll have the infinite
// recursion, because ReadHeader() for main header will attempt to load
// QOpen and call QuickOpen::Load again. If BlockPos points to long chain
// of other main headers, we'll have multiple recursive calls of this
// function wasting resources. So we prohibit QOpen temporarily to
// prevent this. ReadHeader() calls QOpen.Init and sets MainHead Locator
// and QOpenOffset fields, so we cannot use them to prohibit QOpen.
Arc->SetProhibitQOpen(true);
size_t ReadSize=Arc->ReadHeader();
Arc->SetProhibitQOpen(false);
if (ReadSize==0 || Arc->GetHeaderType()!=HEAD_SERVICE ||
!Arc->SubHead.CmpName(SUBHEAD_TYPE_QOPEN))
{
Arc->Seek(SavePos,SEEK_SET);
return;
}
QOHeaderPos=Arc->CurBlockPos;
RawDataStart=Arc->Tell();
RawDataSize=Arc->SubHead.UnpSize;
Arc->Seek(SavePos,SEEK_SET);
Loaded=true; // Set only after all file processing calls like Tell, Seek, ReadHeader.
}
if (Arc->SubHead.Encrypted)
{
CommandData *Cmd=Arc->GetCommandData();
#ifndef RAR_NOCRYPT
if (Cmd->Password.IsSet())
Crypt.SetCryptKeys(false,CRYPT_RAR50,&Cmd->Password,Arc->SubHead.Salt,
Arc->SubHead.InitV,Arc->SubHead.Lg2Count,
Arc->SubHead.HashKey,Arc->SubHead.PswCheck);
else
#endif
{
Loaded=false;
return;
}
}
RawDataPos=0;
ReadBufSize=0;
ReadBufPos=0;
LastReadHeader.clear();
LastReadHeaderPos=0;
ReadBuffer();
}
bool QuickOpen::Read(void *Data,size_t Size,size_t &Result)
{
if (!Loaded)
return false;
// Find next suitable cached block.
while (LastReadHeaderPos+LastReadHeader.size()<=SeekPos)
if (!ReadNext())
break;
if (!Loaded)
{
// If something wrong happened, let's set the correct file pointer
// and stop further quick open processing.
if (UnsyncSeekPos)
Arc->File::Seek(SeekPos,SEEK_SET);
return false;
}
if (SeekPos>=LastReadHeaderPos && SeekPos+Size<=LastReadHeaderPos+LastReadHeader.size())
{
memcpy(Data,&LastReadHeader[size_t(SeekPos-LastReadHeaderPos)],Size);
Result=Size;
SeekPos+=Size;
UnsyncSeekPos=true;
}
else
{
if (UnsyncSeekPos)
{
Arc->File::Seek(SeekPos,SEEK_SET);
UnsyncSeekPos=false;
}
int ReadSize=Arc->File::Read(Data,Size);
if (ReadSize<0)
{
Loaded=false;
return false;
}
Result=ReadSize;
SeekPos+=ReadSize;
}
return true;
}
bool QuickOpen::Seek(int64 Offset,int Method)
{
if (!Loaded)
return false;
// Normally we process an archive sequentially from beginning to end,
// so we read quick open data sequentially. But some operations like
// archive updating involve several passes. So if we detect that file
// pointer is moved back, we reload quick open data from beginning.
if (Method==SEEK_SET && (uint64)Offset<SeekPos && (uint64)Offset<LastReadHeaderPos)
Load(QOHeaderPos);
if (Method==SEEK_SET)
SeekPos=Offset;
if (Method==SEEK_CUR)
SeekPos+=Offset;
UnsyncSeekPos=true;
if (Method==SEEK_END)
{
Arc->File::Seek(Offset,SEEK_END);
SeekPos=Arc->File::Tell();
UnsyncSeekPos=false;
}
return true;
}
bool QuickOpen::Tell(int64 *Pos)
{
if (!Loaded)
return false;
*Pos=SeekPos;
return true;
}
uint QuickOpen::ReadBuffer()
{
int64 SavePos=Arc->Tell();
Arc->File::Seek(RawDataStart+RawDataPos,SEEK_SET);
size_t SizeToRead=(size_t)Min(RawDataSize-RawDataPos,MaxBufSize-ReadBufSize);
if (Arc->SubHead.Encrypted)
SizeToRead &= ~CRYPT_BLOCK_MASK;
int ReadSize=0;
if (SizeToRead!=0)
{
ReadSize=Arc->File::Read(Buf+ReadBufSize,SizeToRead);
if (ReadSize<=0)
ReadSize=0;
else
{
#ifndef RAR_NOCRYPT
if (Arc->SubHead.Encrypted)
Crypt.DecryptBlock(Buf+ReadBufSize,ReadSize & ~CRYPT_BLOCK_MASK);
#endif
RawDataPos+=ReadSize;
ReadBufSize+=ReadSize;
}
}
Arc->Seek(SavePos,SEEK_SET);
return ReadSize;
}
// Fill RawRead object from buffer.
bool QuickOpen::ReadRaw(RawRead &Raw)
{
if (MaxBufSize-ReadBufPos<0x100) // We are close to end of buffer.
{
// Ensure that we have enough data to read CRC and header size.
size_t DataLeft=ReadBufSize-ReadBufPos;
memcpy(Buf,Buf+ReadBufPos,DataLeft);
ReadBufPos=0;
ReadBufSize=DataLeft;
ReadBuffer();
}
const size_t FirstReadSize=7;
if (ReadBufPos+FirstReadSize>ReadBufSize)
return false;
Raw.Read(Buf+ReadBufPos,FirstReadSize);
ReadBufPos+=FirstReadSize;
uint SavedCRC=Raw.Get4();
uint SizeBytes=Raw.GetVSize(4);
uint64 BlockSize=Raw.GetV();
int SizeToRead=int(BlockSize);
SizeToRead-=FirstReadSize-SizeBytes-4; // Adjust overread size bytes if any.
if (SizeToRead<0 || SizeBytes==0 || BlockSize==0)
{
Loaded=false; // Invalid data.
return false;
}
// If rest of block data crosses Buf boundary, read it in loop.
while (SizeToRead>0)
{
size_t DataLeft=ReadBufSize-ReadBufPos;
size_t CurSizeToRead=Min(DataLeft,(size_t)SizeToRead);
Raw.Read(Buf+ReadBufPos,CurSizeToRead);
ReadBufPos+=CurSizeToRead;
SizeToRead-=int(CurSizeToRead);
if (SizeToRead>0) // We read the entire buffer and still need more data.
{
ReadBufPos=0;
ReadBufSize=0;
if (ReadBuffer()==0)
return false;
}
}
return SavedCRC==Raw.GetCRC50();
}
// Read next cached header.
bool QuickOpen::ReadNext()
{
RawRead Raw(NULL);
if (!ReadRaw(Raw)) // Read internal quick open header preceding stored block.
return false;
uint Flags=(uint)Raw.GetV();
uint64 Offset=Raw.GetV();
size_t HeaderSize=(size_t)Raw.GetV();
if (HeaderSize>MAX_HEADER_SIZE_RAR5)
return false;
LastReadHeader.resize(HeaderSize);
Raw.GetB(LastReadHeader.data(),HeaderSize);
// Calculate the absolute position as offset from quick open service header.
LastReadHeaderPos=QOHeaderPos-Offset;
return true;
}