2018-05-20 01:19:41 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <nx/HierarchicalSha256Header.h>
|
|
|
|
|
|
|
|
nx::HierarchicalSha256Header::HierarchicalSha256Header()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nx::HierarchicalSha256Header::HierarchicalSha256Header(const HierarchicalSha256Header & other)
|
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
*this = other;
|
2018-05-20 01:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 08:18:54 +00:00
|
|
|
void nx::HierarchicalSha256Header::operator=(const HierarchicalSha256Header & other)
|
2018-05-20 01:19:41 +00:00
|
|
|
{
|
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-05-20 01:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nx::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-05-20 01:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nx::HierarchicalSha256Header::operator!=(const HierarchicalSha256Header & other) const
|
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
return !(*this == other);
|
2018-05-20 01:19:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 08:18:54 +00:00
|
|
|
void nx::HierarchicalSha256Header::toBytes()
|
2018-05-20 01:19:41 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "exportBinary() not implemented");
|
|
|
|
}
|
|
|
|
|
2018-06-24 08:18:54 +00:00
|
|
|
void nx::HierarchicalSha256Header::fromBytes(const byte_t* data, size_t len)
|
2018-05-20 01:19:41 +00:00
|
|
|
{
|
2018-05-20 13:57:38 +00:00
|
|
|
std::stringstream error_str;
|
|
|
|
|
2018-05-20 01:19:41 +00:00
|
|
|
if (len < sizeof(nx::sHierarchicalSha256Header))
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Header too small");
|
|
|
|
}
|
|
|
|
|
2018-06-24 08:18:54 +00:00
|
|
|
const nx::sHierarchicalSha256Header* hdr = (const nx::sHierarchicalSha256Header*)data;
|
2018-05-20 01:19:41 +00:00
|
|
|
|
2018-05-20 13:57:38 +00:00
|
|
|
if (hdr->layer_num.get() != nx::hierarchicalsha256::kDefaultLayerNum)
|
2018-05-20 01:19:41 +00:00
|
|
|
{
|
2018-05-20 13:57:38 +00:00
|
|
|
error_str.clear();
|
|
|
|
error_str << "Invalid layer count. ";
|
|
|
|
error_str << "(actual=" << std::dec << hdr->layer_num.get() << ", expected=" << nx::hierarchicalsha256::kDefaultLayerNum << ")";
|
|
|
|
throw fnd::Exception(kModuleName, error_str.str());
|
2018-05-20 01:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-24 08:18:54 +00:00
|
|
|
const fnd::Vec<byte_t>& nx::HierarchicalSha256Header::getBytes() const
|
|
|
|
{
|
|
|
|
return mRawBinary;
|
|
|
|
}
|
|
|
|
|
2018-05-20 01:19:41 +00:00
|
|
|
void nx::HierarchicalSha256Header::clear()
|
|
|
|
{
|
|
|
|
memset(mMasterHash.bytes, 0, sizeof(crypto::sha::sSha256Hash));
|
|
|
|
mHashBlockSize = 0;
|
|
|
|
mLayerInfo.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const crypto::sha::sSha256Hash & nx::HierarchicalSha256Header::getMasterHash() const
|
|
|
|
{
|
|
|
|
return mMasterHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::HierarchicalSha256Header::setMasterHash(const crypto::sha::sSha256Hash & master_hash)
|
|
|
|
{
|
|
|
|
mMasterHash = master_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nx::HierarchicalSha256Header::getHashBlockSize() const
|
|
|
|
{
|
|
|
|
return mHashBlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::HierarchicalSha256Header::setHashBlockSize(size_t hash_block_size)
|
|
|
|
{
|
|
|
|
mHashBlockSize = hash_block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fnd::List<nx::HierarchicalSha256Header::sLayer>& nx::HierarchicalSha256Header::getLayerInfo() const
|
|
|
|
{
|
|
|
|
return mLayerInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nx::HierarchicalSha256Header::setLayerInfo(const fnd::List<sLayer>& layer_info)
|
|
|
|
{
|
|
|
|
mLayerInfo = layer_info;
|
2018-06-24 08:18:54 +00:00
|
|
|
}
|