summaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/io')
-rw-r--r--src/io/izlibstream.cpp123
-rw-r--r--src/io/ozlibstream.cpp155
-rw-r--r--src/io/stream_reader.cpp155
-rw-r--r--src/io/stream_writer.cpp53
4 files changed, 242 insertions, 244 deletions
diff --git a/src/io/izlibstream.cpp b/src/io/izlibstream.cpp
index b361971081..9d57545360 100644
--- a/src/io/izlibstream.cpp
+++ b/src/io/izlibstream.cpp
@@ -27,76 +27,75 @@
namespace zlib
{
-inflate_streambuf::inflate_streambuf(std::istream& input, size_t bufsize, int window_bits):
- zlib_streambuf(bufsize), is(input), stream_end(false)
-{
- 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);
+ inflate_streambuf::inflate_streambuf(std::istream& input, size_t bufsize,
+ int window_bits)
+ : zlib_streambuf(bufsize), is(input), stream_end(false)
+ {
+ 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);
-}
+ char* end = out.data() + out.size();
+ setg(end, end, end);
+ }
-inflate_streambuf::~inflate_streambuf() noexcept
-{
- inflateEnd(&zstr);
-}
+ inflate_streambuf::~inflate_streambuf() noexcept
+ {
+ inflateEnd(&zstr);
+ }
-inflate_streambuf::int_type inflate_streambuf::underflow()
-{
- if(gptr() < egptr())
- return traits_type::to_int_type(*gptr());
+ 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);
+ 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_in = reinterpret_cast<Bytef*>(in.data());
+ zstr.avail_in = count;
+ }
- zstr.next_out = reinterpret_cast<Bytef*>(out.data());
- zstr.avail_out = out.size();
+ 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);
+ 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_MEM_ERROR:
+ throw std::bad_alloc();
- case Z_STREAM_END:
- if(!stream_end)
- {
- stream_end = true;
- //In case we consumed too much, we have to rewind the input stream
- is.clear();
- is.seekg(-static_cast<std::streamoff>(zstr.avail_in), std::ios_base::cur);
- }
- if(have == 0)
- return traits_type::eof();
- break;
- }
- } while(have == 0);
+ case Z_STREAM_END:
+ if (!stream_end) {
+ stream_end = true;
+ // In case we consumed too much, we have to rewind the
+ // input stream
+ is.clear();
+ is.seekg(-static_cast<std::streamoff>(zstr.avail_in),
+ std::ios_base::cur);
+ }
+ 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());
-}
+ setg(out.data(), out.data(), out.data() + have);
+ return traits_type::to_int_type(*gptr());
+ }
-}
+} // namespace zlib
diff --git a/src/io/ozlibstream.cpp b/src/io/ozlibstream.cpp
index eefe38a91c..c8a3e862a1 100644
--- a/src/io/ozlibstream.cpp
+++ b/src/io/ozlibstream.cpp
@@ -27,93 +27,88 @@
namespace zlib
{
-deflate_streambuf::deflate_streambuf(std::ostream& output, size_t bufsize, int level, int window_bits, int mem_level, int strategy):
- zlib_streambuf(bufsize), os(output)
-{
- int ret = deflateInit2(&zstr, level, Z_DEFLATED, window_bits, mem_level, strategy);
- if(ret != Z_OK)
- throw zlib_error(zstr.msg, ret);
+ deflate_streambuf::deflate_streambuf(std::ostream& output, size_t bufsize,
+ int level, int window_bits,
+ int mem_level, int strategy)
+ : zlib_streambuf(bufsize), os(output)
+ {
+ int ret = deflateInit2(&zstr, level, Z_DEFLATED, window_bits, mem_level,
+ strategy);
+ if (ret != Z_OK)
+ throw zlib_error(zstr.msg, ret);
- setp(in.data(), in.data() + in.size());
-}
+ setp(in.data(), in.data() + in.size());
+ }
-deflate_streambuf::~deflate_streambuf() noexcept
-{
- try
- {
- close();
- }
- catch(...)
- {
- //ignore as we can't do anything about it
- }
- deflateEnd(&zstr);
-}
+ deflate_streambuf::~deflate_streambuf() noexcept
+ {
+ try {
+ close();
+ } catch (...) {
+ // ignore as we can't do anything about it
+ }
+ deflateEnd(&zstr);
+ }
-void deflate_streambuf::close()
-{
- deflate_chunk(Z_FINISH);
-}
+ void deflate_streambuf::close()
+ {
+ deflate_chunk(Z_FINISH);
+ }
-void deflate_streambuf::deflate_chunk(int flush)
-{
- zstr.next_in = reinterpret_cast<Bytef*>(pbase());
- zstr.avail_in = pptr() - pbase();
- do
- {
- zstr.next_out = reinterpret_cast<Bytef*>(out.data());
- zstr.avail_out = out.size();
- int ret = deflate(&zstr, flush);
- if(ret != Z_OK && ret != Z_STREAM_END)
- {
- os.setstate(std::ios_base::failbit);
- throw zlib_error(zstr.msg, ret);
- }
- int have = out.size() - zstr.avail_out;
- if(!os.write(out.data(), have))
- throw std::ios_base::failure("Could not write to the output stream");
- } while(zstr.avail_out == 0);
- setp(in.data(), in.data() + in.size());
-}
+ void deflate_streambuf::deflate_chunk(int flush)
+ {
+ zstr.next_in = reinterpret_cast<Bytef*>(pbase());
+ zstr.avail_in = pptr() - pbase();
+ do {
+ zstr.next_out = reinterpret_cast<Bytef*>(out.data());
+ zstr.avail_out = out.size();
+ int ret = deflate(&zstr, flush);
+ if (ret != Z_OK && ret != Z_STREAM_END) {
+ os.setstate(std::ios_base::failbit);
+ throw zlib_error(zstr.msg, ret);
+ }
+ int have = out.size() - zstr.avail_out;
+ if (!os.write(out.data(), have))
+ throw std::ios_base::failure(
+ "Could not write to the output stream");
+ } while (zstr.avail_out == 0);
+ setp(in.data(), in.data() + in.size());
+ }
-deflate_streambuf::int_type deflate_streambuf::overflow(int_type ch)
-{
- deflate_chunk();
- if(ch != traits_type::eof())
- {
- *pptr() = ch;
- pbump(1);
- }
- return ch;
-}
+ deflate_streambuf::int_type deflate_streambuf::overflow(int_type ch)
+ {
+ deflate_chunk();
+ if (ch != traits_type::eof()) {
+ *pptr() = ch;
+ pbump(1);
+ }
+ return ch;
+ }
-int deflate_streambuf::sync()
-{
- deflate_chunk();
- return 0;
-}
+ int deflate_streambuf::sync()
+ {
+ deflate_chunk();
+ return 0;
+ }
-void ozlibstream::close()
-{
- // Capture the exception mask so we can restore it if close() fails.
- std::ios_base::iostate old_ex = exceptions();
- try
- {
- buf.close();
- return;
- }
- catch(...)
- {
- // fall through to mark the stream as bad
- }
+ void ozlibstream::close()
+ {
+ // Capture the exception mask so we can restore it if close() fails.
+ std::ios_base::iostate old_ex = exceptions();
+ try {
+ buf.close();
+ return;
+ } catch (...) {
+ // fall through to mark the stream as bad
+ }
- // Setting the stream state while exceptions are enabled may cause
- // `setstate` to throw an `ios_base::failure` which would replace
- // the original exception. Temporarily disable exceptions on this
- // stream, set the `badbit`, then restore the exception mask.
- exceptions(std::ios_base::goodbit);
- setstate(std::ios_base::badbit);
- exceptions(old_ex);
-}
+ // Setting the stream state while exceptions are enabled may cause
+ // `setstate` to throw an `ios_base::failure` which would replace
+ // the original exception. Temporarily disable exceptions on this
+ // stream, set the `badbit`, then restore the exception mask.
+ exceptions(std::ios_base::goodbit);
+ setstate(std::ios_base::badbit);
+ exceptions(old_ex);
+ }
} // namespace zlib
diff --git a/src/io/stream_reader.cpp b/src/io/stream_reader.cpp
index 06375ca0d5..43032b1120 100644
--- a/src/io/stream_reader.cpp
+++ b/src/io/stream_reader.cpp
@@ -28,92 +28,95 @@
namespace nbt
{
-namespace io
-{
+ namespace io
+ {
-static constexpr int MAX_DEPTH = 1024;
+ static constexpr int MAX_DEPTH = 1024;
-std::pair<std::string, std::unique_ptr<tag_compound>> read_compound(std::istream& is, endian::endian e)
-{
- return stream_reader(is, e).read_compound();
-}
+ std::pair<std::string, std::unique_ptr<tag_compound>>
+ read_compound(std::istream& is, endian::endian e)
+ {
+ return stream_reader(is, e).read_compound();
+ }
-std::pair<std::string, std::unique_ptr<tag>> read_tag(std::istream& is, endian::endian e)
-{
- return stream_reader(is, e).read_tag();
-}
+ std::pair<std::string, std::unique_ptr<tag>> read_tag(std::istream& is,
+ endian::endian e)
+ {
+ return stream_reader(is, e).read_tag();
+ }
-stream_reader::stream_reader(std::istream& is, endian::endian e) noexcept:
- is(is), endian(e)
-{}
+ stream_reader::stream_reader(std::istream& is,
+ endian::endian e) noexcept
+ : is(is), endian(e)
+ {
+ }
-std::istream& stream_reader::get_istr() const
-{
- return is;
-}
+ std::istream& stream_reader::get_istr() const
+ {
+ return is;
+ }
-endian::endian stream_reader::get_endian() const
-{
- return endian;
-}
+ endian::endian stream_reader::get_endian() const
+ {
+ return endian;
+ }
-std::pair<std::string, std::unique_ptr<tag_compound>> stream_reader::read_compound()
-{
- if(read_type() != tag_type::Compound)
- {
- is.setstate(std::ios::failbit);
- throw input_error("Tag is not a compound");
- }
- std::string key = read_string();
- auto comp = make_unique<tag_compound>();
- comp->read_payload(*this);
- return {std::move(key), std::move(comp)};
-}
+ std::pair<std::string, std::unique_ptr<tag_compound>>
+ stream_reader::read_compound()
+ {
+ if (read_type() != tag_type::Compound) {
+ is.setstate(std::ios::failbit);
+ throw input_error("Tag is not a compound");
+ }
+ std::string key = read_string();
+ auto comp = make_unique<tag_compound>();
+ comp->read_payload(*this);
+ return {std::move(key), std::move(comp)};
+ }
-std::pair<std::string, std::unique_ptr<tag>> stream_reader::read_tag()
-{
- tag_type type = read_type();
- std::string key = read_string();
- std::unique_ptr<tag> t = read_payload(type);
- return {std::move(key), std::move(t)};
-}
+ std::pair<std::string, std::unique_ptr<tag>> stream_reader::read_tag()
+ {
+ tag_type type = read_type();
+ std::string key = read_string();
+ std::unique_ptr<tag> t = read_payload(type);
+ return {std::move(key), std::move(t)};
+ }
-std::unique_ptr<tag> stream_reader::read_payload(tag_type type)
-{
- if (++depth > MAX_DEPTH)
- throw input_error("Too deeply nested");
- std::unique_ptr<tag> t = tag::create(type);
- t->read_payload(*this);
- --depth;
- return t;
-}
+ std::unique_ptr<tag> stream_reader::read_payload(tag_type type)
+ {
+ if (++depth > MAX_DEPTH)
+ throw input_error("Too deeply nested");
+ std::unique_ptr<tag> t = tag::create(type);
+ t->read_payload(*this);
+ --depth;
+ return t;
+ }
-tag_type stream_reader::read_type(bool allow_end)
-{
- int type = is.get();
- if(!is)
- throw input_error("Error reading tag type");
- if(!is_valid_type(type, allow_end))
- {
- is.setstate(std::ios::failbit);
- throw input_error("Invalid tag type: " + std::to_string(type));
- }
- return static_cast<tag_type>(type);
-}
+ tag_type stream_reader::read_type(bool allow_end)
+ {
+ int type = is.get();
+ if (!is)
+ throw input_error("Error reading tag type");
+ if (!is_valid_type(type, allow_end)) {
+ is.setstate(std::ios::failbit);
+ throw input_error("Invalid tag type: " + std::to_string(type));
+ }
+ return static_cast<tag_type>(type);
+ }
-std::string stream_reader::read_string()
-{
- uint16_t len;
- read_num(len);
- if(!is)
- throw input_error("Error reading string");
+ std::string stream_reader::read_string()
+ {
+ uint16_t len;
+ read_num(len);
+ if (!is)
+ throw input_error("Error reading string");
- std::string ret(len, '\0');
- is.read(&ret[0], len); //C++11 allows us to do this
- if(!is)
- throw input_error("Error reading string");
- return ret;
-}
+ std::string ret(len, '\0');
+ is.read(&ret[0], len); // C++11 allows us to do this
+ if (!is)
+ throw input_error("Error reading string");
+ return ret;
+ }
-}
-}
+ } // namespace io
+} // namespace nbt
diff --git a/src/io/stream_writer.cpp b/src/io/stream_writer.cpp
index 0294a6d4e9..5275c5a1b4 100644
--- a/src/io/stream_writer.cpp
+++ b/src/io/stream_writer.cpp
@@ -26,33 +26,34 @@
namespace nbt
{
-namespace io
-{
+ namespace io
+ {
-void write_tag(const std::string& key, const tag& t, std::ostream& os, endian::endian e)
-{
- stream_writer(os, e).write_tag(key, t);
-}
+ void write_tag(const std::string& key, const tag& t, std::ostream& os,
+ endian::endian e)
+ {
+ stream_writer(os, e).write_tag(key, t);
+ }
-void stream_writer::write_tag(const std::string& key, const tag& t)
-{
- write_type(t.get_type());
- write_string(key);
- write_payload(t);
-}
+ void stream_writer::write_tag(const std::string& key, const tag& t)
+ {
+ write_type(t.get_type());
+ write_string(key);
+ write_payload(t);
+ }
-void stream_writer::write_string(const std::string& str)
-{
- if(str.size() > max_string_len)
- {
- os.setstate(std::ios::failbit);
- std::ostringstream sstr;
- sstr << "String is too long for NBT (" << str.size() << " > " << max_string_len << ")";
- throw std::length_error(sstr.str());
- }
- write_num(static_cast<uint16_t>(str.size()));
- os.write(str.data(), str.size());
-}
+ void stream_writer::write_string(const std::string& str)
+ {
+ if (str.size() > max_string_len) {
+ os.setstate(std::ios::failbit);
+ std::ostringstream sstr;
+ sstr << "String is too long for NBT (" << str.size() << " > "
+ << max_string_len << ")";
+ throw std::length_error(sstr.str());
+ }
+ write_num(static_cast<uint16_t>(str.size()));
+ os.write(str.data(), str.size());
+ }
-}
-}
+ } // namespace io
+} // namespace nbt