Botan 3.0.0-alpha0
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(const std::string& 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(const std::string& 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(const std::string& 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(const std::string& 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
134inline Compression_Algorithm* make_compressor(const std::string& type)
135 {
136 return Compression_Algorithm::create(type).release();
137 }
138
140 {
141 return Decompression_Algorithm::create(type).release();
142 }
143
144/**
145* An error that occurred during compression (or decompression)
146*/
148 {
149 public:
150
151 /**
152 * @param func_name the name of the compression API that was called
153 * (eg "BZ2_bzCompressInit" or "lzma_code")
154 * @param type what library this came from
155 * @param rc the error return code from the compression API. The
156 * interpretation of this value will depend on the library.
157 */
158 Compression_Error(const char* func_name, ErrorType type, int rc) :
159 Exception("Compression API " + std::string(func_name) +
160 " failed with return code " + std::to_string(rc)),
161 m_type(type),
162 m_rc(rc)
163 {}
164
165 ErrorType error_type() const noexcept override { return m_type; }
166
167 int error_code() const noexcept override { return m_rc; }
168
169 private:
170 ErrorType m_type;
171 int m_rc;
172 };
173
174/**
175* Adapts a zlib style API
176*/
178 {
179 public:
180 virtual ~Compression_Stream() = default;
181
182 virtual void next_in(uint8_t* b, size_t len) = 0;
183
184 virtual void next_out(uint8_t* b, size_t len) = 0;
185
186 virtual size_t avail_in() const = 0;
187
188 virtual size_t avail_out() const = 0;
189
190 virtual uint32_t run_flag() const = 0;
191 virtual uint32_t flush_flag() const = 0;
192 virtual uint32_t finish_flag() const = 0;
193
194 virtual bool run(uint32_t flags) = 0;
195 };
196
197/**
198* Used to implement compression using Compression_Stream
199*/
201 {
202 public:
203 void update(secure_vector<uint8_t>& buf, size_t offset, bool flush) final override;
204
205 void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
206
207 void clear() final override;
208
209 private:
210 void start(size_t level) final override;
211
212 void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
213
214 virtual std::unique_ptr<Compression_Stream> make_stream(size_t level) const = 0;
215
216 secure_vector<uint8_t> m_buffer;
217 std::unique_ptr<Compression_Stream> m_stream;
218 };
219
220/**
221* FIXME add doc
222*/
224 {
225 public:
226 void update(secure_vector<uint8_t>& buf, size_t offset) final override;
227
228 void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
229
230 void clear() final override;
231
232 private:
233 void start() final override;
234
235 void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
236
237 virtual std::unique_ptr<Compression_Stream> make_stream() const = 0;
238
239 secure_vector<uint8_t> m_buffer;
240 std::unique_ptr<Compression_Stream> m_stream;
241 };
242
243}
244
245#endif
static std::unique_ptr< Compression_Algorithm > create(const std::string &algo_spec)
Definition: compression.cpp:29
virtual void start(size_t comp_level=0)=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, bool flush=false)=0
virtual std::string name() const =0
virtual ~Compression_Algorithm()=default
ErrorType error_type() const noexcept override
Definition: compression.h:165
int error_code() const noexcept override
Definition: compression.h:167
Compression_Error(const char *func_name, ErrorType type, int rc)
Definition: compression.h:158
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(const std::string &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 update(secure_vector< uint8_t > &buf, size_t offset, bool flush) final override
void finish(secure_vector< uint8_t > &buf, size_t offset) final override
void clear() final override
void update(secure_vector< uint8_t > &buf, size_t offset) final override
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
PolynomialVector b
Definition: kyber.cpp:821
Flags flags(Flag flags)
Definition: p11.h:860
Definition: alg_id.cpp:13
Decompression_Algorithm * make_decompressor(const std::string &type)
Definition: compression.h:139
ErrorType
Definition: exceptn.h:20
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
Compression_Algorithm * make_compressor(const std::string &type)
Definition: compression.h:134
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
Definition: bigint.h:1077
MechanismType type