aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/Bzip2ReadBuffer.cpp
blob: a08367dedc7afe338cf8fdbcab391a9dc9f27739 (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "clickhouse_config.h"

#if USE_BZIP2
#    include <IO/Bzip2ReadBuffer.h>
#    error #include <bzlib.h>
#    include <IO/WithFileName.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int BZIP2_STREAM_DECODER_FAILED;
    extern const int UNEXPECTED_END_OF_FILE;
}


class Bzip2ReadBuffer::Bzip2StateWrapper
{
public:
    Bzip2StateWrapper()
    {
        memset(&stream, 0, sizeof(stream));

        int ret = BZ2_bzDecompressInit(&stream, 0, 0);

        if (ret != BZ_OK)
            throw Exception(
                ErrorCodes::BZIP2_STREAM_DECODER_FAILED,
                "bzip2 stream encoder init failed: error code: {}",
                ret);
    }

    ~Bzip2StateWrapper()
    {
        BZ2_bzDecompressEnd(&stream);
    }

    void reinitialize()
    {
        auto avail_out = stream.avail_out;
        auto * next_out = stream.next_out;

        int ret = BZ2_bzDecompressEnd(&stream);

        if (ret != BZ_OK)
            throw Exception(
                ErrorCodes::BZIP2_STREAM_DECODER_FAILED,
                "bzip2 stream encoder reinit decompress end failed: error code: {}",
                ret);

        memset(&stream, 0, sizeof(bz->stream));

        ret = BZ2_bzDecompressInit(&stream, 0, 0);

        if (ret != BZ_OK)
            throw Exception(
                ErrorCodes::BZIP2_STREAM_DECODER_FAILED,
                "bzip2 stream encoder reinit failed: error code: {}",
                ret);

        stream.avail_out = avail_out;
        stream.next_out = next_out;
    }

    bz_stream stream;
};

Bzip2ReadBuffer::Bzip2ReadBuffer(std::unique_ptr<ReadBuffer> in_, size_t buf_size, char *existing_memory, size_t alignment)
    : CompressedReadBufferWrapper(std::move(in_), buf_size, existing_memory, alignment)
    , bz(std::make_unique<Bzip2StateWrapper>())
    , eof_flag(false)
{
}

Bzip2ReadBuffer::~Bzip2ReadBuffer() = default;

bool Bzip2ReadBuffer::nextImpl()
{
    if (eof_flag)
        return false;

    int ret;
    do
    {
        if (!bz->stream.avail_in)
        {
            in->nextIfAtEnd();
            bz->stream.avail_in = static_cast<unsigned>(in->buffer().end() - in->position());
            bz->stream.next_in = in->position();
        }

        bz->stream.avail_out = static_cast<unsigned>(internal_buffer.size());
        bz->stream.next_out = internal_buffer.begin();

        ret = BZ2_bzDecompress(&bz->stream);

        in->position() = in->buffer().end() - bz->stream.avail_in;

        if (ret == BZ_STREAM_END && !in->eof())
        {
            bz->reinitialize();
            bz->stream.avail_in = static_cast<unsigned>(in->buffer().end() - in->position());
            bz->stream.next_in = in->position();

            ret = BZ_OK;
        }
    }
    while (bz->stream.avail_out == internal_buffer.size() && ret == BZ_OK && !in->eof());

    working_buffer.resize(internal_buffer.size() - bz->stream.avail_out);

    if (ret == BZ_STREAM_END && in->eof())
    {
        eof_flag = true;
        return !working_buffer.empty();
    }

    if (ret != BZ_OK)
        throw Exception(
            ErrorCodes::BZIP2_STREAM_DECODER_FAILED,
            "bzip2 stream decoder failed: error code: {}{}",
            ret,
            getExceptionEntryWithFileName(*in));

    if (in->eof())
    {
        eof_flag = true;
        throw Exception(
            ErrorCodes::UNEXPECTED_END_OF_FILE,
            "Unexpected end of bzip2 archive{}",
            getExceptionEntryWithFileName(*in));
    }

    return true;
}
}

#endif