Skip to content

Commit

Permalink
Properly map locale file
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Sep 12, 2024
1 parent 08cc693 commit 7040054
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 16 deletions.
124 changes: 124 additions & 0 deletions src/common/utils/io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "io.hpp"
#include <fstream>
#include <sstream>

namespace utils::io
{
bool remove_file(const std::filesystem::path& file)
{
std::error_code ec{};
return std::filesystem::remove(file, ec) && !ec;
}

bool move_file(const std::filesystem::path& src, const std::filesystem::path& target)
{
copy_folder(src, target);
return remove_file(src);
}

bool file_exists(const std::filesystem::path& file)
{
return std::ifstream(file).good();
}

bool write_file(const std::filesystem::path& file, const std::vector<uint8_t>& data, const bool append)
{
if (file.has_parent_path())
{
io::create_directory(file.parent_path());
}

std::basic_ofstream<uint8_t> stream(
file, std::ios::binary | std::ofstream::out | (append ? std::ofstream::app : std::ofstream::out));

if (stream.is_open())
{
stream.write(data.data(), static_cast<std::streamsize>(data.size()));
stream.close();
return true;
}

return false;
}

std::vector<uint8_t> read_file(const std::filesystem::path& file)
{
std::vector<uint8_t> data;
read_file(file, &data);
return data;
}

bool read_file(const std::filesystem::path& file, std::vector<uint8_t>* data)
{
if (!data) return false;
data->clear();

std::ifstream stream(file, std::ios::binary);
if (!stream) return false;

*data = std::vector<uint8_t>{(std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>()};
return true;
}

std::size_t file_size(const std::filesystem::path& file)
{
std::ifstream stream(file, std::ios::binary);

if (stream)
{
stream.seekg(0, std::ios::end);
return static_cast<std::size_t>(stream.tellg());
}

return 0;
}

bool create_directory(const std::filesystem::path& directory)
{
std::error_code ec{};
return std::filesystem::create_directories(directory, ec) && !ec;
}

bool directory_exists(const std::filesystem::path& directory)
{
std::error_code ec{};
return std::filesystem::is_directory(directory, ec) && !ec;
}

bool directory_is_empty(const std::filesystem::path& directory)
{
std::error_code ec{};
return std::filesystem::is_empty(directory, ec) && !ec;
}

void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target)
{
std::error_code ec{};
std::filesystem::copy(src, target,
std::filesystem::copy_options::overwrite_existing |
std::filesystem::copy_options::recursive, ec);
}

std::vector<std::filesystem::path> list_files(const std::filesystem::path& directory, const bool recursive)
{
std::error_code code{};
std::vector<std::filesystem::path> files;

if (recursive)
{
for (auto& file : std::filesystem::recursive_directory_iterator(directory, code))
{
files.push_back(file.path());
}
}
else
{
for (auto& file : std::filesystem::directory_iterator(directory, code))
{
files.push_back(file.path());
}
}

return files;
}
}
22 changes: 22 additions & 0 deletions src/common/utils/io.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <string>
#include <vector>
#include <filesystem>

namespace utils::io
{
bool remove_file(const std::filesystem::path& file);
bool move_file(const std::filesystem::path& src, const std::filesystem::path& target);
bool file_exists(const std::filesystem::path& file);
bool write_file(const std::filesystem::path& file, const std::vector<uint8_t>& data, bool append = false);
bool read_file(const std::filesystem::path& file, std::vector<uint8_t>* data);
std::vector<uint8_t> read_file(const std::filesystem::path& file);
size_t file_size(const std::filesystem::path& file);
bool create_directory(const std::filesystem::path& directory);
bool directory_exists(const std::filesystem::path& directory);
bool directory_is_empty(const std::filesystem::path& directory);
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target);

std::vector<std::filesystem::path> list_files(const std::filesystem::path& directory, bool recursive = false);
}
11 changes: 11 additions & 0 deletions src/emulator/memory_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ class memory_manager

region_info get_region_info(uint64_t address);

