Coverage Report

Created: 2026-03-31 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/duckdb/third_party/snappy/snappy.h
Line
Count
Source
1
// Copyright 2005 and onwards Google Inc.
2
//
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are
5
// met:
6
//
7
//     * Redistributions of source code must retain the above copyright
8
// notice, this list of conditions and the following disclaimer.
9
//     * Redistributions in binary form must reproduce the above
10
// copyright notice, this list of conditions and the following disclaimer
11
// in the documentation and/or other materials provided with the
12
// distribution.
13
//     * Neither the name of Google Inc. nor the names of its
14
// contributors may be used to endorse or promote products derived from
15
// this software without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
//
29
// A light-weight compression algorithm.  It is designed for speed of
30
// compression and decompression, rather than for the utmost in space
31
// savings.
32
//
33
// For getting better compression ratios when you are compressing data
34
// with long repeated sequences or compressing data that is similar to
35
// other data, while still compressing fast, you might look at first
36
// using BMDiff and then compressing the output of BMDiff with
37
// Snappy.
38
39
#ifndef THIRD_PARTY_SNAPPY_SNAPPY_H__
40
#define THIRD_PARTY_SNAPPY_SNAPPY_H__
41
42
#include "snappy_version.hpp"
43
44
#if SNAPPY_NEW_VERSION
45
46
#include <stddef.h>
47
#include <stdint.h>
48
49
#include <string>
50
51
#include "snappy-stubs-public.h"
52
53
namespace duckdb_snappy {
54
  class Source;
55
  class Sink;
56
57
  struct CompressionOptions {
58
    // Compression level.
59
    // Level 1 is the fastest
60
    // Level 2 is a little slower but provides better compression. Level 2 is
61
    // **EXPERIMENTAL** for the time being. It might happen that we decide to
62
    // fall back to level 1 in the future.
63
    // Levels 3+ are currently not supported. We plan to support levels up to
64
    // 9 in the future.
65
    // If you played with other compression algorithms, level 1 is equivalent to
66
    // fast mode (level 1) of LZ4, level 2 is equivalent to LZ4's level 2 mode
67
    // and compresses somewhere around zstd:-3 and zstd:-2 but generally with
68
    // faster decompression speeds than snappy:1 and zstd:-3.
69
    int level = DefaultCompressionLevel();
70
71
0
    constexpr CompressionOptions() = default;
72
    constexpr CompressionOptions(int compression_level)
73
0
        : level(compression_level) {}
74
0
    static constexpr int MinCompressionLevel() { return 1; }
75
0
    static constexpr int MaxCompressionLevel() { return 2; }
76
0
    static constexpr int DefaultCompressionLevel() { return 1; }
77
  };
78
79
  // ------------------------------------------------------------------------
80
  // Generic compression/decompression routines.
81
  // ------------------------------------------------------------------------
82
83
  // Compress the bytes read from "*reader" and append to "*writer". Return the
84
  // number of bytes written.
85
  // First version is to preserve ABI.
86
  size_t Compress(Source* reader, Sink* writer);
87
  size_t Compress(Source* reader, Sink* writer,
88
                  CompressionOptions options);
89
90
  // Find the uncompressed length of the given stream, as given by the header.
91
  // Note that the true length could deviate from this; the stream could e.g.
92
  // be truncated.
93
  //
94
  // Also note that this leaves "*source" in a state that is unsuitable for
95
  // further operations, such as RawUncompress(). You will need to rewind
96
  // or recreate the source yourself before attempting any further calls.
97
  bool GetUncompressedLength(Source* source, uint32_t* result);
98
99
  // ------------------------------------------------------------------------
100
  // Higher-level string based routines (should be sufficient for most users)
101
  // ------------------------------------------------------------------------
102
103
  // Sets "*compressed" to the compressed version of "input[0..input_length-1]".
104
  // Original contents of *compressed are lost.
105
  //
106
  // REQUIRES: "input[]" is not an alias of "*compressed".
107
  // First version is to preserve ABI.
108
  size_t Compress(const char* input, size_t input_length,
109
                  std::string* compressed);
110
  size_t Compress(const char* input, size_t input_length,
111
                  std::string* compressed, CompressionOptions options);
112
113
  // Same as `Compress` above but taking an `iovec` array as input. Note that
114
  // this function preprocesses the inputs to compute the sum of
115
  // `iov[0..iov_cnt-1].iov_len` before reading. To avoid this, use
116
  // `RawCompressFromIOVec` below.
117
  // First version is to preserve ABI.
118
  size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
119
                           std::string* compressed);
