diff --git a/src/common/byteBuffer.h b/src/common/byteBuffer.h deleted file mode 100644 index 29ce3a48..00000000 --- a/src/common/byteBuffer.h +++ /dev/null @@ -1,166 +0,0 @@ -#ifndef KYTY_COMMON_BYTEBUFFER_H_ -#define KYTY_COMMON_BYTEBUFFER_H_ - -#include "common/assert.h" -#include "common/common.h" - -#include -#include // IWYU pragma: export -#include -#include -#include - -namespace Common { - -using Byte = std::byte; - -class ByteBuffer { -public: - using DataType = std::vector; - - static constexpr uint32_t INVALID_INDEX = static_cast(-1); - - using iterator = DataType::iterator; - using const_iterator = DataType::const_iterator; - - ByteBuffer() = default; - ByteBuffer(const ByteBuffer&) = default; - ByteBuffer(ByteBuffer&&) = default; - - ByteBuffer& operator=(const ByteBuffer&) = default; - ByteBuffer& operator=(ByteBuffer&&) = default; - - explicit ByteBuffer(uint32_t size): m_data(size) {} - - ByteBuffer(std::initializer_list list) { - for (const uint8_t& e: list) { - m_data.emplace_back(static_cast(e)); - } - } - - explicit ByteBuffer(const void* buf, uint32_t size) - : ByteBuffer(size) // @suppress("Ambiguous problem") - { - EXIT_IF(buf == nullptr && size != 0); - - if (size != 0) { - std::memcpy(m_data.data(), buf, size); - } - } - - [[nodiscard]] uint32_t Size() const { return static_cast(m_data.size()); } - [[nodiscard]] bool IsEmpty() const { return m_data.empty(); } - - void Clear() { m_data.clear(); } - - void Add(const Byte& val) { m_data.push_back(val); } - void Add(Byte&& val) { m_data.push_back(std::move(val)); } - - void Add(const Byte* val, uint32_t num) { - EXIT_IF(val == nullptr && num != 0); - m_data.insert(m_data.end(), val, val + num); - } - - void Add(const ByteBuffer& v) { - if (this == &v) { - const ByteBuffer copy(v); - Add(copy.GetDataConst(), copy.Size()); - return; - } - Add(v.GetDataConst(), v.Size()); - } - - void InsertAt(uint32_t index, const Byte& val) { - EXIT_IF(index > Size()); - m_data.insert(m_data.begin() + index, val); - } - - void InsertAt(uint32_t index, Byte&& val) { - EXIT_IF(index > Size()); - m_data.insert(m_data.begin() + index, std::move(val)); - } - - [[nodiscard]] uint32_t Find(const Byte& t) const { - const auto it = std::find(m_data.begin(), m_data.end(), t); - return (it == m_data.end() ? INVALID_INDEX - : static_cast(std::distance(m_data.begin(), it))); - } - - [[nodiscard]] bool Contains(const Byte& t) const { return Find(t) != INVALID_INDEX; } - - bool Remove(const Byte& t) { - const auto it = std::find(m_data.begin(), m_data.end(), t); - if (it == m_data.end()) { - return false; - } - m_data.erase(it); - return true; - } - - bool RemoveAt(uint32_t index, uint32_t count = 1) { - if (index >= Size() || count == 0) { - return false; - } - - const uint32_t erase_count = std::min(count, Size() - index); - m_data.erase(m_data.begin() + index, m_data.begin() + index + erase_count); - return true; - } - - [[nodiscard]] bool IndexValid(uint32_t index) const { return index < Size(); } - - Byte& operator[](uint32_t index) { - EXIT_IF(index >= Size()); - return m_data[index]; - } - - const Byte& operator[](uint32_t index) const { - EXIT_IF(index >= Size()); - return m_data[index]; - } - - [[nodiscard]] const Byte& At(uint32_t index) const { - EXIT_IF(index >= Size()); - return m_data[index]; - } - - Byte* GetData() { return m_data.data(); } - [[nodiscard]] const Byte* GetData() const { return m_data.data(); } - [[nodiscard]] const Byte* GetDataConst() const { return m_data.data(); } - - void Sort() { - std::sort(m_data.begin(), m_data.end(), [](Byte a, Byte b) { - return std::to_integer(a) < std::to_integer(b); - }); - } - - template - void Sort(OP&& comp_func) { - std::sort(m_data.begin(), m_data.end(), std::forward(comp_func)); - } - - bool operator==(const ByteBuffer& s) const { return m_data == s.m_data; } - bool operator!=(const ByteBuffer& s) const { return !(*this == s); } - - iterator begin() { return m_data.begin(); } // NOLINT(readability-identifier-naming) - iterator end() { return m_data.end(); } // NOLINT(readability-identifier-naming) - [[nodiscard]] const_iterator begin() const { - return m_data.begin(); - } // NOLINT(readability-identifier-naming) - [[nodiscard]] const_iterator end() const { - return m_data.end(); - } // NOLINT(readability-identifier-naming) - [[nodiscard]] const_iterator cbegin() const { - return m_data.cbegin(); - } // NOLINT(readability-identifier-naming) - [[nodiscard]] const_iterator cend() const { - return m_data.cend(); - } // NOLINT(readability-identifier-naming) - -private: - DataType m_data; -}; - -} // namespace Common - -#endif /* KYTY_COMMON_BYTEBUFFER_H_ */ diff --git a/src/common/file.cpp b/src/common/file.cpp index c335190e..086effbd 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -136,10 +136,6 @@ bool File::OpenInMem(void* buf, uint32_t buf_size) { return true; } -bool File::OpenInMem(ByteBuffer& buf) { - return OpenInMem(buf.GetData(), buf.Size()); -} - bool File::CreateInMem() { EXIT_IF(m_p->f != nullptr); @@ -345,7 +341,7 @@ bool File::Flush() { return SysFileFlush(*m_p->f); } -ByteBuffer File::ReadWholeBuffer() { +std::vector File::ReadWholeBuffer() { EXIT_IF(IsInvalid()); EXIT_IF(Tell() != 0); @@ -353,25 +349,14 @@ ByteBuffer File::ReadWholeBuffer() { EXIT_IF((s >> 32u) != 0); - ByteBuffer buf(s); + const auto read_size = static_cast(s); + std::vector buf(read_size); - Read(buf.GetData(), s); + Read(buf.data(), read_size); return buf; } -ByteBuffer File::Read(uint32_t size) { - ByteBuffer buf(size); - uint32_t b = 0; - Read(buf.GetData(), size, &b); - buf.RemoveAt(b, size - b); - return buf; -} - -void File::Write(const ByteBuffer& buf, uint32_t* bytes_written) { - Write(buf.GetDataConst(), buf.Size(), bytes_written); -} - DateTime File::GetLastAccessTimeUTC(const std::filesystem::path& name) { SysTimeStruct t {}; SysFileToSystemTimeUtc(SysFileGetLastAccessTimeUtc(name), t); diff --git a/src/common/file.h b/src/common/file.h index f5ebbe82..1428b4a1 100644 --- a/src/common/file.h +++ b/src/common/file.h @@ -1,10 +1,10 @@ #ifndef KYTY_COMMON_FILE_H_ #define KYTY_COMMON_FILE_H_ -#include "common/byteBuffer.h" #include "common/common.h" #include "common/dateTime.h" +#include #include #include #include @@ -51,7 +51,6 @@ public: bool Create(const std::filesystem::path& name); bool Open(const std::filesystem::path& name, Mode mode); bool OpenInMem(void* buf, uint32_t buf_size); - bool OpenInMem(ByteBuffer& buf); // NOLINT(google-runtime-references) bool CreateInMem(); void Close(); @@ -73,14 +72,12 @@ public: void GetLastAccessAndWriteTimeUTC(DateTime* access, DateTime* write); - void Read(void* data, uint32_t size, uint32_t* bytes_read = nullptr); - ByteBuffer Read(uint32_t size); - void Write(const void* data, uint32_t size, uint32_t* bytes_written = nullptr); - void Write(const ByteBuffer& buf, uint32_t* bytes_written = nullptr); + void Read(void* data, uint32_t size, uint32_t* bytes_read = nullptr); + void Write(const void* data, uint32_t size, uint32_t* bytes_written = nullptr); void Printf(const char* format, ...) KYTY_FORMAT_PRINTF(2, 3); - ByteBuffer ReadWholeBuffer(); + std::vector ReadWholeBuffer(); static uint64_t Size(const std::filesystem::path& name); diff --git a/src/common/stringUtils.h b/src/common/stringUtils.h index 5f6deaf9..3e838617 100644 --- a/src/common/stringUtils.h +++ b/src/common/stringUtils.h @@ -1,7 +1,6 @@ #ifndef KYTY_COMMON_STRING_UTILS_H_ #define KYTY_COMMON_STRING_UTILS_H_ -#include "common/byteBuffer.h" #include "common/common.h" #include @@ -9,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -151,21 +149,6 @@ inline std::vector Split(std::string_view text, char sep, bool keep return Split(text, std::string_view(&sep, 1), keep_empty); } -inline std::string Concat(const std::vector& list, std::string_view sep) { - std::string ret; - for (size_t i = 0; i < list.size(); i++) { - if (i != 0) { - ret += sep; - } - ret += list[i]; - } - return ret; -} - -inline std::string Concat(const std::vector& list, char sep) { - return Concat(list, std::string_view(&sep, 1)); -} - inline int32_t ToInt32(std::string_view text, int base = 10) { int32_t value = 0; std::from_chars(text.data(), text.data() + text.size(), value, base); @@ -180,15 +163,6 @@ inline std::string Utf16ToUtf8(std::u16string_view utf16) { return convert.to_bytes(utf16.data(), utf16.data() + utf16.size()); } -inline std::string HexFromBin(const ByteBuffer& bin) { - std::string ret; - ret.reserve(bin.Size() * 2); - for (uint32_t i = 0; i < bin.Size(); i++) { - ret += fmt::format("{:02X}", std::to_integer(bin.At(i))); - } - return ret; -} - } // namespace Common #endif /* KYTY_COMMON_STRING_UTILS_H_ */ diff --git a/src/graphics/guest_gpu/tile.cpp b/src/graphics/guest_gpu/tile.cpp index a8891b6d..651390ce 100644 --- a/src/graphics/guest_gpu/tile.cpp +++ b/src/graphics/guest_gpu/tile.cpp @@ -4,7 +4,6 @@ #include "common/assert.h" #include "common/logging/log.h" #include "common/profiler.h" -#include "common/stringUtils.h" #include "graphics/guest_gpu/gpu_defs.h" #include "graphics/guest_gpu/gpu_format.h" @@ -12,6 +11,8 @@ #include #include #include +#include +#include #include namespace Libs::Graphics { @@ -1328,7 +1329,7 @@ void TileGetTextureSize(Prospero::BufferFormat format, uint32_t width, uint32_t list.push_back(fmt::format("height = {}", height)); list.push_back(fmt::format("levels = {}", levels)); list.push_back(fmt::format("tile = {}", static_cast(tile))); - EXIT("unknown format:\n%s\n", Common::Concat(list, '\n').c_str()); + EXIT("unknown format:\n%s\n", fmt::format("{}", fmt::join(list, "\n"))); } } diff --git a/src/libs/libDbgAsan.cpp b/src/libs/libDbgAsan.cpp index dfa5e367..6a9c27fc 100644 --- a/src/libs/libDbgAsan.cpp +++ b/src/libs/libDbgAsan.cpp @@ -11,6 +11,7 @@ #include "loader/symbolDatabase.h" #include +#include #include namespace Libs { diff --git a/src/libs/libHmd2.cpp b/src/libs/libHmd2.cpp index f1dd5469..f01c2ee9 100644 --- a/src/libs/libHmd2.cpp +++ b/src/libs/libHmd2.cpp @@ -1,3 +1,4 @@ +#include "common/assert.h" #include "common/emulatorConfig.h" #include "graphics/guest_gpu/tile.h" #include "libs/agc.h" diff --git a/src/libs/network.cpp b/src/libs/network.cpp index 8c84340d..c6a72007 100644 --- a/src/libs/network.cpp +++ b/src/libs/network.cpp @@ -22,10 +22,8 @@ #endif #include "common/assert.h" -#include "common/byteBuffer.h" #include "common/common.h" #include "common/logging/log.h" -#include "common/stringUtils.h" #include "common/threads.h" #include "kernel/pthread.h" #include "libs/errno.h" @@ -40,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -3460,7 +3459,7 @@ int KYTY_SYSV_ABI NpSetNpTitleId(const NpTitleId* title_id, const NpTitleSecret* LOGF("\t title_id = %.12s\n" "\t title_secret = %s\n", - title_id->id, Common::HexFromBin(Common::ByteBuffer(title_secret->data, 128)).c_str()); + title_id->id, fmt::format("{:02X}", fmt::join(title_secret->data, ""))); return OK; } diff --git a/src/loader/gamePatch.cpp b/src/loader/gamePatch.cpp index d4db4ef5..6404fdc6 100644 --- a/src/loader/gamePatch.cpp +++ b/src/loader/gamePatch.cpp @@ -1,5 +1,6 @@ #include "loader/gamePatch.h" +#include "common/assert.h" #include "common/stringUtils.h" #include "common/virtualMemory.h" #include "kernel/memory.h" diff --git a/src/loader/systemContent.cpp b/src/loader/systemContent.cpp index efa8b480..2ce0a2e8 100644 --- a/src/loader/systemContent.cpp +++ b/src/loader/systemContent.cpp @@ -1,7 +1,6 @@ #include "loader/systemContent.h" #include "common/assert.h" -#include "common/byteBuffer.h" #include "common/common.h" #include "common/file.h" #include "common/logging/log.h" @@ -121,7 +120,7 @@ void Psf::OpenJson(const std::filesystem::path& file_name) { } auto buf = f.ReadWholeBuffer(); - if (buf.Size() == 0) { + if (buf.empty()) { LOGF("invalid json file: %s\n", Common::PathToString(file_name).c_str()); f.Close(); return; @@ -131,8 +130,8 @@ void Psf::OpenJson(const std::filesystem::path& file_name) { std::vector localized_languages; std::vector localized_titles; - const auto* json_begin = reinterpret_cast(buf.GetDataConst()); - const auto* json_end = json_begin + buf.Size(); + const auto* json_begin = reinterpret_cast(buf.data()); + const auto* json_end = json_begin + buf.size(); auto param_json = nlohmann::ordered_json::parse(json_begin, json_end, nullptr, false); if (param_json.is_discarded() || !param_json.is_object()) { LOGF("invalid json file: %s\n", Common::PathToString(file_name).c_str()); @@ -432,12 +431,12 @@ void PlayGo::OpenChunkDefs(const std::filesystem::path& file_name) { auto buf = f.ReadWholeBuffer(); f.Close(); - if (buf.Size() == 0) { + if (buf.empty()) { LOGF("invalid file: %s\n", Common::PathToString(file_name).c_str()); return; } - const std::string_view xml(reinterpret_cast(buf.GetDataConst()), buf.Size()); + const std::string_view xml(reinterpret_cast(buf.data()), buf.size()); uint32_t max_chunk_id = 0; bool found = false; @@ -509,9 +508,9 @@ void PlayGo::OpenChunkManifest(const std::filesystem::path& file_name) { bool chunks[1000] {}; uint32_t max_chunk_id = 0; bool found = false; - const auto* data = buf.GetDataConst(); + const auto* data = buf.data(); - for (size_t pos = 0; pos + sizeof(id_field) + sizeof(uint32_t) <= buf.Size(); pos++) { + for (size_t pos = 0; pos + sizeof(id_field) + sizeof(uint32_t) <= buf.size(); pos++) { if (std::memcmp(data + pos, id_field, sizeof(id_field)) != 0) { continue; } diff --git a/tests/KernelFileSystemTests.cpp b/tests/KernelFileSystemTests.cpp index 53d49064..a673bc39 100644 --- a/tests/KernelFileSystemTests.cpp +++ b/tests/KernelFileSystemTests.cpp @@ -84,8 +84,8 @@ void CheckSaveRename(const std::filesystem::path &root, Check(!result.IsInvalid(), "open renamed save file"); const auto data = result.ReadWholeBuffer(); const std::string expected = std::string(payload) + Suffix; - Check(data.Size() == expected.size(), "renamed save size"); - Check(std::memcmp(data.GetData(), expected.data(), expected.size()) == 0, + Check(data.size() == expected.size(), "renamed save size"); + Check(std::memcmp(data.data(), expected.data(), expected.size()) == 0, "renamed save contents"); }