Line data Source code
1 : //
2 : // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2024 Christian Mazakas
4 : //
5 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 : //
8 : // Official repository: https://github.com/cppalliance/http_proto
9 : //
10 :
11 : #ifndef BOOST_HTTP_PROTO_SERVICE_ZLIB_SERVICE_HPP
12 : #define BOOST_HTTP_PROTO_SERVICE_ZLIB_SERVICE_HPP
13 :
14 : #include <boost/http_proto/context.hpp>
15 : #include <boost/http_proto/detail/config.hpp>
16 : #include <boost/http_proto/detail/workspace.hpp>
17 : #include <boost/http_proto/service/service.hpp>
18 :
19 : #include <boost/buffers/const_buffer.hpp>
20 : #include <boost/buffers/mutable_buffer.hpp>
21 :
22 : namespace boost {
23 : namespace http_proto {
24 : namespace zlib {
25 :
26 : struct decoder_config
27 : {
28 : unsigned max_window_bits = 15;
29 : unsigned mem_level = 8;
30 : };
31 :
32 : //------------------------------------------------
33 :
34 : constexpr
35 : inline
36 : std::size_t
37 24 : encoding_size_hint(decoder_config cfg = {}) noexcept
38 : {
39 : // from: https://www.zlib.net/zlib_tech.html
40 : //
41 : // Memory Footprint
42 : //
43 : // zlib's memory footprint can also be specified fairly
44 : // precisely. It is larger for compression than for
45 : // decompression, and the exact requirements depend on
46 : // how the library was compiled.
47 : //
48 : // The memory requirements for compression depend on two
49 : // parameters, windowBits and memLevel:
50 : // deflate memory usage (bytes) = (1 << (windowBits+2)) + (1 << (memLevel+9)) + 6 KB
51 : return
52 24 : (1 << (cfg.max_window_bits + 2)) +
53 24 : (1 << (cfg.mem_level + 9)) +
54 24 : (6 * 1024);
55 : }
56 :
57 : struct BOOST_HTTP_PROTO_ZLIB_DECL
58 : service
59 : : http_proto::service
60 : {
61 : struct stream
62 : {
63 : enum class flush
64 : {
65 : none,
66 : partial,
67 : sync,
68 : full,
69 : finish,
70 : block,
71 : trees
72 : };
73 :
74 : struct results
75 : {
76 : std::size_t out_bytes;
77 : std::size_t in_bytes;
78 : system::error_code ec;
79 : bool finished;
80 : };
81 :
82 : virtual results
83 : write(
84 : buffers::mutable_buffer out,
85 : buffers::const_buffer in,
86 : flush) noexcept = 0;
87 : };
88 :
89 : virtual
90 : std::size_t
91 : space_needed() const noexcept = 0;
92 :
93 : virtual stream&
94 : make_deflator(
95 : http_proto::detail::workspace& ws,
96 : int level,
97 : int window_bits,
98 : int mem_level) const = 0;
99 :
100 : virtual stream&
101 : make_inflator(
102 : http_proto::detail::workspace& ws,
103 : int window_bits) const = 0;
104 : };
105 :
106 : void BOOST_HTTP_PROTO_ZLIB_DECL
107 : install_service(context& ctx);
108 :
109 : } // zlib
110 : } // http_proto
111 : } // boost
112 :
113 : #endif
|