2023-09-05 02:06:35 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2017-2018 Adubbz
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//https://switchbrew.org/wiki/NCA
|
|
|
|
|
|
|
|
#include "install/nsp.hpp"
|
|
|
|
|
|
|
|
#include <threads.h>
|
|
|
|
#include "data/buffered_placeholder_writer.hpp"
|
|
|
|
#include "util/title_util.hpp"
|
|
|
|
#include "util/error.hpp"
|
|
|
|
#include "util/debug.h"
|
|
|
|
|
2023-09-25 15:48:26 +01:00
|
|
|
//
|
|
|
|
#include "util/lang.hpp"
|
|
|
|
#include "ui/MainApplication.hpp"
|
|
|
|
namespace inst::ui {
|
|
|
|
extern MainApplication* mainApp;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
2023-09-05 02:06:35 +01:00
|
|
|
namespace tin::install::nsp
|
|
|
|
{
|
|
|
|
NSP::NSP() {}
|
|
|
|
|
|
|
|
// TODO: Do verification: PFS0 magic, sizes not zero
|
|
|
|
void NSP::RetrieveHeader()
|
|
|
|
{
|
|
|
|
LOG_DEBUG("Retrieving remote NSP header...\n");
|
|
|
|
|
|
|
|
// Retrieve the base header
|
|
|
|
m_headerBytes.resize(sizeof(PFS0BaseHeader), 0);
|
|
|
|
this->BufferData(m_headerBytes.data(), 0x0, sizeof(PFS0BaseHeader));
|
|
|
|
|
|
|
|
LOG_DEBUG("Base header: \n");
|
|
|
|
printBytes(m_headerBytes.data(), sizeof(PFS0BaseHeader), true);
|
|
|
|
|
|
|
|
// Retrieve the full header
|
|
|
|
size_t remainingHeaderSize = this->GetBaseHeader()->numFiles * sizeof(PFS0FileEntry) + this->GetBaseHeader()->stringTableSize;
|
|
|
|
m_headerBytes.resize(sizeof(PFS0BaseHeader) + remainingHeaderSize, 0);
|
|
|
|
this->BufferData(m_headerBytes.data() + sizeof(PFS0BaseHeader), sizeof(PFS0BaseHeader), remainingHeaderSize);
|
|
|
|
|
|
|
|
LOG_DEBUG("Full header: \n");
|
|
|
|
printBytes(m_headerBytes.data(), m_headerBytes.size(), true);
|
|
|
|
|
|
|
|
/* debug print header
|
|
|
|
FILE * pFile;
|
|
|
|
pFile = fopen ("header.hxd", "wb");
|
|
|
|
fwrite (m_headerBytes.data(), sizeof(char), m_headerBytes.size(), pFile);
|
|
|
|
fclose (pFile);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
const PFS0FileEntry* NSP::GetFileEntry(unsigned int index)
|
|
|
|
{
|
|
|
|
if (index >= this->GetBaseHeader()->numFiles)
|
|
|
|
THROW_FORMAT("File entry index is out of bounds\n")
|
|
|
|
|
|
|
|
size_t fileEntryOffset = sizeof(PFS0BaseHeader) + index * sizeof(PFS0FileEntry);
|
|
|
|
|
|
|
|
if (m_headerBytes.size() < fileEntryOffset + sizeof(PFS0FileEntry))
|
|
|
|
THROW_FORMAT("Header bytes is too small to get file entry!");
|
|
|
|
|
|
|
|
return reinterpret_cast<PFS0FileEntry*>(m_headerBytes.data() + fileEntryOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const PFS0FileEntry*> NSP::GetFileEntriesByExtension(std::string extension)
|
|
|
|
{
|
|
|
|
std::vector<const PFS0FileEntry*> entryList;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < this->GetBaseHeader()->numFiles; i++)
|
|
|
|
{
|
|
|
|
const PFS0FileEntry* fileEntry = this->GetFileEntry(i);
|
|
|
|
std::string name(this->GetFileEntryName(fileEntry));
|
2023-09-20 23:09:07 +01:00
|
|
|
auto foundExtension = name.substr(name.find(".") + 1);
|
2023-09-25 15:48:26 +01:00
|
|
|
|
2023-09-22 05:01:17 +01:00
|
|
|
// fix cert filename extension becoming corrupted when xcz/nsz is installing certs.
|
2023-09-20 23:09:07 +01:00
|
|
|
std::string cert ("cert");
|
|
|
|
std::size_t found = name.find(cert);
|
|
|
|
if (found!=std::string::npos){
|
|
|
|
int pos = 0;
|
|
|
|
std::string mystr = name;
|
|
|
|
pos = mystr.find_last_of('.');
|
|
|
|
mystr = mystr.substr(5, pos);
|
|
|
|
foundExtension = mystr.substr(mystr.find(".") + 1);
|
|
|
|
}
|
2023-09-05 02:06:35 +01:00
|
|
|
|
|
|
|
if (foundExtension == extension)
|
|
|
|
entryList.push_back(fileEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entryList;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PFS0FileEntry* NSP::GetFileEntryByName(std::string name)
|
|
|
|
{
|
2023-09-20 21:04:52 +01:00
|
|
|
//returns only the .nca and .cnmt.nca filenames
|
2023-09-05 02:06:35 +01:00
|
|
|
for (unsigned int i = 0; i < this->GetBaseHeader()->numFiles; i++)
|
|
|
|
{
|
|
|
|
const PFS0FileEntry* fileEntry = this->GetFileEntry(i);
|
|
|
|
std::string foundName(this->GetFileEntryName(fileEntry));
|
|
|
|
|
2023-09-20 21:04:52 +01:00
|
|
|
|
|
|
|
if (foundName == name) {
|
|
|
|
/*
|
|
|
|
//Debug code
|
|
|
|
FILE * fp;
|
|
|
|
fp = fopen ("name.txt", "a+");
|
|
|
|
std::string x = foundName;
|
|
|
|
const char *info = x.c_str();
|
|
|
|
fprintf(fp, "%s\n", info);
|
|
|
|
fclose(fp);
|
|
|
|
*/
|
|
|
|
|
2023-09-05 02:06:35 +01:00
|
|
|
return fileEntry;
|
2023-09-20 21:04:52 +01:00
|
|
|
}
|
2023-09-05 02:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PFS0FileEntry* NSP::GetFileEntryByNcaId(const NcmContentId& ncaId)
|
|
|
|
{
|
|
|
|
const PFS0FileEntry* fileEntry = nullptr;
|
|
|
|
std::string ncaIdStr = tin::util::GetNcaIdString(ncaId);
|
|
|
|
|
|
|
|
if ((fileEntry = this->GetFileEntryByName(ncaIdStr + ".nca")) == nullptr)
|
|
|
|
{
|
|
|
|
if ((fileEntry = this->GetFileEntryByName(ncaIdStr + ".cnmt.nca")) == nullptr)
|
|
|
|
{
|
|
|
|
if ((fileEntry = this->GetFileEntryByName(ncaIdStr + ".ncz")) == nullptr)
|
|
|
|
{
|
|
|
|
if ((fileEntry = this->GetFileEntryByName(ncaIdStr + ".cnmt.ncz")) == nullptr)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* NSP::GetFileEntryName(const PFS0FileEntry* fileEntry)
|
|
|
|
{
|
|
|
|
u64 stringTableStart = sizeof(PFS0BaseHeader) + this->GetBaseHeader()->numFiles * sizeof(PFS0FileEntry);
|
2023-09-26 00:57:18 +01:00
|
|
|
|
2023-09-20 21:04:52 +01:00
|
|
|
//check for messed up filenames in our table.... usually when instaling xcz/xci
|
|
|
|
/*
|
|
|
|
FILE * fp;
|
|
|
|
fp = fopen ("st.txt", "a+");
|
|
|
|
std::string x = reinterpret_cast<const char*>(m_headerBytes.data() + stringTableStart + fileEntry->stringTableOffset);
|
|
|
|
const char *info = x.c_str();
|
|
|
|
fprintf(fp, "%s\n", info);
|
|
|
|
fclose(fp);
|
|
|
|
*/
|
|
|
|
|
2023-09-05 02:06:35 +01:00
|
|
|
return reinterpret_cast<const char*>(m_headerBytes.data() + stringTableStart + fileEntry->stringTableOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PFS0BaseHeader* NSP::GetBaseHeader()
|
|
|
|
{
|
|
|
|
if (m_headerBytes.empty())
|
|
|
|
THROW_FORMAT("Cannot retrieve header as header bytes are empty. Have you retrieved it yet?\n");
|
|
|
|
|
|
|
|
return reinterpret_cast<PFS0BaseHeader*>(m_headerBytes.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 NSP::GetDataOffset()
|
|
|
|
{
|
|
|
|
if (m_headerBytes.empty())
|
|
|
|
THROW_FORMAT("Cannot get data offset as header is empty. Have you retrieved it yet?\n");
|
|
|
|
return m_headerBytes.size();
|
|
|
|
}
|
|
|
|
}
|