]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/src/lzma.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / iostreams / src / lzma.cpp
1 // (C) Copyright Milan Svoboda 2008.
2 // Originally developed under the fusecompress project.
3 // Based on bzip2.cpp by:
4 // (C) Copyright Jonathan Turkanis 2003.
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
7
8 // See http://www.boost.org/libs/iostreams for documentation.
9
10 // Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp>
11 // knows that we are building the library (possibly exporting code), rather
12 // than using it (possibly importing code).
13 #define BOOST_IOSTREAMS_SOURCE
14
15 #include <lzma.h>
16
17 #include <boost/throw_exception.hpp>
18 #include <boost/iostreams/detail/config/dyn_link.hpp>
19 #include <boost/iostreams/filter/lzma.hpp>
20
21 namespace boost { namespace iostreams {
22
23 namespace lzma {
24
25 // Compression levels
26
27 const uint32_t no_compression = 0;
28 const uint32_t best_speed = 1;
29 const uint32_t best_compression = 9;
30 const uint32_t default_compression = 2;
31
32 // Status codes
33
34 const int okay = LZMA_OK;
35 const int stream_end = LZMA_STREAM_END;
36 const int unsupported_check = LZMA_UNSUPPORTED_CHECK;
37 const int mem_error = LZMA_MEM_ERROR;
38 const int options_error = LZMA_OPTIONS_ERROR;
39 const int data_error = LZMA_DATA_ERROR;
40 const int buf_error = LZMA_BUF_ERROR;
41 const int prog_error = LZMA_PROG_ERROR;
42
43 // Flush codes
44
45 const int finish = LZMA_FINISH;
46 const int full_flush = LZMA_FULL_FLUSH;
47 const int sync_flush = LZMA_SYNC_FLUSH;
48 const int run = LZMA_RUN;
49
50 // Code for current OS
51
52 } // End namespace lzma.
53
54 //------------------Implementation of lzma_error------------------------------//
55
56 lzma_error::lzma_error(int error)
57 : BOOST_IOSTREAMS_FAILURE("lzma error"), error_(error)
58 { }
59
60 void lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(int error)
61 {
62 switch (error) {
63 case LZMA_OK:
64 case LZMA_STREAM_END:
65 return;
66 case LZMA_MEM_ERROR:
67 boost::throw_exception(std::bad_alloc());
68 default:
69 boost::throw_exception(lzma_error(error));
70 }
71 }
72
73 //------------------Implementation of lzma_base-------------------------------//
74
75 namespace detail {
76
77 lzma_base::lzma_base()
78 : stream_(new lzma_stream)
79 { }
80
81 lzma_base::~lzma_base() { delete static_cast<lzma_stream*>(stream_); }
82
83 void lzma_base::before( const char*& src_begin, const char* src_end,
84 char*& dest_begin, char* dest_end )
85 {
86 lzma_stream* s = static_cast<lzma_stream*>(stream_);
87 s->next_in = reinterpret_cast<uint8_t*>(const_cast<char*>(src_begin));
88 s->avail_in = static_cast<size_t>(src_end - src_begin);
89 s->next_out = reinterpret_cast<uint8_t*>(dest_begin);
90 s->avail_out= static_cast<size_t>(dest_end - dest_begin);
91 }
92
93 void lzma_base::after(const char*& src_begin, char*& dest_begin, bool)
94 {
95 lzma_stream* s = static_cast<lzma_stream*>(stream_);
96 src_begin = const_cast<const char*>(reinterpret_cast<const char*>(s->next_in));
97 dest_begin = reinterpret_cast<char*>(s->next_out);
98 }
99
100 int lzma_base::deflate(int action)
101 {
102 return lzma_code(static_cast<lzma_stream*>(stream_), static_cast<lzma_action>(action));
103 }
104
105 int lzma_base::inflate(int action)
106 {
107 return lzma_code(static_cast<lzma_stream*>(stream_), static_cast<lzma_action>(action));
108 }
109
110 void lzma_base::reset(bool compress, bool realloc)
111 {
112 lzma_stream* s = static_cast<lzma_stream*>(stream_);
113 lzma_end(s);
114 if (realloc)
115 {
116 memset(s, 0, sizeof(*s));
117
118 lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
119 compress ?
120 lzma_easy_encoder(s, level, LZMA_CHECK_CRC32) :
121 lzma_stream_decoder(s, 100 * 1024 * 1024, LZMA_CONCATENATED)
122 );
123 }
124 }
125
126 void lzma_base::do_init
127 ( const lzma_params& p, bool compress,
128 lzma::alloc_func, lzma::free_func,
129 void* )
130 {
131 lzma_stream* s = static_cast<lzma_stream*>(stream_);
132
133 memset(s, 0, sizeof(*s));
134
135 level = p.level;
136 lzma_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
137 compress ?
138 lzma_easy_encoder(s, p.level, LZMA_CHECK_CRC32) :
139 lzma_stream_decoder(s, 100 * 1024 * 1024, LZMA_CONCATENATED)
140 );
141 }
142
143 } // End namespace detail.
144
145 //----------------------------------------------------------------------------//
146
147 } } // End namespaces iostreams, boost.