Skip to content
Snippets Groups Projects
Commit 5a363d8d authored by Andreas Schenk's avatar Andreas Schenk
Browse files

added zlib.cpp and gzip.cpp. They are needed for boost::iostream

parent f76a66d5
Branches
Tags
No related merge requests found
...@@ -31,7 +31,12 @@ ring_finder.cc ...@@ -31,7 +31,12 @@ ring_finder.cc
module(NAME conop SOURCES ${OST_CONOP_SOURCES} module(NAME conop SOURCES ${OST_CONOP_SOURCES}
HEADERS ${OST_CONOP_HEADERS} DEPENDS_ON ost_mol ost_mol_alg ost_geom ost_db) HEADERS ${OST_CONOP_HEADERS} DEPENDS_ON ost_mol ost_mol_alg ost_geom ost_db)
executable(NAME chemdict_tool SOURCES chemdict_tool.cc DEPENDS_ON ost_io STATIC)
if (WIN32)
executable(NAME chemdict_tool SOURCES chemdict_tool.cc ../../io/src/zlib.cpp ../../io/src/gzip.cpp DEPENDS_ON ost_io STATIC)
else(WIN32)
executable(NAME chemdict_tool SOURCES chemdict_tool.cc DEPENDS_ON ost_io STATIC)
endif(WIN32)
if (COMPOUND_LIB) if (COMPOUND_LIB)
if (EXISTS "${COMPOUND_LIB}") if (EXISTS "${COMPOUND_LIB}")
......
...@@ -22,7 +22,9 @@ io_manager.cc ...@@ -22,7 +22,9 @@ io_manager.cc
convert.cc convert.cc
io_utils.cc io_utils.cc
) )
if (WIN32)
set(OST_IO_SOURCES ${OST_IO_SOURCES} zlib.cpp gzip.cpp)
endif(WIN32)
foreach(fname ${OST_IO_MOL_SOURCES}) foreach(fname ${OST_IO_MOL_SOURCES})
set(OST_IO_SOURCES ${OST_IO_SOURCES} mol/${fname}) set(OST_IO_SOURCES ${OST_IO_SOURCES} mol/${fname})
...@@ -61,10 +63,7 @@ module(NAME io SOURCES "${OST_IO_SOURCES}" ...@@ -61,10 +63,7 @@ module(NAME io SOURCES "${OST_IO_SOURCES}"
${OST_IO_SEQ_HEADERS} IN_DIR seq ${OST_IO_SEQ_HEADERS} IN_DIR seq
${OST_IO_HEADERS} ${OST_IO_HEADERS}
DEPENDS_ON ${OST_IO_DEPENDENCIES}) DEPENDS_ON ${OST_IO_DEPENDENCIES})
if (NOT WIN32) target_link_libraries(ost_io ${BOOST_IOSTREAM_LIBRARIES})
# see note in modules/gui/src/CMakeLists.txt
target_link_libraries(ost_io ${BOOST_IOSTREAM_LIBRARIES})
endif()
if (ENABLE_IMG) if (ENABLE_IMG)
target_link_libraries(ost_io ${TIFF_LIBRARIES} ${PNG_LIBRARIES}) target_link_libraries(ost_io ${TIFF_LIBRARIES} ${PNG_LIBRARIES})
endif() endif()
\ No newline at end of file
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2003-2007 Jonathan Turkanis
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
// See http://www.boost.org/libs/iostreams for documentation.
// To configure Boost to work with libbz2, see the
// installation instructions here:
// http://boost.org/libs/iostreams/doc/index.html?path=7
// Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp>
// knows that we are building the library (possibly exporting code), rather
// than using it (possibly importing code).
#define BOOST_IOSTREAMS_SOURCE
#include <boost/iostreams/detail/config/dyn_link.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/throw_exception.hpp>
namespace boost { namespace iostreams {
//------------------Implementation of gzip_header-----------------------------//
namespace detail {
void gzip_header::process(char c)
{
uint8_t value = static_cast<uint8_t>(c);
switch (state_) {
case s_id1:
if (value != gzip::magic::id1)
boost::throw_exception(gzip_error(gzip::bad_header));
state_ = s_id2;
break;
case s_id2:
if (value != gzip::magic::id2)
boost::throw_exception(gzip_error(gzip::bad_header));
state_ = s_cm;
break;
case s_cm:
if (value != gzip::method::deflate)
boost::throw_exception(gzip_error(gzip::bad_method));
state_ = s_flg;
break;
case s_flg:
flags_ = value;
state_ = s_mtime;
break;
case s_mtime:
mtime_ += value << (offset_ * 8);
if (offset_ == 3) {
state_ = s_xfl;
offset_ = 0;
} else {
++offset_;
}
break;
case s_xfl:
state_ = s_os;
break;
case s_os:
os_ = value;
if (flags_ & gzip::flags::extra) {
state_ = s_extra;
} else if (flags_ & gzip::flags::name) {
state_ = s_name;
} else if (flags_ & gzip::flags::comment) {
state_ = s_comment;
} else if (flags_ & gzip::flags::header_crc) {
state_ = s_hcrc;
} else {
state_ = s_done;
}
break;
case s_xlen:
xlen_ += value << (offset_ * 8);
if (offset_ == 1) {
state_ = s_extra;
offset_ = 0;
} else {
++offset_;
}
break;
case s_extra:
if (--xlen_ == 0) {
if (flags_ & gzip::flags::name) {
state_ = s_name;
} else if (flags_ & gzip::flags::comment) {
state_ = s_comment;
} else if (flags_ & gzip::flags::header_crc) {
state_ = s_hcrc;
} else {
state_ = s_done;
}
}
break;
case s_name:
if (c != 0) {
file_name_ += c;
} else if (flags_ & gzip::flags::comment) {
state_ = s_comment;
} else if (flags_ & gzip::flags::header_crc) {
state_ = s_hcrc;
} else {
state_ = s_done;
}
break;
case s_comment:
if (c != 0) {
comment_ += c;
} else if (flags_ & gzip::flags::header_crc) {
state_ = s_hcrc;
} else {
state_ = s_done;
}
break;
case s_hcrc:
if (offset_ == 1) {
state_ = s_done;
offset_ = 0;
} else {
++offset_;
}
break;
default:
BOOST_ASSERT(0);
}
}
void gzip_header::reset()
{
file_name_.clear();
comment_.clear();
os_ = flags_ = offset_ = xlen_ = 0;
mtime_ = 0;
state_ = s_id1;
}
//------------------Implementation of gzip_footer-----------------------------//
void gzip_footer::process(char c)
{
uint8_t value = static_cast<uint8_t>(c);
if (state_ == s_crc) {
crc_ += value << (offset_ * 8);
if (offset_ == 3) {
state_ = s_isize;
offset_ = 0;
} else {
++offset_;
}
} else if (state_ == s_isize) {
isize_ += value << (offset_ * 8);
if (offset_ == 3) {
state_ = s_done;
offset_ = 0;
} else {
++offset_;
}
} else {
BOOST_ASSERT(0);
}
}
void gzip_footer::reset()
{
crc_ = isize_ = offset_ = 0;
state_ = s_crc;
}
} // End namespace boost::iostreams::detail.
} } // End namespaces iostreams, boost.
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2003-2007 Jonathan Turkanis
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
// See http://www.boost.org/libs/iostreams for documentation.
// To configure Boost to work with zlib, see the
// installation instructions here:
// http://boost.org/libs/iostreams/doc/index.html?path=7
// Define BOOST_IOSTREAMS_SOURCE so that <boost/iostreams/detail/config.hpp>
// knows that we are building the library (possibly exporting code), rather
// than using it (possibly importing code).
#define BOOST_IOSTREAMS_SOURCE
#include <boost/throw_exception.hpp>
#include <boost/iostreams/detail/config/dyn_link.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include "zlib.h" // Jean-loup Gailly's and Mark Adler's "zlib.h" header.
// To configure Boost to work with zlib, see the
// installation instructions here:
// http://boost.org/libs/iostreams/doc/index.html?path=7
namespace boost { namespace iostreams {
namespace zlib {
// Compression levels
const int no_compression = Z_NO_COMPRESSION;
const int best_speed = Z_BEST_SPEED;
const int best_compression = Z_BEST_COMPRESSION;
const int default_compression = Z_DEFAULT_COMPRESSION;
// Compression methods
const int deflated = Z_DEFLATED;
// Compression strategies
const int default_strategy = Z_DEFAULT_STRATEGY;
const int filtered = Z_FILTERED;
const int huffman_only = Z_HUFFMAN_ONLY;
// Status codes
const int okay = Z_OK;
const int stream_end = Z_STREAM_END;
const int stream_error = Z_STREAM_ERROR;
const int version_error = Z_VERSION_ERROR;
const int data_error = Z_DATA_ERROR;
const int mem_error = Z_MEM_ERROR;
const int buf_error = Z_BUF_ERROR;
// Flush codes
const int finish = Z_FINISH;
const int no_flush = Z_NO_FLUSH;
const int sync_flush = Z_SYNC_FLUSH;
// Code for current OS
//const int os_code = OS_CODE;
} // End namespace zlib.
//------------------Implementation of zlib_error------------------------------//
zlib_error::zlib_error(int error)
: BOOST_IOSTREAMS_FAILURE("zlib error"), error_(error)
{ }
void zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(int error)
{
switch (error) {
case Z_OK:
case Z_STREAM_END:
//case Z_BUF_ERROR:
return;
case Z_MEM_ERROR:
boost::throw_exception(std::bad_alloc());
default:
boost::throw_exception(zlib_error(error));
;
}
}
//------------------Implementation of zlib_base-------------------------------//
namespace detail {
zlib_base::zlib_base()
: stream_(new z_stream), calculate_crc_(false), crc_(0), crc_imp_(0)
{ }
zlib_base::~zlib_base() { delete static_cast<z_stream*>(stream_); }
void zlib_base::before( const char*& src_begin, const char* src_end,
char*& dest_begin, char* dest_end )
{
z_stream* s = static_cast<z_stream*>(stream_);
s->next_in = reinterpret_cast<zlib::byte*>(const_cast<char*>(src_begin));
s->avail_in = static_cast<zlib::uint>(src_end - src_begin);
s->next_out = reinterpret_cast<zlib::byte*>(dest_begin);
s->avail_out= static_cast<zlib::uint>(dest_end - dest_begin);
}
void zlib_base::after(const char*& src_begin, char*& dest_begin, bool compress)
{
z_stream* s = static_cast<z_stream*>(stream_);
char* next_in = reinterpret_cast<char*>(s->next_in);
char* next_out = reinterpret_cast<char*>(s->next_out);
if (calculate_crc_) {
const zlib::byte* buf = compress ?
reinterpret_cast<const zlib::byte*>(src_begin) :
reinterpret_cast<const zlib::byte*>(
const_cast<const char*>(dest_begin)
);
zlib::uint length = compress ?
static_cast<zlib::uint>(next_in - src_begin) :
static_cast<zlib::uint>(next_out - dest_begin);
if (length > 0)
crc_ = crc_imp_ = crc32(crc_imp_, buf, length);
}
total_in_ = s->total_in;
total_out_ = s->total_out;
src_begin = const_cast<const char*>(next_in);
dest_begin = next_out;
}
int zlib_base::xdeflate(int flush)
{
return ::deflate(static_cast<z_stream*>(stream_), flush);
}
int zlib_base::xinflate(int flush)
{
return ::inflate(static_cast<z_stream*>(stream_), flush);
}
void zlib_base::reset(bool compress, bool realloc)
{
z_stream* s = static_cast<z_stream*>(stream_);
// Undiagnosed bug:
// deflateReset(), etc., return Z_DATA_ERROR
//zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
realloc ?
(compress ? deflateReset(s) : inflateReset(s)) :
(compress ? deflateEnd(s) : inflateEnd(s))
;
//);
crc_imp_ = 0;
}
void zlib_base::do_init
( const zlib_params& p, bool compress,
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
zlib::xalloc_func /* alloc */, zlib::xfree_func /* free*/,
#endif
void* derived )
{
calculate_crc_ = p.calculate_crc;
z_stream* s = static_cast<z_stream*>(stream_);
// Current interface for customizing memory management
// is non-conforming and has been disabled:
//#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
// s->zalloc = alloc;
// s->zfree = free;
//#else
s->zalloc = 0;
s->zfree = 0;
//#endif
s->opaque = derived;
int window_bits = p.noheader? -p.window_bits : p.window_bits;
zlib_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(
compress ?
deflateInit2( s,
p.level,
p.method,
window_bits,
p.mem_level,
p.strategy ) :
inflateInit2(s, window_bits)
);
}
} // End namespace detail.
//----------------------------------------------------------------------------//
} } // End namespaces iostreams, boost.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment