From d85a33ba467244be677a27313ed23fd87b5a2954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henri=20Hyyryl=C3=A4inen?= Date: Wed, 20 Mar 2024 14:33:13 +0200 Subject: [PATCH] Added option to print pck file content hashes --- src/PckTool.cpp | 2 +- src/PckTool.h | 1 + src/main.cpp | 8 +++++++- src/pck/PckFile.cpp | 21 ++++++++++++++++++--- src/pck/PckFile.h | 2 +- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/PckTool.cpp b/src/PckTool.cpp index 1eae0c6..76d4701 100644 --- a/src/PckTool.cpp +++ b/src/PckTool.cpp @@ -24,7 +24,7 @@ int PckTool::Run() std::cout << "Contents of '" << Opts.Pack << "':\n"; - pck->PrintFileList(); + pck->PrintFileList(Opts.PrintHashes); std::cout << "end of contents.\n"; return 0; diff --git a/src/PckTool.h b/src/PckTool.h index 5d2fd34..4eb386f 100644 --- a/src/PckTool.h +++ b/src/PckTool.h @@ -41,6 +41,7 @@ class PckTool { FileFilter Filter; bool ReducedVerbosity; + bool PrintHashes; }; public: diff --git a/src/main.cpp b/src/main.cpp index 465085d..fb36f94 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -82,6 +82,7 @@ int main(int argc, char* argv[]) "matching this", cxxopts::value>()) ("q,quieter", "Don't output all processed files to keep output more compact") + ("print-hashes", "Print hashes of contained files in the .pck") ("v,version", "Print version and quit") ("h,help", "Print help and quit") ; @@ -119,6 +120,7 @@ int main(int argc, char* argv[]) nlohmann::json fileCommands; pcktool::FileFilter filter; bool reducedVerbosity = false; + bool printHashes = false; if(result.count("file")) { files = result["file"].as(); @@ -170,6 +172,10 @@ int main(int argc, char* argv[]) reducedVerbosity = true; } + if(result.count("print-hashes")) { + printHashes = true; + } + action = result["action"].as(); try { @@ -212,7 +218,7 @@ int main(int argc, char* argv[]) } auto tool = pcktool::PckTool({pack, action, files, output, removePrefix, godotMajor, - godotMinor, godotPatch, fileCommands, filter, reducedVerbosity}); + godotMinor, godotPatch, fileCommands, filter, reducedVerbosity, printHashes}); return tool.Run(); } diff --git a/src/pck/PckFile.cpp b/src/pck/PckFile.cpp index 4ef067a..6157081 100644 --- a/src/pck/PckFile.cpp +++ b/src/pck/PckFile.cpp @@ -1,14 +1,17 @@ // ------------------------------------ // #include "PckFile.h" +#include #include #include #include #include "md5.h" -using namespace pcktool; +static_assert(MD5_SIZE == 16, "MD5 size changed"); // ------------------------------------ // +using namespace pcktool; + PckFile::PckFile(std::string path) : Path(std::move(path)) {} // ------------------------------------ // bool PckFile::Load() @@ -239,7 +242,7 @@ bool PckFile::Save() // Update MD5 // The md5 library assumes the write target size here - static_assert(sizeof(entry.MD5) == 16); + static_assert(sizeof(entry.MD5) == MD5_SIZE); md5::md5_t(data.data(), data.size(), entry.MD5.data()); PadToAlignment(); @@ -438,13 +441,25 @@ bool PckFile::Extract(const std::string& outputPrefix, bool printExtracted) return true; } // ------------------------------------ // -void PckFile::PrintFileList(bool includeSize /*= true*/) +void PckFile::PrintFileList(bool printHashes, bool includeSize /*= true*/) { + char signatureTempBuffer[MD5_STRING_SIZE]; + for(const auto& [path, entry] : Contents) { std::cout << path; if(includeSize) std::cout << " size: " << entry.Size; + // TODO: flag to also verify the hashes? + if(printHashes) { + static_assert(sizeof(entry.MD5) == MD5_SIZE); + md5::sig_to_string(entry.MD5.data(), signatureTempBuffer, MD5_STRING_SIZE); + + std::cout << " md5: " + << std::string_view( + signatureTempBuffer, std::strlen(signatureTempBuffer)); + } + std::cout << "\n"; } } diff --git a/src/pck/PckFile.h b/src/pck/PckFile.h index 5af8325..dc3f9cf 100644 --- a/src/pck/PckFile.h +++ b/src/pck/PckFile.h @@ -54,7 +54,7 @@ class PckFile { //! \brief Extracts the read contents to the outputPrefix bool Extract(const std::string& outputPrefix, bool printExtracted); - void PrintFileList(bool includeSize = true); + void PrintFileList(bool printHashes, bool includeSize = true); void AddFile(ContainedFile&& file);