nstool/lib/libnx/source/AciHeader.cpp

304 lines
6.4 KiB
C++
Raw Normal View History

#include <nx/AciHeader.h>
2017-07-05 15:39:41 +00:00
2017-07-06 11:17:21 +00:00
using namespace nx;
2018-06-24 08:18:54 +00:00
AciHeader::AciHeader()
2017-07-05 15:39:41 +00:00
{
2018-06-24 08:18:54 +00:00
clear();
2017-07-05 15:39:41 +00:00
}
2018-06-24 08:18:54 +00:00
AciHeader::AciHeader(const AciHeader & other)
{
2018-06-24 08:18:54 +00:00
*this = other;
}
2018-06-24 08:18:54 +00:00
void AciHeader::operator=(const AciHeader & other)
{
2018-06-24 08:18:54 +00:00
if (other.getBytes().size())
{
2018-06-24 08:18:54 +00:00
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
mHeaderOffset = other.mHeaderOffset;
mType = other.mType;
mIsProduction = other.mIsProduction;
mIsUnqualifiedApproval = other.mIsUnqualifiedApproval;
mAcidSize = other.mAcidSize;
mProgramIdMin = other.mProgramIdMin;
mProgramIdMax = other.mProgramIdMax;
mProgramId = other.mProgramId;
mFac = other.mFac;
mSac = other.mSac;
mKc = other.mKc;
}
}
bool AciHeader::operator==(const AciHeader & other) const
{
2018-06-24 08:18:54 +00:00
return (mHeaderOffset == other.mHeaderOffset) \
&& (mType == other.mType) \
&& (mIsProduction == other.mIsProduction) \
&& (mIsUnqualifiedApproval == other.mIsUnqualifiedApproval) \
&& (mAcidSize == other.mAcidSize) \
&& (mProgramIdMin == other.mProgramIdMin) \
&& (mProgramIdMax == other.mProgramIdMax) \
&& (mProgramId == other.mProgramId) \
&& (mFac == other.mFac) \
&& (mSac == other.mSac) \
&& (mKc == other.mKc);
}
bool AciHeader::operator!=(const AciHeader & other) const
{
2018-06-24 08:18:54 +00:00
return !(*this == other);
}
2018-06-24 08:18:54 +00:00
void AciHeader::toBytes()
2017-07-05 15:39:41 +00:00
{
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(sizeof(sAciHeader));
sAciHeader* hdr = (sAciHeader*)mRawBinary.data();
2017-07-05 15:39:41 +00:00
// set type
switch (mType)
{
case (TYPE_ACI0):
hdr->signature = aci::kAciStructSig;
2017-07-05 15:39:41 +00:00
break;
case (TYPE_ACID):
hdr->signature = aci::kAciDescStructSig;
2017-07-05 15:39:41 +00:00
break;
default:
throw fnd::Exception(kModuleName, "Unexpected ACI type");
}
// set offset/size
calculateSectionOffsets();
hdr->fac.offset = (uint32_t)mFac.offset;
hdr->fac.size = (uint32_t)mFac.size;
hdr->sac.offset = (uint32_t)mSac.offset;
hdr->sac.size = (uint32_t)mSac.size;
hdr->kc.offset = (uint32_t)mKc.offset;
hdr->kc.size = (uint32_t)mKc.size;
2018-03-22 05:26:22 +00:00
uint32_t flags = 0;
if (mIsProduction)
flags |= _BIT(aci::FLAG_PRODUCTION);
2018-04-29 03:22:43 +00:00
if (mIsUnqualifiedApproval)
flags |= _BIT(aci::FLAG_UNQUALIFIED_APPROVAL);
hdr->flags = flags;
if (mType == TYPE_ACI0)
{
// set program
hdr->program_id_info.program_id = mProgramId;
}
else if (mType == TYPE_ACID)
{
mAcidSize = getAciSize();
hdr->size = (uint32_t)mAcidSize;
hdr->program_id_info.program_id_restrict.min = mProgramIdMin;
hdr->program_id_info.program_id_restrict.max = mProgramIdMax;
}
2017-07-05 15:39:41 +00:00
}
2018-06-24 08:18:54 +00:00
void AciHeader::fromBytes(const byte_t * bytes, size_t len)
2017-07-05 15:39:41 +00:00
{
if (len < sizeof(sAciHeader))
{
throw fnd::Exception(kModuleName, "ACI header too small");
}
clear();
2017-07-05 15:39:41 +00:00
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(sizeof(sAciHeader));
memcpy(mRawBinary.data(), bytes, sizeof(sAciHeader));
2017-07-05 15:39:41 +00:00
2018-06-24 08:18:54 +00:00
sAciHeader* hdr = (sAciHeader*)mRawBinary.data();
2017-07-05 15:39:41 +00:00
switch (hdr->signature.get())
2017-07-05 15:39:41 +00:00
{
case (aci::kAciStructSig):
2017-07-05 15:39:41 +00:00
mType = TYPE_ACI0;
break;
case (aci::kAciDescStructSig):
2017-07-05 15:39:41 +00:00
mType = TYPE_ACID;
break;
default:
2017-07-05 15:39:41 +00:00
throw fnd::Exception(kModuleName, "ACI header corrupt");
}
if (mType == TYPE_ACI0)
{
mProgramId = hdr->program_id_info.program_id.get();
mIsProduction = false;
mIsUnqualifiedApproval = false;
mAcidSize = 0;
mProgramIdMin = 0;
mProgramIdMax = 0;
}
else if (mType == TYPE_ACID)
{
mProgramId = 0;
mIsProduction = _HAS_BIT(hdr->flags.get(), aci::FLAG_PRODUCTION);
mIsUnqualifiedApproval = _HAS_BIT(hdr->flags.get(), aci::FLAG_UNQUALIFIED_APPROVAL);
mAcidSize = hdr->size.get();
mProgramIdMin = hdr->program_id_info.program_id_restrict.min.get();
mProgramIdMax = hdr->program_id_info.program_id_restrict.max.get();
}
// the header offset is the MIN(sac.offset, fac.offset, kc.offset) - sizeof(sHeader)
mHeaderOffset = MAX(MIN(hdr->sac.offset.get(), MIN(hdr->fac.offset.get(), hdr->kc.offset.get())), align(sizeof(sAciHeader), aci::kAciAlignSize)) - align(sizeof(sAciHeader), aci::kAciAlignSize);
mFac.offset = hdr->fac.offset.get() - mHeaderOffset;
mFac.size = hdr->fac.size.get();
mSac.offset = hdr->sac.offset.get() - mHeaderOffset;
mSac.size = hdr->sac.size.get();
mKc.offset = hdr->kc.offset.get() - mHeaderOffset;
mKc.size = hdr->kc.size.get();
2017-07-05 15:39:41 +00:00
}
2018-06-24 08:18:54 +00:00
const fnd::Vec<byte_t>& AciHeader::getBytes() const
{
return mRawBinary;
}
void nx::AciHeader::clear()
{
2018-06-24 08:18:54 +00:00
mRawBinary.clear();
mHeaderOffset = 0;
mType = TYPE_ACI0;
mProgramId = 0;
mProgramIdMin = 0;
mProgramIdMax = 0;
mAcidSize = 0;
mIsProduction = false;
mIsUnqualifiedApproval = false;
mFac.offset = 0;
mFac.size = 0;
mSac.offset = 0;
mSac.size = 0;
mKc.offset = 0;
mKc.size = 0;
}
size_t nx::AciHeader::getAciSize() const
{
return MAX(MAX(MAX(mSac.offset + mSac.size, mKc.offset + mKc.size), mFac.offset + mFac.size), sizeof(sAciHeader));
}
size_t nx::AciHeader::getAcidSize() const
{
return mAcidSize;
}
/*
void nx::AciHeader::setAcidSize(size_t size)
{
mAcidSize = size;
}
*/
2018-03-22 05:26:22 +00:00
uint64_t nx::AciHeader::getProgramIdMin() const
{
return mProgramIdMin;
}
2018-03-22 05:26:22 +00:00
void nx::AciHeader::setProgramIdMin(uint64_t program_id)
{
mProgramIdMin = program_id;
}
2018-03-22 05:26:22 +00:00
uint64_t nx::AciHeader::getProgramIdMax() const
{
return mProgramIdMax;
}
2018-03-22 05:26:22 +00:00
void nx::AciHeader::setProgramIdMax(uint64_t program_id)
{
mProgramIdMax = program_id;
}
void nx::AciHeader::setHeaderOffset(size_t offset)
{
mHeaderOffset = offset;
}
2017-07-05 15:39:41 +00:00
AciHeader::AciType AciHeader::getAciType() const
{
return mType;
}
void AciHeader::setAciType(AciType type)
{
mType = type;
}
bool nx::AciHeader::isProduction() const
{
return mIsProduction;
}
void nx::AciHeader::setIsProduction(bool isProduction)
{
mIsProduction = isProduction;
}
bool nx::AciHeader::isUnqualifiedApproval() const
{
return mIsUnqualifiedApproval;
}
void nx::AciHeader::setIsUnqualifiedApproval(bool isUnqualifiedApproval)
{
mIsUnqualifiedApproval = isUnqualifiedApproval;
}
2018-03-22 05:26:22 +00:00
uint64_t AciHeader::getProgramId() const
2017-07-05 15:39:41 +00:00
{
return mProgramId;
}
2018-03-22 05:26:22 +00:00
void AciHeader::setProgramId(uint64_t program_id)
2017-07-05 15:39:41 +00:00
{
mProgramId = program_id;
}
const AciHeader::sSection & AciHeader::getFacPos() const
2017-07-05 15:39:41 +00:00
{
return mFac;
}
void AciHeader::setFacSize(size_t size)
2017-07-05 15:39:41 +00:00
{
mFac.size = size;
}
const AciHeader::sSection & AciHeader::getSacPos() const
2017-07-05 15:39:41 +00:00
{
return mSac;
}
void AciHeader::setSacSize(size_t size)
2017-07-05 15:39:41 +00:00
{
mSac.size = size;
}
const AciHeader::sSection & AciHeader::getKcPos() const
2017-07-05 15:39:41 +00:00
{
return mKc;
}
void AciHeader::setKcSize(size_t size)
2017-07-05 15:39:41 +00:00
{
mKc.size = size;
}
2018-06-24 08:18:54 +00:00
void AciHeader::calculateSectionOffsets()
{
mFac.offset = align(mHeaderOffset, aci::kAciAlignSize) + align(sizeof(sAciHeader), aci::kAciAlignSize);
mSac.offset = mFac.offset + align(mFac.size, aci::kAciAlignSize);
mKc.offset = mSac.offset + align(mSac.size, aci::kAciAlignSize);
}