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

randombit / botan / 15654190339

14 Jun 2025 04:56PM UTC coverage: 90.571% (-0.01%) from 90.585%
15654190339

push

github

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

Further clang-tidy fixes in header files

98789 of 109074 relevant lines covered (90.57%)

12363815.23 hits per line

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

98.7
/src/lib/utils/codec_base.h
1
/*
2
* Base Encoding and Decoding
3
* (C) 2018 Erwan Chaussy
4
* (C) 2018 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#ifndef BOTAN_BASE_CODEC_H_
10
#define BOTAN_BASE_CODEC_H_
11

12
#include <botan/exceptn.h>
13
#include <botan/mem_ops.h>
14
#include <array>
15
#include <string>
16
#include <type_traits>
17

18
namespace Botan {
19

20
/**
21
* Perform encoding using the base provided
22
* @param base object giving access to the encodings specifications
23
* @param output an array of at least base.encode_max_output bytes
24
* @param input is some binary data
25
* @param input_length length of input in bytes
26
* @param input_consumed is an output parameter which says how many
27
*        bytes of input were actually consumed. If less than
28
*        input_length, then the range input[consumed:length]
29
*        should be passed in later along with more input.
30
* @param final_inputs true iff this is the last input, in which case
31
         padding chars will be applied if needed
32
* @return number of bytes written to output
33
*/
34
template <class Base>
35
size_t base_encode(const Base& base,
2,535✔
36
                   char output[],
37
                   const uint8_t input[],
38
                   size_t input_length,
39
                   size_t& input_consumed,
40
                   bool final_inputs) {
41
   input_consumed = 0;
2,535✔
42

43
   // TODO(Botan4) Check if we can use just base. or Base:: here instead
44
   constexpr size_t encoding_bytes_in = std::remove_reference_t<Base>::encoding_bytes_in();
2,535✔
45
   constexpr size_t encoding_bytes_out = std::remove_reference_t<Base>::encoding_bytes_out();
2,535✔
46

47
   size_t input_remaining = input_length;
2,535✔
48
   size_t output_produced = 0;
2,535✔
49

50
   while(input_remaining >= encoding_bytes_in) {
2,181,603✔
51
      base.encode(output + output_produced, input + input_consumed);
2,179,068✔
52

53
      input_consumed += encoding_bytes_in;
2,179,068✔
54
      output_produced += encoding_bytes_out;
2,179,068✔
55
      input_remaining -= encoding_bytes_in;
2,179,068✔
56
   }
57

58
   if(final_inputs && input_remaining) {
2,535✔
59
      std::array<uint8_t, encoding_bytes_in> remainder{};
2,135✔
60
      for(size_t i = 0; i != input_remaining; ++i) {
5,363✔
61
         remainder[i] = input[input_consumed + i];
3,228✔
62
      }
63

64
      base.encode(output + output_produced, remainder.data());
2,135✔
65

66
      const size_t bits_consumed = base.bits_consumed();
2,135✔
67
      const size_t remaining_bits_before_padding = base.remaining_bits_before_padding();
2,135✔
68

69
      size_t empty_bits = 8 * (encoding_bytes_in - input_remaining);
2,135✔
70
      size_t index = output_produced + encoding_bytes_out - 1;
2,135✔
71
      while(empty_bits >= remaining_bits_before_padding) {
5,369✔
72
         output[index--] = '=';
3,234✔
73
         empty_bits -= bits_consumed;
3,234✔
74
      }
75

76
      input_consumed += input_remaining;
2,135✔
77
      output_produced += encoding_bytes_out;
2,135✔
78
   }
79

80
   return output_produced;
2,535✔
81
}
82

83
template <typename Base>
84
std::string base_encode_to_string(const Base& base, const uint8_t input[], size_t input_length) {
2,529✔
85
   const size_t output_length = base.encode_max_output(input_length);
2,529✔
86
   std::string output(output_length, 0);
2,529✔
87

88
   size_t consumed = 0;
2,529✔
89
   size_t produced = 0;
2,529✔
90

91
   if(output_length > 0) {
2,529✔
92
      produced = base_encode(base, &output.front(), input, input_length, consumed, true);
2,527✔
93
   }
94

95
   BOTAN_ASSERT_EQUAL(consumed, input_length, "Consumed the entire input");
2,529✔
96
   BOTAN_ASSERT_EQUAL(produced, output.size(), "Produced expected size");
2,529✔
97

98
   return output;
2,529✔
99
}
×
100

