diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h index 596ad0ae92..63321357ff 100644 --- a/runtime/src/shader_library.h +++ b/runtime/src/shader_library.h @@ -247,8 +247,21 @@ inline ZlibError inflate_codes(BitReader& in, std::vector& out, co const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); if (in.failed() || distance > out.size()) return ZlibError::data; if (out.size() > limit || length > limit - out.size()) return ZlibError::limit; - const std::size_t from = out.size() - distance; - for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); + const std::size_t start = out.size(); + out.resize(start + length); + auto* dest = out.data() + start; + if (distance == 1) { + std::fill_n(dest, length, dest[-1]); + } else { + // Overlapping matches repeat their first distance bytes. Each copy reads only completed output. + std::size_t copied = std::min(distance, length); + std::memcpy(dest, dest - distance, copied); + while (copied < length) { + const auto count = std::min(copied, length - copied); + std::memcpy(dest + copied, dest, count); + copied += count; + } + } } } @@ -351,9 +364,16 @@ inline bool zlib_inflate(std::span in, std::vector in.size() || in.size() - at < 4) return fail(ZlibError::data); std::uint32_t a = 1, b = 0; - for (const std::uint8_t x : out) { - a = (a + x) % 65521; - b = (b + a) % 65521; + // At most 5,552 bytes of 0xff fit in both 32-bit accumulators between reductions. + for (std::span tail(out); !tail.empty();) { + const auto block = tail.first(std::min(tail.size(), 5552)); + for (const auto x : block) { + a += x; + b += a; + } + a %= 65521; + b %= 65521; + tail = tail.subspan(block.size()); } const std::uint32_t expected = (std::uint32_t(in[at]) << 24) | (std::uint32_t(in[at + 1]) << 16) | (std::uint32_t(in[at + 2]) << 8) | in[at + 3]; diff --git a/runtime/test/shader_library_test.cpp b/runtime/test/shader_library_test.cpp index 55105d987f..053bff5767 100644 --- a/runtime/test/shader_library_test.cpp +++ b/runtime/test/shader_library_test.cpp @@ -562,6 +562,22 @@ int main() { CHECK(zlib_inflate(stored, out) && out.size() == 40); for (std::uint8_t i = 0; i < 40; ++i) CHECK(out[i] == i); CHECK(zlib_inflate(fixed, out) && out == bytes("hello hello hello leonida")); + { + // Python zlib.compress(b'\xff' * 5553 + b'abc' * 5553). Exercises overlapping + // matches with periods one and three across multiple Adler reduction boundaries. + const std::vector repeated = { + 0x78, 0x9c, 0xed, 0xc2, 0x41, 0x11, 0x00, 0x00, 0x08, 0x03, 0xa0, 0xac, + 0xd3, 0xfe, 0x19, 0x66, 0x0d, 0x1f, 0x70, 0xb4, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xbc, 0x90, 0x59, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, + 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5, 0x3c, 0x7b, 0xa2, 0x71, + 0x87, 0x48, + }; + std::vector expected(5553, 0xff); + for (int i = 0; i < 5553; ++i) expected.insert(expected.end(), {'a', 'b', 'c'}); + CHECK(zlib_inflate(repeated, out, expected.size()) && out == expected); + ZlibError why{}; + CHECK(!zlib_inflate(repeated, out, expected.size() - 1, &why) && why == ZlibError::limit); + } CHECK(zlib_inflate(dynamic, out) && out == skewed(600)); CHECK(sha256_hex(out) == "9cd6848f765bff2a9f42a8b42f6164042dad67d3e9f82e0044ddc7d1ecdeebb2"); auto broken = dynamic;