• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 24011819536

05 Apr 2026 10:23PM UTC coverage: 89.447% (-0.007%) from 89.454%
24011819536

Pull #5521

github

web-flow
Merge 8a2a3cca2 into 417709dd7
Pull Request #5521: Rollup of small fixes

105807 of 118290 relevant lines covered (89.45%)

11758282.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.19
/src/lib/compression/compress_utils.cpp
1
/*
2
* Compression Utils
3
* (C) 2014,2016 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/internal/compress_utils.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/mem_ops.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/int_utils.h>
14
#include <cstdlib>
15

16
namespace Botan {
17

18
Compression_Error::Compression_Error(const char* func_name, ErrorType type, int rc) :
×
19
      Exception(fmt("Compression API {} failed with return code {}", func_name, rc)), m_type(type), m_rc(rc) {}
×
20

21
void* Compression_Alloc_Info::do_malloc(size_t n, size_t size) {
1,133✔
22
   // Precheck for integer overflow in the multiplication
23
   // before passing to calloc, which may or may not check.
24
   if(!checked_mul(n, size)) {
2,266✔
25
      return nullptr;
26
   }
27

28
   void* ptr = std::calloc(n, size);  // NOLINT(*-no-malloc,*-owning-memory,*-const-correctness)
1,133✔
29

30
   /*
31
   * Return null rather than throwing here as we are being called by a
32
   * C library and it may not be possible for an exception to unwind
33
   * the call stack from here. The compression library is expecting a
34
   * function written in C and a null return on error, which it will
35
   * send upwards to the compression wrappers.
36
   */
37

38
   if(ptr != nullptr) {
1,133✔
39
      m_current_allocs[ptr] = n * size;
1,133✔
40
   }
41

42
   return ptr;
1,133✔
43
}
44

45
void Compression_Alloc_Info::do_free(void* ptr) {
1,205✔
46
   if(ptr != nullptr) {
1,205✔
47
      auto i = m_current_allocs.find(ptr);
1,133✔
48

49
      if(i == m_current_allocs.end()) {
1,133✔
50
         throw Internal_Error("Compression_Alloc_Info::free got pointer not allocated by us");
×
51
      }
52

53
      secure_scrub_memory(ptr, i->second);
1,133✔
54
      std::free(ptr);  // NOLINT(*-no-malloc,*-owning-memory)
1,133✔
55
      m_current_allocs.erase(i);
1,133✔
56
   }
57
}
1,205✔
58

59
void Stream_Compression::clear() {
123✔
60
   m_stream.reset();
×
61
}
×
62

63
void Stream_Compression::start(size_t level) {
123✔
64
   m_stream = make_stream(level);
123✔
65
}
123✔
66

67
void Stream_Compression::process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags) {
276✔
68
   BOTAN_ASSERT(m_stream, "Initialized");
276✔
69
   BOTAN_ASSERT(buf.size() >= offset, "Offset is sane");
276✔
70

71
   // bzip doesn't like being called with no input and BZ_RUN
72
   if(buf.size() == offset && flags == m_stream->run_flag()) {
276✔
73
      return;
74
   }
75

76
   if(m_buffer.size() < buf.size() + offset) {
256✔
77
      m_buffer.resize(buf.size() + offset);
103✔
78
   }
79

80
   // If the output buffer has zero length, .data() might return nullptr. This would
81
   // make some compression algorithms (notably those provided by zlib) fail.
82
   // Any small positive value works fine, but we choose 32 as it is the smallest power
83
   // of two that is large enough to hold all the headers and trailers of the common
84
   // formats, preventing further resizings to make room for output data.
85
   if(m_buffer.empty()) {
256✔
86
      m_buffer.resize(32);
70✔
87
   }
88

89
   m_stream->next_in(buf.data() + offset, buf.size() - offset);
256✔
90
   m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
256✔
91

92
   while(true) {
341✔
93
      const bool stream_end = m_stream->run(flags);
341✔
94

95
      if(stream_end) {
341✔
96
         BOTAN_ASSERT(m_stream->avail_in() == 0, "After stream is done, no input remains to be processed");
133✔
97
         m_buffer.resize(m_buffer.size() - m_stream->avail_out());
133✔
98
         break;
133✔
99
      } else if(m_stream->avail_out() == 0) {
208✔
100
         const size_t added = 8 + m_buffer.size();
85✔
101
         m_buffer.resize(m_buffer.size() + added);
85✔
102
         m_stream->next_out(m_buffer.data() + m_buffer.size() - added, added);
85✔
103
      } else if(m_stream->avail_in() == 0) {
123✔
104
         m_buffer.resize(m_buffer.size() - m_stream->avail_out());
123✔
105
         break;
123✔
106
      }
107
   }
108

109
   copy_mem(m_buffer.data(), buf.data(), offset);
256✔
110
   buf.swap(m_buffer);
256✔
111
}
112

