nstool/lib/nx/NcaHeader.cpp

168 lines
3.4 KiB
C++
Raw Normal View History

2017-07-05 08:57:14 +00:00
#include "NcaHeader.h"
#include <fnd/exception.h>
void NcaHeader::exportBinary()
{
mBinaryBlob.alloc(sizeof(sNcaHeader));
sNcaHeader* hdr = (sNcaHeader*)mBinaryBlob.data();
hdr->set_signature(kNcaSig.c_str());
hdr->set_block_size(kDefaultBlockSize);
hdr->set_nca_size(mNcaSize);
hdr->set_program_id(mProgramId);
hdr->set_unk0(mUnk0);
// TODO: properly reconstruct NCA layout? atm in hands of user
for (size_t i = 0; i < mSections.size(); i++)
{
// determine section index
u8 section = mSections.size() - 1 - i;
hdr->section(section).set_start(mSections[i].start_blk);
hdr->section(section).set_end(mSections[i].end_blk);
hdr->section(section).set_key_type(mSections[i].key_type);
hdr->section_hash(section) = mSections[i].hash;
}
for (size_t i = 0; i < kAesKeyNum; i++)
{
hdr->aes_key(i) = mAesKeys[i];
}
2017-07-05 08:57:14 +00:00
}
void NcaHeader::importBinary(const u8 * bytes)
{
clearVariables();
mBinaryBlob.alloc(sizeof(sNcaHeader));
memcpy(mBinaryBlob.data(), bytes, sizeof(sNcaHeader));
2017-07-05 08:57:14 +00:00
sNcaHeader* hdr = (sNcaHeader*)mBinaryBlob.data();
2017-07-05 08:57:14 +00:00
if (memcmp(hdr->signature(), kNcaSig.c_str(), 4) != 0)
{
throw fnd::Exception(kModuleName, "NCA header corrupt");
}
mBlockSize = hdr->block_size();
mNcaSize = hdr->nca_size();
mProgramId = hdr->program_id();
mUnk0 = hdr->unk0();
2017-07-05 08:57:14 +00:00
for (size_t i = 0; i < kSectionNum; i++)
{
// determine section index
u8 section = kSectionNum - 1 - i;
// skip sections that don't exist
if (hdr->section(section).start() == 0 && hdr->section(section).end() == 0) continue;
// add high level struct
mSections.push_back({ hdr->section(section).start(), hdr->section(section).end(), blockNumToSize(hdr->section(section).start()), blockNumToSize(hdr->section(section).end()- hdr->section(section).start()), hdr->section(section).key_type(), hdr->section_hash(section) });
2017-07-05 08:57:14 +00:00
}
for (size_t i = 0; i < kAesKeyNum; i++)
{
mAesKeys.push_back(hdr->aes_key(i));
2017-07-05 08:57:14 +00:00
}
}
u64 NcaHeader::getNcaSize() const
{
return mNcaSize;
2017-07-05 08:57:14 +00:00
}
void NcaHeader::setNcaSize(u64 size)
{
mNcaSize = size;
2017-07-05 08:57:14 +00:00
}
u64 NcaHeader::getProgramId() const
{
return mProgramId;
2017-07-05 08:57:14 +00:00
}
void NcaHeader::setProgramId(u64 program_id)
{
mProgramId = program_id;
2017-07-05 08:57:14 +00:00
}
u32 NcaHeader::getUnk() const
{
return mUnk0;
2017-07-05 08:57:14 +00:00
}
const std::vector<NcaHeader::sSection>& NcaHeader::getSections() const
{
return mSections;
2017-07-05 08:57:14 +00:00
}
void NcaHeader::addSection(const sSection & section)
{
if (mSections.size() >= kSectionNum)
2017-07-05 08:57:14 +00:00
{
throw fnd::Exception(kModuleName, "Too many NCA sections");
}
mSections.push_back(section);
2017-07-05 08:57:14 +00:00
}
const std::vector<crypto::aes::sAes128Key>& NcaHeader::getAesKeys() const
{
return mAesKeys;
2017-07-05 08:57:14 +00:00
}
void NcaHeader::addKey(const crypto::aes::sAes128Key & key)
{
if (mAesKeys.size() >= kAesKeyNum)
2017-07-05 08:57:14 +00:00
{
throw fnd::Exception(kModuleName, "Too many NCA aes keys");
}
mAesKeys.push_back(key);
2017-07-05 08:57:14 +00:00
}
void NcaHeader::clearVariables()
{
mBlockSize = 0;
mNcaSize = 0;
mProgramId = 0;
mUnk0 = 0;
mSections.clear();
mAesKeys.clear();
2017-07-05 08:57:14 +00:00
}
u64 NcaHeader::blockNumToSize(u32 block_num) const
{
return block_num*mBlockSize;
2017-07-05 08:57:14 +00:00
}
u32 NcaHeader::sizeToBlockNum(u64 real_size) const
{
return align(real_size, mBlockSize)/mBlockSize;
2017-07-05 08:57:14 +00:00
}
NcaHeader::NcaHeader()
{
clearVariables();
}
NcaHeader::NcaHeader(const NcaHeader & other)
{
importBinary(other.getBytes());
}
NcaHeader::NcaHeader(const u8 * bytes)
{
importBinary(bytes);
}
const u8 * NcaHeader::getBytes() const
{
return mBinaryBlob.data();
2017-07-05 08:57:14 +00:00
}
size_t NcaHeader::getSize() const
{
return mBinaryBlob.size();
2017-07-05 08:57:14 +00:00
}