nstool/lib/libhac/source/HierarchicalIntegrityHeader.cpp

137 lines
4.1 KiB
C++
Raw Permalink Normal View History

#include <sstream>
2018-08-07 07:17:51 +00:00
#include <nn/hac/HierarchicalIntegrityHeader.h>
2018-08-07 07:17:51 +00:00
nn::hac::HierarchicalIntegrityHeader::HierarchicalIntegrityHeader()
{
clear();
}
2018-08-07 07:17:51 +00:00
nn::hac::HierarchicalIntegrityHeader::HierarchicalIntegrityHeader(const HierarchicalIntegrityHeader & other)
{
2018-06-24 08:18:54 +00:00
*this = other;
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalIntegrityHeader::operator=(const HierarchicalIntegrityHeader & other)
{
2018-06-24 08:18:54 +00:00
if (other.getBytes().size() != 0)
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
clear();
mLayerInfo = other.mLayerInfo;
mMasterHashList = other.mMasterHashList;
}
}
2018-08-07 07:17:51 +00:00
bool nn::hac::HierarchicalIntegrityHeader::operator==(const HierarchicalIntegrityHeader & other) const
{
2018-06-24 08:18:54 +00:00
return (mLayerInfo == other.mLayerInfo) \
&& (mMasterHashList == other.mMasterHashList);
}
2018-08-07 07:17:51 +00:00
bool nn::hac::HierarchicalIntegrityHeader::operator!=(const HierarchicalIntegrityHeader & other) const
{
2018-06-24 08:18:54 +00:00
return !(*this == other);
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalIntegrityHeader::toBytes()
{
throw fnd::Exception(kModuleName, "exportBinary() not implemented");
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalIntegrityHeader::fromBytes(const byte_t* data, size_t len)
{
std::stringstream error_str;
// validate size for at least header
2018-08-07 07:17:51 +00:00
if (len < sizeof(nn::hac::sHierarchicalIntegrityHeader))
{
throw fnd::Exception(kModuleName, "Header too small");
}
2018-08-07 07:17:51 +00:00
const nn::hac::sHierarchicalIntegrityHeader* hdr = (const nn::hac::sHierarchicalIntegrityHeader*)data;
// Validate Header Sig "IVFC"
if (hdr->st_magic.get() != hierarchicalintegrity::kStructMagic)
{
throw fnd::Exception(kModuleName, "Invalid struct magic");
}
// Validate TypeId
2018-08-07 07:17:51 +00:00
if (hdr->type_id.get() != nn::hac::hierarchicalintegrity::kRomfsTypeId)
{
error_str.clear();
error_str << "Unsupported type id (" << std::hex << hdr->type_id.get() << ")";
throw fnd::Exception(kModuleName, error_str.str());
}
// Validate Layer Num
if (hdr->layer_num.get() != hierarchicalintegrity::kDefaultLayerNum+1)
{
error_str.clear();
error_str << "Invalid layer count. ";
2018-08-07 07:17:51 +00:00
error_str << "(actual=" << std::dec << hdr->layer_num.get() << ", expected=" << nn::hac::hierarchicalintegrity::kDefaultLayerNum+1 << ")";
throw fnd::Exception(kModuleName, error_str.str());
}
// Get Sizes/Offsets
2018-08-07 07:17:51 +00:00
size_t master_hash_offset = align((sizeof(nn::hac::sHierarchicalIntegrityHeader) + sizeof(nn::hac::sHierarchicalIntegrityLayerInfo) * hdr->layer_num.get()), nn::hac::hierarchicalintegrity::kHeaderAlignLen);
size_t total_size = master_hash_offset + hdr->master_hash_size.get();
// Validate total size
if (len < total_size)
{
throw fnd::Exception(kModuleName, "Header too small");
}
// copy to internal storage
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(total_size);
memcpy(mRawBinary.data(), data, mRawBinary.size());
// save layer info
2018-08-07 07:17:51 +00:00
const nn::hac::sHierarchicalIntegrityLayerInfo* layer_info = (const nn::hac::sHierarchicalIntegrityLayerInfo*)(mRawBinary.data() + sizeof(nn::hac::sHierarchicalIntegrityHeader));
for (size_t i = 0; i < hierarchicalintegrity::kDefaultLayerNum; i++)
{
mLayerInfo.addElement({layer_info[i].offset.get(), layer_info[i].size.get(), layer_info[i].block_size.get()});
}
// save hash list
const fnd::sha::sSha256Hash* hash_list = (const fnd::sha::sSha256Hash*)(mRawBinary.data() + master_hash_offset);
for (size_t i = 0; i < hdr->master_hash_size.get()/sizeof(fnd::sha::sSha256Hash); i++)
{
mMasterHashList.addElement(hash_list[i]);
}
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::hac::HierarchicalIntegrityHeader::getBytes() const
2018-06-24 08:18:54 +00:00
{
return mRawBinary;
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalIntegrityHeader::clear()
{
mLayerInfo.clear();
mMasterHashList.clear();
}
2018-08-07 07:17:51 +00:00
const fnd::List<nn::hac::HierarchicalIntegrityHeader::sLayer>& nn::hac::HierarchicalIntegrityHeader::getLayerInfo() const
{
return mLayerInfo;
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalIntegrityHeader::setLayerInfo(const fnd::List<sLayer>& layer_info)
{
mLayerInfo = layer_info;
}
const fnd::List<fnd::sha::sSha256Hash>& nn::hac::HierarchicalIntegrityHeader::getMasterHashList() const
{
return mMasterHashList;
}
void nn::hac::HierarchicalIntegrityHeader::setMasterHashList(const fnd::List<fnd::sha::sSha256Hash>& master_hash_list)
{
mMasterHashList = master_hash_list;
2018-06-24 08:18:54 +00:00
}