Coverage Report

Created: 2025-07-11 06:15

/src/Botan-3.4.0/build/include/public/botan/buf_comp.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Buffered Computation
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_BUFFERED_COMPUTATION_H_
9
#define BOTAN_BUFFERED_COMPUTATION_H_
10
11
#include <botan/concepts.h>
12
#include <botan/mem_ops.h>
13
#include <botan/secmem.h>
14
#include <span>
15
#include <string_view>
16
17
namespace Botan {
18
19
/**
20
* This class represents any kind of computation which uses an internal
21
* state, such as hash functions or MACs
22
*/
23
class BOTAN_PUBLIC_API(2, 0) Buffered_Computation {
24
   public:
25
      /**
26
      * @return length of the output of this function in bytes
27
      */
28
      virtual size_t output_length() const = 0;
29
30
      /**
31
      * Add new input to process.
32
      * @param in the input to process as a byte array
33
      * @param length of param in in bytes
34
      */
35
37.1M
      void update(const uint8_t in[], size_t length) { add_data({in, length}); }
36
37
      /**
38
      * Add new input to process.
39
      * @param in the input to process as a contiguous data range
40
      */
41
8.65M
      void update(std::span<const uint8_t> in) { add_data(in); }
42
43
      void update_be(uint16_t val);
44
      void update_be(uint32_t val);
45
      void update_be(uint64_t val);
46
47
      void update_le(uint16_t val);
48
      void update_le(uint32_t val);
49
      void update_le(uint64_t val);
50
51
      /**
52
      * Add new input to process.
53
      * @param str the input to process as a std::string_view. Will be interpreted
54
      * as a byte array based on the strings encoding.
55
      */
56
7.71k
      void update(std::string_view str) { add_data({cast_char_ptr_to_uint8(str.data()), str.size()}); }
57
58
      /**
59
      * Process a single byte.
60
      * @param in the byte to process
61
      */
62
553k
      void update(uint8_t in) { add_data({&in, 1}); }
63
64
      /**
65
      * Complete the computation and retrieve the
66
      * final result.
67
      * @param out The byte array to be filled with the result.
68
      * Must be of length output_length()
69
      */
70
406k
      void final(uint8_t out[]) { final_result({out, output_length()}); }
71
72
      /**
73
      * Complete the computation and retrieve the
74
      * final result as a container of your choice.
75
      * @return a contiguous container holding the result
76
      */
77
      template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
78
69.8k
      T final() {
79
69.8k
         T output(output_length());
80
69.8k
         final_result(output);
81
69.8k
         return output;
82
69.8k
      }
Unexecuted instantiation: _ZN5Botan20Buffered_Computation5finalITkNS_8concepts21resizable_byte_bufferENSt3__16vectorIhNS3_9allocatorIhEEEEEET_v
_ZN5Botan20Buffered_Computation5finalITkNS_8concepts21resizable_byte_bufferENSt3__16vectorIhNS_16secure_allocatorIhEEEEEET_v
Line
Count
Source
78
69.8k
      T final() {
79
69.8k
         T output(output_length());
80
69.8k
         final_result(output);
81
69.8k
         return output;
82
69.8k
      }
83
84
0
      std::vector<uint8_t> final_stdvec() { return final<std::vector<uint8_t>>(); }
85
86
2.51M
      void final(std::span<uint8_t> out) {
87
2.51M
         BOTAN_ARG_CHECK(out.size() >= output_length(), "provided output buffer has insufficient capacity");
88
2.51M
         final_result(out);
89
2.51M
      }
90
91
      template <concepts::resizable_byte_buffer T>
92
1.25M
      void final(T& out) {
93
1.25M
         out.resize(output_length());
94
1.25M
         final_result(out);
95
1.25M
      }
96
97
      /**
98
      * Update and finalize computation. Does the same as calling update()
99
      * and final() consecutively.
100
      * @param in the input to process as a byte array
101
      * @param length the length of the byte array
102
      * @result the result of the call to final()
103
      */
104
      template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
105
0
      T process(const uint8_t in[], size_t length) {
106
0
         update(in, length);
107
0
         return final<T>();
108
0
      }
109
110
      /**
111
      * Update and finalize computation. Does the same as calling update()
112
      * and final() consecutively.
113
      * @param in the input to process as a string
114
      * @result the result of the call to final()
115
      */
116
      template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
117
      T process(std::string_view in) {
118
         update(in);
119
         return final<T>();
120
      }
121
122
      /**
123
      * Update and finalize computation. Does the same as calling update()
124
      * and final() consecutively.
125
      * @param in the input to process as a contiguous container
126
      * @result the result of the call to final()
127
      */
128
      template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
129
0
      T process(std::span<const uint8_t> in) {
130
0
         update(in);
131
0
         return final<T>();
132
0
      }
Unexecuted instantiation: _ZN5Botan20Buffered_Computation7processITkNS_8concepts21resizable_byte_bufferENSt3__16vectorIhNS_16secure_allocatorIhEEEEEET_NS3_4spanIKhLm18446744073709551615EEE
Unexecuted instantiation: _ZN5Botan20Buffered_Computation7processITkNS_8concepts21resizable_byte_bufferENSt3__16vectorIhNS3_9allocatorIhEEEEEET_NS3_4spanIKhLm18446744073709551615EEE
133
134
990k
      virtual ~Buffered_Computation() = default;
135
136
   private:
137
      /**
138
      * Add more data to the computation
139
      * @param input is an input buffer
140
      */
141
      virtual void add_data(std::span<const uint8_t> input) = 0;
142
143
      /**
144
      * Write the final output to out
145
      * @param out is an output buffer of output_length()
146
      */
147
      virtual void final_result(std::span<uint8_t> out) = 0;
148
};
149
150
}  // namespace Botan
151
152
#endif