Coverage Report

Created: 2021-02-21 07:20

/src/botan/src/lib/codec/hex/hex.cpp
Line
Count
Source (jump to first uncovered line)
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
#include <botan/mem_ops.h>
10
#include <botan/exceptn.h>
11
#include <botan/internal/ct_utils.h>
12
13
namespace Botan {
14
15
namespace {
16
17
char hex_encode_nibble(uint8_t n, bool uppercase)
18
1.42M
   {
19
1.42M
   BOTAN_DEBUG_ASSERT(n <= 15);
20
21
1.42M
   const auto in_09 = CT::Mask<uint8_t>::is_lt(n, 10);
22
23
1.42M
   const char c_09 = n + '0';
24
1.42M
   const char c_af = n + (uppercase ? 'A' : 'a') - 10;
25
26
1.42M
   return in_09.select(c_09, c_af);
27
1.42M
   }
28
29
}
30
31
void hex_encode(char output[],
32
                const uint8_t input[],
33
                size_t input_length,
34
                bool uppercase)
35
26.5k
   {
36
740k
   for(size_t i = 0; i != input_length; ++i)
37
713k
      {
38
713k
      const uint8_t n0 = (input[i] >> 4) & 0xF;
39
713k
      const uint8_t n1 = (input[i]     ) & 0xF;
40
41
713k
      output[2*i  ] = hex_encode_nibble(n0, uppercase);
42
713k
      output[2*i+1] = hex_encode_nibble(n1, uppercase);
43
713k
      }
44
26.5k
   }
45
46
std::string hex_encode(const uint8_t input[],
47
                       size_t input_length,
48
                       bool uppercase)
49
26.5k
   {
50
26.5k
   std::string output(2 * input_length, 0);
51
52
26.5k
   if(input_length)
53
26.5k
      hex_encode(&output.front(), input, input_length, uppercase);
54
55
26.5k
   return output;
56
26.5k
   }
57
58
namespace {
59
60
uint8_t hex_char_to_bin(char input)
61
1.88M
   {
62
1.88M
   const uint8_t c = static_cast<uint8_t>(input);
63
64
1.88M
   const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('F'));
65
1.88M
   const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('f'));
66
1.88M
   const auto is_decimal     = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9'));
67
68
1.88M
   const auto is_whitespace  = CT::Mask<uint8_t>::is_any_of(c, {
69
1.88M
         uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')
70
1.88M
      });
71
72
1.88M
   const uint8_t c_upper = c - uint8_t('A') + 10;
73
1.88M
   const uint8_t c_lower = c - uint8_t('a') + 10;
74
1.88M
   const uint8_t c_decim = c - uint8_t('0');
75
76
1.88M
   uint8_t ret = 0xFF; // default value
77
78
1.88M
   ret = is_alpha_upper.select(c_upper, ret);
79
1.88M
   ret = is_alpha_lower.select(c_lower, ret);
80
1.88M
   ret = is_decimal.select(c_decim, ret);
81
1.88M
   ret = is_whitespace.select(0x80, ret);
82
83
1.88M
   return ret;
84
1.88M
   }
85
86
}
87
88
89
size_t hex_decode(uint8_t output[],
90
                  const char input[],
91
                  size_t input_length,
92
                  size_t& input_consumed,
93
                  bool ignore_ws)
94
52.3k
   {
95
52.3k
   uint8_t* out_ptr = output;
96
52.3k
   bool top_nibble = true;
97
98
52.3k
   clear_mem(output, input_length / 2);
99
100
1.93M
   for(size_t i = 0; i != input_length; ++i)
101
1.88M
      {
102
1.88M
      const uint8_t bin = hex_char_to_bin(input[i]);
103
104
1.88M
      if(bin >= 0x10)
105
0
         {
106
0
         if(bin == 0x80 && ignore_ws)
107
0
            continue;
108
109
0
         std::string bad_char(1, input[i]);
110
0
         if(bad_char == "\t")
111
0
           bad_char = "\\t";
112
0
         else if(bad_char == "\n")
113
0
           bad_char = "\\n";
114
115
0
         throw Invalid_Argument(
116
0
           std::string("hex_decode: invalid hex character '") +
117
0
           bad_char + "'");
118
0
         }
119
120
1.88M
      if(top_nibble)
121
943k
         *out_ptr |= bin << 4;
122
943k
      else
123
943k
         *out_ptr |= bin;
124
125
1.88M
      top_nibble = !top_nibble;
126
1.88M
      if(top_nibble)
127
943k
         ++out_ptr;
128
1.88M
      }
129
130
52.3k
   input_consumed = input_length;
131
52.3k
   size_t written = (out_ptr - output);
132
133
   /*
134
   * We only got half of a uint8_t at the end; zap the half-written
135
   * output and mark it as unread
136
   */
137
52.3k
   if(!top_nibble)
138
0
      {
139
0
      *out_ptr = 0;
140
0
      input_consumed -= 1;
141
0
      }
142
143
52.3k
   return written;
144
52.3k
   }
145
146
size_t hex_decode(uint8_t output[],
147
                  const char input[],
148
                  size_t input_length,
149
                  bool ignore_ws)
150
52.3k
   {
151
52.3k
   size_t consumed = 0;
152
52.3k
   size_t written = hex_decode(output, input, input_length,
153
52.3k
                               consumed, ignore_ws);
154
155
52.3k
   if(consumed != input_length)
156
0
      throw Invalid_Argument("hex_decode: input did not have full bytes");
157
158
52.3k
   return written;
159
52.3k
   }
160
161
size_t hex_decode(uint8_t output[],
162
                  const std::string& input,
163
                  bool ignore_ws)
164
43.2k
   {
165
43.2k
   return hex_decode(output, input.data(), input.length(), ignore_ws);
166
43.2k
   }
167
168
secure_vector<uint8_t> hex_decode_locked(const char input[],
169
                                      size_t input_length,
170
                                      bool ignore_ws)
171
9.14k
   {
172
9.14k
   secure_vector<uint8_t> bin(1 + input_length / 2);
173
174
9.14k
   size_t written = hex_decode(bin.data(),
175
9.14k
                               input,
176
9.14k
                               input_length,
177
9.14k
                               ignore_ws);
178
179
9.14k
   bin.resize(written);
180
9.14k
   return bin;
181
9.14k
   }
182
183
secure_vector<uint8_t> hex_decode_locked(const std::string& input,
184
                                      bool ignore_ws)
185
0
   {
186
0
   return hex_decode_locked(input.data(), input.size(), ignore_ws);
187
0
   }
188
189
std::vector<uint8_t> hex_decode(const char input[],
190
                             size_t input_length,
191
                             bool ignore_ws)
192
0
   {
193
0
   std::vector<uint8_t> bin(1 + input_length / 2);
194
195
0
   size_t written = hex_decode(bin.data(),
196
0
                               input,
197
0
                               input_length,
198
0
                               ignore_ws);
199
200
0
   bin.resize(written);
201
0
   return bin;
202
0
   }
203
204
std::vector<uint8_t> hex_decode(const std::string& input,
205
                             bool ignore_ws)
206
0
   {
207
0
   return hex_decode(input.data(), input.size(), ignore_ws);
208
0
   }
209
210
}