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#pragma once
2
3#include <array>
4
5#include "../PackFile.h"
6
7namespace vpkpp {
8
9constexpr uint32_t VPK_SIGNATURE = 0x55aa1234;
10constexpr uint16_t VPK_DIR_INDEX = 0x7fff;
11constexpr uint16_t VPK_ENTRY_TERM = 0xffff;
12constexpr std::string_view VPK_DIR_SUFFIX = "_dir";
13constexpr std::string_view VPK_EXTENSION = ".vpk";
14
15constexpr std::string_view VPK_KEYPAIR_PUBLIC_KEY_TEMPLATE = "public_key\n{\n\ttype \"rsa\"\n\trsa_public_key \"%s\"\n}\n";
16constexpr 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";
17
19constexpr uint16_t VPK_MAX_PRELOAD_BYTES = 1024;
20
22constexpr uint32_t VPK_DEFAULT_CHUNK_SIZE = 200 * 1024 * 1024;
23
24class VPK : public PackFile {
25protected:
26#pragma pack(push, 1)
27 struct Header1 {
28 uint32_t signature;
29 uint32_t version;
30 uint32_t treeSize;
31 };
32
33 struct Header2 {
38 };
39
40 struct Footer2 {
41 std::array<std::byte, 16> treeChecksum{};
42 std::array<std::byte, 16> md5EntriesChecksum{};
43 std::array<std::byte, 16> wholeFileChecksum{};
44 std::vector<std::byte> publicKey{};
45 std::vector<std::byte> signature{};
46 };
47
48 struct MD5Entry {
50 uint32_t archiveIndex;
52 uint32_t offset;
54 uint32_t length;
56 std::array<std::byte, 16> checksum;
57 };
58#pragma pack(pop)
59
60 struct FreedChunk {
61 uint64_t offset;
62 uint64_t length;
63 uint32_t archiveIndex;
64 };
65
66public:
68 static std::unique_ptr<PackFile> create(const std::string& path, uint32_t version = 2);
69
71 [[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr);
72
73 static constexpr inline std::string_view GUID = "98148F7C8701469CB2D8F8620FD738A3";
74
75 [[nodiscard]] constexpr std::string_view getGUID() const override {
76 return VPK::GUID;
77 }
78
79 [[nodiscard]] constexpr bool hasEntryChecksums() const override {
80 return true;
81 }
82
83 [[nodiscard]] std::vector<std::string> verifyEntryChecksums() const override;
84
85 [[nodiscard]] bool hasPackFileChecksum() const override;
86
87 [[nodiscard]] bool verifyPackFileChecksum() const override;
88
89 [[nodiscard]] bool hasPackFileSignature() const override;
90
91 [[nodiscard]] bool verifyPackFileSignature() const override;
92
93 [[nodiscard]] std::optional<std::vector<std::byte>> readEntry(const std::string& path_) const override;
94
95 bool removeEntry(const std::string& filename_) override;
96
97 std::size_t removeDirectory(const std::string& dirName_) override;
98
99 bool bake(const std::string& outputDir_ /*= ""*/, BakeOptions options /*= {}*/, const EntryCallback& callback /*= nullptr*/) override;
100
101 [[nodiscard]] std::string getTruncatedFilestem() const override;
102
103 [[nodiscard]] Attribute getSupportedEntryAttributes() const override;
104
105 [[nodiscard]] explicit operator std::string() const override;
106
110 static bool generateKeyPairFiles(const std::string& name);
111
113 bool sign(const std::string& filename_);
114
116 bool sign(const std::vector<std::byte>& privateKey, const std::vector<std::byte>& publicKey);
117
119 [[nodiscard]] uint32_t getVersion() const;
120
122 void setVersion(uint32_t version);
123
125 [[nodiscard]] uint32_t getChunkSize() const;
126
128 void setChunkSize(uint32_t newChunkSize);
129
130protected:
131 using PackFile::PackFile;
132
133 [[nodiscard]] static std::unique_ptr<PackFile> openInternal(const std::string& path, const EntryCallback& callback = nullptr);
134
135 void addEntryInternal(Entry& entry, const std::string& path, std::vector<std::byte>& buffer, EntryOptions options) override;
136
137 [[nodiscard]] bool hasExtendedHeader() const;
138
139 [[nodiscard]] bool hasCompression() const;
140
141 [[nodiscard]] uint32_t getHeaderLength() const;
142
143 int32_t numArchives = -1;
146
147 std::vector<FreedChunk> freedChunks;
148
149 Header1 header1{}; // Present in all VPK versions
150 Header2 header2{}; // Present in VPK v2
151 Footer2 footer2{}; // Present in VPK v2
152
153 std::vector<MD5Entry> md5Entries;
154
155private:
157};
158
159} // namespace vpkpp
#define VPKPP_REGISTER_PACKFILE_OPEN(extension, function)
Definition: PackFile.h:242
This class represents the metadata that a file has inside a PackFile.
Definition: Entry.h:14
EntryCallbackBase< void > EntryCallback
Definition: PackFile.h:30
PackFile(const PackFile &other)=delete
Definition: VPK.h:24
Footer2 footer2
Definition: VPK.h:151
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:144
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:79
std::vector< FreedChunk > freedChunks
Definition: VPK.h:147
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:73
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:143
Header2 header2
Definition: VPK.h:150
constexpr std::string_view getGUID() const override
Get the GUID corresponding to the pack file type.
Definition: VPK.h:75
std::string getTruncatedFilestem() const override
/home/user/pak01_dir.vpk -> pak01
Definition: VPK.cpp:785
std::vector< MD5Entry > md5Entries
Definition: VPK.h:153
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
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:145
Header1 header1
Definition: VPK.h:149
Definition: Attribute.h:5
constexpr uint32_t VPK_SIGNATURE
Definition: VPK.h:9
constexpr std::string_view VPK_DIR_SUFFIX
Definition: VPK.h:12
constexpr std::string_view VPK_KEYPAIR_PUBLIC_KEY_TEMPLATE
Definition: VPK.h:15
Attribute
Definition: Attribute.h:7
constexpr uint16_t VPK_ENTRY_TERM
Definition: VPK.h:11
constexpr std::string_view VPK_EXTENSION
Definition: VPK.h:13
constexpr std::string_view VPK_KEYPAIR_PRIVATE_KEY_TEMPLATE
Definition: VPK.h:16
constexpr uint32_t VPK_DEFAULT_CHUNK_SIZE
Chunk size in bytes (default is 200mb)
Definition: VPK.h:22
constexpr uint16_t VPK_DIR_INDEX
Definition: VPK.h:10
constexpr uint16_t VPK_MAX_PRELOAD_BYTES
Maximum preload data size in bytes.
Definition: VPK.h:19
std::array< std::byte, 16 > treeChecksum
Definition: VPK.h:41
std::array< std::byte, 16 > wholeFileChecksum
Definition: VPK.h:43
std::vector< std::byte > publicKey
Definition: VPK.h:44
std::array< std::byte, 16 > md5EntriesChecksum
Definition: VPK.h:42
std::vector< std::byte > signature
Definition: VPK.h:45
uint32_t archiveIndex
Definition: VPK.h:63
uint64_t length
Definition: VPK.h:62
uint64_t offset
Definition: VPK.h:61
uint32_t treeSize
Definition: VPK.h:30
uint32_t signature
Definition: VPK.h:28
uint32_t version
Definition: VPK.h:29
uint32_t otherMD5SectionSize
Definition: VPK.h:36
uint32_t signatureSectionSize
Definition: VPK.h:37
uint32_t archiveMD5SectionSize
Definition: VPK.h:35
uint32_t fileDataSectionSize
Definition: VPK.h:34
uint32_t length
The length in bytes.
Definition: VPK.h:54
std::array< std::byte, 16 > checksum
The CRC32 checksum of this entry.
Definition: VPK.h:56
uint32_t archiveIndex
The archive index of the file.
Definition: VPK.h:50
uint32_t offset
The offset in the archive.
Definition: VPK.h:52