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

randombit / botan / 23969876719

04 Apr 2026 02:57AM UTC coverage: 89.456% (-0.04%) from 89.494%
23969876719

push

github

web-flow
Merge pull request #5514 from randombit/jack/ber-decoder-limits

Add BER_Decoder::Limits

105558 of 118000 relevant lines covered (89.46%)

11583859.33 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,528,351✔
28
   return read(&out, 1);
29,528,351✔
29
}
30

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

43
/*
44
* Peek a single byte from the DataSource
45
*/
46
size_t DataSource::peek_byte(uint8_t& out) const {
51,054✔
47
   return peek(&out, 1, 0);
51,054✔
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,621,404✔
74
   const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
23,621,404✔
75
   copy_mem(out, m_source.data() + m_offset, got);
23,621,404✔
76
   m_offset += got;
23,621,404✔
77
   return got;
23,621,404✔
78
}
79

80
bool DataSource_Memory::check_available(size_t n) {
5,722,769✔
81
   return (n <= (m_source.size() - m_offset));
5,722,769✔
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 {
238,049✔
88
   const size_t bytes_left = m_source.size() - m_offset;
238,049✔
89
   if(peek_offset >= bytes_left) {
238,049✔
90
      return 0;
91
   }
92

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

98
/*
99
* Check if the memory buffer is empty
100
*/
101
bool DataSource_Memory::end_of_data() const {
433,322✔
102
   return (m_offset == m_source.size());
433,322✔
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,894,619✔
114
   m_source.read(cast_uint8_ptr_to_char(out), length);
18,894,619✔
115
   if(m_source.bad()) {
18,894,619✔
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,894,619✔
120
   m_total_read += got;
18,894,619✔
121
   return got;
18,894,619✔
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,272✔
136
   if(end_of_data()) {
8,272✔
137
      throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
×
138
   }
139

140
   size_t got = 0;
8,272✔
141

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

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

164
   return got;
8,272✔
165
}
166

167
/*
168
* Check if the stream is empty or in error
169
*/
170
bool DataSource_Stream::end_of_data() const {
20,926✔
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,926✔
176
   return (!m_source.good());
20,926✔
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,833✔
192
      m_identifier(path),
5,833✔
193
      m_source_memory(std::make_unique<std::ifstream>(std::string(path), use_binary ? std::ios::binary : std::ios::in)),
15,466✔
194
      m_source(*m_source_memory),
5,833✔
195
      m_total_read(0) {
5,833✔
196
   if(!m_source.good()) {
5,833✔
197
      throw Stream_IO_Error(fmt("DataSource: Failure opening file '{}'", path));
2✔
198
   }
199
}
5,834✔
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,665✔
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