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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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,887,974✔
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,763✔
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

49
   return discarded;
673,989✔
50
}
51

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

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

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

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

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

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

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

98
   const size_t got = static_cast<size_t>(m_source.gcount());
12,205,058✔
99
   m_total_read += got;
12,205,058✔
100
   return got;
12,205,058✔
101
}
102

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

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

119
   size_t got = 0;
5,451✔
120

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

130
   if(got == offset) {
5,451✔
131
      m_source.read(cast_uint8_ptr_to_char(out), length);
5,451✔
132
      if(m_source.bad()) {
5,451✔
133
         throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
×
134
      }
135
      got = static_cast<size_t>(m_source.gcount());
5,451✔
136
   }
137

138
   if(m_source.eof()) {
5,451✔
139
      m_source.clear();
930✔
140
   }
141
   m_source.seekg(m_total_read, std::ios::beg);
5,451✔
142

143
   return got;
5,451✔
144
}
145

146
/*
147
* Check if the stream is empty or in error
148
*/
149
bool DataSource_Stream::end_of_data() const { return (!m_source.good()); }
14,658✔
150

151
/*
152
* Return a human-readable ID for this stream
153
*/
154
std::string DataSource_Stream::id() const { return m_identifier; }
×
155

156
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
157

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

171
#endif
172

173
/*
174
* DataSource_Stream Constructor
175
*/
176
DataSource_Stream::DataSource_Stream(std::istream& in, std::string_view name) :
1✔
177
      m_identifier(name), m_source(in), m_total_read(0) {}
1✔
178

179
DataSource_Stream::~DataSource_Stream() = default;
6,885✔
180

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