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

randombit / botan / 23931252384

03 Apr 2026 02:35AM UTC coverage: 89.461% (-0.06%) from 89.524%
23931252384

Pull #5514

github

web-flow
Merge ac6c66214 into 042e5bd98
Pull Request #5514: Add BER_Decoder::Limits

105548 of 117982 relevant lines covered (89.46%)

11556564.64 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 <botan/internal/mem_utils.h>
15
#include <algorithm>
16
#include <istream>
17

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

22
namespace Botan {
23

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

31
/*
32
* Read a single byte from the DataSource
33
*/
34
std::optional<uint8_t> DataSource::read_byte() {
31,832,868✔
35
   uint8_t b = 0;
31,832,868✔
36
   if(this->read(&b, 1) == 1) {
31,832,868✔
37
      return b;
31,770,381✔
38
   } else {
39
      return {};
62,487✔
40
   }
41
}
42

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

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

57
   while(n > 0) {
380,567✔
58
      const size_t got = this->read(buf, std::min(n, sizeof(buf)));
160,379✔
59
      discarded += got;
147,615✔
60
      n -= got;
147,615✔
61

62
      if(got == 0) {
147,615✔
63
         break;
64
      }
65
   }
66

67
   return discarded;
233,138✔
68
}
69

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

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

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

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

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

105
/*
106
* DataSource_Memory Constructor
107
*/
108
DataSource_Memory::DataSource_Memory(std::string_view in) : DataSource_Memory(as_span_of_bytes(in)) {}
2,679✔
109

110
/*
111
* Read from a stream
112
*/
113
size_t DataSource_Stream::read(uint8_t out[], size_t length) {
18,891,920✔
114
   m_source.read(cast_uint8_ptr_to_char(out), length);
18,891,920✔
115
   if(m_source.bad()) {
18,891,920✔
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,891,920✔
120
   m_total_read += got;
18,891,920✔
121
   return got;
18,891,920✔
122
}
123

124
bool DataSource_Stream::check_available(size_t n) {
1,168✔
125
   const std::streampos orig_pos = m_source.tellg();
1,168✔
126
   m_source.seekg(0, std::ios::end);
1,168✔
127
   const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
1,168✔
128
   m_source.seekg(orig_pos);
1,168✔
129
   return (avail >= n);
1,168✔
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 {
8,270✔
136
   if(end_of_data()) {
8,270✔
137
      throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
×
138
   }
139

140
   size_t got = 0;
8,270✔
141

142
   if(offset > 0) {
8,270✔
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);
8,270✔
153
      if(m_source.bad()) {
8,270✔
154
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
155
      }
156
      got = static_cast<size_t>(m_source.gcount());
8,270✔
157
   }
158

159
   if(m_source.eof()) {
8,270✔
160
      m_source.clear();
1,167✔
161
   }
162
   m_source.seekg(m_total_read, std::ios::beg);
8,270✔
163

164
   return got;
8,270✔
165
}
166

167
/*
168
* Check if the stream is empty or in error
169
*/
170
bool DataSource_Stream::end_of_data() const {
20,924✔
171
   /*
172
   Peek to trigger EOF indicator if positioned at the end of the stream.
173
   Without this, good() returns true even when all data has been read.
174
   */
175
   m_source.peek();
20,924✔
176
   return (!m_source.good());
20,924✔
177
}
178

179
/*
180
* Return a human-readable ID for this stream
181
*/
182
std::string DataSource_Stream::id() const {
×
183
   return m_identifier;
×
184
}
185

186
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
187

188
/*
189
* DataSource_Stream Constructor
190
*/
191
DataSource_Stream::DataSource_Stream(std::string_view path, bool use_binary) :
5,831✔
192
      m_identifier(path),
5,831✔
193
      m_source_memory(std::make_unique<std::ifstream>(std::string(path), use_binary ? std::ios::binary : std::ios::in)),
15,462✔
194
      m_source(*m_source_memory),
5,831✔
195
      m_total_read(0) {
5,831✔
196
   if(!m_source.good()) {
5,831✔
197
      throw Stream_IO_Error(fmt("DataSource: Failure opening file '{}'", path));
2✔
198
   }
199
}
5,832✔
200

201
#endif
202

203
/*
204
* DataSource_Stream Constructor
205
*/
206
DataSource_Stream::DataSource_Stream(std::istream& in, std::string_view name) :
1✔
207
      m_identifier(name), m_source(in), m_total_read(0) {}
1✔
208

209
DataSource_Stream::~DataSource_Stream() = default;
11,661✔
210

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