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