Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/enum.h
ShadPKG / common / enum.h
1// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3 
4#pragma once
5 
6#include <type_traits>
7#include "common/shadpkg_types.h"
8 
9#define DECLARE_ENUM_FLAG_OPERATORS(type) \
10 [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \
11 using T = std::underlying_type_t<type>; \
12 return static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \
13 } \
14 [[nodiscard]] constexpr type operator&(type a, type b) noexcept { \
15 using T = std::underlying_type_t<type>; \
16 return static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \
17 } \
18 [[nodiscard]] constexpr type operator^(type a, type b) noexcept { \
19 using T = std::underlying_type_t<type>; \
20 return static_cast<type>(static_cast<T>(a) ^ static_cast<T>(b)); \
21 } \
22 [[nodiscard]] constexpr type operator<<(type a, type b) noexcept { \
23 using T = std::underlying_type_t<type>; \
24 return static_cast<type>(static_cast<T>(a) << static_cast<T>(b)); \
25 } \
26 [[nodiscard]] constexpr type operator>>(type a, type b) noexcept { \
27 using T = std::underlying_type_t<type>; \
28 return static_cast<type>(static_cast<T>(a) >> static_cast<T>(b)); \
29 } \
30 constexpr type& operator|=(type& a, type b) noexcept { \
31 a = a | b; \
32 return a; \
33 } \
34 constexpr type& operator&=(type& a, type b) noexcept { \
35 a = a & b; \
36 return a; \
37 } \
38 constexpr type& operator^=(type& a, type b) noexcept { \
39 a = a ^ b; \
40 return a; \
41 } \
42 constexpr type& operator<<=(type& a, type b) noexcept { \
43 a = a << b; \
44 return a; \
45 } \
46 constexpr type& operator>>=(type& a, type b) noexcept { \
47 a = a >> b; \
48 return a; \
49 } \
50 [[nodiscard]] constexpr type operator~(type key) noexcept { \
51 using T = std::underlying_type_t<type>; \
52 return static_cast<type>(~static_cast<T>(key)); \
53 } \
54 [[nodiscard]] constexpr bool True(type key) noexcept { \
55 using T = std::underlying_type_t<type>; \
56 return static_cast<T>(key) != 0; \
57 } \
58 [[nodiscard]] constexpr bool False(type key) noexcept { \
59 using T = std::underlying_type_t<type>; \
60 return static_cast<T>(key) == 0; \
61 }
62 
63namespace Common {
64 
65template <typename T>
66class Flags {
67public:
68 using IntType = std::underlying_type_t<T>;
69 
70 Flags() {}
71 
72 Flags(IntType t) : m_bits(t) {}
73 
74 template <typename... Tx>
75 Flags(T f, Tx... fx) {
76 this->set(f, fx...);
77 }
78 
79 template <typename... Tx>
80 void set(Tx... fx) {
81 m_bits |= bits(fx...);
82 }
83 
84 void set(Flags flags) {
85 m_bits |= flags.m_bits;
86 }
87 
88 template <typename... Tx>
89 void clr(Tx... fx) {
90 m_bits &= ~bits(fx...);
91 }
92 
93 void clr(Flags flags) {
94 m_bits &= ~flags.m_bits;
95 }
96 
97 template <typename... Tx>
98 bool any(Tx... fx) const {
99 return (m_bits & bits(fx...)) != 0;
100 }
101 
102 template <typename... Tx>
103 bool all(Tx... fx) const {
104 const IntType mask = bits(fx...);
105 return (m_bits & mask) == mask;
106 }
107 
108 bool test(T f) const {
109 return this->any(f);
110 }
111 
112 bool isClear() const {
113 return m_bits == 0;
114 }
115 
116 void clrAll() {
117 m_bits = 0;
118 }
119 
120 u32 raw() const {
121 return m_bits;
122 }
123 
124 Flags operator&(const Flags& other) const {
125 return Flags(m_bits & other.m_bits);
126 }
127 
128 Flags operator|(const Flags& other) const {
129 return Flags(m_bits | other.m_bits);
130 }
131 
132 Flags operator^(const Flags& other) const {
133 return Flags(m_bits ^ other.m_bits);
134 }
135 
136 bool operator==(const Flags& other) const {
137 return m_bits == other.m_bits;
138 }
139 
140 bool operator!=(const Flags& other) const {
141 return m_bits != other.m_bits;
142 }
143 
144private:
145 IntType m_bits = 0;
146 
147 static IntType bit(T f) {
148 return IntType(1) << static_cast<IntType>(f);
149 }
150 
151 template <typename... Tx>
152 static IntType bits(T f, Tx... fx) {
153 return bit(f) | bits(fx...);
154 }
155 
156 static IntType bits() {
157 return 0;
158 }
159};
160 
161} // namespace Common
162