xref: /aosp_15_r20/external/lzma/CPP/7zip/Compress/ZlibDecoder.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // ZlibDecoder.h
2 
3 #ifndef ZIP7_INC_ZLIB_DECODER_H
4 #define ZIP7_INC_ZLIB_DECODER_H
5 
6 #include "DeflateDecoder.h"
7 
8 namespace NCompress {
9 namespace NZlib {
10 
11 const UInt32 ADLER_INIT_VAL = 1;
12 
13 Z7_CLASS_IMP_NOQIB_1(
14   COutStreamWithAdler
15   , ISequentialOutStream
16 )
17   UInt32 _adler;
18   CMyComPtr<ISequentialOutStream> _stream;
19   UInt64 _size;
20 public:
SetStream(ISequentialOutStream * stream)21   void SetStream(ISequentialOutStream *stream) { _stream = stream; }
ReleaseStream()22   void ReleaseStream() { _stream.Release(); }
Init()23   void Init() { _adler = ADLER_INIT_VAL; _size = 0; }
GetAdler()24   UInt32 GetAdler() const { return _adler; }
GetSize()25   UInt64 GetSize() const { return _size; }
26 };
27 
28 Z7_CLASS_IMP_NOQIB_1(
29   CDecoder
30   , ICompressCoder
31 )
32   CMyComPtr2<ISequentialOutStream, COutStreamWithAdler> AdlerStream;
33   CMyComPtr2<ICompressCoder, NDeflate::NDecoder::CCOMCoder> DeflateDecoder;
34   Int32 _inputProcessedSize_Additional;
35 public:
36   bool IsAdlerOptional;
37 
CDecoder()38   CDecoder(): IsAdlerOptional(false) {}
GetInputProcessedSize()39   UInt64 GetInputProcessedSize() const
40   {
41     return (UInt64)(
42       (Int64)DeflateDecoder->GetInputProcessedSize() +
43       (Int64)_inputProcessedSize_Additional);
44   }
GetOutputProcessedSize()45   UInt64 GetOutputProcessedSize() const { return AdlerStream->GetSize(); }
46 };
47 
IsZlib(const Byte * p)48 static bool inline IsZlib(const Byte *p)
49 {
50   if ((p[0] & 0xF) != 8) // method
51     return false;
52   if (((unsigned)p[0] >> 4) > 7) // logar_window_size minus 8.
53     return false;
54   if ((p[1] & 0x20) != 0) // dictPresent
55     return false;
56   if ((((UInt32)p[0] << 8) + p[1]) % 31 != 0)
57     return false;
58   return true;
59 }
60 
61 // IsZlib_3bytes checks 2 bytes of zlib header and starting byte of Deflate stream
62 
IsZlib_3bytes(const Byte * p)63 static bool inline IsZlib_3bytes(const Byte *p)
64 {
65   if (!IsZlib(p))
66     return false;
67   const unsigned val = p[2];
68   const unsigned blockType = (val >> 1) & 0x3;
69   if (blockType == 3) // unsupported block type for deflate
70     return false;
71   if (blockType == NCompress::NDeflate::NBlockType::kStored && (val >> 3) != 0)
72     return false;
73   return true;
74 }
75 
76 }}
77 
78 #endif
79