summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/io/izlibstream.h96
-rw-r--r--include/io/ozlibstream.h12
-rw-r--r--include/io/zlib_error.h19
3 files changed, 116 insertions, 11 deletions
diff --git a/include/io/izlibstream.h b/include/io/izlibstream.h
new file mode 100644
index 0000000000..edf632eddf
--- /dev/null
+++ b/include/io/izlibstream.h
@@ -0,0 +1,96 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+#ifndef IZLIBSTREAM_H_INCLUDED
+#define IZLIBSTREAM_H_INCLUDED
+
+#include <istream>
+#include <vector>
+#include <zlib.h>
+
+namespace zlib
+{
+
+/**
+ * @brief Stream buffer used by zlib::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;
+
+ //No copying or moving
+ 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:
+ std::istream& is;
+ std::vector<char> in;
+ std::vector<char> out;
+ z_stream zstr;
+ bool stream_end;
+
+ int_type underflow() override;
+};
+
+/**
+ * @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.
+ *
+ * @sa inflate_streambuf
+ */
+class izlibstream : public std::istream
+{
+public:
+ /**
+ * @param input the istream to wrap
+ * @param bufsize the size of the internal buffers
+ */
+ 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
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 <ostream>
#include <vector>
#include <zlib.h>
@@ -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..ce375a6ccd
--- /dev/null
+++ b/include/io/zlib_error.h
@@ -0,0 +1,19 @@
+#ifndef ZLIB_ERROR_H_INCLUDED
+#define ZLIB_ERROR_H_INCLUDED
+
+#include <stdexcept>
+#include <zlib.h>
+
+///Exception thrown in case zlib encounters a problem
+class zlib_error : public std::runtime_error
+{
+public:
+ const int errcode;
+
+ explicit zlib_error(const char* msg, int errcode):
+ std::runtime_error(std::string(zError(errcode)) + ": " + msg),
+ errcode(errcode)
+ {}
+};
+
+#endif // ZLIB_ERROR_H_INCLUDED