summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-09-15 14:01:35 +0200
committerljfa-ag <ljfa-ag@web.de>2015-09-15 14:01:35 +0200
commitc782034564ed422de61235917917963c5e5c3fca (patch)
tree403a69650fe94324f2f64e404b6e01c6dcd563dd /src
parentec792d93a73e49738d4ded361f23acfae3a00e94 (diff)
parent7a8a1833dc766abc1626a1e590e3f39a7d503062 (diff)
downloadProject-Tick-c782034564ed422de61235917917963c5e5c3fca.tar.gz
Project-Tick-c782034564ed422de61235917917963c5e5c3fca.zip
Merge branch 'zlibstream'
Diffstat (limited to 'src')
-rw-r--r--src/io/izlibstream.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/io/izlibstream.cpp b/src/io/izlibstream.cpp
new file mode 100644
index 0000000000..db69e0e626
--- /dev/null
+++ b/src/io/izlibstream.cpp
@@ -0,0 +1,95 @@
+/*
+ * 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/>.
+ */
+#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), stream_end(false)
+{
+ 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(zstr.msg, 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());
+ 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<Bytef*>(in.data());
+ zstr.avail_in = count;
+ }
+
+ zstr.next_out = reinterpret_cast<Bytef*>(out.data());
+ zstr.avail_out = out.size();
+
+ int ret = inflate(&zstr, Z_NO_FLUSH);
+ have = out.size() - zstr.avail_out;
+ switch(ret)
+ {
+ case Z_NEED_DICT:
+ case Z_DATA_ERROR:
+ throw zlib_error(zstr.msg, ret);
+
+ case Z_MEM_ERROR:
+ throw std::bad_alloc();
+
+ case Z_STREAM_END:
+ stream_end = true;
+ if(have == 0)
+ return traits_type::eof();
+ break;
+ }
+ } while(have == 0);
+
+ setg(out.data(), out.data(), out.data() + have);
+ return traits_type::to_int_type(*gptr());
+}
+
+}