120
  size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
121
                           std::string* compressed,
122
                           CompressionOptions options);
123
124
  // Decompresses "compressed[0..compressed_length-1]" to "*uncompressed".
125
  // Original contents of "*uncompressed" are lost.
126
  //
127
  // REQUIRES: "compressed[]" is not an alias of "*uncompressed".
128
  //
129
  // returns false if the message is corrupted and could not be decompressed
130
  bool Uncompress(const char* compressed, size_t compressed_length,
131
                  std::string* uncompressed);
132
133
  // Decompresses "compressed" to "*uncompressed".
134
  //
135
  // returns false if the message is corrupted and could not be decompressed
136
  bool Uncompress(Source* compressed, Sink* uncompressed);
137
138
  // This routine uncompresses as much of the "compressed" as possible
139
  // into sink.  It returns the number of valid bytes added to sink
140
  // (extra invalid bytes may have been added due to errors; the caller
141
  // should ignore those). The emitted data typically has length
142
  // GetUncompressedLength(), but may be shorter if an error is
143
  // encountered.
144
  size_t UncompressAsMuchAsPossible(Source* compressed, Sink* uncompressed);
145
146
  // ------------------------------------------------------------------------
147
  // Lower-level character array based routines.  May be useful for
148
  // efficiency reasons in certain circumstances.
149
  // ------------------------------------------------------------------------
150
151
  // REQUIRES: "compressed" must point to an area of memory that is at
152
  // least "MaxCompressedLength(input_length)" bytes in length.
153
  //
154
  // Takes the data stored in "input[0..input_length]" and stores
155
  // it in the array pointed to by "compressed".
156
  //
157
  // "*compressed_length" is set to the length of the compressed output.
158
  //
159
  // Example:
160
  //    char* output = new char[snappy::MaxCompressedLength(input_length)];
161
  //    size_t output_length;
162
  //    RawCompress(input, input_length, output, &output_length);
163
  //    ... Process(output, output_length) ...
164
  //    delete [] output;
165
  void RawCompress(const char* input, size_t input_length, char* compressed,
166
                   size_t* compressed_length);
167
  void RawCompress(const char* input, size_t input_length, char* compressed,
168
                   size_t* compressed_length, CompressionOptions options);
169
170
  // Same as `RawCompress` above but taking an `iovec` array as input. Note that
171
  // `uncompressed_length` is the total number of bytes to be read from the
172
  // elements of `iov` (_not_ the number of elements in `iov`).
173
  void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
174
                            char* compressed, size_t* compressed_length);
175
  void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
176
                            char* compressed, size_t* compressed_length,
177
                            CompressionOptions options);
178
179
  // Given data in "compressed[0..compressed_length-1]" generated by
180
  // calling the Snappy::Compress routine, this routine
181
  // stores the uncompressed data to
182
  //    uncompressed[0..GetUncompressedLength(compressed)-1]
183
  // returns false if the message is corrupted and could not be decrypted
184
  bool RawUncompress(const char* compressed, size_t compressed_length,
185
                     char* uncompressed);
186
187
  // Given data from the byte source 'compressed' generated by calling
188
  // the Snappy::Compress routine, this routine stores the uncompressed
189
  // data to
190
  //    uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1]
191
  // returns false if the message is corrupted and could not be decrypted
192
  bool RawUncompress(Source* compressed, char* uncompressed);
193
194
  // Given data in "compressed[0..compressed_length-1]" generated by
195
  // calling the Snappy::Compress routine, this routine
196
  // stores the uncompressed data to the iovec "iov". The number of physical
197
  // buffers in "iov" is given by iov_cnt and their cumulative size
198
  // must be at least GetUncompressedLength(compressed). The individual buffers
199
  // in "iov" must not overlap with each other.
200
  //
201
  // returns false if the message is corrupted and could not be decrypted
202
  bool RawUncompressToIOVec(const char* compressed, size_t compressed_length,
203
                            const struct iovec* iov, size_t iov_cnt);
204
205
  // Given data from the byte source 'compressed' generated by calling
206
  // the Snappy::Compress routine, this routine stores the uncompressed
207
  // data to the iovec "iov". The number of physical
208
  // buffers in "iov" is given by iov_cnt and their cumulative size
209
  // must be at least GetUncompressedLength(compressed). The individual buffers
210
  // in "iov" must not overlap with each other.
211
  //
212
  // returns false if the message is corrupted and could not be decrypted
213
  bool RawUncompressToIOVec(Source* compressed, const struct iovec* iov,
214
                            size_t iov_cnt);
