• 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

85.9
/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/internal/fmt.h>
13
#include <algorithm>
14
#include <istream>
15

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

20
namespace Botan {
21

22
/*
23
* Read a single byte from the DataSource
24
*/
25
size_t DataSource::read_byte(uint8_t& out) {
50,166,409✔
26
   return read(&out, 1);
50,166,409✔
27
}
28

29
/*
30
* Peek a single byte from the DataSource
31
*/
32
size_t DataSource::peek_byte(uint8_t& out) const {
41,850✔
33
   return peek(&out, 1, 0);
41,850✔
34
}
35

36
/*
37
* Discard the next N bytes of the data
38
*/
39
size_t DataSource::discard_next(size_t n) {
1,125,552✔
40
   uint8_t buf[64] = {0};
1,125,552✔
41
   size_t discarded = 0;
1,125,552✔
42

43
   while(n) {
2,346,198✔
44
      const size_t got = this->read(buf, std::min(n, sizeof(buf)));
1,647,650✔
45
      discarded += got;
1,221,755✔
46
      n -= got;
1,221,755✔
47

48
      if(got == 0) {
1,221,755✔
49
         break;
50
      }
51
   }
52

53
   return discarded;
1,125,552✔
54
}
55

56
/*
57
* Read from a memory buffer
58
*/
59
size_t DataSource_Memory::read(uint8_t out[], size_t length) {
25,139,485✔
60
   const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
25,139,485✔
61
   copy_mem(out, m_source.data() + m_offset, got);
25,139,485✔
62
   m_offset += got;
25,139,485✔
63
   return got;
25,139,485✔
64
}
65

66
bool DataSource_Memory::check_available(size_t n) {
5,512,107✔
67
   return (n <= (m_source.size() - m_offset));
5,512,107✔
68
}
69

70
/*
71
* Peek into a memory buffer
72
*/
73
size_t DataSource_Memory::peek(uint8_t out[], size_t length, size_t peek_offset) const {
656,010✔
74
   const size_t bytes_left = m_source.size() - m_offset;
656,010✔
75
   if(peek_offset >= bytes_left) {
656,010✔
76
      return 0;
77
   }
78

79
   const size_t got = std::min(bytes_left - peek_offset, length);
357,059✔
80
   copy_mem(out, &m_source[m_offset + peek_offset], got);
357,059✔
81
   return got;
357,059✔
82
}
83

84
/*
85
* Check if the memory buffer is empty
86
*/
87
bool DataSource_Memory::end_of_data() const {
940,807✔
88
   return (m_offset == m_source.size());
940,807✔
89
}
90

91
/*
92
* DataSource_Memory Constructor
93
*/
94
DataSource_Memory::DataSource_Memory(std::string_view in) :
2,464✔
95
      m_source(cast_char_ptr_to_uint8(in.data()), cast_char_ptr_to_uint8(in.data()) + in.length()), m_offset(0) {}
2,464✔
96

97
/*
98
* Read from a stream
99
*/
100
size_t DataSource_Stream::read(uint8_t out[], size_t length) {
12,257,891✔
101
   m_source.read(cast_uint8_ptr_to_char(out), length);
12,257,891✔
102
   if(m_source.bad()) {
12,257,891✔
103
      throw Stream_IO_Error("DataSource_Stream::read: Source failure");
×
104
   }
105

106
   const size_t got = static_cast<size_t>(m_source.gcount());
12,257,891✔
107
   m_total_read += got;
12,257,891✔
108
   return got;
12,257,891✔
109
}
110

111
bool DataSource_Stream::check_available(size_t n) {
730✔
112
   const std::streampos orig_pos = m_source.tellg();
730✔
113
   m_source.seekg(0, std::ios::end);
730✔
114
   const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
730✔
115
   m_source.seekg(orig_pos);
730✔
116
   return (avail >= n);
730✔
117
}
118

119
/*
120
* Peek into a stream
121
*/
122
size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) const {
5,458✔
123
   if(end_of_data()) {
5,458✔
124
      throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
×
125
   }
126

127
   size_t got = 0;
5,458✔
128

129
   if(offset) {
5,458✔
130
      secure_vector<uint8_t> buf(offset);
×
131
      m_source.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
×
132
      if(m_source.bad()) {
×
133
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
134
      }
135
      got = static_cast<size_t>(m_source.gcount());
×
136
   }
×
137

138
   if(got == offset) {
5,458✔
139
      m_source.read(cast_uint8_ptr_to_char(out), length);
5,458✔
140
      if(m_source.bad()) {
5,458✔
141
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
142
      }
143
      got = static_cast<size_t>(m_source.gcount());
5,458✔
144
   }
145

146
   if(m_source.eof()) {
5,458✔
147
      m_source.clear();
930✔
148
   }
149
   m_source.seekg(m_total_read, std::ios::beg);
5,458✔
150

151
   return got;
5,458✔
152
}
153

154
/*
155
* Check if the stream is empty or in error
156
*/
157
bool DataSource_Stream::end_of_data() const {
14,665✔
158
   return (!m_source.good());
14,665✔
159
}
160

161
/*
162
* Return a human-readable ID for this stream
163
*/
164
std::string DataSource_Stream::id() const {
×
165
   return m_identifier;
×
166
}
167

168
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
169

170
/*
171
* DataSource_Stream Constructor
172
*/
173
DataSource_Stream::DataSource_Stream(std::string_view path, bool use_binary) :
3,450✔
174
      m_identifier(path),
3,450✔
175
      m_source_memory(std::make_unique<std::ifstream>(std::string(path), use_binary ? std::ios::binary : std::ios::in)),
9,040✔
176
      m_source(*m_source_memory),
3,450✔
177
      m_total_read(0) {
3,450✔
178
   if(!m_source.good()) {
3,450✔
179
      throw Stream_IO_Error(fmt("DataSource: Failure opening file '{}'", path));
2✔
180
   }
181
}
3,451✔
182

183
#endif
184

185
/*
186
* DataSource_Stream Constructor
187
*/
188
DataSource_Stream::DataSource_Stream(std::istream& in, std::string_view name) :
1✔
189
      m_identifier(name), m_source(in), m_total_read(0) {}
1✔
190

191
DataSource_Stream::~DataSource_Stream() = default;
6,899✔
192

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