nstool/lib/libhac/source/HierarchicalSha256Header.cpp

112 lines
2.9 KiB
C++
Raw Permalink Normal View History

#include <sstream>
2018-08-07 07:17:51 +00:00
#include <nn/hac/HierarchicalSha256Header.h>
2018-08-07 07:17:51 +00:00
nn::hac::HierarchicalSha256Header::HierarchicalSha256Header()
{
clear();
}
2018-08-07 07:17:51 +00:00
nn::hac::HierarchicalSha256Header::HierarchicalSha256Header(const HierarchicalSha256Header & other)
{
2018-06-24 08:18:54 +00:00
*this = other;
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalSha256Header::operator=(const HierarchicalSha256Header & other)
{
2018-06-24 08:18:54 +00:00
if (other.getBytes().size() != 0)
{
fromBytes(other.getBytes().data(), other.getBytes().size());
}
else
{
mMasterHash = other.mMasterHash;
mHashBlockSize = other.mHashBlockSize;
mLayerInfo = other.mLayerInfo;
}
}
2018-08-07 07:17:51 +00:00
bool nn::hac::HierarchicalSha256Header::operator==(const HierarchicalSha256Header & other) const
{
2018-06-24 08:18:54 +00:00
return (mMasterHash == other.mMasterHash) \
&& (mHashBlockSize == other.mHashBlockSize) \
&& (mLayerInfo == other.mLayerInfo);
}
2018-08-07 07:17:51 +00:00
bool nn::hac::HierarchicalSha256Header::operator!=(const HierarchicalSha256Header & other) const
{
2018-06-24 08:18:54 +00:00
return !(*this == other);
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalSha256Header::toBytes()
{
throw fnd::Exception(kModuleName, "exportBinary() not implemented");
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalSha256Header::fromBytes(const byte_t* data, size_t len)
{
std::stringstream error_str;
2018-08-07 07:17:51 +00:00
if (len < sizeof(nn::hac::sHierarchicalSha256Header))
{
throw fnd::Exception(kModuleName, "Header too small");
}
2018-08-07 07:17:51 +00:00
const nn::hac::sHierarchicalSha256Header* hdr = (const nn::hac::sHierarchicalSha256Header*)data;
2018-08-07 07:17:51 +00:00
if (hdr->layer_num.get() != nn::hac::hierarchicalsha256::kDefaultLayerNum)
{
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::hierarchicalsha256::kDefaultLayerNum << ")";
throw fnd::Exception(kModuleName, error_str.str());
}
mMasterHash = hdr->master_hash;
mHashBlockSize = hdr->hash_block_size.get();
for (size_t i = 0; i < hdr->layer_num.get(); i++)
{
mLayerInfo.addElement({hdr->layer[i].offset.get(), hdr->layer[i].size.get()});
}
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::hac::HierarchicalSha256Header::getBytes() const
2018-06-24 08:18:54 +00:00
{
return mRawBinary;
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalSha256Header::clear()
{
memset(mMasterHash.bytes, 0, sizeof(fnd::sha::sSha256Hash));
mHashBlockSize = 0;
mLayerInfo.clear();
}
const fnd::sha::sSha256Hash & nn::hac::HierarchicalSha256Header::getMasterHash() const
{
return mMasterHash;
}
void nn::hac::HierarchicalSha256Header::setMasterHash(const fnd::sha::sSha256Hash & master_hash)
{
mMasterHash = master_hash;
}
2018-08-07 07:17:51 +00:00
size_t nn::hac::HierarchicalSha256Header::getHashBlockSize() const
{
return mHashBlockSize;
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalSha256Header::setHashBlockSize(size_t hash_block_size)
{
mHashBlockSize = hash_block_size;
}
2018-08-07 07:17:51 +00:00
const fnd::List<nn::hac::HierarchicalSha256Header::sLayer>& nn::hac::HierarchicalSha256Header::getLayerInfo() const
{
return mLayerInfo;
}
2018-08-07 07:17:51 +00:00
void nn::hac::HierarchicalSha256Header::setLayerInfo(const fnd::List<sLayer>& layer_info)
{
mLayerInfo = layer_info;
2018-06-24 08:18:54 +00:00
}