From ec4d2df1f946fdf109d13c6d49f13869f11ae95e Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Mon, 14 Sep 2015 19:50:30 +0200 Subject: Put zlib_error into its own file --- include/io/ozlibstream.h | 12 +----------- include/io/zlib_error.h | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 include/io/zlib_error.h (limited to 'include') diff --git a/include/io/ozlibstream.h b/include/io/ozlibstream.h index c427281763..6ebd3c0dd0 100644 --- a/include/io/ozlibstream.h +++ b/include/io/ozlibstream.h @@ -20,6 +20,7 @@ #ifndef OZLIBSTREAM_H_INCLUDED #define OZLIBSTREAM_H_INCLUDED +#include "io/zlib_error.h" #include #include #include @@ -27,17 +28,6 @@ namespace zlib { -///Exception thrown in case zlib encounters a problem -class zlib_error : public std::runtime_error -{ -public: - const int errcode; - - explicit zlib_error(const char* what_arg, int errcode = Z_ERRNO): - std::runtime_error(what_arg), errcode(errcode) - {} -}; - /** * @brief Stream buffer used by zlib::ozlibstream * @see ozlibstream diff --git a/include/io/zlib_error.h b/include/io/zlib_error.h new file mode 100644 index 0000000000..12eaf20a3f --- /dev/null +++ b/include/io/zlib_error.h @@ -0,0 +1,17 @@ +#ifndef ZLIB_ERROR_H_INCLUDED +#define ZLIB_ERROR_H_INCLUDED + +#include + +///Exception thrown in case zlib encounters a problem +class zlib_error : public std::runtime_error +{ +public: + const int errcode; + + explicit zlib_error(const char* what_arg, int errcode = -1): + std::runtime_error(what_arg), errcode(errcode) + {} +}; + +#endif // ZLIB_ERROR_H_INCLUDED -- cgit 0.0.5-2-1-g0f52 From 7668b2e16894ef6cfe13c30a33823986d02fa5b1 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Mon, 14 Sep 2015 20:23:29 +0200 Subject: Add preliminary header for izlibstream --- include/io/izlibstream.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 include/io/izlibstream.h (limited to 'include') diff --git a/include/io/izlibstream.h b/include/io/izlibstream.h new file mode 100644 index 0000000000..99b56ac5cf --- /dev/null +++ b/include/io/izlibstream.h @@ -0,0 +1,80 @@ +/* + * libnbt++ - A library for the Minecraft Named Binary Tag format. + * Copyright (C) 2013, 2015 ljfa-ag + * + * This file is part of libnbt++. + * + * libnbt++ is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libnbt++ 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libnbt++. If not, see . + */ +#ifndef IZLIBSTREAM_H_INCLUDED +#define IZLIBSTREAM_H_INCLUDED + +#include +#include +#include + +namespace zlib +{ + +/** + * @brief Stream buffer used by zlib::izlibstream + * @see izlibstream + */ +class inflate_streambuf : public std::streambuf +{ +public: + explicit inflate_streambuf(std::istream& input, size_t bufsize = 32768, int window_bits = 32); + ~inflate_streambuf() noexcept; + + //No copying or moving + inflate_streambuf(const inflate_streambuf&) = delete; + inflate_streambuf& operator=(const inflate_streambuf&) = delete; + + std::istream& get_istr() const { return is; } + +private: + std::istream& is; + std::vector in; + std::vector out; + z_stream zstr; +}; + +/** + * @brief An istream adapter that decompresses data using zlib + * + * This istream wraps another istream. The izlibstream will read compressed + * data from the wrapped istream and inflate (decompress) it with zlib. + */ +class izlibstream : public std::istream +{ +public: + /** + * @param input the istream to wrap + * @param bufsize the size of the internal buffer + */ + explicit izlibstream(std::istream& input, size_t bufsize = 32768): + buf(input, bufsize) + { + init(&buf); + } + ///@return the wrapped istream + std::istream& get_istr() const { return buf.get_istr(); } + +private: + inflate_streambuf buf; +}; + +} + +#endif // IZLIBSTREAM_H_INCLUDED -- cgit 0.0.5-2-1-g0f52 From f3cd19f1fa88b2bc33bfb96fb6c877a4d62c5c97 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Mon, 14 Sep 2015 23:03:31 +0200 Subject: Add implementation of inflate_streambuf (still broken though) --- CMakeLists.txt | 1 + include/io/izlibstream.h | 2 ++ src/io/izlibstream.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/io/izlibstream.cpp (limited to 'include') diff --git a/CMakeLists.txt b/CMakeLists.txt index cd073e0e56..c16860a8ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ add_library(nbt++ STATIC src/value.cpp src/value_initializer.cpp + src/io/izlibstream.cpp src/io/stream_reader.cpp src/io/stream_writer.cpp diff --git a/include/io/izlibstream.h b/include/io/izlibstream.h index 99b56ac5cf..16c82d9f9d 100644 --- a/include/io/izlibstream.h +++ b/include/io/izlibstream.h @@ -48,6 +48,8 @@ private: std::vector in; std::vector out; z_stream zstr; + + int_type underflow() override; }; /** diff --git a/src/io/izlibstream.cpp b/src/io/izlibstream.cpp new file mode 100644 index 0000000000..632f0cf7c6 --- /dev/null +++ b/src/io/izlibstream.cpp @@ -0,0 +1,86 @@ +/* + * libnbt++ - A library for the Minecraft Named Binary Tag format. + * Copyright (C) 2013, 2015 ljfa-ag + * + * This file is part of libnbt++. + * + * libnbt++ is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libnbt++ 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libnbt++. If not, see . + */ +#include "io/izlibstream.h" +#include "io/zlib_error.h" + +namespace zlib +{ + +inflate_streambuf::inflate_streambuf(std::istream& input, size_t bufsize, int window_bits): + is(input), in(bufsize), out(bufsize) +{ + zstr.zalloc = Z_NULL; + zstr.zfree = Z_NULL; + zstr.opaque = Z_NULL; + zstr.next_in = Z_NULL; + zstr.avail_in = 0; + int ret = inflateInit2(&zstr, window_bits); + if(ret != Z_OK) + throw zlib_error("inflateInit failed", ret); + + char* end = out.data() + out.size(); + setg(end, end, end); +} + +inflate_streambuf::~inflate_streambuf() +{ + inflateEnd(&zstr); +} + +inflate_streambuf::int_type inflate_streambuf::underflow() +{ + if(gptr() < egptr()) + return traits_type::to_int_type(*gptr()); + + size_t have; + do + { + //Read if input buffer is empty + if(zstr.avail_in <= 0) + { + is.read(in.data(), in.size()); + size_t count = is.gcount(); + zstr.next_in = reinterpret_cast(in.data()); + zstr.avail_in = count; + } + + zstr.next_out = reinterpret_cast(out.data()); + zstr.avail_out = out.size(); + + int ret = inflate(&zstr, Z_NO_FLUSH); + switch(ret) + { + case Z_NEED_DICT: + case Z_DATA_ERROR: + throw zlib_error("Error decompressing data", ret); + case Z_MEM_ERROR: + throw std::bad_alloc(); + case Z_STREAM_END: + return traits_type::eof(); + } + + have = out.size() - zstr.avail_out; + } while(have == 0); + + setg(out.data(), out.data(), out.data() + have); + return traits_type::to_int_type(*gptr()); +} + +} -- cgit 0.0.5-2-1-g0f52 From 0c6f7fcccc1a9e20bbd68b5bc09bc8853d15134c Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 15 Sep 2015 10:37:52 +0200 Subject: Use different windowBits value to avoid zlib bug --- include/io/izlibstream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/io/izlibstream.h b/include/io/izlibstream.h index 16c82d9f9d..b88839ea99 100644 --- a/include/io/izlibstream.h +++ b/include/io/izlibstream.h @@ -34,7 +34,7 @@ namespace zlib class inflate_streambuf : public std::streambuf { public: - explicit inflate_streambuf(std::istream& input, size_t bufsize = 32768, int window_bits = 32); + explicit inflate_streambuf(std::istream& input, size_t bufsize = 32768, int window_bits = 32 + 15); ~inflate_streambuf() noexcept; //No copying or moving -- cgit 0.0.5-2-1-g0f52 From 4f96ef6b0d4aede214d208090e86cb7b8091f49c Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 15 Sep 2015 10:58:49 +0200 Subject: Change zlib_error to take message and code --- include/io/zlib_error.h | 6 ++++-- src/io/izlibstream.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/io/zlib_error.h b/include/io/zlib_error.h index 12eaf20a3f..ce375a6ccd 100644 --- a/include/io/zlib_error.h +++ b/include/io/zlib_error.h @@ -2,6 +2,7 @@ #define ZLIB_ERROR_H_INCLUDED #include +#include ///Exception thrown in case zlib encounters a problem class zlib_error : public std::runtime_error @@ -9,8 +10,9 @@ class zlib_error : public std::runtime_error public: const int errcode; - explicit zlib_error(const char* what_arg, int errcode = -1): - std::runtime_error(what_arg), errcode(errcode) + explicit zlib_error(const char* msg, int errcode): + std::runtime_error(std::string(zError(errcode)) + ": " + msg), + errcode(errcode) {} }; diff --git a/src/io/izlibstream.cpp b/src/io/izlibstream.cpp index 9079489507..f9d9463d95 100644 --- a/src/io/izlibstream.cpp +++ b/src/io/izlibstream.cpp @@ -33,7 +33,7 @@ inflate_streambuf::inflate_streambuf(std::istream& input, size_t bufsize, int wi zstr.avail_in = 0; int ret = inflateInit2(&zstr, window_bits); if(ret != Z_OK) - throw zlib_error("inflateInit failed", ret); + throw zlib_error(zstr.msg, ret); char* end = out.data() + out.size(); setg(end, end, end); @@ -70,15 +70,16 @@ inflate_streambuf::int_type inflate_streambuf::underflow() { case Z_NEED_DICT: case Z_DATA_ERROR: - throw zlib_error("Error decompressing data", ret); + throw zlib_error(zstr.msg, ret); + case Z_MEM_ERROR: throw std::bad_alloc(); + case Z_STREAM_END: if(have == 0) return traits_type::eof(); break; } - } while(have == 0); setg(out.data(), out.data(), out.data() + have); -- cgit 0.0.5-2-1-g0f52 From a692b3be10084aec02a5b6483c878e1dc20081f2 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 15 Sep 2015 11:34:44 +0200 Subject: Check for errors in input stream --- include/io/izlibstream.h | 1 + src/io/izlibstream.cpp | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/io/izlibstream.h b/include/io/izlibstream.h index b88839ea99..2b67003232 100644 --- a/include/io/izlibstream.h +++ b/include/io/izlibstream.h @@ -48,6 +48,7 @@ private: std::vector in; std::vector out; z_stream zstr; + bool stream_end; int_type underflow() override; }; diff --git a/src/io/izlibstream.cpp b/src/io/izlibstream.cpp index f9d9463d95..db69e0e626 100644 --- a/src/io/izlibstream.cpp +++ b/src/io/izlibstream.cpp @@ -24,7 +24,7 @@ namespace zlib { inflate_streambuf::inflate_streambuf(std::istream& input, size_t bufsize, int window_bits): - is(input), in(bufsize), out(bufsize) + is(input), in(bufsize), out(bufsize), stream_end(false) { zstr.zalloc = Z_NULL; zstr.zfree = Z_NULL; @@ -56,7 +56,12 @@ inflate_streambuf::int_type inflate_streambuf::underflow() if(zstr.avail_in <= 0) { is.read(in.data(), in.size()); + if(is.bad()) + throw std::ios_base::failure("Input stream is bad"); size_t count = is.gcount(); + if(count == 0 && !stream_end) + throw zlib_error("Unexpected end of stream", Z_DATA_ERROR); + zstr.next_in = reinterpret_cast(in.data()); zstr.avail_in = count; } @@ -76,6 +81,7 @@ inflate_streambuf::int_type inflate_streambuf::underflow() throw std::bad_alloc(); case Z_STREAM_END: + stream_end = true; if(have == 0) return traits_type::eof(); break; -- cgit 0.0.5-2-1-g0f52 From 82efd65efe324e0a78425b57bff255653db2c92b Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 15 Sep 2015 11:43:22 +0200 Subject: Add more doxygen to inflate_streambuf --- include/io/izlibstream.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/io/izlibstream.h b/include/io/izlibstream.h index 2b67003232..edf632eddf 100644 --- a/include/io/izlibstream.h +++ b/include/io/izlibstream.h @@ -29,11 +29,21 @@ namespace zlib /** * @brief Stream buffer used by zlib::izlibstream - * @see izlibstream + * @sa izlibstream */ class inflate_streambuf : public std::streambuf { public: + /** + * @param input the istream to wrap + * @param bufsize the size of the internal buffers + * @param window_bits the base two logarithm of the maximum window size that + * zlib will use. This parameter also determines which type of input to expect. + * The default argument will autodetect between zlib and gzip data. + * Refer to the zlib documentation of inflateInit2 for more details. + * + * @throw zlib_error if zlib encounters a problem during initialization + */ explicit inflate_streambuf(std::istream& input, size_t bufsize = 32768, int window_bits = 32 + 15); ~inflate_streambuf() noexcept; @@ -41,6 +51,7 @@ public: inflate_streambuf(const inflate_streambuf&) = delete; inflate_streambuf& operator=(const inflate_streambuf&) = delete; + ///@return the wrapped istream std::istream& get_istr() const { return is; } private: @@ -58,13 +69,15 @@ private: * * This istream wraps another istream. The izlibstream will read compressed * data from the wrapped istream and inflate (decompress) it with zlib. + * + * @sa inflate_streambuf */ class izlibstream : public std::istream { public: /** * @param input the istream to wrap - * @param bufsize the size of the internal buffer + * @param bufsize the size of the internal buffers */ explicit izlibstream(std::istream& input, size_t bufsize = 32768): buf(input, bufsize) -- cgit 0.0.5-2-1-g0f52