/src/WasmEdge/lib/common/hexstr.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "common/hexstr.h" |
5 | | |
6 | | #include <algorithm> |
7 | | #include <spdlog/fmt/fmt.h> |
8 | | |
9 | | using namespace std::literals; |
10 | | |
11 | | namespace WasmEdge { |
12 | | |
13 | 0 | uint8_t convertCharToHex(const char C) { |
14 | 0 | if (C >= '0' && C <= '9') { |
15 | 0 | return static_cast<uint8_t>(C - '0') + UINT8_C(0); |
16 | 0 | } |
17 | 0 | if (C >= 'a' && C <= 'f') { |
18 | 0 | return static_cast<uint8_t>(C - 'a') + UINT8_C(10); |
19 | 0 | } |
20 | 0 | if (C >= 'A' && C <= 'F') { |
21 | 0 | return static_cast<uint8_t>(C - 'A') + UINT8_C(10); |
22 | 0 | } |
23 | 0 | return UINT8_C(0); |
24 | 0 | } |
25 | | |
26 | | void convertBytesToHexStr(Span<const uint8_t> Src, std::string &Dst, |
27 | 0 | const uint32_t Padding, const bool IsLittleEndian) { |
28 | 0 | Dst.clear(); |
29 | 0 | Dst.reserve(Src.size() * 2); |
30 | 0 | if (IsLittleEndian) { |
31 | 0 | for (auto It = Src.rbegin(); It != Src.rend(); It++) { |
32 | 0 | Dst += fmt::format("{:02x}"sv, *It); |
33 | 0 | } |
34 | 0 | } else { |
35 | 0 | for (auto It = Src.begin(); It != Src.end(); It++) { |
36 | 0 | Dst += fmt::format("{:02x}"sv, *It); |
37 | 0 | } |
38 | 0 | } |
39 | 0 | if (Dst.length() < Padding) { |
40 | 0 | Dst = std::string(Padding - Dst.length(), '0').append(Dst); |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | | void convertValVecToHexStr(Span<const uint8_t> Src, std::string &Dst, |
45 | 0 | const uint32_t Padding) { |
46 | 0 | convertBytesToHexStr(Src, Dst, Padding, true); |
47 | 0 | } |
48 | | |
49 | | void convertHexStrToBytes(std::string_view Src, std::vector<uint8_t> &Dst, |
50 | 0 | uint32_t Padding, const bool IsLittleEndian) { |
51 | 0 | if (Padding & 0x01U) { |
52 | 0 | Padding++; |
53 | 0 | } |
54 | 0 | Dst.clear(); |
55 | 0 | if (Src.size() == 0) { |
56 | 0 | return; |
57 | 0 | } |
58 | 0 | std::string S(Src); |
59 | 0 | if (S.length() < Padding) { |
60 | 0 | S = std::string(Padding - S.length(), '0').append(S); |
61 | 0 | } |
62 | 0 | if (S.length() & 0x01U) { |
63 | 0 | S = '0' + S; |
64 | 0 | } |
65 | 0 | Dst.reserve(S.size() / 2); |
66 | 0 | if (IsLittleEndian) { |
67 | 0 | for (auto It = S.crbegin(); It != S.crend(); It += 2) { |
68 | 0 | uint8_t CL = convertCharToHex(*It); |
69 | 0 | uint8_t CH = convertCharToHex(*(It + 1)) * static_cast<uint8_t>(16); |
70 | 0 | Dst.push_back(CL + CH); |
71 | 0 | } |
72 | 0 | } else { |
73 | 0 | for (auto It = S.cbegin(); It != S.cend(); It += 2) { |
74 | 0 | uint8_t CH = convertCharToHex(*It) * static_cast<uint8_t>(16); |
75 | 0 | uint8_t CL = convertCharToHex(*(It + 1)); |
76 | 0 | Dst.push_back(CL + CH); |
77 | 0 | } |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | void convertHexStrToValVec(std::string_view Src, std::vector<uint8_t> &Dst, |
82 | 0 | const uint32_t Padding) { |
83 | 0 | convertHexStrToBytes(Src, Dst, Padding); |
84 | 0 | } |
85 | | |
86 | 0 | std::string convertUIntToHexStr(const uint64_t Num, uint32_t MinLen) { |
87 | | return fmt::format("0x{:0{}x}"sv, Num, std::min(MinLen, UINT32_C(16))); |
88 | 0 | } |
89 | | |
90 | | } // namespace WasmEdge |