Botan  2.6.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 <string>
13 
14 namespace Botan {
15 
16 /*
17 * Interface for a compression algorithm.
18 */
20  {
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>
27  create(const std::string& algo_spec);
28 
29  /**
30  * Create an instance based on a name
31  * @param algo_spec algorithm name
32  * Throws Lookup_Error if not found.
33  */
34  static std::unique_ptr<Compression_Algorithm>
35  create_or_throw(const std::string& algo_spec);
36 
37  /**
38  * Begin compressing. Most compression algorithms offer a tunable
39  * time/compression tradeoff parameter generally represented by
40  * an integer in the range of 1 to 9.
41  *
42  * If 0 or a value out of range is provided, a compression algorithm
43  * specific default is used.
44  */
45  virtual void start(size_t comp_level = 0) = 0;
46 
47  /**
48  * Process some data.
49  * @param buf in/out parameter which will possibly be resized or swapped
50  * @param offset an offset into blocks to begin processing
51  * @param flush if true the compressor will be told to flush state
52  */
53  virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0, bool flush = false) = 0;
54 
55  /**
56  * Finish compressing
57  *
58  * @param final_block in/out parameter
59  * @param offset an offset into final_block to begin processing
60  */
61  virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
62 
63  /**
64  * @return name of the compression algorithm
65  */
66  virtual std::string name() const = 0;
67 
68  /**
69  * Reset the state and abort the current message; start can be
70  * called again to process a new message.
71  */
72  virtual void clear() = 0;
73 
74  virtual ~Compression_Algorithm() = default;
75  };
76 
77 /*
78 * Interface for a decompression algorithm.
79 */
81  {
82  public:
83  /**
84  * Create an instance based on a name, or return null if the
85  * algo combination cannot be found.
86  */
87  static std::unique_ptr<Decompression_Algorithm>
88  create(const std::string& algo_spec);
89 
90  /**
91  * Create an instance based on a name
92  * @param algo_spec algorithm name
93  * Throws Lookup_Error if not found.
94  */
95  static std::unique_ptr<Decompression_Algorithm>
96  create_or_throw(const std::string& algo_spec);
97 
98  /**
99  * Begin decompressing.
100  * Decompression does not support levels, as compression does.
101  */
102  virtual void start() = 0;
103 
104  /**
105  * Process some data.
106  * @param buf in/out parameter which will possibly be resized or swapped
107  * @param offset an offset into blocks to begin processing
108  */
109  virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0) = 0;
110 
111  /**
112  * Finish decompressing
113  *
114  * @param final_block in/out parameter
115  * @param offset an offset into final_block to begin processing
116  */
117  virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
118 
119  /**
120  * @return name of the decompression algorithm
121  */
122  virtual std::string name() const = 0;
123 
124  /**
125  * Reset the state and abort the current message; start can be
126  * called again to process a new message.
127  */
128  virtual void clear() = 0;
129 
130  virtual ~Decompression_Algorithm() = default;
131  };
132 
135 
136 /**
137 * Adapts a zlib style API
138 */
140  {
141  public:
142  virtual ~Compression_Stream() = default;
143 
144  virtual void next_in(uint8_t* b, size_t len) = 0;
145 
146  virtual void next_out(uint8_t* b, size_t len) = 0;
147 
148  virtual size_t avail_in() const = 0;
149 
150  virtual size_t avail_out() const = 0;
151 
152  virtual uint32_t run_flag() const = 0;
153  virtual uint32_t flush_flag() const = 0;
154  virtual uint32_t finish_flag() const = 0;
155 
156  virtual bool run(uint32_t flags) = 0;
157  };
158 
159 /**
160 * Used to implement compression using Compression_Stream
161 */
163  {
164  public:
165  void update(secure_vector<uint8_t>& buf, size_t offset, bool flush) final override;
166 
167  void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
168 
169  void clear() final override;
170 
171  private:
172  void start(size_t level) final override;
173 
174  void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
175 
176  virtual Compression_Stream* make_stream(size_t level) const = 0;
177 
178  secure_vector<uint8_t> m_buffer;
179  std::unique_ptr<Compression_Stream> m_stream;
180  };
181 
182 /**
183 * FIXME add doc
184 */
186  {
187  public:
188  void update(secure_vector<uint8_t>& buf, size_t offset) final override;
189 
190  void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
191 
192  void clear() final override;
193 
194  private:
195  void start() final override;
196 
197  void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
198 
199  virtual Compression_Stream* make_stream() const = 0;
200 
201  secure_vector<uint8_t> m_buffer;
202  std::unique_ptr<Compression_Stream> m_stream;
203  };
204 
205 }
206 
207 #endif
void update(secure_vector< uint8_t > &buf, size_t offset, bool flush) final override
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Flags flags(Flag flags)
Definition: p11.h:858
Definition: bigint.h:719
MechanismType type
void finish(secure_vector< uint8_t > &buf, size_t offset) final override
void clear() final override
Decompression_Algorithm * make_decompressor(const std::string &name)
Definition: compression.cpp:71
Compression_Algorithm * make_compressor(const std::string &name)
Definition: compression.cpp:27
Definition: alg_id.cpp:13
void update(secure_vector< uint8_t > &buf, size_t offset) final override
void clear() final override
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
void finish(secure_vector< uint8_t > &buf, size_t offset) final override