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 <algorithm> |
| 6 | #include <sstream> |
| 7 | #include <string> |
| 8 | #include "common/string_util.h" |
| 9 | #include "common/shadpkg_types.h" |
| 10 | |
| 11 | #ifdef _WIN32 |
| 12 | #include <windows.h> |
| 13 | #endif |
| 14 | |
| 15 | namespace Common { |
| 16 | |
| 17 | std::string ToLower(std::string_view input) { |
| 18 | std::string str; |
| 19 | str.resize(input.size()); |
| 20 | std::ranges::transform(input, str.begin(), tolower); |
| 21 | return str; |
| 22 | } |
| 23 | |
| 24 | void ToLowerInPlace(std::string& str) { |
| 25 | std::ranges::transform(str, str.begin(), tolower); |
| 26 | } |
| 27 | |
| 28 | std::vector<std::string> SplitString(const std::string& str, char delimiter) { |
| 29 | std::istringstream iss(str); |
| 30 | std::vector<std::string> output(1); |
| 31 | |
| 32 | while (std::getline(iss, *output.rbegin(), delimiter)) { |
| 33 | output.emplace_back(); |
| 34 | } |
| 35 | |
| 36 | output.pop_back(); |
| 37 | return output; |
| 38 | } |
| 39 | |
| 40 | std::string_view U8stringToString(std::u8string_view u8str) { |
| 41 | return std::string_view{reinterpret_cast<const char*>(u8str.data()), u8str.size()}; |
| 42 | } |
| 43 | |
| 44 | #ifdef _WIN32 |
| 45 | static std::wstring CPToUTF16(u32 code_page, std::string_view input) { |
| 46 | const auto size = |
| 47 | MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0); |
| 48 | |
| 49 | if (size == 0) { |
| 50 | return {}; |
| 51 | } |
| 52 | |
| 53 | std::wstring output(size, L'\0'); |
| 54 | |
| 55 | if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), |
| 56 | &output[0], static_cast<int>(output.size()))) { |
| 57 | output.clear(); |
| 58 | } |
| 59 | |
| 60 | return output; |
| 61 | } |
| 62 | |
| 63 | std::string UTF16ToUTF8(std::wstring_view input) { |
| 64 | const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), |
| 65 | nullptr, 0, nullptr, nullptr); |
| 66 | if (size == 0) { |
| 67 | return {}; |
| 68 | } |
| 69 | |
| 70 | std::string output(size, '\0'); |
| 71 | |
| 72 | if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), |
| 73 | &output[0], static_cast<int>(output.size()), nullptr, |
| 74 | nullptr)) { |
| 75 | output.clear(); |
| 76 | } |
| 77 | |
| 78 | return output; |
| 79 | } |
| 80 | |
| 81 | std::wstring UTF8ToUTF16W(std::string_view input) { |
| 82 | return CPToUTF16(CP_UTF8, input); |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | } // namespace Common |
| 87 |