Electroneum
gzip_encoding.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 
29 
30 #ifndef _GZIP_ENCODING_H_
31 #define _GZIP_ENCODING_H_
32 #include "net/http_client_base.h"
33 #include "zlib/zlib.h"
34 //#include "http.h"
35 
36 
37 namespace epee
38 {
39 namespace net_utils
40 {
41 
42 
43 
45  {
46  public:
51  inline
52  content_encoding_gzip(i_target_handler* powner_filter, bool is_deflate_mode = false):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  }
74  inline
76  {
77  inflateEnd(& m_zstream_in );
78  deflateEnd(& m_zstream_out );
79  }
84  inline
85  virtual bool update_in( std::string& piece_of_transfer)
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  }
183  inline
184  virtual void stop(std::string& OUT collect_remains)
185  {
186  }
187  protected:
188  private:
192  i_target_handler* m_powner_filter;
196  z_stream m_zstream_in;
200  z_stream m_zstream_out;
204  std::string m_pre_decode;
208  std::string m_pre_encode;
212  bool m_is_stream_ended;
216  bool m_is_deflate_mode;
220  bool m_is_first_update_in;
221  };
222 }
223 }
224 
225 
226 
227 #endif //_GZIP_ENCODING_H_
const char * res
Definition: hmac_keccak.cpp:41
~content_encoding_gzip()
Function content_encoding_gzip : Destructor.
Definition: gzip_encoding.h:75
#define LOCAL_ASSERT(expr)
Definition: misc_log_ex.h:122
content_encoding_gzip(i_target_handler *powner_filter, bool is_deflate_mode=false)
Function content_encoding_gzip : Constructor.
Definition: gzip_encoding.h:52
::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 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.
Definition: gzip_encoding.h:85
#define OUT
Definition: string_tools.h:55
virtual bool handle_target_data(std::string &piece_of_transfer)=0
return true
#define false
Definition: stdbool.h:38