Botan 3.6.1
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
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
13
14#if defined(BOTAN_HAS_COMPRESSION)
15 #include <botan/compression.h>
16#endif
17
18namespace Botan {
19
20#if defined(BOTAN_HAS_COMPRESSION)
21
22Compression_Filter::Compression_Filter(std::string_view type, size_t level, size_t bs) :
23 m_comp(Compression_Algorithm::create(type)), m_buffersize(std::max<size_t>(bs, 256)), m_level(level) {
24 if(!m_comp) {
25 throw Invalid_Argument(fmt("Compression type '{}' not found", type));
26 }
27}
28
29Compression_Filter::~Compression_Filter() = default;
30
31std::string Compression_Filter::name() const {
32 return m_comp->name();
33}
34
35void Compression_Filter::start_msg() {
36 m_comp->start(m_level);
37}
38
39void Compression_Filter::write(const uint8_t input[], size_t input_length) {
40 while(input_length) {
41 const size_t take = std::min(m_buffersize, input_length);
42 BOTAN_ASSERT(take > 0, "Consumed something");
43
44 m_buffer.assign(input, input + take);
45 m_comp->update(m_buffer);
46
47 send(m_buffer);
48
49 input += take;
50 input_length -= take;
51 }
52}
53
54void Compression_Filter::flush() {
55 m_buffer.clear();
56 m_comp->update(m_buffer, 0, true);
57 send(m_buffer);
58}
59
60void Compression_Filter::end_msg() {
61 m_buffer.clear();
62 m_comp->finish(m_buffer);
63 send(m_buffer);
64}
65
66Decompression_Filter::Decompression_Filter(std::string_view type, size_t bs) :
67 m_comp(Decompression_Algorithm::create(type)), m_buffersize(std::max<size_t>(bs, 256)) {
68 if(!m_comp) {
69 throw Invalid_Argument(fmt("Compression type '{}' not found", type));
70 }
71}
72
73Decompression_Filter::~Decompression_Filter() = default;
74
75std::string Decompression_Filter::name() const {
76 return m_comp->name();
77}
78
79void Decompression_Filter::start_msg() {
80 m_comp->start();
81}
82
83void Decompression_Filter::write(const uint8_t input[], size_t input_length) {
84 while(input_length) {
85 const size_t take = std::min(m_buffersize, input_length);
86 BOTAN_ASSERT(take > 0, "Consumed something");
87
88 m_buffer.assign(input, input + take);
89 m_comp->update(m_buffer);
90
91 send(m_buffer);
92
93 input += take;
94 input_length -= take;
95 }
96}
97
98void Decompression_Filter::end_msg() {
99 m_buffer.clear();
100 m_comp->finish(m_buffer);
101 send(m_buffer);
102}
103
104#endif
105
106} // namespace Botan
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53