]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/include/beast/zlib/inflate_stream.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / Beast / include / beast / zlib / inflate_stream.hpp
1 //
2 // Copyright (c) 2013-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 // This is a derivative work based on Zlib, copyright below:
8 /*
9 Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
10
11 This software is provided 'as-is', without any express or implied
12 warranty. In no event will the authors be held liable for any damages
13 arising from the use of this software.
14
15 Permission is granted to anyone to use this software for any purpose,
16 including commercial applications, and to alter it and redistribute it
17 freely, subject to the following restrictions:
18
19 1. The origin of this software must not be misrepresented; you must not
20 claim that you wrote the original software. If you use this software
21 in a product, an acknowledgment in the product documentation would be
22 appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and must not be
24 misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source distribution.
26
27 Jean-loup Gailly Mark Adler
28 jloup@gzip.org madler@alumni.caltech.edu
29
30 The data format used by the zlib library is described by RFCs (Request for
31 Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
32 (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
33 */
34
35 #ifndef BEAST_ZLIB_INFLATE_STREAM_HPP
36 #define BEAST_ZLIB_INFLATE_STREAM_HPP
37
38 #include <beast/config.hpp>
39 #include <beast/zlib/detail/inflate_stream.hpp>
40
41 namespace beast {
42 namespace zlib {
43
44 /** Raw deflate stream decompressor.
45
46 This implements a raw deflate stream decompressor. The deflate
47 protocol is a compression protocol described in
48 "DEFLATE Compressed Data Format Specification version 1.3"
49 located here: https://tools.ietf.org/html/rfc1951
50
51 The implementation is a refactored port to C++ of ZLib's "inflate".
52 A more detailed description of ZLib is at http://zlib.net/.
53
54 Compression can be done in a single step if the buffers are large
55 enough (for example if an input file is memory mapped), or can be done
56 by repeated calls of the compression function. In the latter case, the
57 application must provide more input and/or consume the output (providing
58 more output space) before each call.
59 */
60 class inflate_stream
61 : private detail::inflate_stream
62 {
63 public:
64 /** Construct a raw deflate decompression stream.
65
66 The window size is set to the default of 15 bits.
67 */
68 inflate_stream() = default;
69
70 /** Reset the stream.
71
72 This puts the stream in a newly constructed state with
73 the previously specified window size, but without de-allocating
74 any dynamically created structures.
75 */
76 void
77 reset()
78 {
79 doReset();
80 }
81
82 /** Reset the stream.
83
84 This puts the stream in a newly constructed state with the
85 specified window size, but without de-allocating any dynamically
86 created structures.
87 */
88 void
89 reset(int windowBits)
90 {
91 doReset(windowBits);
92 }
93
94 /** Put the stream in a newly constructed state.
95
96 All dynamically allocated memory is de-allocated.
97 */
98 void
99 clear()
100 {
101 doClear();
102 }
103
104 /** Decompress input and produce output.
105
106 This function decompresses as much data as possible, and stops when
107 the input buffer becomes empty or the output buffer becomes full. It
108 may introduce some output latency (reading input without producing any
109 output) except when forced to flush.
110
111 One or both of the following actions are performed:
112
113 @li Decompress more input starting at `zs.next_in` and update `zs.next_in`
114 and `zs.avail_in` accordingly. If not all input can be processed (because
115 there is not enough room in the output buffer), `zs.next_in` is updated
116 and processing will resume at this point for the next call.
117
118 @li Provide more output starting at `zs.next_out` and update `zs.next_out`
119 and `zs.avail_out` accordingly. `write` provides as much output as
120 possible, until there is no more input data or no more space in the output
121 buffer (see below about the flush parameter).
122
123 Before the call, the application should ensure that at least one of the
124 actions is possible, by providing more input and/or consuming more output,
125 and updating the values in `zs` accordingly. The application can consume
126 the uncompressed output when it wants, for example when the output buffer
127 is full (`zs.avail_out == 0`), or after each call. If `write` returns no
128 error and with zero `zs.avail_out`, it must be called again after making
129 room in the output buffer because there might be more output pending.
130
131 The flush parameter may be `Flush::none`, `Flush::sync`, `Flush::finish`,
132 `Flush::block`, or `Flush::trees`. `Flush::sync` requests to flush as much
133 output as possible to the output buffer. `Flush::block` requests to stop if
134 and when it gets to the next deflate block boundary. When decoding the
135 zlib or gzip format, this will cause `write` to return immediately after
136 the header and before the first block. When doing a raw inflate, `write` will
137 go ahead and process the first block, and will return when it gets to the
138 end of that block, or when it runs out of data.
139
140 The `Flush::block` option assists in appending to or combining deflate
141 streams. Also to assist in this, on return `write` will set `zs.data_type`
142 to the number of unused bits in the last byte taken from `zs.next_in`, plus
143 64 if `write` is currently decoding the last block in the deflate stream,
144 plus 128 if `write` returned immediately after decoding an end-of-block code
145 or decoding the complete header up to just before the first byte of the
146 deflate stream. The end-of-block will not be indicated until all of the
147 uncompressed data from that block has been written to `zs.next_out`. The
148 number of unused bits may in general be greater than seven, except when
149 bit 7 of `zs.data_type` is set, in which case the number of unused bits
150 will be less than eight. `zs.data_type` is set as noted here every time
151 `write` returns for all flush options, and so can be used to determine the
152 amount of currently consumed input in bits.
153
154 The `Flush::trees` option behaves as `Flush::block` does, but it also returns
155 when the end of each deflate block header is reached, before any actual data
156 in that block is decoded. This allows the caller to determine the length of
157 the deflate block header for later use in random access within a deflate block.
158 256 is added to the value of `zs.data_type` when `write` returns immediately
159 after reaching the end of the deflate block header.
160
161 `write` should normally be called until it returns `error::end_of_stream` or
162 another error. However if all decompression is to be performed in a single
163 step (a single call of `write`), the parameter flush should be set to
164 `Flush::finish`. In this case all pending input is processed and all pending
165 output is flushed; `zs.avail_out` must be large enough to hold all of the
166 uncompressed data for the operation to complete. (The size of the uncompressed
167 data may have been saved by the compressor for this purpose.) The use of
168 `Flush::finish` is not required to perform an inflation in one step. However
169 it may be used to inform inflate that a faster approach can be used for the
170 single call. `Flush::finish` also informs inflate to not maintain a sliding
171 window if the stream completes, which reduces inflate's memory footprint.
172 If the stream does not complete, either because not all of the stream is
173 provided or not enough output space is provided, then a sliding window will be
174 allocated and `write` can be called again to continue the operation as if
175 `Flush::none` had been used.
176
177 In this implementation, `write` always flushes as much output as possible to
178 the output buffer, and always uses the faster approach on the first call. So
179 the effects of the flush parameter in this implementation are on the return value
180 of `write` as noted below, when `write` returns early when `Flush::block` or
181 `Flush::trees` is used, and when `write` avoids the allocation of memory for a
182 sliding window when `Flush::finsih` is used.
183
184 If a preset dictionary is needed after this call,
185 `write` sets `zs.adler` to the Adler-32 checksum of the dictionary chosen by
186 the compressor and returns `error::need_dictionary`; otherwise it sets
187 `zs.adler` to the Adler-32 checksum of all output produced so far (that is,
188 `zs.total_out bytes`) and returns no error, `error::end_of_stream`, or an
189 error code as described below. At the end of the stream, `write` checks that
190 its computed adler32 checksum is equal to that saved by the compressor and
191 returns `error::end_of_stream` only if the checksum is correct.
192
193 This function returns no error if some progress has been made (more input
194 processed or more output produced), `error::end_of_stream` if the end of the
195 compressed data has been reached and all uncompressed output has been produced,
196 `error::need_dictionary` if a preset dictionary is needed at this point,
197 `error::invalid_data` if the input data was corrupted (input stream not
198 conforming to the zlib format or incorrect check value), `error::stream_error`
199 if the stream structure was inconsistent (for example if `zs.next_in` or
200 `zs.next_out` was null), `error::need_buffers` if no progress is possible or
201 if there was not enough room in the output buffer when `Flush::finish` is
202 used. Note that `error::need_buffers` is not fatal, and `write` can be called
203 again with more input and more output space to continue decompressing.
204 */
205 void
206 write(z_params& zs, Flush flush, error_code& ec)
207 {
208 doWrite(zs, flush, ec);
209 }
210 };
211
212 } // zlib
213 } // beast
214
215 #endif