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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

83.84
/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() {
126✔
56
   m_stream.reset();
×
57
}
×
58

59
void Stream_Compression::start(size_t level) {
126✔
60
   m_stream = make_stream(level);
126✔
61
}
126✔
62

63
void Stream_Compression::process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags) {
279✔
64
   BOTAN_ASSERT(m_stream, "Initialized");
279✔
65
   BOTAN_ASSERT(buf.size() >= offset, "Offset is sane");
279✔
66

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

72
   if(m_buffer.size() < buf.size() + offset)
259✔
73
      m_buffer.resize(buf.size() + offset);
106✔
74

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

83
   m_stream->next_in(buf.data() + offset, buf.size() - offset);
259✔
84
   m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
259✔
85

86
   while(true) {
347✔
87
      const bool stream_end = m_stream->run(flags);
347✔
88

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

103
   copy_mem(m_buffer.data(), buf.data(), offset);
259✔
104
   buf.swap(m_buffer);
259✔
105
}
106

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

112
void Stream_Compression::finish(secure_vector<uint8_t>& buf, size_t offset) {
126✔
113
   BOTAN_ASSERT(m_stream, "Initialized");
126✔
114
   process(buf, offset, m_stream->finish_flag());
126✔
115
   clear();
126✔
116
}
126✔
117

118
void Stream_Decompression::clear() {
103✔
119
   m_stream.reset();
×
120
}
×
121

122
void Stream_Decompression::start() {
103✔
123
   m_stream = make_stream();
103✔
124
}
103✔
125

126
void Stream_Decompression::process(secure_vector<uint8_t>& buf, size_t offset, uint32_t flags) {
103✔
127
   BOTAN_ASSERT(m_stream, "Initialized");
103✔
128
   BOTAN_ASSERT(buf.size() >= offset, "Offset is sane");
103✔
129

130
   if(m_buffer.size() < buf.size() + offset)
103✔
131
      m_buffer.resize(buf.size() + offset);
33✔
132

133
   m_stream->next_in(buf.data() + offset, buf.size() - offset);
103✔
134
   m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
103✔
135

136
   while(true) {
219✔
137
      const bool stream_end = m_stream->run(flags);
219✔
138

139
      if(stream_end) {
219✔
140
         if(m_stream->avail_in() == 0)  // all data consumed?
103✔
141
         {
142
            m_buffer.resize(m_buffer.size() - m_stream->avail_out());
103✔
143
            clear();
103✔
144
            break;
145
         }
146

147
         // More data follows: try to process as a following stream
148
         const size_t read = (buf.size() - offset) - m_stream->avail_in();
×
149
         start();
×
150
         m_stream->next_in(buf.data() + offset + read, buf.size() - offset - read);
×
151
      }
152

153
      if(m_stream->avail_out() == 0) {
116✔
154
         const size_t added = 8 + m_buffer.size();
116✔
155
         m_buffer.resize(m_buffer.size() + added);
116✔
156
         m_stream->next_out(m_buffer.data() + m_buffer.size() - added, added);
116✔
157
      } else if(m_stream->avail_in() == 0) {
×
158
         m_buffer.resize(m_buffer.size() - m_stream->avail_out());
×
159
         break;
×
160
      }
161
   }
162

163
   copy_mem(m_buffer.data(), buf.data(), offset);
103✔
164
   buf.swap(m_buffer);
103✔
165
}
103✔
166

167
void Stream_Decompression::update(secure_vector<uint8_t>& buf, size_t offset) {
103✔
168
   process(buf, offset, m_stream->run_flag());
103✔
169
}
103✔
170

171
void Stream_Decompression::finish(secure_vector<uint8_t>& buf, size_t offset) {
103✔
172
   if(buf.size() != offset || m_stream.get())
103✔
173
      process(buf, offset, m_stream->finish_flag());
×
174

175
   if(m_stream.get())
103✔
176
      throw Invalid_State(fmt("{} finished but not at stream end", name()));
×
177
}
103✔
178

179
}  // 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

© 2025 Coveralls, Inc