zlib.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. * https://github.com/r-lyeh-archived/bundle
  11. *
  12. */
  13. #ifndef __ASIO2_ZLIB_HPP__
  14. #define __ASIO2_ZLIB_HPP__
  15. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #pragma once
  17. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  18. #include <string>
  19. #include <string_view>
  20. #include <algorithm>
  21. #include <asio2/external/asio.hpp>
  22. #include <asio2/external/beast.hpp>
  23. #include <asio2/base/error.hpp>
  24. #ifdef ASIO2_HEADER_ONLY
  25. namespace bho::beast::zlib
  26. #else
  27. namespace boost::beast::zlib
  28. #endif
  29. {
  30. /**
  31. * impl : use the beast::zlib for compress and uncompress
  32. * Problem: compress data with beast::zlib and uncompress data with zlib(www.zlib.net) will fail
  33. */
  34. class impl
  35. {
  36. public:
  37. impl(
  38. beast::zlib::Flush compress_flush = beast::zlib::Flush::sync,
  39. beast::zlib::Flush uncompress_flush = beast::zlib::Flush::sync
  40. )
  41. : deflate_flush_( compress_flush)
  42. , inflate_flush_(uncompress_flush)
  43. {
  44. }
  45. ~impl() = default;
  46. impl(impl&&) = delete;
  47. impl(const impl&) = delete;
  48. impl& operator=(impl&&) = delete;
  49. impl& operator=(const impl&) = delete;
  50. inline std::string compress(std::string_view data)
  51. {
  52. return work(deflate_stream_, deflate_flush_, data, asio2::get_last_error());
  53. }
  54. inline std::string uncompress(std::string_view data)
  55. {
  56. return work(inflate_stream_, inflate_flush_, data, asio2::get_last_error());
  57. }
  58. inline static std::size_t compress_bound(std::size_t size)
  59. {
  60. return beast::zlib::deflate_upper_bound(size);
  61. }
  62. inline static std::size_t uncompress_bound(std::size_t size)
  63. {
  64. std::size_t bound = size;
  65. std::size_t deflate_bound = beast::zlib::deflate_upper_bound(bound) + 5 + 6 + 1;
  66. while (deflate_bound < size)
  67. {
  68. bound += (size - deflate_bound) * 2;
  69. deflate_bound = beast::zlib::deflate_upper_bound(bound) + 5 + 6 + 1;
  70. }
  71. return (std::max)(bound, deflate_bound);
  72. }
  73. inline beast::zlib::deflate_stream& compressor() { return deflate_stream_; }
  74. inline beast::zlib::inflate_stream& uncompressor() { return inflate_stream_; }
  75. protected:
  76. template<class CompressOrUncompress>
  77. inline std::size_t calc_bound(CompressOrUncompress&, beast::zlib::Flush, std::size_t size)
  78. {
  79. using optype = std::remove_cv_t<std::remove_reference_t<CompressOrUncompress>>;
  80. if constexpr /**/ (std::is_same_v<beast::zlib::deflate_stream, optype>)
  81. {
  82. return compress_bound(size);
  83. }
  84. else if constexpr (std::is_same_v<beast::zlib::inflate_stream, optype>)
  85. {
  86. return uncompress_bound(size);
  87. }
  88. else
  89. {
  90. // http://www.purecpp.cn/detail?id=2293
  91. static_assert(!sizeof(CompressOrUncompress));
  92. }
  93. }
  94. template<class CompressOrUncompress>
  95. inline std::string work(CompressOrUncompress& op, beast::zlib::Flush flush, std::string_view data, asio::error_code& ec)
  96. {
  97. ec.clear();
  98. std::string result{};
  99. result.resize(calc_bound(op, flush, data.size()));
  100. beast::zlib::z_params zs{};
  101. zs.next_in = decltype(zs.next_in)(data.data());
  102. zs.avail_in = data.size();
  103. zs.next_out = decltype(zs.next_out)(result.data());
  104. zs.avail_out = result.size();
  105. while (true)
  106. {
  107. op.write(zs, flush, ec);
  108. if (ec)
  109. break;
  110. if (zs.avail_in == std::size_t(0))
  111. break;
  112. result.resize(result.size() + calc_bound(op, flush, zs.avail_in));
  113. zs.next_out = decltype(zs.next_out)(result.data() + zs.total_out);
  114. zs.avail_out = result.size() - zs.total_out;
  115. }
  116. if (!ec)
  117. result.resize(zs.total_out);
  118. else
  119. result.resize(0);
  120. return result;
  121. }
  122. protected:
  123. beast::zlib::deflate_stream deflate_stream_{};
  124. beast::zlib::inflate_stream inflate_stream_{};
  125. beast::zlib::Flush deflate_flush_{ beast::zlib::Flush::sync };
  126. beast::zlib::Flush inflate_flush_{ beast::zlib::Flush::sync };
  127. };
  128. }
  129. #endif // !__ASIO2_ZLIB_HPP__