Botan 3.13.0
Crypto and TLS for C&
compression.h
Go to the documentation of this file.
1/*
2* Compression Transform
3* (C) 2014 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_COMPRESSION_TRANSFORM_H_
9#define BOTAN_COMPRESSION_TRANSFORM_H_
10
11#include <botan/exceptn.h>
12#include <botan/secmem.h>
13#include <memory>
14#include <string>
15
16namespace Botan {
17
18/**
19* Interface for a compression algorithm.
20*/
21class BOTAN_PUBLIC_API(2, 0) Compression_Algorithm /* NOLINT(*-special-member-functions) */ {
22 public:
23 /**
24 * Create an instance based on a name, or return null if the
25 * algo combination cannot be found.
26 */
27 static std::unique_ptr<Compression_Algorithm> create(std::string_view algo_spec);
28
29 /**
30 * Create an instance based on a name
31 * @param algo_spec algorithm name
32 * Throws Lookup_Error if not found.
33 */
34 static std::unique_ptr<Compression_Algorithm> create_or_throw(std::string_view algo_spec);
35
36 /**
37 * Begin compressing. Most compression algorithms offer a tunable
38 * time/compression tradeoff parameter generally represented by an integer
39 * in the range of 1 to 9. Higher values typically imply better compression
40 * and more memory and/or CPU time consumed by the compression process.
41 *
42 * If 0 or a value out of range is provided, a compression algorithm
43 * specific default is used.
44 *
45 * @param comp_level the desired level of compression (typically from 1 to 9)
46 */
47 virtual void start(size_t comp_level = 0) = 0;
48
49 /**
50 * Process some data.
51 *
52 * The leading @p offset bytes of @p buf are ignored and remain untouched;
53 * this can be useful for ignoring packet headers. If @p flush is true,
54 * the compression state is flushed, allowing the decompressor to recover
55 * the entire message up to this point without having to see the rest of
56 * the compressed stream.
57 *
58 * @param buf in/out parameter which will possibly be resized or swapped
59 * @param offset an offset into blocks to begin processing
60 * @param flush if true the compressor will be told to flush state
61 */
62 virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0, bool flush = false) = 0;
63
64 /**
65 * Finish compressing
66 *
67 * The @p buf and @p offset parameters are treated as in update(). It is
68 * acceptable to call start() followed by finish() with the entire message,
69 * without any intervening call to update().
70 *
71 * @param final_block in/out parameter
72 * @param offset an offset into final_block to begin processing
73 */
74 virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
75
76 /**
77 * Return the name of this compression algorithm
78 * @return name of the compression algorithm
79 */
80 virtual std::string name() const = 0;
81
82 /**
83 * Reset the state and abort the current message; start can be
84 * called again to process a new message.
85 */
86 virtual void clear() = 0;
87
88 virtual ~Compression_Algorithm() = default;
89};
90
91/**
92* Interface for a decompression algorithm.
93*/
94class BOTAN_PUBLIC_API(2, 0) Decompression_Algorithm /* NOLINT(*-special-member-functions) */ {
95 public:
96 /**
97 * Create an instance based on a name, or return null if the
98 * algo combination cannot be found.
99 */
100 static std::unique_ptr<Decompression_Algorithm> create(std::string_view algo_spec);
101
102 /**
103 * Create an instance based on a name
104 * @param algo_spec algorithm name
105 * Throws Lookup_Error if not found.
106 */
107 static std::unique_ptr<Decompression_Algorithm> create_or_throw(std::string_view algo_spec);
108
109 /**
110 * Begin decompressing.
111 *
112 * This initializes the decompression engine and must be done before
113 * calling update() or finish(). No level is provided here; the
114 * decompressor can accept input generated by any compression parameters.
115 */
116 virtual void start() = 0;
117
118 /**
119 * Process some data.
120 * @param buf in/out parameter which will possibly be resized or swapped
121 * @param offset an offset into blocks to begin processing
122 */
123 virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0) = 0;
124
125 /**
126 * Finish decompressing
127 *
128 * Decompress the material in the in/out parameter @p buf. The leading
129 * @p offset bytes of @p buf are ignored and remain untouched; this can
130 * be useful for ignoring packet headers.
131 *
132 * This function may throw if the data seems to be invalid.
133 *
134 * @param final_block in/out parameter
135 * @param offset an offset into final_block to begin processing
136 */
137 virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
138
139 /**
140 * Return the name of this decompression algorithm
141 * @return name of the decompression algorithm
142 */
143 virtual std::string name() const = 0;
144
145 /**
146 * Reset the state and abort the current message; start can be
147 * called again to process a new message.
148 */
149 virtual void clear() = 0;
150
151 virtual ~Decompression_Algorithm() = default;
152};
153
154/**
155* Create a compression algorithm by name
156* @param type the algorithm to create
157* @return the new object, or nullptr if not available
158*/
159BOTAN_DEPRECATED("Use Compression_Algorithm::create")
160inline Compression_Algorithm* make_compressor(std::string_view type) {
161 return Compression_Algorithm::create(type).release();
162}
163
164/**
165* Create a decompression algorithm by name
166* @param type the algorithm to create
167* @return the new object, or nullptr if not available
168*/
169BOTAN_DEPRECATED("Use Decompression_Algorithm::create")
170inline Decompression_Algorithm* make_decompressor(std::string_view type) {
171 return Decompression_Algorithm::create(type).release();
172}
173
174/**
175* An error that occurred during compression (or decompression)
176*/
177class BOTAN_PUBLIC_API(2, 9) Compression_Error final : public Exception {
178 public:
179 /**
180 * Create a Compression_Error
181 * @param func_name the name of the compression API that was called
182 * (eg "BZ2_bzCompressInit" or "lzma_code")
183 * @param type what library this came from
184 * @param rc the error return code from the compression API. The
185 * interpretation of this value will depend on the library.
186 */
187 Compression_Error(const char* func_name, ErrorType type, int rc);
188
189 /**
190 * Return the error type of this exception
191 * @return the error type passed at construction
192 */
193 ErrorType error_type() const noexcept override { return m_type; }
194
195 /**
196 * Return the library specific error code
197 * @return the return code from the underlying compression API
198 */
199 int error_code() const noexcept override { return m_rc; }
200
201 private:
202 ErrorType m_type;
203 int m_rc;
204};
205
206/**
207* Adapts a zlib style API
208*/
209class Compression_Stream /* NOLINT(*-special-member-functions) */ {
210 public:
211 virtual ~Compression_Stream() = default;
212
213 /**
214 * Set the input buffer
215 * @param b the input buffer
216 * @param len the length of b in bytes
217 */
218 virtual void next_in(uint8_t* b, size_t len) = 0;
219
220 /**
221 * Set the output buffer
222 * @param b the output buffer
223 * @param len the length of b in bytes
224 */
225 virtual void next_out(uint8_t* b, size_t len) = 0;
226
227 /**
228 * Return how much input remains unconsumed
229 * @return the number of unread input bytes
230 */
231 virtual size_t avail_in() const = 0;
232
233 /**
234 * Return how much output space remains
235 * @return the number of unwritten output bytes
236 */
237 virtual size_t avail_out() const = 0;
238
239 /**
240 * Return the library specific flag for ordinary processing
241 * @return the run flag
242 */
243 virtual uint32_t run_flag() const = 0;
244
245 /**
246 * Return the library specific flag for flushing the stream
247 * @return the flush flag
248 */
249 virtual uint32_t flush_flag() const = 0;
250
251 /**
252 * Return the library specific flag for finishing the stream
253 * @return the finish flag
254 */
255 virtual uint32_t finish_flag() const = 0;
256
257 /**
258 * Run the compression engine over the current buffers
259 * @param flags one of run_flag(), flush_flag() or finish_flag()
260 * @return true once the stream is complete
261 */
262 virtual bool run(uint32_t flags) = 0;
263};
264
265/**
266* Used to implement compression using Compression_Stream
267*/
269 public:
270 /**
271 * Compress some data
272 * @param buf in/out parameter which will possibly be resized or swapped
273 * @param offset an offset into buf to begin processing
274 * @param flush if true the compressor will be told to flush state
275 */
276 void update(secure_vector<uint8_t>& buf, size_t offset, bool flush) final;
277
278 /**
279 * Finish compressing
280 * @param buf in/out parameter which will possibly be resized or swapped
281 * @param offset an offset into buf to begin processing
282 */
283 void finish(secure_vector<uint8_t>& buf, size_t offset) final;
284
285 /**
286 * Reset the state and abort the current message
287 */
288 void clear() final;
289
290 private:
291 void start(size_t level) final;
292
293 void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
294
295 virtual std::unique_ptr<Compression_Stream> make_stream(size_t level) const = 0;
296
297 secure_vector<uint8_t> m_buffer;
298 std::unique_ptr<Compression_Stream> m_stream;
299};
300
301/**
302* Used to implement decompression using Compression_Stream
303*/
305 public:
306 /**
307 * Decompress some data
308 * @param buf in/out parameter which will possibly be resized or swapped
309 * @param offset an offset into buf to begin processing
310 */
311 void update(secure_vector<uint8_t>& buf, size_t offset) final;
312
313 /**
314 * Finish decompressing
315 * @param buf in/out parameter which will possibly be resized or swapped
316 * @param offset an offset into buf to begin processing
317 */
318 void finish(secure_vector<uint8_t>& buf, size_t offset) final;
319
320 /**
321 * Reset the state and abort the current message
322 */
323 void clear() final;
324
325 private:
326 void start() final;
327
328 void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
329
330 virtual std::unique_ptr<Compression_Stream> make_stream() const = 0;
331
332 secure_vector<uint8_t> m_buffer;
333 std::unique_ptr<Compression_Stream> m_stream;
334};
335
336} // namespace Botan
337
338#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
virtual void start(size_t comp_level=0)=0
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0
static std::unique_ptr< Compression_Algorithm > create_or_throw(std::string_view algo_spec)
static std::unique_ptr< Compression_Algorithm > create(std::string_view algo_spec)
virtual void update(secure_vector< uint8_t > &buf, size_t offset=0, bool flush=false)=0
virtual std::string name() const =0
virtual ~Compression_Algorithm()=default
ErrorType error_type() const noexcept override
int error_code() const noexcept override
Compression_Error(const char *func_name, ErrorType type, int rc)
virtual void next_in(uint8_t *b, size_t len)=0
virtual size_t avail_in() const =0
virtual ~Compression_Stream()=default
virtual uint32_t finish_flag() const =0
virtual size_t avail_out() const =0
virtual uint32_t run_flag() const =0
virtual bool run(uint32_t flags)=0
virtual void next_out(uint8_t *b, size_t len)=0
virtual uint32_t flush_flag() const =0
static std::unique_ptr< Decompression_Algorithm > create(std::string_view algo_spec)
virtual ~Decompression_Algorithm()=default
virtual std::string name() const =0
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0
static std::unique_ptr< Decompression_Algorithm > create_or_throw(std::string_view algo_spec)
virtual void update(secure_vector< uint8_t > &buf, size_t offset=0)=0
Exception(std::string_view msg)
Definition exceptn.cpp:71
void update(secure_vector< uint8_t > &buf, size_t offset, bool flush) final
void finish(secure_vector< uint8_t > &buf, size_t offset) final
void update(secure_vector< uint8_t > &buf, size_t offset) final
void finish(secure_vector< uint8_t > &buf, size_t offset) final
Decompression_Algorithm * make_decompressor(std::string_view type)
Compression_Algorithm * make_compressor(std::string_view type)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
ErrorType
Definition exceptn.h:21