]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/iostreams/src/zlib.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / iostreams / src / zlib.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 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
25namespace boost { namespace iostreams {
26
27namespace zlib {
28
29 // Compression levels
30
31const int no_compression = Z_NO_COMPRESSION;
32const int best_speed = Z_BEST_SPEED;
33const int best_compression = Z_BEST_COMPRESSION;
34const int default_compression = Z_DEFAULT_COMPRESSION;
35
36 // Compression methods
37
38const int deflated = Z_DEFLATED;
39
40 // Compression strategies
41
42const int default_strategy = Z_DEFAULT_STRATEGY;
43const int filtered = Z_FILTERED;
44const int huffman_only = Z_HUFFMAN_ONLY;
45
46 // Status codes
47
48const int okay = Z_OK;
49const int stream_end = Z_STREAM_END;
50const int stream_error = Z_STREAM_ERROR;
51const int version_error = Z_VERSION_ERROR;
52const int data_error = Z_DATA_ERROR;
53const int mem_error = Z_MEM_ERROR;
54const int buf_error = Z_BUF_ERROR;
55
56 // Flush codes
57
58const int finish = Z_FINISH;
59const int no_flush = Z_NO_FLUSH;
60const 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
70zlib_error::zlib_error(int error)
71 : BOOST_IOSTREAMS_FAILURE("zlib error"), error_(error)
72 { }
73
74void 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
91namespace detail {
92
93zlib_base::zlib_base()
94 : stream_(new z_stream), calculate_crc_(false), crc_(0), crc_imp_(0)
95 { }
96
97zlib_base::~zlib_base() { delete static_cast<z_stream*>(stream_); }
98
99void 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
109void zlib_base::after(const char*& src_begin, char*& dest_begin, bool compress)
110{
111 z_stream* s = static_cast<z_stream*>(stream_);
112 char* next_in = reinterpret_cast<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 if (length > 0)
124 crc_ = crc_imp_ = crc32(crc_imp_, buf, length);
125 }
126 total_in_ = s->total_in;
127 total_out_ = s->total_out;
128 src_begin = const_cast<const char*>(next_in);
129 dest_begin = next_out;
130}
131
132int zlib_base::xdeflate(int flush)
133{
134 return ::deflate(static_cast<z_stream*>(stream_), flush);
135}
136
137int zlib_base::xinflate(int flush)
138{
139 return ::inflate(static_cast<z_stream*>(stream_), flush);
140}
141
142void zlib_base::reset(bool compress, bool realloc)
143{
144 z_stream* s = static_cast<z_stream*>(stream_);
145 // Undiagnosed bug:
146 // deflateReset(), etc., return Z_DATA_ERROR
147 //zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
148 realloc ?
149 (compress ? deflateReset(s) : inflateReset(s)) :
150 (compress ? deflateEnd(s) : inflateEnd(s))
151 ;
152 //);
153 crc_imp_ = 0;
154}
155
156void zlib_base::do_init
157 ( const zlib_params& p, bool compress,
158 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
159 zlib::xalloc_func /* alloc */, zlib::xfree_func /* free*/,
160 #endif
161 void* derived )
162{
163 calculate_crc_ = p.calculate_crc;
164 z_stream* s = static_cast<z_stream*>(stream_);
165
166 // Current interface for customizing memory management
167 // is non-conforming and has been disabled:
168 //#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
169 // s->zalloc = alloc;
170 // s->zfree = free;
171 //#else
172 s->zalloc = 0;
173 s->zfree = 0;
174 //#endif
175 s->opaque = derived;
176 int window_bits = p.noheader? -p.window_bits : p.window_bits;
177 zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
178 compress ?
179 deflateInit2( s,
180 p.level,
181 p.method,
182 window_bits,
183 p.mem_level,
184 p.strategy ) :
185 inflateInit2(s, window_bits)
186 );
187}
188
189} // End namespace detail.
190
191//----------------------------------------------------------------------------//
192
193} } // End namespaces iostreams, boost.