nstool/lib/libhac/source/PfsHeader.cpp

253 lines
6.6 KiB
C++
Raw Permalink Normal View History

2018-08-07 07:17:51 +00:00
#include <nn/hac/PfsHeader.h>
2017-07-18 14:17:32 +00:00
2018-08-07 07:17:51 +00:00
nn::hac::PfsHeader::PfsHeader()
2018-06-24 08:18:54 +00:00
{
clear();
}
2017-07-18 14:17:32 +00:00
2018-08-07 07:17:51 +00:00
nn::hac::PfsHeader::PfsHeader(const PfsHeader & other)
2017-07-18 14:17:32 +00:00
{
2018-06-24 08:18:54 +00:00
*this = other;
}
2018-08-07 07:17:51 +00:00
void nn::hac::PfsHeader::operator=(const PfsHeader & other)
2018-06-24 08:18:54 +00:00
{
if (other.getBytes().size())
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
mFsType = other.mFsType;
mFileList = other.mFileList;
}
}
2018-08-07 07:17:51 +00:00
bool nn::hac::PfsHeader::operator==(const PfsHeader & other) const
2018-06-24 08:18:54 +00:00
{
return (mFsType == other.mFsType) \
&& (mFileList == other.mFileList);
2017-07-18 14:17:32 +00:00
}
2018-08-07 07:17:51 +00:00
bool nn::hac::PfsHeader::operator!=(const PfsHeader & other) const
2017-07-18 14:17:32 +00:00
{
2018-06-24 08:18:54 +00:00
return !(*this == other);
2017-07-18 14:17:32 +00:00
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::hac::PfsHeader::getBytes() const
2018-06-24 08:18:54 +00:00
{
return mRawBinary;
}
2018-08-07 07:17:51 +00:00
void nn::hac::PfsHeader::toBytes()
2017-07-18 14:17:32 +00:00
{
// calculate name table size
size_t name_table_size = 0;
2018-06-24 08:18:54 +00:00
for (size_t i = 0; i < mFileList.size(); i++)
2017-07-18 14:17:32 +00:00
{
name_table_size += mFileList[i].name.length() + 1;
}
2018-06-24 08:18:54 +00:00
size_t pfs_header_size = align(sizeof(sPfsHeader) + getFileEntrySize(mFsType) * mFileList.size() + name_table_size, pfs::kHeaderAlign);
2017-07-18 14:17:32 +00:00
// align name_table_size
2018-06-24 08:18:54 +00:00
name_table_size = pfs_header_size - (sizeof(sPfsHeader) + getFileEntrySize(mFsType) * mFileList.size());
2017-07-18 14:17:32 +00:00
// allocate pfs header binary
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(pfs_header_size);
sPfsHeader* hdr = (sPfsHeader*)mRawBinary.data();
2017-07-18 14:17:32 +00:00
// set header fields
switch (mFsType)
{
case (TYPE_PFS0):
hdr->st_magic = pfs::kPfsStructMagic;
break;
case (TYPE_HFS0):
hdr->st_magic = pfs::kHashedPfsStructMagic;
break;
}
2018-06-24 08:18:54 +00:00
hdr->file_num = (uint32_t)mFileList.size();
hdr->name_table_size = (uint32_t)name_table_size;
2017-07-18 14:17:32 +00:00
// set file entries
if (mFsType == TYPE_PFS0)
{
2018-06-24 08:18:54 +00:00
sPfsFile* raw_files = (sPfsFile*)(mRawBinary.data() + sizeof(sPfsHeader));
char* raw_name_table = (char*)(mRawBinary.data() + sizeof(sPfsHeader) + sizeof(sPfsFile) * mFileList.size());
size_t raw_name_table_pos = 0;
2017-07-18 14:17:32 +00:00
calculateOffsets(pfs_header_size);
2018-06-24 08:18:54 +00:00
for (size_t i = 0; i < mFileList.size(); i++)
{
raw_files[i].data_offset = (mFileList[i].offset - pfs_header_size);
raw_files[i].size = mFileList[i].size;
raw_files[i].name_offset = (uint32_t)raw_name_table_pos;
strcpy(raw_name_table + raw_name_table_pos, mFileList[i].name.c_str());
raw_name_table_pos += (uint32_t)(mFileList[i].name.length() + 1);
}
}
else if (mFsType == TYPE_HFS0)
2017-07-18 14:17:32 +00:00
{
2018-06-24 08:18:54 +00:00
sHashedPfsFile* raw_files = (sHashedPfsFile*)(mRawBinary.data() + sizeof(sPfsHeader));
char* raw_name_table = (char*)(mRawBinary.data() + sizeof(sPfsHeader) + sizeof(sHashedPfsFile) * mFileList.size());
size_t raw_name_table_pos = 0;
calculateOffsets(pfs_header_size);
2018-06-24 08:18:54 +00:00
for (size_t i = 0; i < mFileList.size(); i++)
{
raw_files[i].data_offset = (mFileList[i].offset - pfs_header_size);
raw_files[i].size = mFileList[i].size;
raw_files[i].name_offset = (uint32_t)raw_name_table_pos;
raw_files[i].hash_protected_size = (uint32_t)mFileList[i].hash_protected_size;
raw_files[i].hash = mFileList[i].hash;
2017-07-18 14:17:32 +00:00
strcpy(raw_name_table + raw_name_table_pos, mFileList[i].name.c_str());
raw_name_table_pos += mFileList[i].name.length() + 1;
}
2017-07-18 14:17:32 +00:00
}
2017-07-18 14:17:32 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::hac::PfsHeader::fromBytes(const byte_t* data, size_t len)
2017-07-18 14:17:32 +00:00
{
// check input length meets minimum size
if (len < sizeof(sPfsHeader))
{
throw fnd::Exception(kModuleName, "PFS header too small");
}
// clear variables
clear();
2017-07-18 14:17:32 +00:00
// import minimum header
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(sizeof(sPfsHeader));
memcpy(mRawBinary.data(), data, mRawBinary.size());
const sPfsHeader* hdr = (const sPfsHeader*)mRawBinary.data();
2017-07-18 14:17:32 +00:00
// check struct signature
FsType fs_type;
switch(hdr->st_magic.get())
{
case (pfs::kPfsStructMagic):
fs_type = TYPE_PFS0;
break;
case (pfs::kHashedPfsStructMagic):
fs_type = TYPE_HFS0;
break;
default:
throw fnd::Exception(kModuleName, "PFS header corrupt");
}
2017-07-18 14:17:32 +00:00
// determine complete header size
size_t pfs_full_header_size = sizeof(sPfsHeader) + getFileEntrySize(fs_type) * hdr->file_num.get() + hdr->name_table_size.get();
2017-07-18 14:17:32 +00:00
// check input length meets complete size
if (len < pfs_full_header_size)
{
throw fnd::Exception(kModuleName, "PFS header too small");
}
// import full header
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(pfs_full_header_size);
memcpy(mRawBinary.data(), data, mRawBinary.size());
hdr = (const sPfsHeader*)mRawBinary.data();
2017-07-18 14:17:32 +00:00
mFsType = fs_type;
if (mFsType == TYPE_PFS0)
{
// get pointers to raw data
2018-06-24 08:18:54 +00:00
const sPfsFile* raw_files = (const sPfsFile*)(mRawBinary.data() + sizeof(sPfsHeader));
const char* raw_name_table = (const char*)(mRawBinary.data() + sizeof(sPfsHeader) + sizeof(sPfsFile) * hdr->file_num.get());
2017-07-18 14:17:32 +00:00
// process file entries
for (size_t i = 0; i < hdr->file_num.get(); i++)
{
mFileList.addElement({
std::string(raw_name_table + raw_files[i].name_offset.get()),
raw_files[i].data_offset.get() + pfs_full_header_size,
raw_files[i].size.get()
});
}
}
else if (mFsType == TYPE_HFS0)
2017-07-18 14:17:32 +00:00
{
// get pointers to raw data
2018-06-24 08:18:54 +00:00
const sHashedPfsFile* raw_files = (const sHashedPfsFile*)(mRawBinary.data() + sizeof(sPfsHeader));
const char* raw_name_table = (const char*)(mRawBinary.data() + sizeof(sPfsHeader) + sizeof(sHashedPfsFile) * hdr->file_num.get());
// process file entries
for (size_t i = 0; i < hdr->file_num.get(); i++)
{
mFileList.addElement({
std::string(raw_name_table + raw_files[i].name_offset.get()),
raw_files[i].data_offset.get() + pfs_full_header_size,
raw_files[i].size.get(),
raw_files[i].hash_protected_size.get(),
raw_files[i].hash
});
}
2017-07-18 14:17:32 +00:00
}
2017-07-18 14:17:32 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::hac::PfsHeader::clear()
2017-07-18 14:17:32 +00:00
{
2018-06-24 08:18:54 +00:00
mRawBinary.clear();
mFsType = TYPE_PFS0;
2017-07-18 14:17:32 +00:00
mFileList.clear();
}
2018-08-07 07:17:51 +00:00
nn::hac::PfsHeader::FsType nn::hac::PfsHeader::getFsType() const
{
return mFsType;
}
2018-08-07 07:17:51 +00:00
void nn::hac::PfsHeader::setFsType(FsType type)
{
mFsType = type;
}
2018-08-07 07:17:51 +00:00
const fnd::List<nn::hac::PfsHeader::sFile>& nn::hac::PfsHeader::getFileList() const
2017-07-18 14:17:32 +00:00
{
return mFileList;
}
2018-08-07 07:17:51 +00:00
void nn::hac::PfsHeader::addFile(const std::string & name, size_t size)
2017-07-18 14:17:32 +00:00
{
mFileList.addElement({ name, 0, size, 0 });
}
void nn::hac::PfsHeader::addFile(const std::string & name, size_t size, size_t hash_protected_size, const fnd::sha::sSha256Hash& hash)
{
mFileList.addElement({ name, 0, size, hash_protected_size, hash });
}
2018-08-07 07:17:51 +00:00
size_t nn::hac::PfsHeader::getFileEntrySize(FsType fs_type)
{
size_t size = 0;
switch(fs_type)
{
case (TYPE_PFS0):
size = sizeof(sPfsFile);
break;
case (TYPE_HFS0):
size = sizeof(sHashedPfsFile);
break;
default:
throw fnd::Exception(kModuleName, "Unknown PFS type");
}
return size;
2017-07-18 14:17:32 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::hac::PfsHeader::calculateOffsets(size_t data_offset)
2017-07-18 14:17:32 +00:00
{
2018-06-24 08:18:54 +00:00
for (size_t i = 0; i < mFileList.size(); i++)
2017-07-18 14:17:32 +00:00
{
mFileList[i].offset = (i == 0) ? data_offset : mFileList[i - 1].offset + mFileList[i - 1].size;
}
2018-06-24 08:18:54 +00:00
}