2017-08-05 13:09:50 +00:00
|
|
|
#include <nx/AciHeader.h>
|
2017-07-05 15:39:41 +00:00
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
nx::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 15:01:16 +00:00
|
|
|
nx::AciHeader::AciHeader(const AciHeader & other)
|
2017-07-06 10:56:52 +00:00
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
*this = other;
|
2017-07-06 10:56:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::operator=(const AciHeader & other)
|
2017-07-06 10:56:52 +00:00
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
if (other.getBytes().size())
|
2017-07-06 10:56:52 +00:00
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
fromBytes(other.getBytes().data(), other.getBytes().size());
|
2017-07-06 10:56:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
mHeaderOffset = other.mHeaderOffset;
|
2017-07-06 10:56:52 +00:00
|
|
|
mType = other.mType;
|
2018-04-27 16:13:35 +00:00
|
|
|
mIsProduction = other.mIsProduction;
|
|
|
|
mIsUnqualifiedApproval = other.mIsUnqualifiedApproval;
|
2017-07-15 08:28:01 +00:00
|
|
|
mAcidSize = other.mAcidSize;
|
2018-04-27 16:13:35 +00:00
|
|
|
mProgramIdMin = other.mProgramIdMin;
|
|
|
|
mProgramIdMax = other.mProgramIdMax;
|
2017-07-06 10:56:52 +00:00
|
|
|
mProgramId = other.mProgramId;
|
2017-07-06 23:56:49 +00:00
|
|
|
mFac = other.mFac;
|
|
|
|
mSac = other.mSac;
|
|
|
|
mKc = other.mKc;
|
2017-07-06 10:56:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
bool nx::AciHeader::operator==(const AciHeader & other) const
|
2017-07-06 10:56:52 +00:00
|
|
|
{
|
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);
|
2017-07-06 10:56:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
bool nx::AciHeader::operator!=(const AciHeader & other) const
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
return !(*this == other);
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::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):
|
2018-06-26 04:23:40 +00:00
|
|
|
hdr->st_magic = aci::kAciStructMagic;
|
2017-07-05 15:39:41 +00:00
|
|
|
break;
|
|
|
|
case (TYPE_ACID):
|
2018-06-26 04:23:40 +00:00
|
|
|
hdr->st_magic = aci::kAciDescStructMagic;
|
2017-07-05 15:39:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw fnd::Exception(kModuleName, "Unexpected ACI type");
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
// set offset/size
|
|
|
|
calculateSectionOffsets();
|
2018-05-26 04:07:42 +00:00
|
|
|
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;
|
2017-07-17 08:21:39 +00:00
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
uint32_t flags = 0;
|
2017-07-21 10:30:16 +00:00
|
|
|
if (mIsProduction)
|
2018-04-27 16:13:35 +00:00
|
|
|
flags |= _BIT(aci::FLAG_PRODUCTION);
|
2018-04-29 03:22:43 +00:00
|
|
|
if (mIsUnqualifiedApproval)
|
2018-04-27 16:13:35 +00:00
|
|
|
flags |= _BIT(aci::FLAG_UNQUALIFIED_APPROVAL);
|
2017-07-21 10:30:16 +00:00
|
|
|
|
2018-04-27 16:13:35 +00:00
|
|
|
hdr->flags = flags;
|
2017-07-17 08:21:39 +00:00
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
if (mType == TYPE_ACI0)
|
|
|
|
{
|
|
|
|
// set program
|
2018-04-27 16:13:35 +00:00
|
|
|
hdr->program_id_info.program_id = mProgramId;
|
2017-07-15 08:28:01 +00:00
|
|
|
}
|
|
|
|
else if (mType == TYPE_ACID)
|
|
|
|
{
|
2017-07-21 10:30:16 +00:00
|
|
|
mAcidSize = getAciSize();
|
2018-05-26 04:07:42 +00:00
|
|
|
hdr->size = (uint32_t)mAcidSize;
|
2018-04-27 16:13:35 +00:00
|
|
|
hdr->program_id_info.program_id_restrict.min = mProgramIdMin;
|
|
|
|
hdr->program_id_info.program_id_restrict.max = mProgramIdMax;
|
2017-07-15 08:28:01 +00:00
|
|
|
}
|
2017-07-05 15:39:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::fromBytes(const byte_t * bytes, size_t len)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
if (len < sizeof(sAciHeader))
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "ACI header too small");
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
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
|
|
|
|
2018-06-26 04:23:40 +00:00
|
|
|
switch (hdr->st_magic.get())
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
2018-06-26 04:23:40 +00:00
|
|
|
case (aci::kAciStructMagic):
|
2017-07-05 15:39:41 +00:00
|
|
|
mType = TYPE_ACI0;
|
2018-06-03 07:41:56 +00:00
|
|
|
break;
|
2018-06-26 04:23:40 +00:00
|
|
|
case (aci::kAciDescStructMagic):
|
2017-07-05 15:39:41 +00:00
|
|
|
mType = TYPE_ACID;
|
2018-06-03 07:41:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-07-05 15:39:41 +00:00
|
|
|
throw fnd::Exception(kModuleName, "ACI header corrupt");
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
|
|
|
|
if (mType == TYPE_ACI0)
|
|
|
|
{
|
2018-04-27 16:13:35 +00:00
|
|
|
mProgramId = hdr->program_id_info.program_id.get();
|
2017-07-21 10:30:16 +00:00
|
|
|
mIsProduction = false;
|
2018-04-27 16:13:35 +00:00
|
|
|
mIsUnqualifiedApproval = false;
|
2017-07-15 08:28:01 +00:00
|
|
|
mAcidSize = 0;
|
|
|
|
mProgramIdMin = 0;
|
|
|
|
mProgramIdMax = 0;
|
|
|
|
}
|
|
|
|
else if (mType == TYPE_ACID)
|
|
|
|
{
|
|
|
|
mProgramId = 0;
|
2018-04-27 16:13:35 +00:00
|
|
|
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();
|
2017-07-15 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
// 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);
|
2017-07-17 08:21:39 +00:00
|
|
|
|
2018-04-27 16:13:35 +00:00
|
|
|
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 15:01:16 +00:00
|
|
|
const fnd::Vec<byte_t>& nx::AciHeader::getBytes() const
|
2018-06-24 08:18:54 +00:00
|
|
|
{
|
|
|
|
return mRawBinary;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::AciHeader::clear()
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
mRawBinary.clear();
|
2017-07-17 08:21:39 +00:00
|
|
|
mHeaderOffset = 0;
|
|
|
|
mType = TYPE_ACI0;
|
|
|
|
mProgramId = 0;
|
2017-07-21 10:30:16 +00:00
|
|
|
mProgramIdMin = 0;
|
|
|
|
mProgramIdMax = 0;
|
|
|
|
mAcidSize = 0;
|
|
|
|
mIsProduction = false;
|
2018-04-27 16:13:35 +00:00
|
|
|
mIsUnqualifiedApproval = false;
|
2017-07-17 08:21:39 +00:00
|
|
|
mFac.offset = 0;
|
|
|
|
mFac.size = 0;
|
|
|
|
mSac.offset = 0;
|
|
|
|
mSac.size = 0;
|
|
|
|
mKc.offset = 0;
|
|
|
|
mKc.size = 0;
|
2017-07-15 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t nx::AciHeader::getAciSize() const
|
|
|
|
{
|
2018-06-24 15:01:16 +00:00
|
|
|
return _MAX(_MAX(_MAX(mSac.offset + mSac.size, mKc.offset + mKc.size), mFac.offset + mFac.size), sizeof(sAciHeader));
|
2017-07-15 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t nx::AciHeader::getAcidSize() const
|
|
|
|
{
|
|
|
|
return mAcidSize;
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
/*
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::AciHeader::setAcidSize(size_t size)
|
|
|
|
{
|
|
|
|
mAcidSize = size;
|
|
|
|
}
|
2017-07-17 08:21:39 +00:00
|
|
|
*/
|
2017-07-15 08:28:01 +00:00
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
uint64_t nx::AciHeader::getProgramIdMin() const
|
2017-07-15 08:28:01 +00:00
|
|
|
{
|
|
|
|
return mProgramIdMin;
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
void nx::AciHeader::setProgramIdMin(uint64_t program_id)
|
2017-07-15 08:28:01 +00:00
|
|
|
{
|
|
|
|
mProgramIdMin = program_id;
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
uint64_t nx::AciHeader::getProgramIdMax() const
|
2017-07-15 08:28:01 +00:00
|
|
|
{
|
|
|
|
return mProgramIdMax;
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
void nx::AciHeader::setProgramIdMax(uint64_t program_id)
|
2017-07-15 08:28:01 +00:00
|
|
|
{
|
|
|
|
mProgramIdMax = program_id;
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
void nx::AciHeader::setHeaderOffset(size_t offset)
|
|
|
|
{
|
|
|
|
mHeaderOffset = offset;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
nx::AciHeader::AciType nx::AciHeader::getAciType() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::setAciType(AciType type)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mType = type;
|
|
|
|
}
|
|
|
|
|
2017-07-21 10:30:16 +00:00
|
|
|
bool nx::AciHeader::isProduction() const
|
|
|
|
{
|
|
|
|
return mIsProduction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::AciHeader::setIsProduction(bool isProduction)
|
|
|
|
{
|
|
|
|
mIsProduction = isProduction;
|
|
|
|
}
|
|
|
|
|
2018-04-27 16:13:35 +00:00
|
|
|
bool nx::AciHeader::isUnqualifiedApproval() const
|
|
|
|
{
|
|
|
|
return mIsUnqualifiedApproval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::AciHeader::setIsUnqualifiedApproval(bool isUnqualifiedApproval)
|
|
|
|
{
|
|
|
|
mIsUnqualifiedApproval = isUnqualifiedApproval;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
uint64_t nx::AciHeader::getProgramId() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mProgramId;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::setProgramId(uint64_t program_id)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mProgramId = program_id;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
const nx::AciHeader::sSection & nx::AciHeader::getFacPos() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mFac;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::setFacSize(size_t size)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mFac.size = size;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
const nx::AciHeader::sSection & nx::AciHeader::getSacPos() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mSac;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::setSacSize(size_t size)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mSac.size = size;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
const nx::AciHeader::sSection & nx::AciHeader::getKcPos() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mKc;
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::setKcSize(size_t size)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mKc.size = size;
|
|
|
|
}
|
2018-06-24 08:18:54 +00:00
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
void nx::AciHeader::calculateSectionOffsets()
|
2018-06-24 08:18:54 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|