xref: /aosp_15_r20/external/lzma/CPP/7zip/Compress/DeflateEncoder.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // DeflateEncoder.h
2 
3 #ifndef ZIP7_INC_DEFLATE_ENCODER_H
4 #define ZIP7_INC_DEFLATE_ENCODER_H
5 
6 #include "../../../C/LzFind.h"
7 
8 #include "../../Common/MyCom.h"
9 
10 #include "../ICoder.h"
11 
12 #include "BitlEncoder.h"
13 #include "DeflateConst.h"
14 
15 namespace NCompress {
16 namespace NDeflate {
17 namespace NEncoder {
18 
19 struct CCodeValue
20 {
21   UInt16 Len;
22   UInt16 Pos;
SetAsLiteralCCodeValue23   void SetAsLiteral() { Len = (1 << 15); }
IsLiteralCCodeValue24   bool IsLiteral() const { return (Len >= (1 << 15)); }
25 };
26 
27 struct COptimal
28 {
29   UInt32 Price;
30   UInt16 PosPrev;
31   UInt16 BackPrev;
32 };
33 
34 const UInt32 kNumOptsBase = 1 << 12;
35 const UInt32 kNumOpts = kNumOptsBase + kMatchMaxLen;
36 
37 class CCoder;
38 
39 struct CTables: public CLevels
40 {
41   bool UseSubBlocks;
42   bool StoreMode;
43   bool StaticMode;
44   UInt32 BlockSizeRes;
45   UInt32 m_Pos;
46   void InitStructures();
47 };
48 
49 
50 struct CEncProps
51 {
52   int Level;
53   int algo;
54   int fb;
55   int btMode;
56   UInt32 mc;
57   UInt32 numPasses;
58 
CEncPropsCEncProps59   CEncProps()
60   {
61     Level = -1;
62     mc = 0;
63     algo = fb = btMode = -1;
64     numPasses = (UInt32)(Int32)-1;
65   }
66   void Normalize();
67 };
68 
69 class CCoder
70 {
71   CMatchFinder _lzInWindow;
72   CBitlEncoder m_OutStream;
73 
74 public:
75   CCodeValue *m_Values;
76 
77   UInt16 *m_MatchDistances;
78   UInt32 m_NumFastBytes;
79   bool _fastMode;
80   bool _btMode;
81 
82   UInt16 *m_OnePosMatchesMemory;
83   UInt16 *m_DistanceMemory;
84 
85   UInt32 m_Pos;
86 
87   unsigned m_NumPasses;
88   unsigned m_NumDivPasses;
89   bool m_CheckStatic;
90   bool m_IsMultiPass;
91   UInt32 m_ValueBlockSize;
92 
93   UInt32 m_NumLenCombinations;
94   UInt32 m_MatchMaxLen;
95   const Byte *m_LenStart;
96   const Byte *m_LenDirectBits;
97 
98   bool m_Created;
99   bool m_Deflate64Mode;
100 
101   Byte m_LevelLevels[kLevelTableSize];
102   unsigned m_NumLitLenLevels;
103   unsigned m_NumDistLevels;
104   UInt32 m_NumLevelCodes;
105   UInt32 m_ValueIndex;
106 
107   bool m_SecondPass;
108   UInt32 m_AdditionalOffset;
109 
110   UInt32 m_OptimumEndIndex;
111   UInt32 m_OptimumCurrentIndex;
112 
113   Byte  m_LiteralPrices[256];
114   Byte  m_LenPrices[kNumLenSymbolsMax];
115   Byte  m_PosPrices[kDistTableSize64];
116 
117   CLevels m_NewLevels;
118   UInt32 mainFreqs[kFixedMainTableSize];
119   UInt32 distFreqs[kDistTableSize64];
120   UInt32 mainCodes[kFixedMainTableSize];
121   UInt32 distCodes[kDistTableSize64];
122   UInt32 levelCodes[kLevelTableSize];
123   Byte levelLens[kLevelTableSize];
124 
125   UInt32 BlockSizeRes;
126 
127   CTables *m_Tables;
128   COptimal m_Optimum[kNumOpts];
129 
130   UInt32 m_MatchFinderCycles;
131 
132   void GetMatches();
133   void MovePos(UInt32 num);
134   UInt32 Backward(UInt32 &backRes, UInt32 cur);
135   UInt32 GetOptimal(UInt32 &backRes);
136   UInt32 GetOptimalFast(UInt32 &backRes);
137 
138   void LevelTableDummy(const Byte *levels, unsigned numLevels, UInt32 *freqs);
139 
140   void WriteBits(UInt32 value, unsigned numBits);
141   void LevelTableCode(const Byte *levels, unsigned numLevels, const Byte *lens, const UInt32 *codes);
142 
143   void MakeTables(unsigned maxHuffLen);
144   UInt32 GetLzBlockPrice() const;
145   void TryBlock();
146   UInt32 TryDynBlock(unsigned tableIndex, UInt32 numPasses);
147 
148   UInt32 TryFixedBlock(unsigned tableIndex);
149 
150   void SetPrices(const CLevels &levels);
151   void WriteBlock();
152 
153   HRESULT Create();
154   void Free();
155 
156   void WriteStoreBlock(UInt32 blockSize, UInt32 additionalOffset, bool finalBlock);
157   void WriteTables(bool writeMode, bool finalBlock);
158 
159   void WriteBlockData(bool writeMode, bool finalBlock);
160 
161   UInt32 GetBlockPrice(unsigned tableIndex, unsigned numDivPasses);
162   void CodeBlock(unsigned tableIndex, bool finalBlock);
163 
164   void SetProps(const CEncProps *props2);
165 public:
166   CCoder(bool deflate64Mode = false);
167   ~CCoder();
168 
169   HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
170       const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
171 
172   HRESULT BaseCode(ISequentialInStream *inStream, ISequentialOutStream *outStream,
173       const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
174 
175   HRESULT BaseSetEncoderProperties2(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
176 };
177 
178 
179 class CCOMCoder Z7_final:
180   public ICompressCoder,
181   public ICompressSetCoderProperties,
182   public CMyUnknownImp,
183   public CCoder
184 {
Z7_IFACES_IMP_UNK_2(ICompressCoder,ICompressSetCoderProperties)185   Z7_IFACES_IMP_UNK_2(ICompressCoder, ICompressSetCoderProperties)
186 public:
187   CCOMCoder(): CCoder(false) {}
188 };
189 
190 class CCOMCoder64 Z7_final:
191   public ICompressCoder,
192   public ICompressSetCoderProperties,
193   public CMyUnknownImp,
194   public CCoder
195 {
Z7_IFACES_IMP_UNK_2(ICompressCoder,ICompressSetCoderProperties)196   Z7_IFACES_IMP_UNK_2(ICompressCoder, ICompressSetCoderProperties)
197 public:
198   CCOMCoder64(): CCoder(true) {}
199 };
200 
201 }}}
202 
203 #endif
204