|
3 | 3 | // Distributed under the MIT software license, see the accompanying |
4 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | 5 |
|
6 | | -#include <span.h> |
7 | 6 | #include <util/strencodings.h> |
8 | 7 |
|
| 8 | +#include <crypto/hex_base.h> |
| 9 | +#include <span.h> |
| 10 | + |
9 | 11 | #include <array> |
10 | 12 | #include <cassert> |
11 | 13 | #include <cstring> |
@@ -36,29 +38,6 @@ std::string SanitizeString(std::string_view str, int rule) |
36 | 38 | return result; |
37 | 39 | } |
38 | 40 |
|
39 | | -const signed char p_util_hexdigit[256] = |
40 | | -{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
41 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
42 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
43 | | - 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, |
44 | | - -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
45 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
46 | | - -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
47 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
48 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
49 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
50 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
51 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
52 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
53 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
54 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
55 | | - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; |
56 | | - |
57 | | -signed char HexDigit(char c) |
58 | | -{ |
59 | | - return p_util_hexdigit[(unsigned char)c]; |
60 | | -} |
61 | | - |
62 | 41 | bool IsHex(std::string_view str) |
63 | 42 | { |
64 | 43 | for (char c : str) { |
@@ -466,40 +445,6 @@ std::string Capitalize(std::string str) |
466 | 445 | return str; |
467 | 446 | } |
468 | 447 |
|
469 | | -namespace { |
470 | | - |
471 | | -using ByteAsHex = std::array<char, 2>; |
472 | | - |
473 | | -constexpr std::array<ByteAsHex, 256> CreateByteToHexMap() |
474 | | -{ |
475 | | - constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
476 | | - |
477 | | - std::array<ByteAsHex, 256> byte_to_hex{}; |
478 | | - for (size_t i = 0; i < byte_to_hex.size(); ++i) { |
479 | | - byte_to_hex[i][0] = hexmap[i >> 4]; |
480 | | - byte_to_hex[i][1] = hexmap[i & 15]; |
481 | | - } |
482 | | - return byte_to_hex; |
483 | | -} |
484 | | - |
485 | | -} // namespace |
486 | | - |
487 | | -std::string HexStr(const Span<const uint8_t> s) |
488 | | -{ |
489 | | - std::string rv(s.size() * 2, '\0'); |
490 | | - static constexpr auto byte_to_hex = CreateByteToHexMap(); |
491 | | - static_assert(sizeof(byte_to_hex) == 512); |
492 | | - |
493 | | - char* it = rv.data(); |
494 | | - for (uint8_t v : s) { |
495 | | - std::memcpy(it, byte_to_hex[v].data(), 2); |
496 | | - it += 2; |
497 | | - } |
498 | | - |
499 | | - assert(it == rv.data() + rv.size()); |
500 | | - return rv; |
501 | | -} |
502 | | - |
503 | 448 | std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier) |
504 | 449 | { |
505 | 450 | if (str.empty()) { |
|
0 commit comments