uint64_t allocate_memory(const size_t size, const memory_permission permissions, const bool reserve_only = false)
{
const auto allocation_base = this->find_free_allocation_base(size);
if (!allocate_memory(allocation_base, size, permissions, reserve_only))
{
return 0;
}

return allocation_base;
}

private:
using reserved_region_map = std::map<uint64_t, reserved_region>;
reserved_region_map reserved_regions_{};
Expand Down
2 changes: 1 addition & 1 deletion src/windows_emulator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ namespace
context.process_params.access([&](RTL_USER_PROCESS_PARAMETERS& proc_params)
{
proc_params.Length = sizeof(proc_params);
proc_params.Flags = 0x6001 | 0x80000000; // Prevent CsrClientConnectToServer
proc_params.Flags = 0x6001; //| 0x80000000; // Prevent CsrClientConnectToServer

proc_params.ConsoleHandle = CONSOLE_HANDLE.h;
proc_params.StandardOutput = STDOUT_HANDLE.h;
Expand Down
9 changes: 2 additions & 7 deletions src/windows_emulator/module/module_mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "module_mapping.hpp"
#include <address_utils.hpp>

#include <utils/io.hpp>
#include <utils/buffer_accessor.hpp>

namespace
Expand Down Expand Up @@ -173,12 +174,6 @@ namespace
}
}

std::vector<uint8_t> load_file(const std::filesystem::path& file)
{
std::ifstream stream(file, std::ios::in | std::ios::binary);
return {(std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>()};
}

std::optional<mapped_module> map_module(emulator& emu, const std::span<const uint8_t> data,
std::filesystem::path file)
{
Expand Down Expand Up @@ -247,7 +242,7 @@ std::optional<mapped_module> map_module_from_data(emulator& emu, const std::span

std::optional<mapped_module> map_module_from_file(emulator& emu, std::filesystem::path file)
{
const auto data = load_file(file);
const auto data = utils::io::read_file(file);
if (data.empty())
{
return {};
Expand Down
24 changes: 16 additions & 8 deletions src/windows_emulator/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "syscalls.hpp"
#include "context_frame.hpp"

#include <utils/io.hpp>

struct syscall_context
{
x64_emulator& emu;
Expand Down Expand Up @@ -631,8 +633,6 @@ namespace

if (info_class == SystemProcessorInformation)
{
puts("PROC INFO");
c.proc.verbose = true;
if (return_length)
{
return_length.write(sizeof(SYSTEM_PROCESSOR_INFORMATION));
Expand Down Expand Up @@ -1231,10 +1231,7 @@ namespace

client_shared_memory.access([&](PORT_VIEW& view)
{
const auto address = c.emu.find_free_allocation_base(view.ViewSize);
c.emu.allocate_memory(address,
view.ViewSize, memory_permission::read_write);

const auto address = c.emu.allocate_memory(view.ViewSize, memory_permission::read_write);
view.ViewBase = reinterpret_cast<void*>(address);
});

Expand Down Expand Up @@ -1319,12 +1316,23 @@ namespace
return STATUS_NOT_SUPPORTED;
}

NTSTATUS handle_NtInitializeNlsFiles(const syscall_context& /*c*/, const emulator_object<uint64_t> base_address,
NTSTATUS handle_NtInitializeNlsFiles(const syscall_context& c, const emulator_object<uint64_t> base_address,
const emulator_object<LCID> default_locale_id,
const emulator_object<LARGE_INTEGER> /*default_casing_table_size*/)
{
const auto locale_file = utils::io::read_file(R"(C:\Windows\System32\locale.nls)");
if (locale_file.empty())
{
return STATUS_FILE_INVALID;
}

const auto size = page_align_up(locale_file.size());
const auto base = c.emu.allocate_memory(size, memory_permission::read);
c.emu.write_memory(base, locale_file.data(), locale_file.size());

base_address.write(base);
default_locale_id.write(0x407);
base_address.write(0x1337);

return STATUS_SUCCESS;
}

Expand Down

0 comments on commit 7040054

Please sign in to comment.