Botan  2.1.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 */
19 class BOTAN_DLL Compression_Algorithm
20  {
21  public:
22  /**
23  * Begin compressing. Most compression algorithms offer a tunable
24  * time/compression tradeoff parameter generally represented by
25  * an integer in the range of 1 to 9.
26  *
27  * If 0 or a value out of range is provided, a compression algorithm
28  * specific default is used.
29  */
30  virtual void start(size_t comp_level = 0) = 0;
31 
32  /**
33  * Process some data. Input must be in size update_granularity() uint8_t blocks.
34  * @param buf in/out parameter which will possibly be resized or swapped
35  * @param offset an offset into blocks to begin processing
36  * @param flush if true the compressor will be told to flush state
37  */
38  virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0, bool flush = false) = 0;
39 
40  /**
41  * Finish compressing
42  *
43  * @param final_block in/out parameter
44  * @param offset an offset into final_block to begin processing
45  */
46  virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
47 
48  /**
49  * @return name of the compression algorithm
50  */
51  virtual std::string name() const = 0;
52 
53  /**
54  * Reset the state and abort the current message; start can be
55  * called again to process a new message.
56  */
57  virtual void clear() = 0;
58 
60  };
61 
62 /*
63 * Interface for a decompression algorithm.
64 */
65 class BOTAN_DLL Decompression_Algorithm
66  {
67  public:
68  /**
69  * Begin decompressing.
70  * Decompression does not support levels, as compression does.
71  */
72  virtual void start() = 0;
73 
74  /**
75  * Process some data. Input must be in size update_granularity() uint8_t blocks.
76  * @param buf in/out parameter which will possibly be resized or swapped
77  * @param offset an offset into blocks to begin processing
78  */
79  virtual void update(secure_vector<uint8_t>& buf, size_t offset = 0) = 0;
80 
81  /**
82  * Finish decompressing
83  *
84  * @param final_block in/out parameter
85  * @param offset an offset into final_block to begin processing
86  */
87  virtual void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) = 0;
88 
89  /**
90  * @return name of the decompression algorithm
91  */
92  virtual std::string name() const = 0;
93 
94  /**
95  * Reset the state and abort the current message; start can be
96  * called again to process a new message.
97  */
98  virtual void clear() = 0;
99 
101  };
102 
103 BOTAN_DLL Compression_Algorithm* make_compressor(const std::string& type);
104 BOTAN_DLL Decompression_Algorithm* make_decompressor(const std::string& type);
105 
106 /**
107 * Adapts a zlib style API
108 */
110  {
111  public:
112  virtual ~Compression_Stream() {}
113 
114  virtual void next_in(uint8_t* b, size_t len) = 0;
115 
116  virtual void next_out(uint8_t* b, size_t len) = 0;
117 
118  virtual size_t avail_in() const = 0;
119 
120  virtual size_t avail_out() const = 0;
121 
122  virtual uint32_t run_flag() const = 0;
123  virtual uint32_t flush_flag() const = 0;
124  virtual uint32_t finish_flag() const = 0;
125 
126  virtual bool run(uint32_t flags) = 0;
127  };
128 
129 /**
130 * Used to implement compression using Compression_Stream
131 */
133  {
134  public:
135  void update(secure_vector<uint8_t>& buf, size_t offset, bool flush) final override;
136 
137  void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
138 
139  void clear() final override;
140 
141  private:
142  void start(size_t level) final override;
143 
144  void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
145 
146  virtual Compression_Stream* make_stream(size_t level) const = 0;
147 
148  secure_vector<uint8_t> m_buffer;
149  std::unique_ptr<Compression_Stream> m_stream;
150  };
151 
152 /**
153 * FIXME add doc
154 */
156  {
157  public:
158  void update(secure_vector<uint8_t>& buf, size_t offset) final override;
159 
160  void finish(secure_vector<uint8_t>& buf, size_t offset) final override;
161 
162  void clear() final override;
163 
164  private:
165  void start() final override;
166 
167  void process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags);
168 
169  virtual Compression_Stream* make_stream() const = 0;
170 
171  secure_vector<uint8_t> m_buffer;
172  std::unique_ptr<Compression_Stream> m_stream;
173  };
174 
175 }
176 
177 #endif
void finish(secure_vector< uint8_t > &buf, size_t offset) finaloverride
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) finaloverride
virtual size_t avail_out() const =0
virtual uint32_t run_flag() const =0
void update(secure_vector< uint8_t > &buf, size_t offset, bool flush) finaloverride
Flags flags(Flag flags)
Definition: p11.h:858
MechanismType type
Compression_Algorithm * make_compressor(const std::string &name)
Definition: compression.cpp:26
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Decompression_Algorithm * make_decompressor(const std::string &name)
Definition: compression.cpp:51
void finish(secure_vector< uint8_t > &buf, size_t offset) finaloverride
Definition: alg_id.cpp:13
virtual void next_out(uint8_t *b, size_t len)=0
void clear() finaloverride
virtual uint32_t flush_flag() const =0
virtual uint32_t finish_flag() const =0
void clear() finaloverride
virtual bool run(uint32_t flags)=0