|
||||
File indexing completed on 2025-01-18 09:29:37
0001 // 0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 0003 // 0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying 0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0006 // 0007 // Official repository: https://github.com/boostorg/beast 0008 // 0009 // This is a derivative work based on Zlib, copyright below: 0010 /* 0011 Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler 0012 0013 This software is provided 'as-is', without any express or implied 0014 warranty. In no event will the authors be held liable for any damages 0015 arising from the use of this software. 0016 0017 Permission is granted to anyone to use this software for any purpose, 0018 including commercial applications, and to alter it and redistribute it 0019 freely, subject to the following restrictions: 0020 0021 1. The origin of this software must not be misrepresented; you must not 0022 claim that you wrote the original software. If you use this software 0023 in a product, an acknowledgment in the product documentation would be 0024 appreciated but is not required. 0025 2. Altered source versions must be plainly marked as such, and must not be 0026 misrepresented as being the original software. 0027 3. This notice may not be removed or altered from any source distribution. 0028 0029 Jean-loup Gailly Mark Adler 0030 jloup@gzip.org madler@alumni.caltech.edu 0031 0032 The data format used by the zlib library is described by RFCs (Request for 0033 Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 0034 (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). 0035 */ 0036 0037 #ifndef BOOST_BEAST_ZLIB_INFLATE_STREAM_HPP 0038 #define BOOST_BEAST_ZLIB_INFLATE_STREAM_HPP 0039 0040 #include <boost/beast/core/detail/config.hpp> 0041 #include <boost/beast/zlib/detail/inflate_stream.hpp> 0042 0043 namespace boost { 0044 namespace beast { 0045 namespace zlib { 0046 0047 /** Raw deflate stream decompressor. 0048 0049 This implements a raw deflate stream decompressor. The deflate 0050 protocol is a compression protocol described in 0051 "DEFLATE Compressed Data Format Specification version 1.3" 0052 located here: https://tools.ietf.org/html/rfc1951 0053 0054 The implementation is a refactored port to C++ of ZLib's "inflate". 0055 A more detailed description of ZLib is at http://zlib.net/. 0056 0057 Compression can be done in a single step if the buffers are large 0058 enough (for example if an input file is memory mapped), or can be done 0059 by repeated calls of the compression function. In the latter case, the 0060 application must provide more input and/or consume the output (providing 0061 more output space) before each call. 0062 */ 0063 class inflate_stream 0064 : private detail::inflate_stream 0065 { 0066 public: 0067 /** Construct a raw deflate decompression stream. 0068 0069 The window size is set to the default of 15 bits. 0070 */ 0071 inflate_stream() = default; 0072 0073 /** Reset the stream. 0074 0075 This puts the stream in a newly constructed state with 0076 the previously specified window size, but without de-allocating 0077 any dynamically created structures. 0078 */ 0079 void 0080 reset() 0081 { 0082 doReset(); 0083 } 0084 0085 /** Reset the stream. 0086 0087 This puts the stream in a newly constructed state with the 0088 specified window size, but without de-allocating any dynamically 0089 created structures. 0090 */ 0091 void 0092 reset(int windowBits) 0093 { 0094 doReset(windowBits); 0095 } 0096 0097 /** Put the stream in a newly constructed state. 0098 0099 All dynamically allocated memory is de-allocated. 0100 */ 0101 void 0102 clear() 0103 { 0104 doClear(); 0105 } 0106 0107 /** Decompress input and produce output. 0108 0109 This function decompresses as much data as possible, and stops when 0110 the input buffer becomes empty or the output buffer becomes full. It 0111 may introduce some output latency (reading input without producing any 0112 output) except when forced to flush. 0113 0114 One or both of the following actions are performed: 0115 0116 @li Decompress more input starting at `zs.next_in` and update `zs.next_in` 0117 and `zs.avail_in` accordingly. If not all input can be processed (because 0118 there is not enough room in the output buffer), `zs.next_in` is updated 0119 and processing will resume at this point for the next call. 0120 0121 @li Provide more output starting at `zs.next_out` and update `zs.next_out` 0122 and `zs.avail_out` accordingly. `write` provides as much output as 0123 possible, until there is no more input data or no more space in the output 0124 buffer (see below about the flush parameter). 0125 0126 Before the call, the application should ensure that at least one of the 0127 actions is possible, by providing more input and/or consuming more output, 0128 and updating the values in `zs` accordingly. The application can consume 0129 the uncompressed output when it wants, for example when the output buffer 0130 is full (`zs.avail_out == 0`), or after each call. If `write` returns no 0131 error and with zero `zs.avail_out`, it must be called again after making 0132 room in the output buffer because there might be more output pending. 0133 0134 The flush parameter may be `Flush::none`, `Flush::sync`, `Flush::finish`, 0135 `Flush::block`, or `Flush::trees`. `Flush::sync` requests to flush as much 0136 output as possible to the output buffer. `Flush::block` requests to stop if 0137 and when it gets to the next deflate block boundary. When decoding the 0138 zlib or gzip format, this will cause `write` to return immediately after 0139 the header and before the first block. When doing a raw inflate, `write` will 0140 go ahead and process the first block, and will return when it gets to the 0141 end of that block, or when it runs out of data. 0142 0143 The `Flush::block` option assists in appending to or combining deflate 0144 streams. Also to assist in this, on return `write` will set `zs.data_type` 0145 to the number of unused bits in the last byte taken from `zs.next_in`, plus 0146 64 if `write` is currently decoding the last block in the deflate stream, 0147 plus 128 if `write` returned immediately after decoding an end-of-block code 0148 or decoding the complete header up to just before the first byte of the 0149 deflate stream. The end-of-block will not be indicated until all of the 0150 uncompressed data from that block has been written to `zs.next_out`. The 0151 number of unused bits may in general be greater than seven, except when 0152 bit 7 of `zs.data_type` is set, in which case the number of unused bits 0153 will be less than eight. `zs.data_type` is set as noted here every time 0154 `write` returns for all flush options, and so can be used to determine the 0155 amount of currently consumed input in bits. 0156 0157 The `Flush::trees` option behaves as `Flush::block` does, but it also returns 0158 when the end of each deflate block header is reached, before any actual data 0159 in that block is decoded. This allows the caller to determine the length of 0160 the deflate block header for later use in random access within a deflate block. 0161 256 is added to the value of `zs.data_type` when `write` returns immediately 0162 after reaching the end of the deflate block header. 0163 0164 `write` should normally be called until it returns `error::end_of_stream` or 0165 another error. However if all decompression is to be performed in a single 0166 step (a single call of `write`), the parameter flush should be set to 0167 `Flush::finish`. In this case all pending input is processed and all pending 0168 output is flushed; `zs.avail_out` must be large enough to hold all of the 0169 uncompressed data for the operation to complete. (The size of the uncompressed 0170 data may have been saved by the compressor for this purpose.) The use of 0171 `Flush::finish` is not required to perform an inflation in one step. However 0172 it may be used to inform inflate that a faster approach can be used for the 0173 single call. `Flush::finish` also informs inflate to not maintain a sliding 0174 window if the stream completes, which reduces inflate's memory footprint. 0175 If the stream does not complete, either because not all of the stream is 0176 provided or not enough output space is provided, then a sliding window will be 0177 allocated and `write` can be called again to continue the operation as if 0178 `Flush::none` had been used. 0179 0180 In this implementation, `write` always flushes as much output as possible to 0181 the output buffer, and always uses the faster approach on the first call. So 0182 the effects of the flush parameter in this implementation are on the return value 0183 of `write` as noted below, when `write` returns early when `Flush::block` or 0184 `Flush::trees` is used, and when `write` avoids the allocation of memory for a 0185 sliding window when `Flush::finish` is used. 0186 0187 If a preset dictionary is needed after this call, 0188 `write` sets `zs.adler` to the Adler-32 checksum of the dictionary chosen by 0189 the compressor and returns `error::need_dictionary`; otherwise it sets 0190 `zs.adler` to the Adler-32 checksum of all output produced so far (that is, 0191 `zs.total_out bytes`) and returns no error, `error::end_of_stream`, or an 0192 error code as described below. At the end of the stream, `write` checks that 0193 its computed adler32 checksum is equal to that saved by the compressor and 0194 returns `error::end_of_stream` only if the checksum is correct. 0195 0196 This function returns no error if some progress has been made (more input 0197 processed or more output produced), `error::end_of_stream` if the end of the 0198 compressed data has been reached and all uncompressed output has been produced, 0199 `error::need_dictionary` if a preset dictionary is needed at this point, 0200 `error::invalid_data` if the input data was corrupted (input stream not 0201 conforming to the zlib format or incorrect check value), `error::stream_error` 0202 if the stream structure was inconsistent (for example if `zs.next_in` or 0203 `zs.next_out` was null), `error::need_buffers` if no progress is possible or 0204 if there was not enough room in the output buffer when `Flush::finish` is 0205 used. Note that `error::need_buffers` is not fatal, and `write` can be called 0206 again with more input and more output space to continue decompressing. 0207 */ 0208 void 0209 write(z_params& zs, Flush flush, error_code& ec) 0210 { 0211 doWrite(zs, flush, ec); 0212 } 0213 }; 0214 0215 } // zlib 0216 } // beast 0217 } // boost 0218 0219 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |