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

randombit / botan / 13776460423

10 Mar 2025 11:00PM UTC coverage: 91.641% (-0.02%) from 91.665%
13776460423

push

github

web-flow
Merge pull request #4765 from randombit/jack/swar-base32

Implement SWAR based base32 encoding

95874 of 104619 relevant lines covered (91.64%)

11254446.13 hits per line

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

82.43
/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 size_t encoding_bytes_in() noexcept { return m_encoding_bytes_in; }
28

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

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

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

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

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

39
      static 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 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) { return final_truncate ? (final_truncate / 2) + 1 : 0; }
61✔
62

63
   private:
64
      static const size_t m_encoding_bits = 5;
65
      static const size_t m_remaining_bits_before_padding = 6;
66

67
      static const size_t m_encoding_bytes_in = 5;
68
      static const size_t m_encoding_bytes_out = 8;
69
};
70

71
namespace {
72

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

78
   return r;
50✔
79
}
80

81
}  // namespace
82

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

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

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

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

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

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

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

120
   uint8_t ret = 0xFF;  // default value
911✔
121

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

127
   return ret;
911✔
128
}
129

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

140
}  // namespace
141

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

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

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

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

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

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

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

171
size_t base32_encode_max_output(size_t input_length) {
×
172
   return Base32::encode_max_output(input_length);
×
173
}
174

175
size_t base32_decode_max_output(size_t input_length) {
×
176
   return Base32::decode_max_output(input_length);
×
177
}
178

179
}  // 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