2017-08-05 13:09:50 +00:00
|
|
|
#include <nx/FacHeader.h>
|
2017-07-06 10:57:33 +00:00
|
|
|
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
nx::FacHeader::FacHeader() :
|
|
|
|
mFsaRights()
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
clear();
|
2017-07-06 10:57:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
nx::FacHeader::FacHeader(const FacHeader & other) :
|
|
|
|
mFsaRights()
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
copyFrom(other);
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
nx::FacHeader::FacHeader(const byte_t * bytes, size_t len) :
|
2017-07-17 08:21:39 +00:00
|
|
|
mFsaRights()
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
importBinary(bytes, len);
|
2017-07-06 10:57:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
bool nx::FacHeader::operator==(const FacHeader & other) const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return isEqual(other);
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
bool nx::FacHeader::operator!=(const FacHeader & other) const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return !isEqual(other);
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::FacHeader::operator=(const FacHeader & other)
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
copyFrom(other);
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
const byte_t * nx::FacHeader::getBytes() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return mBinaryBlob.getBytes();
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t nx::FacHeader::getSize() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return mBinaryBlob.getSize();
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::FacHeader::exportBinary()
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
mBinaryBlob.alloc(sizeof(sFacHeader));
|
|
|
|
sFacHeader* hdr = (sFacHeader*)mBinaryBlob.getBytes();
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
if (mVersion != kFacFormatVersion)
|
|
|
|
{
|
|
|
|
fnd::Exception(kModuleName, "Unsupported format version");
|
|
|
|
}
|
2018-03-22 05:26:22 +00:00
|
|
|
hdr->version = (mVersion);
|
2017-07-17 08:21:39 +00:00
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
uint64_t flag = 0;
|
2017-07-17 08:21:39 +00:00
|
|
|
for (size_t i = 0; i < mFsaRights.getSize(); i++)
|
|
|
|
{
|
2018-03-22 05:26:22 +00:00
|
|
|
flag |= BIT((uint64_t)mFsaRights[i]);
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
2018-03-22 05:26:22 +00:00
|
|
|
hdr->fac_flags = (flag);
|
2017-07-06 10:57:33 +00:00
|
|
|
|
|
|
|
calculateOffsets();
|
2018-05-26 04:07:42 +00:00
|
|
|
hdr->content_owner_ids.start = (uint32_t)(mContentOwnerIdPos.offset);
|
|
|
|
hdr->content_owner_ids.end = (uint32_t)(mContentOwnerIdPos.offset + mContentOwnerIdPos.size);
|
|
|
|
hdr->save_data_owner_ids.start = (uint32_t)(mSaveDataOwnerIdPos.offset);
|
|
|
|
hdr->save_data_owner_ids.end = (uint32_t)(mSaveDataOwnerIdPos.offset + mSaveDataOwnerIdPos.size);
|
2017-07-06 10:57:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
void nx::FacHeader::importBinary(const byte_t * bytes, size_t len)
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
if (len < sizeof(sFacHeader))
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "FAC header too small");
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:57:33 +00:00
|
|
|
mBinaryBlob.alloc(sizeof(sFacHeader));
|
|
|
|
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
|
|
|
|
sFacHeader* hdr = (sFacHeader*)mBinaryBlob.getBytes();
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
if (hdr->version.get() != kFacFormatVersion)
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Unsupported FAC format version");
|
|
|
|
}
|
2018-03-22 05:26:22 +00:00
|
|
|
mVersion = hdr->version.get();
|
2017-07-06 10:57:33 +00:00
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
clear();
|
2018-03-22 05:26:22 +00:00
|
|
|
for (uint64_t i = 0; i < 64; i++)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-03-22 05:26:22 +00:00
|
|
|
if ((hdr->fac_flags.get() >> i) & 1)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
mFsaRights.addElement((FsAccessFlag)i);
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 05:26:22 +00:00
|
|
|
mContentOwnerIdPos.offset = hdr->content_owner_ids.start.get();
|
|
|
|
mContentOwnerIdPos.size = hdr->content_owner_ids.end.get() > hdr->content_owner_ids.start.get() ? hdr->content_owner_ids.end.get() - hdr->content_owner_ids.start.get() : 0;
|
|
|
|
mSaveDataOwnerIdPos.offset = hdr->save_data_owner_ids.start.get();
|
|
|
|
mSaveDataOwnerIdPos.size = hdr->save_data_owner_ids.end.get() > hdr->save_data_owner_ids.start.get() ? hdr->save_data_owner_ids.end.get() - hdr->save_data_owner_ids.start.get() : 0;
|
2017-07-06 10:57:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::FacHeader::clear()
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
mFsaRights.clear();
|
|
|
|
mContentOwnerIdPos.offset = 0;
|
|
|
|
mContentOwnerIdPos.size = 0;
|
|
|
|
mSaveDataOwnerIdPos.offset = 0;
|
|
|
|
mSaveDataOwnerIdPos.size = 0;
|
2017-07-06 10:57:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t nx::FacHeader::getFacSize() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t savedata = getSaveDataOwnerIdOffset() + getSaveDataOwnerIdSize();
|
|
|
|
size_t content = getContentOwnerIdOffset() + getContentOwnerIdSize();
|
|
|
|
return MAX(MAX(savedata, content), sizeof(sFacHeader));
|
2017-07-06 10:57:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
uint32_t nx::FacHeader::getFormatVersion() const
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
return mVersion;
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
void nx::FacHeader::setFormatVersion(uint32_t version)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
mVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fnd::List<nx::FacHeader::FsAccessFlag>& nx::FacHeader::getFsaRightsList() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return mFsaRights;
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
void nx::FacHeader::setFsaRightsList(const fnd::List<FsAccessFlag>& list)
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
mFsaRights.clear();
|
|
|
|
for (size_t i = 0; i < list.getSize(); i++)
|
|
|
|
{
|
|
|
|
mFsaRights.hasElement(list[i]) ? mFsaRights.addElement(list[i]) : throw fnd::Exception(kModuleName, "FSA right already exists");
|
|
|
|
}
|
2017-07-06 10:57:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t nx::FacHeader::getContentOwnerIdOffset() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return mContentOwnerIdPos.offset;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t nx::FacHeader::getContentOwnerIdSize() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return mContentOwnerIdPos.size;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::FacHeader::setContentOwnerIdSize(size_t size)
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
mContentOwnerIdPos.size = size;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t nx::FacHeader::getSaveDataOwnerIdOffset() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return mSaveDataOwnerIdPos.offset;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
size_t nx::FacHeader::getSaveDataOwnerIdSize() const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return mSaveDataOwnerIdPos.size;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::FacHeader::setSaveDataOwnerIdSize(size_t size)
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
mSaveDataOwnerIdPos.size = size;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::FacHeader::calculateOffsets()
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
mContentOwnerIdPos.offset = align(sizeof(sFacHeader), 4);
|
|
|
|
mSaveDataOwnerIdPos.offset = mContentOwnerIdPos.offset + align(mContentOwnerIdPos.size, 4);
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
bool nx::FacHeader::isEqual(const FacHeader & other) const
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
return (mFsaRights == other.mFsaRights) \
|
|
|
|
&& (mContentOwnerIdPos.offset == other.mContentOwnerIdPos.offset) \
|
|
|
|
&& (mContentOwnerIdPos.size == other.mContentOwnerIdPos.size) \
|
|
|
|
&& (mSaveDataOwnerIdPos.offset == other.mSaveDataOwnerIdPos.offset) \
|
|
|
|
&& (mSaveDataOwnerIdPos.size == other.mSaveDataOwnerIdPos.size);
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::FacHeader::copyFrom(const FacHeader & other)
|
2017-07-06 10:57:33 +00:00
|
|
|
{
|
|
|
|
if (other.getSize())
|
|
|
|
{
|
|
|
|
importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mBinaryBlob.clear();
|
|
|
|
mFsaRights = other.mFsaRights;
|
|
|
|
mContentOwnerIdPos.offset = other.mContentOwnerIdPos.offset;
|
|
|
|
mContentOwnerIdPos.size = other.mContentOwnerIdPos.size;
|
|
|
|
mSaveDataOwnerIdPos.offset = other.mSaveDataOwnerIdPos.offset;
|
|
|
|
mSaveDataOwnerIdPos.size = other.mSaveDataOwnerIdPos.size;
|
|
|
|
}
|
|
|
|
}
|