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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

87.67
/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/fmt.h>
14
#include <botan/internal/int_utils.h>
15
#include <botan/internal/loadstor.h>
16

17
namespace Botan {
18

19
namespace {
20

21
uint16_t hex_encode_2nibble(uint8_t n8, bool uppercase) {
1,235,354✔
22
   // Offset for upper or lower case 'a' resp
23
   const uint16_t a_mask = uppercase ? 0x0707 : 0x2727;
2,470,708✔
24

25
   const uint16_t n = (static_cast<uint16_t>(n8 & 0xF0) << 4) | (n8 & 0x0F);
1,235,354✔
26
   // n >= 10? If so add offset
27
   const uint16_t diff = swar_lt<uint16_t>(0x0909, n) & a_mask;
1,235,354✔
28
   // Can't overflow between bytes, so don't need explicit SWAR addition:
29
   return n + 0x3030 + diff;
1,235,354✔
30
}
31

32
}  // namespace
33

34
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase) {
44,279✔
35
   for(size_t i = 0; i != input_length; ++i) {
1,279,633✔
36
      const uint16_t h = hex_encode_2nibble(input[i], uppercase);
1,235,354✔
37
      output[2 * i] = get_byte<0>(h);
1,235,354✔
38
      output[2 * i + 1] = get_byte<1>(h);
1,235,354✔
39
   }
40
}
44,279✔
41

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

45
   if(input_length > 0) {
44,815✔
46
      hex_encode(&output.front(), input, input_length, uppercase);
44,239✔
47
   }
48

49
   return output;
44,815✔
50
}
×
51

52
namespace {
53

54
uint8_t hex_char_to_bin(char input) {
33,307,340✔
55
   // Starts of valid value ranges (v_lo) and their lengths (v_range)
56
   constexpr uint64_t v_lo = make_uint64(0, '0', 'a', 'A', ' ', '\n', '\t', '\r');
33,307,340✔
57
   constexpr uint64_t v_range = make_uint64(0, 10, 6, 6, 1, 1, 1, 1);
33,307,340✔
58

59
   const uint8_t x = static_cast<uint8_t>(input);
33,307,340✔
60
   const uint64_t x8 = x * 0x0101010101010101;
33,307,340✔
61

62
   const uint64_t v_mask = swar_in_range<uint64_t>(x8, v_lo, v_range) ^ 0x8000000000000000;
33,307,340✔
63

64
   // This is the offset added to x to get the value we need
65
   const uint64_t val_v = 0xd0a9c960767773 ^ static_cast<uint64_t>(0xFF - x) << 56;
33,307,340✔
66

67
   return x + static_cast<uint8_t>(val_v >> (8 * index_of_first_set_byte(v_mask)));
33,307,340✔
68
}
69

70
}  // namespace
71

72
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t& input_consumed, bool ignore_ws) {
235,221✔
73
   uint8_t* out_ptr = output;
235,221✔
74
   bool top_nibble = true;
235,221✔
75

76
   clear_mem(output, input_length / 2);
235,221✔
77

78
   for(size_t i = 0; i != input_length; ++i) {
33,542,561✔
79
      const uint8_t bin = hex_char_to_bin(input[i]);
33,307,340✔
80

81
      if(bin >= 0x10) {
33,307,340✔
82
         if(bin == 0x80 && ignore_ws) {
9,536✔
83
            continue;
9,536✔
84
         }
85

86
         throw Invalid_Argument(fmt("hex_decode: invalid character '{}'", format_char_for_display(input[i])));
×
87
      }
88

89
      if(top_nibble) {
33,297,804✔
90
         *out_ptr |= bin << 4;
16,648,902✔
91
      } else {
92
         *out_ptr |= bin;
16,648,902✔
93
      }
94

95
      top_nibble = !top_nibble;
33,297,804✔
96
      if(top_nibble) {
33,297,804✔
97
         ++out_ptr;
16,648,902✔
98
      }
99
   }
100

101
   input_consumed = input_length;
235,221✔
102
   size_t written = (out_ptr - output);
235,221✔
103

104
   /*
105
   * We only got half of a uint8_t at the end; zap the half-written
106
   * output and mark it as unread
107
   */
108
   if(!top_nibble) {
235,221✔
109
      *out_ptr = 0;
×
110
      input_consumed -= 1;
×
111
   }
112

113
   return written;
235,221✔
114
}
115

116
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
235,175✔
117
   size_t consumed = 0;
235,175✔
118
   size_t written = hex_decode(output, input, input_length, consumed, ignore_ws);
235,175✔
119

120
   if(consumed != input_length) {
235,175✔
121
      throw Invalid_Argument("hex_decode: input did not have full bytes");
×
122
   }
123

124
   return written;
235,175✔
125
}
126

127
size_t hex_decode(uint8_t output[], std::string_view input, bool ignore_ws) {
12,579✔
128
   return hex_decode(output, input.data(), input.length(), ignore_ws);
12,579✔
129
}
130

131
size_t hex_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws) {
×
132
   return hex_decode(output.data(), input.data(), input.length(), ignore_ws);
×
133
}
134

135
secure_vector<uint8_t> hex_decode_locked(const char input[], size_t input_length, bool ignore_ws) {
48,195✔
136
   secure_vector<uint8_t> bin(1 + input_length / 2);
48,195✔
137

138
   size_t written = hex_decode(bin.data(), input, input_length, ignore_ws);
48,195✔
139

140
   bin.resize(written);
48,195✔
141
   return bin;
48,195✔
142
}
×
143

144
secure_vector<uint8_t> hex_decode_locked(std::string_view input, bool ignore_ws) {
2,736✔
145
   return hex_decode_locked(input.data(), input.size(), ignore_ws);
2,736✔
146
}
147

148
std::vector<uint8_t> hex_decode(const char input[], size_t input_length, bool ignore_ws) {
174,401✔
149
   std::vector<uint8_t> bin(1 + input_length / 2);
174,401✔
150

151
   size_t written = hex_decode(bin.data(), input, input_length, ignore_ws);
174,401✔
152

153
   bin.resize(written);
174,401✔
154
   return bin;
174,401✔
155
}
×
156

157
std::vector<uint8_t> hex_decode(std::string_view input, bool ignore_ws) {
174,393✔
158
   return hex_decode(input.data(), input.size(), ignore_ws);
174,393✔
159
}
160

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