xref: /aosp_15_r20/external/lzma/CPP/7zip/Compress/ZlibEncoder.cpp (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
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 
Z7_COM7F_IMF(CInStreamWithAdler::Read (void * data,UInt32 size,UInt32 * processedSize))17 Z7_COM7F_IMF(CInStreamWithAdler::Read(void *data, UInt32 size, UInt32 *processedSize))
18 {
19   const HRESULT result = _stream->Read(data, size, &size);
20   _adler = Adler32_Update(_adler, (const Byte *)data, size);
21   _size += size;
22   if (processedSize)
23     *processedSize = size;
24   return result;
25 }
26 
Create()27 void CEncoder::Create()
28 {
29   if (!DeflateEncoder)
30     DeflateEncoder = DeflateEncoderSpec = new NDeflate::NEncoder::CCOMCoder;
31 }
32 
Z7_COM7F_IMF(CEncoder::Code (ISequentialInStream * inStream,ISequentialOutStream * outStream,const UInt64 * inSize,const UInt64 *,ICompressProgressInfo * progress))33 Z7_COM7F_IMF(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   const HRESULT res = DeflateEncoder->Code(AdlerStream, outStream, inSize, NULL, progress);
49   AdlerSpec->ReleaseStream();
50 
51   RINOK(res)
52 
53   {
54     const UInt32 a = AdlerSpec->GetAdler();
55     const 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 }}
62