pwn++  0.1.4
A (toy) Windows & Linux pwn library to play with modern C++.
Architecture.hpp
1 #pragma once
2 
3 
4 #include "Common.hpp"
5 #include "Log.hpp"
6 
10 enum class Endianess : uint8_t
11 {
12  unknown,
13  little,
14  big,
15 };
16 
24 std::ostream&
25 operator<<(std::ostream& os, Endianess e);
26 
34 std::wostream&
35 operator<<(std::wostream& wos, Endianess e);
36 
37 
41 enum class ArchitectureType : uint8_t
42 {
43  unknown,
44  x86,
45  x64,
46  arm64,
47  arm,
48  arm_thumb,
49  mips,
50  mips64,
51  max
52 };
53 
54 
59 {
60  std::string_view name {};
61  ArchitectureType id {};
62  usize ptrsize {};
63  Endianess endian {};
64  std::array<std::string_view, 4> aliases {};
65 
66  auto
67  operator<=>(Architecture const& other) const = default;
68 
76  friend std::ostream&
77  operator<<(std::ostream& os, Architecture const& a);
78 
86  friend std::wostream&
87  operator<<(std::wostream& wos, Architecture const& a);
88 
91  // if not found.
98  static Architecture const&
99  Find(std::string_view const& architecture_name);
100 };
101 
102 
106 static constexpr CMap<ArchitectureType, Architecture, 4> Architectures {
107  {{
108  {ArchitectureType::x64,
109  {"x64"sv,
110  ArchitectureType::x64,
111  8,
112  Endianess::little,
113  {
114  "x86-64"sv,
115  }}},
116  {ArchitectureType::x86,
117  {"x86"sv,
118  ArchitectureType::x86,
119  4,
120  Endianess::little,
121  {
122  "i386"sv,
123  }}},
124  {ArchitectureType::arm64, {"arm64"sv, ArchitectureType::arm64, 8, Endianess::little, {"aarch64"sv}}},
125  {ArchitectureType::arm, {"arm"sv, ArchitectureType::arm, 4, Endianess::little}},
126  }},
127 };
128 
129 
135 template<>
136 struct std::formatter<Endianess, char> : std::formatter<std::string, char>
137 {
138  template<typename FormatContext>
139  auto
140  format(Endianess a, FormatContext& ctx)
141  {
142  const char* e = (a == Endianess::little) ? "LITTLE" : (a == Endianess::big) ? "BIG" : "UNKNOWN";
143  return std::formatter<std::string, char>::format(std::string(e), ctx);
144  }
145 };
146 
147 
153 template<>
154 struct std::formatter<Architecture, char> : std::formatter<std::string, char>
155 {
156  template<typename FormatContext>
157  auto
158  format(const Architecture& a, FormatContext& ctx) const
159  {
160  return std::formatter<std::string, char>::format(std::string(a.name), ctx);
161  }
162 };
Logging header: Wide string support on linux is at best flaky, so only Windows version gets both stri...
Architecture class definition, with its wstring representation.
Definition: Architecture.hpp:59
friend std::ostream & operator<<(std::ostream &os, Architecture const &a)
Output Architecture to std::wostream.
static Architecture const & Find(std::string_view const &architecture_name)
Find an architecture by name. The function will throw std::range_error
A constexpr map https://xuhuisun.com/post/c++-weekly-2-constexpr-map/.
Definition: Common.hpp:146