xref: /aosp_15_r20/external/lzma/CPP/7zip/Compress/Rar2Decoder.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // Rar2Decoder.h
2 // According to unRAR license, this code may not be used to develop
3 // a program that creates RAR archives
4 
5 #ifndef ZIP7_INC_COMPRESS_RAR2_DECODER_H
6 #define ZIP7_INC_COMPRESS_RAR2_DECODER_H
7 
8 #include "../../Common/MyCom.h"
9 
10 #include "../ICoder.h"
11 
12 #include "../Common/InBuffer.h"
13 
14 #include "BitmDecoder.h"
15 #include "HuffmanDecoder.h"
16 #include "LzOutWindow.h"
17 
18 namespace NCompress {
19 namespace NRar2 {
20 
21 const unsigned kNumReps = 4;
22 const unsigned kDistTableSize = 48;
23 const unsigned kNumLen2Symbols = 8;
24 const unsigned kLenTableSize = 28;
25 const unsigned kMainTableSize = 256 + 2 + kNumReps + kNumLen2Symbols + kLenTableSize;
26 const unsigned kHeapTablesSizesSum = kMainTableSize + kDistTableSize + kLenTableSize;
27 const unsigned k_MM_TableSize = 256 + 1;
28 const unsigned k_MM_NumChanelsMax = 4;
29 const unsigned k_MM_TablesSizesSum = k_MM_TableSize * k_MM_NumChanelsMax;
30 const unsigned kMaxTableSize = k_MM_TablesSizesSum;
31 
32 namespace NMultimedia {
33 
34 struct CFilter
35 {
36   int K1,K2,K3,K4,K5;
37   int D1,D2,D3,D4;
38   int LastDelta;
39   UInt32 Dif[11];
40   UInt32 ByteCount;
41   int LastChar;
42 
InitCFilter43   void Init() { memset(this, 0, sizeof(*this)); }
44   Byte Decode(int &channelDelta, Byte delta);
45 };
46 
47 struct CFilter2
48 {
49   CFilter  m_Filters[k_MM_NumChanelsMax];
50   int m_ChannelDelta;
51   unsigned CurrentChannel;
52 
InitCFilter253   void Init() { memset(this, 0, sizeof(*this)); }
DecodeCFilter254   Byte Decode(Byte delta)
55   {
56     return m_Filters[CurrentChannel].Decode(m_ChannelDelta, delta);
57   }
58 };
59 
60 }
61 
62 typedef NBitm::CDecoder<CInBuffer> CBitDecoder;
63 
64 const unsigned kNumHufBits = 15;
65 
66 Z7_CLASS_IMP_NOQIB_2(
67   CDecoder
68   , ICompressCoder
69   , ICompressSetDecoderProperties2
70 )
71   bool _isSolid;
72   bool _solidAllowed;
73   bool m_TablesOK;
74   bool m_AudioMode;
75 
76   CLzOutWindow m_OutWindowStream;
77   CBitDecoder m_InBitStream;
78 
79   UInt32 m_RepDistPtr;
80   UInt32 m_RepDists[kNumReps];
81   UInt32 m_LastLength;
82   unsigned m_NumChannels;
83 
84   NHuffman::CDecoder<kNumHufBits, kMainTableSize, 9> m_MainDecoder;
85   NHuffman::CDecoder256<kNumHufBits, kDistTableSize, 7> m_DistDecoder;
86   NHuffman::CDecoder256<kNumHufBits, kLenTableSize, 7> m_LenDecoder;
87   NHuffman::CDecoder<kNumHufBits, k_MM_TableSize, 9> m_MMDecoders[k_MM_NumChanelsMax];
88 
89   UInt64 m_PackSize;
90 
91   NMultimedia::CFilter2 m_MmFilter;
92   Byte m_LastLevels[kMaxTableSize];
93 
94   void InitStructures();
95   UInt32 ReadBits(unsigned numBits);
96   bool ReadTables();
97   bool ReadLastTables();
98 
99   bool DecodeMm(UInt32 pos);
100   bool DecodeLz(Int32 pos);
101 
102   HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
103       const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
104 
105 public:
106   CDecoder();
107 };
108 
109 }}
110 
111 #endif
112