• 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

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

8
#include <botan/hex.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/mem_ops.h>
12
#include <botan/internal/charset.h>
13
#include <botan/internal/ct_utils.h>
14
#include <botan/internal/fmt.h>
15

16
namespace Botan {
17

18
namespace {
19

20
char hex_encode_nibble(uint8_t n, bool uppercase) {
1,801,692✔
21
   BOTAN_DEBUG_ASSERT(n <= 15);
1,801,692✔
22

23
   const auto in_09 = CT::Mask<uint8_t>::is_lt(n, 10);
1,801,692✔
24

25
   const char c_09 = n + '0';
1,801,692✔
26
   const char c_af = n + (uppercase ? 'A' : 'a') - 10;
900,846✔
27

28
   return in_09.select(c_09, c_af);
900,846✔
29
}
30

31
}
32

33
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase) {
31,480✔
34
   for(size_t i = 0; i != input_length; ++i) {
932,326✔
35
      const uint8_t n0 = (input[i] >> 4) & 0xF;
900,846✔
36
      const uint8_t n1 = (input[i]) & 0xF;
900,846✔
37

38
      output[2 * i] = hex_encode_nibble(n0, uppercase);
900,846✔
39
      output[2 * i + 1] = hex_encode_nibble(n1, uppercase);
900,846✔
40
   }
41
}
31,480✔
42

43
std::string hex_encode(const uint8_t input[], size_t input_length, bool uppercase) {
31,989✔
44
   std::string output(2 * input_length, 0);
31,989✔
45

46
   if(input_length)
31,989✔
47
      hex_encode(&output.front(), input, input_length, uppercase);
31,440✔
48

49
   return output;
31,989✔
50
}
×
51

52
namespace {
53

54
uint8_t hex_char_to_bin(char input) {
33,766,944✔
55
   const uint8_t c = static_cast<uint8_t>(input);
33,766,944✔
56

57
   const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('F'));
33,766,944✔
58
   const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('f'));
33,766,944✔
59
   const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9'));
33,766,944✔
60

61
   const auto is_whitespace =
33,766,944✔
62
      CT::Mask<uint8_t>::is_any_of(c, {uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')});
33,766,944✔
63

64
   const uint8_t c_upper = c - uint8_t('A') + 10;
33,766,944✔
65
   const uint8_t c_lower = c - uint8_t('a') + 10;
33,766,944✔
66
   const uint8_t c_decim = c - uint8_t('0');
33,766,944✔
67

68
   uint8_t ret = 0xFF;  // default value
33,766,944✔
69

70
   ret = is_alpha_upper.select(c_upper, ret);
33,766,944✔
71
   ret = is_alpha_lower.select(c_lower, ret);
33,766,944✔
72
   ret = is_decimal.select(c_decim, ret);
33,766,944✔
73
   ret = is_whitespace.select(0x80, ret);
33,766,944✔
74

75
   return ret;
33,766,944✔
76
}
77

78
}
79

80
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t& input_consumed, bool ignore_ws) {
206,451✔
81
   uint8_t* out_ptr = output;
206,451✔
82
   bool top_nibble = true;
206,451✔
83

84
   clear_mem(output, input_length / 2);
206,451✔
85

86
   for(size_t i = 0; i != input_length; ++i) {
33,973,395✔
87
      const uint8_t bin = hex_char_to_bin(input[i]);
33,766,944✔
88

89
      if(bin >= 0x10) {
33,766,944✔
90
         if(bin == 0x80 && ignore_ws)
9,176✔
91
            continue;
9,176✔
92

93
         throw Invalid_Argument(fmt("hex_decode: invalid character '{}'", format_char_for_display(input[i])));
×
94
      }
95

96
      if(top_nibble)
33,757,768✔
97
         *out_ptr |= bin << 4;
16,878,884✔
98
      else
99
         *out_ptr |= bin;
16,878,884✔
100

101
      top_nibble = !top_nibble;
33,757,768✔
102
      if(top_nibble)
33,757,768✔
103
         ++out_ptr;
16,878,884✔
104
   }
105

106
   input_consumed = input_length;
206,451✔
107
   size_t written = (out_ptr - output);
206,451✔
108

109
   /*
110
   * We only got half of a uint8_t at the end; zap the half-written
111
   * output and mark it as unread
112
   */
113
   if(!top_nibble) {
206,451✔
114
      *out_ptr = 0;
×
115
      input_consumed -= 1;
×
116
   }
117

118
   return written;
206,451✔
119
}
120

121
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
206,405✔
122
   size_t consumed = 0;
206,405✔
123
   size_t written = hex_decode(output, input, input_length, consumed, ignore_ws);
206,405✔
124

125
   if(consumed != input_length)
206,405✔
126
      throw Invalid_Argument("hex_decode: input did not have full bytes");
×
127

128
   return written;
206,405✔
129
}
130

131
size_t hex_decode(uint8_t output[], std::string_view input, bool ignore_ws) {
15,076✔
132
   return hex_decode(output, input.data(), input.length(), ignore_ws);
15,076✔
133
}
134

135
size_t hex_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws) {
×
136
   return hex_decode(output.data(), input.data(), input.length(), ignore_ws);
×
137
}
138

139
secure_vector<uint8_t> hex_decode_locked(const char input[], size_t input_length, bool ignore_ws) {
42,633✔
140
   secure_vector<uint8_t> bin(1 + input_length / 2);
42,633✔
141

142
   size_t written = hex_decode(bin.data(), input, input_length, ignore_ws);
42,633✔
143

144
   bin.resize(written);
42,633✔
145
   return bin;
42,633✔
146
}
×
147

148
secure_vector<uint8_t> hex_decode_locked(std::string_view input, bool ignore_ws) {
83✔
149
   return hex_decode_locked(input.data(), input.size(), ignore_ws);
83✔
150
}
151

152
std::vector<uint8_t> hex_decode(const char input[], size_t input_length, bool ignore_ws) {
148,696✔
153
   std::vector<uint8_t> bin(1 + input_length / 2);
148,696✔
154

155
   size_t written = hex_decode(bin.data(), input, input_length, ignore_ws);
148,696✔
156

157
   bin.resize(written);
148,696✔
158
   return bin;
148,696✔
159
}
×
160

161
std::vector<uint8_t> hex_decode(std::string_view input, bool ignore_ws) {
148,688✔
162
   return hex_decode(input.data(), input.size(), ignore_ws);
148,688✔
163
}
164

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