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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

84.62
/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/internal/fmt.h>
12
#include <botan/internal/safeint.h>
13
#include <cstdlib>
14

15
namespace Botan {
16

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

20
void* Compression_Alloc_Info::do_malloc(size_t n, size_t size) {
1,148✔
21
   if(!BOTAN_CHECKED_MUL(n, size).has_value()) [[unlikely]] {
1,148✔
22
      return nullptr;
×
23
   }
24

25
   void* ptr = std::calloc(n, size);
1,148✔
26

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

35
   if(ptr) {
1,148✔
36
      m_current_allocs[ptr] = n * size;
1,148✔
37
   }
38

39
   return ptr;
1,148✔
40
}
41

42
void Compression_Alloc_Info::do_free(void* ptr) {
1,268✔
43
   if(ptr) {
1,268✔
44
      auto i = m_current_allocs.find(ptr);
1,148✔
45

46
      if(i == m_current_allocs.end())
1,148✔
47
         throw Internal_Error("Compression_Alloc_Info::free got pointer not allocated by us");
×
48

49
      secure_scrub_memory(ptr, i->second);
1,148✔
50
      std::free(ptr);
1,148✔
51
      m_current_allocs.erase(i);
1,148✔
52
   }
53
}
1,268✔
54

55
void Stream_Compression::clear() { m_stream.reset(); }
×
56

57
void Stream_Compression::start(size_t level) { m_stream = make_stream(level); }
126✔
58

59
void Stream_Compression::process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags) {
279✔
60
   BOTAN_ASSERT(m_stream, "Initialized");
279✔
61
   BOTAN_ASSERT(buf.size() >= offset, "Offset is sane");
279✔
62

63
   // bzip doesn't like being called with no input and BZ_RUN
64
   if(buf.size() == offset && flags == m_stream->run_flag()) {
279✔
65
      return;
66
   }
67

68
   if(m_buffer.size() < buf.size() + offset)
259✔
69
      m_buffer.resize(buf.size() + offset);
106✔
70

71
   // If the output buffer has zero length, .data() might return nullptr. This would
72
   // make some compression algorithms (notably those provided by zlib) fail.
73
   // Any small positive value works fine, but we choose 32 as it is the smallest power
74
   // of two that is large enough to hold all the headers and trailers of the common
75
   // formats, preventing further resizings to make room for output data.
76
   if(m_buffer.size() == 0)
259✔
77
      m_buffer.resize(32);
70✔
78

79
   m_stream->next_in(buf.data() + offset, buf.size() - offset);
259✔
80
   m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
259✔
81

82
   while(true) {
347✔
83
      const bool stream_end = m_stream->run(flags);
347✔
84

85
      if(stream_end) {
347✔
86
         BOTAN_ASSERT(m_stream->avail_in() == 0, "After stream is done, no input remains to be processed");
136✔
87
         m_buffer.resize(m_buffer.size() - m_stream->avail_out());
136✔
88
         break;
136✔
89
      } else if(m_stream->avail_out() == 0) {
211✔
90
         const size_t added = 8 + m_buffer.size();
88✔
91
         m_buffer.resize(m_buffer.size() + added);
88✔
92
         m_stream->next_out(m_buffer.data() + m_buffer.size() - added, added);
88✔
93
      } else if(m_stream->avail_in() == 0) {
123✔
94
         m_buffer.resize(m_buffer.size() - m_stream->avail_out());
123✔
95
         break;
123✔
96
      }
97
   }
98

99
   copy_mem(m_buffer.data(), buf.data(), offset);
259✔
100
   buf.swap(m_buffer);
259✔
101
}
102

103
void Stream_Compression::update(secure_vector<uint8_t>& buf, size_t offset, bool flush) {
153✔
104
   BOTAN_ASSERT(m_stream, "Initialized");
153✔
105
   process(buf, offset, flush ? m_stream->flush_flag() : m_stream->run_flag());
153✔
106
}
153✔
107

108
void Stream_Compression::finish(secure_vector<uint8_t>& buf, size_t offset) {
126✔
109
   BOTAN_ASSERT(m_stream, "Initialized");
126✔
110
   process(buf, offset, m_stream->finish_flag());
126✔
111
   clear();
126✔
112
}
126✔
113

114
void Stream_Decompression::clear() { m_stream.reset(); }
×
115

116
void Stream_Decompression::start() { m_stream = make_stream(); }
103✔
117

118
void Stream_Decompression::process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags) {
103✔
119
   BOTAN_ASSERT(m_stream, "Initialized");
103✔
120
   BOTAN_ASSERT(buf.size() >= offset, "Offset is sane");
103✔
121

122
   if(m_buffer.size() < buf.size() + offset)
103✔
123
      m_buffer.resize(buf.size() + offset);
33✔
124

125
   m_stream->next_in(buf.data() + offset, buf.size() - offset);
103✔
126
   m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
103✔
127

128
   while(true) {
219✔
129
      const bool stream_end = m_stream->run(flags);
219✔
130

131
      if(stream_end) {
219✔
132
         if(m_stream->avail_in() == 0)  // all data consumed?
103✔
133
         {
134
            m_buffer.resize(m_buffer.size() - m_stream->avail_out());
103✔
135
            clear();
103✔
136
            break;
137
         }
138

139
         // More data follows: try to process as a following stream
140
         const size_t read = (buf.size() - offset) - m_stream->avail_in();
×
141
         start();
×
142
         m_stream->next_in(buf.data() + offset + read, buf.size() - offset - read);
×
143
      }
144

145
      if(m_stream->avail_out() == 0) {
116✔
146
         const size_t added = 8 + m_buffer.size();
116✔
147
         m_buffer.resize(m_buffer.size() + added);
116✔
148
         m_stream->next_out(m_buffer.data() + m_buffer.size() - added, added);
116✔
149
      } else if(m_stream->avail_in() == 0) {
×
150
         m_buffer.resize(m_buffer.size() - m_stream->avail_out());
×
151
         break;
×
152
      }
153
   }
154

155
   copy_mem(m_buffer.data(), buf.data(), offset);
103✔
156
   buf.swap(m_buffer);
103✔
157
}
103✔
158

159
void Stream_Decompression::update(secure_vector<uint8_t>& buf, size_t offset) {
103✔
160
   process(buf, offset, m_stream->run_flag());
103✔
161
}
103✔
162

163
void Stream_Decompression::finish(secure_vector<uint8_t>& buf, size_t offset) {
103✔
164
   if(buf.size() != offset || m_stream.get())
103✔
165
      process(buf, offset, m_stream->finish_flag());
×
166

167
   if(m_stream.get())
103✔
168
      throw Invalid_State(fmt("{} finished but not at stream end", name()));
×
169
}
103✔
170

171
}
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

© 2025 Coveralls, Inc