SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
VPK.h
Go to the documentation of this file.
1// ReSharper disable CppRedundantQualifier
2
3#pragma once
4
5#include <array>
6
7#include "../PackFile.h"
8
9namespace vpkpp {
10
11constexpr uint32_t VPK_SIGNATURE = 0x55aa1234;
12constexpr uint16_t VPK_DIR_INDEX = 0x7fff;
13constexpr uint16_t VPK_ENTRY_TERM = 0xffff;
14constexpr std::string_view VPK_DIR_SUFFIX = "_dir";
15constexpr std::string_view VPK_EXTENSION = ".vpk";
16
17constexpr std::string_view VPK_KEYPAIR_PUBLIC_KEY_TEMPLATE = "public_key\n{\n\ttype \"rsa\"\n\trsa_public_key \"%s\"\n}\n";
18constexpr std::string_view VPK_KEYPAIR_PRIVATE_KEY_TEMPLATE = "private_key\n{\n\ttype \"rsa\"\n\trsa_private_key \"%s\"\n\tprivate_key_encrypted 0\n\tpublic_key\n\t{\n\t\ttype \"rsa\"\n\t\trsa_public_key \"%s\"\n\t}\n}\n";
19
21constexpr uint16_t VPK_MAX_PRELOAD_BYTES = 1024;
22
24constexpr uint32_t VPK_DEFAULT_CHUNK_SIZE = 200 * 1024 * 1024;
25
26class VPK : public PackFile {
27protected:
28#pragma pack(push, 1)
29 struct Header1 {
30 uint32_t signature;
31 uint32_t version;
32 uint32_t treeSize;
33 };
34
41
42 struct Footer2 {
43 std::array<std::byte, 16> treeChecksum{};
44 std::array<std::byte, 16> md5EntriesChecksum{};
45 std::array<std::byte, 16> wholeFileChecksum{};
46 std::vector<std::byte> publicKey{};
47 std::vector<std::byte> signature{};
48 };
49
50 struct MD5Entry {
52 uint32_t archiveIndex;
54 uint32_t offset;
56 uint32_t length;
58 std::array<std::byte, 16> checksum;
59 };
60#pragma pack(pop)
61
62 struct FreedChunk {
63 uint64_t offset;
64 uint64_t length;
65 uint32_t archiveIndex;
66 };
67
68public:
70 static std::unique_ptr<PackFile> create(const std::string& path, uint32_t version = 2);
71
73 [[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr);
74
75 static constexpr std::string_view GUID = "98148F7C8701469CB2D8F8620FD738A3";
76
77 [[nodiscard]] constexpr std::string_view getGUID() const override {
78 return VPK::GUID;
79 }
80
81 [[nodiscard]] constexpr bool hasEntryChecksums() const override {
82 return true;
83 }
84
85 [[nodiscard]] std::vector<std::string> verifyEntryChecksums() const override;
86
87 [[nodiscard]] bool hasPackFileChecksum() const override;
88
89 [[nodiscard]] bool verifyPackFileChecksum() const override;
90
91 [[nodiscard]] bool hasPackFileSignature() const override;
92
93 [[nodiscard]] bool verifyPackFileSignature() const override;
94
95 [[nodiscard]] std::optional<std::vector<std::byte>> readEntry(const std::string& path_) const override;
96
97 bool removeEntry(const std::string& filename_) override;
98
99 std::size_t removeDirectory(const std::string& dirName_) override;
100
101 bool bake(const std::string& outputDir_ /*= ""*/, BakeOptions options /*= {}*/, const EntryCallback& callback /*= nullptr*/) override;
102
103 [[nodiscard]] std::string getTruncatedFilestem() const override;
104
105 [[nodiscard]] Attribute getSupportedEntryAttributes() const override;
106
107 [[nodiscard]] explicit operator std::string() const override;
108
112 static bool generateKeyPairFiles(const std::string& name);
113
115 bool sign(const std::string& filename_);
116
118 bool sign(const std::vector<std::byte>& privateKey, const std::vector<std::byte>& publicKey);
119
121 [[nodiscard]] uint32_t getVersion() const;
122
124 void setVersion(uint32_t version);
125
127 [[nodiscard]] uint32_t getChunkSize() const;
128
130 void setChunkSize(uint32_t newChunkSize);
131
132protected:
133 using PackFile::PackFile;
134
135 [[nodiscard]] static std::unique_ptr<PackFile> openInternal(const std::string& path, const EntryCallback& callback = nullptr);
136
137 void addEntryInternal(Entry& entry, const std::string& path, std::vector<std::byte>& buffer, EntryOptions options) override;
138
139 [[nodiscard]] bool hasExtendedHeader() const;
140
141 [[nodiscard]] bool hasCompression() const;
142
143 [[nodiscard]] uint32_t getHeaderLength() const;
144
145 int32_t numArchives = -1;
148
149 std::vector<FreedChunk> freedChunks;
150
151 Header1 header1{}; // Present in all VPK versions
152 Header2 header2{}; // Present in VPK v2
153 Footer2 footer2{}; // Present in VPK v2
154
155 std::vector<MD5Entry> md5Entries;
156
157private:
159};
160
161} // namespace vpkpp
#define VPKPP_REGISTER_PACKFILE_OPEN(extension, function)
Definition PackFile.h:255
This class represents the metadata that a file has inside a PackFile.
Definition Entry.h:14
EntryCallbackBase< void > EntryCallback
Definition PackFile.h:38
PackFile(const PackFile &other)=delete
Footer2 footer2
Definition VPK.h:153
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
Definition VPK.cpp:794
static std::unique_ptr< PackFile > create(const std::string &path, uint32_t version=2)
Create a new directory VPK file - should end in "_dir.vpk"! This is not enforced but STRONGLY recomme...
Definition VPK.cpp:52
std::size_t removeDirectory(const std::string &dirName_) override
Remove a directory.
Definition VPK.cpp:467
void setChunkSize(uint32_t newChunkSize)
Set the VPK chunk size in bytes (size of generated archives when baking)
Definition VPK.cpp:908
uint32_t getHeaderLength() const
Definition VPK.cpp:920
bool hasCompression() const
Definition VPK.cpp:916
bool verifyPackFileSignature() const override
Verify the file signature, returns true on success Will return true if there is no signature ability ...
Definition VPK.cpp:303
uint32_t getChunkSize() const
Get the VPK chunk size in bytes (size of generated archives when baking)
Definition VPK.cpp:904
std::vector< std::string > verifyEntryChecksums() const override
Verify the checksums of each file, if a file fails the check its path will be added to the vector If ...
Definition VPK.cpp:259
uint32_t getVersion() const
Returns 1 for v1, 2 for v2.
Definition VPK.cpp:887
uint32_t currentlyFilledChunkSize
Definition VPK.h:146
bool hasPackFileSignature() const override
Returns true if the file is signed.
Definition VPK.cpp:293
bool hasExtendedHeader() const
Definition VPK.cpp:912
static bool generateKeyPairFiles(const std::string &name)
Generate keypair files, which can be used to sign a VPK Input is a truncated file path,...
Definition VPK.cpp:804
constexpr bool hasEntryChecksums() const override
Returns true if the format has a checksum for each entry.
Definition VPK.h:81
std::vector< FreedChunk > freedChunks
Definition VPK.h:149
bool hasPackFileChecksum() const override
Returns true if the entire file has a checksum.
Definition VPK.cpp:263
static constexpr std::string_view GUID
Definition VPK.h:75
bool verifyPackFileChecksum() const override
Verify the checksum of the entire file, returns true on success Will return true if there is no check...
Definition VPK.cpp:267
void setVersion(uint32_t version)
Change the version of the VPK. Valid values are 1 and 2.
Definition VPK.cpp:891
std::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
Definition VPK.cpp:324
bool sign(const std::string &filename_)
Sign the VPK with the given private key KeyValues file. (See below comment)
Definition VPK.cpp:835
bool removeEntry(const std::string &filename_) override
Remove an entry.
Definition VPK.cpp:459
int32_t numArchives
Definition VPK.h:145
Header2 header2
Definition VPK.h:152
constexpr std::string_view getGUID() const override
Get the GUID corresponding to the pack file type.
Definition VPK.h:77
std::string getTruncatedFilestem() const override
/home/user/pak01_dir.vpk -> pak01
Definition VPK.cpp:785
std::vector< MD5Entry > md5Entries
Definition VPK.h:155
bool bake(const std::string &outputDir_, BakeOptions options, const EntryCallback &callback) override
If output folder is an empty string, it will overwrite the original.
Definition VPK.cpp:480
void addEntryInternal(Entry &entry, const std::string &path, std::vector< std::byte > &buffer, EntryOptions options) override
Definition VPK.cpp:403
PackFile(const PackFile &other)=delete
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr)
Open a VPK file.
Definition VPK.cpp:81
static std::unique_ptr< PackFile > openInternal(const std::string &path, const EntryCallback &callback=nullptr)
Definition VPK.cpp:99
uint32_t chunkSize
Definition VPK.h:147
Header1 header1
Definition VPK.h:151
constexpr uint32_t VPK_SIGNATURE
Definition VPK.h:11
constexpr std::string_view VPK_DIR_SUFFIX
Definition VPK.h:14
constexpr std::string_view VPK_KEYPAIR_PUBLIC_KEY_TEMPLATE
Definition VPK.h:17
Attribute
Definition Attribute.h:7
constexpr uint16_t VPK_ENTRY_TERM
Definition VPK.h:13
constexpr std::string_view VPK_EXTENSION
Definition VPK.h:15
constexpr std::string_view VPK_KEYPAIR_PRIVATE_KEY_TEMPLATE
Definition VPK.h:18
constexpr uint32_t VPK_DEFAULT_CHUNK_SIZE
Chunk size in bytes (default is 200mb)
Definition VPK.h:24
constexpr uint16_t VPK_DIR_INDEX
Definition VPK.h:12
constexpr uint16_t VPK_MAX_PRELOAD_BYTES
Maximum preload data size in bytes.
Definition VPK.h:21
std::array< std::byte, 16 > treeChecksum
Definition VPK.h:43
std::array< std::byte, 16 > wholeFileChecksum
Definition VPK.h:45
std::vector< std::byte > publicKey
Definition VPK.h:46
std::array< std::byte, 16 > md5EntriesChecksum
Definition VPK.h:44
std::vector< std::byte > signature
Definition VPK.h:47
uint32_t archiveIndex
Definition VPK.h:65
uint64_t length
Definition VPK.h:64
uint64_t offset
Definition VPK.h:63
uint32_t treeSize
Definition VPK.h:32
uint32_t signature
Definition VPK.h:30
uint32_t version
Definition VPK.h:31
uint32_t otherMD5SectionSize
Definition VPK.h:38
uint32_t signatureSectionSize
Definition VPK.h:39
uint32_t archiveMD5SectionSize
Definition VPK.h:37
uint32_t fileDataSectionSize
Definition VPK.h:36
uint32_t length
The length in bytes.
Definition VPK.h:56
std::array< std::byte, 16 > checksum
The CRC32 checksum of this entry.
Definition VPK.h:58
uint32_t archiveIndex
The archive index of the file.
Definition VPK.h:52
uint32_t offset
The offset in the archive.
Definition VPK.h:54