Skip to content

Commit

Permalink
Added option to print pck file content hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyyrylainen committed Mar 20, 2024
1 parent 4520f1c commit d85a33b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/PckTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/PckTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class PckTool {
FileFilter Filter;

bool ReducedVerbosity;
bool PrintHashes;
};

public:
Expand Down
8 changes: 7 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ int main(int argc, char* argv[])
"matching this",
cxxopts::value<std::vector<std::string>>())
("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")
;
Expand Down Expand Up @@ -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<decltype(files)>();
Expand Down Expand Up @@ -170,6 +172,10 @@ int main(int argc, char* argv[])
reducedVerbosity = true;
}

if(result.count("print-hashes")) {
printHashes = true;
}

action = result["action"].as<std::string>();

try {
Expand Down Expand Up @@ -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();
}
21 changes: 18 additions & 3 deletions src/pck/PckFile.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// ------------------------------------ //
#include "PckFile.h"

#include <cstring>
#include <filesystem>
#include <iostream>
#include <utility>

#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()
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pck/PckFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d85a33b

Please sign in to comment.