Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Read Windows Installer packages out of setup bundles

Leonardo Lobato55a40a19ddaa
diff --git a/cmake/HomebrewUI.cmake b/cmake/HomebrewUI.cmake
index 2793e9a..aeb1208 100644
--- a/cmake/HomebrewUI.cmake
+++ b/cmake/HomebrewUI.cmake
@@ -70,6 +70,7 @@ add_library(playden_steam_cm_codec STATIC
"${CMAKE_SOURCE_DIR}/src/runtime/game_settings.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/install_script.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/msi_package.cpp"
+ "${CMAKE_SOURCE_DIR}/src/runtime/burn_bundle.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/redist_install.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/game_probe.cpp")
target_include_directories(playden_steam_cm_codec PUBLIC "${CMAKE_SOURCE_DIR}/src")
diff --git a/src/runtime/burn_bundle.cpp b/src/runtime/burn_bundle.cpp
new file mode 100644
index 0000000..0e17074
--- /dev/null
+++ b/src/runtime/burn_bundle.cpp
@@ -0,0 +1,316 @@
+#include "runtime/burn_bundle.h"
+
+#include "runtime/msi_package.h"
+
+#include <algorithm>
+#include <cstdint>
+#include <cstdlib>
+#include <cstring>
+#include <optional>
+#include <set>
+#include <vector>
+
+namespace playden::runtime {
+namespace {
+
+constexpr std::uint32_t kBurnMagic = 0x00f14300;
+constexpr std::size_t kMaxManifestBytes = 4u * 1024u * 1024u;
+constexpr std::size_t kMaxContainers = 64;
+
+std::uint32_t U32(std::string_view bytes, std::size_t at) {
+ const auto* p = reinterpret_cast<const unsigned char*>(bytes.data() + at);
+ return p[0] | (p[1] << 8) | (p[2] << 16) | (static_cast<std::uint32_t>(p[3]) << 24);
+}
+std::uint16_t U16(std::string_view bytes, std::size_t at) {
+ const auto* p = reinterpret_cast<const unsigned char*>(bytes.data() + at);
+ return static_cast<std::uint16_t>(p[0] | (p[1] << 8));
+}
+
+std::string Lower(std::string_view text) {
+ std::string out(text);
+ for (auto& value : out)
+ if (value >= 'A' && value <= 'Z') value = static_cast<char>(value - 'A' + 'a');
+ return out;
+}
+
+// A cabinet that starts at offset and is exactly size bytes long (its header says so).
+bool CabinetAt(std::string_view bundle, std::uint64_t offset, std::uint64_t size) {
+ return size >= 36 && offset <= bundle.size() && size <= bundle.size() - offset &&
+ bundle.compare(offset, 8, std::string_view("MSCF\0\0\0\0", 8)) == 0 && U32(bundle, offset + 8) == size;
+}
+
+// The bundle's containers in order: [0] the UX container, then the attached ones.
+std::optional<std::vector<std::string_view>> SectionContainers(std::string_view bundle) {
+ if (bundle.size() < 64 || bundle[0] != 'M' || bundle[1] != 'Z') return std::nullopt;
+ const auto pe = U32(bundle, 0x3c);
+ if (pe > bundle.size() || bundle.size() - pe < 24 || bundle.compare(pe, 4, std::string_view("PE\0\0", 4)) != 0)
+ return std::nullopt;
+ const unsigned sections = U16(bundle, pe + 6);
+ const unsigned optional_size = U16(bundle, pe + 20);
+ const std::uint64_t table = static_cast<std::uint64_t>(pe) + 24 + optional_size;
+ if (sections > 96 || table + sections * 40ull > bundle.size()) return std::nullopt;
+ for (unsigned index = 0; index < sections; ++index) {
+ const auto row = table + index * 40ull;
+ if (bundle.substr(row, 8) != std::string_view(".wixburn", 8)) continue;
+ const std::uint64_t raw = U32(bundle, row + 20), size = U32(bundle, row + 16);
+ if (raw > bundle.size() || size > bundle.size() - raw || size < 48) return std::nullopt;
+ const auto section = bundle.substr(raw, size);
+ if (U32(section, 0) != kBurnMagic || U32(section, 40) != 1) return std::nullopt; // 1: cabinet containers
+ const std::uint64_t stub = U32(section, 24), signature = U32(section, 32), signature_size = U32(section, 36);
+ const std::uint32_t count = U32(section, 44);
+ if (count == 0 || count > kMaxContainers || 48 + count * 4ull > section.size()) return std::nullopt;
+ std::vector<std::uint64_t> sizes;
+ for (std::uint32_t at = 0; at < count; ++at) sizes.push_back(U32(section, 48 + at * 4));
+ if (!CabinetAt(bundle, stub, sizes[0])) return std::nullopt;
+ std::vector<std::string_view> out{bundle.substr(stub, sizes[0])};
+ if (count == 1) return out;
+ // Attached containers follow the engine: after the original signature when the bundle was signed
+ // before they were attached (Microsoft's are), right after the UX container otherwise.
+ for (const std::uint64_t first : {signature ? signature + signature_size : 0, stub + sizes[0]}) {
+ if (!first) continue;
+ std::vector<std::string_view> attached;
+ std::uint64_t at = first;
+ for (std::uint32_t container = 1; container < count && CabinetAt(bundle, at, sizes[container]); ++container) {
+ attached.push_back(bundle.substr(at, sizes[container]));
+ at += sizes[container];
+ }
+ if (attached.size() == count - 1) {
+ out.insert(out.end(), attached.begin(), attached.end());
+ return out;
+ }
+ }
+ return std::nullopt;
+ }
+ return std::nullopt;
+}
+
+// Members of a cabinet by name; false when one is missing or the cabinet can't be read.
+bool Members(std::string_view cabinet, const std::set<std::string>& names, std::map<std::string, std::string>* out,
+ std::string* reason) {
+ std::string why;
+ const bool read = ExtractCabinet(cabinet, [&](const std::string& name, std::string_view data) {
+ if (names.count(name) && !out->count(name)) (*out)[name] = std::string(data);
+ return out->size() < names.size(); // stop once everything is in
+ }, &why);
+ if (out->size() == names.size()) return true;
+ *reason = read ? "a file is missing from the bundle's container" : "the bundle's container could not be read: " + why;
+ return false;
+}
+
+// Without a usable .wixburn section: the cabinets the bundle holds, in file order. The UX container is
+// the first whose file "0" is a Burn manifest.
+std::vector<std::string_view> ScannedContainers(std::string_view bundle) {
+ std::vector<std::string_view> found;
+ for (std::size_t at = bundle.find(std::string_view("MSCF\0\0\0\0", 8)); at != std::string_view::npos;) {
+ const std::uint64_t size = at + 12 <= bundle.size() ? U32(bundle, at + 8) : 0;
+ if (CabinetAt(bundle, at, size)) {
+ found.push_back(bundle.substr(at, size));
+ at += size;
+ } else {
+ ++at;
+ }
+ at = bundle.find(std::string_view("MSCF\0\0\0\0", 8), at);
+ }
+ for (std::size_t index = 0; index < found.size(); ++index) {
+ std::map<std::string, std::string> manifest;
+ std::string ignored;
+ if (Members(found[index], {"0"}, &manifest, &ignored) && manifest["0"].find("<BurnManifest") != std::string::npos) {
+ found.erase(found.begin(), found.begin() + static_cast<std::ptrdiff_t>(index));
+ return found;
+ }
+ }
+ return {};
+}
+
+// ---- The manifest: just enough XML for its elements and their attributes.
+
+struct Element {
+ std::string name; // "Payload", "/MsiPackage"
+ std::map<std::string, std::string> attributes;
+ bool closed = false; // <name ... />
+};
+
+std::string Unescape(std::string_view text) {
+ static const std::pair<std::string_view, char> kEntities[] = {
+ {"&quot;", '"'}, {"&apos;", '\''}, {"&lt;", '<'}, {"&gt;", '>'}, {"&amp;", '&'}};
+ std::string out;
+ for (std::size_t at = 0; at < text.size();) {
+ bool replaced = false;
+ if (text[at] == '&')
+ for (const auto& [entity, value] : kEntities)
+ if (text.compare(at, entity.size(), entity) == 0) {
+ out += value;
+ at += entity.size();
+ replaced = true;
+ break;
+ }
+ if (!replaced) out += text[at++];
+ }
+ return out;
+}
+
+bool Space(char value) { return value == ' ' || value == '\t' || value == '\r' || value == '\n'; }
+
+// The manifest's elements in order; false when it isn't well formed enough to read.
+bool Elements(std::string_view xml, std::vector<Element>* out) {
+ for (std::size_t at = xml.find('<'); at != std::string_view::npos; at = xml.find('<', at)) {
+ ++at;
+ if (xml.compare(at, 3, "!--") == 0) {
+ const auto end = xml.find("-->", at);
+ if (end == std::string_view::npos) return false;
+ at = end + 3;
+ continue;
+ }
+ if (at < xml.size() && (xml[at] == '?' || xml[at] == '!')) {
+ const auto end = xml.find('>', at);
+ if (end == std::string_view::npos) return false;
+ at = end + 1;
+ continue;
+ }
+ Element element;
+ while (at < xml.size() && !Space(xml[at]) && xml[at] != '>' && xml[at] != '/') element.name += xml[at++];
+ if (at < xml.size() && xml[at] == '/' && element.name.empty()) {
+ element.name = "/";
+ for (++at; at < xml.size() && !Space(xml[at]) && xml[at] != '>';) element.name += xml[at++];
+ }
+ for (;;) {
+ while (at < xml.size() && Space(xml[at])) ++at;
+ if (at >= xml.size()) return false;
+ if (xml[at] == '>') { ++at; break; }
+ if (xml[at] == '/') {
+ element.closed = true;
+ ++at;
+ continue;
+ }
+ std::string key;
+ while (at < xml.size() && !Space(xml[at]) && xml[at] != '=' && xml[at] != '>' && xml[at] != '/') key += xml[at++];
+ while (at < xml.size() && Space(xml[at])) ++at;
+ if (key.empty() || at >= xml.size() || xml[at] != '=') return false;
+ ++at;
+ while (at < xml.size() && Space(xml[at])) ++at;
+ if (at >= xml.size() || (xml[at] != '"' && xml[at] != '\'')) return false;
+ const char quote = xml[at++];
+ const auto end = xml.find(quote, at);
+ if (end == std::string_view::npos) return false;
+ element.attributes[key] = Unescape(xml.substr(at, end - at));
+ at = end + 1;
+ }
+ if (element.name.empty()) return false;
+ out->push_back(std::move(element));
+ }
+ return true;
+}
+
+std::string Attribute(const Element& element, const char* name) {
+ const auto found = element.attributes.find(name);
+ return found == element.attributes.end() ? std::string() : found->second;
+}
+
+std::string FileName(const std::string& path) {
+ const auto slash = path.find_last_of("\\/");
+ return slash == std::string::npos ? path : path.substr(slash + 1);
+}
+
+} // namespace
+
+bool ReadBurnMsiPackage(std::string_view bundle, const std::string& package_id, BurnMsiPackage* package,
+ std::string* reason) {
+ *package = BurnMsiPackage{};
+ auto containers = SectionContainers(bundle);
+ if (!containers) containers = ScannedContainers(bundle);
+ if (containers->empty()) {
+ *reason = "not a setup bundle Playden can read";
+ return false;
+ }
+ std::map<std::string, std::string> ux;
+ if (!Members(containers->front(), {"0"}, &ux, reason)) return false;
+ std::string_view manifest = ux["0"];
+ if (manifest.size() > kMaxManifestBytes) {
+ *reason = "the bundle's manifest is too large";
+ return false;
+ }
+ if (manifest.substr(0, 3) == "\xEF\xBB\xBF") manifest.remove_prefix(3);
+ std::vector<Element> elements;
+ if (!Elements(manifest, &elements)) {
+ *reason = "the bundle's manifest could not be read";
+ return false;
+ }
+ std::map<std::string, const Element*> payloads;
+ std::map<std::string, std::size_t> attached; // container id → index in containers
+ std::vector<std::string> refs;
+ bool found = false, inside = false;
+ for (const auto& element : elements) {
+ if (element.name == "Payload") {
+ payloads[Attribute(element, "Id")] = &element;
+ } else if (element.name == "Container" && Attribute(element, "Attached") == "yes") {
+ const auto index = std::strtoul(Attribute(element, "AttachedIndex").c_str(), nullptr, 10);
+ if (index > 0) attached[Attribute(element, "Id")] = index;
+ } else if (element.name == "MsiPackage" && Attribute(element, "Id") == package_id) {
+ found = true;
+ inside = !element.closed;
+ } else if (element.name == "/MsiPackage") {
+ inside = false;
+ } else if (inside && element.name == "PayloadRef") {
+ refs.push_back(Attribute(element, "Id"));
+ }
+ }
+ if (!found || refs.empty()) {
+ *reason = "the bundle has no package " + package_id;
+ return false;
+ }
+ // The package's payloads, by container.
+ std::map<std::size_t, std::set<std::string>> wanted;
+ std::vector<std::pair<const Element*, std::size_t>> parts;
+ std::set<std::string> listed;
+ for (const auto& id : refs) {
+ if (!listed.insert(id).second) continue;
+ const auto payload = payloads.find(id);
+ if (payload == payloads.end()) {
+ *reason = "the bundle's manifest lists no payload " + id;
+ return false;
+ }
+ const auto container = attached.find(Attribute(*payload->second, "Container"));
+ const auto source = Attribute(*payload->second, "SourcePath");
+ if (Attribute(*payload->second, "Packaging") != "embedded" || container == attached.end() ||
+ container->second >= containers->size() || source.empty()) {
+ *reason = "the bundle doesn't carry the payload " + id;
+ return false;
+ }
+ wanted[container->second].insert(source);
+ parts.emplace_back(payload->second, container->second);
+ }
+ std::map<std::size_t, std::map<std::string, std::string>> members;
+ for (const auto& [index, names] : wanted)
+ if (!Members((*containers)[index], names, &members[index], reason)) return false;
+ for (const auto& [payload, index] : parts) {
+ auto& data = members[index][Attribute(*payload, "SourcePath")];
+ const auto name = FileName(Attribute(*payload, "FilePath"));
+ const auto size = Attribute(*payload, "FileSize");
+ if (size.empty() || std::strtoull(size.c_str(), nullptr, 10) != data.size()) {
+ *reason = "the bundle's copy of " + name + " is not the size its manifest lists";
+ return false;
+ }
+ if (name.empty() || name == "." || name == "..") {
+ *reason = "the bundle names a payload Playden can't use";
+ return false;
+ }
+ const auto lower = Lower(name);
+ if (lower.size() > 4 && lower.compare(lower.size() - 4, 4, ".msi") == 0) {
+ if (!package->msi_name.empty()) {
+ *reason = "the package " + package_id + " has more than one .msi";
+ return false;
+ }
+ package->msi_name = name;
+ package->msi = std::move(data);
+ } else {
+ package->beside[name] = std::move(data);
+ }
+ }
+ if (package->msi_name.empty()) {
+ *reason = "the package " + package_id + " has no .msi";
+ return false;
+ }
+ return true;
+}
+
+} // namespace playden::runtime
diff --git a/src/runtime/burn_bundle.h b/src/runtime/burn_bundle.h
new file mode 100644
index 0000000..592c140
--- /dev/null
+++ b/src/runtime/burn_bundle.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <map>
+#include <string>
+#include <string_view>
+
+namespace playden::runtime {
+
+// A WiX Burn bundle (a setup .exe such as Microsoft's VC_redist.x64.exe) read without running it: its
+// .wixburn section locates the UX container, whose file "0" is the bundle's manifest, and the attached
+// container that holds the packages. Both containers are cabinets (runtime/msi_package.h ExtractCabinet).
+
+// One Windows Installer package of the bundle, by its MsiPackage Id: the .msi and the bundle's other
+// payloads for it (its external cabinets), by the file name of their FilePath ("cab1.cab").
+struct BurnMsiPackage {
+ std::string msi_name; // the package's file name ("vc_runtimeAdditional_x64.msi")
+ std::string msi;
+ std::map<std::string, std::string> beside; // file name → bytes
+};
+
+// false with a reason when the bundle, its manifest or the package can't be read. Payloads are found
+// by the manifest's ids (SourcePath), never by position, and each must be the size the manifest lists.
+bool ReadBurnMsiPackage(std::string_view bundle, const std::string& package_id, BurnMsiPackage* package,
+ std::string* reason);
+
+} // namespace playden::runtime
diff --git a/tests/msi_fixture.h b/tests/msi_fixture.h
new file mode 100644
index 0000000..c071eda
--- /dev/null
+++ b/tests/msi_fixture.h
@@ -0,0 +1,437 @@
+#pragma once
+// Builders for the Windows Installer and cabinet tests: cabinets (stored or MSZIP), compound files and
+// minimal .msi packages, all made in memory. Shared by runtime_redist_test.cpp and the runtime host tests.
+#include <algorithm>
+#include <cstdint>
+#include <cstdlib>
+#include <map>
+#include <string>
+#include <utility>
+#include <vector>
+
+namespace playden::test::msi {
+
+inline void Put16(std::string* out, unsigned value) {
+ out->push_back(static_cast<char>(value & 0xff));
+ out->push_back(static_cast<char>((value >> 8) & 0xff));
+}
+
+inline void Put32(std::string* out, std::uint32_t value) {
+ Put16(out, value & 0xffff);
+ Put16(out, value >> 16);
+}
+
+// Deflate bits, least significant first; Huffman codes go in most significant bit first.
+struct Bits {
+ std::string bytes;
+ unsigned current = 0, count = 0;
+ void Put(unsigned value, unsigned width) {
+ for (unsigned bit = 0; bit < width; ++bit) Push((value >> bit) & 1);
+ }
+ void Code(unsigned value, unsigned width) {
+ for (unsigned bit = width; bit-- > 0;) Push((value >> bit) & 1);
+ }
+ void Push(unsigned bit) {
+ current |= bit << count;
+ if (++count == 8) Flush();
+ }
+ void Flush() {
+ if (count) bytes.push_back(static_cast<char>(current));
+ current = count = 0;
+ }
+};
+
+// A cabinet with one MSZIP folder of two blocks: a stored block "0123456789", then a fixed
+// Huffman block that copies those ten bytes back (length 10, distance 10) — only right when
+// the second block sees the first one's output. Members: "first" (block 1), "second" (block 2),
+// "empty" (no bytes).
+inline std::string TwoBlockCabinet() {
+ std::string block1 = "CK";
+ {
+ Bits bits;
+ bits.Put(1, 1); // final
+ bits.Put(0, 2); // stored
+ bits.Flush();
+ block1 += bits.bytes;
+ Put16(&block1, 10);
+ Put16(&block1, 0xffff ^ 10);
+ block1 += "0123456789";
+ }
+ std::string block2 = "CK";
+ {
+ Bits bits;
+ bits.Put(1, 1); // final
+ bits.Put(1, 2); // fixed Huffman
+ bits.Code(264 - 256, 7); // length 10
+ bits.Code(6, 5); // distance 9-12
+ bits.Put(1, 2); // + 1 = 10
+ bits.Code(0, 7); // end of block
+ bits.Flush();
+ block2 += bits.bytes;
+ }
+ struct Member { const char* name; std::uint32_t size, offset; };
+ const Member members[] = {{"first", 10, 0}, {"second", 10, 10}, {"empty", 0, 20}};
+ std::string files;
+ for (const auto& member : members) {
+ Put32(&files, member.size);
+ Put32(&files, member.offset);
+ Put16(&files, 0); // folder
+ Put16(&files, 0);
+ Put16(&files, 0);
+ Put16(&files, 0x20);
+ files += member.name;
+ files.push_back('\0');
+ }
+ const std::uint32_t header = 36, folder = 8;
+ const std::uint32_t files_at = header + folder;
+ const std::uint32_t data_at = files_at + static_cast<std::uint32_t>(files.size());
+ std::string data;
+ for (const auto* block : {&block1, &block2}) {
+ Put32(&data, 0);
+ Put16(&data, static_cast<unsigned>(block->size()));
+ Put16(&data, 10);
+ data += *block;
+ }
+ std::string cabinet = "MSCF";
+ Put32(&cabinet, 0);
+ Put32(&cabinet, data_at + static_cast<std::uint32_t>(data.size()));
+ Put32(&cabinet, 0);
+ Put32(&cabinet, files_at);
+ Put32(&cabinet, 0);
+ cabinet.push_back(3);
+ cabinet.push_back(1);
+ Put16(&cabinet, 1); // folders
+ Put16(&cabinet, 3); // files
+ Put16(&cabinet, 0); // flags
+ Put16(&cabinet, 0);
+ Put16(&cabinet, 0);
+ Put32(&cabinet, data_at);
+ Put16(&cabinet, 2); // blocks
+ Put16(&cabinet, 1); // MSZIP
+ return cabinet + files + data;
+}
+
+// ---------------------------------------------------------------------------------------------
+// A minimal Windows Installer package: a compound file (512-byte sectors, every stream in
+// ordinary sectors) holding the string pool, _Columns and a few tables.
+
+inline std::vector<std::uint16_t> EncodeStreamName(const std::string& name) {
+ static const std::string kAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._";
+ std::vector<std::uint16_t> out;
+ std::size_t at = 0;
+ if (!name.empty() && name[0] == '!') {
+ out.push_back(0x4840);
+ at = 1;
+ }
+ while (at < name.size()) {
+ const auto first = kAlphabet.find(name[at]);
+ if (first == std::string::npos) {
+ out.push_back(static_cast<unsigned char>(name[at++]));
+ continue;
+ }
+ const auto second = at + 1 < name.size() ? kAlphabet.find(name[at + 1]) : std::string::npos;
+ if (second != std::string::npos) {
+ out.push_back(static_cast<std::uint16_t>(0x3800 + first + (second << 6)));
+ at += 2;
+ } else {
+ out.push_back(static_cast<std::uint16_t>(0x4800 + first));
+ ++at;
+ }
+ }
+ return out;
+}
+
+inline std::string CompoundFile(const std::vector<std::pair<std::string, std::string>>& streams) {
+ constexpr std::uint32_t kSector = 512, kEnd = 0xfffffffe, kFree = 0xffffffff;
+ std::vector<std::uint32_t> stream_sectors;
+ std::uint32_t data_sectors = 0;
+ for (const auto& stream : streams) {
+ const auto count = static_cast<std::uint32_t>((stream.second.size() + kSector - 1) / kSector);
+ stream_sectors.push_back(count);
+ data_sectors += count;
+ }
+ const auto directory_sectors = static_cast<std::uint32_t>(((streams.size() + 1) * 128 + kSector - 1) / kSector);
+ std::uint32_t fat_sectors = 1;
+ while (fat_sectors * (kSector / 4) < fat_sectors + directory_sectors + data_sectors) ++fat_sectors;
+ const std::uint32_t total = fat_sectors + directory_sectors + data_sectors;
+ std::vector<std::uint32_t> fat(fat_sectors * (kSector / 4), kFree);
+ for (std::uint32_t index = 0; index < fat_sectors; ++index) fat[index] = 0xfffffffd;
+ const auto chain = [&](std::uint32_t first, std::uint32_t count) {
+ for (std::uint32_t index = 0; index < count; ++index) fat[first + index] = index + 1 < count ? first + index + 1 : kEnd;
+ };
+ chain(fat_sectors, directory_sectors);
+ std::string directory;
+ const auto entry = [&](const std::vector<std::uint16_t>& name, int type, std::uint32_t start, std::uint32_t size) {
+ std::string out;
+ for (const auto unit : name) Put16(&out, unit);
+ out.resize(64, '\0');
+ Put16(&out, static_cast<unsigned>((name.size() + 1) * 2));
+ out.push_back(static_cast<char>(type));
+ out.push_back(1);
+ Put32(&out, 0xffffffff);
+ Put32(&out, 0xffffffff);
+ Put32(&out, type == 5 ? 1 : 0xffffffff);
+ out.resize(0x74, '\0');
+ Put32(&out, start);
+ Put32(&out, size);
+ Put32(&out, 0);
+ directory += out;
+ };
+ entry({'R', 'o', 'o', 't', ' ', 'E', 'n', 't', 'r', 'y'}, 5, kEnd, 0);
+ std::uint32_t next = fat_sectors + directory_sectors;
+ std::string body;
+ for (std::size_t index = 0; index < streams.size(); ++index) {
+ const auto& [name, data] = streams[index];
+ entry(EncodeStreamName(name), 2, stream_sectors[index] ? next : kEnd, static_cast<std::uint32_t>(data.size()));
+ chain(next, stream_sectors[index]);
+ next += stream_sectors[index];
+ std::string padded = data;
+ padded.resize(stream_sectors[index] * kSector, '\0');
+ body += padded;
+ }
+ directory.resize(directory_sectors * kSector, '\0');
+ std::string header = "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1";
+ header.resize(0x18, '\0');
+ Put16(&header, 0x3e);
+ Put16(&header, 3);
+ Put16(&header, 0xfffe);
+ Put16(&header, 9);
+ Put16(&header, 6);
+ header.resize(0x2c, '\0');
+ Put32(&header, fat_sectors);
+ Put32(&header, fat_sectors); // first directory sector
+ Put32(&header, 0);
+ Put32(&header, 0); // mini stream cutoff: nothing in the mini stream
+ Put32(&header, kEnd);
+ Put32(&header, 0);
+ Put32(&header, kEnd);
+ Put32(&header, 0);
+ for (std::uint32_t index = 0; index < 109; ++index) Put32(&header, index < fat_sectors ? index : kFree);
+ std::string fat_bytes;
+ for (const auto value : fat) Put32(&fat_bytes, value);
+ (void)total;
+ return header + fat_bytes + directory + body;
+}
+
+struct Column {
+ std::string name;
+ char kind; // 's' string, 'i' 2-byte integer, 'l' 4-byte integer
+};
+
+struct TableSpec {
+ std::string name;
+ std::vector<Column> columns;
+ std::vector<std::vector<std::string>> rows; // "" is null for strings; integers as text, "" null
+};
+
+inline std::string Package(const std::vector<TableSpec>& tables, const std::vector<std::pair<std::string, std::string>>& extra) {
+ std::vector<std::string> strings{""};
+ std::map<std::string, std::uint16_t> ids;
+ const auto id = [&](const std::string& text) -> std::uint16_t {
+ if (text.empty()) return 0;
+ const auto found = ids.find(text);
+ if (found != ids.end()) return found->second;
+ strings.push_back(text);
+ return ids[text] = static_cast<std::uint16_t>(strings.size() - 1);
+ };
+ std::vector<std::pair<std::string, std::string>> streams;
+ std::vector<std::string> column_table, column_name;
+ std::vector<unsigned> column_number, column_type;
+ for (const auto& table : tables) {
+ for (std::size_t index = 0; index < table.columns.size(); ++index) {
+ column_table.push_back(table.name);
+ column_name.push_back(table.columns[index].name);
+ column_number.push_back(static_cast<unsigned>(index + 1));
+ const char kind = table.columns[index].kind;
+ column_type.push_back(kind == 's' ? 0x1dff : kind == 'i' ? 0x1502 : 0x1104);
+ }
+ std::string data;
+ for (std::size_t column = 0; column < table.columns.size(); ++column) {
+ for (const auto& row : table.rows) {
+ const auto& cell = row[column];
+ const char kind = table.columns[column].kind;
+ if (kind == 's') Put16(&data, id(cell));
+ else if (kind == 'i') Put16(&data, cell.empty() ? 0 : static_cast<unsigned>(std::atoi(cell.c_str()) + 0x8000));
+ else Put32(&data, cell.empty() ? 0 : static_cast<std::uint32_t>(std::atoll(cell.c_str()) + 0x80000000ll));
+ }
+ }
+ streams.emplace_back("!" + table.name, data);
+ }
+ std::string columns;
+ for (const auto& name : column_table) Put16(&columns, id(name));
+ for (const auto number : column_number) Put16(&columns, number + 0x8000);
+ for (const auto& name : column_name) Put16(&columns, id(name));
+ for (const auto type : column_type) Put16(&columns, type | 0x8000);
+ streams.emplace_back("!_Columns", columns);
+ std::string pool, pool_data;
+ Put32(&pool, 1252);
+ for (std::size_t index = 1; index < strings.size(); ++index) {
+ Put16(&pool, static_cast<unsigned>(strings[index].size()));
+ Put16(&pool, 1);
+ pool_data += strings[index];
+ }
+ streams.emplace_back("!_StringPool", pool);
+ streams.emplace_back("!_StringData", pool_data);
+ for (const auto& stream : extra) streams.push_back(stream);
+ return CompoundFile(streams);
+}
+
+// A cabinet with one stored (uncompressed) folder holding members in order.
+inline std::string StoredCabinet(const std::vector<std::pair<std::string, std::string>>& members) {
+ std::string files, content;
+ for (const auto& [name, data] : members) {
+ Put32(&files, static_cast<std::uint32_t>(data.size()));
+ Put32(&files, static_cast<std::uint32_t>(content.size()));
+ Put16(&files, 0); // folder
+ Put16(&files, 0);
+ Put16(&files, 0);
+ Put16(&files, 0x20);
+ files += name;
+ files.push_back('\0');
+ content += data;
+ }
+ std::vector<std::string> chunks;
+ for (std::size_t at = 0; at < content.size(); at += 32768) chunks.push_back(content.substr(at, 32768));
+ if (chunks.empty()) chunks.emplace_back();
+ std::string data;
+ for (const auto& chunk : chunks) {
+ Put32(&data, 0);
+ Put16(&data, static_cast<unsigned>(chunk.size()));
+ Put16(&data, static_cast<unsigned>(chunk.size()));
+ data += chunk;
+ }
+ const auto blocks = static_cast<unsigned>(chunks.size());
+ const std::uint32_t files_at = 36 + 8;
+ const std::uint32_t data_at = files_at + static_cast<std::uint32_t>(files.size());
+ std::string cabinet = "MSCF";
+ Put32(&cabinet, 0);
+ Put32(&cabinet, data_at + static_cast<std::uint32_t>(data.size()));
+ Put32(&cabinet, 0);
+ Put32(&cabinet, files_at);
+ Put32(&cabinet, 0);
+ cabinet.push_back(3);
+ cabinet.push_back(1);
+ Put16(&cabinet, 1); // folders
+ Put16(&cabinet, static_cast<unsigned>(members.size()));
+ Put16(&cabinet, 0); // flags
+ Put16(&cabinet, 0);
+ Put16(&cabinet, 0);
+ Put32(&cabinet, data_at);
+ Put16(&cabinet, blocks);
+ Put16(&cabinet, 0); // stored
+ return cabinet + files + data;
+}
+
+// A WiX Burn bundle: a small PE whose .wixburn section lists the UX container (file "0" is the manifest)
+// and one attached container. signed_form: the attached container follows an original signature, as in
+// Microsoft's bundles; otherwise it follows the UX container. with_section false: no .wixburn section.
+inline std::string BurnBundle(const std::string& manifest, const std::string& attached, bool signed_form = true,
+ bool with_section = true) {
+ const auto ux = StoredCabinet({{"0", manifest}, {"u0", "ux-dll"}});
+ std::string stub(0x400, '\0');
+ stub[0] = 'M'; stub[1] = 'Z'; stub[0x3c] = 0x40;
+ stub[0x40] = 'P'; stub[0x41] = 'E';
+ stub[0x44] = 0x4c; stub[0x45] = 0x01; // i386
+ stub[0x46] = 1; // one section
+ stub[0x54] = static_cast<char>(0xe0); // optional header size
+ stub[0x58] = 0x0b; stub[0x59] = 0x01; // PE32
+ const std::size_t row = 0x58 + 0xe0;
+ stub.replace(row, 8, with_section ? ".wixburn" : ".rsrc\0\0\0", 8);
+ std::string field;
+ Put32(&field, 0x200); Put32(&field, 0x1000); // virtual size, address
+ Put32(&field, 0x200); Put32(&field, 0x200); // raw size, raw offset
+ stub.replace(row + 8, field.size(), field);
+ const std::uint32_t signature = signed_form ? static_cast<std::uint32_t>(0x400 + ux.size() + 4) : 0;
+ const std::string fake_signature(24, '\x5a');
+ std::string section;
+ Put32(&section, 0x00f14300);
+ Put32(&section, 2);
+ section += std::string(16, '\x11'); // bundle id
+ Put32(&section, 0x400); // stub size
+ Put32(&section, 0);
+ Put32(&section, signature);
+ Put32(&section, signed_form ? static_cast<std::uint32_t>(fake_signature.size()) : 0);
+ Put32(&section, 1); // cabinet containers
+ Put32(&section, 2);
+ Put32(&section, static_cast<std::uint32_t>(ux.size()));
+ Put32(&section, static_cast<std::uint32_t>(attached.size()));
+ stub.replace(0x200, section.size(), section);
+ std::string bundle = stub + ux;
+ if (signed_form) bundle += std::string(4, '\0') + fake_signature;
+ return bundle + attached + std::string(40, '\x77'); // the signature Microsoft adds at the end
+}
+
+// Like Microsoft's vc_runtimeAdditional package for one architecture: MFC files in the system folder
+// (System64Folder for x64, SystemFolder for x86) taken from an external cab1.cab, a C runtime file that
+// must not be installed, and a registry value. MfcCabinet is its cab1.cab.
+inline std::string MfcCabinet(bool x64) {
+ const std::string suffix = x64 ? "_amd64" : "_x86";
+ return StoredCabinet({{"mfc140u.dll" + suffix, x64 ? "MFC64U" : "MFC32U"},
+ {"mfcm140.dll" + suffix, x64 ? "MFCM64" : "MFCM32"},
+ {"msvcp140.dll" + suffix, "MICROSOFT-CRT"}});
+}
+inline std::string MfcPackage(bool x64) {
+ const std::string suffix = x64 ? "_amd64" : "_x86";
+ const std::string system = x64 ? "System64Folder" : "SystemFolder";
+ std::vector<TableSpec> tables;
+ tables.push_back({"Property", {{"Property", 's'}, {"Value", 's'}},
+ {{"ProductCode", "{86AB2CC9-08BD-4643-B0F9-F82D006D72FF}"},
+ {"ProductName", "Microsoft Visual C++ 2022 Additional Runtime"}, {"ProductVersion", "14.44.35211"}}});
+ tables.push_back({"Directory", {{"Directory", 's'}, {"Directory_Parent", 's'}, {"DefaultDir", 's'}},
+ {{"TARGETDIR", "", "SourceDir"}, {system, "TARGETDIR", "."}}});
+ tables.push_back({"Feature", {{"Feature", 's'}, {"Feature_Parent", 's'}, {"Level", 'i'}}, {{"F_Main", "", "1"}}});
+ tables.push_back({"Condition", {{"Feature_", 's'}, {"Level", 'i'}, {"Condition", 's'}}, {}});
+ tables.push_back({"Component",
+ {{"Component", 's'}, {"ComponentId", 's'}, {"Directory_", 's'}, {"Attributes", 'i'}, {"Condition", 's'}, {"KeyPath", 's'}},
+ {{"C_MFC", "{A}", system, x64 ? "256" : "0", "", ""}, {"C_CRT", "{B}", system, x64 ? "256" : "0", "", ""}}});
+ tables.push_back({"FeatureComponents", {{"Feature_", 's'}, {"Component_", 's'}}, {{"F_Main", "C_MFC"}, {"F_Main", "C_CRT"}}});
+ tables.push_back({"File",
+ {{"File", 's'}, {"Component_", 's'}, {"FileName", 's'}, {"FileSize", 'l'}, {"Version", 's'},
+ {"Language", 's'}, {"Attributes", 'i'}, {"Sequence", 'i'}},
+ {{"mfc140u.dll" + suffix, "C_MFC", "mfc140u.dll", "6", "", "", "512", "1"},
+ {"mfcm140.dll" + suffix, "C_MFC", "mfcm140.dll", "6", "", "", "512", "2"},
+ {"msvcp140.dll" + suffix, "C_CRT", "msvcp140.dll", "13", "", "", "512", "3"}}});
+ tables.push_back({"Media", {{"DiskId", 'i'}, {"LastSequence", 'i'}, {"DiskPrompt", 's'}, {"Cabinet", 's'}},
+ {{"1", "3", "", "cab1.cab"}}});
+ tables.push_back({"Registry",
+ {{"Registry", 's'}, {"Root", 'i'}, {"Key", 's'}, {"Name", 's'}, {"Value", 's'}, {"Component_", 's'}},
+ {{"r1", "2", "SOFTWARE\\Microsoft\\DevDiv\\VC\\Servicing\\14.0\\RuntimeAdditional", "Install", "#1", "C_MFC"}}});
+ tables.push_back({"Environment", {{"Environment", 's'}, {"Name", 's'}, {"Value", 's'}, {"Component_", 's'}}, {}});
+ tables.push_back({"CustomAction", {{"Action", 's'}, {"Type", 'i'}, {"Source", 's'}, {"Target", 's'}}, {}});
+ return Package(tables, {});
+}
+
+// A bundle like Microsoft's VC_redist.<arch>.exe: a Minimum package and the Additional one (MFC), listed
+// out of order in the attached container, with an InstallCondition that holds an escaped quote.
+inline std::string MfcBundle(bool x64, bool signed_form = true, bool with_section = true) {
+ const std::string arch = x64 ? "x64" : "x86", folder = x64 ? "amd64" : "x86";
+ const auto msi = MfcPackage(x64), cab = MfcCabinet(x64);
+ const std::string minimum = "minimum-msi", minimum_cab = StoredCabinet({{"vcruntime140.dll", "CRT"}});
+ const auto payload = [](const std::string& id, const std::string& path, std::size_t size, const std::string& source) {
+ return "<Payload Id=\"" + id + "\" FilePath=\"" + path + "\" FileSize=\"" + std::to_string(size) +
+ "\" Hash=\"00\" Packaging=\"embedded\" SourcePath=\"" + source + "\" Container=\"WixAttachedContainer\" />\r\n";
+ };
+ std::string manifest = "\xEF\xBB\xBF<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
+ "<BurnManifest xmlns=\"http://schemas.microsoft.com/wix/2008/Burn\">\r\n"
+ "<!-- <MsiPackage Id=\"vcRuntimeAdditional_" + arch + "\"> in a comment is not read -->\r\n"
+ "<UX><Payload Id=\"wixstdba\" FilePath=\"wixstdba.dll\" FileSize=\"6\" SourcePath=\"u0\" /></UX>\r\n"
+ "<Container Id=\"WixAttachedContainer\" FileSize=\"1\" Hash=\"00\" FilePath=\"VC_redist." + arch +
+ ".exe\" AttachedIndex=\"1\" Attached=\"yes\" Primary=\"yes\" />\r\n" +
+ payload("vcRuntimeMinimum_" + arch, "packages\\vcRuntimeMinimum_" + folder + "\\vc_runtimeMinimum_" + arch + ".msi",
+ minimum.size(), "a0") +
+ payload("vcRuntimeAdditional_" + arch, "packages\\vcRuntimeAdditional_" + folder + "\\vc_runtimeAdditional_" + arch + ".msi",
+ msi.size(), "a3") +
+ payload("cabMinimum", "packages\\vcRuntimeMinimum_" + folder + "\\cab1.cab", minimum_cab.size(), "a2") +
+ payload("cabAdditional", "packages\\vcRuntimeAdditional_" + folder + "\\cab1.cab", cab.size(), "a1") +
+ "<Chain>\r\n"
+ "<MsiPackage Id=\"vcRuntimeMinimum_" + arch + "\" InstallCondition=\"(VersionNT64) AND NOT (Arm64_Check = &quot;ARM64&quot;)\">"
+ "<PayloadRef Id=\"vcRuntimeMinimum_" + arch + "\" /><PayloadRef Id=\"cabMinimum\" /></MsiPackage>\r\n"
+ "<MsiPackage Id=\"vcRuntimeAdditional_" + arch + "\" InstallCondition=\"a &gt; b\" Version=\"14.44.35211\">"
+ "<MsiProperty Id=\"ARPSYSTEMCOMPONENT\" Value=\"1\" />"
+ "<PayloadRef Id=\"vcRuntimeAdditional_" + arch + "\" /><PayloadRef Id=\"cabAdditional\" /></MsiPackage>\r\n"
+ "</Chain></BurnManifest>\r\n";
+ const auto attached = StoredCabinet({{"a0", minimum}, {"a1", cab}, {"a2", minimum_cab}, {"a3", msi}});
+ return BurnBundle(manifest, attached, signed_form, with_section);
+}
+
+} // namespace playden::test::msi
diff --git a/tests/runtime_redist_test.cpp b/tests/runtime_redist_test.cpp
index fc71b68..cbb393b 100644
--- a/tests/runtime_redist_test.cpp
+++ b/tests/runtime_redist_test.cpp
@@ -1,7 +1,10 @@
+#include "runtime/burn_bundle.h"
#include "runtime/install_script.h"
#include "runtime/msi_package.h"
#include "runtime/redist_install.h"
+#include "msi_fixture.h"
+
#include <algorithm>
#include <cstdio>
#include <cstdlib>
@@ -23,274 +26,10 @@
} while (0)
using namespace playden::runtime;
+using namespace playden::test::msi;
namespace {
-void Put16(std::string* out, unsigned value) {
- out->push_back(static_cast<char>(value & 0xff));
- out->push_back(static_cast<char>((value >> 8) & 0xff));
-}
-
-void Put32(std::string* out, std::uint32_t value) {
- Put16(out, value & 0xffff);
- Put16(out, value >> 16);
-}
-
-// Deflate bits, least significant first; Huffman codes go in most significant bit first.
-struct Bits {
- std::string bytes;
- unsigned current = 0, count = 0;
- void Put(unsigned value, unsigned width) {
- for (unsigned bit = 0; bit < width; ++bit) Push((value >> bit) & 1);
- }
- void Code(unsigned value, unsigned width) {
- for (unsigned bit = width; bit-- > 0;) Push((value >> bit) & 1);
- }
- void Push(unsigned bit) {
- current |= bit << count;
- if (++count == 8) Flush();
- }
- void Flush() {
- if (count) bytes.push_back(static_cast<char>(current));
- current = count = 0;
- }
-};
-
-// A cabinet with one MSZIP folder of two blocks: a stored block "0123456789", then a fixed
-// Huffman block that copies those ten bytes back (length 10, distance 10) — only right when
-// the second block sees the first one's output. Members: "first" (block 1), "second" (block 2),
-// "empty" (no bytes).
-std::string TwoBlockCabinet() {
- std::string block1 = "CK";
- {
- Bits bits;
- bits.Put(1, 1); // final
- bits.Put(0, 2); // stored
- bits.Flush();
- block1 += bits.bytes;
- Put16(&block1, 10);
- Put16(&block1, 0xffff ^ 10);
- block1 += "0123456789";
- }
- std::string block2 = "CK";
- {
- Bits bits;
- bits.Put(1, 1); // final
- bits.Put(1, 2); // fixed Huffman
- bits.Code(264 - 256, 7); // length 10
- bits.Code(6, 5); // distance 9-12
- bits.Put(1, 2); // + 1 = 10
- bits.Code(0, 7); // end of block
- bits.Flush();
- block2 += bits.bytes;
- }
- struct Member { const char* name; std::uint32_t size, offset; };
- const Member members[] = {{"first", 10, 0}, {"second", 10, 10}, {"empty", 0, 20}};
- std::string files;
- for (const auto& member : members) {
- Put32(&files, member.size);
- Put32(&files, member.offset);
- Put16(&files, 0); // folder
- Put16(&files, 0);
- Put16(&files, 0);
- Put16(&files, 0x20);
- files += member.name;
- files.push_back('\0');
- }
- const std::uint32_t header = 36, folder = 8;
- const std::uint32_t files_at = header + folder;
- const std::uint32_t data_at = files_at + static_cast<std::uint32_t>(files.size());
- std::string data;
- for (const auto* block : {&block1, &block2}) {
- Put32(&data, 0);
- Put16(&data, static_cast<unsigned>(block->size()));
- Put16(&data, 10);
- data += *block;
- }
- std::string cabinet = "MSCF";
- Put32(&cabinet, 0);
- Put32(&cabinet, data_at + static_cast<std::uint32_t>(data.size()));
- Put32(&cabinet, 0);
- Put32(&cabinet, files_at);
- Put32(&cabinet, 0);
- cabinet.push_back(3);
- cabinet.push_back(1);
- Put16(&cabinet, 1); // folders
- Put16(&cabinet, 3); // files
- Put16(&cabinet, 0); // flags
- Put16(&cabinet, 0);
- Put16(&cabinet, 0);
- Put32(&cabinet, data_at);
- Put16(&cabinet, 2); // blocks
- Put16(&cabinet, 1); // MSZIP
- return cabinet + files + data;
-}
-
-// ---------------------------------------------------------------------------------------------
-// A minimal Windows Installer package: a compound file (512-byte sectors, every stream in
-// ordinary sectors) holding the string pool, _Columns and a few tables.
-
-std::vector<std::uint16_t> EncodeStreamName(const std::string& name) {
- static const std::string kAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._";
- std::vector<std::uint16_t> out;
- std::size_t at = 0;
- if (!name.empty() && name[0] == '!') {
- out.push_back(0x4840);
- at = 1;
- }
- while (at < name.size()) {
- const auto first = kAlphabet.find(name[at]);
- if (first == std::string::npos) {
- out.push_back(static_cast<unsigned char>(name[at++]));
- continue;
- }
- const auto second = at + 1 < name.size() ? kAlphabet.find(name[at + 1]) : std::string::npos;
- if (second != std::string::npos) {
- out.push_back(static_cast<std::uint16_t>(0x3800 + first + (second << 6)));
- at += 2;
- } else {
- out.push_back(static_cast<std::uint16_t>(0x4800 + first));
- ++at;
- }
- }
- return out;
-}
-
-std::string CompoundFile(const std::vector<std::pair<std::string, std::string>>& streams) {
- constexpr std::uint32_t kSector = 512, kEnd = 0xfffffffe, kFree = 0xffffffff;
- std::vector<std::uint32_t> stream_sectors;
- std::uint32_t data_sectors = 0;
- for (const auto& stream : streams) {
- const auto count = static_cast<std::uint32_t>((stream.second.size() + kSector - 1) / kSector);
- stream_sectors.push_back(count);
- data_sectors += count;
- }
- const auto directory_sectors = static_cast<std::uint32_t>(((streams.size() + 1) * 128 + kSector - 1) / kSector);
- std::uint32_t fat_sectors = 1;
- while (fat_sectors * (kSector / 4) < fat_sectors + directory_sectors + data_sectors) ++fat_sectors;
- const std::uint32_t total = fat_sectors + directory_sectors + data_sectors;
- std::vector<std::uint32_t> fat(fat_sectors * (kSector / 4), kFree);
- for (std::uint32_t index = 0; index < fat_sectors; ++index) fat[index] = 0xfffffffd;
- const auto chain = [&](std::uint32_t first, std::uint32_t count) {
- for (std::uint32_t index = 0; index < count; ++index) fat[first + index] = index + 1 < count ? first + index + 1 : kEnd;
- };
- chain(fat_sectors, directory_sectors);
- std::string directory;
- const auto entry = [&](const std::vector<std::uint16_t>& name, int type, std::uint32_t start, std::uint32_t size) {
- std::string out;
- for (const auto unit : name) Put16(&out, unit);
- out.resize(64, '\0');
- Put16(&out, static_cast<unsigned>((name.size() + 1) * 2));
- out.push_back(static_cast<char>(type));
- out.push_back(1);
- Put32(&out, 0xffffffff);
- Put32(&out, 0xffffffff);
- Put32(&out, type == 5 ? 1 : 0xffffffff);
- out.resize(0x74, '\0');
- Put32(&out, start);
- Put32(&out, size);
- Put32(&out, 0);
- directory += out;
- };
- entry({'R', 'o', 'o', 't', ' ', 'E', 'n', 't', 'r', 'y'}, 5, kEnd, 0);
- std::uint32_t next = fat_sectors + directory_sectors;
- std::string body;
- for (std::size_t index = 0; index < streams.size(); ++index) {
- const auto& [name, data] = streams[index];
- entry(EncodeStreamName(name), 2, stream_sectors[index] ? next : kEnd, static_cast<std::uint32_t>(data.size()));
- chain(next, stream_sectors[index]);
- next += stream_sectors[index];
- std::string padded = data;
- padded.resize(stream_sectors[index] * kSector, '\0');
- body += padded;
- }
- directory.resize(directory_sectors * kSector, '\0');
- std::string header = "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1";
- header.resize(0x18, '\0');
- Put16(&header, 0x3e);
- Put16(&header, 3);
- Put16(&header, 0xfffe);
- Put16(&header, 9);
- Put16(&header, 6);
- header.resize(0x2c, '\0');
- Put32(&header, fat_sectors);
- Put32(&header, fat_sectors); // first directory sector
- Put32(&header, 0);
- Put32(&header, 0); // mini stream cutoff: nothing in the mini stream
- Put32(&header, kEnd);
- Put32(&header, 0);
- Put32(&header, kEnd);
- Put32(&header, 0);
- for (std::uint32_t index = 0; index < 109; ++index) Put32(&header, index < fat_sectors ? index : kFree);
- std::string fat_bytes;
- for (const auto value : fat) Put32(&fat_bytes, value);
- (void)total;
- return header + fat_bytes + directory + body;
-}
-
-struct Column {
- std::string name;
- char kind; // 's' string, 'i' 2-byte integer, 'l' 4-byte integer
-};
-
-struct TableSpec {
- std::string name;
- std::vector<Column> columns;
- std::vector<std::vector<std::string>> rows; // "" is null for strings; integers as text, "" null
-};
-
-std::string Package(const std::vector<TableSpec>& tables, const std::vector<std::pair<std::string, std::string>>& extra) {
- std::vector<std::string> strings{""};
- std::map<std::string, std::uint16_t> ids;
- const auto id = [&](const std::string& text) -> std::uint16_t {
- if (text.empty()) return 0;
- const auto found = ids.find(text);
- if (found != ids.end()) return found->second;
- strings.push_back(text);
- return ids[text] = static_cast<std::uint16_t>(strings.size() - 1);
- };
- std::vector<std::pair<std::string, std::string>> streams;
- std::vector<std::string> column_table, column_name;
- std::vector<unsigned> column_number, column_type;
- for (const auto& table : tables) {
- for (std::size_t index = 0; index < table.columns.size(); ++index) {
- column_table.push_back(table.name);
- column_name.push_back(table.columns[index].name);
- column_number.push_back(static_cast<unsigned>(index + 1));
- const char kind = table.columns[index].kind;
- column_type.push_back(kind == 's' ? 0x1dff : kind == 'i' ? 0x1502 : 0x1104);
- }
- std::string data;
- for (std::size_t column = 0; column < table.columns.size(); ++column) {
- for (const auto& row : table.rows) {
- const auto& cell = row[column];
- const char kind = table.columns[column].kind;
- if (kind == 's') Put16(&data, id(cell));
- else if (kind == 'i') Put16(&data, cell.empty() ? 0 : static_cast<unsigned>(std::atoi(cell.c_str()) + 0x8000));
- else Put32(&data, cell.empty() ? 0 : static_cast<std::uint32_t>(std::atoll(cell.c_str()) + 0x80000000ll));
- }
- }
- streams.emplace_back("!" + table.name, data);
- }
- std::string columns;
- for (const auto& name : column_table) Put16(&columns, id(name));
- for (const auto number : column_number) Put16(&columns, number + 0x8000);
- for (const auto& name : column_name) Put16(&columns, id(name));
- for (const auto type : column_type) Put16(&columns, type | 0x8000);
- streams.emplace_back("!_Columns", columns);
- std::string pool, pool_data;
- Put32(&pool, 1252);
- for (std::size_t index = 1; index < strings.size(); ++index) {
- Put16(&pool, static_cast<unsigned>(strings[index].size()));
- Put16(&pool, 1);
- pool_data += strings[index];
- }
- streams.emplace_back("!_StringPool", pool);
- streams.emplace_back("!_StringData", pool_data);
- for (const auto& stream : extra) streams.push_back(stream);
- return CompoundFile(streams);
-}
-
// A package like NVIDIA's legacy PhysX one: a 32-bit loader in Vendor\Common (added to PATH),
// an engine in Vendor\Engine\v1 found through a registry value, a 64-bit feature turned on by
// VersionNT64, a feature left off, and files in the two-block cabinet above.
@@ -461,6 +200,44 @@ void TestPlan() {
CHECK(plan.path_append.empty());
}
+void TestBurnBundle() {
+ std::string reason;
+ for (const bool x64 : {true, false})
+ for (const auto& [signed_form, with_section] : {std::pair{true, true}, std::pair{false, true}, std::pair{true, false}}) {
+ BurnMsiPackage package;
+ const auto id = std::string("vcRuntimeAdditional_") + (x64 ? "x64" : "x86");
+ const bool ok = ReadBurnMsiPackage(MfcBundle(x64, signed_form, with_section), id, &package, &reason);
+ if (!ok) std::fprintf(stderr, "bundle: %s\n", reason.c_str());
+ CHECK(ok);
+ CHECK(package.msi_name == std::string("vc_runtimeAdditional_") + (x64 ? "x64" : "x86") + ".msi");
+ CHECK(package.msi == MfcPackage(x64));
+ CHECK(package.beside.size() == 1 && package.beside.at("cab1.cab") == MfcCabinet(x64));
+ // The other package of the chain, by its own payloads.
+ CHECK(ReadBurnMsiPackage(MfcBundle(x64, signed_form, with_section), std::string("vcRuntimeMinimum_") + (x64 ? "x64" : "x86"),
+ &package, &reason));
+ CHECK(package.msi == "minimum-msi" && package.beside.at("cab1.cab") == StoredCabinet({{"vcruntime140.dll", "CRT"}}));
+ }
+ BurnMsiPackage package;
+ CHECK(!ReadBurnMsiPackage(MfcBundle(true), "vcRuntimeAdditional_arm64", &package, &reason));
+ CHECK(reason == "the bundle has no package vcRuntimeAdditional_arm64");
+ CHECK(!ReadBurnMsiPackage("MZ not a bundle", "x", &package, &reason));
+ CHECK(reason == "not a setup bundle Playden can read");
+ // A payload that isn't the size the manifest lists is refused.
+ auto bundle = MfcBundle(true);
+ const auto listed = "FileSize=\"" + std::to_string(MfcCabinet(true).size()) + "\"";
+ // The manifest is stored, so its text is in the bundle as is; same length keeps the cabinet valid.
+ auto wrong = std::to_string(MfcCabinet(true).size() + 1);
+ wrong.resize(std::to_string(MfcCabinet(true).size()).size(), '9');
+ const auto at = bundle.find(listed);
+ CHECK(at != std::string::npos);
+ bundle.replace(at, listed.size(), "FileSize=\"" + wrong + "\"");
+ CHECK(!ReadBurnMsiPackage(bundle, "vcRuntimeAdditional_x64", &package, &reason));
+ CHECK(reason == "the bundle's copy of cab1.cab is not the size its manifest lists");
+ // A cut bundle has no attached container to read.
+ const auto whole = MfcBundle(true);
+ CHECK(!ReadBurnMsiPackage(whole.substr(0, whole.size() - 200), "vcRuntimeAdditional_x64", &package, &reason));
+}
+
void TestPath() {
CHECK(ExtendPath("A;B", {"C:\\X"}, {}) == "C:\\X;A;B");
CHECK(ExtendPath("A;c:\\x\\", {"C:\\X"}, {"D"}) == "A;c:\\x\\;D");
@@ -623,6 +400,7 @@ int main() {
TestCabinet();
TestPlan();
TestPath();
+ TestBurnBundle();
TestScripts();
TestInstall();
TestPhysxPackage();