~ubuntu-branches/ubuntu/precise/p7zip/precise-updates

« back to all changes in this revision

Viewing changes to CPP/7zip/Compress/DeflateEncoder.h

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-02-14 20:12:27 UTC
  • mfrom: (1.1.11 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090214201227-go63qxm9ozfdma60
Tags: 4.65~dfsg.1-1
* New upstream release.
* Remove wx2.8 Build-Depends added by mistakes (7zG is not yet
  intended to be built).
* Use dh_clean without -k.

Show diffs side-by-side

added added

removed removed

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