Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:36

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-2013 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_DETAIL_INFLATE_STREAM_HPP
0038 #define BOOST_BEAST_ZLIB_DETAIL_INFLATE_STREAM_HPP
0039 
0040 #include <boost/beast/zlib/error.hpp>
0041 #include <boost/beast/zlib/zlib.hpp>
0042 #include <boost/beast/zlib/detail/bitstream.hpp>
0043 #include <boost/beast/zlib/detail/ranges.hpp>
0044 #include <boost/beast/zlib/detail/window.hpp>
0045 #if 0
0046 #include <boost/beast/core/detail/type_traits.hpp>
0047 #include <boost/throw_exception.hpp>
0048 #include <algorithm>
0049 #include <array>
0050 #include <cstdint>
0051 #include <cstring>
0052 #include <stdexcept>
0053 #endif
0054 
0055 namespace boost {
0056 namespace beast {
0057 namespace zlib {
0058 namespace detail {
0059 
0060 class inflate_stream
0061 {
0062 protected:
0063     inflate_stream()
0064     {
0065         w_.reset(15);
0066     }
0067 
0068     BOOST_BEAST_DECL
0069     void
0070     doClear();
0071 
0072     BOOST_BEAST_DECL
0073     void
0074     doReset(int windowBits);
0075 
0076     BOOST_BEAST_DECL
0077     void
0078     doWrite(z_params& zs, Flush flush, error_code& ec);
0079 
0080     void
0081     doReset()
0082     {
0083         doReset(w_.bits());
0084     }
0085 
0086 private:
0087     enum Mode
0088     {
0089         HEAD,       // i: waiting for magic header
0090         FLAGS,      // i: waiting for method and flags (gzip)
0091         TIME,       // i: waiting for modification time (gzip)
0092         OS,         // i: waiting for extra flags and operating system (gzip)
0093         EXLEN,      // i: waiting for extra length (gzip)
0094         EXTRA,      // i: waiting for extra bytes (gzip)
0095         NAME,       // i: waiting for end of file name (gzip)
0096         COMMENT,    // i: waiting for end of comment (gzip)
0097         HCRC,       // i: waiting for header crc (gzip)
0098         TYPE,       // i: waiting for type bits, including last-flag bit
0099         TYPEDO,     // i: same, but skip check to exit inflate on new block
0100         STORED,     // i: waiting for stored size (length and complement)
0101         COPY_,      // i/o: same as COPY below, but only first time in
0102         COPY,       // i/o: waiting for input or output to copy stored block
0103         TABLE,      // i: waiting for dynamic block table lengths
0104         LENLENS,    // i: waiting for code length code lengths
0105         CODELENS,   // i: waiting for length/lit and distance code lengths
0106             LEN_,   // i: same as LEN below, but only first time in
0107             LEN,    // i: waiting for length/lit/eob code
0108             LENEXT, // i: waiting for length extra bits
0109             DIST,   // i: waiting for distance code
0110             DISTEXT,// i: waiting for distance extra bits
0111             MATCH,  // o: waiting for output space to copy string
0112             LIT,    // o: waiting for output space to write literal
0113         CHECK,      // i: waiting for 32-bit check value
0114         LENGTH,     // i: waiting for 32-bit length (gzip)
0115         DONE,       // finished check, done -- remain here until reset
0116         BAD,        // got a data error -- remain here until reset
0117         SYNC        // looking for synchronization bytes to restart inflate()
0118     };
0119 
0120     /*  Structure for decoding tables.  Each entry provides either the
0121         information needed to do the operation requested by the code that
0122         indexed that table entry, or it provides a pointer to another
0123         table that indexes more bits of the code.  op indicates whether
0124         the entry is a pointer to another table, a literal, a length or
0125         distance, an end-of-block, or an invalid code.  For a table
0126         pointer, the low four bits of op is the number of index bits of
0127         that table.  For a length or distance, the low four bits of op
0128         is the number of extra bits to get after the code.  bits is
0129         the number of bits in this code or part of the code to drop off
0130         of the bit buffer.  val is the actual byte to output in the case
0131         of a literal, the base length or distance, or the offset from
0132         the current table to the next table.  Each entry is four bytes.
0133 
0134         op values as set by inflate_table():
0135 
0136         00000000 - literal
0137         0000tttt - table link, tttt != 0 is the number of table index bits
0138         0001eeee - length or distance, eeee is the number of extra bits
0139         01100000 - end of block
0140         01000000 - invalid code
0141     */
0142     struct code
0143     {
0144         std::uint8_t  op;   // operation, extra bits, table bits
0145         std::uint8_t  bits; // bits in this part of the code
0146         std::uint16_t val;  // offset in table or code value
0147     };
0148 
0149     /*  Maximum size of the dynamic table.  The maximum number of code
0150         structures is 1444, which is the sum of 852 for literal/length codes
0151         and 592 for distance codes.  These values were found by exhaustive
0152         searches using the program examples/enough.c found in the zlib
0153         distribtution.  The arguments to that program are the number of
0154         symbols, the initial root table size, and the maximum bit length
0155         of a code.  "enough 286 9 15" for literal/length codes returns
0156         returns 852, and "enough 30 6 15" for distance codes returns 592.
0157         The initial root table size (9 or 6) is found in the fifth argument
0158         of the inflate_table() calls in inflate.c and infback.c.  If the
0159         root table size is changed, then these maximum sizes would be need
0160         to be recalculated and updated.
0161     */
0162     static std::uint16_t constexpr kEnoughLens = 852;
0163     static std::uint16_t constexpr kEnoughDists = 592;
0164     static std::uint16_t constexpr kEnough = kEnoughLens + kEnoughDists;
0165 
0166     struct codes
0167     {
0168         code const* lencode;
0169         code const* distcode;
0170         unsigned lenbits; // VFALCO use std::uint8_t
0171         unsigned distbits;
0172     };
0173 
0174     // Type of code to build for inflate_table()
0175     enum class build
0176     {
0177         codes,
0178         lens,
0179         dists
0180     };
0181 
0182     BOOST_BEAST_DECL
0183     static
0184     void
0185     inflate_table(
0186         build type,
0187         std::uint16_t* lens,
0188         std::size_t codes,
0189         code** table,
0190         unsigned *bits,
0191         std::uint16_t* work,
0192         error_code& ec);
0193 
0194     BOOST_BEAST_DECL
0195     static
0196     codes const&
0197     get_fixed_tables();
0198 
0199     BOOST_BEAST_DECL
0200     void
0201     fixedTables();
0202 
0203     BOOST_BEAST_DECL
0204     void
0205     inflate_fast(ranges& r, error_code& ec);
0206 
0207     bitstream bi_;
0208 
0209     Mode mode_ = HEAD;              // current inflate mode
0210     int last_ = 0;                  // true if processing last block
0211     unsigned dmax_ = 32768U;        // zlib header max distance (INFLATE_STRICT)
0212 
0213     // sliding window
0214     window w_;
0215 
0216     // for string and stored block copying
0217     unsigned length_;               // literal or length of data to copy
0218     unsigned offset_;               // distance back to copy string from
0219 
0220     // for table and code decoding
0221     unsigned extra_;                // extra bits needed
0222 
0223     // dynamic table building
0224     unsigned ncode_;                // number of code length code lengths
0225     unsigned nlen_;                 // number of length code lengths
0226     unsigned ndist_;                // number of distance code lengths
0227     unsigned have_;                 // number of code lengths in lens[]
0228     unsigned short lens_[320];      // temporary storage for code lengths
0229     unsigned short work_[288];      // work area for code table building
0230     code codes_[kEnough];           // space for code tables
0231     code *next_ = codes_;           // next available space in codes[]
0232     int back_ = -1;                 // bits back of last unprocessed length/lit
0233     unsigned was_;                  // initial length of match
0234 
0235     // fixed and dynamic code tables
0236     code const* lencode_ = codes_   ; // starting table for length/literal codes
0237     code const* distcode_ = codes_; // starting table for distance codes
0238     unsigned lenbits_;              // index bits for lencode
0239     unsigned distbits_;             // index bits for distcode
0240 };
0241 
0242 } // detail
0243 } // zlib
0244 } // beast
0245 } // boost
0246 
0247 #ifdef BOOST_BEAST_HEADER_ONLY
0248 #include <boost/beast/zlib/detail/inflate_stream.ipp>
0249 #endif
0250 
0251 #endif