Skip to content

Commit

Permalink
Implemented pck path padding null byte handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyyrylainen committed May 11, 2020
1 parent b98ad13 commit ab37ddd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.10)
project(GodotPckTool)

set(GODOT_PCK_TOOL_VERSION_MAJOR 1)
set(GODOT_PCK_TOOL_VERSION_MINOR 2)
set(GODOT_PCK_TOOL_VERSION_MINOR 3)

set(GODOT_PCK_TOOL_VERSION_STR "${GODOT_PCK_TOOL_VERSION_MAJOR}.${GODOT_PCK_TOOL_VERSION_MINOR}")

Expand Down
16 changes: 15 additions & 1 deletion src/pck/PckFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ bool PckFile::Load()

File->read(entry.Path.data(), pathLength);

// Remove trailing null bytes
while(!entry.Path.empty() && entry.Path.back() == '\0')
entry.Path.pop_back();

entry.Offset = Read64();
entry.Size = Read64();

Expand Down Expand Up @@ -118,9 +122,19 @@ bool PckFile::Save()
// First write blank file entries as placeholders
for(const auto& [_, entry] : Contents) {

Write32(entry.Path.size());
const size_t pathToWriteSize =
entry.Path.size() + (entry.Path.size() % PadPathsToMultipleWithNULLS);
const size_t padding = pathToWriteSize - entry.Path.size();

Write32(pathToWriteSize);
File->write(entry.Path.data(), entry.Path.size());

// Padding null bytes
for(size_t i = 0; i < padding; ++i) {
char null = '\0';
File->write(&null, 1);
}

continueWriteIndex[&entry] = File->tellg();
// No offset yet
Write64(0);
Expand Down
3 changes: 3 additions & 0 deletions src/pck/PckFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class PckFile {
uint32_t MinorGodotVersion = 0;
uint32_t PatchGodotVersion = 0;

//! Add trailing null bytes to the length of a path until it is a multiple of this size
size_t PadPathsToMultipleWithNULLS = 4;

std::map<std::string, ContainedFile> Contents;
};

Expand Down

0 comments on commit ab37ddd

Please sign in to comment.