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

randombit / botan / 16055899095

03 Jul 2025 04:34PM UTC coverage: 90.571% (-0.003%) from 90.574%
16055899095

push

github

web-flow
Merge pull request #4931 from randombit/jack/moar-tidy

Address various warnings from clang-tidy

99050 of 109362 relevant lines covered (90.57%)

12478386.65 hits per line

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

82.67
/src/lib/codec/base32/base32.cpp
1
/*
2
* Base32 Encoding and Decoding
3
* (C) 2018 Erwan Chaussy
4
* (C) 2018,2020,2025 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/base32.h>
10

11
#include <botan/internal/charset.h>
12
#include <botan/internal/codec_base.h>
13
#include <botan/internal/ct_utils.h>
14
#include <botan/internal/fmt.h>
15
#include <botan/internal/int_utils.h>
16
#include <botan/internal/loadstor.h>
17
#include <botan/internal/rounding.h>
18

19
namespace Botan {
20

21
namespace {
22

23
class Base32 final {
24
   public:
25
      static std::string name() noexcept { return "base32"; }
×
26

27
      static constexpr size_t encoding_bytes_in() noexcept { return m_encoding_bytes_in; }
28

29
      static constexpr size_t encoding_bytes_out() noexcept { return m_encoding_bytes_out; }
30

31
      static constexpr size_t decoding_bytes_in() noexcept { return m_encoding_bytes_out; }
32

33
      static constexpr size_t decoding_bytes_out() noexcept { return m_encoding_bytes_in; }
34

35
      static constexpr size_t bits_consumed() noexcept { return m_encoding_bits; }
36

37
      static constexpr size_t remaining_bits_before_padding() noexcept { return m_remaining_bits_before_padding; }
38

39
      static constexpr size_t encode_max_output(size_t input_length) {
25✔
40
         return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out;
25✔
41
      }
42

43
      static constexpr size_t decode_max_output(size_t input_length) {
198✔
44
         return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out;
198✔
45
      }
46

47
      static void encode(char out[8], const uint8_t in[5]) noexcept;
48

49
      static uint8_t lookup_binary_value(char input) noexcept;
50

51
      static bool check_bad_char(uint8_t bin, char input, bool ignore_ws);
52

53
      static void decode(uint8_t* out_ptr, const uint8_t decode_buf[8]) {
86✔
54
         out_ptr[0] = (decode_buf[0] << 3) | (decode_buf[1] >> 2);
86✔
55
         out_ptr[1] = (decode_buf[1] << 6) | (decode_buf[2] << 1) | (decode_buf[3] >> 4);
86✔
56
         out_ptr[2] = (decode_buf[3] << 4) | (decode_buf[4] >> 1);
86✔
57
         out_ptr[3] = (decode_buf[4] << 7) | (decode_buf[5] << 2) | (decode_buf[6] >> 3);
86✔
58
         out_ptr[4] = (decode_buf[6] << 5) | decode_buf[7];
86✔
59
      }
86✔
60

61
      static size_t bytes_to_remove(size_t final_truncate) {
61✔
62
         return (final_truncate > 0) ? (final_truncate / 2) + 1 : 0;
61✔
63
      }
64

65
   private:
66
      static constexpr size_t m_encoding_bits = 5;
67
      static constexpr size_t m_remaining_bits_before_padding = 6;
68

69
      static constexpr size_t m_encoding_bytes_in = 5;
70
      static constexpr size_t m_encoding_bytes_out = 8;
71
};
72

73
namespace {
74

75
uint64_t lookup_base32_char(uint64_t x) {
50✔
76
   uint64_t r = x;
50✔
77
   r += swar_lt<uint64_t>(x, 0x1a1a1a1a1a1a1a1a) & 0x2929292929292929;
50✔
78
   r += 0x1818181818181818;
50✔
79

80
   return r;
50✔
81
}
82

83
}  // namespace
84

85
//static
86
void Base32::encode(char out[8], const uint8_t in[5]) noexcept {
50✔
87
   const uint8_t b0 = (in[0] & 0xF8) >> 3;
50✔
88
   const uint8_t b1 = ((in[0] & 0x07) << 2) | (in[1] >> 6);
50✔
89
   const uint8_t b2 = ((in[1] & 0x3E) >> 1);
50✔
90
   const uint8_t b3 = ((in[1] & 0x01) << 4) | (in[2] >> 4);
50✔
91
   const uint8_t b4 = ((in[2] & 0x0F) << 1) | (in[3] >> 7);
50✔
92
   const uint8_t b5 = ((in[3] & 0x7C) >> 2);
50✔
93
   const uint8_t b6 = ((in[3] & 0x03) << 3) | (in[4] >> 5);
50✔
94
   const uint8_t b7 = in[4] & 0x1F;
50✔
95

96
   auto b = lookup_base32_char(make_uint64(b0, b1, b2, b3, b4, b5, b6, b7));
50✔
97

98
   out[0] = static_cast<char>(get_byte<0>(b));
50✔
99
   out[1] = static_cast<char>(get_byte<1>(b));
50✔
100
   out[2] = static_cast<char>(get_byte<2>(b));
50✔
101
   out[3] = static_cast<char>(get_byte<3>(b));
50✔
102
   out[4] = static_cast<char>(get_byte<4>(b));
50✔
103
   out[5] = static_cast<char>(get_byte<5>(b));
50✔
104
   out[6] = static_cast<char>(get_byte<6>(b));
50✔
105
   out[7] = static_cast<char>(get_byte<7>(b));
50✔
106
}
50✔
107

108
//static
109
uint8_t Base32::lookup_binary_value(char input) noexcept {
911✔
110
   const uint8_t c = static_cast<uint8_t>(input);
911✔
111

112
   const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z'));
911✔
113
   const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('2'), uint8_t('7'));
911✔
114

115
   const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('='));
911✔
116
   const auto is_whitespace =
911✔
117
      CT::Mask<uint8_t>::is_any_of(c, {uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')});
911✔
118

119
   const uint8_t c_upper = c - uint8_t('A');
911✔
120
   const uint8_t c_decim = c - uint8_t('2') + 26;
911✔
121

122
   uint8_t ret = 0xFF;  // default value
911✔
123

124
   ret = is_alpha_upper.select(c_upper, ret);
911✔
125
   ret = is_decimal.select(c_decim, ret);
911✔
126
   ret = is_equal.select(0x81, ret);
911✔
127
   ret = is_whitespace.select(0x80, ret);
911✔
128

129
   return ret;
911✔
130
}
131

132
//static
133
bool Base32::check_bad_char(uint8_t bin, char input, bool ignore_ws) {
911✔
134
   if(bin <= 0x1F) {
911✔
135
      return true;
136
   } else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) {
441✔
137
      throw Invalid_Argument(fmt("base32_decode: invalid character '{}'", format_char_for_display(input)));
76✔
138
   }
139
   return false;
140
}
141

142
}  // namespace
143

144
size_t base32_encode(char out[], const uint8_t in[], size_t input_length, size_t& input_consumed, bool final_inputs) {
×
145
   return base_encode(Base32(), out, in, input_length, input_consumed, final_inputs);
×
146
}
147

148
std::string base32_encode(const uint8_t input[], size_t input_length) {
25✔
149
   return base_encode_to_string(Base32(), input, input_length);
25✔
150
}
151

152
size_t base32_decode(
×
153
   uint8_t out[], const char in[], size_t input_length, size_t& input_consumed, bool final_inputs, bool ignore_ws) {
154
   return base_decode(Base32(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
×
155
}
156

157
size_t base32_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
×
158
   return base_decode_full(Base32(), output, input, input_length, ignore_ws);
×
159
}
160

161
size_t base32_decode(uint8_t output[], std::string_view input, bool ignore_ws) {
×
162
   return base32_decode(output, input.data(), input.length(), ignore_ws);
×
163
}
164

165
secure_vector<uint8_t> base32_decode(const char input[], size_t input_length, bool ignore_ws) {
99✔
166
   return base_decode_to_vec<secure_vector<uint8_t>>(Base32(), input, input_length, ignore_ws);
99✔
167
}
168

169
secure_vector<uint8_t> base32_decode(std::string_view input, bool ignore_ws) {
98✔
170
   return base32_decode(input.data(), input.size(), ignore_ws);
98✔
171
}
172

173
size_t base32_encode_max_output(size_t input_length) {
×
174
   return Base32::encode_max_output(input_length);
×
175
}
176

177
size_t base32_decode_max_output(size_t input_length) {
×
178
   return Base32::decode_max_output(input_length);
×
179
}
180

181
}  // namespace Botan
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