summaryrefslogtreecommitdiff
path: root/src/io/izlibstream.cpp
blob: b361971081fbc315a3da28a520fe9d6ba8631d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-FileCopyrightText: 2013, 2015 ljfa-ag <ljfa-ag@web.de>
//
// SPDX-License-Identifier: LGPL-3.0-or-later

/*
 * 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_streambuf.h"

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);

    char* end = out.data() + out.size();
    setg(end, end, end);
}

inflate_streambuf::~inflate_streambuf() noexcept
{
    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:
            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());
}

}