113
void Stream_Compression::update(secure_vector<uint8_t>& buf, size_t offset, bool flush) {
153✔
114
   BOTAN_ASSERT(m_stream, "Initialized");
153✔
115
   process(buf, offset, flush ? m_stream->flush_flag() : m_stream->run_flag());
153✔
116
}
153✔
117

118
void Stream_Compression::finish(secure_vector<uint8_t>& buf, size_t offset) {
123✔
119
   BOTAN_ASSERT(m_stream, "Initialized");
123✔
120
   process(buf, offset, m_stream->finish_flag());
123✔
121
   clear();
123✔
122
}
123✔
123

124
void Stream_Decompression::clear() {
103✔
125
   m_stream.reset();
×
126
}
×
127

128
void Stream_Decompression::start() {
103✔
129
   m_stream = make_stream();
103✔
130
}
103✔
131

132
void Stream_Decompression::process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags) {
103✔
133
   BOTAN_ASSERT(m_stream, "Initialized");
103✔
134
   BOTAN_ASSERT(buf.size() >= offset, "Offset is sane");
103✔
135

136
   if(m_buffer.size() < buf.size() + offset) {
103✔
137
      m_buffer.resize(buf.size() + offset);
33✔
138
   }
139

140
   m_stream->next_in(buf.data() + offset, buf.size() - offset);
103✔
141
   m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
103✔
142

143
   while(true) {
219✔
144
      const bool stream_end = m_stream->run(flags);
219✔
145

146
      if(stream_end) {
219✔
147
         if(m_stream->avail_in() == 0) {
103✔
148
            // all data consumed
149
            m_buffer.resize(m_buffer.size() - m_stream->avail_out());
103✔
150
            clear();
103✔
151
            break;
152
         }
153

154
         // More data follows: try to process as a following stream
155
         const size_t read = (buf.size() - offset) - m_stream->avail_in();
×
156
         start();
×
157
         m_stream->next_in(buf.data() + offset + read, buf.size() - offset - read);
×
158
      }
159

160
      if(m_stream->avail_out() == 0) {
116✔
161
         const size_t added = 8 + m_buffer.size();
116✔
162
         m_buffer.resize(m_buffer.size() + added);
116✔
163
         m_stream->next_out(m_buffer.data() + m_buffer.size() - added, added);
116✔
164
      } else if(m_stream->avail_in() == 0) {
×
165
         m_buffer.resize(m_buffer.size() - m_stream->avail_out());
×
166
         break;
×
167
      }
168
   }
169

170
   copy_mem(m_buffer.data(), buf.data(), offset);
103✔
171
   buf.swap(m_buffer);
103✔
172
}
103✔
173

174
void Stream_Decompression::update(secure_vector<uint8_t>& buf, size_t offset) {
103✔
175
   if(!m_stream) {
103✔
176
      if(buf.size() == offset) {
×
177
         return;
178
      }
179
      throw Invalid_State(fmt("{}: got additional data after end of compressed stream", name()));
×
180
   }
181
   process(buf, offset, m_stream->run_flag());
103✔
182
}
183

184
void Stream_Decompression::finish(secure_vector<uint8_t>& buf, size_t offset) {
103✔
185
   if(!m_stream) {
103✔
186
      if(buf.size() != offset) {
103✔
187
         throw Invalid_State(fmt("{}: got additional data after end of compressed stream", name()));
×
188
      }
189
      return;
190
   }
191

192
   process(buf, offset, m_stream->finish_flag());
×
193

194
   if(m_stream) {
×
195
      throw Invalid_State(fmt("{} finished but not at stream end", name()));
×
196
   }
197
}
198

199
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc