aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/blockcodecs/codecs/bzip/bzip.cpp
blob: 3a5cfdd0e9a2dd962a4c9b5ffb8a5f02c5e51fd0 (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
#include <library/cpp/blockcodecs/core/codecs.h>
#include <library/cpp/blockcodecs/core/common.h>
#include <library/cpp/blockcodecs/core/register.h>

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

using namespace NBlockCodecs;

namespace {
    struct TBZipCodec: public TAddLengthCodec<TBZipCodec> {
        inline TBZipCodec(int level)
            : Level(level)
            , MyName("bzip2-" + ToString(Level))
        {
        }

        static inline size_t DoMaxCompressedLength(size_t in) noexcept {
            // very strange
            return in * 2 + 128;
        }

        TStringBuf Name() const noexcept override {
            return MyName;
        }

        inline size_t DoCompress(const TData& in, void* buf) const {
            unsigned int ret = DoMaxCompressedLength(in.size());
            const int res = BZ2_bzBuffToBuffCompress((char*)buf, &ret, (char*)in.data(), in.size(), Level, 0, 0);
            if (res != BZ_OK) {
                ythrow TCompressError(res);
            }

            return ret;
        }

        inline void DoDecompress(const TData& in, void* out, size_t len) const {
            unsigned int tmp = SafeIntegerCast<unsigned int>(len);
            const int res = BZ2_bzBuffToBuffDecompress((char*)out, &tmp, (char*)in.data(), in.size(), 0, 0);

            if (res != BZ_OK) {
                ythrow TDecompressError(res);
            }

            if (len != tmp) {
                ythrow TDecompressError(len, tmp);
            }
        }

        const int Level;
        const TString MyName;
    };

    struct TBZipRegistrar {
        TBZipRegistrar() {
            for (int i = 1; i < 10; ++i) {
                RegisterCodec(MakeHolder<TBZipCodec>(i));
            }
            RegisterAlias("bzip2", "bzip2-6");
        }
    };
    const TBZipRegistrar Registrar{};
}