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

randombit / botan / 19596147897

22 Nov 2025 01:25PM UTC coverage: 90.629% (+0.002%) from 90.627%
19596147897

Pull #5168

github

web-flow
Merge 9ea34d5a5 into f8eb34002
Pull Request #5168: Add clang-format 17 formatting rules for attributes and inline assembly

100659 of 111067 relevant lines covered (90.63%)

12696690.87 hits per line

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

63.64
/src/lib/utils/data_src.h
1
/*
2
* DataSource
3
* (C) 1999-2007 Jack Lloyd
4
*     2012 Markus Wanner
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#ifndef BOTAN_DATA_SRC_H_
10
#define BOTAN_DATA_SRC_H_
11

12
#include <botan/secmem.h>
13
#include <iosfwd>
14
#include <memory>
15
#include <optional>
16
#include <span>
17
#include <string>
18
#include <string_view>
19

20
namespace Botan {
21

22
/**
23
* This class represents an abstract data source object.
24
*/
25
class BOTAN_PUBLIC_API(2, 0) DataSource {
26
   public:
27
      /**
28
      * Read from the source. Moves the internal offset so that every
29
      * call to read will return a new portion of the source.
30
      *
31
      * @param out the byte array to write the result to
32
      * @param length the length of the byte array out
33
      * @return length in bytes that was actually read and put
34
      * into out
35
      */
36
      [[nodiscard]]
37
      virtual size_t read(uint8_t out[], size_t length) = 0;
38

39
      virtual bool check_available(size_t n) = 0;
40

41
      /**
42
      * Read from the source but do not modify the internal
43
      * offset. Consecutive calls to peek() will return portions of
44
      * the source starting at the same position.
45
      *
46
      * @param out the byte array to write the output to
47
      * @param length the length of the byte array out
48
      * @param peek_offset the offset into the stream to read at
49
      * @return length in bytes that was actually read and put
50
      * into out
51
      */
52
      [[nodiscard]]
53
      virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const = 0;
54

55
      /**
56
      * Test whether the source still has data that can be read.
57
      * @return true if there is no more data to read, false otherwise
58
      */
59
      virtual bool end_of_data() const = 0;
60

61
      /**
62
      * return the id of this data source
63
      * @return std::string representing the id of this data source
64
      */
65
      virtual std::string id() const { return ""; }
×
66

67
      /**
68
      * Read one byte.
69
      * @param out the byte to read to
70
      * @return length in bytes that was actually read and put
71
      * into out
72
      */
73
      size_t read_byte(uint8_t& out);
74

75
      /**
76
      * Read one byte.
77
      *
78
      * Returns nullopt if no further bytes are available
79
      */
80
      std::optional<uint8_t> read_byte();
81

82
      /**
83
      * Peek at one byte.
84
      * @param out an output byte
85
      * @return length in bytes that was actually read and put
86
      * into out
87
      */
88
      size_t peek_byte(uint8_t& out) const;
89

90
      /**
91
      * Discard the next N bytes of the data
92
      * @param N the number of bytes to discard
93
      * @return number of bytes actually discarded
94
      */
95
      size_t discard_next(size_t N);
96

97
      /**
98
      * @return number of bytes read so far.
99
      */
100
      virtual size_t get_bytes_read() const = 0;
101

102
      DataSource() = default;
1,547,947✔
103
      virtual ~DataSource() = default;
335,848✔
104
      DataSource(const DataSource&) = delete;
105
      DataSource(DataSource&&) = default;
×
106
      DataSource& operator=(const DataSource&) = delete;
107
      DataSource& operator=(DataSource&&) = default;
108
};
109

110
/**
111
* This class represents a Memory-Based DataSource
112
*/
113
class BOTAN_PUBLIC_API(2, 0) DataSource_Memory final : public DataSource {
652,373✔
114
   public:
115
      size_t read(uint8_t buf[], size_t length) override;
116
      size_t peek(uint8_t buf[], size_t length, size_t offset) const override;
117
      bool check_available(size_t n) override;
118
      bool end_of_data() const override;
119

120
      /**
121
      * Construct a memory source that reads from a string
122
      * @param in the string to read from
123
      */
124
      explicit DataSource_Memory(std::string_view in);
125

126
      /**
127
      * Construct a memory source that reads from a byte array
128
      * @param in the byte array to read from
129
      * @param length the length of the byte array
130
      */
131
      DataSource_Memory(const uint8_t in[], size_t length) : m_source(in, in + length), m_offset(0) {}
366,488✔
132

133
      /**
134
      * Construct a memory source that reads from a secure_vector
135
      * @param in the MemoryRegion to read from
136
      */
137
      explicit DataSource_Memory(secure_vector<uint8_t> in) : m_source(std::move(in)), m_offset(0) {}
282,695✔
138

139
      /**
140
      * Construct a memory source that reads from an arbitrary byte buffer
141
      * @param in the MemoryRegion to read from
142
      */
143
      explicit DataSource_Memory(std::span<const uint8_t> in) : m_source(in.begin(), in.end()), m_offset(0) {}
13,674✔
144

145
      /**
146
      * Construct a memory source that reads from a std::vector
147
      * @param in the MemoryRegion to read from
148
      */
149
      explicit DataSource_Memory(const std::vector<uint8_t>& in) : m_source(in.begin(), in.end()), m_offset(0) {}
24,346✔
150

151
      size_t get_bytes_read() const override { return m_offset; }
×
152

153
   private:
154
      secure_vector<uint8_t> m_source;
155
      size_t m_offset;
156
};
157

158
/**
159
* This class represents a Stream-Based DataSource.
160
*/
161
class BOTAN_PUBLIC_API(2, 0) DataSource_Stream final : public DataSource {
162
   public:
163
      size_t read(uint8_t buf[], size_t length) override;
164
      size_t peek(uint8_t buf[], size_t length, size_t offset) const override;
165
      bool check_available(size_t n) override;
166
      bool end_of_data() const override;
167
      std::string id() const override;
168

169
      BOTAN_FUTURE_EXPLICIT DataSource_Stream(std::istream& in, std::string_view id = "<std::istream>");
170

171
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
172
      /**
173
      * Construct a Stream-Based DataSource from filesystem path
174
      * @param filename the path to the file
175
      * @param use_binary whether to treat the file as binary or not
176
      */
177
      BOTAN_FUTURE_EXPLICIT DataSource_Stream(std::string_view filename, bool use_binary = false);
178
#endif
179

180
      DataSource_Stream(const DataSource_Stream&) = delete;
181
      DataSource_Stream(DataSource_Stream&&) = delete;
182
      DataSource_Stream& operator=(const DataSource_Stream&) = delete;
183
      DataSource_Stream& operator=(DataSource_Stream&&) = delete;
184

185
      ~DataSource_Stream() override;
186

187
      size_t get_bytes_read() const override { return m_total_read; }
×
188

189
   private:
190
      const std::string m_identifier;
191

192
      std::unique_ptr<std::istream> m_source_memory;
193
      std::istream& m_source;
194
      size_t m_total_read;
195
};
196

197
}  // namespace Botan
198

199
#endif
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