-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpack.hpp
439 lines (353 loc) · 13.1 KB
/
unpack.hpp
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
#ifndef _RAR_UNPACK_
#define _RAR_UNPACK_
// Maximum allowed number of compressed bits processed in quick mode.
#define MAX_QUICK_DECODE_BITS 9
// Maximum number of filters per entire data block. Must be at least
// twice more than MAX_PACK_FILTERS to store filters from two data blocks.
#define MAX_UNPACK_FILTERS 8192
// Maximum number of filters per entire data block for RAR3 unpack.
// Must be at least twice more than v3_MAX_PACK_FILTERS to store filters
// from two data blocks.
#define MAX3_UNPACK_FILTERS 8192
// Limit maximum number of channels in RAR3 delta filter to some reasonable
// value to prevent too slow processing of corrupt archives with invalid
// channels number. Must be equal or larger than v3_MAX_FILTER_CHANNELS.
// No need to provide it for RAR5, which uses only 5 bits to store channels.
#define MAX3_UNPACK_CHANNELS 1024
// Maximum size of single filter block. We restrict it to limit memory
// allocation. Must be equal or larger than MAX_ANALYZE_SIZE.
#define MAX_FILTER_BLOCK_SIZE 0x400000
// Write data in 4 MB or smaller blocks. Must not exceed PACK_MAX_READ,
// so we keep the number of buffered filters in unpacker reasonable.
#define UNPACK_MAX_WRITE 0x400000
// Decode compressed bit fields to alphabet numbers.
struct DecodeTable:PackDef
{
// Real size of DecodeNum table.
uint MaxNum;
// Left aligned start and upper limit codes defining code space
// ranges for bit lengths. DecodeLen[BitLength-1] defines the start of
// range for bit length and DecodeLen[BitLength] defines next code
// after the end of range or in other words the upper limit code
// for specified bit length.
uint DecodeLen[16];
// Every item of this array contains the sum of all preceding items.
// So it contains the start position in code list for every bit length.
uint DecodePos[16];
// Number of compressed bits processed in quick mode.
// Must not exceed MAX_QUICK_DECODE_BITS.
uint QuickBits;
// Translates compressed bits (up to QuickBits length)
// to bit length in quick mode.
byte QuickLen[1<<MAX_QUICK_DECODE_BITS];
// Translates compressed bits (up to QuickBits length)
// to position in alphabet in quick mode.
// 'ushort' saves some memory and even provides a little speed gain
// comparing to 'uint' here.
ushort QuickNum[1<<MAX_QUICK_DECODE_BITS];
// Translate the position in code list to position in alphabet.
// We do not allocate it dynamically to avoid performance overhead
// introduced by pointer, so we use the largest possible table size
// as array dimension. Real size of this array is defined in MaxNum.
// We use this array if compressed bit field is too lengthy
// for QuickLen based translation.
// 'ushort' saves some memory and even provides a little speed gain
// comparting to 'uint' here.
ushort DecodeNum[LARGEST_TABLE_SIZE];
};
struct UnpackBlockHeader
{
int BlockSize;
int BlockBitSize;
int BlockStart;
int HeaderSize;
bool LastBlockInFile;
bool TablePresent;
};
struct UnpackBlockTables
{
DecodeTable LD; // Decode literals.
DecodeTable DD; // Decode distances.
DecodeTable LDD; // Decode lower bits of distances.
DecodeTable RD; // Decode repeating distances.
DecodeTable BD; // Decode bit lengths in Huffman table.
};
#ifdef RAR_SMP
enum UNP_DEC_TYPE {
UNPDT_LITERAL=0,UNPDT_MATCH,UNPDT_FULLREP,UNPDT_REP,UNPDT_FILTER
};
struct UnpackDecodedItem
{
byte Type; // 'byte' instead of enum type to reduce memory use.
ushort Length;
union
{
size_t Distance;
byte Literal[8]; // Store up to 8 chars here to speed up extraction.
};
};
struct UnpackThreadData
{
Unpack *UnpackPtr;
BitInput Inp;
bool HeaderRead;
UnpackBlockHeader BlockHeader;
bool TableRead;
UnpackBlockTables BlockTables;
int DataSize; // Data left in buffer. Can be less than block size.
bool DamagedData;
bool LargeBlock;
bool NoDataLeft; // 'true' if file is read completely.
bool Incomplete; // Not entire block was processed, need to read more data.
UnpackDecodedItem *Decoded;
uint DecodedSize;
uint DecodedAllocated;
uint ThreadNumber; // For debugging.
UnpackThreadData()
:Inp(false)
{
Decoded=NULL;
}
~UnpackThreadData()
{
if (Decoded!=NULL)
free(Decoded);
}
};
#endif
struct UnpackFilter
{
// Groop 'byte' and 'bool' together to reduce the actual struct size.
byte Type;
byte Channels;
bool NextWindow;
size_t BlockStart;
uint BlockLength;
};
struct UnpackFilter30
{
unsigned int BlockStart;
unsigned int BlockLength;
bool NextWindow;
// Position of parent filter in Filters array used as prototype for filter
// in PrgStack array. Not defined for filters in Filters array.
unsigned int ParentFilter;
VM_PreparedProgram Prg;
};
struct AudioVariables // For RAR 2.0 archives only.
{
int K1,K2,K3,K4,K5;
int D1,D2,D3,D4;
int LastDelta;
unsigned int Dif[11];
unsigned int ByteCount;
int LastChar;
};
// We can use the fragmented dictionary in case heap does not have the single
// large enough memory block. It is slower than normal dictionary.
class FragmentedWindow
{
private:
enum {MAX_MEM_BLOCKS=32};
void Reset();
byte *Mem[MAX_MEM_BLOCKS];
size_t MemSize[MAX_MEM_BLOCKS];
size_t LastAllocated;
public:
FragmentedWindow();
~FragmentedWindow();
void Init(size_t WinSize);
byte& operator [](size_t Item);
void CopyString(uint Length,size_t Distance,size_t &UnpPtr,bool FirstWinDone,size_t MaxWinSize);
void CopyData(byte *Dest,size_t WinPos,size_t Size);
size_t GetBlockSize(size_t StartPos,size_t RequiredSize);
size_t GetWinSize() {return LastAllocated;}
};
class Unpack:PackDef
{
private:
void Unpack5(bool Solid);
void Unpack5MT(bool Solid);
bool UnpReadBuf();
void UnpWriteBuf();
byte* ApplyFilter(byte *Data,uint DataSize,UnpackFilter *Flt);
void UnpWriteArea(size_t StartPtr,size_t EndPtr);
void UnpWriteData(byte *Data,size_t Size);
_forceinline uint SlotToLength(BitInput &Inp,uint Slot);
void UnpInitData50(bool Solid);
bool ReadBlockHeader(BitInput &Inp,UnpackBlockHeader &Header);
bool ReadTables(BitInput &Inp,UnpackBlockHeader &Header,UnpackBlockTables &Tables);
void MakeDecodeTables(byte *LengthTable,DecodeTable *Dec,uint Size);
_forceinline uint DecodeNumber(BitInput &Inp,DecodeTable *Dec);
inline void InsertOldDist(size_t Distance);
void UnpInitData(bool Solid);
_forceinline void CopyString(uint Length,size_t Distance);
uint ReadFilterData(BitInput &Inp);
bool ReadFilter(BitInput &Inp,UnpackFilter &Filter);
bool AddFilter(UnpackFilter &Filter);
bool AddFilter();
void InitFilters();
ComprDataIO *UnpIO;
BitInput Inp;
#ifdef RAR_SMP
void InitMT();
bool UnpackLargeBlock(UnpackThreadData &D);
bool ProcessDecoded(UnpackThreadData &D);
ThreadPool *UnpThreadPool;
UnpackThreadData *UnpThreadData;
uint MaxUserThreads;
byte *ReadBufMT;
#endif
LargePageAlloc Alloc;
std::vector<byte> FilterSrcMemory;
std::vector<byte> FilterDstMemory;
// Filters code, one entry per filter.
std::vector<UnpackFilter> Filters;
size_t OldDist[4],OldDistPtr;
uint LastLength;
// LastDist is necessary only for RAR2 and older with circular OldDist
// array. In RAR3 last distance is always stored in OldDist[0].
uint LastDist;
size_t UnpPtr; // Current position in window.
size_t PrevPtr; // UnpPtr value for previous loop iteration.
bool FirstWinDone; // At least one dictionary was processed.
size_t WrPtr; // Last written unpacked data position.
// Top border of read packed data.
int ReadTop;
// Border to call UnpReadBuf. We use it instead of (ReadTop-C)
// for optimization reasons. Ensures that we have C bytes in buffer
// unless we are at the end of file.
int ReadBorder;
UnpackBlockHeader BlockHeader;
UnpackBlockTables BlockTables;
size_t WriteBorder; // Perform write when reaching this border.
byte *Window;
FragmentedWindow FragWindow;
bool Fragmented;
int64 DestUnpSize;
bool Suspended;
bool UnpSomeRead;
int64 WrittenFileSize;
bool FileExtracted;
/***************************** Unpack v 1.5 *********************************/
void Unpack15(bool Solid);
void ShortLZ();
void LongLZ();
void HuffDecode();
void GetFlagsBuf();
void UnpInitData15(bool Solid);
void InitHuff();
void CorrHuff(ushort *CharSet,byte *NumToPlace);
void CopyString15(uint Distance,uint Length);
uint DecodeNum(uint Num,uint StartPos,uint *DecTab,uint *PosTab);
ushort ChSet[256],ChSetA[256],ChSetB[256],ChSetC[256];
byte NToPl[256],NToPlB[256],NToPlC[256];
uint FlagBuf,AvrPlc,AvrPlcB,AvrLn1,AvrLn2,AvrLn3;
int Buf60,NumHuf,StMode,LCount,FlagsCnt;
uint Nhfb,Nlzb,MaxDist3;
/***************************** Unpack v 1.5 *********************************/
/***************************** Unpack v 2.0 *********************************/
void Unpack20(bool Solid);
DecodeTable MD[4]; // Decode multimedia data, up to 4 channels.
unsigned char UnpOldTable20[MC20*4];
bool UnpAudioBlock;
uint UnpChannels,UnpCurChannel;
int UnpChannelDelta;
void CopyString20(uint Length,uint Distance);
bool ReadTables20();
void UnpWriteBuf20();
void UnpInitData20(int Solid);
void ReadLastTables();
byte DecodeAudio(int Delta);
struct AudioVariables AudV[4];
/***************************** Unpack v 2.0 *********************************/
/***************************** Unpack v 3.0 *********************************/
enum BLOCK_TYPES {BLOCK_LZ,BLOCK_PPM};
void UnpInitData30(bool Solid);
void Unpack29(bool Solid);
void InitFilters30(bool Solid);
bool ReadEndOfBlock();
bool ReadVMCode();
bool ReadVMCodePPM();
bool AddVMCode(uint FirstByte,byte *Code,uint CodeSize);
int SafePPMDecodeChar();
bool ReadTables30();
bool UnpReadBuf30();
void UnpWriteBuf30();
void ExecuteCode(VM_PreparedProgram *Prg);
int PrevLowDist,LowDistRepCount;
ModelPPM PPM;
int PPMEscChar;
byte UnpOldTable[HUFF_TABLE_SIZE30];
int UnpBlockType;
// If we already read decoding tables for Unpack v2,v3,v5.
// We should not use a single variable for all algorithm versions,
// because we can have a corrupt archive with one algorithm file
// followed by another algorithm file with "solid" flag and we do not
// want to reuse tables from one algorithm in another.
bool TablesRead2,TablesRead3,TablesRead5;
// Virtual machine to execute filters code.
RarVM VM;
// Buffer to read VM filters code. We moved it here from AddVMCode
// function to reduce time spent in BitInput constructor.
BitInput VMCodeInp;
// Filters code, one entry per filter.
std::vector<UnpackFilter30 *> Filters30;
// Filters stack, several entrances of same filter are possible.
std::vector<UnpackFilter30 *> PrgStack;
// Lengths of preceding data blocks, one length of one last block
// for every filter. Used to reduce the size required to write
// the data block length if lengths are repeating.
std::vector<int> OldFilterLengths;
int LastFilter;
/***************************** Unpack v 3.0 *********************************/
public:
Unpack(ComprDataIO *DataIO);
~Unpack();
void Init(uint64 WinSize,bool Solid);
void AllowLargePages(bool Allow) {Alloc.AllowLargePages(Allow);}
void DoUnpack(uint Method,bool Solid);
bool IsFileExtracted() {return FileExtracted;}
void SetDestSize(int64 DestSize) {DestUnpSize=DestSize;FileExtracted=false;}
void SetSuspended(bool Suspended) {Unpack::Suspended=Suspended;}
#ifdef RAR_SMP
void SetThreads(uint Threads);
void UnpackDecode(UnpackThreadData &D);
#endif
uint64 AllocWinSize;
size_t MaxWinSize;
size_t MaxWinMask;
bool ExtraDist; // Allow distances up to 1 TB.
byte GetChar()
{
if (Inp.InAddr>BitInput::MAX_SIZE-30)
{
UnpReadBuf();
if (Inp.InAddr>=BitInput::MAX_SIZE) // If nothing was read.
return 0;
}
return Inp.InBuf[Inp.InAddr++];
}
// If window position crosses the window beginning, wrap it to window end.
// Replaces &MaxWinMask for non-power 2 window sizes.
// We can't use %WinSize everywhere not only for performance reason,
// but also because C++ % is reminder instead of modulo.
// We need additional checks in the code if WinPos distance from 0
// can exceed MaxWinSize. Alternatively we could add such check here.
inline size_t WrapDown(size_t WinPos)
{
return WinPos >= MaxWinSize ? WinPos + MaxWinSize : WinPos;
}
// If window position crosses the window end, wrap it to window beginning.
// Replaces &MaxWinMask for non-power 2 window sizes.
// Unlike WrapDown, we can use %WinSize here if there was no size_t
// overflow when calculating WinPos.
// We need additional checks in the code if WinPos distance from MaxWinSize
// can be MaxWinSize or more. Alternatively we could add such check here
// or use %WinSize.
inline size_t WrapUp(size_t WinPos)
{
return WinPos >= MaxWinSize ? WinPos - MaxWinSize : WinPos;
}
};
#endif