diff --git a/Makefile.am b/Makefile.am index 1ec2cade..e193b552 100644 --- a/Makefile.am +++ b/Makefile.am @@ -139,7 +139,9 @@ UTIL_FILES = \ vector.cpp \ vector.hpp \ version.cpp \ - version.hpp + version.hpp \ + zlib.cpp \ + zlib.hpp if PLATFORM_LINUX diff --git a/configure.ac b/configure.ac index ce86e6f6..9105f25e 100644 --- a/configure.ac +++ b/configure.ac @@ -209,6 +209,7 @@ if test "x$ax_cv_boost_filesystem" != "xyes"; then fi ## Optional packages +PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.0]) ## Output diff --git a/zlib.cpp b/zlib.cpp new file mode 100644 index 00000000..7299807f --- /dev/null +++ b/zlib.cpp @@ -0,0 +1,52 @@ +#include "zlib.hpp" + +#include "debug.hpp" + +const char * +util::zlib::version (void) { + return zlibVersion (); +} + + +size_t +util::zlib::uncompress (uint8_t *dst, size_t dst_len, + const uint8_t *src, size_t src_len) { + size_t actual_len = dst_len; + int err = ::uncompress (dst, &actual_len, src, src_len); + error::try_code (err); + return actual_len; +} + + +void +util::zlib::error::try_code (int code) { + if (Z_OK == code) + return; + + throw_code(code); +} + + +util::zlib::error::error (const std::string &_what): + runtime_error (_what) +{ ; } + + +void +util::zlib::error::throw_code (int code) { + CHECK_NEQ (code, Z_OK); + + switch (code) { + #define do_code(c) case c: throw code_error (); + + do_code(Z_STREAM_ERROR); + do_code(Z_DATA_ERROR); + do_code(Z_MEM_ERROR); + do_code(Z_BUF_ERROR); + do_code(Z_VERSION_ERROR); + + default: + panic ("Unknown code"); + } +} + diff --git a/zlib.hpp b/zlib.hpp new file mode 100644 index 00000000..d96cdc25 --- /dev/null +++ b/zlib.hpp @@ -0,0 +1,72 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2013 Danny Robson + */ + +#ifndef __UTIL_ZLIB_HPP +#define __UTIL_ZLIB_HPP + +#include +#include + +#include + + +namespace util { + namespace zlib { + extern const char* version (void); + + extern void compress (uint8_t *dst, size_t dst_len, + const uint8_t *src, size_t src_len, + int level); + + extern size_t compress_bound (size_t len); + + extern size_t uncompress (uint8_t *dst, size_t dst_len, + const uint8_t *src, size_t src_len); + + + class stream { + public: + + public: + stream(); + ~stream(); + + int deflate (bool flush = false); + int inflate (bool flush = false); + }; + + + class error : public std::runtime_error { + public: + static void try_code (int code); + static void throw_code (int code); + + error(const std::string&); + }; + + + template + class code_error : public error { + public: + code_error(void); + }; + } +} + +#endif