Botan 2.1.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 <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
59 virtual ~Compression_Algorithm() {}
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
100 virtual ~Decompression_Algorithm() {}
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 */
109 class Compression_Stream
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 */
132 class Stream_Compression : public Compression_Algorithm
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 */
155 class Stream_Decompression : public Decompression_Algorithm
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
Botan::Compression_Stream
Definition: compression.h:109
Botan::Stream_Decompression::finish
void finish(secure_vector< uint8_t > &buf, size_t offset) finaloverride
Definition: compress_utils.cpp:180
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:175
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::Stream_Compression::update
void update(secure_vector< uint8_t > &buf, size_t offset, bool flush) finaloverride
Definition: compress_utils.cpp:105
Botan::Stream_Decompression
Definition: compression.h:155
Botan::PKCS11::flags
Flags flags(Flag flags)
Definition: p11.h:858
Botan::Decompression_Algorithm::~Decompression_Algorithm
virtual ~Decompression_Algorithm()
Definition: compression.h:100
Botan::Stream_Compression
Definition: compression.h:132
type
MechanismType type
Definition: p11_mechanism.cpp:43
Botan::make_compressor
Compression_Algorithm * make_compressor(const std::string &name)
Definition: compression.cpp:26
Botan::Decompression_Algorithm
Definition: compression.h:65
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::make_decompressor
Decompression_Algorithm * make_decompressor(const std::string &name)
Definition: compression.cpp:51
Botan::Stream_Compression::finish
void finish(secure_vector< uint8_t > &buf, size_t offset) finaloverride
Definition: compress_utils.cpp:111
Botan
Definition: alg_id.cpp:13
Botan::Compression_Stream::next_out
virtual void next_out(uint8_t *b, size_t len)=0
Botan::Compression_Stream::~Compression_Stream
virtual ~Compression_Stream()
Definition: compression.h:112
Botan::Stream_Compression::clear
void clear() finaloverride
Definition: compress_utils.cpp:55
Botan::Compression_Stream::flush_flag
virtual uint32_t flush_flag() const =0
Botan::Compression_Algorithm
Definition: compression.h:19
Botan::Compression_Stream::finish_flag
virtual uint32_t finish_flag() const =0
Botan::Stream_Decompression::clear
void clear() finaloverride
Definition: compress_utils.cpp:118
Botan::Compression_Stream::run
virtual bool run(uint32_t flags)=0
Botan::Compression_Algorithm::~Compression_Algorithm
virtual ~Compression_Algorithm()
Definition: compression.h:59