unrarsrc  6.1.7
About: unrar extracts, views and tests the contents of archives created with the RAR archiver.
  Fossies Dox: unrarsrc-6.1.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

unpack.hpp
Go to the documentation of this file.
1#ifndef _RAR_UNPACK_
2#define _RAR_UNPACK_
3
4// Maximum allowed number of compressed bits processed in quick mode.
5#define MAX_QUICK_DECODE_BITS 10
6
7// Maximum number of filters per entire data block. Must be at least
8// twice more than MAX_PACK_FILTERS to store filters from two data blocks.
9#define MAX_UNPACK_FILTERS 8192
10
11// Maximum number of filters per entire data block for RAR3 unpack.
12// Must be at least twice more than v3_MAX_PACK_FILTERS to store filters
13// from two data blocks.
14#define MAX3_UNPACK_FILTERS 8192
15
16// Limit maximum number of channels in RAR3 delta filter to some reasonable
17// value to prevent too slow processing of corrupt archives with invalid
18// channels number. Must be equal or larger than v3_MAX_FILTER_CHANNELS.
19// No need to provide it for RAR5, which uses only 5 bits to store channels.
20#define MAX3_UNPACK_CHANNELS 1024
21
22// Maximum size of single filter block. We restrict it to limit memory
23// allocation. Must be equal or larger than MAX_ANALYZE_SIZE.
24#define MAX_FILTER_BLOCK_SIZE 0x400000
25
26// Write data in 4 MB or smaller blocks. Must not exceed PACK_MAX_READ,
27// so we keep the number of buffered filters in unpacker reasonable.
28#define UNPACK_MAX_WRITE 0x400000
29
30// Decode compressed bit fields to alphabet numbers.
32{
33 // Real size of DecodeNum table.
35
36 // Left aligned start and upper limit codes defining code space
37 // ranges for bit lengths. DecodeLen[BitLength-1] defines the start of
38 // range for bit length and DecodeLen[BitLength] defines next code
39 // after the end of range or in other words the upper limit code
40 // for specified bit length.
42
43 // Every item of this array contains the sum of all preceding items.
44 // So it contains the start position in code list for every bit length.
46
47 // Number of compressed bits processed in quick mode.
48 // Must not exceed MAX_QUICK_DECODE_BITS.
50
51 // Translates compressed bits (up to QuickBits length)
52 // to bit length in quick mode.
54
55 // Translates compressed bits (up to QuickBits length)
56 // to position in alphabet in quick mode.
57 // 'ushort' saves some memory and even provides a little speed gain
58 // comparting to 'uint' here.
60
61 // Translate the position in code list to position in alphabet.
62 // We do not allocate it dynamically to avoid performance overhead
63 // introduced by pointer, so we use the largest possible table size
64 // as array dimension. Real size of this array is defined in MaxNum.
65 // We use this array if compressed bit field is too lengthy
66 // for QuickLen based translation.
67 // 'ushort' saves some memory and even provides a little speed gain
68 // comparting to 'uint' here.
70};
71
72
74{
81};
82
83
85{
86 DecodeTable LD; // Decode literals.
87 DecodeTable DD; // Decode distances.
88 DecodeTable LDD; // Decode lower bits of distances.
89 DecodeTable RD; // Decode repeating distances.
90 DecodeTable BD; // Decode bit lengths in Huffman table.
91};
92
93
94#ifdef RAR_SMP
95enum UNP_DEC_TYPE {
96 UNPDT_LITERAL,UNPDT_MATCH,UNPDT_FULLREP,UNPDT_REP,UNPDT_FILTER
97};
98
99struct UnpackDecodedItem
100{
101 UNP_DEC_TYPE Type;
102 ushort Length;
103 union
104 {
105 uint Distance;
106 byte Literal[4];
107 };
108};
109
110
111struct UnpackThreadData
112{
113 Unpack *UnpackPtr;
114 BitInput Inp;
115 bool HeaderRead;
117 bool TableRead;
118 UnpackBlockTables BlockTables;
119 int DataSize; // Data left in buffer. Can be less than block size.
120 bool DamagedData;
121 bool LargeBlock;
122 bool NoDataLeft; // 'true' if file is read completely.
123 bool Incomplete; // Not entire block was processed, need to read more data.
124
125 UnpackDecodedItem *Decoded;
126 uint DecodedSize;
127 uint DecodedAllocated;
128 uint ThreadNumber; // For debugging.
129
130 UnpackThreadData()
131 :Inp(false)
132 {
133 Decoded=NULL;
134 }
135 ~UnpackThreadData()
136 {
137 if (Decoded!=NULL)
138 free(Decoded);
139 }
140};
141#endif
142
143
145{
146 byte Type;
150// uint Width;
151// byte PosR;
153};
154
155
157{
158 unsigned int BlockStart;
159 unsigned int BlockLength;
161
162 // Position of parent filter in Filters array used as prototype for filter
163 // in PrgStack array. Not defined for filters in Filters array.
164 unsigned int ParentFilter;
165
167};
168
169
170struct AudioVariables // For RAR 2.0 archives only.
171{
172 int K1,K2,K3,K4,K5;
173 int D1,D2,D3,D4;
175 unsigned int Dif[11];
176 unsigned int ByteCount;
178};
179
180
181// We can use the fragmented dictionary in case heap does not have the single
182// large enough memory block. It is slower than normal dictionary.
184{
185 private:
186 enum {MAX_MEM_BLOCKS=32};
187
188 void Reset();
191 public:
194 void Init(size_t WinSize);
195 byte& operator [](size_t Item);
196 void CopyString(uint Length,uint Distance,size_t &UnpPtr,size_t MaxWinMask);
197 void CopyData(byte *Dest,size_t WinPos,size_t Size);
198 size_t GetBlockSize(size_t StartPos,size_t RequiredSize);
199};
200
201
203{
204 private:
205
206 void Unpack5(bool Solid);
207 void Unpack5MT(bool Solid);
208 bool UnpReadBuf();
209 void UnpWriteBuf();
210 byte* ApplyFilter(byte *Data,uint DataSize,UnpackFilter *Flt);
211 void UnpWriteArea(size_t StartPtr,size_t EndPtr);
212 void UnpWriteData(byte *Data,size_t Size);
213 _forceinline uint SlotToLength(BitInput &Inp,uint Slot);
214 void UnpInitData50(bool Solid);
217 void MakeDecodeTables(byte *LengthTable,DecodeTable *Dec,uint Size);
218 _forceinline uint DecodeNumber(BitInput &Inp,DecodeTable *Dec);
220 inline void InsertOldDist(unsigned int Distance);
221 void UnpInitData(bool Solid);
222 _forceinline void CopyString(uint Length,uint Distance);
224 bool ReadFilter(BitInput &Inp,UnpackFilter &Filter);
225 bool AddFilter(UnpackFilter &Filter);
226 bool AddFilter();
227 void InitFilters();
228
231
232#ifdef RAR_SMP
233 void InitMT();
234 bool UnpackLargeBlock(UnpackThreadData &D);
235 bool ProcessDecoded(UnpackThreadData &D);
236
237 ThreadPool *UnpThreadPool;
238 UnpackThreadData *UnpThreadData;
239 uint MaxUserThreads;
240 byte *ReadBufMT;
241#endif
242
245
246 // Filters code, one entry per filter.
248
251
252 // LastDist is necessary only for RAR2 and older with circular OldDist
253 // array. In RAR3 last distance is always stored in OldDist[0].
255
256 size_t UnpPtr,WrPtr;
257
258 // Top border of read packed data.
260
261 // Border to call UnpReadBuf. We use it instead of (ReadTop-C)
262 // for optimization reasons. Ensures that we have C bytes in buffer
263 // unless we are at the end of file.
265
268
270
271 byte *Window;
272
275
276
278
284
285
286/***************************** Unpack v 1.5 *********************************/
287 void Unpack15(bool Solid);
288 void ShortLZ();
289 void LongLZ();
290 void HuffDecode();
291 void GetFlagsBuf();
292 void UnpInitData15(int Solid);
293 void InitHuff();
294 void CorrHuff(ushort *CharSet,byte *NumToPlace);
295 void CopyString15(uint Distance,uint Length);
296 uint DecodeNum(uint Num,uint StartPos,uint *DecTab,uint *PosTab);
297
298 ushort ChSet[256],ChSetA[256],ChSetB[256],ChSetC[256];
299 byte NToPl[256],NToPlB[256],NToPlC[256];
303/***************************** Unpack v 1.5 *********************************/
304
305/***************************** Unpack v 2.0 *********************************/
306 void Unpack20(bool Solid);
307
308 DecodeTable MD[4]; // Decode multimedia data, up to 4 channels.
309
310 unsigned char UnpOldTable20[MC20*4];
314 void CopyString20(uint Length,uint Distance);
315 bool ReadTables20();
316 void UnpWriteBuf20();
317 void UnpInitData20(int Solid);
318 void ReadLastTables();
319 byte DecodeAudio(int Delta);
321/***************************** Unpack v 2.0 *********************************/
322
323/***************************** Unpack v 3.0 *********************************/
325
326 void UnpInitData30(bool Solid);
327 void Unpack29(bool Solid);
328 void InitFilters30(bool Solid);
329 bool ReadEndOfBlock();
330 bool ReadVMCode();
331 bool ReadVMCodePPM();
332 bool AddVMCode(uint FirstByte,byte *Code,uint CodeSize);
333 int SafePPMDecodeChar();
334 bool ReadTables30();
335 bool UnpReadBuf30();
336 void UnpWriteBuf30();
338
340
343
346
347 // If we already read decoding tables for Unpack v2,v3,v5.
348 // We should not use a single variable for all algorithm versions,
349 // because we can have a corrupt archive with one algorithm file
350 // followed by another algorithm file with "solid" flag and we do not
351 // want to reuse tables from one algorithm in another.
353
354 // Virtual machine to execute filters code.
356
357 // Buffer to read VM filters code. We moved it here from AddVMCode
358 // function to reduce time spent in BitInput constructor.
360
361 // Filters code, one entry per filter.
363
364 // Filters stack, several entrances of same filter are possible.
366
367 // Lengths of preceding data blocks, one length of one last block
368 // for every filter. Used to reduce the size required to write
369 // the data block length if lengths are repeating.
371
373/***************************** Unpack v 3.0 *********************************/
374
375 public:
376 Unpack(ComprDataIO *DataIO);
377 ~Unpack();
378 void Init(size_t WinSize,bool Solid);
379 void DoUnpack(uint Method,bool Solid);
381 void SetDestSize(int64 DestSize) {DestUnpSize=DestSize;FileExtracted=false;}
383
384#ifdef RAR_SMP
385 void SetThreads(uint Threads);
386 void UnpackDecode(UnpackThreadData &D);
387#endif
388
391
393 {
395 {
396 UnpReadBuf();
397 if (Inp.InAddr>=BitInput::MAX_SIZE) // If nothing was read.
398 return 0;
399 }
400 return Inp.InBuf[Inp.InAddr++];
401 }
402};
403
404#endif
@ MAX_SIZE
Definition: getbits.hpp:7
int InAddr
Definition: getbits.hpp:9
byte * InBuf
Definition: getbits.hpp:17
byte * Mem[MAX_MEM_BLOCKS]
Definition: unpack.hpp:189
byte & operator[](size_t Item)
void CopyString(uint Length, uint Distance, size_t &UnpPtr, size_t MaxWinMask)
size_t GetBlockSize(size_t StartPos, size_t RequiredSize)
size_t MemSize[MAX_MEM_BLOCKS]
Definition: unpack.hpp:190
void Init(size_t WinSize)
void CopyData(byte *Dest, size_t WinPos, size_t Size)
static const uint LARGEST_TABLE_SIZE
Definition: compress.hpp:43
static const uint MC20
Definition: compress.hpp:40
static const uint HUFF_TABLE_SIZE30
Definition: compress.hpp:34
Definition: rarvm.hpp:26
UnpackBlockHeader BlockHeader
Definition: unpack.hpp:266
byte UnpOldTable[HUFF_TABLE_SIZE30]
Definition: unpack.hpp:344
~Unpack()
Definition: unpack.cpp:48
DecodeTable MD[4]
Definition: unpack.hpp:308
void UnpInitData30(bool Solid)
Definition: unpack30.cpp:738
UnpackBlockTables BlockTables
Definition: unpack.hpp:267
int SafePPMDecodeChar()
Definition: unpack30.cpp:4
bool AddFilter()
bool ReadEndOfBlock()
Definition: unpack30.cpp:257
void UnpInitData(bool Solid)
Definition: unpack.cpp:193
byte NToPl[256]
Definition: unpack.hpp:299
int LowDistRepCount
Definition: unpack.hpp:339
int LastFilter
Definition: unpack.hpp:372
ushort ChSet[256]
Definition: unpack.hpp:298
void Unpack5MT(bool Solid)
Definition: unpack50mt.cpp:56
int PrevLowDist
Definition: unpack.hpp:339
void Unpack20(bool Solid)
Definition: unpack20.cpp:13
bool UnpAllBuf
Definition: unpack.hpp:280
size_t WrPtr
Definition: unpack.hpp:256
int64 WrittenFileSize
Definition: unpack.hpp:282
void Init(size_t WinSize, bool Solid)
Definition: unpack.cpp:73
bool TablesRead2
Definition: unpack.hpp:352
void CopyString()
Array< UnpackFilter30 * > Filters30
Definition: unpack.hpp:362
uint UnpChannels
Definition: unpack.hpp:312
void UnpInitData50(bool Solid)
Definition: unpack50.cpp:528
void DoUnpack(uint Method, bool Solid)
Definition: unpack.cpp:149
int Buf60
Definition: unpack.hpp:301
bool AddVMCode(uint FirstByte, byte *Code, uint CodeSize)
Definition: unpack30.cpp:361
uint UnpCurChannel
Definition: unpack.hpp:312
bool ReadTables20()
Definition: unpack20.cpp:166
void InsertOldDist(unsigned int Distance)
Definition: unpackinline.cpp:1
uint GetChar()
Definition: unpack.hpp:392
uint OldDist[4]
Definition: unpack.hpp:249
bool Suspended
Definition: unpack.hpp:279
void ExecuteCode(VM_PreparedProgram *Prg)
Definition: unpack30.cpp:621
size_t UnpPtr
Definition: unpack.hpp:256
bool UnpReadBuf30()
Definition: unpack30.cpp:491
uint LastDist
Definition: unpack.hpp:254
int UnpBlockType
Definition: unpack.hpp:345
size_t WriteBorder
Definition: unpack.hpp:269
uint FlagBuf
Definition: unpack.hpp:300
bool ReadBlockHeader(BitInput &Inp, UnpackBlockHeader &Header)
Definition: unpack50.cpp:535
int PPMEscChar
Definition: unpack.hpp:342
BitInput Inp
Definition: unpack.hpp:230
bool Fragmented
Definition: unpack.hpp:274
byte * Window
Definition: unpack.hpp:271
void CorrHuff(ushort *CharSet, byte *NumToPlace)
Definition: unpack15.cpp:459
uint ReadFilterData(BitInput &Inp)
Definition: unpack50.cpp:158
void UnpWriteBuf30()
Definition: unpack30.cpp:519
int LCount
Definition: unpack.hpp:301
int UnpChannelDelta
Definition: unpack.hpp:313
void LongLZ()
Definition: unpack15.cpp:229
ModelPPM PPM
Definition: unpack.hpp:341
void HuffDecode()
Definition: unpack15.cpp:321
int NumHuf
Definition: unpack.hpp:301
byte NToPlC[256]
Definition: unpack.hpp:299
Array< byte > FilterDstMemory
Definition: unpack.hpp:244
void UnpInitData20(int Solid)
Definition: unpack20.cpp:271
struct AudioVariables AudV[4]
Definition: unpack.hpp:320
BitInput VMCodeInp
Definition: unpack.hpp:359
bool ReadTables(BitInput &Inp, UnpackBlockHeader &Header, UnpackBlockTables &Tables)
Definition: unpack50.cpp:579
bool ReadTables30()
Definition: unpack30.cpp:628
_forceinline uint SlotToLength(BitInput &Inp, uint Slot)
int ReadBorder
Definition: unpack.hpp:264
void Unpack15(bool Solid)
Definition: unpack15.cpp:40
int FlagsCnt
Definition: unpack.hpp:301
ComprDataIO * UnpIO
Definition: unpack.hpp:229
uint AvrLn1
Definition: unpack.hpp:300
bool UnpSomeRead
Definition: unpack.hpp:281
uint DecodeNum(uint Num, uint StartPos, uint *DecTab, uint *PosTab)
Definition: unpack15.cpp:482
void UnpWriteBuf20()
Definition: unpack20.cpp:150
void SetSuspended(bool Suspended)
Definition: unpack.hpp:382
bool TablesRead5
Definition: unpack.hpp:352
bool FileExtracted
Definition: unpack.hpp:283
unsigned char UnpOldTable20[MC20 *4]
Definition: unpack.hpp:310
uint LastLength
Definition: unpack.hpp:250
uint Nhfb
Definition: unpack.hpp:302
size_t MaxWinSize
Definition: unpack.hpp:389
bool ReadVMCode()
Definition: unpack30.cpp:288
void InitFilters30(bool Solid)
Definition: unpack30.cpp:751
int StMode
Definition: unpack.hpp:301
byte DecodeAudio(int Delta)
Definition: unpack20.cpp:288
Array< UnpackFilter30 * > PrgStack
Definition: unpack.hpp:365
void InitHuff()
Definition: unpack15.cpp:444
bool IsFileExtracted()
Definition: unpack.hpp:380
void UnpWriteBuf()
Definition: unpack50.cpp:255
Array< UnpackFilter > Filters
Definition: unpack.hpp:247
void CopyString20(uint Length, uint Distance)
Definition: unpack20.cpp:3
bool UnpAudioBlock
Definition: unpack.hpp:311
FragmentedWindow FragWindow
Definition: unpack.hpp:273
byte NToPlB[256]
Definition: unpack.hpp:299
BLOCK_TYPES
Definition: unpack.hpp:324
@ BLOCK_LZ
Definition: unpack.hpp:324
@ BLOCK_PPM
Definition: unpack.hpp:324
uint AvrPlcB
Definition: unpack.hpp:300
ushort ChSetB[256]
Definition: unpack.hpp:298
void UnpWriteData(byte *Data, size_t Size)
Definition: unpack50.cpp:515
void SetDestSize(int64 DestSize)
Definition: unpack.hpp:381
bool ReadFilter(BitInput &Inp, UnpackFilter &Filter)
Definition: unpack50.cpp:173
uint OldDistPtr
Definition: unpack.hpp:249
bool ReadVMCodePPM()
Definition: unpack30.cpp:323
uint MaxDist3
Definition: unpack.hpp:302
bool TablesRead3
Definition: unpack.hpp:352
void Unpack29(bool Solid)
Definition: unpack30.cpp:16
ushort ChSetC[256]
Definition: unpack.hpp:298
void ShortLZ()
Definition: unpack15.cpp:117
_forceinline uint DecodeNumber(BitInput &Inp, DecodeTable *Dec)
int64 DestUnpSize
Definition: unpack.hpp:277
Array< byte > FilterSrcMemory
Definition: unpack.hpp:243
void ReadLastTables()
Definition: unpack20.cpp:257
RarVM VM
Definition: unpack.hpp:355
void UnpWriteArea(size_t StartPtr, size_t EndPtr)
Definition: unpack50.cpp:486
ushort ChSetA[256]
Definition: unpack.hpp:298
bool UnpReadBuf()
Definition: unpack50.cpp:217
void GetFlagsBuf()
Definition: unpack15.cpp:399
uint AvrLn3
Definition: unpack.hpp:300
void UnpInitData15(int Solid)
Definition: unpack15.cpp:427
uint AvrLn2
Definition: unpack.hpp:300
uint Nlzb
Definition: unpack.hpp:302
Array< int > OldFilterLengths
Definition: unpack.hpp:370
byte * ApplyFilter(byte *Data, uint DataSize, UnpackFilter *Flt)
Definition: unpack50.cpp:398
void CopyString15(uint Distance, uint Length)
Definition: unpack15.cpp:471
int ReadTop
Definition: unpack.hpp:259
uint AvrPlc
Definition: unpack.hpp:300
size_t MaxWinMask
Definition: unpack.hpp:390
Unpack(ComprDataIO *DataIO)
Definition: unpack.cpp:18
void MakeDecodeTables(byte *LengthTable, DecodeTable *Dec, uint Size)
Definition: unpack.cpp:227
void InitFilters()
Definition: unpack50.cpp:684
void Unpack5(bool Solid)
Definition: unpack50.cpp:1
int64_t int64
Definition: rartypes.hpp:12
unsigned int uint
Definition: rartypes.hpp:8
uint16_t ushort
Definition: rartypes.hpp:7
unsigned int Dif[11]
Definition: unpack.hpp:175
unsigned int ByteCount
Definition: unpack.hpp:176
uint MaxNum
Definition: unpack.hpp:34
uint QuickBits
Definition: unpack.hpp:49
ushort DecodeNum[LARGEST_TABLE_SIZE]
Definition: unpack.hpp:69
uint DecodePos[16]
Definition: unpack.hpp:45
ushort QuickNum[1<< 10]
Definition: unpack.hpp:59
byte QuickLen[1<< 10]
Definition: unpack.hpp:53
uint DecodeLen[16]
Definition: unpack.hpp:41
bool LastBlockInFile
Definition: unpack.hpp:79
DecodeTable DD
Definition: unpack.hpp:87
DecodeTable LDD
Definition: unpack.hpp:88
DecodeTable LD
Definition: unpack.hpp:86
DecodeTable RD
Definition: unpack.hpp:89
DecodeTable BD
Definition: unpack.hpp:90
bool NextWindow
Definition: unpack.hpp:160
unsigned int BlockLength
Definition: unpack.hpp:159
unsigned int ParentFilter
Definition: unpack.hpp:164
unsigned int BlockStart
Definition: unpack.hpp:158
VM_PreparedProgram Prg
Definition: unpack.hpp:166
bool NextWindow
Definition: unpack.hpp:152
uint BlockLength
Definition: unpack.hpp:148
uint BlockStart
Definition: unpack.hpp:147
byte Channels
Definition: unpack.hpp:149
#define MAX_QUICK_DECODE_BITS
Definition: unpack.hpp:5