Botan  2.8.0
Crypto and TLS for C++11
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/comp_filter.h>
10 #include <botan/exceptn.h>
11 
12 #if defined(BOTAN_HAS_COMPRESSION)
13  #include <botan/compression.h>
14 #endif
15 
16 namespace Botan {
17 
18 #if defined(BOTAN_HAS_COMPRESSION)
19 
20 Compression_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 
31 Compression_Filter::~Compression_Filter() { /* for unique_ptr */ }
32 
33 std::string Compression_Filter::name() const
34  {
35  return m_comp->name();
36  }
37 
38 void Compression_Filter::start_msg()
39  {
40  m_comp->start(m_level);
41  }
42 
43 void 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 
60 void Compression_Filter::flush()
61  {
62  m_buffer.clear();
63  m_comp->update(m_buffer, 0, true);
64  send(m_buffer);
65  }
66 
67 void Compression_Filter::end_msg()
68  {
69  m_buffer.clear();
70  m_comp->finish(m_buffer);
71  send(m_buffer);
72  }
73 
74 Decompression_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 
84 Decompression_Filter::~Decompression_Filter() { /* for unique_ptr */ }
85 
86 std::string Decompression_Filter::name() const
87  {
88  return m_comp->name();
89  }
90 
91 void Decompression_Filter::start_msg()
92  {
93  m_comp->start();
94  }
95 
96 void 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 
113 void 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 }
Definition: bigint.h:891
MechanismType type
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
std::string name
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