]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/zlib/deflate_stream.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / zlib / deflate_stream.hpp
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/core/detail/config.hpp>
14 #include <boost/beast/zlib/error.hpp>
15 #include <boost/beast/zlib/zlib.hpp>
16 #include <boost/beast/zlib/detail/deflate_stream.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
55 /** Raw deflate compressor.
56
57 This is a port of zlib's "deflate" functionality to C++.
58 */
59 class deflate_stream
60 : private detail::deflate_stream
61 {
62 public:
63 /** Construct a default deflate stream.
64
65 Upon construction, the stream settings will be set
66 to these default values:
67
68 @li `level = 6`
69
70 @li `windowBits = 15`
71
72 @li `memLevel = 8`
73
74 @li `strategy = Strategy::normal`
75
76 Although the stream is ready to be used immediately
77 after construction, any required internal buffers are
78 not dynamically allocated until needed.
79 */
80 deflate_stream()
81 {
82 reset(6, 15, DEF_MEM_LEVEL, Strategy::normal);
83 }
84
85 /** Reset the stream and compression settings.
86
87 This function initializes the stream to the specified
88 compression settings.
89
90 Although the stream is ready to be used immediately
91 after a reset, any required internal buffers are not
92 dynamically allocated until needed.
93
94 @note Any unprocessed input or pending output from
95 previous calls are discarded.
96 */
97 void
98 reset(
99 int level,
100 int windowBits,
101 int memLevel,
102 Strategy strategy)
103 {
104 doReset(level, windowBits, memLevel, strategy);
105 }
106
107 /** Reset the stream without deallocating memory.
108
109 This function performs the equivalent of calling `clear`
110 followed by `reset` with the same compression settings,
111 without deallocating the internal buffers.
112
113 @note Any unprocessed input or pending output from
114 previous calls are discarded.
115 */
116 void
117 reset()
118 {
119 doReset();
120 }
121
122 /** Clear the stream.
123
124 This function resets the stream and frees all dynamically
125 allocated internal buffers. The compression settings are
126 left unchanged.
127
128 @note Any unprocessed input or pending output from
129 previous calls are discarded.
130 */
131 void
132 clear()
133 {
134 doClear();
135 }
136
137 /** Returns the upper limit on the size of a compressed block.
138
139 This function makes a conservative estimate of the maximum number
140 of bytes needed to store the result of compressing a block of
141 data based on the current compression level and strategy.
142
143 @param sourceLen The size of the uncompressed data.
144
145 @return The maximum number of resulting compressed bytes.
146 */
147 std::size_t
148 upper_bound(std::size_t sourceLen) const
149 {
150 return doUpperBound(sourceLen);
151 }
152
153 /** Fine tune internal compression parameters.
154
155 Compression parameters should only be tuned by someone who
156 understands the algorithm used by zlib's deflate for searching
157 for the best matching string, and even then only by the most
158 fanatic optimizer trying to squeeze out the last compressed bit
159 for their specific input data. Read the deflate.c source code
160 (ZLib) for the meaning of the max_lazy, good_length, nice_length,
161 and max_chain parameters.
162 */
163 void
164 tune(
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
173 /** Compress input and write output.
174
175 This function compresses as much data as possible, and stops when
176 the input buffer becomes empty or the output buffer becomes full.
177 It may introduce some output latency (reading input without
178 producing any output) except when forced to flush.
179
180 In each call, one or both of these actions are performed:
181
182 @li Compress more input starting at `zs.next_in` and update
183 `zs.next_in` and `zs.avail_in` accordingly. If not all
184 input can be processed (because there is not enough room in
185 the output buffer), `zs.next_in` and `zs.avail_in` are updated
186 and processing will resume at this point for the next call.
187
188 @li Provide more output starting at `zs.next_out` and update
189 `zs.next_out` and `zs.avail_out` accordingly. This action is
190 forced if the parameter flush is not `Flush::none`. Forcing
191 flush frequently degrades the compression ratio, so this parameter
192 should be set only when necessary (in interactive applications).
193 Some output may be provided even if flush is not set.
194
195 Before the call, the application must ensure that at least one
196 of the actions is possible, by providing more input and/or
197 consuming more output, and updating `zs.avail_in` or `zs.avail_out`
198 accordingly; `zs.avail_out` should never be zero before the call.
199 The application can consume the compressed output when it wants,
200 for example when the output buffer is full (`zs.avail_out == 0`),
201 or after each call of `write`. If `write` returns no error
202 with zero `zs.avail_out`, it must be called again after making
203 room in the output buffer because there might be more output
204 pending.
205
206 Normally the parameter flush is set to `Flush::none`, which allows
207 deflate to decide how much data to accumulate before producing
208 output, in order to maximize compression.
209
210 If the parameter flush is set to `Flush::sync`, all pending output
211 is flushed to the output buffer and the output is aligned on a
212 byte boundary, so that the decompressor can get all input data
213 available so far. In particular `zs.avail_in` is zero after the
214 call if enough output space has been provided before the call.
215 Flushing may degrade compression for some compression algorithms
216 and so it should be used only when necessary. This completes the
217 current deflate block and follows it with an empty stored block
218 that is three bits plus filler bits to the next byte, followed
219 by the four bytes `{ 0x00, 0x00 0xff 0xff }`.
220
221 If flush is set to `Flush::partial`, all pending output is flushed
222 to the output buffer, but the output is not aligned to a byte
223 boundary. All of the input data so far will be available to the
224 decompressor, as for Z_SYNC_FLUSH. This completes the current
225 deflate block and follows it with an empty fixed codes block that
226 is 10 bits long. This assures that enough bytes are output in order
227 for the decompressor to finish the block before the empty fixed
228 code block.
229
230 If flush is set to `Flush::block`, a deflate block is completed
231 and emitted, as for `Flush::sync`, but the output is not aligned
232 on a byte boundary, and up to seven bits of the current block are
233 held to be written as the next byte after the next deflate block
234 is completed. In this case, the decompressor may not be provided
235 enough bits at this point in order to complete decompression of
236 the data provided so far to the compressor. It may need to wait
237 for the next block to be emitted. This is for advanced applications
238 that need to control the emission of deflate blocks.
239
240 If flush is set to `Flush::full`, all output is flushed as with
241 `Flush::sync`, and the compression state is reset so that
242 decompression can restart from this point if previous compressed
243 data has been damaged or if random access is desired. Using
244 `Flush::full` too often can seriously degrade compression.
245
246 If `write` returns with `zs.avail_out == 0`, this function must
247 be called again with the same value of the flush parameter and
248 more output space (updated `zs.avail_out`), until the flush is
249 complete (`write` returns with non-zero `zs.avail_out`). In the
250 case of a `Flush::full`or `Flush::sync`, make sure that
251 `zs.avail_out` is greater than six to avoid repeated flush markers
252 due to `zs.avail_out == 0` on return.
253
254 If the parameter flush is set to `Flush::finish`, pending input
255 is processed, pending output is flushed and deflate returns the
256 error `error::end_of_stream` if there was enough output space;
257 if deflate returns with no error, this function must be called
258 again with `Flush::finish` and more output space (updated
259 `zs.avail_out`) but no more input data, until it returns the
260 error `error::end_of_stream` or another error. After `write` has
261 returned the `error::end_of_stream` error, the only possible
262 operations on the stream are to reset or destroy.
263
264 `Flush::finish` can be used immediately after initialization
265 if all the compression is to be done in a single step. In this
266 case, `zs.avail_out` must be at least value returned by
267 `upper_bound` (see below). Then `write` is guaranteed to return
268 the `error::end_of_stream` error. If not enough output space
269 is provided, deflate will not return `error::end_of_stream`,
270 and it must be called again as described above.
271
272 `write` returns no error if some progress has been made (more
273 input processed or more output produced), `error::end_of_stream`
274 if all input has been consumed and all output has been produced
275 (only when flush is set to `Flush::finish`), `error::stream_error`
276 if the stream state was inconsistent (for example if `zs.next_in`
277 or `zs.next_out` was `nullptr`), `error::need_buffers` if no
278 progress is possible (for example `zs.avail_in` or `zs.avail_out`
279 was zero). Note that `error::need_buffers` is not fatal, and
280 `write` can be called again with more input and more output space
281 to continue compressing.
282 */
283 void
284 write(
285 z_params& zs,
286 Flush flush,
287 error_code& ec)
288 {
289 doWrite(zs, flush, ec);
290 }
291
292 /** Update the compression level and strategy.
293
294 This function dynamically updates the compression level and
295 compression strategy. The interpretation of level and strategy
296 is as in @ref reset. This can be used to switch between compression
297 and straight copy of the input data, or to switch to a different kind
298 of input data requiring a different strategy. If the compression level
299 is changed, the input available so far is compressed with the old level
300 (and may be flushed); the new level will take effect only at the next
301 call of @ref write.
302
303 Before the call of `params`, the stream state must be set as for a
304 call of @ref write, since the currently available input may have to be
305 compressed and flushed. In particular, `zs.avail_out` must be non-zero.
306
307 @return `Z_OK` if success, `Z_STREAM_ERROR` if the source stream state
308 was inconsistent or if a parameter was invalid, `error::need_buffers`
309 if `zs.avail_out` was zero.
310 */
311 void
312 params(
313 z_params& zs,
314 int level,
315 Strategy strategy,
316 error_code& ec)
317 {
318 doParams(zs, level, strategy, ec);
319 }
320
321 /** Return bits pending in the output.
322
323 This function returns the number of bytes and bits of output
324 that have been generated, but not yet provided in the available
325 output. The bytes not provided would be due to the available
326 output space having being consumed. The number of bits of output
327 not provided are between 0 and 7, where they await more bits to
328 join them in order to fill out a full byte. If pending or bits
329 are `nullptr`, then those values are not set.
330
331 @return `Z_OK` if success, or `Z_STREAM_ERROR` if the source
332 stream state was inconsistent.
333 */
334 void
335 pending(unsigned *value, int *bits)
336 {
337 doPending(value, bits);
338 }
339
340 /** Insert bits into the compressed output stream.
341
342 This function inserts bits in the deflate output stream. The
343 intent is that this function is used to start off the deflate
344 output with the bits leftover from a previous deflate stream when
345 appending to it. As such, this function can only be used for raw
346 deflate, and must be used before the first `write` call after an
347 initialization. `bits` must be less than or equal to 16, and that
348 many of the least significant bits of `value` will be inserted in
349 the output.
350
351 @return `error::need_buffers` if there was not enough room in
352 the internal buffer to insert the bits.
353 */
354 void
355 prime(int bits, int value, error_code& ec)
356 {
357 return doPrime(bits, value, ec);
358 }
359 };
360
361 /** Returns the upper limit on the size of a compressed block.
362
363 This function makes a conservative estimate of the maximum number
364 of bytes needed to store the result of compressing a block of
365 data.
366
367 @param bytes The size of the uncompressed data.
368
369 @return The maximum number of resulting compressed bytes.
370 */
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