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

randombit / botan / 13215274653

08 Feb 2025 11:38AM UTC coverage: 91.655% (-0.009%) from 91.664%
13215274653

Pull #4650

github

web-flow
Merge 107f31833 into bc555cd3c
Pull Request #4650: Reorganize code and reduce header dependencies

94836 of 103471 relevant lines covered (91.65%)

11230958.94 hits per line

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

70.0
/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,378,582✔
93
      virtual ~DataSource() = default;
328,732✔
94
      DataSource& operator=(const DataSource&) = delete;
95
      DataSource(const DataSource&) = delete;
96
};
97

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

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

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

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

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

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

139
      size_t get_bytes_read() const override { return m_offset; }
×
140

141
   private:
142
      secure_vector<uint8_t> m_source;
143
      size_t m_offset;
144
};
145

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

157
      DataSource_Stream(std::istream&, std::string_view id = "<std::istream>");
158

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

168
      DataSource_Stream(const DataSource_Stream&) = delete;
169

170
      DataSource_Stream& operator=(const DataSource_Stream&) = delete;
171

172
      ~DataSource_Stream() override;
173

174
      size_t get_bytes_read() const override { return m_total_read; }
×
175

176
   private:
177
      const std::string m_identifier;
178

179
      std::unique_ptr<std::istream> m_source_memory;
180
      std::istream& m_source;
181
      size_t m_total_read;
182
};
183

184
}  // namespace Botan
185

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

© 2025 Coveralls, Inc