aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/streams/bzip2/bzip2.cpp
blob: bccc5c68078cd2f8bbde3d2b68227132a052ca6c (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "bzip2.h"

#include <util/memory/addstorage.h>
#include <util/generic/scope.h>

#include <contrib/libs/libbz2/bzlib.h>

class TBZipDecompress::TImpl: public TAdditionalStorage<TImpl> {
public:
    inline TImpl(IInputStream* input)
        : Stream_(input)
    {
        Zero(BzStream_);
        Init();
    }

    inline ~TImpl() {
        Clear();
    }

    inline void Init() {
        if (BZ2_bzDecompressInit(&BzStream_, 0, 0) != BZ_OK) {
            ythrow TBZipDecompressError() << "can not init bzip engine";
        }
    }

    inline void Clear() noexcept {
        BZ2_bzDecompressEnd(&BzStream_);
    }

    inline size_t Read(void* buf, size_t size) {
        BzStream_.next_out = (char*)buf;
        BzStream_.avail_out = size;

        while (true) {
            if (BzStream_.avail_in == 0) {
                if (FillInputBuffer() == 0) {
                    return 0;
                }
            }

            switch (BZ2_bzDecompress(&BzStream_)) {
                case BZ_STREAM_END: {
                    Clear();
                    Init();
                    [[fallthrough]];
                }

                case BZ_OK: {
                    const size_t processed = size - BzStream_.avail_out;

                    if (processed) {
                        return processed;
                    }

                    break;
                }

                default:
                    ythrow TBZipDecompressError() << "bzip error";
            }
        }
    }

    inline size_t FillInputBuffer() {
        BzStream_.next_in = (char*)AdditionalData();
        BzStream_.avail_in = Stream_->Read(BzStream_.next_in, AdditionalDataLength());

        return BzStream_.avail_in;
    }

private:
    IInputStream* Stream_;
    bz_stream BzStream_;
};

TBZipDecompress::TBZipDecompress(IInputStream* input, size_t bufLen)
    : Impl_(new (bufLen) TImpl(input))
{
}

TBZipDecompress::~TBZipDecompress() {
}

size_t TBZipDecompress::DoRead(void* buf, size_t size) {
    return Impl_->Read(buf, size);
}

class TBZipCompress::TImpl: public TAdditionalStorage<TImpl> {
public:
    inline TImpl(IOutputStream* stream, size_t level)
        : Stream_(stream)
    {
        Zero(BzStream_);

        if (BZ2_bzCompressInit(&BzStream_, level, 0, 0) != BZ_OK) {
            ythrow TBZipCompressError() << "can not init bzip engine";
        }

        BzStream_.next_out = TmpBuf();
        BzStream_.avail_out = TmpBufLen();
    }

    inline ~TImpl() {
        BZ2_bzCompressEnd(&BzStream_);
    }

    inline void Write(const void* buf, size_t size) {
        BzStream_.next_in = (char*)buf;
        BzStream_.avail_in = size;

        Y_DEFER {
            BzStream_.next_in = 0;
            BzStream_.avail_in = 0;
        };

        while (BzStream_.avail_in) {
            const int ret = BZ2_bzCompress(&BzStream_, BZ_RUN);

            switch (ret) {
                case BZ_RUN_OK:
                    continue;

                case BZ_PARAM_ERROR:
                case BZ_OUTBUFF_FULL:
                    Stream_->Write(TmpBuf(), TmpBufLen() - BzStream_.avail_out);
                    BzStream_.next_out = TmpBuf();
                    BzStream_.avail_out = TmpBufLen();

                    break;

                default:
                    ythrow TBZipCompressError() << "bzip error(" << ret << ", " << BzStream_.avail_out << ")";
            }
        }
    }

    inline void Flush() {
        /*
         * TODO ?
         */
    }

    inline void Finish() {
        int ret = BZ2_bzCompress(&BzStream_, BZ_FINISH);

        while (ret != BZ_STREAM_END) {
            Stream_->Write(TmpBuf(), TmpBufLen() - BzStream_.avail_out);
            BzStream_.next_out = TmpBuf();
            BzStream_.avail_out = TmpBufLen();

            ret = BZ2_bzCompress(&BzStream_, BZ_FINISH);
        }

        Stream_->Write(TmpBuf(), TmpBufLen() - BzStream_.avail_out);
    }

private:
    inline char* TmpBuf() noexcept {
        return (char*)AdditionalData();
    }

    inline size_t TmpBufLen() const noexcept {
        return AdditionalDataLength();
    }

private:
    IOutputStream* Stream_;
    bz_stream BzStream_;
};

TBZipCompress::TBZipCompress(IOutputStream* out, size_t compressionLevel, size_t bufLen)
    : Impl_(new (bufLen) TImpl(out, compressionLevel))
{
}

TBZipCompress::~TBZipCompress() {
    try {
        Finish();
    } catch (...) {
    }
}

void TBZipCompress::DoWrite(const void* buf, size_t size) {
    if (!Impl_) {
        ythrow TBZipCompressError() << "can not write to finished bzip stream";
    }

    Impl_->Write(buf, size);
}

void TBZipCompress::DoFlush() {
    if (Impl_) {
        Impl_->Flush();
    }
}

void TBZipCompress::DoFinish() {
    THolder<TImpl> impl(Impl_.Release());

    if (impl) {
        impl->Finish();
    }
}