215
216
  // Returns the maximal size of the compressed representation of
217
  // input data that is "source_bytes" bytes in length;
218
  size_t MaxCompressedLength(size_t source_bytes);
219
220
  // REQUIRES: "compressed[]" was produced by RawCompress() or Compress()
221
  // Returns true and stores the length of the uncompressed data in
222
  // *result normally.  Returns false on parsing error.
223
  // This operation takes O(1) time.
224
  bool GetUncompressedLength(const char* compressed, size_t compressed_length,
225
                             size_t* result);
226
227
  // Returns true iff the contents of "compressed[]" can be uncompressed
228
  // successfully.  Does not return the uncompressed data.  Takes
229
  // time proportional to compressed_length, but is usually at least
230
  // a factor of four faster than actual decompression.
231
  bool IsValidCompressedBuffer(const char* compressed,
232
                               size_t compressed_length);
233
234
  // Returns true iff the contents of "compressed" can be uncompressed
235
  // successfully.  Does not return the uncompressed data.  Takes
236
  // time proportional to *compressed length, but is usually at least
237
  // a factor of four faster than actual decompression.
238
  // On success, consumes all of *compressed.  On failure, consumes an
239
  // unspecified prefix of *compressed.
240
  bool IsValidCompressed(Source* compressed);
241
242
  // The size of a compression block. Note that many parts of the compression
243
  // code assumes that kBlockSize <= 65536; in particular, the hash table
244
  // can only store 16-bit offsets, and EmitCopy() also assumes the offset
245
  // is 65535 bytes or less. Note also that if you change this, it will
246
  // affect the framing format (see framing_format.txt).
247
  //
248
  // Note that there might be older data around that is compressed with larger
249
  // block sizes, so the decompression code should not rely on the
250
  // non-existence of long backreferences.
251
  static constexpr int kBlockLog = 16;
252
  static constexpr size_t kBlockSize = 1 << kBlockLog;
253
254
  static constexpr int kMinHashTableBits = 8;
255
  static constexpr size_t kMinHashTableSize = 1 << kMinHashTableBits;
256
257
  static constexpr int kMaxHashTableBits = 15;
258
  static constexpr size_t kMaxHashTableSize = 1 << kMaxHashTableBits;
