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

randombit / botan / 15656488073

14 Jun 2025 04:56PM UTC coverage: 90.561% (-0.02%) from 90.585%
15656488073

push

github

web-flow
Merge pull request #4912 from randombit/jack/clang-tidy-headers-part-2

Further clang-tidy fixes in header files

98779 of 109074 relevant lines covered (90.56%)

12362398.38 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 <span>
16
#include <string>
17
#include <string_view>
18

19
namespace Botan {
20

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

37
      virtual bool check_available(size_t n) = 0;
38

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

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

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

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

72
      /**
73
      * Peek at one byte.
74
      * @param out an output byte
75
      * @return length in bytes that was actually read and put
76
      * into out
77
      */
78
      size_t peek_byte(uint8_t& out) const;
79

80
      /**
81
      * Discard the next N bytes of the data
82
      * @param N the number of bytes to discard
83
      * @return number of bytes actually discarded
84
      */
85
      size_t discard_next(size_t N);
86

87
      /**
88
      * @return number of bytes read so far.
89
      */
90
      virtual size_t get_bytes_read() const = 0;
91

92
      DataSource() = default;
1,539,355✔
93
      virtual ~DataSource() = default;
335,432✔
94
      DataSource(const DataSource&) = delete;
95
      DataSource(DataSource&&) = default;
×
96
      DataSource& operator=(const DataSource&) = delete;
97
      DataSource& operator=(DataSource&&) = default;
98
};
99

100
/**
101
* This class represents a Memory-Based DataSource
102
*/
103
class BOTAN_PUBLIC_API(2, 0) DataSource_Memory final : public DataSource {
651,576✔
104
   public:
105
      size_t read(uint8_t[], size_t) override;
106
      size_t peek(uint8_t[], size_t, size_t) const override;
107
      bool check_available(size_t n) override;
108
      bool end_of_data() const override;
109

110
      /**
111
      * Construct a memory source that reads from a string
112
      * @param in the string to read from
113
      */
114
      explicit DataSource_Memory(std::string_view in);
115

116
      /**
117
      * Construct a memory source that reads from a byte array
118
      * @param in the byte array to read from
119
      * @param length the length of the byte array
120
      */
121
      DataSource_Memory(const uint8_t in[], size_t length) : m_source(in, in + length), m_offset(0) {}
361,947✔
122

123
      /**
124
      * Construct a memory source that reads from a secure_vector
125
      * @param in the MemoryRegion to read from
126
      */
127
      explicit DataSource_Memory(secure_vector<uint8_t> in) : m_source(std::move(in)), m_offset(0) {}
282,691✔
128

129
      /**
130
      * Construct a memory source that reads from an arbitrary byte buffer
131
      * @param in the MemoryRegion to read from
132
      */
133
      explicit DataSource_Memory(std::span<const uint8_t> in) : m_source(in.begin(), in.end()), m_offset(0) {}
10,995✔
134

135
      /**
136
      * Construct a memory source that reads from a std::vector
137
      * @param in the MemoryRegion to read from
138
      */
139
      explicit DataSource_Memory(const std::vector<uint8_t>& in) : m_source(in.begin(), in.end()), m_offset(0) {}
24,106✔
140

141
      size_t get_bytes_read() const override { return m_offset; }
×
142

143
   private:
144
      secure_vector<uint8_t> m_source;
145
      size_t m_offset;
146
};
147

148
/**
149
* This class represents a Stream-Based DataSource.
150
*/
151
class BOTAN_PUBLIC_API(2, 0) DataSource_Stream final : public DataSource {
152
   public:
153
      size_t read(uint8_t[], size_t) override;
154
      size_t peek(uint8_t[], size_t, size_t) const override;
155
      bool check_available(size_t n) override;
156
      bool end_of_data() const override;
157
      std::string id() const override;
158

159
      DataSource_Stream(std::istream&, std::string_view id = "<std::istream>");
160

161
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
162
      /**
163
      * Construct a Stream-Based DataSource from filesystem path
164
      * @param filename the path to the file
165
      * @param use_binary whether to treat the file as binary or not
166
      */
167
      DataSource_Stream(std::string_view filename, bool use_binary = false);
168
#endif
169

170
      DataSource_Stream(const DataSource_Stream&) = delete;
171
      DataSource_Stream(DataSource_Stream&&) = delete;
172
      DataSource_Stream& operator=(const DataSource_Stream&) = delete;
173
      DataSource_Stream& operator=(DataSource_Stream&&) = delete;
174

175
      ~DataSource_Stream() override;
176

177
      size_t get_bytes_read() const override { return m_total_read; }
×
178

179
   private:
180
      const std::string m_identifier;
181

182
      std::unique_ptr<std::istream> m_source_memory;
183
      std::istream& m_source;
184
      size_t m_total_read;
185
};
186

187
}  // namespace Botan
188

189
#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