Electroneum
epee::net_utils::content_encoding_gzip Class Reference

#include <gzip_encoding.h>

Inheritance diagram for epee::net_utils::content_encoding_gzip:
Collaboration diagram for epee::net_utils::content_encoding_gzip:

Public Member Functions

 content_encoding_gzip (i_target_handler *powner_filter, bool is_deflate_mode=false)
 Function content_encoding_gzip : Constructor. More...
 
 ~content_encoding_gzip ()
 Function content_encoding_gzip : Destructor. More...
 
virtual bool update_in (std::string &piece_of_transfer)
 Function update_in : Entry point for income data. More...
 
virtual void stop (std::string &OUT collect_remains)
 Function stop : Entry point for stop signal and flushing cached data buffer. More...
 
- Public Member Functions inherited from epee::net_utils::i_sub_handler
virtual ~i_sub_handler ()
 
virtual void stop (std::string &collect_remains)=0
 
virtual bool update_and_stop (std::string &collect_remains, bool &is_changed)
 

Detailed Description

Definition at line 44 of file gzip_encoding.h.

Constructor & Destructor Documentation

◆ content_encoding_gzip()

epee::net_utils::content_encoding_gzip::content_encoding_gzip ( i_target_handler powner_filter,
bool  is_deflate_mode = false 
)
inline

Function content_encoding_gzip : Constructor.

Definition at line 52 of file gzip_encoding.h.

52  :m_powner_filter(powner_filter),
53  m_is_stream_ended(false),
54  m_is_deflate_mode(is_deflate_mode),
55  m_is_first_update_in(true)
56  {
57  memset(&m_zstream_in, 0, sizeof(m_zstream_in));
58  memset(&m_zstream_out, 0, sizeof(m_zstream_out));
59  int ret = 0;
60  if(is_deflate_mode)
61  {
62  ret = inflateInit(&m_zstream_in);
63  ret = deflateInit(&m_zstream_out, Z_DEFAULT_COMPRESSION);
64  }else
65  {
66  ret = inflateInit2(&m_zstream_in, 0x1F);
67  ret = deflateInit2(&m_zstream_out, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 0x1F, 8, Z_DEFAULT_STRATEGY);
68  }
69  }

◆ ~content_encoding_gzip()

epee::net_utils::content_encoding_gzip::~content_encoding_gzip ( )
inline

Function content_encoding_gzip : Destructor.

Definition at line 75 of file gzip_encoding.h.

76  {
77  inflateEnd(& m_zstream_in );
78  deflateEnd(& m_zstream_out );
79  }

Member Function Documentation

◆ stop()

virtual void epee::net_utils::content_encoding_gzip::stop ( std::string &OUT  collect_remains)
inlinevirtual

Function stop : Entry point for stop signal and flushing cached data buffer.

Definition at line 184 of file gzip_encoding.h.

185  {
186  }

◆ update_in()

virtual bool epee::net_utils::content_encoding_gzip::update_in ( std::string &  piece_of_transfer)
inlinevirtual

Function update_in : Entry point for income data.

Implements epee::net_utils::i_sub_handler.

Definition at line 85 of file gzip_encoding.h.

86  {
87 
88  bool is_first_time_here = m_is_first_update_in;
89  m_is_first_update_in = false;
90 
91  if(m_pre_decode.size())
92  m_pre_decode += piece_of_transfer;
93  else
94  m_pre_decode.swap(piece_of_transfer);
95  piece_of_transfer.clear();
96 
97  std::string decode_summary_buff;
98 
99  size_t ungzip_size = m_pre_decode.size() * 0x30;
100  std::string current_decode_buff(ungzip_size, 'X');
101 
102  //Here the cycle is introduced where we unpack the buffer, the cycle is required
103  //because of the case where if after unpacking the data will exceed the awaited size, we will not halt with error
104  bool continue_unpacking = true;
105  bool first_step = true;
106  while(m_pre_decode.size() && continue_unpacking)
107  {
108 
109  //fill buffers
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;
114 
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);
118 
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)
122  {
123  // some servers (notably Apache with mod_deflate) don't generate zlib headers
124  // insert a dummy header and try again
125  static char dummy_head[2] =
126  {
127  0x8 + 0x7 * 0x10,
128  (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
129  };
130  inflateReset(&m_zstream_in);
131  m_zstream_in.next_in = (Bytef*) dummy_head;
132  m_zstream_in.avail_in = sizeof(dummy_head);
133 
134  ret = inflate(&m_zstream_in, Z_NO_FLUSH);
135  if (ret != Z_OK)
136  {
137  LOCAL_ASSERT(0);
138  m_pre_decode.swap(piece_of_transfer);
139  return false;
140  }
141  m_zstream_in.next_in = (Bytef*)m_pre_decode.data();
142  m_zstream_in.avail_in = (uInt)m_pre_decode.size();
143 
144  ret = inflate(&m_zstream_in, Z_NO_FLUSH);
145  if (ret != Z_OK)
146  {
147  LOCAL_ASSERT(0);
148  m_pre_decode.swap(piece_of_transfer);
149  return false;
150  }
151  }
152 
153 
154  //leave only unpacked part in the output buffer to start with it the next time
155  m_pre_decode.erase(0, m_pre_decode.size()-m_zstream_in.avail_in);
156  //if decoder gave nothing to return, then everything is ahead, now simply break
157  if(ungzip_size == m_zstream_in.avail_out)
158  break;
159 
160  //decode_buff currently stores data parts that were unpacked, fix this size
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;
164  else
165  current_decode_buff.swap(decode_summary_buff);
166 
167  current_decode_buff.resize(ungzip_size);
168  first_step = false;
169  }
170 
171  //Process these data if required
172  bool res = true;
173 
174  res = m_powner_filter->handle_target_data(decode_summary_buff);
175 
176  return true;
177 
178  }
const char * res
Definition: hmac_keccak.cpp:41
#define LOCAL_ASSERT(expr)
Definition: misc_log_ex.h:122
::std::string string
Definition: gtest-port.h:1097
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
virtual bool handle_target_data(std::string &piece_of_transfer)=0
Here is the call graph for this function:

The documentation for this class was generated from the following file: