diff options
Diffstat (limited to 'CPP/7zip/Compress/ZlibEncoder.cpp')
-rw-r--r-- | CPP/7zip/Compress/ZlibEncoder.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/CPP/7zip/Compress/ZlibEncoder.cpp b/CPP/7zip/Compress/ZlibEncoder.cpp new file mode 100644 index 0000000..09235c3 --- /dev/null +++ b/CPP/7zip/Compress/ZlibEncoder.cpp | |||
@@ -0,0 +1,61 @@ | |||
1 | // ZlibEncoder.cpp | ||
2 | |||
3 | #include "StdAfx.h" | ||
4 | |||
5 | #include "../Common/StreamUtils.h" | ||
6 | |||
7 | #include "ZlibEncoder.h" | ||
8 | |||
9 | namespace NCompress { | ||
10 | namespace NZlib { | ||
11 | |||
12 | #define DEFLATE_TRY_BEGIN try { | ||
13 | #define DEFLATE_TRY_END } catch(...) { return S_FALSE; } | ||
14 | |||
15 | UInt32 Adler32_Update(UInt32 adler, const Byte *buf, size_t size); | ||
16 | |||
17 | STDMETHODIMP CInStreamWithAdler::Read(void *data, UInt32 size, UInt32 *processedSize) | ||
18 | { | ||
19 | HRESULT result = _stream->Read(data, size, &size); | ||
20 | _adler = Adler32_Update(_adler, (const Byte *)data, size); | ||
21 | _size += size; | ||
22 | if (processedSize != NULL) | ||
23 | *processedSize = size; | ||
24 | return result; | ||
25 | } | ||
26 | |||
27 | void CEncoder::Create() | ||
28 | { | ||
29 | if (!DeflateEncoder) | ||
30 | DeflateEncoder = DeflateEncoderSpec = new NDeflate::NEncoder::CCOMCoder; | ||
31 | } | ||
32 | |||
33 | STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, | ||
34 | const UInt64 *inSize, const UInt64 * /* outSize */, ICompressProgressInfo *progress) | ||
35 | { | ||
36 | DEFLATE_TRY_BEGIN | ||
37 | if (!AdlerStream) | ||
38 | AdlerStream = AdlerSpec = new CInStreamWithAdler; | ||
39 | Create(); | ||
40 | |||
41 | { | ||
42 | Byte buf[2] = { 0x78, 0xDA }; | ||
43 | RINOK(WriteStream(outStream, buf, 2)); | ||
44 | } | ||
45 | |||
46 | AdlerSpec->SetStream(inStream); | ||
47 | AdlerSpec->Init(); | ||
48 | HRESULT res = DeflateEncoder->Code(AdlerStream, outStream, inSize, NULL, progress); | ||
49 | AdlerSpec->ReleaseStream(); | ||
50 | |||
51 | RINOK(res); | ||
52 | |||
53 | { | ||
54 | UInt32 a = AdlerSpec->GetAdler(); | ||
55 | Byte buf[4] = { (Byte)(a >> 24), (Byte)(a >> 16), (Byte)(a >> 8), (Byte)(a) }; | ||
56 | return WriteStream(outStream, buf, 4); | ||
57 | } | ||
58 | DEFLATE_TRY_END | ||
59 | } | ||
60 | |||
61 | }} | ||