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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

93.9
/src/lib/codec/base64/base64.cpp
1
/*
2
* Base64 Encoding and Decoding
3
* (C) 2010,2015,2020 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/base64.h>
9

10
#include <botan/exceptn.h>
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/rounding.h>
16

17
namespace Botan {
18

19
namespace {
20

21
class Base64 final {
22
   public:
23
      static inline std::string name() noexcept { return "base64"; }
14✔
24

25
      static inline size_t encoding_bytes_in() noexcept { return m_encoding_bytes_in; }
26

27
      static inline size_t encoding_bytes_out() noexcept { return m_encoding_bytes_out; }
28

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

31
      static inline size_t decoding_bytes_out() noexcept { return m_encoding_bytes_in; }
32

33
      static inline size_t bits_consumed() noexcept { return m_encoding_bits; }
34

35
      static inline size_t remaining_bits_before_padding() noexcept { return m_remaining_bits_before_padding; }
36

37
      static inline size_t encode_max_output(size_t input_length) {
2,131✔
38
         return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out;
3,986✔
39
      }
40

41
      static inline size_t decode_max_output(size_t input_length) {
22,912✔
42
         return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out;
41,540✔
43
      }
44

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

47
      static uint8_t lookup_binary_value(char input) noexcept;
48

49
      static bool check_bad_char(uint8_t bin, char input, bool ignore_ws);
50

51
      static void decode(uint8_t* out_ptr, const uint8_t decode_buf[4]) {
3,857,806✔
52
         out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
3,857,806✔
53
         out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
3,857,806✔
54
         out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
3,857,806✔
55
      }
56

57
      static inline size_t bytes_to_remove(size_t final_truncate) { return final_truncate; }
58

59
   private:
60
      static const size_t m_encoding_bits = 6;
61
      static const size_t m_remaining_bits_before_padding = 8;
62

63
      static const size_t m_encoding_bytes_in = 3;
64
      static const size_t m_encoding_bytes_out = 4;
65
};
66

67
char lookup_base64_char(uint8_t x) {
2,324,356✔
68
   BOTAN_DEBUG_ASSERT(x < 64);
2,324,356✔
69

70
   const auto in_AZ = CT::Mask<uint8_t>::is_lt(x, 26);
2,324,356✔
71
   const auto in_09 = CT::Mask<uint8_t>::is_within_range(x, 52, 61);
2,324,356✔
72
   const auto eq_plus = CT::Mask<uint8_t>::is_equal(x, 62);
2,324,356✔
73
   const auto eq_slash = CT::Mask<uint8_t>::is_equal(x, 63);
2,324,356✔
74

75
   const char c_AZ = 'A' + x;
2,324,356✔
76
   const char c_az = 'a' + (x - 26);
2,324,356✔
77
   const char c_09 = '0' + (x - 2 * 26);
2,324,356✔
78
   const char c_plus = '+';
2,324,356✔
79
   const char c_slash = '/';
2,324,356✔
80

81
   char ret = c_az;
2,324,356✔
82
   ret = in_AZ.select(c_AZ, ret);
2,324,356✔
83
   ret = in_09.select(c_09, ret);
2,324,356✔
84
   ret = eq_plus.select(c_plus, ret);
2,324,356✔
85
   ret = eq_slash.select(c_slash, ret);
2,324,356✔
86

87
   return ret;
2,324,356✔
88
}
89

90
//static
91
void Base64::encode(char out[8], const uint8_t in[5]) noexcept {
581,089✔
92
   const uint8_t b0 = (in[0] & 0xFC) >> 2;
581,089✔
93
   const uint8_t b1 = ((in[0] & 0x03) << 4) | (in[1] >> 4);
581,089✔
94
   const uint8_t b2 = ((in[1] & 0x0F) << 2) | (in[2] >> 6);
581,089✔
95
   const uint8_t b3 = in[2] & 0x3F;
581,089✔
96
   out[0] = lookup_base64_char(b0);
581,089✔
97
   out[1] = lookup_base64_char(b1);
581,089✔
98
   out[2] = lookup_base64_char(b2);
581,089✔
99
   out[3] = lookup_base64_char(b3);
581,089✔
100
}
581,089✔
101

102
//static
103
uint8_t Base64::lookup_binary_value(char input) noexcept {
15,696,533✔
104
   const uint8_t c = static_cast<uint8_t>(input);
15,696,533✔
105

106
   const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z'));
15,696,533✔
107
   const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('z'));
15,696,533✔
108
   const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9'));
15,696,533✔
109

110
   const auto is_plus = CT::Mask<uint8_t>::is_equal(c, uint8_t('+'));
15,696,533✔
111
   const auto is_slash = CT::Mask<uint8_t>::is_equal(c, uint8_t('/'));
15,696,533✔
112
   const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('='));
15,696,533✔
113

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

117
   const uint8_t c_upper = c - uint8_t('A');
15,696,533✔
118
   const uint8_t c_lower = c - uint8_t('a') + 26;
15,696,533✔
119
   const uint8_t c_decim = c - uint8_t('0') + 2 * 26;
15,696,533✔
120

121
   uint8_t ret = 0xFF;  // default value
15,696,533✔
122

123
   ret = is_alpha_upper.select(c_upper, ret);
15,696,533✔
124
   ret = is_alpha_lower.select(c_lower, ret);
15,696,533✔
125
   ret = is_decimal.select(c_decim, ret);
15,696,533✔
126
   ret = is_plus.select(62, ret);
15,696,533✔
127
   ret = is_slash.select(63, ret);
15,696,533✔
128
   ret = is_equal.select(0x81, ret);
15,696,533✔
129
   ret = is_whitespace.select(0x80, ret);
15,696,533✔
130

131
   return ret;
15,696,533✔
132
}
133

134
//static
135
bool Base64::check_bad_char(uint8_t bin, char input, bool ignore_ws) {
15,690,527✔
136
   if(bin <= 0x3F) {
15,690,527✔
137
      return true;
138
   } else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) {
268,810✔
139
      throw Invalid_Argument(fmt("base64_decode: invalid character '{}'", format_char_for_display(input)));
76✔
140
   }
141
   return false;
142
}
143

144
}
145

146
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t& input_consumed, bool final_inputs) {
8✔
147
   return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs);
8✔
148
}
149

150
std::string base64_encode(const uint8_t input[], size_t input_length) {
2,131✔
151
   return base_encode_to_string(Base64(), input, input_length);
2,131✔
152
}
153

154
size_t base64_decode(
2✔
155
   uint8_t out[], const char in[], size_t input_length, size_t& input_consumed, bool final_inputs, bool ignore_ws) {
156
   return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
2✔
157
}
158

159
size_t base64_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
17✔
160
   return base_decode_full(Base64(), output, input, input_length, ignore_ws);
17✔
161
}
162

163
size_t base64_decode(uint8_t output[], std::string_view input, bool ignore_ws) {
17✔
164
   return base64_decode(output, input.data(), input.length(), ignore_ws);
17✔
165
}
166

167
size_t base64_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws) {
×
168
   if(output.size() < base64_decode_max_output(input.size())) {
×
169
      throw Invalid_Argument("base64_decode: output buffer is too short");
×
170
   }
171
   return base64_decode(output.data(), input.data(), input.length(), ignore_ws);
×
172
}
173

174
secure_vector<uint8_t> base64_decode(const char input[], size_t input_length, bool ignore_ws) {
11,437✔
175
   return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws);
11,437✔
176
}
177

178
secure_vector<uint8_t> base64_decode(std::string_view input, bool ignore_ws) {
659✔
179
   return base64_decode(input.data(), input.size(), ignore_ws);
659✔
180
}
181

182
size_t base64_encode_max_output(size_t input_length) { return Base64::encode_max_output(input_length); }
×
183

184
size_t base64_decode_max_output(size_t input_length) { return Base64::decode_max_output(input_length); }
19✔
185

186
}
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

© 2025 Coveralls, Inc