rpcsx/rpcs3/Loader/PSF.cpp

258 lines
7 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "PSF.h"
2016-01-07 23:12:33 +01:00
namespace psf
{
2016-02-01 22:47:09 +01:00
_log::channel log("PSF", _log::level::notice);
2016-01-26 19:13:36 +01:00
struct header_t
{
2016-01-26 19:13:36 +01:00
le_t<u32> magic;
le_t<u32> version;
le_t<u32> off_key_table;
le_t<u32> off_data_table;
le_t<u32> entries_num;
};
struct def_table_t
2015-04-16 17:33:55 +02:00
{
2016-01-26 19:13:36 +01:00
le_t<u16> key_off;
le_t<format> param_fmt;
le_t<u32> param_len;
le_t<u32> param_max;
le_t<u32> data_off;
};
const std::string& entry::as_string() const
{
2016-02-01 22:47:09 +01:00
Expects(m_type == format::string || m_type == format::array);
2016-01-07 23:12:33 +01:00
return m_value_string;
}
2016-01-07 23:12:33 +01:00
u32 entry::as_integer() const
2015-04-16 17:33:55 +02:00
{
2016-02-01 22:47:09 +01:00
Expects(m_type == format::integer);
2016-01-07 23:12:33 +01:00
return m_value_integer;
}
2016-01-26 19:13:36 +01:00
entry& entry::operator =(const std::string& value)
2015-04-16 17:33:55 +02:00
{
2016-02-01 22:47:09 +01:00
Expects(m_type == format::string || m_type == format::array);
2016-01-26 19:13:36 +01:00
m_value_string = value;
2016-01-07 23:12:33 +01:00
return *this;
}
2015-04-16 18:19:41 +02:00
2016-01-26 19:13:36 +01:00
entry& entry::operator =(u32 value)
2015-04-16 17:33:55 +02:00
{
2016-02-01 22:47:09 +01:00
Expects(m_type == format::integer);
2016-01-26 19:13:36 +01:00
m_value_integer = value;
2016-01-07 23:12:33 +01:00
return *this;
}
2015-04-16 17:33:55 +02:00
2016-01-26 19:13:36 +01:00
u32 entry::size() const
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
switch (m_type)
{
2016-01-26 19:13:36 +01:00
case format::string:
case format::array:
return std::min(m_max_size, gsl::narrow<u32>(m_value_string.size() + (m_type == format::string)));
2016-01-26 19:13:36 +01:00
case format::integer:
return SIZE_32(u32);
}
2015-04-16 01:17:42 +02:00
2016-02-01 22:47:09 +01:00
throw fmt::exception("Invalid format (0x%x)" HERE, m_type);
2016-01-07 23:12:33 +01:00
}
2015-04-16 01:17:42 +02:00
2016-02-01 22:47:09 +01:00
registry load_object(const std::vector<char>& data)
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
registry result;
2015-04-16 17:33:55 +02:00
2016-01-26 19:13:36 +01:00
// Hack for empty input (TODO)
if (data.empty())
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
return result;
2016-01-07 23:12:33 +01:00
}
2015-04-16 17:33:55 +02:00
2016-01-26 19:13:36 +01:00
// Check size
2016-02-01 22:47:09 +01:00
Expects(data.size() >= sizeof(header_t));
Expects((std::uintptr_t)data.data() % 8 == 0);
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Get header
const header_t& header = reinterpret_cast<const header_t&>(data[0]);
2015-04-16 18:19:41 +02:00
2016-01-26 19:13:36 +01:00
// Check magic and version
2016-02-01 22:47:09 +01:00
Expects(header.magic == "\0PSF"_u32);
Expects(header.version == 0x101);
Expects(sizeof(header_t) + header.entries_num * sizeof(def_table_t) <= header.off_key_table);
Expects(header.off_key_table <= header.off_data_table);
Expects(header.off_data_table <= data.size());
2015-04-16 01:17:42 +02:00
2016-01-26 19:13:36 +01:00
// Get indices (alignment should be fine)
const def_table_t* indices = reinterpret_cast<const def_table_t*>(data.data() + sizeof(header_t));
2015-04-16 18:19:41 +02:00
2016-01-26 19:13:36 +01:00
// Load entries
for (u32 i = 0; i < header.entries_num; ++i)
2016-01-07 23:12:33 +01:00
{
2016-02-01 22:47:09 +01:00
Expects(indices[i].key_off < header.off_data_table - header.off_key_table);
2015-04-21 21:43:40 +02:00
2016-01-26 19:13:36 +01:00
// Get key name range
const auto name_ptr = data.begin() + header.off_key_table + indices[i].key_off;
const auto name_end = std::find(name_ptr , data.begin() + header.off_data_table, '\0');
2015-04-16 18:19:41 +02:00
2016-01-26 19:13:36 +01:00
// Get name (must be unique)
std::string key(name_ptr, name_end);
2015-04-16 18:19:41 +02:00
2016-02-01 22:47:09 +01:00
Expects(result.count(key) == 0);
Expects(indices[i].param_len <= indices[i].param_max);
Expects(indices[i].data_off < data.size() - header.off_data_table);
Expects(indices[i].param_max < data.size() - indices[i].data_off);
2015-04-16 18:19:41 +02:00
2016-01-26 19:13:36 +01:00
// Get data pointer
const auto value_ptr = data.begin() + header.off_data_table + indices[i].data_off;
2015-04-16 18:19:41 +02:00
2016-01-26 19:13:36 +01:00
if (indices[i].param_fmt == format::integer && indices[i].param_max == sizeof(u32) && indices[i].param_len == sizeof(u32))
{
2016-01-26 19:13:36 +01:00
// Integer data
result.emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(key)),
std::forward_as_tuple(reinterpret_cast<const le_t<u32>&>(*value_ptr)));
}
2016-01-26 19:13:36 +01:00
else if (indices[i].param_fmt == format::string || indices[i].param_fmt == format::array)
2015-04-16 18:19:41 +02:00
{
2016-01-26 19:13:36 +01:00
// String/array data
std::string value;
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
if (indices[i].param_fmt == format::string)
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
// Find null terminator
value.assign(value_ptr, std::find(value_ptr, value_ptr + indices[i].param_len, '\0'));
2016-01-07 23:12:33 +01:00
}
2016-01-26 19:13:36 +01:00
else
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
value.assign(value_ptr, value_ptr + indices[i].param_len);
2016-01-07 23:12:33 +01:00
}
2016-01-26 19:13:36 +01:00
result.emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(key)),
std::forward_as_tuple(indices[i].param_fmt, indices[i].param_max, std::move(value)));
2015-04-16 18:19:41 +02:00
}
2016-01-07 23:12:33 +01:00
else
2015-04-16 18:19:41 +02:00
{
2016-01-26 19:13:36 +01:00
// Possibly unsupported format, entry ignored
log.error("Unknown entry format (key='%s', fmt=0x%x, len=0x%x, max=0x%x)", key, indices[i].param_fmt, indices[i].param_len, indices[i].param_max);
2015-04-16 18:19:41 +02:00
}
}
2015-04-16 17:33:55 +02:00
2016-01-26 19:13:36 +01:00
return result;
2015-04-16 18:19:41 +02:00
}
2016-02-01 22:47:09 +01:00
std::vector<char> save_object(const registry& psf)
2015-04-16 18:19:41 +02:00
{
2016-01-26 19:13:36 +01:00
std::vector<def_table_t> indices; indices.reserve(psf.size());
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Generate indices and calculate key table length
std::size_t key_offset = 0, data_offset = 0;
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
for (const auto& entry : psf)
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
def_table_t index;
index.key_off = gsl::narrow<u32>(key_offset);
index.param_fmt = entry.second.type();
index.param_len = entry.second.size();
index.param_max = entry.second.max();
index.data_off = gsl::narrow<u32>(data_offset);
// Update offsets:
key_offset += gsl::narrow<u32>(entry.first.size() + 1); // key size
data_offset += index.param_max;
indices.push_back(index);
}
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Align next section (data) offset
key_offset = ::align(key_offset, 4);
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Generate header
header_t header;
2016-02-01 22:47:09 +01:00
header.magic = "\0PSF"_u32;
2016-01-26 19:13:36 +01:00
header.version = 0x101;
header.off_key_table = gsl::narrow<u32>(sizeof(header_t) + sizeof(def_table_t) * psf.size());
header.off_data_table = gsl::narrow<u32>(header.off_key_table + key_offset);
header.entries_num = gsl::narrow<u32>(psf.size());
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Save header and indices
std::vector<char> result; result.reserve(header.off_data_table + data_offset);
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
result.insert(result.end(), (char*)&header, (char*)&header + sizeof(header_t));
result.insert(result.end(), (char*)indices.data(), (char*)indices.data() + sizeof(def_table_t) * psf.size());
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Save key table
for (const auto& entry : psf)
2015-04-16 18:19:41 +02:00
{
2016-01-26 19:13:36 +01:00
result.insert(result.end(), entry.first.begin(), entry.first.end());
result.push_back('\0');
2015-04-16 18:19:41 +02:00
}
2016-01-26 19:13:36 +01:00
// Insert zero padding
result.insert(result.end(), header.off_data_table - result.size(), '\0');
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Save data
for (const auto& entry : psf)
2015-04-16 18:19:41 +02:00
{
2016-01-26 19:13:36 +01:00
const auto fmt = entry.second.type();
const u32 max = entry.second.max();
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
if (fmt == format::integer && max == sizeof(u32))
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
const le_t<u32> value = entry.second.as_integer();
result.insert(result.end(), (char*)&value, (char*)&value + sizeof(u32));
}
2016-01-26 19:13:36 +01:00
else if (fmt == format::string || fmt == format::array)
2015-04-16 18:19:41 +02:00
{
2016-01-26 19:13:36 +01:00
const std::string& value = entry.second.as_string();
const std::size_t size = std::min<std::size_t>(max, value.size());
if (value.size() + (fmt == format::string) > max)
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
// TODO: check real limitations of PSF format
log.error("Entry value shrinkage (key='%s', value='%s', size=0x%zx, max=0x%x)", entry.first, value, size, max);
2016-01-07 23:12:33 +01:00
}
2016-01-26 19:13:36 +01:00
result.insert(result.end(), value.begin(), value.begin() + size);
result.insert(result.end(), max - size, '\0'); // Write zeros up to max_size
2016-01-07 23:12:33 +01:00
}
2016-01-26 19:13:36 +01:00
else
2016-01-07 23:12:33 +01:00
{
2016-01-26 19:13:36 +01:00
throw EXCEPTION("Invalid entry format (key='%s', fmt=0x%x)", entry.first, fmt);
2015-04-16 18:19:41 +02:00
}
}
2015-04-16 17:33:55 +02:00
2016-01-26 19:13:36 +01:00
return result;
}
2016-01-26 19:13:36 +01:00
std::string get_string(const registry& psf, const std::string& key, const std::string& def)
2015-04-16 01:17:42 +02:00
{
2016-01-26 19:13:36 +01:00
const auto found = psf.find(key);
2016-01-08 00:07:55 +01:00
2016-01-26 19:13:36 +01:00
if (found == psf.end() || (found->second.type() != format::string && found->second.type() != format::array))
2016-01-08 00:07:55 +01:00
{
2016-01-26 19:13:36 +01:00
return def;
2016-01-08 00:07:55 +01:00
}
2016-01-26 19:13:36 +01:00
return found->second.as_string();
2016-01-08 00:07:55 +01:00
}
2016-01-26 19:13:36 +01:00
u32 get_integer(const registry& psf, const std::string& key, u32 def)
{
2016-01-26 19:13:36 +01:00
const auto found = psf.find(key);
2016-01-26 19:13:36 +01:00
if (found == psf.end() || found->second.type() != format::integer)
{
2016-01-26 19:13:36 +01:00
return def;
}
2016-01-26 19:13:36 +01:00
return found->second.as_integer();
}
2013-11-19 11:30:58 +01:00
}