A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project |
| 2 | // SPDX-FileCopyrightText: 2014 Citra Emulator Project |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | |
| 5 | #include <cstddef> |
| 6 | #ifdef _WIN32 |
| 7 | #include <windows.h> |
| 8 | #else |
| 9 | #include <cerrno> |
| 10 | #include <cstring> |
| 11 | #endif |
| 12 | |
| 13 | #include "common/error.h" |
| 14 | |
| 15 | namespace Common { |
| 16 | |
| 17 | std::string NativeErrorToString(int e) { |
| 18 | #ifdef _WIN32 |
| 19 | LPSTR err_str; |
| 20 | |
| 21 | DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 22 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 23 | nullptr, e, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), |
| 24 | reinterpret_cast<LPSTR>(&err_str), 1, nullptr); |
| 25 | if (!res) { |
| 26 | return "(FormatMessageA failed to format error)"; |
| 27 | } |
| 28 | std::string ret(err_str); |
| 29 | LocalFree(err_str); |
| 30 | return ret; |
| 31 | #else |
| 32 | char err_str[255]; |
| 33 | #if defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) || \ |
| 34 | defined(ANDROID) |
| 35 | // Thread safe (GNU-specific) |
| 36 | const char* str = strerror_r(e, err_str, sizeof(err_str)); |
| 37 | return std::string(str); |
| 38 | #else |
| 39 | // Thread safe (XSI-compliant) |
| 40 | int second_err = strerror_r(e, err_str, sizeof(err_str)); |
| 41 | if (second_err != 0) { |
| 42 | return "(strerror_r failed to format error)"; |
| 43 | } |
| 44 | return std::string(err_str); |
| 45 | #endif // GLIBC etc. |
| 46 | #endif // _WIN32 |
| 47 | } |
| 48 | |
| 49 | std::string GetLastErrorMsg() { |
| 50 | #ifdef _WIN32 |
| 51 | return NativeErrorToString(GetLastError()); |
| 52 | #else |
| 53 | return NativeErrorToString(errno); |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | } // namespace Common |
| 58 |