Botan  2.9.0
Crypto and TLS for C++11
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 */
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 
136 
137 /**
138 * An error that occurred during compression (or decompression)
139 */
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 */
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 */
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 */
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
virtual void next_in(uint8_t *b, size_t len)=0
virtual size_t avail_in() const =0
void update(secure_vector< uint8_t > &buf, size_t offset, bool flush) final override
virtual size_t avail_out() const =0
virtual uint32_t run_flag() const =0
Compression_Error(const char *func_name, ErrorType type, int rc)
Definition: compression.h:151
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Flags flags(Flag flags)
Definition: p11.h:858
Definition: bigint.h:1125
ErrorType error_type() const noexcept override
Definition: compression.h:158
MechanismType type
void finish(secure_vector< uint8_t > &buf, size_t offset) final override
int error_code() const noexcept override
Definition: compression.h:160
std::string name
void clear() final override
Decompression_Algorithm * make_decompressor(const std::string &name)
Definition: compression.cpp:71
ErrorType
Definition: exceptn.h:20
Compression_Algorithm * make_compressor(const std::string &name)
Definition: compression.cpp:27
Definition: alg_id.cpp:13
virtual void next_out(uint8_t *b, size_t len)=0
void update(secure_vector< uint8_t > &buf, size_t offset) final override
int(* update)(CTX *, const void *, CC_LONG len)
std::string to_string(const secure_vector< uint8_t > &bytes)
Definition: stl_util.h:25
void clear() final override
virtual uint32_t flush_flag() const =0
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
virtual uint32_t finish_flag() const =0
virtual ~Compression_Stream()=default
virtual bool run(uint32_t flags)=0
void finish(secure_vector< uint8_t > &buf, size_t offset) final override