]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/iostreams/src/bzip2.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / iostreams / src / bzip2.cpp
CommitLineData
7c673cae
FG
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
6// See http://www.boost.org/libs/iostreams for documentation.
7
8// To configure Boost to work with libbz2, see the
9// installation instructions here:
10// http://boost.org/libs/iostreams/doc/index.html?path=7
11
12// Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp>
13// knows that we are building the library (possibly exporting code), rather
14// than using it (possibly importing code).
15#define BOOST_IOSTREAMS_SOURCE
16
17#include <boost/throw_exception.hpp>
18#include <boost/iostreams/detail/config/dyn_link.hpp>
19#include <boost/iostreams/filter/bzip2.hpp>
20#include "bzlib.h" // Julian Seward's "bzip.h" header.
21 // To configure Boost to work with libbz2, see the
22 // installation instructions here:
23 // http://boost.org/libs/iostreams/doc/index.html?path=7
24
25namespace boost { namespace iostreams {
26
27namespace bzip2 {
28
29 // Status codes
30
31const int ok = BZ_OK;
32const int run_ok = BZ_RUN_OK;
33const int flush_ok = BZ_FLUSH_OK;
34const int finish_ok = BZ_FINISH_OK;
35const int stream_end = BZ_STREAM_END;
36const int sequence_error = BZ_SEQUENCE_ERROR;
37const int param_error = BZ_PARAM_ERROR;
38const int mem_error = BZ_MEM_ERROR;
39const int data_error = BZ_DATA_ERROR;
40const int data_error_magic = BZ_DATA_ERROR_MAGIC;
41const int io_error = BZ_IO_ERROR;
42const int unexpected_eof = BZ_UNEXPECTED_EOF;
43const int outbuff_full = BZ_OUTBUFF_FULL;
44const int config_error = BZ_CONFIG_ERROR;
45
46 // Action codes
47
48const int finish = BZ_FINISH;
49const int run = BZ_RUN;
50
51} // End namespace bzip2.
52
53//------------------Implementation of bzip2_error-----------------------------//
54
55bzip2_error::bzip2_error(int error)
56 : BOOST_IOSTREAMS_FAILURE("bzip2 error"), error_(error)
57 { }
58
59void bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(int error)
60{
61 switch (error) {
62 case BZ_OK:
63 case BZ_RUN_OK:
64 case BZ_FLUSH_OK:
65 case BZ_FINISH_OK:
66 case BZ_STREAM_END:
67 return;
68 case BZ_MEM_ERROR:
69 boost::throw_exception(std::bad_alloc());
70 default:
71 boost::throw_exception(bzip2_error(error));
72 }
73}
74
75//------------------Implementation of bzip2_base------------------------------//
76
77namespace detail {
78
79bzip2_base::bzip2_base(const bzip2_params& params)
80 : params_(params), stream_(new bz_stream), ready_(false)
81 { }
82
83bzip2_base::~bzip2_base() { delete static_cast<bz_stream*>(stream_); }
84
85void bzip2_base::before( const char*& src_begin, const char* src_end,
86 char*& dest_begin, char* dest_end )
87{
88 bz_stream* s = static_cast<bz_stream*>(stream_);
89 s->next_in = const_cast<char*>(src_begin);
90 s->avail_in = static_cast<unsigned>(src_end - src_begin);
91 s->next_out = reinterpret_cast<char*>(dest_begin);
92 s->avail_out= static_cast<unsigned>(dest_end - dest_begin);
93}
94
95void bzip2_base::after(const char*& src_begin, char*& dest_begin)
96{
97 bz_stream* s = static_cast<bz_stream*>(stream_);
98 src_begin = const_cast<char*>(s->next_in);
99 dest_begin = s->next_out;
100}
101
102int bzip2_base::check_end(const char* src_begin, const char* dest_begin)
103{
104 bz_stream* s = static_cast<bz_stream*>(stream_);
105 if( src_begin == s->next_in &&
106 s->avail_in == 0 &&
107 dest_begin == s->next_out) {
108 return bzip2::unexpected_eof;
109 } else {
110 return bzip2::ok;
111 }
112}
113
114void bzip2_base::end(bool compress)
115{
116 if(!ready_) return;
117 ready_ = false;
118 bz_stream* s = static_cast<bz_stream*>(stream_);
119 bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
120 compress ?
121 BZ2_bzCompressEnd(s) :
122 BZ2_bzDecompressEnd(s)
123 );
124}
125
126int bzip2_base::compress(int action)
127{
128 return BZ2_bzCompress(static_cast<bz_stream*>(stream_), action);
129}
130
131int bzip2_base::decompress()
132{
133 return BZ2_bzDecompress(static_cast<bz_stream*>(stream_));
134}
135
136void bzip2_base::do_init
137 ( bool compress,
b32b8144
FG
138 bzip2::alloc_func /* alloc */,
139 bzip2::free_func /* free */,
7c673cae
FG
140 void* derived )
141{
142 bz_stream* s = static_cast<bz_stream*>(stream_);
143
144 // Current interface for customizing memory management
145 // is non-conforming and has been disabled:
7c673cae
FG
146 // s->bzalloc = alloc;
147 // s->bzfree = free;
7c673cae
FG
148 s->bzalloc = 0;
149 s->bzfree = 0;
7c673cae
FG
150 s->opaque = derived;
151 bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
152 compress ?
153 BZ2_bzCompressInit( s,
154 params_.block_size,
155 0,
156 params_.work_factor ) :
157 BZ2_bzDecompressInit( s,
158 0,
159 params_.small )
160 );
161 ready_ = true;
162}
163
164} // End namespace detail.
165
166//----------------------------------------------------------------------------//
167
168} } // End namespaces iostreams, boost.