aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/streams/lz/lz.h
blob: cdad6df4edcd222105f34b1b0d09621f6bc4da9d (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#pragma once

#include <util/stream/output.h>
#include <util/stream/input.h>
#include <util/generic/ptr.h>
#include <util/generic/yexception.h>

/** 
 * @file 
 * 
 * All lz compressors compress blocks. `Write` method splits input data into 
 * blocks, compresses each block and then writes each compressed block to the 
 * underlying output stream. Thus compression classes are not buffered. 
 * MaxBlockSize parameter specified max allowed block size. 
 * 
 * See http://altdevblogaday.com/2011/04/22/survey-of-fast-compression-algorithms-part-1/ 
 * for some comparisons. 
 */ 

struct TDecompressorError: public yexception {
};

/** 
 * @addtogroup Streams_Archs 
 * @{ 
 */
 
/** 
 * Lz4 compressing stream. 
 * 
 * @see http://code.google.com/p/lz4/ 
 */ 
class TLz4Compress: public IOutputStream {
public:
    TLz4Compress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
    ~TLz4Compress() override;

private:
    void DoWrite(const void* buf, size_t len) override;
    void DoFlush() override;
    void DoFinish() override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * Lz4 decompressing stream. 
 * 
 * @see http://code.google.com/p/lz4/ 
 */ 
class TLz4Decompress: public IInputStream {
public:
    TLz4Decompress(IInputStream* slave);
    ~TLz4Decompress() override;

private:
    size_t DoRead(void* buf, size_t len) override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * Snappy compressing stream. 
 * 
 * @see http://code.google.com/p/snappy/ 
 */
class TSnappyCompress: public IOutputStream {
public:
    TSnappyCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
    ~TSnappyCompress() override;

private:
    void DoWrite(const void* buf, size_t len) override;
    void DoFlush() override;
    void DoFinish() override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * Snappy decompressing stream. 
 * 
 * @see http://code.google.com/p/snappy/ 
 */ 
class TSnappyDecompress: public IInputStream {
public:
    TSnappyDecompress(IInputStream* slave);
    ~TSnappyDecompress() override;

private:
    size_t DoRead(void* buf, size_t len) override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * MiniLZO compressing stream. 
 */
class TLzoCompress: public IOutputStream {
public:
    TLzoCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
    ~TLzoCompress() override;

private:
    void DoWrite(const void* buf, size_t len) override;
    void DoFlush() override;
    void DoFinish() override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * MiniLZO decompressing stream. 
 */ 
class TLzoDecompress: public IInputStream {
public:
    TLzoDecompress(IInputStream* slave);
    ~TLzoDecompress() override;

private:
    size_t DoRead(void* buf, size_t len) override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * FastLZ compressing stream. 
 */
class TLzfCompress: public IOutputStream {
public:
    TLzfCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
    ~TLzfCompress() override;

private:
    void DoWrite(const void* buf, size_t len) override;
    void DoFlush() override;
    void DoFinish() override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * FastLZ decompressing stream. 
 */ 
class TLzfDecompress: public IInputStream {
public:
    TLzfDecompress(IInputStream* slave);
    ~TLzfDecompress() override;

private:
    size_t DoRead(void* buf, size_t len) override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * QuickLZ compressing stream. 
 */
class TLzqCompress: public IOutputStream {
public:
    enum EVersion {
        V_1_31 = 0,
        V_1_40 = 1,
        V_1_51 = 2
    };

    /*
     * streaming mode - actually, backlog size 
     */ 
    enum EMode {
        M_0 = 0,
        M_100000 = 1,
        M_1000000 = 2
    };

    TLzqCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15,
                 EVersion ver = V_1_31,
                 unsigned level = 0,
                 EMode mode = M_0);
    ~TLzqCompress() override;

private:
    void DoWrite(const void* buf, size_t len) override;
    void DoFlush() override;
    void DoFinish() override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** 
 * QuickLZ decompressing stream. 
 */ 
class TLzqDecompress: public IInputStream {
public:
    TLzqDecompress(IInputStream* slave);
    ~TLzqDecompress() override;

private:
    size_t DoRead(void* buf, size_t len) override;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

/** @} */ 

/** 
 * Reads a compression signature from the provided input stream and returns a 
 * corresponding decompressing stream. 
 * 
 * Note that returned stream doesn't own the provided input stream, thus it's 
 * up to the user to free them both. 
 * 
 * @param input                         Stream to decompress. 
 * @return                              Decompressing proxy input stream. 
 */ 
TAutoPtr<IInputStream> OpenLzDecompressor(IInputStream* input);
TAutoPtr<IInputStream> TryOpenLzDecompressor(IInputStream* input);
TAutoPtr<IInputStream> TryOpenLzDecompressor(const TStringBuf& signature, IInputStream* input);

TAutoPtr<IInputStream> OpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(const TStringBuf& signature, TAutoPtr<IInputStream> input);