From 7b34bd404d37b01345a3934c8004a07af7620dc0 Mon Sep 17 00:00:00 2001 From: jakcron Date: Thu, 6 Jul 2017 21:17:21 +1000 Subject: [PATCH] [nx] Added nx namespace. --- lib/nx/AciHeader.cpp | 2 + lib/nx/AciHeader.h | 163 +++++++++++------------ lib/nx/FacBinary.cpp | 2 +- lib/nx/FacBinary.h | 143 ++++++++++---------- lib/nx/FacHeader.cpp | 2 +- lib/nx/FacHeader.h | 138 ++++++++++---------- lib/nx/ISerialiseableBinary.h | 23 ++-- lib/nx/NcaHeader.cpp | 3 + lib/nx/NcaHeader.h | 236 +++++++++++++++++----------------- lib/nx/SacBinary.cpp | 2 +- lib/nx/SacBinary.h | 61 ++++----- lib/nx/SacEntry.cpp | 2 + lib/nx/SacEntry.h | 87 +++++++------ programs/ncatool/main.cpp | 6 +- 14 files changed, 450 insertions(+), 420 deletions(-) diff --git a/lib/nx/AciHeader.cpp b/lib/nx/AciHeader.cpp index 6acb027..234bc57 100644 --- a/lib/nx/AciHeader.cpp +++ b/lib/nx/AciHeader.cpp @@ -1,5 +1,7 @@ #include "AciHeader.h" +using namespace nx; + void AciHeader::clearVariables() { mType = TYPE_ACI0; diff --git a/lib/nx/AciHeader.h b/lib/nx/AciHeader.h index 657b68e..1c22694 100644 --- a/lib/nx/AciHeader.h +++ b/lib/nx/AciHeader.h @@ -4,106 +4,109 @@ #include #include -class AciHeader : public ISerialiseableBinary +namespace nx { -public: - enum AciType + class AciHeader : public ISerialiseableBinary { - TYPE_ACI0, // for Access Control Info - TYPE_ACID // for Access Control Info Desc - }; + public: + enum AciType + { + TYPE_ACI0, // for Access Control Info + TYPE_ACID // for Access Control Info Desc + }; - struct sSection - { - size_t offset; - size_t size; - }; + struct sSection + { + size_t offset; + size_t size; + }; - AciHeader(); - AciHeader(const AciHeader& other); - AciHeader(const u8* bytes); + AciHeader(); + AciHeader(const AciHeader& other); + AciHeader(const u8* bytes); - bool operator==(const AciHeader& other) const; - bool operator!=(const AciHeader& other) const; - void operator=(const AciHeader& other); + bool operator==(const AciHeader& other) const; + bool operator!=(const AciHeader& other) const; + void operator=(const AciHeader& other); - // to be used after export - const u8* getBytes() const; - size_t getSize() const; + // to be used after export + const u8* getBytes() const; + size_t getSize() const; - // export/import binary - void exportBinary(); - void importBinary(const u8* bytes); - void importBinary(const u8* bytes, size_t len); + // export/import binary + void exportBinary(); + void importBinary(const u8* bytes); + void importBinary(const u8* bytes, size_t len); - // variables - AciType getAciType() const; - void setAciType(AciType type); - u64 getProgramId() const; - void setProgramId(u64 program_id); - const sSection& getFileAccessControl() const; - void setFileAccessControl(u32 size); - const sSection& getServiceAccessControl() const; - void setServiceAccessControl(u32 size); - const sSection& getKernelCapabilities() const; - void setKernelCapabilities(u32 size); + // variables + AciType getAciType() const; + void setAciType(AciType type); + u64 getProgramId() const; + void setProgramId(u64 program_id); + const sSection& getFileAccessControl() const; + void setFileAccessControl(u32 size); + const sSection& getServiceAccessControl() const; + void setServiceAccessControl(u32 size); + const sSection& getKernelCapabilities() const; + void setKernelCapabilities(u32 size); -private: - const std::string kModuleName = "ACI_HEADER"; - const std::string kAciStructSig = "ACI0"; - const std::string kAciDescStructSig = "ACID"; - static const size_t kAciAlignSize = 0x10; + private: + const std::string kModuleName = "ACI_HEADER"; + const std::string kAciStructSig = "ACI0"; + const std::string kAciDescStructSig = "ACID"; + static const size_t kAciAlignSize = 0x10; #pragma pack(push, 1) - struct sAciHeader - { - private: - u8 signature_[4]; - u8 reserved_1[12]; - u64 program_id_; - u8 reserved_2[8]; - struct sAciSection + struct sAciHeader { private: - u32 offset_; // aligned by 0x10 from the last one - u32 mSize; + u8 signature_[4]; + u8 reserved_1[12]; + u64 program_id_; + u8 reserved_2[8]; + struct sAciSection + { + private: + u32 offset_; // aligned by 0x10 from the last one + u32 mSize; + public: + u32 offset() const { return le_word(offset_); } + void set_offset(u32 offset) { offset_ = le_word(offset); } + + u32 size() const { return le_word(mSize); } + void set_size(u32 size) { mSize = le_word(size); } + } fac_, sac_, kc_; + u8 reserved_3[8]; public: - u32 offset() const { return le_word(offset_); } - void set_offset(u32 offset) { offset_ = le_word(offset); } + const char* signature() const { return (const char*)signature_; } + void set_signature(const char* signature) { memcpy(signature_, signature, 4); } - u32 size() const { return le_word(mSize); } - void set_size(u32 size) { mSize = le_word(size); } - } fac_, sac_, kc_; - u8 reserved_3[8]; - public: - const char* signature() const { return (const char*)signature_; } - void set_signature(const char* signature) { memcpy(signature_, signature, 4); } + u64 program_id() const { return le_dword(program_id_); } + void set_program_id(u64 program_id) { program_id_ = le_dword(program_id); } - u64 program_id() const { return le_dword(program_id_); } - void set_program_id(u64 program_id) { program_id_ = le_dword(program_id); } + const sAciSection& fac() const { return fac_; } + sAciSection& fac() { return fac_; } - const sAciSection& fac() const { return fac_; } - sAciSection& fac() { return fac_; } + const sAciSection& sac() const { return sac_; } + sAciSection& sac() { return sac_; } - const sAciSection& sac() const { return sac_; } - sAciSection& sac() { return sac_; } - - const sAciSection& kc() const { return kc_; } - sAciSection& kc() { return kc_; } - }; + const sAciSection& kc() const { return kc_; } + sAciSection& kc() { return kc_; } + }; #pragma pack(pop) - // raw data - fnd::MemoryBlob mBinaryBlob; + // raw data + fnd::MemoryBlob mBinaryBlob; - // variables - AciType mType; - u64 mProgramId; - sSection mFac, mSac, mKc; + // variables + AciType mType; + u64 mProgramId; + sSection mFac, mSac, mKc; - void clearVariables(); - void calculateSectionOffsets(); - bool isEqual(const AciHeader& other) const; - void copyFrom(const AciHeader& other); -}; + void clearVariables(); + void calculateSectionOffsets(); + bool isEqual(const AciHeader& other) const; + void copyFrom(const AciHeader& other); + }; +} diff --git a/lib/nx/FacBinary.cpp b/lib/nx/FacBinary.cpp index 5da0b5f..88923ed 100644 --- a/lib/nx/FacBinary.cpp +++ b/lib/nx/FacBinary.cpp @@ -1,6 +1,6 @@ #include "FacBinary.h" - +using namespace nx; FacBinary::FacBinary() : mHeader() diff --git a/lib/nx/FacBinary.h b/lib/nx/FacBinary.h index 61eebba..1305a9b 100644 --- a/lib/nx/FacBinary.h +++ b/lib/nx/FacBinary.h @@ -5,77 +5,80 @@ #include #include -class FacBinary : - public ISerialiseableBinary +namespace nx { -public: - enum FsAccessFlag + class FacBinary : + public ISerialiseableBinary { - FSA_APPLICATION_INFO = BIT(0), - FSA_BOOT_MODE_CONTROL = BIT(1), - FSA_CALIBRATION = BIT(2), - FSA_SYSTEM_SAVE_DATA = BIT(3), - FSA_GAME_CARD = BIT(4), - FSA_SAVE_DATA_BACKUP = BIT(5), - FSA_SAVE_DATA_MANAGEMENT = BIT(6), - FSA_BIS_ALL_RAW = BIT(7), - FSA_GAME_CARD_RAW = BIT(8), - FSA_GAME_CARD_PRIVATE = BIT(9), - FSA_SET_TIME = BIT(10), - FSA_CONTENT_MANAGER = BIT(11), - FSA_IMAGE_MANAGER = BIT(12), - FSA_CREATE_SAVE_DATA = BIT(13), - FSA_SYSTEM_SAVE_DATA_MANAGEMENT = BIT(14), - FSA_BIS_FILE_SYSTEM = BIT(15), - FSA_SYSTEM_UPDATE = BIT(16), - FSA_SAVE_DATA_META = BIT(17), - FSA_DEVICE_SAVE_CONTROL = BIT(19), - FSA_SETTINGS_CONTROL = BIT(20), - FSA_DEBUG = BIT(62), - FSA_FULL_PERMISSION = BIT(63), + public: + enum FsAccessFlag + { + FSA_APPLICATION_INFO = BIT(0), + FSA_BOOT_MODE_CONTROL = BIT(1), + FSA_CALIBRATION = BIT(2), + FSA_SYSTEM_SAVE_DATA = BIT(3), + FSA_GAME_CARD = BIT(4), + FSA_SAVE_DATA_BACKUP = BIT(5), + FSA_SAVE_DATA_MANAGEMENT = BIT(6), + FSA_BIS_ALL_RAW = BIT(7), + FSA_GAME_CARD_RAW = BIT(8), + FSA_GAME_CARD_PRIVATE = BIT(9), + FSA_SET_TIME = BIT(10), + FSA_CONTENT_MANAGER = BIT(11), + FSA_IMAGE_MANAGER = BIT(12), + FSA_CREATE_SAVE_DATA = BIT(13), + FSA_SYSTEM_SAVE_DATA_MANAGEMENT = BIT(14), + FSA_BIS_FILE_SYSTEM = BIT(15), + FSA_SYSTEM_UPDATE = BIT(16), + FSA_SAVE_DATA_META = BIT(17), + FSA_DEVICE_SAVE_CONTROL = BIT(19), + FSA_SETTINGS_CONTROL = BIT(20), + FSA_DEBUG = BIT(62), + FSA_FULL_PERMISSION = BIT(63), + }; + + FacBinary(); + FacBinary(const FacBinary& other); + FacBinary(const u8* bytes, size_t len); + + bool operator==(const FacBinary& other) const; + bool operator!=(const FacBinary& other) const; + void operator=(const FacBinary& other); + + // to be used after export + const u8* getBytes() const; + size_t getSize() const; + + // export/import binary + void exportBinary(); + void importBinary(const u8* bytes); + void importBinary(const u8* bytes, size_t len); + + // variables + bool isPermissionSet(FsAccessFlag flag) const; + void addPermission(FsAccessFlag flag); + void removePermission(FsAccessFlag flag); + + const fnd::List& getContentOwnerIds() const; + void addContentOwnerId(u32 id); + + const fnd::List& getSaveDataOwnerIds() const; + void addSaveDataOwnerId(u32 id); + private: + const std::string kModuleName = "FAC_BINARY"; + + // raw binary + fnd::MemoryBlob mBinaryBlob; + + // variables + FacHeader mHeader; + u64 mFsaRights; + fnd::List mContentOwnerIds; + fnd::List mSaveDataOwnerIds; + + void clearVariables(); + bool isEqual(const FacBinary& other) const; + void copyFrom(const FacBinary& other); }; - - FacBinary(); - FacBinary(const FacBinary& other); - FacBinary(const u8* bytes, size_t len); - - bool operator==(const FacBinary& other) const; - bool operator!=(const FacBinary& other) const; - void operator=(const FacBinary& other); - - // to be used after export - const u8* getBytes() const; - size_t getSize() const; - - // export/import binary - void exportBinary(); - void importBinary(const u8* bytes); - void importBinary(const u8* bytes, size_t len); - - // variables - bool isPermissionSet(FsAccessFlag flag) const; - void addPermission(FsAccessFlag flag); - void removePermission(FsAccessFlag flag); - - const fnd::List& getContentOwnerIds() const; - void addContentOwnerId(u32 id); - - const fnd::List& getSaveDataOwnerIds() const; - void addSaveDataOwnerId(u32 id); -private: - const std::string kModuleName = "FAC_BINARY"; - - // raw binary - fnd::MemoryBlob mBinaryBlob; - - // variables - FacHeader mHeader; - u64 mFsaRights; - fnd::List mContentOwnerIds; - fnd::List mSaveDataOwnerIds; - - void clearVariables(); - bool isEqual(const FacBinary& other) const; - void copyFrom(const FacBinary& other); -}; +} diff --git a/lib/nx/FacHeader.cpp b/lib/nx/FacHeader.cpp index b415892..1a7187c 100644 --- a/lib/nx/FacHeader.cpp +++ b/lib/nx/FacHeader.cpp @@ -1,6 +1,6 @@ #include "FacHeader.h" - +using namespace nx; FacHeader::FacHeader() { diff --git a/lib/nx/FacHeader.h b/lib/nx/FacHeader.h index 143f7c9..b00510f 100644 --- a/lib/nx/FacHeader.h +++ b/lib/nx/FacHeader.h @@ -3,91 +3,95 @@ #include #include -class FacHeader : - public ISerialiseableBinary +namespace nx { -public: - FacHeader(); - FacHeader(const FacHeader& other); - FacHeader(const u8* bytes); + class FacHeader : + public ISerialiseableBinary + { + public: + FacHeader(); + FacHeader(const FacHeader& other); + FacHeader(const u8* bytes); - bool operator==(const FacHeader& other) const; - bool operator!=(const FacHeader& other) const; - void operator=(const FacHeader& other); + bool operator==(const FacHeader& other) const; + bool operator!=(const FacHeader& other) const; + void operator=(const FacHeader& other); - // to be used after export - const u8* getBytes() const; - size_t getSize() const; + // to be used after export + const u8* getBytes() const; + size_t getSize() const; - // export/import binary - void exportBinary(); - void importBinary(const u8* bytes); - void importBinary(const u8* bytes, size_t len); + // export/import binary + void exportBinary(); + void importBinary(const u8* bytes); + void importBinary(const u8* bytes, size_t len); - // variables - u64 getFacSize() const; + // variables + u64 getFacSize() const; - u64 getFsaRights() const; - void setFsaRights(u64 flag); - - size_t getContentOwnerIdOffset() const; - size_t getContentOwnerIdSize() const; - void setContentOwnerIdSize(size_t size); + u64 getFsaRights() const; + void setFsaRights(u64 flag); - size_t getSaveDataOwnerIdOffset() const; - size_t getSaveDataOwnerIdSize() const; - void setSaveDataOwnerIdSize(size_t size); + size_t getContentOwnerIdOffset() const; + size_t getContentOwnerIdSize() const; + void setContentOwnerIdSize(size_t size); -private: - const std::string kModuleName = "FAC_HEADER"; - static const u32 kFacFormatVersion = 1; + size_t getSaveDataOwnerIdOffset() const; + size_t getSaveDataOwnerIdSize() const; + void setSaveDataOwnerIdSize(size_t size); + + private: + const std::string kModuleName = "FAC_HEADER"; + static const u32 kFacFormatVersion = 1; #pragma pack (push, 1) - struct sFacHeader - { - private: - u32 version_; // default 1 - u64 fac_flags_; - struct sFacSection + struct sFacHeader { private: - u32 start_; - u32 end_; + u32 version_; // default 1 + u64 fac_flags_; + struct sFacSection + { + private: + u32 start_; + u32 end_; + public: + u32 start() const { return le_word(start_); } + void set_start(u32 start) { start_ = le_word(start); } + + u32 end() const { return le_word(end_); } + void set_end(u32 end) { end_ = le_word(end); } + } content_owner_ids_, save_data_owner_ids_; // the data for these follow later in binary. start/end relative to base of FacData instance public: - u32 start() const { return le_word(start_); } - void set_start(u32 start) { start_ = le_word(start); } + u32 version() const { return le_word(version_); } + void set_version(u32 version) { version_ = le_word(version); } - u32 end() const { return le_word(end_); } - void set_end(u32 end) { end_ = le_word(end); } - } content_owner_ids_, save_data_owner_ids_; // the data for these follow later in binary. start/end relative to base of FacData instance - public: - u32 version() const { return le_word(version_); } - void set_version(u32 version) { version_ = le_word(version); } + u64 fac_flags() const { return le_dword(fac_flags_); } + void set_fac_flags(u64 fac_flags) { fac_flags_ = le_dword(fac_flags); } - u64 fac_flags() const { return le_dword(fac_flags_); } - void set_fac_flags(u64 fac_flags) { fac_flags_ = le_dword(fac_flags); } + const sFacSection& content_owner_ids() const { return content_owner_ids_; } + sFacSection& content_owner_ids() { return content_owner_ids_; } - const sFacSection& content_owner_ids() const { return content_owner_ids_; } - sFacSection& content_owner_ids() { return content_owner_ids_; } - - const sFacSection& save_data_owner_ids() const { return save_data_owner_ids_; } - sFacSection& save_data_owner_ids() { return save_data_owner_ids_; } - }; + const sFacSection& save_data_owner_ids() const { return save_data_owner_ids_; } + sFacSection& save_data_owner_ids() { return save_data_owner_ids_; } + }; #pragma pack (pop) - // raw binary - fnd::MemoryBlob mBinaryBlob; + // raw binary + fnd::MemoryBlob mBinaryBlob; - // variables - u64 mFsaRights; - struct sSection { - size_t offset; - size_t size; - } mContentOwnerIdPos, mSaveDataOwnerIdPos; + // variables + u64 mFsaRights; + struct sSection + { + size_t offset; + size_t size; + } mContentOwnerIdPos, mSaveDataOwnerIdPos; - void clearVariables(); - void calculateOffsets(); - bool isEqual(const FacHeader& other) const; - void copyFrom(const FacHeader& other); -}; + void clearVariables(); + void calculateOffsets(); + bool isEqual(const FacHeader& other) const; + void copyFrom(const FacHeader& other); + }; +} diff --git a/lib/nx/ISerialiseableBinary.h b/lib/nx/ISerialiseableBinary.h index 7bb425d..9dcb807 100644 --- a/lib/nx/ISerialiseableBinary.h +++ b/lib/nx/ISerialiseableBinary.h @@ -1,17 +1,20 @@ #pragma once #include -class ISerialiseableBinary +namespace nx { -public: - //virtual bool operator==(const ISerialiseableBinary& other) = 0; - //virtual void operator=(const ISerialiseableBinary& other) = 0; + class ISerialiseableBinary + { + public: + //virtual bool operator==(const ISerialiseableBinary& other) = 0; + //virtual void operator=(const ISerialiseableBinary& other) = 0; - virtual const u8* getBytes() const = 0; - virtual size_t getSize() const = 0; + virtual const u8* getBytes() const = 0; + virtual size_t getSize() const = 0; - virtual void exportBinary() = 0; - virtual void importBinary(const u8* bytes) = 0; - virtual void importBinary(const u8* bytes, size_t len) = 0; -}; + virtual void exportBinary() = 0; + virtual void importBinary(const u8* bytes) = 0; + virtual void importBinary(const u8* bytes, size_t len) = 0; + }; +} diff --git a/lib/nx/NcaHeader.cpp b/lib/nx/NcaHeader.cpp index 9c6f7cc..755adf0 100644 --- a/lib/nx/NcaHeader.cpp +++ b/lib/nx/NcaHeader.cpp @@ -1,6 +1,9 @@ #include "NcaHeader.h" #include +using namespace nx; + + void NcaHeader::exportBinary() { mBinaryBlob.alloc(sizeof(sNcaHeader)); diff --git a/lib/nx/NcaHeader.h b/lib/nx/NcaHeader.h index 1901cd4..cb47f0b 100644 --- a/lib/nx/NcaHeader.h +++ b/lib/nx/NcaHeader.h @@ -7,149 +7,153 @@ #include #include -class NcaHeader : public ISerialiseableBinary +namespace nx { -public: - struct sSection + class NcaHeader : public ISerialiseableBinary { - u64 offset; - u64 size; - u8 key_type; - crypto::sha::sSha256Hash hash; - - const sSection& operator=(const sSection& other) + public: + struct sSection { - offset = other.offset; - size = other.size; - key_type = other.key_type; - hash = other.hash; + u64 offset; + u64 size; + u8 key_type; + crypto::sha::sSha256Hash hash; - return *this; - } + const sSection& operator=(const sSection& other) + { + offset = other.offset; + size = other.size; + key_type = other.key_type; + hash = other.hash; - bool operator==(const sSection& other) const - { - return (offset == other.offset) \ - && (size == other.size) \ - && (key_type == other.key_type) \ - && (hash == other.hash); - } + return *this; + } - bool operator!=(const sSection& other) const - { - return operator==(other); - } - }; + bool operator==(const sSection& other) const + { + return (offset == other.offset) \ + && (size == other.size) \ + && (key_type == other.key_type) \ + && (hash == other.hash); + } - static const size_t kDefaultBlockSize = 0x200; + bool operator!=(const sSection& other) const + { + return operator==(other); + } + }; - NcaHeader(); - NcaHeader(const NcaHeader& other); - NcaHeader(const u8* bytes); + static const size_t kDefaultBlockSize = 0x200; - bool operator==(const NcaHeader& other) const; - bool operator!=(const NcaHeader& other) const; - void operator=(const NcaHeader& other); + NcaHeader(); + NcaHeader(const NcaHeader& other); + NcaHeader(const u8* bytes); - // to be used after export - const u8* getBytes() const; - size_t getSize() const; + bool operator==(const NcaHeader& other) const; + bool operator!=(const NcaHeader& other) const; + void operator=(const NcaHeader& other); - // export/import binary - void exportBinary(); - void importBinary(const u8* bytes); - void importBinary(const u8* bytes, size_t len); + // to be used after export + const u8* getBytes() const; + size_t getSize() const; - // variables - u64 getNcaSize() const; - void setNcaSize(u64 size); - u64 getProgramId() const; - void setProgramId(u64 program_id); - u32 getUnk() const; - const fnd::List& getSections() const; - void addSection(const sSection& section); - const fnd::List& getAesKeys() const; - void addKey(const crypto::aes::sAes128Key& key); + // export/import binary + void exportBinary(); + void importBinary(const u8* bytes); + void importBinary(const u8* bytes, size_t len); -private: - const std::string kModuleName = "NCA_HEADER"; - const std::string kNcaSig = "NCA2"; - static const size_t kSectionNum = 4; - static const size_t kAesKeyNum = 4; + // variables + u64 getNcaSize() const; + void setNcaSize(u64 size); + u64 getProgramId() const; + void setProgramId(u64 program_id); + u32 getUnk() const; + const fnd::List& getSections() const; + void addSection(const sSection& section); + const fnd::List& getAesKeys() const; + void addKey(const crypto::aes::sAes128Key& key); + + private: + const std::string kModuleName = "NCA_HEADER"; + const std::string kNcaSig = "NCA2"; + static const size_t kSectionNum = 4; + static const size_t kAesKeyNum = 4; #pragma pack (push, 1) - struct sNcaHeader - { - private: - u8 signature_[4]; - u8 reserved_0[2]; - u16 block_size_; - u64 nca_size_; - u64 program_id_; - u8 reserved_1[4]; - u32 unk_0_; - u8 reserved_2[0x20]; - struct sNcaSection + struct sNcaHeader { private: - u32 start_; // block units - u32 end_; // block units - u8 key_type_; - u8 reserved[7]; + u8 signature_[4]; + u8 reserved_0[2]; + u16 block_size_; + u64 nca_size_; + u64 program_id_; + u8 reserved_1[4]; + u32 unk_0_; + u8 reserved_2[0x20]; + struct sNcaSection + { + private: + u32 start_; // block units + u32 end_; // block units + u8 key_type_; + u8 reserved[7]; + public: + u32 start() const { return le_word(start_); } + void set_start(u32 offset) { start_ = le_word(offset); } + + u32 end() const { return le_word(end_); } + void set_end(u32 offset) { end_ = le_word(offset); } + + u8 key_type() const { return key_type_; } + void set_key_type(u8 key_type) { key_type_ = key_type; } + } section_[kSectionNum]; + crypto::sha::sSha256Hash section_hash_[kSectionNum]; + crypto::aes::sAes128Key aes_keys_[kAesKeyNum]; public: - u32 start() const { return le_word(start_); } - void set_start(u32 offset) { start_ = le_word(offset); } + const char* signature() const { return (const char*)signature_; } + void set_signature(const char* signature) { memcpy(signature_, signature, 4); } - u32 end() const { return le_word(end_); } - void set_end(u32 offset) { end_ = le_word(offset); } + u16 block_size() const { return le_hword(block_size_); } + void set_block_size(u16 block_size) { block_size_ = le_hword(block_size); } - u8 key_type() const { return key_type_; } - void set_key_type(u8 key_type) { key_type_ = key_type; } - } section_[kSectionNum]; - crypto::sha::sSha256Hash section_hash_[kSectionNum]; - crypto::aes::sAes128Key aes_keys_[kAesKeyNum]; - public: - const char* signature() const { return (const char*)signature_; } - void set_signature(const char* signature) { memcpy(signature_, signature, 4); } + u64 nca_size() const { return le_dword(nca_size_); } + void set_nca_size(u64 nca_size) { nca_size_ = le_dword(nca_size); } - u16 block_size() const { return le_hword(block_size_); } - void set_block_size(u16 block_size) { block_size_ = le_hword(block_size); } + u64 program_id() const { return le_dword(program_id_); } + void set_program_id(u64 program_id) { program_id_ = le_dword(program_id); } - u64 nca_size() const { return le_dword(nca_size_); } - void set_nca_size(u64 nca_size) { nca_size_ = le_dword(nca_size); } + u32 unk0() const { return le_word(unk_0_); } + void set_unk0(u32 val) { unk_0_ = le_word(val); } - u64 program_id() const { return le_dword(program_id_); } - void set_program_id(u64 program_id) { program_id_ = le_dword(program_id); } + const sNcaSection& section(u8 index) const { return section_[index%kSectionNum]; } + sNcaSection& section(u8 index) { return section_[index%kSectionNum]; } - u32 unk0() const { return le_word(unk_0_); } - void set_unk0(u32 val) { unk_0_ = le_word(val); } + const crypto::sha::sSha256Hash& section_hash(u8 index) const { return section_hash_[index%kSectionNum]; } + crypto::sha::sSha256Hash& section_hash(u8 index) { return section_hash_[index%kSectionNum]; } - const sNcaSection& section(u8 index) const { return section_[index%kSectionNum]; } - sNcaSection& section(u8 index) { return section_[index%kSectionNum]; } - - const crypto::sha::sSha256Hash& section_hash(u8 index) const { return section_hash_[index%kSectionNum]; } - crypto::sha::sSha256Hash& section_hash(u8 index) { return section_hash_[index%kSectionNum]; } - - const crypto::aes::sAes128Key& aes_key(u8 index) const { return aes_keys_[index%kAesKeyNum]; } - crypto::aes::sAes128Key& aes_key(u8 index) { return aes_keys_[index%kAesKeyNum]; } - }; + const crypto::aes::sAes128Key& aes_key(u8 index) const { return aes_keys_[index%kAesKeyNum]; } + crypto::aes::sAes128Key& aes_key(u8 index) { return aes_keys_[index%kAesKeyNum]; } + }; #pragma pack (pop) - // binary - fnd::MemoryBlob mBinaryBlob; + // binary + fnd::MemoryBlob mBinaryBlob; - // data - u16 mBlockSize; - u64 mNcaSize; - u64 mProgramId; - u32 mUnk0; - fnd::List mSections; - fnd::List mAesKeys; + // data + u16 mBlockSize; + u64 mNcaSize; + u64 mProgramId; + u32 mUnk0; + fnd::List mSections; + fnd::List mAesKeys; - void clearVariables(); - u64 blockNumToSize(u32 block_num) const; - u32 sizeToBlockNum(u64 real_size) const; - bool isEqual(const NcaHeader& other) const; - void copyFrom(const NcaHeader& other); -}; + void clearVariables(); + u64 blockNumToSize(u32 block_num) const; + u32 sizeToBlockNum(u64 real_size) const; + bool isEqual(const NcaHeader& other) const; + void copyFrom(const NcaHeader& other); + }; + +} \ No newline at end of file diff --git a/lib/nx/SacBinary.cpp b/lib/nx/SacBinary.cpp index 6f16c3e..cc452a7 100644 --- a/lib/nx/SacBinary.cpp +++ b/lib/nx/SacBinary.cpp @@ -1,6 +1,6 @@ #include "SacBinary.h" - +using namespace nx; SacBinary::SacBinary() { diff --git a/lib/nx/SacBinary.h b/lib/nx/SacBinary.h index f9b7d81..ed87936 100644 --- a/lib/nx/SacBinary.h +++ b/lib/nx/SacBinary.h @@ -6,41 +6,44 @@ #include #include -class SacBinary : - public ISerialiseableBinary +namespace nx { -public: - SacBinary(); - SacBinary(const SacBinary& other); - SacBinary(const u8* bytes, size_t len); + class SacBinary : + public ISerialiseableBinary + { + public: + SacBinary(); + SacBinary(const SacBinary& other); + SacBinary(const u8* bytes, size_t len); - bool operator==(const SacBinary& other) const; - bool operator!=(const SacBinary& other) const; - void operator=(const SacBinary& other); + bool operator==(const SacBinary& other) const; + bool operator!=(const SacBinary& other) const; + void operator=(const SacBinary& other); - // to be used after export - const u8* getBytes() const; - size_t getSize() const; + // to be used after export + const u8* getBytes() const; + size_t getSize() const; - // export/import binary - void exportBinary(); - void importBinary(const u8* bytes); - void importBinary(const u8* bytes, size_t len); + // export/import binary + void exportBinary(); + void importBinary(const u8* bytes); + void importBinary(const u8* bytes, size_t len); - // variables - const fnd::List& getServiceList() const; - void addService(const SacEntry& service); -private: - const std::string kModuleName = "SAC_BINARY"; + // variables + const fnd::List& getServiceList() const; + void addService(const SacEntry& service); + private: + const std::string kModuleName = "SAC_BINARY"; - // raw binary - fnd::MemoryBlob mBinaryBlob; + // raw binary + fnd::MemoryBlob mBinaryBlob; - // variables - fnd::List mServices; + // variables + fnd::List mServices; - void clearVariables(); - bool isEqual(const SacBinary& other) const; - void copyFrom(const SacBinary& other); -}; + void clearVariables(); + bool isEqual(const SacBinary& other) const; + void copyFrom(const SacBinary& other); + }; +} diff --git a/lib/nx/SacEntry.cpp b/lib/nx/SacEntry.cpp index dcf748e..994e8e3 100644 --- a/lib/nx/SacEntry.cpp +++ b/lib/nx/SacEntry.cpp @@ -1,5 +1,7 @@ #include "SacEntry.h" +using namespace nx; + SacEntry::SacEntry() : mIsServer(false), mName("") diff --git a/lib/nx/SacEntry.h b/lib/nx/SacEntry.h index 14ca515..7e1878b 100644 --- a/lib/nx/SacEntry.h +++ b/lib/nx/SacEntry.h @@ -4,48 +4,51 @@ #include #include -class SacEntry : public ISerialiseableBinary +namespace nx { -public: - SacEntry(); - SacEntry(const std::string& name, bool isServer); - SacEntry(const SacEntry& other); - - bool operator==(const SacEntry& other) const; - bool operator!=(const SacEntry& other) const; - void operator=(const SacEntry& other); - - // to be used after export - const u8* getBytes() const; - size_t getSize() const; - - // export/import binary - void exportBinary(); - void importBinary(const u8* bytes); - void importBinary(const u8* bytes, size_t len); - - // variables - bool isServer() const; - void setIsServer(bool isServer); - const std::string& getName() const; - void setName(const std::string& name); -private: - const std::string kModuleName = "SAC_ENTRY"; - static const size_t kMaxServiceNameLen = 8; - - enum SacEntryFlag + class SacEntry : public ISerialiseableBinary { - SAC_IS_SERVER = BIT(7), - SAC_NAME_LEN_MASK = BIT(7)-1 + public: + SacEntry(); + SacEntry(const std::string& name, bool isServer); + SacEntry(const SacEntry& other); + + bool operator==(const SacEntry& other) const; + bool operator!=(const SacEntry& other) const; + void operator=(const SacEntry& other); + + // to be used after export + const u8* getBytes() const; + size_t getSize() const; + + // export/import binary + void exportBinary(); + void importBinary(const u8* bytes); + void importBinary(const u8* bytes, size_t len); + + // variables + bool isServer() const; + void setIsServer(bool isServer); + const std::string& getName() const; + void setName(const std::string& name); + private: + const std::string kModuleName = "SAC_ENTRY"; + static const size_t kMaxServiceNameLen = 8; + + enum SacEntryFlag + { + SAC_IS_SERVER = BIT(7), + SAC_NAME_LEN_MASK = BIT(7) - 1 + }; + + // raw binary + fnd::MemoryBlob mBinaryBlob; + + // variables + bool mIsServer; + std::string mName; + + bool isEqual(const SacEntry& other) const; + void copyFrom(const SacEntry& other); }; - - // raw binary - fnd::MemoryBlob mBinaryBlob; - - // variables - bool mIsServer; - std::string mName; - - bool isEqual(const SacEntry& other) const; - void copyFrom(const SacEntry& other); -}; \ No newline at end of file +} \ No newline at end of file diff --git a/programs/ncatool/main.cpp b/programs/ncatool/main.cpp index 5e5177a..c47f0a3 100644 --- a/programs/ncatool/main.cpp +++ b/programs/ncatool/main.cpp @@ -6,7 +6,7 @@ #include #include -const size_t kNcaSectorSize = NcaHeader::kDefaultBlockSize; +const size_t kNcaSectorSize = nx::NcaHeader::kDefaultBlockSize; void initNcaCtr(u8 ctr[crypto::aes::kAesBlockSize], u32 generation) { @@ -69,7 +69,7 @@ int main(int argc, char** argv) { decryptNcaSectorXts(nca, sector, 1, nx::crypto::aes::nca_header_key[0], nx::crypto::aes::nca_header_key[1]); - NcaHeader hdr; + nx::NcaHeader hdr; hdr.importBinary(sector); printf("[NCA Header]\n"); @@ -79,7 +79,7 @@ int main(int argc, char** argv) printf(" Sections:\n"); for (size_t i = 0; i < hdr.getSections().getSize(); i++) { - const NcaHeader::sSection& section = hdr.getSections()[i]; + const nx::NcaHeader::sSection& section = hdr.getSections()[i]; printf(" %lu:\n", i); //printf(" Start Blk: %" PRId32 "\n", section.start_blk); //printf(" End Blk: %" PRId32 "\n", section.end_blk);