ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 lines
6.0 KiB

  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. // To configure Boost to work with zlib, see the
  7. // installation instructions here:
  8. // http://boost.org/libs/iostreams/doc/index.html?path=7
  9. // Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp>
  10. // knows that we are building the library (possibly exporting code), rather
  11. // than using it (possibly importing code).
  12. #define CPPCMS_BOOST_IOSTREAMS_SOURCE
  13. #include <cppcms_boost/iostreams/detail/config/dyn_link.hpp>
  14. #include <cppcms_boost/iostreams/filter/zlib.hpp>
  15. #include "zlib.h" // Jean-loup Gailly's and Mark Adler's "zlib.h" header.
  16. // To configure Boost to work with zlib, see the
  17. // installation instructions here:
  18. // http://boost.org/libs/iostreams/doc/index.html?path=7
  19. namespace cppcms_boost { namespace iostreams {
  20. namespace zlib {
  21. // Compression levels
  22. const int no_compression = Z_NO_COMPRESSION;
  23. const int best_speed = Z_BEST_SPEED;
  24. const int best_compression = Z_BEST_COMPRESSION;
  25. const int default_compression = Z_DEFAULT_COMPRESSION;
  26. // Compression methods
  27. const int deflated = Z_DEFLATED;
  28. // Compression strategies
  29. const int default_strategy = Z_DEFAULT_STRATEGY;
  30. const int filtered = Z_FILTERED;
  31. const int huffman_only = Z_HUFFMAN_ONLY;
  32. // Status codes
  33. const int okay = Z_OK;
  34. const int stream_end = Z_STREAM_END;
  35. const int stream_error = Z_STREAM_ERROR;
  36. const int version_error = Z_VERSION_ERROR;
  37. const int data_error = Z_DATA_ERROR;
  38. const int mem_error = Z_MEM_ERROR;
  39. const int buf_error = Z_BUF_ERROR;
  40. // Flush codes
  41. const int finish = Z_FINISH;
  42. const int no_flush = Z_NO_FLUSH;
  43. const int sync_flush = Z_SYNC_FLUSH;
  44. // Code for current OS
  45. //const int os_code = OS_CODE;
  46. } // End namespace zlib.
  47. //------------------Implementation of zlib_error------------------------------//
  48. zlib_error::zlib_error(int error)
  49. : CPPCMS_BOOST_IOSTREAMS_FAILURE("zlib error"), error_(error)
  50. { }
  51. void zlib_error::check(int error)
  52. {
  53. switch (error) {
  54. case Z_OK:
  55. case Z_STREAM_END:
  56. //case Z_BUF_ERROR:
  57. return;
  58. case Z_MEM_ERROR:
  59. throw std::bad_alloc();
  60. default:
  61. throw zlib_error(error);
  62. ;
  63. }
  64. }
  65. //------------------Implementation of zlib_base-------------------------------//
  66. namespace detail {
  67. zlib_base::zlib_base()
  68. : stream_(new z_stream), calculate_crc_(false), crc_(0)
  69. { }
  70. zlib_base::~zlib_base() { delete static_cast<z_stream*>(stream_); }
  71. void zlib_base::before( const char*& src_begin, const char* src_end,
  72. char*& dest_begin, char* dest_end )
  73. {
  74. z_stream* s = static_cast<z_stream*>(stream_);
  75. s->next_in = reinterpret_cast<zlib::byte*>(const_cast<char*>(src_begin));
  76. s->avail_in = static_cast<zlib::uint>(src_end - src_begin);
  77. s->next_out = reinterpret_cast<zlib::byte*>(dest_begin);
  78. s->avail_out= static_cast<zlib::uint>(dest_end - dest_begin);
  79. }
  80. void zlib_base::after(const char*& src_begin, char*& dest_begin, bool compress)
  81. {
  82. z_stream* s = static_cast<z_stream*>(stream_);
  83. char* next_in = reinterpret_cast<char*>(s->next_in);
  84. char* next_out = reinterpret_cast<char*>(s->next_out);
  85. if (calculate_crc_) {
  86. const zlib::byte* buf = compress ?
  87. reinterpret_cast<const zlib::byte*>(src_begin) :
  88. reinterpret_cast<const zlib::byte*>(
  89. const_cast<const char*>(dest_begin)
  90. );
  91. zlib::uint length = compress ?
  92. static_cast<zlib::uint>(next_in - src_begin) :
  93. static_cast<zlib::uint>(next_out - dest_begin);
  94. if (length > 0)
  95. crc_ = crc32(crc_, buf, length);
  96. }
  97. total_in_ = s->total_in;
  98. total_out_ = s->total_out;
  99. src_begin = const_cast<const char*>(next_in);
  100. dest_begin = next_out;
  101. }
  102. int zlib_base::xdeflate(int flush)
  103. {
  104. return ::deflate(static_cast<z_stream*>(stream_), flush);
  105. }
  106. int zlib_base::xinflate(int flush)
  107. {
  108. return ::inflate(static_cast<z_stream*>(stream_), flush);
  109. }
  110. void zlib_base::reset(bool compress, bool realloc)
  111. {
  112. z_stream* s = static_cast<z_stream*>(stream_);
  113. // Undiagnosed bug:
  114. // deflateReset(), etc., return Z_DATA_ERROR
  115. //zlib_error::check(
  116. realloc ?
  117. (compress ? deflateReset(s) : inflateReset(s)) :
  118. (compress ? deflateEnd(s) : inflateEnd(s))
  119. ;
  120. //);
  121. }
  122. void zlib_base::do_init
  123. ( const zlib_params& p, bool compress,
  124. #if !CPPCMS_BOOST_WORKAROUND(CPPCMS_BOOST_MSVC, < 1300)
  125. zlib::xalloc_func /* alloc */, zlib::xfree_func /* free*/,
  126. #endif
  127. void* derived )
  128. {
  129. calculate_crc_ = p.calculate_crc;
  130. z_stream* s = static_cast<z_stream*>(stream_);
  131. // Current interface for customizing memory management
  132. // is non-conforming and has been disabled:
  133. //#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  134. // s->zalloc = alloc;
  135. // s->zfree = free;
  136. //#else
  137. s->zalloc = 0;
  138. s->zfree = 0;
  139. //#endif
  140. s->opaque = derived;
  141. int window_bits = p.noheader? -p.window_bits : p.window_bits;
  142. zlib_error::check(
  143. compress ?
  144. deflateInit2( s,
  145. p.level,
  146. p.method,
  147. window_bits,
  148. p.mem_level,
  149. p.strategy ) :
  150. inflateInit2(s, window_bits)
  151. );
  152. }
  153. } // End namespace detail.
  154. //----------------------------------------------------------------------------//
  155. } } // End namespaces iostreams, boost.