Coverage Report

Created: 2025-09-05 08:05

/src/duckdb/third_party/snappy/snappy-sinksource.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2011 Google Inc. All Rights Reserved.
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
#ifndef THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
30
#define THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
31
32
#include "snappy_version.hpp"
33
34
#if SNAPPY_NEW_VERSION
35
36
#include <stddef.h>
37
38
namespace duckdb_snappy {
39
40
// A Sink is an interface that consumes a sequence of bytes.
41
class Sink {
42
 public:
43
0
  Sink() { }
44
  virtual ~Sink();
45
46
  // Append "bytes[0,n-1]" to this.
47
  virtual void Append(const char* bytes, size_t n) = 0;
48
49
  // Returns a writable buffer of the specified length for appending.
50
  // May return a pointer to the caller-owned scratch buffer which
51
  // must have at least the indicated length.  The returned buffer is
52
  // only valid until the next operation on this Sink.
53
  //
54
  // After writing at most "length" bytes, call Append() with the
55
  // pointer returned from this function and the number of bytes
56
  // written.  Many Append() implementations will avoid copying
57
  // bytes if this function returned an internal buffer.
58
  //
59
  // If a non-scratch buffer is returned, the caller may only pass a
60
  // prefix of it to Append().  That is, it is not correct to pass an
61
  // interior pointer of the returned array to Append().
62
  //
63
  // The default implementation always returns the scratch buffer.
64
  virtual char* GetAppendBuffer(size_t length, char* scratch);
65
66
  // For higher performance, Sink implementations can provide custom
67
  // AppendAndTakeOwnership() and GetAppendBufferVariable() methods.
68
  // These methods can reduce the number of copies done during
69
  // compression/decompression.
70
71
  // Append "bytes[0,n-1] to the sink. Takes ownership of "bytes"
72
  // and calls the deleter function as (*deleter)(deleter_arg, bytes, n)
73
  // to free the buffer. deleter function must be non NULL.
74
  //
75
  // The default implementation just calls Append and frees "bytes".
76
  // Other implementations may avoid a copy while appending the buffer.
77
  virtual void AppendAndTakeOwnership(
78
      char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
79
      void *deleter_arg);
80
81
  // Returns a writable buffer for appending and writes the buffer's capacity to
82
  // *allocated_size. Guarantees *allocated_size >= min_size.
83
  // May return a pointer to the caller-owned scratch buffer which must have
84
  // scratch_size >= min_size.
85
  //
86
  // The returned buffer is only valid until the next operation
87
  // on this ByteSink.
88
  //
89
  // After writing at most *allocated_size bytes, call Append() with the
90
  // pointer returned from this function and the number of bytes written.
91
  // Many Append() implementations will avoid copying bytes if this function
92
  // returned an internal buffer.
93
  //
94
  // If the sink implementation allocates or reallocates an internal buffer,
95
  // it should use the desired_size_hint if appropriate. If a caller cannot
96
  // provide a reasonable guess at the desired capacity, it should set
97
  // desired_size_hint = 0.
98
  //
99
  // If a non-scratch buffer is returned, the caller may only pass
100
  // a prefix to it to Append(). That is, it is not correct to pass an
101
  // interior pointer to Append().
102
  //
103
  // The default implementation always returns the scratch buffer.
104
  virtual char* GetAppendBufferVariable(
105
      size_t min_size, size_t desired_size_hint, char* scratch,
106
      size_t scratch_size, size_t* allocated_size);
107
108
 private:
109
  // No copying
110
  Sink(const Sink&);
111
  void operator=(const Sink&);
112
};
113
114
// A Source is an interface that yields a sequence of bytes
115
class Source {
116
 public:
117
0
  Source() { }
118
  virtual ~Source();
119
120
  // Return the number of bytes left to read from the source
121
  virtual size_t Available() const = 0;
122
123
  // Peek at the next flat region of the source.  Does not reposition
124
  // the source.  The returned region is empty iff Available()==0.
125
  //
126
  // Returns a pointer to the beginning of the region and store its
127
  // length in *len.
128
  //
129
  // The returned region is valid until the next call to Skip() or
130
  // until this object is destroyed, whichever occurs first.
131
  //
132
  // The returned region may be larger than Available() (for example
133
  // if this ByteSource is a view on a substring of a larger source).
134
  // The caller is responsible for ensuring that it only reads the
135
  // Available() bytes.
136
  virtual const char* Peek(size_t* len) = 0;
137
138
  // Skip the next n bytes.  Invalidates any buffer returned by
139
  // a previous call to Peek().
140
  // REQUIRES: Available() >= n
141
  virtual void Skip(size_t n) = 0;
142
143
 private:
144
  // No copying
145
  Source(const Source&);
146
  void operator=(const Source&);
147
};
148
149
// A Source implementation that yields the contents of a flat array
150
class ByteArraySource : public Source {
151
 public:
152
0
  ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { }
153
  ~ByteArraySource() override;
154
  size_t Available() const override;
155
  const char* Peek(size_t* len) override;
156
  void Skip(size_t n) override;
157
 private:
158
  const char* ptr_;
159
  size_t left_;
160
};
161
162
// A Sink implementation that writes to a flat array without any bound checks.
163
class UncheckedByteArraySink : public Sink {
164
 public:
165
0
  explicit UncheckedByteArraySink(char* dest) : dest_(dest) { }
166
  ~UncheckedByteArraySink() override;
167
  void Append(const char* data, size_t n) override;
168
  char* GetAppendBuffer(size_t len, char* scratch) override;
169
  char* GetAppendBufferVariable(
170
      size_t min_size, size_t desired_size_hint, char* scratch,
171
      size_t scratch_size, size_t* allocated_size) override;
172
  void AppendAndTakeOwnership(
173
      char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
174
      void *deleter_arg) override;
175
176
  // Return the current output pointer so that a caller can see how
177
  // many bytes were produced.
178
  // Note: this is not a Sink method.
179
0
  char* CurrentDestination() const { return dest_; }
180
 private:
181
  char* dest_;
182
};
183
184
}  // namespace duckdb_snappy
185
186
#else // #if SNAPPY_NEW_VERSION
187
188
#include <stddef.h>
189
190
namespace duckdb_snappy {
191
192
// A Sink is an interface that consumes a sequence of bytes.
193
class Sink {
194
 public:
195
  Sink() { }
196
  virtual ~Sink();
197
198
  // Append "bytes[0,n-1]" to this.
199
  virtual void Append(const char* bytes, size_t n) = 0;
200
201
  // Returns a writable buffer of the specified length for appending.
202
  // May return a pointer to the caller-owned scratch buffer which
203
  // must have at least the indicated length.  The returned buffer is
204
  // only valid until the next operation on this Sink.
205
  //
206
  // After writing at most "length" bytes, call Append() with the
207
  // pointer returned from this function and the number of bytes
208
  // written.  Many Append() implementations will avoid copying
209
  // bytes if this function returned an internal buffer.
210
  //
211
  // If a non-scratch buffer is returned, the caller may only pass a
212
  // prefix of it to Append().  That is, it is not correct to pass an
213
  // interior pointer of the returned array to Append().
214
  //
215
  // The default implementation always returns the scratch buffer.
216
  virtual char* GetAppendBuffer(size_t length, char* scratch);
217
218
  // For higher performance, Sink implementations can provide custom
219
  // AppendAndTakeOwnership() and GetAppendBufferVariable() methods.
220
  // These methods can reduce the number of copies done during
221
  // compression/decompression.
222
223
  // Append "bytes[0,n-1] to the sink. Takes ownership of "bytes"
224
  // and calls the deleter function as (*deleter)(deleter_arg, bytes, n)
225
  // to free the buffer. deleter function must be non NULL.
226
  //
227
  // The default implementation just calls Append and frees "bytes".
228
  // Other implementations may avoid a copy while appending the buffer.
229
  virtual void AppendAndTakeOwnership(
230
      char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
231
      void *deleter_arg);
232
233
  // Returns a writable buffer for appending and writes the buffer's capacity to
234
  // *allocated_size. Guarantees *allocated_size >= min_size.
235
  // May return a pointer to the caller-owned scratch buffer which must have
236
  // scratch_size >= min_size.
237
  //
238
  // The returned buffer is only valid until the next operation
239
  // on this ByteSink.
240
  //
241
  // After writing at most *allocated_size bytes, call Append() with the
242
  // pointer returned from this function and the number of bytes written.
243
  // Many Append() implementations will avoid copying bytes if this function
244
  // returned an internal buffer.
245
  //
246
  // If the sink implementation allocates or reallocates an internal buffer,
247
  // it should use the desired_size_hint if appropriate. If a caller cannot
248
  // provide a reasonable guess at the desired capacity, it should set
249
  // desired_size_hint = 0.
250
  //
251
  // If a non-scratch buffer is returned, the caller may only pass
252
  // a prefix to it to Append(). That is, it is not correct to pass an
253
  // interior pointer to Append().
254
  //
255
  // The default implementation always returns the scratch buffer.
256
  virtual char* GetAppendBufferVariable(
257
      size_t min_size, size_t desired_size_hint, char* scratch,
258
      size_t scratch_size, size_t* allocated_size);
259
260
 private:
261
  // No copying
262
  Sink(const Sink&);
263
  void operator=(const Sink&);
264
};
265
266
// A Source is an interface that yields a sequence of bytes
267
class Source {
268
 public:
269
  Source() { }
270
  virtual ~Source();
271
272
  // Return the number of bytes left to read from the source
273
  virtual size_t Available() const = 0;
274
275
  // Peek at the next flat region of the source.  Does not reposition
276
  // the source.  The returned region is empty iff Available()==0.
277
  //
278
  // Returns a pointer to the beginning of the region and store its
279
  // length in *len.
280
  //
281
  // The returned region is valid until the next call to Skip() or
282
  // until this object is destroyed, whichever occurs first.
283
  //
284
  // The returned region may be larger than Available() (for example
285
  // if this ByteSource is a view on a substring of a larger source).
286
  // The caller is responsible for ensuring that it only reads the
287
  // Available() bytes.
288
  virtual const char* Peek(size_t* len) = 0;
289
290
  // Skip the next n bytes.  Invalidates any buffer returned by
291
  // a previous call to Peek().
292
  // REQUIRES: Available() >= n
293
  virtual void Skip(size_t n) = 0;
294
295
 private:
296
  // No copying
297
  Source(const Source&);
298
  void operator=(const Source&);
299
};
300
301
// A Source implementation that yields the contents of a flat array
302
class ByteArraySource : public Source {
303
 public:
304
  ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { }
305
  virtual ~ByteArraySource();
306
  virtual size_t Available() const;
307
  virtual const char* Peek(size_t* len);
308
  virtual void Skip(size_t n);
309
 private:
310
  const char* ptr_;
311
  size_t left_;
312
};
313
314
// A Sink implementation that writes to a flat array without any bound checks.
315
class UncheckedByteArraySink : public Sink {
316
 public:
317
  explicit UncheckedByteArraySink(char* dest) : dest_(dest) { }
318
  virtual ~UncheckedByteArraySink();
319
  virtual void Append(const char* data, size_t n);
320
  virtual char* GetAppendBuffer(size_t len, char* scratch);
321
  virtual char* GetAppendBufferVariable(
322
      size_t min_size, size_t desired_size_hint, char* scratch,
323
      size_t scratch_size, size_t* allocated_size);
324
  virtual void AppendAndTakeOwnership(
325
      char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
326
      void *deleter_arg);
327
328
  // Return the current output pointer so that a caller can see how
329
  // many bytes were produced.
330
  // Note: this is not a Sink method.
331
  char* CurrentDestination() const { return dest_; }
332
 private:
333
  char* dest_;
334
};
335
336
}  // namespace duckdb_snappy
337
338
#endif  // #if SNAPPY_NEW_VERSION # else
339
340
#endif  // THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_