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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

86.11
/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) { return read(&out, 1); }
46,884,195✔
26

27
/*
28
* Peek a single byte from the DataSource
29
*/
30
size_t DataSource::peek_byte(uint8_t& out) const { return peek(&out, 1, 0); }
41,745✔
31

32
/*
33
* Discard the next N bytes of the data
34
*/
35
size_t DataSource::discard_next(size_t n) {
673,989✔
36
   uint8_t buf[64] = {0};
673,989✔
37
   size_t discarded = 0;
673,989✔
38

39
   while(n) {
1,372,762✔
40
      const size_t got = this->read(buf, std::min(n, sizeof(buf)));
959,616✔
41
      discarded += got;
703,358✔
42
      n -= got;
703,358✔
43

44
      if(got == 0)
703,358✔
45
         break;
46
   }
47

48
   return discarded;
673,989✔
49
}
50

51
/*
52
* Read from a memory buffer
53
*/
54
size_t DataSource_Memory::read(uint8_t out[], size_t length) {
23,459,741✔
55
   const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
23,459,741✔
56
   copy_mem(out, m_source.data() + m_offset, got);
23,459,741✔
57
   m_offset += got;
23,459,741✔
58
   return got;
23,459,741✔
59
}
60

61
bool DataSource_Memory::check_available(size_t n) { return (n <= (m_source.size() - m_offset)); }
5,463,665✔
62

63
/*
64
* Peek into a memory buffer
65
*/
66
size_t DataSource_Memory::peek(uint8_t out[], size_t length, size_t peek_offset) const {
464,310✔
67
   const size_t bytes_left = m_source.size() - m_offset;
464,310✔
68
   if(peek_offset >= bytes_left)
464,310✔
69
      return 0;
70

71
   const size_t got = std::min(bytes_left - peek_offset, length);
270,773✔
72
   copy_mem(out, &m_source[m_offset + peek_offset], got);
270,773✔
73
   return got;
270,773✔
74
}
75

76
/*
77
* Check if the memory buffer is empty
78
*/
79
bool DataSource_Memory::end_of_data() const { return (m_offset == m_source.size()); }
917,774✔
80

81
/*
82
* DataSource_Memory Constructor
83
*/
84
DataSource_Memory::DataSource_Memory(std::string_view in) :
2,464✔
85
      m_source(cast_char_ptr_to_uint8(in.data()), cast_char_ptr_to_uint8(in.data()) + in.length()), m_offset(0) {}
2,464✔
86

87
/*
88
* Read from a stream
89
*/
90
size_t DataSource_Stream::read(uint8_t out[], size_t length) {
12,205,070✔
91
   m_source.read(cast_uint8_ptr_to_char(out), length);
12,205,070✔
92
   if(m_source.bad())
12,205,070✔
93
      throw Stream_IO_Error("DataSource_Stream::read: Source failure");
×
94

95
   const size_t got = static_cast<size_t>(m_source.gcount());
12,205,070✔
96
   m_total_read += got;
12,205,070✔
97
   return got;
12,205,070✔
98
}
99

100
bool DataSource_Stream::check_available(size_t n) {
730✔
101
   const std::streampos orig_pos = m_source.tellg();
730✔
102
   m_source.seekg(0, std::ios::end);
730✔
103
   const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
730✔
104
   m_source.seekg(orig_pos);
730✔
105
   return (avail >= n);
730✔
106
}
107

108
/*
109
* Peek into a stream
110
*/
111
size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) const {
5,451✔
112
   if(end_of_data())
5,451✔
113
      throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
×
114

115
   size_t got = 0;
5,451✔
116

117
   if(offset) {
5,451✔
118
      secure_vector<uint8_t> buf(offset);
×
119
      m_source.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
×
120
      if(m_source.bad())
×
121
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
122
      got = static_cast<size_t>(m_source.gcount());
×
123
   }
×
124

125
   if(got == offset) {
5,451✔
126
      m_source.read(cast_uint8_ptr_to_char(out), length);
5,451✔
127
      if(m_source.bad())
5,451✔
128
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
129
      got = static_cast<size_t>(m_source.gcount());
5,451✔
130
   }
131

132
   if(m_source.eof())
5,451✔
133
      m_source.clear();
930✔
134
   m_source.seekg(m_total_read, std::ios::beg);
5,451✔
135

136
   return got;
5,451✔
137
}
138

139
/*
140
* Check if the stream is empty or in error
141
*/
142
bool DataSource_Stream::end_of_data() const { return (!m_source.good()); }
14,658✔
143

144
/*
145
* Return a human-readable ID for this stream
146
*/
147
std::string DataSource_Stream::id() const { return m_identifier; }
×
148

149
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
150

151
/*
152
* DataSource_Stream Constructor
153
*/
154
DataSource_Stream::DataSource_Stream(std::string_view path, bool use_binary) :
3,443✔
155
      m_identifier(path),
3,443✔
156
      m_source_memory(std::make_unique<std::ifstream>(std::string(path), use_binary ? std::ios::binary : std::ios::in)),
9,023✔
157
      m_source(*m_source_memory),
3,443✔
158
      m_total_read(0) {
3,443✔
159
   if(!m_source.good()) {
3,443✔
160
      throw Stream_IO_Error(fmt("DataSource: Failure opening file '{}'", path));
2✔
161
   }
162
}
3,444✔
163

164
#endif
165

166
/*
167
* DataSource_Stream Constructor
168
*/
169
DataSource_Stream::DataSource_Stream(std::istream& in, std::string_view name) :
1✔
170
      m_identifier(name), m_source(in), m_total_read(0) {}
1✔
171

172
DataSource_Stream::~DataSource_Stream() = default;
6,885✔
173

174
}
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