gzip.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. // Contains the definitions of the class templates gzip_compressor and
  7. // gzip_decompressor for reading and writing files in the gzip file format
  8. // (RFC 1952). Based in part on work of Jonathan de Halleux; see [...]
  9. #ifndef BOOST_IOSTREAMS_GZIP_HPP_INCLUDED
  10. #define BOOST_IOSTREAMS_GZIP_HPP_INCLUDED
  11. #if defined(_MSC_VER)
  12. # pragma once
  13. #endif
  14. #include <boost/config.hpp> // STATIC_CONSTANT, STDC_NAMESPACE,
  15. // DINKUMWARE_STDLIB, __STL_CONFIG_H.
  16. #include <algorithm> // min.
  17. #include <boost/assert.hpp>
  18. #include <cstdio> // EOF.
  19. #include <cstddef> // size_t.
  20. #include <ctime> // std::time_t.
  21. #include <memory> // allocator.
  22. #include <boost/config.hpp> // Put size_t in std.
  23. #include <boost/detail/workaround.hpp>
  24. #include <boost/cstdint.hpp> // uint8_t, uint32_t.
  25. #include <boost/iostreams/checked_operations.hpp>
  26. #include <boost/iostreams/constants.hpp> // buffer size.
  27. #include <boost/iostreams/detail/adapter/non_blocking_adapter.hpp>
  28. #include <boost/iostreams/detail/adapter/range_adapter.hpp>
  29. #include <boost/iostreams/detail/char_traits.hpp>
  30. #include <boost/iostreams/detail/ios.hpp> // failure, streamsize.
  31. #include <boost/iostreams/detail/error.hpp>
  32. #include <boost/iostreams/operations.hpp>
  33. #include <boost/iostreams/device/back_inserter.hpp>
  34. #include <boost/iostreams/filter/zlib.hpp>
  35. #include <boost/iostreams/pipeline.hpp>
  36. #include <boost/iostreams/putback.hpp>
  37. #include <boost/throw_exception.hpp>
  38. // Must come last.
  39. #if defined(BOOST_MSVC)
  40. # pragma warning(push)
  41. # pragma warning(disable:4244) // Possible truncation
  42. # pragma warning(disable:4251) // Missing DLL interface for std::string
  43. # pragma warning(disable:4309) // Truncation of constant value.
  44. #endif
  45. #ifdef BOOST_NO_STDC_NAMESPACE
  46. namespace std { using ::time_t; }
  47. #endif
  48. namespace boost { namespace iostreams {
  49. //------------------Definitions of constants----------------------------------//
  50. namespace gzip {
  51. using namespace boost::iostreams::zlib;
  52. // Error codes used by gzip_error.
  53. const int zlib_error = 1;
  54. const int bad_crc = 2; // Recorded crc doesn't match data.
  55. const int bad_length = 3; // Recorded length doesn't match data.
  56. const int bad_header = 4; // Malformed header.
  57. const int bad_footer = 5; // Malformed footer.
  58. const int bad_method = 6; // Unsupported compression method.
  59. namespace magic {
  60. // Magic numbers used by gzip header.
  61. const int id1 = 0x1f;
  62. const int id2 = 0x8b;
  63. } // End namespace magic.
  64. namespace method {
  65. // Codes used for the 'CM' byte of the gzip header.
  66. const int deflate = 8;
  67. } // End namespace method.
  68. namespace flags {
  69. // Codes used for the 'FLG' byte of the gzip header.
  70. const int text = 1;
  71. const int header_crc = 2;
  72. const int extra = 4;
  73. const int name = 8;
  74. const int comment = 16;
  75. } // End namespace flags.
  76. namespace extra_flags {
  77. // Codes used for the 'XFL' byte of the gzip header.
  78. const int best_compression = 2;
  79. const int best_speed = 4;
  80. } // End namespace extra_flags.
  81. // Codes used for the 'OS' byte of the gzip header.
  82. const int os_fat = 0;
  83. const int os_amiga = 1;
  84. const int os_vms = 2;
  85. const int os_unix = 3;
  86. const int os_vm_cms = 4;
  87. const int os_atari = 5;
  88. const int os_hpfs = 6;
  89. const int os_macintosh = 7;
  90. const int os_z_system = 8;
  91. const int os_cp_m = 9;
  92. const int os_tops_20 = 10;
  93. const int os_ntfs = 11;
  94. const int os_qdos = 12;
  95. const int os_acorn = 13;
  96. const int os_unknown = 255;
  97. } // End namespace gzip.
  98. //------------------Definition of gzip_params---------------------------------//
  99. //
  100. // Class name: gzip_params.
  101. // Description: Subclass of zlib_params with an additional field
  102. // representing a file name.
  103. //
  104. struct gzip_params : zlib_params {
  105. // Non-explicit constructor.
  106. gzip_params( int level = gzip::default_compression,
  107. int method = gzip::deflated,
  108. int window_bits = gzip::default_window_bits,
  109. int mem_level = gzip::default_mem_level,
  110. int strategy = gzip::default_strategy,
  111. std::string file_name_ = "",
  112. std::string comment_ = "",
  113. std::time_t mtime_ = 0 )
  114. : zlib_params(level, method, window_bits, mem_level, strategy),
  115. file_name(file_name_), comment(comment_), mtime(mtime_)
  116. { }
  117. std::string file_name;
  118. std::string comment;
  119. std::time_t mtime;
  120. };
  121. //------------------Definition of gzip_error----------------------------------//
  122. //
  123. // Class name: gzip_error.
  124. // Description: Subclass of std::ios_base::failure thrown to indicate
  125. // zlib errors other than out-of-memory conditions.
  126. //
  127. class gzip_error : public BOOST_IOSTREAMS_FAILURE {
  128. public:
  129. explicit gzip_error(int error)
  130. : BOOST_IOSTREAMS_FAILURE("gzip error"),
  131. error_(error), zlib_error_code_(zlib::okay) { }
  132. explicit gzip_error(const zlib_error& e)
  133. : BOOST_IOSTREAMS_FAILURE("gzip error"),
  134. error_(gzip::zlib_error), zlib_error_code_(e.error())
  135. { }
  136. int error() const { return error_; }
  137. int zlib_error_code() const { return zlib_error_code_; }
  138. private:
  139. int error_;
  140. int zlib_error_code_;
  141. };
  142. //------------------Definition of gzip_compressor-----------------------------//
  143. //
  144. // Template name: gzip_compressor
  145. // Description: Model of OutputFilter implementing compression in the
  146. // gzip format.
  147. //
  148. template<typename Alloc = std::allocator<char> >
  149. class basic_gzip_compressor : basic_zlib_compressor<Alloc> {
  150. private:
  151. typedef basic_zlib_compressor<Alloc> base_type;
  152. public:
  153. typedef char char_type;
  154. struct category
  155. : dual_use,
  156. filter_tag,
  157. multichar_tag,
  158. closable_tag
  159. { };
  160. basic_gzip_compressor( const gzip_params& = gzip::default_compression,
  161. std::streamsize buffer_size = default_device_buffer_size );
  162. template<typename Source>
  163. std::streamsize read(Source& src, char_type* s, std::streamsize n)
  164. {
  165. std::streamsize result = 0;
  166. // Read header.
  167. if (!(flags_ & f_header_done))
  168. result += read_string(s, n, header_);
  169. // Read body.
  170. if (!(flags_ & f_body_done)) {
  171. // Read from basic_zlib_filter.
  172. std::streamsize amt = base_type::read(src, s + result, n - result);
  173. if (amt != -1) {
  174. result += amt;
  175. if (amt < n - result) { // Double-check for EOF.
  176. amt = base_type::read(src, s + result, n - result);
  177. if (amt != -1)
  178. result += amt;
  179. }
  180. }
  181. if (amt == -1)
  182. prepare_footer();
  183. }
  184. // Read footer.
  185. if ((flags_ & f_body_done) != 0 && result < n)
  186. result += read_string(s + result, n - result, footer_);
  187. return result != 0 ? result : -1;
  188. }
  189. template<typename Sink>
  190. std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
  191. {
  192. if (!(flags_ & f_header_done)) {
  193. std::streamsize amt =
  194. static_cast<std::streamsize>(header_.size() - offset_);
  195. offset_ += boost::iostreams::write_if(snk, header_.data() + offset_, amt);
  196. if (offset_ == header_.size())
  197. flags_ |= f_header_done;
  198. else
  199. return 0;
  200. }
  201. return base_type::write(snk, s, n);
  202. }
  203. template<typename Sink>
  204. void close(Sink& snk, BOOST_IOS::openmode m)
  205. {
  206. try {
  207. if (m == BOOST_IOS::out && !(flags_ & f_header_done))
  208. this->write(snk, 0, 0);
  209. // Close zlib compressor.
  210. base_type::close(snk, m);
  211. if (m == BOOST_IOS::out) {
  212. if (flags_ & f_header_done) {
  213. // Write final fields of gzip file format.
  214. write_long(this->crc(), snk);
  215. write_long(this->total_in(), snk);
  216. }
  217. }
  218. } catch(...) {
  219. close_impl();
  220. throw;
  221. }
  222. close_impl();
  223. }
  224. private:
  225. static gzip_params normalize_params(gzip_params p);
  226. void prepare_footer();
  227. std::streamsize read_string(char* s, std::streamsize n, std::string& str);
  228. template<typename Sink>
  229. static void write_long(long n, Sink& next, boost::mpl::true_)
  230. {
  231. boost::iostreams::put(next, static_cast<char>(0xFF & n));
  232. boost::iostreams::put(next, static_cast<char>(0xFF & (n >> 8)));
  233. boost::iostreams::put(next, static_cast<char>(0xFF & (n >> 16)));
  234. boost::iostreams::put(next, static_cast<char>(0xFF & (n >> 24)));
  235. }
  236. template<typename Sink>
  237. static void write_long(long, Sink&, boost::mpl::false_)
  238. {
  239. }
  240. template<typename Sink>
  241. static void write_long(long n, Sink& next)
  242. {
  243. typedef typename category_of<Sink>::type category;
  244. typedef is_convertible<category, output> can_write;
  245. write_long(n, next, can_write());
  246. }
  247. void close_impl()
  248. {
  249. footer_.clear();
  250. offset_ = 0;
  251. flags_ = 0;
  252. }
  253. enum state_type {
  254. f_header_done = 1,
  255. f_body_done = f_header_done << 1,
  256. f_footer_done = f_body_done << 1
  257. };
  258. std::string header_;
  259. std::string footer_;
  260. std::size_t offset_;
  261. int flags_;
  262. };
  263. BOOST_IOSTREAMS_PIPABLE(basic_gzip_compressor, 1)
  264. typedef basic_gzip_compressor<> gzip_compressor;
  265. //------------------Definition of helper templates for decompression----------//
  266. namespace detail {
  267. // Processes gzip headers
  268. class BOOST_IOSTREAMS_DECL gzip_header {
  269. public:
  270. gzip_header() { reset(); }
  271. // Members for processing header data
  272. void process(char c);
  273. bool done() const { return state_ == s_done; }
  274. void reset();
  275. // Members for accessing header data
  276. std::string file_name() const { return file_name_; }
  277. std::string comment() const { return comment_; }
  278. bool text() const { return (flags_ & gzip::flags::text) != 0; }
  279. int os() const { return os_; }
  280. std::time_t mtime() const { return mtime_; }
  281. private:
  282. enum state_type {
  283. s_id1 = 1,
  284. s_id2 = s_id1 + 1,
  285. s_cm = s_id2 + 1,
  286. s_flg = s_cm + 1,
  287. s_mtime = s_flg + 1,
  288. s_xfl = s_mtime + 1,
  289. s_os = s_xfl + 1,
  290. s_xlen = s_os + 1,
  291. s_extra = s_xlen + 1,
  292. s_name = s_extra + 1,
  293. s_comment = s_name + 1,
  294. s_hcrc = s_comment + 1,
  295. s_done = s_hcrc + 1
  296. };
  297. std::string file_name_;
  298. std::string comment_;
  299. int os_;
  300. std::time_t mtime_;
  301. int flags_;
  302. int state_;
  303. int offset_; // Offset within fixed-length region.
  304. int xlen_; // Bytes remaining in extra field.
  305. };
  306. // Processes gzip footers
  307. class BOOST_IOSTREAMS_DECL gzip_footer {
  308. public:
  309. gzip_footer() { reset(); }
  310. // Members for processing footer data
  311. void process(char c);
  312. bool done() const { return state_ == s_done; }
  313. void reset();
  314. // Members for accessing footer data
  315. zlib::ulong crc() const { return crc_; }
  316. zlib::ulong uncompressed_size() const { return isize_; }
  317. private:
  318. enum state_type {
  319. s_crc = 1,
  320. s_isize = s_crc + 1,
  321. s_done = s_isize + 1
  322. };
  323. zlib::ulong crc_;
  324. zlib::ulong isize_;
  325. int state_;
  326. int offset_;
  327. };
  328. } // End namespace boost::iostreams::detail.
  329. //------------------Definition of basic_gzip_decompressor---------------------//
  330. //
  331. // Template name: basic_gzip_decompressor
  332. // Description: Model of InputFilter implementing compression in the
  333. // gzip format.
  334. //
  335. template<typename Alloc = std::allocator<char> >
  336. class basic_gzip_decompressor : basic_zlib_decompressor<Alloc> {
  337. private:
  338. typedef basic_zlib_decompressor<Alloc> base_type;
  339. typedef typename base_type::string_type string_type;
  340. public:
  341. typedef char char_type;
  342. struct category
  343. : dual_use,
  344. filter_tag,
  345. multichar_tag,
  346. closable_tag
  347. { };
  348. basic_gzip_decompressor( int window_bits = gzip::default_window_bits,
  349. std::streamsize buffer_size = default_device_buffer_size );
  350. template<typename Sink>
  351. std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
  352. {
  353. std::streamsize result = 0;
  354. while(result < n) {
  355. if(state_ == s_start) {
  356. state_ = s_header;
  357. header_.reset();
  358. footer_.reset();
  359. }
  360. if (state_ == s_header) {
  361. int c = s[result++];
  362. header_.process(c);
  363. if (header_.done())
  364. state_ = s_body;
  365. } else if (state_ == s_body) {
  366. try {
  367. std::streamsize amt =
  368. base_type::write(snk, s + result, n - result);
  369. result += amt;
  370. if (!this->eof()) {
  371. break;
  372. } else {
  373. state_ = s_footer;
  374. }
  375. } catch (const zlib_error& e) {
  376. boost::throw_exception(gzip_error(e));
  377. }
  378. } else { // state_ == s_footer
  379. if (footer_.done()) {
  380. if (footer_.crc() != this->crc())
  381. boost::throw_exception(gzip_error(gzip::bad_crc));
  382. base_type::close(snk, BOOST_IOS::out);
  383. state_ = s_start;
  384. } else {
  385. int c = s[result++];
  386. footer_.process(c);
  387. }
  388. }
  389. }
  390. return result;
  391. }
  392. template<typename Source>
  393. std::streamsize read(Source& src, char_type* s, std::streamsize n)
  394. {
  395. typedef char_traits<char> traits_type;
  396. std::streamsize result = 0;
  397. peekable_source<Source> peek(src, putback_);
  398. while (result < n && state_ != s_done) {
  399. if (state_ == s_start) {
  400. state_ = s_header;
  401. header_.reset();
  402. footer_.reset();
  403. }
  404. if (state_ == s_header) {
  405. int c = boost::iostreams::get(peek);
  406. if (traits_type::is_eof(c)) {
  407. boost::throw_exception(gzip_error(gzip::bad_header));
  408. } else if (traits_type::would_block(c)) {
  409. break;
  410. }
  411. header_.process(c);
  412. if (header_.done())
  413. state_ = s_body;
  414. } else if (state_ == s_body) {
  415. try {
  416. std::streamsize amt =
  417. base_type::read(peek, s + result, n - result);
  418. if (amt != -1) {
  419. result += amt;
  420. if (amt < n - result)
  421. break;
  422. } else {
  423. peek.putback(this->unconsumed_input());
  424. state_ = s_footer;
  425. }
  426. } catch (const zlib_error& e) {
  427. boost::throw_exception(gzip_error(e));
  428. }
  429. } else { // state_ == s_footer
  430. int c = boost::iostreams::get(peek);
  431. if (traits_type::is_eof(c)) {
  432. boost::throw_exception(gzip_error(gzip::bad_footer));
  433. } else if (traits_type::would_block(c)) {
  434. break;
  435. }
  436. footer_.process(c);
  437. if (footer_.done()) {
  438. if (footer_.crc() != this->crc())
  439. boost::throw_exception(gzip_error(gzip::bad_crc));
  440. c = boost::iostreams::get(peek);
  441. if (traits_type::is_eof(c)) {
  442. state_ = s_done;
  443. } else {
  444. peek.putback(c);
  445. base_type::close(peek, BOOST_IOS::in);
  446. state_ = s_start;
  447. header_.reset();
  448. footer_.reset();
  449. }
  450. }
  451. }
  452. }
  453. if (peek.has_unconsumed_input()) {
  454. putback_ = peek.unconsumed_input();
  455. } else {
  456. putback_.clear();
  457. }
  458. return result != 0 || state_ != s_done ?
  459. result :
  460. -1;
  461. }
  462. template<typename Source>
  463. void close(Source& src, BOOST_IOS::openmode m)
  464. {
  465. try {
  466. base_type::close(src, m);
  467. } catch (const zlib_error& e) {
  468. state_ = s_start;
  469. boost::throw_exception(gzip_error(e));
  470. }
  471. if (m == BOOST_IOS::out) {
  472. if (state_ == s_start || state_ == s_header)
  473. boost::throw_exception(gzip_error(gzip::bad_header));
  474. else if (state_ == s_body)
  475. boost::throw_exception(gzip_error(gzip::bad_footer));
  476. else if (state_ == s_footer) {
  477. if (!footer_.done())
  478. boost::throw_exception(gzip_error(gzip::bad_footer));
  479. else if(footer_.crc() != this->crc())
  480. boost::throw_exception(gzip_error(gzip::bad_crc));
  481. } else {
  482. BOOST_ASSERT(!"Bad state");
  483. }
  484. }
  485. state_ = s_start;
  486. }
  487. std::string file_name() const { return header_.file_name(); }
  488. std::string comment() const { return header_.comment(); }
  489. bool text() const { return header_.text(); }
  490. int os() const { return header_.os(); }
  491. std::time_t mtime() const { return header_.mtime(); }
  492. private:
  493. static gzip_params make_params(int window_bits);
  494. // Source adapter allowing an arbitrary character sequence to be put back.
  495. template<typename Source>
  496. struct peekable_source {
  497. typedef char char_type;
  498. struct category : source_tag, peekable_tag { };
  499. explicit peekable_source(Source& src, const string_type& putback = "")
  500. : src_(src), putback_(putback), offset_(0)
  501. { }
  502. std::streamsize read(char* s, std::streamsize n)
  503. {
  504. std::streamsize result = 0;
  505. // Copy characters from putback buffer
  506. std::streamsize pbsize =
  507. static_cast<std::streamsize>(putback_.size());
  508. if (offset_ < pbsize) {
  509. result = (std::min)(n, pbsize - offset_);
  510. BOOST_IOSTREAMS_CHAR_TRAITS(char)::copy(
  511. s, putback_.data() + offset_, result);
  512. offset_ += result;
  513. if (result == n)
  514. return result;
  515. }
  516. // Read characters from src_
  517. std::streamsize amt =
  518. boost::iostreams::read(src_, s + result, n - result);
  519. return amt != -1 ?
  520. result + amt :
  521. result ? result : -1;
  522. }
  523. bool putback(char c)
  524. {
  525. if (offset_) {
  526. putback_[--offset_] = c;
  527. } else {
  528. boost::throw_exception(
  529. boost::iostreams::detail::bad_putback());
  530. }
  531. return true;
  532. }
  533. void putback(const string_type& s)
  534. {
  535. putback_.replace(0, offset_, s);
  536. offset_ = 0;
  537. }
  538. // Returns true if some characters have been putback but not re-read.
  539. bool has_unconsumed_input() const
  540. {
  541. return offset_ < static_cast<std::streamsize>(putback_.size());
  542. }
  543. // Returns the sequence of characters that have been put back but not re-read.
  544. string_type unconsumed_input() const
  545. {
  546. return string_type(putback_, offset_, putback_.size() - offset_);
  547. }
  548. Source& src_;
  549. string_type putback_;
  550. std::streamsize offset_;
  551. };
  552. enum state_type {
  553. s_start = 1,
  554. s_header = s_start + 1,
  555. s_body = s_header + 1,
  556. s_footer = s_body + 1,
  557. s_done = s_footer + 1
  558. };
  559. detail::gzip_header header_;
  560. detail::gzip_footer footer_;
  561. string_type putback_;
  562. int state_;
  563. };
  564. BOOST_IOSTREAMS_PIPABLE(basic_gzip_decompressor, 1)
  565. typedef basic_gzip_decompressor<> gzip_decompressor;
  566. //------------------Implementation of gzip_compressor-------------------------//
  567. template<typename Alloc>
  568. basic_gzip_compressor<Alloc>::basic_gzip_compressor
  569. (const gzip_params& p, std::streamsize buffer_size)
  570. : base_type(normalize_params(p), buffer_size),
  571. offset_(0), flags_(0)
  572. {
  573. // Calculate gzip header.
  574. bool has_name = !p.file_name.empty();
  575. bool has_comment = !p.comment.empty();
  576. std::string::size_type length =
  577. 10 +
  578. (has_name ? p.file_name.size() + 1 : 0) +
  579. (has_comment ? p.comment.size() + 1 : 0);
  580. // + 2; // Header crc confuses gunzip.
  581. int flags =
  582. //gzip::flags::header_crc +
  583. (has_name ? gzip::flags::name : 0) +
  584. (has_comment ? gzip::flags::comment : 0);
  585. int extra_flags =
  586. ( p.level == zlib::best_compression ?
  587. gzip::extra_flags::best_compression :
  588. 0 ) +
  589. ( p.level == zlib::best_speed ?
  590. gzip::extra_flags::best_speed :
  591. 0 );
  592. header_.reserve(length);
  593. header_ += gzip::magic::id1; // ID1.
  594. header_ += static_cast<char>(gzip::magic::id2); // ID2.
  595. header_ += gzip::method::deflate; // CM.
  596. header_ += static_cast<char>(flags); // FLG.
  597. header_ += static_cast<char>(0xFF & p.mtime); // MTIME.
  598. header_ += static_cast<char>(0xFF & (p.mtime >> 8));
  599. header_ += static_cast<char>(0xFF & (p.mtime >> 16));
  600. header_ += static_cast<char>(0xFF & (p.mtime >> 24));
  601. header_ += static_cast<char>(extra_flags); // XFL.
  602. header_ += static_cast<char>(gzip::os_unknown); // OS.
  603. if (has_name) {
  604. header_ += p.file_name;
  605. header_ += '\0';
  606. }
  607. if (has_comment) {
  608. header_ += p.comment;
  609. header_ += '\0';
  610. }
  611. }
  612. template<typename Alloc>
  613. gzip_params basic_gzip_compressor<Alloc>::normalize_params(gzip_params p)
  614. {
  615. p.noheader = true;
  616. p.calculate_crc = true;
  617. return p;
  618. }
  619. template<typename Alloc>
  620. void basic_gzip_compressor<Alloc>::prepare_footer()
  621. {
  622. boost::iostreams::back_insert_device<std::string> out(footer_);
  623. write_long(this->crc(), out);
  624. write_long(this->total_in(), out);
  625. flags_ |= f_body_done;
  626. offset_ = 0;
  627. }
  628. template<typename Alloc>
  629. std::streamsize basic_gzip_compressor<Alloc>::read_string
  630. (char* s, std::streamsize n, std::string& str)
  631. {
  632. std::streamsize avail =
  633. static_cast<std::streamsize>(str.size() - offset_);
  634. std::streamsize amt = (std::min)(avail, n);
  635. std::copy( str.data() + offset_,
  636. str.data() + offset_ + amt,
  637. s );
  638. offset_ += amt;
  639. if ( !(flags_ & f_header_done) &&
  640. offset_ == static_cast<std::size_t>(str.size()) )
  641. {
  642. flags_ |= f_header_done;
  643. }
  644. return amt;
  645. }
  646. //------------------Implementation of gzip_decompressor-----------------------//
  647. template<typename Alloc>
  648. basic_gzip_decompressor<Alloc>::basic_gzip_decompressor
  649. (int window_bits, std::streamsize buffer_size)
  650. : base_type(make_params(window_bits), buffer_size),
  651. state_(s_start)
  652. { }
  653. template<typename Alloc>
  654. gzip_params basic_gzip_decompressor<Alloc>::make_params(int window_bits)
  655. {
  656. gzip_params p;
  657. p.window_bits = window_bits;
  658. p.noheader = true;
  659. p.calculate_crc = true;
  660. return p;
  661. }
  662. //----------------------------------------------------------------------------//
  663. } } // End namespaces iostreams, boost.
  664. #if defined(BOOST_MSVC)
  665. # pragma warning(pop)
  666. #endif
  667. #endif // #ifndef BOOST_IOSTREAMS_GZIP_HPP_INCLUDED