ゴミ箱
deflate_stream.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_ZLIB_DEFLATE_STREAM_HPP
11 #define BOOST_BEAST_ZLIB_DEFLATE_STREAM_HPP
12 
13 #include <boost/beast/config.hpp>
17 #include <algorithm>
18 #include <cstdlib>
19 #include <cstdint>
20 #include <cstring>
21 #include <memory>
22 
23 namespace boost {
24 namespace beast {
25 namespace zlib {
26 
27 // This is a derivative work based on Zlib, copyright below:
28 /*
29  Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
30 
31  This software is provided 'as-is', without any express or implied
32  warranty. In no event will the authors be held liable for any damages
33  arising from the use of this software.
34 
35  Permission is granted to anyone to use this software for any purpose,
36  including commercial applications, and to alter it and redistribute it
37  freely, subject to the following restrictions:
38 
39  1. The origin of this software must not be misrepresented; you must not
40  claim that you wrote the original software. If you use this software
41  in a product, an acknowledgment in the product documentation would be
42  appreciated but is not required.
43  2. Altered source versions must be plainly marked as such, and must not be
44  misrepresented as being the original software.
45  3. This notice may not be removed or altered from any source distribution.
46 
47  Jean-loup Gailly Mark Adler
48  jloup@gzip.org madler@alumni.caltech.edu
49 
50  The data format used by the zlib library is described by RFCs (Request for
51  Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
52  (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
53 */
54 
60  : private detail::deflate_stream
61 {
62 public:
81  {
83  }
84 
97  void
99  int level,
100  int windowBits,
101  int memLevel,
102  Strategy strategy)
103  {
104  doReset(level, windowBits, memLevel, strategy);
105  }
106 
116  void
118  {
119  doReset();
120  }
121 
131  void
133  {
134  doClear();
135  }
136 
147  std::size_t
148  upper_bound(std::size_t sourceLen) const
149  {
150  return doUpperBound(sourceLen);
151  }
152 
163  void
165  int good_length,
166  int max_lazy,
167  int nice_length,
168  int max_chain)
169  {
170  doTune(good_length, max_lazy, nice_length, max_chain);
171  }
172 
283  void
285  z_params& zs,
286  Flush flush,
287  error_code& ec)
288  {
289  doWrite(zs, flush, ec);
290  }
291 
311  void
313  z_params& zs,
314  int level,
315  Strategy strategy,
316  error_code& ec)
317  {
318  doParams(zs, level, strategy, ec);
319  }
320 
334  void
335  pending(unsigned *value, int *bits)
336  {
337  doPending(value, bits);
338  }
339 
354  void
355  prime(int bits, int value, error_code& ec)
356  {
357  return doPrime(bits, value, ec);
358  }
359 };
360 
371 std::size_t
372 deflate_upper_bound(std::size_t bytes);
373 
374 /* For the default windowBits of 15 and memLevel of 8, this function returns
375  a close to exact, as well as small, upper bound on the compressed size.
376  They are coded as constants here for a reason--if the #define's are
377  changed, then this function needs to be changed as well. The return
378  value for 15 and 8 only works for those exact settings.
379 
380  For any setting other than those defaults for windowBits and memLevel,
381  the value returned is a conservative worst case for the maximum expansion
382  resulting from using fixed blocks instead of stored blocks, which deflate
383  can emit on compressed data for some combinations of the parameters.
384 
385  This function could be more sophisticated to provide closer upper bounds for
386  every combination of windowBits and memLevel. But even the conservative
387  upper bound of about 14% expansion does not seem onerous for output buffer
388  allocation.
389 */
390 inline
391 std::size_t
392 deflate_upper_bound(std::size_t bytes)
393 {
394  return bytes +
395  ((bytes + 7) >> 3) +
396  ((bytes + 63) >> 6) + 5 +
397  6;
398 }
399 
400 } // zlib
401 } // beast
402 } // boost
403 
404 #endif
deflate_stream()
Definition: deflate_stream.hpp:80
Definition: async_result.hpp:20
void reset(int level, int windowBits, int memLevel, Strategy strategy)
Definition: deflate_stream.hpp:98
Definition: zlib.hpp:79
void clear()
Definition: deflate_stream.hpp:132
Definition: deflate_stream.hpp:59
void doParams(z_params &zs, int level, Strategy strategy, error_code &ec)
Definition: deflate_stream.hpp:990
void params(z_params &zs, int level, Strategy strategy, error_code &ec)
Definition: deflate_stream.hpp:312
Flush
Definition: zlib.hpp:114
void tune(int good_length, int max_lazy, int nice_length, int max_chain)
Definition: deflate_stream.hpp:164
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
std::size_t upper_bound(std::size_t sourceLen) const
Definition: deflate_stream.hpp:148
void pending(unsigned *value, int *bits)
Definition: deflate_stream.hpp:335
void reset()
Definition: deflate_stream.hpp:117
Definition: deflate_stream.hpp:110
std::size_t deflate_upper_bound(std::size_t bytes)
Definition: deflate_stream.hpp:392
void write(z_params &zs, Flush flush, error_code &ec)
Definition: deflate_stream.hpp:284
void doTune(int good_length, int max_lazy, int nice_length, int max_chain)
Definition: deflate_stream.hpp:975
void prime(int bits, int value, error_code &ec)
Definition: deflate_stream.hpp:355
static std::uint8_t constexpr DEF_MEM_LEVEL
Definition: deflate_stream.hpp:164
void doWrite(z_params &zs, boost::optional< Flush > flush, error_code &ec)
Definition: deflate_stream.hpp:1029
void doReset()
Definition: deflate_stream.hpp:934
void doClear()
Definition: deflate_stream.hpp:942
std::size_t doUpperBound(std::size_t sourceLen) const
Definition: deflate_stream.hpp:951
void doPrime(int bits, int value, error_code &ec)
Definition: deflate_stream.hpp:1227
Strategy
Definition: zlib.hpp:140
void doPending(unsigned *value, int *bits)
Definition: deflate_stream.hpp:1254