Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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