From 811c86f7355111fbdac3fb6fd9cb39f0180ce59e Mon Sep 17 00:00:00 2001 From: Mohammad Nejati Date: Wed, 18 Sep 2024 07:46:29 +0000 Subject: [PATCH] zlib_service uses buffer types --- .../boost/http_proto/service/zlib_service.hpp | 21 ++-- src/serializer.cpp | 23 ++-- src_zlib/service/zlib_service.cpp | 114 +++++++----------- 3 files changed, 68 insertions(+), 90 deletions(-) diff --git a/include/boost/http_proto/service/zlib_service.hpp b/include/boost/http_proto/service/zlib_service.hpp index 87ceb442..a272b565 100644 --- a/include/boost/http_proto/service/zlib_service.hpp +++ b/include/boost/http_proto/service/zlib_service.hpp @@ -16,6 +16,9 @@ #include #include +#include +#include + namespace boost { namespace http_proto { namespace zlib { @@ -68,19 +71,19 @@ struct BOOST_HTTP_PROTO_ZLIB_DECL trees }; - struct params + struct results { - void const* next_in; - std::size_t avail_in; - void* next_out; - std::size_t avail_out; + std::size_t out_bytes; + std::size_t in_bytes; + system::error_code ec; + bool finished; }; - virtual bool + virtual results write( - params&, - flush, - system::error_code& ec) noexcept = 0; + buffers::mutable_buffer out, + buffers::const_buffer in, + flush) noexcept = 0; }; virtual diff --git a/src/serializer.cpp b/src/serializer.cpp index 496b6096..b56a2480 100644 --- a/src/serializer.cpp +++ b/src/serializer.cpp @@ -51,27 +51,24 @@ class deflator_filter for(;;) { - auto params = stream_t::params{ in.data(), in.size(), - out.data(), out.size() }; + auto r = deflator_.write(out, in, flush); - results.finished = - deflator_.write(params, flush, results.ec); + results.out_bytes += r.out_bytes; + results.in_bytes += r.in_bytes; + results.ec = r.ec; + results.finished = r.finished; - results.in_bytes += (in.size() - params.avail_in); - results.out_bytes += (out.size() - params.avail_out); - - if(results.ec || results.finished) + if(r.ec || r.finished) return results; - auto prev_out_size = out.size(); - in = buffers::suffix(in, params.avail_in); - out = buffers::suffix(out, params.avail_out); + out = buffers::sans_prefix(out, r.out_bytes); + in = buffers::sans_prefix(in, r.in_bytes); if(in.size() == 0) { - // TODO: is this necessary? - if(prev_out_size == params.avail_out) + if(r.out_bytes == 0) { + // TODO: is this necessary? flush = stream_t::flush::sync; continue; } diff --git a/src_zlib/service/zlib_service.cpp b/src_zlib/service/zlib_service.cpp index f12cbe3c..946f5e1c 100644 --- a/src_zlib/service/zlib_service.cpp +++ b/src_zlib/service/zlib_service.cpp @@ -254,55 +254,41 @@ struct service_impl { using key_type = service; - class BOOST_ATTRIBUTE_NODISCARD - adapter + static ::uInt + clamp(std::size_t x) noexcept { - static constexpr auto uint_max = - (std::numeric_limits<::uInt>::max)(); - z_stream* zs_; - stream::params* params_; - - public: - adapter( - z_stream* zs, - stream::params* params) noexcept - : zs_{ zs } - , params_{ params } - { - zs->next_in = reinterpret_cast( - const_cast(params->next_in)); - zs->avail_in = clamp(params->avail_in); - zs->next_out = reinterpret_cast( - params->next_out); - zs->avail_out = clamp(params->avail_out); - } - - static ::uInt - clamp(std::size_t x) noexcept - { - if(x >= uint_max) - return uint_max; - return static_cast<::uInt>(x); - } + if(x >= (std::numeric_limits<::uInt>::max)()) + return (std::numeric_limits<::uInt>::max)(); + return static_cast<::uInt>(x); + } - static std::size_t - unclamp(std::size_t org, ::uInt x) noexcept - { - if(org >= uint_max) - return org - uint_max + x; - return x; - } + static void + sync( + z_stream* zs, + buffers::mutable_buffer const& out, + buffers::const_buffer const& in) noexcept + { + zs->next_in = reinterpret_cast< + unsigned char*>(const_cast(in.data())); + zs->avail_in = clamp(in.size()); + zs->next_out = reinterpret_cast< + unsigned char*>(out.data()); + zs->avail_out = clamp(out.size()); + } - ~adapter() - { - *params_ = { - zs_->next_in, - unclamp(params_->avail_in, zs_->avail_in), - zs_->next_out, - unclamp(params_->avail_out, zs_->avail_out) - }; - } - }; + static stream::results + make_results( + z_stream const& zs, + buffers::mutable_buffer const& out, + buffers::const_buffer const& in, + int ret) noexcept + { + return { + clamp(out.size()) - zs.avail_out, + clamp(in.size()) - zs.avail_in, + ret < 0 ? static_cast(ret) : system::error_code{}, + ret == Z_STREAM_END }; + } class deflator : public stream @@ -326,19 +312,15 @@ struct service_impl throw_zlib_error(ret); } - virtual bool + virtual results write( - params& params, - flush flush, - system::error_code& ec) noexcept override + buffers::mutable_buffer out, + buffers::const_buffer in, + flush flush) noexcept override { - auto adp = adapter{ &zs_, ¶ms }; - auto ret = deflate(&zs_, static_cast(flush)); - if(ret == Z_STREAM_END) - return true; - if(ret != Z_OK) - ec = static_cast(ret); - return false; + sync(&zs_, out, in); + return make_results(zs_, out, in, + deflate(&zs_, static_cast(flush))); } }; @@ -361,19 +343,15 @@ struct service_impl throw_zlib_error(ret); } - virtual bool + virtual results write( - params& params, - flush flush, - system::error_code& ec) noexcept override + buffers::mutable_buffer out, + buffers::const_buffer in, + flush flush) noexcept override { - auto adp = adapter{ &zs_, ¶ms }; - auto ret = inflate(&zs_, static_cast(flush)); - if(ret == Z_STREAM_END) - return true; - if(ret != Z_OK) - ec = static_cast(ret); - return false; + sync(&zs_, out, in); + return make_results(zs_, out, in, + inflate(&zs_, static_cast(flush))); } };