inflate_stream.hpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. // 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. #include <boost/beast/core/detail/type_traits.hpp>
  39. #include <boost/throw_exception.hpp>
  40. #include <algorithm>
  41. #include <array>
  42. #include <cstdint>
  43. #include <cstring>
  44. #include <stdexcept>
  45. namespace boost {
  46. namespace beast {
  47. namespace zlib {
  48. namespace detail {
  49. class inflate_stream
  50. {
  51. protected:
  52. inflate_stream()
  53. {
  54. w_.reset(15);
  55. }
  56. template<class = void> void doClear();
  57. template<class = void> void doReset(int windowBits);
  58. template<class = void> void doWrite(z_params& zs, Flush flush, error_code& ec);
  59. void
  60. doReset()
  61. {
  62. doReset(w_.bits());
  63. }
  64. private:
  65. enum Mode
  66. {
  67. HEAD, // i: waiting for magic header
  68. FLAGS, // i: waiting for method and flags (gzip)
  69. TIME, // i: waiting for modification time (gzip)
  70. OS, // i: waiting for extra flags and operating system (gzip)
  71. EXLEN, // i: waiting for extra length (gzip)
  72. EXTRA, // i: waiting for extra bytes (gzip)
  73. NAME, // i: waiting for end of file name (gzip)
  74. COMMENT, // i: waiting for end of comment (gzip)
  75. HCRC, // i: waiting for header crc (gzip)
  76. TYPE, // i: waiting for type bits, including last-flag bit
  77. TYPEDO, // i: same, but skip check to exit inflate on new block
  78. STORED, // i: waiting for stored size (length and complement)
  79. COPY_, // i/o: same as COPY below, but only first time in
  80. COPY, // i/o: waiting for input or output to copy stored block
  81. TABLE, // i: waiting for dynamic block table lengths
  82. LENLENS, // i: waiting for code length code lengths
  83. CODELENS, // i: waiting for length/lit and distance code lengths
  84. LEN_, // i: same as LEN below, but only first time in
  85. LEN, // i: waiting for length/lit/eob code
  86. LENEXT, // i: waiting for length extra bits
  87. DIST, // i: waiting for distance code
  88. DISTEXT,// i: waiting for distance extra bits
  89. MATCH, // o: waiting for output space to copy string
  90. LIT, // o: waiting for output space to write literal
  91. CHECK, // i: waiting for 32-bit check value
  92. LENGTH, // i: waiting for 32-bit length (gzip)
  93. DONE, // finished check, done -- remain here until reset
  94. BAD, // got a data error -- remain here until reset
  95. SYNC // looking for synchronization bytes to restart inflate()
  96. };
  97. /* Structure for decoding tables. Each entry provides either the
  98. information needed to do the operation requested by the code that
  99. indexed that table entry, or it provides a pointer to another
  100. table that indexes more bits of the code. op indicates whether
  101. the entry is a pointer to another table, a literal, a length or
  102. distance, an end-of-block, or an invalid code. For a table
  103. pointer, the low four bits of op is the number of index bits of
  104. that table. For a length or distance, the low four bits of op
  105. is the number of extra bits to get after the code. bits is
  106. the number of bits in this code or part of the code to drop off
  107. of the bit buffer. val is the actual byte to output in the case
  108. of a literal, the base length or distance, or the offset from
  109. the current table to the next table. Each entry is four bytes.
  110. op values as set by inflate_table():
  111. 00000000 - literal
  112. 0000tttt - table link, tttt != 0 is the number of table index bits
  113. 0001eeee - length or distance, eeee is the number of extra bits
  114. 01100000 - end of block
  115. 01000000 - invalid code
  116. */
  117. struct code
  118. {
  119. std::uint8_t op; // operation, extra bits, table bits
  120. std::uint8_t bits; // bits in this part of the code
  121. std::uint16_t val; // offset in table or code value
  122. };
  123. /* Maximum size of the dynamic table. The maximum number of code
  124. structures is 1444, which is the sum of 852 for literal/length codes
  125. and 592 for distance codes. These values were found by exhaustive
  126. searches using the program examples/enough.c found in the zlib
  127. distribtution. The arguments to that program are the number of
  128. symbols, the initial root table size, and the maximum bit length
  129. of a code. "enough 286 9 15" for literal/length codes returns
  130. returns 852, and "enough 30 6 15" for distance codes returns 592.
  131. The initial root table size (9 or 6) is found in the fifth argument
  132. of the inflate_table() calls in inflate.c and infback.c. If the
  133. root table size is changed, then these maximum sizes would be need
  134. to be recalculated and updated.
  135. */
  136. static std::uint16_t constexpr kEnoughLens = 852;
  137. static std::uint16_t constexpr kEnoughDists = 592;
  138. static std::uint16_t constexpr kEnough = kEnoughLens + kEnoughDists;
  139. struct codes
  140. {
  141. code const* lencode;
  142. code const* distcode;
  143. unsigned lenbits; // VFALCO use std::uint8_t
  144. unsigned distbits;
  145. };
  146. // Type of code to build for inflate_table()
  147. enum class build
  148. {
  149. codes,
  150. lens,
  151. dists
  152. };
  153. template<class = void>
  154. static
  155. void
  156. inflate_table(
  157. build type,
  158. std::uint16_t* lens,
  159. std::size_t codes,
  160. code** table,
  161. unsigned *bits,
  162. std::uint16_t* work,
  163. error_code& ec);
  164. template<class = void>
  165. static
  166. codes const&
  167. get_fixed_tables();
  168. template<class = void>
  169. void
  170. fixedTables();
  171. template<class = void>
  172. void
  173. inflate_fast(ranges& r, error_code& ec);
  174. bitstream bi_;
  175. Mode mode_ = HEAD; // current inflate mode
  176. int last_ = 0; // true if processing last block
  177. unsigned dmax_ = 32768U; // zlib header max distance (INFLATE_STRICT)
  178. // sliding window
  179. window w_;
  180. // for string and stored block copying
  181. unsigned length_; // literal or length of data to copy
  182. unsigned offset_; // distance back to copy string from
  183. // for table and code decoding
  184. unsigned extra_; // extra bits needed
  185. // dynamic table building
  186. unsigned ncode_; // number of code length code lengths
  187. unsigned nlen_; // number of length code lengths
  188. unsigned ndist_; // number of distance code lengths
  189. unsigned have_; // number of code lengths in lens[]
  190. unsigned short lens_[320]; // temporary storage for code lengths
  191. unsigned short work_[288]; // work area for code table building
  192. code codes_[kEnough]; // space for code tables
  193. code *next_ = codes_; // next available space in codes[]
  194. int back_ = -1; // bits back of last unprocessed length/lit
  195. unsigned was_; // initial length of match
  196. // fixed and dynamic code tables
  197. code const* lencode_ = codes_ ; // starting table for length/literal codes
  198. code const* distcode_ = codes_; // starting table for distance codes
  199. unsigned lenbits_; // index bits for lencode
  200. unsigned distbits_; // index bits for distcode
  201. };
  202. //------------------------------------------------------------------------------
  203. template<class>
  204. void
  205. inflate_stream::
  206. doReset(int windowBits)
  207. {
  208. if(windowBits < 8 || windowBits > 15)
  209. BOOST_THROW_EXCEPTION(std::domain_error{
  210. "windowBits out of range"});
  211. w_.reset(windowBits);
  212. bi_.flush();
  213. mode_ = HEAD;
  214. last_ = 0;
  215. dmax_ = 32768U;
  216. lencode_ = codes_;
  217. distcode_ = codes_;
  218. next_ = codes_;
  219. back_ = -1;
  220. }
  221. template<class>
  222. void
  223. inflate_stream::
  224. doClear()
  225. {
  226. }
  227. template<class>
  228. void
  229. inflate_stream::
  230. doWrite(z_params& zs, Flush flush, error_code& ec)
  231. {
  232. ranges r;
  233. r.in.first = static_cast<
  234. std::uint8_t const*>(zs.next_in);
  235. r.in.last = r.in.first + zs.avail_in;
  236. r.in.next = r.in.first;
  237. r.out.first = static_cast<
  238. std::uint8_t*>(zs.next_out);
  239. r.out.last = r.out.first + zs.avail_out;
  240. r.out.next = r.out.first;
  241. auto const done =
  242. [&]
  243. {
  244. /*
  245. Return from inflate(), updating the total counts and the check value.
  246. If there was no progress during the inflate() call, return a buffer
  247. error. Call updatewindow() to create and/or update the window state.
  248. Note: a memory error from inflate() is non-recoverable.
  249. */
  250. // VFALCO TODO Don't allocate update the window unless necessary
  251. if(/*wsize_ ||*/ (r.out.used() && mode_ < BAD &&
  252. (mode_ < CHECK || flush != Flush::finish)))
  253. w_.write(r.out.first, r.out.used());
  254. zs.next_in = r.in.next;
  255. zs.avail_in = r.in.avail();
  256. zs.next_out = r.out.next;
  257. zs.avail_out = r.out.avail();
  258. zs.total_in += r.in.used();
  259. zs.total_out += r.out.used();
  260. zs.data_type = bi_.size() + (last_ ? 64 : 0) +
  261. (mode_ == TYPE ? 128 : 0) +
  262. (mode_ == LEN_ || mode_ == COPY_ ? 256 : 0);
  263. if(((! r.in.used() && ! r.out.used()) ||
  264. flush == Flush::finish) && ! ec)
  265. ec = error::need_buffers;
  266. };
  267. auto const err =
  268. [&](error e)
  269. {
  270. ec = e;
  271. mode_ = BAD;
  272. };
  273. if(mode_ == TYPE)
  274. mode_ = TYPEDO;
  275. for(;;)
  276. {
  277. switch(mode_)
  278. {
  279. case HEAD:
  280. mode_ = TYPEDO;
  281. break;
  282. case TYPE:
  283. if(flush == Flush::block || flush == Flush::trees)
  284. return done();
  285. // fall through
  286. case TYPEDO:
  287. {
  288. if(last_)
  289. {
  290. bi_.flush_byte();
  291. mode_ = CHECK;
  292. break;
  293. }
  294. if(! bi_.fill(3, r.in.next, r.in.last))
  295. return done();
  296. std::uint8_t v;
  297. bi_.read(v, 1);
  298. last_ = v != 0;
  299. bi_.read(v, 2);
  300. switch(v)
  301. {
  302. case 0:
  303. // uncompressed block
  304. mode_ = STORED;
  305. break;
  306. case 1:
  307. // fixed Huffman table
  308. fixedTables();
  309. mode_ = LEN_; /* decode codes */
  310. if(flush == Flush::trees)
  311. return done();
  312. break;
  313. case 2:
  314. // dynamic Huffman table
  315. mode_ = TABLE;
  316. break;
  317. default:
  318. return err(error::invalid_block_type);
  319. }
  320. break;
  321. }
  322. case STORED:
  323. {
  324. bi_.flush_byte();
  325. std::uint32_t v;
  326. if(! bi_.fill(32, r.in.next, r.in.last))
  327. return done();
  328. bi_.peek(v, 32);
  329. length_ = v & 0xffff;
  330. if(length_ != ((v >> 16) ^ 0xffff))
  331. return err(error::invalid_stored_length);
  332. // flush instead of read, otherwise
  333. // undefined right shift behavior.
  334. bi_.flush();
  335. mode_ = COPY_;
  336. if(flush == Flush::trees)
  337. return done();
  338. BOOST_FALLTHROUGH;
  339. }
  340. case COPY_:
  341. mode_ = COPY;
  342. BOOST_FALLTHROUGH;
  343. case COPY:
  344. {
  345. auto copy = length_;
  346. if(copy == 0)
  347. {
  348. mode_ = TYPE;
  349. break;
  350. }
  351. copy = clamp(copy, r.in.avail());
  352. copy = clamp(copy, r.out.avail());
  353. if(copy == 0)
  354. return done();
  355. std::memcpy(r.out.next, r.in.next, copy);
  356. r.in.next += copy;
  357. r.out.next += copy;
  358. length_ -= copy;
  359. break;
  360. }
  361. case TABLE:
  362. if(! bi_.fill(5 + 5 + 4, r.in.next, r.in.last))
  363. return done();
  364. bi_.read(nlen_, 5);
  365. nlen_ += 257;
  366. bi_.read(ndist_, 5);
  367. ndist_ += 1;
  368. bi_.read(ncode_, 4);
  369. ncode_ += 4;
  370. if(nlen_ > 286 || ndist_ > 30)
  371. return err(error::too_many_symbols);
  372. have_ = 0;
  373. mode_ = LENLENS;
  374. BOOST_FALLTHROUGH;
  375. case LENLENS:
  376. {
  377. static std::array<std::uint8_t, 19> constexpr order = {{
  378. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}};
  379. while(have_ < ncode_)
  380. {
  381. if(! bi_.fill(3, r.in.next, r.in.last))
  382. return done();
  383. bi_.read(lens_[order[have_]], 3);
  384. ++have_;
  385. }
  386. while(have_ < order.size())
  387. lens_[order[have_++]] = 0;
  388. next_ = &codes_[0];
  389. lencode_ = next_;
  390. lenbits_ = 7;
  391. inflate_table(build::codes, &lens_[0],
  392. order.size(), &next_, &lenbits_, work_, ec);
  393. if(ec)
  394. {
  395. mode_ = BAD;
  396. break;
  397. }
  398. have_ = 0;
  399. mode_ = CODELENS;
  400. BOOST_FALLTHROUGH;
  401. }
  402. case CODELENS:
  403. {
  404. while(have_ < nlen_ + ndist_)
  405. {
  406. std::uint16_t v;
  407. if(! bi_.fill(lenbits_, r.in.next, r.in.last))
  408. return done();
  409. bi_.peek(v, lenbits_);
  410. auto cp = &lencode_[v];
  411. if(cp->val < 16)
  412. {
  413. bi_.drop(cp->bits);
  414. lens_[have_++] = cp->val;
  415. }
  416. else
  417. {
  418. std::uint16_t len;
  419. std::uint16_t copy;
  420. if(cp->val == 16)
  421. {
  422. if(! bi_.fill(cp->bits + 2, r.in.next, r.in.last))
  423. return done();
  424. bi_.drop(cp->bits);
  425. if(have_ == 0)
  426. return err(error::invalid_bit_length_repeat);
  427. bi_.read(copy, 2);
  428. len = lens_[have_ - 1];
  429. copy += 3;
  430. }
  431. else if(cp->val == 17)
  432. {
  433. if(! bi_.fill(cp->bits + 3, r.in.next, r.in.last))
  434. return done();
  435. bi_.drop(cp->bits);
  436. bi_.read(copy, 3);
  437. len = 0;
  438. copy += 3;
  439. }
  440. else
  441. {
  442. if(! bi_.fill(cp->bits + 7, r.in.next, r.in.last))
  443. return done();
  444. bi_.drop(cp->bits);
  445. bi_.read(copy, 7);
  446. len = 0;
  447. copy += 11;
  448. }
  449. if(have_ + copy > nlen_ + ndist_)
  450. return err(error::invalid_bit_length_repeat);
  451. std::fill(&lens_[have_], &lens_[have_ + copy], len);
  452. have_ += copy;
  453. copy = 0;
  454. }
  455. }
  456. // handle error breaks in while
  457. if(mode_ == BAD)
  458. break;
  459. // check for end-of-block code (better have one)
  460. if(lens_[256] == 0)
  461. return err(error::missing_eob);
  462. /* build code tables -- note: do not change the lenbits or distbits
  463. values here (9 and 6) without reading the comments in inftrees.hpp
  464. concerning the kEnough constants, which depend on those values */
  465. next_ = &codes_[0];
  466. lencode_ = next_;
  467. lenbits_ = 9;
  468. inflate_table(build::lens, &lens_[0],
  469. nlen_, &next_, &lenbits_, work_, ec);
  470. if(ec)
  471. {
  472. mode_ = BAD;
  473. return;
  474. }
  475. distcode_ = next_;
  476. distbits_ = 6;
  477. inflate_table(build::dists, lens_ + nlen_,
  478. ndist_, &next_, &distbits_, work_, ec);
  479. if(ec)
  480. {
  481. mode_ = BAD;
  482. return;
  483. }
  484. mode_ = LEN_;
  485. if(flush == Flush::trees)
  486. return done();
  487. BOOST_FALLTHROUGH;
  488. }
  489. case LEN_:
  490. mode_ = LEN;
  491. BOOST_FALLTHROUGH;
  492. case LEN:
  493. {
  494. if(r.in.avail() >= 6 && r.out.avail() >= 258)
  495. {
  496. inflate_fast(r, ec);
  497. if(ec)
  498. {
  499. mode_ = BAD;
  500. return;
  501. }
  502. if(mode_ == TYPE)
  503. back_ = -1;
  504. break;
  505. }
  506. if(! bi_.fill(lenbits_, r.in.next, r.in.last))
  507. return done();
  508. std::uint16_t v;
  509. back_ = 0;
  510. bi_.peek(v, lenbits_);
  511. auto cp = &lencode_[v];
  512. if(cp->op && (cp->op & 0xf0) == 0)
  513. {
  514. auto prev = cp;
  515. if(! bi_.fill(prev->bits + prev->op, r.in.next, r.in.last))
  516. return done();
  517. bi_.peek(v, prev->bits + prev->op);
  518. cp = &lencode_[prev->val + (v >> prev->bits)];
  519. bi_.drop(prev->bits + cp->bits);
  520. back_ += prev->bits + cp->bits;
  521. }
  522. else
  523. {
  524. bi_.drop(cp->bits);
  525. back_ += cp->bits;
  526. }
  527. length_ = cp->val;
  528. if(cp->op == 0)
  529. {
  530. mode_ = LIT;
  531. break;
  532. }
  533. if(cp->op & 32)
  534. {
  535. back_ = -1;
  536. mode_ = TYPE;
  537. break;
  538. }
  539. if(cp->op & 64)
  540. return err(error::invalid_literal_length);
  541. extra_ = cp->op & 15;
  542. mode_ = LENEXT;
  543. BOOST_FALLTHROUGH;
  544. }
  545. case LENEXT:
  546. if(extra_)
  547. {
  548. if(! bi_.fill(extra_, r.in.next, r.in.last))
  549. return done();
  550. std::uint16_t v;
  551. bi_.read(v, extra_);
  552. length_ += v;
  553. back_ += extra_;
  554. }
  555. was_ = length_;
  556. mode_ = DIST;
  557. BOOST_FALLTHROUGH;
  558. case DIST:
  559. {
  560. if(! bi_.fill(distbits_, r.in.next, r.in.last))
  561. return done();
  562. std::uint16_t v;
  563. bi_.peek(v, distbits_);
  564. auto cp = &distcode_[v];
  565. if((cp->op & 0xf0) == 0)
  566. {
  567. auto prev = cp;
  568. if(! bi_.fill(prev->bits + prev->op, r.in.next, r.in.last))
  569. return done();
  570. bi_.peek(v, prev->bits + prev->op);
  571. cp = &distcode_[prev->val + (v >> prev->bits)];
  572. bi_.drop(prev->bits + cp->bits);
  573. back_ += prev->bits + cp->bits;
  574. }
  575. else
  576. {
  577. bi_.drop(cp->bits);
  578. back_ += cp->bits;
  579. }
  580. if(cp->op & 64)
  581. return err(error::invalid_distance_code);
  582. offset_ = cp->val;
  583. extra_ = cp->op & 15;
  584. mode_ = DISTEXT;
  585. BOOST_FALLTHROUGH;
  586. }
  587. case DISTEXT:
  588. if(extra_)
  589. {
  590. std::uint16_t v;
  591. if(! bi_.fill(extra_, r.in.next, r.in.last))
  592. return done();
  593. bi_.read(v, extra_);
  594. offset_ += v;
  595. back_ += extra_;
  596. }
  597. #ifdef INFLATE_STRICT
  598. if(offset_ > dmax_)
  599. return err(error::invalid_distance);
  600. #endif
  601. mode_ = MATCH;
  602. BOOST_FALLTHROUGH;
  603. case MATCH:
  604. {
  605. if(! r.out.avail())
  606. return done();
  607. if(offset_ > r.out.used())
  608. {
  609. // copy from window
  610. auto offset = static_cast<std::uint16_t>(
  611. offset_ - r.out.used());
  612. if(offset > w_.size())
  613. return err(error::invalid_distance);
  614. auto const n = clamp(clamp(
  615. length_, offset), r.out.avail());
  616. w_.read(r.out.next, offset, n);
  617. r.out.next += n;
  618. length_ -= n;
  619. }
  620. else
  621. {
  622. // copy from output
  623. auto in = r.out.next - offset_;
  624. auto n = clamp(length_, r.out.avail());
  625. length_ -= n;
  626. while(n--)
  627. *r.out.next++ = *in++;
  628. }
  629. if(length_ == 0)
  630. mode_ = LEN;
  631. break;
  632. }
  633. case LIT:
  634. {
  635. if(! r.out.avail())
  636. return done();
  637. auto const v = static_cast<std::uint8_t>(length_);
  638. *r.out.next++ = v;
  639. mode_ = LEN;
  640. break;
  641. }
  642. case CHECK:
  643. mode_ = DONE;
  644. BOOST_FALLTHROUGH;
  645. case DONE:
  646. ec = error::end_of_stream;
  647. return done();
  648. case BAD:
  649. return done();
  650. case SYNC:
  651. default:
  652. BOOST_THROW_EXCEPTION(std::logic_error{
  653. "stream error"});
  654. }
  655. }
  656. }
  657. //------------------------------------------------------------------------------
  658. /* Build a set of tables to decode the provided canonical Huffman code.
  659. The code lengths are lens[0..codes-1]. The result starts at *table,
  660. whose indices are 0..2^bits-1. work is a writable array of at least
  661. lens shorts, which is used as a work area. type is the type of code
  662. to be generated, build::codes, build::lens, or build::dists. On
  663. return, zero is success, -1 is an invalid code, and +1 means that
  664. kEnough isn't enough. table on return points to the next available
  665. entry's address. bits is the requested root table index bits, and
  666. on return it is the actual root table index bits. It will differ if
  667. the request is greater than the longest code or if it is less than
  668. the shortest code.
  669. */
  670. template<class>
  671. void
  672. inflate_stream::
  673. inflate_table(
  674. build type,
  675. std::uint16_t* lens,
  676. std::size_t codes,
  677. code** table,
  678. unsigned *bits,
  679. std::uint16_t* work,
  680. error_code& ec)
  681. {
  682. unsigned len; // a code's length in bits
  683. unsigned sym; // index of code symbols
  684. unsigned min, max; // minimum and maximum code lengths
  685. unsigned root; // number of index bits for root table
  686. unsigned curr; // number of index bits for current table
  687. unsigned drop; // code bits to drop for sub-table
  688. int left; // number of prefix codes available
  689. unsigned used; // code entries in table used
  690. unsigned huff; // Huffman code
  691. unsigned incr; // for incrementing code, index
  692. unsigned fill; // index for replicating entries
  693. unsigned low; // low bits for current root entry
  694. unsigned mask; // mask for low root bits
  695. code here; // table entry for duplication
  696. code *next; // next available space in table
  697. std::uint16_t const* base; // base value table to use
  698. std::uint16_t const* extra; // extra bits table to use
  699. int end; // use base and extra for symbol > end
  700. std::uint16_t count[15+1]; // number of codes of each length
  701. std::uint16_t offs[15+1]; // offsets in table for each length
  702. // Length codes 257..285 base
  703. static std::uint16_t constexpr lbase[31] = {
  704. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  705. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  706. // Length codes 257..285 extra
  707. static std::uint16_t constexpr lext[31] = {
  708. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  709. 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78};
  710. // Distance codes 0..29 base
  711. static std::uint16_t constexpr dbase[32] = {
  712. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  713. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  714. 8193, 12289, 16385, 24577, 0, 0};
  715. // Distance codes 0..29 extra
  716. static std::uint16_t constexpr dext[32] = {
  717. 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  718. 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  719. 28, 28, 29, 29, 64, 64};
  720. /*
  721. Process a set of code lengths to create a canonical Huffman code. The
  722. code lengths are lens[0..codes-1]. Each length corresponds to the
  723. symbols 0..codes-1. The Huffman code is generated by first sorting the
  724. symbols by length from short to long, and retaining the symbol order
  725. for codes with equal lengths. Then the code starts with all zero bits
  726. for the first code of the shortest length, and the codes are integer
  727. increments for the same length, and zeros are appended as the length
  728. increases. For the deflate format, these bits are stored backwards
  729. from their more natural integer increment ordering, and so when the
  730. decoding tables are built in the large loop below, the integer codes
  731. are incremented backwards.
  732. This routine assumes, but does not check, that all of the entries in
  733. lens[] are in the range 0..15. The caller must assure this.
  734. 1..15 is interpreted as that code length. zero means that that
  735. symbol does not occur in this code.
  736. The codes are sorted by computing a count of codes for each length,
  737. creating from that a table of starting indices for each length in the
  738. sorted table, and then entering the symbols in order in the sorted
  739. table. The sorted table is work[], with that space being provided by
  740. the caller.
  741. The length counts are used for other purposes as well, i.e. finding
  742. the minimum and maximum length codes, determining if there are any
  743. codes at all, checking for a valid set of lengths, and looking ahead
  744. at length counts to determine sub-table sizes when building the
  745. decoding tables.
  746. */
  747. /* accumulate lengths for codes (assumes lens[] all in 0..15) */
  748. for (len = 0; len <= 15; len++)
  749. count[len] = 0;
  750. for (sym = 0; sym < codes; sym++)
  751. count[lens[sym]]++;
  752. /* bound code lengths, force root to be within code lengths */
  753. root = *bits;
  754. for (max = 15; max >= 1; max--)
  755. if (count[max] != 0)
  756. break;
  757. if (root > max)
  758. root = max;
  759. if (max == 0)
  760. { /* no symbols to code at all */
  761. here.op = (std::uint8_t)64; /* invalid code marker */
  762. here.bits = (std::uint8_t)1;
  763. here.val = (std::uint16_t)0;
  764. *(*table)++ = here; /* make a table to force an error */
  765. *(*table)++ = here;
  766. *bits = 1;
  767. return; /* no symbols, but wait for decoding to report error */
  768. }
  769. for (min = 1; min < max; min++)
  770. if (count[min] != 0)
  771. break;
  772. if (root < min)
  773. root = min;
  774. /* check for an over-subscribed or incomplete set of lengths */
  775. left = 1;
  776. for (len = 1; len <= 15; len++)
  777. {
  778. left <<= 1;
  779. left -= count[len];
  780. if (left < 0)
  781. {
  782. ec = error::over_subscribed_length;
  783. return;
  784. }
  785. }
  786. if (left > 0 && (type == build::codes || max != 1))
  787. {
  788. ec = error::incomplete_length_set;
  789. return;
  790. }
  791. /* generate offsets into symbol table for each length for sorting */
  792. offs[1] = 0;
  793. for (len = 1; len < 15; len++)
  794. offs[len + 1] = offs[len] + count[len];
  795. /* sort symbols by length, by symbol order within each length */
  796. for (sym = 0; sym < codes; sym++)
  797. if (lens[sym] != 0)
  798. work[offs[lens[sym]]++] = (std::uint16_t)sym;
  799. /*
  800. Create and fill in decoding tables. In this loop, the table being
  801. filled is at next and has curr index bits. The code being used is huff
  802. with length len. That code is converted to an index by dropping drop
  803. bits off of the bottom. For codes where len is less than drop + curr,
  804. those top drop + curr - len bits are incremented through all values to
  805. fill the table with replicated entries.
  806. root is the number of index bits for the root table. When len exceeds
  807. root, sub-tables are created pointed to by the root entry with an index
  808. of the low root bits of huff. This is saved in low to check for when a
  809. new sub-table should be started. drop is zero when the root table is
  810. being filled, and drop is root when sub-tables are being filled.
  811. When a new sub-table is needed, it is necessary to look ahead in the
  812. code lengths to determine what size sub-table is needed. The length
  813. counts are used for this, and so count[] is decremented as codes are
  814. entered in the tables.
  815. used keeps track of how many table entries have been allocated from the
  816. provided *table space. It is checked for build::lens and DIST tables against
  817. the constants kEnoughLens and kEnoughDists to guard against changes in
  818. the initial root table size constants. See the comments in inftrees.hpp
  819. for more information.
  820. sym increments through all symbols, and the loop terminates when
  821. all codes of length max, i.e. all codes, have been processed. This
  822. routine permits incomplete codes, so another loop after this one fills
  823. in the rest of the decoding tables with invalid code markers.
  824. */
  825. /* set up for code type */
  826. switch (type)
  827. {
  828. case build::codes:
  829. base = extra = work; /* dummy value--not used */
  830. end = 19;
  831. break;
  832. case build::lens:
  833. base = lbase;
  834. base -= 257;
  835. extra = lext;
  836. extra -= 257;
  837. end = 256;
  838. break;
  839. default: /* build::dists */
  840. base = dbase;
  841. extra = dext;
  842. end = -1;
  843. }
  844. /* initialize state for loop */
  845. huff = 0; /* starting code */
  846. sym = 0; /* starting code symbol */
  847. len = min; /* starting code length */
  848. next = *table; /* current table to fill in */
  849. curr = root; /* current table index bits */
  850. drop = 0; /* current bits to drop from code for index */
  851. low = (unsigned)(-1); /* trigger new sub-table when len > root */
  852. used = 1U << root; /* use root table entries */
  853. mask = used - 1; /* mask for comparing low */
  854. auto const not_enough = []
  855. {
  856. BOOST_THROW_EXCEPTION(std::logic_error{
  857. "insufficient output size when inflating tables"});
  858. };
  859. // check available table space
  860. if ((type == build::lens && used > kEnoughLens) ||
  861. (type == build::dists && used > kEnoughDists))
  862. return not_enough();
  863. /* process all codes and make table entries */
  864. for (;;)
  865. {
  866. /* create table entry */
  867. here.bits = (std::uint8_t)(len - drop);
  868. if ((int)(work[sym]) < end)
  869. {
  870. here.op = (std::uint8_t)0;
  871. here.val = work[sym];
  872. }
  873. else if ((int)(work[sym]) > end)
  874. {
  875. here.op = (std::uint8_t)(extra[work[sym]]);
  876. here.val = base[work[sym]];
  877. }
  878. else
  879. {
  880. here.op = (std::uint8_t)(32 + 64); /* end of block */
  881. here.val = 0;
  882. }
  883. /* replicate for those indices with low len bits equal to huff */
  884. incr = 1U << (len - drop);
  885. fill = 1U << curr;
  886. min = fill; /* save offset to next table */
  887. do
  888. {
  889. fill -= incr;
  890. next[(huff >> drop) + fill] = here;
  891. } while (fill != 0);
  892. /* backwards increment the len-bit code huff */
  893. incr = 1U << (len - 1);
  894. while (huff & incr)
  895. incr >>= 1;
  896. if (incr != 0)
  897. {
  898. huff &= incr - 1;
  899. huff += incr;
  900. }
  901. else
  902. huff = 0;
  903. /* go to next symbol, update count, len */
  904. sym++;
  905. if (--(count[len]) == 0)
  906. {
  907. if (len == max) break;
  908. len = lens[work[sym]];
  909. }
  910. /* create new sub-table if needed */
  911. if (len > root && (huff & mask) != low)
  912. {
  913. /* if first time, transition to sub-tables */
  914. if (drop == 0)
  915. drop = root;
  916. /* increment past last table */
  917. next += min; /* here min is 1 << curr */
  918. /* determine length of next table */
  919. curr = len - drop;
  920. left = (int)(1 << curr);
  921. while (curr + drop < max)
  922. {
  923. left -= count[curr + drop];
  924. if (left <= 0) break;
  925. curr++;
  926. left <<= 1;
  927. }
  928. /* check for enough space */
  929. used += 1U << curr;
  930. if ((type == build::lens && used > kEnoughLens) ||
  931. (type == build::dists && used > kEnoughDists))
  932. return not_enough();
  933. /* point entry in root table to sub-table */
  934. low = huff & mask;
  935. (*table)[low].op = (std::uint8_t)curr;
  936. (*table)[low].bits = (std::uint8_t)root;
  937. (*table)[low].val = (std::uint16_t)(next - *table);
  938. }
  939. }
  940. /* fill in remaining table entry if code is incomplete (guaranteed to have
  941. at most one remaining entry, since if the code is incomplete, the
  942. maximum code length that was allowed to get this far is one bit) */
  943. if (huff != 0)
  944. {
  945. here.op = 64; // invalid code marker
  946. here.bits = (std::uint8_t)(len - drop);
  947. here.val = 0;
  948. next[huff] = here;
  949. }
  950. *table += used;
  951. *bits = root;
  952. }
  953. template<class>
  954. auto
  955. inflate_stream::
  956. get_fixed_tables() ->
  957. codes const&
  958. {
  959. struct fixed_codes : codes
  960. {
  961. code len_[512];
  962. code dist_[32];
  963. fixed_codes()
  964. {
  965. lencode = len_;
  966. lenbits = 9;
  967. distcode = dist_;
  968. distbits = 5;
  969. std::uint16_t lens[320];
  970. std::uint16_t work[288];
  971. std::fill(&lens[ 0], &lens[144], std::uint16_t{8});
  972. std::fill(&lens[144], &lens[256], std::uint16_t{9});
  973. std::fill(&lens[256], &lens[280], std::uint16_t{7});
  974. std::fill(&lens[280], &lens[288], std::uint16_t{8});
  975. {
  976. error_code ec;
  977. auto next = &len_[0];
  978. inflate_table(build::lens,
  979. lens, 288, &next, &lenbits, work, ec);
  980. if(ec)
  981. BOOST_THROW_EXCEPTION(std::logic_error{ec.message()});
  982. }
  983. // VFALCO These fixups are from ZLib
  984. len_[ 99].op = 64;
  985. len_[227].op = 64;
  986. len_[355].op = 64;
  987. len_[483].op = 64;
  988. {
  989. error_code ec;
  990. auto next = &dist_[0];
  991. std::fill(&lens[0], &lens[32], std::uint16_t{5});
  992. inflate_table(build::dists,
  993. lens, 32, &next, &distbits, work, ec);
  994. if(ec)
  995. BOOST_THROW_EXCEPTION(std::logic_error{ec.message()});
  996. }
  997. }
  998. };
  999. static fixed_codes const fc;
  1000. return fc;
  1001. }
  1002. template<class>
  1003. void
  1004. inflate_stream::
  1005. fixedTables()
  1006. {
  1007. auto const fc = get_fixed_tables();
  1008. lencode_ = fc.lencode;
  1009. lenbits_ = fc.lenbits;
  1010. distcode_ = fc.distcode;
  1011. distbits_ = fc.distbits;
  1012. }
  1013. /*
  1014. Decode literal, length, and distance codes and write out the resulting
  1015. literal and match bytes until either not enough input or output is
  1016. available, an end-of-block is encountered, or a data error is encountered.
  1017. When large enough input and output buffers are supplied to inflate(), for
  1018. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  1019. inflate execution time is spent in this routine.
  1020. Entry assumptions:
  1021. state->mode_ == LEN
  1022. zs.avail_in >= 6
  1023. zs.avail_out >= 258
  1024. start >= zs.avail_out
  1025. state->bits_ < 8
  1026. On return, state->mode_ is one of:
  1027. LEN -- ran out of enough output space or enough available input
  1028. TYPE -- reached end of block code, inflate() to interpret next block
  1029. BAD -- error in block data
  1030. Notes:
  1031. - The maximum input bits used by a length/distance pair is 15 bits for the
  1032. length code, 5 bits for the length extra, 15 bits for the distance code,
  1033. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  1034. Therefore if zs.avail_in >= 6, then there is enough input to avoid
  1035. checking for available input while decoding.
  1036. - The maximum bytes that a single length/distance pair can output is 258
  1037. bytes, which is the maximum length that can be coded. inflate_fast()
  1038. requires zs.avail_out >= 258 for each loop to avoid checking for
  1039. output space.
  1040. inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
  1041. - Using bit fields for code structure
  1042. - Different op definition to avoid & for extra bits (do & for table bits)
  1043. - Three separate decoding do-loops for direct, window, and wnext == 0
  1044. - Special case for distance > 1 copies to do overlapped load and store copy
  1045. - Explicit branch predictions (based on measured branch probabilities)
  1046. - Deferring match copy and interspersed it with decoding subsequent codes
  1047. - Swapping literal/length else
  1048. - Swapping window/direct else
  1049. - Larger unrolled copy loops (three is about right)
  1050. - Moving len -= 3 statement into middle of loop
  1051. */
  1052. template<class>
  1053. void
  1054. inflate_stream::
  1055. inflate_fast(ranges& r, error_code& ec)
  1056. {
  1057. unsigned char const* last; // have enough input while in < last
  1058. unsigned char *end; // while out < end, enough space available
  1059. std::size_t op; // code bits, operation, extra bits, or window position, window bytes to copy
  1060. unsigned len; // match length, unused bytes
  1061. unsigned dist; // match distance
  1062. unsigned const lmask =
  1063. (1U << lenbits_) - 1; // mask for first level of length codes
  1064. unsigned const dmask =
  1065. (1U << distbits_) - 1; // mask for first level of distance codes
  1066. last = r.in.next + (r.in.avail() - 5);
  1067. end = r.out.next + (r.out.avail() - 257);
  1068. /* decode literals and length/distances until end-of-block or not enough
  1069. input data or output space */
  1070. do
  1071. {
  1072. if(bi_.size() < 15)
  1073. bi_.fill_16(r.in.next);
  1074. auto cp = &lencode_[bi_.peek_fast() & lmask];
  1075. dolen:
  1076. bi_.drop(cp->bits);
  1077. op = (unsigned)(cp->op);
  1078. if(op == 0)
  1079. {
  1080. // literal
  1081. *r.out.next++ = (unsigned char)(cp->val);
  1082. }
  1083. else if(op & 16)
  1084. {
  1085. // length base
  1086. len = (unsigned)(cp->val);
  1087. op &= 15; // number of extra bits
  1088. if(op)
  1089. {
  1090. if(bi_.size() < op)
  1091. bi_.fill_8(r.in.next);
  1092. len += (unsigned)bi_.peek_fast() & ((1U << op) - 1);
  1093. bi_.drop(op);
  1094. }
  1095. if(bi_.size() < 15)
  1096. bi_.fill_16(r.in.next);
  1097. cp = &distcode_[bi_.peek_fast() & dmask];
  1098. dodist:
  1099. bi_.drop(cp->bits);
  1100. op = (unsigned)(cp->op);
  1101. if(op & 16)
  1102. {
  1103. // distance base
  1104. dist = (unsigned)(cp->val);
  1105. op &= 15; // number of extra bits
  1106. if(bi_.size() < op)
  1107. {
  1108. bi_.fill_8(r.in.next);
  1109. if(bi_.size() < op)
  1110. bi_.fill_8(r.in.next);
  1111. }
  1112. dist += (unsigned)bi_.peek_fast() & ((1U << op) - 1);
  1113. #ifdef INFLATE_STRICT
  1114. if(dist > dmax_)
  1115. {
  1116. ec = error::invalid_distance;
  1117. mode_ = BAD;
  1118. break;
  1119. }
  1120. #endif
  1121. bi_.drop(op);
  1122. op = r.out.used();
  1123. if(dist > op)
  1124. {
  1125. // copy from window
  1126. op = dist - op; // distance back in window
  1127. if(op > w_.size())
  1128. {
  1129. ec = error::invalid_distance;
  1130. mode_ = BAD;
  1131. break;
  1132. }
  1133. auto const n = clamp(len, op);
  1134. w_.read(r.out.next, op, n);
  1135. r.out.next += n;
  1136. len -= n;
  1137. }
  1138. if(len > 0)
  1139. {
  1140. // copy from output
  1141. auto in = r.out.next - dist;
  1142. auto n = clamp(len, r.out.avail());
  1143. len -= n;
  1144. while(n--)
  1145. *r.out.next++ = *in++;
  1146. }
  1147. }
  1148. else if((op & 64) == 0)
  1149. {
  1150. // 2nd level distance code
  1151. cp = &distcode_[cp->val + (bi_.peek_fast() & ((1U << op) - 1))];
  1152. goto dodist;
  1153. }
  1154. else
  1155. {
  1156. ec = error::invalid_distance_code;
  1157. mode_ = BAD;
  1158. break;
  1159. }
  1160. }
  1161. else if((op & 64) == 0)
  1162. {
  1163. // 2nd level length code
  1164. cp = &lencode_[cp->val + (bi_.peek_fast() & ((1U << op) - 1))];
  1165. goto dolen;
  1166. }
  1167. else if(op & 32)
  1168. {
  1169. // end-of-block
  1170. mode_ = TYPE;
  1171. break;
  1172. }
  1173. else
  1174. {
  1175. ec = error::invalid_literal_length;
  1176. mode_ = BAD;
  1177. break;
  1178. }
  1179. }
  1180. while(r.in.next < last && r.out.next < end);
  1181. // return unused bytes (on entry, bits < 8, so in won't go too far back)
  1182. bi_.rewind(r.in.next);
  1183. }
  1184. } // detail
  1185. } // zlib
  1186. } // beast
  1187. } // boost
  1188. #endif