DGtal  0.9.3
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 
17 #define BOOST_IOSTREAMS_SOURCE
18 
19 #include <boost/throw_exception.hpp>
20 #include <boost/iostreams/detail/config/dyn_link.hpp>
21 #include <boost/iostreams/filter/zlib.hpp>
22 #include "zlib.h" // Jean-loup Gailly's and Mark Adler's "zlib.h" header.
23  // To configure Boost to work with zlib, see the
24  // installation instructions here:
25  // http://boost.org/libs/iostreams/doc/index.html?path=7
26 
27 namespace boost { namespace iostreams {
28 
29 namespace zlib {
30 
31  // Compression levels
32 
33 const int no_compression = Z_NO_COMPRESSION;
34 const int best_speed = Z_BEST_SPEED;
35 const int best_compression = Z_BEST_COMPRESSION;
36 const int default_compression = Z_DEFAULT_COMPRESSION;
37 
38  // Compression methods
39 
40 const int deflated = Z_DEFLATED;
41 
42  // Compression strategies
43 
44 const int default_strategy = Z_DEFAULT_STRATEGY;
45 const int filtered = Z_FILTERED;
46 const int huffman_only = Z_HUFFMAN_ONLY;
47 
48  // Status codes
49 
50 const int okay = Z_OK;
51 const int stream_end = Z_STREAM_END;
52 const int stream_error = Z_STREAM_ERROR;
53 const int version_error = Z_VERSION_ERROR;
54 const int data_error = Z_DATA_ERROR;
55 const int mem_error = Z_MEM_ERROR;
56 const int buf_error = Z_BUF_ERROR;
57 
58  // Flush codes
59 
60 const int finish = Z_FINISH;
61 const int no_flush = Z_NO_FLUSH;
62 const int sync_flush = Z_SYNC_FLUSH;
63 
64  // Code for current OS
65 
66 //const int os_code = OS_CODE;
67 
68 } // End namespace zlib.
69 
70 //------------------Implementation of zlib_error------------------------------//
71 
72 zlib_error::zlib_error(int _error)
73  : BOOST_IOSTREAMS_FAILURE("zlib error"), error_(_error)
74  { }
75 
76 void zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(int _error)
77 {
78  switch (_error) {
79  case Z_OK:
80  case Z_STREAM_END:
81  //case Z_BUF_ERROR:
82  return;
83  case Z_MEM_ERROR:
84  boost::throw_exception(std::bad_alloc());
85  default:
86  boost::throw_exception(zlib_error(_error));
87  ;
88  }
89 }
90 
91 //------------------Implementation of zlib_base-------------------------------//
92 
93 namespace detail {
94 
95 zlib_base::zlib_base()
96  : stream_(new z_stream), calculate_crc_(false), crc_(0), crc_imp_(0)
97  { }
98 
99 zlib_base::~zlib_base() { delete static_cast<z_stream*>(stream_); }
100 
101 void zlib_base::before( const char*& src_begin, const char* src_end,
102  char*& dest_begin, char* dest_end )
103 {
104  z_stream* s = static_cast<z_stream*>(stream_);
105  s->next_in = reinterpret_cast<zlib::byte*>(const_cast<char*>(src_begin));
106  s->avail_in = static_cast<zlib::uint>(src_end - src_begin);
107  s->next_out = reinterpret_cast<zlib::byte*>(dest_begin);
108  s->avail_out= static_cast<zlib::uint>(dest_end - dest_begin);
109 }
110 
111 void zlib_base::after(const char*& src_begin, char*& dest_begin, bool compress)
112 {
113  z_stream* s = static_cast<z_stream*>(stream_);
114  char* next_in = reinterpret_cast<char*>(s->next_in);
115  char* next_out = reinterpret_cast<char*>(s->next_out);
116  if (calculate_crc_) {
117  const zlib::byte* buf = compress ?
118  reinterpret_cast<const zlib::byte*>(src_begin) :
119  reinterpret_cast<const zlib::byte*>(
120  const_cast<const char*>(dest_begin)
121  );
122  zlib::uint length = compress ?
123  static_cast<zlib::uint>(next_in - src_begin) :
124  static_cast<zlib::uint>(next_out - dest_begin);
125  if (length > 0)
126  crc_ = crc_imp_ = crc32(crc_imp_, buf, length);
127  }
128  total_in_ = s->total_in;
129  total_out_ = s->total_out;
130  src_begin = const_cast<const char*>(next_in);
131  dest_begin = next_out;
132 }
133 
134 int zlib_base::xdeflate(int flush)
135 {
136  return ::deflate(static_cast<z_stream*>(stream_), flush);
137 }
138 
139 int zlib_base::xinflate(int flush)
140 {
141  return ::inflate(static_cast<z_stream*>(stream_), flush);
142 }
143 
144 void zlib_base::reset(bool compress, bool realloc)
145 {
146  z_stream* s = static_cast<z_stream*>(stream_);
147  // Undiagnosed bug:
148  // deflateReset(), etc., return Z_DATA_ERROR
149  //zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
150  realloc ?
151  (compress ? deflateReset(s) : inflateReset(s)) :
152  (compress ? deflateEnd(s) : inflateEnd(s))
153  ;
154  //);
155  crc_imp_ = 0;
156 }
157 
158 void zlib_base::do_init
159  ( const zlib_params& p, bool compress,
160  #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
161  zlib::xalloc_func /* alloc */, zlib::xfree_func /* free*/,
162  #endif
163  void* derived )
164 {
165  calculate_crc_ = p.calculate_crc;
166  z_stream* s = static_cast<z_stream*>(stream_);
167 
168  // Current interface for customizing memory management
169  // is non-conforming and has been disabled:
170  //#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
171  // s->zalloc = alloc;
172  // s->zfree = free;
173  //#else
174  s->zalloc = 0;
175  s->zfree = 0;
176  //#endif
177  s->opaque = derived;
178  int window_bits = p.noheader? -p.window_bits : p.window_bits;
179  zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
180  compress ?
181  deflateInit2( s,
182  p.level,
183  p.method,
184  window_bits,
185  p.mem_level,
186  p.strategy ) :
187  inflateInit2(s, window_bits)
188  );
189 }
190 
191 } // End namespace detail.
192 
193 //----------------------------------------------------------------------------//
194 
195 } } // End namespaces iostreams, boost.
const int stream_error
Definition: zlib.cpp:52
const int huffman_only
Definition: zlib.cpp:46
Definition: zlib.cpp:27
const int no_compression
Definition: zlib.cpp:33
const int sync_flush
Definition: zlib.cpp:62
const int buf_error
Definition: zlib.cpp:56
void zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(int _error)
Definition: zlib.cpp:76
const int stream_end
Definition: zlib.cpp:51
const int version_error
Definition: zlib.cpp:53
const int data_error
Definition: zlib.cpp:54
const int filtered
Definition: zlib.cpp:45
const int deflated
Definition: zlib.cpp:40
const int mem_error
Definition: zlib.cpp:55
const int default_strategy
Definition: zlib.cpp:44
const int no_flush
Definition: zlib.cpp:61
const int best_compression
Definition: zlib.cpp:35
const int finish
Definition: zlib.cpp:60
const int okay
Definition: zlib.cpp:50
const int best_speed
Definition: zlib.cpp:34
const int default_compression
Definition: zlib.cpp:36