A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | #include "SymbolDatabase.h" |
| 2 | #include <iomanip> |
| 3 | #include <sstream> |
| 4 | |
| 5 | namespace ShadPKG::Decompiler::Analysis { |
| 6 | |
| 7 | void SymbolDatabase::addSymbol(uint64_t address, const std::string &name, |
| 8 | SymbolType type, SymbolSource source) { |
| 9 | std::lock_guard<std::mutex> lock(mutex_); |
| 10 | |
| 11 | // If symbol exists and is User-defined, don't overwrite with Auto unless |
| 12 | // forced? For now, if source is Auto and existing is User, ignore. |
| 13 | if (symbols_.count(address)) { |
| 14 | if (symbols_[address].source == SymbolSource::User && |
| 15 | source == SymbolSource::Auto) { |
| 16 | return; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | SymbolInfo info; |
| 21 | info.address = address; |
| 22 | info.name = name; |
| 23 | info.type = type; |
| 24 | info.source = source; |
| 25 | |
| 26 | if (symbols_.count(address)) { |
| 27 | // Preserve original name if we are renaming manually? |
| 28 | // This is a simplification. |
| 29 | if (symbols_[address].originalName.empty()) { |
| 30 | info.originalName = symbols_[address].name; |
| 31 | } else { |
| 32 | info.originalName = symbols_[address].originalName; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | symbols_[address] = info; |
| 37 | } |
| 38 | |
| 39 | void SymbolDatabase::renameSymbol(uint64_t address, |
| 40 | const std::string &newName) { |
| 41 | std::lock_guard<std::mutex> lock(mutex_); |
| 42 | |
| 43 | if (symbols_.count(address)) { |
| 44 | symbols_[address].name = newName; |
| 45 | symbols_[address].source = SymbolSource::User; |
| 46 | } else { |
| 47 | // Create new entry if it didn't exist (e.g. renaming an arbitrary address) |
| 48 | addSymbol(address, newName, SymbolType::Label, SymbolSource::User); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | std::optional<SymbolInfo> SymbolDatabase::getSymbol(uint64_t address) const { |
| 53 | std::lock_guard<std::mutex> lock(mutex_); |
| 54 | auto it = symbols_.find(address); |
| 55 | if (it != symbols_.end()) { |
| 56 | return it->second; |
| 57 | } |
| 58 | return std::nullopt; |
| 59 | } |
| 60 | |
| 61 | std::string SymbolDatabase::getSymbolName(uint64_t address) const { |
| 62 | std::lock_guard<std::mutex> lock(mutex_); |
| 63 | auto it = symbols_.find(address); |
| 64 | if (it != symbols_.end()) { |
| 65 | return it->second.name; |
| 66 | } |
| 67 | |
| 68 | // Fallback naming |
| 69 | std::stringstream ss; |
| 70 | ss << "loc_" << std::hex << address; |
| 71 | return ss.str(); |
| 72 | } |
| 73 | |
| 74 | void SymbolDatabase::addXRef(uint64_t source, uint64_t target, XRefType type) { |
| 75 | std::lock_guard<std::mutex> lock(mutex_); |
| 76 | |
| 77 | XRef xref{source, target, type}; |
| 78 | xrefsTo_[target].push_back(xref); |
| 79 | xrefsFrom_[source].push_back(xref); |
| 80 | } |
| 81 | |
| 82 | std::vector<XRef> SymbolDatabase::getXRefsTo(uint64_t target) const { |
| 83 | std::lock_guard<std::mutex> lock(mutex_); |
| 84 | if (xrefsTo_.count(target)) { |
| 85 | return xrefsTo_.at(target); |
| 86 | } |
| 87 | return {}; |
| 88 | } |
| 89 | |
| 90 | std::vector<XRef> SymbolDatabase::getXRefsFrom(uint64_t source) const { |
| 91 | std::lock_guard<std::mutex> lock(mutex_); |
| 92 | if (xrefsFrom_.count(source)) { |
| 93 | return xrefsFrom_.at(source); |
| 94 | } |
| 95 | return {}; |
| 96 | } |
| 97 | |
| 98 | void SymbolDatabase::clear() { |
| 99 | std::lock_guard<std::mutex> lock(mutex_); |
| 100 | symbols_.clear(); |
| 101 | xrefsTo_.clear(); |
| 102 | xrefsFrom_.clear(); |
| 103 | } |
| 104 | |
| 105 | } // namespace ShadPKG::Decompiler::Analysis |
| 106 |