• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // FilterCoder.h
2 
3 #ifndef __FILTER_CODER_H
4 #define __FILTER_CODER_H
5 
6 #include "../../../C/Alloc.h"
7 
8 #include "../../Common/MyCom.h"
9 #include "../ICoder.h"
10 
11 #ifndef _NO_CRYPTO
12 #include "../IPassword.h"
13 #endif
14 
15 #define MY_QUERYINTERFACE_ENTRY_AG(i, sub0, sub) else if (iid == IID_ ## i) \
16   { if (!sub) RINOK(sub0->QueryInterface(IID_ ## i, (void **)&sub)) \
17     *outObject = (void *)(i *)this; }
18 
19 
20 struct CAlignedMidBuffer
21 {
22   Byte *_buf;
23 
CAlignedMidBufferCAlignedMidBuffer24   CAlignedMidBuffer(): _buf(NULL) {}
25   ~CAlignedMidBuffer();
26   void AllocAligned(size_t size);
27 };
28 
29 class CFilterCoder:
30   public ICompressCoder,
31 
32   public ICompressSetOutStreamSize,
33   public ICompressInitEncoder,
34 
35   public ICompressSetInStream,
36   public ISequentialInStream,
37 
38   public ICompressSetOutStream,
39   public ISequentialOutStream,
40   public IOutStreamFinish,
41 
42   public ICompressSetBufSize,
43 
44   #ifndef _NO_CRYPTO
45   public ICryptoSetPassword,
46   public ICryptoProperties,
47   #endif
48 
49   #ifndef EXTRACT_ONLY
50   public ICompressSetCoderProperties,
51   public ICompressWriteCoderProperties,
52   // public ICryptoResetSalt,
53   public ICryptoResetInitVector,
54   #endif
55 
56   public ICompressSetDecoderProperties2,
57   public CMyUnknownImp,
58   public CAlignedMidBuffer
59 {
60   UInt32 _bufSize;
61   UInt32 _inBufSize;
62   UInt32 _outBufSize;
63 
64   bool _encodeMode;
65   bool _outSizeIsDefined;
66   UInt64 _outSize;
67   UInt64 _nowPos64;
68 
69   CMyComPtr<ISequentialInStream> _inStream;
70   CMyComPtr<ISequentialOutStream> _outStream;
71   UInt32 _bufPos;
72   UInt32 _convPos;    // current pos in buffer for converted data
73   UInt32 _convSize;   // size of converted data starting from _convPos
74 
InitSpecVars()75   void InitSpecVars()
76   {
77     _bufPos = 0;
78     _convPos = 0;
79     _convSize = 0;
80 
81     _outSizeIsDefined = false;
82     _outSize = 0;
83     _nowPos64 = 0;
84   }
85 
86   HRESULT Alloc();
87   HRESULT Init_and_Alloc();
88   HRESULT Flush2();
89 
90   #ifndef _NO_CRYPTO
91   CMyComPtr<ICryptoSetPassword> _SetPassword;
92   CMyComPtr<ICryptoProperties> _CryptoProperties;
93   #endif
94 
95   #ifndef EXTRACT_ONLY
96   CMyComPtr<ICompressSetCoderProperties> _SetCoderProperties;
97   CMyComPtr<ICompressWriteCoderProperties> _WriteCoderProperties;
98   // CMyComPtr<ICryptoResetSalt> _CryptoResetSalt;
99   CMyComPtr<ICryptoResetInitVector> _CryptoResetInitVector;
100   #endif
101 
102   CMyComPtr<ICompressSetDecoderProperties2> _SetDecoderProperties2;
103 
104 public:
105   CMyComPtr<ICompressFilter> Filter;
106 
107   CFilterCoder(bool encodeMode);
108   ~CFilterCoder();
109 
110   class C_InStream_Releaser
111   {
112   public:
113     CFilterCoder *FilterCoder;
C_InStream_Releaser()114     C_InStream_Releaser(): FilterCoder(NULL) {}
~C_InStream_Releaser()115     ~C_InStream_Releaser() { if (FilterCoder) FilterCoder->ReleaseInStream(); }
116   };
117 
118   class C_OutStream_Releaser
119   {
120   public:
121     CFilterCoder *FilterCoder;
C_OutStream_Releaser()122     C_OutStream_Releaser(): FilterCoder(NULL) {}
~C_OutStream_Releaser()123     ~C_OutStream_Releaser() { if (FilterCoder) FilterCoder->ReleaseOutStream(); }
124   };
125 
126   class C_Filter_Releaser
127   {
128   public:
129     CFilterCoder *FilterCoder;
C_Filter_Releaser()130     C_Filter_Releaser(): FilterCoder(NULL) {}
~C_Filter_Releaser()131     ~C_Filter_Releaser() { if (FilterCoder) FilterCoder->Filter.Release(); }
132   };
133 
134 
135   MY_QUERYINTERFACE_BEGIN2(ICompressCoder)
136 
137     MY_QUERYINTERFACE_ENTRY(ICompressSetOutStreamSize)
138     MY_QUERYINTERFACE_ENTRY(ICompressInitEncoder)
139 
140     MY_QUERYINTERFACE_ENTRY(ICompressSetInStream)
141     MY_QUERYINTERFACE_ENTRY(ISequentialInStream)
142 
143     MY_QUERYINTERFACE_ENTRY(ICompressSetOutStream)
144     MY_QUERYINTERFACE_ENTRY(ISequentialOutStream)
145     MY_QUERYINTERFACE_ENTRY(IOutStreamFinish)
146 
147     MY_QUERYINTERFACE_ENTRY(ICompressSetBufSize)
148 
149     #ifndef _NO_CRYPTO
150     MY_QUERYINTERFACE_ENTRY_AG(ICryptoSetPassword, Filter, _SetPassword)
151     MY_QUERYINTERFACE_ENTRY_AG(ICryptoProperties, Filter, _CryptoProperties)
152     #endif
153 
154     #ifndef EXTRACT_ONLY
155     MY_QUERYINTERFACE_ENTRY_AG(ICompressSetCoderProperties, Filter, _SetCoderProperties)
156     MY_QUERYINTERFACE_ENTRY_AG(ICompressWriteCoderProperties, Filter, _WriteCoderProperties)
157     // MY_QUERYINTERFACE_ENTRY_AG(ICryptoResetSalt, Filter, _CryptoResetSalt)
158     MY_QUERYINTERFACE_ENTRY_AG(ICryptoResetInitVector, Filter, _CryptoResetInitVector)
159     #endif
160 
161     MY_QUERYINTERFACE_ENTRY_AG(ICompressSetDecoderProperties2, Filter, _SetDecoderProperties2)
162   MY_QUERYINTERFACE_END
163   MY_ADDREF_RELEASE
164 
165 
166   STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
167       const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
168 
169   STDMETHOD(SetOutStreamSize)(const UInt64 *outSize);
170   STDMETHOD(InitEncoder)();
171 
172   STDMETHOD(SetInStream)(ISequentialInStream *inStream);
173   STDMETHOD(ReleaseInStream)();
174   STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
175 
176   STDMETHOD(SetOutStream)(ISequentialOutStream *outStream);
177   STDMETHOD(ReleaseOutStream)();
178   STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
179   STDMETHOD(OutStreamFinish)();
180 
181   STDMETHOD(SetInBufSize)(UInt32 streamIndex, UInt32 size);
182   STDMETHOD(SetOutBufSize)(UInt32 streamIndex, UInt32 size);
183 
184   #ifndef _NO_CRYPTO
185   STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
186 
187   STDMETHOD(SetKey)(const Byte *data, UInt32 size);
188   STDMETHOD(SetInitVector)(const Byte *data, UInt32 size);
189   #endif
190 
191   #ifndef EXTRACT_ONLY
192   STDMETHOD(SetCoderProperties)(const PROPID *propIDs,
193       const PROPVARIANT *properties, UInt32 numProperties);
194   STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
195   // STDMETHOD(ResetSalt)();
196   STDMETHOD(ResetInitVector)();
197   #endif
198 
199   STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
200 
201 
202   HRESULT Init_NoSubFilterInit();
203 };
204 
205 #endif
206