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

randombit / botan / 16094367594

06 Jul 2025 02:24AM UTC coverage: 90.574%. Remained the same
16094367594

push

github

web-flow
Merge pull request #4959 from randombit/jack/fix-clang-tidy-cppcoreguidelines-init-variables

Enable and fix clang-tidy warning cppcoreguidelines-init-variables

99051 of 109359 relevant lines covered (90.57%)

12385300.74 hits per line

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

85.54
/src/lib/utils/data_src.cpp
1
/*
2
* DataSource
3
* (C) 1999-2007 Jack Lloyd
4
*     2005 Matthew Gregan
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/data_src.h>
10

11
#include <botan/exceptn.h>
12
#include <botan/mem_ops.h>
13
#include <botan/internal/fmt.h>
14
#include <algorithm>
15
#include <istream>
16

17
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
18
   #include <fstream>
19
#endif
20

21
namespace Botan {
22

23
/*
24
* Read a single byte from the DataSource
25
*/
26
size_t DataSource::read_byte(uint8_t& out) {
31,487,672✔
27
   return read(&out, 1);
31,487,672✔
28
}
29

30
/*
31
* Read a single byte from the DataSource
32
*/
33
std::optional<uint8_t> DataSource::read_byte() {
32,268,897✔
34
   uint8_t b = 0;
32,268,897✔
35
   if(this->read(&b, 1) == 1) {
32,268,897✔
36
      return b;
32,141,488✔
37
   } else {
38
      return {};
127,409✔
39
   }
40
}
41

42
/*
43
* Peek a single byte from the DataSource
44
*/
45
size_t DataSource::peek_byte(uint8_t& out) const {
58,623✔
46
   return peek(&out, 1, 0);
58,623✔
47
}
48

49
/*
50
* Discard the next N bytes of the data
51
*/
52
size_t DataSource::discard_next(size_t n) {
932,553✔
53
   uint8_t buf[64] = {0};
932,553✔
54
   size_t discarded = 0;
932,553✔
55

56
   while(n > 0) {
1,974,532✔
57
      const size_t got = this->read(buf, std::min(n, sizeof(buf)));
1,447,969✔
58
      discarded += got;
1,043,040✔
59
      n -= got;
1,043,040✔
60

61
      if(got == 0) {
1,043,040✔
62
         break;
63
      }
64
   }
65

66
   return discarded;
932,553✔
67
}
68

69
/*
70
* Read from a memory buffer
71
*/
72
size_t DataSource_Memory::read(uint8_t out[], size_t length) {
26,039,091✔
73
   const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
26,039,091✔
74
   copy_mem(out, m_source.data() + m_offset, got);
26,039,091✔
75
   m_offset += got;
26,039,091✔
76
   return got;
26,039,091✔
77
}
78

79
bool DataSource_Memory::check_available(size_t n) {
5,732,663✔
80
   return (n <= (m_source.size() - m_offset));
5,732,663✔
81
}
82

83
/*
84
* Peek into a memory buffer
85
*/
86
size_t DataSource_Memory::peek(uint8_t out[], size_t length, size_t peek_offset) const {
605,148✔
87
   const size_t bytes_left = m_source.size() - m_offset;
605,148✔
88
   if(peek_offset >= bytes_left) {
605,148✔
89
      return 0;
90
   }
91

92
   const size_t got = std::min(bytes_left - peek_offset, length);
354,447✔
93
   copy_mem(out, &m_source[m_offset + peek_offset], got);
354,447✔
94
   return got;
354,447✔
95
}
96

97
/*
98
* Check if the memory buffer is empty
99
*/
100
bool DataSource_Memory::end_of_data() const {
287,963✔
101
   return (m_offset == m_source.size());
287,963✔
102
}
103

104
/*
105
* DataSource_Memory Constructor
106
*/
107
DataSource_Memory::DataSource_Memory(std::string_view in) :
2,679✔
108
      m_source(cast_char_ptr_to_uint8(in.data()), cast_char_ptr_to_uint8(in.data()) + in.length()), m_offset(0) {}
2,679✔
109

110
/*
111
* Read from a stream
112
*/
113
size_t DataSource_Stream::read(uint8_t out[], size_t length) {
18,527,332✔
114
   m_source.read(cast_uint8_ptr_to_char(out), length);
18,527,332✔
115
   if(m_source.bad()) {
18,527,332✔
116
      throw Stream_IO_Error("DataSource_Stream::read: Source failure");
×
117
   }
118

119
   const size_t got = static_cast<size_t>(m_source.gcount());
18,527,332✔
120
   m_total_read += got;
18,527,332✔
121
   return got;
18,527,332✔
122
}
123

124
bool DataSource_Stream::check_available(size_t n) {
766✔
125
   const std::streampos orig_pos = m_source.tellg();
766✔
126
   m_source.seekg(0, std::ios::end);
766✔
127
   const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
766✔
128
   m_source.seekg(orig_pos);
766✔
129
   return (avail >= n);
766✔
130
}
131

132
/*
133
* Peek into a stream
134
*/
135
size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) const {
7,275✔
136
   if(end_of_data()) {
7,275✔
137
      throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
×
138
   }
139

140
   size_t got = 0;
7,275✔
141

142
   if(offset > 0) {
7,275✔
143
      secure_vector<uint8_t> buf(offset);
×
144
      m_source.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
×
145
      if(m_source.bad()) {
×
146
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
147
      }
148
      got = static_cast<size_t>(m_source.gcount());
×
149
   }
×
150

151
   if(got == offset) {
×
152
      m_source.read(cast_uint8_ptr_to_char(out), length);
7,275✔
153
      if(m_source.bad()) {
7,275✔
154
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
155
      }
156
      got = static_cast<size_t>(m_source.gcount());
7,275✔
157
   }
158

159
   if(m_source.eof()) {
7,275✔
160
      m_source.clear();
966✔
161
   }
162
   m_source.seekg(m_total_read, std::ios::beg);
7,275✔
163

164
   return got;
7,275✔
165
}
166

167
/*
168
* Check if the stream is empty or in error
169
*/
170
bool DataSource_Stream::end_of_data() const {
19,693✔
171
   return (!m_source.good());
19,693✔
172
}
173

174
/*
175
* Return a human-readable ID for this stream
176
*/
177
std::string DataSource_Stream::id() const {
×
178
   return m_identifier;
×
179
}
180

181
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
182

183
/*
184
* DataSource_Stream Constructor
185
*/
186
DataSource_Stream::DataSource_Stream(std::string_view path, bool use_binary) :
5,180✔
187
      m_identifier(path),
5,180✔
188
      m_source_memory(std::make_unique<std::ifstream>(std::string(path), use_binary ? std::ios::binary : std::ios::in)),
14,122✔
189
      m_source(*m_source_memory),
5,180✔
190
      m_total_read(0) {
5,180✔
191
   if(!m_source.good()) {
5,180✔
192
      throw Stream_IO_Error(fmt("DataSource: Failure opening file '{}'", path));
2✔
193
   }
194
}
5,181✔
195

196
#endif
197

198
/*
199
* DataSource_Stream Constructor
200
*/
201
DataSource_Stream::DataSource_Stream(std::istream& in, std::string_view name) :
1✔
202
      m_identifier(name), m_source(in), m_total_read(0) {}
1✔
203

204
DataSource_Stream::~DataSource_Stream() = default;
10,359✔
205

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