Coverage Report

Created: 2023-06-07 07:00

/src/botan/build/include/botan/data_src.h
Line
Count
Source (jump to first uncovered line)
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 <span>
15
#include <string>
16
#include <string_view>
17
18
namespace Botan {
19
20
/**
21
* This class represents an abstract data source object.
22
*/
23
class BOTAN_PUBLIC_API(2, 0) DataSource {
24
   public:
25
      /**
26
      * Read from the source. Moves the internal offset so that every
27
      * call to read will return a new portion of the source.
28
      *
29
      * @param out the byte array to write the result to
30
      * @param length the length of the byte array out
31
      * @return length in bytes that was actually read and put
32
      * into out
33
      */
34
      [[nodiscard]] virtual size_t read(uint8_t out[], size_t length) = 0;
35
36
      virtual bool check_available(size_t n) = 0;
37
38
      /**
39
      * Read from the source but do not modify the internal
40
      * offset. Consecutive calls to peek() will return portions of
41
      * the source starting at the same position.
42
      *
43
      * @param out the byte array to write the output to
44
      * @param length the length of the byte array out
45
      * @param peek_offset the offset into the stream to read at
46
      * @return length in bytes that was actually read and put
47
      * into out
48
      */
49
      [[nodiscard]] virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const = 0;
50
51
      /**
52
      * Test whether the source still has data that can be read.
53
      * @return true if there is no more data to read, false otherwise
54
      */
55
      virtual bool end_of_data() const = 0;
56
57
      /**
58
      * return the id of this data source
59
      * @return std::string representing the id of this data source
60
      */
61
0
      virtual std::string id() const { return ""; }
62
63
      /**
64
      * Read one byte.
65
      * @param out the byte to read to
66
      * @return length in bytes that was actually read and put
67
      * into out
68
      */
69
      size_t read_byte(uint8_t& out);
70
71
      /**
72
      * Peek at one byte.
73
      * @param out an output byte
74
      * @return length in bytes that was actually read and put
75
      * into out
76
      */
77
      size_t peek_byte(uint8_t& out) const;
78
79
      /**
80
      * Discard the next N bytes of the data
81
      * @param N the number of bytes to discard
82
      * @return number of bytes actually discarded
83
      */
84
      size_t discard_next(size_t N);
85
86
      /**
87
      * @return number of bytes read so far.
88
      */
89
      virtual size_t get_bytes_read() const = 0;
90
91
0
      DataSource() = default;
92
0
      virtual ~DataSource() = default;
93
      DataSource& operator=(const DataSource&) = delete;
94
      DataSource(const DataSource&) = delete;
95
};
96
97
/**
98
* This class represents a Memory-Based DataSource
99
*/
100
class BOTAN_PUBLIC_API(2, 0) DataSource_Memory final : public DataSource {
101
   public:
102
      size_t read(uint8_t[], size_t) override;
103
      size_t peek(uint8_t[], size_t, size_t) const override;
104
      bool check_available(size_t n) override;
105
      bool end_of_data() const override;
106
107
      /**
108
      * Construct a memory source that reads from a string
109
      * @param in the string to read from
110
      */
111
      explicit DataSource_Memory(std::string_view in);
112
113
      /**
114
      * Construct a memory source that reads from a byte array
115
      * @param in the byte array to read from
116
      * @param length the length of the byte array
117
      */
118
0
      DataSource_Memory(const uint8_t in[], size_t length) : m_source(in, in + length), m_offset(0) {}
119
120
      /**
121
      * Construct a memory source that reads from a secure_vector
122
      * @param in the MemoryRegion to read from
123
      */
124
0
      explicit DataSource_Memory(secure_vector<uint8_t> in) : m_source(std::move(in)), m_offset(0) {}
125
126
      /**
127
      * Construct a memory source that reads from an arbitrary byte buffer
128
      * @param in the MemoryRegion to read from
129
      */
130
0
      explicit DataSource_Memory(std::span<const uint8_t> in) : m_source(in.begin(), in.end()), m_offset(0) {}
131
132
      /**
133
      * Construct a memory source that reads from a std::vector
134
      * @param in the MemoryRegion to read from
135
      */
136
0
      explicit DataSource_Memory(const std::vector<uint8_t>& in) : m_source(in.begin(), in.end()), m_offset(0) {}
137
138
0
      size_t get_bytes_read() const override { return m_offset; }
139
140
   private:
141
      secure_vector<uint8_t> m_source;
142
      size_t m_offset;
143
};
144
145
/**
146
* This class represents a Stream-Based DataSource.
147
*/
148
class BOTAN_PUBLIC_API(2, 0) DataSource_Stream final : public DataSource {
149
   public:
150
      size_t read(uint8_t[], size_t) override;
151
      size_t peek(uint8_t[], size_t, size_t) const override;
152
      bool check_available(size_t n) override;
153
      bool end_of_data() const override;
154
      std::string id() const override;
155
156
      DataSource_Stream(std::istream&, std::string_view id = "<std::istream>");
157
158
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
159
      /**
160
      * Construct a Stream-Based DataSource from filesystem path
161
      * @param filename the path to the file
162
      * @param use_binary whether to treat the file as binary or not
163
      */
164
      DataSource_Stream(std::string_view filename, bool use_binary = false);
165
#endif
166
167
      DataSource_Stream(const DataSource_Stream&) = delete;
168
169
      DataSource_Stream& operator=(const DataSource_Stream&) = delete;
170
171
      ~DataSource_Stream();
172
173
0
      size_t get_bytes_read() const override { return m_total_read; }
174
175
   private:
176
      const std::string m_identifier;
177
178
      std::unique_ptr<std::istream> m_source_memory;
179
      std::istream& m_source;
180
      size_t m_total_read;
181
};
182
183
}  // namespace Botan
184
185
#endif