Botan 3.6.1
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 <string>
14
15namespace Botan {
16
17/**
18* Interface for a compression algorithm.
19*/
21 public:
22 /**
23 * Create an instance based on a name, or return null if the
24 * algo combination cannot be found.
25 */
26 static std::unique_ptr<Compression_Algorithm> create(std::string_view algo_spec);
27
28 /**
29 * Create an instance based on a name
30 * @param algo_spec algorithm name
31 * Throws Lookup_Error if not found.
32 */
33 static std::unique_ptr<Compression_Algorithm> create_or_throw(std::string_view algo_spec);
34
35 /**
36 * Begin compressing. Most compression algorithms offer a tunable
37 * time/compression tradeoff parameter generally represented by an integer
38 * in the range of 1 to 9. Higher values typically imply better compression
39 * and more memory and/or CPU time consumed by the compression process.
40 *
41 * If 0 or a value out of range is provided, a compression algorithm
42 * specific default is used.
43 *
44 * @param comp_level the desired level of compression (typically from 1 to 9)
45 */
46 virtual void start(size_t comp_level = 0) = 0;
47
48 /**
49 * Process some data.
50 *
51 * The leading @p offset bytes of @p buf are ignored and remain untouched;
52 * this can be useful for ignoring packet headers. If @p flush is true,
53 * the compression state is flushed, allowing the decompressor to recover
54 * the entire message up to this point without having to see the rest of
55 * the compressed stream.
56 *
57 * @param buf in/out parameter which will possibly be resized or swapped
58 * @param offset an offset into blocks to begin processing
59 * @param flush if true the compressor will be told to flush state
60 */
61 virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0, bool flush = false) = 0;
62
63 /**
64 * Finish compressing
65 *
66 * The @p buf and @p offset parameters are treated as in update(). It is
67 * acceptable to call start() followed by finish() with the entire message,
68 * without any intervening call to update().
69 *
70 * @param final_block in/out parameter
71 * @param offset an offset into final_block to begin processing
72 */
73 virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
74
75 /**
76 * @return name of the compression algorithm
77 */
78 virtual std::string name() const = 0;
79
80 /**
81 * Reset the state and abort the current message; start can be
82 * called again to process a new message.
83 */
84 virtual void clear() = 0;
85
86 virtual ~Compression_Algorithm() = default;
87};
88
89/*
90* Interface for a decompression algorithm.
91*/
93 public:
94 /**
95 * Create an instance based on a name, or return null if the
96 * algo combination cannot be found.
97 */
98 static std::unique_ptr<Decompression_Algorithm> create(std::string_view algo_spec);
99
100 /**
101 * Create an instance based on a name
102 * @param algo_spec algorithm name
103 * Throws Lookup_Error if not found.
104 */
105 static std::unique_ptr<Decompression_Algorithm> create_or_throw(std::string_view algo_spec);
106
107 /**
108 * Begin decompressing.
109 *
110 * This initializes the decompression engine and must be done before
111 * calling update() or finish(). No level is provided here; the
112 * decompressor can accept input generated by any compression parameters.
113 */
114 virtual void start() = 0;
115
116 /**
117 * Process some data.
118 * @param buf in/out parameter which will possibly be resized or swapped
119 * @param offset an offset into blocks to begin processing
120 */
121 virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0) = 0;
122
123 /**
124 * Finish decompressing
125 *
126 * Decompress the material in the in/out parameter @p buf. The leading
127 * @p offset bytes of @p buf are ignored and remain untouched; this can
128 * be useful for ignoring packet headers.
129 *
130 * This function may throw if the data seems to be invalid.
131 *
132 * @param final_block in/out parameter
133 * @param offset an offset into final_block to begin processing
134 */
135 virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
136
137 /**
138 * @return name of the decompression algorithm
139 */
140 virtual std::string name() const = 0;
141
142 /**
143 * Reset the state and abort the current message; start can be
144 * called again to process a new message.
145 */
146 virtual void clear() = 0;
147
148 virtual ~Decompression_Algorithm() = default;
149};
150
151BOTAN_DEPRECATED("Use Compression_Algorithm::create")
152
153inline Compression_Algorithm* make_compressor(std::string_view type) {
154 return Compression_Algorithm::create(type).release();
155}
156
157BOTAN_DEPRECATED("Use Decompression_Algorithm::create")
158
159inline Decompression_Algorithm* make_decompressor(std::string_view type) {
160 return Decompression_Algorithm::create(type).release();
161}
162
163/**
164* An error that occurred during compression (or decompression)
165*/
167 public:
168 /**
169 * @param func_name the name of the compression API that was called
170 * (eg "BZ2_bzCompressInit" or "lzma_code")
171 * @param type what library this came from
172 * @param rc the error return code from the compression API. The
173 * interpretation of this value will depend on the library.
174 */
175 Compression_Error(const char* func_name, ErrorType type, int rc);
176
177 ErrorType error_type() const noexcept override { return m_type; }
178
179 int error_code() const noexcept override { return m_rc; }
180
181 private:
182 ErrorType m_type;
183 int m_rc;
184};
185
186/**
187* Adapts a zlib style API
188*/
190 public:
191 virtual ~Compression_Stream() = default;
192
193 virtual void next_in(uint8_t* b, size_t len) = 0;
194
195 virtual void next_out(uint8_t* b, size_t len) = 0;
196
197 virtual size_t avail_in() const = 0;
198
199 virtual size_t avail_out() const = 0;
200
201 virtual uint32_t run_flag() const = 0;
202 virtual uint32_t flush_flag() const = 0;
203 virtual uint32_t finish_flag() const = 0;
204
205 virtual bool run(uint32_t flags) = 0;
206};
207
208/**
209* Used to implement compression using Compression_Stream
210*/
212 public:
213 void update(secure_vector<uint8_t>& buf, size_t offset, bool flush) final;
214
215 void finish(secure_vector<uint8_t>& buf, size_t offset) final;
216
217 void clear() final;
218
219 private:
220 void start(size_t level) final;
221
222 void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
223
224 virtual std::unique_ptr<Compression_Stream> make_stream(size_t level) const = 0;
225
226 secure_vector<uint8_t> m_buffer;
227 std::unique_ptr<Compression_Stream> m_stream;
228};
229
230/**
231* FIXME add doc
232*/
234 public:
235 void update(secure_vector<uint8_t>& buf, size_t offset) final;
236
237 void finish(secure_vector<uint8_t>& buf, size_t offset) final;
238
239 void clear() final;
240
241 private:
242 void start() final;
243
244 void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
245
246 virtual std::unique_ptr<Compression_Stream> make_stream() const = 0;
247
248 secure_vector<uint8_t> m_buffer;
249 std::unique_ptr<Compression_Stream> m_stream;
250};
251
252} // namespace Botan
253
254#endif
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(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
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
virtual void update(secure_vector< uint8_t > &buf, size_t offset=0)=0
void finish(secure_vector< uint8_t > &buf, size_t offset) final
void finish(secure_vector< uint8_t > &buf, size_t offset) final
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
Decompression_Algorithm * make_decompressor(std::string_view type)
Compression_Algorithm * make_compressor(std::string_view type)
ErrorType
Definition exceptn.h:20
const SIMD_8x32 & b
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61