// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later // io/Zip: round trips, damage, unsafe names; argv[1]: an archive java.util.zip wrote // (with data descriptors, as Android's ZipOutputStream does), holding a.txt = "hello\n" // and dir/b.bin; argv[2]: where to write an archive for `unzip -t`. #include "io/Zip.hpp" #include #include #include using namespace sizzle::io; static int failures = 0; #define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++failures; } } while (0) int main(int argc, char **argv) { std::vector in = {{"project.szg", std::vector(5000, 'x')}, {"empty", {}}, {"samples/0.wav", {}}}; for (int i = 0; i < 3000; ++i) in[2].data.push_back((uint8_t)(i * 7919 >> 3)); auto bytes = zip::write(in); std::vector out; CHECK(zip::read(bytes.data(), bytes.size(), out)); CHECK(out.size() == 3 && out[0].name == "project.szg" && out[0].data == in[0].data && out[1].data.empty() && out[2].data == in[2].data); CHECK(bytes.size() < 5000); // deflated auto bad = bytes; bad[50] ^= 0xFF; // inside the first entry's data CHECK(!zip::read(bad.data(), bad.size(), out) && out.empty()); CHECK(!zip::read(bytes.data(), bytes.size() - 10, out)); CHECK(!zip::read(bytes.data(), bytes.size(), out, 1000)); // over the size budget CHECK(!zip::read(bytes.data(), bytes.size(), out, 1 << 30, 2)); CHECK(zip::safeName("songs/a b.szg") && zip::safeName("x")); CHECK(!zip::safeName("../x") && !zip::safeName("a/../../x") && !zip::safeName("/etc/x") && !zip::safeName("a\\b") && !zip::safeName("")); if (argc > 1) { std::ifstream f(argv[1], std::ios::binary); std::vector j((std::istreambuf_iterator(f)), {}); CHECK(zip::read(j.data(), j.size(), out)); bool a = false, b = false; for (auto &e : out) { if (e.name == "a.txt") a = std::string(e.data.begin(), e.data.end()) == "hello\n"; if (e.name == "dir/b.bin") b = e.data.size() == 4096; } CHECK(a && b); } if (argc > 2) { std::ofstream o(argv[2], std::ios::binary); o.write((const char *)bytes.data(), (std::streamsize)bytes.size()); } std::printf(failures ? "zip_test: %d failure(s)\n" : "zip_test: ok\n", failures); return failures ? 1 : 0; }