2017-08-05 13:09:50 +00:00
|
|
|
#include <nx/AciHeader.h>
|
2017-07-05 15:39:41 +00:00
|
|
|
|
2017-07-06 11:17:21 +00:00
|
|
|
using namespace nx;
|
|
|
|
|
2017-07-05 15:39:41 +00:00
|
|
|
void AciHeader::calculateSectionOffsets()
|
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
mFac.offset = align(mHeaderOffset, kAciAlignSize) + align(sizeof(sAciHeader), kAciAlignSize);
|
2017-07-05 15:39:41 +00:00
|
|
|
mSac.offset = mFac.offset + align(mFac.size, kAciAlignSize);
|
|
|
|
mKc.offset = mSac.offset + align(mSac.size, kAciAlignSize);
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:56:52 +00:00
|
|
|
bool AciHeader::isEqual(const AciHeader & other) const
|
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
return (mHeaderOffset == other.mHeaderOffset) \
|
|
|
|
&& (mType == other.mType) \
|
2017-07-15 08:28:01 +00:00
|
|
|
&& (mAcidSize == other.mAcidSize) \
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void AciHeader::copyFrom(const AciHeader & other)
|
|
|
|
{
|
|
|
|
if (other.getSize())
|
|
|
|
{
|
|
|
|
importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
mHeaderOffset = other.mHeaderOffset;
|
2017-07-06 10:56:52 +00:00
|
|
|
mType = other.mType;
|
2017-07-15 08:28:01 +00:00
|
|
|
mAcidSize = other.mAcidSize;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
AciHeader::AciHeader()
|
|
|
|
{
|
2017-07-17 08:21:39 +00:00
|
|
|
clear();
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AciHeader::AciHeader(const AciHeader & other)
|
|
|
|
{
|
|
|
|
importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
AciHeader::AciHeader(const byte_t * bytes, size_t len)
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
importBinary(bytes, len);
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:56:52 +00:00
|
|
|
bool AciHeader::operator==(const AciHeader & other) const
|
|
|
|
{
|
|
|
|
return isEqual(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AciHeader::operator!=(const AciHeader & other) const
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2017-07-06 10:56:52 +00:00
|
|
|
return !isEqual(other);
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AciHeader::operator=(const AciHeader & other)
|
|
|
|
{
|
|
|
|
this->importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
const byte_t * AciHeader::getBytes() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
2017-07-06 03:30:27 +00:00
|
|
|
return mBinaryBlob.getBytes();
|
2017-07-05 15:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t AciHeader::getSize() const
|
|
|
|
{
|
2017-07-06 03:30:27 +00:00
|
|
|
return mBinaryBlob.getSize();
|
2017-07-05 15:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AciHeader::exportBinary()
|
|
|
|
{
|
|
|
|
mBinaryBlob.alloc(sizeof(sAciHeader));
|
2017-07-06 03:30:27 +00:00
|
|
|
sAciHeader* hdr = (sAciHeader*)mBinaryBlob.getBytes();
|
2017-07-05 15:39:41 +00:00
|
|
|
|
|
|
|
// set type
|
|
|
|
switch (mType)
|
|
|
|
{
|
|
|
|
case (TYPE_ACI0):
|
|
|
|
hdr->set_signature(kAciStructSig.c_str());
|
|
|
|
break;
|
|
|
|
case (TYPE_ACID):
|
|
|
|
hdr->set_signature(kAciDescStructSig.c_str());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw fnd::Exception(kModuleName, "Unexpected ACI type");
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
// set offset/size
|
|
|
|
calculateSectionOffsets();
|
|
|
|
hdr->fac().set_offset(mFac.offset);
|
|
|
|
hdr->fac().set_size(mFac.size);
|
|
|
|
hdr->sac().set_offset(mSac.offset);
|
|
|
|
hdr->sac().set_size(mSac.size);
|
|
|
|
hdr->kc().set_offset(mKc.offset);
|
|
|
|
hdr->kc().set_size(mKc.size);
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
uint32_t flags = 0;
|
2017-07-21 10:30:16 +00:00
|
|
|
if (mIsProduction)
|
|
|
|
flags |= BIT(0);
|
|
|
|
|
|
|
|
hdr->set_flags(flags);
|
2017-07-17 08:21:39 +00:00
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
if (mType == TYPE_ACI0)
|
|
|
|
{
|
|
|
|
// set program
|
|
|
|
hdr->set_program_id(mProgramId);
|
|
|
|
}
|
|
|
|
else if (mType == TYPE_ACID)
|
|
|
|
{
|
2017-07-21 10:30:16 +00:00
|
|
|
mAcidSize = getAciSize();
|
|
|
|
hdr->set_size(mAcidSize);
|
|
|
|
hdr->set_program_id_min(mProgramIdMin);
|
|
|
|
hdr->set_program_id_max(mProgramIdMax);
|
2017-07-15 08:28:01 +00:00
|
|
|
}
|
2017-07-05 15:39:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:26:22 +00:00
|
|
|
void AciHeader::importBinary(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
|
|
|
|
|
|
|
mBinaryBlob.alloc(sizeof(sAciHeader));
|
2017-07-06 03:30:27 +00:00
|
|
|
memcpy(mBinaryBlob.getBytes(), bytes, sizeof(sAciHeader));
|
2017-07-05 15:39:41 +00:00
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
sAciHeader* hdr = (sAciHeader*)mBinaryBlob.getBytes();
|
2017-07-05 15:39:41 +00:00
|
|
|
|
|
|
|
if (memcmp(hdr->signature(), kAciStructSig.c_str(), 4) == 0)
|
|
|
|
{
|
|
|
|
mType = TYPE_ACI0;
|
|
|
|
}
|
|
|
|
else if (memcmp(hdr->signature(), kAciDescStructSig.c_str(), 4) == 0)
|
|
|
|
{
|
|
|
|
mType = TYPE_ACID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "ACI header corrupt");
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
|
|
|
|
if (mType == TYPE_ACI0)
|
|
|
|
{
|
|
|
|
mProgramId = hdr->program_id();
|
2017-07-21 10:30:16 +00:00
|
|
|
mIsProduction = false;
|
2017-07-15 08:28:01 +00:00
|
|
|
mAcidSize = 0;
|
|
|
|
mProgramIdMin = 0;
|
|
|
|
mProgramIdMax = 0;
|
|
|
|
}
|
|
|
|
else if (mType == TYPE_ACID)
|
|
|
|
{
|
|
|
|
mProgramId = 0;
|
2017-07-21 10:30:16 +00:00
|
|
|
mIsProduction = (hdr->flags() & BIT(0)) == BIT(0);
|
|
|
|
mAcidSize = hdr->size();
|
|
|
|
mProgramIdMin = hdr->program_id_min();
|
|
|
|
mProgramIdMax = hdr->program_id_max();
|
2017-07-15 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
// the header offset is the MIN(sac.offset, fac.offset, kc.offset) - sizeof(sHeader)
|
|
|
|
mHeaderOffset = MAX(MIN(hdr->sac().offset(), MIN(hdr->fac().offset(), hdr->kc().offset())), align(sizeof(sAciHeader), kAciAlignSize)) - align(sizeof(sAciHeader), kAciAlignSize);
|
|
|
|
|
|
|
|
mFac.offset = hdr->fac().offset() - mHeaderOffset;
|
2017-07-05 15:39:41 +00:00
|
|
|
mFac.size = hdr->fac().size();
|
2017-07-17 08:21:39 +00:00
|
|
|
mSac.offset = hdr->sac().offset() - mHeaderOffset;
|
2017-07-05 15:39:41 +00:00
|
|
|
mSac.size = hdr->sac().size();
|
2017-07-17 08:21:39 +00:00
|
|
|
mKc.offset = hdr->kc().offset() - mHeaderOffset;
|
2017-07-05 15:39:41 +00:00
|
|
|
mKc.size = hdr->kc().size();
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::AciHeader::clear()
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2017-07-21 10:30:16 +00:00
|
|
|
mBinaryBlob.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;
|
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
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-05 15:39:41 +00:00
|
|
|
AciHeader::AciType AciHeader::getAciType() const
|
|
|
|
{
|
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AciHeader::setAciType(AciType type)
|
|
|
|
{
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
const AciHeader::sSection & AciHeader::getFacPos() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mFac;
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
void AciHeader::setFacSize(size_t size)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mFac.size = size;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
const AciHeader::sSection & AciHeader::getSacPos() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mSac;
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
void AciHeader::setSacSize(size_t size)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mSac.size = size;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
const AciHeader::sSection & AciHeader::getKcPos() const
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
return mKc;
|
|
|
|
}
|
|
|
|
|
2017-07-17 08:21:39 +00:00
|
|
|
void AciHeader::setKcSize(size_t size)
|
2017-07-05 15:39:41 +00:00
|
|
|
{
|
|
|
|
mKc.size = size;
|
|
|
|
}
|