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
|
#include "lz.h"
#include <util/system/yassert.h>
#include <util/system/byteorder.h>
#include <util/memory/addstorage.h>
#include <util/generic/buffer.h>
#include <util/generic/utility.h>
#include <util/generic/singleton.h>
#include <util/generic/yexception.h>
#include <util/stream/mem.h>
#include <library/cpp/streams/lz/common/compressor.h>
#include <library/cpp/streams/lz/lz4/block.h>
#include <library/cpp/streams/lz/snappy/block.h>
#include <contrib/libs/fastlz/fastlz.h>
#ifndef OPENSOURCE
#include "minilzo.h"
#include "quicklz.h"
#endif
/*
* FastLZ
*/
class TFastLZ {
public:
static const char signature[];
static inline size_t Hint(size_t len) noexcept {
return Max<size_t>((size_t)(len * 1.06), 100);
}
inline size_t Compress(const char* data, size_t len, char* ptr, size_t /*dstMaxSize*/) {
return fastlz_compress(data, len, ptr);
}
inline size_t Decompress(const char* data, size_t len, char* ptr, size_t max) {
return fastlz_decompress(data, len, ptr, max);
}
inline void InitFromStream(IInputStream*) const noexcept {
}
static inline bool SaveIncompressibleChunks() noexcept {
return false;
}
};
const char TFastLZ::signature[] = "YLZF";
DEF_COMPRESSOR(TLzfCompress, TFastLZ)
DEF_DECOMPRESSOR(TLzfDecompress, TFastLZ)
template <class T>
static TAutoPtr<IInputStream> TryOpenLzDecompressorX(const TDecompressSignature& s, T input) {
if (s.Check<TLZ4>())
return new TLzDecompressInput<T, TLZ4>(input);
if (s.Check<TSnappy>())
return new TLzDecompressInput<T, TSnappy>(input);
#ifndef OPENSOURCE
if (auto result = TryOpenMiniLzoDecompressor(s, input))
return result;
#endif
if (s.Check<TFastLZ>())
return new TLzDecompressInput<T, TFastLZ>(input);
#ifndef OPENSOURCE
if (auto result = TryOpenQuickLzDecompressor(s, input))
return result;
#endif
return nullptr;
}
template <class T>
static inline TAutoPtr<IInputStream> TryOpenLzDecompressorImpl(const TStringBuf& signature, T input) {
if (signature.size() == SIGNATURE_SIZE) {
TMemoryInput mem(signature.data(), signature.size());
TDecompressSignature s(&mem);
return TryOpenLzDecompressorX(s, input);
}
return nullptr;
}
template <class T>
static inline TAutoPtr<IInputStream> TryOpenLzDecompressorImpl(T input) {
TDecompressSignature s(&*input);
return TryOpenLzDecompressorX(s, input);
}
template <class T>
static inline TAutoPtr<IInputStream> OpenLzDecompressorImpl(T input) {
TAutoPtr<IInputStream> ret = TryOpenLzDecompressorImpl(input);
if (!ret) {
ythrow TDecompressorError() << "Unknown compression format";
}
return ret;
}
TAutoPtr<IInputStream> OpenLzDecompressor(IInputStream* input) {
return OpenLzDecompressorImpl(input);
}
TAutoPtr<IInputStream> TryOpenLzDecompressor(IInputStream* input) {
return TryOpenLzDecompressorImpl(input);
}
TAutoPtr<IInputStream> TryOpenLzDecompressor(const TStringBuf& signature, IInputStream* input) {
return TryOpenLzDecompressorImpl(signature, input);
}
TAutoPtr<IInputStream> OpenOwnedLzDecompressor(TAutoPtr<IInputStream> input) {
return OpenLzDecompressorImpl(input);
}
TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(TAutoPtr<IInputStream> input) {
return TryOpenLzDecompressorImpl(input);
}
TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(const TStringBuf& signature, TAutoPtr<IInputStream> input) {
return TryOpenLzDecompressorImpl(signature, input);
}
|