inflate_stream.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // Copyright (c) 2016-2019 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. // This is a derivative work based on Zlib, copyright below:
  10. /*
  11. Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
  12. This software is provided 'as-is', without any express or implied
  13. warranty. In no event will the authors be held liable for any damages
  14. arising from the use of this software.
  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. 1. The origin of this software must not be misrepresented; you must not
  19. claim that you wrote the original software. If you use this software
  20. in a product, an acknowledgment in the product documentation would be
  21. appreciated but is not required.
  22. 2. Altered source versions must be plainly marked as such, and must not be
  23. misrepresented as being the original software.
  24. 3. This notice may not be removed or altered from any source distribution.
  25. Jean-loup Gailly Mark Adler
  26. jloup@gzip.org madler@alumni.caltech.edu
  27. The data format used by the zlib library is described by RFCs (Request for
  28. Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
  29. (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
  30. */
  31. #ifndef BOOST_BEAST_ZLIB_DETAIL_INFLATE_STREAM_HPP
  32. #define BOOST_BEAST_ZLIB_DETAIL_INFLATE_STREAM_HPP
  33. #include <boost/beast/zlib/error.hpp>
  34. #include <boost/beast/zlib/zlib.hpp>
  35. #include <boost/beast/zlib/detail/bitstream.hpp>
  36. #include <boost/beast/zlib/detail/ranges.hpp>
  37. #include <boost/beast/zlib/detail/window.hpp>
  38. #if 0
  39. #include <boost/beast/core/detail/type_traits.hpp>
  40. #include <boost/throw_exception.hpp>
  41. #include <algorithm>
  42. #include <array>
  43. #include <cstdint>
  44. #include <cstring>
  45. #include <stdexcept>
  46. #endif
  47. namespace boost {
  48. namespace beast {
  49. namespace zlib {
  50. namespace detail {
  51. class inflate_stream
  52. {
  53. protected:
  54. inflate_stream()
  55. {
  56. w_.reset(15);
  57. }
  58. BOOST_BEAST_DECL
  59. void
  60. doClear();
  61. BOOST_BEAST_DECL
  62. void
  63. doReset(int windowBits);
  64. BOOST_BEAST_DECL
  65. void
  66. doWrite(z_params& zs, Flush flush, error_code& ec);
  67. void
  68. doReset()
  69. {
  70. doReset(w_.bits());
  71. }
  72. private:
  73. enum Mode
  74. {
  75. HEAD, // i: waiting for magic header
  76. FLAGS, // i: waiting for method and flags (gzip)
  77. TIME, // i: waiting for modification time (gzip)
  78. OS, // i: waiting for extra flags and operating system (gzip)
  79. EXLEN, // i: waiting for extra length (gzip)
  80. EXTRA, // i: waiting for extra bytes (gzip)
  81. NAME, // i: waiting for end of file name (gzip)
  82. COMMENT, // i: waiting for end of comment (gzip)
  83. HCRC, // i: waiting for header crc (gzip)
  84. TYPE, // i: waiting for type bits, including last-flag bit
  85. TYPEDO, // i: same, but skip check to exit inflate on new block
  86. STORED, // i: waiting for stored size (length and complement)
  87. COPY_, // i/o: same as COPY below, but only first time in
  88. COPY, // i/o: waiting for input or output to copy stored block
  89. TABLE, // i: waiting for dynamic block table lengths
  90. LENLENS, // i: waiting for code length code lengths
  91. CODELENS, // i: waiting for length/lit and distance code lengths
  92. LEN_, // i: same as LEN below, but only first time in
  93. LEN, // i: waiting for length/lit/eob code
  94. LENEXT, // i: waiting for length extra bits
  95. DIST, // i: waiting for distance code
  96. DISTEXT,// i: waiting for distance extra bits
  97. MATCH, // o: waiting for output space to copy string
  98. LIT, // o: waiting for output space to write literal
  99. CHECK, // i: waiting for 32-bit check value
  100. LENGTH, // i: waiting for 32-bit length (gzip)
  101. DONE, // finished check, done -- remain here until reset
  102. BAD, // got a data error -- remain here until reset
  103. SYNC // looking for synchronization bytes to restart inflate()
  104. };
  105. /* Structure for decoding tables. Each entry provides either the
  106. information needed to do the operation requested by the code that
  107. indexed that table entry, or it provides a pointer to another
  108. table that indexes more bits of the code. op indicates whether
  109. the entry is a pointer to another table, a literal, a length or
  110. distance, an end-of-block, or an invalid code. For a table
  111. pointer, the low four bits of op is the number of index bits of
  112. that table. For a length or distance, the low four bits of op
  113. is the number of extra bits to get after the code. bits is
  114. the number of bits in this code or part of the code to drop off
  115. of the bit buffer. val is the actual byte to output in the case
  116. of a literal, the base length or distance, or the offset from
  117. the current table to the next table. Each entry is four bytes.
  118. op values as set by inflate_table():
  119. 00000000 - literal
  120. 0000tttt - table link, tttt != 0 is the number of table index bits
  121. 0001eeee - length or distance, eeee is the number of extra bits
  122. 01100000 - end of block
  123. 01000000 - invalid code
  124. */
  125. struct code
  126. {
  127. std::uint8_t op; // operation, extra bits, table bits
  128. std::uint8_t bits; // bits in this part of the code
  129. std::uint16_t val; // offset in table or code value
  130. };
  131. /* Maximum size of the dynamic table. The maximum number of code
  132. structures is 1444, which is the sum of 852 for literal/length codes
  133. and 592 for distance codes. These values were found by exhaustive
  134. searches using the program examples/enough.c found in the zlib
  135. distribtution. The arguments to that program are the number of
  136. symbols, the initial root table size, and the maximum bit length
  137. of a code. "enough 286 9 15" for literal/length codes returns
  138. returns 852, and "enough 30 6 15" for distance codes returns 592.
  139. The initial root table size (9 or 6) is found in the fifth argument
  140. of the inflate_table() calls in inflate.c and infback.c. If the
  141. root table size is changed, then these maximum sizes would be need
  142. to be recalculated and updated.
  143. */
  144. static std::uint16_t constexpr kEnoughLens = 852;
  145. static std::uint16_t constexpr kEnoughDists = 592;
  146. static std::uint16_t constexpr kEnough = kEnoughLens + kEnoughDists;
  147. struct codes
  148. {
  149. code const* lencode;
  150. code const* distcode;
  151. unsigned lenbits; // VFALCO use std::uint8_t
  152. unsigned distbits;
  153. };
  154. // Type of code to build for inflate_table()
  155. enum class build
  156. {
  157. codes,
  158. lens,
  159. dists
  160. };
  161. BOOST_BEAST_DECL
  162. static
  163. void
  164. inflate_table(
  165. build type,
  166. std::uint16_t* lens,
  167. std::size_t codes,
  168. code** table,
  169. unsigned *bits,
  170. std::uint16_t* work,
  171. error_code& ec);
  172. BOOST_BEAST_DECL
  173. static
  174. codes const&
  175. get_fixed_tables();
  176. BOOST_BEAST_DECL
  177. void
  178. fixedTables();
  179. BOOST_BEAST_DECL
  180. void
  181. inflate_fast(ranges& r, error_code& ec);
  182. bitstream bi_;
  183. Mode mode_ = HEAD; // current inflate mode
  184. int last_ = 0; // true if processing last block
  185. unsigned dmax_ = 32768U; // zlib header max distance (INFLATE_STRICT)
  186. // sliding window
  187. window w_;
  188. // for string and stored block copying
  189. unsigned length_; // literal or length of data to copy
  190. unsigned offset_; // distance back to copy string from
  191. // for table and code decoding
  192. unsigned extra_; // extra bits needed
  193. // dynamic table building
  194. unsigned ncode_; // number of code length code lengths
  195. unsigned nlen_; // number of length code lengths
  196. unsigned ndist_; // number of distance code lengths
  197. unsigned have_; // number of code lengths in lens[]
  198. unsigned short lens_[320]; // temporary storage for code lengths
  199. unsigned short work_[288]; // work area for code table building
  200. code codes_[kEnough]; // space for code tables
  201. code *next_ = codes_; // next available space in codes[]
  202. int back_ = -1; // bits back of last unprocessed length/lit
  203. unsigned was_; // initial length of match
  204. // fixed and dynamic code tables
  205. code const* lencode_ = codes_ ; // starting table for length/literal codes
  206. code const* distcode_ = codes_; // starting table for distance codes
  207. unsigned lenbits_; // index bits for lencode
  208. unsigned distbits_; // index bits for distcode
  209. };
  210. } // detail
  211. } // zlib
  212. } // beast
  213. } // boost
  214. #ifdef BOOST_BEAST_HEADER_ONLY
  215. #include <boost/beast/zlib/detail/inflate_stream.ipp>
  216. #endif
  217. #endif