259
}  // end namespace duckdb_snappy
260
261
#else // #if SNAPPY_NEW_VERSION
262
263
#include <cstddef>
264
#include <string>
265
266
#include "snappy-stubs-public.h"
267
268
namespace duckdb_snappy {
269
  class Source;
270
  class Sink;
271
272
  // ------------------------------------------------------------------------
273
  // Generic compression/decompression routines.
274
  // ------------------------------------------------------------------------
275
276
  // Compress the bytes read from "*source" and append to "*sink". Return the
277
  // number of bytes written.
278
  size_t Compress(Source* source, Sink* sink);
279
280
  // Find the uncompressed length of the given stream, as given by the header.
281
  // Note that the true length could deviate from this; the stream could e.g.
282
  // be truncated.
283
  //
284
  // Also note that this leaves "*source" in a state that is unsuitable for
285
  // further operations, such as RawUncompress(). You will need to rewind
286
  // or recreate the source yourself before attempting any further calls.
287
  bool GetUncompressedLength(Source* source, uint32* result);
288
289
  // ------------------------------------------------------------------------
290
  // Higher-level string based routines (should be sufficient for most users)
291
  // ------------------------------------------------------------------------
292
293
  // Sets "*output" to the compressed version of "input[0,input_length-1]".
294
  // Original contents of *output are lost.
295
  //
296
  // REQUIRES: "input[]" is not an alias of "*output".
297
  size_t Compress(const char* input, size_t input_length, string* output);
298
299
  // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed".
300
  // Original contents of "*uncompressed" are lost.
301
  //
302
  // REQUIRES: "compressed[]" is not an alias of "*uncompressed".
303
  //
304
  // returns false if the message is corrupted and could not be decompressed
305
  bool Uncompress(const char* compressed, size_t compressed_length,
306
                  string* uncompressed);
307
308
  // Decompresses "compressed" to "*uncompressed".
309
  //
310
  // returns false if the message is corrupted and could not be decompressed
311
  bool Uncompress(Source* compressed, Sink* uncompressed);
312
313
  // This routine uncompresses as much of the "compressed" as possible
314
  // into sink.  It returns the number of valid bytes added to sink
315
  // (extra invalid bytes may have been added due to errors; the caller
316
  // should ignore those). The emitted data typically has length
317
  // GetUncompressedLength(), but may be shorter if an error is
318
  // encountered.
319
  size_t UncompressAsMuchAsPossible(Source* compressed, Sink* uncompressed);
320
321
  // ------------------------------------------------------------------------
322
  // Lower-level character array based routines.  May be useful for
323
  // efficiency reasons in certain circumstances.
324
  // ------------------------------------------------------------------------
325
326
  // REQUIRES: "compressed" must point to an area of memory that is at
327
  // least "MaxCompressedLength(input_length)" bytes in length.
328
  //
329
  // Takes the data stored in "input[0..input_length]" and stores
330
  // it in the array pointed to by "compressed".
331
  //
332
  // "*compressed_length" is set to the length of the compressed output.
333
  //
334
  // Example:
335
  //    char* output = new char[snappy::MaxCompressedLength(input_length)];
336
  //    size_t output_length;
337
  //    RawCompress(input, input_length, output, &output_length);
338
  //    ... Process(output, output_length) ...
339
  //    delete [] output;
340
  void RawCompress(const char* input,
341
                   size_t input_length,
342
                   char* compressed,
343
                   size_t* compressed_length);
344
345
  // Given data in "compressed[0..compressed_length-1]" generated by
346
  // calling the Snappy::Compress routine, this routine
347
  // stores the uncompressed data to
348
  //    uncompressed[0..GetUncompressedLength(compressed)-1]
349
  // returns false if the message is corrupted and could not be decrypted
350
  bool RawUncompress(const char* compressed, size_t compressed_length,
351
                     char* uncompressed);
352
353
  // Given data from the byte source 'compressed' generated by calling
354
  // the Snappy::Compress routine, this routine stores the uncompressed
355
  // data to
356
  //    uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1]
357
  // returns false if the message is corrupted and could not be decrypted
358
  bool RawUncompress(Source* compressed, char* uncompressed);
359
360
  // Given data in "compressed[0..compressed_length-1]" generated by
361
  // calling the Snappy::Compress routine, this routine
362
  // stores the uncompressed data to the iovec "iov". The number of physical
363
  // buffers in "iov" is given by iov_cnt and their cumulative size
364
  // must be at least GetUncompressedLength(compressed). The individual buffers
365
  // in "iov" must not overlap with each other.
366
  //
367
  // returns false if the message is corrupted and could not be decrypted
368
  bool RawUncompressToIOVec(const char* compressed, size_t compressed_length,
369
                            const struct iovec* iov, size_t iov_cnt);
370
371
  // Given data from the byte source 'compressed' generated by calling
372
  // the Snappy::Compress routine, this routine stores the uncompressed
373
  // data to the iovec "iov". The number of physical
374
  // buffers in "iov" is given by iov_cnt and their cumulative size
375
  // must be at least GetUncompressedLength(compressed). The individual buffers
376
  // in "iov" must not overlap with each other.
377
  //
378
  // returns false if the message is corrupted and could not be decrypted
379
  bool RawUncompressToIOVec(Source* compressed, const struct iovec* iov,
380
                            size_t iov_cnt);
381
382
  // Returns the maximal size of the compressed representation of
383
  // input data that is "source_bytes" bytes in length;
384
  size_t MaxCompressedLength(size_t source_bytes);
385
386
  // REQUIRES: "compressed[]" was produced by RawCompress() or Compress()
387
  // Returns true and stores the length of the uncompressed data in
388
  // *result normally.  Returns false on parsing error.
389
  // This operation takes O(1) time.
390
  bool GetUncompressedLength(const char* compressed, size_t compressed_length,
391
                             size_t* result);
392
393
  // Returns true iff the contents of "compressed[]" can be uncompressed
394
  // successfully.  Does not return the uncompressed data.  Takes
395
  // time proportional to compressed_length, but is usually at least
396
  // a factor of four faster than actual decompression.
397
  bool IsValidCompressedBuffer(const char* compressed,
398
                               size_t compressed_length);
399
400
  // Returns true iff the contents of "compressed" can be uncompressed
401
  // successfully.  Does not return the uncompressed data.  Takes
402
  // time proportional to *compressed length, but is usually at least
403
  // a factor of four faster than actual decompression.
404
  // On success, consumes all of *compressed.  On failure, consumes an
405
  // unspecified prefix of *compressed.
406
  bool IsValidCompressed(Source* compressed);
407
408
}  // end namespace duckdb_snappy
409
410
#endif  // #if SNAPPY_NEW_VERSION # else
411
412
#endif  // THIRD_PARTY_SNAPPY_SNAPPY_H__