Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/src/lib/utils/data_src.cpp
Line
Count
Source
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
0
size_t DataSource::read_byte(uint8_t& out) {
28
0
   return read(&out, 1);
29
0
}
30
31
/*
32
* Read a single byte from the DataSource
33
*/
34
0
std::optional<uint8_t> DataSource::read_byte() {
35
0
   uint8_t b = 0;
36
0
   if(this->read(&b, 1) == 1) {
37
0
      return b;
38
0
   } else {
39
0
      return {};
40
0
   }
41
0
}
42
43
/*
44
* Peek a single byte from the DataSource
45
*/
46
0
size_t DataSource::peek_byte(uint8_t& out) const {
47
0
   return peek(&out, 1, 0);
48
0
}
49
50
/*
51
* Discard the next N bytes of the data
52
*/
53
0
size_t DataSource::discard_next(size_t n) {
54
0
   uint8_t buf[64] = {0};
55
0
   size_t discarded = 0;
56
57
0
   while(n > 0) {
58
0
      const size_t got = this->read(buf, std::min(n, sizeof(buf)));
59
0
      discarded += got;
60
0
      n -= got;
61
62
0
      if(got == 0) {
63
0
         break;
64
0
      }
65
0
   }
66
67
0
   return discarded;
68
0
}
69
70
/*
71
* Read from a memory buffer
72
*/
73
0
size_t DataSource_Memory::read(uint8_t out[], size_t length) {
74
0
   const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
75
0
   copy_mem(out, m_source.data() + m_offset, got);
76
0
   m_offset += got;
77
0
   return got;
78
0
}
79
80
0
bool DataSource_Memory::check_available(size_t n) {
81
0
   return (n <= (m_source.size() - m_offset));
82
0
}
83
84
/*
85
* Peek into a memory buffer
86
*/
87
0
size_t DataSource_Memory::peek(uint8_t out[], size_t length, size_t peek_offset) const {
88
0
   const size_t bytes_left = m_source.size() - m_offset;
89
0
   if(peek_offset >= bytes_left) {
90
0
      return 0;
91
0
   }
92
93
0
   const size_t got = std::min(bytes_left - peek_offset, length);
94
0
   copy_mem(out, &m_source[m_offset + peek_offset], got);
95
0
   return got;
96
0
}
97
98
/*
99
* Check if the memory buffer is empty
100
*/
101
0
bool DataSource_Memory::end_of_data() const {
102
0
   return (m_offset == m_source.size());
103
0
}
104
105
/*
106
* DataSource_Memory Constructor
107
*/
108
0
DataSource_Memory::DataSource_Memory(std::string_view in) : DataSource_Memory(as_span_of_bytes(in)) {}
109
110
/*
111
* Read from a stream
112
*/
113
0
size_t DataSource_Stream::read(uint8_t out[], size_t length) {
114
0
   m_source.read(cast_uint8_ptr_to_char(out), length);
115
0
   if(m_source.bad()) {
116
0
      throw Stream_IO_Error("DataSource_Stream::read: Source failure");
117
0
   }
118
119
0
   const size_t got = static_cast<size_t>(m_source.gcount());
120
0
   m_total_read += got;
121
0
   return got;
122
0
}
123
124
0
bool DataSource_Stream::check_available(size_t n) {
125
0
   const std::streampos orig_pos = m_source.tellg();
126
0
   m_source.seekg(0, std::ios::end);
127
0
   const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
128
0
   m_source.seekg(orig_pos);
129
0
   return (avail >= n);
130
0
}
131
132
/*
133
* Peek into a stream
134
*/
135
0
size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) const {
136
0
   if(end_of_data()) {
137
0
      throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
138
0
   }
139
140
0
   size_t got = 0;
141
142
0
   if(offset > 0) {
143
0
      m_source.seekg(offset, std::ios::cur);
144
0
      if(!m_source.good()) {
145
0
         m_source.clear();
146
0
         m_source.seekg(m_total_read, std::ios::beg);
147
0
         return 0;
148
0
      }
149
0
   }
150
151
0
   m_source.read(cast_uint8_ptr_to_char(out), length);
152
0
   if(m_source.bad()) {
153
0
      throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
154
0
   }
155
0
   got = static_cast<size_t>(m_source.gcount());
156
157
0
   if(m_source.eof()) {
158
0
      m_source.clear();
159
0
   }
160
0
   m_source.seekg(m_total_read, std::ios::beg);
161
162
0
   return got;
163
0
}
164
165
/*
166
* Check if the stream is empty or in error
167
*/
168
0
bool DataSource_Stream::end_of_data() const {
169
   /*
170
   Peek to trigger EOF indicator if positioned at the end of the stream.
171
   Without this, good() returns true even when all data has been read.
172
   */
173
0
   m_source.peek();
174
0
   return (!m_source.good());
175
0
}
176
177
/*
178
* Return a human-readable ID for this stream
179
*/
180
0
std::string DataSource_Stream::id() const {
181
0
   return m_identifier;
182
0
}
183
184
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
185
186
/*
187
* DataSource_Stream Constructor
188
*/
189
DataSource_Stream::DataSource_Stream(std::string_view path, bool use_binary) :
190
0
      m_identifier(path),
191
0
      m_source_memory(std::make_unique<std::ifstream>(std::string(path), use_binary ? std::ios::binary : std::ios::in)),
192
0
      m_source(*m_source_memory),
193
0
      m_total_read(0) {
194
0
   if(!m_source.good()) {
195
0
      throw Stream_IO_Error(fmt("DataSource: Failure opening file '{}'", path));
196
0
   }
197
0
}
198
199
#endif
200
201
/*
202
* DataSource_Stream Constructor
203
*/
204
DataSource_Stream::DataSource_Stream(std::istream& in, std::string_view name) :
205
0
      m_identifier(name), m_source(in), m_total_read(0) {}
206
207
0
DataSource_Stream::~DataSource_Stream() = default;
208
209
}  // namespace Botan