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