xref: /aosp_15_r20/external/lzma/CPP/7zip/Compress/LzxDecoder.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // LzxDecoder.h
2 
3 #ifndef ZIP7_INC_LZX_DECODER_H
4 #define ZIP7_INC_LZX_DECODER_H
5 
6 #include "HuffmanDecoder.h"
7 #include "Lzx.h"
8 
9 namespace NCompress {
10 namespace NLzx {
11 
12 const unsigned kAdditionalOutputBufSize = 32 * 2;
13 
14 const unsigned kNumTableBits_Main = 11;
15 const unsigned kNumTableBits_Len = 8;
16 
17 // if (kNumLenSymols_Big <= 256) we can  use NHuffman::CDecoder256
18 // if (kNumLenSymols_Big >  256) we must use NHuffman::CDecoder
19 // const unsigned kNumLenSymols_Big_Start = kNumLenSlots - 1 + kMatchMinLen;  // 8 - 1 + 2
20 const unsigned kNumLenSymols_Big_Start = 0;
21 // const unsigned kNumLenSymols_Big_Start = 0;
22 const unsigned kNumLenSymols_Big = kNumLenSymols_Big_Start + kNumLenSymbols;
23 
24 #if 1
25   // for smallest structure size:
26   const unsigned kPosSlotOffset = 0;
27 #else
28   // use virtual entries for mispredicted branches:
29   const unsigned kPosSlotOffset = 256 / kNumLenSlots;
30 #endif
31 
32 class CBitByteDecoder;
33 
34 class CDecoder
35 {
36 public:
37   UInt32 _pos;
38   UInt32 _winSize;
39   Byte *_win;
40 
41   bool _overDict;
42   bool _isUncompressedBlock;
43   bool _skipByte;
44   bool _keepHistory;
45   bool _keepHistoryForNext;
46   bool _needAlloc;
47   bool _wimMode;
48   Byte _numDictBits;
49 
50   // unsigned _numAlignBits_PosSlots;
51   // unsigned _numAlignBits;
52   UInt32 _numAlignBits_Dist;
53 private:
54   unsigned _numPosLenSlots;
55   UInt32 _unpackBlockSize;
56 
57   UInt32 _writePos;
58 
59   UInt32 _x86_translationSize;
60   UInt32 _x86_processedSize;
61   Byte *_x86_buf;
62 
63   Byte *_unpackedData;
64 public:
65   Byte  _extra[kPosSlotOffset + kNumPosSlots];
66   UInt32 _reps[kPosSlotOffset + kNumPosSlots];
67 
68   NHuffman::CDecoder<kNumHuffmanBits, kMainTableSize, kNumTableBits_Main> _mainDecoder;
69   NHuffman::CDecoder256<kNumHuffmanBits, kNumLenSymols_Big, kNumTableBits_Len> _lenDecoder;
70   NHuffman::CDecoder7b<kAlignTableSize> _alignDecoder;
71 private:
72   Byte _mainLevels[kMainTableSize];
73   Byte _lenLevels[kNumLenSymols_Big];
74 
75   HRESULT Flush() throw();
76   bool ReadTables(CBitByteDecoder &_bitStream) throw();
77 
78   HRESULT CodeSpec(const Byte *inData, size_t inSize, UInt32 outSize) throw();
79   HRESULT SetParams2(unsigned numDictBits) throw();
80 public:
81   CDecoder() throw();
82   ~CDecoder() throw();
83 
Set_WimMode(bool wimMode)84   void Set_WimMode(bool wimMode) { _wimMode = wimMode; }
Set_KeepHistory(bool keepHistory)85   void Set_KeepHistory(bool keepHistory) { _keepHistory = keepHistory; }
Set_KeepHistoryForNext(bool keepHistoryForNext)86   void Set_KeepHistoryForNext(bool keepHistoryForNext) { _keepHistoryForNext = keepHistoryForNext; }
87 
Set_ExternalWindow_DictBits(Byte * win,unsigned numDictBits)88   HRESULT Set_ExternalWindow_DictBits(Byte *win, unsigned numDictBits)
89   {
90     _needAlloc = false;
91     _win = win;
92     _winSize = (UInt32)1 << numDictBits;
93     return SetParams2(numDictBits);
94   }
95   HRESULT Set_DictBits_and_Alloc(unsigned numDictBits) throw();
96 
97   HRESULT Code_WithExceedReadWrite(const Byte *inData, size_t inSize, UInt32 outSize) throw();
98 
WasBlockFinished()99   bool WasBlockFinished()     const { return _unpackBlockSize == 0; }
GetUnpackData()100   const Byte *GetUnpackData() const { return _unpackedData; }
GetUnpackSize()101   UInt32 GetUnpackSize()      const { return _pos - _writePos; }
102 };
103 
104 }}
105 
106 #endif
107