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