30 #ifndef _GZIP_ENCODING_H_ 31 #define _GZIP_ENCODING_H_ 33 #include "zlib/zlib.h" 53 m_is_stream_ended(
false),
54 m_is_deflate_mode(is_deflate_mode),
55 m_is_first_update_in(
true)
57 memset(&m_zstream_in, 0,
sizeof(m_zstream_in));
58 memset(&m_zstream_out, 0,
sizeof(m_zstream_out));
62 ret = inflateInit(&m_zstream_in);
63 ret = deflateInit(&m_zstream_out, Z_DEFAULT_COMPRESSION);
66 ret = inflateInit2(&m_zstream_in, 0x1F);
67 ret = deflateInit2(&m_zstream_out, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 0x1F, 8, Z_DEFAULT_STRATEGY);
77 inflateEnd(& m_zstream_in );
78 deflateEnd(& m_zstream_out );
88 bool is_first_time_here = m_is_first_update_in;
89 m_is_first_update_in =
false;
91 if(m_pre_decode.size())
92 m_pre_decode += piece_of_transfer;
94 m_pre_decode.swap(piece_of_transfer);
95 piece_of_transfer.clear();
99 size_t ungzip_size = m_pre_decode.size() * 0x30;
104 bool continue_unpacking =
true;
105 bool first_step =
true;
106 while(m_pre_decode.size() && continue_unpacking)
110 m_zstream_in.next_in = (Bytef*)m_pre_decode.data();
111 m_zstream_in.avail_in = (uInt)m_pre_decode.size();
112 m_zstream_in.next_out = (Bytef*)current_decode_buff.data();
113 m_zstream_in.avail_out = (uInt)ungzip_size;
115 int flag = Z_SYNC_FLUSH;
116 int ret = inflate(&m_zstream_in, flag);
117 CHECK_AND_ASSERT_MES(ret>=0 || m_zstream_in.avail_out ||m_is_deflate_mode,
false,
"content_encoding_gzip::update_in() Failed to inflate. err = " << ret);
119 if(Z_STREAM_END == ret)
120 m_is_stream_ended =
true;
121 else if(Z_DATA_ERROR == ret && is_first_time_here && m_is_deflate_mode&& first_step)
125 static char dummy_head[2] =
128 (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
130 inflateReset(&m_zstream_in);
131 m_zstream_in.next_in = (Bytef*) dummy_head;
132 m_zstream_in.avail_in =
sizeof(dummy_head);
134 ret = inflate(&m_zstream_in, Z_NO_FLUSH);
138 m_pre_decode.swap(piece_of_transfer);
141 m_zstream_in.next_in = (Bytef*)m_pre_decode.data();
142 m_zstream_in.avail_in = (uInt)m_pre_decode.size();
144 ret = inflate(&m_zstream_in, Z_NO_FLUSH);
148 m_pre_decode.swap(piece_of_transfer);
155 m_pre_decode.erase(0, m_pre_decode.size()-m_zstream_in.avail_in);
157 if(ungzip_size == m_zstream_in.avail_out)
161 current_decode_buff.resize(ungzip_size - m_zstream_in.avail_out);
162 if(decode_summary_buff.size())
163 decode_summary_buff += current_decode_buff;
165 current_decode_buff.swap(decode_summary_buff);
167 current_decode_buff.resize(ungzip_size);
196 z_stream m_zstream_in;
200 z_stream m_zstream_out;
212 bool m_is_stream_ended;
216 bool m_is_deflate_mode;
220 bool m_is_first_update_in;
227 #endif //_GZIP_ENCODING_H_
~content_encoding_gzip()
Function content_encoding_gzip : Destructor.
#define LOCAL_ASSERT(expr)
content_encoding_gzip(i_target_handler *powner_filter, bool is_deflate_mode=false)
Function content_encoding_gzip : Constructor.
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
virtual void stop(std::string &OUT collect_remains)
Function stop : Entry point for stop signal and flushing cached data buffer.
virtual bool update_in(std::string &piece_of_transfer)
Function update_in : Entry point for income data.
virtual bool handle_target_data(std::string &piece_of_transfer)=0