Skip to content

Commit 54b1efb

Browse files
vpkpp: write PAK directory at end of file, ensure we add null terminator to paths
1 parent c993221 commit 54b1efb

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/vpkpp/format/PAK.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ReSharper disable CppRedundantQualifier
2+
13
#include <vpkpp/format/PAK.h>
24

35
#include <filesystem>
@@ -129,27 +131,28 @@ bool PAK::bake(const std::string& outputDir_, BakeOptions options, const EntryCa
129131
stream.seek_out(0);
130132

131133
// Signature
132-
stream.write(this->getSignature());
134+
stream.write<uint32_t>(this->getSignature());
135+
136+
// Offset and size of directory
137+
static constexpr auto HEADER_OFFSET = sizeof(uint32_t) * 3;
138+
stream
139+
.write<uint32_t>(HEADER_OFFSET + fileData.size())
140+
.write<uint32_t>(entriesToBake.size() * (sizeof(uint32_t) * 2 + this->getFilenameLength()));
133141

134-
// Index and size of directory
135-
static constexpr uint32_t DIRECTORY_INDEX = sizeof(PAK_SIGNATURE) + sizeof(uint32_t) * 2;
136-
stream.write(DIRECTORY_INDEX);
137-
const uint32_t directorySize = entriesToBake.size() * (sizeof(uint32_t) * 2 + this->getFilenameLength());
138-
stream.write(directorySize);
142+
// File data
143+
stream.write(fileData);
139144

140145
// Directory
141146
for (const auto& [path, entry] : entriesToBake) {
142-
stream.write(path, false, this->getFilenameLength());
143-
stream.write(static_cast<uint32_t>(entry->offset + DIRECTORY_INDEX + directorySize));
144-
stream.write(static_cast<uint32_t>(entry->length));
147+
stream
148+
.write(path, true, this->getFilenameLength())
149+
.write<uint32_t>(entry->offset + HEADER_OFFSET)
150+
.write<uint32_t>(entry->length);
145151

146152
if (callback) {
147153
callback(path, *entry);
148154
}
149155
}
150-
151-
// File data
152-
stream.write(fileData);
153156
}
154157

155158
// Clean up

0 commit comments

Comments
 (0)