]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/src/zlib.cpp
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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 // To configure Boost to work with zlib, see the
9 // installation instructions here:
10 // http://boost.org/libs/iostreams/doc/index.html?path=7
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
17 #include <boost/throw_exception.hpp>
18 #include <boost/iostreams/detail/config/dyn_link.hpp>
19 #include <boost/iostreams/filter/zlib.hpp>
20 #include "zlib.h" // Jean-loup Gailly's and Mark Adler's "zlib.h" header.
21 // To configure Boost to work with zlib, see the
22 // installation instructions here:
23 // http://boost.org/libs/iostreams/doc/index.html?path=7
25 namespace boost
{ namespace iostreams
{
31 const int no_compression
= Z_NO_COMPRESSION
;
32 const int best_speed
= Z_BEST_SPEED
;
33 const int best_compression
= Z_BEST_COMPRESSION
;
34 const int default_compression
= Z_DEFAULT_COMPRESSION
;
36 // Compression methods
38 const int deflated
= Z_DEFLATED
;
40 // Compression strategies
42 const int default_strategy
= Z_DEFAULT_STRATEGY
;
43 const int filtered
= Z_FILTERED
;
44 const int huffman_only
= Z_HUFFMAN_ONLY
;
48 const int okay
= Z_OK
;
49 const int stream_end
= Z_STREAM_END
;
50 const int stream_error
= Z_STREAM_ERROR
;
51 const int version_error
= Z_VERSION_ERROR
;
52 const int data_error
= Z_DATA_ERROR
;
53 const int mem_error
= Z_MEM_ERROR
;
54 const int buf_error
= Z_BUF_ERROR
;
58 const int finish
= Z_FINISH
;
59 const int no_flush
= Z_NO_FLUSH
;
60 const int sync_flush
= Z_SYNC_FLUSH
;
62 // Code for current OS
64 //const int os_code = OS_CODE;
66 } // End namespace zlib.
68 //------------------Implementation of zlib_error------------------------------//
70 zlib_error::zlib_error(int error
)
71 : BOOST_IOSTREAMS_FAILURE("zlib error"), error_(error
)
74 void zlib_error::check
BOOST_PREVENT_MACRO_SUBSTITUTION(int error
)
82 boost::throw_exception(std::bad_alloc());
84 boost::throw_exception(zlib_error(error
));
89 //------------------Implementation of zlib_base-------------------------------//
93 zlib_base::zlib_base()
94 : stream_(new z_stream
), calculate_crc_(false), crc_(0), crc_imp_(0)
97 zlib_base::~zlib_base() { delete static_cast<z_stream
*>(stream_
); }
99 void zlib_base::before( const char*& src_begin
, const char* src_end
,
100 char*& dest_begin
, char* dest_end
)
102 z_stream
* s
= static_cast<z_stream
*>(stream_
);
103 s
->next_in
= reinterpret_cast<zlib::byte
*>(const_cast<char*>(src_begin
));
104 s
->avail_in
= static_cast<zlib::uint
>(src_end
- src_begin
);
105 s
->next_out
= reinterpret_cast<zlib::byte
*>(dest_begin
);
106 s
->avail_out
= static_cast<zlib::uint
>(dest_end
- dest_begin
);
109 void zlib_base::after(const char*& src_begin
, char*& dest_begin
, bool compress
)
111 z_stream
* s
= static_cast<z_stream
*>(stream_
);
112 const char* next_in
= reinterpret_cast<const char*>(s
->next_in
);
113 char* next_out
= reinterpret_cast<char*>(s
->next_out
);
114 if (calculate_crc_
) {
115 const zlib::byte
* buf
= compress
?
116 reinterpret_cast<const zlib::byte
*>(src_begin
) :
117 reinterpret_cast<const zlib::byte
*>(
118 const_cast<const char*>(dest_begin
)
120 zlib::uint length
= compress
?
121 static_cast<zlib::uint
>(next_in
- src_begin
) :
122 static_cast<zlib::uint
>(next_out
- dest_begin
);
123 crc_
= crc_imp_
= crc32(crc_imp_
, buf
, length
);
125 total_in_
= s
->total_in
;
126 total_out_
= s
->total_out
;
128 dest_begin
= next_out
;
131 int zlib_base::xdeflate(int flush
)
133 return ::deflate(static_cast<z_stream
*>(stream_
), flush
);
136 int zlib_base::xinflate(int flush
)
138 return ::inflate(static_cast<z_stream
*>(stream_
), flush
);
141 void zlib_base::reset(bool compress
, bool realloc
)
143 z_stream
* s
= static_cast<z_stream
*>(stream_
);
145 // deflateReset(), etc., return Z_DATA_ERROR
146 //zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
148 (compress
? deflateReset(s
) : inflateReset(s
)) :
149 (compress
? deflateEnd(s
) : inflateEnd(s
))
155 void zlib_base::do_init
156 ( const zlib_params
& p
, bool compress
,
157 zlib::xalloc_func
/* alloc */, zlib::xfree_func
/* free*/,
160 calculate_crc_
= p
.calculate_crc
;
161 z_stream
* s
= static_cast<z_stream
*>(stream_
);
163 // Current interface for customizing memory management
164 // is non-conforming and has been disabled:
165 // s->zalloc = alloc;
170 int window_bits
= p
.noheader
? -p
.window_bits
: p
.window_bits
;
171 zlib_error::check
BOOST_PREVENT_MACRO_SUBSTITUTION(
179 inflateInit2(s
, window_bits
)
183 } // End namespace detail.
185 //----------------------------------------------------------------------------//
187 } } // End namespaces iostreams, boost.