101
/**
102
* Perform decoding using the base provided
103
* @param base object giving access to the encodings specifications
104
* @param output an array of at least Base::decode_max_output bytes
105
* @param input some base input
106
* @param input_length length of input in bytes
107
* @param input_consumed is an output parameter which says how many
108
*        bytes of input were actually consumed. If less than
109
*        input_length, then the range input[consumed:length]
110
*        should be passed in later along with more input.
111
* @param final_inputs true iff this is the last input, in which case
112
         padding is allowed
113
* @param ignore_ws ignore whitespace on input; if false, throw an
114
                   exception if whitespace is encountered
115
* @return number of bytes written to output
116
*/
117
template <typename Base>
118
size_t base_decode(const Base& base,
16,445✔
119
                   uint8_t output[],
120
                   const char input[],
121
                   size_t input_length,
122
                   size_t& input_consumed,
123
                   bool final_inputs,
124
                   bool ignore_ws = true) {
125
   // TODO(Botan4) Check if we can use just base. or Base:: here instead
126
   constexpr size_t decoding_bytes_in = std::remove_reference_t<Base>::decoding_bytes_in();
16,445✔
127
   constexpr size_t decoding_bytes_out = std::remove_reference_t<Base>::decoding_bytes_out();
16,445✔
128

129
   uint8_t* out_ptr = output;
16,445✔
130
   std::array<uint8_t, decoding_bytes_in> decode_buf{};
16,445✔
131
   size_t decode_buf_pos = 0;
16,445✔
132
   size_t final_truncate = 0;
16,445✔
133

134
   clear_mem(output, base.decode_max_output(input_length));
16,445✔
135

136
   for(size_t i = 0; i != input_length; ++i) {
22,134,032✔
137
      const uint8_t bin = base.lookup_binary_value(input[i]);
22,117,663✔
138

139
      // This call might throw Invalid_Argument
140
      if(base.check_bad_char(bin, input[i], ignore_ws)) {
22,117,663✔
141
         decode_buf[decode_buf_pos] = bin;
21,752,757✔
142
         ++decode_buf_pos;
21,752,757✔
143
      }
144

145
      /*
146
      * If we're at the end of the input, pad with 0s and truncate
147
      */
148
      if(final_inputs && (i == input_length - 1)) {
22,117,587✔
149
         if(decode_buf_pos) {
16,354✔
150
            for(size_t j = decode_buf_pos; j < decoding_bytes_in; ++j) {
28,805✔
151
               decode_buf[j] = 0;
16,653✔
152
            }
153

154
            final_truncate = decoding_bytes_in - decode_buf_pos;
12,152✔
155
            decode_buf_pos = decoding_bytes_in;
156
         }
157
      }
158

159
      if(decode_buf_pos == decoding_bytes_in) {
22,101,233✔
160
         base.decode(out_ptr, decode_buf.data());
5,442,241✔
161

162
         out_ptr += decoding_bytes_out;
5,442,241✔
163
         decode_buf_pos = 0;
5,442,241✔
164
         input_consumed = i + 1;
5,442,241✔
165
      }
166
   }
167

168
   while(input_consumed < input_length && base.lookup_binary_value(input[input_consumed]) == 0x80) {
21,523✔
169
      ++input_consumed;
5,154✔
170
   }
171

172
   size_t written = (out_ptr - output) - base.bytes_to_remove(final_truncate);
16,369✔
173

174
   return written;
16,369✔
175
}
176

177
template <typename Base>
178
size_t base_decode_full(const Base& base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
16,443✔
179
   size_t consumed = 0;
16,443✔
180
   const size_t written = base_decode(base, output, input, input_length, consumed, true, ignore_ws);
16,443✔
181

182
   if(consumed != input_length) {
16,367✔
183
      throw Invalid_Argument(base.name() + " decoding failed, input did not have full bytes");
56✔
184
   }
185

186
   return written;
16,353✔
187
}
188

189
template <typename Vector, typename Base>
190
Vector base_decode_to_vec(const Base& base, const char input[], size_t input_length, bool ignore_ws) {
16,426✔
191
   const size_t output_length = base.decode_max_output(input_length);
16,426✔
192
   Vector bin(output_length);
16,426✔
193

194
   const size_t written = base_decode_full(base, bin.data(), input, input_length, ignore_ws);
16,426✔
195

196
   bin.resize(written);
16,336✔
197
   return bin;
16,336✔
198
}
90✔
199

200
}  // namespace Botan
201

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