From 0c6efad6037cb3ffbf3212ecebb7862b1016d6b3 Mon Sep 17 00:00:00 2001 From: jakcron Date: Thu, 30 Sep 2021 19:41:57 +0800 Subject: [PATCH] Port CnmtProcess to libtoolchain. --- src/CnmtProcess.cpp | 163 ++++++++++++++++++++++---------------------- src/CnmtProcess.h | 2 +- 2 files changed, 83 insertions(+), 82 deletions(-) diff --git a/src/CnmtProcess.cpp b/src/CnmtProcess.cpp index 5849c16..75a46c4 100644 --- a/src/CnmtProcess.cpp +++ b/src/CnmtProcess.cpp @@ -1,11 +1,9 @@ #include "CnmtProcess.h" -#include -#include - #include nstool::CnmtProcess::CnmtProcess() : + mModuleName("nstool::CnmtProcess"), mFile(), mCliOutputMode(true, false, false, false), mVerify(false) @@ -42,96 +40,102 @@ const nn::hac::ContentMeta& nstool::CnmtProcess::getContentMeta() const void nstool::CnmtProcess::importCnmt() { - tc::ByteData scratch; - - if (*mFile == nullptr) + if (mFile == nullptr) { - throw tc::Exception(kModuleName, "No file reader set."); + throw tc::Exception(mModuleName, "No file reader set."); } - scratch.alloc((*mFile)->size()); - (*mFile)->read(scratch.data(), 0, scratch.size()); + + // check if file_size is greater than 20MB, don't import. + size_t cnmt_file_size = tc::io::IOUtil::castInt64ToSize(mFile->length()); + if (cnmt_file_size > (0x100000 * 20)) + { + throw tc::Exception(mModuleName, "File too large."); + } + // read cnmt + tc::ByteData scratch = tc::ByteData(cnmt_file_size); + mFile->seek(0, tc::io::SeekOrigin::Begin); + mFile->read(scratch.data(), scratch.size()); + + // parse cnmt mCnmt.fromBytes(scratch.data(), scratch.size()); } void nstool::CnmtProcess::displayCnmt() { - std::cout << "[ContentMeta]" << std::endl; - std::cout << " TitleId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getTitleId() << std::endl; - std::cout << " Version: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getTitleVersion()) << " (v" << std::dec << mCnmt.getTitleVersion() << ")"<< std::endl; - std::cout << " Type: " << nn::hac::ContentMetaUtil::getContentMetaTypeAsString(mCnmt.getContentMetaType()) << " (" << std::dec << (uint32_t)mCnmt.getContentMetaType() << ")" << std::endl; - std::cout << " Attributes: 0x" << std::hex << mCnmt.getAttribute().to_ullong(); - if (mCnmt.getAttribute().any()) + const nn::hac::sContentMetaHeader* cnmt_hdr = (const nn::hac::sContentMetaHeader*)mCnmt.getBytes().data(); + fmt::print("[ContentMeta]\n"); + fmt::print(" TitleId: 0x{:016x}\n", mCnmt.getTitleId()); + fmt::print(" Version: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getTitleVersion()), mCnmt.getTitleVersion()); + fmt::print(" Type: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getContentMetaTypeAsString(mCnmt.getContentMetaType()), (uint32_t)mCnmt.getContentMetaType()); + fmt::print(" Attributes: 0x{:x}", *((byte_t*)&cnmt_hdr->attributes)); + if (mCnmt.getAttribute().size()) { std::vector attribute_list; - for (size_t flag = 0; flag < mCnmt.getAttribute().size(); flag++) + for (auto itr = mCnmt.getAttribute().begin(); itr != mCnmt.getAttribute().end(); itr++) { - - if (mCnmt.getAttribute().test(flag)) - { - attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(flag))); - } + attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(*itr))); } - std::cout << " ["; + fmt::print(" ["); for (auto itr = attribute_list.begin(); itr != attribute_list.end(); itr++) { - std::cout << *itr; + fmt::print("{:s}",*itr); if ((itr + 1) != attribute_list.end()) { - std::cout << ", "; + fmt::print(", "); } } - std::cout << "]"; + fmt::print("]"); } - std::cout << std::endl; + fmt::print("\n"); - std::cout << " StorageId: " << nn::hac::ContentMetaUtil::getStorageIdAsString(mCnmt.getStorageId()) << " (" << std::dec << (uint32_t)mCnmt.getStorageId() << ")" << std::endl; - std::cout << " ContentInstallType: " << nn::hac::ContentMetaUtil::getContentInstallTypeAsString(mCnmt.getContentInstallType()) << " (" << std::dec << (uint32_t)mCnmt.getContentInstallType() << ")" << std::endl; - std::cout << " RequiredDownloadSystemVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getRequiredDownloadSystemVersion()) << " (v" << mCnmt.getRequiredDownloadSystemVersion() << ")"<< std::endl; + fmt::print(" StorageId: {:s} ({:d})\n", nn::hac::ContentMetaUtil::getStorageIdAsString(mCnmt.getStorageId()), (uint32_t)mCnmt.getStorageId()); + fmt::print(" ContentInstallType: {:s} ({:d})\n", nn::hac::ContentMetaUtil::getContentInstallTypeAsString(mCnmt.getContentInstallType()),(uint32_t)mCnmt.getContentInstallType()); + fmt::print(" RequiredDownloadSystemVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getRequiredDownloadSystemVersion()), mCnmt.getRequiredDownloadSystemVersion()); switch(mCnmt.getContentMetaType()) { case (nn::hac::cnmt::ContentMetaType::Application): - std::cout << " ApplicationExtendedHeader:" << std::endl; - std::cout << " RequiredApplicationVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion()) << " (v" << std::dec << mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion() << ")"<< std::endl; - std::cout << " RequiredSystemVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion()) << " (v" << std::dec << mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion() << ")"<< std::endl; - std::cout << " PatchId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getApplicationMetaExtendedHeader().getPatchId() << std::endl; + fmt::print(" ApplicationExtendedHeader:\n"); + fmt::print(" RequiredApplicationVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion()), mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion()); + fmt::print(" RequiredSystemVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion()), mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion()); + fmt::print(" PatchId: 0x{:016x}\n", mCnmt.getApplicationMetaExtendedHeader().getPatchId()); break; case (nn::hac::cnmt::ContentMetaType::Patch): - std::cout << " PatchMetaExtendedHeader:" << std::endl; - std::cout << " RequiredSystemVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion()) << " (v" << std::dec << mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion() << ")"<< std::endl; - std::cout << " ApplicationId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getPatchMetaExtendedHeader().getApplicationId() << std::endl; + fmt::print(" PatchMetaExtendedHeader:\n"); + fmt::print(" RequiredSystemVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion()), mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion()); + fmt::print(" ApplicationId: 0x{:016x}\n", mCnmt.getPatchMetaExtendedHeader().getApplicationId()); break; case (nn::hac::cnmt::ContentMetaType::AddOnContent): - std::cout << " AddOnContentMetaExtendedHeader:" << std::endl; - std::cout << " RequiredApplicationVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion()) << " (v" << std::dec << mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion() << ")" << std::endl; - std::cout << " ApplicationId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getAddOnContentMetaExtendedHeader().getApplicationId() << std::endl; + fmt::print(" AddOnContentMetaExtendedHeader:\n"); + fmt::print(" RequiredApplicationVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion()), mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion()); + fmt::print(" ApplicationId: 0x{:016x}\n", mCnmt.getAddOnContentMetaExtendedHeader().getApplicationId()); break; case (nn::hac::cnmt::ContentMetaType::Delta): - std::cout << " DeltaMetaExtendedHeader:" << std::endl; - std::cout << " ApplicationId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getDeltaMetaExtendedHeader().getApplicationId() << std::endl; + fmt::print(" DeltaMetaExtendedHeader:\n"); + fmt::print(" ApplicationId: 0x{:016x}\n", mCnmt.getDeltaMetaExtendedHeader().getApplicationId()); break; default: break; } if (mCnmt.getContentInfo().size() > 0) { - printf(" ContentInfo:\n"); + fmt::print(" ContentInfo:\n"); for (size_t i = 0; i < mCnmt.getContentInfo().size(); i++) { const nn::hac::ContentInfo& info = mCnmt.getContentInfo()[i]; - std::cout << " " << std::dec << i << std::endl; - std::cout << " Type: " << nn::hac::ContentMetaUtil::getContentTypeAsString(info.getContentType()) << " (" << std::dec << (uint32_t)info.getContentType() << ")" << std::endl; - std::cout << " Id: " << fnd::SimpleTextOutput::arrayToString(info.getContentId().data(), info.getContentId().size(), false, "") << std::endl; - std::cout << " Size: 0x" << std::hex << info.getContentSize() << std::endl; - std::cout << " Hash: " << fnd::SimpleTextOutput::arrayToString(info.getContentHash().bytes, sizeof(info.getContentHash()), false, "") << std::endl; + fmt::print(" {:d}\n", i); + fmt::print(" Type: {:s} ({:d})\n", nn::hac::ContentMetaUtil::getContentTypeAsString(info.getContentType()), (uint32_t)info.getContentType()); + fmt::print(" Id: {:s}\n", tc::cli::FormatUtil::formatBytesAsString(info.getContentId().data(), info.getContentId().size(), false, "")); + fmt::print(" Size: 0x{:x}\n", info.getContentSize()); + fmt::print(" Hash: {:s}\n", tc::cli::FormatUtil::formatBytesAsString(info.getContentHash().data(), info.getContentHash().size(), false, "")); } } if (mCnmt.getContentMetaInfo().size() > 0) { - std::cout << " ContentMetaInfo:" << std::endl; + fmt::print(" ContentMetaInfo:\n"); displayContentMetaInfoList(mCnmt.getContentMetaInfo(), " "); } @@ -139,31 +143,31 @@ void nstool::CnmtProcess::displayCnmt() if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::Patch && mCnmt.getPatchMetaExtendedHeader().getExtendedDataSize() != 0) { // this is stubbed as the raw output is for development purposes - //std::cout << " PatchMetaExtendedData:" << std::endl; - //fnd::SimpleTextOutput::hxdStyleDump(mCnmt.getPatchMetaExtendedData().data(), mCnmt.getPatchMetaExtendedData().size()); + //fmt::print(" PatchMetaExtendedData:\n"); + //tc::cli::FormatUtil::formatBytesAsHxdHexString(mCnmt.getPatchMetaExtendedData().data(), mCnmt.getPatchMetaExtendedData().size()); } else if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::Delta && mCnmt.getDeltaMetaExtendedHeader().getExtendedDataSize() != 0) { // this is stubbed as the raw output is for development purposes - //std::cout << " DeltaMetaExtendedData:" << std::endl; - //fnd::SimpleTextOutput::hxdStyleDump(mCnmt.getDeltaMetaExtendedData().data(), mCnmt.getDeltaMetaExtendedData().size()); + //fmt::print(" DeltaMetaExtendedData:\n"); + //tc::cli::FormatUtil::formatBytesAsHxdHexString(mCnmt.getDeltaMetaExtendedData().data(), mCnmt.getDeltaMetaExtendedData().size()); } else if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::SystemUpdate && mCnmt.getSystemUpdateMetaExtendedHeader().getExtendedDataSize() != 0) { - std::cout << " SystemUpdateMetaExtendedData:" << std::endl; - std::cout << " FormatVersion: " << std::dec << mCnmt.getSystemUpdateMetaExtendedData().getFormatVersion() << std::endl; - std::cout << " FirmwareVariation:" << std::endl; + fmt::print(" SystemUpdateMetaExtendedData:\n"); + fmt::print(" FormatVersion: {:d}\n", mCnmt.getSystemUpdateMetaExtendedData().getFormatVersion()); + fmt::print(" FirmwareVariation:\n"); auto variation_info = mCnmt.getSystemUpdateMetaExtendedData().getFirmwareVariationInfo(); for (size_t i = 0; i < mCnmt.getSystemUpdateMetaExtendedData().getFirmwareVariationInfo().size(); i++) { - std::cout << " " << std::dec << i << std::endl; - std::cout << " FirmwareVariationId: 0x" << std::hex << variation_info[i].variation_id << std::endl; + fmt::print(" {:d}\n", i); + fmt::print(" FirmwareVariationId: 0x{:x}\n", variation_info[i].variation_id); if (mCnmt.getSystemUpdateMetaExtendedData().getFormatVersion() == 2) { - std::cout << " ReferToBase: " << std::boolalpha << variation_info[i].meta.empty() << std::endl; + fmt::print(" ReferToBase: {}\n", variation_info[i].meta.empty()); if (variation_info[i].meta.empty() == false) { - std::cout << " ContentMeta:" << std::endl; + fmt::print(" ContentMeta:\n"); displayContentMetaInfoList(variation_info[i].meta, " "); } } @@ -172,48 +176,45 @@ void nstool::CnmtProcess::displayCnmt() - std::cout << " Digest: " << fnd::SimpleTextOutput::arrayToString(mCnmt.getDigest().data(), mCnmt.getDigest().size(), false, "") << std::endl; + fmt::print(" Digest: {:s}\n", tc::cli::FormatUtil::formatBytesAsString(mCnmt.getDigest().data(), mCnmt.getDigest().size(), false, "")); } void nstool::CnmtProcess::displayContentMetaInfo(const nn::hac::ContentMetaInfo& content_meta_info, const std::string& prefix) { - std::cout << prefix << "Id: 0x" << std::hex << std::setw(16) << std::setfill('0') << content_meta_info.getTitleId() << std::endl; - std::cout << prefix << "Version: " << nn::hac::ContentMetaUtil::getVersionAsString(content_meta_info.getTitleVersion()) << " (v" << std::dec << content_meta_info.getTitleVersion() << ")"<< std::endl; - std::cout << prefix << "Type: " << nn::hac::ContentMetaUtil::getContentMetaTypeAsString(content_meta_info.getContentMetaType()) << " (" << std::dec << (uint32_t)content_meta_info.getContentMetaType() << ")" << std::endl; - std::cout << prefix << "Attributes: 0x" << std::hex << content_meta_info.getAttribute().to_ullong(); - if (content_meta_info.getAttribute().any()) + const nn::hac::sContentMetaInfo* content_meta_info_raw = (const nn::hac::sContentMetaInfo*)content_meta_info.getBytes().data(); + fmt::print("{:s}Id: 0x{:016x}\n", prefix, content_meta_info.getTitleId()); + fmt::print("{:s}Version: {:s} (v{:d})\n", prefix, nn::hac::ContentMetaUtil::getVersionAsString(content_meta_info.getTitleVersion()), content_meta_info.getTitleVersion()); + fmt::print("{:s}Type: {:s} ({:d})\n", prefix, nn::hac::ContentMetaUtil::getContentMetaTypeAsString(content_meta_info.getContentMetaType()), (uint32_t)content_meta_info.getContentMetaType()); + fmt::print("{:s}Attributes: 0x{:x}", prefix, *((byte_t*)&content_meta_info_raw->attributes) ); + if (content_meta_info.getAttribute().size()) { std::vector attribute_list; - for (size_t flag = 0; flag < content_meta_info.getAttribute().size(); flag++) + for (auto itr = content_meta_info.getAttribute().begin(); itr != content_meta_info.getAttribute().end(); itr++) { - - if (content_meta_info.getAttribute().test(flag)) - { - attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(flag))); - } + attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(*itr))); } - std::cout << " ["; + fmt::print(" ["); for (auto itr = attribute_list.begin(); itr != attribute_list.end(); itr++) { - std::cout << *itr; + fmt::print("{:s}",*itr); if ((itr + 1) != attribute_list.end()) { - std::cout << ", "; + fmt::print(", "); } } - std::cout << "]"; + fmt::print("]"); } + fmt::print("\n"); } void nstool::CnmtProcess::displayContentMetaInfoList(const std::vector& content_meta_info_list, const std::string& prefix) { for (size_t i = 0; i < content_meta_info_list.size(); i++) - { - const nn::hac::ContentMetaInfo& info = mCnmt.getContentMetaInfo()[i]; - std::cout << prefix << std::dec << i << std::endl; - displayContentMetaInfo(info, prefix + " "); - std::cout << std::endl; - } + { + const nn::hac::ContentMetaInfo& info = mCnmt.getContentMetaInfo()[i]; + fmt::print("{:s}{:d}\n", i); + displayContentMetaInfo(info, prefix + " "); + } } \ No newline at end of file diff --git a/src/CnmtProcess.h b/src/CnmtProcess.h index 99c9462..bcb407d 100644 --- a/src/CnmtProcess.h +++ b/src/CnmtProcess.h @@ -19,7 +19,7 @@ public: const nn::hac::ContentMeta& getContentMeta() const; private: - const std::string kModuleName = "CnmtProcess"; + std::string mModuleName; std::shared_ptr mFile; CliOutputMode mCliOutputMode;