From 65b39483dc83a5832acabb9d54a2946d7e7ca39a Mon Sep 17 00:00:00 2001 From: jakcron Date: Fri, 29 Jun 2018 12:00:15 +0800 Subject: [PATCH] [ns|nxtool] Update SacBinary/SacEntry --- .../include/nx/AccessControlInfoBinary.h | 8 +- .../include/nx/AccessControlInfoDescBinary.h | 8 +- lib/libnx/include/nx/SacBinary.h | 40 --------- .../include/nx/ServiceAccessControlBinary.h | 40 +++++++++ ...SacEntry.h => ServiceAccessControlEntry.h} | 22 ++--- lib/libnx/source/AccessControlInfoBinary.cpp | 4 +- .../source/AccessControlInfoDescBinary.cpp | 4 +- lib/libnx/source/SacBinary.cpp | 85 ------------------- .../source/ServiceAccessControlBinary.cpp | 78 +++++++++++++++++ ...ntry.cpp => ServiceAccessControlEntry.cpp} | 32 +++---- programs/nstool/source/NpdmProcess.cpp | 4 +- programs/nstool/source/NpdmProcess.h | 2 +- 12 files changed, 160 insertions(+), 167 deletions(-) delete mode 100644 lib/libnx/include/nx/SacBinary.h create mode 100644 lib/libnx/include/nx/ServiceAccessControlBinary.h rename lib/libnx/include/nx/{SacEntry.h => ServiceAccessControlEntry.h} (52%) delete mode 100644 lib/libnx/source/SacBinary.cpp create mode 100644 lib/libnx/source/ServiceAccessControlBinary.cpp rename lib/libnx/source/{SacEntry.cpp => ServiceAccessControlEntry.cpp} (62%) diff --git a/lib/libnx/include/nx/AccessControlInfoBinary.h b/lib/libnx/include/nx/AccessControlInfoBinary.h index 9743374..ea9596e 100644 --- a/lib/libnx/include/nx/AccessControlInfoBinary.h +++ b/lib/libnx/include/nx/AccessControlInfoBinary.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include namespace nx @@ -33,8 +33,8 @@ namespace nx const nx::FileSystemAccessControlBinary& getFileSystemAccessControl() const; void setFileSystemAccessControl(const FileSystemAccessControlBinary& fac); - const nx::SacBinary& getServiceAccessControl() const; - void setServiceAccessControl(const SacBinary& sac); + const nx::ServiceAccessControlBinary& getServiceAccessControl() const; + void setServiceAccessControl(const ServiceAccessControlBinary& sac); const nx::KcBinary& getKernelCapabilities() const; void setKernelCapabilities(const KcBinary& kc); @@ -47,7 +47,7 @@ namespace nx // variables uint64_t mProgramId; nx::FileSystemAccessControlBinary mFileSystemAccessControl; - nx::SacBinary mServiceAccessControl; + nx::ServiceAccessControlBinary mServiceAccessControl; nx::KcBinary mKernelCapabilities; }; } \ No newline at end of file diff --git a/lib/libnx/include/nx/AccessControlInfoDescBinary.h b/lib/libnx/include/nx/AccessControlInfoDescBinary.h index dc72356..de4fbf0 100644 --- a/lib/libnx/include/nx/AccessControlInfoDescBinary.h +++ b/lib/libnx/include/nx/AccessControlInfoDescBinary.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include namespace nx @@ -66,8 +66,8 @@ namespace nx const nx::FileSystemAccessControlBinary& getFileSystemAccessControl() const; void setFileSystemAccessControl(const FileSystemAccessControlBinary& fac); - const nx::SacBinary& getServiceAccessControl() const; - void setServiceAccessControl(const SacBinary& sac); + const nx::ServiceAccessControlBinary& getServiceAccessControl() const; + void setServiceAccessControl(const ServiceAccessControlBinary& sac); const nx::KcBinary& getKernelCapabilities() const; void setKernelCapabilities(const KcBinary& kc); @@ -82,7 +82,7 @@ namespace nx fnd::List mFlags; sProgramIdRestrict mProgramIdRestrict; nx::FileSystemAccessControlBinary mFileSystemAccessControl; - nx::SacBinary mServiceAccessControl; + nx::ServiceAccessControlBinary mServiceAccessControl; nx::KcBinary mKernelCapabilities; }; } \ No newline at end of file diff --git a/lib/libnx/include/nx/SacBinary.h b/lib/libnx/include/nx/SacBinary.h deleted file mode 100644 index 77d025c..0000000 --- a/lib/libnx/include/nx/SacBinary.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -namespace nx -{ - class SacBinary : - public fnd::ISerialisable - { - public: - SacBinary(); - SacBinary(const SacBinary& other); - - void operator=(const SacBinary& other); - bool operator==(const SacBinary& other) const; - bool operator!=(const SacBinary& other) const; - - // export/import binary - void toBytes(); - void fromBytes(const byte_t* bytes, size_t len); - const fnd::Vec& getBytes() const; - - // variables - void clear(); - const fnd::List& getServiceList() const; - void addService(const SacEntry& service); - private: - const std::string kModuleName = "SAC_BINARY"; - - // raw binary - fnd::Vec mRawBinary; - - // variables - fnd::List mServices; - }; - -} diff --git a/lib/libnx/include/nx/ServiceAccessControlBinary.h b/lib/libnx/include/nx/ServiceAccessControlBinary.h new file mode 100644 index 0000000..3066e5f --- /dev/null +++ b/lib/libnx/include/nx/ServiceAccessControlBinary.h @@ -0,0 +1,40 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace nx +{ + class ServiceAccessControlBinary : + public fnd::ISerialisable + { + public: + ServiceAccessControlBinary(); + ServiceAccessControlBinary(const ServiceAccessControlBinary& other); + + void operator=(const ServiceAccessControlBinary& other); + bool operator==(const ServiceAccessControlBinary& other) const; + bool operator!=(const ServiceAccessControlBinary& other) const; + + // export/import binary + void toBytes(); + void fromBytes(const byte_t* bytes, size_t len); + const fnd::Vec& getBytes() const; + + // variables + void clear(); + const fnd::List& getServiceList() const; + void addService(const ServiceAccessControlEntry& service); + private: + const std::string kModuleName = "SERVICE_ACCESS_CONTROL_BINARY"; + + // raw binary + fnd::Vec mRawBinary; + + // variables + fnd::List mServices; + }; + +} diff --git a/lib/libnx/include/nx/SacEntry.h b/lib/libnx/include/nx/ServiceAccessControlEntry.h similarity index 52% rename from lib/libnx/include/nx/SacEntry.h rename to lib/libnx/include/nx/ServiceAccessControlEntry.h index 7e1d4b1..71a6587 100644 --- a/lib/libnx/include/nx/SacEntry.h +++ b/lib/libnx/include/nx/ServiceAccessControlEntry.h @@ -5,17 +5,17 @@ namespace nx { - class SacEntry : + class ServiceAccessControlEntry : public fnd::ISerialisable { public: - SacEntry(); - SacEntry(const std::string& name, bool isServer); - SacEntry(const SacEntry& other); + ServiceAccessControlEntry(); + ServiceAccessControlEntry(const std::string& name, bool isServer); + ServiceAccessControlEntry(const ServiceAccessControlEntry& other); - void operator=(const SacEntry& other); - bool operator==(const SacEntry& other) const; - bool operator!=(const SacEntry& other) const; + void operator=(const ServiceAccessControlEntry& other); + bool operator==(const ServiceAccessControlEntry& other) const; + bool operator!=(const ServiceAccessControlEntry& other) const; // export/import binary void toBytes(); @@ -29,10 +29,10 @@ namespace nx const std::string& getName() const; void setName(const std::string& name); private: - const std::string kModuleName = "SAC_ENTRY"; + const std::string kModuleName = "SERVICE_ACCESS_CONTROL_ENTRY"; static const size_t kMaxServiceNameLen = 8; - enum SacEntryFlag + enum ServiceAccessControlEntryFlag { SAC_IS_SERVER = _BIT(7), SAC_NAME_LEN_MASK = _BIT(7) - 1 @@ -45,7 +45,7 @@ namespace nx bool mIsServer; std::string mName; - bool isEqual(const SacEntry& other) const; - void copyFrom(const SacEntry& other); + bool isEqual(const ServiceAccessControlEntry& other) const; + void copyFrom(const ServiceAccessControlEntry& other); }; } \ No newline at end of file diff --git a/lib/libnx/source/AccessControlInfoBinary.cpp b/lib/libnx/source/AccessControlInfoBinary.cpp index 6839500..41e4ea9 100644 --- a/lib/libnx/source/AccessControlInfoBinary.cpp +++ b/lib/libnx/source/AccessControlInfoBinary.cpp @@ -151,12 +151,12 @@ void nx::AccessControlInfoBinary::setFileSystemAccessControl(const nx::FileSyste mFileSystemAccessControl = fac; } -const nx::SacBinary& nx::AccessControlInfoBinary::getServiceAccessControl() const +const nx::ServiceAccessControlBinary& nx::AccessControlInfoBinary::getServiceAccessControl() const { return mServiceAccessControl; } -void nx::AccessControlInfoBinary::setServiceAccessControl(const nx::SacBinary& sac) +void nx::AccessControlInfoBinary::setServiceAccessControl(const nx::ServiceAccessControlBinary& sac) { mServiceAccessControl = sac; } diff --git a/lib/libnx/source/AccessControlInfoDescBinary.cpp b/lib/libnx/source/AccessControlInfoDescBinary.cpp index 28b2085..0b07e0e 100644 --- a/lib/libnx/source/AccessControlInfoDescBinary.cpp +++ b/lib/libnx/source/AccessControlInfoDescBinary.cpp @@ -229,12 +229,12 @@ void nx::AccessControlInfoDescBinary::setFileSystemAccessControl(const nx::FileS mFileSystemAccessControl = fac; } -const nx::SacBinary& nx::AccessControlInfoDescBinary::getServiceAccessControl() const +const nx::ServiceAccessControlBinary& nx::AccessControlInfoDescBinary::getServiceAccessControl() const { return mServiceAccessControl; } -void nx::AccessControlInfoDescBinary::setServiceAccessControl(const nx::SacBinary& sac) +void nx::AccessControlInfoDescBinary::setServiceAccessControl(const nx::ServiceAccessControlBinary& sac) { mServiceAccessControl = sac; } diff --git a/lib/libnx/source/SacBinary.cpp b/lib/libnx/source/SacBinary.cpp deleted file mode 100644 index 40b0283..0000000 --- a/lib/libnx/source/SacBinary.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include - -nx::SacBinary::SacBinary() -{ - clear(); -} - -nx::SacBinary::SacBinary(const SacBinary & other) -{ - *this = other; -} - -void nx::SacBinary::operator=(const SacBinary & other) -{ - if (other.getBytes().data()) - { - fromBytes(other.getBytes().data(), other.getBytes().size()); - } - else - { - clear(); - mServices = other.mServices; - } -} - -bool nx::SacBinary::operator==(const SacBinary & other) const -{ - return (mServices == other.mServices); -} - -bool nx::SacBinary::operator!=(const SacBinary & other) const -{ - return !(*this == other); -} - -void nx::SacBinary::toBytes() -{ - size_t totalSize = 0; - for (size_t i = 0; i < mServices.size(); i++) - { - mServices[i].toBytes(); - totalSize += mServices[i].getBytes().size(); - } - - mRawBinary.alloc(totalSize); - for (size_t i = 0, pos = 0; i < mServices.size(); pos += mServices[i].getBytes().size(), i++) - { - memcpy((mRawBinary.data() + pos), mServices[i].getBytes().data(), mServices[i].getBytes().size()); - } -} - -void nx::SacBinary::fromBytes(const byte_t* data, size_t len) -{ - clear(); - mRawBinary.alloc(len); - memcpy(mRawBinary.data(), data, mRawBinary.size()); - - SacEntry sac; - for (size_t pos = 0; pos < len; pos += mServices.atBack().getBytes().size()) - { - sac.fromBytes((const byte_t*)(mRawBinary.data() + pos), len - pos); - mServices.addElement(sac); - } -} - -const fnd::Vec& nx::SacBinary::getBytes() const -{ - return mRawBinary; -} - -void nx::SacBinary::clear() -{ - mRawBinary.clear(); - mServices.clear(); -} - -const fnd::List& nx::SacBinary::getServiceList() const -{ - return mServices; -} - -void nx::SacBinary::addService(const SacEntry& service) -{ - mServices.addElement(service); -} \ No newline at end of file diff --git a/lib/libnx/source/ServiceAccessControlBinary.cpp b/lib/libnx/source/ServiceAccessControlBinary.cpp new file mode 100644 index 0000000..b46f4f9 --- /dev/null +++ b/lib/libnx/source/ServiceAccessControlBinary.cpp @@ -0,0 +1,78 @@ +#include + +nx::ServiceAccessControlBinary::ServiceAccessControlBinary() +{ + clear(); +} + +nx::ServiceAccessControlBinary::ServiceAccessControlBinary(const ServiceAccessControlBinary & other) +{ + *this = other; +} + +void nx::ServiceAccessControlBinary::operator=(const ServiceAccessControlBinary & other) +{ + mRawBinary = other.mRawBinary; + mServices = other.mServices; +} + +bool nx::ServiceAccessControlBinary::operator==(const ServiceAccessControlBinary & other) const +{ + return (mServices == other.mServices); +} + +bool nx::ServiceAccessControlBinary::operator!=(const ServiceAccessControlBinary & other) const +{ + return !(*this == other); +} + +void nx::ServiceAccessControlBinary::toBytes() +{ + size_t totalSize = 0; + for (size_t i = 0; i < mServices.size(); i++) + { + mServices[i].toBytes(); + totalSize += mServices[i].getBytes().size(); + } + + mRawBinary.alloc(totalSize); + for (size_t i = 0, pos = 0; i < mServices.size(); pos += mServices[i].getBytes().size(), i++) + { + memcpy((mRawBinary.data() + pos), mServices[i].getBytes().data(), mServices[i].getBytes().size()); + } +} + +void nx::ServiceAccessControlBinary::fromBytes(const byte_t* data, size_t len) +{ + clear(); + mRawBinary.alloc(len); + memcpy(mRawBinary.data(), data, mRawBinary.size()); + + ServiceAccessControlEntry sac; + for (size_t pos = 0; pos < len; pos += mServices.atBack().getBytes().size()) + { + sac.fromBytes((const byte_t*)(mRawBinary.data() + pos), len - pos); + mServices.addElement(sac); + } +} + +const fnd::Vec& nx::ServiceAccessControlBinary::getBytes() const +{ + return mRawBinary; +} + +void nx::ServiceAccessControlBinary::clear() +{ + mRawBinary.clear(); + mServices.clear(); +} + +const fnd::List& nx::ServiceAccessControlBinary::getServiceList() const +{ + return mServices; +} + +void nx::ServiceAccessControlBinary::addService(const ServiceAccessControlEntry& service) +{ + mServices.addElement(service); +} \ No newline at end of file diff --git a/lib/libnx/source/SacEntry.cpp b/lib/libnx/source/ServiceAccessControlEntry.cpp similarity index 62% rename from lib/libnx/source/SacEntry.cpp rename to lib/libnx/source/ServiceAccessControlEntry.cpp index 8944954..00d3b9c 100644 --- a/lib/libnx/source/SacEntry.cpp +++ b/lib/libnx/source/ServiceAccessControlEntry.cpp @@ -1,23 +1,23 @@ -#include +#include -nx::SacEntry::SacEntry() +nx::ServiceAccessControlEntry::ServiceAccessControlEntry() { clear(); } -nx::SacEntry::SacEntry(const std::string & name, bool isServer) : +nx::ServiceAccessControlEntry::ServiceAccessControlEntry(const std::string & name, bool isServer) : mIsServer(isServer), mName(name) { toBytes(); } -nx::SacEntry::SacEntry(const SacEntry & other) +nx::ServiceAccessControlEntry::ServiceAccessControlEntry(const ServiceAccessControlEntry & other) { *this = other; } -void nx::SacEntry::operator=(const SacEntry & other) +void nx::ServiceAccessControlEntry::operator=(const ServiceAccessControlEntry & other) { if (other.getBytes().size()) { @@ -31,26 +31,26 @@ void nx::SacEntry::operator=(const SacEntry & other) } } -bool nx::SacEntry::operator==(const SacEntry & other) const +bool nx::ServiceAccessControlEntry::operator==(const ServiceAccessControlEntry & other) const { return (mIsServer == other.mIsServer) \ && (mName == other.mName); } -bool nx::SacEntry::operator!=(const SacEntry & other) const +bool nx::ServiceAccessControlEntry::operator!=(const ServiceAccessControlEntry & other) const { return !(*this == other); } -void nx::SacEntry::toBytes() +void nx::ServiceAccessControlEntry::toBytes() { try { mRawBinary.alloc(mName.size() + 1); } catch (const fnd::Exception& e) { - throw fnd::Exception(kModuleName, "Failed to allocate memory for SacEntry: " + std::string(e.what())); + throw fnd::Exception(kModuleName, "Failed to allocate memory for ServiceAccessControlEntry: " + std::string(e.what())); } if (mName.length() == 0) @@ -68,7 +68,7 @@ void nx::SacEntry::toBytes() memcpy(mRawBinary.data() + 1, mName.c_str(), mName.length()); } -void nx::SacEntry::fromBytes(const byte_t* data, size_t len) +void nx::ServiceAccessControlEntry::fromBytes(const byte_t* data, size_t len) { bool isServer = (data[0] & SAC_IS_SERVER) == SAC_IS_SERVER; size_t nameLen = (data[0] & SAC_NAME_LEN_MASK) + 1; // bug? @@ -94,33 +94,33 @@ void nx::SacEntry::fromBytes(const byte_t* data, size_t len) mName = std::string((const char*)(mRawBinary.data() + 1), nameLen); } -const fnd::Vec& nx::SacEntry::getBytes() const +const fnd::Vec& nx::ServiceAccessControlEntry::getBytes() const { return mRawBinary; } -void nx::SacEntry::clear() +void nx::ServiceAccessControlEntry::clear() { mIsServer = false; mName.clear(); } -bool nx::SacEntry::isServer() const +bool nx::ServiceAccessControlEntry::isServer() const { return mIsServer; } -void nx::SacEntry::setIsServer(bool isServer) +void nx::ServiceAccessControlEntry::setIsServer(bool isServer) { mIsServer = isServer; } -const std::string & nx::SacEntry::getName() const +const std::string & nx::ServiceAccessControlEntry::getName() const { return mName; } -void nx::SacEntry::setName(const std::string & name) +void nx::ServiceAccessControlEntry::setName(const std::string & name) { if (name.length() > kMaxServiceNameLen) { diff --git a/programs/nstool/source/NpdmProcess.cpp b/programs/nstool/source/NpdmProcess.cpp index 65fc8a8..b06e353 100644 --- a/programs/nstool/source/NpdmProcess.cpp +++ b/programs/nstool/source/NpdmProcess.cpp @@ -586,7 +586,7 @@ void NpdmProcess::displayFac(const nx::FileSystemAccessControlBinary& fac) } printf("%s", kFsaFlag[fac.getFsaRightsList()[i]].c_str()); if (_HAS_BIT(mCliOutputMode, OUTPUT_EXTENDED)) - printf(" (mask 0x%" PRIx64 ")", _BIT(fac.getFsaRightsList()[i])); + printf(" (bit %" PRId32 ")", fac.getFsaRightsList()[i]); printf("%s", fac.getFsaRightsList()[i] != fac.getFsaRightsList().atBack() ? ", " : "\n"); @@ -616,7 +616,7 @@ void NpdmProcess::displayFac(const nx::FileSystemAccessControlBinary& fac) } -void NpdmProcess::displaySac(const nx::SacBinary& sac) +void NpdmProcess::displaySac(const nx::ServiceAccessControlBinary& sac) { printf("[Service Access Control]\n"); printf(" Service List:\n"); diff --git a/programs/nstool/source/NpdmProcess.h b/programs/nstool/source/NpdmProcess.h index 9997f9b..d0ab52a 100644 --- a/programs/nstool/source/NpdmProcess.h +++ b/programs/nstool/source/NpdmProcess.h @@ -39,6 +39,6 @@ private: void displayAciHdr(const nx::AccessControlInfoBinary& aci); void displayAciDescHdr(const nx::AccessControlInfoDescBinary& aci); void displayFac(const nx::FileSystemAccessControlBinary& fac); - void displaySac(const nx::SacBinary& sac); + void displaySac(const nx::ServiceAccessControlBinary& sac); void displayKernelCap(const nx::KcBinary& kern); }; \ No newline at end of file