[nstool] Add HashTreeMeta.

This commit is contained in:
jakcron 2018-05-21 20:49:46 +08:00
parent e4b86ab566
commit 1f3865c979
2 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,124 @@
#include "HashTreeMeta.h"
HashTreeMeta::HashTreeMeta() :
mLayerInfo(),
mDataLayer(),
mMasterHashList()
{
}
HashTreeMeta::HashTreeMeta(const nx::HierarchicalIntegrityHeader& hdr) :
mLayerInfo(),
mDataLayer(),
mMasterHashList()
{
importHierarchicalIntergityHeader(hdr);
}
HashTreeMeta::HashTreeMeta(const nx::HierarchicalSha256Header& hdr) :
mLayerInfo(),
mDataLayer(),
mMasterHashList()
{
importHierarchicalSha256Header(hdr);
}
bool HashTreeMeta::operator==(const HashTreeMeta& other) const
{
return isEqual(other);
}
bool HashTreeMeta::operator!=(const HashTreeMeta& other) const
{
return !isEqual(other);
}
void HashTreeMeta::operator=(const HashTreeMeta& other)
{
copyFrom(other);
}
void HashTreeMeta::importHierarchicalIntergityHeader(const nx::HierarchicalIntegrityHeader& hdr)
{
for (size_t i = 0; i < hdr.getLayerInfo().getSize(); i++)
{
sLayer layer;
layer.offset = hdr.getLayerInfo()[i].offset;
layer.size = hdr.getLayerInfo()[i].size;
layer.block_size = _BIT(hdr.getLayerInfo()[i].block_size);
if (i+1 == hdr.getLayerInfo().getSize())
{
mDataLayer = layer;
}
else
{
mLayerInfo.addElement(layer);
}
}
mMasterHashList = hdr.getMasterHashList();
}
void HashTreeMeta::importHierarchicalSha256Header(const nx::HierarchicalSha256Header& hdr)
{
for (size_t i = 0; i < hdr.getLayerInfo().getSize(); i++)
{
sLayer layer;
layer.offset = hdr.getLayerInfo()[i].offset;
layer.size = hdr.getLayerInfo()[i].size;
layer.block_size = hdr.getHashBlockSize();
if (i+1 == hdr.getLayerInfo().getSize())
{
mDataLayer = layer;
}
else
{
mLayerInfo.addElement(layer);
}
}
mMasterHashList.addElement(hdr.getMasterHash());
}
const fnd::List<HashTreeMeta::sLayer>& HashTreeMeta::getHashLayerInfo() const
{
return mLayerInfo;
}
void HashTreeMeta::setHashLayerInfo(const fnd::List<sLayer>& layer_info)
{
mLayerInfo = layer_info;
}
const HashTreeMeta::sLayer& HashTreeMeta::getDataLayer() const
{
return mDataLayer;
}
void HashTreeMeta::setDataLayer(const sLayer& data_info)
{
mDataLayer = data_info;
}
const fnd::List<crypto::sha::sSha256Hash>& HashTreeMeta::getMasterHashList() const
{
return mMasterHashList;
}
void HashTreeMeta::setMasterHashList(const fnd::List<crypto::sha::sSha256Hash>& master_hash_list)
{
mMasterHashList = master_hash_list;
}
bool HashTreeMeta::isEqual(const HashTreeMeta& other) const
{
return (mLayerInfo == other.mLayerInfo) \
&& (mDataLayer == other.mDataLayer) \
&& (mMasterHashList == other.mMasterHashList);
}
void HashTreeMeta::copyFrom(const HashTreeMeta& other)
{
mLayerInfo = other.mLayerInfo;
mDataLayer = other.mDataLayer;
mMasterHashList = other.mMasterHashList;
}

View file

@ -0,0 +1,60 @@
#pragma once
#include <nx/HierarchicalIntegrityHeader.h>
#include <nx/HierarchicalSha256Header.h>
class HashTreeMeta
{
public:
struct sLayer
{
size_t offset;
size_t size;
size_t block_size;
void operator=(const sLayer& other)
{
offset = other.offset;
size = other.size;
block_size = other.block_size;
}
bool operator==(const sLayer& other) const
{
return (offset == other.offset && size == other.size && block_size == other.block_size);
}
bool operator!=(const sLayer& other) const
{
return !(*this == other);
}
};
HashTreeMeta();
HashTreeMeta(const nx::HierarchicalIntegrityHeader& hdr);
HashTreeMeta(const nx::HierarchicalSha256Header& hdr);
bool operator==(const HashTreeMeta& other) const;
bool operator!=(const HashTreeMeta& other) const;
void operator=(const HashTreeMeta& other);
void importHierarchicalIntergityHeader(const nx::HierarchicalIntegrityHeader& hdr);
void importHierarchicalSha256Header(const nx::HierarchicalSha256Header& hdr);
const fnd::List<sLayer>& getHashLayerInfo() const;
void setHashLayerInfo(const fnd::List<sLayer>& layer_info);
const sLayer& getDataLayer() const;
void setDataLayer(const sLayer& data_info);
const fnd::List<crypto::sha::sSha256Hash>& getMasterHashList() const;
void setMasterHashList(const fnd::List<crypto::sha::sSha256Hash>& master_hash_list);
private:
// data
fnd::List<sLayer> mLayerInfo;
sLayer mDataLayer;
fnd::List<crypto::sha::sSha256Hash> mMasterHashList;
bool isEqual(const HashTreeMeta& other) const;
void copyFrom(const HashTreeMeta& other);
};