2017-08-05 13:09:50 +00:00
|
|
|
#include <nx/PfsHeader.h>
|
2017-07-18 14:17:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nx::PfsHeader::PfsHeader()
|
|
|
|
{}
|
|
|
|
|
|
|
|
nx::PfsHeader::PfsHeader(const PfsHeader & other)
|
|
|
|
{
|
|
|
|
copyFrom(other);
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
nx::PfsHeader::PfsHeader(const byte_t * bytes, size_t len)
|
2017-07-18 14:17:32 +00:00
|
|
|
{
|
|
|
|
importBinary(bytes, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::exportBinary()
|
|
|
|
{
|
|
|
|
// calculate name table size
|
|
|
|
size_t name_table_size = 0;
|
|
|
|
for (size_t i = 0; i < mFileList.getSize(); i++)
|
|
|
|
{
|
|
|
|
name_table_size += mFileList[i].name.length() + 1;
|
|
|
|
}
|
|
|
|
|
2018-04-15 02:41:29 +00:00
|
|
|
size_t pfs_header_size = align(sizeof(sPfsHeader) + getFileEntrySize(mFsType) * mFileList.getSize() + name_table_size, pfs::kHeaderAlign);
|
2017-07-18 14:17:32 +00:00
|
|
|
|
|
|
|
// align name_table_size
|
2018-04-07 08:03:01 +00:00
|
|
|
name_table_size = pfs_header_size - (sizeof(sPfsHeader) + getFileEntrySize(mFsType) * mFileList.getSize());
|
2017-07-18 14:17:32 +00:00
|
|
|
|
|
|
|
// allocate pfs header binary
|
|
|
|
mBinaryBlob.alloc(pfs_header_size);
|
|
|
|
sPfsHeader* hdr = (sPfsHeader*)mBinaryBlob.getBytes();
|
|
|
|
|
|
|
|
// set header fields
|
2018-04-07 08:03:01 +00:00
|
|
|
switch (mFsType)
|
|
|
|
{
|
|
|
|
case (TYPE_PFS0):
|
|
|
|
strncpy(hdr->signature, pfs::kPfsSig.c_str(), 4);
|
|
|
|
break;
|
|
|
|
case (TYPE_HFS0):
|
|
|
|
strncpy(hdr->signature, pfs::kHashedPfsSig.c_str(), 4);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-26 04:07:42 +00:00
|
|
|
hdr->file_num = (uint32_t)mFileList.getSize();
|
|
|
|
hdr->name_table_size = (uint32_t)name_table_size;
|
2017-07-18 14:17:32 +00:00
|
|
|
|
|
|
|
// set file entries
|
2018-04-07 08:03:01 +00:00
|
|
|
if (mFsType == TYPE_PFS0)
|
|
|
|
{
|
|
|
|
sPfsFile* raw_files = (sPfsFile*)(mBinaryBlob.getBytes() + sizeof(sPfsHeader));
|
|
|
|
char* raw_name_table = (char*)(mBinaryBlob.getBytes() + sizeof(sPfsHeader) + sizeof(sPfsFile) * mFileList.getSize());
|
|
|
|
size_t raw_name_table_pos = 0;
|
2017-07-18 14:17:32 +00:00
|
|
|
|
2018-04-07 08:03:01 +00:00
|
|
|
calculateOffsets(pfs_header_size);
|
|
|
|
for (size_t i = 0; i < mFileList.getSize(); i++)
|
|
|
|
{
|
|
|
|
raw_files[i].data_offset = (mFileList[i].offset - pfs_header_size);
|
|
|
|
raw_files[i].size = mFileList[i].size;
|
2018-05-26 04:07:42 +00:00
|
|
|
raw_files[i].name_offset = (uint32_t)raw_name_table_pos;
|
2018-04-07 08:03:01 +00:00
|
|
|
|
|
|
|
strcpy(raw_name_table + raw_name_table_pos, mFileList[i].name.c_str());
|
2018-05-26 04:07:42 +00:00
|
|
|
raw_name_table_pos += (uint32_t)(mFileList[i].name.length() + 1);
|
2018-04-07 08:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mFsType == TYPE_HFS0)
|
2017-07-18 14:17:32 +00:00
|
|
|
{
|
2018-04-07 08:03:01 +00:00
|
|
|
sHashedPfsFile* raw_files = (sHashedPfsFile*)(mBinaryBlob.getBytes() + sizeof(sPfsHeader));
|
|
|
|
char* raw_name_table = (char*)(mBinaryBlob.getBytes() + sizeof(sPfsHeader) + sizeof(sHashedPfsFile) * mFileList.getSize());
|
|
|
|
size_t raw_name_table_pos = 0;
|
|
|
|
|
|
|
|
calculateOffsets(pfs_header_size);
|
|
|
|
for (size_t i = 0; i < mFileList.getSize(); i++)
|
|
|
|
{
|
|
|
|
raw_files[i].data_offset = (mFileList[i].offset - pfs_header_size);
|
|
|
|
raw_files[i].size = mFileList[i].size;
|
2018-05-26 04:07:42 +00:00
|
|
|
raw_files[i].name_offset = (uint32_t)raw_name_table_pos;
|
|
|
|
raw_files[i].hash_protected_size = (uint32_t)mFileList[i].hash_protected_size;
|
2018-04-07 08:03:01 +00:00
|
|
|
raw_files[i].hash = mFileList[i].hash;
|
2017-07-18 14:17:32 +00:00
|
|
|
|
2018-04-07 08:03:01 +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
|
|
|
}
|
2018-04-07 08:03:01 +00:00
|
|
|
|
2017-07-18 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
void nx::PfsHeader::importBinary(const byte_t * bytes, 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");
|
|
|
|
}
|
|
|
|
|
|
|
|
// import minimum header
|
|
|
|
mBinaryBlob.alloc(sizeof(sPfsHeader));
|
|
|
|
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
|
|
|
|
const sPfsHeader* hdr = (const sPfsHeader*)mBinaryBlob.getBytes();
|
|
|
|
|
|
|
|
// check struct signature
|
2018-04-07 08:03:01 +00:00
|
|
|
FsType fs_type;
|
|
|
|
if (memcmp(hdr->signature, pfs::kPfsSig.c_str(), 4) == 0)
|
|
|
|
fs_type = TYPE_PFS0;
|
|
|
|
else if (memcmp(hdr->signature, pfs::kHashedPfsSig.c_str(), 4) == 0)
|
|
|
|
fs_type = TYPE_HFS0;
|
|
|
|
else
|
2017-07-18 14:17:32 +00:00
|
|
|
throw fnd::Exception(kModuleName, "PFS header corrupt");
|
|
|
|
|
|
|
|
// determine complete header size
|
2018-04-07 08:03:01 +00:00
|
|
|
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
|
|
|
|
mBinaryBlob.alloc(pfs_full_header_size);
|
|
|
|
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
|
|
|
|
hdr = (const sPfsHeader*)mBinaryBlob.getBytes();
|
|
|
|
|
|
|
|
// clear variables
|
|
|
|
clear();
|
|
|
|
|
2018-04-07 08:03:01 +00:00
|
|
|
mFsType = fs_type;
|
|
|
|
if (mFsType == TYPE_PFS0)
|
|
|
|
{
|
|
|
|
// get pointers to raw data
|
|
|
|
const sPfsFile* raw_files = (const sPfsFile*)(mBinaryBlob.getBytes() + sizeof(sPfsHeader));
|
|
|
|
const char* raw_name_table = (const char*)(mBinaryBlob.getBytes() + sizeof(sPfsHeader) + sizeof(sPfsFile) * hdr->file_num.get());
|
2017-07-18 14:17:32 +00:00
|
|
|
|
2018-04-07 08:03:01 +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
|
|
|
{
|
2018-04-07 08:03:01 +00:00
|
|
|
// get pointers to raw data
|
|
|
|
const sHashedPfsFile* raw_files = (const sHashedPfsFile*)(mBinaryBlob.getBytes() + sizeof(sPfsHeader));
|
|
|
|
const char* raw_name_table = (const char*)(mBinaryBlob.getBytes() + 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
|
|
|
}
|
2018-04-07 08:03:01 +00:00
|
|
|
|
2017-07-18 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::clear()
|
|
|
|
{
|
|
|
|
mBinaryBlob.clear();
|
2018-04-07 08:03:01 +00:00
|
|
|
mFsType = TYPE_PFS0;
|
2017-07-18 14:17:32 +00:00
|
|
|
mFileList.clear();
|
|
|
|
}
|
|
|
|
|
2018-04-07 08:03:01 +00:00
|
|
|
nx::PfsHeader::FsType nx::PfsHeader::getFsType() const
|
|
|
|
{
|
|
|
|
return mFsType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::setFsType(FsType type)
|
|
|
|
{
|
|
|
|
mFsType = type;
|
|
|
|
}
|
|
|
|
|
2017-07-18 14:17:32 +00:00
|
|
|
const fnd::List<nx::PfsHeader::sFile>& nx::PfsHeader::getFileList() const
|
|
|
|
{
|
|
|
|
return mFileList;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::addFile(const std::string & name, size_t size)
|
|
|
|
{
|
2018-04-07 08:03:01 +00:00
|
|
|
mFileList.addElement({ name, 0, size, 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::addFile(const std::string & name, size_t size, size_t hash_protected_size, const crypto::sha::sSha256Hash& hash)
|
|
|
|
{
|
|
|
|
mFileList.addElement({ name, 0, size, hash_protected_size, hash });
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nx::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
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::calculateOffsets(size_t data_offset)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < mFileList.getSize(); i++)
|
|
|
|
{
|
|
|
|
mFileList[i].offset = (i == 0) ? data_offset : mFileList[i - 1].offset + mFileList[i - 1].size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nx::PfsHeader::isEqual(const PfsHeader & other) const
|
|
|
|
{
|
2018-04-07 08:03:01 +00:00
|
|
|
return (mFsType == other.mFsType) && (mFileList == other.mFileList);
|
2017-07-18 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::copyFrom(const PfsHeader & other)
|
|
|
|
{
|
|
|
|
if (other.getSize())
|
|
|
|
{
|
|
|
|
importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clear();
|
2018-04-07 08:03:01 +00:00
|
|
|
mFsType = other.mFsType;
|
2017-07-18 14:17:32 +00:00
|
|
|
mFileList = other.mFileList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nx::PfsHeader::operator==(const PfsHeader & other) const
|
|
|
|
{
|
|
|
|
return isEqual(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nx::PfsHeader::operator!=(const PfsHeader & other) const
|
|
|
|
{
|
|
|
|
return !isEqual(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::PfsHeader::operator=(const PfsHeader & other)
|
|
|
|
{
|
|
|
|
copyFrom(other);
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
const byte_t * nx::PfsHeader::getBytes() const
|
2017-07-18 14:17:32 +00:00
|
|
|
{
|
|
|
|
return mBinaryBlob.getBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nx::PfsHeader::getSize() const
|
|
|
|
{
|
|
|
|
return mBinaryBlob.getSize();
|
|
|
|
}
|