From 5af8ec72ae435eee512b618fee7ab3ac9f11512e Mon Sep 17 00:00:00 2001 From: jakcron Date: Sat, 15 Jul 2017 18:28:41 +1000 Subject: [PATCH] [nx] Add AciBinary. --- lib/nx/AciBinary.cpp | 156 ++++++++++++++++++++++++++++++++++++++ lib/nx/AciBinary.h | 60 +++++++++++++++ lib/nx/nx.vcxproj | 2 + lib/nx/nx.vcxproj.filters | 6 ++ 4 files changed, 224 insertions(+) create mode 100644 lib/nx/AciBinary.cpp create mode 100644 lib/nx/AciBinary.h diff --git a/lib/nx/AciBinary.cpp b/lib/nx/AciBinary.cpp new file mode 100644 index 0000000..177512f --- /dev/null +++ b/lib/nx/AciBinary.cpp @@ -0,0 +1,156 @@ +#include "AciBinary.h" + + + +nx::AciBinary::AciBinary() +{} + +nx::AciBinary::AciBinary(const AciBinary & other) +{ + copyFrom(other); +} + +nx::AciBinary::AciBinary(const u8 * bytes, size_t len) +{ + importBinary(bytes, len); +} + +bool nx::AciBinary::operator==(const AciBinary & other) const +{ + return isEqual(other); +} + +bool nx::AciBinary::operator!=(const AciBinary & other) const +{ + return !isEqual(other); +} + +void nx::AciBinary::operator=(const AciBinary & other) +{ + copyFrom(other); +} + +const u8 * nx::AciBinary::getBytes() const +{ + return mBinaryBlob.getBytes(); +} + +size_t nx::AciBinary::getSize() const +{ + return mBinaryBlob.getSize(); +} + +void nx::AciBinary::exportBinary() +{ + // export components + mFac.exportBinary(); + mSac.exportBinary(); + mKc.exportBinary(); + + // set sizes + setFacSize(mFac.getSize()); + setSacSize(mSac.getSize()); + setKcSize(mKc.getSize()); + + // export header + AciHeader::exportBinary(); + + // allocate binary + mBinaryBlob.alloc(getAciSize()); + + // copy header + memcpy(mBinaryBlob.getBytes(), AciHeader::getBytes(), AciHeader::getSize()); + + // copy components + memcpy(mBinaryBlob.getBytes() + getFacPos().offset, mFac.getBytes(), mFac.getSize()); + memcpy(mBinaryBlob.getBytes() + getSacPos().offset, mSac.getBytes(), mSac.getSize()); + memcpy(mBinaryBlob.getBytes() + getKcPos().offset, mKc.getBytes(), mKc.getSize()); +} + +void nx::AciBinary::importBinary(const u8 * bytes, size_t len) +{ + AciHeader::importBinary(bytes, len); + + if (getAciSize() > len) + { + throw fnd::Exception(kModuleName, "ACI binary too small"); + } + + mBinaryBlob.alloc(getAciSize()); + memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize()); + + if (getFacPos().size > 0) + { + mFac.importBinary(mBinaryBlob.getBytes() + getFacPos().offset, getFacPos().size); + } + if (getSacPos().size > 0) + { + mSac.importBinary(mBinaryBlob.getBytes() + getSacPos().offset, getSacPos().size); + } + if (getKcPos().size > 0) + { + mKc.importBinary(mBinaryBlob.getBytes() + getKcPos().offset, getKcPos().size); + } + +} + +void nx::AciBinary::clear() +{ + AciHeader::clear(); + mFac.clear(); + mSac.clear(); + mKc.clear(); +} + +const nx::FacBinary & nx::AciBinary::getFac() const +{ + return mFac; +} + +void nx::AciBinary::setFac(const FacBinary & fac) +{ + mFac = fac; +} + +const nx::SacBinary & nx::AciBinary::getSac() const +{ + return mSac; +} + +void nx::AciBinary::setSac(const SacBinary & sac) +{ + mSac = sac; +} + +const nx::KcBinary & nx::AciBinary::getKc() const +{ + return mKc; +} + +void nx::AciBinary::setKc(const KcBinary & kc) +{ + mKc = kc; +} + +bool nx::AciBinary::isEqual(const AciBinary & other) const +{ + return (AciHeader::operator==(other)) \ + && (mFac == other.mFac) \ + && (mSac == other.mSac) \ + && (mKc == other.mKc); +} + +void nx::AciBinary::copyFrom(const AciBinary & other) +{ + if (other.getSize()) + { + importBinary(other.getBytes(), other.getSize()); + } + else + { + AciHeader::operator=(other); + mFac = other.mFac; + mSac = other.mSac; + mKc = other.mKc; + } +} diff --git a/lib/nx/AciBinary.h b/lib/nx/AciBinary.h new file mode 100644 index 0000000..90c4d63 --- /dev/null +++ b/lib/nx/AciBinary.h @@ -0,0 +1,60 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nx +{ + class AciBinary : + public AciHeader + { + public: + AciBinary(); + AciBinary(const AciBinary& other); + AciBinary(const u8* bytes, size_t len); + + bool operator==(const AciBinary& other) const; + bool operator!=(const AciBinary& other) const; + void operator=(const AciBinary& other); + + // to be used after export + const u8* getBytes() const; + size_t getSize() const; + + // export/import binary + virtual void exportBinary(); + virtual void importBinary(const u8* bytes, size_t len); + + // variables + virtual void clear(); + + const FacBinary& getFac() const; + void setFac(const FacBinary& fac); + + const SacBinary& getSac() const; + void setSac(const SacBinary& sac); + + const KcBinary& getKc() const; + void setKc(const KcBinary& kc); + + private: + const std::string kModuleName = "ACI_BINARY"; + + // raw binary + fnd::MemoryBlob mBinaryBlob; + + // variables + FacBinary mFac; + SacBinary mSac; + KcBinary mKc; + + bool isEqual(const AciBinary& other) const; + void copyFrom(const AciBinary& other); + }; +} + diff --git a/lib/nx/nx.vcxproj b/lib/nx/nx.vcxproj index 4436711..3da5155 100644 --- a/lib/nx/nx.vcxproj +++ b/lib/nx/nx.vcxproj @@ -19,6 +19,7 @@ + @@ -49,6 +50,7 @@ + diff --git a/lib/nx/nx.vcxproj.filters b/lib/nx/nx.vcxproj.filters index 8f4d49b..0907bee 100644 --- a/lib/nx/nx.vcxproj.filters +++ b/lib/nx/nx.vcxproj.filters @@ -96,6 +96,9 @@ Header Files + + Header Files + Header Files @@ -173,6 +176,9 @@ Source Files + + Source Files + Source Files