Coverage Report

Created: 2025-07-01 06:18

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