lzma.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // (C) Copyright Milan Svoboda 2008.
  2. // Originally developed under the fusecompress project.
  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. // Note: custom allocators are not supported on VC6, since that compiler
  7. // had trouble finding the function lzma_base::do_init.
  8. #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED
  9. #define BOOST_IOSTREAMS_LZMA_HPP_INCLUDED
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <cassert>
  14. #include <iosfwd> // streamsize.
  15. #include <memory> // allocator, bad_alloc.
  16. #include <new>
  17. #include <boost/config.hpp> // MSVC, STATIC_CONSTANT, DEDUCED_TYPENAME, DINKUM.
  18. #include <boost/detail/workaround.hpp>
  19. #include <boost/iostreams/constants.hpp> // buffer size.
  20. #include <boost/iostreams/detail/config/auto_link.hpp>
  21. #include <boost/iostreams/detail/config/dyn_link.hpp>
  22. #include <boost/iostreams/detail/config/wide_streams.hpp>
  23. #include <boost/iostreams/detail/ios.hpp> // failure, streamsize.
  24. #include <boost/iostreams/filter/symmetric.hpp>
  25. #include <boost/iostreams/pipeline.hpp>
  26. #include <boost/type_traits/is_same.hpp>
  27. // Must come last.
  28. #ifdef BOOST_MSVC
  29. # pragma warning(push)
  30. # pragma warning(disable:4251 4231 4660) // Dependencies not exported.
  31. #endif
  32. #include <boost/config/abi_prefix.hpp>
  33. namespace boost { namespace iostreams {
  34. namespace lzma {
  35. typedef void* (*alloc_func)(void*, size_t, size_t);
  36. typedef void (*free_func)(void*, void*);
  37. // Compression levels
  38. BOOST_IOSTREAMS_DECL extern const uint32_t no_compression;
  39. BOOST_IOSTREAMS_DECL extern const uint32_t best_speed;
  40. BOOST_IOSTREAMS_DECL extern const uint32_t best_compression;
  41. BOOST_IOSTREAMS_DECL extern const uint32_t default_compression;
  42. // Status codes
  43. BOOST_IOSTREAMS_DECL extern const int okay;
  44. BOOST_IOSTREAMS_DECL extern const int stream_end;
  45. BOOST_IOSTREAMS_DECL extern const int unsupported_check;
  46. BOOST_IOSTREAMS_DECL extern const int mem_error;
  47. BOOST_IOSTREAMS_DECL extern const int options_error;
  48. BOOST_IOSTREAMS_DECL extern const int data_error;
  49. BOOST_IOSTREAMS_DECL extern const int buf_error;
  50. BOOST_IOSTREAMS_DECL extern const int prog_error;
  51. // Flush codes
  52. BOOST_IOSTREAMS_DECL extern const int finish;
  53. BOOST_IOSTREAMS_DECL extern const int full_flush;
  54. BOOST_IOSTREAMS_DECL extern const int sync_flush;
  55. BOOST_IOSTREAMS_DECL extern const int run;
  56. // Code for current OS
  57. // Null pointer constant.
  58. const int null = 0;
  59. // Default values
  60. } // End namespace lzma.
  61. //
  62. // Class name: lzma_params.
  63. // Description: Encapsulates the parameters passed to lzmadec_init
  64. // to customize compression and decompression.
  65. //
  66. struct lzma_params {
  67. // Non-explicit constructor.
  68. lzma_params( uint32_t level = lzma::default_compression, uint32_t threads = 1 )
  69. : level(level)
  70. , threads(threads)
  71. { }
  72. uint32_t level;
  73. uint32_t threads;
  74. };
  75. //
  76. // Class name: lzma_error.
  77. // Description: Subclass of std::ios::failure thrown to indicate
  78. // lzma errors other than out-of-memory conditions.
  79. //
  80. class BOOST_IOSTREAMS_DECL lzma_error : public BOOST_IOSTREAMS_FAILURE {
  81. public:
  82. explicit lzma_error(int error);
  83. int error() const { return error_; }
  84. static void check BOOST_PREVENT_MACRO_SUBSTITUTION(int error);
  85. private:
  86. int error_;
  87. };
  88. namespace detail {
  89. template<typename Alloc>
  90. struct lzma_allocator_traits {
  91. #ifndef BOOST_NO_STD_ALLOCATOR
  92. #if defined(BOOST_NO_CXX11_ALLOCATOR)
  93. typedef typename Alloc::template rebind<char>::other type;
  94. #else
  95. typedef typename std::allocator_traits<Alloc>::template rebind_alloc<char> type;
  96. #endif
  97. #else
  98. typedef std::allocator<char> type;
  99. #endif
  100. };
  101. template< typename Alloc,
  102. typename Base = // VC6 workaround (C2516)
  103. BOOST_DEDUCED_TYPENAME lzma_allocator_traits<Alloc>::type >
  104. struct lzma_allocator : private Base {
  105. private:
  106. #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_NO_STD_ALLOCATOR)
  107. typedef typename Base::size_type size_type;
  108. #else
  109. typedef typename std::allocator_traits<Base>::size_type size_type;
  110. #endif
  111. public:
  112. BOOST_STATIC_CONSTANT(bool, custom =
  113. (!is_same<std::allocator<char>, Base>::value));
  114. typedef typename lzma_allocator_traits<Alloc>::type allocator_type;
  115. static void* allocate(void* self, size_t items, size_t size);
  116. static void deallocate(void* self, void* address);
  117. };
  118. class BOOST_IOSTREAMS_DECL lzma_base {
  119. public:
  120. typedef char char_type;
  121. protected:
  122. lzma_base();
  123. ~lzma_base();
  124. void* stream() { return stream_; }
  125. template<typename Alloc>
  126. void init( const lzma_params& p,
  127. bool compress,
  128. lzma_allocator<Alloc>& zalloc )
  129. {
  130. bool custom = lzma_allocator<Alloc>::custom;
  131. do_init( p, compress,
  132. custom ? lzma_allocator<Alloc>::allocate : 0,
  133. custom ? lzma_allocator<Alloc>::deallocate : 0,
  134. &zalloc );
  135. }
  136. void before( const char*& src_begin, const char* src_end,
  137. char*& dest_begin, char* dest_end );
  138. void after( const char*& src_begin, char*& dest_begin,
  139. bool compress );
  140. int deflate(int action);
  141. int inflate(int action);
  142. void reset(bool compress, bool realloc);
  143. private:
  144. void do_init( const lzma_params& p, bool compress,
  145. lzma::alloc_func,
  146. lzma::free_func,
  147. void* derived );
  148. void init_stream(bool compress);
  149. void* stream_; // Actual type: lzma_stream*.
  150. uint32_t level_;
  151. uint32_t threads_;
  152. };
  153. //
  154. // Template name: lzma_compressor_impl
  155. // Description: Model of C-Style Filter implementing compression by
  156. // delegating to the lzma function deflate.
  157. //
  158. template<typename Alloc = std::allocator<char> >
  159. class lzma_compressor_impl : public lzma_base, public lzma_allocator<Alloc> {
  160. public:
  161. lzma_compressor_impl(const lzma_params& = lzma_params());
  162. ~lzma_compressor_impl();
  163. bool filter( const char*& src_begin, const char* src_end,
  164. char*& dest_begin, char* dest_end, bool flush );
  165. void close();
  166. };
  167. //
  168. // Template name: lzma_compressor_impl
  169. // Description: Model of C-Style Filte implementing decompression by
  170. // delegating to the lzma function inflate.
  171. //
  172. template<typename Alloc = std::allocator<char> >
  173. class lzma_decompressor_impl : public lzma_base, public lzma_allocator<Alloc> {
  174. public:
  175. lzma_decompressor_impl(const lzma_params&);
  176. lzma_decompressor_impl();
  177. ~lzma_decompressor_impl();
  178. bool filter( const char*& begin_in, const char* end_in,
  179. char*& begin_out, char* end_out, bool flush );
  180. void close();
  181. };
  182. } // End namespace detail.
  183. //
  184. // Template name: lzma_compressor
  185. // Description: Model of InputFilter and OutputFilter implementing
  186. // compression using lzma.
  187. //
  188. template<typename Alloc = std::allocator<char> >
  189. struct basic_lzma_compressor
  190. : symmetric_filter<detail::lzma_compressor_impl<Alloc>, Alloc>
  191. {
  192. private:
  193. typedef detail::lzma_compressor_impl<Alloc> impl_type;
  194. typedef symmetric_filter<impl_type, Alloc> base_type;
  195. public:
  196. typedef typename base_type::char_type char_type;
  197. typedef typename base_type::category category;
  198. basic_lzma_compressor( const lzma_params& = lzma_params(),
  199. std::streamsize buffer_size = default_device_buffer_size );
  200. };
  201. BOOST_IOSTREAMS_PIPABLE(basic_lzma_compressor, 1)
  202. typedef basic_lzma_compressor<> lzma_compressor;
  203. //
  204. // Template name: lzma_decompressor
  205. // Description: Model of InputFilter and OutputFilter implementing
  206. // decompression using lzma.
  207. //
  208. template<typename Alloc = std::allocator<char> >
  209. struct basic_lzma_decompressor
  210. : symmetric_filter<detail::lzma_decompressor_impl<Alloc>, Alloc>
  211. {
  212. private:
  213. typedef detail::lzma_decompressor_impl<Alloc> impl_type;
  214. typedef symmetric_filter<impl_type, Alloc> base_type;
  215. public:
  216. typedef typename base_type::char_type char_type;
  217. typedef typename base_type::category category;
  218. basic_lzma_decompressor( std::streamsize buffer_size = default_device_buffer_size );
  219. basic_lzma_decompressor( const lzma_params& p,
  220. std::streamsize buffer_size = default_device_buffer_size );
  221. };
  222. BOOST_IOSTREAMS_PIPABLE(basic_lzma_decompressor, 1)
  223. typedef basic_lzma_decompressor<> lzma_decompressor;
  224. //----------------------------------------------------------------------------//
  225. //------------------Implementation of lzma_allocator--------------------------//
  226. namespace detail {
  227. template<typename Alloc, typename Base>
  228. void* lzma_allocator<Alloc, Base>::allocate
  229. (void* self, size_t items, size_t size)
  230. {
  231. size_type len = items * size;
  232. char* ptr =
  233. static_cast<allocator_type*>(self)->allocate
  234. (len + sizeof(size_type)
  235. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  236. , (char*)0
  237. #endif
  238. );
  239. *reinterpret_cast<size_type*>(ptr) = len;
  240. return ptr + sizeof(size_type);
  241. }
  242. template<typename Alloc, typename Base>
  243. void lzma_allocator<Alloc, Base>::deallocate(void* self, void* address)
  244. {
  245. char* ptr = reinterpret_cast<char*>(address) - sizeof(size_type);
  246. size_type len = *reinterpret_cast<size_type*>(ptr) + sizeof(size_type);
  247. static_cast<allocator_type*>(self)->deallocate(ptr, len);
  248. }
  249. //------------------Implementation of lzma_compressor_impl--------------------//
  250. template<typename Alloc>
  251. lzma_compressor_impl<Alloc>::lzma_compressor_impl(const lzma_params& p)
  252. { init(p, true, static_cast<lzma_allocator<Alloc>&>(*this)); }
  253. template<typename Alloc>
  254. lzma_compressor_impl<Alloc>::~lzma_compressor_impl()
  255. { reset(true, false); }
  256. template<typename Alloc>
  257. bool lzma_compressor_impl<Alloc>::filter
  258. ( const char*& src_begin, const char* src_end,
  259. char*& dest_begin, char* dest_end, bool flush )
  260. {
  261. before(src_begin, src_end, dest_begin, dest_end);
  262. int result = deflate(flush ? lzma::finish : lzma::run);
  263. after(src_begin, dest_begin, true);
  264. lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
  265. return result != lzma::stream_end;
  266. }
  267. template<typename Alloc>
  268. void lzma_compressor_impl<Alloc>::close() { reset(true, true); }
  269. //------------------Implementation of lzma_decompressor_impl------------------//
  270. template<typename Alloc>
  271. lzma_decompressor_impl<Alloc>::lzma_decompressor_impl(const lzma_params& p)
  272. { init(p, false, static_cast<lzma_allocator<Alloc>&>(*this)); }
  273. template<typename Alloc>
  274. lzma_decompressor_impl<Alloc>::~lzma_decompressor_impl()
  275. { reset(false, false); }
  276. template<typename Alloc>
  277. lzma_decompressor_impl<Alloc>::lzma_decompressor_impl()
  278. {
  279. lzma_params p;
  280. init(p, false, static_cast<lzma_allocator<Alloc>&>(*this));
  281. }
  282. template<typename Alloc>
  283. bool lzma_decompressor_impl<Alloc>::filter
  284. ( const char*& src_begin, const char* src_end,
  285. char*& dest_begin, char* dest_end, bool flush )
  286. {
  287. before(src_begin, src_end, dest_begin, dest_end);
  288. int result = inflate(flush ? lzma::finish : lzma::run);
  289. after(src_begin, dest_begin, false);
  290. lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
  291. return result != lzma::stream_end;
  292. }
  293. template<typename Alloc>
  294. void lzma_decompressor_impl<Alloc>::close() { reset(false, true); }
  295. } // End namespace detail.
  296. //------------------Implementation of lzma_compressor-----------------------//
  297. template<typename Alloc>
  298. basic_lzma_compressor<Alloc>::basic_lzma_compressor
  299. (const lzma_params& p, std::streamsize buffer_size)
  300. : base_type(buffer_size, p) { }
  301. //------------------Implementation of lzma_decompressor-----------------------//
  302. template<typename Alloc>
  303. basic_lzma_decompressor<Alloc>::basic_lzma_decompressor
  304. (std::streamsize buffer_size)
  305. : base_type(buffer_size) { }
  306. template<typename Alloc>
  307. basic_lzma_decompressor<Alloc>::basic_lzma_decompressor
  308. (const lzma_params& p, std::streamsize buffer_size)
  309. : base_type(buffer_size, p) { }
  310. //----------------------------------------------------------------------------//
  311. } } // End namespaces iostreams, boost.
  312. #include <boost/config/abi_suffix.hpp> // Pops abi_suffix.hpp pragmas.
  313. #ifdef BOOST_MSVC
  314. # pragma warning(pop)
  315. #endif
  316. #endif // #ifndef BOOST_IOSTREAMS_LZMA_HPP_INCLUDED