A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | // #include <boost/icl/interval_set.hpp> |
| 5 | |
| 6 | #include <limits> |
| 7 | #include <optional> |
| 8 | #include <shared_mutex> |
| 9 | #include <unordered_map> |
| 10 | |
| 11 | #include <memory> |
| 12 | #include <numeric> |
| 13 | |
| 14 | namespace Common { |
| 15 | |
| 16 | template <class IndexType, class ResourceType, |
| 17 | IndexType MaxIndex = std::numeric_limits<IndexType>::max(), IndexType MinIndex = 0> |
| 18 | class SlotArray { |
| 19 | public: |
| 20 | SlotArray() { |
| 21 | std::iota(m_free_indices.begin(), m_free_indices.end(), MinIndex); |
| 22 | } |
| 23 | |
| 24 | template <class... Types> |
| 25 | std::optional<IndexType> Create(Types&&... args) { |
| 26 | if (!HasFreeSlots()) { |
| 27 | return std::nullopt; |
| 28 | } |
| 29 | const auto index = m_free_indices[m_curr_cursor]; |
| 30 | m_resources[index - MinIndex] = ResourceType(std::forward<Types>(args)...); |
| 31 | m_curr_cursor += 1; |
| 32 | return index; |
| 33 | } |
| 34 | |
| 35 | bool Destroy(IndexType index) { |
| 36 | if (!m_resources[index - MinIndex].has_value()) { |
| 37 | return false; |
| 38 | } |
| 39 | m_curr_cursor -= 1; |
| 40 | m_free_indices[m_curr_cursor] = index; |
| 41 | m_resources[index - MinIndex] = std::nullopt; |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | ResourceType* Get(IndexType index) { |
| 46 | auto& resource = m_resources[index - MinIndex]; |
| 47 | if (!resource.has_value()) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | return &resource.value(); |
| 51 | } |
| 52 | |
| 53 | bool HasFreeSlots() { |
| 54 | return m_curr_cursor < m_free_indices.size(); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | size_t m_curr_cursor = 0; |
| 59 | std::array<IndexType, MaxIndex - MinIndex> m_free_indices; |
| 60 | std::array<std::optional<ResourceType>, MaxIndex - MinIndex> m_resources; |
| 61 | }; |
| 62 | |
| 63 | } // namespace Common |
| 64 |