refactor(relinker): remove the obsolete file-based icon builder

This commit is contained in:
boykopovar 2026-10-13 11:01:26 +03:00
parent 6224febfb0
commit 3e658db4b6
4 changed files with 6 additions and 56 deletions

View File

@ -549,7 +549,6 @@ if(BUILD_TESTING)
add_test_executable(windows_icon_resource_tests
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/tests/WindowsIconResourceTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli/src/NativeIcon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsIconResourceBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsPeFormat.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/src/BufferUtils.cpp

View File

@ -1,23 +0,0 @@
#include <elfpatcher/windows/WindowsIconResourceBuilder.hpp>
#include <domain/Types.hpp>
#include <fstream>
namespace Elfpatcher::Windows {
PeDirectory WindowsIconResourceBuilder::Build(const std::filesystem::path& iconPath, std::vector<PeSection>& sections, const std::uint32_t nextRva) const {
if (iconPath.empty()) return {};
std::error_code error;
const bool exists = std::filesystem::exists(iconPath, error);
if (error) throw Domain::RelinkerException("Cannot inspect Windows icon '" + iconPath.string() + "': " + error.message());
if (!exists) return {};
std::ifstream stream(iconPath, std::ios::binary | std::ios::ate);
if (!stream) throw Domain::RelinkerException("Cannot open Windows icon '" + iconPath.string() + "'");
const auto size = stream.tellg();
if (size < 0 || static_cast<std::uint64_t>(size) > 0x7fffffffu - 184) throw Domain::RelinkerException("Invalid Windows icon file size: " + iconPath.string());
std::vector<std::uint8_t> png(static_cast<std::size_t>(size));
stream.seekg(0);
if (!stream.read(reinterpret_cast<char*>(png.data()), size)) throw Domain::RelinkerException("Cannot read Windows icon '" + iconPath.string() + "'");
return Build(png, iconPath, sections, nextRva);
}
}

View File

@ -9,7 +9,6 @@ namespace Elfpatcher::Windows {
class WindowsIconResourceBuilder {
public:
PeDirectory Build(const std::vector<std::uint8_t>& png, const std::filesystem::path& iconPath, std::vector<PeSection>& sections, std::uint32_t nextRva) const;
PeDirectory Build(const std::filesystem::path& iconPath, std::vector<PeSection>& sections, std::uint32_t nextRva) const;
};
}

View File

@ -4,9 +4,7 @@
#include <algorithm>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
@ -29,31 +27,13 @@ std::uint32_t readU32(const std::vector<std::uint8_t>& data, const std::size_t o
static_cast<std::uint32_t>(data[offset + 2]) << 16 | static_cast<std::uint32_t>(data[offset + 3]) << 24;
}
void writeBytes(const std::filesystem::path& path, const std::vector<std::uint8_t>& bytes) {
std::filesystem::create_directories(path.parent_path());
std::ofstream stream(path, std::ios::binary);
stream.write(reinterpret_cast<const char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
require(static_cast<bool>(stream), "Cannot write " + path.string());
}
void requireEmpty(const Elfpatcher::Windows::WindowsIconResourceBuilder& builder, const std::filesystem::path& path, const std::string& name) {
std::vector<Elfpatcher::Windows::PeSection> sections;
const auto directory = builder.Build(path, sections, 0x10000);
require(sections.empty(), "Section added for " + name);
require(directory.Rva == 0 && directory.Size == 0, "Resource directory set for " + name);
}
void run(const std::filesystem::path& root) {
void run() {
const Elfpatcher::Windows::WindowsIconResourceBuilder builder;
requireEmpty(builder, {}, "an empty path");
requireEmpty(builder, root / "missing" / "icon0.png", "a missing icon");
const auto iconPath = root / "valid" / "icon0.png";
writeBytes(iconPath, Png);
const std::filesystem::path iconPath = "valid/icon0.png";
const std::uint32_t nextRva = 0x50000;
std::vector<Elfpatcher::Windows::PeSection> sections;
const auto directory = builder.Build(iconPath, sections, nextRva);
const auto directory = builder.Build(Png, iconPath, sections, nextRva);
require(sections.size() == 1, "Expected one section");
require(sections[0].Name == ".rsrc", "Unexpected section name: " + sections[0].Name);
require(sections[0].Rva == nextRva, "Unexpected section RVA");
@ -68,13 +48,12 @@ void run(const std::filesystem::path& root) {
require(readU32(data, 174) == Png.size(), "Unexpected icon size in group entry");
require(std::equal(Png.begin(), Png.end(), data.begin() + 184), "Icon bytes changed");
const auto badPath = root / "bad" / "icon0.png";
const auto badPath = std::filesystem::path("bad/icon0.png");
auto truncated = Png;
truncated.resize(40);
writeBytes(badPath, truncated);
std::vector<Elfpatcher::Windows::PeSection> badSections;
try {
builder.Build(badPath, badSections, nextRva);
builder.Build(truncated, badPath, badSections, nextRva);
} catch (const Domain::RelinkerException& e) {
require(std::string(e.what()).find(badPath.string()) != std::string::npos, std::string("Failure does not name the icon: ") + e.what());
require(badSections.empty(), "Section added despite the failure");
@ -86,15 +65,11 @@ void run(const std::filesystem::path& root) {
}
int main() {
const auto root = std::filesystem::temp_directory_path() / ("anyps5-icon-resource-tests-" + std::to_string(std::random_device{}()));
std::filesystem::remove_all(root);
try {
run(root);
run();
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
std::filesystem::remove_all(root);
return 1;
}
std::filesystem::remove_all(root);
return 0;
}