1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4  */
5 
6 #ifndef _nsDeflateConverter_h_
7 #define _nsDeflateConverter_h_
8 
9 #include "nsIStreamConverter.h"
10 #include "nsCOMPtr.h"
11 #include "zlib.h"
12 #include "mozilla/Attributes.h"
13 
14 #define DEFLATECONVERTER_CID                         \
15   {                                                  \
16     0x461cd5dd, 0x73c6, 0x47a4, {                    \
17       0x8c, 0xc3, 0x60, 0x3b, 0x37, 0xd8, 0x4a, 0x61 \
18     }                                                \
19   }
20 
21 class nsDeflateConverter final : public nsIStreamConverter {
22  public:
23   static constexpr size_t kZipBufLen = (4 * 1024 - 1);
24 
25   NS_DECL_ISUPPORTS
26   NS_DECL_NSIREQUESTOBSERVER
27   NS_DECL_NSISTREAMLISTENER
28   NS_DECL_NSISTREAMCONVERTER
29 
nsDeflateConverter()30   nsDeflateConverter() : mWrapMode(WRAP_NONE), mOffset(0), mZstream() {
31     // 6 is Z_DEFAULT_COMPRESSION but we need the actual value
32     mLevel = 6;
33   }
34 
nsDeflateConverter(int32_t level)35   explicit nsDeflateConverter(int32_t level)
36       : mWrapMode(WRAP_NONE), mOffset(0), mZstream() {
37     mLevel = level;
38   }
39 
40  private:
~nsDeflateConverter()41   ~nsDeflateConverter() {}
42 
43   enum WrapMode { WRAP_ZLIB, WRAP_GZIP, WRAP_NONE };
44 
45   WrapMode mWrapMode;
46   uint64_t mOffset;
47   int32_t mLevel;
48   nsCOMPtr<nsIStreamListener> mListener;
49   nsCOMPtr<nsISupports> mContext;
50   z_stream mZstream;
51   unsigned char mWriteBuffer[kZipBufLen];
52 
53   nsresult Init();
54   nsresult PushAvailableData(nsIRequest* aRequest);
55 };
56 
57 #endif
58