PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/epee/include/gzip_encoding.h

https://gitlab.com/nexxuz/cryptonote
C Header | 227 lines | 122 code | 30 blank | 75 comment | 18 complexity | 4a69c9562bdcf7b384023d4ee7f4b9ea MD5 | raw 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. #ifndef _GZIP_ENCODING_H_
  27. #define _GZIP_ENCODING_H_
  28. #include "net/http_client_base.h"
  29. #include "zlib/zlib.h"
  30. //#include "http.h"
  31. namespace epee
  32. {
  33. namespace net_utils
  34. {
  35. class content_encoding_gzip: public i_sub_handler
  36. {
  37. public:
  38. /*! \brief
  39. * Function content_encoding_gzip : Constructor
  40. *
  41. */
  42. inline
  43. content_encoding_gzip(i_target_handler* powner_filter, bool is_deflate_mode = false):m_powner_filter(powner_filter),
  44. m_is_stream_ended(false),
  45. m_is_deflate_mode(is_deflate_mode),
  46. m_is_first_update_in(true)
  47. {
  48. memset(&m_zstream_in, 0, sizeof(m_zstream_in));
  49. memset(&m_zstream_out, 0, sizeof(m_zstream_out));
  50. int ret = 0;
  51. if(is_deflate_mode)
  52. {
  53. ret = inflateInit(&m_zstream_in);
  54. ret = deflateInit(&m_zstream_out, Z_DEFAULT_COMPRESSION);
  55. }else
  56. {
  57. ret = inflateInit2(&m_zstream_in, 0x1F);
  58. ret = deflateInit2(&m_zstream_out, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 0x1F, 8, Z_DEFAULT_STRATEGY);
  59. }
  60. }
  61. /*! \brief
  62. * Function content_encoding_gzip : Destructor
  63. *
  64. */
  65. inline
  66. ~content_encoding_gzip()
  67. {
  68. inflateEnd(& m_zstream_in );
  69. deflateEnd(& m_zstream_out );
  70. }
  71. /*! \brief
  72. * Function update_in : Entry point for income data
  73. *
  74. */
  75. inline
  76. virtual bool update_in( std::string& piece_of_transfer)
  77. {
  78. bool is_first_time_here = m_is_first_update_in;
  79. m_is_first_update_in = false;
  80. if(m_pre_decode.size())
  81. m_pre_decode += piece_of_transfer;
  82. else
  83. m_pre_decode.swap(piece_of_transfer);
  84. piece_of_transfer.clear();
  85. std::string decode_summary_buff;
  86. size_t ungzip_size = m_pre_decode.size() * 0x30;
  87. std::string current_decode_buff(ungzip_size, 'X');
  88. //Here the cycle is introduced where we unpack the buffer, the cycle is required
  89. //because of the case where if after unpacking the data will exceed the awaited size, we will not halt with error
  90. bool continue_unpacking = true;
  91. bool first_step = true;
  92. while(m_pre_decode.size() && continue_unpacking)
  93. {
  94. //fill buffers
  95. m_zstream_in.next_in = (Bytef*)m_pre_decode.data();
  96. m_zstream_in.avail_in = (uInt)m_pre_decode.size();
  97. m_zstream_in.next_out = (Bytef*)current_decode_buff.data();
  98. m_zstream_in.avail_out = (uInt)ungzip_size;
  99. int flag = Z_SYNC_FLUSH;
  100. int ret = inflate(&m_zstream_in, flag);
  101. 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);
  102. if(Z_STREAM_END == ret)
  103. m_is_stream_ended = true;
  104. else if(Z_DATA_ERROR == ret && is_first_time_here && m_is_deflate_mode&& first_step)
  105. {
  106. // some servers (notably Apache with mod_deflate) don't generate zlib headers
  107. // insert a dummy header and try again
  108. static char dummy_head[2] =
  109. {
  110. 0x8 + 0x7 * 0x10,
  111. (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
  112. };
  113. inflateReset(&m_zstream_in);
  114. m_zstream_in.next_in = (Bytef*) dummy_head;
  115. m_zstream_in.avail_in = sizeof(dummy_head);
  116. ret = inflate(&m_zstream_in, Z_NO_FLUSH);
  117. if (ret != Z_OK)
  118. {
  119. LOCAL_ASSERT(0);
  120. m_pre_decode.swap(piece_of_transfer);
  121. return false;
  122. }
  123. m_zstream_in.next_in = (Bytef*)m_pre_decode.data();
  124. m_zstream_in.avail_in = (uInt)m_pre_decode.size();
  125. ret = inflate(&m_zstream_in, Z_NO_FLUSH);
  126. if (ret != Z_OK)
  127. {
  128. LOCAL_ASSERT(0);
  129. m_pre_decode.swap(piece_of_transfer);
  130. return false;
  131. }
  132. }
  133. //leave only unpacked part in the output buffer to start with it the next time
  134. m_pre_decode.erase(0, m_pre_decode.size()-m_zstream_in.avail_in);
  135. //if decoder gave nothing to return, then everything is ahead, now simply break
  136. if(ungzip_size == m_zstream_in.avail_out)
  137. break;
  138. //decode_buff currently stores data parts that were unpacked, fix this size
  139. current_decode_buff.resize(ungzip_size - m_zstream_in.avail_out);
  140. if(decode_summary_buff.size())
  141. decode_summary_buff += current_decode_buff;
  142. else
  143. current_decode_buff.swap(decode_summary_buff);
  144. current_decode_buff.resize(ungzip_size);
  145. first_step = false;
  146. }
  147. //Process these data if required
  148. bool res = true;
  149. res = m_powner_filter->handle_target_data(decode_summary_buff);
  150. return true;
  151. }
  152. /*! \brief
  153. * Function stop : Entry point for stop signal and flushing cached data buffer.
  154. *
  155. */
  156. inline
  157. virtual void stop(std::string& OUT collect_remains)
  158. {
  159. }
  160. protected:
  161. private:
  162. /*! \brief
  163. * Pointer to parent HTTP-parser
  164. */
  165. i_target_handler* m_powner_filter;
  166. /*! \brief
  167. * ZLIB object for income stream
  168. */
  169. z_stream m_zstream_in;
  170. /*! \brief
  171. * ZLIB object for outcome stream
  172. */
  173. z_stream m_zstream_out;
  174. /*! \brief
  175. * Data that could not be unpacked immediately, left to wait for the next packet of data
  176. */
  177. std::string m_pre_decode;
  178. /*! \brief
  179. * The data are accumulated for a package in the buffer to send the web client
  180. */
  181. std::string m_pre_encode;
  182. /*! \brief
  183. * Signals that stream looks like ended
  184. */
  185. bool m_is_stream_ended;
  186. /*! \brief
  187. * If this flag is set, income data is in HTTP-deflate mode
  188. */
  189. bool m_is_deflate_mode;
  190. /*! \brief
  191. * Marks that it is a first data packet
  192. */
  193. bool m_is_first_update_in;
  194. };
  195. }
  196. }
  197. #endif //_GZIP_ENCODING_H_