aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/blockcodecs/codecs/fastlz/fastlz.cpp
blob: 1fe7bb3a7d512324878078cd7bbdea7f6fa9b188 (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
#include <library/cpp/blockcodecs/core/codecs.h>
#include <library/cpp/blockcodecs/core/common.h>
#include <library/cpp/blockcodecs/core/register.h>
 
#include <contrib/libs/fastlz/fastlz.h> 
 
using namespace NBlockCodecs; 
 
namespace { 
    struct TFastLZCodec: public TAddLengthCodec<TFastLZCodec> { 
        inline TFastLZCodec(int level) 
            : MyName("fastlz-" + ToString(level)) 
            , Level(level) 
        { 
        } 
 
        static inline size_t DoMaxCompressedLength(size_t in) noexcept { 
            return Max<size_t>(in + in / 20, 128); 
        } 
 
        TStringBuf Name() const noexcept override { 
            return MyName; 
        } 
 
        inline size_t DoCompress(const TData& in, void* buf) const { 
            if (Level) { 
                return fastlz_compress_level(Level, in.data(), in.size(), buf); 
            } 
 
            return fastlz_compress(in.data(), in.size(), buf); 
        } 
 
        inline void DoDecompress(const TData& in, void* out, size_t len) const { 
            const int ret = fastlz_decompress(in.data(), in.size(), out, len); 
 
            if (ret < 0 || (size_t)ret != len) { 
                ythrow TDataError() << TStringBuf("can not decompress");
            } 
        } 
 
        const TString MyName; 
        const int Level; 
    }; 
 
    struct TFastLZRegistrar { 
        TFastLZRegistrar() { 
            for (int i = 0; i < 3; ++i) { 
                RegisterCodec(MakeHolder<TFastLZCodec>(i));
            } 
            RegisterAlias("fastlz", "fastlz-0"); 
        } 
    }; 
    const TFastLZRegistrar Registrar{};
}