]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/src/zlib.cpp
update sources to v12.2.3
[ceph.git] / 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.)
5
6 // See http://www.boost.org/libs/iostreams for documentation.
7
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
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/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
24
25 namespace boost { namespace iostreams {
26
27 namespace zlib {
28
29 // Compression levels
30
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;
35
36 // Compression methods
37
38 const int deflated = Z_DEFLATED;
39
40 // Compression strategies
41
42 const int default_strategy = Z_DEFAULT_STRATEGY;
43 const int filtered = Z_FILTERED;
44 const int huffman_only = Z_HUFFMAN_ONLY;
45
46 // Status codes
47
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;
55
56 // Flush codes
57
58 const int finish = Z_FINISH;
59 const int no_flush = Z_NO_FLUSH;
60 const int sync_flush = Z_SYNC_FLUSH;
61
62 // Code for current OS
63
64 //const int os_code = OS_CODE;
65
66 } // End namespace zlib.
67
68 //------------------Implementation of zlib_error------------------------------//
69
70 zlib_error::zlib_error(int error)
71 : BOOST_IOSTREAMS_FAILURE("zlib error"), error_(error)
72 { }
73
74 void zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(int error)
75 {
76 switch (error) {
77 case Z_OK:
78 case Z_STREAM_END:
79 //case Z_BUF_ERROR:
80 return;
81 case Z_MEM_ERROR:
82 boost::throw_exception(std::bad_alloc());
83 default:
84 boost::throw_exception(zlib_error(error));
85 ;
86 }
87 }
88
89 //------------------Implementation of zlib_base-------------------------------//
90
91 namespace detail {
92
93 zlib_base::zlib_base()
94 : stream_(new z_stream), calculate_crc_(false), crc_(0), crc_imp_(0)
95 { }
96
97 zlib_base::~zlib_base() { delete static_cast<z_stream*>(stream_); }
98
99 void zlib_base::before( const char*& src_begin, const char* src_end,
100 char*& dest_begin, char* dest_end )
101 {
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);
107 }
108
109 void zlib_base::after(const char*& src_begin, char*& dest_begin, bool compress)
110 {
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)
119 );
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);
124 }
125 total_in_ = s->total_in;
126 total_out_ = s->total_out;
127 src_begin = next_in;
128 dest_begin = next_out;
129 }
130
131 int zlib_base::xdeflate(int flush)
132 {
133 return ::deflate(static_cast<z_stream*>(stream_), flush);
134 }
135
136 int zlib_base::xinflate(int flush)
137 {
138 return ::inflate(static_cast<z_stream*>(stream_), flush);
139 }
140
141 void zlib_base::reset(bool compress, bool realloc)
142 {
143 z_stream* s = static_cast<z_stream*>(stream_);
144 // Undiagnosed bug:
145 // deflateReset(), etc., return Z_DATA_ERROR
146 //zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
147 realloc ?
148 (compress ? deflateReset(s) : inflateReset(s)) :
149 (compress ? deflateEnd(s) : inflateEnd(s))
150 ;
151 //);
152 crc_imp_ = 0;
153 }
154
155 void zlib_base::do_init
156 ( const zlib_params& p, bool compress,
157 zlib::xalloc_func /* alloc */, zlib::xfree_func /* free*/,
158 void* derived )
159 {
160 calculate_crc_ = p.calculate_crc;
161 z_stream* s = static_cast<z_stream*>(stream_);
162
163 // Current interface for customizing memory management
164 // is non-conforming and has been disabled:
165 // s->zalloc = alloc;
166 // s->zfree = free;
167 s->zalloc = 0;
168 s->zfree = 0;
169 s->opaque = derived;
170 int window_bits = p.noheader? -p.window_bits : p.window_bits;
171 zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
172 compress ?
173 deflateInit2( s,
174 p.level,
175 p.method,
176 window_bits,
177 p.mem_level,
178 p.strategy ) :
179 inflateInit2(s, window_bits)
180 );
181 }
182
183 } // End namespace detail.
184
185 //----------------------------------------------------------------------------//
186
187 } } // End namespaces iostreams, boost.