Botan 2.19.2
Crypto and TLS for C&
comp_filter.cpp
Go to the documentation of this file.
1/*
2* Filter interface for compression
3* (C) 2014,2015,2016 Jack Lloyd
4* (C) 2015 Matej Kenda
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/filters.h>
10#include <botan/exceptn.h>
11
12#if defined(BOTAN_HAS_COMPRESSION)
13 #include <botan/compression.h>
14#endif
15
16namespace Botan {
17
18#if defined(BOTAN_HAS_COMPRESSION)
19
20Compression_Filter::Compression_Filter(const std::string& type, size_t level, size_t bs) :
21 m_comp(make_compressor(type)),
22 m_buffersize(std::max<size_t>(bs, 256)),
23 m_level(level)
24 {
25 if(!m_comp)
26 {
27 throw Invalid_Argument("Compression type '" + type + "' not found");
28 }
29 }
30
31Compression_Filter::~Compression_Filter() { /* for unique_ptr */ }
32
33std::string Compression_Filter::name() const
34 {
35 return m_comp->name();
36 }
37
38void Compression_Filter::start_msg()
39 {
40 m_comp->start(m_level);
41 }
42
43void Compression_Filter::write(const uint8_t input[], size_t input_length)
44 {
45 while(input_length)
46 {
47 const size_t take = std::min(m_buffersize, input_length);
48 BOTAN_ASSERT(take > 0, "Consumed something");
49
50 m_buffer.assign(input, input + take);
51 m_comp->update(m_buffer);
52
53 send(m_buffer);
54
55 input += take;
56 input_length -= take;
57 }
58 }
59
60void Compression_Filter::flush()
61 {
62 m_buffer.clear();
63 m_comp->update(m_buffer, 0, true);
64 send(m_buffer);
65 }
66
67void Compression_Filter::end_msg()
68 {
69 m_buffer.clear();
70 m_comp->finish(m_buffer);
71 send(m_buffer);
72 }
73
74Decompression_Filter::Decompression_Filter(const std::string& type, size_t bs) :
75 m_comp(make_decompressor(type)),
76 m_buffersize(std::max<size_t>(bs, 256))
77 {
78 if(!m_comp)
79 {
80 throw Invalid_Argument("Compression type '" + type + "' not found");
81 }
82 }
83
84Decompression_Filter::~Decompression_Filter() { /* for unique_ptr */ }
85
86std::string Decompression_Filter::name() const
87 {
88 return m_comp->name();
89 }
90
91void Decompression_Filter::start_msg()
92 {
93 m_comp->start();
94 }
95
96void Decompression_Filter::write(const uint8_t input[], size_t input_length)
97 {
98 while(input_length)
99 {
100 const size_t take = std::min(m_buffersize, input_length);
101 BOTAN_ASSERT(take > 0, "Consumed something");
102
103 m_buffer.assign(input, input + take);
104 m_comp->update(m_buffer);
105
106 send(m_buffer);
107
108 input += take;
109 input_length -= take;
110 }
111 }
112
113void Decompression_Filter::end_msg()
114 {
115 m_buffer.clear();
116 m_comp->finish(m_buffer);
117 send(m_buffer);
118 }
119
120#endif
121
122}
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
std::string name
Definition: alg_id.cpp:13
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: bigint.h:1143
MechanismType type