Mercurial > vba-linux
diff src/win32/7zip/7z/CPP/7zip/Common/FilterCoder.cpp @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Common/FilterCoder.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,262 @@ 1.4 +// FilterCoder.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#include "FilterCoder.h" 1.9 +extern "C" 1.10 +{ 1.11 +#include "../../../C/Alloc.h" 1.12 +} 1.13 +#include "../../Common/Defs.h" 1.14 +#include "StreamUtils.h" 1.15 + 1.16 +static const UInt32 kBufferSize = 1 << 17; 1.17 + 1.18 +CFilterCoder::CFilterCoder() 1.19 +{ 1.20 + _buffer = (Byte *)::MidAlloc(kBufferSize); 1.21 +} 1.22 + 1.23 +CFilterCoder::~CFilterCoder() 1.24 +{ 1.25 + ::MidFree(_buffer); 1.26 +} 1.27 + 1.28 +HRESULT CFilterCoder::WriteWithLimit(ISequentialOutStream *outStream, UInt32 size) 1.29 +{ 1.30 + if (_outSizeIsDefined) 1.31 + { 1.32 + UInt64 remSize = _outSize - _nowPos64; 1.33 + if (size > remSize) 1.34 + size = (UInt32)remSize; 1.35 + } 1.36 + RINOK(WriteStream(outStream, _buffer, size)); 1.37 + _nowPos64 += size; 1.38 + return S_OK; 1.39 +} 1.40 + 1.41 + 1.42 +STDMETHODIMP CFilterCoder::Code(ISequentialInStream *inStream, 1.43 + ISequentialOutStream *outStream, const UInt64 * /* inSize */, const UInt64 *outSize, 1.44 + ICompressProgressInfo *progress) 1.45 +{ 1.46 + RINOK(Init()); 1.47 + UInt32 bufferPos = 0; 1.48 + _outSizeIsDefined = (outSize != 0); 1.49 + if (_outSizeIsDefined) 1.50 + _outSize = *outSize; 1.51 + 1.52 + while(NeedMore()) 1.53 + { 1.54 + size_t processedSize = kBufferSize - bufferPos; 1.55 + 1.56 + // Change it: It can be optimized using ReadPart 1.57 + RINOK(ReadStream(inStream, _buffer + bufferPos, &processedSize)); 1.58 + 1.59 + UInt32 endPos = bufferPos + (UInt32)processedSize; 1.60 + 1.61 + bufferPos = Filter->Filter(_buffer, endPos); 1.62 + if (bufferPos > endPos) 1.63 + { 1.64 + for (; endPos< bufferPos; endPos++) 1.65 + _buffer[endPos] = 0; 1.66 + bufferPos = Filter->Filter(_buffer, endPos); 1.67 + } 1.68 + 1.69 + if (bufferPos == 0) 1.70 + { 1.71 + if (endPos > 0) 1.72 + return WriteWithLimit(outStream, endPos); 1.73 + return S_OK; 1.74 + } 1.75 + RINOK(WriteWithLimit(outStream, bufferPos)); 1.76 + if (progress != NULL) 1.77 + { 1.78 + RINOK(progress->SetRatioInfo(&_nowPos64, &_nowPos64)); 1.79 + } 1.80 + UInt32 i = 0; 1.81 + while(bufferPos < endPos) 1.82 + _buffer[i++] = _buffer[bufferPos++]; 1.83 + bufferPos = i; 1.84 + } 1.85 + return S_OK; 1.86 +} 1.87 + 1.88 +// #ifdef _ST_MODE 1.89 +STDMETHODIMP CFilterCoder::SetOutStream(ISequentialOutStream *outStream) 1.90 +{ 1.91 + _bufferPos = 0; 1.92 + _outStream = outStream; 1.93 + return Init(); 1.94 +} 1.95 + 1.96 +STDMETHODIMP CFilterCoder::ReleaseOutStream() 1.97 +{ 1.98 + _outStream.Release(); 1.99 + return S_OK; 1.100 +}; 1.101 + 1.102 + 1.103 +STDMETHODIMP CFilterCoder::Write(const void *data, UInt32 size, UInt32 *processedSize) 1.104 +{ 1.105 + UInt32 processedSizeTotal = 0; 1.106 + while(size > 0) 1.107 + { 1.108 + UInt32 sizeMax = kBufferSize - _bufferPos; 1.109 + UInt32 sizeTemp = size; 1.110 + if (sizeTemp > sizeMax) 1.111 + sizeTemp = sizeMax; 1.112 + memmove(_buffer + _bufferPos, data, sizeTemp); 1.113 + size -= sizeTemp; 1.114 + processedSizeTotal += sizeTemp; 1.115 + data = (const Byte *)data + sizeTemp; 1.116 + UInt32 endPos = _bufferPos + sizeTemp; 1.117 + _bufferPos = Filter->Filter(_buffer, endPos); 1.118 + if (_bufferPos == 0) 1.119 + { 1.120 + _bufferPos = endPos; 1.121 + break; 1.122 + } 1.123 + if (_bufferPos > endPos) 1.124 + { 1.125 + if (size != 0) 1.126 + return E_FAIL; 1.127 + break; 1.128 + } 1.129 + RINOK(WriteWithLimit(_outStream, _bufferPos)); 1.130 + UInt32 i = 0; 1.131 + while(_bufferPos < endPos) 1.132 + _buffer[i++] = _buffer[_bufferPos++]; 1.133 + _bufferPos = i; 1.134 + } 1.135 + if (processedSize != NULL) 1.136 + *processedSize = processedSizeTotal; 1.137 + return S_OK; 1.138 +} 1.139 + 1.140 +STDMETHODIMP CFilterCoder::Flush() 1.141 +{ 1.142 + if (_bufferPos != 0) 1.143 + { 1.144 + UInt32 endPos = Filter->Filter(_buffer, _bufferPos); 1.145 + if (endPos > _bufferPos) 1.146 + { 1.147 + for (; _bufferPos < endPos; _bufferPos++) 1.148 + _buffer[_bufferPos] = 0; 1.149 + if (Filter->Filter(_buffer, endPos) != endPos) 1.150 + return E_FAIL; 1.151 + } 1.152 + RINOK(WriteStream(_outStream, _buffer, _bufferPos)); 1.153 + _bufferPos = 0; 1.154 + } 1.155 + CMyComPtr<IOutStreamFlush> flush; 1.156 + _outStream.QueryInterface(IID_IOutStreamFlush, &flush); 1.157 + if (flush) 1.158 + return flush->Flush(); 1.159 + return S_OK; 1.160 +} 1.161 + 1.162 + 1.163 +STDMETHODIMP CFilterCoder::SetInStream(ISequentialInStream *inStream) 1.164 +{ 1.165 + _convertedPosBegin = _convertedPosEnd = _bufferPos = 0; 1.166 + _inStream = inStream; 1.167 + return Init(); 1.168 +} 1.169 + 1.170 +STDMETHODIMP CFilterCoder::ReleaseInStream() 1.171 +{ 1.172 + _inStream.Release(); 1.173 + return S_OK; 1.174 +}; 1.175 + 1.176 +STDMETHODIMP CFilterCoder::Read(void *data, UInt32 size, UInt32 *processedSize) 1.177 +{ 1.178 + UInt32 processedSizeTotal = 0; 1.179 + while(size > 0) 1.180 + { 1.181 + if (_convertedPosBegin != _convertedPosEnd) 1.182 + { 1.183 + UInt32 sizeTemp = MyMin(size, _convertedPosEnd - _convertedPosBegin); 1.184 + memmove(data, _buffer + _convertedPosBegin, sizeTemp); 1.185 + _convertedPosBegin += sizeTemp; 1.186 + data = (void *)((Byte *)data + sizeTemp); 1.187 + size -= sizeTemp; 1.188 + processedSizeTotal += sizeTemp; 1.189 + break; 1.190 + } 1.191 + int i; 1.192 + for (i = 0; _convertedPosEnd + i < _bufferPos; i++) 1.193 + _buffer[i] = _buffer[i + _convertedPosEnd]; 1.194 + _bufferPos = i; 1.195 + _convertedPosBegin = _convertedPosEnd = 0; 1.196 + size_t processedSizeTemp = kBufferSize - _bufferPos; 1.197 + RINOK(ReadStream(_inStream, _buffer + _bufferPos, &processedSizeTemp)); 1.198 + _bufferPos = _bufferPos + (UInt32)processedSizeTemp; 1.199 + _convertedPosEnd = Filter->Filter(_buffer, _bufferPos); 1.200 + if (_convertedPosEnd == 0) 1.201 + { 1.202 + if (_bufferPos == 0) 1.203 + break; 1.204 + else 1.205 + { 1.206 + _convertedPosEnd = _bufferPos; // check it 1.207 + continue; 1.208 + } 1.209 + } 1.210 + if (_convertedPosEnd > _bufferPos) 1.211 + { 1.212 + for (; _bufferPos < _convertedPosEnd; _bufferPos++) 1.213 + _buffer[_bufferPos] = 0; 1.214 + _convertedPosEnd = Filter->Filter(_buffer, _bufferPos); 1.215 + } 1.216 + } 1.217 + if (processedSize != NULL) 1.218 + *processedSize = processedSizeTotal; 1.219 + return S_OK; 1.220 +} 1.221 + 1.222 +// #endif // _ST_MODE 1.223 + 1.224 +#ifndef _NO_CRYPTO 1.225 +STDMETHODIMP CFilterCoder::CryptoSetPassword(const Byte *data, UInt32 size) 1.226 +{ 1.227 + return _setPassword->CryptoSetPassword(data, size); 1.228 +} 1.229 +#endif 1.230 + 1.231 +#ifndef EXTRACT_ONLY 1.232 +STDMETHODIMP CFilterCoder::SetCoderProperties(const PROPID *propIDs, 1.233 + const PROPVARIANT *properties, UInt32 numProperties) 1.234 +{ 1.235 + return _SetCoderProperties->SetCoderProperties(propIDs, properties, numProperties); 1.236 +} 1.237 + 1.238 +STDMETHODIMP CFilterCoder::WriteCoderProperties(ISequentialOutStream *outStream) 1.239 +{ 1.240 + return _writeCoderProperties->WriteCoderProperties(outStream); 1.241 +} 1.242 + 1.243 +/* 1.244 +STDMETHODIMP CFilterCoder::ResetSalt() 1.245 +{ 1.246 + return _CryptoResetSalt->ResetSalt(); 1.247 +} 1.248 +*/ 1.249 + 1.250 +STDMETHODIMP CFilterCoder::ResetInitVector() 1.251 +{ 1.252 + return _CryptoResetInitVector->ResetInitVector(); 1.253 +} 1.254 +#endif 1.255 + 1.256 +STDMETHODIMP CFilterCoder::SetDecoderProperties2(const Byte *data, UInt32 size) 1.257 +{ 1.258 + return _setDecoderProperties->SetDecoderProperties2(data, size); 1.259 +} 1.260 + 1.261 + 1.262 + 1.263 +void foo() 1.264 +{ 1.265 +}