CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/ELF/ParamSFO.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <cstdio>18#include <cstring>1920#include "Common/CommonTypes.h"21#include "Common/Log.h"22#include "Common/StringUtils.h"23#include "Common/Swap.h"24#include "Core/ELF/ParamSFO.h"25#include "Core/Core.h"2627struct Header28{29u32_le magic; /* Always PSF */30u32_le version; /* Usually 1.1 */31u32_le key_table_start; /* Start position of key_table */32u32_le data_table_start; /* Start position of data_table */33u32_le index_table_entries; /* Number of entries in index_table*/34};3536struct IndexTable37{38u16_le key_table_offset; /* Offset of the param_key from start of key_table */39u16_le param_fmt; /* Type of data of param_data in the data_table */40u32_le param_len; /* Used Bytes by param_data in the data_table */41u32_le param_max_len; /* Total bytes reserved for param_data in the data_table */42u32_le data_table_offset; /* Offset of the param_data from start of data_table */43};4445void ParamSFOData::SetValue(const std::string &key, unsigned int value, int max_size) {46values[key].type = VT_INT;47values[key].i_value = value;48values[key].max_size = max_size;49}50void ParamSFOData::SetValue(const std::string &key, const std::string &value, int max_size) {51values[key].type = VT_UTF8;52values[key].s_value = value;53values[key].max_size = max_size;54}5556void ParamSFOData::SetValue(const std::string &key, const u8 *value, unsigned int size, int max_size) {57values[key].type = VT_UTF8_SPE;58values[key].SetData(value, size);59values[key].max_size = max_size;60}6162int ParamSFOData::GetValueInt(const std::string &key) const {63std::map<std::string,ValueData>::const_iterator it = values.find(key);64if (it == values.end() || it->second.type != VT_INT)65return 0;66return it->second.i_value;67}6869std::string ParamSFOData::GetValueString(const std::string &key) const {70std::map<std::string,ValueData>::const_iterator it = values.find(key);71if (it == values.end() || (it->second.type != VT_UTF8))72return "";73return it->second.s_value;74}7576bool ParamSFOData::HasKey(const std::string &key) const {77return values.find(key) != values.end();78}7980const u8 *ParamSFOData::GetValueData(const std::string &key, unsigned int *size) const {81std::map<std::string,ValueData>::const_iterator it = values.find(key);82if (it == values.end() || (it->second.type != VT_UTF8_SPE)) {83return 0;84}85if (size) {86*size = it->second.u_size;87}88return it->second.u_value;89}9091std::vector<std::string> ParamSFOData::GetKeys() const {92std::vector<std::string> result;93for (const auto &pair : values) {94result.push_back(pair.first);95}96return result;97}9899std::string ParamSFOData::GetDiscID() {100const std::string discID = GetValueString("DISC_ID");101if (discID.empty()) {102std::string fakeID = GenerateFakeID(Path());103WARN_LOG(Log::Loader, "No DiscID found - generating a fake one: '%s' (from %s)", fakeID.c_str(), PSP_CoreParameter().fileToStart.c_str());104ValueData data;105data.type = VT_UTF8;106data.s_value = fakeID;107values["DISC_ID"] = data;108return fakeID;109}110return discID;111}112113// I'm so sorry Ced but this is highly endian unsafe :(114bool ParamSFOData::ReadSFO(const u8 *paramsfo, size_t size) {115if (size < sizeof(Header))116return false;117const Header *header = (const Header *)paramsfo;118if (header->magic != 0x46535000)119return false;120if (header->version != 0x00000101)121WARN_LOG(Log::Loader, "Unexpected SFO header version: %08x", header->version);122123const IndexTable *indexTables = (const IndexTable *)(paramsfo + sizeof(Header));124125if (header->key_table_start > size || header->data_table_start > size) {126return false;127}128129const u8 *data_start = paramsfo + header->data_table_start;130131auto readStringCapped = [paramsfo, size](size_t offset, size_t maxLen) -> std::string {132std::string str;133while (offset < size) {134char c = (char)(paramsfo[offset]);135if (c) {136str.push_back(c);137} else {138break;139}140offset++;141if (maxLen != 0 && str.size() == maxLen)142break;143}144return str;145};146147for (u32 i = 0; i < header->index_table_entries; i++)148{149size_t key_offset = header->key_table_start + indexTables[i].key_table_offset;150if (key_offset >= size) {151return false;152}153154size_t data_offset = header->data_table_start + indexTables[i].data_table_offset;155if (data_offset >= size) {156return false;157}158159std::string key = readStringCapped(key_offset, 0);160if (key.empty())161continue; // Likely ran into a truncated PARAMSFO.162163switch (indexTables[i].param_fmt) {164case 0x0404:165{166if (data_offset + 4 > size)167continue;168// Unsigned int169const u32_le *data = (const u32_le *)(paramsfo + data_offset);170SetValue(key, *data, indexTables[i].param_max_len);171VERBOSE_LOG(Log::Loader, "%s %08x", key.c_str(), *data);172}173break;174case 0x0004:175// Special format UTF-8176{177if (data_offset + indexTables[i].param_len > size)178continue;179const u8 *utfdata = (const u8 *)(paramsfo + data_offset);180VERBOSE_LOG(Log::Loader, "%s %s", key.c_str(), utfdata);181SetValue(key, utfdata, indexTables[i].param_len, indexTables[i].param_max_len);182}183break;184case 0x0204:185// Regular UTF-8186{187// TODO: Likely should use param_len here, but there's gotta be a reason we avoided it before.188std::string str = readStringCapped(data_offset, indexTables[i].param_max_len);189VERBOSE_LOG(Log::Loader, "%s %s", key.c_str(), str.c_str());190SetValue(key, str, indexTables[i].param_max_len);191}192break;193default:194break;195}196}197198return true;199}200201int ParamSFOData::GetDataOffset(const u8 *paramsfo, const char *dataName) {202const Header *header = (const Header *)paramsfo;203if (header->magic != 0x46535000)204return -1;205if (header->version != 0x00000101)206WARN_LOG(Log::Loader, "Unexpected SFO header version: %08x", header->version);207208const IndexTable *indexTables = (const IndexTable *)(paramsfo + sizeof(Header));209210const u8 *key_start = paramsfo + header->key_table_start;211int data_start = header->data_table_start;212213for (u32 i = 0; i < header->index_table_entries; i++)214{215const char *key = (const char *)(key_start + indexTables[i].key_table_offset);216if (!strcmp(key, dataName))217{218return data_start + indexTables[i].data_table_offset;219}220}221222return -1;223}224225void ParamSFOData::WriteSFO(u8 **paramsfo, size_t *size) const {226size_t total_size = 0;227size_t key_size = 0;228size_t data_size = 0;229230Header header;231header.magic = 0x46535000;232header.version = 0x00000101;233header.index_table_entries = 0;234235total_size += sizeof(Header);236237// Get size info238for (const auto &[k, v] : values)239{240key_size += k.size() + 1;241data_size += v.max_size;242243header.index_table_entries++;244}245246// Padding247while ((key_size % 4) != 0) key_size++;248249header.key_table_start = sizeof(Header) + header.index_table_entries * sizeof(IndexTable);250header.data_table_start = header.key_table_start + (u32)key_size;251252total_size += sizeof(IndexTable) * header.index_table_entries;253total_size += key_size;254total_size += data_size;255*size = total_size;256257size_t aligned_size = (total_size + 15) & ~15;258u8* data = new u8[aligned_size];259*paramsfo = data;260memset(data, 0, aligned_size);261memcpy(data, &header, sizeof(Header));262263// Now fill264IndexTable *index_ptr = (IndexTable*)(data + sizeof(Header));265u8* key_ptr = data + header.key_table_start;266u8* data_ptr = data + header.data_table_start;267268for (const auto &[k, v] : values)269{270u16 offset = (u16)(key_ptr - (data+header.key_table_start));271index_ptr->key_table_offset = offset;272offset = (u16)(data_ptr - (data+header.data_table_start));273index_ptr->data_table_offset = offset;274index_ptr->param_max_len = v.max_size;275if (v.type == VT_INT)276{277index_ptr->param_fmt = 0x0404;278index_ptr->param_len = 4;279280*(s32_le *)data_ptr = v.i_value;281}282else if (v.type == VT_UTF8_SPE)283{284index_ptr->param_fmt = 0x0004;285index_ptr->param_len = v.u_size;286287memset(data_ptr,0,index_ptr->param_max_len);288memcpy(data_ptr,v.u_value,index_ptr->param_len);289}290else if (v.type == VT_UTF8)291{292index_ptr->param_fmt = 0x0204;293index_ptr->param_len = (u32)v.s_value.size()+1;294295memcpy(data_ptr,v.s_value.c_str(),index_ptr->param_len);296data_ptr[index_ptr->param_len] = 0;297}298299memcpy(key_ptr,k.c_str(),k.size());300key_ptr[k.size()] = 0;301302data_ptr += index_ptr->param_max_len;303key_ptr += k.size() + 1;304index_ptr++;305306}307}308309void ParamSFOData::Clear() {310values.clear();311}312313void ParamSFOData::ValueData::SetData(const u8* data, int size) {314if (u_value) {315delete[] u_value;316u_value = 0;317}318if (size > 0) {319u_value = new u8[size];320memcpy(u_value, data, size);321}322u_size = size;323}324325std::string ParamSFOData::GenerateFakeID(const Path &filename) const {326// Generates fake gameID for homebrew based on it's folder name.327// Should probably not be a part of ParamSFO, but it'll be called in same places.328// FileToStart here is actually a directory name, not a file, so taking GetFilename on it gets what we want.329Path path = PSP_CoreParameter().fileToStart;330if (!filename.empty())331path = filename;332333std::string file = path.GetFilename();334335int sumOfAllLetters = 0;336for (char &c : file) {337sumOfAllLetters += c;338// Get rid of some garbage characters than can arise when opening content URIs. Well, I've only seen '%', but...339if (strchr("%() []", c) != nullptr) {340c = 'X';341} else {342c = toupper(c);343}344}345346if (file.size() < 4) {347file += "HOME";348}349file = file.substr(0, 4);350351std::string fakeID = file + StringFromFormat("%05d", sumOfAllLetters);352return fakeID;353}354355356